-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathreadPly.h
118 lines (90 loc) · 3.83 KB
/
readPly.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
#ifndef _READPLY_H
#define _READPLY_H
#include <vector>
#include <cstring>
#include "object.h"
#include "libs/ply.h"
typedef struct Vertex {
float x,y,z; /* the usual 3-space position of a vertex */
} Vertex;
typedef struct Face {
unsigned char intensity; /* this user attaches intensity to faces */
unsigned char nverts; /* number of vertex indices in list */
int *verts; /* vertex index list */
} Face;
PlyProperty vert_props[] = { /* list of property information for a vertex */
{(char*) "x", PLY_FLOAT, PLY_FLOAT, offsetof(Vertex,x), 0, 0, 0, 0},
{(char*) "y", PLY_FLOAT, PLY_FLOAT, offsetof(Vertex,y), 0, 0, 0, 0},
{(char*) "z", PLY_FLOAT, PLY_FLOAT, offsetof(Vertex,z), 0, 0, 0, 0},
};
PlyProperty face_props[] = { /* list of property information for a vertex */
//{(char*) "intensity", PLY_UCHAR, PLY_UCHAR, offsetof(Face,intensity), 0, 0, 0, 0},
{(char*) "vertex_indices", PLY_INT, PLY_INT, offsetof(Face,verts),
1, PLY_UCHAR, PLY_UCHAR, offsetof(Face,nverts)},
};
std::vector<Triangle> readPlyFile(std::string filename, Color col) {
std::vector<Triangle> listOfTriangles;
// We need a char* for the ply.h lib
char *charFilename = new char[filename.length()+1];
std::strcpy(charFilename, filename.c_str());
PlyFile *ply;
int nelems;
char **elist;
int file_type;
float version;
char *elem_name;
int nprops;
int num_elems;
Vertex **vlist;
Face **flist;
/* open a PLY file for reading */
ply = ply_open_for_reading(charFilename, &nelems, &elist, &file_type, &version);
/* go through each kind of element that we learned is in the file */
/* and read them */
for (int i = 0; i < nelems; ++i) {
/* get the description of the first element */
elem_name = elist[i];
ply_get_element_description (ply, elem_name, &num_elems, &nprops);
/* if we're on vertex elements, read them in */
if (!std::strcmp("vertex", elem_name)) {
/* create a vertex list to hold all the vertices */
vlist = (Vertex **) malloc (sizeof (Vertex *) * num_elems);
/* set up for getting vertex elements */
ply_get_property (ply, elem_name, &vert_props[0]);
ply_get_property (ply, elem_name, &vert_props[1]);
ply_get_property (ply, elem_name, &vert_props[2]);
/* grab all the vertex elements */
for (int j = 0; j < num_elems; ++j) {
/* grab and element from the file */
vlist[j] = (Vertex *) malloc (sizeof (Vertex));
ply_get_element (ply, (void *) vlist[j]);
}
}
/* if we're on face elements, read them in */
if (!std::strcmp("face", elem_name)) {
/* create a list to hold all the face elements */
flist = (Face **) malloc (sizeof (Face *) * num_elems);
/* set up for getting face elements */
ply_get_property (ply, elem_name, &face_props[0]);
//ply_get_property (ply, elem_name, &face_props[1]);
/* grab all the face elements */
for (int j = 0; j < num_elems; ++j) {
/* grab and element from the file */
flist[j] = (Face *) malloc (sizeof (Face));
ply_get_element (ply, (void *) flist[j]);
// We know it's a triangle
int a = flist[j]->verts[0];
int b = flist[j]->verts[1];
int c = flist[j]->verts[2];
Point p1(vlist[a]->x, vlist[a]->y, vlist[a]->z);
Point p2(vlist[b]->x, vlist[b]->y, vlist[b]->z);
Point p3(vlist[c]->x, vlist[c]->y, vlist[c]->z);
listOfTriangles.push_back( Triangle(p1,p2,p3,col) );
}
}
}
/* close the PLY file */
ply_close (ply);
return listOfTriangles;
}
#endif