Debian Codegraph @

lib/database/src/so/nodes/SoPendulum.c++

raw symbols SCIP
1 /*
2 *
3 * Copyright (C) 2000 Silicon Graphics, Inc. All Rights Reserved.
4 *
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2.1 of the License, or (at your option) any later version.
9 *
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
14 *
15 * Further, this software is distributed without any warranty that it is
16 * free of the rightful claim of any third person regarding infringement
17 * or the like. Any license provided herein, whether implied or
18 * otherwise, applies only to this software file. Patent licenses, if
19 * any, provided herein do not apply to combinations of this program with
20 * other software, or any other product whatsoever.
21 *
22 * You should have received a copy of the GNU Lesser General Public
23 * License along with this library; if not, write to the Free Software
24 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
25 *
26 * Contact information: Silicon Graphics, Inc., 1600 Amphitheatre Pkwy,
27 * Mountain View, CA 94043, or:
28 *
29 * http://www.sgi.com
30 *
31 * For further information regarding this notice, see:
32 *
33 * http://oss.sgi.com/projects/GenInfo/NoticeExplan/
34 *
35 */
36
37 /*
38 * Copyright (C) 1990,91 Silicon Graphics, Inc.
39 *
40 _______________________________________________________________________
41 ______________ S I L I C O N G R A P H I C S I N C . ____________
42 |
43 | $Revision: 1.1.1.1 $
44 |
45 | Classes:
46 | SoPendulum
47 |
48 | Author(s) : Ronen Barzel
49 |
50 ______________ S I L I C O N G R A P H I C S I N C . ____________
51 _______________________________________________________________________
52 */
53
54 #include <Inventor/SoDB.h>
55 #include <Inventor/engines/SoElapsedTime.h>
56 #include <Inventor/engines/SoCalculator.h>
57 #include <Inventor/engines/SoInterpolate.h>
58 #include <Inventor/nodes/SoPendulum.h>
59 #include <Inventor/sensors/SoFieldSensor.h>
60 #include <Inventor/sensors/SoOneShotSensor.h>
61
62 SO_NODE_SOURCE(SoPendulum);
63
64 ////////////////////////////////////////////////////////////////////////
65 //
66 // Description:
67 // Constructor
68 //
69 // Use: public
70
71 SoPendulum::SoPendulum()
72 //
73 ////////////////////////////////////////////////////////////////////////
74 {
75 SO_NODE_CONSTRUCTOR(SoPendulum);
76 SO_NODE_ADD_FIELD(rotation0, (SbRotation(0,0,0,1)));
77 SO_NODE_ADD_FIELD(rotation1, (SbRotation(0,0,0,1)));
78 SO_NODE_ADD_FIELD(speed, (1));
79 SO_NODE_ADD_FIELD(on, (TRUE));
80
81 time = new SoElapsedTime;
82 calc = new SoCalculator;
83 interp = new SoInterpolateRotation;
84 time->ref();
85 calc->ref();
86 interp->ref();
87
88 // next make the sensors (when wiring the engines
89 // together the notify method will try to schedule
90 // the sensor, so make sure it exists).
91 rotationSensor = new SoOneShotSensor(rotationSensorCB, this);
92 rotationSensor->setPriority(0);
93 rotation0Sensor = new SoFieldSensor(rotationSensorCB, this);
94 rotation0Sensor->setPriority(0);
95 rotation0Sensor->attach(&rotation0);
96 rotation1Sensor = new SoFieldSensor(rotationSensorCB, this);
97 rotation1Sensor->setPriority(0);
98 rotation1Sensor->attach(&rotation1);
99 onSensor = new SoFieldSensor(onSensorCB, this);
100 onSensor->setPriority(0);
101 onSensor->attach(&on);
102
103 time->on.connectFrom(&on);
104 calc->a.connectFrom(&speed);
105 calc->b.connectFrom(&time->timeOut);
106 // input "c" to the calculation is a phase offset that is
107 // set by the rotation sensor callback.
108 calc->expression.setValue("oa = 0.5 + 0.5 * sin(a*b*2*M_PI + c)");
109 rotationSensorCB(this, NULL); // init the offset to the default rotation
110 interp->input0.connectFrom(&rotation0);
111 interp->input1.connectFrom(&rotation1);
112 interp->alpha.connectFrom(&calc->oa);
113 rotation.connectFrom(&interp->output);
114
115 // keep the internal connection, so we can tell
116 // later if somebody overrode it.
117 rotation.getConnectedEngine(internalConnection);
118
119 isBuiltIn = TRUE;
120 }
121
122 ////////////////////////////////////////////////////////////////////////
123 //
124 // Description:
125 // Destructor (necessary since inline destructor is too complex)
126 //
127 // Use: private
128
129 SoPendulum::~SoPendulum()
130 //
131 ////////////////////////////////////////////////////////////////////////
132 {
133 time->unref();
134 calc->unref();
135 interp->unref();
136
137 delete rotationSensor;
138 delete rotation0Sensor;
139 delete rotation1Sensor;
140 delete onSensor;
141 }
142
143 ////////////////////////////////////////////////////////////////////////
144 //
145 // Description:
146 // Write action. Disables connections to internal engines
147 // during the write.
148 //
149 // Use: protected
150
151 void
152 SoPendulum::write(SoWriteAction *action)
153 //
154 ////////////////////////////////////////////////////////////////////////
155 {
156 SbBool save = rotation.isConnectionEnabled();
157 SoEngineOutput *rotationSource;
158
159 // if the connection is to the internal engine, disable it
160 if (rotation.isConnected() &&
161 rotation.getConnectedEngine(rotationSource) &&
162 rotationSource == internalConnection) {
163
164 rotation.enableConnection(FALSE);
165 }
166
167 SoRotation::write(action);
168
169 rotation.enableConnection(save);
170 }
171
172 ////////////////////////////////////////////////////////////////////////
173 //
174 // Description:
175 // Override notify to notice when the rotation field is touched.
176 // Can't use field sensor because we need to distinguish
177 // our own touching of the field from a setValue by the client.
178 //
179 // Use: protected
180
181 void
182 SoPendulum::notify(SoNotList *list)
183 //
184 ////////////////////////////////////////////////////////////////////////
185 {
186 SoNotRec *rec = list->getFirstRec();
187
188 // only interested in a field-to-container notification
189 if (rec->getType() == SoNotRec::CONTAINER &&
190 rec->getBase() == this &&
191 list->getLastField() == &rotation) {
192
193 rotationSensor->schedule();
194 }
195
196 SoRotation::notify(list);
197 }
198
199 ////////////////////////////////////////////////////////////////////////
200 //
201 // Description:
202 // Callback is called when somebody sets the rotation field.
203 //
204 // Use: protected
205
206 void
207 SoPendulum::rotationSensorCB(void *data, SoSensor *)
208 //
209 ////////////////////////////////////////////////////////////////////////
210 {
211 SoPendulum *pendulum = (SoPendulum *) data;
212 SbVec4f q, q1, q0;
213 pendulum->rotation.getValue().getValue(q[0],q[1],q[2],q[3]);
214 pendulum->rotation0.getValue().getValue(q0[0],q0[1],q0[2],q0[3]);
215 pendulum->rotation1.getValue().getValue(q1[0],q1[1],q1[2],q1[3]);
216
217 // find the value of the interpolant that would
218 // line up with q. See where q projects onto the line
219 // from q0 to q1
220 SbVec4f diff = q1 - q0;
221 float len = diff.length(), alpha;
222 if (len == 0)
223 alpha = 0;
224 else {
225 alpha = (q - q0).dot(diff)/(len*len);
226 if (alpha > 1)
227 alpha = 1;
228 if (alpha < 0)
229 alpha = 0;
230 }
231
232 float a = pendulum->calc->a[0];
233 float b = pendulum->calc->b[0];
234 // invert the expression: alpha = .5 + .5 sin(a*b*2*PI + c)
235 pendulum->calc->c.setValue(asin(2*alpha-1) - a*b*2*M_PI);
236 }
237
238 ////////////////////////////////////////////////////////////////////////
239 //
240 // Description:
241 // Callback is called when somebody sets the on field
242 //
243 // Use: protected
244
245 void
246 SoPendulum::onSensorCB(void *data, SoSensor *)
247 //
248 ////////////////////////////////////////////////////////////////////////
249 {
250 SoPendulum *pendulum = (SoPendulum *) data;
251
252 if (pendulum->on.getValue()) {
253 // if turned on, force the internal connection to be made and enabled,
254 // no matter what sort of mucking has happened to it.
255 pendulum->rotation.enableConnection(TRUE);
256 pendulum->rotation.connectFrom(&pendulum->interp->output);
257 pendulum->rotation.getConnectedEngine(pendulum->internalConnection);
258 }
259 }