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