Debian Codegraph @

lib/interaction/src/draggers/SoRotateCylindricalDragger.c++

raw
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 | SoRotateCylindricalDragger
47 |
48 | Author(s): Paul Isaacs, Howard Look
49 |
50 ______________ S I L I C O N G R A P H I C S I N C . ____________
51 _______________________________________________________________________
52 */
53
54
55 #include <cstdio>
56 #include <Inventor/SbLinear.h>
57 #include <Inventor/SoDB.h>
58 #include <Inventor/sensors/SoFieldSensor.h>
59 #include <Inventor/SoInput.h>
60 #include <Inventor/SoPath.h>
61 #include <Inventor/projectors/SbCylinderPlaneProjector.h>
62 #include <Inventor/draggers/SoRotateCylindricalDragger.h>
63 #include <Inventor/nodes/SoSeparator.h>
64 #include <Inventor/nodes/SoSwitch.h>
65
66 #include "geom/SoRotateCylindricalDraggerGeom.h"
67
68
69 SO_KIT_SOURCE(SoRotateCylindricalDragger);
70
71
72 ////////////////////////////////////////////////////////////////////////
73 //
74 // Description:
75 // Constructor
76 //
77 SoRotateCylindricalDragger::SoRotateCylindricalDragger()
78 //
79 ////////////////////////////////////////////////////////////////////////
80 {
81 SO_KIT_CONSTRUCTOR(SoRotateCylindricalDragger);
82
83 isBuiltIn = TRUE;
84
85 // Put this stuff under the geomSeparator so it will draw more
86 // efficiently.
87 SO_KIT_ADD_CATALOG_ENTRY(rotatorSwitch, SoSwitch, TRUE,
88 geomSeparator, ,FALSE);
89 SO_KIT_ADD_CATALOG_ENTRY(rotator, SoSeparator, TRUE,
90 rotatorSwitch, ,TRUE);
91 SO_KIT_ADD_CATALOG_ENTRY(rotatorActive, SoSeparator, TRUE,
92 rotatorSwitch, ,TRUE);
93 SO_KIT_ADD_CATALOG_ENTRY(feedbackSwitch, SoSwitch, TRUE,
94 geomSeparator, ,FALSE);
95 SO_KIT_ADD_CATALOG_ENTRY(feedback, SoSeparator, TRUE,
96 feedbackSwitch, ,TRUE);
97 SO_KIT_ADD_CATALOG_ENTRY(feedbackActive, SoSeparator, TRUE,
98 feedbackSwitch, ,TRUE);
99
100 // read geometry for shared parts
101 if (SO_KIT_IS_FIRST_INSTANCE())
102 readDefaultParts("rotateCylindricalDragger.iv", geomBuffer, sizeof(geomBuffer) );
103
104 SO_KIT_ADD_FIELD(rotation, (0.0, 0.0, 0.0, 1.0));
105
106 SO_KIT_INIT_INSTANCE();
107
108 // create the parts...
109 setPartAsDefault("rotator", "rotateCylindricalRotator");
110 setPartAsDefault("rotatorActive",
111 "rotateCylindricalRotatorActive");
112 setPartAsDefault("feedback", "rotateCylindricalFeedback");
113 setPartAsDefault("feedbackActive",
114 "rotateCylindricalFeedbackActive");
115
116 // Set the switches to 0...
117 setSwitchValue( rotatorSwitch.getValue(), 0 );
118 setSwitchValue( feedbackSwitch.getValue(), 0 );
119
120 // start with our own default projector
121 // the user can replace if they want
122 cylinderProj = new SbCylinderPlaneProjector();
123 userProj = FALSE;
124
125 // add the callbacks to perform the dragging
126 addStartCallback( &SoRotateCylindricalDragger::startCB );
127 addMotionCallback( &SoRotateCylindricalDragger::motionCB );
128 addFinishCallback( &SoRotateCylindricalDragger::doneCB );
129
130 // Updates the scaleFactor field when the motionMatrix is set.
131 addValueChangedCallback( &SoRotateCylindricalDragger::valueChangedCB );
132
133 // Updates the motionMatrix when the scaleFactor field is set.
134 fieldSensor
135 = new SoFieldSensor(&SoRotateCylindricalDragger::fieldSensorCB, this);
136 fieldSensor->setPriority( 0 );
137
138 setUpConnections( TRUE, TRUE );
139 }
140
141 ////////////////////////////////////////////////////////////////////////
142 //
143 // Description:
144 // Destructor
145 //
146 SoRotateCylindricalDragger::~SoRotateCylindricalDragger()
147 //
148 ////////////////////////////////////////////////////////////////////////
149 {
150 if ( cylinderProj )
151 delete cylinderProj;
152 if (fieldSensor )
153 delete fieldSensor;
154 }
155
156 ////////////////////////////////////////////////////////////////////////
157 //
158 // Description:
159 // Redefines this to also copy the projector.
160 //
161 // Use: protected, virtual
162
163 void
164 SoRotateCylindricalDragger::copyContents(const SoFieldContainer *fromFC,
165 SbBool copyConnections)
166 //
167 ////////////////////////////////////////////////////////////////////////
168 {
169 // Do the usual stuff
170 SoDragger::copyContents(fromFC, copyConnections);
171
172 // Now, copy the projector variables...
173
174 const SoRotateCylindricalDragger *origDragger =
175 (const SoRotateCylindricalDragger *) fromFC;
176
177 if ( cylinderProj )
178 delete cylinderProj;
179
180 if ( origDragger->cylinderProj )
181 cylinderProj = (SbCylinderProjector *)
182 origDragger->cylinderProj->copy();
183 else
184 cylinderProj = NULL;
185
186 userProj = origDragger->userProj;
187 }
188
189 // detach/attach any sensors, callbacks, and/or field connections.
190 // Called by: start/end of SoBaseKit::readInstance
191 // and on new copy by: start/end of SoBaseKit::copy.
192 // Classes that redefine must call setUpConnections(TRUE,TRUE)
193 // at end of constructor.
194 // Returns the state of the node when this was called.
195 SbBool
196 SoRotateCylindricalDragger::setUpConnections( SbBool onOff, SbBool doItAlways )
197 {
198 if ( !doItAlways && connectionsSetUp == onOff)
199 return onOff;
200
201 if ( onOff ) {
202
203 // We connect AFTER base class.
204 SoDragger::setUpConnections( onOff, FALSE );
205
206 // Call the sensor CBs to make things are up-to-date.
207 fieldSensorCB( this, NULL );
208
209 // Connect the field sensors
210 if (fieldSensor->getAttachedField() != &rotation)
211 fieldSensor->attach( &rotation );
212 }
213 else {
214
215 // We disconnect BEFORE base class.
216
217 // Disconnect the field sensors.
218 if (fieldSensor->getAttachedField())
219 fieldSensor->detach();
220
221 SoDragger::setUpConnections( onOff, FALSE );
222 }
223
224 return !(connectionsSetUp = onOff);
225 }
226
227 ////////////////////////////////////////////////////////////////////////
228 //
229 // Description:
230 // Set the cylinder projector back to the default or to something
231 // new.
232 //
233 // Use: public
234 //
235 void
236 SoRotateCylindricalDragger::setProjector(SbCylinderProjector *proj)
237 //
238 ////////////////////////////////////////////////////////////////////////
239 {
240 if ( cylinderProj )
241 delete cylinderProj;
242
243 if (proj == NULL) {
244 // passing in null resets the projector to the default
245 userProj = FALSE;
246 cylinderProj = new SbCylinderPlaneProjector();
247 }
248 else {
249 // use the projector passed in
250 cylinderProj = proj;
251 userProj = TRUE;
252 }
253 }
254
255 ////////////////////////////////////////////////////////////////////////
256 //
257 // Description:
258 // Rotate the rotateCylindricalDragger based on mouse motion.
259 //
260 // Use: private
261 //
262 void
263 SoRotateCylindricalDragger::dragStart()
264 //
265 ////////////////////////////////////////////////////////////////////////
266 {
267 // Set the switches to 1...
268 setSwitchValue( rotatorSwitch.getValue(), 1 );
269 setSwitchValue( feedbackSwitch.getValue(), 1 );
270
271 // Establish the projector cylinder in working space.
272 // Working space is space at end of motion matrix.
273 // translate direction is defined as (1,0,0) in local space.
274 // The axis in working space is the Y axis.
275 SbLine wsAxis(SbVec3f(0,0,0), SbVec3f(0,1,0));
276
277 SbVec3f startLocalHitPt = getLocalStartingPoint();
278 float rad = (startLocalHitPt
279 - wsAxis.getClosestPoint(startLocalHitPt)).length();
280 cylinderProj->setCylinder( SbCylinder(wsAxis, rad) );
281
282 // If the hit point is on the near side of the center from where
283 // the eye is, then tell the projector to intersect front.
284 // Else, tell it to intersect back.
285 SbMatrix lclToWld = getLocalToWorldMatrix();
286 cylinderProj->setViewVolume( getViewVolume() );
287 cylinderProj->setWorkingSpace( lclToWld );
288 if (getFrontOnProjector() == USE_PICK )
289 cylinderProj->setFront(cylinderProj->isPointInFront(startLocalHitPt));
290 else if (getFrontOnProjector() == FRONT )
291 cylinderProj->setFront( TRUE );
292 else
293 cylinderProj->setFront( FALSE );
294
295 // The cylindrical (and spherical) projectors are sort of weird in
296 // that the initial hit defines the projector, but may not actually lie
297 // on the cylinder or sphere selected.
298 // This happens when the inital hit is too close to the edge to fit within
299 // 'tolerance.'
300 // So, to be sure that we get accurate performance, we need to
301 // project the mouse onto the projector once it is defined in order to
302 // get our starting point.
303 SbVec3f localProjectPt, worldProjectPt;
304 localProjectPt = cylinderProj->project( getNormalizedLocaterPosition());
305 lclToWld.multVecMatrix( localProjectPt, worldProjectPt );
306 setStartingPoint( worldProjectPt );
307 }
308
309 ////////////////////////////////////////////////////////////////////////
310 //
311 // Description:
312 // Rotate the rotateCylindricalDragger based on mouse motion.
313 //
314 // Use: private
315 //
316 void
317 SoRotateCylindricalDragger::drag()
318 //
319 ////////////////////////////////////////////////////////////////////////
320 {
321 // Set up the projector space and view.
322 // Working space is space at end of motion matrix.
323 cylinderProj->setViewVolume( getViewVolume() );
324 cylinderProj->setWorkingSpace( getLocalToWorldMatrix() );
325
326 // Get newHitPt and startHitPt in workspace.
327 SbVec3f newHitPt
328 = cylinderProj->project( getNormalizedLocaterPosition());
329 SbVec3f startHitPt = getLocalStartingPoint();
330
331 // deltaRot is how much we rotated since the mouse button went down.
332 SbRotation deltaRot =
333 cylinderProj->getRotation( startHitPt, newHitPt );
334
335 // Append this to the startMotionMatrix, which we saved at the beginning
336 // of the drag, to find the current motion matrix.
337 setMotionMatrix(
338 appendRotation( getStartMotionMatrix(), deltaRot, SbVec3f(0,0,0)));
339 }
340
341 ////////////////////////////////////////////////////////////////////////
342 //
343 // Description:
344 // Rotate the rotateCylindricalDragger based on mouse motion.
345 //
346 // Use: private
347 //
348 void
349 SoRotateCylindricalDragger::dragFinish()
350 //
351 ////////////////////////////////////////////////////////////////////////
352 {
353 // Set the switches to 0...
354 setSwitchValue( rotatorSwitch.getValue(), 0 );
355 setSwitchValue( feedbackSwitch.getValue(), 0 );
356 }
357
358 ////////////////////////////////////////////////////////////////////
359 // Stubs for callbacks
360 ////////////////////////////////////////////////////////////////////
361 void
362 SoRotateCylindricalDragger::startCB( void *, SoDragger *inDragger )
363 {
364 SoRotateCylindricalDragger *dl = (SoRotateCylindricalDragger *) inDragger;
365 dl->dragStart();
366 }
367
368 void
369 SoRotateCylindricalDragger::motionCB( void *, SoDragger *inDragger )
370 {
371 SoRotateCylindricalDragger *dl = (SoRotateCylindricalDragger *) inDragger;
372 dl->drag();
373 }
374
375 void
376 SoRotateCylindricalDragger::doneCB( void *, SoDragger *inDragger )
377 {
378 SoRotateCylindricalDragger *dl = (SoRotateCylindricalDragger *) inDragger;
379 dl->dragFinish();
380 }
381
382 void
383 SoRotateCylindricalDragger::valueChangedCB( void *, SoDragger *inDragger )
384 {
385 SoRotateCylindricalDragger *m = (SoRotateCylindricalDragger *) inDragger;
386 SbMatrix motMat = m->getMotionMatrix();
387
388 SbVec3f trans, scale;
389 SbRotation rot, scaleOrient;
390 getTransformFast( motMat, trans, rot, scale, scaleOrient );
391
392 // Disconnect the field sensor
393 m->fieldSensor->detach();
394
395 if ( m->rotation.getValue() != rot )
396 m->rotation = rot;
397
398 // Reconnect the field sensor
399 m->fieldSensor->attach( &(m->rotation) );
400 }
401
402 void
403 SoRotateCylindricalDragger::fieldSensorCB( void *inDragger, SoSensor * )
404 {
405 SoRotateCylindricalDragger *dragger
406 = (SoRotateCylindricalDragger *) inDragger;
407
408 // Incorporate the new field value into the matrix...
409 SbMatrix motMat = dragger->getMotionMatrix();
410 dragger->workFieldsIntoTransform( motMat );
411
412 dragger->setMotionMatrix( motMat );
413 }