Example #1
0
    def generate_mesh(self, latent_vector):
        assert latent_vector.size(0) == 1, "input should have batch size 1!"
        import pymesh
        input_points = [
            self.template[i].get_regular_points(self.nb_pts_in_primitive,
                                                latent_vector.device)
            for i in range(self.opt.nb_primitives)
        ]
        input_points = [input_points[i] for i in range(self.opt.nb_primitives)]

        # Deform each patch
        output_points = [
            self.decoder[i](input_points[i],
                            latent_vector.unsqueeze(2)).squeeze()
            for i in range(0, self.opt.nb_primitives)
        ]

        output_meshes = [
            pymesh.form_mesh(vertices=output_points[i].transpose(
                1, 0).contiguous().cpu().numpy(),
                             faces=self.template[i].mesh.faces)
            for i in range(self.opt.nb_primitives)
        ]

        # Deform return the deformed pointcloud
        mesh = pymesh.merge_meshes(output_meshes)

        return mesh
Example #2
0
def check_for_self_intersections(opts, mesh, points, points_int):

    si_points = set()

    count = 0
    for i in range(len(points)):
        for j in range(len(points)):
            if i == j:
                continue
            pt0 = points[i]
            pt1 = points[(i + 1) % len(points)]
            pt2 = points[j]
            pt3 = points[(j + 1) % len(points)]
            if closed_segment_intersect(pt0, pt1, pt2, pt3):
                if i < j:
                    si_points.add("%d-%d" % (i, j))
                else:
                    si_points.add("%d-%d" % (j, i))

    si_points = list(si_points)
    dots = []
    for pt in si_points:
        p0, p1 = pt.split('-')
        p_xyz = mesh.vertices[int(p0)]
        dots.append(pymesh.generate_icosphere(1, p_xyz))

    if opts['debug']:
        dots.append(mesh)
        mesh = pymesh.merge_meshes(dots)
        save_mesh("si", mesh)

    return len(si_points)
Example #3
0
def carve_mesh(target_mesh, block, N, batch_size, out_name,
        initial_N=0, save_intermediate=True, debug=False):
    name, ext = os.path.splitext(out_name);
    tree = pymesh.AABBTree();
    tree.load_mesh(target_mesh);

    dodecahedron = pymesh.generate_dodecahedron(1.0, np.zeros(3));
    vertices = dodecahedron.vertices;
    faces = dodecahedron.faces;

    for i in range(initial_N, N, batch_size):
        pts = block.vertices;
        squared_dist, face_indices, closest_pts = \
                tree.look_up_with_closest_points(pts);
        n = np.min([batch_size, N-i, len(pts)]);
        indices = np.argsort(squared_dist)[::-1][:n];
        radii = np.sqrt(squared_dist[indices]);
        centers = pts[indices, :];
        to_remove = [pymesh.form_mesh(
            np.dot(pymesh.Quaternion(numpy.random.rand(4)).to_matrix(), vertices.T * r).T + p, faces)
            for r,p in zip(radii, centers)];
        to_remove = pymesh.merge_meshes(to_remove);
        if debug:
            pymesh.save_mesh("deubg_block.msh", block);
            pymesh.save_mesh("deubg_cut.msh", to_remove);
        block = pymesh.boolean(block, to_remove, "difference", engine="igl");
        block, __ = pymesh.collapse_short_edges(block, 1e-12, False);
        if save_intermediate:
            pymesh.save_mesh("{}_{:06}{}".format(name, i, ext), block);
    return block;
Example #4
0
def carve_mesh(target_mesh, block, N, batch_size, out_name,
        initial_N=0, save_intermediate=True, debug=False):
    name, ext = os.path.splitext(out_name);
    tree = pymesh.AABBTree();
    tree.load_mesh(target_mesh);

    dodecahedron = pymesh.generate_dodecahedron(1.0, np.zeros(3));
    vertices = dodecahedron.vertices;
    faces = dodecahedron.faces;

    for i in range(initial_N, N, batch_size):
        pts = block.vertices;
        squared_dist, face_indices, closest_pts = \
                tree.look_up_with_closest_points(pts);
        n = np.min([batch_size, N-i, len(pts)]);
        indices = np.argsort(squared_dist)[::-1][:n];
        radii = np.sqrt(squared_dist[indices]);
        centers = pts[indices, :];
        to_remove = [pymesh.form_mesh(
            np.dot(pymesh.Quaternion(numpy.random.rand(4)).to_matrix(), vertices.T * r).T + p, faces)
            for r,p in zip(radii, centers)];
        to_remove = pymesh.merge_meshes(to_remove);
        if debug:
            pymesh.save_mesh("deubg_block.msh", block);
            pymesh.save_mesh("deubg_cut.msh", to_remove);
        block = pymesh.boolean(block, to_remove, "difference", engine="igl");
        block, __ = pymesh.collapse_short_edges(block, 1e-12, False);
        if save_intermediate:
            pymesh.save_mesh("{}_{:06}{}".format(name, i, ext), block);
    return block;
