X3DImporter.cpp 60 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735
  1. /*
  2. Open Asset Import Library (assimp)
  3. ----------------------------------------------------------------------
  4. Copyright (c) 2006-2020, 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.cpp
  34. /// \brief X3D-format files importer for Assimp: main algorithm implementation.
  35. /// \date 2015-2016
  36. /// \author [email protected]
  37. #ifndef ASSIMP_BUILD_NO_X3D_IMPORTER
  38. #include "X3DImporter.hpp"
  39. #include "X3DImporter_Macro.hpp"
  40. #include <assimp/StringUtils.h>
  41. // Header files, Assimp.
  42. #include <assimp/DefaultIOSystem.h>
  43. #include <assimp/fast_atof.h>
  44. #include "FIReader.hpp"
  45. // Header files, stdlib.
  46. #include <memory>
  47. #include <string>
  48. #include <iterator>
  49. namespace Assimp {
  50. /// \var aiImporterDesc X3DImporter::Description
  51. /// Constant which holds the importer description
  52. const aiImporterDesc X3DImporter::Description = {
  53. "Extensible 3D(X3D) Importer",
  54. "smalcom",
  55. "",
  56. "See documentation in source code. Chapter: Limitations.",
  57. aiImporterFlags_SupportTextFlavour | aiImporterFlags_SupportBinaryFlavour | aiImporterFlags_LimitedSupport | aiImporterFlags_Experimental,
  58. 0,
  59. 0,
  60. 0,
  61. 0,
  62. "x3d x3db"
  63. };
  64. //const std::regex X3DImporter::pattern_nws(R"([^, \t\r\n]+)");
  65. //const std::regex X3DImporter::pattern_true(R"(^\s*(?:true|1)\s*$)", std::regex::icase);
  66. struct WordIterator {
  67. using iterator_category = std::input_iterator_tag;
  68. using value_type = const char*;
  69. using difference_type = ptrdiff_t;
  70. using pointer = value_type*;
  71. using reference = value_type&;
  72. static const char *whitespace;
  73. const char *start_, *end_;
  74. WordIterator(const char *start, const char *end): start_(start), end_(end) {
  75. start_ = start + strspn(start, whitespace);
  76. if (start_ >= end_) {
  77. start_ = 0;
  78. }
  79. }
  80. WordIterator(): start_(0), end_(0) {}
  81. WordIterator(const WordIterator &other): start_(other.start_), end_(other.end_) {}
  82. WordIterator &operator=(const WordIterator &other) {
  83. start_ = other.start_;
  84. end_ = other.end_;
  85. return *this;
  86. }
  87. bool operator==(const WordIterator &other) const { return start_ == other.start_; }
  88. bool operator!=(const WordIterator &other) const { return start_ != other.start_; }
  89. WordIterator &operator++() {
  90. start_ += strcspn(start_, whitespace);
  91. start_ += strspn(start_, whitespace);
  92. if (start_ >= end_) {
  93. start_ = 0;
  94. }
  95. return *this;
  96. }
  97. WordIterator operator++(int) {
  98. WordIterator result(*this);
  99. ++(*this);
  100. return result;
  101. }
  102. const char *operator*() const { return start_; }
  103. };
  104. const char *WordIterator::whitespace = ", \t\r\n";
  105. X3DImporter::X3DImporter()
  106. : NodeElement_Cur( nullptr )
  107. , mReader( nullptr ) {
  108. // empty
  109. }
  110. X3DImporter::~X3DImporter() {
  111. // Clear() is accounting if data already is deleted. So, just check again if all data is deleted.
  112. Clear();
  113. }
  114. void X3DImporter::Clear() {
  115. NodeElement_Cur = nullptr;
  116. // Delete all elements
  117. if(!NodeElement_List.empty()) {
  118. for ( std::list<CX3DImporter_NodeElement*>::iterator it = NodeElement_List.begin(); it != NodeElement_List.end(); ++it ) {
  119. delete *it;
  120. }
  121. NodeElement_List.clear();
  122. }
  123. }
  124. /*********************************************************************************************************************************************/
  125. /************************************************************ Functions: find set ************************************************************/
  126. /*********************************************************************************************************************************************/
  127. bool X3DImporter::FindNodeElement_FromRoot(const std::string& pID, const CX3DImporter_NodeElement::EType pType, CX3DImporter_NodeElement** pElement)
  128. {
  129. for(std::list<CX3DImporter_NodeElement*>::iterator it = NodeElement_List.begin(); it != NodeElement_List.end(); ++it)
  130. {
  131. if(((*it)->Type == pType) && ((*it)->ID == pID))
  132. {
  133. if(pElement != nullptr) *pElement = *it;
  134. return true;
  135. }
  136. }// for(std::list<CX3DImporter_NodeElement*>::iterator it = NodeElement_List.begin(); it != NodeElement_List.end(); it++)
  137. return false;
  138. }
  139. bool X3DImporter::FindNodeElement_FromNode(CX3DImporter_NodeElement* pStartNode, const std::string& pID,
  140. const CX3DImporter_NodeElement::EType pType, CX3DImporter_NodeElement** pElement)
  141. {
  142. bool found = false;// flag: true - if requested element is found.
  143. // Check if pStartNode - this is the element, we are looking for.
  144. if((pStartNode->Type == pType) && (pStartNode->ID == pID))
  145. {
  146. found = true;
  147. if ( pElement != nullptr )
  148. {
  149. *pElement = pStartNode;
  150. }
  151. goto fne_fn_end;
  152. }// if((pStartNode->Type() == pType) && (pStartNode->ID() == pID))
  153. // Check childs of pStartNode.
  154. for(std::list<CX3DImporter_NodeElement*>::iterator ch_it = pStartNode->Child.begin(); ch_it != pStartNode->Child.end(); ++ch_it)
  155. {
  156. found = FindNodeElement_FromNode(*ch_it, pID, pType, pElement);
  157. if ( found )
  158. {
  159. break;
  160. }
  161. }// for(std::list<CX3DImporter_NodeElement*>::iterator ch_it = it->Child.begin(); ch_it != it->Child.end(); ch_it++)
  162. fne_fn_end:
  163. return found;
  164. }
  165. bool X3DImporter::FindNodeElement(const std::string& pID, const CX3DImporter_NodeElement::EType pType, CX3DImporter_NodeElement** pElement)
  166. {
  167. CX3DImporter_NodeElement* tnd = NodeElement_Cur;// temporary pointer to node.
  168. bool static_search = false;// flag: true if searching in static node.
  169. // At first check if we have deal with static node. Go up through parent nodes and check flag.
  170. while(tnd != nullptr)
  171. {
  172. if(tnd->Type == CX3DImporter_NodeElement::ENET_Group)
  173. {
  174. if(((CX3DImporter_NodeElement_Group*)tnd)->Static)
  175. {
  176. static_search = true;// Flag found, stop walking up. Node with static flag will holded in tnd variable.
  177. break;
  178. }
  179. }
  180. tnd = tnd->Parent;// go up in graph.
  181. }// while(tnd != nullptr)
  182. // at now call appropriate search function.
  183. if ( static_search )
  184. {
  185. return FindNodeElement_FromNode( tnd, pID, pType, pElement );
  186. }
  187. else
  188. {
  189. return FindNodeElement_FromRoot( pID, pType, pElement );
  190. }
  191. }
  192. /*********************************************************************************************************************************************/
  193. /************************************************************ Functions: throw set ***********************************************************/
  194. /*********************************************************************************************************************************************/
  195. void X3DImporter::Throw_ArgOutOfRange(const std::string& pArgument)
  196. {
  197. throw DeadlyImportError("Argument value is out of range for: \"" + pArgument + "\".");
  198. }
  199. void X3DImporter::Throw_CloseNotFound(const std::string& pNode)
  200. {
  201. throw DeadlyImportError("Close tag for node <" + pNode + "> not found. Seems file is corrupt.");
  202. }
  203. void X3DImporter::Throw_ConvertFail_Str2ArrF(const std::string& pAttrValue)
  204. {
  205. throw DeadlyImportError("In <" + std::string(mReader->getNodeName()) + "> failed to convert attribute value \"" + pAttrValue +
  206. "\" from string to array of floats.");
  207. }
  208. void X3DImporter::Throw_DEF_And_USE()
  209. {
  210. throw DeadlyImportError("\"DEF\" and \"USE\" can not be defined both in <" + std::string(mReader->getNodeName()) + ">.");
  211. }
  212. void X3DImporter::Throw_IncorrectAttr(const std::string& pAttrName)
  213. {
  214. throw DeadlyImportError("Node <" + std::string(mReader->getNodeName()) + "> has incorrect attribute \"" + pAttrName + "\".");
  215. }
  216. void X3DImporter::Throw_IncorrectAttrValue(const std::string& pAttrName)
  217. {
  218. throw DeadlyImportError("Attribute \"" + pAttrName + "\" in node <" + std::string(mReader->getNodeName()) + "> has incorrect value.");
  219. }
  220. void X3DImporter::Throw_MoreThanOnceDefined(const std::string& pNodeType, const std::string& pDescription)
  221. {
  222. throw DeadlyImportError("\"" + pNodeType + "\" node can be used only once in " + mReader->getNodeName() + ". Description: " + pDescription);
  223. }
  224. void X3DImporter::Throw_TagCountIncorrect(const std::string& pNode)
  225. {
  226. throw DeadlyImportError("Count of open and close tags for node <" + pNode + "> are not equivalent. Seems file is corrupt.");
  227. }
  228. void X3DImporter::Throw_USE_NotFound(const std::string& pAttrValue)
  229. {
  230. throw DeadlyImportError("Not found node with name \"" + pAttrValue + "\" in <" + std::string(mReader->getNodeName()) + ">.");
  231. }
  232. /*********************************************************************************************************************************************/
  233. /************************************************************* Functions: XML set ************************************************************/
  234. /*********************************************************************************************************************************************/
  235. void X3DImporter::XML_CheckNode_MustBeEmpty()
  236. {
  237. if(!mReader->isEmptyElement()) throw DeadlyImportError(std::string("Node <") + mReader->getNodeName() + "> must be empty.");
  238. }
  239. void X3DImporter::XML_CheckNode_SkipUnsupported(const std::string& pParentNodeName)
  240. {
  241. static const size_t Uns_Skip_Len = 192;
  242. const char* Uns_Skip[ Uns_Skip_Len ] = {
  243. // CAD geometry component
  244. "CADAssembly", "CADFace", "CADLayer", "CADPart", "IndexedQuadSet", "QuadSet",
  245. // Core
  246. "ROUTE", "ExternProtoDeclare", "ProtoDeclare", "ProtoInstance", "ProtoInterface", "WorldInfo",
  247. // Distributed interactive simulation (DIS) component
  248. "DISEntityManager", "DISEntityTypeMapping", "EspduTransform", "ReceiverPdu", "SignalPdu", "TransmitterPdu",
  249. // Cube map environmental texturing component
  250. "ComposedCubeMapTexture", "GeneratedCubeMapTexture", "ImageCubeMapTexture",
  251. // Environmental effects component
  252. "Background", "Fog", "FogCoordinate", "LocalFog", "TextureBackground",
  253. // Environmental sensor component
  254. "ProximitySensor", "TransformSensor", "VisibilitySensor",
  255. // Followers component
  256. "ColorChaser", "ColorDamper", "CoordinateChaser", "CoordinateDamper", "OrientationChaser", "OrientationDamper", "PositionChaser", "PositionChaser2D",
  257. "PositionDamper", "PositionDamper2D", "ScalarChaser", "ScalarDamper", "TexCoordChaser2D", "TexCoordDamper2D",
  258. // Geospatial component
  259. "GeoCoordinate", "GeoElevationGrid", "GeoLocation", "GeoLOD", "GeoMetadata", "GeoOrigin", "GeoPositionInterpolator", "GeoProximitySensor",
  260. "GeoTouchSensor", "GeoTransform", "GeoViewpoint",
  261. // Humanoid Animation (H-Anim) component
  262. "HAnimDisplacer", "HAnimHumanoid", "HAnimJoint", "HAnimSegment", "HAnimSite",
  263. // Interpolation component
  264. "ColorInterpolator", "CoordinateInterpolator", "CoordinateInterpolator2D", "EaseInEaseOut", "NormalInterpolator", "OrientationInterpolator",
  265. "PositionInterpolator", "PositionInterpolator2D", "ScalarInterpolator", "SplinePositionInterpolator", "SplinePositionInterpolator2D",
  266. "SplineScalarInterpolator", "SquadOrientationInterpolator",
  267. // Key device sensor component
  268. "KeySensor", "StringSensor",
  269. // Layering component
  270. "Layer", "LayerSet", "Viewport",
  271. // Layout component
  272. "Layout", "LayoutGroup", "LayoutLayer", "ScreenFontStyle", "ScreenGroup",
  273. // Navigation component
  274. "Billboard", "Collision", "LOD", "NavigationInfo", "OrthoViewpoint", "Viewpoint", "ViewpointGroup",
  275. // Networking component
  276. "EXPORT", "IMPORT", "Anchor", "LoadSensor",
  277. // NURBS component
  278. "Contour2D", "ContourPolyline2D", "CoordinateDouble", "NurbsCurve", "NurbsCurve2D", "NurbsOrientationInterpolator", "NurbsPatchSurface",
  279. "NurbsPositionInterpolator", "NurbsSet", "NurbsSurfaceInterpolator", "NurbsSweptSurface", "NurbsSwungSurface", "NurbsTextureCoordinate",
  280. "NurbsTrimmedSurface",
  281. // Particle systems component
  282. "BoundedPhysicsModel", "ConeEmitter", "ExplosionEmitter", "ForcePhysicsModel", "ParticleSystem", "PointEmitter", "PolylineEmitter", "SurfaceEmitter",
  283. "VolumeEmitter", "WindPhysicsModel",
  284. // Picking component
  285. "LinePickSensor", "PickableGroup", "PointPickSensor", "PrimitivePickSensor", "VolumePickSensor",
  286. // Pointing device sensor component
  287. "CylinderSensor", "PlaneSensor", "SphereSensor", "TouchSensor",
  288. // Rendering component
  289. "ClipPlane",
  290. // Rigid body physics
  291. "BallJoint", "CollidableOffset", "CollidableShape", "CollisionCollection", "CollisionSensor", "CollisionSpace", "Contact", "DoubleAxisHingeJoint",
  292. "MotorJoint", "RigidBody", "RigidBodyCollection", "SingleAxisHingeJoint", "SliderJoint", "UniversalJoint",
  293. // Scripting component
  294. "Script",
  295. // Programmable shaders component
  296. "ComposedShader", "FloatVertexAttribute", "Matrix3VertexAttribute", "Matrix4VertexAttribute", "PackagedShader", "ProgramShader", "ShaderPart",
  297. "ShaderProgram",
  298. // Shape component
  299. "FillProperties", "LineProperties", "TwoSidedMaterial",
  300. // Sound component
  301. "AudioClip", "Sound",
  302. // Text component
  303. "FontStyle", "Text",
  304. // Texturing3D Component
  305. "ComposedTexture3D", "ImageTexture3D", "PixelTexture3D", "TextureCoordinate3D", "TextureCoordinate4D", "TextureTransformMatrix3D", "TextureTransform3D",
  306. // Texturing component
  307. "MovieTexture", "MultiTexture", "MultiTextureCoordinate", "MultiTextureTransform", "PixelTexture", "TextureCoordinateGenerator", "TextureProperties",
  308. // Time component
  309. "TimeSensor",
  310. // Event Utilities component
  311. "BooleanFilter", "BooleanSequencer", "BooleanToggle", "BooleanTrigger", "IntegerSequencer", "IntegerTrigger", "TimeTrigger",
  312. // Volume rendering component
  313. "BlendedVolumeStyle", "BoundaryEnhancementVolumeStyle", "CartoonVolumeStyle", "ComposedVolumeStyle", "EdgeEnhancementVolumeStyle", "IsoSurfaceVolumeData",
  314. "OpacityMapVolumeStyle", "ProjectionVolumeStyle", "SegmentedVolumeData", "ShadedVolumeStyle", "SilhouetteEnhancementVolumeStyle", "ToneMappedVolumeStyle",
  315. "VolumeData"
  316. };
  317. const std::string nn( mReader->getNodeName() );
  318. bool found = false;
  319. bool close_found = false;
  320. for(size_t i = 0; i < Uns_Skip_Len; i++)
  321. {
  322. if(nn == Uns_Skip[i])
  323. {
  324. found = true;
  325. if(mReader->isEmptyElement())
  326. {
  327. close_found = true;
  328. goto casu_cres;
  329. }
  330. while(mReader->read())
  331. {
  332. if((mReader->getNodeType() == irr::io::EXN_ELEMENT_END) && (nn == mReader->getNodeName()))
  333. {
  334. close_found = true;
  335. goto casu_cres;
  336. }
  337. }
  338. }
  339. }
  340. casu_cres:
  341. if(!found) throw DeadlyImportError("Unknown node \"" + nn + "\" in " + pParentNodeName + ".");
  342. if(close_found)
  343. LogInfo("Skipping node \"" + nn + "\" in " + pParentNodeName + ".");
  344. else
  345. Throw_CloseNotFound(nn);
  346. }
  347. bool X3DImporter::XML_SearchNode(const std::string& pNodeName)
  348. {
  349. while(mReader->read())
  350. {
  351. if((mReader->getNodeType() == irr::io::EXN_ELEMENT) && XML_CheckNode_NameEqual(pNodeName)) return true;
  352. }
  353. return false;
  354. }
  355. bool X3DImporter::XML_ReadNode_GetAttrVal_AsBool(const int pAttrIdx)
  356. {
  357. auto boolValue = std::dynamic_pointer_cast<const FIBoolValue>(mReader->getAttributeEncodedValue(pAttrIdx));
  358. if (boolValue) {
  359. if (boolValue->value.size() == 1) {
  360. return boolValue->value.front();
  361. }
  362. throw DeadlyImportError("Invalid bool value");
  363. }
  364. else {
  365. std::string val(mReader->getAttributeValue(pAttrIdx));
  366. if(val == "false")
  367. return false;
  368. else if(val == "true")
  369. return true;
  370. else
  371. throw DeadlyImportError("Bool attribute value can contain \"false\" or \"true\" not the \"" + val + "\"");
  372. }
  373. }
  374. float X3DImporter::XML_ReadNode_GetAttrVal_AsFloat(const int pAttrIdx)
  375. {
  376. auto floatValue = std::dynamic_pointer_cast<const FIFloatValue>(mReader->getAttributeEncodedValue(pAttrIdx));
  377. if (floatValue) {
  378. if (floatValue->value.size() == 1) {
  379. return floatValue->value.front();
  380. }
  381. throw DeadlyImportError("Invalid float value");
  382. }
  383. else {
  384. std::string val;
  385. float tvalf;
  386. ParseHelper_FixTruncatedFloatString(mReader->getAttributeValue(pAttrIdx), val);
  387. fast_atoreal_move(val.c_str(), tvalf, false);
  388. return tvalf;
  389. }
  390. }
  391. int32_t X3DImporter::XML_ReadNode_GetAttrVal_AsI32(const int pAttrIdx)
  392. {
  393. auto intValue = std::dynamic_pointer_cast<const FIIntValue>(mReader->getAttributeEncodedValue(pAttrIdx));
  394. if (intValue) {
  395. if (intValue->value.size() == 1) {
  396. return intValue->value.front();
  397. }
  398. throw DeadlyImportError("Invalid int value");
  399. }
  400. else {
  401. return strtol10(mReader->getAttributeValue(pAttrIdx));
  402. }
  403. }
  404. void X3DImporter::XML_ReadNode_GetAttrVal_AsCol3f(const int pAttrIdx, aiColor3D& pValue)
  405. {
  406. std::vector<float> tlist;
  407. std::vector<float>::iterator it;
  408. XML_ReadNode_GetAttrVal_AsArrF(pAttrIdx, tlist);
  409. if(tlist.size() != 3) Throw_ConvertFail_Str2ArrF(mReader->getAttributeValue(pAttrIdx));
  410. it = tlist.begin();
  411. pValue.r = *it++;
  412. pValue.g = *it++;
  413. pValue.b = *it;
  414. }
  415. void X3DImporter::XML_ReadNode_GetAttrVal_AsVec2f(const int pAttrIdx, aiVector2D& pValue)
  416. {
  417. std::vector<float> tlist;
  418. std::vector<float>::iterator it;
  419. XML_ReadNode_GetAttrVal_AsArrF(pAttrIdx, tlist);
  420. if(tlist.size() != 2) Throw_ConvertFail_Str2ArrF(mReader->getAttributeValue(pAttrIdx));
  421. it = tlist.begin();
  422. pValue.x = *it++;
  423. pValue.y = *it;
  424. }
  425. void X3DImporter::XML_ReadNode_GetAttrVal_AsVec3f(const int pAttrIdx, aiVector3D& pValue)
  426. {
  427. std::vector<float> tlist;
  428. std::vector<float>::iterator it;
  429. XML_ReadNode_GetAttrVal_AsArrF(pAttrIdx, tlist);
  430. if(tlist.size() != 3) Throw_ConvertFail_Str2ArrF(mReader->getAttributeValue(pAttrIdx));
  431. it = tlist.begin();
  432. pValue.x = *it++;
  433. pValue.y = *it++;
  434. pValue.z = *it;
  435. }
  436. void X3DImporter::XML_ReadNode_GetAttrVal_AsArrB(const int pAttrIdx, std::vector<bool>& pValue)
  437. {
  438. auto boolValue = std::dynamic_pointer_cast<const FIBoolValue>(mReader->getAttributeEncodedValue(pAttrIdx));
  439. if (boolValue) {
  440. pValue = boolValue->value;
  441. }
  442. else {
  443. const char *val = mReader->getAttributeValue(pAttrIdx);
  444. pValue.clear();
  445. //std::cregex_iterator wordItBegin(val, val + strlen(val), pattern_nws);
  446. //const std::cregex_iterator wordItEnd;
  447. //std::transform(wordItBegin, wordItEnd, std::back_inserter(pValue), [](const std::cmatch &match) { return std::regex_match(match.str(), pattern_true); });
  448. WordIterator wordItBegin(val, val + strlen(val));
  449. WordIterator wordItEnd;
  450. std::transform(wordItBegin, wordItEnd, std::back_inserter(pValue), [](const char *match) { return (::tolower(match[0]) == 't') || (match[0] == '1'); });
  451. }
  452. }
  453. void X3DImporter::XML_ReadNode_GetAttrVal_AsArrI32(const int pAttrIdx, std::vector<int32_t>& pValue)
  454. {
  455. auto intValue = std::dynamic_pointer_cast<const FIIntValue>(mReader->getAttributeEncodedValue(pAttrIdx));
  456. if (intValue) {
  457. pValue = intValue->value;
  458. }
  459. else {
  460. const char *val = mReader->getAttributeValue(pAttrIdx);
  461. pValue.clear();
  462. //std::cregex_iterator wordItBegin(val, val + strlen(val), pattern_nws);
  463. //const std::cregex_iterator wordItEnd;
  464. //std::transform(wordItBegin, wordItEnd, std::back_inserter(pValue), [](const std::cmatch &match) { return std::stoi(match.str()); });
  465. WordIterator wordItBegin(val, val + strlen(val));
  466. WordIterator wordItEnd;
  467. std::transform(wordItBegin, wordItEnd, std::back_inserter(pValue), [](const char *match) { return atoi(match); });
  468. }
  469. }
  470. void X3DImporter::XML_ReadNode_GetAttrVal_AsArrF(const int pAttrIdx, std::vector<float>& pValue)
  471. {
  472. auto floatValue = std::dynamic_pointer_cast<const FIFloatValue>(mReader->getAttributeEncodedValue(pAttrIdx));
  473. if (floatValue) {
  474. pValue = floatValue->value;
  475. }
  476. else {
  477. const char *val = mReader->getAttributeValue(pAttrIdx);
  478. pValue.clear();
  479. //std::cregex_iterator wordItBegin(val, val + strlen(val), pattern_nws);
  480. //const std::cregex_iterator wordItEnd;
  481. //std::transform(wordItBegin, wordItEnd, std::back_inserter(pValue), [](const std::cmatch &match) { return std::stof(match.str()); });
  482. WordIterator wordItBegin(val, val + strlen(val));
  483. WordIterator wordItEnd;
  484. std::transform(wordItBegin, wordItEnd, std::back_inserter(pValue), [](const char *match) { return static_cast<float>(atof(match)); });
  485. }
  486. }
  487. void X3DImporter::XML_ReadNode_GetAttrVal_AsArrD(const int pAttrIdx, std::vector<double>& pValue)
  488. {
  489. auto doubleValue = std::dynamic_pointer_cast<const FIDoubleValue>(mReader->getAttributeEncodedValue(pAttrIdx));
  490. if (doubleValue) {
  491. pValue = doubleValue->value;
  492. }
  493. else {
  494. const char *val = mReader->getAttributeValue(pAttrIdx);
  495. pValue.clear();
  496. //std::cregex_iterator wordItBegin(val, val + strlen(val), pattern_nws);
  497. //const std::cregex_iterator wordItEnd;
  498. //std::transform(wordItBegin, wordItEnd, std::back_inserter(pValue), [](const std::cmatch &match) { return std::stod(match.str()); });
  499. WordIterator wordItBegin(val, val + strlen(val));
  500. WordIterator wordItEnd;
  501. std::transform(wordItBegin, wordItEnd, std::back_inserter(pValue), [](const char *match) { return atof(match); });
  502. }
  503. }
  504. void X3DImporter::XML_ReadNode_GetAttrVal_AsListCol3f(const int pAttrIdx, std::list<aiColor3D>& pValue)
  505. {
  506. std::vector<float> tlist;
  507. XML_ReadNode_GetAttrVal_AsArrF(pAttrIdx, tlist);// read as list
  508. if(tlist.size() % 3) Throw_ConvertFail_Str2ArrF(mReader->getAttributeValue(pAttrIdx));
  509. // copy data to array
  510. for(std::vector<float>::iterator it = tlist.begin(); it != tlist.end();)
  511. {
  512. aiColor3D tcol;
  513. tcol.r = *it++;
  514. tcol.g = *it++;
  515. tcol.b = *it++;
  516. pValue.push_back(tcol);
  517. }
  518. }
  519. void X3DImporter::XML_ReadNode_GetAttrVal_AsArrCol3f(const int pAttrIdx, std::vector<aiColor3D>& pValue)
  520. {
  521. std::list<aiColor3D> tlist;
  522. XML_ReadNode_GetAttrVal_AsListCol3f(pAttrIdx, tlist);// read as list
  523. // and copy to array
  524. if(!tlist.empty())
  525. {
  526. pValue.reserve(tlist.size());
  527. for(std::list<aiColor3D>::iterator it = tlist.begin(); it != tlist.end(); ++it) pValue.push_back(*it);
  528. }
  529. }
  530. void X3DImporter::XML_ReadNode_GetAttrVal_AsListCol4f(const int pAttrIdx, std::list<aiColor4D>& pValue)
  531. {
  532. std::vector<float> tlist;
  533. XML_ReadNode_GetAttrVal_AsArrF(pAttrIdx, tlist);// read as list
  534. if(tlist.size() % 4) Throw_ConvertFail_Str2ArrF(mReader->getAttributeValue(pAttrIdx));
  535. // copy data to array
  536. for(std::vector<float>::iterator it = tlist.begin(); it != tlist.end();)
  537. {
  538. aiColor4D tcol;
  539. tcol.r = *it++;
  540. tcol.g = *it++;
  541. tcol.b = *it++;
  542. tcol.a = *it++;
  543. pValue.push_back(tcol);
  544. }
  545. }
  546. void X3DImporter::XML_ReadNode_GetAttrVal_AsArrCol4f(const int pAttrIdx, std::vector<aiColor4D>& pValue)
  547. {
  548. std::list<aiColor4D> tlist;
  549. XML_ReadNode_GetAttrVal_AsListCol4f(pAttrIdx, tlist);// read as list
  550. // and copy to array
  551. if(!tlist.empty())
  552. {
  553. pValue.reserve(tlist.size());
  554. for ( std::list<aiColor4D>::iterator it = tlist.begin(); it != tlist.end(); ++it )
  555. {
  556. pValue.push_back( *it );
  557. }
  558. }
  559. }
  560. void X3DImporter::XML_ReadNode_GetAttrVal_AsListVec2f(const int pAttrIdx, std::list<aiVector2D>& pValue)
  561. {
  562. std::vector<float> tlist;
  563. XML_ReadNode_GetAttrVal_AsArrF(pAttrIdx, tlist);// read as list
  564. if ( tlist.size() % 2 )
  565. {
  566. Throw_ConvertFail_Str2ArrF( mReader->getAttributeValue( pAttrIdx ) );
  567. }
  568. // copy data to array
  569. for(std::vector<float>::iterator it = tlist.begin(); it != tlist.end();)
  570. {
  571. aiVector2D tvec;
  572. tvec.x = *it++;
  573. tvec.y = *it++;
  574. pValue.push_back(tvec);
  575. }
  576. }
  577. void X3DImporter::XML_ReadNode_GetAttrVal_AsArrVec2f(const int pAttrIdx, std::vector<aiVector2D>& pValue)
  578. {
  579. std::list<aiVector2D> tlist;
  580. XML_ReadNode_GetAttrVal_AsListVec2f(pAttrIdx, tlist);// read as list
  581. // and copy to array
  582. if(!tlist.empty())
  583. {
  584. pValue.reserve(tlist.size());
  585. for ( std::list<aiVector2D>::iterator it = tlist.begin(); it != tlist.end(); ++it )
  586. {
  587. pValue.push_back( *it );
  588. }
  589. }
  590. }
  591. void X3DImporter::XML_ReadNode_GetAttrVal_AsListVec3f(const int pAttrIdx, std::list<aiVector3D>& pValue)
  592. {
  593. std::vector<float> tlist;
  594. XML_ReadNode_GetAttrVal_AsArrF(pAttrIdx, tlist);// read as list
  595. if ( tlist.size() % 3 )
  596. {
  597. Throw_ConvertFail_Str2ArrF( mReader->getAttributeValue( pAttrIdx ) );
  598. }
  599. // copy data to array
  600. for(std::vector<float>::iterator it = tlist.begin(); it != tlist.end();)
  601. {
  602. aiVector3D tvec;
  603. tvec.x = *it++;
  604. tvec.y = *it++;
  605. tvec.z = *it++;
  606. pValue.push_back(tvec);
  607. }
  608. }
  609. void X3DImporter::XML_ReadNode_GetAttrVal_AsArrVec3f(const int pAttrIdx, std::vector<aiVector3D>& pValue)
  610. {
  611. std::list<aiVector3D> tlist;
  612. XML_ReadNode_GetAttrVal_AsListVec3f(pAttrIdx, tlist);// read as list
  613. // and copy to array
  614. if(!tlist.empty())
  615. {
  616. pValue.reserve(tlist.size());
  617. for ( std::list<aiVector3D>::iterator it = tlist.begin(); it != tlist.end(); ++it )
  618. {
  619. pValue.push_back( *it );
  620. }
  621. }
  622. }
  623. void X3DImporter::XML_ReadNode_GetAttrVal_AsListS(const int pAttrIdx, std::list<std::string>& pValue)
  624. {
  625. // make copy of attribute value - strings list.
  626. const size_t tok_str_len = strlen(mReader->getAttributeValue(pAttrIdx));
  627. if ( 0 == tok_str_len )
  628. {
  629. Throw_IncorrectAttrValue( mReader->getAttributeName( pAttrIdx ) );
  630. }
  631. // get pointer to begin of value.
  632. char *tok_str = const_cast<char*>(mReader->getAttributeValue(pAttrIdx));
  633. char *tok_str_end = tok_str + tok_str_len;
  634. // string list has following format: attr_name='"s1" "s2" "sn"'.
  635. do
  636. {
  637. char* tbeg;
  638. char* tend;
  639. size_t tlen;
  640. std::string tstr;
  641. // find begin of string(element of string list): "sn".
  642. tbeg = strstr(tok_str, "\"");
  643. if(tbeg == nullptr) Throw_IncorrectAttrValue(mReader->getAttributeName(pAttrIdx));
  644. tbeg++;// forward pointer from '\"' symbol to next after it.
  645. tok_str = tbeg;
  646. // find end of string(element of string list): "sn".
  647. tend = strstr(tok_str, "\"");
  648. if(tend == nullptr) Throw_IncorrectAttrValue(mReader->getAttributeName(pAttrIdx));
  649. tok_str = tend + 1;
  650. // create storage for new string
  651. tlen = tend - tbeg;
  652. tstr.resize(tlen);// reserve enough space and copy data
  653. memcpy((void*)tstr.data(), tbeg, tlen);// not strcpy because end of copied string from tok_str has no terminator.
  654. // and store string in output list.
  655. pValue.push_back(tstr);
  656. } while(tok_str < tok_str_end);
  657. }
  658. /*********************************************************************************************************************************************/
  659. /****************************************************** Functions: geometry helper set ******************************************************/
  660. /*********************************************************************************************************************************************/
  661. aiVector3D X3DImporter::GeometryHelper_Make_Point2D(const float pAngle, const float pRadius)
  662. {
  663. return aiVector3D(pRadius * std::cos(pAngle), pRadius * std::sin(pAngle), 0);
  664. }
  665. void X3DImporter::GeometryHelper_Make_Arc2D(const float pStartAngle, const float pEndAngle, const float pRadius, size_t pNumSegments,
  666. std::list<aiVector3D>& pVertices)
  667. {
  668. // check argument values ranges.
  669. if ( ( pStartAngle < -AI_MATH_TWO_PI_F ) || ( pStartAngle > AI_MATH_TWO_PI_F ) )
  670. {
  671. Throw_ArgOutOfRange( "GeometryHelper_Make_Arc2D.pStartAngle" );
  672. }
  673. if ( ( pEndAngle < -AI_MATH_TWO_PI_F ) || ( pEndAngle > AI_MATH_TWO_PI_F ) )
  674. {
  675. Throw_ArgOutOfRange( "GeometryHelper_Make_Arc2D.pEndAngle" );
  676. }
  677. if ( pRadius <= 0 )
  678. {
  679. Throw_ArgOutOfRange( "GeometryHelper_Make_Arc2D.pRadius" );
  680. }
  681. // calculate arc angle and check type of arc
  682. float angle_full = std::fabs(pEndAngle - pStartAngle);
  683. if ( ( angle_full > AI_MATH_TWO_PI_F ) || ( angle_full == 0.0f ) )
  684. {
  685. angle_full = AI_MATH_TWO_PI_F;
  686. }
  687. // calculate angle for one step - angle to next point of line.
  688. float angle_step = angle_full / (float)pNumSegments;
  689. // make points
  690. for(size_t pi = 0; pi <= pNumSegments; pi++)
  691. {
  692. float tangle = pStartAngle + pi * angle_step;
  693. pVertices.push_back(GeometryHelper_Make_Point2D(tangle, pRadius));
  694. }// for(size_t pi = 0; pi <= pNumSegments; pi++)
  695. // if we making full circle then add last vertex equal to first vertex
  696. if(angle_full == AI_MATH_TWO_PI_F) pVertices.push_back(*pVertices.begin());
  697. }
  698. void X3DImporter::GeometryHelper_Extend_PointToLine(const std::list<aiVector3D>& pPoint, std::list<aiVector3D>& pLine)
  699. {
  700. std::list<aiVector3D>::const_iterator pit = pPoint.begin();
  701. std::list<aiVector3D>::const_iterator pit_last = pPoint.end();
  702. --pit_last;
  703. if ( pPoint.size() < 2 )
  704. {
  705. Throw_ArgOutOfRange( "GeometryHelper_Extend_PointToLine.pPoint.size() can not be less than 2." );
  706. }
  707. // add first point of first line.
  708. pLine.push_back(*pit++);
  709. // add internal points
  710. while(pit != pit_last)
  711. {
  712. pLine.push_back(*pit);// second point of previous line
  713. pLine.push_back(*pit);// first point of next line
  714. ++pit;
  715. }
  716. // add last point of last line
  717. pLine.push_back(*pit);
  718. }
  719. void X3DImporter::GeometryHelper_Extend_PolylineIdxToLineIdx(const std::list<int32_t>& pPolylineCoordIdx, std::list<int32_t>& pLineCoordIdx)
  720. {
  721. std::list<int32_t>::const_iterator plit = pPolylineCoordIdx.begin();
  722. while(plit != pPolylineCoordIdx.end())
  723. {
  724. // add first point of polyline
  725. pLineCoordIdx.push_back(*plit++);
  726. while((*plit != (-1)) && (plit != pPolylineCoordIdx.end()))
  727. {
  728. std::list<int32_t>::const_iterator plit_next;
  729. plit_next = plit, ++plit_next;
  730. pLineCoordIdx.push_back(*plit);// second point of previous line.
  731. pLineCoordIdx.push_back(-1);// delimiter
  732. if((*plit_next == (-1)) || (plit_next == pPolylineCoordIdx.end())) break;// current polyline is finished
  733. pLineCoordIdx.push_back(*plit);// first point of next line.
  734. plit = plit_next;
  735. }// while((*plit != (-1)) && (plit != pPolylineCoordIdx.end()))
  736. }// while(plit != pPolylineCoordIdx.end())
  737. }
  738. #define MESH_RectParallelepiped_CREATE_VERT \
  739. aiVector3D vert_set[8]; \
  740. float x1, x2, y1, y2, z1, z2, hs; \
  741. \
  742. hs = pSize.x / 2, x1 = -hs, x2 = hs; \
  743. hs = pSize.y / 2, y1 = -hs, y2 = hs; \
  744. hs = pSize.z / 2, z1 = -hs, z2 = hs; \
  745. vert_set[0].Set(x2, y1, z2); \
  746. vert_set[1].Set(x2, y2, z2); \
  747. vert_set[2].Set(x2, y2, z1); \
  748. vert_set[3].Set(x2, y1, z1); \
  749. vert_set[4].Set(x1, y1, z2); \
  750. vert_set[5].Set(x1, y2, z2); \
  751. vert_set[6].Set(x1, y2, z1); \
  752. vert_set[7].Set(x1, y1, z1)
  753. void X3DImporter::GeometryHelper_MakeQL_RectParallelepiped(const aiVector3D& pSize, std::list<aiVector3D>& pVertices)
  754. {
  755. MESH_RectParallelepiped_CREATE_VERT;
  756. MACRO_FACE_ADD_QUAD_FA(true, pVertices, vert_set, 3, 2, 1, 0);// front
  757. MACRO_FACE_ADD_QUAD_FA(true, pVertices, vert_set, 6, 7, 4, 5);// back
  758. MACRO_FACE_ADD_QUAD_FA(true, pVertices, vert_set, 7, 3, 0, 4);// left
  759. MACRO_FACE_ADD_QUAD_FA(true, pVertices, vert_set, 2, 6, 5, 1);// right
  760. MACRO_FACE_ADD_QUAD_FA(true, pVertices, vert_set, 0, 1, 5, 4);// top
  761. MACRO_FACE_ADD_QUAD_FA(true, pVertices, vert_set, 7, 6, 2, 3);// bottom
  762. }
  763. #undef MESH_RectParallelepiped_CREATE_VERT
  764. void X3DImporter::GeometryHelper_CoordIdxStr2FacesArr(const std::vector<int32_t>& pCoordIdx, std::vector<aiFace>& pFaces, unsigned int& pPrimitiveTypes) const
  765. {
  766. std::vector<int32_t> f_data(pCoordIdx);
  767. std::vector<unsigned int> inds;
  768. unsigned int prim_type = 0;
  769. if ( f_data.back() != ( -1 ) )
  770. {
  771. f_data.push_back( -1 );
  772. }
  773. // reserve average size.
  774. pFaces.reserve(f_data.size() / 3);
  775. inds.reserve(4);
  776. //PrintVectorSet("build. ci", pCoordIdx);
  777. for(std::vector<int32_t>::iterator it = f_data.begin(); it != f_data.end(); ++it)
  778. {
  779. // when face is got count how many indices in it.
  780. if(*it == (-1))
  781. {
  782. aiFace tface;
  783. size_t ts;
  784. ts = inds.size();
  785. switch(ts)
  786. {
  787. case 0: goto mg_m_err;
  788. case 1: prim_type |= aiPrimitiveType_POINT; break;
  789. case 2: prim_type |= aiPrimitiveType_LINE; break;
  790. case 3: prim_type |= aiPrimitiveType_TRIANGLE; break;
  791. default: prim_type |= aiPrimitiveType_POLYGON; break;
  792. }
  793. tface.mNumIndices = static_cast<unsigned int>(ts);
  794. tface.mIndices = new unsigned int[ts];
  795. memcpy(tface.mIndices, inds.data(), ts * sizeof(unsigned int));
  796. pFaces.push_back(tface);
  797. inds.clear();
  798. }// if(*it == (-1))
  799. else
  800. {
  801. inds.push_back(*it);
  802. }// if(*it == (-1)) else
  803. }// for(std::list<int32_t>::iterator it = f_data.begin(); it != f_data.end(); it++)
  804. //PrintVectorSet("build. faces", pCoordIdx);
  805. pPrimitiveTypes = prim_type;
  806. return;
  807. mg_m_err:
  808. for(size_t i = 0, i_e = pFaces.size(); i < i_e; i++) delete [] pFaces.at(i).mIndices;
  809. pFaces.clear();
  810. }
  811. void X3DImporter::MeshGeometry_AddColor(aiMesh& pMesh, const std::list<aiColor3D>& pColors, const bool pColorPerVertex) const
  812. {
  813. std::list<aiColor4D> tcol;
  814. // create RGBA array from RGB.
  815. for(std::list<aiColor3D>::const_iterator it = pColors.begin(); it != pColors.end(); ++it) tcol.push_back(aiColor4D((*it).r, (*it).g, (*it).b, 1));
  816. // call existing function for adding RGBA colors
  817. MeshGeometry_AddColor(pMesh, tcol, pColorPerVertex);
  818. }
  819. void X3DImporter::MeshGeometry_AddColor(aiMesh& pMesh, const std::list<aiColor4D>& pColors, const bool pColorPerVertex) const
  820. {
  821. std::list<aiColor4D>::const_iterator col_it = pColors.begin();
  822. if(pColorPerVertex)
  823. {
  824. if(pColors.size() < pMesh.mNumVertices)
  825. {
  826. throw DeadlyImportError("MeshGeometry_AddColor1. Colors count(" + to_string(pColors.size()) + ") can not be less than Vertices count(" +
  827. to_string(pMesh.mNumVertices) + ").");
  828. }
  829. // copy colors to mesh
  830. pMesh.mColors[0] = new aiColor4D[pMesh.mNumVertices];
  831. for(size_t i = 0; i < pMesh.mNumVertices; i++) pMesh.mColors[0][i] = *col_it++;
  832. }// if(pColorPerVertex)
  833. else
  834. {
  835. if(pColors.size() < pMesh.mNumFaces)
  836. {
  837. throw DeadlyImportError("MeshGeometry_AddColor1. Colors count(" + to_string(pColors.size()) + ") can not be less than Faces count(" +
  838. to_string(pMesh.mNumFaces) + ").");
  839. }
  840. // copy colors to mesh
  841. pMesh.mColors[0] = new aiColor4D[pMesh.mNumVertices];
  842. for(size_t fi = 0; fi < pMesh.mNumFaces; fi++)
  843. {
  844. // apply color to all vertices of face
  845. for ( size_t vi = 0, vi_e = pMesh.mFaces[ fi ].mNumIndices; vi < vi_e; vi++ )
  846. {
  847. pMesh.mColors[ 0 ][ pMesh.mFaces[ fi ].mIndices[ vi ] ] = *col_it;
  848. }
  849. ++col_it;
  850. }
  851. }// if(pColorPerVertex) else
  852. }
  853. void X3DImporter::MeshGeometry_AddColor(aiMesh& pMesh, const std::vector<int32_t>& pCoordIdx, const std::vector<int32_t>& pColorIdx,
  854. const std::list<aiColor3D>& pColors, const bool pColorPerVertex) const
  855. {
  856. std::list<aiColor4D> tcol;
  857. // create RGBA array from RGB.
  858. for ( std::list<aiColor3D>::const_iterator it = pColors.begin(); it != pColors.end(); ++it )
  859. {
  860. tcol.push_back( aiColor4D( ( *it ).r, ( *it ).g, ( *it ).b, 1 ) );
  861. }
  862. // call existing function for adding RGBA colors
  863. MeshGeometry_AddColor(pMesh, pCoordIdx, pColorIdx, tcol, pColorPerVertex);
  864. }
  865. void X3DImporter::MeshGeometry_AddColor(aiMesh& pMesh, const std::vector<int32_t>& pCoordIdx, const std::vector<int32_t>& pColorIdx,
  866. const std::list<aiColor4D>& pColors, const bool pColorPerVertex) const
  867. {
  868. std::vector<aiColor4D> col_tgt_arr;
  869. std::list<aiColor4D> col_tgt_list;
  870. std::vector<aiColor4D> col_arr_copy;
  871. if ( pCoordIdx.size() == 0 )
  872. {
  873. throw DeadlyImportError( "MeshGeometry_AddColor2. pCoordIdx can not be empty." );
  874. }
  875. // copy list to array because we are need indexed access to colors.
  876. col_arr_copy.reserve(pColors.size());
  877. for ( std::list<aiColor4D>::const_iterator it = pColors.begin(); it != pColors.end(); ++it )
  878. {
  879. col_arr_copy.push_back( *it );
  880. }
  881. if(pColorPerVertex)
  882. {
  883. if(pColorIdx.size() > 0)
  884. {
  885. // check indices array count.
  886. if(pColorIdx.size() < pCoordIdx.size())
  887. {
  888. throw DeadlyImportError("MeshGeometry_AddColor2. Colors indices count(" + to_string(pColorIdx.size()) +
  889. ") can not be less than Coords inidces count(" + to_string(pCoordIdx.size()) + ").");
  890. }
  891. // create list with colors for every vertex.
  892. col_tgt_arr.resize(pMesh.mNumVertices);
  893. for(std::vector<int32_t>::const_iterator colidx_it = pColorIdx.begin(), coordidx_it = pCoordIdx.begin(); colidx_it != pColorIdx.end(); ++colidx_it, ++coordidx_it)
  894. {
  895. if ( *colidx_it == ( -1 ) )
  896. {
  897. continue;// skip faces delimiter
  898. }
  899. if ( ( unsigned int ) ( *coordidx_it ) > pMesh.mNumVertices )
  900. {
  901. throw DeadlyImportError( "MeshGeometry_AddColor2. Coordinate idx is out of range." );
  902. }
  903. if ( ( unsigned int ) *colidx_it > pMesh.mNumVertices )
  904. {
  905. throw DeadlyImportError( "MeshGeometry_AddColor2. Color idx is out of range." );
  906. }
  907. col_tgt_arr[*coordidx_it] = col_arr_copy[*colidx_it];
  908. }
  909. }// if(pColorIdx.size() > 0)
  910. else
  911. {
  912. // when color indices list is absent use CoordIdx.
  913. // check indices array count.
  914. if(pColors.size() < pMesh.mNumVertices)
  915. {
  916. throw DeadlyImportError("MeshGeometry_AddColor2. Colors count(" + to_string(pColors.size()) + ") can not be less than Vertices count(" +
  917. to_string(pMesh.mNumVertices) + ").");
  918. }
  919. // create list with colors for every vertex.
  920. col_tgt_arr.resize(pMesh.mNumVertices);
  921. for ( size_t i = 0; i < pMesh.mNumVertices; i++ )
  922. {
  923. col_tgt_arr[ i ] = col_arr_copy[ i ];
  924. }
  925. }// if(pColorIdx.size() > 0) else
  926. }// if(pColorPerVertex)
  927. else
  928. {
  929. if(pColorIdx.size() > 0)
  930. {
  931. // check indices array count.
  932. if(pColorIdx.size() < pMesh.mNumFaces)
  933. {
  934. throw DeadlyImportError("MeshGeometry_AddColor2. Colors indices count(" + to_string(pColorIdx.size()) +
  935. ") can not be less than Faces count(" + to_string(pMesh.mNumFaces) + ").");
  936. }
  937. // create list with colors for every vertex using faces indices.
  938. col_tgt_arr.resize(pMesh.mNumFaces);
  939. std::vector<int32_t>::const_iterator colidx_it = pColorIdx.begin();
  940. for(size_t fi = 0; fi < pMesh.mNumFaces; fi++)
  941. {
  942. if((unsigned int)*colidx_it > pMesh.mNumFaces) throw DeadlyImportError("MeshGeometry_AddColor2. Face idx is out of range.");
  943. col_tgt_arr[fi] = col_arr_copy[*colidx_it++];
  944. }
  945. }// if(pColorIdx.size() > 0)
  946. else
  947. {
  948. // when color indices list is absent use CoordIdx.
  949. // check indices array count.
  950. if(pColors.size() < pMesh.mNumFaces)
  951. {
  952. throw DeadlyImportError("MeshGeometry_AddColor2. Colors count(" + to_string(pColors.size()) + ") can not be less than Faces count(" +
  953. to_string(pMesh.mNumFaces) + ").");
  954. }
  955. // create list with colors for every vertex using faces indices.
  956. col_tgt_arr.resize(pMesh.mNumFaces);
  957. for(size_t fi = 0; fi < pMesh.mNumFaces; fi++) col_tgt_arr[fi] = col_arr_copy[fi];
  958. }// if(pColorIdx.size() > 0) else
  959. }// if(pColorPerVertex) else
  960. // copy array to list for calling function that add colors.
  961. for(std::vector<aiColor4D>::const_iterator it = col_tgt_arr.begin(); it != col_tgt_arr.end(); ++it) col_tgt_list.push_back(*it);
  962. // add prepared colors list to mesh.
  963. MeshGeometry_AddColor(pMesh, col_tgt_list, pColorPerVertex);
  964. }
  965. void X3DImporter::MeshGeometry_AddNormal(aiMesh& pMesh, const std::vector<int32_t>& pCoordIdx, const std::vector<int32_t>& pNormalIdx,
  966. const std::list<aiVector3D>& pNormals, const bool pNormalPerVertex) const
  967. {
  968. std::vector<size_t> tind;
  969. std::vector<aiVector3D> norm_arr_copy;
  970. // copy list to array because we are need indexed access to normals.
  971. norm_arr_copy.reserve(pNormals.size());
  972. for ( std::list<aiVector3D>::const_iterator it = pNormals.begin(); it != pNormals.end(); ++it )
  973. {
  974. norm_arr_copy.push_back( *it );
  975. }
  976. if(pNormalPerVertex)
  977. {
  978. if(pNormalIdx.size() > 0)
  979. {
  980. // check indices array count.
  981. if(pNormalIdx.size() != pCoordIdx.size()) throw DeadlyImportError("Normals and Coords inidces count must be equal.");
  982. tind.reserve(pNormalIdx.size());
  983. for(std::vector<int32_t>::const_iterator it = pNormalIdx.begin(); it != pNormalIdx.end(); ++it)
  984. {
  985. if(*it != (-1)) tind.push_back(*it);
  986. }
  987. // copy normals to mesh
  988. pMesh.mNormals = new aiVector3D[pMesh.mNumVertices];
  989. for(size_t i = 0; (i < pMesh.mNumVertices) && (i < tind.size()); i++)
  990. {
  991. if(tind[i] >= norm_arr_copy.size())
  992. throw DeadlyImportError("MeshGeometry_AddNormal. Normal index(" + to_string(tind[i]) +
  993. ") is out of range. Normals count: " + to_string(norm_arr_copy.size()) + ".");
  994. pMesh.mNormals[i] = norm_arr_copy[tind[i]];
  995. }
  996. }
  997. else
  998. {
  999. if(pNormals.size() != pMesh.mNumVertices) throw DeadlyImportError("MeshGeometry_AddNormal. Normals and vertices count must be equal.");
  1000. // copy normals to mesh
  1001. pMesh.mNormals = new aiVector3D[pMesh.mNumVertices];
  1002. std::list<aiVector3D>::const_iterator norm_it = pNormals.begin();
  1003. for(size_t i = 0; i < pMesh.mNumVertices; i++) pMesh.mNormals[i] = *norm_it++;
  1004. }
  1005. }// if(pNormalPerVertex)
  1006. else
  1007. {
  1008. if(pNormalIdx.size() > 0)
  1009. {
  1010. if(pMesh.mNumFaces != pNormalIdx.size()) throw DeadlyImportError("Normals faces count must be equal to mesh faces count.");
  1011. std::vector<int32_t>::const_iterator normidx_it = pNormalIdx.begin();
  1012. tind.reserve(pNormalIdx.size());
  1013. for(size_t i = 0, i_e = pNormalIdx.size(); i < i_e; i++) tind.push_back(*normidx_it++);
  1014. }
  1015. else
  1016. {
  1017. tind.reserve(pMesh.mNumFaces);
  1018. for(size_t i = 0; i < pMesh.mNumFaces; i++) tind.push_back(i);
  1019. }
  1020. // copy normals to mesh
  1021. pMesh.mNormals = new aiVector3D[pMesh.mNumVertices];
  1022. for(size_t fi = 0; fi < pMesh.mNumFaces; fi++)
  1023. {
  1024. aiVector3D tnorm;
  1025. tnorm = norm_arr_copy[tind[fi]];
  1026. for(size_t vi = 0, vi_e = pMesh.mFaces[fi].mNumIndices; vi < vi_e; vi++) pMesh.mNormals[pMesh.mFaces[fi].mIndices[vi]] = tnorm;
  1027. }
  1028. }// if(pNormalPerVertex) else
  1029. }
  1030. void X3DImporter::MeshGeometry_AddNormal(aiMesh& pMesh, const std::list<aiVector3D>& pNormals, const bool pNormalPerVertex) const
  1031. {
  1032. std::list<aiVector3D>::const_iterator norm_it = pNormals.begin();
  1033. if(pNormalPerVertex)
  1034. {
  1035. if(pNormals.size() != pMesh.mNumVertices) throw DeadlyImportError("MeshGeometry_AddNormal. Normals and vertices count must be equal.");
  1036. // copy normals to mesh
  1037. pMesh.mNormals = new aiVector3D[pMesh.mNumVertices];
  1038. for(size_t i = 0; i < pMesh.mNumVertices; i++) pMesh.mNormals[i] = *norm_it++;
  1039. }// if(pNormalPerVertex)
  1040. else
  1041. {
  1042. if(pNormals.size() != pMesh.mNumFaces) throw DeadlyImportError("MeshGeometry_AddNormal. Normals and faces count must be equal.");
  1043. // copy normals to mesh
  1044. pMesh.mNormals = new aiVector3D[pMesh.mNumVertices];
  1045. for(size_t fi = 0; fi < pMesh.mNumFaces; fi++)
  1046. {
  1047. // apply color to all vertices of face
  1048. for(size_t vi = 0, vi_e = pMesh.mFaces[fi].mNumIndices; vi < vi_e; vi++) pMesh.mNormals[pMesh.mFaces[fi].mIndices[vi]] = *norm_it;
  1049. ++norm_it;
  1050. }
  1051. }// if(pNormalPerVertex) else
  1052. }
  1053. void X3DImporter::MeshGeometry_AddTexCoord(aiMesh& pMesh, const std::vector<int32_t>& pCoordIdx, const std::vector<int32_t>& pTexCoordIdx,
  1054. const std::list<aiVector2D>& pTexCoords) const
  1055. {
  1056. std::vector<aiVector3D> texcoord_arr_copy;
  1057. std::vector<aiFace> faces;
  1058. unsigned int prim_type;
  1059. // copy list to array because we are need indexed access to normals.
  1060. texcoord_arr_copy.reserve(pTexCoords.size());
  1061. for(std::list<aiVector2D>::const_iterator it = pTexCoords.begin(); it != pTexCoords.end(); ++it)
  1062. {
  1063. texcoord_arr_copy.push_back(aiVector3D((*it).x, (*it).y, 0));
  1064. }
  1065. if(pTexCoordIdx.size() > 0)
  1066. {
  1067. GeometryHelper_CoordIdxStr2FacesArr(pTexCoordIdx, faces, prim_type);
  1068. if ( faces.empty() )
  1069. {
  1070. throw DeadlyImportError( "Failed to add texture coordinates to mesh, faces list is empty." );
  1071. }
  1072. if ( faces.size() != pMesh.mNumFaces )
  1073. {
  1074. throw DeadlyImportError( "Texture coordinates faces count must be equal to mesh faces count." );
  1075. }
  1076. }
  1077. else
  1078. {
  1079. GeometryHelper_CoordIdxStr2FacesArr(pCoordIdx, faces, prim_type);
  1080. }
  1081. pMesh.mTextureCoords[0] = new aiVector3D[pMesh.mNumVertices];
  1082. pMesh.mNumUVComponents[0] = 2;
  1083. for(size_t fi = 0, fi_e = faces.size(); fi < fi_e; fi++)
  1084. {
  1085. if(pMesh.mFaces[fi].mNumIndices != faces.at(fi).mNumIndices)
  1086. throw DeadlyImportError("Number of indices in texture face and mesh face must be equal. Invalid face index: " + to_string(fi) + ".");
  1087. for(size_t ii = 0; ii < pMesh.mFaces[fi].mNumIndices; ii++)
  1088. {
  1089. size_t vert_idx = pMesh.mFaces[fi].mIndices[ii];
  1090. size_t tc_idx = faces.at(fi).mIndices[ii];
  1091. pMesh.mTextureCoords[0][vert_idx] = texcoord_arr_copy.at(tc_idx);
  1092. }
  1093. }// for(size_t fi = 0, fi_e = faces.size(); fi < fi_e; fi++)
  1094. }
  1095. void X3DImporter::MeshGeometry_AddTexCoord(aiMesh& pMesh, const std::list<aiVector2D>& pTexCoords) const
  1096. {
  1097. std::vector<aiVector3D> tc_arr_copy;
  1098. if ( pTexCoords.size() != pMesh.mNumVertices )
  1099. {
  1100. throw DeadlyImportError( "MeshGeometry_AddTexCoord. Texture coordinates and vertices count must be equal." );
  1101. }
  1102. // copy list to array because we are need convert aiVector2D to aiVector3D and also get indexed access as a bonus.
  1103. tc_arr_copy.reserve(pTexCoords.size());
  1104. for ( std::list<aiVector2D>::const_iterator it = pTexCoords.begin(); it != pTexCoords.end(); ++it )
  1105. {
  1106. tc_arr_copy.push_back( aiVector3D( ( *it ).x, ( *it ).y, 0 ) );
  1107. }
  1108. // copy texture coordinates to mesh
  1109. pMesh.mTextureCoords[0] = new aiVector3D[pMesh.mNumVertices];
  1110. pMesh.mNumUVComponents[0] = 2;
  1111. for ( size_t i = 0; i < pMesh.mNumVertices; i++ )
  1112. {
  1113. pMesh.mTextureCoords[ 0 ][ i ] = tc_arr_copy[ i ];
  1114. }
  1115. }
  1116. aiMesh* X3DImporter::GeometryHelper_MakeMesh(const std::vector<int32_t>& pCoordIdx, const std::list<aiVector3D>& pVertices) const
  1117. {
  1118. std::vector<aiFace> faces;
  1119. unsigned int prim_type = 0;
  1120. // create faces array from input string with vertices indices.
  1121. GeometryHelper_CoordIdxStr2FacesArr(pCoordIdx, faces, prim_type);
  1122. if ( !faces.size() )
  1123. {
  1124. throw DeadlyImportError( "Failed to create mesh, faces list is empty." );
  1125. }
  1126. //
  1127. // Create new mesh and copy geometry data.
  1128. //
  1129. aiMesh *tmesh = new aiMesh;
  1130. size_t ts = faces.size();
  1131. // faces
  1132. tmesh->mFaces = new aiFace[ts];
  1133. tmesh->mNumFaces = static_cast<unsigned int>(ts);
  1134. for(size_t i = 0; i < ts; i++) tmesh->mFaces[i] = faces.at(i);
  1135. // vertices
  1136. std::list<aiVector3D>::const_iterator vit = pVertices.begin();
  1137. ts = pVertices.size();
  1138. tmesh->mVertices = new aiVector3D[ts];
  1139. tmesh->mNumVertices = static_cast<unsigned int>(ts);
  1140. for ( size_t i = 0; i < ts; i++ )
  1141. {
  1142. tmesh->mVertices[ i ] = *vit++;
  1143. }
  1144. // set primitives type and return result.
  1145. tmesh->mPrimitiveTypes = prim_type;
  1146. return tmesh;
  1147. }
  1148. /*********************************************************************************************************************************************/
  1149. /************************************************************ Functions: parse set ***********************************************************/
  1150. /*********************************************************************************************************************************************/
  1151. void X3DImporter::ParseHelper_Group_Begin(const bool pStatic)
  1152. {
  1153. CX3DImporter_NodeElement_Group* new_group = new CX3DImporter_NodeElement_Group(NodeElement_Cur, pStatic);// create new node with current node as parent.
  1154. // if we are adding not the root element then add new element to current element child list.
  1155. if ( NodeElement_Cur != nullptr )
  1156. {
  1157. NodeElement_Cur->Child.push_back( new_group );
  1158. }
  1159. NodeElement_List.push_back(new_group);// it's a new element - add it to list.
  1160. NodeElement_Cur = new_group;// switch current element to new one.
  1161. }
  1162. void X3DImporter::ParseHelper_Node_Enter(CX3DImporter_NodeElement* pNode)
  1163. {
  1164. NodeElement_Cur->Child.push_back(pNode);// add new element to current element child list.
  1165. NodeElement_Cur = pNode;// switch current element to new one.
  1166. }
  1167. void X3DImporter::ParseHelper_Node_Exit()
  1168. {
  1169. // check if we can walk up.
  1170. if ( NodeElement_Cur != nullptr )
  1171. {
  1172. NodeElement_Cur = NodeElement_Cur->Parent;
  1173. }
  1174. }
  1175. void X3DImporter::ParseHelper_FixTruncatedFloatString(const char* pInStr, std::string& pOutString)
  1176. {
  1177. pOutString.clear();
  1178. const size_t instr_len = strlen(pInStr);
  1179. if ( 0 == instr_len )
  1180. {
  1181. return;
  1182. }
  1183. pOutString.reserve(instr_len * 3 / 2);
  1184. // check and correct floats in format ".x". Must be "x.y".
  1185. if ( pInStr[ 0 ] == '.' )
  1186. {
  1187. pOutString.push_back( '0' );
  1188. }
  1189. pOutString.push_back(pInStr[0]);
  1190. for(size_t ci = 1; ci < instr_len; ci++)
  1191. {
  1192. if((pInStr[ci] == '.') && ((pInStr[ci - 1] == ' ') || (pInStr[ci - 1] == '-') || (pInStr[ci - 1] == '+') || (pInStr[ci - 1] == '\t')))
  1193. {
  1194. pOutString.push_back('0');
  1195. pOutString.push_back('.');
  1196. }
  1197. else
  1198. {
  1199. pOutString.push_back(pInStr[ci]);
  1200. }
  1201. }
  1202. }
  1203. extern FIVocabulary X3D_vocabulary_3_2;
  1204. extern FIVocabulary X3D_vocabulary_3_3;
  1205. void X3DImporter::ParseFile(const std::string& pFile, IOSystem* pIOHandler)
  1206. {
  1207. std::unique_ptr<FIReader> OldReader = std::move(mReader);// store current XMLreader.
  1208. std::unique_ptr<IOStream> file(pIOHandler->Open(pFile, "rb"));
  1209. // Check whether we can read from the file
  1210. if ( file.get() == nullptr )
  1211. {
  1212. throw DeadlyImportError( "Failed to open X3D file " + pFile + "." );
  1213. }
  1214. mReader = FIReader::create(file.get());
  1215. if ( !mReader )
  1216. {
  1217. throw DeadlyImportError( "Failed to create XML reader for file" + pFile + "." );
  1218. }
  1219. mReader->registerVocabulary("urn:web3d:x3d:fi-vocabulary-3.2", &X3D_vocabulary_3_2);
  1220. mReader->registerVocabulary("urn:web3d:x3d:fi-vocabulary-3.3", &X3D_vocabulary_3_3);
  1221. // start reading
  1222. ParseNode_Root();
  1223. // restore old XMLreader
  1224. mReader = std::move(OldReader);
  1225. }
  1226. void X3DImporter::ParseNode_Root()
  1227. {
  1228. // search for root tag <X3D>
  1229. if ( !XML_SearchNode( "X3D" ) )
  1230. {
  1231. throw DeadlyImportError( "Root node \"X3D\" not found." );
  1232. }
  1233. ParseHelper_Group_Begin();// create root node element.
  1234. // parse other contents
  1235. while(mReader->read())
  1236. {
  1237. if ( mReader->getNodeType() != irr::io::EXN_ELEMENT )
  1238. {
  1239. continue;
  1240. }
  1241. if(XML_CheckNode_NameEqual("head"))
  1242. ParseNode_Head();
  1243. else if(XML_CheckNode_NameEqual("Scene"))
  1244. ParseNode_Scene();
  1245. else
  1246. XML_CheckNode_SkipUnsupported("Root");
  1247. }
  1248. // exit from root node element.
  1249. ParseHelper_Node_Exit();
  1250. }
  1251. void X3DImporter::ParseNode_Head()
  1252. {
  1253. bool close_found = false;// flag: true if close tag of node are found.
  1254. while(mReader->read())
  1255. {
  1256. if(mReader->getNodeType() == irr::io::EXN_ELEMENT)
  1257. {
  1258. if(XML_CheckNode_NameEqual("meta"))
  1259. {
  1260. XML_CheckNode_MustBeEmpty();
  1261. // adding metadata from <head> as MetaString from <Scene>
  1262. bool added( false );
  1263. CX3DImporter_NodeElement_MetaString* ms = new CX3DImporter_NodeElement_MetaString(NodeElement_Cur);
  1264. ms->Name = mReader->getAttributeValueSafe("name");
  1265. // name must not be empty
  1266. if(!ms->Name.empty())
  1267. {
  1268. ms->Value.push_back(mReader->getAttributeValueSafe("content"));
  1269. NodeElement_List.push_back(ms);
  1270. if ( NodeElement_Cur != nullptr )
  1271. {
  1272. NodeElement_Cur->Child.push_back( ms );
  1273. added = true;
  1274. }
  1275. }
  1276. // if an error has occurred, release instance
  1277. if ( !added ) {
  1278. delete ms;
  1279. }
  1280. }// if(XML_CheckNode_NameEqual("meta"))
  1281. }// if(mReader->getNodeType() == irr::io::EXN_ELEMENT)
  1282. else if(mReader->getNodeType() == irr::io::EXN_ELEMENT_END)
  1283. {
  1284. if(XML_CheckNode_NameEqual("head"))
  1285. {
  1286. close_found = true;
  1287. break;
  1288. }
  1289. }// if(mReader->getNodeType() == irr::io::EXN_ELEMENT) else
  1290. }// while(mReader->read())
  1291. if ( !close_found )
  1292. {
  1293. Throw_CloseNotFound( "head" );
  1294. }
  1295. }
  1296. void X3DImporter::ParseNode_Scene()
  1297. {
  1298. auto GroupCounter_Increase = [](size_t& pCounter, const char* pGroupName) -> void
  1299. {
  1300. pCounter++;
  1301. if(pCounter == 0) throw DeadlyImportError("Group counter overflow. Too much groups with type: " + std::string(pGroupName) + ".");
  1302. };
  1303. auto GroupCounter_Decrease = [&](size_t& pCounter, const char* pGroupName) -> void
  1304. {
  1305. if(pCounter == 0) Throw_TagCountIncorrect(pGroupName);
  1306. pCounter--;
  1307. };
  1308. static const char* GroupName_Group = "Group";
  1309. static const char* GroupName_StaticGroup = "StaticGroup";
  1310. static const char* GroupName_Transform = "Transform";
  1311. static const char* GroupName_Switch = "Switch";
  1312. bool close_found = false;
  1313. size_t counter_group = 0;
  1314. size_t counter_transform = 0;
  1315. size_t counter_switch = 0;
  1316. // while create static node? Because objects name used deeper in "USE" attribute can be equal to some meta in <head> node.
  1317. ParseHelper_Group_Begin(true);
  1318. while(mReader->read())
  1319. {
  1320. if(mReader->getNodeType() == irr::io::EXN_ELEMENT)
  1321. {
  1322. if(XML_CheckNode_NameEqual("Shape"))
  1323. {
  1324. ParseNode_Shape_Shape();
  1325. }
  1326. else if(XML_CheckNode_NameEqual(GroupName_Group))
  1327. {
  1328. GroupCounter_Increase(counter_group, GroupName_Group);
  1329. ParseNode_Grouping_Group();
  1330. // if node is empty then decrease group counter at this place.
  1331. if(mReader->isEmptyElement()) GroupCounter_Decrease(counter_group, GroupName_Group);
  1332. }
  1333. else if(XML_CheckNode_NameEqual(GroupName_StaticGroup))
  1334. {
  1335. GroupCounter_Increase(counter_group, GroupName_StaticGroup);
  1336. ParseNode_Grouping_StaticGroup();
  1337. // if node is empty then decrease group counter at this place.
  1338. if(mReader->isEmptyElement()) GroupCounter_Decrease(counter_group, GroupName_StaticGroup);
  1339. }
  1340. else if(XML_CheckNode_NameEqual(GroupName_Transform))
  1341. {
  1342. GroupCounter_Increase(counter_transform, GroupName_Transform);
  1343. ParseNode_Grouping_Transform();
  1344. // if node is empty then decrease group counter at this place.
  1345. if(mReader->isEmptyElement()) GroupCounter_Decrease(counter_transform, GroupName_Transform);
  1346. }
  1347. else if(XML_CheckNode_NameEqual(GroupName_Switch))
  1348. {
  1349. GroupCounter_Increase(counter_switch, GroupName_Switch);
  1350. ParseNode_Grouping_Switch();
  1351. // if node is empty then decrease group counter at this place.
  1352. if(mReader->isEmptyElement()) GroupCounter_Decrease(counter_switch, GroupName_Switch);
  1353. }
  1354. else if(XML_CheckNode_NameEqual("DirectionalLight"))
  1355. {
  1356. ParseNode_Lighting_DirectionalLight();
  1357. }
  1358. else if(XML_CheckNode_NameEqual("PointLight"))
  1359. {
  1360. ParseNode_Lighting_PointLight();
  1361. }
  1362. else if(XML_CheckNode_NameEqual("SpotLight"))
  1363. {
  1364. ParseNode_Lighting_SpotLight();
  1365. }
  1366. else if(XML_CheckNode_NameEqual("Inline"))
  1367. {
  1368. ParseNode_Networking_Inline();
  1369. }
  1370. else if(!ParseHelper_CheckRead_X3DMetadataObject())
  1371. {
  1372. XML_CheckNode_SkipUnsupported("Scene");
  1373. }
  1374. }// if(mReader->getNodeType() == irr::io::EXN_ELEMENT)
  1375. else if(mReader->getNodeType() == irr::io::EXN_ELEMENT_END)
  1376. {
  1377. if(XML_CheckNode_NameEqual("Scene"))
  1378. {
  1379. close_found = true;
  1380. break;
  1381. }
  1382. else if(XML_CheckNode_NameEqual(GroupName_Group))
  1383. {
  1384. GroupCounter_Decrease(counter_group, GroupName_Group);
  1385. ParseNode_Grouping_GroupEnd();
  1386. }
  1387. else if(XML_CheckNode_NameEqual(GroupName_StaticGroup))
  1388. {
  1389. GroupCounter_Decrease(counter_group, GroupName_StaticGroup);
  1390. ParseNode_Grouping_StaticGroupEnd();
  1391. }
  1392. else if(XML_CheckNode_NameEqual(GroupName_Transform))
  1393. {
  1394. GroupCounter_Decrease(counter_transform, GroupName_Transform);
  1395. ParseNode_Grouping_TransformEnd();
  1396. }
  1397. else if(XML_CheckNode_NameEqual(GroupName_Switch))
  1398. {
  1399. GroupCounter_Decrease(counter_switch, GroupName_Switch);
  1400. ParseNode_Grouping_SwitchEnd();
  1401. }
  1402. }// if(mReader->getNodeType() == irr::io::EXN_ELEMENT) else
  1403. }// while(mReader->read())
  1404. ParseHelper_Node_Exit();
  1405. if(counter_group) Throw_TagCountIncorrect("Group");
  1406. if(counter_transform) Throw_TagCountIncorrect("Transform");
  1407. if(counter_switch) Throw_TagCountIncorrect("Switch");
  1408. if(!close_found) Throw_CloseNotFound("Scene");
  1409. }
  1410. /*********************************************************************************************************************************************/
  1411. /******************************************************** Functions: BaseImporter set ********************************************************/
  1412. /*********************************************************************************************************************************************/
  1413. bool X3DImporter::CanRead(const std::string& pFile, IOSystem* pIOHandler, bool pCheckSig) const
  1414. {
  1415. const std::string extension = GetExtension(pFile);
  1416. if((extension == "x3d") || (extension == "x3db")) return true;
  1417. if(!extension.length() || pCheckSig)
  1418. {
  1419. const char* tokens[] = { "DOCTYPE X3D PUBLIC", "http://www.web3d.org/specifications/x3d" };
  1420. return SearchFileHeaderForToken(pIOHandler, pFile, tokens, 2);
  1421. }
  1422. return false;
  1423. }
  1424. void X3DImporter::GetExtensionList(std::set<std::string>& pExtensionList)
  1425. {
  1426. pExtensionList.insert("x3d");
  1427. pExtensionList.insert("x3db");
  1428. }
  1429. const aiImporterDesc* X3DImporter::GetInfo () const
  1430. {
  1431. return &Description;
  1432. }
  1433. void X3DImporter::InternReadFile(const std::string& pFile, aiScene* pScene, IOSystem* pIOHandler)
  1434. {
  1435. mpIOHandler = pIOHandler;
  1436. Clear();// delete old graph.
  1437. std::string::size_type slashPos = pFile.find_last_of("\\/");
  1438. pIOHandler->PushDirectory(slashPos == std::string::npos ? std::string() : pFile.substr(0, slashPos + 1));
  1439. ParseFile(pFile, pIOHandler);
  1440. pIOHandler->PopDirectory();
  1441. //
  1442. // Assimp use static arrays of objects for fast speed of rendering. That's good, but need some additional operations/
  1443. // We know that geometry objects(meshes) are stored in <Shape>, also in <Shape>-><Appearance> materials(in Assimp logical view)
  1444. // are stored. So at first we need to count how meshes and materials are stored in scene graph.
  1445. //
  1446. // at first creating root node for aiScene.
  1447. pScene->mRootNode = new aiNode;
  1448. pScene->mRootNode->mParent = nullptr;
  1449. pScene->mFlags |= AI_SCENE_FLAGS_ALLOW_SHARED;
  1450. //search for root node element
  1451. NodeElement_Cur = NodeElement_List.front();
  1452. while(NodeElement_Cur->Parent != nullptr) NodeElement_Cur = NodeElement_Cur->Parent;
  1453. {// fill aiScene with objects.
  1454. std::list<aiMesh*> mesh_list;
  1455. std::list<aiMaterial*> mat_list;
  1456. std::list<aiLight*> light_list;
  1457. // create nodes tree
  1458. Postprocess_BuildNode(*NodeElement_Cur, *pScene->mRootNode, mesh_list, mat_list, light_list);
  1459. // copy needed data to scene
  1460. if(!mesh_list.empty())
  1461. {
  1462. std::list<aiMesh*>::const_iterator it = mesh_list.begin();
  1463. pScene->mNumMeshes = static_cast<unsigned int>(mesh_list.size());
  1464. pScene->mMeshes = new aiMesh*[pScene->mNumMeshes];
  1465. for(size_t i = 0; i < pScene->mNumMeshes; i++) pScene->mMeshes[i] = *it++;
  1466. }
  1467. if(!mat_list.empty())
  1468. {
  1469. std::list<aiMaterial*>::const_iterator it = mat_list.begin();
  1470. pScene->mNumMaterials = static_cast<unsigned int>(mat_list.size());
  1471. pScene->mMaterials = new aiMaterial*[pScene->mNumMaterials];
  1472. for(size_t i = 0; i < pScene->mNumMaterials; i++) pScene->mMaterials[i] = *it++;
  1473. }
  1474. if(!light_list.empty())
  1475. {
  1476. std::list<aiLight*>::const_iterator it = light_list.begin();
  1477. pScene->mNumLights = static_cast<unsigned int>(light_list.size());
  1478. pScene->mLights = new aiLight*[pScene->mNumLights];
  1479. for(size_t i = 0; i < pScene->mNumLights; i++) pScene->mLights[i] = *it++;
  1480. }
  1481. }// END: fill aiScene with objects.
  1482. ///TODO: IME optimize tree
  1483. }
  1484. }// namespace Assimp
  1485. #endif // !ASSIMP_BUILD_NO_X3D_IMPORTER