PlyLoader.cpp 40 KB

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