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 SoResetTransform SoResetTransform.h Inventor/nodes/SoResetTransform.h
35   \brief The SoResetTransform class is a node type which makes it possible to "nullify" state during traversal.
36 
37   \ingroup nodes
38 
39   SoResetTransform is useful for setting up geometry in the scene
40   graph which will not be influenced by the transformation nodes
41   before it during traversal.
42 
43   <b>FILE FORMAT/DEFAULTS:</b>
44   \code
45     ResetTransform {
46         whatToReset TRANSFORM
47     }
48   \endcode
49 */
50 
51 // *************************************************************************
52 
53 #include <Inventor/nodes/SoResetTransform.h>
54 
55 #include <Inventor/actions/SoGLRenderAction.h>
56 #include <Inventor/actions/SoGetBoundingBoxAction.h>
57 #include <Inventor/actions/SoGetMatrixAction.h>
58 #include <Inventor/elements/SoGLModelMatrixElement.h>
59 #include <Inventor/elements/SoGLCacheContextElement.h>
60 #include <Inventor/elements/SoCacheElement.h>
61 
62 #include "nodes/SoSubNodeP.h"
63 
64 // *************************************************************************
65 
66 /*!
67   \enum SoResetTransform::ResetType
68 
69   The different options for what to reset.
70 */
71 /*!
72   \var SoResetTransform::ResetType SoResetTransform::TRANSFORM
73 
74   Reset current model transformation.
75 */
76 /*!
77   \var SoResetTransform::ResetType SoResetTransform::BBOX
78 
79   Reset current bounding box settings.
80 */
81 
82 
83 /*!
84   \var SoSFBitMask SoResetTransform::whatToReset
85 
86   What this node instance should reset in the state when met during
87   traversal. Default value is SoResetTransform::TRANSFORM.
88 */
89 
90 
91 // *************************************************************************
92 
93 SO_NODE_SOURCE(SoResetTransform);
94 
95 /*!
96   Constructor.
97 */
SoResetTransform(void)98 SoResetTransform::SoResetTransform(void)
99 {
100   SO_NODE_INTERNAL_CONSTRUCTOR(SoResetTransform);
101 
102   SO_NODE_ADD_FIELD(whatToReset, (SoResetTransform::TRANSFORM));
103 
104   SO_NODE_DEFINE_ENUM_VALUE(ResetType, TRANSFORM);
105   SO_NODE_DEFINE_ENUM_VALUE(ResetType, BBOX);
106   SO_NODE_SET_SF_ENUM_TYPE(whatToReset, ResetType);
107 }
108 
109 /*!
110   Destructor.
111 */
~SoResetTransform()112 SoResetTransform::~SoResetTransform()
113 {
114 }
115 
116 // Doc from superclass.
117 void
initClass(void)118 SoResetTransform::initClass(void)
119 {
120   SO_NODE_INTERNAL_INIT_CLASS(SoResetTransform, SO_FROM_INVENTOR_1);
121 }
122 
123 // Doc from superclass.
124 void
GLRender(SoGLRenderAction * action)125 SoResetTransform::GLRender(SoGLRenderAction * action)
126 {
127   if (!this->whatToReset.isIgnored() &&
128       (this->whatToReset.getValue() & SoResetTransform::TRANSFORM)) {
129     SoState * state = action->getState();
130     SoGLModelMatrixElement::makeIdentity(state, this);
131   }
132 }
133 
134 // Doc from superclass.
135 void
getBoundingBox(SoGetBoundingBoxAction * action)136 SoResetTransform::getBoundingBox(SoGetBoundingBoxAction * action)
137 {
138   if (!this->whatToReset.isIgnored() &&
139       (this->whatToReset.getValue() & SoResetTransform::TRANSFORM)) {
140     SoState * state = action->getState();
141     // do this instead of calling SoResetTransform::doAction(action), so
142     // that SoLocalBBoxMatrixElement and SoBBoxModelMatrixElement can do
143     // the right thing.
144     SoModelMatrixElement::mult(state,
145                                this,
146                                SoModelMatrixElement::get(state).inverse());
147   }
148   if (!this->whatToReset.isIgnored() &&
149       (this->whatToReset.getValue() & SoResetTransform::BBOX)) {
150     action->getXfBoundingBox().makeEmpty();
151     action->resetCenter();
152   }
153 }
154 
155 // Doc from superclass.
156 void
doAction(SoAction * action)157 SoResetTransform::doAction(SoAction *action)
158 {
159   if (!this->whatToReset.isIgnored() &&
160       (this->whatToReset.getValue() & SoResetTransform::TRANSFORM)) {
161     SoModelMatrixElement::makeIdentity(action->getState(), this);
162   }
163 }
164 
165 // Doc from superclass.
166 void
callback(SoCallbackAction * action)167 SoResetTransform::callback(SoCallbackAction *action)
168 {
169   SoResetTransform::doAction((SoAction*)action);
170 }
171 
172 // Doc from superclass.
173 void
getMatrix(SoGetMatrixAction * action)174 SoResetTransform::getMatrix(SoGetMatrixAction *action)
175 {
176   if (!this->whatToReset.isIgnored() &&
177       (this->whatToReset.getValue() & SoResetTransform::TRANSFORM)) {
178     action->getMatrix().makeIdentity();
179     action->getInverse().makeIdentity();
180   }
181 }
182 
183 // Doc from superclass.
184 void
pick(SoPickAction * action)185 SoResetTransform::pick(SoPickAction *action)
186 {
187   SoResetTransform::doAction((SoAction*)action);
188 }
189 
190 // Doc from superclass. Overrides the traversal method in this class for
191 // the SoGetPrimitiveCountAction because the number of primitives can
192 // be different depending on scene location (and thereby distance to
193 // camera) if there are e.g. SoLOD nodes in the scene.
194 void
getPrimitiveCount(SoGetPrimitiveCountAction * action)195 SoResetTransform::getPrimitiveCount(SoGetPrimitiveCountAction *action)
196 {
197   SoResetTransform::doAction((SoAction*)action);
198 }
199