def show_polyscope(self):
    # Shows a polyscope window (https://polyscope.run/py/) of the current MeshSet, rendering all the meshes contained
    # in it. Requires the polyscope package (pip install polyscope).
    import polyscope
    import numpy

    polyscope.init()
    for m in self:
        is_enabled = m.is_visible()
        if m.is_point_cloud():
            psm = polyscope.register_point_cloud(m.label(),
                                                 m.transformed_vertex_matrix(),
                                                 enabled=is_enabled)
        else:
            psm = polyscope.register_surface_mesh(
                m.label(),
                m.transformed_vertex_matrix(),
                m.face_matrix(),
                enabled=is_enabled)
        if m.has_vertex_color():
            vc = m.vertex_color_matrix()
            vc = numpy.delete(vc, 3, 1)
            psm.add_color_quantity('vertex_color', vc)
        if m.has_vertex_scalar():
            psm.add_scalar_quantity('vertex_scalar', m.vertex_scalar_array())
        if not m.is_point_cloud() and m.has_face_color():
            fc = m.face_color_matrix()
            fc = numpy.delete(fc, 3, 1)
            psm.add_color_quantity('face_color', fc, defined_on='faces')
        if not m.is_point_cloud() and m.has_face_scalar():
            psm.add_scalar_quantity('face_scalar',
                                    m.face_scalar_array(),
                                    defined_on='faces')
    polyscope.show()
Exemple #2
0
    def show(self):
        "use polyscope : https://polyscope.run/py/ "

        assert 0, "TODO"
        import polyscope as ps

        ps.init()

        ### Register a point cloud
        # `my_points` is a Nx3 numpy array
        #ps.register_point_cloud("my points", my_points)

        ### Register a mesh
        # `verts` is a Nx3 numpy array of vertex positions
        # `faces` is a Fx3 array of indices, or a nested list
        # ...

        ps.register_surface_mesh("my mesh", verts, faces, smooth_shade=True)

        ps.show()
Exemple #3
0
def show_mesh_in_ply_file(obj_file):
    ps.init()
    vertex_pos = []
    face_indices = []
    flag = False
    with open(obj_file) as f_in:
        for line in f_in:
            temp = line.split(' ')
            if line == 'end_header\n':
                print('loading data')
                flag = True
                continue
            if flag == True:
                if temp[0] == '3':
                    face_indices.append(np.array([int(temp[1]), int(temp[2]), int(temp[3])]))
                else:
                    vertex_pos.append(np.array([float(temp[0]), float(temp[1]), float(temp[2])]))
    points = np.array(vertex_pos)
    faces = np.array(face_indices)
    ps_mesh = ps.register_surface_mesh("result mesh", points, faces)
    ps.set_autoscale_structures(True)
    ps.show()
Exemple #4
0
    numpy.random.seed(777)
    return numpy.random.rand(n_pts, 3)


def generate_faces(n_pts=10):
    numpy.random.seed(777)
    return numpy.random.randint(0, n_pts, size=(2 * n_pts, 3))


verts, faces = generate_verts(), generate_faces()

### Register a point cloud
# `my_points` is a Nx3 numpy array
# ps.register_point_cloud("my points", my_points)

### Register a mesh
# `verts` is a Nx3 numpy array of vertex positions
# `faces` is a Fx3 array of indices, or a nested list
polyscope.register_surface_mesh("my mesh", verts, faces, smooth_shade=True)

# Add a scalar function and a vector function defined on the mesh
# vertex_scalar is a length V numpy array of values
# face_vectors is an Fx3 array of vectors per face
# ps.get_surface_mesh("my mesh").add_scalar_quantity("my_scalar",
# vertex_scalar, defined_on='vertices', cmap='blues')
# ps.get_surface_mesh("my mesh").add_vector_quantity("my_vector",
# face_vectors, defined_on='faces', color=(0.2, 0.5, 0.5))

