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 SoPathSwitch SoPathSwitch.h Inventor/nodes/SoPathSwitch.h
35   \brief The SoPathSwitch class traverses only when current path matches a configurable path.
36 
37   \ingroup nodes
38 
39   <b>FILE FORMAT/DEFAULTS:</b>
40   \code
41     PathSwitch {
42         path NULL
43     }
44   \endcode
45 */
46 
47 // *************************************************************************
48 
49 #include <Inventor/nodes/SoPathSwitch.h>
50 
51 #include <Inventor/actions/SoGetBoundingBoxAction.h>
52 #include <Inventor/actions/SoGLRenderAction.h>
53 #include <Inventor/actions/SoGetMatrixAction.h>
54 #include <Inventor/actions/SoHandleEventAction.h>
55 #include <Inventor/actions/SoGetPrimitiveCountAction.h>
56 #include <Inventor/actions/SoPickAction.h>
57 #include <Inventor/actions/SoSearchAction.h>
58 #include <Inventor/actions/SoCallbackAction.h>
59 #include <Inventor/actions/SoAudioRenderAction.h>
60 
61 #include "nodes/SoSubNodeP.h"
62 
63 // *************************************************************************
64 
65 /*!
66   \var SoSFPath SoPathSwitch::path
67 
68   The path that must match the current action path. A \c NULL path
69   will never match. An empty path will always match. The path should
70   go up to (not including) the SoPathSwitch node, but need not go all
71   the way back to the root node.
72 */
73 
74 // *************************************************************************
75 
76 SO_NODE_SOURCE(SoPathSwitch);
77 
78 // *************************************************************************
79 
80 /*!
81   Default constructor.
82 */
SoPathSwitch(void)83 SoPathSwitch::SoPathSwitch(void)
84 {
85   this->commonConstructor();
86 }
87 
88 
89 static SbBool
is_matching_paths(const SoPath * currentpath,const SoPath * pathswitchpath)90 is_matching_paths(const SoPath * currentpath, const SoPath * pathswitchpath)
91 {
92   if (pathswitchpath == NULL) return FALSE;
93 
94   const SoFullPath * current = (const SoFullPath*) currentpath;
95   const SoFullPath * swpath = (const SoFullPath *) pathswitchpath;
96 
97   int swidx = swpath->getLength() - 1;
98   if (swidx < 0) return TRUE; // an empty path will always match
99 
100   int curidx = current->getLength() - 2; // last node is this node. Skip it.
101 
102   // test if swpath is a valid path. Return FALSE if not.
103   if (swidx > curidx) return FALSE;
104 
105   // we know curidx >= swidx
106   while (swidx > 0) {
107     if ((swpath->getNode(swidx) != current->getNode(curidx)) ||
108         (swpath->getIndex(swidx) != current->getIndex(curidx))) break;
109     swidx--;
110     curidx--;
111   }
112 
113   if (swidx == 0) { // don't test index for head node
114     return swpath->getHead() == current->getNode(curidx);
115   }
116   return FALSE;
117 }
118 
119 
120 /*!
121   Constructor.
122 
123   The argument should be the approximate number of children which is
124   expected to be inserted below this node. The number need not be
125   exact, as it is only used as a hint for better memory resource
126   allocation.
127 */
SoPathSwitch(int numchildren)128 SoPathSwitch::SoPathSwitch(int numchildren)
129   : inherited(numchildren)
130 {
131   this->commonConstructor();
132 }
133 
134 // private
135 void
commonConstructor(void)136 SoPathSwitch::commonConstructor(void)
137 {
138   SO_NODE_INTERNAL_CONSTRUCTOR(SoPathSwitch);
139   SO_NODE_ADD_FIELD(path, (NULL));
140 }
141 
142 /*!
143   Destructor.
144 */
~SoPathSwitch()145 SoPathSwitch::~SoPathSwitch()
146 {
147 }
148 
149 // doc in parent
150 void
initClass(void)151 SoPathSwitch::initClass(void)
152 {
153   SO_NODE_INTERNAL_INIT_CLASS(SoPathSwitch, SO_FROM_INVENTOR_1);
154 }
155 
156 // doc in parent
157 void
getBoundingBox(SoGetBoundingBoxAction * action)158 SoPathSwitch::getBoundingBox(SoGetBoundingBoxAction * action)
159 {
160   if (is_matching_paths(action->getCurPath(), this->path.getValue())) {
161     inherited::getBoundingBox(action);
162   }
163 }
164 
165 // doc in parent
166 void
doAction(SoAction * action)167 SoPathSwitch::doAction(SoAction * action)
168 {
169   if (is_matching_paths(action->getCurPath(), this->path.getValue())) {
170     inherited::doAction(action);
171   }
172 }
173 
174 // doc in parent
175 void
GLRender(SoGLRenderAction * action)176 SoPathSwitch::GLRender(SoGLRenderAction * action)
177 {
178   if (is_matching_paths(action->getCurPath(), this->path.getValue())) {
179     inherited::GLRender(action);
180   }
181 }
182 
183 // doc in parent
184 void
pick(SoPickAction * action)185 SoPathSwitch::pick(SoPickAction * action)
186 {
187   if (is_matching_paths(action->getCurPath(), this->path.getValue())) {
188     inherited::pick(action);
189   }
190 }
191 
192 // doc in parent
193 void
audioRender(SoAudioRenderAction * action)194 SoPathSwitch::audioRender(SoAudioRenderAction * action)
195 {
196   if (is_matching_paths(action->getCurPath(), this->path.getValue())) {
197     inherited::audioRender(action);
198   }
199 }
200 
201 // doc in parent
202 void
handleEvent(SoHandleEventAction * action)203 SoPathSwitch::handleEvent(SoHandleEventAction * action)
204 {
205   if (is_matching_paths(action->getCurPath(), this->path.getValue())) {
206     inherited::handleEvent(action);
207   }
208 }
209 
210 // doc in parent
211 void
getMatrix(SoGetMatrixAction * action)212 SoPathSwitch::getMatrix(SoGetMatrixAction * action)
213 {
214   if (is_matching_paths(action->getCurPath(), this->path.getValue())) {
215     inherited::getMatrix(action);
216   }
217 }
218 
219 // doc in parent
220 void
search(SoSearchAction * action)221 SoPathSwitch::search(SoSearchAction * action)
222 {
223   if (action->isSearchingAll()) inherited::search(action);
224   else {
225     SoNode::search(action);
226     if (!action->isFound()) {
227       SoPathSwitch::doAction(action);
228     }
229   }
230 }
231 
232 // doc in parent
233 void
callback(SoCallbackAction * action)234 SoPathSwitch::callback(SoCallbackAction *action)
235 {
236   if (action->isCallbackAll()) inherited::doAction(action);
237   else SoPathSwitch::doAction(action);
238 }
239 
240 // doc in parent
241 void
getPrimitiveCount(SoGetPrimitiveCountAction * action)242 SoPathSwitch::getPrimitiveCount(SoGetPrimitiveCountAction * action)
243 {
244   if (is_matching_paths(action->getCurPath(), this->path.getValue())) {
245     inherited::getPrimitiveCount(action);
246   }
247 }
248