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 SoMatrixTransform SoMatrixTransform.h Inventor/nodes/SoMatrixTransform.h
35 \brief The SoMatrixTransform class is a transformation node.
36
37 \ingroup nodes
38
39 This class is the most flexible transformation node, as you can use
40 it to accumulate any kind of transformation matrix on top of the
41 current model transformation matrix.
42
43 <b>FILE FORMAT/DEFAULTS:</b>
44 \code
45 MatrixTransform {
46 matrix 1 0 0 0
47 0 1 0 0
48 0 0 1 0
49 0 0 0 1
50 }
51 \endcode
52
53 \sa SoTransform
54 */
55
56 // *************************************************************************
57
58 #include <Inventor/nodes/SoMatrixTransform.h>
59
60 #include <Inventor/actions/SoGetMatrixAction.h>
61 #include <Inventor/actions/SoGLRenderAction.h>
62 #include <Inventor/elements/SoModelMatrixElement.h>
63
64 #include "nodes/SoSubNodeP.h"
65
66 // *************************************************************************
67
68 /*!
69 \var SoSFMatrix SoMatrixTransform::matrix
70 The transformation matrix. Defaults to the identity matrix.
71 */
72
73 // *************************************************************************
74
75 SO_NODE_SOURCE(SoMatrixTransform);
76
77 /*!
78 Constructor.
79 */
SoMatrixTransform(void)80 SoMatrixTransform::SoMatrixTransform(void)
81 {
82 SO_NODE_INTERNAL_CONSTRUCTOR(SoMatrixTransform);
83
84 SO_NODE_ADD_FIELD(matrix, (SbMatrix::identity()));
85 }
86
87 /*!
88 Destructor.
89 */
~SoMatrixTransform()90 SoMatrixTransform::~SoMatrixTransform()
91 {
92 }
93
94 // Doc from superclass.
95 void
initClass(void)96 SoMatrixTransform::initClass(void)
97 {
98 SO_NODE_INTERNAL_INIT_CLASS(SoMatrixTransform, SO_FROM_INVENTOR_1|SoNode::VRML1);
99 }
100
101 // Doc from superclass.
102 void
doAction(SoAction * action)103 SoMatrixTransform::doAction(SoAction * action)
104 {
105 if (this->matrix.isIgnored()) { return; }
106
107 const SbMatrix & mat = this->matrix.getValue();
108 if (mat != SbMatrix::identity()) {
109 SoModelMatrixElement::mult(action->getState(), this,
110 mat);
111 }
112 }
113
114 // Doc from superclass.
115 void
GLRender(SoGLRenderAction * action)116 SoMatrixTransform::GLRender(SoGLRenderAction * action)
117 {
118 SoMatrixTransform::doAction((SoAction *)action);
119 }
120
121 // Doc from superclass.
122 void
getBoundingBox(SoGetBoundingBoxAction * action)123 SoMatrixTransform::getBoundingBox(SoGetBoundingBoxAction * action)
124 {
125 SoMatrixTransform::doAction((SoAction *)action);
126 }
127
128 // Doc from superclass.
129 void
callback(SoCallbackAction * action)130 SoMatrixTransform::callback(SoCallbackAction * action)
131 {
132 SoMatrixTransform::doAction((SoAction *)action);
133 }
134
135 // Doc from superclass.
136 void
getMatrix(SoGetMatrixAction * action)137 SoMatrixTransform::getMatrix(SoGetMatrixAction * action)
138 {
139 if (this->matrix.isIgnored()) { return; }
140
141 SbMatrix m = this->matrix.getValue();
142 action->getMatrix().multLeft(m);
143
144 SbMatrix mi = m.inverse();
145 action->getInverse().multRight(mi);
146 }
147
148 // Doc from superclass.
149 void
pick(SoPickAction * action)150 SoMatrixTransform::pick(SoPickAction * action)
151 {
152 SoMatrixTransform::doAction((SoAction *)action);
153 }
154
155 // Doc from superclass. Overrides the traversal method in this class for
156 // the SoGetPrimitiveCountAction because the number of primitives can
157 // be different depending on scene location (and thereby distance to
158 // camera) if there are e.g. SoLOD nodes in the scene.
159 void
getPrimitiveCount(SoGetPrimitiveCountAction * action)160 SoMatrixTransform::getPrimitiveCount(SoGetPrimitiveCountAction * action)
161 {
162 SoMatrixTransform::doAction((SoAction *)action);
163 }
164