X3DImporter_Postprocess.cpp 42 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731
  1. /*
  2. Open Asset Import Library (assimp)
  3. ----------------------------------------------------------------------
  4. Copyright (c) 2006-2019, assimp team
  5. All rights reserved.
  6. Redistribution and use of this software in source and binary forms,
  7. with or without modification, are permitted provided that the
  8. following conditions are met:
  9. * Redistributions of source code must retain the above
  10. copyright notice, this list of conditions and the
  11. following disclaimer.
  12. * Redistributions in binary form must reproduce the above
  13. copyright notice, this list of conditions and the
  14. following disclaimer in the documentation and/or other
  15. materials provided with the distribution.
  16. * Neither the name of the assimp team, nor the names of its
  17. contributors may be used to endorse or promote products
  18. derived from this software without specific prior
  19. written permission of the assimp team.
  20. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  21. "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  22. LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  23. A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  24. OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  25. SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  26. LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  27. DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  28. THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  29. (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  30. OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  31. ----------------------------------------------------------------------
  32. */
  33. /// \file X3DImporter_Postprocess.cpp
  34. /// \brief Convert built scenegraph and objects to Assimp scenegraph.
  35. /// \date 2015-2016
  36. /// \author [email protected]
  37. #ifndef ASSIMP_BUILD_NO_X3D_IMPORTER
  38. #include "X3DGeoHelper.h"
  39. #include "X3DImporter.hpp"
  40. // Header files, Assimp.
  41. #include <assimp/StandardShapes.h>
  42. #include <assimp/StringUtils.h>
  43. #include <assimp/ai_assert.h>
  44. // Header files, stdlib.
  45. #include <algorithm>
  46. #include <iterator>
  47. #include <string>
  48. namespace Assimp {
  49. aiMatrix4x4 X3DImporter::PostprocessHelper_Matrix_GlobalToCurrent() const {
  50. X3DNodeElementBase *cur_node;
  51. std::list<aiMatrix4x4> matr;
  52. aiMatrix4x4 out_matr;
  53. // starting walk from current element to root
  54. cur_node = mNodeElementCur;
  55. if (cur_node != nullptr) {
  56. do {
  57. // if cur_node is group then store group transformation matrix in list.
  58. if (cur_node->Type == X3DElemType::ENET_Group) matr.push_back(((X3DNodeElementGroup *)cur_node)->Transformation);
  59. cur_node = cur_node->Parent;
  60. } while (cur_node != nullptr);
  61. }
  62. // multiplicate all matrices in reverse order
  63. for (std::list<aiMatrix4x4>::reverse_iterator rit = matr.rbegin(); rit != matr.rend(); ++rit)
  64. out_matr = out_matr * (*rit);
  65. return out_matr;
  66. }
  67. void X3DImporter::PostprocessHelper_CollectMetadata(const X3DNodeElementBase &pNodeElement, std::list<X3DNodeElementBase *> &pList) const {
  68. // walk through childs and find for metadata.
  69. for (std::list<X3DNodeElementBase *>::const_iterator el_it = pNodeElement.Children.begin(); el_it != pNodeElement.Children.end(); ++el_it) {
  70. if (((*el_it)->Type == X3DElemType::ENET_MetaBoolean) || ((*el_it)->Type == X3DElemType::ENET_MetaDouble) ||
  71. ((*el_it)->Type == X3DElemType::ENET_MetaFloat) || ((*el_it)->Type == X3DElemType::ENET_MetaInteger) ||
  72. ((*el_it)->Type == X3DElemType::ENET_MetaString)) {
  73. pList.push_back(*el_it);
  74. } else if ((*el_it)->Type == X3DElemType::ENET_MetaSet) {
  75. PostprocessHelper_CollectMetadata(**el_it, pList);
  76. }
  77. } // for(std::list<X3DNodeElementBase*>::const_iterator el_it = pNodeElement.Children.begin(); el_it != pNodeElement.Children.end(); el_it++)
  78. }
  79. bool X3DImporter::PostprocessHelper_ElementIsMetadata(const X3DElemType pType) const {
  80. if ((pType == X3DElemType::ENET_MetaBoolean) || (pType == X3DElemType::ENET_MetaDouble) ||
  81. (pType == X3DElemType::ENET_MetaFloat) || (pType == X3DElemType::ENET_MetaInteger) ||
  82. (pType == X3DElemType::ENET_MetaString) || (pType == X3DElemType::ENET_MetaSet)) {
  83. return true;
  84. } else {
  85. return false;
  86. }
  87. }
  88. bool X3DImporter::PostprocessHelper_ElementIsMesh(const X3DElemType pType) const {
  89. if ((pType == X3DElemType::ENET_Arc2D) || (pType == X3DElemType::ENET_ArcClose2D) ||
  90. (pType == X3DElemType::ENET_Box) || (pType == X3DElemType::ENET_Circle2D) ||
  91. (pType == X3DElemType::ENET_Cone) || (pType == X3DElemType::ENET_Cylinder) ||
  92. (pType == X3DElemType::ENET_Disk2D) || (pType == X3DElemType::ENET_ElevationGrid) ||
  93. (pType == X3DElemType::ENET_Extrusion) || (pType == X3DElemType::ENET_IndexedFaceSet) ||
  94. (pType == X3DElemType::ENET_IndexedLineSet) || (pType == X3DElemType::ENET_IndexedTriangleFanSet) ||
  95. (pType == X3DElemType::ENET_IndexedTriangleSet) || (pType == X3DElemType::ENET_IndexedTriangleStripSet) ||
  96. (pType == X3DElemType::ENET_PointSet) || (pType == X3DElemType::ENET_LineSet) ||
  97. (pType == X3DElemType::ENET_Polyline2D) || (pType == X3DElemType::ENET_Polypoint2D) ||
  98. (pType == X3DElemType::ENET_Rectangle2D) || (pType == X3DElemType::ENET_Sphere) ||
  99. (pType == X3DElemType::ENET_TriangleFanSet) || (pType == X3DElemType::ENET_TriangleSet) ||
  100. (pType == X3DElemType::ENET_TriangleSet2D) || (pType == X3DElemType::ENET_TriangleStripSet)) {
  101. return true;
  102. } else {
  103. return false;
  104. }
  105. }
  106. void X3DImporter::Postprocess_BuildLight(const X3DNodeElementBase &pNodeElement, std::list<aiLight *> &pSceneLightList) const {
  107. const X3DNodeElementLight &ne = *((X3DNodeElementLight *)&pNodeElement);
  108. aiMatrix4x4 transform_matr = PostprocessHelper_Matrix_GlobalToCurrent();
  109. aiLight *new_light = new aiLight;
  110. new_light->mName = ne.ID;
  111. new_light->mColorAmbient = ne.Color * ne.AmbientIntensity;
  112. new_light->mColorDiffuse = ne.Color * ne.Intensity;
  113. new_light->mColorSpecular = ne.Color * ne.Intensity;
  114. switch (pNodeElement.Type) {
  115. case X3DElemType::ENET_DirectionalLight:
  116. new_light->mType = aiLightSource_DIRECTIONAL;
  117. new_light->mDirection = ne.Direction, new_light->mDirection *= transform_matr;
  118. break;
  119. case X3DElemType::ENET_PointLight:
  120. new_light->mType = aiLightSource_POINT;
  121. new_light->mPosition = ne.Location, new_light->mPosition *= transform_matr;
  122. new_light->mAttenuationConstant = ne.Attenuation.x;
  123. new_light->mAttenuationLinear = ne.Attenuation.y;
  124. new_light->mAttenuationQuadratic = ne.Attenuation.z;
  125. break;
  126. case X3DElemType::ENET_SpotLight:
  127. new_light->mType = aiLightSource_SPOT;
  128. new_light->mPosition = ne.Location, new_light->mPosition *= transform_matr;
  129. new_light->mDirection = ne.Direction, new_light->mDirection *= transform_matr;
  130. new_light->mAttenuationConstant = ne.Attenuation.x;
  131. new_light->mAttenuationLinear = ne.Attenuation.y;
  132. new_light->mAttenuationQuadratic = ne.Attenuation.z;
  133. new_light->mAngleInnerCone = ne.BeamWidth;
  134. new_light->mAngleOuterCone = ne.CutOffAngle;
  135. break;
  136. default:
  137. throw DeadlyImportError("Postprocess_BuildLight. Unknown type of light: " + ai_to_string(pNodeElement.Type) + ".");
  138. }
  139. pSceneLightList.push_back(new_light);
  140. }
  141. void X3DImporter::Postprocess_BuildMaterial(const X3DNodeElementBase &pNodeElement, aiMaterial **pMaterial) const {
  142. // check argument
  143. if (pMaterial == nullptr) throw DeadlyImportError("Postprocess_BuildMaterial. pMaterial is nullptr.");
  144. if (*pMaterial != nullptr) throw DeadlyImportError("Postprocess_BuildMaterial. *pMaterial must be nullptr.");
  145. *pMaterial = new aiMaterial;
  146. aiMaterial &taimat = **pMaterial; // creating alias for convenience.
  147. // at this point pNodeElement point to <Appearance> node. Walk through childs and add all stored data.
  148. for (std::list<X3DNodeElementBase *>::const_iterator el_it = pNodeElement.Children.begin(); el_it != pNodeElement.Children.end(); ++el_it) {
  149. if ((*el_it)->Type == X3DElemType::ENET_Material) {
  150. aiColor3D tcol3;
  151. float tvalf;
  152. X3DNodeElementMaterial &tnemat = *((X3DNodeElementMaterial *)*el_it);
  153. tcol3.r = tnemat.AmbientIntensity, tcol3.g = tnemat.AmbientIntensity, tcol3.b = tnemat.AmbientIntensity;
  154. taimat.AddProperty(&tcol3, 1, AI_MATKEY_COLOR_AMBIENT);
  155. taimat.AddProperty(&tnemat.DiffuseColor, 1, AI_MATKEY_COLOR_DIFFUSE);
  156. taimat.AddProperty(&tnemat.EmissiveColor, 1, AI_MATKEY_COLOR_EMISSIVE);
  157. taimat.AddProperty(&tnemat.SpecularColor, 1, AI_MATKEY_COLOR_SPECULAR);
  158. tvalf = 1;
  159. taimat.AddProperty(&tvalf, 1, AI_MATKEY_SHININESS_STRENGTH);
  160. taimat.AddProperty(&tnemat.Shininess, 1, AI_MATKEY_SHININESS);
  161. tvalf = 1.0f - tnemat.Transparency;
  162. taimat.AddProperty(&tvalf, 1, AI_MATKEY_OPACITY);
  163. } // if((*el_it)->Type == X3DElemType::ENET_Material)
  164. else if ((*el_it)->Type == X3DElemType::ENET_ImageTexture) {
  165. X3DNodeElementImageTexture &tnetex = *((X3DNodeElementImageTexture *)*el_it);
  166. aiString url_str(tnetex.URL.c_str());
  167. int mode = aiTextureOp_Multiply;
  168. taimat.AddProperty(&url_str, AI_MATKEY_TEXTURE_DIFFUSE(0));
  169. taimat.AddProperty(&tnetex.RepeatS, 1, AI_MATKEY_MAPPINGMODE_U_DIFFUSE(0));
  170. taimat.AddProperty(&tnetex.RepeatT, 1, AI_MATKEY_MAPPINGMODE_V_DIFFUSE(0));
  171. taimat.AddProperty(&mode, 1, AI_MATKEY_TEXOP_DIFFUSE(0));
  172. } // else if((*el_it)->Type == X3DElemType::ENET_ImageTexture)
  173. else if ((*el_it)->Type == X3DElemType::ENET_TextureTransform) {
  174. aiUVTransform trans;
  175. X3DNodeElementTextureTransform &tnetextr = *((X3DNodeElementTextureTransform *)*el_it);
  176. trans.mTranslation = tnetextr.Translation - tnetextr.Center;
  177. trans.mScaling = tnetextr.Scale;
  178. trans.mRotation = tnetextr.Rotation;
  179. taimat.AddProperty(&trans, 1, AI_MATKEY_UVTRANSFORM_DIFFUSE(0));
  180. } // else if((*el_it)->Type == X3DElemType::ENET_TextureTransform)
  181. } // for(std::list<X3DNodeElementBase*>::const_iterator el_it = pNodeElement.Children.begin(); el_it != pNodeElement.Children.end(); el_it++)
  182. }
  183. void X3DImporter::Postprocess_BuildMesh(const X3DNodeElementBase &pNodeElement, aiMesh **pMesh) const {
  184. // check argument
  185. if (pMesh == nullptr) throw DeadlyImportError("Postprocess_BuildMesh. pMesh is nullptr.");
  186. if (*pMesh != nullptr) throw DeadlyImportError("Postprocess_BuildMesh. *pMesh must be nullptr.");
  187. /************************************************************************************************************************************/
  188. /************************************************************ Geometry2D ************************************************************/
  189. /************************************************************************************************************************************/
  190. if ((pNodeElement.Type == X3DElemType::ENET_Arc2D) || (pNodeElement.Type == X3DElemType::ENET_ArcClose2D) ||
  191. (pNodeElement.Type == X3DElemType::ENET_Circle2D) || (pNodeElement.Type == X3DElemType::ENET_Disk2D) ||
  192. (pNodeElement.Type == X3DElemType::ENET_Polyline2D) || (pNodeElement.Type == X3DElemType::ENET_Polypoint2D) ||
  193. (pNodeElement.Type == X3DElemType::ENET_Rectangle2D) || (pNodeElement.Type == X3DElemType::ENET_TriangleSet2D)) {
  194. X3DNodeElementGeometry2D &tnemesh = *((X3DNodeElementGeometry2D *)&pNodeElement); // create alias for convenience
  195. std::vector<aiVector3D> tarr;
  196. tarr.reserve(tnemesh.Vertices.size());
  197. for (std::list<aiVector3D>::iterator it = tnemesh.Vertices.begin(); it != tnemesh.Vertices.end(); ++it)
  198. tarr.push_back(*it);
  199. *pMesh = StandardShapes::MakeMesh(tarr, static_cast<unsigned int>(tnemesh.NumIndices)); // create mesh from vertices using Assimp help.
  200. return; // mesh is build, nothing to do anymore.
  201. }
  202. /************************************************************************************************************************************/
  203. /************************************************************ Geometry3D ************************************************************/
  204. /************************************************************************************************************************************/
  205. //
  206. // Predefined figures
  207. //
  208. if ((pNodeElement.Type == X3DElemType::ENET_Box) || (pNodeElement.Type == X3DElemType::ENET_Cone) ||
  209. (pNodeElement.Type == X3DElemType::ENET_Cylinder) || (pNodeElement.Type == X3DElemType::ENET_Sphere)) {
  210. X3DNodeElementGeometry3D &tnemesh = *((X3DNodeElementGeometry3D *)&pNodeElement); // create alias for convenience
  211. std::vector<aiVector3D> tarr;
  212. tarr.reserve(tnemesh.Vertices.size());
  213. for (std::list<aiVector3D>::iterator it = tnemesh.Vertices.begin(); it != tnemesh.Vertices.end(); ++it)
  214. tarr.push_back(*it);
  215. *pMesh = StandardShapes::MakeMesh(tarr, static_cast<unsigned int>(tnemesh.NumIndices)); // create mesh from vertices using Assimp help.
  216. return; // mesh is build, nothing to do anymore.
  217. }
  218. //
  219. // Parametric figures
  220. //
  221. if (pNodeElement.Type == X3DElemType::ENET_ElevationGrid) {
  222. X3DNodeElementElevationGrid &tnemesh = *((X3DNodeElementElevationGrid *)&pNodeElement); // create alias for convenience
  223. // at first create mesh from existing vertices.
  224. *pMesh = X3DGeoHelper::make_mesh(tnemesh.CoordIdx, tnemesh.Vertices);
  225. // copy additional information from children
  226. for (std::list<X3DNodeElementBase *>::iterator ch_it = tnemesh.Children.begin(); ch_it != tnemesh.Children.end(); ++ch_it) {
  227. if ((*ch_it)->Type == X3DElemType::ENET_Color)
  228. X3DGeoHelper::add_color(**pMesh, ((X3DNodeElementColor *)*ch_it)->Value, tnemesh.ColorPerVertex);
  229. else if ((*ch_it)->Type == X3DElemType::ENET_ColorRGBA)
  230. X3DGeoHelper::add_color(**pMesh, ((X3DNodeElementColorRGBA *)*ch_it)->Value, tnemesh.ColorPerVertex);
  231. else if ((*ch_it)->Type == X3DElemType::ENET_Normal)
  232. X3DGeoHelper::add_normal(**pMesh, ((X3DNodeElementNormal *)*ch_it)->Value, tnemesh.NormalPerVertex);
  233. else if ((*ch_it)->Type == X3DElemType::ENET_TextureCoordinate)
  234. X3DGeoHelper::add_tex_coord(**pMesh, ((X3DNodeElementTextureCoordinate *)*ch_it)->Value);
  235. else
  236. throw DeadlyImportError("Postprocess_BuildMesh. Unknown child of ElevationGrid: " + ai_to_string((*ch_it)->Type) + ".");
  237. } // for(std::list<X3DNodeElementBase*>::iterator ch_it = tnemesh.Children.begin(); ch_it != tnemesh.Children.end(); ++ch_it)
  238. return; // mesh is build, nothing to do anymore.
  239. } // if(pNodeElement.Type == X3DElemType::ENET_ElevationGrid)
  240. //
  241. // Indexed primitives sets
  242. //
  243. if (pNodeElement.Type == X3DElemType::ENET_IndexedFaceSet) {
  244. X3DNodeElementIndexedSet &tnemesh = *((X3DNodeElementIndexedSet *)&pNodeElement); // create alias for convenience
  245. // at first search for <Coordinate> node and create mesh.
  246. for (std::list<X3DNodeElementBase *>::iterator ch_it = tnemesh.Children.begin(); ch_it != tnemesh.Children.end(); ++ch_it) {
  247. if ((*ch_it)->Type == X3DElemType::ENET_Coordinate) {
  248. *pMesh = X3DGeoHelper::make_mesh(tnemesh.CoordIndex, ((X3DNodeElementCoordinate *)*ch_it)->Value);
  249. }
  250. }
  251. // copy additional information from children
  252. for (std::list<X3DNodeElementBase *>::iterator ch_it = tnemesh.Children.begin(); ch_it != tnemesh.Children.end(); ++ch_it) {
  253. if ((*ch_it)->Type == X3DElemType::ENET_Color)
  254. X3DGeoHelper::add_color(**pMesh, tnemesh.CoordIndex, tnemesh.ColorIndex, ((X3DNodeElementColor *)*ch_it)->Value, tnemesh.ColorPerVertex);
  255. else if ((*ch_it)->Type == X3DElemType::ENET_ColorRGBA)
  256. X3DGeoHelper::add_color(**pMesh, tnemesh.CoordIndex, tnemesh.ColorIndex, ((X3DNodeElementColorRGBA *)*ch_it)->Value,
  257. tnemesh.ColorPerVertex);
  258. else if ((*ch_it)->Type == X3DElemType::ENET_Coordinate) {
  259. } // skip because already read when mesh created.
  260. else if ((*ch_it)->Type == X3DElemType::ENET_Normal)
  261. X3DGeoHelper::add_normal(**pMesh, tnemesh.CoordIndex, tnemesh.NormalIndex, ((X3DNodeElementNormal *)*ch_it)->Value,
  262. tnemesh.NormalPerVertex);
  263. else if ((*ch_it)->Type == X3DElemType::ENET_TextureCoordinate)
  264. X3DGeoHelper::add_tex_coord(**pMesh, tnemesh.CoordIndex, tnemesh.TexCoordIndex, ((X3DNodeElementTextureCoordinate *)*ch_it)->Value);
  265. else
  266. throw DeadlyImportError("Postprocess_BuildMesh. Unknown child of IndexedFaceSet: " + ai_to_string((*ch_it)->Type) + ".");
  267. } // for(std::list<X3DNodeElementBase*>::iterator ch_it = tnemesh.Children.begin(); ch_it != tnemesh.Children.end(); ++ch_it)
  268. return; // mesh is build, nothing to do anymore.
  269. } // if(pNodeElement.Type == X3DElemType::ENET_IndexedFaceSet)
  270. if (pNodeElement.Type == X3DElemType::ENET_IndexedLineSet) {
  271. X3DNodeElementIndexedSet &tnemesh = *((X3DNodeElementIndexedSet *)&pNodeElement); // create alias for convenience
  272. // at first search for <Coordinate> node and create mesh.
  273. for (std::list<X3DNodeElementBase *>::iterator ch_it = tnemesh.Children.begin(); ch_it != tnemesh.Children.end(); ++ch_it) {
  274. if ((*ch_it)->Type == X3DElemType::ENET_Coordinate) {
  275. *pMesh = X3DGeoHelper::make_line_mesh(tnemesh.CoordIndex, ((X3DNodeElementCoordinate *)*ch_it)->Value);
  276. }
  277. }
  278. // copy additional information from children
  279. for (std::list<X3DNodeElementBase *>::iterator ch_it = tnemesh.Children.begin(); ch_it != tnemesh.Children.end(); ++ch_it) {
  280. ai_assert(*pMesh);
  281. if ((*ch_it)->Type == X3DElemType::ENET_Color)
  282. X3DGeoHelper::add_color(**pMesh, tnemesh.CoordIndex, tnemesh.ColorIndex, ((X3DNodeElementColor *)*ch_it)->Value, tnemesh.ColorPerVertex);
  283. else if ((*ch_it)->Type == X3DElemType::ENET_ColorRGBA)
  284. X3DGeoHelper::add_color(**pMesh, tnemesh.CoordIndex, tnemesh.ColorIndex, ((X3DNodeElementColorRGBA *)*ch_it)->Value,
  285. tnemesh.ColorPerVertex);
  286. else if ((*ch_it)->Type == X3DElemType::ENET_Coordinate) {
  287. } // skip because already read when mesh created.
  288. else
  289. throw DeadlyImportError("Postprocess_BuildMesh. Unknown child of IndexedLineSet: " + ai_to_string((*ch_it)->Type) + ".");
  290. } // for(std::list<X3DNodeElementBase*>::iterator ch_it = tnemesh.Children.begin(); ch_it != tnemesh.Children.end(); ++ch_it)
  291. return; // mesh is build, nothing to do anymore.
  292. } // if(pNodeElement.Type == X3DElemType::ENET_IndexedLineSet)
  293. if ((pNodeElement.Type == X3DElemType::ENET_IndexedTriangleSet) ||
  294. (pNodeElement.Type == X3DElemType::ENET_IndexedTriangleFanSet) ||
  295. (pNodeElement.Type == X3DElemType::ENET_IndexedTriangleStripSet)) {
  296. X3DNodeElementIndexedSet &tnemesh = *((X3DNodeElementIndexedSet *)&pNodeElement); // create alias for convenience
  297. // at first search for <Coordinate> node and create mesh.
  298. for (std::list<X3DNodeElementBase *>::iterator ch_it = tnemesh.Children.begin(); ch_it != tnemesh.Children.end(); ++ch_it) {
  299. if ((*ch_it)->Type == X3DElemType::ENET_Coordinate) {
  300. *pMesh = X3DGeoHelper::make_mesh(tnemesh.CoordIndex, ((X3DNodeElementCoordinate *)*ch_it)->Value);
  301. }
  302. }
  303. // copy additional information from children
  304. for (std::list<X3DNodeElementBase *>::iterator ch_it = tnemesh.Children.begin(); ch_it != tnemesh.Children.end(); ++ch_it) {
  305. ai_assert(*pMesh);
  306. if ((*ch_it)->Type == X3DElemType::ENET_Color)
  307. X3DGeoHelper::add_color(**pMesh, tnemesh.CoordIndex, tnemesh.ColorIndex, ((X3DNodeElementColor *)*ch_it)->Value, tnemesh.ColorPerVertex);
  308. else if ((*ch_it)->Type == X3DElemType::ENET_ColorRGBA)
  309. X3DGeoHelper::add_color(**pMesh, tnemesh.CoordIndex, tnemesh.ColorIndex, ((X3DNodeElementColorRGBA *)*ch_it)->Value,
  310. tnemesh.ColorPerVertex);
  311. else if ((*ch_it)->Type == X3DElemType::ENET_Coordinate) {
  312. } // skip because already read when mesh created.
  313. else if ((*ch_it)->Type == X3DElemType::ENET_Normal)
  314. X3DGeoHelper::add_normal(**pMesh, tnemesh.CoordIndex, tnemesh.NormalIndex, ((X3DNodeElementNormal *)*ch_it)->Value,
  315. tnemesh.NormalPerVertex);
  316. else if ((*ch_it)->Type == X3DElemType::ENET_TextureCoordinate)
  317. X3DGeoHelper::add_tex_coord(**pMesh, tnemesh.CoordIndex, tnemesh.TexCoordIndex, ((X3DNodeElementTextureCoordinate *)*ch_it)->Value);
  318. else
  319. throw DeadlyImportError("Postprocess_BuildMesh. Unknown child of IndexedTriangleSet or IndexedTriangleFanSet, or \
  320. IndexedTriangleStripSet: " +
  321. ai_to_string((*ch_it)->Type) + ".");
  322. } // for(std::list<X3DNodeElementBase*>::iterator ch_it = tnemesh.Children.begin(); ch_it != tnemesh.Children.end(); ++ch_it)
  323. return; // mesh is build, nothing to do anymore.
  324. } // if((pNodeElement.Type == X3DElemType::ENET_IndexedTriangleFanSet) || (pNodeElement.Type == X3DElemType::ENET_IndexedTriangleStripSet))
  325. if (pNodeElement.Type == X3DElemType::ENET_Extrusion) {
  326. X3DNodeElementIndexedSet &tnemesh = *((X3DNodeElementIndexedSet *)&pNodeElement); // create alias for convenience
  327. *pMesh = X3DGeoHelper::make_mesh(tnemesh.CoordIndex, tnemesh.Vertices);
  328. return; // mesh is build, nothing to do anymore.
  329. } // if(pNodeElement.Type == X3DElemType::ENET_Extrusion)
  330. //
  331. // Primitives sets
  332. //
  333. if (pNodeElement.Type == X3DElemType::ENET_PointSet) {
  334. X3DNodeElementSet &tnemesh = *((X3DNodeElementSet *)&pNodeElement); // create alias for convenience
  335. // at first search for <Coordinate> node and create mesh.
  336. for (std::list<X3DNodeElementBase *>::iterator ch_it = tnemesh.Children.begin(); ch_it != tnemesh.Children.end(); ++ch_it) {
  337. if ((*ch_it)->Type == X3DElemType::ENET_Coordinate) {
  338. std::vector<aiVector3D> vec_copy;
  339. vec_copy.reserve(((X3DNodeElementCoordinate *)*ch_it)->Value.size());
  340. for (std::list<aiVector3D>::const_iterator it = ((X3DNodeElementCoordinate *)*ch_it)->Value.begin();
  341. it != ((X3DNodeElementCoordinate *)*ch_it)->Value.end(); ++it) {
  342. vec_copy.push_back(*it);
  343. }
  344. *pMesh = StandardShapes::MakeMesh(vec_copy, 1);
  345. }
  346. }
  347. // copy additional information from children
  348. for (std::list<X3DNodeElementBase *>::iterator ch_it = tnemesh.Children.begin(); ch_it != tnemesh.Children.end(); ++ch_it) {
  349. ai_assert(*pMesh);
  350. if ((*ch_it)->Type == X3DElemType::ENET_Color)
  351. X3DGeoHelper::add_color(**pMesh, ((X3DNodeElementColor *)*ch_it)->Value, true);
  352. else if ((*ch_it)->Type == X3DElemType::ENET_ColorRGBA)
  353. X3DGeoHelper::add_color(**pMesh, ((X3DNodeElementColorRGBA *)*ch_it)->Value, true);
  354. else if ((*ch_it)->Type == X3DElemType::ENET_Coordinate) {
  355. } // skip because already read when mesh created.
  356. else
  357. throw DeadlyImportError("Postprocess_BuildMesh. Unknown child of PointSet: " + ai_to_string((*ch_it)->Type) + ".");
  358. } // for(std::list<X3DNodeElementBase*>::iterator ch_it = tnemesh.Children.begin(); ch_it != tnemesh.Children.end(); ++ch_it)
  359. return; // mesh is build, nothing to do anymore.
  360. } // if(pNodeElement.Type == X3DElemType::ENET_PointSet)
  361. if (pNodeElement.Type == X3DElemType::ENET_LineSet) {
  362. X3DNodeElementSet &tnemesh = *((X3DNodeElementSet *)&pNodeElement); // create alias for convenience
  363. // at first search for <Coordinate> node and create mesh.
  364. for (std::list<X3DNodeElementBase *>::iterator ch_it = tnemesh.Children.begin(); ch_it != tnemesh.Children.end(); ++ch_it) {
  365. if ((*ch_it)->Type == X3DElemType::ENET_Coordinate) {
  366. *pMesh = X3DGeoHelper::make_mesh(tnemesh.CoordIndex, ((X3DNodeElementCoordinate *)*ch_it)->Value);
  367. }
  368. }
  369. // copy additional information from children
  370. for (std::list<X3DNodeElementBase *>::iterator ch_it = tnemesh.Children.begin(); ch_it != tnemesh.Children.end(); ++ch_it) {
  371. ai_assert(*pMesh);
  372. if ((*ch_it)->Type == X3DElemType::ENET_Color)
  373. X3DGeoHelper::add_color(**pMesh, ((X3DNodeElementColor *)*ch_it)->Value, true);
  374. else if ((*ch_it)->Type == X3DElemType::ENET_ColorRGBA)
  375. X3DGeoHelper::add_color(**pMesh, ((X3DNodeElementColorRGBA *)*ch_it)->Value, true);
  376. else if ((*ch_it)->Type == X3DElemType::ENET_Coordinate) {
  377. } // skip because already read when mesh created.
  378. else
  379. throw DeadlyImportError("Postprocess_BuildMesh. Unknown child of LineSet: " + ai_to_string((*ch_it)->Type) + ".");
  380. } // for(std::list<X3DNodeElementBase*>::iterator ch_it = tnemesh.Children.begin(); ch_it != tnemesh.Children.end(); ++ch_it)
  381. return; // mesh is build, nothing to do anymore.
  382. } // if(pNodeElement.Type == X3DElemType::ENET_LineSet)
  383. if (pNodeElement.Type == X3DElemType::ENET_TriangleFanSet) {
  384. X3DNodeElementSet &tnemesh = *((X3DNodeElementSet *)&pNodeElement); // create alias for convenience
  385. // at first search for <Coordinate> node and create mesh.
  386. for (std::list<X3DNodeElementBase *>::iterator ch_it = tnemesh.Children.begin(); ch_it != tnemesh.Children.end(); ++ch_it) {
  387. if ((*ch_it)->Type == X3DElemType::ENET_Coordinate) {
  388. *pMesh = X3DGeoHelper::make_mesh(tnemesh.CoordIndex, ((X3DNodeElementCoordinate *)*ch_it)->Value);
  389. }
  390. }
  391. // copy additional information from children
  392. for (std::list<X3DNodeElementBase *>::iterator ch_it = tnemesh.Children.begin(); ch_it != tnemesh.Children.end(); ++ch_it) {
  393. if (nullptr == *pMesh) {
  394. break;
  395. }
  396. if ((*ch_it)->Type == X3DElemType::ENET_Color)
  397. X3DGeoHelper::add_color(**pMesh, ((X3DNodeElementColor *)*ch_it)->Value, tnemesh.ColorPerVertex);
  398. else if ((*ch_it)->Type == X3DElemType::ENET_ColorRGBA)
  399. X3DGeoHelper::add_color(**pMesh, ((X3DNodeElementColorRGBA *)*ch_it)->Value, tnemesh.ColorPerVertex);
  400. else if ((*ch_it)->Type == X3DElemType::ENET_Coordinate) {
  401. } // skip because already read when mesh created.
  402. else if ((*ch_it)->Type == X3DElemType::ENET_Normal)
  403. X3DGeoHelper::add_normal(**pMesh, tnemesh.CoordIndex, tnemesh.NormalIndex, ((X3DNodeElementNormal *)*ch_it)->Value,
  404. tnemesh.NormalPerVertex);
  405. else if ((*ch_it)->Type == X3DElemType::ENET_TextureCoordinate)
  406. X3DGeoHelper::add_tex_coord(**pMesh, tnemesh.CoordIndex, tnemesh.TexCoordIndex, ((X3DNodeElementTextureCoordinate *)*ch_it)->Value);
  407. else
  408. throw DeadlyImportError("Postprocess_BuildMesh. Unknown child of TrianlgeFanSet: " + ai_to_string((*ch_it)->Type) + ".");
  409. } // for(std::list<X3DNodeElementBase*>::iterator ch_it = tnemesh.Children.begin(); ch_it != tnemesh.Children.end(); ++ch_it)
  410. return; // mesh is build, nothing to do anymore.
  411. } // if(pNodeElement.Type == X3DElemType::ENET_TriangleFanSet)
  412. if (pNodeElement.Type == X3DElemType::ENET_TriangleSet) {
  413. X3DNodeElementSet &tnemesh = *((X3DNodeElementSet *)&pNodeElement); // create alias for convenience
  414. // at first search for <Coordinate> node and create mesh.
  415. for (std::list<X3DNodeElementBase *>::iterator ch_it = tnemesh.Children.begin(); ch_it != tnemesh.Children.end(); ++ch_it) {
  416. if ((*ch_it)->Type == X3DElemType::ENET_Coordinate) {
  417. std::vector<aiVector3D> vec_copy;
  418. vec_copy.reserve(((X3DNodeElementCoordinate *)*ch_it)->Value.size());
  419. for (std::list<aiVector3D>::const_iterator it = ((X3DNodeElementCoordinate *)*ch_it)->Value.begin();
  420. it != ((X3DNodeElementCoordinate *)*ch_it)->Value.end(); ++it) {
  421. vec_copy.push_back(*it);
  422. }
  423. *pMesh = StandardShapes::MakeMesh(vec_copy, 3);
  424. }
  425. }
  426. // copy additional information from children
  427. for (std::list<X3DNodeElementBase *>::iterator ch_it = tnemesh.Children.begin(); ch_it != tnemesh.Children.end(); ++ch_it) {
  428. ai_assert(*pMesh);
  429. if ((*ch_it)->Type == X3DElemType::ENET_Color)
  430. X3DGeoHelper::add_color(**pMesh, ((X3DNodeElementColor *)*ch_it)->Value, tnemesh.ColorPerVertex);
  431. else if ((*ch_it)->Type == X3DElemType::ENET_ColorRGBA)
  432. X3DGeoHelper::add_color(**pMesh, ((X3DNodeElementColorRGBA *)*ch_it)->Value, tnemesh.ColorPerVertex);
  433. else if ((*ch_it)->Type == X3DElemType::ENET_Coordinate) {
  434. } // skip because already read when mesh created.
  435. else if ((*ch_it)->Type == X3DElemType::ENET_Normal)
  436. X3DGeoHelper::add_normal(**pMesh, tnemesh.CoordIndex, tnemesh.NormalIndex, ((X3DNodeElementNormal *)*ch_it)->Value,
  437. tnemesh.NormalPerVertex);
  438. else if ((*ch_it)->Type == X3DElemType::ENET_TextureCoordinate)
  439. X3DGeoHelper::add_tex_coord(**pMesh, tnemesh.CoordIndex, tnemesh.TexCoordIndex, ((X3DNodeElementTextureCoordinate *)*ch_it)->Value);
  440. else
  441. throw DeadlyImportError("Postprocess_BuildMesh. Unknown child of TrianlgeSet: " + ai_to_string((*ch_it)->Type) + ".");
  442. } // for(std::list<X3DNodeElementBase*>::iterator ch_it = tnemesh.Children.begin(); ch_it != tnemesh.Children.end(); ++ch_it)
  443. return; // mesh is build, nothing to do anymore.
  444. } // if(pNodeElement.Type == X3DElemType::ENET_TriangleSet)
  445. if (pNodeElement.Type == X3DElemType::ENET_TriangleStripSet) {
  446. X3DNodeElementSet &tnemesh = *((X3DNodeElementSet *)&pNodeElement); // create alias for convenience
  447. // at first search for <Coordinate> node and create mesh.
  448. for (std::list<X3DNodeElementBase *>::iterator ch_it = tnemesh.Children.begin(); ch_it != tnemesh.Children.end(); ++ch_it) {
  449. if ((*ch_it)->Type == X3DElemType::ENET_Coordinate) {
  450. *pMesh = X3DGeoHelper::make_mesh(tnemesh.CoordIndex, ((X3DNodeElementCoordinate *)*ch_it)->Value);
  451. }
  452. }
  453. // copy additional information from children
  454. for (std::list<X3DNodeElementBase *>::iterator ch_it = tnemesh.Children.begin(); ch_it != tnemesh.Children.end(); ++ch_it) {
  455. ai_assert(*pMesh);
  456. if ((*ch_it)->Type == X3DElemType::ENET_Color)
  457. X3DGeoHelper::add_color(**pMesh, ((X3DNodeElementColor *)*ch_it)->Value, tnemesh.ColorPerVertex);
  458. else if ((*ch_it)->Type == X3DElemType::ENET_ColorRGBA)
  459. X3DGeoHelper::add_color(**pMesh, ((X3DNodeElementColorRGBA *)*ch_it)->Value, tnemesh.ColorPerVertex);
  460. else if ((*ch_it)->Type == X3DElemType::ENET_Coordinate) {
  461. } // skip because already read when mesh created.
  462. else if ((*ch_it)->Type == X3DElemType::ENET_Normal)
  463. X3DGeoHelper::add_normal(**pMesh, tnemesh.CoordIndex, tnemesh.NormalIndex, ((X3DNodeElementNormal *)*ch_it)->Value,
  464. tnemesh.NormalPerVertex);
  465. else if ((*ch_it)->Type == X3DElemType::ENET_TextureCoordinate)
  466. X3DGeoHelper::add_tex_coord(**pMesh, tnemesh.CoordIndex, tnemesh.TexCoordIndex, ((X3DNodeElementTextureCoordinate *)*ch_it)->Value);
  467. else
  468. throw DeadlyImportError("Postprocess_BuildMesh. Unknown child of TriangleStripSet: " + ai_to_string((*ch_it)->Type) + ".");
  469. } // for(std::list<X3DNodeElementBase*>::iterator ch_it = tnemesh.Children.begin(); ch_it != tnemesh.Children.end(); ++ch_it)
  470. return; // mesh is build, nothing to do anymore.
  471. } // if(pNodeElement.Type == X3DElemType::ENET_TriangleStripSet)
  472. throw DeadlyImportError("Postprocess_BuildMesh. Unknown mesh type: " + ai_to_string(pNodeElement.Type) + ".");
  473. }
  474. void X3DImporter::Postprocess_BuildNode(const X3DNodeElementBase &pNodeElement, aiNode &pSceneNode, std::list<aiMesh *> &pSceneMeshList,
  475. std::list<aiMaterial *> &pSceneMaterialList, std::list<aiLight *> &pSceneLightList) const {
  476. std::list<X3DNodeElementBase *>::const_iterator chit_begin = pNodeElement.Children.begin();
  477. std::list<X3DNodeElementBase *>::const_iterator chit_end = pNodeElement.Children.end();
  478. std::list<aiNode *> SceneNode_Child;
  479. std::list<unsigned int> SceneNode_Mesh;
  480. // At first read all metadata
  481. Postprocess_CollectMetadata(pNodeElement, pSceneNode);
  482. // check if we have deal with grouping node. Which can contain transformation or switch
  483. if (pNodeElement.Type == X3DElemType::ENET_Group) {
  484. const X3DNodeElementGroup &tne_group = *((X3DNodeElementGroup *)&pNodeElement); // create alias for convenience
  485. pSceneNode.mTransformation = tne_group.Transformation;
  486. if (tne_group.UseChoice) {
  487. // If Choice is less than zero or greater than the number of nodes in the children field, nothing is chosen.
  488. if ((tne_group.Choice < 0) || ((size_t)tne_group.Choice >= pNodeElement.Children.size())) {
  489. chit_begin = pNodeElement.Children.end();
  490. chit_end = pNodeElement.Children.end();
  491. } else {
  492. for (size_t i = 0; i < (size_t)tne_group.Choice; i++)
  493. ++chit_begin; // forward iterator to chosen node.
  494. chit_end = chit_begin;
  495. ++chit_end; // point end iterator to next element after chosen node.
  496. }
  497. } // if(tne_group.UseChoice)
  498. } // if(pNodeElement.Type == X3DElemType::ENET_Group)
  499. // Reserve memory for fast access and check children.
  500. for (std::list<X3DNodeElementBase *>::const_iterator it = chit_begin; it != chit_end; ++it) { // in this loop we do not read metadata because it's already read at begin.
  501. if ((*it)->Type == X3DElemType::ENET_Group) {
  502. // if child is group then create new node and do recursive call.
  503. aiNode *new_node = new aiNode;
  504. new_node->mName = (*it)->ID;
  505. new_node->mParent = &pSceneNode;
  506. SceneNode_Child.push_back(new_node);
  507. Postprocess_BuildNode(**it, *new_node, pSceneMeshList, pSceneMaterialList, pSceneLightList);
  508. } else if ((*it)->Type == X3DElemType::ENET_Shape) {
  509. // shape can contain only one geometry and one appearance nodes.
  510. Postprocess_BuildShape(*((X3DNodeElementShape *)*it), SceneNode_Mesh, pSceneMeshList, pSceneMaterialList);
  511. } else if (((*it)->Type == X3DElemType::ENET_DirectionalLight) || ((*it)->Type == X3DElemType::ENET_PointLight) ||
  512. ((*it)->Type == X3DElemType::ENET_SpotLight)) {
  513. Postprocess_BuildLight(*((X3DNodeElementLight *)*it), pSceneLightList);
  514. } else if (!PostprocessHelper_ElementIsMetadata((*it)->Type)) // skip metadata
  515. {
  516. throw DeadlyImportError("Postprocess_BuildNode. Unknown type: " + ai_to_string((*it)->Type) + ".");
  517. }
  518. } // for(std::list<X3DNodeElementBase*>::const_iterator it = chit_begin; it != chit_end; it++)
  519. // copy data about children and meshes to aiNode.
  520. if (!SceneNode_Child.empty()) {
  521. std::list<aiNode *>::const_iterator it = SceneNode_Child.begin();
  522. pSceneNode.mNumChildren = static_cast<unsigned int>(SceneNode_Child.size());
  523. pSceneNode.mChildren = new aiNode *[pSceneNode.mNumChildren];
  524. for (size_t i = 0; i < pSceneNode.mNumChildren; i++)
  525. pSceneNode.mChildren[i] = *it++;
  526. }
  527. if (!SceneNode_Mesh.empty()) {
  528. std::list<unsigned int>::const_iterator it = SceneNode_Mesh.begin();
  529. pSceneNode.mNumMeshes = static_cast<unsigned int>(SceneNode_Mesh.size());
  530. pSceneNode.mMeshes = new unsigned int[pSceneNode.mNumMeshes];
  531. for (size_t i = 0; i < pSceneNode.mNumMeshes; i++)
  532. pSceneNode.mMeshes[i] = *it++;
  533. }
  534. // that's all. return to previous deals
  535. }
  536. void X3DImporter::Postprocess_BuildShape(const X3DNodeElementShape &pShapeNodeElement, std::list<unsigned int> &pNodeMeshInd,
  537. std::list<aiMesh *> &pSceneMeshList, std::list<aiMaterial *> &pSceneMaterialList) const {
  538. aiMaterial *tmat = nullptr;
  539. aiMesh *tmesh = nullptr;
  540. X3DElemType mesh_type = X3DElemType::ENET_Invalid;
  541. unsigned int mat_ind = 0;
  542. for (std::list<X3DNodeElementBase *>::const_iterator it = pShapeNodeElement.Children.begin(); it != pShapeNodeElement.Children.end(); ++it) {
  543. if (PostprocessHelper_ElementIsMesh((*it)->Type)) {
  544. Postprocess_BuildMesh(**it, &tmesh);
  545. if (tmesh != nullptr) {
  546. // if mesh successfully built then add data about it to arrays
  547. pNodeMeshInd.push_back(static_cast<unsigned int>(pSceneMeshList.size()));
  548. pSceneMeshList.push_back(tmesh);
  549. // keep mesh type. Need above for texture coordinate generation.
  550. mesh_type = (*it)->Type;
  551. }
  552. } else if ((*it)->Type == X3DElemType::ENET_Appearance) {
  553. Postprocess_BuildMaterial(**it, &tmat);
  554. if (tmat != nullptr) {
  555. // if material successfully built then add data about it to array
  556. mat_ind = static_cast<unsigned int>(pSceneMaterialList.size());
  557. pSceneMaterialList.push_back(tmat);
  558. }
  559. }
  560. } // for(std::list<X3DNodeElementBase*>::const_iterator it = pShapeNodeElement.Children.begin(); it != pShapeNodeElement.Children.end(); it++)
  561. // associate read material with read mesh.
  562. if ((tmesh != nullptr) && (tmat != nullptr)) {
  563. tmesh->mMaterialIndex = mat_ind;
  564. // Check texture mapping. If material has texture but mesh has no texture coordinate then try to ask Assimp to generate texture coordinates.
  565. if ((tmat->GetTextureCount(aiTextureType_DIFFUSE) != 0) && !tmesh->HasTextureCoords(0)) {
  566. int32_t tm;
  567. aiVector3D tvec3;
  568. switch (mesh_type) {
  569. case X3DElemType::ENET_Box:
  570. tm = aiTextureMapping_BOX;
  571. break;
  572. case X3DElemType::ENET_Cone:
  573. case X3DElemType::ENET_Cylinder:
  574. tm = aiTextureMapping_CYLINDER;
  575. break;
  576. case X3DElemType::ENET_Sphere:
  577. tm = aiTextureMapping_SPHERE;
  578. break;
  579. default:
  580. tm = aiTextureMapping_PLANE;
  581. break;
  582. } // switch(mesh_type)
  583. tmat->AddProperty(&tm, 1, AI_MATKEY_MAPPING_DIFFUSE(0));
  584. } // if((tmat->GetTextureCount(aiTextureType_DIFFUSE) != 0) && !tmesh->HasTextureCoords(0))
  585. } // if((tmesh != nullptr) && (tmat != nullptr))
  586. }
  587. void X3DImporter::Postprocess_CollectMetadata(const X3DNodeElementBase &pNodeElement, aiNode &pSceneNode) const {
  588. std::list<X3DNodeElementBase *> meta_list;
  589. size_t meta_idx;
  590. PostprocessHelper_CollectMetadata(pNodeElement, meta_list); // find metadata in current node element.
  591. if (!meta_list.empty()) {
  592. if (pSceneNode.mMetaData != nullptr) {
  593. throw DeadlyImportError("Postprocess. MetaData member in node are not nullptr. Something went wrong.");
  594. }
  595. // copy collected metadata to output node.
  596. pSceneNode.mMetaData = aiMetadata::Alloc(static_cast<unsigned int>(meta_list.size()));
  597. meta_idx = 0;
  598. for (std::list<X3DNodeElementBase *>::const_iterator it = meta_list.begin(); it != meta_list.end(); ++it, ++meta_idx) {
  599. X3DNodeElementMeta *cur_meta = (X3DNodeElementMeta *)*it;
  600. // due to limitations we can add only first element of value list.
  601. // Add an element according to its type.
  602. if ((*it)->Type == X3DElemType::ENET_MetaBoolean) {
  603. if (((X3DNodeElementMetaBoolean *)cur_meta)->Value.size() > 0)
  604. pSceneNode.mMetaData->Set(static_cast<unsigned int>(meta_idx), cur_meta->Name, *(((X3DNodeElementMetaBoolean *)cur_meta)->Value.begin()) == true);
  605. } else if ((*it)->Type == X3DElemType::ENET_MetaDouble) {
  606. if (((X3DNodeElementMetaDouble *)cur_meta)->Value.size() > 0)
  607. pSceneNode.mMetaData->Set(static_cast<unsigned int>(meta_idx), cur_meta->Name, (float)*(((X3DNodeElementMetaDouble *)cur_meta)->Value.begin()));
  608. } else if ((*it)->Type == X3DElemType::ENET_MetaFloat) {
  609. if (((X3DNodeElementMetaFloat *)cur_meta)->Value.size() > 0)
  610. pSceneNode.mMetaData->Set(static_cast<unsigned int>(meta_idx), cur_meta->Name, *(((X3DNodeElementMetaFloat *)cur_meta)->Value.begin()));
  611. } else if ((*it)->Type == X3DElemType::ENET_MetaInteger) {
  612. if (((X3DNodeElementMetaInt *)cur_meta)->Value.size() > 0)
  613. pSceneNode.mMetaData->Set(static_cast<unsigned int>(meta_idx), cur_meta->Name, *(((X3DNodeElementMetaInt *)cur_meta)->Value.begin()));
  614. } else if ((*it)->Type == X3DElemType::ENET_MetaString) {
  615. if (((X3DNodeElementMetaString *)cur_meta)->Value.size() > 0) {
  616. aiString tstr(((X3DNodeElementMetaString *)cur_meta)->Value.begin()->data());
  617. pSceneNode.mMetaData->Set(static_cast<unsigned int>(meta_idx), cur_meta->Name, tstr);
  618. }
  619. } else {
  620. throw DeadlyImportError("Postprocess. Unknown metadata type.");
  621. } // if((*it)->Type == X3DElemType::ENET_Meta*) else
  622. } // for(std::list<X3DNodeElementBase*>::const_iterator it = meta_list.begin(); it != meta_list.end(); it++)
  623. } // if( !meta_list.empty() )
  624. }
  625. } // namespace Assimp
  626. #endif // !ASSIMP_BUILD_NO_X3D_IMPORTER