Example #5
0
def main():
    args = parse_args();

    input_meshes = [load_mesh(filename) for filename in args.input_meshes];
    output_mesh = merge_meshes(input_meshes);
    save_mesh(args.output, output_mesh,
            *output_mesh.get_attribute_names());
Example #6
0
def main():
    args = parse_args()
    mesh = pymesh.load_mesh(args.input_mesh)
    slices = pymesh.slice_mesh(mesh, np.array(args.axis), args.N)

    slices = pymesh.merge_meshes(slices)
    pymesh.save_mesh(args.output_slices, slices, *slices.get_attribute_names())
Example #7
0
def repousse(mesh, logger):
    cell_ids = mesh.get_attribute("cell").ravel().astype(int);
    mesh.add_attribute("edge_length");
    tol = np.amax(mesh.get_attribute("edge_length")) * 0.1;

    bbox_min, bbox_max = mesh.bbox;
    scaling = 2.0 / norm(bbox_max - bbox_min);

    start_time = time();
    num_cells = np.amax(cell_ids)+1;
    results = [];
    for i in range(num_cells):
        to_keep = np.arange(mesh.num_faces, dtype=int)[cell_ids == i];
        if not np.any(to_keep):
            continue;

        cut_mesh = pymesh.submesh(mesh, to_keep, 0);
        pymesh.save_mesh("debug.msh", cut_mesh);
        cut_mesh, __ = pymesh.remove_degenerated_triangles(cut_mesh, 100);
        cut_mesh, __ = pymesh.split_long_edges(cut_mesh, tol);

        dof = cut_mesh.num_vertices;
        assembler = pymesh.Assembler(cut_mesh);
        L = assembler.assemble("laplacian");
        M = assembler.assemble("mass");

        L_rhs = M * np.ones(dof) * -0.5;
        bd_indices = cut_mesh.boundary_vertices;
        n = len(bd_indices);
        C = scipy.sparse.coo_matrix((np.ones(n),
            (np.arange(n, dtype=int), bd_indices)), shape=(n, dof));
        C_rhs = np.zeros(n);

        A = scipy.sparse.bmat([
            [-L, C.T],
            [C, None]
            ]);
        rhs = np.concatenate((L_rhs.ravel(), C_rhs));

        solver = pymesh.SparseSolver.create("SparseLU");
        solver.compute(A);
        x = solver.solve(rhs);

        z = x[:dof].reshape((-1, 1));

        vertices = np.hstack((cut_mesh.vertices, z));
        out_mesh = pymesh.form_mesh(vertices, cut_mesh.faces);
        results.append(out_mesh);

    finish_time = time();
    t = finish_time - start_time;
    logger.info("Repousse running time: {}".format(t));

    mesh = pymesh.merge_meshes(results);

    vertices = mesh.vertices[:,:2];
    mesh_2d = pymesh.form_mesh(vertices, mesh.faces);
    pymesh.save_mesh("out_2d.msh", mesh_2d)
    return mesh;
Example #8
0
def repousse(mesh, logger):
    cell_ids = mesh.get_attribute("cell").ravel().astype(int)
    mesh.add_attribute("edge_length")
    tol = np.amax(mesh.get_attribute("edge_length")) * 0.1

    bbox_min, bbox_max = mesh.bbox
    scaling = 2.0 / norm(bbox_max - bbox_min)

    start_time = time()
    num_cells = np.amax(cell_ids) + 1
    results = []
    for i in range(num_cells):
        to_keep = np.arange(mesh.num_faces, dtype=int)[cell_ids == i]
        if not np.any(to_keep):
            continue

        cut_mesh = pymesh.submesh(mesh, to_keep, 0)
        pymesh.save_mesh("debug.msh", cut_mesh)
        cut_mesh, __ = pymesh.remove_degenerated_triangles(cut_mesh, 100)
        cut_mesh, __ = pymesh.split_long_edges(cut_mesh, tol)

        dof = cut_mesh.num_vertices
        assembler = pymesh.Assembler(cut_mesh)
        L = assembler.assemble("laplacian")
        M = assembler.assemble("mass")

        L_rhs = M * np.ones(dof) * -0.5
        bd_indices = cut_mesh.boundary_vertices
        n = len(bd_indices)
        C = scipy.sparse.coo_matrix(
            (np.ones(n), (np.arange(n, dtype=int), bd_indices)),
            shape=(n, dof))
        C_rhs = np.zeros(n)

        A = scipy.sparse.bmat([[-L, C.T], [C, None]])
        rhs = np.concatenate((L_rhs.ravel(), C_rhs))

        solver = pymesh.SparseSolver.create("SparseLU")
        solver.compute(A)
        x = solver.solve(rhs)

        z = x[:dof].reshape((-1, 1))

        vertices = np.hstack((cut_mesh.vertices, z))
        out_mesh = pymesh.form_mesh(vertices, cut_mesh.faces)
        results.append(out_mesh)

    finish_time = time()
    t = finish_time - start_time
    logger.info("Repousse running time: {}".format(t))

    mesh = pymesh.merge_meshes(results)

    vertices = mesh.vertices[:, :2]
    mesh_2d = pymesh.form_mesh(vertices, mesh.faces)
    pymesh.save_mesh("out_2d.msh", mesh_2d)
    return mesh
