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