Beispiel #1
0
def write_ply_file(points):
  '''
  Given a string of comma-separated values that represent 2d points, parse
  the string, create a ply file in the temp storage directory representing
  the shape formed by the string, and return the filepath.
  '''

  import voxel_globe.tools
  with voxel_globe.tools.storage_dir('event_trigger_ply') as ply_dir:
    num_files = len([name for name in os.listdir(ply_dir)])
    filepath = os.path.join(ply_dir, 'mesh_%d.ply' % num_files)
  
  import numpy
  import plyfile
  from plyfile import PlyData, PlyElement
  
  vertex = numpy.array(points,
                      dtype=[('x', 'double'), ('y', 'double'),
                             ('z', 'double')])
  face = numpy.array([(range(len(points)),)], [('vertex_indices', '|O')])
  el1 = PlyElement.describe(vertex, 'vertex')
  el2 = PlyElement.describe(face, 'face')
  
  PlyData([el1, el2], text=True, comments=['mesh-feature'], 
    obj_info=['a bmsh3d_mesh object']).write(filepath)

  return filepath
Beispiel #2
0
def tet_ply(text, byte_order):
    vertex = numpy.array([(0, 0, 0), (0, 1, 1), (1, 0, 1), (1, 1, 0)], dtype=[("x", "f4"), ("y", "f4"), ("z", "f4")])

    face = numpy.array(
        [([0, 1, 2], 255, 255, 255), ([0, 2, 3], 255, 0, 0), ([0, 1, 3], 0, 255, 0), ([1, 2, 3], 0, 0, 255)],
        dtype=[("vertex_indices", "i4", (3,)), ("red", "u1"), ("green", "u1"), ("blue", "u1")],
    )

    return PlyData(
        [PlyElement.describe(vertex, "vertex", comments=["tetrahedron vertices"]), PlyElement.describe(face, "face")],
        text=text,
        byte_order=byte_order,
        comments=["single tetrahedron with colored faces"],
    )
Beispiel #3
0
    def save_ply(self, filename):
        vertex = np.array([tuple(i) for i in self.v], dtype=[('x', 'f4'), ('y', 'f4'), ('z', 'f4')])
        face = np.array([(tuple(i), 0, 100, 255) for i in self.f] , 
            dtype=[('vertex_indices', 'i4', (3,)),
            ('red', 'u1'), ('green', 'u1'),
            ('blue', 'u1')])
	edge = np.array([(tuple(i)[0], tuple(i)[1], 255, 255, 255) for i in self.e] , 
            dtype=[('vertex1', 'i4'), ('vertex2', 'i4'),
            ('red', 'u1'), ('green', 'u1'),
            ('blue', 'u1')])
        el = PlyElement.describe(vertex, 'vertex')
        el2 = PlyElement.describe(face, 'face')
	el3 = PlyElement.describe(edge, 'edge')
        plydata = PlyData([el, el2, el3])
        plydata.write(filename)
Beispiel #4
0
def get_face_ele(vidx_per_face, face_label, extra):
    # face
    num_face = vidx_per_face.shape[0]
    vnpf = vidx_per_face.shape[1]
    assert vnpf > 2
    is_face_color = face_label is not None
    if is_face_color:
        if face_label is not None:
            if extra == 'label_color_default':
                face_label = np.squeeze(face_label)
                face_color = np.take(color_dic.rgb_order, face_label, 0)

        face = np.zeros(shape=(num_face)).astype(
            dtype=[('vertex_indices', 'i4',
                    (vnpf, )), ('red', 'u1'), ('green', 'u1'), ('blue', 'u1')])
    else:
        face = np.zeros(shape=(num_face)).astype(dtype=[('vertex_indices',
                                                         'i4', (vnpf, ))])

    for i in range(num_face):
        if is_face_color:
            face[i] = (vidx_per_face[i], face_color[i, 0], face_color[i, 1],
                       face_color[i, 2])
        else:
            face[i] = (vidx_per_face[i], )

    el_face = PlyElement.describe(face, 'face')
    return el_face
Beispiel #5
0
def visualize(args, preds):
    vertex = []
    for i in range(preds.shape[0]):
        vertex.append(
            (
                preds[i][0],
                preds[i][1],
                preds[i][2],
                preds[i][3],
                preds[i][4],
                preds[i][5],
            )
        )

    vertex = np.array(
        vertex,
        dtype=[
            ("x", np.dtype("float32")), 
            ("y", np.dtype("float32")), 
            ("z", np.dtype("float32")),
            ("red", np.dtype("uint8")),
            ("green", np.dtype("uint8")),
            ("blue", np.dtype("uint8"))
        ]
    )

    output_pc = PlyElement.describe(vertex, "vertex")
    output_pc = PlyData([output_pc])
    output_root = os.path.join(CONF.OUTPUT_ROOT, args.folder, "preds")
    os.makedirs(output_root, exist_ok=True)
    output_pc.write(os.path.join(output_root, "{}.ply".format(args.scene_id)))
Beispiel #6
0
def visualize(ids, mesh_file, output_file):
    if not output_file.endswith('.ply'):
        sys.stderr.write('ERROR: ' + 'output file must be a .ply file' + '\n')
    colors = NYU40_colors.create_color_palette_legend()
    num_colors = len(colors)
    with open(mesh_file, 'rb') as f:
        plydata = PlyData.read(f)
        vert = plydata['vertex'] #same as plydata.elements[0]
        faces = plydata.elements[1]
        num_verts = plydata['vertex'].count
        if num_verts != len(ids):
           sys.stderr.write('ERROR: ' + '#predicted labels = ' + str(len(ids)) + 'vs #mesh vertices = ' + str(num_verts))
        # *_vh_clean_2.ply has colors already, save label id instead of alpha value
        for i in range(num_verts):
            if ids[i]+1 >= num_colors:
               sys.stderr.write('ERROR: ' + 'found predicted label ' + str(ids[i]) + ' not in nyu40 label set')
            color = colors[ids[i]+1]
            plydata['vertex']['red'][i] = color[0]
            plydata['vertex']['green'][i] = color[1]
            plydata['vertex']['blue'][i] = color[2]
        #add the label field to the ply file
        (x, y, z, r, g, b, alpha) = (vert[t] for t in ('x', 'y', 'z', 'red', 'green', 'blue', 'alpha'))
        new_data = np.column_stack((x,y,z,r,g,b,ids))
        points_tuple = list([tuple(row) for row in new_data])
        new_points = np.core.records.fromrecords(points_tuple,
                                                 names='x,y,z,red,green,blue,label',
                                                 formats='f4,f4,f4,u1,u1,u1,u1')
        el = PlyElement.describe(new_points, 'vertex')
    PlyData([el, faces], text=False).write(output_file)