Example #9
0
    def test_diag_box(self):
        mesh = generate_box_mesh(np.array([0, 0, 0]), np.array([1, 1, 1]))
        N = 5

        slices = slice_mesh(mesh, [1, 1, 1], N)
        self.assertEqual(N, len(slices))

        slices = merge_meshes(slices)
        self.assert_bbox_is_embedded(slices.bbox, mesh.bbox)
Example #10
0
    def test_diag_box(self):
        mesh = generate_box_mesh(
                np.array([0, 0, 0]), np.array([1, 1, 1]));
        N = 5;

        slices = slice_mesh(mesh, [1, 1, 1], N);
        self.assertEqual(N, len(slices));

        slices = merge_meshes(slices);
        self.assert_bbox_is_embedded(slices.bbox, mesh.bbox);
Example #11
0
    def test_2D_meshes(self):
        vertices = np.array([[0.0, 0.0], [1.0, 0.0], [1.0, 1.0], [0.0, 1.0]],
                            dtype=float)
        faces = np.array([[0, 1, 2], [0, 2, 3]], dtype=int)
        mesh_1 = form_mesh(vertices, faces)
        out_mesh = merge_meshes([mesh_1] * 3)

        self.assertEqual(mesh_1.num_vertices * 3, out_mesh.num_vertices)
        self.assertEqual(mesh_1.num_faces * 3, out_mesh.num_faces)
        self.assertEqual(mesh_1.num_voxels * 3, out_mesh.num_voxels)
Example #12
0
    def test_2_meshes(self):
        mesh_1 = generate_box_mesh(np.zeros(3), np.ones(3))
        mesh_2 = generate_box_mesh(np.ones(3), np.ones(3) * 2, subdiv_order=2)
        out_mesh = merge_meshes([mesh_1, mesh_2])

        self.assertEqual(mesh_1.num_vertices + mesh_2.num_vertices,
                         out_mesh.num_vertices)
        self.assertEqual(mesh_1.num_faces + mesh_2.num_faces,
                         out_mesh.num_faces)
        self.assertEqual(mesh_1.num_voxels + mesh_2.num_voxels,
                         out_mesh.num_voxels)
Example #13
0
 def _load_mesh(desc):
     path = desc['objs_dir']
     objs = desc['objs']
     mesh_list = []
     for obj in eval(objs):
         obj_path = os.path.join(path, obj + '.obj')
         mesh_tmp = load_pc(obj_path)
         mesh_tmp, info = pymesh.remove_isolated_vertices(mesh_tmp)
         mesh_list.append(mesh_tmp)
     mesh = pymesh.merge_meshes(mesh_list)
     return mesh
    def test_simple(self):
        mesh_1 = generate_box_mesh(np.zeros(3), np.ones(3));
        mesh_2 = generate_box_mesh(np.array([0.5, 0.5, 0.5]), np.ones(3));
        out_mesh = merge_meshes([mesh_1, mesh_2]);

        components = separate_mesh(out_mesh);
        self.assertEqual(2, len(components));

        for comp in components:
            self.assertEqual(8, comp.num_vertices);
            self.assertEqual(12, comp.num_faces);
Example #15
0
    def test_2_meshes(self):
        mesh_1 = generate_box_mesh(np.zeros(3), np.ones(3));
        mesh_2 = generate_box_mesh(np.ones(3), np.ones(3)*2, subdiv_order=2);
        out_mesh = merge_meshes([mesh_1, mesh_2]);

        self.assertEqual(mesh_1.num_vertices + mesh_2.num_vertices,
                out_mesh.num_vertices);
        self.assertEqual(mesh_1.num_faces + mesh_2.num_faces,
                out_mesh.num_faces);
        self.assertEqual(mesh_1.num_voxels + mesh_2.num_voxels,
                out_mesh.num_voxels);
Example #16
0
    def reduced(self, n=10):
        components = self.components()

        areas = [c.total_area for c in components]

        area_tuples = zip(components, areas)

        components = sorted(area_tuples, key=itemgetter(1), reverse=True)
        components = components[:10]
        components = [c[0].mesh for c in components]
        mesh = pm.merge_meshes(components)
        return Topex(mesh)
Example #17
0
    def test_2D_meshes(self):
        vertices = np.array([
            [0.0, 0.0],
            [1.0, 0.0],
            [1.0, 1.0],
            [0.0, 1.0] ], dtype=float);
        faces = np.array([
            [0, 1, 2],
            [0, 2, 3] ], dtype=int);
        mesh_1 = form_mesh(vertices, faces);
        out_mesh = merge_meshes([mesh_1] * 3);

        self.assertEqual(mesh_1.num_vertices * 3, out_mesh.num_vertices);
        self.assertEqual(mesh_1.num_faces * 3, out_mesh.num_faces);
        self.assertEqual(mesh_1.num_voxels * 3, out_mesh.num_voxels);
