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

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