X3DImporter_Group.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316
  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_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. namespace Assimp
  41. {
  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::ParseNode_Grouping_Group()
  56. {
  57. std::string def, use;
  58. MACRO_ATTRREAD_LOOPBEG;
  59. MACRO_ATTRREAD_CHECKUSEDEF_RET(def, use);
  60. MACRO_ATTRREAD_LOOPEND;
  61. // if "USE" defined then find already defined element.
  62. if(!use.empty())
  63. {
  64. CX3DImporter_NodeElement* ne;
  65. MACRO_USE_CHECKANDAPPLY(def, use, ENET_Group, ne);
  66. }
  67. else
  68. {
  69. ParseHelper_Group_Begin();// create new grouping element and go deeper if node has children.
  70. // at this place new group mode created and made current, so we can name it.
  71. if(!def.empty()) NodeElement_Cur->ID = def;
  72. // in grouping set of nodes check X3DMetadataObject is not needed, because it is done in <Scene> parser function.
  73. // for empty element exit from node in that place
  74. if(mReader->isEmptyElement()) ParseHelper_Node_Exit();
  75. }// if(!use.empty()) else
  76. }
  77. void X3DImporter::ParseNode_Grouping_GroupEnd()
  78. {
  79. ParseHelper_Node_Exit();// go up in scene graph
  80. }
  81. // <StaticGroup
  82. // DEF="" ID
  83. // USE="" IDREF
  84. // bboxCenter="0 0 0" SFVec3f [initializeOnly]
  85. // bboxSize="-1 -1 -1" SFVec3f [initializeOnly]
  86. // >
  87. // <!-- ChildContentModel -->
  88. // ChildContentModel is the child-node content model corresponding to X3DChildNode, combining all profiles. ChildContentModel can contain most nodes,
  89. // other Grouping nodes, Prototype declarations and ProtoInstances in any order and any combination. When the assigned profile is less than Full, the
  90. // precise palette of legal nodes that are available depends on assigned profile and components.
  91. // A ProtoInstance node (with the proper node type) can be substituted for any node in this content model.
  92. // </StaticGroup>
  93. // The StaticGroup node contains children nodes which cannot be modified. StaticGroup children are guaranteed to not change, send events, receive events or
  94. // contain any USE references outside the StaticGroup.
  95. void X3DImporter::ParseNode_Grouping_StaticGroup()
  96. {
  97. std::string def, use;
  98. MACRO_ATTRREAD_LOOPBEG;
  99. MACRO_ATTRREAD_CHECKUSEDEF_RET(def, use);
  100. MACRO_ATTRREAD_LOOPEND;
  101. // if "USE" defined then find already defined element.
  102. if(!use.empty())
  103. {
  104. CX3DImporter_NodeElement* ne;
  105. MACRO_USE_CHECKANDAPPLY(def, use, ENET_Group, ne);
  106. }
  107. else
  108. {
  109. ParseHelper_Group_Begin(true);// create new grouping element and go deeper if node has children.
  110. // at this place new group mode created and made current, so we can name it.
  111. if(!def.empty()) NodeElement_Cur->ID = def;
  112. // in grouping set of nodes check X3DMetadataObject is not needed, because it is done in <Scene> parser function.
  113. // for empty element exit from node in that place
  114. if(mReader->isEmptyElement()) ParseHelper_Node_Exit();
  115. }// if(!use.empty()) else
  116. }
  117. void X3DImporter::ParseNode_Grouping_StaticGroupEnd()
  118. {
  119. ParseHelper_Node_Exit();// go up in scene graph
  120. }
  121. // <Switch
  122. // DEF="" ID
  123. // USE="" IDREF
  124. // bboxCenter="0 0 0" SFVec3f [initializeOnly]
  125. // bboxSize="-1 -1 -1" SFVec3f [initializeOnly]
  126. // whichChoice="-1" SFInt32 [inputOutput]
  127. // >
  128. // <!-- ChildContentModel -->
  129. // ChildContentModel is the child-node content model corresponding to X3DChildNode, combining all profiles. ChildContentModel can contain most nodes,
  130. // other Grouping nodes, Prototype declarations and ProtoInstances in any order and any combination. When the assigned profile is less than Full, the
  131. // precise palette of legal nodes that are available depends on assigned profile and components.
  132. // A ProtoInstance node (with the proper node type) can be substituted for any node in this content model.
  133. // </Switch>
  134. // 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
  135. // 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
  136. // is chosen.
  137. void X3DImporter::ParseNode_Grouping_Switch()
  138. {
  139. std::string def, use;
  140. int32_t whichChoice = -1;
  141. MACRO_ATTRREAD_LOOPBEG;
  142. MACRO_ATTRREAD_CHECKUSEDEF_RET(def, use);
  143. MACRO_ATTRREAD_CHECK_RET("whichChoice", whichChoice, XML_ReadNode_GetAttrVal_AsI32);
  144. MACRO_ATTRREAD_LOOPEND;
  145. // if "USE" defined then find already defined element.
  146. if(!use.empty())
  147. {
  148. CX3DImporter_NodeElement* ne;
  149. MACRO_USE_CHECKANDAPPLY(def, use, ENET_Group, ne);
  150. }
  151. else
  152. {
  153. ParseHelper_Group_Begin();// create new grouping element and go deeper if node has children.
  154. // at this place new group mode created and made current, so we can name it.
  155. if(!def.empty()) NodeElement_Cur->ID = def;
  156. // also set values specific to this type of group
  157. ((CX3DImporter_NodeElement_Group*)NodeElement_Cur)->UseChoice = true;
  158. ((CX3DImporter_NodeElement_Group*)NodeElement_Cur)->Choice = whichChoice;
  159. // in grouping set of nodes check X3DMetadataObject is not needed, because it is done in <Scene> parser function.
  160. // for empty element exit from node in that place
  161. if(mReader->isEmptyElement()) ParseHelper_Node_Exit();
  162. }// if(!use.empty()) else
  163. }
  164. void X3DImporter::ParseNode_Grouping_SwitchEnd()
  165. {
  166. // just exit from node. Defined choice will be accepted at postprocessing stage.
  167. ParseHelper_Node_Exit();// go up in scene graph
  168. }
  169. // <Transform
  170. // DEF="" ID
  171. // USE="" IDREF
  172. // bboxCenter="0 0 0" SFVec3f [initializeOnly]
  173. // bboxSize="-1 -1 -1" SFVec3f [initializeOnly]
  174. // center="0 0 0" SFVec3f [inputOutput]
  175. // rotation="0 0 1 0" SFRotation [inputOutput]
  176. // scale="1 1 1" SFVec3f [inputOutput]
  177. // scaleOrientation="0 0 1 0" SFRotation [inputOutput]
  178. // translation="0 0 0" SFVec3f [inputOutput]
  179. // >
  180. // <!-- ChildContentModel -->
  181. // ChildContentModel is the child-node content model corresponding to X3DChildNode, combining all profiles. ChildContentModel can contain most nodes,
  182. // other Grouping nodes, Prototype declarations and ProtoInstances in any order and any combination. When the assigned profile is less than Full, the
  183. // precise palette of legal nodes that are available depends on assigned profile and components.
  184. // A ProtoInstance node (with the proper node type) can be substituted for any node in this content model.
  185. // </Transform>
  186. // 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.
  187. // 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
  188. // transformations. In matrix transformation notation, where C (center), SR (scaleOrientation), T (translation), R (rotation), and S (scale) are the
  189. // equivalent transformation matrices,
  190. // P' = T * C * R * SR * S * -SR * -C * P
  191. void X3DImporter::ParseNode_Grouping_Transform()
  192. {
  193. aiVector3D center(0, 0, 0);
  194. float rotation[4] = {0, 0, 1, 0};
  195. aiVector3D scale(1, 1, 1);// A value of zero indicates that any child geometry shall not be displayed
  196. float scale_orientation[4] = {0, 0, 1, 0};
  197. aiVector3D translation(0, 0, 0);
  198. aiMatrix4x4 matr, tmatr;
  199. std::string use, def;
  200. MACRO_ATTRREAD_LOOPBEG;
  201. MACRO_ATTRREAD_CHECKUSEDEF_RET(def, use);
  202. MACRO_ATTRREAD_CHECK_REF("center", center, XML_ReadNode_GetAttrVal_AsVec3f);
  203. MACRO_ATTRREAD_CHECK_REF("scale", scale, XML_ReadNode_GetAttrVal_AsVec3f);
  204. MACRO_ATTRREAD_CHECK_REF("translation", translation, XML_ReadNode_GetAttrVal_AsVec3f);
  205. if(an == "rotation")
  206. {
  207. std::vector<float> tvec;
  208. XML_ReadNode_GetAttrVal_AsArrF(idx, tvec);
  209. if(tvec.size() != 4) throw DeadlyImportError("<Transform>: rotation vector must have 4 elements.");
  210. memcpy(rotation, tvec.data(), sizeof(rotation));
  211. continue;
  212. }
  213. if(an == "scaleOrientation")
  214. {
  215. std::vector<float> tvec;
  216. XML_ReadNode_GetAttrVal_AsArrF(idx, tvec);
  217. if ( tvec.size() != 4 )
  218. {
  219. throw DeadlyImportError( "<Transform>: scaleOrientation vector must have 4 elements." );
  220. }
  221. ::memcpy(scale_orientation, tvec.data(), sizeof(scale_orientation) );
  222. continue;
  223. }
  224. MACRO_ATTRREAD_LOOPEND;
  225. // if "USE" defined then find already defined element.
  226. if(!use.empty())
  227. {
  228. CX3DImporter_NodeElement* ne( nullptr );
  229. MACRO_USE_CHECKANDAPPLY(def, use, ENET_Group, ne);
  230. }
  231. else
  232. {
  233. ParseHelper_Group_Begin();// create new grouping element and go deeper if node has children.
  234. // at this place new group mode created and made current, so we can name it.
  235. if ( !def.empty() )
  236. {
  237. NodeElement_Cur->ID = def;
  238. }
  239. //
  240. // also set values specific to this type of group
  241. //
  242. // calculate transformation matrix
  243. aiMatrix4x4::Translation(translation, matr);// T
  244. aiMatrix4x4::Translation(center, tmatr);// C
  245. matr *= tmatr;
  246. aiMatrix4x4::Rotation(rotation[3], aiVector3D(rotation[0], rotation[1], rotation[2]), tmatr);// R
  247. matr *= tmatr;
  248. aiMatrix4x4::Rotation(scale_orientation[3], aiVector3D(scale_orientation[0], scale_orientation[1], scale_orientation[2]), tmatr);// SR
  249. matr *= tmatr;
  250. aiMatrix4x4::Scaling(scale, tmatr);// S
  251. matr *= tmatr;
  252. aiMatrix4x4::Rotation(-scale_orientation[3], aiVector3D(scale_orientation[0], scale_orientation[1], scale_orientation[2]), tmatr);// -SR
  253. matr *= tmatr;
  254. aiMatrix4x4::Translation(-center, tmatr);// -C
  255. matr *= tmatr;
  256. // and assign it
  257. ((CX3DImporter_NodeElement_Group*)NodeElement_Cur)->Transformation = matr;
  258. // in grouping set of nodes check X3DMetadataObject is not needed, because it is done in <Scene> parser function.
  259. // for empty element exit from node in that place
  260. if ( mReader->isEmptyElement() )
  261. {
  262. ParseHelper_Node_Exit();
  263. }
  264. }// if(!use.empty()) else
  265. }
  266. void X3DImporter::ParseNode_Grouping_TransformEnd()
  267. {
  268. ParseHelper_Node_Exit();// go up in scene graph
  269. }
  270. }// namespace Assimp
  271. #endif // !ASSIMP_BUILD_NO_X3D_IMPORTER