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