# View the point cloud and mesh we just registered in the 3D UI
polyscope.show()
Exemple #5
0
        verts, faces = util.triangulate_surface(surface, n)

        uv = surface.coordinates(n)
        normals = surface.unit_normals(uv)
        jacobians = surface.df(uv)
        k1d, k2d = np.array(surface.principal_direction(uv))

        push_forward_k1d = np.einsum('ij,ikj->ik', k1d, jacobians)
        push_forward_k2d = np.einsum('ij,ikj->ik', k2d, jacobians)

        k1, k2 = np.array(surface.principal_curvature(uv))

        #===================================

        # Register Surface
        ps_mesh = ps.register_surface_mesh(name, verts, faces)

        S = surface.shape_index(uv)
        S_colors = np.array([np.array(shape_scale_to_color(s)) for s in S])
        ps.get_surface_mesh(name).add_color_quantity("S_colormap",
                                                     S_colors,
                                                     defined_on='vertices')
        ps.get_surface_mesh(name).add_scalar_quantity("S",
                                                      S,
                                                      defined_on='vertices',
                                                      cmap='viridis')

        # Register Surface Normals
        ps.get_surface_mesh(name).add_vector_quantity(
            "normals",
            np.array(normals),
def main():

    parser = argparse.ArgumentParser()

    parser.add_argument('model_weights_path',
                        type=str,
                        help='path to the model checkpoint')
    parser.add_argument('input_path', type=str, help='path to the input')

    parser.add_argument('--disable_cuda',
                        action='store_true',
                        help='disable cuda')

    parser.add_argument('--sample_cloud',
                        type=int,
                        help='run on sampled points')

    parser.add_argument('--n_rounds',
                        type=int,
                        default=5,
                        help='number of rounds')
    parser.add_argument('--prob_thresh',
                        type=float,
                        default=.9,
                        help='threshold for final surface')

    parser.add_argument(
        '--output',
        type=str,
        help='path to save the resulting high prob mesh to. also disables viz')
    parser.add_argument('--output_trim_unused',
                        action='store_true',
                        help='trim unused vertices when outputting')

    # Parse arguments
    args = parser.parse_args()
    set_args_defaults(args)

    viz = not args.output
    args.polyscope = False

    # Initialize polyscope
    if viz:
        polyscope.init()

    # === Load the input

    if args.input_path.endswith(".npz"):
        record = np.load(args.input_path)
        verts = torch.tensor(record['vert_pos'],
                             dtype=args.dtype,
                             device=args.device)
        surf_samples = torch.tensor(record['surf_pos'],
                                    dtype=args.dtype,
                                    device=args.device)

        samples = verts.clone()
        faces = torch.zeros((0, 3), dtype=torch.int64, device=args.device)

        polyscope.register_point_cloud("surf samples", toNP(surf_samples))

    if args.input_path.endswith(".xyz"):
        raw_pts = np.loadtxt(args.input_path)
        verts = torch.tensor(raw_pts, dtype=args.dtype, device=args.device)

        samples = verts.clone()
        faces = torch.zeros((0, 3), dtype=torch.int64, device=args.device)

        polyscope.register_point_cloud("surf samples", toNP(verts))

    else:
        print("reading mesh")
        verts, faces = utils.read_mesh(args.input_path)
        print("  {} verts   {} faces".format(verts.shape[0], faces.shape[0]))
        verts = torch.tensor(verts, dtype=args.dtype, device=args.device)
        faces = torch.tensor(faces, dtype=torch.long, device=args.device)

        # verts = verts[::10,:]

        if args.sample_cloud:
            samples = mesh_utils.sample_points_on_surface(
                verts, faces, args.sample_cloud)
        else:
            samples = verts.clone()

    # === Load the model

    print("loading model weights")
    model = PointTriNet_Mesher()
    model.load_state_dict(torch.load(args.model_weights_path))

    model.eval()

    with torch.no_grad():

        # Sample lots of faces from the vertices
        print("predicting")
        candidate_triangles, candidate_probs = model.predict_mesh(
            samples.unsqueeze(0), n_rounds=args.n_rounds)
        candidate_triangles = candidate_triangles.squeeze(0)
        candidate_probs = candidate_probs.squeeze(0)
        print("done predicting")

        # Visualize
        high_prob = args.prob_thresh
        high_faces = candidate_triangles[candidate_probs > high_prob]
        closed_faces = mesh_utils.fill_holes_greedy(high_faces)

        if viz:
            polyscope.register_point_cloud("input points", toNP(samples))

            spmesh = polyscope.register_surface_mesh("all faces",
                                                     toNP(samples),
                                                     toNP(candidate_triangles),
                                                     enabled=False)
            spmesh.add_scalar_quantity("probs",
                                       toNP(candidate_probs),
                                       defined_on='faces')

            spmesh = polyscope.register_surface_mesh(
                "high prob mesh " + str(high_prob), toNP(samples),
                toNP(high_faces))
            spmesh.add_scalar_quantity(
                "probs",
                toNP(candidate_probs[candidate_probs > high_prob]),
                defined_on='faces')

            spmesh = polyscope.register_surface_mesh("hole-closed mesh " +
                                                     str(high_prob),
                                                     toNP(samples),
                                                     toNP(closed_faces),
                                                     enabled=False)

            polyscope.show()

        # Save output
        if args.output:

            high_prob = args.prob_thresh
            out_verts = toNP(samples)
            out_faces = toNP(high_faces)
            out_faces_closed = toNP(closed_faces)

            if args.output_trim_unused:
                out_verts, out_faces, _, _ = igl.remove_unreferenced(
                    out_verts, out_faces)

            igl.write_triangle_mesh(args.output + "_mesh.ply", out_verts,
                                    out_faces)
            write_ply_points(args.output + "_samples.ply", toNP(samples))

            igl.write_triangle_mesh(args.output + "_pred_mesh.ply", out_verts,
                                    out_faces)
            igl.write_triangle_mesh(args.output + "_pred_mesh_closed.ply",
                                    out_verts, out_faces_closed)
            write_ply_points(args.output + "_samples.ply", toNP(samples))
Exemple #7
0
tree = Union('u2', [
    Cylinder(1.0, 2.0, 's1'),
    Pose([1.0, 0.0, 0.0], [45.0, 45.0, 45.0], [Box([1.0, 1.0, 1.0], 'b1')],
         'p1')
])

with open('data/bobbin.json') as json_file:
    tree = node_from_old_json_format(json.load(json_file), False)
    print('loaded tree: {}'.format(tree.to_dict()))

print(tree.to_dict())

tree2 = CSGNode.from_dict(tree.to_dict())

print(tree2.to_dict())

print(json.dumps(tree2.to_dict()))

# uncomment the following line for stack test
# tree = node_from_stack_format('data/stack.txt')

pc = point_cloud_from_node(tree, [-20, -20, -20], [20, 20, 20], 0.2, 0.2)

v, f, n = node_to_mesh(tree, [-20, -20, -20], [20, 20, 20], 0.8)

ps.init()
ps.register_surface_mesh("mesh 1", v, f)
ps.register_point_cloud("points 1", pc)
ps.show()
Exemple #8
0
    # Load SMPL model
    smpl = SMPL(config.SMPL_MODEL_DIR,
                batch_size=1,
                create_transl=False).to(device)
    model.eval()

    ret, first_frame = cap.read()
    cv2.imshow('frame', first_frame)
    # Preprocess input image and generate predictions
    first_joints, first_vertices = get_prediction(first_frame)
    edges = np.array([[2, 3], [3, 4], [5, 6], [6, 7], [9, 10], [10, 11], [12, 13], [13, 14], [14, 19], [14, 20],
                      [14, 21], [11, 22], [11, 23], [11, 24], [12, 8], [8, 9], [8, 41], [41, 40], [37, 43],
                      [40, 5], [40, 2], [40, 37]])
    ps.init()
    ps_net = ps.register_curve_network("my network", first_joints, edges)
    ps_mesh = ps.register_surface_mesh("my mesh", first_vertices, smpl.faces)
    ps.show()

    # Setup renderer for visualization
    # renderer = Renderer(focal_length=constants.FOCAL_LENGTH, img_res=constants.IMG_RES, faces=smpl.faces)

    # outfile = args.img.split('.')[0] if args.outfile is None else args.outfile
    while True:
        _, current_frame = cap.read()
        cv2.imshow('frame', current_frame)
        current_joints, current_vertices = get_prediction(current_frame)
        next_frame(current_joints, current_vertices)

        if cv2.waitKey(1) & 0xFF == ord('q'):
            break
Exemple #9
0
# import scipy.sparse.linalg as sla

# Path to where the bindings live
sys.path.append(os.path.join(os.path.dirname(__file__), "../build/"))
sys.path.append(os.path.join(os.path.dirname(__file__), "../src/"))

import potpourri3d as pp3d

ps.init()

# Read input

## = Mesh test
# V, F = pp3d.read_mesh("/Users/nick/mesh/spot.obj")
V, F = pp3d.read_mesh("bunny_small.ply")
ps_mesh = ps.register_surface_mesh("mesh", V, F)

# Distance
dists = pp3d.compute_distance(V, F, 4)
ps_mesh.add_scalar_quantity("dist", dists)

# Vector heat
solver = pp3d.MeshVectorHeatSolver(V, F)

# Vector heat (extend scalar)
ext = solver.extend_scalar([1, 22], [0., 6.])
ps_mesh.add_scalar_quantity("ext", ext)

# Vector heat (tangent frames)
basisX, basisY, basisN = solver.get_tangent_frames()
ps_mesh.add_vector_quantity("basisX", basisX)
Exemple #10
0
 def add_surface_mesh(self, name, obj_path, **kwargs):
     verts, faces = load_obj_robust(obj_path)
     ps.register_surface_mesh(name, verts.numpy(), faces.numpy(), **kwargs)
Exemple #11
0
#!/usr/bin/env python3
# Visualization example with polyscope (pip install polyscope)
# https://polyscope.run/py/
import vedo
import polyscope

m = vedo.load(vedo.dataurl+'embryo.tif').isosurface().extractLargestRegion()
# m = vedo.load(vedo.dataurl+'man.vtk')

polyscope.set_program_name("vedo using polyscope")
polyscope.set_verbosity(0)
polyscope.set_up_dir("z_up")
polyscope.init()
ps_mesh = polyscope.register_surface_mesh('My vedo mesh',
                                          m.points(), m.faces(),
                                          color=[0.5,0,0],
                                          smooth_shade=True,
                                         )
ps_mesh.add_scalar_quantity("heights", m.points()[:,2], defined_on='vertices')
ps_mesh.set_material("wax") # wax, mud, jade, candy
polyscope.show()

vedo.show(m, axes=11)
Exemple #12
0
import open3d
import polyscope as ps
import numpy as np

path = "/Users/chandler/Downloads/shared_training_cloud_omg_dataset_234_geometry_triangle_mesh.ply"

mesh = open3d.io.read_triangle_mesh(path)
mesh.compute_triangle_normals()

ps.init()

ps.register_surface_mesh("my mesh",
                         np.array(mesh.vertices),
                         np.array(mesh.triangles),
                         smooth_shade=True)

ps.get_surface_mesh("my mesh").add_vector_quantity("normals",
                                                   np.array(
                                                       mesh.triangle_normals),
                                                   defined_on='faces',
                                                   color=(0.2, 0.5, 0.5))

ps.show()
Exemple #13
0
import sys

sys.path.append("src")

from geometry.smooth import curves
import numpy as np
import polyscope as ps
from thirdparty.octasphere import octasphere

ps.init()

verts, faces = octasphere(0, 1)

ps_mesh = ps.register_surface_mesh("name", verts, faces)

ps.show()
Exemple #14
0
path = "/Users/cbabraham/data/mesh/spot/spot_triangulated.obj"

# verts and faces
V,F = igl.read_triangle_mesh(path)

# cotan laplacian and mass matrix
L = igl.cotmatrix(V,F)
M = igl.massmatrix(V,F)

# eigenvectors of the laplacian
_, E = scipy.sparse.linalg.eigsh(L, 1000, M, which='BE')

C = V.T.dot(E) # inner product of each eigenvector and the mesh
R = np.einsum('km,nm->nk',C,E)

# V: n x k verts (k = 3)
# E: n x m eigenvectors, to use as bases
# C: k x m basis weights
# R: n x k synthesized vertices from weighted bases

ps.init()
original = ps.register_surface_mesh("mesh", V,F)

for i, eigenvector in enumerate(E.T):
	original.add_scalar_quantity(
		"eigenvector-{}".format(i),
		eigenvector.astype(np.float32), # 
		defined_on='vertices'
	)
test = ps.register_surface_mesh("m2", R.astype(np.float32), F)
ps.show()
Exemple #15
0
import numpy as np
from pygel3d import hmesh

m = hmesh.load("../cube.obj")
print(m.positions())

faces = m.faces()

## Old school
allfaces=[]
for f in faces:
 face=[]
 for v in m.circulate_face(f):
     face.append(v)
 allfaces = allfaces + [face]

## Fancy version
allfaces2 = [[v for v in m.circulate_face(f)] for f in faces]

polyscope.init()

#Display the vertices as point cloud
polyscope.register_point_cloud("data", m.positions())

#Display the vertices as mesh
polyscope.register_surface_mesh("data mesh", m.positions(), allfaces)
#Display the vertices as mesh
polyscope.register_surface_mesh("data mesh2", m.positions(), allfaces2)

polyscope.show()
Exemple #16
0
surface          = smooth_surfaces["plane"]
# uv               = surface.coordinates(100)
# smooth_points    = surface.f(uv)
# smooth_laplacian = surface.vector_laplacian(uv)


mesh = TriangleMesh.read('/Users/cbabraham/Downloads/Damaliscus Korrigum PLY/damaliscus_korrigum.ply')

# mesh               = TriangleMesh.from_surface(surface, 100)

compute_distance = GeodesicDistanceComputation(mesh.vs, np.array(mesh.faces))
distance_of_each_vertex_to_vertex_0 = compute_distance(62132)


ps.init()
ps.register_surface_mesh("mesh1", mesh.vs, mesh.faces)
ps.get_surface_mesh("mesh1").add_scalar_quantity("distance", 
    distance_of_each_vertex_to_vertex_0, defined_on='vertices', cmap='reds')
ps.show()

# # descrete
# mesh               = TriangleMesh.from_surface(surface, 100)
# discrete_laplacian = \
#     get_topological_laplacian(mesh.faces, mesh.vs).dot(mesh.vs)

# A = np.linalg.norm(smooth_laplacian,                 axis=1)
# B = np.linalg.norm(smooth_laplacian + smooth_points, axis=1)
# C = np.linalg.norm(discrete_laplacian,               axis=1)
# D = np.linalg.norm(mesh.vs - discrete_laplacian,       axis=1)

# ps.init()