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 SoScale SoScale.h Inventor/nodes/SoScale.h
35 \brief The SoScale class is a node type for scaling scene graph geometry.
36
37 \ingroup nodes
38
39 Use nodes of this type to apply scaling operations during scenegraph
40 traversals for e.g. rendering. Scale values are specified in a
41 triple-value vector, with one scale factor for each of the 3
42 principal axes.
43
44 <b>FILE FORMAT/DEFAULTS:</b>
45 \code
46 Scale {
47 scaleFactor 1 1 1
48 }
49 \endcode
50 */
51
52 // *************************************************************************
53
54 #include <Inventor/nodes/SoScale.h>
55
56 #include <Inventor/actions/SoGetMatrixAction.h>
57 #include <Inventor/actions/SoGLRenderAction.h>
58 #include <Inventor/elements/SoModelMatrixElement.h>
59
60 #include "nodes/SoSubNodeP.h"
61
62 // *************************************************************************
63
64 /*!
65 \var SoSFVec3f SoScale::scaleFactor
66
67 Specifies scaling along the 3 axes.
68
69 To get a uniform scale applied to the affected shapes, set the
70 scaleFactor field to a vector with the same value for all
71 components.
72
73 A common error when doing non-uniform scaling in a single direction
74 is to set the value for the other two components of the scaleFactor
75 vector to 0. This is obviously wrong, they should be set to 1 to \e
76 not scale the shape(s) in the other two directions.
77
78 Be careful with setting scaleFactor component values to 0 or to
79 negative values. Most shapes should handle those cases somehow, but
80 the results are undefined unless otherwise specified.
81
82 The default value of this vector field is [1.0, 1.0, 1.0].
83 */
84
85 // *************************************************************************
86
87 SO_NODE_SOURCE(SoScale);
88
89 // *************************************************************************
90
91 /*!
92 Constructor.
93 */
SoScale(void)94 SoScale::SoScale(void)
95 {
96 SO_NODE_INTERNAL_CONSTRUCTOR(SoScale);
97
98 SO_NODE_ADD_FIELD(scaleFactor, (1.0f, 1.0f, 1.0f));
99 }
100
101 /*!
102 Destructor.
103 */
~SoScale()104 SoScale::~SoScale()
105 {
106 }
107
108 // Doc in superclass.
109 void
initClass(void)110 SoScale::initClass(void)
111 {
112 SO_NODE_INTERNAL_INIT_CLASS(SoScale, SO_FROM_INVENTOR_1|SoNode::VRML1);
113 }
114
115 // Doc in superclass.
116 void
doAction(SoAction * action)117 SoScale::doAction(SoAction * action)
118 {
119 SoModelMatrixElement::scaleBy(action->getState(), this,
120 this->scaleFactor.getValue());
121 }
122
123 // Doc in superclass.
124 void
GLRender(SoGLRenderAction * action)125 SoScale::GLRender(SoGLRenderAction * action)
126 {
127 SoScale::doAction((SoAction *)action);
128 }
129
130 // Doc in superclass.
131 void
getBoundingBox(SoGetBoundingBoxAction * action)132 SoScale::getBoundingBox(SoGetBoundingBoxAction * action)
133 {
134 SoScale::doAction((SoAction *)action);
135 }
136
137 // Doc in superclass.
138 void
callback(SoCallbackAction * action)139 SoScale::callback(SoCallbackAction *action)
140 {
141 SoScale::doAction((SoAction*)action);
142 }
143
144 // Doc in superclass.
145 void
getMatrix(SoGetMatrixAction * action)146 SoScale::getMatrix(SoGetMatrixAction * action)
147 {
148 SbVec3f scalevec = this->scaleFactor.getValue();
149 SbMatrix m;
150
151 m.setScale(scalevec);
152 action->getMatrix().multLeft(m);
153
154 m.setScale(SbVec3f(1.0f / scalevec[0], 1.0f / scalevec[1], 1.0f / scalevec[2]));
155 action->getInverse().multRight(m);
156 }
157
158 // Doc in superclass.
159 void
pick(SoPickAction * action)160 SoScale::pick(SoPickAction *action)
161 {
162 SoScale::doAction((SoAction*)action);
163 }
164
165 // Doc in superclass. Overrides the traversal method in this class for
166 // the SoGetPrimitiveCountAction because the number of primitives can
167 // be different depending on scene location (and thereby distance to
168 // camera) if there are e.g. SoLOD nodes in the scene.
169 void
getPrimitiveCount(SoGetPrimitiveCountAction * action)170 SoScale::getPrimitiveCount(SoGetPrimitiveCountAction *action)
171 {
172 SoScale::doAction((SoAction*)action);
173 }
174