1 /**************************************************************************\
2  * Copyright (c) Kongsberg Oil & Gas Technologies AS
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions are
7  * met:
8  *
9  * Redistributions of source code must retain the above copyright notice,
10  * this list of conditions and the following disclaimer.
11  *
12  * Redistributions in binary form must reproduce the above copyright
13  * notice, this list of conditions and the following disclaimer in the
14  * documentation and/or other materials provided with the distribution.
15  *
16  * Neither the name of the copyright holder nor the names of its
17  * contributors may be used to endorse or promote products derived from
18  * this software without specific prior written permission.
19  *
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  * HOLDER 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 /*!
34   \class SoRotation SoRotation.h Inventor/nodes/SoRotation.h
35   \brief The SoRotation class specifies a rotation transformation.
36 
37   \ingroup nodes
38 
39   Use nodes of this class type to re-orient geometry data within the
40   scene graph.
41 
42   See SoTransformation class documentation for a short usage example.
43 
44   <b>FILE FORMAT/DEFAULTS:</b>
45   \code
46     Rotation {
47         rotation 0 0 1  0
48     }
49   \endcode
50 
51   \sa SbRotation, SoRotationXYZ
52 */
53 
54 // *************************************************************************
55 
56 #include <Inventor/nodes/SoRotation.h>
57 
58 #include <Inventor/actions/SoGetMatrixAction.h>
59 #include <Inventor/elements/SoModelMatrixElement.h>
60 
61 #include "nodes/SoSubNodeP.h"
62 
63 // *************************************************************************
64 
65 /*!
66   \var SoSFRotation SoRotation::rotation
67 
68   Rotation specification. Defaults to no rotation at all. See the
69   SbRotation documentation for instructions on how to set the value of
70   this field.
71 
72 
73   Note that there is one \e very common mistake that is easy to make
74   when setting the value of a an SoSFRotation field, and that is to
75   inadvertently use the wrong SbRotation constructor. This example
76   should clarify the problem:
77 
78   \code
79   mytransformnode->rotation.setValue(0, 0, 1, 1.5707963f);
80   \endcode
81 
82   The programmer clearly tries to set a PI/2 rotation around the Z
83   axis, but this will fail, as the SbRotation constructor invoked
84   above is the one that takes as arguments the 4 floats of a \e
85   quaternion. What the programmer almost certainly wanted to do was to
86   use the SbRotation constructor that takes a rotation vector and a
87   rotation angle, which is invoked like this:
88 
89   \code
90   mytransformnode->rotation.setValue(SbVec3f(0, 0, 1), 1.5707963f);
91   \endcode
92 
93 
94   Another common problem is to set the rotation value to exactly 0.0,
95   while wanting to store just a rotation \e angle in the field:
96   rotations are internally handled as quaternions, and when converting
97   from an angle and a rotation value to a quaternion representation,
98   the information about the angle "gets lost" if there is no actual
99   rotation.
100 */
101 
102 // *************************************************************************
103 
104 SO_NODE_SOURCE(SoRotation);
105 
106 /*!
107   Constructor.
108 */
SoRotation()109 SoRotation::SoRotation()
110 {
111   SO_NODE_INTERNAL_CONSTRUCTOR(SoRotation);
112 
113   SO_NODE_ADD_FIELD(rotation, (SbRotation(SbVec3f(0.0f, 0.0f, 1.0f), 0.0f)));
114 }
115 
116 /*!
117   Destructor.
118 */
~SoRotation()119 SoRotation::~SoRotation()
120 {
121 }
122 
123 // Doc from superclass.
124 void
initClass(void)125 SoRotation::initClass(void)
126 {
127   SO_NODE_INTERNAL_INIT_CLASS(SoRotation, SO_FROM_INVENTOR_1|SoNode::VRML1);
128 }
129 
130 // Doc from superclass.
131 void
doAction(SoAction * action)132 SoRotation::doAction(SoAction * action)
133 {
134   if (!this->rotation.isIgnored()) {
135     SoModelMatrixElement::rotateBy(action->getState(), this,
136                                    this->rotation.getValue());
137   }
138 }
139 
140 // Doc from superclass.
141 void
callback(SoCallbackAction * action)142 SoRotation::callback(SoCallbackAction * action)
143 {
144   SoRotation::doAction((SoAction *)action);
145 }
146 
147 // Doc from superclass.
148 void
GLRender(SoGLRenderAction * action)149 SoRotation::GLRender(SoGLRenderAction * action)
150 {
151   SoRotation::doAction((SoAction *)action);
152 }
153 
154 // Doc from superclass.
155 void
getBoundingBox(SoGetBoundingBoxAction * action)156 SoRotation::getBoundingBox(SoGetBoundingBoxAction * action)
157 {
158   SoRotation::doAction((SoAction *)action);
159 }
160 
161 // Doc from superclass.
162 void
getMatrix(SoGetMatrixAction * action)163 SoRotation::getMatrix(SoGetMatrixAction * action)
164 {
165   SbMatrix m;
166 
167   SbRotation r = this->rotation.getValue();
168   r.getValue(m);
169   action->getMatrix().multLeft(m);
170 
171   SbRotation ri = r.inverse();
172   ri.getValue(m);
173   action->getInverse().multRight(m);
174 }
175 
176 // Doc from superclass.
177 void
pick(SoPickAction * action)178 SoRotation::pick(SoPickAction * action)
179 {
180   SoRotation::doAction((SoAction *)action);
181 }
182 
183 // Doc from superclass. Overrides the traversal method in this class for
184 // the SoGetPrimitiveCountAction because the number of primitives can
185 // be different depending on scene location (and thereby distance to
186 // camera) if there are e.g. SoLOD nodes in the scene.
187 void
getPrimitiveCount(SoGetPrimitiveCountAction * action)188 SoRotation::getPrimitiveCount(SoGetPrimitiveCountAction *action)
189 {
190   SoRotation::doAction((SoAction *)action);
191 }
192