PlyLoader.cpp 32 KB

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