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 SoShuttle SoShuttle.h Inventor/nodes/SoShuttle.h
35 \brief The SoShuttle class is used to oscillate between two translations.
36
37 \ingroup nodes
38
39 A smooth transition between translation0 and translation1 is created
40 using a cosine function. In the beginning of the cycle, translation0
41 is used. Halfway through the cycle, the resulting translation equals
42 translation1, and at the end of the cycle, we're at translation0
43 again.
44
45 <b>FILE FORMAT/DEFAULTS:</b>
46 \code
47 Shuttle {
48 translation 0 0 0
49 translation0 0 0 0
50 translation1 0 0 0
51 speed 1
52 on TRUE
53 }
54 \endcode
55 */
56
57 // *************************************************************************
58
59 #include <Inventor/nodes/SoShuttle.h>
60
61 #include <Inventor/SoOutput.h>
62 #include <Inventor/actions/SoWriteAction.h>
63 #include <Inventor/engines/SoCalculator.h>
64 #include <Inventor/engines/SoElapsedTime.h>
65 #include <Inventor/engines/SoInterpolateVec3f.h>
66
67 #include "nodes/SoSubNodeP.h"
68
69 // *************************************************************************
70
71 /*!
72 \var SoSFVec3f SoShuttle::translation0
73
74 Translation at the start and end of the cycle. Default value is (0,
75 0, 0).
76 */
77 /*!
78 \var SoSFVec3f SoShuttle::translation1
79
80 Translation at the middle of the cycle. Default value is (0, 0, 0).
81 */
82 /*!
83 \var SoSFFloat SoShuttle::speed
84 Number of cycles per second. Default value is 1.
85 */
86 /*!
87 \var SoSFBool SoShuttle::on
88 Toggles animation on or off. Defauls to \c TRUE.
89 */
90
91 // *************************************************************************
92
93 SO_NODE_SOURCE(SoShuttle);
94
95 // *************************************************************************
96
97 /*!
98 Constructor.
99 */
SoShuttle(void)100 SoShuttle::SoShuttle(void)
101 {
102 SO_NODE_INTERNAL_CONSTRUCTOR(SoShuttle);
103
104 SO_NODE_ADD_FIELD(translation0, (SbVec3f(0.0f, 0.0f, 0.0f)));
105 SO_NODE_ADD_FIELD(translation1, (SbVec3f(0.0f, 0.0f, 0.0f)));
106 SO_NODE_ADD_FIELD(speed, (1.0f));
107 SO_NODE_ADD_FIELD(on, (TRUE));
108
109 this->interpolator = new SoInterpolateVec3f;
110 this->interpolator->ref();
111 this->calculator = new SoCalculator;
112 this->calculator->ref();
113 this->timer = new SoElapsedTime;
114 this->timer->ref();
115
116 this->calculator->expression = "oa = (1.0 - cos(a*b*2*M_PI)) * 0.5";
117 this->calculator->a.connectFrom(&this->timer->timeOut);
118 this->timer->on.connectFrom(&this->on);
119 this->calculator->b.connectFrom(&this->speed);
120 this->interpolator->input0.connectFrom(&this->translation0);
121 this->interpolator->input1.connectFrom(&this->translation1);
122 this->interpolator->alpha.connectFrom(&this->calculator->oa);
123
124 this->translation.connectFrom(&this->interpolator->output, TRUE);
125 }
126
127 /*!
128 Destructor.
129 */
~SoShuttle()130 SoShuttle::~SoShuttle()
131 {
132 this->interpolator->unref();
133 this->calculator->unref();
134 this->timer->unref();
135 }
136
137 // doc in parent
138 void
initClass(void)139 SoShuttle::initClass(void)
140 {
141 SO_NODE_INTERNAL_INIT_CLASS(SoShuttle, SO_FROM_INVENTOR_1);
142 }
143
144 // Documented in superclass.
145 void
write(SoWriteAction * action)146 SoShuttle::write(SoWriteAction * action)
147 {
148 // Overridden to not write out internal engine connections.
149
150 SoOutput * out = action->getOutput();
151
152 // Decouple connections to/from internal engines to avoid them being
153 // written. (Only done at first pass.)
154 if (out->getStage() == SoOutput::COUNT_REFS)
155 this->deconnectInternalEngines();
156
157 inherited::write(action);
158
159 // Reenable all connections to/from internal engine. (Only done at
160 // last pass.)
161 if (out->getStage() == SoOutput::WRITE)
162 this->reconnectInternalEngines();
163 }
164
165 // FIXME: I _think_ we made a mistake when overriding SoNode::copy()
166 // and making it virtual. See FIXME-comment above
167 // SoBlinker::copy(). 20011220 mortene.
168
169 // Overridden to decouple and reconnect engine around copy operation.
170 SoNode *
copy(SbBool copyconnections) const171 SoShuttle::copy(SbBool copyconnections) const
172 {
173 // Decouple connections to/from internal engines to avoid them being
174 // copied.
175 ((SoShuttle *)this)->deconnectInternalEngines();
176
177 SoShuttle * cp = (SoShuttle *)inherited::copy(copyconnections);
178
179 // Reenable all connections to/from internal engines.
180 ((SoShuttle *)this)->reconnectInternalEngines();
181
182 return cp;
183 }
184
185 // Remove connections to and from internal engines.
186 void
deconnectInternalEngines(void)187 SoShuttle::deconnectInternalEngines(void)
188 {
189 // Do this first, to avoid field being set due to subsequent engine
190 // input value change.
191 this->translation.disconnect(&this->interpolator->output);
192
193 this->timer->on.disconnect(&this->on);
194 this->timer->on = FALSE;
195 this->calculator->b.disconnect(&this->speed);
196 this->interpolator->input0.disconnect(&this->translation0);
197 this->interpolator->input1.disconnect(&this->translation1);
198 }
199
200
201 // Reenable all connections to/from internal engines.
202 void
reconnectInternalEngines(void)203 SoShuttle::reconnectInternalEngines(void)
204 {
205 this->timer->on.connectFrom(&this->on);
206 this->calculator->b.connectFrom(&this->speed);
207 this->interpolator->input0.connectFrom(&this->translation0);
208 this->interpolator->input1.connectFrom(&this->translation1);
209
210 this->translation.connectFrom(&this->interpolator->output, TRUE);
211 }
212