Debian Codegraph @

lib/interaction/src/draggers/SoTabPlaneDragger.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) 1992 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.2 $
44 |
45 | Classes:
46 | SoTabPlaneDragger
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 #include <Inventor/SbLinear.h>
55 #include <Inventor/SoDB.h>
56 #include <Inventor/SoPath.h>
57 #include <Inventor/SoPickedPoint.h>
58
59 #include <Inventor/events/SoEvent.h>
60 #include <Inventor/events/SoKeyboardEvent.h>
61 #include <Inventor/sensors/SoFieldSensor.h>
62
63 #include <Inventor/actions/SoGLRenderAction.h>
64 #include <Inventor/actions/SoHandleEventAction.h>
65
66 #include <Inventor/elements/SoCacheElement.h>
67 #include <Inventor/elements/SoProjectionMatrixElement.h>
68 #include <Inventor/elements/SoViewingMatrixElement.h>
69 #include <Inventor/elements/SoViewportRegionElement.h>
70
71 #include <Inventor/nodes/SoCoordinate3.h>
72 #include <Inventor/nodes/SoIndexedFaceSet.h>
73 #include <Inventor/nodes/SoMaterial.h>
74 #include <Inventor/nodes/SoMaterialBinding.h>
75 #include <Inventor/nodes/SoNormalBinding.h>
76 #include <Inventor/nodes/SoNormal.h>
77 #include <Inventor/nodes/SoSeparator.h>
78 #include <Inventor/nodes/SoShapeHints.h>
79 #include <Inventor/nodes/SoSwitch.h>
80 #include <Inventor/nodes/SoTransform.h>
81
82 #include <Inventor/draggers/SoTabPlaneDragger.h>
83
84 #include "geom/SoTabPlaneDraggerGeom.h"
85
86 #ifndef __sgi
87 #define _ABS(x) ((x) < 0 ? -(x) : (x))
88 #endif // !__sgi
89
90
91 SO_KIT_SOURCE(SoTabPlaneDragger);
92
93 ////////////////////////////////////////////////////////////////////////
94 //
95 // Description:
96 // Constructors. Each constructor calls constructorSub(), which
97 // does work that is common to each.
98 //
99 // Use: public
100 //
101 ////////////////////////////////////////////////////////////////////////
102
103 // Default constructor.
104 SoTabPlaneDragger::SoTabPlaneDragger()
105 {
106 SO_KIT_CONSTRUCTOR(SoTabPlaneDragger);
107
108 isBuiltIn = TRUE;
109
110 SO_KIT_ADD_CATALOG_ENTRY( planeSwitch, SoSwitch, TRUE,
111 geomSeparator, , FALSE );
112 SO_KIT_ADD_CATALOG_ENTRY( translator, SoSeparator, TRUE,
113 planeSwitch, , TRUE );
114 SO_KIT_ADD_CATALOG_ENTRY( scaleTabs, SoSeparator, TRUE,
115 planeSwitch, , FALSE );
116 SO_KIT_ADD_CATALOG_ENTRY( scaleTabMaterial, SoMaterial, TRUE,
117 scaleTabs, , TRUE );
118 SO_KIT_ADD_CATALOG_ENTRY( scaleTabHints, SoShapeHints, TRUE,
119 scaleTabs, , TRUE );
120 SO_KIT_ADD_CATALOG_ENTRY( scaleTabMaterialBinding, SoMaterialBinding, TRUE,
121 scaleTabs, , FALSE );
122 SO_KIT_ADD_CATALOG_ENTRY( scaleTabNormalBinding, SoNormalBinding, TRUE,
123 scaleTabs, , FALSE );
124 SO_KIT_ADD_CATALOG_ENTRY( scaleTabNormal, SoNormal, TRUE,
125 scaleTabs, , FALSE );
126 SO_KIT_ADD_CATALOG_ENTRY( edgeScaleCoords, SoCoordinate3, TRUE,
127 scaleTabs, , FALSE );
128 SO_KIT_ADD_CATALOG_ENTRY( edgeScaleTab0, SoIndexedFaceSet, TRUE,
129 scaleTabs, , FALSE );
130 SO_KIT_ADD_CATALOG_ENTRY( edgeScaleTab1, SoIndexedFaceSet, TRUE,
131 scaleTabs, , FALSE );
132 SO_KIT_ADD_CATALOG_ENTRY( edgeScaleTab2, SoIndexedFaceSet, TRUE,
133 scaleTabs, , FALSE );
134 SO_KIT_ADD_CATALOG_ENTRY( edgeScaleTab3, SoIndexedFaceSet, TRUE,
135 scaleTabs, , FALSE );
136 SO_KIT_ADD_CATALOG_ENTRY( cornerScaleCoords, SoCoordinate3, TRUE,
137 scaleTabs, , FALSE );
138 SO_KIT_ADD_CATALOG_ENTRY( cornerScaleTab0, SoIndexedFaceSet, TRUE,
139 scaleTabs, , FALSE );
140 SO_KIT_ADD_CATALOG_ENTRY( cornerScaleTab1, SoIndexedFaceSet, TRUE,
141 scaleTabs, , FALSE );
142 SO_KIT_ADD_CATALOG_ENTRY( cornerScaleTab2, SoIndexedFaceSet, TRUE,
143 scaleTabs, , FALSE );
144 SO_KIT_ADD_CATALOG_ENTRY( cornerScaleTab3, SoIndexedFaceSet, TRUE,
145 scaleTabs, , FALSE );
146
147 if (SO_KIT_IS_FIRST_INSTANCE())
148 readDefaultParts("tabPlaneDragger.iv", geomBuffer, sizeof(geomBuffer));
149
150 SO_KIT_ADD_FIELD(translation, (0.0, 0.0, 0.0));
151 SO_KIT_ADD_FIELD(scaleFactor, (1.0, 1.0, 1.0));
152
153 SO_KIT_INIT_INSTANCE();
154
155 // Create All the parts...
156
157 setPartAsDefault("translator", "tabPlaneTranslator");
158 setPartAsDefault("scaleTabMaterial",
159 "tabPlaneScaleTabMaterial");
160 setPartAsDefault("scaleTabHints", "tabPlaneScaleTabHints");
161
162 // Initialize the materialBinding part
163 SoMaterialBinding *mb = new SoMaterialBinding;
164 mb->value = SoMaterialBinding::OVERALL;
165 setAnyPartAsDefault("scaleTabMaterialBinding", mb );
166
167 // Initialize the normalBinding part
168 SoNormalBinding *nb = new SoNormalBinding;
169 nb->value = SoNormalBinding::OVERALL;
170 setAnyPartAsDefault("scaleTabNormalBinding", nb );
171
172 // Initialize the normal part
173 SoNormal *nrm = new SoNormal;
174 nrm->vector = SbVec3f( 0, 0, 1);
175 setAnyPartAsDefault("scaleTabNormal", nrm );
176
177 // Initialize the coordinate nodes.
178 SbVec3f *pts;
179 SoCoordinate3 *crdn;
180
181 crdn = SO_GET_ANY_PART(this,"edgeScaleCoords", SoCoordinate3);
182 crdn->point.setNum(16);
183 pts = crdn->point.startEditing();
184 pts[0].setValue( -.1, .9, .002);
185 pts[1].setValue( .1, .9, .002);
186 pts[2].setValue( .1, 1, .002);
187 pts[3].setValue( -.1, 1, .002);
188 pts[4].setValue( -.1, -1, .002);
189 pts[5].setValue( .1, -1, .002);
190 pts[6].setValue( .1, -.9, .002);
191 pts[7].setValue( -.1, -.9, .002);
192 pts[8].setValue( .9, -.1, .002);
193 pts[9].setValue( 1, -.1, .002);
194 pts[10].setValue( 1, .1, .002);
195 pts[11].setValue( .9, .1, .002);
196 pts[12].setValue(-1, -.1, .002);
197 pts[13].setValue( -.9, -.1, .002);
198 pts[14].setValue( -.9, .1, .002);
199 pts[15].setValue(-1, .1, .002);
200 crdn->point.finishEditing();
201
202 crdn = SO_GET_ANY_PART(this,"cornerScaleCoords", SoCoordinate3);
203 crdn->point.setNum(16);
204 pts = crdn->point.startEditing();
205 pts[0].setValue( .9, .9, .002);
206 pts[1].setValue( 1, .9, .002);
207 pts[2].setValue( 1, 1, .002);
208 pts[3].setValue( .9, 1, .002);
209 pts[4].setValue( .9, -1, .002);
210 pts[5].setValue( 1, -1, .002);
211 pts[6].setValue( 1, -.9, .002);
212 pts[7].setValue( .9, -.9, .002);
213 pts[8].setValue( -1, -1, .002);
214 pts[9].setValue( -.9, -1, .002);
215 pts[10].setValue( -.9, -.9, .002);
216 pts[11].setValue( -1, -.9, .002);
217 pts[12].setValue( -1, .9, .002);
218 pts[13].setValue( -.9, .9, .002);
219 pts[14].setValue( -.9, 1, .002);
220 pts[15].setValue( -1, 1, .002);
221 crdn->point.finishEditing();
222
223 // Initialize the indexed face set nodes
224 int32_t *inds;
225 SoIndexedFaceSet *fsn;
226
227 fsn = new SoIndexedFaceSet;
228 fsn->coordIndex.setNum(5);
229 inds = fsn->coordIndex.startEditing();
230 inds[0] = 0;
231 inds[1] = 1;
232 inds[2] = 2;
233 inds[3] = 3;
234 inds[4] = -1;
235 fsn->coordIndex.finishEditing();
236 setAnyPartAsDefault("edgeScaleTab0", fsn );
237 setAnyPartAsDefault("cornerScaleTab0", fsn->copy() );
238
239 fsn = new SoIndexedFaceSet;
240 fsn->coordIndex.setNum(5);
241 inds = fsn->coordIndex.startEditing();
242 inds[0] = 4;
243 inds[1] = 5;
244 inds[2] = 6;
245 inds[3] = 7;
246 inds[4] = -1;
247 fsn->coordIndex.finishEditing();
248 setAnyPartAsDefault("edgeScaleTab1", fsn );
249 setAnyPartAsDefault("cornerScaleTab1", fsn->copy() );
250
251 fsn = new SoIndexedFaceSet;
252 fsn->coordIndex.setNum(5);
253 inds = fsn->coordIndex.startEditing();
254 inds[0] = 8;
255 inds[1] = 9;
256 inds[2] = 10;
257 inds[3] = 11;
258 inds[4] = -1;
259 fsn->coordIndex.finishEditing();
260 setAnyPartAsDefault("edgeScaleTab2", fsn );
261 setAnyPartAsDefault("cornerScaleTab2", fsn->copy() );
262
263 fsn = new SoIndexedFaceSet;
264 fsn->coordIndex.setNum(5);
265 inds = fsn->coordIndex.startEditing();
266 inds[0] = 12;
267 inds[1] = 13;
268 inds[2] = 14;
269 inds[3] = 15;
270 inds[4] = -1;
271 fsn->coordIndex.finishEditing();
272 setAnyPartAsDefault("edgeScaleTab3", fsn );
273 setAnyPartAsDefault("cornerScaleTab3", fsn->copy() );
274
275
276 // Set the switch to display everything under it...
277 SoSwitch *sw = SO_GET_ANY_PART(this,"planeSwitch",SoSwitch);
278 setSwitchValue( sw, SO_SWITCH_ALL);
279
280 // Create the projectors
281 lineProj = new SbLineProjector();
282 planeProj = new SbPlaneProjector();
283
284 // add the callbacks to perform the dragging
285 addStartCallback( &SoTabPlaneDragger::startCB );
286 addMotionCallback( &SoTabPlaneDragger::motionCB );
287 addFinishCallback( &SoTabPlaneDragger::finishCB );
288
289 // add the callback to update things each time a meta key changes.
290 addOtherEventCallback( &SoTabPlaneDragger::metaKeyChangeCB );
291
292 // Updates the translation and scale fields when the motionMatrix is set.
293 addValueChangedCallback( &SoTabPlaneDragger::valueChangedCB );
294
295 // Updates the motionMatrix when the translationFactor field is set.
296 translFieldSensor
297 = new SoFieldSensor( &SoTabPlaneDragger::fieldSensorCB, this);
298 translFieldSensor->setPriority( 0 );
299
300 // Updates the motionMatrix when the scaleFactor field is set.
301 scaleFieldSensor
302 = new SoFieldSensor( &SoTabPlaneDragger::fieldSensorCB, this);
303 scaleFieldSensor->setPriority( 0 );
304
305 currentState = INACTIVE;
306 restartState = INACTIVE;
307 adjustScaleTabSize();
308
309 setUpConnections( TRUE, TRUE );
310 }
311
312 ////////////////////////////////////////////////////////////////////////
313 //
314 // Description:
315 // Destructor
316 //
317 SoTabPlaneDragger::~SoTabPlaneDragger()
318 //
319 ////////////////////////////////////////////////////////////////////////
320 {
321 delete lineProj;
322 delete planeProj;
323
324 if (translFieldSensor)
325 delete translFieldSensor;
326 if (scaleFieldSensor)
327 delete scaleFieldSensor;
328 }
329
330 // detach/attach any sensors, callbacks, and/or field connections.
331 // Called by: start/end of SoBaseKit::readInstance
332 // and on new copy by: start/end of SoBaseKit::copy.
333 // Classes that redefine must call setUpConnections(TRUE,TRUE)
334 // at end of constructor.
335 // Returns the state of the node when this was called.
336 SbBool
337 SoTabPlaneDragger::setUpConnections( SbBool onOff, SbBool doItAlways )
338 {
339 if ( !doItAlways && connectionsSetUp == onOff)
340 return onOff;
341
342 if ( onOff ) {
343
344 // We connect AFTER base class.
345 SoDragger::setUpConnections( onOff, FALSE );
346
347 // Call the sensor CBs to make things are up-to-date.
348 fieldSensorCB( this, NULL );
349
350 // Connect the field sensors
351 if (translFieldSensor->getAttachedField() != &translation)
352 translFieldSensor->attach( &translation );
353 if (scaleFieldSensor->getAttachedField() != &scaleFactor)
354 scaleFieldSensor->attach( &scaleFactor );
355 }
356 else {
357
358 // We disconnect BEFORE base class.
359
360 // Disconnect the field sensors.
361 if (translFieldSensor->getAttachedField())
362 translFieldSensor->detach();
363 if (scaleFieldSensor->getAttachedField())
364 scaleFieldSensor->detach();
365
366 SoDragger::setUpConnections( onOff, FALSE );
367 }
368
369 return !(connectionsSetUp = onOff);
370 }
371
372 ////////////////////////////////////////////////////////////////////////
373 //
374 // Description:
375 // Move the tabPlaneDragger based on mouse motion.
376 //
377 // Use: private
378 //
379 void
380 SoTabPlaneDragger::dragStart()
381 //
382 ////////////////////////////////////////////////////////////////////////
383 {
384 const SoPath *pickPath = getPickPath();
385
386 currentState = INACTIVE;
387 shftDown = getEvent()->wasShiftDown();
388
389 if (restartState != INACTIVE ) {
390 currentState = restartState;
391 }
392 else if (pickPath && pickPath->containsNode(translator.getValue()) ||
393 getSurrogatePartPickedName() == "translator" ) {
394 currentState = TRANSLATING;
395 }
396 else if ( pickPath && pickPath->containsNode( scaleTabs.getValue() )) {
397
398 if (pickPath->containsNode( edgeScaleTab0.getValue()) ) {
399 currentScalePatch = 0;
400 currentState = EDGE_SCALING;
401 }
402 else if (pickPath->containsNode( edgeScaleTab1.getValue()) ) {
403 currentScalePatch = 1;
404 currentState = EDGE_SCALING;
405 }
406 else if (pickPath->containsNode( edgeScaleTab2.getValue()) ) {
407 currentScalePatch = 2;
408 currentState = EDGE_SCALING;
409 }
410 else if (pickPath->containsNode( edgeScaleTab3.getValue()) ) {
411 currentScalePatch = 3;
412 currentState = EDGE_SCALING;
413 }
414 else if (pickPath->containsNode( cornerScaleTab0.getValue()) ) {
415 currentScalePatch = 0;
416 currentState = CORNER_SCALING;
417 }
418 else if (pickPath->containsNode( cornerScaleTab1.getValue()) ) {
419 currentScalePatch = 1;
420 currentState = CORNER_SCALING;
421 }
422 else if (pickPath->containsNode( cornerScaleTab2.getValue()) ) {
423 currentScalePatch = 2;
424 currentState = CORNER_SCALING;
425 }
426 else if (pickPath->containsNode( cornerScaleTab3.getValue()) ) {
427 currentScalePatch = 3;
428 currentState = CORNER_SCALING;
429 }
430
431 if ( currentState == CORNER_SCALING && shftDown )
432 currentState = UNIFORM_SCALING;
433 }
434
435 if (currentState == TRANSLATING )
436 translateStart();
437 else if (currentState == EDGE_SCALING )
438 edgeScaleStart();
439 else if (currentState == CORNER_SCALING )
440 cornerScaleStart();
441 else if (currentState == UNIFORM_SCALING )
442 scaleUniformStart();
443 }
444
445 ////////////////////////////////////////////////////////////////////////
446 //
447 // Description:
448 //
449 // Use: private
450 //
451 void
452 SoTabPlaneDragger::drag()
453 //
454 ////////////////////////////////////////////////////////////////////////
455 {
456 if ( currentState == TRANSLATING )
457 translateDrag();
458 else if ( currentState == EDGE_SCALING )
459 edgeScaleDrag();
460 else if ( currentState == CORNER_SCALING )
461 cornerScaleDrag();
462 else if ( currentState == UNIFORM_SCALING )
463 scaleUniformDrag();
464 }
465
466 ////////////////////////////////////////////////////////////////////////
467 //
468 // Description:
469 //
470 // Use: private
471 //
472 void
473 SoTabPlaneDragger::dragFinish()
474 //
475 ////////////////////////////////////////////////////////////////////////
476 {
477 // turn off highlighting and adjust scale tabs
478 currentState = INACTIVE;
479 restartState = INACTIVE;
480 adjustScaleTabSize();
481 }
482
483 ////////////////////////////////////////////////////////////////////////
484 //
485 // Description:
486 // Initializes for motion
487 //
488 void
489 SoTabPlaneDragger::translateStart()
490 //
491 ////////////////////////////////////////////////////////////////////////
492 {
493 // This is the point we'll use if a metaKey callback makes us re-start.
494 worldRestartPt = getWorldStartingPoint();
495
496 // Establish the projector line in local space.
497 SbVec3f startLocalHitPt = getLocalStartingPoint();
498
499 SbVec3f planeNormal(0,0,1);
500 planeProj->setPlane( SbPlane( planeNormal, startLocalHitPt ) );
501
502 // as of yet, translation direction (for 1-D translating) is undefined.
503 translateDir = -1;
504 }
505
506 ////////////////////////////////////////////////////////////////////////
507 //
508 void
509 SoTabPlaneDragger::translateDrag()
510 //
511 ////////////////////////////////////////////////////////////////////////
512 {
513 if (shftDown && ! isAdequateConstraintMotion())
514 return;
515
516 // Get startHitPt in local space.
517 SbVec3f startHitPt = getLocalStartingPoint();;
518
519 // Calculate intersection with plane projector.
520 // Find mouse hit on plane currently in use...
521 planeProj->setViewVolume( getViewVolume() );
522 planeProj->setWorkingSpace( getLocalToWorldMatrix() );
523 SbVec3f newHitPt
524 = planeProj->project(getNormalizedLocaterPosition());
525
526 // If we need to start a new gesture, we'll carry on from this pt.
527 getLocalToWorldMatrix().multVecMatrix( newHitPt, worldRestartPt);
528
529 // find the difference between current and previous intersections.
530 SbVec3f localMotion = newHitPt - startHitPt;
531
532 if ( !shftDown )
533 // constrainKey not down. Clear the 1-D translation dir.
534 translateDir = -1;
535 else {
536 // the constrain key is pressed. This means 1-D translation.
537 if ( translateDir == -1 ) {
538 // The 1-D direction is not defined. Calculate it
539 // based on which direction got the maximum locater motion.
540 if ( _ABS( localMotion[0]) > _ABS( localMotion[1])
541 && _ABS( localMotion[0]) > _ABS( localMotion[2]) )
542 translateDir = 0;
543 else if ( _ABS( localMotion[1]) > _ABS( localMotion[2]) )
544 translateDir = 1;
545 else
546 translateDir = 2;
547 }
548
549 // get the projection of 'localMotion' onto the preferred axis.
550 SbVec3f constrainedMotion(0,0,0);
551 constrainedMotion[translateDir] = localMotion[translateDir];
552 localMotion = constrainedMotion;
553 }
554
555 // Append this to the startMotionMatrix, which we saved at the beginning
556 // of the drag, to find the current motion matrix.
557 setMotionMatrix(appendTranslation(getStartMotionMatrix(),localMotion));
558
559 return;
560 }
561
562 ////////////////////////////////////////////////////////////////////////
563 //
564 void
565 SoTabPlaneDragger::edgeScaleStart()
566 //
567 ////////////////////////////////////////////////////////////////////////
568 {
569 // This is the point we'll use if a metaKey callback makes us re-start.
570 worldRestartPt = getWorldStartingPoint();
571
572 // Setup scale center and direction for projector line.
573 SbVec3f lineDir;
574 switch (currentScalePatch) {
575 case 0:
576 scaleCenter.setValue( 0, -1, 0 );
577 lineDir.setValue( 0, 1, 0);
578 break;
579 case 1:
580 scaleCenter.setValue( 0, 1, 0 );
581 lineDir.setValue( 0, 1, 0);
582 break;
583 case 2:
584 scaleCenter.setValue( -1, 0, 0 );
585 lineDir.setValue( 1, 0, 0);
586 break;
587 case 3:
588 scaleCenter.setValue( 1, 0, 0 );
589 lineDir.setValue( 1, 0, 0);
590 break;
591 }
592
593 // Establish the projector line
594 SbVec3f startLocalHitPt = getLocalStartingPoint();
595
596 lineProj->setLine( SbLine(startLocalHitPt, startLocalHitPt + lineDir));
597 }
598
599 ////////////////////////////////////////////////////////////////////////
600 void
601 SoTabPlaneDragger::edgeScaleDrag()
602 //
603 ////////////////////////////////////////////////////////////////////////
604 {
605 lineProj->setViewVolume( getViewVolume() );
606 lineProj->setWorkingSpace( getLocalToWorldMatrix() );
607
608 SbVec3f startHitPt = getLocalStartingPoint();
609
610 SbVec3f newHitPt = lineProj->project( getNormalizedLocaterPosition());
611 // If we need to start a new gesture, we'll carry on from this pt.
612 getLocalToWorldMatrix().multVecMatrix( newHitPt, worldRestartPt);
613
614 // Figure out 'delta', the amount to scale by.
615 // To do this:
616 // [1] the line we are really supposed to scale along passes through
617 // scaleCenter in the direction of our projection line.
618 // Drop a perpendicular from 'scaleCenter' to our projection line
619 // to find the origin to use in calculating change of scale.
620 SbVec3f projectedScaleCenter
621 = lineProj->getLine().getClosestPoint(scaleCenter);
622
623 // [2] Find the distance from the old and new projected points to this
624 // center.
625 SbVec3f oldDiff = startHitPt - projectedScaleCenter;
626 SbVec3f newDiff = newHitPt - projectedScaleCenter;
627
628 float oldDist = oldDiff.length();
629 float newDist = newDiff.length();
630
631 if ( oldDiff.dot( newDiff ) < 0.0 )
632 newDist *= -1.0;
633
634 // [3] Change in scale is the ratio of newDist to oldDist
635 #define TINY 0.0001
636 float delta = ( fabs(oldDist) < TINY || fabs(newDist) < TINY)
637 ? 1.0 : newDist / oldDist;
638 #undef TINY
639
640 // Constrain the scaling to be greater than getMinScale();
641 if (delta < getMinScale() )
642 delta = getMinScale();
643
644 // Get the scale we want to apply. It's 'delta' in the direction of our
645 // projector line and 1 in the other directions
646 SbVec3f newScaling(1,1,1);
647 SbVec3f lineDir = lineProj->getLine().getDirection();
648 for (int i = 0; i < 3; i++ )
649 if (lineDir[i] == 1.0)
650 newScaling[i] = delta;
651
652 // append this to the startMotionMatrix.
653 setMotionMatrix(appendScale(getStartMotionMatrix(),newScaling,scaleCenter));
654 }
655
656
657 ////////////////////////////////////////////////////////////////////////
658 //
659 void
660 SoTabPlaneDragger::cornerScaleStart()
661 //
662 ////////////////////////////////////////////////////////////////////////
663 {
664 // This is the point we'll use if a metaKey callback makes us re-start.
665 worldRestartPt = getWorldStartingPoint();
666
667 // Setup scale center
668 switch (currentScalePatch) {
669 case 0: scaleCenter.setValue( -1, -1, 0 ); break;
670 case 1: scaleCenter.setValue( -1, 1, 0 ); break;
671 case 2: scaleCenter.setValue( 1, 1, 0 ); break;
672 case 3: scaleCenter.setValue( 1, -1, 0 ); break;
673 }
674
675 SbVec3f startLocalHitPt = getLocalStartingPoint();
676
677 // 2-D scaling motion within plane. Establish the projector plane
678 SbVec3f planeNormal(0,0,1);
679 planeProj->setPlane( SbPlane( planeNormal, startLocalHitPt ) );
680 }
681
682 ////////////////////////////////////////////////////////////////////////
683 void
684 SoTabPlaneDragger::cornerScaleDrag()
685 //
686 ////////////////////////////////////////////////////////////////////////
687 {
688 planeProj->setViewVolume( getViewVolume() );
689 planeProj->setWorkingSpace( getLocalToWorldMatrix() );
690
691 SbVec3f startHitPt = getLocalStartingPoint();
692
693 SbVec3f newHitPt = planeProj->project( getNormalizedLocaterPosition());
694 // If we need to start a new gesture, we'll carry on from this pt.
695 getLocalToWorldMatrix().multVecMatrix( newHitPt, worldRestartPt);
696
697 // Figure out 'delta', the amount to scale by.
698 // To do this:
699 // [1] the plane we are really supposed to scale in passes through
700 // scaleCenter with normal of zAxis
701 // Drop perpendicular from 'scaleCenter' to projection plane
702 // to find the origin to use in calculating change of scale.
703 SbVec3f projectedScaleCenter;
704 SbLine lineThroughCenter( scaleCenter, scaleCenter + SbVec3f(0,0,1) );
705 planeProj->getPlane().intersect(lineThroughCenter,projectedScaleCenter);
706
707 // [2] Now find the scale change in each of the x and y directions.
708 // Start by projecting each newHitPt's offset onto the two axes.
709 SbVec3f oldDiff = startHitPt - projectedScaleCenter;
710 SbVec3f newDiff = newHitPt - projectedScaleCenter;
711
712 // [3] Change in scale is the ratio of new to old
713 #define TINY 0.0001
714 float xAxisDelta = (_ABS(newDiff[0]) < TINY || _ABS(oldDiff[0])
715 < TINY) ? 1.0 : newDiff[0] / oldDiff[0];
716 float yAxisDelta = (_ABS(newDiff[1]) < TINY || _ABS(oldDiff[1])
717 < TINY) ? 1.0 : newDiff[1] / oldDiff[1];
718 #undef TINY
719
720 // Make sure we didn't fall below minscale
721 if ( xAxisDelta < getMinScale() )
722 xAxisDelta = getMinScale();
723 if ( yAxisDelta < getMinScale() )
724 yAxisDelta = getMinScale();
725
726 SbVec3f scaleDelta( xAxisDelta, yAxisDelta, 1 );
727
728
729 setMotionMatrix( appendScale( getStartMotionMatrix(),
730 scaleDelta, scaleCenter ) );
731 }
732
733 ////////////////////////////////////////////////////////////////////////
734 //
735 void
736 SoTabPlaneDragger::scaleUniformStart()
737 //
738 ////////////////////////////////////////////////////////////////////////
739 {
740 // This is the point we'll use if a metaKey callback makes us re-start.
741 worldRestartPt = getWorldStartingPoint();
742
743 // We wish to scale in 2 dimensions about the corner opposite
744 // the patch that we have hit.
745 switch (currentScalePatch) {
746 case 0:
747 scaleCenter.setValue( -1, -1, 0);
748 break;
749 case 1:
750 scaleCenter.setValue( -1, 1, 0);
751 break;
752 case 2:
753 scaleCenter.setValue( 1, 1, 0);
754 break;
755 case 3:
756 scaleCenter.setValue( 1, -1, 0);
757 break;
758 }
759
760 // Find the projection line.
761 // This line should lie within the same local z-plane
762 // as the startLocalHitPt.
763 // It should pass through the startLocalHitPt.
764 // It should also pass through the point where the scaleCenter
765 // projects onto the same z-plane as the startLocalHitPt.
766 SbVec3f startLocalHitPt = getLocalStartingPoint();
767 SbVec3f zPlaneCenter( scaleCenter[0], scaleCenter[1], startLocalHitPt[2]);
768 lineProj->setLine( SbLine( startLocalHitPt, zPlaneCenter ) );
769 }
770
771 ////////////////////////////////////////////////////////////////////////
772 void
773 SoTabPlaneDragger::scaleUniformDrag()
774 //
775 ////////////////////////////////////////////////////////////////////////
776 {
777 lineProj->setViewVolume( getViewVolume() );
778 lineProj->setWorkingSpace( getLocalToWorldMatrix() );
779
780 SbVec3f startHitPt = getLocalStartingPoint();
781
782 SbVec3f newHitPt = lineProj->project( getNormalizedLocaterPosition());
783 // If we need to start a new gesture, we'll carry on from this pt.
784 getLocalToWorldMatrix().multVecMatrix( newHitPt, worldRestartPt);
785
786 // [1] Find distance from old and new projected points to projectedCenter
787 // projectedCenter is zero-scale point on the lineProj.
788 SbVec3f projectedCenter = lineProj->getLine().getClosestPoint(scaleCenter);
789 SbVec3f oldDiff = startHitPt - projectedCenter;
790 SbVec3f newDiff = newHitPt - projectedCenter;
791
792 float oldDist = oldDiff.length();
793 float newDist = newDiff.length();
794
795 // [2] Change in scale is ratio of newDist to oldDist
796 #define TINY 0.0001
797 float delta =(oldDist < TINY || newDist < TINY) ? 1.0 : newDist/oldDist;
798 #undef TINY
799
800 // [3] Check if sign of scale changed.
801 if (oldDiff.dot(newDiff) < 0 )
802 delta *= -1.0;
803
804 if ( delta < getMinScale() )
805 delta = getMinScale();
806
807 SbVec3f scaleDelta( delta, delta, delta );
808
809 setMotionMatrix( appendScale( getStartMotionMatrix(),
810 scaleDelta, scaleCenter ));
811 }
812
813 ////////////////////////////////////////////////////////////////////////
814 //
815 // Description:
816 // In addition to usual rendering, this dragger
817 // turns off the faces of the dragger that face away from the camera.
818 void
819 SoTabPlaneDragger::GLRender( SoGLRenderAction *action )
820 //
821 ////////////////////////////////////////////////////////////////////////
822 {
823 if ( needScaleTabAdjustment == TRUE)
824 reallyAdjustScaleTabSize(action);
825
826 SoDragger::GLRender( action );
827 }
828
829 ////////////////////////////////////////////////////////////////////////
830 //
831 // Description:
832 // Adjusts the size of the scale tabs to be something reasonable in
833 // screen space.
834 //
835 ////////////////////////////////////////////////////////////////////////
836
837 #define SCREEN_TAB_SIZE 8
838
839 void
840 SoTabPlaneDragger::adjustScaleTabSize()
841 {
842 needScaleTabAdjustment = TRUE;
843 // This forces a redraw, so any caches containing this will be broken
844 touch();
845 }
846
847 void
848 SoTabPlaneDragger::reallyAdjustScaleTabSize(SoGLRenderAction *action)
849 {
850 // If the viewport size is still (1,1) or (0,0) then the
851 // Window we are in has not yet been mapped and realized.
852 // So we need to just return and wait until next time.
853 if ( action == NULL )
854 return;
855 SoState *state = action->getState();
856 const SbVec2s &vpSize
857 = SoViewportRegionElement::get(state).getViewportSizePixels();
858 if (vpSize[0] < 2 || vpSize[1] < 2)
859 return;
860
861 needScaleTabAdjustment = FALSE;
862
863 setCameraInfo( action );
864 setTempPathToThis( action->getCurPath() );
865
866 SbMatrix localToWorld = getLocalToWorldMatrix();
867 SbMatrix viewMat = SoViewingMatrixElement::get(action->getState());
868 SbMatrix viewMatInv = viewMat.inverse();
869 SbMatrix projMat = SoProjectionMatrixElement::get(action->getState());
870 SbMatrix lclToScr = localToWorld * viewMat * projMat;
871
872 // This will give the length of the x and y axes,
873 // each axis going from -1 to +1.
874 SbVec2f screenLengths;
875 getXYScreenLengths(screenLengths,
876 lclToScr, getViewportRegion().getViewportSizePixels());
877
878 //
879 // Here's how big the tabs will be, in local space.
880 // The 2.0 is because the box
881 // is of width 2.
882 // Each dimension is considered separately, because they will almost
883 // always be different sizes on screen.
884 //
885 SbVec2f localTabSize(2.0 * SCREEN_TAB_SIZE/screenLengths[0],
886 2.0 * SCREEN_TAB_SIZE/screenLengths[1]);
887
888 SbVec2f innerEdgeDist;
889 innerEdgeDist[0] = 1.0 - localTabSize[0];
890 innerEdgeDist[1] = 1.0 - localTabSize[1];
891
892 SbVec2f outerEdgeDist(1.0, 1.0);
893
894 SbVec2f halfTabWidth;
895 halfTabWidth[0] = (localTabSize[0]/2.0);
896 halfTabWidth[1] = (localTabSize[1]/2.0);
897
898 // Be sure to keep vertices in counter-clockwise order. For each
899 // group of four, they go in order: 3 2
900 // when front-facing 0 1
901
902 // use a small z value to move points off of cube faces...
903 const float z = .002;
904
905 SoCoordinate3 *esc = (SoCoordinate3 *) edgeScaleCoords.getValue();
906 if ( esc != NULL ) {
907
908 // Make sure there is enough room in the coordinate node.
909 esc->point.setNum( 16 );
910 SbVec3f *pts = esc->point.startEditing();
911
912 pts[0].setValue( -halfTabWidth[0], innerEdgeDist[1], z);
913 pts[1].setValue( halfTabWidth[0], innerEdgeDist[1], z);
914 pts[2].setValue( halfTabWidth[0], outerEdgeDist[1], z);
915 pts[3].setValue( -halfTabWidth[0], outerEdgeDist[1], z);
916
917 pts[4].setValue( -halfTabWidth[0], -outerEdgeDist[1], z);
918 pts[5].setValue( halfTabWidth[0], -outerEdgeDist[1], z);
919 pts[6].setValue( halfTabWidth[0], -innerEdgeDist[1], z);
920 pts[7].setValue( -halfTabWidth[0], -innerEdgeDist[1], z);
921
922 pts[8].setValue( innerEdgeDist[0], -halfTabWidth[1], z);
923 pts[9].setValue( outerEdgeDist[0], -halfTabWidth[1], z);
924 pts[10].setValue( outerEdgeDist[0], halfTabWidth[1], z);
925 pts[11].setValue( innerEdgeDist[0], halfTabWidth[1], z);
926
927 pts[12].setValue( -outerEdgeDist[0], -halfTabWidth[1], z);
928 pts[13].setValue( -innerEdgeDist[0], -halfTabWidth[1], z);
929 pts[14].setValue( -innerEdgeDist[0], halfTabWidth[1], z);
930 pts[15].setValue( -outerEdgeDist[0], halfTabWidth[1], z);
931
932 esc->point.finishEditing();
933 }
934
935 SoCoordinate3 *csc = (SoCoordinate3 *) cornerScaleCoords.getValue();
936 if ( csc != NULL ) {
937
938 // Make sure there is enough room in the coordinate node.
939 csc->point.setNum( 16 );
940 SbVec3f *pts = csc->point.startEditing();
941
942 pts[0].setValue( innerEdgeDist[0], innerEdgeDist[1], z);
943 pts[1].setValue( outerEdgeDist[0], innerEdgeDist[1], z);
944 pts[2].setValue( outerEdgeDist[0], outerEdgeDist[1], z);
945 pts[3].setValue( innerEdgeDist[0], outerEdgeDist[1], z);
946
947 pts[4].setValue( innerEdgeDist[0], -outerEdgeDist[1], z);
948 pts[5].setValue( outerEdgeDist[0], -outerEdgeDist[1], z);
949 pts[6].setValue( outerEdgeDist[0], -innerEdgeDist[1], z);
950 pts[7].setValue( innerEdgeDist[0], -innerEdgeDist[1], z);
951
952 pts[8].setValue( -outerEdgeDist[0], -outerEdgeDist[1], z);
953 pts[9].setValue( -innerEdgeDist[0], -outerEdgeDist[1], z);
954 pts[10].setValue(-innerEdgeDist[0], -innerEdgeDist[1], z);
955 pts[11].setValue(-outerEdgeDist[0], -innerEdgeDist[1], z);
956
957 pts[12].setValue(-outerEdgeDist[0], innerEdgeDist[1], z);
958 pts[13].setValue(-innerEdgeDist[0], innerEdgeDist[1], z);
959 pts[14].setValue(-innerEdgeDist[0], outerEdgeDist[1], z);
960 pts[15].setValue(-outerEdgeDist[0], outerEdgeDist[1], z);
961
962 csc->point.finishEditing();
963 }
964 }
965
966 ////////////////////////////////////////////////////////////////////
967 // Stubs for callbacks
968 ////////////////////////////////////////////////////////////////////
969 void
970 SoTabPlaneDragger::startCB( void *, SoDragger *inDragger )
971 {
972 SoTabPlaneDragger *dl = (SoTabPlaneDragger *) inDragger;
973 dl->dragStart();
974 }
975
976 void
977 SoTabPlaneDragger::motionCB( void *, SoDragger *inDragger )
978 {
979 SoTabPlaneDragger *dl = (SoTabPlaneDragger *) inDragger;
980 dl->drag();
981 }
982
983 void
984 SoTabPlaneDragger::finishCB( void *, SoDragger *inDragger )
985 {
986 SoTabPlaneDragger *dl = (SoTabPlaneDragger *) inDragger;
987 dl->dragFinish();
988 }
989
990 //
991 // For each of the two primary axes, x and y, see how int32_t the segment is
992 // that goes from -1 to 1.
993 //
994 void
995 SoTabPlaneDragger::getXYScreenLengths(SbVec2f &lengths,
996 const SbMatrix &localToScreen, const SbVec2s &winSize)
997 {
998 // Set the values of the endpoints in local space.
999 SbVec3f plusX(1,0,0), minusX(-1, 0,0);
1000 SbVec3f plusY(0,1,0), minusY( 0,-1,0);
1001
1002 // Transform them into screen space.
1003 localToScreen.multVecMatrix( plusX, plusX );
1004 localToScreen.multVecMatrix( minusX, minusX );
1005 localToScreen.multVecMatrix( plusY, plusY );
1006 localToScreen.multVecMatrix( minusY, minusY );
1007
1008 // These results are in -1 to 1 space.
1009 // Get them in 0 to 1 space.
1010 plusX[0] = 0.5 * (plusX[0] + 1.0);
1011 plusX[1] = 0.5 * (plusX[1] + 1.0);
1012 minusX[0] = 0.5 * (minusX[0] + 1.0);
1013 minusX[1] = 0.5 * (minusX[1] + 1.0);
1014
1015 plusY[0] = 0.5 * (plusY[0] + 1.0);
1016 plusY[1] = 0.5 * (plusY[1] + 1.0);
1017 minusY[0] = 0.5 * (minusY[0] + 1.0);
1018 minusY[1] = 0.5 * (minusY[1] + 1.0);
1019
1020 // Find square of the length of each transformed segment,
1021 // in pixels.
1022 SbVec2f xAxisPix;
1023 xAxisPix[0] = (plusX[0] - minusX[0]) * winSize[0];
1024 xAxisPix[1] = (plusX[1] - minusX[1]) * winSize[1];
1025
1026 SbVec2f yAxisPix;
1027 yAxisPix[0] = (plusY[0] - minusY[0]) * winSize[0];
1028 yAxisPix[1] = (plusY[1] - minusY[1]) * winSize[1];
1029
1030 lengths[0] = xAxisPix.length();
1031 lengths[1] = yAxisPix.length();
1032 }
1033
1034 //
1035 ////////////////////////////////////////////////////////////////////////
1036 // keypress/release callback functions
1037 //
1038 // These assure that the proper changes to the highlights,
1039 // currentState, and projectors are made
1040 //
1041 ////////////////////////////////////////////////////////////////////////
1042 //
1043
1044 void
1045 SoTabPlaneDragger::metaKeyChangeCB( void *, SoDragger *inDragger)
1046 {
1047 SoTabPlaneDragger *tpd = (SoTabPlaneDragger *) inDragger;
1048
1049 SoHandleEventAction *ha = tpd->getHandleEventAction();
1050
1051 //[1] Only do this if we are grabbing events
1052 if ( ha->getGrabber() != tpd )
1053 return;
1054
1055 //[2] We only want key press or release events.
1056 const SoEvent *event = tpd->getEvent();
1057 if ( !SO_KEY_PRESS_EVENT(event, ANY) && !SO_KEY_RELEASE_EVENT(event, ANY))
1058 return;
1059
1060 //[3] Is the key 'shift'
1061 const SoKeyboardEvent *ke = (const SoKeyboardEvent *) event;
1062 SoKeyboardEvent::Key key = ke->getKey();
1063
1064 if ( ( key == SoKeyboardEvent::LEFT_SHIFT ||
1065 key == SoKeyboardEvent::RIGHT_SHIFT ) &&
1066 ( tpd->currentState == TRANSLATING ||
1067 tpd->currentState == UNIFORM_SCALING ||
1068 tpd->currentState == CORNER_SCALING )) {
1069
1070 // We want to end the old gesture and start a new one.
1071
1072 // [A] Release the grabber. This ends the gesture and calls all
1073 // finishCallbacks (on parent dragger, too, if we're registered)
1074 State savedState = tpd->currentState;
1075 ha->releaseGrabber();
1076
1077 // [B] Set the starting point to be our saved worldRestartPoint
1078 tpd->setStartingPoint( tpd->worldRestartPt );
1079 tpd->restartState = savedState;
1080
1081 // [C] Become the grabber again. This begins a new gesture and calls all
1082 // startCallbacks (parent dragger, too). Info like viewVolume,
1083 // viewportRegion, handleEventAction, and tempPathToThis
1084 // is still valid.
1085 ha->setGrabber(tpd);
1086
1087 // [D] set handled
1088 ha->setHandled();
1089 }
1090 }
1091
1092 void
1093 SoTabPlaneDragger::valueChangedCB( void *, SoDragger *inDragger )
1094 {
1095 SoTabPlaneDragger *tpd = (SoTabPlaneDragger *) inDragger;
1096 SbMatrix motMat = tpd->getMotionMatrix();
1097
1098 SbVec3f trans, scale;
1099 SbRotation rot, scaleOrient;
1100 getTransformFast( motMat, trans, rot, scale, scaleOrient);
1101
1102 // Disconnect the field sensors
1103 tpd->translFieldSensor->detach();
1104 tpd->scaleFieldSensor->detach();
1105
1106 if ( tpd->translation.getValue() != trans )
1107 tpd->translation = trans;
1108 if ( tpd->scaleFactor.getValue() != scale )
1109 tpd->scaleFactor = scale;
1110
1111 // Reconnect the field sensors
1112 tpd->translFieldSensor->attach( &(tpd->translation) );
1113 tpd->scaleFieldSensor->attach( &(tpd->scaleFactor) );
1114 }
1115
1116 void
1117 SoTabPlaneDragger::fieldSensorCB( void *inDragger, SoSensor * )
1118 {
1119 SoTabPlaneDragger *tpd = (SoTabPlaneDragger *) inDragger;
1120
1121 // Incorporate the new field values into the matrix
1122 SbMatrix motMat = tpd->getMotionMatrix();
1123 tpd->workFieldsIntoTransform(motMat);
1124
1125 tpd->setMotionMatrix( motMat );
1126 }
1127
1128
1129 void
1130 SoTabPlaneDragger::setDefaultOnNonWritingFields()
1131 {
1132 // This node may change after construction, but we still
1133 // don't want to write it out.
1134 edgeScaleCoords.setDefault(TRUE);
1135 cornerScaleCoords.setDefault(TRUE);
1136
1137 // Call the base class...
1138 SoDragger::setDefaultOnNonWritingFields();
1139 }