Beispiel #7
0
def merge_point_clouds(base_dir, out_path=None, adjustment=False):
    print('merging point clouds for dir {}'.format(base_dir))
    extrinsics_path = osp.join(base_dir, 'final_extrinsics.txt')
    extrinsics = get_extrinsics(extrinsics_path)
    all_pc = list()
    for location_i, extrinsic in enumerate(extrinsics):
        ply_path = osp.join(base_dir, 'derived/{}/output.ply'.format(location_i))
        transformed_pc = pc_transfrom(read_file=True, pc_file=ply_path, trans_matrix=extrinsic)
        all_pc.append(transformed_pc)

    all_pc = np.concatenate(all_pc, axis=0)

    adjustment_trans_mat = np.array([[1, 0, 0, 0],
                                     [0, 0, 1, 0],
                                     [0, -1, 0, 0],
                                     [0, 0, 0, 1]])

    all_pc = pc_transfrom(read_file=False, pc_file=all_pc, trans_matrix=adjustment_trans_mat, downsample=False)

    vertex_el = PlyElement.describe(all_pc, 'vertex')

    if not out_path:
        out_path = osp.join(base_dir, 'output_all.ply')
    PlyData([vertex_el]).write(out_path)  # write the new ply file
    print('write merged ply file to {}'.format(out_path))
Beispiel #8
0
def write_ply(points, filename, text=True):
    """ input: Nx3, write points to filename as PLY format. """
    points = [(points[i, 0], points[i, 1], points[i, 2])
              for i in range(points.shape[0])]
    vertex = np.array(points, dtype=[('x', 'f4'), ('y', 'f4'), ('z', 'f4')])
    el = PlyElement.describe(vertex, 'vertex', comments=['vertices'])
    PlyData([el], text=text).write(filename)
Beispiel #9
0
def save_ply_data(filename, vertex, face):
    """ save ply file, only vertices and faces """

    vertices = np.zeros(vertex.shape[0],
                        dtype=[('x', 'f4'), ('y', 'f4'), ('z', 'f4')])
    for i in range(vertex.shape[0]):
        vertices[i] = (vertex[i][0], vertex[i][1], vertex[i][2])

    faces = np.zeros(face.shape[0], dtype=[('vertex_indices', 'i4', (3, ))])
    for i in range(face.shape[0]):
        faces[i] = ([face[i][0], face[i][1], face[i][2]])

    e1 = PlyElement.describe(vertices, 'vertex')
    e2 = PlyElement.describe(faces, 'face')

    PlyData([e1, e2], text=True).write(filename)
Beispiel #10
0
def cli_main():
    parser = argparse.ArgumentParser(
        description=
        'Extract geometry from a trained model (only for learnable embeddings).'
    )
    parser.add_argument('--path', type=str, required=True)
    parser.add_argument('--output', type=str, required=True)
    parser.add_argument('--name', type=str, default='sparsevoxel')
    parser.add_argument('--user-dir', default='fairnr')
    args = options.parse_args_and_arch(parser)

    models, model_args, task = checkpoint_utils.load_model_ensemble_and_task(
        [args.path],
        suffix=getattr(args, "checkpoint_suffix", ""),
    )
    model = models[0]
    voxel_idx, voxel_pts = model.encoder.extract_voxels()

    # write to ply file.
    points = [(voxel_pts[k, 0], voxel_pts[k, 1], voxel_pts[k, 2], voxel_idx[k])
              for k in range(voxel_idx.size(0))]
    vertex = np.array(points,
                      dtype=[('x', 'f4'), ('y', 'f4'), ('z', 'f4'),
                             ('quality', 'f4')])
    PlyData([PlyElement.describe(vertex, 'vertex')],
            text=True).write(os.path.join(args.output, args.name + '.ply'))
def save_ply(scene,name):
    scene_ply=[]
    for elem in scene:
        scene_ply.append(tuple(elem))
    scene_ply=np.array(scene_ply,dtype=[('x','f4'),('y','f4'),('z','f4')])
    el=PlyElement.describe(scene_ply,'vertex',comments=[name])
    PlyData([el],text=True).write(name+'.ply')
Beispiel #12
0
def get_edge_ele(vidx_per_edge, edge_label, extra):
    # edge
    num_edge = vidx_per_edge.shape[0]
    vnpf = vidx_per_edge.shape[1]
    assert vnpf == 2
    is_edge_color = edge_label is not None
    if is_edge_color:
        if edge_label is not None:
            if extra == 'label_color_default':
                edge_label = np.squeeze(edge_label)
                edge_color = np.take(color_dic.rgb_order, edge_label, 0)

        edge = np.zeros(shape=(num_edge)).astype(
            dtype=[('vertex1', 'i4'), ('vertex2',
                                       'i4'), ('red',
                                               'u1'), ('green',
                                                       'u1'), ('blue', 'u1')])
    else:
        edge = np.zeros(shape=(num_edge)).astype(
            dtype=[('vertex1', 'i4'), ('vertex2', 'i4')])

    for i in range(num_edge):
        if is_edge_color:
            edge[i] = (vidx_per_edge[i][0], vidx_per_edge[i][1],
                       edge_color[i, 0], edge_color[i, 1], edge_color[i, 2])
        else:
            edge[i] = (vidx_per_edge[i][0], vidx_per_edge[i][1])

    el_edge = PlyElement.describe(edge, 'edge')
    return el_edge
Beispiel #13
0
def output_test_with_pc_and_views(
        v_predict_acc,
        v_predict_com,
        v_gt_acc,
        v_gt_com,
        v_smith_error,
        v_num_total_points,
        v_views,
        v_points,  # (x,y,z)
):
    # Write views
    vertexes = v_points[:, :3]

    vertexes_describer = PlyElement.describe(np.array(
        [(item[0], item[1], item[2], item[3], item[4], item[5], item[6], item[7]) for item in
         np.concatenate([vertexes,
                         v_predict_acc[:, np.newaxis],
                         v_predict_com[:, np.newaxis],
                         v_gt_acc[:, np.newaxis],
                         v_gt_com[:, np.newaxis],
                         v_smith_error[:, np.newaxis],
                         ], axis=1)],
        dtype=[('x', 'f8'), ('y', 'f8'), ('z', 'f8'),
               ('Predict_Recon', 'f8'), ('Predict_Gt', 'f8'),
               ('GT_Recon', 'f8'), ('GT_Gt', 'f8'),
               ('Smith_Recon', 'f8'),
               ]), 'vertex')

    PlyData([vertexes_describer]).write('temp/test_scene_output/whole_point.ply')

    # thread_map(write_views_to_txt_file, zip(v_views, v_points[:,3]), max_workers=16)
    process_map(write_views_to_txt_file, list(enumerate(v_views)), max_workers=10, chunksize=1)

    return
