PlyLoader.cpp 31 KB

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