def arrow(start=[0, 0, 0],
          end=[0, 0, 1],
          thickness=0.1,
          color=QColor("blue"),
          effect_f=CustomEffects.material,
          matrix=np.eye(4, dtype='f4'),
          name="arrow"):
    start = utils.to_numpy(start)
    end = utils.to_numpy(end)
    direction = end - start
    cyl = pymesh.generate_cylinder(start, start + direction * 0.9, thickness,
                                   thickness, 10)
    head = pymesh.generate_cylinder(start + direction * 0.9, end,
                                    thickness * 2, thickness * 0.1, 10)
    return from_mesh(pymesh.merge_meshes([cyl, head]), color, effect_f, 1,
                     matrix, name)
Example #19
0
def clean_single_mesh(src_mesh, tar_mesh, dist_thresh, num_thresh):
    src_mesh_obj = pymesh.load_mesh(src_mesh)
    dis_meshes = pymesh.separate_mesh(src_mesh_obj, connectivity_type='auto')
    max_mesh_verts = 0
    for dis_mesh in dis_meshes:
       if dis_mesh.vertices.shape[0] > max_mesh_verts:
           max_mesh_verts = dis_mesh.vertices.shape[0]

    collection=[]
    for dis_mesh in dis_meshes:
       if dis_mesh.vertices.shape[0] > max_mesh_verts*num_thresh:
           centroid = np.mean(dis_mesh.vertices, axis=0)
           if np.sqrt(np.sum(np.square(centroid))) < dist_thresh:
            collection.append(dis_mesh)
    tar_mesh_obj = pymesh.merge_meshes(collection)
    pymesh.save_mesh_raw(tar_mesh, tar_mesh_obj.vertices, tar_mesh_obj.faces)
    print("threshes:", str(dist_thresh), str(num_thresh), " clean: ", src_mesh, " create: ",tar_mesh)
    def _getitem_leaf(self, index):
        if index in self.cache:
            mesh = self.cache[index]
        else:
            path = self.leafs_in_action.iloc[index]['objs_dir']
            objs = self.leafs_in_action.iloc[index]['objs']
            mesh_list = []
            for obj in eval(objs):
                obj_path = os.path.join(path, obj + '.obj')
                mesh_tmp = load_pc(obj_path)
                mesh_tmp, info = pymesh.remove_isolated_vertices(mesh_tmp)
                mesh_list.append(mesh_tmp)
            mesh = pymesh.merge_meshes(mesh_list)
            if len(self.cache) == self.cache_maxsize:
                pop_key = random.choice(list(self.cache.keys()))
                self.cache.pop(pop_key)
            self.cache[index] = mesh

        if self.return_mode == "vertex":
            res = get_pc(mesh)
        else:
            res = mesh
        return res
Example #21
0
def main():
    args = parse_args()
    mesh = pymesh.load_mesh(args.input_mesh)
    if not mesh.has_attribute("corner_texture"):
        raise RuntimeError("Mesh contains no uv!")
    mesh.add_attribute("face_area")

    uv = mesh.get_attribute("corner_texture").reshape((-1, 2))
    if len(uv) == 0:
        raise RuntimeError("Invalid uv size.")

    faces = np.arange(mesh.num_faces * mesh.vertex_per_face).reshape(
        (-1, mesh.vertex_per_face))
    uv_mesh = pymesh.form_mesh(uv, faces)
    uv_mesh.add_attribute("face_area")

    ori_area = mesh.get_face_attribute("face_area")
    uv_area = uv_mesh.get_face_attribute("face_area")
    area_ratio = np.divide(uv_area, ori_area)

    uv_mesh.add_attribute("area_ratio")
    uv_mesh.set_attribute("area_ratio", area_ratio)
    mesh.add_attribute("area_ratio")
    mesh.set_attribute("area_ratio", area_ratio)
    mesh.add_attribute("u")
    mesh.set_attribute("u", uv[:, 0])

    if (args.separate):
        uv_mesh, info = pymesh.remove_duplicated_vertices(uv_mesh)
        comps = pymesh.separate_mesh(uv_mesh)
        segments = []
        uv = uv.reshape((-1, 6), order="C")
        vertices = mesh.vertices
        combined_uv = []
        for comp in comps:
            ori_vertex_indices = comp.get_attribute(
                "ori_vertex_index").ravel()
            ori_elem_indices = comp.get_attribute(
                "ori_elem_index").ravel().astype(int)

            segment = pymesh.submesh(mesh, ori_elem_indices, 0)
            ori_face_indices = segment.get_attribute(
                "ori_face_index").ravel().astype(int)
            ori_uv = uv[ori_face_indices]
            combined_uv.append(ori_uv)
            segment.add_attribute("corner_texture")
            segment.set_attribute("corner_texture", ori_uv.ravel())
            segments.append(segment)

        combined_uv = np.vstack(combined_uv)
        mesh = pymesh.merge_meshes(segments)
        mesh.add_attribute("corner_texture")
        mesh.set_attribute("corner_texture", combined_uv.ravel(order="C"))
    elif args.cut:
        uv_mesh, info = pymesh.remove_duplicated_vertices(uv_mesh)
        index_map = info["index_map"]
        vertices = mesh.vertices
        faces = mesh.faces
        vertices = vertices[faces.ravel(order="C")]
        new_vertices = np.zeros((uv_mesh.num_vertices, 3))
        new_vertices[index_map] = vertices

        mesh = pymesh.form_mesh(new_vertices, uv_mesh.faces)
        mesh.add_attribute("corner_texture")
        mesh.set_attribute("corner_texture", uv)

    if args.save_uv:
        pymesh.save_mesh(args.output_mesh, uv_mesh, *uv_mesh.attribute_names)
    else:
        pymesh.save_mesh(args.output_mesh, mesh, *mesh.attribute_names)
