X3DImporter_Group.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  1. /*
  2. Open Asset Import Library (assimp)
  3. ----------------------------------------------------------------------
  4. Copyright (c) 2006-2019, assimp team
  5. All rights reserved.
  6. Redistribution and use of this software in source and binary forms,
  7. with or without modification, are permitted provided that the
  8. following conditions are met:
  9. * Redistributions of source code must retain the above
  10. copyright notice, this list of conditions and the
  11. following disclaimer.
  12. * Redistributions in binary form must reproduce the above
  13. copyright notice, this list of conditions and the
  14. following disclaimer in the documentation and/or other
  15. materials provided with the distribution.
  16. * Neither the name of the assimp team, nor the names of its
  17. contributors may be used to endorse or promote products
  18. derived from this software without specific prior
  19. written permission of the assimp team.
  20. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  21. "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  22. LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  23. A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  24. OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  25. SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  26. LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  27. DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  28. THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  29. (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  30. OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  31. ----------------------------------------------------------------------
  32. */
  33. /// \file X3DImporter_Group.cpp
  34. /// \brief Parsing data from nodes of "Grouping" 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_Macro.hpp"
  40. #include "X3DXmlHelper.h"
  41. namespace Assimp {
  42. // <Group
  43. // DEF="" ID
  44. // USE="" IDREF
  45. // bboxCenter="0 0 0" SFVec3f [initializeOnly]
  46. // bboxSize="-1 -1 -1" SFVec3f [initializeOnly]
  47. // >
  48. // <\!-- ChildContentModel -->
  49. // ChildContentModel is the child-node content model corresponding to X3DChildNode, combining all profiles. ChildContentModel can contain most nodes,
  50. // other Grouping nodes, Prototype declarations and ProtoInstances in any order and any combination. When the assigned profile is less than Full, the
  51. // precise palette of legal nodes that are available depends on assigned profile and components.
  52. // A ProtoInstance node (with the proper node type) can be substituted for any node in this content model.
  53. // </Group>
  54. // A Group node contains children nodes without introducing a new transformation. It is equivalent to a Transform node containing an identity transform.
  55. void X3DImporter::startReadGroup(XmlNode &node) {
  56. std::string def, use;
  57. MACRO_ATTRREAD_CHECKUSEDEF_RET(node, def, use);
  58. // if "USE" defined then find already defined element.
  59. if (!use.empty()) {
  60. X3DNodeElementBase *ne(nullptr);
  61. ne = MACRO_USE_CHECKANDAPPLY(node, def, use, ENET_Group, ne);
  62. } else {
  63. ParseHelper_Group_Begin(); // create new grouping element and go deeper if node has children.
  64. // at this place new group mode created and made current, so we can name it.
  65. if (!def.empty()) mNodeElementCur->ID = def;
  66. // in grouping set of nodes check X3DMetadataObject is not needed, because it is done in <Scene> parser function.
  67. // for empty element exit from node in that place
  68. if (isNodeEmpty(node)) ParseHelper_Node_Exit();
  69. } // if(!use.empty()) else
  70. }
  71. void X3DImporter::endReadGroup() {
  72. ParseHelper_Node_Exit(); // go up in scene graph
  73. }
  74. // <StaticGroup
  75. // DEF="" ID
  76. // USE="" IDREF
  77. // bboxCenter="0 0 0" SFVec3f [initializeOnly]
  78. // bboxSize="-1 -1 -1" SFVec3f [initializeOnly]
  79. // >
  80. // <\!-- ChildContentModel -->
  81. // ChildContentModel is the child-node content model corresponding to X3DChildNode, combining all profiles. ChildContentModel can contain most nodes,
  82. // other Grouping nodes, Prototype declarations and ProtoInstances in any order and any combination. When the assigned profile is less than Full, the
  83. // precise palette of legal nodes that are available depends on assigned profile and components.
  84. // A ProtoInstance node (with the proper node type) can be substituted for any node in this content model.
  85. // </StaticGroup>
  86. // The StaticGroup node contains children nodes which cannot be modified. StaticGroup children are guaranteed to not change, send events, receive events or
  87. // contain any USE references outside the StaticGroup.
  88. void X3DImporter::startReadStaticGroup(XmlNode &node) {
  89. std::string def, use;
  90. MACRO_ATTRREAD_CHECKUSEDEF_RET(node, def, use);
  91. // if "USE" defined then find already defined element.
  92. if (!use.empty()) {
  93. X3DNodeElementBase *ne(nullptr);
  94. ne = MACRO_USE_CHECKANDAPPLY(node, def, use, ENET_Group, ne);
  95. } else {
  96. ParseHelper_Group_Begin(true); // create new grouping element and go deeper if node has children.
  97. // at this place new group mode created and made current, so we can name it.
  98. if (!def.empty()) mNodeElementCur->ID = def;
  99. // in grouping set of nodes check X3DMetadataObject is not needed, because it is done in <Scene> parser function.
  100. // for empty element exit from node in that place
  101. if (isNodeEmpty(node)) ParseHelper_Node_Exit();
  102. } // if(!use.empty()) else
  103. }
  104. void X3DImporter::endReadStaticGroup() {
  105. ParseHelper_Node_Exit(); // go up in scene graph
  106. }
  107. // <Switch
  108. // DEF="" ID
  109. // USE="" IDREF
  110. // bboxCenter="0 0 0" SFVec3f [initializeOnly]
  111. // bboxSize="-1 -1 -1" SFVec3f [initializeOnly]
  112. // whichChoice="-1" SFInt32 [inputOutput]
  113. // >
  114. // <\!-- ChildContentModel -->
  115. // ChildContentModel is the child-node content model corresponding to X3DChildNode, combining all profiles. ChildContentModel can contain most nodes,
  116. // other Grouping nodes, Prototype declarations and ProtoInstances in any order and any combination. When the assigned profile is less than Full, the
  117. // precise palette of legal nodes that are available depends on assigned profile and components.
  118. // A ProtoInstance node (with the proper node type) can be substituted for any node in this content model.
  119. // </Switch>
  120. // The Switch grouping node traverses zero or one of the nodes specified in the children field. The whichChoice field specifies the index of the child
  121. // to traverse, with the first child having index 0. If whichChoice is less than zero or greater than the number of nodes in the children field, nothing
  122. // is chosen.
  123. void X3DImporter::startReadSwitch(XmlNode &node) {
  124. std::string def, use;
  125. int32_t whichChoice = -1;
  126. MACRO_ATTRREAD_CHECKUSEDEF_RET(node, def, use);
  127. XmlParser::getIntAttribute(node, "whichChoice", whichChoice);
  128. // if "USE" defined then find already defined element.
  129. if (!use.empty()) {
  130. X3DNodeElementBase *ne(nullptr);
  131. ne = MACRO_USE_CHECKANDAPPLY(node, def, use, ENET_Group, ne);
  132. } else {
  133. ParseHelper_Group_Begin(); // create new grouping element and go deeper if node has children.
  134. // at this place new group mode created and made current, so we can name it.
  135. if (!def.empty()) mNodeElementCur->ID = def;
  136. // also set values specific to this type of group
  137. ((X3DNodeElementGroup *)mNodeElementCur)->UseChoice = true;
  138. ((X3DNodeElementGroup *)mNodeElementCur)->Choice = whichChoice;
  139. // in grouping set of nodes check X3DMetadataObject is not needed, because it is done in <Scene> parser function.
  140. // for empty element exit from node in that place
  141. if (isNodeEmpty(node)) ParseHelper_Node_Exit();
  142. } // if(!use.empty()) else
  143. }
  144. void X3DImporter::endReadSwitch() {
  145. // just exit from node. Defined choice will be accepted at postprocessing stage.
  146. ParseHelper_Node_Exit(); // go up in scene graph
  147. }
  148. // <Transform
  149. // DEF="" ID
  150. // USE="" IDREF
  151. // bboxCenter="0 0 0" SFVec3f [initializeOnly]
  152. // bboxSize="-1 -1 -1" SFVec3f [initializeOnly]
  153. // center="0 0 0" SFVec3f [inputOutput]
  154. // rotation="0 0 1 0" SFRotation [inputOutput]
  155. // scale="1 1 1" SFVec3f [inputOutput]
  156. // scaleOrientation="0 0 1 0" SFRotation [inputOutput]
  157. // translation="0 0 0" SFVec3f [inputOutput]
  158. // >
  159. // <\!-- ChildContentModel -->
  160. // ChildContentModel is the child-node content model corresponding to X3DChildNode, combining all profiles. ChildContentModel can contain most nodes,
  161. // other Grouping nodes, Prototype declarations and ProtoInstances in any order and any combination. When the assigned profile is less than Full, the
  162. // precise palette of legal nodes that are available depends on assigned profile and components.
  163. // A ProtoInstance node (with the proper node type) can be substituted for any node in this content model.
  164. // </Transform>
  165. // The Transform node is a grouping node that defines a coordinate system for its children that is relative to the coordinate systems of its ancestors.
  166. // Given a 3-dimensional point P and Transform node, P is transformed into point P' in its parent's coordinate system by a series of intermediate
  167. // transformations. In matrix transformation notation, where C (center), SR (scaleOrientation), T (translation), R (rotation), and S (scale) are the
  168. // equivalent transformation matrices,
  169. // P' = T * C * R * SR * S * -SR * -C * P
  170. void X3DImporter::startReadTransform(XmlNode &node) {
  171. aiVector3D center(0, 0, 0);
  172. float rotation[4] = { 0, 0, 1, 0 };
  173. aiVector3D scale(1, 1, 1); // A value of zero indicates that any child geometry shall not be displayed
  174. float scale_orientation[4] = { 0, 0, 1, 0 };
  175. aiVector3D translation(0, 0, 0);
  176. aiMatrix4x4 matr, tmatr;
  177. std::string use, def;
  178. MACRO_ATTRREAD_CHECKUSEDEF_RET(node, def, use);
  179. X3DXmlHelper::getVector3DAttribute(node, "center", center);
  180. X3DXmlHelper::getVector3DAttribute(node, "scale", scale);
  181. X3DXmlHelper::getVector3DAttribute(node, "translation", translation);
  182. std::vector<float> tvec;
  183. if (X3DXmlHelper::getFloatArrayAttribute(node, "rotation", tvec)) {
  184. if (tvec.size() != 4) throw DeadlyImportError("<Transform>: rotation vector must have 4 elements.");
  185. memcpy(rotation, tvec.data(), sizeof(rotation));
  186. tvec.clear();
  187. }
  188. if (X3DXmlHelper::getFloatArrayAttribute(node, "scaleOrientation", tvec)) {
  189. if (tvec.size() != 4) throw DeadlyImportError("<Transform>: scaleOrientation vector must have 4 elements.");
  190. memcpy(scale_orientation, tvec.data(), sizeof(scale_orientation));
  191. tvec.clear();
  192. }
  193. // if "USE" defined then find already defined element.
  194. if (!use.empty()) {
  195. X3DNodeElementBase *ne(nullptr);
  196. bool newgroup = (nullptr == mNodeElementCur);
  197. if(newgroup)
  198. ParseHelper_Group_Begin();
  199. ne = MACRO_USE_CHECKANDAPPLY(node, def, use, ENET_Group, ne);
  200. if (newgroup && isNodeEmpty(node)) {
  201. ParseHelper_Node_Exit();
  202. }
  203. } else {
  204. ParseHelper_Group_Begin(); // create new grouping element and go deeper if node has children.
  205. // at this place new group mode created and made current, so we can name it.
  206. if (!def.empty()) {
  207. mNodeElementCur->ID = def;
  208. }
  209. //
  210. // also set values specific to this type of group
  211. //
  212. // calculate transformation matrix
  213. aiMatrix4x4::Translation(translation, matr); // T
  214. aiMatrix4x4::Translation(center, tmatr); // C
  215. matr *= tmatr;
  216. aiMatrix4x4::Rotation(rotation[3], aiVector3D(rotation[0], rotation[1], rotation[2]), tmatr); // R
  217. matr *= tmatr;
  218. aiMatrix4x4::Rotation(scale_orientation[3], aiVector3D(scale_orientation[0], scale_orientation[1], scale_orientation[2]), tmatr); // SR
  219. matr *= tmatr;
  220. aiMatrix4x4::Scaling(scale, tmatr); // S
  221. matr *= tmatr;
  222. aiMatrix4x4::Rotation(-scale_orientation[3], aiVector3D(scale_orientation[0], scale_orientation[1], scale_orientation[2]), tmatr); // -SR
  223. matr *= tmatr;
  224. aiMatrix4x4::Translation(-center, tmatr); // -C
  225. matr *= tmatr;
  226. // and assign it
  227. ((X3DNodeElementGroup *)mNodeElementCur)->Transformation = matr;
  228. // in grouping set of nodes check X3DMetadataObject is not needed, because it is done in <Scene> parser function.
  229. // for empty element exit from node in that place
  230. if (isNodeEmpty(node)) {
  231. ParseHelper_Node_Exit();
  232. }
  233. } // if(!use.empty()) else
  234. }
  235. void X3DImporter::endReadTransform() {
  236. ParseHelper_Node_Exit(); // go up in scene graph
  237. }
  238. } // namespace Assimp
  239. #endif // !ASSIMP_BUILD_NO_X3D_IMPORTER