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

Revision 1979, 6.8 KB checked in by mattausch, 17 years ago (diff)

removed errors in loader

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