source: GTP/trunk/Lib/Vis/Preprocessing/src/UnigraphicsParser.cpp @ 1004

Revision 1004, 4.5 KB checked in by mattausch, 18 years ago (diff)

environment as a singleton

RevLine 
[162]1#include  <stdlib.h>
2#include <iostream>
3#include <list>
4#include <map>
5using namespace std;
6
[167]7#include "Vector3.h"
8#include "Mesh.h"
[162]9#include "SceneGraph.h"
10
11#include "UnigraphicsParser.h"
12#include "Material.h"
13#include "Environment.h"
[1001]14#include "ResourceManager.h"
[162]15
[1001]16
[863]17namespace GtpVisibilityPreprocessor {
[162]18
[704]19#define ROTATE_SCENE 0
[694]20// HACK
21static void RotateMesh(Mesh *mesh)
22{
23        VertexContainer::iterator it, it_end = mesh->mVertices.end();
24
25        const float angle = 30.0f * PI / 180.0f;
26        const Matrix4x4 rot = RotationYMatrix(angle);
27
28        for (it = mesh->mVertices.begin(); it != it_end; ++ it)
29        {
30                (*it) = rot * (*it);       
31        }
32}
33
34
[162]35struct ltstr
36{
37  bool operator()(const string s1, const string s2) const
38  {
39    return s1 < s2;
40  }
41};
42
43bool
44UnigraphicsParser::ParseFile(const string filename,
[658]45                             SceneGraphNode **proot,
46                                 const bool loadPolygonsAsMeshese)
[162]47{
48 
49  map<string, Vector3, ltstr> vht; // hash table for vectors
50  map<string, Vector3, ltstr> cht; // hash table for colors
51 
52  FILE         *file;
53  char str[80];
54  Face     *face;
[179]55  //Vector3  *vptr;
[162]56 
57  float x,y,z;
58  int i;
59 
60  if ((file = fopen(filename.c_str(),"rt")) == NULL)
61    return false;
62
63  SceneGraphNode *root = new SceneGraphNode;
[1001]64  Mesh *currentMesh = MeshManager::GetSingleton()->CreateResource();
[162]65
66  int meshGrouping;
[1004]67  Environment::GetSingleton()->GetIntValue("Unigraphics.meshGrouping",
[162]68                           meshGrouping);
69 
70  while(fscanf(file,"%s",str)!=EOF) {
71    switch (str[0]) {
72     
73    case 'c':
74      if (strcmp(str,"c_rgb")==0)
75        {
76          cout<<"c";
77          cout.flush();
78          /* vertex */
79          fscanf(file,"%s",str); // label
80          fscanf(file,"%f %f %f",&x,&y,&z);
81         
82          cht[str] = Vector3(x,y,z); // swap the file coordinates
83        }
84      break;
85     
86     
87    case 'v':            // is it v ?
88      cout<<".";
89      cout.flush();
90      /* vertex */
91      fscanf(file,"%s",str); // label
92      fscanf(file,"%f %f %f",&x,&y,&z);
93     
94      vht[str] = Vector3(x,z,-y); // swap the file coordinates
95      //         vptr = new Vector3C(x,y,z); // swap the file coordinates
96      break;
97     
98    case 'f': {
99      cout<<"+";
100      vector<Vector3> vertices;
[752]101      VertexIndexContainer vertexIndices;
[162]102
103      /* face */
104      fscanf(file,"%s",str); // label may  be ?
105      if (str[0]!='(')
106        fscanf(file,"%s",str);
107     
108      i = 0;                              // index of the vertex
109     
110     
111      map<string, Vector3, ltstr>::const_iterator c = vht.find(&str[1]);
112     
113      if (c == vht.end()) {
114        cout<<"error reading file : "<<
115          filename<<" ---missing vertex:"<<str<<endl; exit(1);
116      }
117     
118      vertices.push_back((*c).second);
119     
120      fscanf(file,"%s",str);      // get the vertex label
121     
122      while(str[0]!=')') {
123       
124        c = vht.find(str);
125       
126        if (c == vht.end())
127          {
128            cout<<"error reading file : "<<filename<<" -- missing vertex\n";
129            exit(1);  }
130       
131        vertices.push_back((*c).second);
132        fscanf(file,"%s",str);   // get the next vertex label
133      }
134
135      // add vertices to the mesh vertex list
[312]136      int index = (int)currentMesh->mVertices.size();
[162]137      for (i=0; i < vertices.size(); i++, index++) {
[752]138                currentMesh->mVertices.push_back(vertices[i]);
139                vertexIndices.push_back(index);
[162]140      }
141     
142      face = new Face(vertexIndices);
143     
144      fscanf(file,"%s",str);   // get the next vertex label
145     
146      if (str[0]!=';') {
147        c = cht.find(str);
148        if (currentMesh->mMaterial == NULL) {
[1001]149                Material *mat = MaterialManager::GetSingleton()->CreateResource();
[162]150          if (c != cht.end()) {
151            mat->mDiffuseColor = RgbColor((*c).second.x, (*c).second.y, (*c).second.z);
152            currentMesh->mMaterial = mat;
153          }
154          else
[1001]155            currentMesh->AssignRandomMaterial();
[162]156        }
157      }
158     
159      currentMesh->AddFace(face);
[726]160      if (meshGrouping != 0 && currentMesh->mFaces.size() >=  meshGrouping) {
[697]161                if (ROTATE_SCENE)
[694]162                        RotateMesh(currentMesh);
[1002]163
164                currentMesh->Preprocess();
165                // make an instance of this mesh
166                MeshInstance *mi = new MeshInstance(currentMesh);
167                root->mGeometry.push_back(mi); 
168                currentMesh = MeshManager::GetSingleton()->CreateResource();
[162]169      }
170    }
171    break;
172    }      /* end face */
173   
174    // get the rest of the line
175    fgets(str,80,file);
176  }
177 
178  if (currentMesh->mVertices.size()) {
[694]179          if (1)
180                RotateMesh(currentMesh);
[697]181 
[162]182    currentMesh->Preprocess();
183    MeshInstance *mi = new MeshInstance(currentMesh);
184    root->mGeometry.push_back(mi);
185  }
186 
187  fclose(file);
188 
189  *proot = root;
190
191  return true;
192}
193
[860]194}
Note: See TracBrowser for help on using the repository browser.