[749] | 1 | #include <iostream>
|
---|
| 2 | using namespace std;
|
---|
| 3 | #include <ply.h>
|
---|
[752] | 4 | #include <stdio.h>
|
---|
[749] | 5 |
|
---|
| 6 | #include "PlyParser.h"
|
---|
| 7 |
|
---|
| 8 | #include "SceneGraph.h"
|
---|
| 9 | #include "Mesh.h"
|
---|
| 10 |
|
---|
[863] | 11 | namespace GtpVisibilityPreprocessor {
|
---|
[749] | 12 |
|
---|
[752] | 13 | // int facesPerMesh = 100000000;
|
---|
| 14 | int facesPerMesh = 30;
|
---|
| 15 | bool useRandomMaterial = false;
|
---|
| 16 | bool indexVertices = true;
|
---|
[749] | 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 |
|
---|
[752] | 39 |
|
---|
[749] | 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 |
|
---|
[752] | 54 |
|
---|
[749] | 55 | bool
|
---|
[752] | 56 | PlyParser::ParseSingleFile(const string filename,
|
---|
| 57 | SceneGraphNode *root)
|
---|
[749] | 58 | {
|
---|
[752] | 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;
|
---|
[749] | 71 |
|
---|
[752] | 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 |
|
---|
[749] | 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 |
|
---|
[752] | 168 | SceneGraphNode *currentNode = root;
|
---|
[749] | 169 |
|
---|
[752] | 170 | cerr<<"Baking faces into meshes"<<endl;
|
---|
[749] | 171 |
|
---|
[752] | 172 | // bake the faces into meshes
|
---|
| 173 | Mesh meshProxy;
|
---|
| 174 | // only one face per mesh
|
---|
| 175 | VertexContainer vertices;
|
---|
[749] | 176 |
|
---|
[752] | 177 | for (i = 0; i < nfaces; i++) {
|
---|
| 178 | if (i % facesPerMesh == 0) {
|
---|
| 179 | if (meshProxy.mFaces.size()) {
|
---|
[749] | 180 |
|
---|
[752] | 181 | if (indexVertices)
|
---|
| 182 | meshProxy.IndexVertices();
|
---|
| 183 |
|
---|
[1419] | 184 | Mesh *mesh = new Mesh((int)meshProxy.mVertices.size(),
|
---|
| 185 | (int)meshProxy.mFaces.size());
|
---|
[752] | 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();
|
---|
[749] | 196 |
|
---|
[752] | 197 | mesh->Preprocess();
|
---|
| 198 | // make an instance of this mesh
|
---|
| 199 | MeshInstance *mi = new MeshInstance(mesh);
|
---|
| 200 | currentNode->mGeometry.push_back(mi);
|
---|
[749] | 201 | }
|
---|
[752] | 202 |
|
---|
| 203 | meshProxy.Clear();
|
---|
| 204 | }
|
---|
[749] | 205 |
|
---|
[752] | 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]];
|
---|
[1419] | 212 | vc.push_back((int)meshProxy.mVertices.size());
|
---|
[752] | 213 | meshProxy.mVertices.push_back(Vector3(v->x, v->y, v->z));
|
---|
| 214 | }
|
---|
| 215 |
|
---|
| 216 | meshProxy.mFaces.push_back(new Face(vc));
|
---|
[749] | 217 | }
|
---|
| 218 |
|
---|
[752] | 219 | if (meshProxy.mFaces.size()) {
|
---|
| 220 |
|
---|
| 221 | if (indexVertices)
|
---|
| 222 | meshProxy.IndexVertices();
|
---|
| 223 |
|
---|
[1419] | 224 | Mesh *mesh = new Mesh((int)meshProxy.mVertices.size(),
|
---|
| 225 | (int)meshProxy.mFaces.size());
|
---|
[752] | 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 |
|
---|
[749] | 243 | for (i=0; i < nverts; i++)
|
---|
| 244 | free(vlist[i]);
|
---|
[752] | 245 |
|
---|
[749] | 246 | free(vlist);
|
---|
| 247 |
|
---|
| 248 | for (i=0; i < nfaces; i++)
|
---|
| 249 | free(flist[i]);
|
---|
[752] | 250 |
|
---|
[749] | 251 | free(flist);
|
---|
| 252 |
|
---|
[752] | 253 |
|
---|
[749] | 254 | return true;
|
---|
[752] | 255 |
|
---|
| 256 |
|
---|
[749] | 257 | }
|
---|
[752] | 258 |
|
---|
| 259 | bool
|
---|
| 260 | PlyParser::ParseFile(const string filename,
|
---|
[1344] | 261 | SceneGraphNode *root,
|
---|
[1379] | 262 | const bool loadMeshes,
|
---|
[1281] | 263 | vector<FaceParentInfo> *parents)
|
---|
[752] | 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 | for (int i=0; i < filelist.size(); i++) {
|
---|
[1344] | 286 | if (!ParseSingleFile(filelist[i], root)) {
|
---|
[752] | 287 | cerr<<"Ply parse error. Quiting ...\n";
|
---|
| 288 | exit(1);
|
---|
| 289 | }
|
---|
| 290 | }
|
---|
[1344] | 291 |
|
---|
[752] | 292 | return true;
|
---|
| 293 | }
|
---|
[860] | 294 |
|
---|
[1281] | 295 | }
|
---|