PlyLoader.cpp 33 KB

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