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