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

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