Beispiel #14
0
def write_ply_rgb_normal(points, colors, normals, filename, text=True):
    """ input: Nx3, Nx3, Nx3 write points and colors to filename as PLY format. """
    num_points = len(points)
    assert len(colors) == num_points

    points = [(points[i, 0], points[i, 1], points[i, 2])
              for i in range(points.shape[0])]
    colors = [(colors[i, 0], colors[i, 1], colors[i, 2])
              for i in range(colors.shape[0])]
    normals = [(normals[i, 0], normals[i, 1], normals[i, 2])
               for i in range(normals.shape[0])]
    vertex = np.array(points, dtype=[('x', 'f4'), ('y', 'f4'), ('z', 'f4')])
    color = np.array(colors,
                     dtype=[('red', 'u1'), ('green', 'u1'), ('blue', 'u1')])
    normal = np.array(normals,
                      dtype=[('nx', 'f4'), ('ny', 'f4'), ('nz', 'f4')])

    vertex_all = np.empty(
        num_points,
        vertex.dtype.descr + color.dtype.descr + normal.dtype.descr)

    for prop in vertex.dtype.names:
        vertex_all[prop] = vertex[prop]

    for prop in color.dtype.names:
        vertex_all[prop] = color[prop]

    for prop in normal.dtype.names:
        vertex_all[prop] = normal[prop]

    el = PlyElement.describe(vertex_all, 'vertex', comments=['vertices'])
    PlyData([el], text=text).write(filename)
def write_ply_color(points,
                    labels,
                    filename,
                    num_classes=None,
                    colormap=pyplot.cm.jet):
    """ Color (N,3) points with labels (N) within range 0 ~ num_classes-1 as OBJ file """
    labels = labels.astype(int)
    N = points.shape[0]
    if num_classes is None:
        num_classes = np.max(labels) + 1
    else:
        assert (num_classes > np.max(labels))

    vertex = []
    #colors = [pyplot.cm.jet(i/float(num_classes)) for i in range(num_classes)]
    colors = [colormap(i / float(num_classes)) for i in range(num_classes)]
    for i in range(N):
        c = colors[labels[i]]
        c = [int(x * 255) for x in c]
        vertex.append(
            (points[i, 0], points[i, 1], points[i, 2], c[0], c[1], c[2]))
    vertex = np.array(vertex,
                      dtype=[('x', 'f4'), ('y', 'f4'), ('z', 'f4'),
                             ('red', 'u1'), ('green', 'u1'), ('blue', 'u1')])

    el = PlyElement.describe(vertex, 'vertex', comments=['vertices'])
    PlyData([el], text=True).write(filename)
Beispiel #16
0
def embedding2ply(filename, xyz, embeddings):
    """write a ply with colors corresponding to geometric features"""

    if embeddings.shape[1] > 3:
        pca = PCA(n_components=3)
        #pca.fit(np.eye(embeddings.shape[1]))
        pca.fit(
            np.vstack((np.zeros(
                (embeddings.shape[1], )), np.eye(embeddings.shape[1]))))
        embeddings = pca.transform(embeddings)

    #value = (embeddings-embeddings.mean(axis=0))/(2*embeddings.std())+0.5
    #value = np.minimum(np.maximum(value,0),1)
    #value = (embeddings)/(3 * embeddings.std())+0.5
    value = np.minimum(np.maximum((embeddings + 1) / 2, 0), 1)

    color = np.array(255 * value, dtype='uint8')
    prop = [('x', 'f4'), ('y', 'f4'), ('z', 'f4'), ('red', 'u1'),
            ('green', 'u1'), ('blue', 'u1')]
    vertex_all = np.empty(len(xyz), dtype=prop)
    for i in range(0, 3):
        vertex_all[prop[i][0]] = xyz[:, i]
    for i in range(0, 3):
        vertex_all[prop[i + 3][0]] = color[:, i]
    ply = PlyData([PlyElement.describe(vertex_all, 'vertex')], text=True)

    ply.write(filename)
Beispiel #17
0
def error2ply(filename, xyz, rgb, labels, prediction):
    """write a ply with green hue for correct classifcation and red for error"""
    if len(prediction.shape) > 1 and prediction.shape[1] > 1:
        prediction = np.argmax(prediction, axis=1)
    if len(labels.shape) > 1 and labels.shape[1] > 1:
        labels = np.argmax(labels, axis=1)
    color_rgb = rgb / 255
    for i_ver in range(0, len(labels)):

        color_hsv = list(
            colorsys.rgb_to_hsv(color_rgb[i_ver, 0], color_rgb[i_ver, 1],
                                color_rgb[i_ver, 2]))
        if (labels[i_ver] == prediction[i_ver]) or (labels[i_ver] == 0):
            color_hsv[0] = 0.333333
        else:
            color_hsv[0] = 0
        color_hsv[1] = min(1, color_hsv[1] + 0.3)
        color_hsv[2] = min(1, color_hsv[2] + 0.1)
        color_rgb[i_ver, :] = list(
            colorsys.hsv_to_rgb(color_hsv[0], color_hsv[1], color_hsv[2]))
    color_rgb = np.array(color_rgb * 255, dtype='u1')
    prop = [('x', 'f4'), ('y', 'f4'), ('z', 'f4'), ('red', 'u1'),
            ('green', 'u1'), ('blue', 'u1')]
    vertex_all = np.empty(len(xyz), dtype=prop)
    for i in range(0, 3):
        vertex_all[prop[i][0]] = xyz[:, i]
    for i in range(0, 3):
        vertex_all[prop[i + 3][0]] = color_rgb[:, i]
    ply = PlyData([PlyElement.describe(vertex_all, 'vertex')], text=True)
    ply.write(filename)
