PlyLoader.cpp 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080
  1. /*
  2. ---------------------------------------------------------------------------
  3. Open Asset Import Library (ASSIMP)
  4. ---------------------------------------------------------------------------
  5. Copyright (c) 2006-2008, ASSIMP Development Team
  6. All rights reserved.
  7. Redistribution and use of this software in source and binary forms,
  8. with or without modification, are permitted provided that the following
  9. conditions are met:
  10. * Redistributions of source code must retain the above
  11. copyright notice, this list of conditions and the
  12. following disclaimer.
  13. * Redistributions in binary form must reproduce the above
  14. copyright notice, this list of conditions and the
  15. following disclaimer in the documentation and/or other
  16. materials provided with the distribution.
  17. * Neither the name of the ASSIMP team, nor the names of its
  18. contributors may be used to endorse or promote products
  19. derived from this software without specific prior
  20. written permission of the ASSIMP Development Team.
  21. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  22. "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  23. LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  24. A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  25. OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  26. SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  27. LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  28. DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  29. THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  30. (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  31. OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  32. ---------------------------------------------------------------------------
  33. */
  34. /** @file Implementation of the PLY importer class */
  35. // internal headers
  36. #include "PlyLoader.h"
  37. #include "MaterialSystem.h"
  38. #include "StringComparison.h"
  39. // public ASSIMP headers
  40. #include "../include/IOStream.h"
  41. #include "../include/IOSystem.h"
  42. #include "../include/aiMesh.h"
  43. #include "../include/aiScene.h"
  44. #include "../include/aiAssert.h"
  45. #include "../include/DefaultLogger.h"
  46. // boost headeers
  47. #include <boost/scoped_ptr.hpp>
  48. using namespace Assimp;
  49. // ------------------------------------------------------------------------------------------------
  50. // Constructor to be privately used by Importer
  51. PLYImporter::PLYImporter()
  52. {
  53. }
  54. // ------------------------------------------------------------------------------------------------
  55. // Destructor, private as well
  56. PLYImporter::~PLYImporter()
  57. {
  58. }
  59. // ------------------------------------------------------------------------------------------------
  60. // Returns whether the class can handle the format of the given file.
  61. bool PLYImporter::CanRead( const std::string& pFile, IOSystem* pIOHandler) const
  62. {
  63. // simple check of file extension is enough for the moment
  64. std::string::size_type pos = pFile.find_last_of('.');
  65. // no file extension - can't read
  66. if( pos == std::string::npos)
  67. return false;
  68. std::string extension = pFile.substr( pos);
  69. if (extension.length() < 4)return false;
  70. if (extension[0] != '.')return false;
  71. if (extension[1] != 'p' && extension[1] != 'P')return false;
  72. if (extension[2] != 'l' && extension[2] != 'L')return false;
  73. if (extension[3] != 'y' && extension[3] != 'Y')return false;
  74. return true;
  75. }
  76. // ------------------------------------------------------------------------------------------------
  77. // Imports the given file into the given scene structure.
  78. void PLYImporter::InternReadFile(
  79. const std::string& pFile, aiScene* pScene, IOSystem* pIOHandler)
  80. {
  81. boost::scoped_ptr<IOStream> file( pIOHandler->Open( pFile));
  82. // Check whether we can read from the file
  83. if( file.get() == NULL)
  84. throw new ImportErrorException( "Failed to open PLY file " + pFile + ".");
  85. // check whether the ply file is large enough to contain
  86. // at least the file header
  87. size_t fileSize = file->FileSize();
  88. if( fileSize < 10)
  89. throw new ImportErrorException( "PLY File is too small.");
  90. // allocate storage and copy the contents of the file to a memory buffer
  91. // (terminate it with zero)
  92. std::vector<unsigned char> mBuffer2(fileSize+1);
  93. file->Read( &mBuffer2[0], 1, fileSize);
  94. this->mBuffer = &mBuffer2[0];
  95. this->mBuffer[fileSize] = '\0';
  96. // the beginning of the file must be PLY
  97. if (this->mBuffer[0] != 'P' && this->mBuffer[0] != 'p' ||
  98. this->mBuffer[1] != 'L' && this->mBuffer[1] != 'l' ||
  99. this->mBuffer[2] != 'Y' && this->mBuffer[2] != 'y')
  100. {
  101. throw new ImportErrorException( "Invalid .ply file: Magic number \'ply\' is no there");
  102. }
  103. char* szMe = (char*)&this->mBuffer[3];
  104. SkipSpacesAndLineEnd(szMe,(const char**)&szMe);
  105. // determine the format of the file data
  106. PLY::DOM sPlyDom;
  107. if (0 == ASSIMP_strincmp(szMe,"format",6) && IsSpace(*(szMe+6)))
  108. {
  109. szMe += 7;
  110. if (0 == ASSIMP_strincmp(szMe,"ascii",5) && IsSpace(*(szMe+5)))
  111. {
  112. szMe += 6;
  113. SkipLine(szMe,(const char**)&szMe);
  114. if(!PLY::DOM::ParseInstance(szMe,&sPlyDom))
  115. throw new ImportErrorException( "Invalid .ply file: Unable to build DOM (#1)");
  116. }
  117. else if (0 == ASSIMP_strincmp(szMe,"binary_",7))
  118. {
  119. bool bIsBE = false;
  120. // binary_little_endian
  121. // binary_big_endian
  122. szMe += 7;
  123. #if (defined AI_BUILD_BIG_ENDIAN)
  124. if ('l' == *szMe || 'L' == *szMe)bIsBE = true;
  125. #else
  126. if ('b' == *szMe || 'B' == *szMe)bIsBE = true;
  127. #endif // ! AI_BUILD_BIG_ENDIAN
  128. // skip the line, parse the rest of the header and build the DOM
  129. SkipLine(szMe,(const char**)&szMe);
  130. if(!PLY::DOM::ParseInstanceBinary(szMe,&sPlyDom,bIsBE))
  131. throw new ImportErrorException( "Invalid .ply file: Unable to build DOM (#2)");
  132. }
  133. else throw new ImportErrorException( "Invalid .ply file: Unknown file format");
  134. }
  135. else
  136. {
  137. delete[] this->mBuffer;
  138. AI_DEBUG_INVALIDATE_PTR(this->mBuffer);
  139. throw new ImportErrorException( "Invalid .ply file: Missing format specification");
  140. }
  141. this->pcDOM = &sPlyDom;
  142. // now load a list of vertices. This must be sucessfull in order to procede
  143. std::vector<aiVector3D> avPositions;
  144. this->LoadVertices(&avPositions,false);
  145. if (avPositions.empty())
  146. throw new ImportErrorException( "Invalid .ply file: No vertices found. "
  147. "Unable to parse the data format of the PLY file.");
  148. // now load a list of normals.
  149. std::vector<aiVector3D> avNormals;
  150. this->LoadVertices(&avNormals,true);
  151. // load the face list
  152. std::vector<PLY::Face> avFaces;
  153. this->LoadFaces(&avFaces);
  154. // if no face list is existing we assume that the vertex
  155. // list is containing a list of triangles
  156. if (avFaces.empty())
  157. {
  158. if (avPositions.size() < 3)
  159. {
  160. throw new ImportErrorException( "Invalid .ply file: Not enough "
  161. "vertices to build a face list. ");
  162. }
  163. unsigned int iNum = (unsigned int)avPositions.size() / 3;
  164. for (unsigned int i = 0; i< iNum;++i)
  165. {
  166. PLY::Face sFace;
  167. sFace.mIndices.push_back((iNum*3));
  168. sFace.mIndices.push_back((iNum*3)+1);
  169. sFace.mIndices.push_back((iNum*3)+2);
  170. avFaces.push_back(sFace);
  171. }
  172. }
  173. // now load a list of all materials
  174. std::vector<MaterialHelper*> avMaterials;
  175. this->LoadMaterial(&avMaterials);
  176. // now load a list of all vertex color channels
  177. std::vector<aiColor4D> avColors;
  178. this->LoadVertexColor(&avColors);
  179. // now try to load texture coordinates
  180. std::vector<aiVector2D> avTexCoords;
  181. this->LoadTextureCoordinates(&avTexCoords);
  182. // now replace the default material in all faces and validate all material indices
  183. this->ReplaceDefaultMaterial(&avFaces,&avMaterials);
  184. // now convert this to a list of aiMesh instances
  185. std::vector<aiMesh*> avMeshes;
  186. this->ConvertMeshes(&avFaces,&avPositions,&avNormals,
  187. &avColors,&avTexCoords,&avMaterials,&avMeshes);
  188. if (avMeshes.empty())
  189. throw new ImportErrorException( "Invalid .ply file: Unable to extract mesh data ");
  190. // now generate the output scene object. Fill the material list
  191. pScene->mNumMaterials = (unsigned int)avMaterials.size();
  192. pScene->mMaterials = new aiMaterial*[pScene->mNumMaterials];
  193. for (unsigned int i = 0; i < pScene->mNumMaterials;++i)
  194. pScene->mMaterials[i] = avMaterials[i];
  195. // fill the mesh list
  196. pScene->mNumMeshes = (unsigned int)avMeshes.size();
  197. pScene->mMeshes = new aiMesh*[pScene->mNumMeshes];
  198. for (unsigned int i = 0; i < pScene->mNumMeshes;++i)
  199. pScene->mMeshes[i] = avMeshes[i];
  200. // generate a simple node structure
  201. pScene->mRootNode = new aiNode();
  202. pScene->mRootNode->mNumMeshes = pScene->mNumMeshes;
  203. pScene->mRootNode->mMeshes = new unsigned int[pScene->mNumMeshes];
  204. for (unsigned int i = 0; i < pScene->mRootNode->mNumMeshes;++i)
  205. pScene->mRootNode->mMeshes[i] = i;
  206. }
  207. // ------------------------------------------------------------------------------------------------
  208. void PLYImporter::ConvertMeshes(std::vector<PLY::Face>* avFaces,
  209. const std::vector<aiVector3D>* avPositions,
  210. const std::vector<aiVector3D>* avNormals,
  211. const std::vector<aiColor4D>* avColors,
  212. const std::vector<aiVector2D>* avTexCoords,
  213. const std::vector<MaterialHelper*>* avMaterials,
  214. std::vector<aiMesh*>* avOut)
  215. {
  216. ai_assert(NULL != avFaces);
  217. ai_assert(NULL != avPositions);
  218. ai_assert(NULL != avMaterials);
  219. // split by materials
  220. std::vector<unsigned int>* aiSplit = new std::vector<unsigned int>[
  221. avMaterials->size()];
  222. unsigned int iNum = 0;
  223. for (std::vector<PLY::Face>::const_iterator
  224. i = avFaces->begin();
  225. i != avFaces->end();++i,++iNum)
  226. {
  227. // index has already been checked
  228. aiSplit[(*i).iMaterialIndex].push_back(iNum);
  229. }
  230. // now generate submeshes
  231. for (unsigned int p = 0; p < avMaterials->size();++p)
  232. {
  233. if (aiSplit[p].size() != 0)
  234. {
  235. // allocate the mesh object
  236. aiMesh* p_pcOut = new aiMesh();
  237. p_pcOut->mMaterialIndex = p;
  238. p_pcOut->mNumFaces = (unsigned int)aiSplit[p].size();
  239. p_pcOut->mFaces = new aiFace[aiSplit[p].size()];
  240. // at first we need to determine the size of the output vector array
  241. unsigned int iNum = 0;
  242. for (unsigned int i = 0; i < aiSplit[p].size();++i)
  243. {
  244. iNum += (unsigned int)(*avFaces)[aiSplit[p][i]].mIndices.size();
  245. }
  246. p_pcOut->mNumVertices = iNum;
  247. p_pcOut->mVertices = new aiVector3D[iNum];
  248. if (!avColors->empty())
  249. p_pcOut->mColors[0] = new aiColor4D[iNum];
  250. if (!avTexCoords->empty())
  251. {
  252. p_pcOut->mNumUVComponents[0] = 2;
  253. p_pcOut->mTextureCoords[0] = new aiVector3D[iNum];
  254. }
  255. if (!avNormals->empty())
  256. p_pcOut->mNormals = new aiVector3D[iNum];
  257. // add all faces
  258. iNum = 0;
  259. unsigned int iVertex = 0;
  260. for (std::vector<unsigned int>::const_iterator
  261. i = aiSplit[p].begin();
  262. i != aiSplit[p].end();++i,++iNum)
  263. {
  264. p_pcOut->mFaces[iNum].mNumIndices = (unsigned int)(*avFaces)[*i].mIndices.size();
  265. p_pcOut->mFaces[iNum].mIndices = new unsigned int[p_pcOut->mFaces[iNum].mNumIndices];
  266. // build an unique set of vertices/colors for this face
  267. for (unsigned int q = 0; q < p_pcOut->mFaces[iNum].mNumIndices;++q)
  268. {
  269. p_pcOut->mFaces[iNum].mIndices[q] = iVertex;
  270. p_pcOut->mVertices[iVertex] = (*avPositions)[(*avFaces)[*i].mIndices[q]];
  271. if (!avColors->empty())
  272. p_pcOut->mColors[0][iVertex] = (*avColors)[(*avFaces)[*i].mIndices[q]];
  273. if (!avTexCoords->empty())
  274. {
  275. const aiVector2D& vec = (*avTexCoords)[(*avFaces)[*i].mIndices[q]];
  276. p_pcOut->mTextureCoords[0][iVertex].x = vec.x;
  277. p_pcOut->mTextureCoords[0][iVertex].y = vec.y;
  278. }
  279. if (!avNormals->empty())
  280. p_pcOut->mNormals[iVertex] = (*avNormals)[(*avFaces)[*i].mIndices[q]];
  281. iVertex++;
  282. }
  283. }
  284. // add the mesh to the output list
  285. avOut->push_back(p_pcOut);
  286. }
  287. }
  288. delete[] aiSplit;
  289. return;
  290. }
  291. // ------------------------------------------------------------------------------------------------
  292. void PLYImporter::ReplaceDefaultMaterial(std::vector<PLY::Face>* avFaces,
  293. std::vector<MaterialHelper*>* avMaterials)
  294. {
  295. bool bNeedDefaultMat = false;
  296. for (std::vector<PLY::Face>::iterator
  297. i = avFaces->begin();i != avFaces->end();++i)
  298. {
  299. if (0xFFFFFFFF == (*i).iMaterialIndex)
  300. {
  301. bNeedDefaultMat = true;
  302. (*i).iMaterialIndex = (unsigned int)avMaterials->size();
  303. }
  304. else if ((*i).iMaterialIndex >= avMaterials->size() )
  305. {
  306. // clamp the index
  307. (*i).iMaterialIndex = (unsigned int)avMaterials->size()-1;
  308. }
  309. }
  310. if (bNeedDefaultMat)
  311. {
  312. // generate a default material
  313. MaterialHelper* pcHelper = new MaterialHelper();
  314. // fill in a default material
  315. int iMode = (int)aiShadingMode_Gouraud;
  316. pcHelper->AddProperty<int>(&iMode, 1, AI_MATKEY_SHADING_MODEL);
  317. aiColor3D clr;
  318. clr.b = clr.g = clr.r = 0.6f;
  319. pcHelper->AddProperty<aiColor3D>(&clr, 1,AI_MATKEY_COLOR_DIFFUSE);
  320. pcHelper->AddProperty<aiColor3D>(&clr, 1,AI_MATKEY_COLOR_SPECULAR);
  321. clr.b = clr.g = clr.r = 0.05f;
  322. pcHelper->AddProperty<aiColor3D>(&clr, 1,AI_MATKEY_COLOR_AMBIENT);
  323. avMaterials->push_back(pcHelper);
  324. }
  325. return;
  326. }
  327. // ------------------------------------------------------------------------------------------------
  328. void PLYImporter::LoadTextureCoordinates(std::vector<aiVector2D>* pvOut)
  329. {
  330. ai_assert(NULL != pvOut);
  331. unsigned int aiPositions[2] = {0xFFFFFFFF,0xFFFFFFFF};
  332. PLY::EDataType aiTypes[2];
  333. PLY::ElementInstanceList* pcList = NULL;
  334. unsigned int cnt = 0;
  335. // serach in the DOM for a vertex entry
  336. unsigned int _i = 0;
  337. for (std::vector<PLY::Element*>::const_iterator
  338. i = this->pcDOM->alElements.begin();
  339. i != this->pcDOM->alElements.end();++i,++_i)
  340. {
  341. if (PLY::EEST_Vertex == (*i)->eSemantic)
  342. {
  343. pcList = this->pcDOM->alElementData[_i];
  344. // now check whether which normal components are available
  345. unsigned int _a = 0;
  346. for (std::vector<PLY::Property*>::const_iterator
  347. a = (*i)->alProperties.begin();
  348. a != (*i)->alProperties.end();++a,++_a)
  349. {
  350. if ((*a)->bIsList)continue;
  351. if (PLY::EST_UTextureCoord == (*a)->Semantic)
  352. {
  353. cnt++;
  354. aiPositions[0] = _a;
  355. aiTypes[0] = (*a)->eType;
  356. }
  357. else if (PLY::EST_VTextureCoord == (*a)->Semantic)
  358. {
  359. cnt++;
  360. aiPositions[1] = _a;
  361. aiTypes[1] = (*a)->eType;
  362. }
  363. }
  364. }
  365. }
  366. // check whether we have a valid source for the texture coordinates data
  367. if (NULL != pcList && 0 != cnt)
  368. {
  369. pvOut->reserve(pcList->alInstances.size());
  370. for (std::vector<ElementInstance*>::const_iterator
  371. i = pcList->alInstances.begin();
  372. i != pcList->alInstances.end();++i)
  373. {
  374. // convert the vertices to sp floats
  375. aiVector2D vOut;
  376. if (0xFFFFFFFF != aiPositions[0])
  377. {
  378. vOut.x = PLY::PropertyInstance::ConvertTo<float>(
  379. (*i)->alProperties[aiPositions[0]].avList.front(),aiTypes[0]);
  380. }
  381. if (0xFFFFFFFF != aiPositions[1])
  382. {
  383. vOut.y = PLY::PropertyInstance::ConvertTo<float>(
  384. (*i)->alProperties[aiPositions[1]].avList.front(),aiTypes[1]);
  385. }
  386. // and add them to our nice list
  387. pvOut->push_back(vOut);
  388. }
  389. }
  390. }
  391. // ------------------------------------------------------------------------------------------------
  392. void PLYImporter::LoadVertices(std::vector<aiVector3D>* pvOut, bool p_bNormals)
  393. {
  394. ai_assert(NULL != pvOut);
  395. unsigned int aiPositions[3] = {0xFFFFFFFF,0xFFFFFFFF,0xFFFFFFFF};
  396. PLY::EDataType aiTypes[3];
  397. PLY::ElementInstanceList* pcList = NULL;
  398. unsigned int cnt = 0;
  399. // serach in the DOM for a vertex entry
  400. unsigned int _i = 0;
  401. for (std::vector<PLY::Element*>::const_iterator
  402. i = this->pcDOM->alElements.begin();
  403. i != this->pcDOM->alElements.end();++i,++_i)
  404. {
  405. if (PLY::EEST_Vertex == (*i)->eSemantic)
  406. {
  407. pcList = this->pcDOM->alElementData[_i];
  408. // load normal vectors?
  409. if (p_bNormals)
  410. {
  411. // now check whether which normal components are available
  412. unsigned int _a = 0;
  413. for (std::vector<PLY::Property*>::const_iterator
  414. a = (*i)->alProperties.begin();
  415. a != (*i)->alProperties.end();++a,++_a)
  416. {
  417. if ((*a)->bIsList)continue;
  418. if (PLY::EST_XNormal == (*a)->Semantic)
  419. {
  420. cnt++;
  421. aiPositions[0] = _a;
  422. aiTypes[0] = (*a)->eType;
  423. }
  424. else if (PLY::EST_YNormal == (*a)->Semantic)
  425. {
  426. cnt++;
  427. aiPositions[1] = _a;
  428. aiTypes[1] = (*a)->eType;
  429. }
  430. else if (PLY::EST_ZNormal == (*a)->Semantic)
  431. {
  432. cnt++;
  433. aiPositions[2] = _a;
  434. aiTypes[2] = (*a)->eType;
  435. }
  436. }
  437. }
  438. // load vertex coordinates
  439. else
  440. {
  441. // now check whether which coordinate sets are available
  442. unsigned int _a = 0;
  443. for (std::vector<PLY::Property*>::const_iterator
  444. a = (*i)->alProperties.begin();
  445. a != (*i)->alProperties.end();++a,++_a)
  446. {
  447. if ((*a)->bIsList)continue;
  448. if (PLY::EST_XCoord == (*a)->Semantic)
  449. {
  450. cnt++;
  451. aiPositions[0] = _a;
  452. aiTypes[0] = (*a)->eType;
  453. }
  454. else if (PLY::EST_YCoord == (*a)->Semantic)
  455. {
  456. cnt++;
  457. aiPositions[1] = _a;
  458. aiTypes[1] = (*a)->eType;
  459. }
  460. else if (PLY::EST_ZCoord == (*a)->Semantic)
  461. {
  462. cnt++;
  463. aiPositions[2] = _a;
  464. aiTypes[2] = (*a)->eType;
  465. }
  466. if (3 == cnt)break;
  467. }
  468. }
  469. break;
  470. }
  471. }
  472. // check whether we have a valid source for the vertex data
  473. if (NULL != pcList && 0 != cnt)
  474. {
  475. pvOut->reserve(pcList->alInstances.size());
  476. for (std::vector<ElementInstance*>::const_iterator
  477. i = pcList->alInstances.begin();
  478. i != pcList->alInstances.end();++i)
  479. {
  480. // convert the vertices to sp floats
  481. aiVector3D vOut;
  482. if (0xFFFFFFFF != aiPositions[0])
  483. {
  484. vOut.x = PLY::PropertyInstance::ConvertTo<float>(
  485. (*i)->alProperties[aiPositions[0]].avList.front(),aiTypes[0]);
  486. }
  487. if (0xFFFFFFFF != aiPositions[1])
  488. {
  489. vOut.y = PLY::PropertyInstance::ConvertTo<float>(
  490. (*i)->alProperties[aiPositions[1]].avList.front(),aiTypes[1]);
  491. }
  492. if (0xFFFFFFFF != aiPositions[2])
  493. {
  494. vOut.z = PLY::PropertyInstance::ConvertTo<float>(
  495. (*i)->alProperties[aiPositions[2]].avList.front(),aiTypes[2]);
  496. }
  497. // and add them to our nice list
  498. pvOut->push_back(vOut);
  499. }
  500. }
  501. return;
  502. }
  503. // ------------------------------------------------------------------------------------------------
  504. float PLYImporter::NormalizeColorValue (PLY::PropertyInstance::ValueUnion val,
  505. PLY::EDataType eType)
  506. {
  507. switch (eType)
  508. {
  509. case EDT_Float:
  510. return val.fFloat;
  511. case EDT_Double:
  512. return (float)val.fDouble;
  513. case EDT_UChar:
  514. return (float)val.iUInt / (float)0xFF;
  515. case EDT_Char:
  516. return (float)(val.iInt+(0xFF/2)) / (float)0xFF;
  517. case EDT_UShort:
  518. return (float)val.iUInt / (float)0xFFFF;
  519. case EDT_Short:
  520. return (float)(val.iInt+(0xFFFF/2)) / (float)0xFFFF;
  521. case EDT_UInt:
  522. return (float)val.iUInt / (float)0xFFFF;
  523. case EDT_Int:
  524. return ((float)val.iInt / (float)0xFF) + 0.5f;
  525. };
  526. return 0.0f;
  527. }
  528. // ------------------------------------------------------------------------------------------------
  529. void PLYImporter::LoadVertexColor(std::vector<aiColor4D>* pvOut)
  530. {
  531. ai_assert(NULL != pvOut);
  532. unsigned int aiPositions[4] = {0xFFFFFFFF,0xFFFFFFFF,0xFFFFFFFF,0xFFFFFFFF};
  533. PLY::EDataType aiTypes[4];
  534. unsigned int cnt = 0;
  535. PLY::ElementInstanceList* pcList = NULL;
  536. // serach in the DOM for a vertex entry
  537. unsigned int _i = 0;
  538. for (std::vector<PLY::Element*>::const_iterator
  539. i = this->pcDOM->alElements.begin();
  540. i != this->pcDOM->alElements.end();++i,++_i)
  541. {
  542. if (PLY::EEST_Vertex == (*i)->eSemantic)
  543. {
  544. pcList = this->pcDOM->alElementData[_i];
  545. // now check whether which coordinate sets are available
  546. unsigned int _a = 0;
  547. for (std::vector<PLY::Property*>::const_iterator
  548. a = (*i)->alProperties.begin();
  549. a != (*i)->alProperties.end();++a,++_a)
  550. {
  551. if ((*a)->bIsList)continue;
  552. if (PLY::EST_Red == (*a)->Semantic)
  553. {
  554. cnt++;
  555. aiPositions[0] = _a;
  556. aiTypes[0] = (*a)->eType;
  557. }
  558. else if (PLY::EST_Green == (*a)->Semantic)
  559. {
  560. cnt++;
  561. aiPositions[1] = _a;
  562. aiTypes[1] = (*a)->eType;
  563. }
  564. else if (PLY::EST_Blue == (*a)->Semantic)
  565. {
  566. cnt++;
  567. aiPositions[2] = _a;
  568. aiTypes[2] = (*a)->eType;
  569. }
  570. else if (PLY::EST_Alpha == (*a)->Semantic)
  571. {
  572. cnt++;
  573. aiPositions[3] = _a;
  574. aiTypes[3] = (*a)->eType;
  575. }
  576. if (4 == cnt)break;
  577. }
  578. break;
  579. }
  580. }
  581. // check whether we have a valid source for the vertex data
  582. if (NULL != pcList && 0 != cnt)
  583. {
  584. pvOut->reserve(pcList->alInstances.size());
  585. for (std::vector<ElementInstance*>::const_iterator
  586. i = pcList->alInstances.begin();
  587. i != pcList->alInstances.end();++i)
  588. {
  589. // convert the vertices to sp floats
  590. aiColor4D vOut;
  591. if (0xFFFFFFFF != aiPositions[0])
  592. {
  593. vOut.r = NormalizeColorValue((*i)->alProperties[
  594. aiPositions[0]].avList.front(),aiTypes[0]);
  595. }
  596. if (0xFFFFFFFF != aiPositions[1])
  597. {
  598. vOut.g = NormalizeColorValue((*i)->alProperties[
  599. aiPositions[1]].avList.front(),aiTypes[1]);
  600. }
  601. if (0xFFFFFFFF != aiPositions[2])
  602. {
  603. vOut.b = NormalizeColorValue((*i)->alProperties[
  604. aiPositions[2]].avList.front(),aiTypes[2]);
  605. }
  606. // assume 1.0 for the alpha channel ifit is not set
  607. if (0xFFFFFFFF == aiPositions[3])vOut.a = 1.0f;
  608. else
  609. {
  610. vOut.a = NormalizeColorValue((*i)->alProperties[
  611. aiPositions[3]].avList.front(),aiTypes[3]);
  612. }
  613. // and add them to our nice list
  614. pvOut->push_back(vOut);
  615. }
  616. }
  617. return;
  618. }
  619. // ------------------------------------------------------------------------------------------------
  620. void PLYImporter::LoadFaces(std::vector<PLY::Face>* pvOut)
  621. {
  622. ai_assert(NULL != pvOut);
  623. PLY::ElementInstanceList* pcList = NULL;
  624. bool bOne = false;
  625. // index of the vertex index list
  626. unsigned int iProperty = 0xFFFFFFFF;
  627. PLY::EDataType eType;
  628. bool bIsTristrip = false;
  629. // index of the material index property
  630. unsigned int iMaterialIndex = 0xFFFFFFFF;
  631. PLY::EDataType eType2;
  632. // serach in the DOM for a face entry
  633. unsigned int _i = 0;
  634. for (std::vector<PLY::Element*>::const_iterator
  635. i = this->pcDOM->alElements.begin();
  636. i != this->pcDOM->alElements.end();++i,++_i)
  637. {
  638. // face = unique number of vertex indices
  639. if (PLY::EEST_Face == (*i)->eSemantic)
  640. {
  641. pcList = this->pcDOM->alElementData[_i];
  642. unsigned int _a = 0;
  643. for (std::vector<PLY::Property*>::const_iterator
  644. a = (*i)->alProperties.begin();
  645. a != (*i)->alProperties.end();++a,++_a)
  646. {
  647. if (PLY::EST_VertexIndex == (*a)->Semantic)
  648. {
  649. // must be a dynamic list!
  650. if (!(*a)->bIsList)continue;
  651. iProperty = _a;
  652. bOne = true;
  653. eType = (*a)->eType;
  654. }
  655. else if (PLY::EST_MaterialIndex == (*a)->Semantic)
  656. {
  657. if ((*a)->bIsList)continue;
  658. iMaterialIndex = _a;
  659. bOne = true;
  660. eType2 = (*a)->eType;
  661. }
  662. }
  663. break;
  664. }
  665. // triangle strip
  666. // TODO: triangle strip and material index support???
  667. else if (PLY::EEST_TriStrip == (*i)->eSemantic)
  668. {
  669. // find a list property in this ...
  670. pcList = this->pcDOM->alElementData[_i];
  671. unsigned int _a = 0;
  672. for (std::vector<PLY::Property*>::const_iterator
  673. a = (*i)->alProperties.begin();
  674. a != (*i)->alProperties.end();++a,++_a)
  675. {
  676. // must be a dynamic list!
  677. if (!(*a)->bIsList)continue;
  678. iProperty = _a;
  679. bOne = true;
  680. bIsTristrip = true;
  681. eType = (*a)->eType;
  682. break;
  683. }
  684. break;
  685. }
  686. }
  687. // check whether we have at least one per-face information set
  688. if (pcList && bOne)
  689. {
  690. if (!bIsTristrip)
  691. {
  692. pvOut->reserve(pcList->alInstances.size());
  693. for (std::vector<ElementInstance*>::const_iterator
  694. i = pcList->alInstances.begin();
  695. i != pcList->alInstances.end();++i)
  696. {
  697. PLY::Face sFace;
  698. // parse the list of vertex indices
  699. if (0xFFFFFFFF != iProperty)
  700. {
  701. const unsigned int iNum = (unsigned int)(*i)->alProperties[iProperty].avList.size();
  702. sFace.mIndices.resize(iNum);
  703. if (3 > iNum)
  704. {
  705. // We must filter out all degenerates.
  706. DefaultLogger::get()->warn("PLY: Found degenerated triangle");
  707. continue;
  708. }
  709. std::list<PLY::PropertyInstance::ValueUnion>::const_iterator p =
  710. (*i)->alProperties[iProperty].avList.begin();
  711. for (unsigned int a = 0; a < iNum;++a,++p)
  712. {
  713. sFace.mIndices[a] = PLY::PropertyInstance::ConvertTo<unsigned int>(*p,eType);
  714. }
  715. }
  716. // parse the material index
  717. if (0xFFFFFFFF != iMaterialIndex)
  718. {
  719. sFace.iMaterialIndex = PLY::PropertyInstance::ConvertTo<unsigned int>(
  720. (*i)->alProperties[iMaterialIndex].avList.front(),eType2);
  721. }
  722. pvOut->push_back(sFace);
  723. }
  724. }
  725. else // triangle strips
  726. {
  727. // normally we have only one triangle strip instance where
  728. // a value of -1 indicates a restart of the strip
  729. for (std::vector<ElementInstance*>::const_iterator
  730. i = pcList->alInstances.begin();
  731. i != pcList->alInstances.end();++i)
  732. {
  733. int aiTable[2] = {-1,-1};
  734. for (std::list<PLY::PropertyInstance::ValueUnion>::const_iterator
  735. a = (*i)->alProperties[iProperty].avList.begin();
  736. a != (*i)->alProperties[iProperty].avList.end();++a)
  737. {
  738. int p = PLY::PropertyInstance::ConvertTo<int>(*a,eType);
  739. if (-1 == p)
  740. {
  741. // restart the strip ...
  742. aiTable[0] = aiTable[1] = -1;
  743. continue;
  744. }
  745. if (-1 == aiTable[0])
  746. {
  747. aiTable[0] = p;
  748. continue;
  749. }
  750. if (-1 == aiTable[1])
  751. {
  752. aiTable[1] = p;
  753. continue;
  754. }
  755. PLY::Face sFace;
  756. sFace.mIndices.push_back((unsigned int)aiTable[0]);
  757. sFace.mIndices.push_back((unsigned int)aiTable[1]);
  758. sFace.mIndices.push_back((unsigned int)p);
  759. pvOut->push_back(sFace);
  760. aiTable[0] = aiTable[1];
  761. aiTable[1] = p;
  762. }
  763. }
  764. }
  765. }
  766. return;
  767. }
  768. // ------------------------------------------------------------------------------------------------
  769. void PLYImporter::GetMaterialColor(const std::vector<PLY::PropertyInstance>& avList,
  770. unsigned int aiPositions[4],
  771. PLY::EDataType aiTypes[4],
  772. aiColor4D* clrOut)
  773. {
  774. ai_assert(NULL != clrOut);
  775. if (0xFFFFFFFF == aiPositions[0])clrOut->r = 0.0f;
  776. else
  777. {
  778. clrOut->r = NormalizeColorValue(avList[
  779. aiPositions[0]].avList.front(),aiTypes[0]);
  780. }
  781. if (0xFFFFFFFF == aiPositions[1])clrOut->g = 0.0f;
  782. else
  783. {
  784. clrOut->g = NormalizeColorValue(avList[
  785. aiPositions[1]].avList.front(),aiTypes[1]);
  786. }
  787. if (0xFFFFFFFF == aiPositions[2])clrOut->b = 0.0f;
  788. else
  789. {
  790. clrOut->b = NormalizeColorValue(avList[
  791. aiPositions[2]].avList.front(),aiTypes[2]);
  792. }
  793. // assume 1.0 for the alpha channel ifit is not set
  794. if (0xFFFFFFFF == aiPositions[3])clrOut->a = 1.0f;
  795. else
  796. {
  797. clrOut->a = NormalizeColorValue(avList[
  798. aiPositions[3]].avList.front(),aiTypes[3]);
  799. }
  800. return;
  801. }
  802. // ------------------------------------------------------------------------------------------------
  803. void PLYImporter::LoadMaterial(std::vector<MaterialHelper*>* pvOut)
  804. {
  805. ai_assert(NULL != pvOut);
  806. // diffuse[4], specular[4], ambient[4]
  807. // rgba order
  808. unsigned int aaiPositions[3][4] = {
  809. {0xFFFFFFFF,0xFFFFFFFF,0xFFFFFFFF,0xFFFFFFFF},
  810. {0xFFFFFFFF,0xFFFFFFFF,0xFFFFFFFF,0xFFFFFFFF},
  811. {0xFFFFFFFF,0xFFFFFFFF,0xFFFFFFFF,0xFFFFFFFF},
  812. };
  813. // dto.
  814. PLY::EDataType aaiTypes[3][4];
  815. PLY::ElementInstanceList* pcList = NULL;
  816. unsigned int iPhong = 0xFFFFFFFF;
  817. PLY::EDataType ePhong;
  818. unsigned int iOpacity = 0xFFFFFFFF;
  819. PLY::EDataType eOpacity;
  820. // serach in the DOM for a vertex entry
  821. unsigned int _i = 0;
  822. for (std::vector<PLY::Element*>::const_iterator
  823. i = this->pcDOM->alElements.begin();
  824. i != this->pcDOM->alElements.end();++i,++_i)
  825. {
  826. if (PLY::EEST_Material == (*i)->eSemantic)
  827. {
  828. pcList = this->pcDOM->alElementData[_i];
  829. // now check whether which coordinate sets are available
  830. unsigned int _a = 0;
  831. for (std::vector<PLY::Property*>::const_iterator
  832. a = (*i)->alProperties.begin();
  833. a != (*i)->alProperties.end();++a,++_a)
  834. {
  835. if ((*a)->bIsList)continue;
  836. // pohng specularity -----------------------------------
  837. if (PLY::EST_PhongPower == (*a)->Semantic)
  838. {
  839. iPhong = _a;
  840. ePhong = (*a)->eType;
  841. }
  842. // general opacity -----------------------------------
  843. if (PLY::EST_Opacity == (*a)->Semantic)
  844. {
  845. iOpacity = _a;
  846. eOpacity = (*a)->eType;
  847. }
  848. // diffuse color channels -----------------------------------
  849. if (PLY::EST_DiffuseRed == (*a)->Semantic)
  850. {
  851. aaiPositions[0][0] = _a;
  852. aaiTypes[0][0] = (*a)->eType;
  853. }
  854. else if (PLY::EST_DiffuseGreen == (*a)->Semantic)
  855. {
  856. aaiPositions[0][1] = _a;
  857. aaiTypes[0][1] = (*a)->eType;
  858. }
  859. else if (PLY::EST_DiffuseBlue == (*a)->Semantic)
  860. {
  861. aaiPositions[0][2] = _a;
  862. aaiTypes[0][2] = (*a)->eType;
  863. }
  864. else if (PLY::EST_DiffuseAlpha == (*a)->Semantic)
  865. {
  866. aaiPositions[0][3] = _a;
  867. aaiTypes[0][3] = (*a)->eType;
  868. }
  869. // specular color channels -----------------------------------
  870. else if (PLY::EST_SpecularRed == (*a)->Semantic)
  871. {
  872. aaiPositions[1][0] = _a;
  873. aaiTypes[1][0] = (*a)->eType;
  874. }
  875. else if (PLY::EST_SpecularGreen == (*a)->Semantic)
  876. {
  877. aaiPositions[1][1] = _a;
  878. aaiTypes[1][1] = (*a)->eType;
  879. }
  880. else if (PLY::EST_SpecularBlue == (*a)->Semantic)
  881. {
  882. aaiPositions[1][2] = _a;
  883. aaiTypes[1][2] = (*a)->eType;
  884. }
  885. else if (PLY::EST_SpecularAlpha == (*a)->Semantic)
  886. {
  887. aaiPositions[1][3] = _a;
  888. aaiTypes[1][3] = (*a)->eType;
  889. }
  890. // ambient color channels -----------------------------------
  891. else if (PLY::EST_AmbientRed == (*a)->Semantic)
  892. {
  893. aaiPositions[2][0] = _a;
  894. aaiTypes[2][0] = (*a)->eType;
  895. }
  896. else if (PLY::EST_AmbientGreen == (*a)->Semantic)
  897. {
  898. aaiPositions[2][1] = _a;
  899. aaiTypes[2][1] = (*a)->eType;
  900. }
  901. else if (PLY::EST_AmbientBlue == (*a)->Semantic)
  902. {
  903. aaiPositions[22][2] = _a;
  904. aaiTypes[2][2] = (*a)->eType;
  905. }
  906. else if (PLY::EST_AmbientAlpha == (*a)->Semantic)
  907. {
  908. aaiPositions[2][3] = _a;
  909. aaiTypes[2][3] = (*a)->eType;
  910. }
  911. }
  912. break;
  913. }
  914. }
  915. // check whether we have a valid source for the material data
  916. if (NULL != pcList)
  917. {
  918. for (std::vector<ElementInstance*>::const_iterator
  919. i = pcList->alInstances.begin();
  920. i != pcList->alInstances.end();++i)
  921. {
  922. aiColor4D clrOut;
  923. MaterialHelper* pcHelper = new MaterialHelper();
  924. // build the diffuse material color
  925. GetMaterialColor((*i)->alProperties,aaiPositions[0],aaiTypes[0],&clrOut);
  926. pcHelper->AddProperty<aiColor4D>(&clrOut,1,AI_MATKEY_COLOR_DIFFUSE);
  927. // build the specular material color
  928. GetMaterialColor((*i)->alProperties,aaiPositions[1],aaiTypes[1],&clrOut);
  929. pcHelper->AddProperty<aiColor4D>(&clrOut,1,AI_MATKEY_COLOR_SPECULAR);
  930. // build the ambient material color
  931. GetMaterialColor((*i)->alProperties,aaiPositions[2],aaiTypes[2],&clrOut);
  932. pcHelper->AddProperty<aiColor4D>(&clrOut,1,AI_MATKEY_COLOR_AMBIENT);
  933. // handle phong power and shading mode
  934. int iMode;
  935. if (0xFFFFFFFF != iPhong)
  936. {
  937. float fSpec = PLY::PropertyInstance::ConvertTo<float>(
  938. (*i)->alProperties[iPhong].avList.front(),ePhong);
  939. // if shininess is 0 (and the pow() calculation would therefore always
  940. // become 1, not depending on the angle) use gouraud lighting
  941. if (fSpec)
  942. {
  943. // scale this with 15 ... hopefully this is correct
  944. fSpec *= 15;
  945. pcHelper->AddProperty<float>(&fSpec, 1, AI_MATKEY_SHININESS);
  946. iMode = (int)aiShadingMode_Phong;
  947. }
  948. else iMode = (int)aiShadingMode_Gouraud;
  949. }
  950. else iMode = (int)aiShadingMode_Gouraud;
  951. pcHelper->AddProperty<int>(&iMode, 1, AI_MATKEY_SHADING_MODEL);
  952. // handle opacity
  953. if (0xFFFFFFFF != iOpacity)
  954. {
  955. float fOpacity = PLY::PropertyInstance::ConvertTo<float>(
  956. (*i)->alProperties[iPhong].avList.front(),eOpacity);
  957. pcHelper->AddProperty<float>(&fOpacity, 1, AI_MATKEY_OPACITY);
  958. }
  959. // add the newly created material instance to the list
  960. pvOut->push_back(pcHelper);
  961. }
  962. }
  963. return;
  964. }