Example #22
0
def main():
    args = parse_args();
    mesh = pymesh.load_mesh(args.input_mesh);
    num_tets = mesh.num_voxels;
    assert(mesh.dim == 3);
    assert(mesh.vertex_per_voxel == 4);
    assert(num_tets > 0);
    mesh = fit_into_unit_sphere(mesh);
    mesh.add_attribute("voxel_centroid");
    assembler = pymesh.Assembler(mesh);

    if args.charge_distribution == "sphere":
        ball = pymesh.generate_icosphere(1.25 + 1e-3, np.zeros(3), 0);
        charges = ball.vertices;
    elif args.charge_distribution == "center":
        charges = np.zeros(3);
    else:
        raise NotImplementedError("Unsupported charge distribution ({})."\
                .format(args.charge_distribution));

    if args.output_kernels:
        kernels = pymesh.merge_meshes([
            pymesh.generate_icosphere(0.05, v, 2) for v in charges ]);
        pymesh.save_mesh("kernel.msh", kernels);
    f = test_function(charges);
    g = test_function_grad(charges);

    v_pts = mesh.vertices;
    q_pts = get_quadrature_pts(mesh);
    c_pts = mesh.get_voxel_attribute("voxel_centroid");

    v_values = f(v_pts);
    q_values = f(q_pts);
    c_values = f(c_pts);
    q_grad = g(q_pts);
    c_grad = g(c_pts);
    c_grad_norm = numpy.linalg.norm(c_grad, axis=1);
    q_grad_norm = numpy.linalg.norm(q_grad, axis=1);
    #for v,val in zip(v_pts, v_values):
    #    print(v, val);

    assert(len(v_values) == len(v_pts));
    assert(len(q_values) == len(q_pts));
    assert(len(c_values) == len(c_pts));
    assert(len(q_grad) == len(q_pts));
    assert(len(c_grad) == len(c_pts));

    bd_indices = np.unique(mesh.faces.ravel()).astype(int);
    bd_values = v_values[bd_indices];

    solver = pymesh.HarmonicSolver.create(mesh);
    solver.order = 1;
    solver.boundary_indices = bd_indices;
    solver.boundary_values = bd_values;
    solver.pre_process();
    solver.solve();

    sol_values = solver.solution.ravel();
    G = assembler.assemble("gradient");
    sol_grad = (G * sol_values).reshape((-1,3), order="C");
    sol_q_values, sol_q_grad = \
            interpolate_at_quadrature_pts(mesh, sol_values, sol_grad);
    sol_c_values, sol_c_grad = \
            interpolate_at_centroids(mesh, sol_values, sol_grad);

    v_err = sol_values - v_values;
    c_err = sol_c_values - c_values;
    q_err = sol_q_values - q_values;
    v_rel_err = np.divide(v_err, v_values);
    c_rel_err = np.divide(c_err, c_values);
    q_rel_err = np.divide(q_err, q_values);

    c_grad_err = sol_c_grad - c_grad;
    q_grad_err = sol_q_grad - q_grad;
    c_grad_err_norm = numpy.linalg.norm(c_grad_err, axis=1);
    q_grad_err_norm = numpy.linalg.norm(q_grad_err, axis=1);
    c_grad_rel_err_norm = np.divide(c_grad_err_norm, c_grad_norm);
    q_grad_rel_err_norm = np.divide(q_grad_err_norm, q_grad_norm);

    print("num_tets: {}".format(num_tets));

    print("==Ground Truth==");
    print("max solution at nodes: {}".format(np.amax(np.absolute(v_values))));
    print("max solution at centroids: {}".format(np.amax(np.absolute(c_values))));
    print("max solution at quadrature pts: {}".format(np.amax(np.absolute(q_values))));
    print("max grad solution at centroids: {}".format(np.amax(c_grad_norm)));
    print("max grad solution at quadrature pts: {}".format(np.amax(q_grad_norm)));

    print("L2 solution at nodes: {}".format(numpy.linalg.norm(v_values)));
    print("L2 solution at centroids: {}".format(numpy.linalg.norm(c_values)));
    print("L2 solution at quadrature pts: {}".format(numpy.linalg.norm(q_values)));
    print("L2 grad solution at centroids: {}".format(numpy.linalg.norm(c_grad_norm)));
    print("L2 grad solution at quadrature pts: {}".format(numpy.linalg.norm(q_grad_norm)));

    print("sqrt ave at nodes: {}".format(math.sqrt(np.mean(np.square(v_values)))));
    print("sqrt ave at centroids: {}".format(math.sqrt(np.mean(np.square(c_values)))));
    print("sqrt ave at quadrature pts: {}".format(math.sqrt(np.mean(np.square(q_values)))));
    print("sqrt ave grad solution at centroids: {}".format(math.sqrt(np.mean(np.square(c_grad_norm)))));
    print("sqrt ave grad solution at quadrature pts: {}".format(math.sqrt(np.mean(np.square(q_grad_norm)))));

    print("==Absolute errors==");
    print("max error at nodes: {}".format(np.amax(np.absolute(v_err))));
    print("max error at centroids: {}".format(np.amax(np.absolute(c_err))));
    print("max error at quadrature pts: {}".format(np.amax(np.absolute(q_err))));
    print("max grad error at centroids: {}".format(np.amax(c_grad_err_norm)));
    print("max grad error at quadrature pts: {}".format(np.amax(q_grad_err_norm)));

    print("average error at nodes: {}".format(np.mean(np.absolute(v_err))));
    print("average error at centroids: {}".format(np.mean(np.absolute(c_err))));
    print("average error at quadrature pts: {}".format(np.mean(np.absolute(q_err))));
    print("average grad error at centroids: {}".format(np.mean(c_grad_err_norm)));
    print("average grad error at quadrature pts: {}".format(np.mean(q_grad_err_norm)));

    print("L2 error at nodes: {}".format(numpy.linalg.norm(v_err)));
    print("L2 error at centroids: {}".format(numpy.linalg.norm(c_err)));
    print("L2 error at quadrature pts: {}".format(numpy.linalg.norm(q_err)));
    print("L2 grad error at centroids: {}".format(numpy.linalg.norm(c_grad_err_norm)));
    print("L2 grad error at quadrature pts: {}".format(numpy.linalg.norm(q_grad_err_norm)));

    print("==Relative errors==");
    print("max rel error at nodes: {}".format(np.amax(np.absolute(v_rel_err))));
    print("max rel error at centroids: {}".format(np.amax(np.absolute(c_rel_err))));
    print("max rel error at quadrature pts: {}".format(np.amax(np.absolute(q_rel_err))));
    print("max grad rel error at centroids: {}".format(np.amax(c_grad_rel_err_norm)));
    print("max grad rel error at quadrature pts: {}".format(np.amax(q_grad_rel_err_norm)));

    mesh.add_attribute("target_solution");
    mesh.set_attribute("target_solution", v_values);
    mesh.add_attribute("solution");
    mesh.set_attribute("solution", sol_values);
    mesh.add_attribute("v_error");
    mesh.set_attribute("v_error", v_err);
    mesh.add_attribute("v_rel_error");
    mesh.set_attribute("v_rel_error", v_rel_err);
    mesh.add_attribute("c_error");
    mesh.set_attribute("c_error", c_err);
    mesh.add_attribute("c_rel_error");
    mesh.set_attribute("c_rel_error", c_rel_err);
    mesh.add_attribute("q_error");
    mesh.set_attribute("q_error", np.amax(np.absolute(q_err.reshape((-1,4),
        order="C")), axis=1));
    mesh.add_attribute("q_rel_error");
    mesh.set_attribute("q_rel_error", np.amax(np.absolute(q_rel_err.reshape((-1,4),
        order="C")), axis=1));
    mesh.add_attribute("c_grad_error");
    mesh.set_attribute("c_grad_error", c_grad_err_norm);
    mesh.add_attribute("c_grad_rel_error");
    mesh.set_attribute("c_grad_rel_error", c_grad_rel_err_norm);
    mesh.add_attribute("q_grad_error");
    mesh.set_attribute("q_grad_error", np.amax(q_grad_err_norm.reshape((-1,4),
        order="C"), axis=1));
    mesh.add_attribute("q_grad_rel_error");
    mesh.set_attribute("q_grad_rel_error", np.amax(q_grad_rel_err_norm.reshape((-1,4),
        order="C"), axis=1));
    mesh.add_attribute("solution_grad");
    mesh.set_attribute("solution_grad", sol_c_grad);
    mesh.add_attribute("target_grad");
    mesh.set_attribute("target_grad", c_grad);
    pymesh.save_mesh(args.output_mesh, mesh, *mesh.attribute_names);