Beispiel #18
0
def save_point_cloud(points_3d,
                     filename,
                     binary=True,
                     with_label=False,
                     verbose=True):
    """Save an RGB point cloud as a PLY file.

  Args:
    points_3d: Nx6 matrix where points_3d[:, :3] are the XYZ coordinates and points_3d[:, 4:] are
        the RGB values. If Nx3 matrix, save all points with [128, 128, 128] (gray) color.
  """
    assert points_3d.ndim == 2
    if with_label:
        assert points_3d.shape[1] == 7
        python_types = (float, float, float, int, int, int, int)
        npy_types = [('x', 'f4'), ('y', 'f4'), ('z', 'f4'), ('red', 'u1'),
                     ('green', 'u1'), ('blue', 'u1'), ('label', 'u1')]
    else:
        if points_3d.shape[1] == 3:
            gray_concat = np.tile(np.array([128], dtype=np.uint8),
                                  (points_3d.shape[0], 3))
            points_3d = np.hstack((points_3d, gray_concat))
        assert points_3d.shape[1] == 6
        python_types = (float, float, float, int, int, int)
        npy_types = [('x', 'f4'), ('y', 'f4'), ('z', 'f4'), ('red', 'u1'),
                     ('green', 'u1'), ('blue', 'u1')]
    if binary is True:
        # Format into NumPy structured array
        vertices = []
        for row_idx in range(points_3d.shape[0]):
            cur_point = points_3d[row_idx]
            vertices.append(
                tuple(
                    dtype(point)
                    for dtype, point in zip(python_types, cur_point)))
        vertices_array = np.array(vertices, dtype=npy_types)
        el = PlyElement.describe(vertices_array, 'vertex')

        # Write
        PlyData([el]).write(filename)
    else:
        # PlyData([el], text=True).write(filename)
        with open(filename, 'w') as f:
            f.write('ply\n'
                    'format ascii 1.0\n'
                    'element vertex %d\n'
                    'property float x\n'
                    'property float y\n'
                    'property float z\n'
                    'property uchar red\n'
                    'property uchar green\n'
                    'property uchar blue\n'
                    'property uchar alpha\n'
                    'end_header\n' % points_3d.shape[0])
            for row_idx in range(points_3d.shape[0]):
                X, Y, Z, R, G, B = points_3d[row_idx]
                f.write('%f %f %f %d %d %d 0\n' % (X, Y, Z, R, G, B))
    if verbose is True:
        print('Saved point cloud to: %s' % filename)
def export_ply(pc, filename):
    vertex = np.zeros(pc.shape[0],
                      dtype=[('x', 'f4'), ('y', 'f4'), ('z', 'f4')])
    for i in range(pc.shape[0]):
        vertex[i] = (pc[i][0], pc[i][1], pc[i][2])
    ply_out = PlyData(
        [PlyElement.describe(vertex, 'vertex', comments=['vertices'])])
    ply_out.write(filename)
def savePointCloudToPly(pt_3d, pt_c, filename):
    pt_3d_c = np.concatenate((pt_3d, pt_c), axis=1)
    pt_3d_ct = [tuple(l) for l in pt_3d_c.tolist()]
    pt_3d_ct = np.array(pt_3d_ct,
                        dtype=[('x', 'f4'), ('y', 'f4'), ('z', 'f4'),
                               ('red', 'u1'), ('green', 'u1'), ('blue', 'u1')])
    el = PlyElement.describe(pt_3d_ct, 'vertex')
    PlyData([el], text=True).write(filename)
Beispiel #21
0
def write_color_ply(points, filename, text=True):
    points = [(points[i, 0], points[i, 1], points[i, 2], points[i, 3],
               points[i, 4], points[i, 5]) for i in range(points.shape[0])]
    vertex = np.array(points,
                      dtype=[('x', 'f4'), ('y', 'f4'), ('z', 'f4'),
                             ('red', 'u1'), ('green', 'u1'), ('blue', 'u1')])
    el = PlyElement.describe(vertex, 'vertex', comments=['vertices'])
    PlyData([el], text=text).write(filename)
Beispiel #22
0
 def write_ply(self, coords, normals, path):
     l = []
     size = coords.shape[0]
     for i in range(0, size):
         l.append((coords[i][0], coords[i][1], coords[i][2], normals[i][0], normals[i][1], normals[i][2]))
     vertex = np.array(l, dtype=[('x', 'f4'),('y', 'f4'),('z', 'f4'),('nx','f4'),('ny','f4'),('nz','f4')])
     el = PlyElement.describe(vertex, 'vertex')
     PlyData([el]).write(path)
def export_ply(pc, filename):
    vertex = np.zeros(pc.shape[0],
                      dtype=[("x", "f4"), ("y", "f4"), ("z", "f4")])
    for i in range(pc.shape[0]):
        vertex[i] = (pc[i][0], pc[i][1], pc[i][2])
    ply_out = PlyData(
        [PlyElement.describe(vertex, "vertex", comments=["vertices"])])
    ply_out.write(filename)
Beispiel #24
0
def export_pointcloud(vertices, out_file, as_text=True):
    assert (vertices.shape[1] == 3)
    vertices = vertices.astype(np.float32)
    vector_dtype = [('x', 'f4'), ('y', 'f4'), ('z', 'f4')]
    vertices = vertices.view(dtype=vector_dtype).flatten()
    plyel = PlyElement.describe(vertices, 'vertex')
    plydata = PlyData([plyel], text=as_text)
    plydata.write(out_file)
def tet_ply(text, byte_order):
    vertex = numpy.array([(0, 0, 0), (0, 1, 1), (1, 0, 1), (1, 1, 0)],
                         dtype=[('x', 'f4'), ('y', 'f4'), ('z', 'f4')])

    face = numpy.array([([0, 1, 2], 255, 255, 255), ([0, 2, 3], 255, 0, 0),
                        ([0, 1, 3], 0, 255, 0), ([1, 2, 3], 0, 0, 255)],
                       dtype=[('vertex_indices', 'i4', (3, )), ('red', 'u1'),
                              ('green', 'u1'), ('blue', 'u1')])

    return PlyData([
        PlyElement.describe(
            vertex, 'vertex', comments=['tetrahedron vertices']),
        PlyElement.describe(face, 'face')
    ],
                   text=text,
                   byte_order=byte_order,
                   comments=['single tetrahedron with colored faces'])
Beispiel #26
0
def write_ply(points, filename, text=True):
    """ input: Nx3, write points to filename as PLY format. """
    points = [
        (points[i, 0], points[i, 1], points[i, 2]) for i in range(points.shape[0])
    ]
    vertex = np.array(points, dtype=[("x", "f4"), ("y", "f4"), ("z", "f4")])
    el = PlyElement.describe(vertex, "vertex", comments=["vertices"])
    PlyData([el], text=text).write(filename)
Beispiel #27
0
def spg2ply(filename, spg_graph):
    """write a ply displaying the SPG by adding edges between its centroid"""
    vertex_prop = [('x', 'f4'), ('y', 'f4'), ('z', 'f4')]
    vertex_val = np.empty((spg_graph['sp_centroids']).shape[0],
                          dtype=vertex_prop)
    for i in range(0, 3):
        vertex_val[vertex_prop[i][0]] = spg_graph['sp_centroids'][:, i]
    edges_prop = [('vertex1', 'int32'), ('vertex2', 'int32')]
    edges_val = np.empty((spg_graph['source']).shape[0], dtype=edges_prop)
    edges_val[edges_prop[0][0]] = spg_graph['source'].flatten()
    edges_val[edges_prop[1][0]] = spg_graph['target'].flatten()
    ply = PlyData([
        PlyElement.describe(vertex_val, 'vertex'),
        PlyElement.describe(edges_val, 'edge')
    ],
                  text=True)
    ply.write(filename)
