PlyLoader.cpp 33 KB

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