Repository navigation
Expand file tree
/
Copy pathfingerproxy.cpp
More file actions
1449 lines (1198 loc) · 59.1 KB
/
Copy pathfingerproxy.cpp
File metadata and controls
1449 lines (1198 loc) · 59.1 KB
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
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
//==============================================================================
/*
Modified Emanuele Ruffaldi 2016 Standalone
*/
//==============================================================================
/*
Software License Agreement (BSD License)
Copyright (c) 2003-2016, CHAI3D.
(www.chai3d.org)
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions
are met:
* Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above
copyright notice, this list of conditions and the following
disclaimer in the documentation and/or other materials provided
with the distribution.
* Neither the name of CHAI3D nor the names of its contributors may
be used to endorse or promote products derived from this software
without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
POSSIBILITY OF SUCH DAMAGE.
\author <http://www.chai3d.org>
\author Francois Conti
\version 3.1.1 $Rev: 1869 $
*/
//==============================================================================
#include "fingerproxy.hpp"
namespace chai3d {
//==============================================================================
/*!
Constructor of cAlgorithmFingerProxy.
*/
//==============================================================================
cAlgorithmFingerProxy::cAlgorithmFingerProxy()
{
// no world defined yet
m_world = NULL;
// no contacts yet between proxy and environment
m_numCollisionEvents = 0;
// set epsilon base value
setEpsilonBaseValue(0.0001);
// force model settings
m_frictionDynHysteresisMultiplier = 0.6;
m_forceShadingAngleThreshold = 0.75;
// initialize device and proxy positions
m_deviceGlobalPos.zero();
m_proxyGlobalPos.zero();
m_lastGlobalForce.zero();
// this will generally be over-written by the calling pointer
m_radius = 0.01;
// by default, we do not use dynamic proxy (which handles dynamic objects)
m_useDynamicProxy = false;
// initialize dynamic proxy members
m_collisionRecorderConstraint0.m_nearestCollision.clear();
m_collisionRecorderConstraint1.m_nearestCollision.clear();
m_collisionRecorderConstraint2.m_nearestCollision.clear();
// initilaize local points
m_contactPointLocalPos0.zero();
m_contactPointLocalPos1.zero();
m_contactPointLocalPos2.zero();
// friction properties
m_slipping = true;
// setup collision detector settings
m_collisionSettings.m_checkForNearestCollisionOnly = true;
m_collisionSettings.m_returnMinimalCollisionData = false;
m_collisionSettings.m_checkVisibleObjects = false;
m_collisionSettings.m_checkHapticObjects = true;
m_collisionSettings.m_ignoreShapes = true;
m_collisionSettings.m_adjustObjectMotion = m_useDynamicProxy;
// setup pointers to collision recorders so that user can access
// collision information about each haptic point.
m_collisionEvents[0] = &(m_collisionRecorderConstraint0.m_nearestCollision);
m_collisionEvents[1] = &(m_collisionRecorderConstraint1.m_nearestCollision);
m_collisionEvents[2] = &(m_collisionRecorderConstraint2.m_nearestCollision);
// initialize algorithm variables
m_algoCounter = 0;
// render settings (for debug purposes)
m_showEnabled = true;
}
//==============================================================================
/*!
This method Initializes the algorithm, including setting the pointer to the world
in which the algorithm is to operate, and setting the initial position
of the device.
\param a_world Pointer to world in which force algorithm is operating.
\param a_initialGlobalPosition Initial position of the haptic device.
*/
//==============================================================================
void cAlgorithmFingerProxy::initialize(cWorld* a_world, const cVector3d& a_initialGlobalPosition)
{
// reset some variables
m_lastGlobalForce.zero();
// no contacts yet between proxy and environment
m_numCollisionEvents = 0;
// the proxy can slip along surfaces
m_slipping = true;
// initialize counter
m_algoCounter = 0;
// initialize device and proxy positions
m_deviceGlobalPos = a_initialGlobalPosition;
m_proxyGlobalPos = a_initialGlobalPosition;
// set pointer to world in which force algorithm operates
m_world = a_world;
}
//==============================================================================
/*!
This method reset the algorithm and sets the __proxy__ position to the
__device__ position.
*/
//==============================================================================
void cAlgorithmFingerProxy::reset()
{
// reset some variables
m_lastGlobalForce.zero();
// no contacts yet between proxy and environment
m_numCollisionEvents = 0;
// the proxy can slip along surfaces
m_slipping = true;
// initialize counter
m_algoCounter = 0;
// set proxy position to be equal to the device position
m_proxyGlobalPos = m_deviceGlobalPos;
}
//==============================================================================
/*!
This method sets the epsilon value which is used during geometry
computation of the proxy model.
*/
//==============================================================================
void cAlgorithmFingerProxy::setEpsilonBaseValue(double a_value)
{
m_epsilonBaseValue = a_value;
m_epsilonMinimalValue = 0.01 * m_epsilonBaseValue;
m_epsilon = m_epsilonBaseValue;
m_epsilonCollisionDetection = 1.0 * m_epsilon;
}
//==============================================================================
/*!
This method computes the interaction forces that are associated with the
position of the __device__ and the __proxy__.
\param a_toolPos New position of tool
\param a_toolVel New velocity of tool
\return Haptic force.
*/
//==============================================================================
cVector3d cAlgorithmFingerProxy::computeForces(const cVector3d& a_toolPos,
const cVector3d& a_toolVel)
{
// update device position
m_deviceGlobalPos = a_toolPos;
// check if world has been defined; if so, compute forces
if (m_world != NULL)
{
// compute next best position of proxy
computeNextBestProxyPosition(m_deviceGlobalPos);
// update proxy to next best position
m_proxyGlobalPos = m_nextBestProxyGlobalPos;
// compute force vector applied to device
updateForce();
// return result
return (m_lastGlobalForce);
}
// if no world has been defined in which algorithm operates, there is no force
else
{
return (cVector3d(0.0, 0.0, 0.0));
}
}
//==============================================================================
/*!
Given the new position of the device and considering the current
position of the proxy, this method attempts to move the proxy towards
the device position (the goal). If its path is blocked by an obstacle
(e.g., a triangle in a mesh), the proxy is moved to this intersection
point and a new goal is calculated as the closest point to the original
goal in the half-plane above the intersection triangle.
The process is repeated if necessary, bringing the proxy to its
final location.
\param a_goal The goal position of the __proxy__ subject to constraints.
*/
//==============================================================================
void cAlgorithmFingerProxy::computeNextBestProxyPosition(const cVector3d& a_goal)
{
bool hit0, hit1, hit2;
if (m_useDynamicProxy)
{
// adjust the proxy according moving objects that may have collided with the proxy
adjustDynamicProxy(a_goal);
// search for a first contact
hit0 = computeNextProxyPositionWithContraints0(a_goal);
m_proxyGlobalPos = m_nextBestProxyGlobalPos;
if (!hit0)
{
return;
}
// search for a second contact
hit1 = computeNextProxyPositionWithContraints1(a_goal);
m_proxyGlobalPos = m_nextBestProxyGlobalPos;
if (!hit1)
{
m_contactPointLocalPos0 = cTranspose(m_collisionRecorderConstraint0.m_nearestCollision.m_object->getGlobalRot()) * (m_proxyGlobalPos - m_collisionRecorderConstraint0.m_nearestCollision.m_object->getGlobalPos());
return;
}
// search for a third contact
hit2 = computeNextProxyPositionWithContraints2(a_goal);
m_proxyGlobalPos = m_nextBestProxyGlobalPos;
if (!hit2)
{
m_contactPointLocalPos0 = cTranspose(m_collisionRecorderConstraint0.m_nearestCollision.m_object->getGlobalRot()) * (m_proxyGlobalPos - m_collisionRecorderConstraint0.m_nearestCollision.m_object->getGlobalPos());
m_contactPointLocalPos1 = cTranspose(m_collisionRecorderConstraint1.m_nearestCollision.m_object->getGlobalRot()) * (m_proxyGlobalPos - m_collisionRecorderConstraint1.m_nearestCollision.m_object->getGlobalPos());
return;
}
m_contactPointLocalPos0 = cTranspose(m_collisionRecorderConstraint0.m_nearestCollision.m_object->getGlobalRot()) * (m_proxyGlobalPos - m_collisionRecorderConstraint0.m_nearestCollision.m_object->getGlobalPos());
m_contactPointLocalPos1 = cTranspose(m_collisionRecorderConstraint1.m_nearestCollision.m_object->getGlobalRot()) * (m_proxyGlobalPos - m_collisionRecorderConstraint1.m_nearestCollision.m_object->getGlobalPos());
m_contactPointLocalPos2 = cTranspose(m_collisionRecorderConstraint2.m_nearestCollision.m_object->getGlobalRot()) * (m_proxyGlobalPos - m_collisionRecorderConstraint2.m_nearestCollision.m_object->getGlobalPos());
}
else
{
// In order to keep the finger-proxy algorithm running fast in static mode, we only
// compute collision with one constraint at the time. The next time
// the algorithm is called, it searches for the second or
// third obstacle.
switch(m_algoCounter)
{
case 0:
hit0 = computeNextProxyPositionWithContraints0(a_goal);
if (hit0)
{
m_contactPointLocalPos0 = cTranspose(m_collisionRecorderConstraint0.m_nearestCollision.m_object->getGlobalRot()) * (m_nextBestProxyGlobalPos - m_collisionRecorderConstraint0.m_nearestCollision.m_object->getGlobalPos());
}
break;
case 1:
hit1 = computeNextProxyPositionWithContraints1(a_goal);
if (hit1)
{
m_contactPointLocalPos0 = cTranspose(m_collisionRecorderConstraint0.m_nearestCollision.m_object->getGlobalRot()) * (m_nextBestProxyGlobalPos - m_collisionRecorderConstraint0.m_nearestCollision.m_object->getGlobalPos());
m_contactPointLocalPos1 = cTranspose(m_collisionRecorderConstraint1.m_nearestCollision.m_object->getGlobalRot()) * (m_nextBestProxyGlobalPos - m_collisionRecorderConstraint1.m_nearestCollision.m_object->getGlobalPos());
}
break;
case 2:
hit2 = computeNextProxyPositionWithContraints2(a_goal);
if (hit2)
{
m_contactPointLocalPos0 = cTranspose(m_collisionRecorderConstraint0.m_nearestCollision.m_object->getGlobalRot()) * (m_nextBestProxyGlobalPos - m_collisionRecorderConstraint0.m_nearestCollision.m_object->getGlobalPos());
m_contactPointLocalPos1 = cTranspose(m_collisionRecorderConstraint1.m_nearestCollision.m_object->getGlobalRot()) * (m_nextBestProxyGlobalPos - m_collisionRecorderConstraint1.m_nearestCollision.m_object->getGlobalPos());
m_contactPointLocalPos2 = cTranspose(m_collisionRecorderConstraint2.m_nearestCollision.m_object->getGlobalRot()) * (m_nextBestProxyGlobalPos - m_collisionRecorderConstraint2.m_nearestCollision.m_object->getGlobalPos());
}
break;
}
}
}
//------------------------------------------------------------------------------
void cAlgorithmFingerProxy::adjustDynamicProxy(const cVector3d& a_goal)
{
// set collision settings for dynamic proxy
cCollisionSettings collisionSettings;
collisionSettings.m_checkForNearestCollisionOnly = false;
collisionSettings.m_returnMinimalCollisionData = false;
collisionSettings.m_checkVisibleObjects = false;
collisionSettings.m_checkHapticObjects = true;
collisionSettings.m_ignoreShapes = true;
collisionSettings.m_adjustObjectMotion = true;
collisionSettings.m_collisionRadius = m_radius;
// setup recorder
cCollisionRecorder collisionRecorder;
collisionRecorder.clear();
cVector3d nextProxyOffset(0.0, 0.0, 0.0);
// check if any moving objects have hit the proxy
bool hit = m_world->computeCollisionDetection(m_proxyGlobalPos,
m_proxyGlobalPos,
collisionRecorder,
collisionSettings);
// one or more collisions have occured
if (hit)
{
int numCollisions = collisionRecorder.m_collisions.size();
for (int i=0; i<numCollisions; i++)
{
// retrieve new position of proxy
cVector3d posLocal = collisionRecorder.m_collisions[i].m_adjustedSegmentAPoint;
cGenericObject* obj = collisionRecorder.m_collisions[i].m_object;
cVector3d posGlobal = cAdd(obj->getGlobalPos(), cMul( obj->getGlobalRot(), posLocal ));
cVector3d offset = posGlobal - m_proxyGlobalPos;
if (offset.length() > C_SMALL)
{
cVector3d projection = cProject(nextProxyOffset, offset);
double diff = offset.length() - projection.length();
if (diff > 0.0)
{
nextProxyOffset = nextProxyOffset + diff * cNormalize(offset);
}
}
}
}
// compute new proxy position to stay away from collision with objects moving
m_proxyGlobalPos = m_proxyGlobalPos + nextProxyOffset;
// if possible maintain proxy on surface of objects in contact
if (m_numCollisionEvents == 1)
{
collisionRecorder.clear();
collisionSettings.m_checkForNearestCollisionOnly = true;
// computed new desired position of proxy on consytraint 0
cVector3d globalPos = m_collisionRecorderConstraint0.m_nearestCollision.m_object->getGlobalPos();
cMatrix3d globalRot = m_collisionRecorderConstraint0.m_nearestCollision.m_object->getGlobalRot();
cVector3d proxyDesiredGlobalPos = globalPos + globalRot * m_contactPointLocalPos0;
// check if any objects are in the way
bool hit = m_world->computeCollisionDetection(m_proxyGlobalPos,
proxyDesiredGlobalPos,
collisionRecorder,
collisionSettings);
// no collision has occured, therfore proxy can move with object
if (!hit)
{
m_proxyGlobalPos = proxyDesiredGlobalPos;
}
}
}
//------------------------------------------------------------------------------
bool cAlgorithmFingerProxy::computeNextProxyPositionWithContraints0(const cVector3d& a_goalGlobalPos)
{
// we define the goal position of the proxy.
cVector3d goalGlobalPos = a_goalGlobalPos;
// to address numerical errors of the computer, we make sure to keep the proxy
// slightly above any triangle and not directly on it. If we are using a radius of
// zero, we need to define a default small value for epsilon
m_epsilonInitialValue = fabs(0.0001 * m_radius);
if (m_epsilonInitialValue < m_epsilonBaseValue)
{
m_epsilonInitialValue = m_epsilonBaseValue;
}
// the epsilon value is dynamic (can be reduced); we set it to its initial
// value if the proxy is not touching any triangle.
if (m_numCollisionEvents == 0)
{
m_epsilon = m_epsilonInitialValue;
m_slipping = true;
}
// if the distance between the proxy and the goal position (device) is
// very small then we can be considered done.
if (goalAchieved(m_proxyGlobalPos, goalGlobalPos))
{
m_nextBestProxyGlobalPos = m_proxyGlobalPos;
m_algoCounter = 0;
return (false);
}
// compute the normalized form of the vector going from the
// current proxy position to the desired goal position
// compute the distance between the proxy and the goal positions
double distanceProxyGoal = cDistance(m_proxyGlobalPos, goalGlobalPos);
// a vector from the proxy to the goal
cVector3d vProxyToGoal;
cVector3d vProxyToGoalNormalized;
if (distanceProxyGoal > m_epsilon)
{
goalGlobalPos.subr(m_proxyGlobalPos, vProxyToGoal);
vProxyToGoal.normalizer(vProxyToGoalNormalized);
}
else
{
vProxyToGoal.zero();
vProxyToGoalNormalized.zero();
}
// test whether the path from the proxy to the goal is obstructed;
// for this we create a segment that goes from the proxy position to
// the goal position plus a little extra to take into account the
// physical radius of the proxy.
cVector3d targetPos = goalGlobalPos + cMul(m_epsilonCollisionDetection, vProxyToGoalNormalized);
// setup collision detector
m_collisionSettings.m_collisionRadius = m_radius;
// search for a collision between the first segment (proxy-device)
// and the environment.
m_collisionRecorderConstraint0.clear();
bool hit = m_world->computeCollisionDetection(m_proxyGlobalPos,
targetPos,
m_collisionRecorderConstraint0,
m_collisionSettings);
// check if collision occurred between proxy and goal positions.
double collisionDistance;
if (hit)
{
collisionDistance = sqrt(m_collisionRecorderConstraint0.m_nearestCollision.m_squareDistance);
if (collisionDistance > (distanceProxyGoal + C_SMALL))
{
hit = false;
}
if (hit)
{
// a collision has occurred and we check if the distance from the
// proxy to the collision is smaller than epsilon. If yes, then
// we reduce the epsilon term in order to avoid possible "pop through"
// effect if we suddenly push the proxy "up" again.
if (collisionDistance < m_epsilon)
{
m_epsilon = collisionDistance;
if (m_epsilon < m_epsilonMinimalValue)
{
m_epsilon = m_epsilonMinimalValue;
}
}
}
}
// if no collision occurs, then we move the proxy to its goal, and we're done
if (!hit)
{
m_numCollisionEvents = 0;
m_algoCounter = 0;
m_slipping = true;
m_nextBestProxyGlobalPos = goalGlobalPos;
return (false);
}
// a first collision has occurred
m_algoCounter = 1;
//--------------------------------------------------------------------------
// FIRST COLLISION OCCURES:
//--------------------------------------------------------------------------
// we want the center of the proxy to move as far toward the triangle as it can,
// but we want it to stop when the _sphere_ representing the proxy hits the
// triangle. We want to compute how far the proxy center will have to
// be pushed _away_ from the collision point - along the vector from the proxy
// to the goal - to keep a distance m_radius between the proxy center and the
// triangle.
//
// so we compute the cosine of the angle between the normal and proxy-goal vector...
double cosAngle = vProxyToGoalNormalized.dot(m_collisionRecorderConstraint0.m_nearestCollision.m_globalNormal);
// now we compute how far away from the collision point - _backwards_
// along vProxyGoal - we have to put the proxy to keep it from penetrating
// the triangle.
//
// if only ASCII art were a little more expressive...
double distanceTriangleProxy = m_epsilon / fabs(cosAngle);
if (distanceTriangleProxy > collisionDistance) { distanceTriangleProxy = cMax(collisionDistance, m_epsilon); }
// we compute the projection of the vector between the proxy and the collision
// point onto the normal of the triangle. This is the direction in which
// we'll move the _goal_ to "push it away" from the triangle (to account for
// the radius of the proxy).
// a vector from the most recent collision point to the proxy
cVector3d vCollisionToProxy;
m_proxyGlobalPos.subr(m_collisionEvents[0]->m_globalPos, vCollisionToProxy);
// move the proxy to the collision point, minus the distance along the
// movement vector that we computed above.
//
// note that we're adjusting the 'proxy' variable, which is just a local
// copy of the proxy position. we still might decide not to move the
// 'real' proxy due to friction.
cVector3d vColNextGoal;
vProxyToGoalNormalized.mulr(-distanceTriangleProxy, vColNextGoal);
cVector3d nextProxyPos;
m_collisionEvents[0]->m_globalPos.addr(vColNextGoal, nextProxyPos);
// we can now set the next position of the proxy
m_nextBestProxyGlobalPos = nextProxyPos;
// if the distance between the proxy and the goal position (device) is
// very small then we can be considered done.
if (goalAchieved(goalGlobalPos, nextProxyPos))
{
m_numCollisionEvents = 1;
m_algoCounter = 0;
return (true);
}
return (true);
}
//------------------------------------------------------------------------------
bool cAlgorithmFingerProxy::computeNextProxyPositionWithContraints1(const cVector3d& a_goalGlobalPos)
{
cVector3d goalGlobalPos;
if (m_collisionRecorderConstraint0.m_nearestCollision.m_object->m_material->getUseHapticShading())
{
// computed adjusted surface normal for modeling haptic shading and texture rendering
cVector3d normal = computeShadedSurfaceNormal(&m_collisionRecorderConstraint0.m_nearestCollision);
// the proxy is now constrained on the shaded plane; we now calculate the nearest
// point to the original goal (device position) on this shaded plane; this point
// is computed by projecting the desired goal onto the shaded plane.
cVector3d goalGlobalPosOnForceShadingPlane = cProjectPointOnPlane(a_goalGlobalPos,
m_proxyGlobalPos,
normal);
// we now project the point from the shaded plane back onto the original triangle
// (See publication: The Haptic Display of Complex Graphical Environments from
// Ruspini and al. for more information about the shading algorithm)
goalGlobalPos = cProjectPointOnPlane(goalGlobalPosOnForceShadingPlane,
m_proxyGlobalPos,
m_collisionRecorderConstraint0.m_nearestCollision.m_globalNormal);
}
else
{
// the proxy is now constrained on a plane; we now calculate the nearest
// point to the original goal (device position) on this plane; this point
// is computed by projecting the desired goal onto the plane defined by the
// intersected triangle.
goalGlobalPos = cProjectPointOnPlane(a_goalGlobalPos,
m_proxyGlobalPos,
m_collisionRecorderConstraint0.m_nearestCollision.m_globalNormal);
}
// a vector from the proxy to the goal
cVector3d vProxyToGoal;
goalGlobalPos.subr(m_proxyGlobalPos, vProxyToGoal);
// if the distance between the proxy and the goal position (device) is
// very small then we can be considered done.
if (goalAchieved(m_proxyGlobalPos, goalGlobalPos))
{
m_nextBestProxyGlobalPos = m_proxyGlobalPos;
m_algoCounter = 0;
m_numCollisionEvents = 1;
return (false);
}
// compute the normalized vector going from the
// current proxy position to the desired goal position
cVector3d vProxyToGoalNormalized;
vProxyToGoal.normalizer(vProxyToGoalNormalized);
// test whether the path from the proxy to the goal is obstructed.
// For this we create a segment that goes from the proxy position to
// the goal position plus a little extra to take into account the
// physical radius of the proxy.
cVector3d targetPos = goalGlobalPos +
cMul(m_epsilonCollisionDetection, vProxyToGoalNormalized);
// setup collision detector
m_collisionSettings.m_collisionRadius = m_radius;
// search for collision
m_collisionSettings.m_adjustObjectMotion = false;
m_collisionRecorderConstraint1.clear();
bool hit = m_world->computeCollisionDetection( m_proxyGlobalPos,
targetPos,
m_collisionRecorderConstraint1,
m_collisionSettings);
// check if collision occurred between proxy and goal positions.
double collisionDistance;
if (hit)
{
collisionDistance = sqrt(m_collisionRecorderConstraint1.m_nearestCollision.m_squareDistance);
if (collisionDistance > (cDistance(m_proxyGlobalPos, goalGlobalPos) + C_SMALL))
{
hit = false;
}
else
{
// a collision has occurred and we check if the distance from the
// proxy to the collision is smaller than epsilon. If yes, then
// we reduce the epsilon term in order to avoid possible "pop through"
// effect if we suddenly push the proxy "up" again.
if (collisionDistance < m_epsilon)
{
m_epsilon = collisionDistance;
if (m_epsilon < m_epsilonMinimalValue)
{
m_epsilon = m_epsilonMinimalValue;
}
}
}
}
// If no collision occurs, we move the proxy to its goal, unless
// friction prevents us from doing so.
if (!hit)
{
testFrictionAndMoveProxy(goalGlobalPos,
m_proxyGlobalPos,
m_collisionRecorderConstraint0.m_nearestCollision.m_globalNormal,
m_collisionRecorderConstraint0.m_nearestCollision.m_object);
m_numCollisionEvents = 1;
m_algoCounter = 0;
return (false);
}
// a second collision has occurred
m_algoCounter = 2;
//--------------------------------------------------------------------------
// SECOND COLLISION OCCURES:
//--------------------------------------------------------------------------
// we want the center of the proxy to move as far toward the triangle as it can,
// but we want it to stop when the _sphere_ representing the proxy hits the
// triangle; we want to compute how far the proxy center will have to
// be pushed _away_ from the collision point - along the vector from the proxy
// to the goal - to keep a distance m_radius between the proxy center and the
// triangle.
// so we compute the cosine of the angle between the normal and proxy-goal vector...
double cosAngle = vProxyToGoalNormalized.dot(m_collisionRecorderConstraint1.m_nearestCollision.m_globalNormal);
// now we compute how far away from the collision point - _backwards_
// along vProxyGoal - we have to put the proxy to keep it from penetrating
// the triangle.
//
// if only ASCII art were a little more expressive...
double distanceTriangleProxy = m_epsilon / fabs(cosAngle);
if (distanceTriangleProxy > collisionDistance) { distanceTriangleProxy = cMax(collisionDistance, m_epsilon); }
// we compute the projection of the vector between the proxy and the collision
// point onto the normal of the triangle. This is the direction in which
// we'll move the _goal_ to "push it away" from the triangle (to account for
// the radius of the proxy).
// a vector from the most recent collision point to the proxy
cVector3d vCollisionToProxy;
m_proxyGlobalPos.subr(m_collisionEvents[1]->m_globalPos, vCollisionToProxy);
// move the proxy to the collision point, minus the distance along the
// movement vector that we computed above.
//
// note that we're adjusting the 'proxy' variable, which is just a local
// copy of the proxy position. We still might decide not to move the
// 'real' proxy due to friction.
cVector3d vColNextGoal;
vProxyToGoalNormalized.mulr(-distanceTriangleProxy, vColNextGoal);
cVector3d nextProxyPos;
m_collisionEvents[1]->m_globalPos.addr(vColNextGoal, nextProxyPos);
// we can now set the next position of the proxy
m_nextBestProxyGlobalPos = nextProxyPos;
// if the distance between the proxy and the goal position (device) is
// very small then we can be considered done.
if (goalAchieved(goalGlobalPos, nextProxyPos))
{
m_numCollisionEvents = 2;
m_algoCounter = 0;
return (true);
}
return (true);
}
//------------------------------------------------------------------------------
bool cAlgorithmFingerProxy::computeNextProxyPositionWithContraints2(const cVector3d& a_goalGlobalPos)
{
// the proxy is now constrained by two triangles and can only move along
// a virtual line; we now calculate the nearest point to the original
// goal (device position) along this line by projecting the ideal
// goal onto the line.
//
// The line is expressed by the cross product of both surface normals,
// which have both been oriented to point away from the device
cVector3d line;
m_collisionRecorderConstraint0.m_nearestCollision.m_globalNormal.crossr(m_collisionRecorderConstraint1.m_nearestCollision.m_globalNormal, line);
// check result.
if (line.equals(cVector3d(0,0,0)))
{
m_nextBestProxyGlobalPos = m_proxyGlobalPos;
m_algoCounter = 0;
m_numCollisionEvents = 2;
return (false);
}
line.normalize();
// compute the projection of the device position (goal) onto the line; this
// gives us the new goal position.
cVector3d goalGlobalPos = cProjectPointOnLine(a_goalGlobalPos, m_proxyGlobalPos, line);
// a vector from the proxy to the goal
cVector3d vProxyToGoal;
goalGlobalPos.subr(m_proxyGlobalPos, vProxyToGoal);
// if the distance between the proxy and the goal position (device) is
// very small then we can be considered done.
if (goalAchieved(m_proxyGlobalPos, goalGlobalPos))
{
m_nextBestProxyGlobalPos = m_proxyGlobalPos;
m_algoCounter = 0;
m_numCollisionEvents = 2;
return (false);
}
// compute the normalized form of the vector going from the
// current proxy position to the desired goal position
cVector3d vProxyToGoalNormalized;
vProxyToGoal.normalizer(vProxyToGoalNormalized);
// test whether the path from the proxy to the goal is obstructed.
// For this we create a segment that goes from the proxy position to
// the goal position plus a little extra to take into account the
// physical radius of the proxy.
cVector3d targetPos = goalGlobalPos +
cMul(m_epsilonCollisionDetection, vProxyToGoalNormalized);
// setup collision detector
m_collisionSettings.m_collisionRadius = m_radius;
// search for collision
m_collisionSettings.m_adjustObjectMotion = false;
m_collisionRecorderConstraint2.clear();
bool hit = m_world->computeCollisionDetection( m_proxyGlobalPos,
targetPos,
m_collisionRecorderConstraint2,
m_collisionSettings);
// check if collision occurred between proxy and goal positions.
double collisionDistance;
if (hit)
{
collisionDistance = sqrt(m_collisionRecorderConstraint2.m_nearestCollision.m_squareDistance);
if (collisionDistance > (cDistance(m_proxyGlobalPos, goalGlobalPos) + C_SMALL))
{
hit = false;
}
else
{
// a collision has occurred and we check if the distance from the
// proxy to the collision is smaller than epsilon. If yes, then
// we reduce the epsilon term in order to avoid possible "pop through"
// effect if we suddenly push the proxy "up" again.
if (collisionDistance < m_epsilon)
{
m_epsilon = collisionDistance;
if (m_epsilon < m_epsilonMinimalValue)
{
m_epsilon = m_epsilonMinimalValue;
}
}
}
}
// if no collision occurs, we move the proxy to its goal, unless
// friction prevents us from doing so
if (!hit)
{
cVector3d normal = cMul(0.5,cAdd(m_collisionRecorderConstraint0.m_nearestCollision.m_globalNormal,
m_collisionRecorderConstraint1.m_nearestCollision.m_globalNormal));
testFrictionAndMoveProxy(goalGlobalPos,
m_proxyGlobalPos,
normal,
m_collisionRecorderConstraint1.m_nearestCollision.m_object);
m_numCollisionEvents = 2;
m_algoCounter = 0;
return (false);
}
//--------------------------------------------------------------------------
// THIRD COLLISION OCCURES:
//--------------------------------------------------------------------------
// we want the center of the proxy to move as far toward the triangle as it can,
// but we want it to stop when the _sphere_ representing the proxy hits the
// triangle; we want to compute how far the proxy center will have to
// be pushed _away_ from the collision point - along the vector from the proxy
// to the goal - to keep a distance m_radius between the proxy center and the
// triangle.
// so we compute the cosine of the angle between the normal and proxy-goal vector...
double cosAngle = vProxyToGoalNormalized.dot(m_collisionRecorderConstraint2.m_nearestCollision.m_globalNormal);
// now we compute how far away from the collision point - _backwards_
// along vProxyGoal - we have to put the proxy to keep it from penetrating
// the triangle.
//
// if only ASCII art were a little more expressive...
double distanceTriangleProxy = m_epsilon / fabs(cosAngle);
if (distanceTriangleProxy > collisionDistance) { distanceTriangleProxy = cMax(collisionDistance, m_epsilon); }
// we compute the projection of the vector between the proxy and the collision
// point onto the normal of the triangle. This is the direction in which
// we'll move the _goal_ to "push it away" from the triangle (to account for
// the radius of the proxy).
// a vector from the most recent collision point to the proxy
cVector3d vCollisionToProxy;
m_proxyGlobalPos.subr(m_collisionEvents[2]->m_globalPos, vCollisionToProxy);
// move the proxy to the collision point, minus the distance along the
// movement vector that we computed above.
//
// Note that we're adjusting the 'proxy' variable, which is just a local
// copy of the proxy position. We still might decide not to move the
// 'real' proxy due to friction.
cVector3d vColNextGoal;
vProxyToGoalNormalized.mulr(-distanceTriangleProxy, vColNextGoal);
cVector3d nextProxyPos;
m_collisionEvents[2]->m_globalPos.addr(vColNextGoal, nextProxyPos);
// we can now set the next position of the proxy
m_nextBestProxyGlobalPos = nextProxyPos;
m_algoCounter = 0;
m_numCollisionEvents = 3;
// TODO: There actually should be a third friction test to see if we
// can make it to our new goal position, but this is generally such a
// small movement in one iteration that it's irrelevant...
return (true);
}
//==============================================================================
/*!
This method tests whether the proxy has reached the goal point, allowing
for subclass-specific approximations.
\param a_goal The location to which we'd like to move the proxy.
\param a_proxy The current position of the proxy.
\return __true__ is the proxy has effectively reached the goal, __false__ otherwise.
*/
//==============================================================================
bool cAlgorithmFingerProxy::goalAchieved(const cVector3d& a_proxy, const cVector3d& a_goal) const
{
return (a_proxy.distance(a_goal) < (m_epsilonBaseValue));
}
//==============================================================================
/*!
This method attempts to move the proxy, subject to friction constraints.
This is called from \ref computeNextBestProxyPosition() when the proxy is
ready to move along a known surface.
\param a_goal The location to which we'd like to move the proxy.
\param a_proxy The current position of the proxy.
\param a_normal The surface normal at the obstructing surface.
\param a_parent The surface along which we're moving.
*/
//==============================================================================
void cAlgorithmFingerProxy::testFrictionAndMoveProxy(const cVector3d& a_goal,
const cVector3d& a_proxy,
cVector3d& a_normal,
cGenericObject* a_parent)
{
// check if friction is enabled
if (!a_parent->m_material->getUseHapticFriction())
{
m_nextBestProxyGlobalPos = a_goal;
return;
}
// compute penetration depth; how far is the device "behind" the
// plane of the obstructing surface
cVector3d projectedGoal = cProjectPointOnPlane(m_deviceGlobalPos, a_proxy, a_normal);
double penetrationDepth = cSub(m_deviceGlobalPos,projectedGoal).length();
// find the appropriate friction coefficient
double mud = a_parent->m_material->getDynamicFriction();
double mus = a_parent->m_material->getStaticFriction();
// no friction; don't try to compute friction cones
if ((mud == 0) && (mus == 0))
{
m_nextBestProxyGlobalPos = a_goal;
return;
}
// the corresponding friction cone radii
double atmd = atan(mud);
double atms = atan(mus);
// compute a vector from the device to the proxy, for computing
// the angle of the friction cone
cVector3d vDeviceProxy = cSub(a_proxy, m_deviceGlobalPos);
vDeviceProxy.normalize();
// now compute the angle of the friction cone...
double theta = acos(vDeviceProxy.dot(a_normal));
// manage the "slip-friction" state machine
// if the dynamic friction radius is for some reason larger than the
// static friction radius, always slip
if (mud > mus)
{
m_slipping = true;
}
// if we're slipping...
else if (m_slipping)
{
if (theta < (atmd * m_frictionDynHysteresisMultiplier))
{
m_slipping = false;
}
else