Changeset 752 for GTP/trunk/Lib/Vis/Preprocessing/src/PlyParser.cpp
- Timestamp:
- 04/20/06 13:25:41 (18 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
GTP/trunk/Lib/Vis/Preprocessing/src/PlyParser.cpp
r749 r752 2 2 using namespace std; 3 3 #include <ply.h> 4 #include <stdio.h> 4 5 5 6 #include "PlyParser.h" … … 9 10 10 11 11 12 12 // int facesPerMesh = 100000000; 13 int facesPerMesh = 30; 14 bool useRandomMaterial = false; 15 bool indexVertices = true; 13 16 14 17 /* user's vertex and face definitions for a polygonal object */ … … 33 36 }; 34 37 38 35 39 PlyProperty vert_props[] = { /* list of property information for a vertex */ 36 40 {"x", PLY_FLOAT, PLY_FLOAT, offsetof(Vertex,x), 0, 0, 0, 0}, … … 47 51 static PlyFace **flist; 48 52 53 49 54 bool 50 PlyParser::ParseFile(const string filename, 51 SceneGraphNode **root, 52 const bool loadPolygonsAsMeshes) 55 PlyParser::ParseSingleFile(const string filename, 56 SceneGraphNode *root) 53 57 { 54 55 /* user's vertex and face definitions for a polygonal object */ 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; 56 70 57 58 59 typedef double Matrix[4][4]; 60 61 /*** the PLY object ***/ 62 63 int nverts; 64 Vertex **vlist; 65 PlyOtherElems *other_elements = NULL; 66 PlyOtherProp *vert_other; 67 int nelems; 68 char **elist; 69 int num_comments; 70 char **comments; 71 int num_obj_info; 72 char **obj_info; 73 int file_type; 74 75 int i,j; 76 PlyFile *ply; 77 int nprops; 78 int num_elems; 79 PlyProperty **plist; 80 char *elem_name; 81 float version; 82 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 83 79 84 80 /*** Read in the original PLY object ***/ … … 169 165 ply_close (ply); 170 166 171 172 173 174 *root = new SceneGraphNode; 175 SceneGraphNode *currentNode = *root; 176 177 178 cerr<<"Baking faces into eshes"<<endl; 167 SceneGraphNode *currentNode = root; 168 169 cerr<<"Baking faces into meshes"<<endl; 170 179 171 // bake the faces into meshes 180 Mesh *mesh = NULL; 181 int facesPerMesh = 100; 172 Mesh meshProxy; 173 // only one face per mesh 174 VertexContainer vertices; 175 182 176 for (i = 0; i < nfaces; i++) { 183 177 if (i % facesPerMesh == 0) { 184 if (mesh) { 185 mesh->Preprocess; 186 // make an instance of this mesh 187 MeshInstance *mi = new MeshInstance(mesh); 188 currentNode->mGeometry.push_back(mi); 189 } 190 mesh = new Mesh(); 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); 191 200 } 201 202 meshProxy.Clear(); 203 } 192 204 193 194 195 196 197 198 199 vc.push_back(mesh->mVertices.size());200 mesh->mVertices.push_back(Vector3(v->x, v->y, v->z));201 202 203 mesh->mFaces.push_back(new Face(vc));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)); 204 216 } 205 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 206 242 for (i=0; i < nverts; i++) 207 243 free(vlist[i]); 244 208 245 free(vlist); 209 246 210 247 for (i=0; i < nfaces; i++) 211 248 free(flist[i]); 249 212 250 free(flist); 213 251 214 252 215 253 return true; 254 255 216 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 }
Note: See TracChangeset
for help on using the changeset viewer.