PlyLoader.cpp 33 KB

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