PlyLoader.cpp 33 KB

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