Beispiel #28
0
def save_ply_file(data, filename):
    vertex = np.zeros(data.shape[0],
                      dtype=[('x', 'f4'), ('y', 'f4'), ('z', 'f4')])
    for i in range(data.shape[0]):
        vertex[i] = (data[i][0], data[i][1], data[i][2])
    ply_out = PlyData(
        [PlyElement.describe(vertex, 'vertex', comments=['vertices'])])
    ply_out.write(filename)
Beispiel #29
0
def to_ply_file(np_array):
	data = np_array.transpose(2, 0, 1).reshape(3, -1).transpose(1, 0)

	ply_faces = np.empty(len(data), dtype=[('vertex_indices', 'i4', (3,))])
	ply_faces['vertex_indices'] = data

	el = PlyElement.describe(ply_faces, 'sink')
	PlyData([el]).write('sink_point_cloud.ply')
def write_ply_with_color(fname, coords, colors):
    from plyfile import PlyData, PlyElement
    vertex = np.array([
        tuple(list(vert) + list(col[:3])) for vert, col in zip(coords, colors)
    ],
                      dtype=[('x', 'f4'), ('y', 'f4'), ('z', 'f4'),
                             ('red', 'u1'), ('green', 'u1'), ('blue', 'u1')])
    el = PlyElement.describe(vertex, 'vertex')
    PlyData([el]).write(fname)
Beispiel #31
0
 def build_ply_single_triangle(self):
     from plyfile import PlyElement, PlyData
     points_dt = np.dtype([('x', float), ('y', float), ('z', float)])
     face_dt = np.dtype([('vertex_indices', np.uint32, (3, ))])
     # The numpy array data.
     points = np.array([(-1, 0, 0), (0, 1, 0), (1, 0, 0)], dtype=points_dt)
     triangles = np.array([
         ((1, 0, 2), ),
     ], dtype=face_dt)  #The Tuple with a tuple inside is required Why !?!
     elements = [
         PlyElement.describe(points, "vertex"),
         PlyElement.describe(triangles,
                             "face",
                             len_types={'vertex_indices': 'u1'},
                             val_types={'vertex_indices': 'u4'}),
     ]
     ply_data = PlyData(elements, text=True)
     return ply_data
Beispiel #32
0
def save_sparseCubes_2ply(vxl_mask_list, vxl_ijk_list, rgb_list, \
        param, ply_filePath, normal_list=None):
    """
    save sparse cube to ply file

    ---------
    inputs:
        vxl_mask_list[i]: np.bool (iN_voxels,)
        vxl_ijk_list[i]: np.uint8 (iN_voxels, 3)
        rgb_list[i]: np.uint8 (iN_voxels, 3)
        normal_list[i]: np.float16 (iN_voxels, 3)

        param: np.float32(N_nonempty_cubes, 4)
        ply_filePath: 'xxx.ply'
    outputs:
        save to .ply file
    """
    vxl_mask_np = np.concatenate(vxl_mask_list, axis=0)
    N_voxels = vxl_mask_np.sum()
    vxl_ijk_np = np.vstack(vxl_ijk_list)
    rgb_np = np.vstack(rgb_list)
    if not vxl_mask_np.shape[0] == vxl_ijk_np.shape[0] == rgb_np.shape[0]:
        raise Warning('make sure # of voxels in each cube are consistent.')
    if normal_list is None:
        dt = np.dtype([('x', '<f4'), ('y', '<f4'), ('z', '<f4'), \
                ('red', 'u1'), ('green', 'u1'), ('blue', 'u1')])
    else:
        dt = np.dtype([('x', '<f4'), ('y', '<f4'), ('z', '<f4'), \
                ('nx', '<f4'), ('ny', '<f4'), ('nz', '<f4'), \
                ('red', 'u1'), ('green', 'u1'), ('blue', 'u1')])
        normal_np = np.vstack(normal_list)[vxl_mask_np]
    saved_pts = np.zeros(shape=(N_voxels, ), dtype=dt)

    # calculate voxels' xyz
    xyz_list = []
    for _cube, _select in enumerate(vxl_mask_list):
        resol = param[_cube]['resol']
        xyz_list.append(vxl_ijk_list[_cube][_select] * resol +
                        param[_cube]['xyz'][None, :])  # (iN, 3) + (1, 3)
    xyz_np = np.vstack(xyz_list)
    vxl_ijk_np, rgb_np = vxl_ijk_np[vxl_mask_np], rgb_np[vxl_mask_np]

    saved_pts['x'], saved_pts['y'], saved_pts[
        'z'] = xyz_np[:, 0], xyz_np[:, 1], xyz_np[:, 2]
    saved_pts['red'], saved_pts['green'], saved_pts[
        'blue'] = rgb_np[:, 0], rgb_np[:, 1], rgb_np[:, 2]
    if normal_list is not None:
        saved_pts['nx'], saved_pts['ny'], saved_pts[
            'nz'] = normal_np[:, 0], normal_np[:, 1], normal_np[:, 2]

    el_vertex = PlyElement.describe(saved_pts, 'vertex')
    outputFolder = '/'.join(ply_filePath.split('/')[:-1])
    if not os.path.exists(outputFolder):
        os.makedirs(outputFolder)
    PlyData([el_vertex]).write(ply_filePath)
    print('saved ply file: {}'.format(ply_filePath))
    return 1
def export_ply_points_faces(pc, fc, filename):
    vertex = np.zeros(pc.shape[0],
                      dtype=[('x', 'f4'), ('y', 'f4'), ('z', 'f4')])
    face = np.zeros(fc.shape[0], dtype=[('vertex_indices', 'i4', (3, ))])

    for i in range(pc.shape[0]):
        vertex[i] = (pc[i][0], pc[i][1], pc[i][2])

    for i in range(fc.shape[0]):
        face[i] = (fc[i, 1], fc[i, 2], fc[i, 3])

    ply_out = PlyData([
        PlyElement.describe(vertex, 'vertex', comments=['vertices']),
        PlyElement.describe(face, 'face', comments=['faces'])
    ])

    ply_filename = filename[:-4] + '.ply'
    ply_out.write(ply_filename)
