PlyLoader.cpp 32 KB

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