1 | #include <iostream>
|
---|
2 | #include <cstdio>
|
---|
3 |
|
---|
4 | #include "ply.h"
|
---|
5 | #include "PlyParser.h"
|
---|
6 |
|
---|
7 | #include "SceneGraph.h"
|
---|
8 | #include "Mesh.h"
|
---|
9 |
|
---|
10 | using namespace std;
|
---|
11 |
|
---|
12 | namespace GtpVisibilityPreprocessor {
|
---|
13 |
|
---|
14 | // int facesPerMesh = 100000000;
|
---|
15 | int facesPerMesh = 30;
|
---|
16 | bool useRandomMaterial = false;
|
---|
17 | bool indexVertices = true;
|
---|
18 |
|
---|
19 | /* user's vertex and face definitions for a polygonal object */
|
---|
20 |
|
---|
21 | typedef struct Vertex {
|
---|
22 | int id;
|
---|
23 | float x,y,z;
|
---|
24 | void *other_props; /* other properties */
|
---|
25 | } Vertex;
|
---|
26 |
|
---|
27 | typedef struct PlyFace {
|
---|
28 | int id;
|
---|
29 | unsigned char nverts; /* number of vertex indices in list */
|
---|
30 | int *verts; /* vertex index list */
|
---|
31 | void *other_props; /* other properties */
|
---|
32 | } PlyFace;
|
---|
33 |
|
---|
34 |
|
---|
35 |
|
---|
36 | char *elem_names[] = { /* list of the kinds of elements in the user's object */
|
---|
37 | "vertex", "face"
|
---|
38 | };
|
---|
39 |
|
---|
40 |
|
---|
41 | PlyProperty vert_props[] = { /* list of property information for a vertex */
|
---|
42 | {"x", PLY_FLOAT, PLY_FLOAT, offsetof(Vertex,x), 0, 0, 0, 0},
|
---|
43 | {"y", PLY_FLOAT, PLY_FLOAT, offsetof(Vertex,y), 0, 0, 0, 0},
|
---|
44 | {"z", PLY_FLOAT, PLY_FLOAT, offsetof(Vertex,z), 0, 0, 0, 0}
|
---|
45 | };
|
---|
46 |
|
---|
47 | PlyProperty face_props[] = { /* list of property information for a face */
|
---|
48 | {"vertex_indices", PLY_INT, PLY_INT, offsetof(PlyFace,verts),
|
---|
49 | 1, PLY_UCHAR, PLY_UCHAR, offsetof(PlyFace,nverts)}
|
---|
50 | };
|
---|
51 |
|
---|
52 | unsigned char has_fverts;
|
---|
53 | static PlyFace **flist;
|
---|
54 |
|
---|
55 |
|
---|
56 | bool
|
---|
57 | PlyParser::ParseSingleFile(const std::string filename,
|
---|
58 | SceneGraphLeaf *root)
|
---|
59 | {
|
---|
60 | /*** the PLY object ***/
|
---|
61 | int nverts;
|
---|
62 | Vertex **vlist;
|
---|
63 | PlyOtherElems *other_elements = NULL;
|
---|
64 | PlyOtherProp *vert_other;
|
---|
65 | int nelems;
|
---|
66 | char **elist;
|
---|
67 | int num_comments;
|
---|
68 | char **comments;
|
---|
69 | int num_obj_info;
|
---|
70 | char **obj_info;
|
---|
71 | int file_type;
|
---|
72 |
|
---|
73 | int i,j;
|
---|
74 | PlyFile *ply;
|
---|
75 | int nprops;
|
---|
76 | int num_elems;
|
---|
77 | PlyProperty **plist;
|
---|
78 | char *elem_name;
|
---|
79 | float version;
|
---|
80 |
|
---|
81 |
|
---|
82 | /*** Read in the original PLY object ***/
|
---|
83 | FILE *file = fopen(filename.c_str(), "rb");
|
---|
84 |
|
---|
85 | ply = ply_read (file, &nelems, &elist);
|
---|
86 | ply_get_info (ply, &version, &file_type);
|
---|
87 |
|
---|
88 | int nfaces;
|
---|
89 |
|
---|
90 | for (i = 0; i < nelems; i++) {
|
---|
91 |
|
---|
92 | /* get the description of the first element */
|
---|
93 | elem_name = elist[i];
|
---|
94 | plist = ply_get_element_description (ply, elem_name, &num_elems, &nprops);
|
---|
95 |
|
---|
96 | if (equal_strings ("vertex", elem_name)) {
|
---|
97 |
|
---|
98 | /* create a vertex list to hold all the vertices */
|
---|
99 | vlist = (Vertex **) malloc (sizeof (Vertex *) * num_elems);
|
---|
100 | nverts = num_elems;
|
---|
101 |
|
---|
102 | /* set up for getting vertex elements */
|
---|
103 |
|
---|
104 | ply_get_property (ply, elem_name, &vert_props[0]);
|
---|
105 | ply_get_property (ply, elem_name, &vert_props[1]);
|
---|
106 | ply_get_property (ply, elem_name, &vert_props[2]);
|
---|
107 | vert_other = ply_get_other_properties (ply, elem_name,
|
---|
108 | offsetof(Vertex,other_props));
|
---|
109 |
|
---|
110 | /* grab all the vertex elements */
|
---|
111 | for (j = 0; j < num_elems; j++) {
|
---|
112 | vlist[j] = (Vertex *) malloc (sizeof (Vertex));
|
---|
113 | ply_get_element (ply, (void *) vlist[j]);
|
---|
114 | vlist[j]->id = j;
|
---|
115 | // cout<<"("<<vlist[j]->x<<","<<vlist[j]->y<<","<<vlist[j]->z<<")"<<endl;
|
---|
116 | }
|
---|
117 | }
|
---|
118 | else
|
---|
119 | if (equal_strings ("face", elem_name)) {
|
---|
120 | /* create a list to hold all the face elements */
|
---|
121 | ALLOCN(flist, PlyFace *, num_elems);
|
---|
122 | nfaces = num_elems;
|
---|
123 | cerr<<"num faces="<<nfaces<<endl;
|
---|
124 |
|
---|
125 | /* set up for getting face elements */
|
---|
126 | has_fverts = false;
|
---|
127 |
|
---|
128 | for (j=0; j<nprops; j++)
|
---|
129 | {
|
---|
130 | if (equal_strings("vertex_indices", plist[j]->name))
|
---|
131 | {
|
---|
132 | ply_get_property (ply, elem_name, &face_props[0]);
|
---|
133 | has_fverts = true;
|
---|
134 | }
|
---|
135 | }
|
---|
136 |
|
---|
137 | // face_other = ply_get_other_properties (ply, elem_name,
|
---|
138 | // offsetof(Face,other_props));
|
---|
139 |
|
---|
140 | /* test for necessary properties */
|
---|
141 | if (!has_fverts) {
|
---|
142 | fprintf(stderr,"Faces must have vertex indices\n");
|
---|
143 | exit(-1);
|
---|
144 | }
|
---|
145 |
|
---|
146 | /* grab all the face elements */
|
---|
147 | for (j = 0; j < num_elems; j++) {
|
---|
148 | ALLOCN(flist[j], PlyFace, 1);
|
---|
149 | ply_get_element (ply, (void *) flist[j]);
|
---|
150 | flist[j]->id = j;
|
---|
151 |
|
---|
152 | // for (int k = 0; k < flist[j]->nverts; k++)
|
---|
153 | // cout<<flist[j]->verts[k]<<" ";
|
---|
154 | // cout<<endl;
|
---|
155 | }
|
---|
156 | } else
|
---|
157 | other_elements = ply_get_other_element (ply, elem_name, num_elems);
|
---|
158 |
|
---|
159 |
|
---|
160 | }
|
---|
161 |
|
---|
162 |
|
---|
163 |
|
---|
164 | comments = ply_get_comments (ply, &num_comments);
|
---|
165 | obj_info = ply_get_obj_info (ply, &num_obj_info);
|
---|
166 |
|
---|
167 | ply_close (ply);
|
---|
168 |
|
---|
169 | SceneGraphLeaf *currentNode = root;
|
---|
170 |
|
---|
171 | cerr<<"Baking faces into meshes"<<endl;
|
---|
172 |
|
---|
173 | // bake the faces into meshes
|
---|
174 | Mesh meshProxy;
|
---|
175 | // only one face per mesh
|
---|
176 | VertexContainer vertices;
|
---|
177 |
|
---|
178 | for (i = 0; i < nfaces; i++) {
|
---|
179 | if (i % facesPerMesh == 0) {
|
---|
180 | if (meshProxy.mFaces.size()) {
|
---|
181 |
|
---|
182 | if (indexVertices)
|
---|
183 | meshProxy.IndexVertices();
|
---|
184 |
|
---|
185 | Mesh *mesh = new Mesh((int)meshProxy.mVertices.size(),
|
---|
186 | (int)meshProxy.mFaces.size());
|
---|
187 |
|
---|
188 |
|
---|
189 | // cout<<"C="<<mesh->mVertices.capacity();
|
---|
190 | mesh->mVertices = meshProxy.mVertices;
|
---|
191 | // cout<<" NC="<<mesh->mVertices.capacity()<<" S="<<mesh->mVertices.size()<<endl;
|
---|
192 |
|
---|
193 | mesh->mFaces = meshProxy.mFaces;
|
---|
194 |
|
---|
195 | if (useRandomMaterial)
|
---|
196 | mesh->AssignRandomMaterial();
|
---|
197 |
|
---|
198 | mesh->Preprocess();
|
---|
199 | // make an instance of this mesh
|
---|
200 | MeshInstance *mi = new MeshInstance(mesh);
|
---|
201 | currentNode->mGeometry.push_back(mi);
|
---|
202 | }
|
---|
203 |
|
---|
204 | meshProxy.Clear();
|
---|
205 | }
|
---|
206 |
|
---|
207 | // only one face per mesh
|
---|
208 | VertexIndexContainer vc;
|
---|
209 |
|
---|
210 | // add vertices
|
---|
211 | for (int k = 0; k < flist[i]->nverts; k++) {
|
---|
212 | Vertex *v = vlist[flist[i]->verts[k]];
|
---|
213 | vc.push_back((int)meshProxy.mVertices.size());
|
---|
214 | meshProxy.mVertices.push_back(Vector3(v->x, v->y, v->z));
|
---|
215 | }
|
---|
216 |
|
---|
217 | meshProxy.mFaces.push_back(new Face(vc));
|
---|
218 | }
|
---|
219 |
|
---|
220 | if (meshProxy.mFaces.size()) {
|
---|
221 |
|
---|
222 | if (indexVertices)
|
---|
223 | meshProxy.IndexVertices();
|
---|
224 |
|
---|
225 | Mesh *mesh = new Mesh((int)meshProxy.mVertices.size(),
|
---|
226 | (int)meshProxy.mFaces.size());
|
---|
227 |
|
---|
228 | mesh->mVertices = meshProxy.mVertices;
|
---|
229 |
|
---|
230 | mesh->mFaces = meshProxy.mFaces;
|
---|
231 |
|
---|
232 | if (useRandomMaterial)
|
---|
233 | mesh->AssignRandomMaterial();
|
---|
234 |
|
---|
235 | mesh->Preprocess();
|
---|
236 | // make an instance of this mesh
|
---|
237 | MeshInstance *mi = new MeshInstance(mesh);
|
---|
238 | currentNode->mGeometry.push_back(mi);
|
---|
239 | }
|
---|
240 |
|
---|
241 | // make sure that no face gets deleted by the destructor!
|
---|
242 | meshProxy.Clear();
|
---|
243 |
|
---|
244 | for (i=0; i < nverts; i++)
|
---|
245 | free(vlist[i]);
|
---|
246 |
|
---|
247 | free(vlist);
|
---|
248 |
|
---|
249 | for (i=0; i < nfaces; i++)
|
---|
250 | free(flist[i]);
|
---|
251 |
|
---|
252 | free(flist);
|
---|
253 |
|
---|
254 |
|
---|
255 | return true;
|
---|
256 |
|
---|
257 |
|
---|
258 | }
|
---|
259 |
|
---|
260 | bool
|
---|
261 | PlyParser::ParseFile(const string filename,
|
---|
262 | SceneGraphLeaf *root,
|
---|
263 | const bool loadMeshes,
|
---|
264 | vector<FaceParentInfo> *parents)
|
---|
265 | {
|
---|
266 | vector<string> filelist;
|
---|
267 |
|
---|
268 | if (strstr(filename.c_str(), ".plb")) {
|
---|
269 | // parse the filelist
|
---|
270 | FILE *f = fopen(filename.c_str(), "rt");
|
---|
271 | char s[64];
|
---|
272 | if (!f) {
|
---|
273 | cerr<<"Cannot open .plb file"<<endl;
|
---|
274 | exit(1);
|
---|
275 | }
|
---|
276 | while (fgets(s,64,f)) {
|
---|
277 | // remove nl
|
---|
278 | s[strlen(s)-1] = 0;
|
---|
279 | filelist.push_back(s);
|
---|
280 | }
|
---|
281 | fclose(f);
|
---|
282 |
|
---|
283 | } else
|
---|
284 | filelist.push_back(filename);
|
---|
285 |
|
---|
286 | for (int i=0; i < filelist.size(); i++) {
|
---|
287 | if (!ParseSingleFile(filelist[i], root)) {
|
---|
288 | cerr<<"Ply parse error. Quiting ...\n";
|
---|
289 | exit(1);
|
---|
290 | }
|
---|
291 | }
|
---|
292 |
|
---|
293 | return true;
|
---|
294 | }
|
---|
295 |
|
---|
296 | }
|
---|