PlyLoader.cpp 32 KB

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