Example #23
0
def main():
    args = parse_args()

    input_meshes = [load_mesh(filename) for filename in args.input_meshes]
    output_mesh = merge_meshes(input_meshes)
    save_mesh(args.output, output_mesh, *output_mesh.get_attribute_names())
Example #24
0
def bevel(wires, logger, remove_holes, dist):
    bbox_min, bbox_max = wires.bbox

    #wires = uniform_sampling(wires);
    mesh = constrained_triangulate(wires, logger, remove_holes)

    cell_ids = mesh.get_attribute("cell").ravel().astype(int)
    num_cells = np.amax(cell_ids) + 1

    comps = []
    for i in range(num_cells):
        to_keep = np.arange(mesh.num_faces, dtype=int)[cell_ids == i]
        if not np.any(to_keep):
            continue

        cut_mesh = pymesh.submesh(mesh, to_keep, 0)
        bd_edges = cut_mesh.boundary_edges
        vertices, edges, __ = pymesh.remove_isolated_vertices_raw(
            cut_mesh.vertices, bd_edges)
        bd_wires = pymesh.wires.WireNetwork.create_from_data(vertices, edges)

        offset_dir = np.zeros((bd_wires.num_vertices, 2))
        for ei in edges:
            v0 = ei[0]
            v1 = ei[1]
            adj_vts = bd_wires.get_vertex_neighbors(v0)
            if len(adj_vts) == 2:
                if adj_vts[0] == v1:
                    vp = adj_vts[1]
                else:
                    vp = adj_vts[0]
                e0 = vertices[v1] - vertices[v0]
                e1 = vertices[vp] - vertices[v0]
                e0 /= norm(e0)
                e1 /= norm(e1)
                theta = math.atan2(e0[0] * e1[1] - e0[1] * e1[0], e0.dot(e1))

                if abs(theta) > 0.99 * math.pi:
                    offset = np.array([-e0[1], e0[0]])
                    scale = 1.0
                else:
                    if theta > 0:
                        offset = e0 + e1
                        scale = math.sin(theta / 2)
                    else:
                        offset = -e0 - e1
                        scale = math.cos(math.pi / 2 + theta / 2)
                    offset /= norm(offset)
                offset_dir[v0] = offset * dist / scale

        offset_vertices = vertices + offset_dir
        vertices = np.vstack((vertices, offset_vertices))
        edges = np.vstack((edges, edges + bd_wires.num_vertices))

        vertices, edges, __ = pymesh.remove_duplicated_vertices_raw(
            vertices, edges, dist / 2)

        comp_wires = pymesh.wires.WireNetwork.create_from_data(
            vertices, edges)
        comp = constrained_triangulate(comp_wires, logger, True)
        comps.append(comp)

    mesh = pymesh.merge_meshes(comps)
    bd_vertices = mesh.boundary_vertices
    is_inside = np.ones(mesh.num_vertices, dtype=bool)
    is_inside[bd_vertices] = False

    vertices = np.hstack((mesh.vertices, np.zeros((mesh.num_vertices, 1))))
    vertices[is_inside, 2] = dist
    mesh = pymesh.form_mesh(vertices, mesh.faces)
    return mesh
