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 SoGetMatrixAction SoGetMatrixAction.h Inventor/actions/SoGetMatrixAction.h
35   \brief The SoGetMatrixAction class is an action for accumulating the transformation matrix of a subgraph.
36 
37   \ingroup actions
38 
39   This action makes it easy to calculate and convert to and from the
40   global coordinate system of your scene and local coordinates of
41   parts in a hierarchical model.
42 
43   As opposed to most other action types, the SoGetMatrixAction does
44   not traverse children of the node it is applied to -- just the node
45   itself. When applied to paths, it stops at the last node and does
46   not continue further with the children of the tail node.
47 
48   Typical usage when querying for world space position, orientation
49   and/or scaling would be as follows:
50 
51   \code
52 
53   // First get hold of an SoPath through the scenegraph down to the
54   // node ("mynode") you want to query about its current world space
55   // transformation(s).
56 
57   SoSearchAction * searchaction = new SoSearchAction;
58   searchaction->setNode(mynode);
59   searchaction->apply(myscenegraphroot);
60 
61   SoPath * path = searchaction->getPath();
62   assert(path != NULL);
63 
64   // Then apply the SoGetMatrixAction to get the full transformation
65   // matrix from world space.
66 
67   const SbViewportRegion vpr = myviewer->getViewportRegion();
68   SoGetMatrixAction * getmatrixaction = new SoGetMatrixAction(vpr);
69   getmatrixaction->apply(path);
70 
71   SbMatrix transformation = getmatrixaction->getMatrix();
72 
73   // And if you want to access the individual transformation
74   // components of the matrix:
75 
76   SbVec3f translation;
77   SbRotation rotation;
78   SbVec3f scalevector;
79   SbRotation scaleorientation;
80 
81   transformation.getTransform(translation, rotation, scalevector, scaleorientation);
82 
83   \endcode
84 */
85 
86 
87 // Implementation note: nodes with special behavior on this action
88 // must set both the matrix and the inverse matrix explicitly, as no
89 // tracking is done to see when the matrices have been modified.
90 
91 
92 #include <Inventor/actions/SoGetMatrixAction.h>
93 
94 #include <cassert>
95 
96 #include <Inventor/elements/SoViewportRegionElement.h>
97 #include <Inventor/nodes/SoNode.h>
98 
99 #include "actions/SoSubActionP.h"
100 
101 class SoGetMatrixActionP {
102 public:
103 };
104 
105 SO_ACTION_SOURCE(SoGetMatrixAction);
106 
107 
108 // Overridden from parent.
109 void
initClass(void)110 SoGetMatrixAction::initClass(void)
111 {
112   SO_ACTION_INTERNAL_INIT_CLASS(SoGetMatrixAction, SoAction);
113 
114   SO_ENABLE(SoGetMatrixAction, SoViewportRegionElement);
115 }
116 
117 
118 /*!
119   Constructor.
120 
121   The \a region viewport specification is not used by this action, and
122   is passed along in case it is needed by any nodes.
123 */
SoGetMatrixAction(const SbViewportRegion & region)124 SoGetMatrixAction::SoGetMatrixAction(const SbViewportRegion & region)
125   : viewportregion(region)
126 {
127   SO_ACTION_CONSTRUCTOR(SoGetMatrixAction);
128 }
129 
130 /*!
131   Destructor.
132 */
~SoGetMatrixAction()133 SoGetMatrixAction::~SoGetMatrixAction()
134 {
135 }
136 
137 /*!
138   Set the viewport \a region.
139 
140   \sa SoGetMatrixAction::SoGetMatrixAction()
141 */
142 void
setViewportRegion(const SbViewportRegion & region)143 SoGetMatrixAction::setViewportRegion(const SbViewportRegion & region)
144 {
145   this->viewportregion = region;
146 }
147 
148 /*!
149   Returns the viewport region for the action instance.
150 */
151 const SbViewportRegion &
getViewportRegion(void) const152 SoGetMatrixAction::getViewportRegion(void) const
153 {
154   return this->viewportregion;
155 }
156 
157 /*!
158   Returns the accumulated transformation matrix.
159 
160   Note: don't modify the returned matrix. This should only be done if
161   you are implementing your own transformation type node
162   extensions. This advice is also valid for the other matrix access
163   methods documented below.
164 */
165 SbMatrix &
getMatrix(void)166 SoGetMatrixAction::getMatrix(void)
167 {
168   return this->matrix;
169 }
170 
171 /*!
172   Returns the inverse of the accumulated transformation matrix.
173 */
174 SbMatrix &
getInverse(void)175 SoGetMatrixAction::getInverse(void)
176 {
177   return this->invmatrix;
178 }
179 
180 /*!
181   Returns the accumulated texture matrix.
182 */
183 SbMatrix &
getTextureMatrix(void)184 SoGetMatrixAction::getTextureMatrix(void)
185 {
186   return this->texmatrix;
187 }
188 
189 /*!
190   Returns the inverse of the accumulated texture matrix.
191 */
192 SbMatrix &
getTextureInverse(void)193 SoGetMatrixAction::getTextureInverse(void)
194 {
195   return this->invtexmatrix;
196 }
197 
198 // Documented in superclass. Overridden from parent class to
199 // initialize the matrices before traversal starts.
200 void
beginTraversal(SoNode * node)201 SoGetMatrixAction::beginTraversal(SoNode * node)
202 {
203   assert(this->traversalMethods);
204 
205   SoViewportRegionElement::set(state, this->viewportregion);
206   this->matrix.makeIdentity();
207   this->invmatrix.makeIdentity();
208   this->texmatrix.makeIdentity();
209   this->invtexmatrix.makeIdentity();
210 
211   this->traverse(node);
212 }
213