Beispiel #34
0
def write_ply_rgb(points, colors, filename, text=True):
    """ input: Nx3, write points to filename as PLY format. """
    points = [(points[i, 0], points[i, 1], points[i, 2], colors[i, 0],
               colors[i, 1], colors[i, 2]) for i in range(points.shape[0])]
    vertex = np.array(points,
                      dtype=[('x', 'f4'), ('y', 'f4'), ('z', 'f4'),
                             ('red', 'u1'), ('green', 'u1'), ('blue', 'u1')])
    el = PlyElement.describe(vertex, 'vertex')
    PlyData([el], text=text).write(filename)
Beispiel #35
0
def test_assign_elements(tet_ply_txt):
    test = PlyElement.describe(numpy.zeros(1, dtype=[("a", "i4")]), "test")
    tet_ply_txt.elements = [test]
    assert len(tet_ply_txt.elements) == 1
    assert len(tet_ply_txt) == 1
    assert "vertex" not in tet_ply_txt
    assert "face" not in tet_ply_txt
    assert "test" in tet_ply_txt

    for (k, elt) in enumerate(tet_ply_txt):
        assert elt.name == "test"
        assert k == 0
def CreatePLY(P1,parameters,filename,surf_type,originalPLYfile):
        x1 = P1[:,0]
        y1 = P1[:,1]
        z1 = P1[:,2]

        if surf_type == 'lin':
            z1new = np.hstack((x1,y1,np.matrix(np.ones(np.size(P1,0))).T))*parameters
        else:
            z1new = np.hstack((np.multiply(x1,x1),np.multiply(y1,y1), np.multiply(x1,y1), x1, y1,np.matrix(np.ones(np.size(P1,0))).T))*parameters

        originalPLY = PLYLoader(originalPLYfile)
        point_cloud = (originalPLY.get_points()).T
        color_matrix = (originalPLY.get_colors()).T
        normals_matrix = (originalPLY.get_normals()).T

        x1 = np.array(x1)
        y1 = np.array(y1)
        z1new = np.array(z1new)

        x = point_cloud[:,0]
    	y = point_cloud[:,1]
    	z = point_cloud[:,2]

    	new_pointcloud = np.zeros((np.size(P1,0), 9))
    	pdb.set_trace()
    	
    	for Prow in range(len(x1)):
    		print 'Prow = ' 
    		print Prow
    		for originalPLY_row in range(len(x)):
    			if ((x[originalPLY_row] == x1[Prow]) and (y[originalPLY_row] == y1[Prow])):
    				print 'found'
    				print Prow
    				#point_cloud[originalPLY_row,2] = z1new[Prow]
    				new_pointcloud[Prow,0:2] = (P1[Prow,0:2])
    				new_pointcloud[Prow,2] = (z1new[Prow])
    				new_pointcloud[Prow,3:6] = (normals_matrix[originalPLY_row,:])
    				new_pointcloud[Prow,6:9] = color_matrix[originalPLY_row,:]

    				


    	pdb.set_trace()
    	#vertex = point_cloud

    	#new_mat = np.concatenate((vertex,normals_matrix,color_matrix),axis=1)
    	new_mat = new_pointcloud
    	vertex_new = map(tuple, new_mat)
    	vertex_new = np.array(vertex_new, dtype=[('x', 'f4'), ('y', 'f4'),('z', 'f4'),('nx','f4'),('ny','f4'),('nz','f4'),('diffuse_red', 'u1'), ('diffuse_green', 'u1'),('diffuse_blue', 'u1')])
    	el = PlyElement.describe(vertex_new, 'vertex')

    	
    	PlyData([el], text=True).write(filename)
Beispiel #37
0
def test_list_property_type(tmpdir, np_type):
    a = numpy.array([([0],), ([1, 2, 3],)], dtype=[("x", object)])

    ply0 = PlyData([PlyElement.describe(a, "test", val_types={"x": np_type})])

    assert ply0.elements[0].name == "test"
    assert ply0.elements[0].properties[0].name == "x"
    assert ply0.elements[0].properties[0].val_dtype == np_type

    ply1 = write_read(ply0, tmpdir)

    assert ply1.elements[0].name == "test"
    assert ply1.elements[0].data[0]["x"].dtype == numpy.dtype(np_type)
    verify(ply0, ply1)
Beispiel #38
0
def test_property_type(tmpdir, np_type):
    dtype = [("x", np_type), ("y", np_type), ("z", np_type)]
    a = numpy.array([(1, 2, 3), (4, 5, 6)], dtype=dtype)

    ply0 = PlyData([PlyElement.describe(a, "test")])

    assert ply0.elements[0].name == "test"
    assert ply0.elements[0].properties[0].name == "x"
    assert ply0.elements[0].properties[0].val_dtype == np_type
    assert ply0.elements[0].properties[1].name == "y"
    assert ply0.elements[0].properties[1].val_dtype == np_type
    assert ply0.elements[0].properties[2].name == "z"
    assert ply0.elements[0].properties[2].val_dtype == np_type

    ply1 = write_read(ply0, tmpdir)

    assert ply1.elements[0].name == "test"
    assert ply1.elements[0].data.dtype == dtype
    verify(ply0, ply1)
Beispiel #39
0
def write_ply(points, filename, text=True):
    """ input: Nx3, write points to filename as PLY format. """
    points = [(points[i,0], points[i,1], points[i,2]) for i in range(points.shape[0])]
    vertex = np.array(points, dtype=[('x', 'f4'), ('y', 'f4'),('z', 'f4')])
    el = PlyElement.describe(vertex, 'vertex', comments=['vertices'])
    PlyData([el], text=text).write(filename)
Beispiel #40
0
        vertex_normal[index3] = np.add(vertex_normal[index3], normal2)

    # 3, normalize vector
    for i in range(0, vertex_number):
        vertex_normal[i] = normalize(vertex_normal[i])

    # 4, write to ply
    result = []
    # The properties are 'x, y, z, nx, ny, nz'
    for i in range(0, vertex_number):
        record = (
            plyData["vertex"][i][0],
            plyData["vertex"][i][1],
            plyData["vertex"][i][2],
            vertex_normal[i][0],
            vertex_normal[i][1],
            vertex_normal[i][2],
        )
        result.append(record)

    # print(result)
    new_vertex_data = np.array(
        result,
        dtype=[("x", "float32"), ("y", "float32"), ("z", "float32"), ("nx", "float"), ("ny", "float"), ("nz", "float")],
    )
    elv = PlyElement.describe(new_vertex_data, "vertex")
    PlyData([elv]).write("p1.ply")

    # 5, write to txt
    np.savetxt("test.txt", new_vertex_data, delimiter=" ", fmt="%.6f")
Beispiel #41
0
    return face


