-
Notifications
You must be signed in to change notification settings - Fork 1.4k
/
StandardManipulator.cpp
854 lines (660 loc) · 24.8 KB
/
StandardManipulator.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
/* -*-c++-*- OpenSceneGraph - Copyright (C) 1998-2010 Robert Osfield
*
* This library is open source and may be redistributed and/or modified under
* the terms of the OpenSceneGraph Public License (OSGPL) version 0.0 or
* (at your option) any later version. The full license is in LICENSE file
* included with this distribution, and on the openscenegraph.org website.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* OpenSceneGraph Public License for more details.
*
* StandardManipulator code Copyright (C) 2010 PCJohn (Jan Peciva)
* while some pieces of code were taken from OSG.
* Thanks to company Cadwork (www.cadwork.ch) and
* Brno University of Technology (www.fit.vutbr.cz) for open-sourcing this work.
*/
#include <osgGA/StandardManipulator>
#include <osgViewer/View>
using namespace osg;
using namespace osgGA;
using namespace osgUtil;
int StandardManipulator::numRelativeFlagsAllocated = 0;
int StandardManipulator::allocateRelativeFlag()
{
return numRelativeFlagsAllocated++;
}
/// Constructor.
StandardManipulator::StandardManipulator( int flags )
: inherited(),
_thrown( false ),
_allowThrow( true ),
_mouseCenterX(0.0f), _mouseCenterY(0.0f),
_delta_frame_time(0.01), _last_frame_time(0.0),
_modelSize( 0. ),
_verticalAxisFixed( true ),
_flags( flags ),
_relativeFlags( 0 )
{
}
/// Constructor.
StandardManipulator::StandardManipulator( const StandardManipulator& uim, const CopyOp& copyOp )
: osg::Object(uim, copyOp),
inherited( uim, copyOp ),
_thrown( uim._thrown ),
_allowThrow( uim._allowThrow ),
_mouseCenterX(0.0f), _mouseCenterY(0.0f),
_ga_t1( dynamic_cast< GUIEventAdapter* >( copyOp( uim._ga_t1.get() ) ) ),
_ga_t0( dynamic_cast< GUIEventAdapter* >( copyOp( uim._ga_t0.get() ) ) ),
_delta_frame_time(0.01), _last_frame_time(0.0),
_modelSize( uim._modelSize ),
_verticalAxisFixed( uim._verticalAxisFixed ),
_flags( uim._flags ),
_relativeFlags( uim._relativeFlags )
{
}
/** Attach a node to the manipulator.
Automatically detaches previously attached node.
setNode(NULL) detaches previously attached nodes.
Is ignored by manipulators which do not require a reference model.*/
void StandardManipulator::setNode( Node* node )
{
_node = node;
// update model size
if( _node.get() )
{
const BoundingSphere& boundingSphere = _node->getBound();
_modelSize = boundingSphere.radius();
}
else
{
_modelSize = 0.;
}
// compute home position
if( getAutoComputeHomePosition() )
computeHomePosition( NULL, ( _flags & COMPUTE_HOME_USING_BBOX ) != 0 );
}
/** Return node if attached.*/
const Node* StandardManipulator::getNode() const
{
return _node.get();
}
/** Return node if attached.*/
Node* StandardManipulator::getNode()
{
return _node.get();
}
/** Makes manipulator to keep camera's "UP" vector.
*
* In general, fixed up vector makes camera control more user friendly.
*
* To change up vector, use CameraManipulator::setCoordinateFrameCallback.*/
void StandardManipulator::setVerticalAxisFixed( bool value )
{
_verticalAxisFixed = value;
}
/// Sets manipulator animation time when centering on mouse wheel up is enabled.
void StandardManipulator::setAnimationTime( const double t )
{
if( t <= 0. )
{
finishAnimation();
_animationData = NULL;
return;
}
if( !_animationData )
allocAnimationData();
_animationData->_animationTime = t;
}
/// Returns manipulator animation time when centering on mouse wheel up is enabled.
double StandardManipulator::getAnimationTime() const
{
if( _animationData )
return _animationData->_animationTime;
else
return 0.;
}
/// Returns whether manipulator is performing animation at the moment.
bool StandardManipulator::isAnimating() const
{
if( _animationData )
return _animationData->_isAnimating;
else
return false;
}
/// Finishes the animation by performing a step that moves it to its final position.
void StandardManipulator::finishAnimation()
{
_thrown = false;
if( !isAnimating() )
return;
applyAnimationStep( 1., _animationData->_phase );
}
/** Move the camera to the default position.
The user should probably want to use home(GUIEventAdapter&, GUIActionAdapter&)
instead to set manipulator to the home position. This method does not trigger
any redraw processing or updates continuous update processing.
StandardManipulator implementation only updates its internal structures and
recomputes its home position if autoComputeHomePosition is set.
Descendant classes are expected to update camera position.*/
void StandardManipulator::home( double /*currentTime*/ )
{
if( getAutoComputeHomePosition() )
computeHomePosition( NULL, ( _flags & COMPUTE_HOME_USING_BBOX ) != 0 );
_thrown = false;
setTransformation( _homeEye, _homeCenter, _homeUp );
flushMouseEventStack();
}
/** Move the camera to the default position.
If autoComputeHomePosition is on, home position is computed.
The computation considers camera fov and model size and
positions camera far enough to fit the model to the screen.
StandardManipulator implementation only updates its internal data.
If home position is expected to be supported by the descendant manipulator,
it has to reimplement the method to update manipulator transformation.*/
void StandardManipulator::home( const GUIEventAdapter& /*ea*/, GUIActionAdapter& us )
{
if( getAutoComputeHomePosition() )
{
const Camera *camera = us.asView() ? us.asView()->getCamera() : NULL;
computeHomePosition( camera, ( _flags & COMPUTE_HOME_USING_BBOX ) != 0 );
}
_thrown = false;
setTransformation( _homeEye, _homeCenter, _homeUp );
us.requestRedraw();
us.requestContinuousUpdate( false );
flushMouseEventStack();
}
/** Start/restart the manipulator.*/
void StandardManipulator::init( const GUIEventAdapter& /*ea*/, GUIActionAdapter& us )
{
flushMouseEventStack();
// stop animation
_thrown = false;
us.requestContinuousUpdate(false);
}
/** Handles events. Returns true if handled, false otherwise.*/
bool StandardManipulator::handle( const GUIEventAdapter& ea, GUIActionAdapter& us )
{
switch( ea.getEventType() )
{
case GUIEventAdapter::FRAME:
return handleFrame( ea, us );
case GUIEventAdapter::RESIZE:
return handleResize( ea, us );
default:
break;
}
if( ea.getHandled() )
return false;
switch( ea.getEventType() )
{
case GUIEventAdapter::MOVE:
return handleMouseMove( ea, us );
case GUIEventAdapter::DRAG:
return handleMouseDrag( ea, us );
case GUIEventAdapter::PUSH:
return handleMousePush( ea, us );
case GUIEventAdapter::RELEASE:
return handleMouseRelease( ea, us );
case GUIEventAdapter::KEYDOWN:
return handleKeyDown( ea, us );
case GUIEventAdapter::KEYUP:
return handleKeyUp( ea, us );
case GUIEventAdapter::SCROLL:
if( _flags & PROCESS_MOUSE_WHEEL )
return handleMouseWheel( ea, us );
else
return false;
default:
return false;
}
}
/// Handles GUIEventAdapter::FRAME event.
bool StandardManipulator::handleFrame( const GUIEventAdapter& ea, GUIActionAdapter& us )
{
double current_frame_time = ea.getTime();
_delta_frame_time = current_frame_time - _last_frame_time;
_last_frame_time = current_frame_time;
if( _thrown && performMovement() )
{
us.requestRedraw();
}
if( _animationData && _animationData->_isAnimating )
{
performAnimationMovement( ea, us );
}
return false;
}
/// Handles GUIEventAdapter::RESIZE event.
bool StandardManipulator::handleResize( const GUIEventAdapter& ea, GUIActionAdapter& us )
{
init( ea, us );
us.requestRedraw();
return true;
}
/// Handles GUIEventAdapter::MOVE event.
bool StandardManipulator::handleMouseMove( const GUIEventAdapter& /*ea*/, GUIActionAdapter& /*us*/ )
{
return false;
}
/// Handles GUIEventAdapter::DRAG event.
bool StandardManipulator::handleMouseDrag( const GUIEventAdapter& ea, GUIActionAdapter& us )
{
addMouseEvent( ea );
if( performMovement() )
us.requestRedraw();
us.requestContinuousUpdate( false );
_thrown = false;
return true;
}
/// Handles GUIEventAdapter::PUSH event.
bool StandardManipulator::handleMousePush( const GUIEventAdapter& ea, GUIActionAdapter& us )
{
flushMouseEventStack();
addMouseEvent( ea );
if( performMovement() )
us.requestRedraw();
us.requestContinuousUpdate( false );
_thrown = false;
return true;
}
/// Handles GUIEventAdapter::RELEASE event.
bool StandardManipulator::handleMouseRelease( const GUIEventAdapter& ea, GUIActionAdapter& us )
{
if( ea.getButtonMask() == 0 )
{
double timeSinceLastRecordEvent = _ga_t0.valid() ? (ea.getTime() - _ga_t0->getTime()) : DBL_MAX;
if( timeSinceLastRecordEvent > 0.02 )
flushMouseEventStack();
if( isMouseMoving() )
{
if( performMovement() && _allowThrow )
{
us.requestRedraw();
us.requestContinuousUpdate( true );
_thrown = true;
}
return true;
}
}
flushMouseEventStack();
addMouseEvent( ea );
if( performMovement() )
us.requestRedraw();
us.requestContinuousUpdate( false );
_thrown = false;
return true;
}
/// Handles GUIEventAdapter::KEYDOWN event.
bool StandardManipulator::handleKeyDown( const GUIEventAdapter& ea, GUIActionAdapter& us )
{
if( ea.getKey() == GUIEventAdapter::KEY_Space )
{
flushMouseEventStack();
_thrown = false;
home(ea,us);
return true;
}
return false;
}
/// Handles GUIEventAdapter::KEYUP event.
bool StandardManipulator::handleKeyUp( const GUIEventAdapter& /*ea*/, GUIActionAdapter& /*us*/ )
{
return false;
}
/// Handles GUIEventAdapter::SCROLL event.
bool StandardManipulator::handleMouseWheel( const GUIEventAdapter& /*ea*/, GUIActionAdapter& /*us*/ )
{
return false;
}
/** Get the keyboard and mouse usage of the manipulator.*/
void StandardManipulator::getUsage( ApplicationUsage& usage ) const
{
usage.addKeyboardMouseBinding( getManipulatorName() + ": Space", "Reset the viewing position to home" );
}
/// Make movement step of manipulator. Returns true if any movement was made.
bool StandardManipulator::performMovement()
{
// return if less then two events have been added
if( _ga_t0.get() == NULL || _ga_t1.get() == NULL )
return false;
// get delta time
double eventTimeDelta = _ga_t0->getTime() - _ga_t1->getTime();
if( eventTimeDelta < 0. )
{
OSG_WARN << "Manipulator warning: eventTimeDelta = " << eventTimeDelta << std::endl;
eventTimeDelta = 0.;
}
// get deltaX and deltaY
float dx = _ga_t0->getXnormalized() - _ga_t1->getXnormalized();
float dy = _ga_t0->getYnormalized() - _ga_t1->getYnormalized();
// return if there is no movement.
if( dx == 0. && dy == 0. )
return false;
// call appropriate methods
unsigned int buttonMask = _ga_t1->getButtonMask();
if( buttonMask == GUIEventAdapter::LEFT_MOUSE_BUTTON )
{
return performMovementLeftMouseButton( eventTimeDelta, dx, dy );
}
else if( buttonMask == GUIEventAdapter::MIDDLE_MOUSE_BUTTON ||
buttonMask == (GUIEventAdapter::LEFT_MOUSE_BUTTON | GUIEventAdapter::RIGHT_MOUSE_BUTTON) )
{
return performMovementMiddleMouseButton( eventTimeDelta, dx, dy );
}
else if( buttonMask == GUIEventAdapter::RIGHT_MOUSE_BUTTON )
{
return performMovementRightMouseButton( eventTimeDelta, dx, dy );
}
return false;
}
/** Make movement step of manipulator.
This method implements movement for left mouse button.*/
bool StandardManipulator::performMovementLeftMouseButton( const double /*eventTimeDelta*/, const double /*dx*/, const double /*dy*/ )
{
return false;
}
/** Make movement step of manipulator.
This method implements movement for middle mouse button
or combination of left and right mouse button pressed together.*/
bool StandardManipulator::performMovementMiddleMouseButton( const double /*eventTimeDelta*/, const double /*dx*/, const double /*dy*/ )
{
return false;
}
/** Make movement step of manipulator.
This method implements movement for right mouse button.*/
bool StandardManipulator::performMovementRightMouseButton( const double /*eventTimeDelta*/, const double /*dx*/, const double /*dy*/ )
{
return false;
}
/// The method processes events for manipulation based on relative mouse movement (mouse delta).
bool StandardManipulator::handleMouseDeltaMovement( const GUIEventAdapter& ea, GUIActionAdapter& us )
{
float dx = ea.getX() - _mouseCenterX;
float dy = ea.getY() - _mouseCenterY;
if( dx == 0.f && dy == 0.f )
return false;
addMouseEvent( ea );
centerMousePointer( ea, us );
return performMouseDeltaMovement( dx, dy );
}
/// The method performs manipulator update based on relative mouse movement (mouse delta).
bool StandardManipulator::performMouseDeltaMovement( const float /*dx*/, const float /*dy*/ )
{
return false;
}
/// Makes the manipulator progress in its current animation.
bool StandardManipulator::performAnimationMovement( const GUIEventAdapter& ea, GUIActionAdapter& us )
{
double f = (ea.getTime() - _animationData->_startTime) / _animationData->_animationTime;
if( f >= 1. )
{
f = 1.;
_animationData->_isAnimating = false;
if( !_thrown )
us.requestContinuousUpdate( false );
}
applyAnimationStep( f, _animationData->_phase );
_animationData->_phase = f;
us.requestRedraw();
return _animationData->_isAnimating;
}
/// Updates manipulator by a single animation step
void StandardManipulator::applyAnimationStep( const double /*currentProgress*/, const double /*prevProgress*/ )
{
}
/// Centers mouse pointer
void StandardManipulator::centerMousePointer( const GUIEventAdapter& ea, GUIActionAdapter& us )
{
_mouseCenterX = (ea.getXmin() + ea.getXmax()) / 2.0f;
_mouseCenterY = (ea.getYmin() + ea.getYmax()) / 2.0f;
us.requestWarpPointer( _mouseCenterX, _mouseCenterY );
}
/** Add the current mouse GUIEvent to internal stack.*/
void StandardManipulator::addMouseEvent( const GUIEventAdapter& ea )
{
_ga_t1 = _ga_t0;
_ga_t0 = &ea;
}
/** Reset the internal GUIEvent stack.*/
void StandardManipulator::flushMouseEventStack()
{
_ga_t1 = NULL;
_ga_t0 = NULL;
}
/** Check the speed at which the mouse is moving.
If speed is below a threshold then return false, otherwise return true.*/
bool StandardManipulator::isMouseMoving() const
{
if (_ga_t0.get()==NULL || _ga_t1.get()==NULL) return false;
static const float velocity = 0.1f;
float dx = _ga_t0->getXnormalized()-_ga_t1->getXnormalized();
float dy = _ga_t0->getYnormalized()-_ga_t1->getYnormalized();
float len = sqrtf(dx*dx+dy*dy);
float dt = _ga_t0->getTime()-_ga_t1->getTime();
return (len>dt*velocity);
}
/** Set the 'allow throw' flag. If it is set to true (default), releasing the mouse button
while moving the mouse results in a throw. If manipulator was thrown, it continues spinning
although no mouse button is down at the moment.*/
void StandardManipulator::setAllowThrow( bool allowThrow )
{
_allowThrow = allowThrow;
}
/** Returns the scale that should be applied on animation of "thrown" manipulator state
to avoid its dependency on varying frame rate.
eventTimeDelta parameter gives the time difference between last two
events that started the animation.*/
float StandardManipulator::getThrowScale( const double eventTimeDelta ) const
{
if( _thrown )
{
if (eventTimeDelta == 0.f)
return 0.f;
return float( _delta_frame_time / eventTimeDelta );
}
else return 1.f;
}
/** Update rotation by yaw and pitch.
*
* localUp parameter defines either camera's "UP" vector
* that will be preserved during rotation, or it can be zero (0,0,0) to specify
* that camera's "UP" vector will be not preserved and free rotation will be made.*/
void StandardManipulator::rotateYawPitch( Quat& rotation, const double yaw, const double pitch,
const Vec3d& localUp )
{
bool verticalAxisFixed = (localUp != Vec3d( 0.,0.,0. ));
// fix current rotation
if( verticalAxisFixed )
fixVerticalAxis( rotation, localUp, true );
// rotations
Quat rotateYaw( -yaw, verticalAxisFixed ? localUp : rotation * Vec3d( 0.,1.,0. ) );
Quat rotatePitch;
Quat newRotation;
Vec3d cameraRight( rotation * Vec3d( 1.,0.,0. ) );
double my_dy = pitch;
int i = 0;
do {
// rotations
rotatePitch.makeRotate( my_dy, cameraRight );
newRotation = rotation * rotateYaw * rotatePitch;
// update vertical axis
if( verticalAxisFixed )
fixVerticalAxis( newRotation, localUp, false );
// check for viewer's up vector to be more than 90 degrees from "up" axis
Vec3d newCameraUp = newRotation * Vec3d( 0.,1.,0. );
if( newCameraUp * localUp > 0. )
{
// apply new rotation
rotation = newRotation;
return;
}
my_dy /= 2.;
if( ++i == 20 )
{
rotation = rotation * rotateYaw;
return;
}
} while( true );
}
/** The method corrects the rotation to make impression of fixed up direction.
* Technically said, it makes the roll component of the rotation equal to zero.
*
* Up vector is given by CoordinateFrame and it is +z by default.
* It can be changed by osgGA::CameraManipulator::setCoordinateFrameCallback().
*
* Eye parameter is user position, rotation is the rotation to be fixed, and
* disallowFlipOver, when set on true, avoids pitch rotation component to grow
* over +/- 90 degrees. If this happens and disallowFlipOver is true,
* manipulator is rotated by 180 degrees. More precisely, roll rotation component is changed by 180 degrees,
* making pitch once again between -90..+90 degrees limits.*/
void StandardManipulator::fixVerticalAxis( Vec3d& eye, Quat& rotation, bool disallowFlipOver )
{
CoordinateFrame coordinateFrame = getCoordinateFrame( eye );
Vec3d localUp = getUpVector( coordinateFrame );
fixVerticalAxis( rotation, localUp, disallowFlipOver );
}
/** The method corrects the rotation to make impression of fixed up direction.
* Technically said, it makes the roll component of the rotation equal to zero.
*
* rotation parameter is the rotation to be fixed.
* localUp is UP vector and must not be zero length.
* disallowFlipOver, when set on true, avoids pitch rotation component to grow
* over +/- 90 degrees. If this happens and disallowFlipOver is true,
* manipulator is rotated by 180 degrees. More precisely, roll rotation component is changed by 180 degrees,
* making pitch once again between -90..+90 degrees limits.*/
void StandardManipulator::fixVerticalAxis( Quat& rotation, const Vec3d& localUp, bool disallowFlipOver )
{
// camera direction vectors
Vec3d cameraUp = rotation * Vec3d( 0.,1.,0. );
Vec3d cameraRight = rotation * Vec3d( 1.,0.,0. );
Vec3d cameraForward = rotation * Vec3d( 0.,0.,-1. );
// computed directions
Vec3d newCameraRight1 = cameraForward ^ localUp;
Vec3d newCameraRight2 = cameraUp ^ localUp;
Vec3d newCameraRight = (newCameraRight1.length2() > newCameraRight2.length2()) ?
newCameraRight1 : newCameraRight2;
if( newCameraRight * cameraRight < 0. )
newCameraRight = -newCameraRight;
// vertical axis correction
Quat rotationVerticalAxisCorrection;
rotationVerticalAxisCorrection.makeRotate( cameraRight, newCameraRight );
// rotate camera
rotation *= rotationVerticalAxisCorrection;
if( disallowFlipOver )
{
// make viewer's up vector to be always less than 90 degrees from "up" axis
Vec3d newCameraUp = newCameraRight ^ cameraForward;
if( newCameraUp * localUp < 0. )
rotation = Quat( PI, Vec3d( 0.,0.,1. ) ) * rotation;
}
}
/** The method corrects the rotation to make impression of fixed up direction.
* Technically said, it makes the roll component of the rotation equal to zero.
*
* forward and up parameters are the forward and up vectors of the manipulator.
* newUp will receive corrected UP manipulator vector. localUp is UP vector
* that is used for vertical correction.
* disallowFlipOver when set on true avoids pitch rotation component to grow
* over +/- 90 degrees. If this happens and disallowFlipOver is true,
* right and up camera vectors are negated (changing roll by 180 degrees),
* making pitch once again between -90..+90 degrees limits.*/
void StandardManipulator::fixVerticalAxis( const osg::Vec3d& forward, const osg::Vec3d& up, osg::Vec3d& newUp,
const osg::Vec3d& localUp, bool /*disallowFlipOver*/ )
{
// right direction
osg::Vec3d right1 = forward ^ localUp;
osg::Vec3d right2 = up ^ localUp;
osg::Vec3d right = (right1.length2() > right2.length2()) ? right1 : right2;
// updatedUp
osg::Vec3d updatedUp = right ^ forward;
if( updatedUp.normalize() >= 0. )
// return updatedUp
newUp = updatedUp;
else {
// return original up
OSG_WARN << "StandardManipulator::fixVerticalAxis warning: Can not update vertical axis." << std::endl;
newUp = up;
}
}
/** The method sends a ray into the scene and the point of the closest intersection
is used to set a new center for the manipulator. For Orbit-style manipulators,
the orbiting center is set. For FirstPerson-style manipulators, view is pointed
towards the center.*/
bool StandardManipulator::setCenterByMousePointerIntersection( const GUIEventAdapter& ea, GUIActionAdapter& us )
{
osg::View* view = us.asView();
if( !view )
return false;
Camera *camera = view->getCamera();
if( !camera )
return false;
// prepare variables
float x = ( ea.getX() - ea.getXmin() ) / ( ea.getXmax() - ea.getXmin() );
float y = ( ea.getY() - ea.getYmin() ) / ( ea.getYmax() - ea.getYmin() );
LineSegmentIntersector::CoordinateFrame cf;
Viewport *vp = camera->getViewport();
if( vp ) {
cf = Intersector::WINDOW;
x *= vp->width();
y *= vp->height();
} else
cf = Intersector::PROJECTION;
// perform intersection computation
ref_ptr< LineSegmentIntersector > picker = new LineSegmentIntersector( cf, x, y );
IntersectionVisitor iv( picker.get() );
camera->accept( iv );
// return on no intersections
if( !picker->containsIntersections() )
return false;
// get all intersections
LineSegmentIntersector::Intersections& intersections = picker->getIntersections();
// get current transformation
osg::Vec3d eye, oldCenter, up;
getTransformation( eye, oldCenter, up );
// new center
osg::Vec3d newCenter = (*intersections.begin()).getWorldIntersectPoint();
// make vertical axis correction
if( getVerticalAxisFixed() )
{
CoordinateFrame coordinateFrame = getCoordinateFrame( newCenter );
Vec3d localUp = getUpVector( coordinateFrame );
fixVerticalAxis( newCenter - eye, up, up, localUp, true );
}
// set the new center
setTransformation( eye, newCenter, up );
// warp pointer
// note: this works for me on standard camera on GraphicsWindowEmbedded and Qt,
// while it was necessary to implement requestWarpPointer like follows:
//
// void QOSGWidget::requestWarpPointer( float x, float y )
// {
// osgViewer::Viewer::requestWarpPointer( x, y );
// QCursor::setPos( this->mapToGlobal( QPoint( int( x+.5f ), int( y+.5f ) ) ) );
// }
//
// Additions of .5f are just for the purpose of rounding.
centerMousePointer( ea, us );
return true;
}
/** Makes mouse pointer intersection test with the geometry bellow the pointer
and starts animation to center camera to look at the closest hit bellow the mouse pointer.
If there is a hit, animation is started and true is returned.
Otherwise, the method returns false.*/
bool StandardManipulator::startAnimationByMousePointerIntersection(
const osgGA::GUIEventAdapter& /*ea*/, osgGA::GUIActionAdapter& /*us*/ )
{
return false;
}
StandardManipulator::AnimationData::AnimationData()
:_isAnimating( false )
{
}
void StandardManipulator::AnimationData::start( const double startTime )
{
_isAnimating = true;
_startTime = startTime;
_phase = 0.;
}