Example #25
0
    return pymesh.boolean(meshA, meshB, operation="union", engine="igl")


start_time = time.time()

## Opening up the files -------------------

ArmR = load_mesh("mesh-arm-r")
HandR = load_mesh("mesh-hand-r")
ArmL = load_mesh("mesh-arm-l")
HandL = load_mesh("mesh-hand-l")
Head = load_mesh("mesh-head")
LegL = load_mesh("mesh-leg-l")
FootL = load_mesh("mesh-foot-l")
LegR = load_mesh("mesh-leg-r")
FootR = load_mesh("mesh-foot-r")
Torso = load_mesh("mesh-torso")
Stand = load_mesh("stand")

## Unions -------------------

A = pymesh.merge_meshes([HandR, HandL, FootR, FootL, Torso])
B = pymesh.merge_meshes([ArmR, ArmL, Head, LegL, LegR, Stand])

Output = mesh_union(A, B)

pymesh.save_mesh("/meshes/myCharacter.stl", Output)

print("Mesh merged successfully!")
print("--- %s seconds ---" % (time.time() - start_time))
Example #26
0
def bevel(wires, logger, remove_holes, dist):
    bbox_min, bbox_max = wires.bbox;

    #wires = uniform_sampling(wires);
    mesh = constrained_triangulate(wires, logger, remove_holes);

    cell_ids = mesh.get_attribute("cell").ravel().astype(int);
    num_cells = np.amax(cell_ids) + 1;

    comps = [];
    for i in range(num_cells):
        to_keep = np.arange(mesh.num_faces, dtype=int)[cell_ids == i];
        if not np.any(to_keep):
            continue;

        cut_mesh = pymesh.submesh(mesh, to_keep, 0);
        bd_edges = cut_mesh.boundary_edges;
        vertices, edges, __ = pymesh.remove_isolated_vertices_raw(
                cut_mesh.vertices, bd_edges);
        bd_wires = pymesh.wires.WireNetwork.create_from_data(vertices, edges);

        offset_dir = np.zeros((bd_wires.num_vertices, 2));
        for ei in edges:
            v0 = ei[0];
            v1 = ei[1];
            adj_vts = bd_wires.get_vertex_neighbors(v0);
            if len(adj_vts) == 2:
                if adj_vts[0] == v1:
                    vp = adj_vts[1];
                else:
                    vp = adj_vts[0];
                e0 = vertices[v1] - vertices[v0];
                e1 = vertices[vp] - vertices[v0];
                e0 /= norm(e0);
                e1 /= norm(e1);
                theta = math.atan2(e0[0]*e1[1] - e0[1]*e1[0], e0.dot(e1));

                if abs(theta) > 0.99 * math.pi:
                    offset = np.array([-e0[1], e0[0]]);
                    scale = 1.0;
                else:
                    if theta > 0:
                        offset = e0 + e1;
                        scale = math.sin(theta/2);
                    else:
                        offset = -e0 - e1
                        scale = math.cos(math.pi/2 + theta/2);
                    offset /= norm(offset);
                offset_dir[v0] = offset * dist / scale;

        offset_vertices = vertices + offset_dir;
        vertices = np.vstack((vertices, offset_vertices));
        edges = np.vstack((edges, edges + bd_wires.num_vertices));

        vertices, edges, __ = pymesh.remove_duplicated_vertices_raw(
                vertices, edges, dist/2);

        comp_wires = pymesh.wires.WireNetwork.create_from_data(vertices, edges);
        comp = constrained_triangulate(comp_wires, logger, True);
        comps.append(comp);

    mesh = pymesh.merge_meshes(comps);
    bd_vertices = mesh.boundary_vertices;
    is_inside = np.ones(mesh.num_vertices, dtype=bool);
    is_inside[bd_vertices] = False;

    vertices = np.hstack((mesh.vertices, np.zeros((mesh.num_vertices, 1))));
    vertices[is_inside, 2] = dist;
    mesh = pymesh.form_mesh(vertices, mesh.faces);
    return mesh;
