PlyLoader.cpp 33 KB

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