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