def make(Cosntellation):
    Clines_f = map(lambda s: s[:-1].split(),
                   Clines)  #read constellation lines information
    line_arr = pd.DataFrame(list(Clines_f))
    line_arr = np.array(line_arr[
        line_arr.loc[:, 0] == Cosntellation].iloc[0].dropna()[2:]).astype(int)
    #print(line_arr)

    dat = Table.read('hip.fit', format='fits')
    df = dat.to_pandas().set_index('HIP')  # i llove pandas :)
    df = df.loc[np.unique(line_arr)]
    #print(df)

    RA_center = (np.max(df['_RA_icrs']) +
                 np.min(df['_RA_icrs'])) / 2.  #constellation center
    if np.max(df['_RA_icrs']) - np.min(df['_RA_icrs']) > 180:
        RA_center = (np.max(df['_RA_icrs']) - 360 +
                     np.min(df['_RA_icrs'])) / 2.
        if RA_center < 0:
            RA_center += 360

    DE_center = (np.max(df['_DE_icrs']) + np.min(df['_DE_icrs'])) / 2.

    #print(RA_center,DE_center)

    loc = EarthLocation(lat=DE_center * u.deg,
                        lon=0 * u.deg)  #convert coordinates
    fake_time = Time(
        '2019-09-21T00:01:40.2', format='isot', scale='utc',
        location=loc) + TimeDelta(RA_center / 360 * 86164, format='sec')
    fakeFrame = AltAz(obstime=fake_time, location=loc)
    cords = SkyCoord(
        list(df['_RA_icrs']) * u.deg,
        list(df['_DE_icrs']) * u.deg).transform_to(fakeFrame)
    df['phi'] = np.radians(cords.az.deg) + np.pi / 2
    df['z'] = 90 - cords.alt.deg

    # df['phi'] = np.radians(df['_RA_icrs'])
    # df['z'] = 90 - df['_DE_icrs']

    df['rho'] = df['z']
    df['x'] = df['rho'] * np.cos(df['phi'])
    df['y'] = df['rho'] * np.sin(df['phi'])

    scale = np.max([df['x'], df['y']]) - np.min([df['x'], df['y']])
    scale = 45
    df['x'] *= 230 / scale  #calculate scale
    df['y'] *= 230 / scale
    # import matplotlib.pyplot as plt
    # plt.plot(df['x'],df['y'],'o')
    # plt.show()
    print('coverted' + Cosntellation)

    star_list = []
    for hip, star in df.iterrows():  #make stars
        r = 8 * 1.3**(-star['Vmag'])
        if r > 15: r = 15
        sphere = pymesh.generate_icosphere(r, (star['x'], star['y'], 0),
                                           refinement_order=2)
        sphere = pymesh.form_mesh(sphere.vertices * np.array((1, 1, 0.75)),
                                  sphere.faces)
        star_list.append(sphere)

    mesh = pymesh.merge_meshes(star_list)

    print('stars' + Cosntellation)

    # print(df)
    cil_list = []
    for i in range(0, len(line_arr), 2):  #make lines
        cilinder = pymesh.generate_cylinder(
            (df.loc[line_arr[i], 'x'], df.loc[line_arr[i], 'y'], 0),
            (df.loc[line_arr[i + 1], 'x'], df.loc[line_arr[i + 1], 'y'], 0),
            1.4, 1.4)
        cilinder = pymesh.form_mesh(cilinder.vertices * np.array((1, 1, 0.5)),
                                    cilinder.faces)
        cil_list.append(cilinder)

    cil_mesh = pymesh.merge_meshes(cil_list)

    print('lines' + Cosntellation)

    mesh = pymesh.boolean(mesh, cil_mesh, operation='union')

    DOWN = pymesh.meshutils.generate_box_mesh((-400, -400, -400),
                                              (400, 400, 0))
    mesh = pymesh.boolean(mesh, DOWN, 'difference')

    pymesh.meshio.save_mesh('./' + Cosntellation + '.stl', mesh)

    print('completed' + Cosntellation)