PlyLoader.cpp 32 KB

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