00001 #include "Importer.h"
00002 #include <string.h>
00003 #include "include/func.h"
00004 #include <math.h>
00005
00006 using namespace functions;
00007
00022 Importer::Importer() {
00023
00024 }
00025
00049 bool Importer::importModel(const char* filename, ExampleModel* em) {
00050
00051 globalCIndex=0;
00052 globalNIndex=0;
00053 translation = vector<float>(4,0);
00054 rotation = vector<float>(4,0);
00055 rotation[2] = 1;
00056 scale = vector<float>(4,1);
00057 scaleOrientation = vector<float>(4,0);
00058 scaleOrientation[2] = 1;
00059 center = vector<float>(4,0);
00060
00061
00062 TiXmlDocument doc(filename);
00063 bool loadOkay = doc.LoadFile();
00064 if (loadOkay)
00065 {
00066
00067 readElements(&doc,em);
00068 }
00069 else
00070 {
00071 printf("Failed to load file \"%s\"\n", filename);
00072 }
00073
00074 return loadOkay;
00075 }
00076
00097 void Importer::readElements(TiXmlNode* pParent, ExampleModel* em) {
00098 if ( !pParent ) return;
00099
00100 TiXmlNode* pChild;
00101 int t = pParent->Type();
00102
00103 if(t == TiXmlNode::ELEMENT) {
00104
00105
00106 if( !strcmp(pParent->Value(),"IndexedFaceSet") ||
00107 !strcmp(pParent->Value(),"Coordinate") ||
00108 !strcmp(pParent->Value(),"Normal")) {
00109 readAttributes(pParent->ToElement(),em);
00110 } else if(!strcmp(pParent->Value(),"Transform")) {
00111 readAttributes(pParent->ToElement(),em);
00112 }
00113 }
00114 for ( pChild = pParent->FirstChild(); pChild != 0; pChild = pChild->NextSibling())
00115 {
00116 readElements( pChild, em );
00117 }
00118 }
00119
00140 void Importer::readAttributes(TiXmlElement* pElement, ExampleModel* em)
00141 {
00142 if ( pElement ) {
00143
00144 TiXmlAttribute* pAttrib=pElement->FirstAttribute();
00145
00146
00147 while (pAttrib)
00148 {
00149 if(!strcmp(pAttrib->Name(),"coordIndex")) {
00150 globalCoordIndexs.clear();
00151 vector<string> tokens;
00152 string s = string(pAttrib->Value());
00153
00154 Tokenize(s,tokens,",");
00155
00156 for(int i=0;i<(int)tokens.size();i++) {
00157
00158 globalCoordIndexs.push_back(convertTo<int>(tokens[i]));
00159 }
00160 } else if(!strcmp(pAttrib->Name(),"normalIndex")) {
00161 globalNormalIndexs.clear();
00162 vector<string> tokens;
00163 string s = string(pAttrib->Value());
00164 Tokenize(s,tokens,",");
00165
00166 for(int i=0;i<(int)tokens.size();i++) {
00167
00168 globalNormalIndexs.push_back(convertTo<int>(tokens[i]));
00169 }
00170 } else if(!strcmp(pAttrib->Name(),"point")) {
00171 vector<string> tokens;
00172 string str = string(pAttrib->Value());
00173 Tokenize(str,tokens,",");
00174 Matrix vertexs;
00175
00176 for(int i=0;i<(int)tokens.size();i++) {
00177 vector<string> tokens2;
00178 Tokenize(tokens[i],tokens2," ");
00179
00180
00181
00182 Matrix tmp(4,1);
00183 tmp << convertTo<float>(tokens2[0]) << convertTo<float>(tokens2[1]) << convertTo<float>(tokens2[2]) << 1.0;
00184 if(i==0) {
00185 vertexs = tmp;
00186 } else {
00187 vertexs = vertexs | tmp;
00188 }
00189 }
00190
00191 Matrix translationMatrix(4,4);
00192 Matrix rotationMatrix(4,4);
00193 Matrix scaleMatrix(4,4);
00194 Matrix centerMatrix(4,4);
00195 Matrix scaleOrientationMatrix(4,4);
00196 Matrix minusCenterMatrix(4,4);
00197 Matrix minusScaleOrientationMatrix(4,4);
00198
00199
00200 translationMatrix << 1.0 << 0.0 << 0.0 << translation[0]
00201 << 0.0 << 1.0 << 0.0 << translation[1]
00202 << 0.0 << 0.0 << 1.0 << translation[2]
00203 << 0.0 << 0.0 << 0.0 << 1.0;
00204
00205
00206
00207
00208 float w = rotation[3];
00209 float x = rotation[0];
00210 float y = rotation[1];
00211 float z = rotation[2];
00212 float c = cos(w);
00213 float s = sin(w);
00214 float t = 1 - c;
00215
00216
00217 rotationMatrix << t*pow(x,2)+c << t*x*y-s*z << t*x*z+s*y << 0.0
00218 << t*x*y+s*z << t*pow(y,2)+c << t*y*z-s*x << 0.0
00219 << t*x*z-s*y << t*y*z+s*x << t*pow(z,2)+c << 0.0
00220 << 0.0 << 0.0 << 0.0 << 1.0;
00221
00222
00223 scaleMatrix = 0.0;
00224 scaleMatrix(1,1) = scale[0];
00225 scaleMatrix(2,2) = scale[1];
00226 scaleMatrix(3,3) = scale[2];
00227 scaleMatrix(4,4) = 1.0;
00228
00229
00230 centerMatrix << 1.0 << 0.0 << 0.0 << center[0]
00231 << 0.0 << 1.0 << 0.0 << center[1]
00232 << 0.0 << 0.0 << 1.0 << center[2]
00233 << 0.0 << 0.0 << 0.0 << 1.0;
00234
00235
00236 minusCenterMatrix << 1.0 << 0.0 << 0.0 << -center[0]
00237 << 0.0 << 1.0 << 0.0 << -center[1]
00238 << 0.0 << 0.0 << 1.0 << -center[2]
00239 << 0.0 << 0.0 << 0.0 << 1.0;
00240
00241
00242 scaleOrientationMatrix << 1.0 << 0.0 << 0.0 << scaleOrientation[0]
00243 << 0.0 << 1.0 << 0.0 << scaleOrientation[1]
00244 << 0.0 << 0.0 << 1.0 << scaleOrientation[2]
00245 << 0.0 << 0.0 << 0.0 << 1.0;
00246
00247
00248 minusScaleOrientationMatrix << 1.0 << 0.0 << 0.0 << -scaleOrientation[0]
00249 << 0.0 << 1.0 << 0.0 << -scaleOrientation[1]
00250 << 0.0 << 0.0 << 1.0 << -scaleOrientation[2]
00251 << 0.0 << 0.0 << 0.0 << 1.0;
00252
00253
00254
00255 vertexs = translationMatrix * centerMatrix * rotationMatrix * scaleOrientationMatrix * scaleMatrix * minusScaleOrientationMatrix * minusCenterMatrix * vertexs;
00256
00257
00258
00259
00260
00261
00262
00263
00264
00265
00266
00267
00268
00269
00270
00271
00272
00273
00274
00275
00276
00277 for(int i=1; i<=vertexs.Ncols();i++) {
00278
00279 Vertex* v = new Vertex((float)vertexs(1,i),(float)vertexs(2,i),(float)vertexs(3,i));
00280 em->addVertex(v);
00281 }
00282
00283 for(int i=0;i<(int)globalCoordIndexs.size();i=i+4) {
00284
00285
00286 Face* f = new Face(em->getVertex(globalCIndex+globalCoordIndexs[i]),em->getVertex(globalCIndex+globalCoordIndexs[i+1]),em->getVertex(globalCIndex+globalCoordIndexs[i+2]));
00287 em->addFace(f);
00288 }
00289 globalCIndex += tokens.size();
00290 translation = vector<float>(4,0);
00291 rotation = vector<float>(4,0);
00292 rotation[2] = 1;
00293 scale = vector<float>(4,1);
00294 scaleOrientation = vector<float>(4,0);
00295 scaleOrientation[2] = 1;
00296 center = vector<float>(4,0);
00297 } else if(!strcmp(pAttrib->Name(),"vector")) {
00298 vector<IvVector3*> normals;
00299 vector<string> tokens;
00300 string s = string(pAttrib->Value());
00301 Tokenize(s,tokens,",");
00302
00303 for(int i=0;i<(int)tokens.size();i++) {
00304 vector<string> tokens2;
00305 Tokenize(tokens[i],tokens2," ");
00306
00307
00308 IvVector3* tempVector = new IvVector3(convertTo<float>(tokens2[0]),convertTo<float>(tokens2[1]),convertTo<float>(tokens2[2]));
00309 normals.push_back(tempVector);
00310 }
00311
00312 for(int i=0;i<(int)globalNormalIndexs.size();i=i+4) {
00313
00314
00315
00316
00317 em->getFace(globalNIndex+i/4)->getVertex(0)->setNormal(normals[globalNormalIndexs[i+0]]);
00318 em->getFace(globalNIndex+i/4)->getVertex(1)->setNormal(normals[globalNormalIndexs[i+1]]);
00319 em->getFace(globalNIndex+i/4)->getVertex(2)->setNormal(normals[globalNormalIndexs[i+2]]);
00320 }
00321 globalNIndex += tokens.size();
00322
00323 } else if( !strcmp(pAttrib->Name(),"translation") ||
00324 !strcmp(pAttrib->Name(),"rotation") ||
00325 !strcmp(pAttrib->Name(),"scale") ||
00326 !strcmp(pAttrib->Name(),"center") ||
00327 !strcmp(pAttrib->Name(),"scaleOrientation")) {
00328 vector<string> tokens;
00329 string s = string(pAttrib->Value());
00330 Tokenize(s,tokens," ");
00331
00332 for(int i=0;i<(int)tokens.size();i++) {
00333 if(!strcmp(pAttrib->Name(),"translation")) {
00334 translation[i] = convertTo<float>(tokens[i]);
00335 } else if(!strcmp(pAttrib->Name(),"rotation")) {
00336 rotation[i] = convertTo<float>(tokens[i]);
00337 }else if(!strcmp(pAttrib->Name(),"scale")) {
00338 scale[i] = convertTo<float>(tokens[i]);
00339 }else if(!strcmp(pAttrib->Name(),"center")) {
00340 center[i] = convertTo<float>(tokens[i]);
00341 }else if(!strcmp(pAttrib->Name(),"scaleOrientation")) {
00342 scaleOrientation[i] = convertTo<float>(tokens[i]);
00343 }
00344 }
00345 }
00346 pAttrib=pAttrib->Next();
00347 }
00348 }
00349 }
00350