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