PlyLoader.cpp 32 KB

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