-
Notifications
You must be signed in to change notification settings - Fork 0
/
CoordinateSystemManager.cs
2964 lines (2469 loc) · 132 KB
/
CoordinateSystemManager.cs
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
using UnityEngine;
public class CEllipseData
{
public float a; // Current mayor axis of the ellipse
public float b; // Current minor axis of the ellipse
public float c; // Current minor axis of the ellipse
public float Ec; // Asymptote of the perpendiculars to the ellipse
}
/// <summary>Semi-axes of an ellipse. A is the semi-major axis B the semi-minor axis
class CSemiAxes
{
public float mayorAxis;
public float minorAxis;
public CSemiAxes() { }
public CSemiAxes(float _mayorAxis, float _minorAxis) { mayorAxis = _mayorAxis; minorAxis = _minorAxis; }
}
/// <summary>
/// To store axis options
/// </summary>
enum TAxis { x, y, z };
/// <summary>
/// Class that stores all the information of the virtual ellipsoid over which the camera moves.
/// The desired evolutes cusps values are also stored for the x- and Z-axes.
/// </summary>
class CEllipsoidData
{
public float radiousXAxis;
public float radiousZAxis;
public float radiousYAxis;
float evoluteCusp_XAxis; //Desired value of the evolute cusp for the X-axis
float evoluteCusp_ZAxis; //Desired value of the evolute cusp for the Z-axis
/// <summary>Stores the values of the evolute cusps</summary>
/// <param name="evoluteCusp_XAxis"></param>
/// <param name="evoluteCusp_ZAxis"></param>
public void SetEvoluteCups(float _evoluteCusp_XAxis, float _evoluteCusp_ZAxis)
{
evoluteCusp_XAxis = _evoluteCusp_XAxis;
evoluteCusp_ZAxis = _evoluteCusp_ZAxis;
}
/// <summary>Returns the value of the cuspid of the evolution along the requested axis. Only works for the x-axis and z-axis.</summary>
/// <param name="_axis">X or Z axis for which the evolute cusps is to be known.</param>
/// <returns></returns>
public float GetEvoluteCusp(TAxis _axis)
{
if (_axis == TAxis.x) { return evoluteCusp_XAxis; }
else if (_axis == TAxis.z) { return evoluteCusp_ZAxis; }
else return 0;
}
}
/// <summary>
/// Store camera elliptical trajectory data
/// </summary>
class CTranslationEllipseData
{
public float radiousZAxis;
public float radiousXAxis;
public float evoluteCusp;
/// <summary>
/// Returns both semi-axes of the ellipse
/// </summary>
/// <returns></returns>
public CSemiAxes GetSemiAxes()
{
if (radiousXAxis > radiousZAxis) { return new CSemiAxes(radiousXAxis, radiousZAxis); }
else { return new CSemiAxes(radiousZAxis, radiousXAxis); }
}
/// <summary>
/// Returns in which axis is the semi-major axis of the ellipse
/// </summary>
/// <returns></returns>
public TAxis GetSemiMajorAxis()
{
if (radiousXAxis > radiousZAxis) { return TAxis.x; }
else { return TAxis.z; }
}
public TAxis GetSemiMinorAxis()
{
if (radiousXAxis < radiousZAxis) { return TAxis.x; }
else { return TAxis.z; }
}
}
/// <summary>
/// translationLimited : limited only in t € [0, PI]
/// </summary>
//public enum TCoordinateSystemConstraints { none, translationLimited }
public interface CCoordinateSystemManager
{
/// <summary>Initialize the navigation system</summary>
/// <param name="extents"> Vector3 with the extents of the bounding box to orbit around. X coordinate is assumed to be the object's intrinsic rotation axis. </param>
/// <param name="cameraInitialPosition">Initial position proposed for the camera. There is no restrictions by this interface, but implementations of
/// the Coordinate System Manager can expect some specific positions (e. g. along -Z axis)</param>
/// <param name="fieldOfView">
/// <param name="minimumCameraDistance">Out parameter that indicates the minimum distance allowed between the camera and the 3D object. It is expressed as
/// the distance between the object's intrinsic rotation axis and the point Zo, which is the closest allowed position in the Z axis </param>
/// <param name="pointToLook">Out parameter that indicated the direction in which the camera has to look</param>
/// <returns>Returns true if the initial position proposed for the camera is possible</returns>
bool Init(Vector3 extents, Vector3 cameraInitialPosition, TNavigationSystemConstraints navigationConstraints, /*Vector2 fieldOfView,*/ out float minimumCameraDistance, out Vector3 pointToLook);
/// <summary>Calculate the new camera position in terms of pseudoLatitude, pseudoLongitude and pseusoRadio</summary>
/// <param name="latitudeVariation">Incremental translation movements inside of the camera plane. A value of 2·PI corresponds to a complete revolution.
/// Notice that for spherical coordinates, this corresponds to actual latitude (elevation).</param>
/// <param name="longitudeVariation">Incremental rotations movements of the camera plane. A value of 2·PI corresponds to a complete revolution.
/// Notice that this corresponds to actual longitude "azimuth" for navigation systems with latitude trajectories independent from longitude.</param>
/// <param name="radialVariation">Incremental movements of the camera approaching to the 3D object (zoom). It is expressed in virtual units in the Z axis
/// between previous and next orbits. </param>
/// <param name="cameraPlanePosition">Out parameter that contains the new camera position within the plane (coordinates X and Y, where X corresponds
/// to the object's intrinsic rotation axis). </param>
/// <param name="planeRotation">Out parameter that contains the rotation that has to be applied to the plane. Expressed in radians.</param>
/// <param name="pointToLook">Out parameter that indicated the direction in which the camera has to look</param>
void CalculateCameraPosition(float latitudeVariation, float longitudeVariation, float radialVariation, Vector2 fieldOfView, out Vector3 cameraPlanePosition, out float planeRotation, out Vector3 pointToLook);
}
public class CSphericalCoordinatesManager : CCoordinateSystemManager
{
// Parametric equations to implemented
// x = r cos (t) t€[0, 2PI]
// z = r sin (t) t€[0, 2PI]
//Parameters that define the camera translation circumference
float r; // Current circumference radius
float t; // Current t parameter that defines camera position on the circumference
//Plane rotation angle
float planeAngle; // Current rotation angle
//Circumference limits
float minimunRadious; // Minimum possible radius
float initialRadious;
//Control variable
bool navigationInitialized = false;
Vector3 extents; // Store the product bounding box extents
TNavigationSystemConstraints navigationConstraints;
public bool Init(Vector3 _extents, Vector3 cameraInitialPosition, TNavigationSystemConstraints _navigationConstraints, out float minimumCameraDistance, out Vector3 pointToLook)
{
/////////////////////
// Save parameters
/////////////////////
extents = _extents;
navigationConstraints = _navigationConstraints;
/////////////////////////////////////////////////////////////////
// Get translation parameters from the initial camera position
///////////////////////////////////////////////////////////////
r = cameraInitialPosition.magnitude; // radius of the circumference passing through that point
t = Mathf.Asin(cameraInitialPosition.z / r); // t = ASin(z/r)
t = MathHom3r.NormalizeAngleInRad(t); // Normalize t angle between 0-2PI
initialRadious = r;
////////////////////////////////
// Initialize plane angle to 0
////////////////////////////////
planeAngle = 0.0f;
////////////////////////////////////////////
// Calculate the minimum radius possible
////////////////////////////////////////////
// Calculating the radius of a circumference that encompasses the object.
//float r01 = MathHom3r.Pow2(_extents.x) + MathHom3r.Pow2(_extents.y) + MathHom3r.Pow2(_extents.z);
//minimunRadious = Mathf.Sqrt(r01);
Debug.Log(extents);
float maxExtent = MathHom3r.Max(extents);
float r01 = 3 * MathHom3r.Pow2(maxExtent);
minimunRadious = Mathf.Sqrt(r01);
Debug.Log(minimunRadious);
minimumCameraDistance = minimunRadious; // update out parameter
//////////////////////////////////////////////////
// Calculate the point which camera has to look
//////////////////////////////////////////////////
pointToLook = CalculatePointToLook();
//////////////////////////////////////
// HELPER
//////////////////////////////////////
this.DrawTranslationTrajectory(r); // Draw the trajectory of the translation, for debug reasons
DrawRotationTrajectory(cameraInitialPosition);
DrawReferenceEllipses(r);
/////////////////////////////////////////////////////////////////
//Check if the proposed initial position for the camera is OK
/////////////////////////////////////////////////////////////////
if (r < minimunRadious)
{
Debug.Log("Error");
return false;
}
else
{
navigationInitialized = true;
return true;
}
}
public void CalculateCameraPosition(float latitudeVariation, float longitudeVariation, float radialVariation, Vector2 fieldOfView, out Vector3 cameraPlanePosition, out float planeRotation, out Vector3 pointToLook)
{
cameraPlanePosition = Vector3.zero;
planeRotation = 0.0f;
pointToLook = Vector3.zero;
if (navigationInitialized)
{
/////////////////////////////////////////////////////////
// Apply pseudoLatitude and pseudoLongitude correction
/////////////////////////////////////////////////////////
//float tVariation = latitudeVariation * CalculateCorrectionParameter(fieldOfView.x);
//float angleVariation = longitudeVariation * CalculateCorrectionParameter(fieldOfView.y);
float tVariation = Calculate_t_Variation(latitudeVariation, fieldOfView.x);
float angleVariation = CalculatePlaneAngleVariation(longitudeVariation, fieldOfView.y);
/////////////////////////////////////////////////////
// Mapping of parameters - Translation parameters
/////////////////////////////////////////////////////
r = CalculateNewR(radialVariation);
// Latitude - which is a translation movement on the camera plane
//t += tVariation; // Add to the current translation angle
//t = MathHom3r.NormalizeAngleInRad(t); // Normalize new t angle
float newt = t + tVariation; // Add to the current translation angle
newt = MathHom3r.NormalizeAngleInRad(newt); // Normalize new t angle
t = ApplyTranslationConstraints(newt);
/////////////////////////////////////////////////////
// Mapping of parameters - Plane rotation parameter
/////////////////////////////////////////////////////
if ((0 < t) && (t < Mathf.PI))
{
angleVariation = -angleVariation; // Depends of the camera position, to avoid that the mouse behaviour change between front and back
}
planeAngle = angleVariation;
//////////////////////////////////////
// Calculate new camera position
//////////////////////////////////////
cameraPlanePosition.x = r * Mathf.Cos(t);
cameraPlanePosition.y = 0;
cameraPlanePosition.z = r * Mathf.Sin(t);
//////////////////////////////////////
// Calculate new plane rotation
//////////////////////////////////////
planeRotation = planeAngle;
//////////////////////////////////////////////////
// Calculate the point which camera hast to look
//////////////////////////////////////////////////
pointToLook = CalculatePointToLook();
//////////////////////////////////////
// HELPER
//////////////////////////////////////
this.DrawTranslationTrajectory(r); // Draw the trajectory of the translation, for debug reasons
DrawRotationTrajectory(cameraPlanePosition);
DrawReferenceEllipses(r);
}
}
/// <summary>
/// Calculate the new value of the circunference radious
/// </summary>
/// <param name="radialVariation"></param>
/// <returns></returns>
private float CalculateNewR(float radialVariation)
{
float new_r;
if (radialVariation <= -10000)
{
//new_r = minimunRadious;
float zoomPercentage = -0.01f * (radialVariation + 10000);
float offset = (initialRadious - minimunRadious) * (1 - zoomPercentage);
new_r = minimunRadious + offset;
}
else if (radialVariation >= 10000)
{
//new_r = initialRadious;
float zoomPercentage = 0.01f * (radialVariation - 10000);
float offset = (initialRadious - minimunRadious) * (1 - zoomPercentage);
new_r = minimunRadious + offset;
}
else
{
new_r = r + radialVariation; // Circumference Radius
if (hom3r.quickLinks.scriptsObject.GetComponent<ConfigurationManager>().GetActiveNavigationZoomMaximumLimit())
{
if (Mathf.Abs(new_r) < minimunRadious) { new_r = minimunRadious; } // We can not closer that minimum
}
else if (new_r <= 0.5f)
{
new_r = 0.5f; // To avoid going back // TODO do it in a better/smarter wa
}
else if (hom3r.quickLinks.scriptsObject.GetComponent<ConfigurationManager>().GetActiveNavigationZoomMinimumLimit())
{
float zoomPercentage = 0.01f * hom3r.quickLinks.scriptsObject.GetComponent<ConfigurationManager>().GetNavigationZoomMinimumLimit();
float offset = (initialRadious - minimunRadious) * (1 - zoomPercentage);
float limit_new_r = minimunRadious + offset;
if (new_r > limit_new_r) { new_r = limit_new_r; }
}
}
return new_r;
}
/// <summary>Calculate the point which camera has to look</summary>
/// <returns>point which camera has to look</returns>
private Vector3 CalculatePointToLook()
{
return Vector3.zero;
}
private float Calculate_t_Variation(float latitudeVariation, float fieldOfView_rad)
{
TInteractionMappingCorrectionMode latitudeCorrection = hom3r.quickLinks.scriptsObject.GetComponent<ConfigurationManager>().GetLatitudeInteractionCorrectionMode();
float final_t_variation = latitudeVariation;
if (latitudeCorrection == TInteractionMappingCorrectionMode.distance)
{
// Get Semiaxes of the inscribed ellipse inside the object
float inscribedCircunfereRadius = GetInscribedTranslationCircunferenceRadious();
//Calculate angle variation in function of the ARC variation
final_t_variation *= CalculateCircunferenceArcMappingFactor(inscribedCircunfereRadius);
//Calculate angle variation applying the distance between camera and object correction
final_t_variation *= CalculateDistanceCorrectionFactor(inscribedCircunfereRadius, fieldOfView_rad);
}
else if (latitudeCorrection == TInteractionMappingCorrectionMode.ellipsePerimeter)
{
// Get Semiaxes of the camera rotation ellipse
float radius = r;
//Calculate angle variation in function of the ARC variation
final_t_variation *= CalculateCircunferenceArcMappingFactor(radius);
// Calculate t-variation applying the ellipse perimeter correction
final_t_variation *= (0.5f * CalculateCircunferencePerimeter(radius));
}
else if (latitudeCorrection == TInteractionMappingCorrectionMode.none)
{
// Get Semiaxes of the camera rotation ellipse
float radius = r;
//Calculate angle variation in function of the ARC variation
final_t_variation *= CalculateCircunferenceArcMappingFactor(radius);
}
return final_t_variation;
}
private float CalculatePlaneAngleVariation(float _longitudeVariation, float fieldOfView_rad)
{
TInteractionMappingCorrectionMode longitudeCorrection = hom3r.quickLinks.scriptsObject.GetComponent<ConfigurationManager>().GetLongitudeInteractionCorrectionMode();
float finalAngleVariation = _longitudeVariation;
if (longitudeCorrection == TInteractionMappingCorrectionMode.distance)
{
// Get Semiaxes of the inscribed ellipse inside the object
float inscribedCircunfereRadius = GetInscribedLongitudeCircunferenceRadious();
//Calculate angle variation in function of the ARC variation
finalAngleVariation *= CalculateCircunferenceArcMappingFactor(inscribedCircunfereRadius);
//Calculate angle variation applying the distance between camera and object correction
finalAngleVariation *= CalculateDistanceCorrectionFactor(inscribedCircunfereRadius, fieldOfView_rad);
}
else if (longitudeCorrection == TInteractionMappingCorrectionMode.ellipsePerimeter)
{
// Get Semiaxes of the camera rotation ellipse
float radius = r;
//Calculate angle variation in function of the ARC variation
finalAngleVariation *= CalculateCircunferenceArcMappingFactor(radius);
// Calculate t-variation applying the ellipse perimeter correction
finalAngleVariation *= (0.5f * CalculateCircunferencePerimeter(radius));
}
else if (longitudeCorrection == TInteractionMappingCorrectionMode.none)
{
// Get Semiaxes of the camera rotation ellipse
float radius = r;
//Calculate angle variation in function of the ARC variation
finalAngleVariation *= CalculateCircunferenceArcMappingFactor(radius);
}
return finalAngleVariation;
}
private float CalculateCircunferencePerimeter(float r)
{
return 2 * Mathf.PI * r;
}
private float CalculateCircunferenceArcMappingFactor(float radious)
{
return (1 / radious);
}
private float GetInscribedTranslationCircunferenceRadious()
{
float rMin = MathHom3r.Max(extents.x, extents.z);
return rMin;
}
private float GetInscribedLongitudeCircunferenceRadious()
{
float rMin = MathHom3r.Max(extents.y, extents.z);
return rMin;
}
/// <summary>
/// Calculate pseudo latitude and pseudo longitude correction.
/// Based on the projection from the camera circumference to the circumference inscribed in the object.
/// </summary>
/// <param name="inscribedCircunferenRadious">radious of the inscribed circunference</param>
/// <param name="fieldOfView_rad">field of view of the camera in radians</param>
/// <returns></returns>
private float CalculateDistanceCorrectionFactor(float inscribedCircunferenRadious, float fieldOfView_rad)
{
// Calculate minimum circumference
float rMin = inscribedCircunferenRadious; //MathHom3r.Max(extents.y, extents.z);
float dPQ = r - rMin;
if (dPQ < 1) { dPQ = 1; } //TODO Delete this Chapuza -> Maybe the problem is that the minimum ellipse is to big
float k = 2 * (dPQ) * Mathf.Tan(fieldOfView_rad);
return k;
}
/// <summary>
/// Calculate pseudo latitude and pseudo longitude correction.
/// Based on the projection from the camera circumference to the circumference inscribed in the object.
/// </summary>
/// <param name="fieldOfView_rad">field of view of the camera in radians</param>
/// <returns></returns>
//private float CalculateCorrectionParameter(float fieldOfView_rad)
//{
// float rMin = MathHom3r.Max(extents);
// float k = 2* (r - rMin) * Mathf.Tan(fieldOfView_rad);
// return k * (1/rMin) ;
//}
/// <summary>
/// Check navigation constraints and applied to the translation t parameter
/// </summary>
/// <param name="desire_tValue"></param>
/// <returns></returns>
private float ApplyTranslationConstraints(float desire_tValue)
{
float new_tValue = t;
if (navigationConstraints == TNavigationSystemConstraints.translationLimited)
{
if (!((0 < desire_tValue) && (desire_tValue < Mathf.PI))) { new_tValue = desire_tValue; } //Translation are limited only in t € [0, PI]
}
else
{
new_tValue = desire_tValue;
}
return new_tValue;
}
//////////////////////////////////////
// HELPER
//////////////////////////////////////
/// <summary>Draw the translation trajectory</summary>
/// <param name="a">Axis mayor ellipse parameter</param>
/// <param name="b">Axis minor ellipse parameter</param>
private void DrawTranslationTrajectory(float radious)
{
hom3r.quickLinks.navigationSystemObject.GetComponentInChildren<NavigationHelper>().DrawTranslationEllipse(radious, radious);
}
/// <summary>Draw the translation trajectory, just for support in the editor</summary>
private void DrawRotationTrajectory(Vector3 _cameraPlanePosition)
{
float radio = _cameraPlanePosition.magnitude;
hom3r.quickLinks.navigationSystemObject.GetComponentInChildren<NavigationHelper>().DrawRotationEllipse(_cameraPlanePosition.z, _cameraPlanePosition.z, _cameraPlanePosition.x);
}
/// <summary>
/// Draw a reference ellipses to help understand camera movements.
/// </summary>
private void DrawReferenceEllipses(float radious)
{
float xRadious = radious;
float zRadious = radious;
float yRadious = radious;
hom3r.quickLinks.navigationSystemObject.GetComponentInChildren<NavigationHelper>().DrawHorizontalFrameworkEllipse(xRadious, zRadious);
hom3r.quickLinks.navigationSystemObject.GetComponentInChildren<NavigationHelper>().DrawVerticalFrameworkEllipse(xRadious, yRadious);
}
}
public class CLimitedSphericalCoordinatesManager : CCoordinateSystemManager
{
// Parametric equations to be implemented
// x = r cos (t) t€[0, 2PI]
// z = r sin (t) t€[0, 2PI]
//Parameters that define the camera translation circumference
float r; // Current circumference radius
float t; // Current t parameter that define camera position on the circumference
//Plane rotation angle
float planeAngle; // Current rotation angle
//Circumference limits
float minimunRadious; // Minimum possible radius
//Control variables
bool navigationInitialized = false;
public bool Init(Vector3 extents, Vector3 cameraInitialPosition, TNavigationSystemConstraints _navigationConstraints, out float minimumCameraDistance, out Vector3 pointToLook)
{
/////////////////////////////////////////////////////////////////
// Get translation parameters from the initial camera position
///////////////////////////////////////////////////////////////
r = cameraInitialPosition.magnitude; //radius of the circumference passing through that point
t = Mathf.Asin(cameraInitialPosition.z / r); // t= ASin(z/r)
t = MathHom3r.NormalizeAngleInRad(t); // Normalize t angle between 0-2PI
////////////////////////////////
// Initialize plane angle to 0
////////////////////////////////
planeAngle = 0.0f;
////////////////////////////////////////////
// Calculate the minimum radius possible
////////////////////////////////////////////
float r01 = MathHom3r.Pow2(extents.x) + MathHom3r.Pow2(extents.y) + MathHom3r.Pow2(extents.z);
minimunRadious = Mathf.Sqrt(r01);
minimumCameraDistance = minimunRadious; // update out parameter
//////////////////////////////////////////////////
// Calculate the point which camera has to look
//////////////////////////////////////////////////
pointToLook = CalculatePointToLook();
/////////////////////////////////////////////////////////////////
//Check if the proposed initial position for the camera is OK
/////////////////////////////////////////////////////////////////
if (r < minimunRadious)
{
Debug.Log("Error");
return false;
}else
{
navigationInitialized = true;
return true;
}
}
public void CalculateCameraPosition(float pseudoLatitude, float pseudoLongitude, float pseudoRadio, Vector2 fieldOfView, out Vector3 cameraPlanePosition, out float planeRotation, out Vector3 pointToLook)
{
cameraPlanePosition = Vector3.zero;
planeRotation = 0.0f;
pointToLook = Vector3.zero;
if (navigationInitialized)
{
/////////////////////////////////////////////////////
// Mapping of parameters - Translation parameters
/////////////////////////////////////////////////////
r += pseudoRadio; // Circumference Radius
if (Mathf.Abs(r) < minimunRadious) { r = minimunRadious; } // We can not go closer than that minimum
// Latitude - which is a translation movement on the camera plane
float newt = t + pseudoLatitude; // Add to the current translation angle
newt = MathHom3r.NormalizeAngleInRad(newt); // Normalize new t angle
if (!((0 < newt) && (newt < Mathf.PI))) { t = newt; } //Translation are limited only in t € [PI, 2PI]
/////////////////////////////////////////////////////
// Mapping of parameters - Plane rotation parameter
/////////////////////////////////////////////////////
planeAngle = pseudoLongitude;
//////////////////////////////////////
// Calculate new camera position
//////////////////////////////////////
cameraPlanePosition.x = r * Mathf.Cos(t);
cameraPlanePosition.y = 0;
cameraPlanePosition.z = r * Mathf.Sin(t);
//////////////////////////////////////
// Calculate new plane rotation
//////////////////////////////////////
planeRotation = planeAngle;
//////////////////////////////////////////////////////
// Calculate the point which camera is going to look
//////////////////////////////////////////////////////
pointToLook = CalculatePointToLook();
}
}
/// <summary>Calculate the point which camera has to look</summary>
/// <returns>point which camera has to look</returns>
private Vector3 CalculatePointToLook()
{
return Vector3.zero;
}
}
public class CSpheroidCoordinatesManager : CCoordinateSystemManager
{
// Parametric equations to implemented
// x = a cos (t) t€[0, 2PI]
// z = b sin (t) t€[0, 2PI]
//Parameters that define the camera translation ellipse
CEllipsoidData ellipsoidData = new CEllipsoidData(); // Current ellipsoid data
CTranslationEllipseData translationEllipseNew = new CTranslationEllipseData(); // Current tranlation ellipse data
//CEllipseData translationEllipse = new CEllipseData();
float t_translationEllipse; // Current t parameter that define camera position on the ellipse
//Plane rotation angle
float planeAngle; // Current rotation angle
//Ellipse limits
CEllipsoidData minimunEllipsoidData;
float minimunAllowedAxis; // Minimum possible minor axis
float initialAxis;
//Object Geometry classification
enum TGeometryType { Prolate, Oblate };
TGeometryType geometryType;
//Control variables
bool navigationInitialized = false;
Vector3 extents; // Store the product bounding box extents
//Vector2 fieldOfView; // Store the camera field of View
TNavigationSystemConstraints navigationConstraints;
float k_lastvalidvalue; // TODO Delete me
public bool Init(Vector3 _extents, Vector3 cameraInitialPosition, TNavigationSystemConstraints _navigationConstraints, out float minimumCameraDistance, out Vector3 pointToLook)
{
/////////////////////
// Save parameters
/////////////////////
extents = _extents;
navigationConstraints = _navigationConstraints;
//////////////////////////////////////////////////
// Identify object geometry type. Long or flat
//////////////////////////////////////////////////
geometryType = ClassifyObjectGeometry(_extents); // Get if the object is flat or long
/////////////////////////////////////////////////////////////////
// Get translation parameters from the initial camera position
///////////////////////////////////////////////////////////////
ellipsoidData = CalculateEllipsoid(_extents, cameraInitialPosition); // Init ellipsoid
translationEllipseNew = CalculateInitialTranslationEllipse_new(); // Calculate semi-axis of the ellipse a,b
t_translationEllipse = CalculateTranslationEllipseInitialT_new(); // Calculate the initial value of t parameter
///////////////////////////////////
// Initialize plane angle to 0
///////////////////////////////////
planeAngle = 0.0f;
////////////////////////////////////////////
// Calculate the minimum radius possible
////////////////////////////////////////////
//minimunAllowedAxis = CalculateMinimunEllipse(_extents, translationEllipse); // Calculate the minimum ellipse semi-axes
minimunAllowedAxis = CalculateMinimunEllipse_new(_extents);
minimumCameraDistance = minimunAllowedAxis; // update out parameter
//////////////////////////////////////////////////
// Calculate the point which camera has to look
//////////////////////////////////////////////////
//pointToLook = CalculatePointToLook(t_translationEllipse, translationEllipse);
pointToLook = CalculatePointToLook_new(t_translationEllipse, translationEllipseNew);
//////////////////////////////////////
// HELPER
//////////////////////////////////////
DrawTranslationTrajectory(); // Draw the trajectory of the translation, for debug reasons
DrawRotationTrajectory(cameraInitialPosition);
DrawReferenceEllipses();
/////////////////////////////////////////////////////////////////
//Check if the proposed initial position for the camera is OK
/////////////////////////////////////////////////////////////////
if (CheckInitialMinimunDistance(cameraInitialPosition))
{
navigationInitialized = true;
initialAxis = Mathf.Abs(cameraInitialPosition.z);
return true;
}
else
{
Debug.Log("Error");
return false;
}
}
public void CalculateCameraPosition(float latitudeVariation, float longitudeVariation, float radialVariation, Vector2 _fieldOfView, out Vector3 cameraPlanePosition, out float planeRotation, out Vector3 pointToLook)
{
cameraPlanePosition = Vector3.zero;
planeRotation = 0.0f;
pointToLook = Vector3.zero;
if (navigationInitialized)
{
// Update field of view
//fieldOfView = _fieldOfView;
/////////////////////////////////////////////////////////
// Apply pseudoLatitude and pseudoLongitude correction
/////////////////////////////////////////////////////////
//latitudeVariation = latitudeVariation * CalculatePseudoLatitudeMappingFactor(fieldOfView.x) * CalculatePseudoLatitudeCorrectionParameter(fieldOfView.x, translationEllipse);
float tVariation = Calculate_t_Variation(latitudeVariation, _fieldOfView.x);
float angleVariation = CalculatePlaneAngleVariation(longitudeVariation, _fieldOfView.y);
//longitudeVariation = longitudeVariation * CalculatePseudoLongitudeCorrectionParameter(fieldOfView.y, translationEllipse);
/////////////////////////////////////////////////////
// Mapping of parameters - Translation parameters
/////////////////////////////////////////////////////
//translationEllipse = CalculateNewEllipseSemiAxesParametersAfterRadialMovement(radialVariation, translationEllipse);
CalculateNewEllipseSemiAxesParametersAfterRadialMovement_New(radialVariation);
translationEllipseNew = CalculateInitialTranslationEllipse_new();
// Latitude - which is a translation movement on the camera plane
//t_translationEllipse += tVariation; // Add to the current translation angle
//t_translationEllipse = MathHom3r.NormalizeAngleInRad(t_translationEllipse); // Normalize new t angle
float new_t_translationEllipse = t_translationEllipse + tVariation; // Calculate new translation angle
new_t_translationEllipse = MathHom3r.NormalizeAngleInRad(new_t_translationEllipse); // Normalize new t angle
t_translationEllipse = ApplyTranslationConstraints(new_t_translationEllipse);
/////////////////////////////////////////////////////
// Mapping of parameters - Plane rotation parameter
/////////////////////////////////////////////////////
if ((0 < t_translationEllipse) && (t_translationEllipse < Mathf.PI))
{
angleVariation = -angleVariation; // Depends of the camera position, to avoid that the mouse behaviour change between front and back
}
planeAngle = angleVariation;
//////////////////////////////////////
// Calculate new camera position
//////////////////////////////////////
//cameraPlanePosition = CalculateNewCameraPosition(t_translationEllipse, translationEllipse);
cameraPlanePosition = CalculateNewCameraPosition_New(t_translationEllipse);
//////////////////////////////////////
// Calculate new plane rotation
//////////////////////////////////////
planeRotation = planeAngle;
//////////////////////////////////////////////////
// Calculate the point which camera hast to look
//////////////////////////////////////////////////
//pointToLook = CalculatePointToLook(t_translationEllipse, translationEllipse);
pointToLook = CalculatePointToLook_new(t_translationEllipse, translationEllipseNew);
//////////////////////////////////////
// HELPER
//////////////////////////////////////
DrawTranslationTrajectory(); // Draw the trajectory of the translation, for debug reasons
DrawRotationTrajectory(cameraPlanePosition);
//DrawCameraPosition(cameraPlanePosition, pointToLook);
}
}
/////////////////////////////////
// INIT Auxiliary Methods
/////////////////////////////////
/// <summary>
/// Classify the object geometry. It decides if the object is long or flat.
/// An object is long when its larger dimension coincides with the rotation axis.
/// An object if flat when its larger dimension doesn't coincide with the rotation axis.
/// </summary>
/// <param name="boundingBox"></param>
/// <returns></returns>
private TGeometryType ClassifyObjectGeometry(Vector3 extents)
{
if ((extents.x >= extents.y) && (extents.x >= extents.z)) {
Debug.Log("Prolate Object");
hom3r.coreLink.GetComponent<ConfigurationManager>().SetNavigationObjectType("Prolate");
return TGeometryType.Prolate;
}
else {
Debug.Log("Oblate Object");
hom3r.coreLink.GetComponent<ConfigurationManager>().SetNavigationObjectType("Oblate");
return TGeometryType.Oblate;
}
}
/// <summary>
/// Calculate the reference ellipses, these are the ellipses on the xz and xy plane that frame the projections of the object on those planes
/// </summary>
/// <param name="extents"></param>
/// <param name="cameraPosition"></param>
private CEllipsoidData CalculateEllipsoid(Vector3 extents, Vector3 cameraPosition)
{
CEllipsoidData _ellipsoidData = new CEllipsoidData();
if (geometryType == TGeometryType.Prolate)
{
_ellipsoidData.SetEvoluteCups(extents.x, extents.z); // Evolute cusp will be in the limit of the object in X-axis
_ellipsoidData.radiousZAxis = Mathf.Abs(cameraPosition.z);
_ellipsoidData.radiousXAxis = CalculateMayorAxis(_ellipsoidData.radiousZAxis, extents.x);
_ellipsoidData.radiousYAxis = Mathf.Abs(cameraPosition.z);
}
else if (geometryType == TGeometryType.Oblate)
{
float _EC = GetEvoluteCupsAccordingCorrection();
_ellipsoidData.SetEvoluteCups(extents.x, extents.z);
_ellipsoidData.radiousZAxis = Mathf.Abs(cameraPosition.z);
_ellipsoidData.radiousXAxis = CalculateMinorAxis(_ellipsoidData.radiousZAxis, _EC);
// // The semimayor axis is on the z axis, but its value depends on whether it is mayor ez or ey
// if (extents.z >= extents.y)
// {
//_ellipsoidData.SetEvoluteCups(extents.x, extents.z);
//_ellipsoidData.radiousZAxis = Mathf.Abs(cameraPosition.z);
// _ellipsoidData.radiousXAxis = CalculateMinorAxis(_ellipsoidData.radiousZAxis, extents.z);
// }
// else {
//_ellipsoidData.SetEvoluteCups(extents.x, extents.y);
// //_ellipsoidData.SetEvoluteCups(extents.x, extents.z); //Testing solve limitation
// _ellipsoidData.radiousZAxis = Mathf.Abs(cameraPosition.z);
// _ellipsoidData.radiousXAxis = CalculateMinorAxis(_ellipsoidData.radiousZAxis, extents.y);
// }
_ellipsoidData.radiousYAxis = Mathf.Abs(cameraPosition.z); // Because it is a rotation around X (Spheroid)
}
return _ellipsoidData;
}
private float GetEvoluteCupsAccordingCorrection()
{
if (hom3r.coreLink.GetComponent<ConfigurationManager>().GetActiveNavigationOblateCorrection() == TOblateSpheroidCorrectionMode.none)
{
return Mathf.Max(extents.z, extents.y);
}
else if (hom3r.coreLink.GetComponent<ConfigurationManager>().GetActiveNavigationOblateCorrection() == TOblateSpheroidCorrectionMode.ec_minimun)
{
return Mathf.Min(extents.z, extents.y);
}
else if (hom3r.coreLink.GetComponent<ConfigurationManager>().GetActiveNavigationOblateCorrection() == TOblateSpheroidCorrectionMode.ec_averaged)
{
return 0.5f * (extents.z + extents.y);
}
else if (hom3r.coreLink.GetComponent<ConfigurationManager>().GetActiveNavigationOblateCorrection() == TOblateSpheroidCorrectionMode.cameraOrientationDepedentRotation)
{
return Mathf.Max(extents.z, extents.y);
}
return 0f;
}
private float CalculateMayorAxis(float minorAxis, float Ec)
{
return 0.5f * (Ec + Mathf.Sqrt(MathHom3r.Pow2(Ec) + 4 * MathHom3r.Pow2(minorAxis)));
}
private float CalculateMinorAxis(float mayorAxis, float Ec)
{
return Mathf.Sqrt(MathHom3r.Pow2(mayorAxis) - (Ec * mayorAxis));
}
private float CalculateEllipseEvoluteCusps(float mayorAxis, float minorAxis)
{
return mayorAxis - (MathHom3r.Pow2(minorAxis) / mayorAxis);
}
/// <summary>
/// Calculate the parameter c according to the geometry of the object and its dimensions.
/// If the object is long, c is the extension of the object on the x-axis.
/// If the object is flat, c is the extension of the object on the z-axis.
/// </summary>
/// <param name="extents">Object Bounding Box extents</param>
/// <returns></returns>
private float CalculateEvolute(Vector3 extents)
{
if (geometryType == TGeometryType.Prolate) { return extents.x; }
else {
return extents.z;
//return Mathf.Max(extents.z, extents.y) ;
}
}
private CTranslationEllipseData CalculateInitialTranslationEllipse_new()
{
CTranslationEllipseData _ellipse = new CTranslationEllipseData();
_ellipse.radiousXAxis = ellipsoidData.radiousXAxis;
_ellipse.radiousZAxis = ellipsoidData.radiousZAxis;
_ellipse.evoluteCusp = ellipsoidData.GetEvoluteCusp(_ellipse.GetSemiMajorAxis());
return _ellipse;
}
/// <summary>
/// Calculate the initial T parameter of the ellipse based on b/a and current camera position
/// </summary>
/// <param name="cameraPosition">Current camera position</param>
/// <returns>return the t parameter float value</returns>
private float CalculateTranslationEllipseInitialT_new()
{
return (Mathf.PI * -0.5f);
}
/// <summary>
/// Calculate the minimum ellipse that which surrounds the 3d object without cross it.
/// In the case of long object this value is the b minimum,
/// while in the case of flat objects this vale is the a minimum.
/// </summary>
/// <param name="extents">Bounding box extends of the 3D object</param>
/// <returns>Return the minimum a of b of the allowed ellipse between the camera and the object</returns>
//private float CalculateMinimunEllipse(Vector3 extents, CEllipseData ellipse)
//{
// float minimunAxis = 0.0f;
// // Polynomial Coefficients: x^3 + a1 * x^2 + a2 * x + a3 = 0
// float a1 = -ellipse.Ec;
// float a2;
// if (geometryType == TGeometryType.Prolate) {
// a2 = -(MathHom3r.Pow2(ellipse.Ec) + MathHom3r.Pow2(extents.y) + MathHom3r.Pow2(extents.z));
// }
// else
// {
// a2 = -(MathHom3r.Pow2(ellipse.Ec) + MathHom3r.Pow2(extents.y) + MathHom3r.Pow2(extents.z));
// }
// float a3 = Mathf.Pow(ellipse.Ec, 3);
// //Calculate Q, R
// float Q = (1 / 9.0f) * (3.0f * a2 - MathHom3r.Pow2(a1));
// float R = (1 / 54.0f) * (9.0f * a1 * a2 - 27 * a3 - 2 * Mathf.Pow(a1, 3));
// //Calculate Q^3 and R^2
// float Q3 = Mathf.Pow(Q, 3);
// //float R2 = Mathf.Pow(R, 2);
// //Calculate D
// //float D = Q3 + R2;
// // D is always < 0 in our case
// //if (D < 0)
// //{
// float teta = Mathf.Acos(-R / Mathf.Sqrt(-Q3));
// float x2 = -2.0f * Mathf.Sqrt(-Q) * Mathf.Cos((teta + (2.0f * Constants.Pi)) / 3.0f) - (a1 / 3.0f);
// //}
// if (geometryType == TGeometryType.Prolate)
// {
// float b = Mathf.Sqrt(MathHom3r.Pow2(x2) - x2 * ellipse.Ec);
// minimunAxis = b; // Long object return the b minimum of the ellipse
// }
// else
// {
// minimunAxis = x2; //Flat object return a minimum of the ellipse
// }
// //Debug.Log(minimunAxis);
// return minimunAxis;
//}
private float CalculateMinimunEllipse_new(Vector3 extents)
{
float minimunAxis = 0.0f;
// Initial data
float Ec = ellipsoidData.GetEvoluteCusp(translationEllipseNew.GetSemiMajorAxis());
float r2 = MathHom3r.Pow2(extents.y) + MathHom3r.Pow2(extents.z);
// Polynomial Coefficients: x^3 + a1 * x^2 + a2 * x + a3 = 0
float A = -Ec;