source: GTP/trunk/Lib/Vis/Preprocessing/src/ObjParser.cpp @ 2042

Revision 2042, 6.8 KB checked in by bittner, 17 years ago (diff)
RevLine 
[1233]1#include <stdlib.h>
[1221]2#include <iostream>
3#include <list>
4#include <map>
[1976]5#include <math.h>
6
[1221]7using namespace std;
8
9#include "Vector3.h"
10#include "Mesh.h"
11#include "SceneGraph.h"
12
13#include "ObjParser.h"
14//#include "Material.h"
[1328]15#include "Triangle3.h"
[1221]16#include "Environment.h"
17#include "ResourceManager.h"
[1315]18#include "IntersectableWrapper.h"
[1221]19
20
21namespace GtpVisibilityPreprocessor {
22
[1344]23#define CONNECT_SEQUENTIAL_FACES 0
[1221]24#define ROTATE_SCENE 0
[1328]25
[1976]26// hack: define this as in the intel ray tracer
27#define FLT_EPSILON 1.192092896e-07f
28
[1221]29// HACK
[1328]30static void RotateMesh(Mesh *mesh)
31{
32        VertexContainer::iterator it, it_end = mesh->mVertices.end();
[1221]33
[1328]34        const float angle = 30.0f * PI / 180.0f;
35        const Matrix4x4 rot = RotationYMatrix(angle);
[1221]36
[1328]37        for (it = mesh->mVertices.begin(); it != it_end; ++ it)
38        {
39                (*it) = rot * (*it);       
[1221]40        }
[1328]41}
[1221]42
[1344]43
[1328]44struct ltstr
45{
46        bool operator()(const string s1, const string s2) const
[1221]47        {
[1328]48                return s1 < s2;
49        }
50};
[1221]51
52
[1328]53static Face *LoadFace(char *str,
54                                          const VertexContainer &vertices,
55                                          map<int, Vector3> &hashTable)
[1221]56{
[1328]57        char *pch = strtok(str + 1, " ");
[1221]58       
59        VertexIndexContainer indices;
60        while (pch != NULL)
61        {
62                const int index = (int)strtol(pch, NULL, 10) - 1;
63                //Debug << index << " x ";
64               
65                // store vertex in hash table
66                hashTable[index] = vertices[index];
67                indices.push_back(index);
68               
69                pch = strtok(NULL, " ");
70        }
71
72        return new Face(indices);
73}
74
75
[1972]76static void LoadTriangles(char *str,
77                                                  const VertexContainer &vertices,
[1979]78                                                  vector<Triangle3> &triangles,
79                                                  const int line)
[1221]80{
[1328]81        char *pch = strtok(str + 1, " ");
82       
83        VertexIndexContainer indices;
84       
85        while (pch != NULL)
86        {
[2042]87          const int index = (int)strtol(pch, NULL, 10) - 1;
[1979]88                                               
[2042]89          // store vertex in hash table
90          //hashTable[index] = vertices[index];
91          if (index>=0)
[1328]92                indices.push_back(index);
93               
[2042]94          //if (line == 451703)
95          //    cout << index + 1 << " ";
96         
97          pch = strtok(NULL, " ");
98         
99          // problem: don't know how intel ray tracer tesselates
100          if ((int)indices.size() > 2)
[1972]101                {
[1978]102                        const int index_2 = (int)indices.size() - 2;
103                        const int index_3 = (int)indices.size() - 1;
[1972]104
105                        triangles.push_back(Triangle3(vertices[indices[0]],
[1979]106                                                                                  vertices[indices[index_2]],           
107                                                                                  vertices[indices[index_3]]));
[2042]108                        indices.clear();
[1972]109                }
[1328]110        }
[1979]111        //if (line == 451703)
112        //      cout << "t: " << triangles.size() << endl;
[1328]113}
114
115
116static Mesh *CreateMesh(FaceContainer &faces,
117                                                const map<int, Vector3> &hashTable)
118{
[1221]119        Mesh *mesh = MeshManager::GetSingleton()->CreateResource();
120       
121        FaceContainer::const_iterator fit, fit_end = faces.end();
122
123        for (fit = faces.begin(); fit != fit_end; ++ fit)
124        {
125                Face *face = *fit;
126                VertexIndexContainer::iterator vit, vit_end = face->mVertexIndices.end();
127               
128                for (vit = face->mVertexIndices.begin(); vit != vit_end; ++ vit)
129                {
130                        // go through indices
131                        const int index = *vit;           
132                        //Debug << "old idx: " << (*vit) << endl;
133                        map<int, Vector3>::const_iterator hit = hashTable.find(index);
134
135                        // correct face index (nust be relative to start of verices)
[1314]136                        (*vit) = (int)distance(hashTable.begin(), hit);
[1221]137                        //Debug << "new idx: " << (*vit) << endl;
138                }
139        }
140
141        VertexContainer vertices;
142
143        map<int, Vector3>::const_iterator hit, hit_end = hashTable.end();
144
145        // store vertices in given order
146        for (hit = hashTable.begin(); hit != hit_end; ++ hit)
147        {
148                mesh->mVertices.push_back((*hit).second);
149        }
150
151        mesh->mFaces = faces;
[1292]152        // can't do cleanup because coupling with kdf file for intel ray tracer
153        mesh->Preprocess(false);
154
[1221]155        return mesh;
156}
157
158
159// HACK: associate mesh instances with triangles
[1328]160static void AssociateFacesWithInstance(MeshInstance *mi,
161                                                                           vector<FaceParentInfo> &parents)
[1221]162{
[1344]163        Mesh *mesh = mi->GetMesh();
164
165        int i = 0;
[1655]166        FaceContainer::const_iterator fit, fit_end = mesh->mFaces.end();
167
[1344]168        for (fit = mesh->mFaces.begin(); fit != fit_end; ++ fit, i++)
[1221]169        {
[1344]170                parents.push_back(FaceParentInfo(mi, i));
[1221]171        }
172}
173
174
[1328]175static void ProcessMesh(FaceContainer &faces,
176                                                map<int, Vector3> &hashTable,
177                                                SceneGraphNode *root,
178                                                vector<FaceParentInfo> *parents)
179{
180        Mesh *mesh = CreateMesh(faces, hashTable);
181        // make an instance of this mesh
182        MeshInstance *mi = new MeshInstance(mesh);
183               
184        if (parents)
185        {
186                AssociateFacesWithInstance(mi, *parents);
187        }
188
189        root->mGeometry.push_back(mi); 
190
191        // reset tables
192        hashTable.clear();
193        faces.clear();
194}
195
196
[1973]197bool TriangleValid(const Triangle3 &triangle)
198{
[1976]199        const Vector3 a = triangle.mVertices[0] - triangle.mVertices[1];
200        const Vector3 b = triangle.mVertices[0] - triangle.mVertices[2];
201        const Vector3 cross_a_b = CrossProd(a, b);
[1973]202
[1976]203        if (SqrMagnitude(cross_a_b) <= 0.000001 * FLT_EPSILON * FLT_EPSILON)
204        {
[1978]205                cout << "x";
206                // v0, v1 & v2 lies on a line (area == 0)
[1976]207                return false;
208        }
[1973]209
[1976]210        return true;
[1973]211}
212
[1976]213
[1221]214bool ObjParser::ParseFile(const string filename,
[1344]215                                                  SceneGraphNode *root,
[1379]216                                                  const bool loadMeshes,
[1281]217                                                  vector<FaceParentInfo> *parents)
[1221]218{
219        FILE *file;
220        if ((file = fopen(filename.c_str(), "rt")) == NULL)
[1344]221        {       
[1221]222                return false;
[1344]223        }
224
[1221]225        map<int, Vector3> hashTable; // table associating indices with vectors
[1327]226        VertexContainer vertices; // table for vertices
[1221]227        FaceContainer faces;
228
[1979]229        int line = 0;
230
231        char str[1000];
[1221]232        int meshGrouping;
233        Environment::GetSingleton()->GetIntValue("ObjParser.meshGrouping", meshGrouping);
234
[1292]235        int nMaxFaces = meshGrouping;
236
[1979]237        while (fgets(str, 1000, file) != NULL)
238        {++ line;
[1876]239          switch (str[0])
[1221]240                {
[1876]241                case 'v': // vertex  or normal
242                  {
243                        switch (str[1]) {
244                        case 'n' :
245                          // normal do nothing
246                          break;
247                        default:
248                          float x, y, z; //cout << "v";
249                          sscanf(str + 1, "%f %f %f", &x, &y, &z);
250                          vertices.push_back(Vector3(x,y,z));
251                          //cout << "vertex: " << vertices.back() << endl;
[1233]252                        }
[1876]253                        break;
254                  }
[1221]255                case 'f':
256                        {
[1421]257                                if (loadMeshes)
258                                {
259                                        Face *face = LoadFace(str, vertices, hashTable);
260                                        if (!face) break;
[1344]261       
[1421]262                                        faces.push_back(face);
[1328]263
[1421]264                                        if (faces.size() >= nMaxFaces)
265                                        {
266                                                ProcessMesh(faces, hashTable, root, parents);
267                                        }
268                                }
269                                else
[1221]270                                {
[1972]271                                        vector<Triangle3> triangles;
272                                       
[1979]273                                        LoadTriangles(str, vertices, triangles, line);
[1978]274
[1972]275                                        vector<Triangle3>::const_iterator tit, tit_end = triangles.end();
[1344]276
[1972]277                                        for (tit = triangles.begin(); tit != tit_end; ++ tit)
[1421]278                                        {
[1979]279                                                if (0 && !TriangleValid(*tit)) continue;
280
[1972]281                                                TriangleIntersectable *obj = new TriangleIntersectable(*tit);   
[1979]282                        root->mGeometry.push_back(obj);
[1421]283                                        }
[1344]284                                }
[1221]285                                break;
[1314]286                        }   // end face
[1221]287                default:
288                        break;
289                }
290        }
[1979]291        //cout << "\n** " << root->mGeometry.size() << " " << " lines: " << line << endl;
[1421]292        if (loadMeshes)
293        {
294                // there could be faces remaining
295                if (!faces.empty())
296                {       
297                        ProcessMesh(faces, hashTable, root, parents);
298                }
[1221]299        }
[1421]300
[1344]301        // reset tables
302        hashTable.clear();
303        faces.clear();
[1221]304        fclose(file);
[1344]305       
[1221]306        return true;
307}
308
[1272]309}
Note: See TracBrowser for help on using the repository browser.