X3DImporter_Geometry2D.cpp 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520
  1. /*
  2. Open Asset Import Library (assimp)
  3. ----------------------------------------------------------------------
  4. Copyright (c) 2006-2016, 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_Geometry2D.cpp
  34. /// \brief Parsing data from nodes of "Geometry2D" set of X3D.
  35. /// \date 2015-2016
  36. /// \author [email protected]
  37. #ifndef ASSIMP_BUILD_NO_X3D_IMPORTER
  38. #include "X3DImporter.hpp"
  39. #include "X3DImporter_Node.hpp"
  40. #include "X3DImporter_Macro.hpp"
  41. namespace Assimp
  42. {
  43. // <Arc2D
  44. // DEF="" ID
  45. // USE="" IDREF
  46. // endAngle="1.570796" SFFloat [initializeOnly]
  47. // radius="1" SFFloat [initializeOnly]
  48. // startAngle="0" SFFloat [initializeOnly]
  49. // />
  50. // The Arc2D node specifies a linear circular arc whose center is at (0,0) and whose angles are measured starting at the positive x-axis and sweeping
  51. // towards the positive y-axis. The radius field specifies the radius of the circle of which the arc is a portion. The arc extends from the startAngle
  52. // counterclockwise to the endAngle. The values of startAngle and endAngle shall be in the range [-2pi, 2pi] radians (or the equivalent if a different
  53. // angle base unit has been specified). If startAngle and endAngle have the same value, a circle is specified.
  54. void X3DImporter::ParseNode_Geometry2D_Arc2D()
  55. {
  56. std::string def, use;
  57. float endAngle = AI_MATH_HALF_PI_F;
  58. float radius = 1;
  59. float startAngle = 0;
  60. CX3DImporter_NodeElement* ne( nullptr );
  61. MACRO_ATTRREAD_LOOPBEG;
  62. MACRO_ATTRREAD_CHECKUSEDEF_RET(def, use);
  63. MACRO_ATTRREAD_CHECK_RET("endAngle", endAngle, XML_ReadNode_GetAttrVal_AsFloat);
  64. MACRO_ATTRREAD_CHECK_RET("radius", radius, XML_ReadNode_GetAttrVal_AsFloat);
  65. MACRO_ATTRREAD_CHECK_RET("startAngle", startAngle, XML_ReadNode_GetAttrVal_AsFloat);
  66. MACRO_ATTRREAD_LOOPEND;
  67. // if "USE" defined then find already defined element.
  68. if(!use.empty())
  69. {
  70. MACRO_USE_CHECKANDAPPLY(def, use, ENET_Arc2D, ne);
  71. }
  72. else
  73. {
  74. // create and if needed - define new geometry object.
  75. ne = new CX3DImporter_NodeElement_Geometry2D(CX3DImporter_NodeElement::ENET_Arc2D, NodeElement_Cur);
  76. if(!def.empty()) ne->ID = def;
  77. // create point list of geometry object and convert it to line set.
  78. std::list<aiVector3D> tlist;
  79. GeometryHelper_Make_Arc2D(startAngle, endAngle, radius, 10, tlist);///TODO: IME - AI_CONFIG for NumSeg
  80. GeometryHelper_Extend_PointToLine(tlist, ((CX3DImporter_NodeElement_Geometry2D*)ne)->Vertices);
  81. ((CX3DImporter_NodeElement_Geometry2D*)ne)->NumIndices = 2;
  82. // check for X3DMetadataObject childs.
  83. if(!mReader->isEmptyElement())
  84. ParseNode_Metadata(ne, "Arc2D");
  85. else
  86. NodeElement_Cur->Child.push_back(ne);// add made object as child to current element
  87. NodeElement_List.push_back(ne);// add element to node element list because its a new object in graph
  88. }// if(!use.empty()) else
  89. }
  90. // <ArcClose2D
  91. // DEF="" ID
  92. // USE="" IDREF
  93. // closureType="PIE" SFString [initializeOnly], {"PIE", "CHORD"}
  94. // endAngle="1.570796" SFFloat [initializeOnly]
  95. // radius="1" SFFloat [initializeOnly]
  96. // solid="false" SFBool [initializeOnly]
  97. // startAngle="0" SFFloat [initializeOnly]
  98. // />
  99. // The ArcClose node specifies a portion of a circle whose center is at (0,0) and whose angles are measured starting at the positive x-axis and sweeping
  100. // towards the positive y-axis. The end points of the arc specified are connected as defined by the closureType field. The radius field specifies the radius
  101. // of the circle of which the arc is a portion. The arc extends from the startAngle counterclockwise to the endAngle. The value of radius shall be greater
  102. // than zero. The values of startAngle and endAngle shall be in the range [-2pi, 2pi] radians (or the equivalent if a different default angle base unit has
  103. // been specified). If startAngle and endAngle have the same value, a circle is specified and closureType is ignored. If the absolute difference between
  104. // startAngle and endAngle is greater than or equal to 2pi, a complete circle is produced with no chord or radial line(s) drawn from the center.
  105. // A closureType of "PIE" connects the end point to the start point by defining two straight line segments first from the end point to the center and then
  106. // the center to the start point. A closureType of "CHORD" connects the end point to the start point by defining a straight line segment from the end point
  107. // to the start point. Textures are applied individually to each face of the ArcClose2D. On the front (+Z) and back (-Z) faces of the ArcClose2D, when
  108. // viewed from the +Z-axis, the texture is mapped onto each face with the same orientation as if the image were displayed normally in 2D.
  109. void X3DImporter::ParseNode_Geometry2D_ArcClose2D()
  110. {
  111. std::string def, use;
  112. std::string closureType("PIE");
  113. float endAngle = AI_MATH_HALF_PI_F;
  114. float radius = 1;
  115. bool solid = false;
  116. float startAngle = 0;
  117. CX3DImporter_NodeElement* ne( nullptr );
  118. MACRO_ATTRREAD_LOOPBEG;
  119. MACRO_ATTRREAD_CHECKUSEDEF_RET(def, use);
  120. MACRO_ATTRREAD_CHECK_RET("closureType", closureType, mReader->getAttributeValue);
  121. MACRO_ATTRREAD_CHECK_RET("endAngle", endAngle, XML_ReadNode_GetAttrVal_AsFloat);
  122. MACRO_ATTRREAD_CHECK_RET("radius", radius, XML_ReadNode_GetAttrVal_AsFloat);
  123. MACRO_ATTRREAD_CHECK_RET("solid", solid, XML_ReadNode_GetAttrVal_AsBool);
  124. MACRO_ATTRREAD_CHECK_RET("startAngle", startAngle, XML_ReadNode_GetAttrVal_AsFloat);
  125. MACRO_ATTRREAD_LOOPEND;
  126. // if "USE" defined then find already defined element.
  127. if(!use.empty())
  128. {
  129. MACRO_USE_CHECKANDAPPLY(def, use, ENET_ArcClose2D, ne);
  130. }
  131. else
  132. {
  133. // create and if needed - define new geometry object.
  134. ne = new CX3DImporter_NodeElement_Geometry2D(CX3DImporter_NodeElement::ENET_ArcClose2D, NodeElement_Cur);
  135. if(!def.empty()) ne->ID = def;
  136. ((CX3DImporter_NodeElement_Geometry2D*)ne)->Solid = solid;
  137. // create point list of geometry object.
  138. GeometryHelper_Make_Arc2D(startAngle, endAngle, radius, 10, ((CX3DImporter_NodeElement_Geometry2D*)ne)->Vertices);///TODO: IME - AI_CONFIG for NumSeg
  139. // add chord or two radiuses only if not a circle was defined
  140. if(!((std::fabs(endAngle - startAngle) >= AI_MATH_TWO_PI_F) || (endAngle == startAngle)))
  141. {
  142. std::list<aiVector3D>& vlist = ((CX3DImporter_NodeElement_Geometry2D*)ne)->Vertices;// just short alias.
  143. if((closureType == "PIE") || (closureType == "\"PIE\""))
  144. vlist.push_back(aiVector3D(0, 0, 0));// center point - first radial line
  145. else if((closureType != "CHORD") && (closureType != "\"CHORD\""))
  146. Throw_IncorrectAttrValue("closureType");
  147. vlist.push_back(*vlist.begin());// arc first point - chord from first to last point of arc(if CHORD) or second radial line(if PIE).
  148. }
  149. ((CX3DImporter_NodeElement_Geometry2D*)ne)->NumIndices = ((CX3DImporter_NodeElement_Geometry2D*)ne)->Vertices.size();
  150. // check for X3DMetadataObject childs.
  151. if(!mReader->isEmptyElement())
  152. ParseNode_Metadata(ne, "ArcClose2D");
  153. else
  154. NodeElement_Cur->Child.push_back(ne);// add made object as child to current element
  155. NodeElement_List.push_back(ne);// add element to node element list because its a new object in graph
  156. }// if(!use.empty()) else
  157. }
  158. // <Circle2D
  159. // DEF="" ID
  160. // USE="" IDREF
  161. // radius="1" SFFloat [initializeOnly]
  162. // />
  163. void X3DImporter::ParseNode_Geometry2D_Circle2D()
  164. {
  165. std::string def, use;
  166. float radius = 1;
  167. CX3DImporter_NodeElement* ne( nullptr );
  168. MACRO_ATTRREAD_LOOPBEG;
  169. MACRO_ATTRREAD_CHECKUSEDEF_RET(def, use);
  170. MACRO_ATTRREAD_CHECK_RET("radius", radius, XML_ReadNode_GetAttrVal_AsFloat);
  171. MACRO_ATTRREAD_LOOPEND;
  172. // if "USE" defined then find already defined element.
  173. if(!use.empty())
  174. {
  175. MACRO_USE_CHECKANDAPPLY(def, use, ENET_Circle2D, ne);
  176. }
  177. else
  178. {
  179. // create and if needed - define new geometry object.
  180. ne = new CX3DImporter_NodeElement_Geometry2D(CX3DImporter_NodeElement::ENET_Circle2D, NodeElement_Cur);
  181. if(!def.empty()) ne->ID = def;
  182. // create point list of geometry object and convert it to line set.
  183. std::list<aiVector3D> tlist;
  184. GeometryHelper_Make_Arc2D(0, 0, radius, 10, tlist);///TODO: IME - AI_CONFIG for NumSeg
  185. GeometryHelper_Extend_PointToLine(tlist, ((CX3DImporter_NodeElement_Geometry2D*)ne)->Vertices);
  186. ((CX3DImporter_NodeElement_Geometry2D*)ne)->NumIndices = 2;
  187. // check for X3DMetadataObject childs.
  188. if(!mReader->isEmptyElement())
  189. ParseNode_Metadata(ne, "Circle2D");
  190. else
  191. NodeElement_Cur->Child.push_back(ne);// add made object as child to current element
  192. NodeElement_List.push_back(ne);// add element to node element list because its a new object in graph
  193. }// if(!use.empty()) else
  194. }
  195. // <Disk2D
  196. // DEF="" ID
  197. // USE="" IDREF
  198. // innerRadius="0" SFFloat [initializeOnly]
  199. // outerRadius="1" SFFloat [initializeOnly]
  200. // solid="false" SFBool [initializeOnly]
  201. // />
  202. // The Disk2D node specifies a circular disk which is centred at (0, 0) in the local coordinate system. The outerRadius field specifies the radius of the
  203. // outer dimension of the Disk2D. The innerRadius field specifies the inner dimension of the Disk2D. The value of outerRadius shall be greater than zero.
  204. // The value of innerRadius shall be greater than or equal to zero and less than or equal to outerRadius. If innerRadius is zero, the Disk2D is completely
  205. // filled. Otherwise, the area within the innerRadius forms a hole in the Disk2D. If innerRadius is equal to outerRadius, a solid circular line shall
  206. // be drawn using the current line properties. Textures are applied individually to each face of the Disk2D. On the front (+Z) and back (-Z) faces of
  207. // the Disk2D, when viewed from the +Z-axis, the texture is mapped onto each face with the same orientation as if the image were displayed normally in 2D.
  208. void X3DImporter::ParseNode_Geometry2D_Disk2D()
  209. {
  210. std::string def, use;
  211. float innerRadius = 0;
  212. float outerRadius = 1;
  213. bool solid = false;
  214. CX3DImporter_NodeElement* ne( nullptr );
  215. MACRO_ATTRREAD_LOOPBEG;
  216. MACRO_ATTRREAD_CHECKUSEDEF_RET(def, use);
  217. MACRO_ATTRREAD_CHECK_RET("innerRadius", innerRadius, XML_ReadNode_GetAttrVal_AsFloat);
  218. MACRO_ATTRREAD_CHECK_RET("outerRadius", outerRadius, XML_ReadNode_GetAttrVal_AsFloat);
  219. MACRO_ATTRREAD_CHECK_RET("solid", solid, XML_ReadNode_GetAttrVal_AsBool);
  220. MACRO_ATTRREAD_LOOPEND;
  221. // if "USE" defined then find already defined element.
  222. if(!use.empty())
  223. {
  224. MACRO_USE_CHECKANDAPPLY(def, use, ENET_Disk2D, ne);
  225. }
  226. else
  227. {
  228. std::list<aiVector3D> tlist_o, tlist_i;
  229. if(innerRadius > outerRadius) Throw_IncorrectAttrValue("innerRadius");
  230. // create and if needed - define new geometry object.
  231. ne = new CX3DImporter_NodeElement_Geometry2D(CX3DImporter_NodeElement::ENET_Disk2D, NodeElement_Cur);
  232. if(!def.empty()) ne->ID = def;
  233. // create point list of geometry object.
  234. ///TODO: IME - AI_CONFIG for NumSeg
  235. GeometryHelper_Make_Arc2D(0, 0, outerRadius, 10, tlist_o);// outer circle
  236. if(innerRadius == 0.0f)
  237. {// make filled disk
  238. // in tlist_o we already have points of circle. just copy it and sign as polygon.
  239. ((CX3DImporter_NodeElement_Geometry2D*)ne)->Vertices = tlist_o;
  240. ((CX3DImporter_NodeElement_Geometry2D*)ne)->NumIndices = tlist_o.size();
  241. }
  242. else if(innerRadius == outerRadius)
  243. {// make circle
  244. // in tlist_o we already have points of circle. convert it to line set.
  245. GeometryHelper_Extend_PointToLine(tlist_o, ((CX3DImporter_NodeElement_Geometry2D*)ne)->Vertices);
  246. ((CX3DImporter_NodeElement_Geometry2D*)ne)->NumIndices = 2;
  247. }
  248. else
  249. {// make disk
  250. std::list<aiVector3D>& vlist = ((CX3DImporter_NodeElement_Geometry2D*)ne)->Vertices;// just short alias.
  251. GeometryHelper_Make_Arc2D(0, 0, innerRadius, 10, tlist_i);// inner circle
  252. //
  253. // create quad list from two point lists
  254. //
  255. if(tlist_i.size() < 2) throw DeadlyImportError("Disk2D. Not enough points for creating quad list.");// tlist_i and tlist_o has equal size.
  256. // add all quads except last
  257. for(std::list<aiVector3D>::iterator it_i = tlist_i.begin(), it_o = tlist_o.begin(); it_i != tlist_i.end();)
  258. {
  259. // do not forget - CCW direction
  260. vlist.push_back(*it_i++);// 1st point
  261. vlist.push_back(*it_o++);// 2nd point
  262. vlist.push_back(*it_o);// 3rd point
  263. vlist.push_back(*it_i);// 4th point
  264. }
  265. // add last quad
  266. vlist.push_back(*tlist_i.end());// 1st point
  267. vlist.push_back(*tlist_o.end());// 2nd point
  268. vlist.push_back(*tlist_o.begin());// 3rd point
  269. vlist.push_back(*tlist_o.begin());// 4th point
  270. ((CX3DImporter_NodeElement_Geometry2D*)ne)->NumIndices = 4;
  271. }
  272. ((CX3DImporter_NodeElement_Geometry2D*)ne)->Solid = solid;
  273. // check for X3DMetadataObject childs.
  274. if(!mReader->isEmptyElement())
  275. ParseNode_Metadata(ne, "Disk2D");
  276. else
  277. NodeElement_Cur->Child.push_back(ne);// add made object as child to current element
  278. NodeElement_List.push_back(ne);// add element to node element list because its a new object in graph
  279. }// if(!use.empty()) else
  280. }
  281. // <Polyline2D
  282. // DEF="" ID
  283. // USE="" IDREF
  284. // lineSegments="" MFVec2F [intializeOnly]
  285. // />
  286. void X3DImporter::ParseNode_Geometry2D_Polyline2D()
  287. {
  288. std::string def, use;
  289. std::list<aiVector2D> lineSegments;
  290. CX3DImporter_NodeElement* ne( nullptr );
  291. MACRO_ATTRREAD_LOOPBEG;
  292. MACRO_ATTRREAD_CHECKUSEDEF_RET(def, use);
  293. MACRO_ATTRREAD_CHECK_REF("lineSegments", lineSegments, XML_ReadNode_GetAttrVal_AsListVec2f);
  294. MACRO_ATTRREAD_LOOPEND;
  295. // if "USE" defined then find already defined element.
  296. if(!use.empty())
  297. {
  298. MACRO_USE_CHECKANDAPPLY(def, use, ENET_Polyline2D, ne);
  299. }
  300. else
  301. {
  302. // create and if needed - define new geometry object.
  303. ne = new CX3DImporter_NodeElement_Geometry2D(CX3DImporter_NodeElement::ENET_Polyline2D, NodeElement_Cur);
  304. if(!def.empty()) ne->ID = def;
  305. //
  306. // convert read point list of geometry object to line set.
  307. //
  308. std::list<aiVector3D> tlist;
  309. // convert vec2 to vec3
  310. for(std::list<aiVector2D>::iterator it2 = lineSegments.begin(); it2 != lineSegments.end(); it2++) tlist.push_back(aiVector3D(it2->x, it2->y, 0));
  311. // convert point set to line set
  312. GeometryHelper_Extend_PointToLine(tlist, ((CX3DImporter_NodeElement_Geometry2D*)ne)->Vertices);
  313. ((CX3DImporter_NodeElement_Geometry2D*)ne)->NumIndices = 2;
  314. // check for X3DMetadataObject childs.
  315. if(!mReader->isEmptyElement())
  316. ParseNode_Metadata(ne, "Polyline2D");
  317. else
  318. NodeElement_Cur->Child.push_back(ne);// add made object as child to current element
  319. NodeElement_List.push_back(ne);// add element to node element list because its a new object in graph
  320. }// if(!use.empty()) else
  321. }
  322. // <Polypoint2D
  323. // DEF="" ID
  324. // USE="" IDREF
  325. // point="" MFVec2F [inputOutput]
  326. // />
  327. void X3DImporter::ParseNode_Geometry2D_Polypoint2D()
  328. {
  329. std::string def, use;
  330. std::list<aiVector2D> point;
  331. CX3DImporter_NodeElement* ne( nullptr );
  332. MACRO_ATTRREAD_LOOPBEG;
  333. MACRO_ATTRREAD_CHECKUSEDEF_RET(def, use);
  334. MACRO_ATTRREAD_CHECK_REF("point", point, XML_ReadNode_GetAttrVal_AsListVec2f);
  335. MACRO_ATTRREAD_LOOPEND;
  336. // if "USE" defined then find already defined element.
  337. if(!use.empty())
  338. {
  339. MACRO_USE_CHECKANDAPPLY(def, use, ENET_Polypoint2D, ne);
  340. }
  341. else
  342. {
  343. // create and if needed - define new geometry object.
  344. ne = new CX3DImporter_NodeElement_Geometry2D(CX3DImporter_NodeElement::ENET_Polypoint2D, NodeElement_Cur);
  345. if(!def.empty()) ne->ID = def;
  346. // convert vec2 to vec3
  347. for(std::list<aiVector2D>::iterator it2 = point.begin(); it2 != point.end(); it2++)
  348. {
  349. ((CX3DImporter_NodeElement_Geometry2D*)ne)->Vertices.push_back(aiVector3D(it2->x, it2->y, 0));
  350. }
  351. ((CX3DImporter_NodeElement_Geometry2D*)ne)->NumIndices = 1;
  352. // check for X3DMetadataObject childs.
  353. if(!mReader->isEmptyElement())
  354. ParseNode_Metadata(ne, "Polypoint2D");
  355. else
  356. NodeElement_Cur->Child.push_back(ne);// add made object as child to current element
  357. NodeElement_List.push_back(ne);// add element to node element list because its a new object in graph
  358. }// if(!use.empty()) else
  359. }
  360. // <Rectangle2D
  361. // DEF="" ID
  362. // USE="" IDREF
  363. // size="2 2" SFVec2f [initializeOnly]
  364. // solid="false" SFBool [initializeOnly]
  365. // />
  366. void X3DImporter::ParseNode_Geometry2D_Rectangle2D()
  367. {
  368. std::string def, use;
  369. aiVector2D size(2, 2);
  370. bool solid = false;
  371. CX3DImporter_NodeElement* ne( nullptr );
  372. MACRO_ATTRREAD_LOOPBEG;
  373. MACRO_ATTRREAD_CHECKUSEDEF_RET(def, use);
  374. MACRO_ATTRREAD_CHECK_REF("size", size, XML_ReadNode_GetAttrVal_AsVec2f);
  375. MACRO_ATTRREAD_CHECK_RET("solid", solid, XML_ReadNode_GetAttrVal_AsBool);
  376. MACRO_ATTRREAD_LOOPEND;
  377. // if "USE" defined then find already defined element.
  378. if(!use.empty())
  379. {
  380. MACRO_USE_CHECKANDAPPLY(def, use, ENET_Rectangle2D, ne);
  381. }
  382. else
  383. {
  384. // create and if needed - define new geometry object.
  385. ne = new CX3DImporter_NodeElement_Geometry2D(CX3DImporter_NodeElement::ENET_Rectangle2D, NodeElement_Cur);
  386. if(!def.empty()) ne->ID = def;
  387. float x1 = -size.x / 2.0f;
  388. float x2 = size.x / 2.0f;
  389. float y1 = -size.y / 2.0f;
  390. float y2 = size.y / 2.0f;
  391. std::list<aiVector3D>& vlist = ((CX3DImporter_NodeElement_Geometry2D*)ne)->Vertices;// just short alias.
  392. vlist.push_back(aiVector3D(x2, y1, 0));// 1st point
  393. vlist.push_back(aiVector3D(x2, y2, 0));// 2nd point
  394. vlist.push_back(aiVector3D(x1, y2, 0));// 3rd point
  395. vlist.push_back(aiVector3D(x1, y1, 0));// 4th point
  396. ((CX3DImporter_NodeElement_Geometry2D*)ne)->Solid = solid;
  397. ((CX3DImporter_NodeElement_Geometry2D*)ne)->NumIndices = 4;
  398. // check for X3DMetadataObject childs.
  399. if(!mReader->isEmptyElement())
  400. ParseNode_Metadata(ne, "Rectangle2D");
  401. else
  402. NodeElement_Cur->Child.push_back(ne);// add made object as child to current element
  403. NodeElement_List.push_back(ne);// add element to node element list because its a new object in graph
  404. }// if(!use.empty()) else
  405. }
  406. // <TriangleSet2D
  407. // DEF="" ID
  408. // USE="" IDREF
  409. // solid="false" SFBool [initializeOnly]
  410. // vertices="" MFVec2F [inputOutput]
  411. // />
  412. void X3DImporter::ParseNode_Geometry2D_TriangleSet2D()
  413. {
  414. std::string def, use;
  415. bool solid = false;
  416. std::list<aiVector2D> vertices;
  417. CX3DImporter_NodeElement* ne( nullptr );
  418. MACRO_ATTRREAD_LOOPBEG;
  419. MACRO_ATTRREAD_CHECKUSEDEF_RET(def, use);
  420. MACRO_ATTRREAD_CHECK_REF("vertices", vertices, XML_ReadNode_GetAttrVal_AsListVec2f);
  421. MACRO_ATTRREAD_CHECK_RET("solid", solid, XML_ReadNode_GetAttrVal_AsBool);
  422. MACRO_ATTRREAD_LOOPEND;
  423. // if "USE" defined then find already defined element.
  424. if(!use.empty())
  425. {
  426. MACRO_USE_CHECKANDAPPLY(def, use, ENET_TriangleSet2D, ne);
  427. }
  428. else
  429. {
  430. if(vertices.size() % 3) throw DeadlyImportError("TriangleSet2D. Not enough points for defining triangle.");
  431. // create and if needed - define new geometry object.
  432. ne = new CX3DImporter_NodeElement_Geometry2D(CX3DImporter_NodeElement::ENET_TriangleSet2D, NodeElement_Cur);
  433. if(!def.empty()) ne->ID = def;
  434. // convert vec2 to vec3
  435. for(std::list<aiVector2D>::iterator it2 = vertices.begin(); it2 != vertices.end(); it2++)
  436. {
  437. ((CX3DImporter_NodeElement_Geometry2D*)ne)->Vertices.push_back(aiVector3D(it2->x, it2->y, 0));
  438. }
  439. ((CX3DImporter_NodeElement_Geometry2D*)ne)->Solid = solid;
  440. ((CX3DImporter_NodeElement_Geometry2D*)ne)->NumIndices = 3;
  441. // check for X3DMetadataObject childs.
  442. if(!mReader->isEmptyElement())
  443. ParseNode_Metadata(ne, "TriangleSet2D");
  444. else
  445. NodeElement_Cur->Child.push_back(ne);// add made object as child to current element
  446. NodeElement_List.push_back(ne);// add element to node element list because its a new object in graph
  447. }// if(!use.empty()) else
  448. }
  449. }// namespace Assimp
  450. #endif // !ASSIMP_BUILD_NO_X3D_IMPORTER