if __name__ == "__main__":
    # load image
    img0 = np.array(Image.open("gargoyle/disp1.pgm"), dtype="int64")
    img1 = np.array(Image.open("gargoyle/view3.png"))
    img2 = np.array(Image.open("gargoyle/disp5.pgm"), dtype="int64")

    # construct 3d coordinates
    coords3d = []
    coords3d1 = construct3D(img0, 1)
    coords3d2 = construct3D(img2, 5)
    coords3d = coords3d1 + coords3d2

    # get disp map
    #    depth_map = project_nearest(coords3d)
    disp_map = project_nearest(coords3d)
    #    scipy.misc.imsave('disp3.png', disp_map)

    # connect
    raw_face = generate_mesh(disp_map)
    raw_vertex = construct_and_texture(disp_map, img1, 3)
    vertex = np.array(
        raw_vertex, dtype=[("x", "f4"), ("y", "f4"), ("z", "f4"), ("red", "u1"), ("green", "u1"), ("blue", "u1")]
    )
    face = np.array(raw_face, dtype=[("vertex_indices", "int32", (3,))])
    elv = PlyElement.describe(vertex, "vertex")
    elf = PlyElement.describe(face, "face")
    PlyData([elv, elf]).write("mesh.ply")
    '''


programSV3DRegion = glumpy_setting.ProgramSV3DRegion(data=data, name=None, point_size=1, anchor=anchor_matrix_whole)
gpyWindow.add_program(programSV3DRegion)

print(data['a_position'][0, 0, 0])
print(np.isnan(data['a_position'][0, 0, 0]))
print(~np.isnan(data['a_position'][0, 0, 0]))
data['a_color'] *= 255
data['a_color'].astype(int)

data = data[np.nonzero(~np.isnan(data['a_position'][:, :, 0]))]
xyzzz = np.zeros(len(data), dtype=[('x', 'f4'), ('y', 'f4'), ('z', 'f4'), ('r', 'f4'), ('g', 'f4'), ('b', 'f4')])
xyzzz['x'] = data['a_position'][:, 0]
xyzzz['y'] = data['a_position'][:, 1]
xyzzz['z'] = data['a_position'][:, 2]
xyzzz['r'] = data['a_color'][:, 0]
xyzzz['g'] = data['a_color'][:, 1]
xyzzz['b'] = data['a_color'][:, 2]
el = PlyElement.describe(xyzzz, 'vertex')

PlyData([el]).write('some_binary.ply')
PlyData([el], text=True).write('some_ascii.ply')

programAxis = glumpy_setting.ProgramAxis(line_length=5)
gpyWindow.add_program(programAxis)

gpyWindow.run()

Beispiel #43
0
    def match(self, perceivedpnts, perceivednormals, ddist = 5.0, dangle = 30.0):
        """
        do a match
        :return: rotmats of top matches

        author: weiwei
        date: 20170802
        """

        # save txt
        pverts = np.array([tuple(x) for x in perceivedpnts], dtype=[('x', 'f4'),('y', 'f4'),('z', 'f4')])
        el = PlyElement.describe(pverts, 'vertex')
        PlyData([el], text = True).write('perceivedpnts.ply')
        # save txt
        vandnarray = []
        for i in range(len(self.temppnts)):
            v = self.temppnts[i]
            n = self.tempnormals[i]
            vandn = (v[0],v[1],v[2],n[0],n[1],n[2])
            vandnarray.append(vandn)
        pverts = np.array(vandnarray, dtype=[('x', 'f4'),('y', 'f4'),('z', 'f4'),\
                                                                    ('nx','f4'),('ny','f4'),('nz','f4')])
        el = PlyElement.describe(pverts, 'vertex')
        PlyData([el], text = True).write('ttube.ply')

        accspace = {}
        # get the preceived global model descriptor
        nperceivedpnts = perceivedpnts.shape[0]
        i = np.argmax(perceivedpnts, axis = 0)[2]
        for j in range(0,nperceivedpnts):
            print j, nperceivedpnts
            m_0 = np.asarray(perceivedpnts[i])
            m_1 = np.asarray(perceivedpnts[j])
            v_m0m1 = m_0-m_1
            v_m1m0 = m_1-m_0
            n_m0 = perceivednormals[i]
            n_m1 = perceivednormals[j]
            # f1, namely ||d||2
            f1 = np.linalg.norm(m_0-m_1)
            # f2, namely angle between n_m0 and v_m1m0
            f2 = rm.degree_between(n_m0, v_m1m0)
            # f3, namely angle between n_m1 and v_m0m1
            f3 = rm.degree_between(n_m1, v_m0m1)
            # f4, namely angle between n_m0 and n_m1
            f4 = rm.degree_between(n_m0, n_m1)
            # discretize the values
            try:
                f1d = int(math.floor(f1/ddist)*ddist+ddist)
                f2d = int(math.floor(f2/dangle)*dangle+dangle)
                f3d = int(math.floor(f3/dangle)*dangle+dangle)
                f4d = int(math.floor(f4/dangle)*dangle+dangle)
            except:
                continue
            key = (f1d, f2d, f3d, f4d)
            # angle between n_m0 and x+
            xplus = np.asarray([1,0,0])
            yplus = np.asarray([0,1,0])
            nm0xangle = math.degrees(rm.radian_between(n_m0, xplus))
            rotax = np.cross(xplus, n_m0)
            if np.isnan(rotax).any() or not rotax.any():
                continue
            rotmat = rm.rodrigues(rotax, nm0xangle)
            v_m1m0onxplus = np.dot(v_m1m0, rotmat)
            v_m1m0onxplusyzproj = np.asarray([0, v_m1m0onxplus[1], v_m1m0onxplus[2]])
            alpha_m0 = rm.radian_between(v_m1m0onxplusyzproj, yplus)
            if v_m1m0onxplus[2] < 0:
                alpha_m0 = 2*math.pi - alpha_m0
            if key in self.gmd.keys():
                malist = self.gmd[key]
                print len(malist)
                for maslot in malist:
                    alpha = math.degrees(alpha_m0-maslot[2])
                    try:
                        alphadiscrete = int(math.floor(alpha/dangle)*dangle+dangle)
                    except:
                        continue
                    acckey = (maslot[0], alphadiscrete)
                    if acckey in accspace.keys():
                        accspace[acckey] += 1
                    else:
                        accspace[acckey] = 1
        if len(accspace.keys()) is 0:
            return (None, None)
        # find top matches and rot matrices
        maxn = sorted(accspace.iteritems(), key=operator.itemgetter(1), reverse=True)[:5]
        rotmat4list = []
        silist = []
        milist = []
        for maxnele in maxn:
            mi, alpha = maxnele[0]
            # step1 move to temppnts[mi]
            displacement0 = -self.temppnts[mi]
            rotmat4_0 = Mat4.translateMat(displacement0[0], displacement0[1], displacement0[2])
            # step2 rotate to goal
            normalangle = math.degrees(rm.radian_between(self.tempnormals[mi], perceivednormals[i]))
            normalrotax = np.cross(self.tempnormals[mi], perceivednormals[i])
            normalrotmat = rm.rodrigues(normalrotax, normalangle)
            anglerotmat = rm.rodrigues(perceivednormals[i], -alpha)
            rotmat = np.dot(anglerotmat, normalrotmat)
            rotmat4_1 = pg.npToMat4(rotmat)
            # step3 move to perceivedpnts[i]
            displacement1 = perceivedpnts[i]
            rotmat4_2 = Mat4.translateMat(displacement1[0], displacement1[1], displacement1[2])
            rotmat4 = rotmat4_0*rotmat4_1*rotmat4_2
            rotmat4list.append(rotmat4)
            silist.append(i)
            milist.append(mi)

        return rotmat4list, silist, milist
        if createSV and needOutput:
            data = programSV3DRegion.data
            data['a_color'] *= 255
            data['a_color'].astype(int)

            dataPLY = np.zeros(len(data), dtype=[('x', 'f4'), ('y', 'f4'), ('z', 'f4'), ('red', 'u1'), ('green', 'u1'),
                                                 ('blue', 'u1')])
            dataPLY['x'] = data['a_position'][:, 0]
            dataPLY['y'] = data['a_position'][:, 1]
            dataPLY['z'] = data['a_position'][:, 2]

            dataPLY['red'] = data['a_color'][:, 0]
            dataPLY['green'] = data['a_color'][:, 1]
            dataPLY['blue'] = data['a_color'][:, 2]

            el = PlyElement.describe(dataPLY, 'vertex')

            # PlyData([el], text=True).write('137_4_ascii.ply')
            PlyData([el]).write('/home/andy/src/ply/' + fileID + '_non_ground_binary.ply')

            if needGround:
                dataGnd = programSV3DRegion.data
                dataGnd['a_color'] *= 255
                dataGnd['a_color'].astype(int)

                dataPLY = np.zeros(len(dataGnd), dtype=[('x', 'f4'), ('y', 'f4'), ('z', 'f4'),
                                                        ('red', 'u1'), ('green', 'u1'), ('blue', 'u1')])
                dataPLY['x'] = dataGnd['a_position'][:, 0]
                dataPLY['y'] = dataGnd['a_position'][:, 1]
                dataPLY['z'] = dataGnd['a_position'][:, 2]
Beispiel #45
0
def export_ply(pc, filename):
    vertex = np.zeros(pc.shape[0], dtype=[('x', 'f4'), ('y', 'f4'), ('z', 'f4')])
    for i in range(pc.shape[0]):
        vertex[i] = (pc[i][0], pc[i][1], pc[i][2])
    ply_out = PlyData([PlyElement.describe(vertex, 'vertex', comments=['vertices'])])
    ply_out.write(filename)
Beispiel #46
0
"""
ALL PLY EXPORT IN HERE
"""

data = programSV3DRegion.data
data['a_color'] *= 255
data['a_color'].astype(int)

xyzzz = np.zeros(len(data), dtype=[('x', 'f4'), ('y', 'f4'), ('z', 'f4'), ('red', 'u1'), ('green', 'u1'), ('blue', 'u1')])
xyzzz['x'] = data['a_position'][:, 0]
xyzzz['y'] = data['a_position'][:, 1]
xyzzz['z'] = data['a_position'][:, 2]
xyzzz['red'] = data['a_color'][:, 0]
xyzzz['green'] = data['a_color'][:, 1]
xyzzz['blue'] = data['a_color'][:, 2]
el = PlyElement.describe(xyzzz, 'vertex')

face = np.zeros(len(tri), dtype=[('vertex_indices', 'i4', 3)])
face['vertex_indices'] = tri
tri = PlyElement.describe(face, 'face')

PlyData([el, tri], text=True).write('some_ascii.ply')
#PlyData([el]).write('over_simple_binary.ply')
"""
ALL PLY EXPORT IN HERE
"""

programAxis = glumpy_setting.ProgramAxis(line_length=5)
gpyWindow.add_program(programAxis)

gpyWindow.run()
Beispiel #47
0
    def getMakerXYZ(self, markercolumnid, markerrowid, markerdepth):
        if markerdepth > 0:
            point3d = self.kinect.mapper().MapDepthPointToCameraSpace(
                PyKinectV2._DepthSpacePoint(ctypes.c_float(markercolumnid), ctypes.c_float(markerrowid)),
                ctypes.c_ushort(markerdepth))
            return [point3d.x, point3d.y, point3d.z]
        else:
            return []

if __name__=='__main__':

    base = pandactrl.World(camp=[0,0,3000], lookatp=[0,0,0])

    kapi = KinectAPI()
    # points
    pntclds = kapi.getPointCloud()

    colors = []
    color = [np.random.rand(), np.random.rand(), np.random.rand(), np.random.rand()]
    for pnt in pntclds:
        colors.append(color)
    pntsnp = pg.genPntsnp(pntclds, colors=colors)
    pntsnp.reparentTo(base.render)

    from plyfile import PlyData, PlyElement
    verts = np.array(pntclds, dtype=[('x', 'f4'), ('y', 'f4'), ('z', 'f4')])
    el = PlyElement.describe(verts, 'vertex')
    PlyData([el], text=True).write('pythoncloud.ply')

    base.run()
Beispiel #48
0
                      (0, 1, 1),
                      (1, 0, 1),
                      (1, 1, 0)],
                     dtype=[('x', 'f4'), ('y', 'f4'), ('z', 'f4')])

face = numpy.array([([0, 1, 2], 255, 255, 255),
                    ([0, 2, 3], 255,   0,   0),
                    ([0, 1, 3],   0, 255,   0),
                    ([1, 2, 3],   0,   0, 255)],
                   dtype=[('vertex_indices', 'i4', (3,)),
                          ('red', 'u1'), ('green', 'u1'),
                          ('blue', 'u1')])


print "Assembling initial PlyData instance..."
ply0 = PlyData([PlyElement.describe(vertex, 'vertex',
                                    comments=['tetrahedron vertices']),
                PlyElement.describe(face, 'face')],
               text=True,
               comments=['single tetrahedron with colored faces'])

print "Writing test0.ply (ascii)..."
ply0.write('test0.ply')

print "Reading test0.ply..."
ply1 = PlyData.read('test0.ply')

print "(verifying result...)"
verify(ply0, ply1)

print "Writing test1.ply (binary_little_endian)..."
ply1.text = False