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