forked from CelestiaProject/Celestia
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathobserver.cpp
More file actions
1582 lines (1294 loc) · 49.6 KB
/
observer.cpp
File metadata and controls
1582 lines (1294 loc) · 49.6 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
// observer.cpp
//
// Copyright (C) 2001-2009, the Celestia Development Team
// Original version by Chris Laurel <claurel@gmail.com>
//
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License
// as published by the Free Software Foundation; either version 2
// of the License, or (at your option) any later version.
#include "observer.h"
#include "simulation.h"
#include "frametree.h"
#include <celmath/mathlib.h>
#include <celmath/solve.h>
#include <celmath/geomutil.h>
static const double maximumSimTime = 730486721060.00073; // 2000000000 Jan 01 12:00:00 UTC
static const double minimumSimTime = -730498278941.99951; // -2000000000 Jan 01 12:00:00 UTC
using namespace Eigen;
using namespace std;
using namespace celmath;
#define VELOCITY_CHANGE_TIME 0.25f
static Vector3d slerp(double t, const Vector3d& v0, const Vector3d& v1)
{
double r0 = v0.norm();
double r1 = v1.norm();
Vector3d u = v0 / r0;
Vector3d n = u.cross(v1 / r1);
n.normalize();
Vector3d v = n.cross(u);
if (v.dot(v1) < 0.0)
v = -v;
double cosTheta = u.dot(v1 / r1);
double theta = acos(cosTheta);
return (cos(theta * t) * u + sin(theta * t) * v) * lerp(t, r0, r1);
}
/*! Notes on the Observer class
* The values position and orientation are in observer's reference frame. positionUniv
* and orientationUniv are the equivalent values in the universal coordinate system.
* They must be kept in sync. Generally, it's position and orientation that are modified;
* after they're changed, the method updateUniversal is called. However, when the observer
* frame is changed, positionUniv and orientationUniv are not changed, but the position
* and orientation within the frame /do/ change. Thus, a 'reverse' update is necessary.
*
* There are two types of 'automatic' updates to position and orientation that may
* occur when the observer's update method is called: updates from free travel, and
* updates due to an active goto operation.
*/
Observer::Observer() :
frame(new ObserverFrame)
{
updateUniversal();
}
/*! Copy constructor. */
Observer::Observer(const Observer& o) :
simTime(o.simTime),
position(o.position),
orientation(o.orientation),
velocity(o.velocity),
angularVelocity(o.angularVelocity),
realTime(o.realTime),
targetSpeed(o.targetSpeed),
targetVelocity(o.targetVelocity),
beginAccelTime(o.beginAccelTime),
observerMode(o.observerMode),
journey(o.journey),
trackObject(o.trackObject),
trackingOrientation(o.trackingOrientation),
fov(o.fov),
reverseFlag(o.reverseFlag),
locationFilter(o.locationFilter),
displayedSurface(o.displayedSurface)
{
setFrame(o.frame);
updateUniversal();
}
Observer& Observer::operator=(const Observer& o)
{
simTime = o.simTime;
position = o.position;
orientation = o.orientation;
velocity = o.velocity;
angularVelocity = o.angularVelocity;
frame = nullptr;
realTime = o.realTime;
targetSpeed = o.targetSpeed;
targetVelocity = o.targetVelocity;
beginAccelTime = o.beginAccelTime;
observerMode = o.observerMode;
journey = o.journey;
trackObject = o.trackObject;
trackingOrientation = o.trackingOrientation;
fov = o.fov;
reverseFlag = o.reverseFlag;
locationFilter = o.locationFilter;
displayedSurface = o.displayedSurface;
setFrame(o.frame);
updateUniversal();
return *this;
}
/*! Get the current simulation time. The time returned is a Julian date,
* and the time standard is TDB.
*/
double Observer::getTime() const
{
return simTime;
}
/*! Get the current real time. The time returned is a Julian date,
* and the time standard is TDB.
*/
double Observer::getRealTime() const
{
return realTime;
}
/*! Set the simulation time (Julian date, TDB time standard)
*/
void Observer::setTime(double jd)
{
simTime = jd;
updateUniversal();
}
/*! Return the position of the observer in universal coordinates. The origin
* The origin of this coordinate system is the Solar System Barycenter, and
* axes are defined by the J2000 ecliptic and equinox.
*/
UniversalCoord Observer::getPosition() const
{
return positionUniv;
}
#if 0
// TODO: Low-precision set position that should be removed
void Observer::setPosition(const Vector3d& p)
{
setPosition(UniversalCoord(p));
}
#endif
/*! Set the position of the observer; position is specified in the universal
* coordinate system.
*/
void Observer::setPosition(const UniversalCoord& p)
{
positionUniv = p;
position = frame->convertFromUniversal(p, getTime());
}
/*! Return the orientation of the observer in the universal coordinate
* system.
*/
Quaterniond Observer::getOrientation() const
{
return orientationUniv;
}
/*! Reduced precision version of getOrientation()
*/
Quaternionf Observer::getOrientationf() const
{
return getOrientation().cast<float>();
}
/* Set the orientation of the observer. The orientation is specified in
* the universal coordinate system.
*/
void Observer::setOrientation(const Quaternionf& q)
{
/*
RigidTransform rt = frame.toUniversal(situation, getTime());
rt.rotation = Quatd(q.w, q.x, q.y, q.z);
situation = frame.fromUniversal(rt, getTime());
*/
setOrientation(q.cast<double>());
}
/*! Set the orientation of the observer. The orientation is specified in
* the universal coordinate system.
*/
void Observer::setOrientation(const Quaterniond& q)
{
orientationUniv = q;
orientation = frame->convertFromUniversal(q, getTime());
}
/*! Get the velocity of the observer within the observer's reference frame.
*/
Vector3d Observer::getVelocity() const
{
return velocity;
}
/*! Set the velocity of the observer within the observer's reference frame.
*/
void Observer::setVelocity(const Vector3d& v)
{
velocity = v;
}
Vector3d Observer::getAngularVelocity() const
{
return angularVelocity;
}
void Observer::setAngularVelocity(const Vector3d& v)
{
angularVelocity = v;
}
double Observer::getArrivalTime() const
{
if (observerMode != Travelling)
return realTime;
return journey.startTime + journey.duration;
}
/*! Tick the simulation by dt seconds. Update the observer position
* and orientation due to an active goto command or non-zero velocity
* or angular velocity.
*/
void Observer::update(double dt, double timeScale)
{
realTime += dt;
simTime += (dt / 86400.0) * timeScale;
if (simTime >= maximumSimTime)
simTime = maximumSimTime;
if (simTime <= minimumSimTime)
simTime = minimumSimTime;
if (observerMode == Travelling)
{
// Compute the fraction of the trip that has elapsed; handle zero
// durations correctly by skipping directly to the destination.
float t = 1.0;
if (journey.duration > 0)
t = (float) clamp((realTime - journey.startTime) / journey.duration);
Vector3d jv = journey.to.offsetFromKm(journey.from);
UniversalCoord p;
// Another interpolation method . . . accelerate exponentially,
// maintain a constant velocity for a period of time, then
// decelerate. The portion of the trip spent accelerating is
// controlled by the parameter journey.accelTime; a value of 1 means
// that the entire first half of the trip will be spent accelerating
// and there will be no coasting at constant velocity.
{
double u = t < 0.5 ? t * 2 : (1 - t) * 2;
double x;
if (u < journey.accelTime)
{
x = exp(journey.expFactor * u) - 1.0;
}
else
{
x = exp(journey.expFactor * journey.accelTime) *
(journey.expFactor * (u - journey.accelTime) + 1) - 1;
}
if (journey.traj == Linear)
{
Vector3d v = jv;
if (v.norm() == 0.0)
{
p = journey.from;
}
else
{
v.normalize();
if (t < 0.5)
p = journey.from.offsetKm(v * x);
else
p = journey.to.offsetKm(-v * x);
}
}
else if (journey.traj == GreatCircle)
{
Selection centerObj = frame->getRefObject();
if (centerObj.body() != nullptr)
{
Body* body = centerObj.body();
if (body->getSystem())
{
if (body->getSystem()->getPrimaryBody() != nullptr)
centerObj = Selection(body->getSystem()->getPrimaryBody());
else
centerObj = Selection(body->getSystem()->getStar());
}
}
UniversalCoord ufrom = frame->convertToUniversal(journey.from, simTime);
UniversalCoord uto = frame->convertToUniversal(journey.to, simTime);
UniversalCoord origin = centerObj.getPosition(simTime);
Vector3d v0 = ufrom.offsetFromKm(origin);
Vector3d v1 = uto.offsetFromKm(origin);
if (jv.norm() == 0.0)
{
p = journey.from;
}
else
{
x /= jv.norm();
Vector3d v;
if (t < 0.5)
v = slerp(x, v0, v1);
else
v = slerp(x, v1, v0);
p = frame->convertFromUniversal(origin.offsetKm(v), simTime);
}
}
else if (journey.traj == CircularOrbit)
{
Selection centerObj = frame->getRefObject();
UniversalCoord ufrom = frame->convertToUniversal(journey.from, simTime);
//UniversalCoord uto = frame->convertToUniversal(journey.to, simTime);
UniversalCoord origin = centerObj.getPosition(simTime);
Vector3d v0 = ufrom.offsetFromKm(origin);
//Vector3d v1 = uto.offsetFromKm(origin);
if (jv.norm() == 0.0)
{
p = journey.from;
}
else
{
Quaterniond q0(Quaterniond::Identity());
Quaterniond q1 = journey.rotation1;
p = origin.offsetKm(q0.slerp(t, q1).conjugate() * v0);
p = frame->convertFromUniversal(p, simTime);
}
}
}
// Spherically interpolate the orientation over the first half
// of the journey.
Quaterniond q;
if (t >= journey.startInterpolation && t < journey.endInterpolation )
{
// Smooth out the interpolation to avoid jarring changes in
// orientation
double v;
if (journey.traj == CircularOrbit)
{
// In circular orbit mode, interpolation of orientation must
// match the interpolation of position.
v = t;
}
else
{
v = pow(sin((t - journey.startInterpolation) /
(journey.endInterpolation - journey.startInterpolation) * PI / 2), 2);
}
q = journey.initialOrientation.slerp(v, journey.finalOrientation);
}
else if (t < journey.startInterpolation)
{
q = journey.initialOrientation;
}
else // t >= endInterpolation
{
q = journey.finalOrientation;
}
position = p;
orientation = q;
// If the journey's complete, reset to manual control
if (t == 1.0f)
{
if (journey.traj != CircularOrbit)
{
//situation = RigidTransform(journey.to, journey.finalOrientation);
position = journey.to;
orientation = journey.finalOrientation;
}
observerMode = Free;
setVelocity(Vector3d::Zero());
}
}
if (getVelocity() != targetVelocity)
{
double t = clamp((realTime - beginAccelTime) / VELOCITY_CHANGE_TIME);
Vector3d v = getVelocity() * (1.0 - t) + targetVelocity * t;
// At some threshold, we just set the velocity to zero; otherwise,
// we'll end up with ridiculous velocities like 10^-40 m/s.
if (v.norm() < 1.0e-12)
v = Vector3d::Zero();
setVelocity(v);
}
// Update the position
position = position.offsetKm(getVelocity() * dt);
if (observerMode == Free)
{
// Update the observer's orientation
Vector3d halfAV = getAngularVelocity() * 0.5;
Quaterniond dr = Quaterniond(0.0, halfAV.x(), halfAV.y(), halfAV.z()) * orientation;
orientation = Quaterniond(orientation.coeffs() + dt * dr.coeffs());
orientation.normalize();
}
updateUniversal();
// Update orientation for tracking--must occur after updateUniversal(), as it
// relies on the universal position and orientation of the observer.
if (!trackObject.empty())
{
Vector3d up = getOrientation().conjugate() * Vector3d::UnitY();
Vector3d viewDir = trackObject.getPosition(getTime()).offsetFromKm(getPosition()).normalized();
setOrientation(LookAt<double>(Vector3d::Zero(), viewDir, up));
}
}
Selection Observer::getTrackedObject() const
{
return trackObject;
}
void Observer::setTrackedObject(const Selection& sel)
{
trackObject = sel;
}
const string& Observer::getDisplayedSurface() const
{
return displayedSurface;
}
void Observer::setDisplayedSurface(const string& surf)
{
displayedSurface = surf;
}
uint64_t Observer::getLocationFilter() const
{
return locationFilter;
}
void Observer::setLocationFilter(uint64_t _locationFilter)
{
locationFilter = _locationFilter;
}
void Observer::reverseOrientation()
{
setOrientation(getOrientation() * Quaterniond(AngleAxisd(PI, Vector3d::UnitY())));
reverseFlag = !reverseFlag;
}
struct TravelExpFunc
{
double dist, s;
TravelExpFunc(double d, double _s) : dist(d), s(_s) {};
double operator()(double x) const
{
// return (1.0 / x) * (exp(x / 2.0) - 1.0) - 0.5 - dist / 2.0;
return exp(x * s) * (x * (1 - s) + 1) - 1 - dist;
}
};
void Observer::computeGotoParameters(const Selection& destination,
JourneyParams& jparams,
double gotoTime,
double startInter,
double endInter,
const Vector3d& offset,
ObserverFrame::CoordinateSystem offsetCoordSys,
const Vector3f& up,
ObserverFrame::CoordinateSystem upCoordSys)
{
if (frame->getCoordinateSystem() == ObserverFrame::PhaseLock)
{
//setFrame(FrameOfReference(astro::Ecliptical, destination));
setFrame(ObserverFrame::Ecliptical, destination);
}
else
{
setFrame(frame->getCoordinateSystem(), destination);
}
UniversalCoord targetPosition = destination.getPosition(getTime());
//Vector3d v = targetPosition.offsetFromKm(getPosition()).normalized();
jparams.traj = Linear;
jparams.duration = gotoTime;
jparams.startTime = realTime;
// Right where we are now . . .
jparams.from = getPosition();
if (offsetCoordSys == ObserverFrame::ObserverLocal)
{
jparams.to = targetPosition.offsetKm(orientationUniv.conjugate() * offset);
}
else
{
ObserverFrame offsetFrame(offsetCoordSys, destination);
jparams.to = targetPosition.offsetKm(offsetFrame.getFrame()->getOrientation(getTime()).conjugate() * offset);
}
Vector3d upd = up.cast<double>();
if (upCoordSys == ObserverFrame::ObserverLocal)
{
upd = orientationUniv.conjugate() * upd;
}
else
{
ObserverFrame upFrame(upCoordSys, destination);
upd = upFrame.getFrame()->getOrientation(getTime()).conjugate() * upd;
}
jparams.initialOrientation = getOrientation();
Vector3d focus = targetPosition.offsetFromKm(jparams.to);
jparams.finalOrientation = LookAt<double>(Vector3d::Zero(), focus, upd);
jparams.startInterpolation = min(startInter, endInter);
jparams.endInterpolation = max(startInter, endInter);
jparams.accelTime = 0.5;
double distance = jparams.from.offsetFromKm(jparams.to).norm() / 2.0;
pair<double, double> sol = solve_bisection(TravelExpFunc(distance, jparams.accelTime),
0.0001, 100.0,
1e-10);
jparams.expFactor = sol.first;
// Convert to frame coordinates
jparams.from = frame->convertFromUniversal(jparams.from, getTime());
jparams.initialOrientation = frame->convertFromUniversal(jparams.initialOrientation, getTime());
jparams.to = frame->convertFromUniversal(jparams.to, getTime());
jparams.finalOrientation = frame->convertFromUniversal(jparams.finalOrientation, getTime());
}
void Observer::computeGotoParametersGC(const Selection& destination,
JourneyParams& jparams,
double gotoTime,
double startInter,
double endInter,
const Vector3d& offset,
ObserverFrame::CoordinateSystem offsetCoordSys,
const Vector3f& up,
ObserverFrame::CoordinateSystem upCoordSys,
const Selection& centerObj)
{
setFrame(frame->getCoordinateSystem(), destination);
UniversalCoord targetPosition = destination.getPosition(getTime());
//Vector3d v = targetPosition.offsetFromKm(getPosition()).normalized();
jparams.traj = GreatCircle;
jparams.duration = gotoTime;
jparams.startTime = realTime;
jparams.centerObject = centerObj;
// Right where we are now . . .
jparams.from = getPosition();
ObserverFrame offsetFrame(offsetCoordSys, destination);
Vector3d offsetTransformed = offsetFrame.getFrame()->getOrientation(getTime()).conjugate() * offset;
jparams.to = targetPosition.offsetKm(offsetTransformed);
Vector3d upd = up.cast<double>();
if (upCoordSys == ObserverFrame::ObserverLocal)
{
upd = orientationUniv.conjugate() * upd;
}
else
{
ObserverFrame upFrame(upCoordSys, destination);
upd = upFrame.getFrame()->getOrientation(getTime()).conjugate() * upd;
}
jparams.initialOrientation = getOrientation();
Vector3d focus = targetPosition.offsetFromKm(jparams.to);
jparams.finalOrientation = LookAt<double>(Vector3d::Zero(), focus, upd);
jparams.startInterpolation = min(startInter, endInter);
jparams.endInterpolation = max(startInter, endInter);
jparams.accelTime = 0.5;
double distance = jparams.from.offsetFromKm(jparams.to).norm() / 2.0;
pair<double, double> sol = solve_bisection(TravelExpFunc(distance, jparams.accelTime),
0.0001, 100.0,
1e-10);
jparams.expFactor = sol.first;
// Convert to frame coordinates
jparams.from = frame->convertFromUniversal(jparams.from, getTime());
jparams.initialOrientation = frame->convertFromUniversal(jparams.initialOrientation, getTime());
jparams.to = frame->convertFromUniversal(jparams.to, getTime());
jparams.finalOrientation = frame->convertFromUniversal(jparams.finalOrientation, getTime());
}
void Observer::computeCenterParameters(const Selection& destination,
JourneyParams& jparams,
double centerTime)
{
UniversalCoord targetPosition = destination.getPosition(getTime());
jparams.duration = centerTime;
jparams.startTime = realTime;
jparams.traj = Linear;
// Don't move through space, just rotate the camera
jparams.from = getPosition();
jparams.to = jparams.from;
Vector3d up = getOrientation().conjugate() * Vector3d::UnitY();
jparams.initialOrientation = getOrientation();
Vector3d focus = targetPosition.offsetFromKm(jparams.to);
jparams.finalOrientation = LookAt<double>(Vector3d::Zero(), focus, up);
jparams.startInterpolation = 0;
jparams.endInterpolation = 1;
jparams.accelTime = 0.5;
jparams.expFactor = 0;
// Convert to frame coordinates
jparams.from = frame->convertFromUniversal(jparams.from, getTime());
jparams.initialOrientation = frame->convertFromUniversal(jparams.initialOrientation, getTime());
jparams.to = frame->convertFromUniversal(jparams.to, getTime());
jparams.finalOrientation = frame->convertFromUniversal(jparams.finalOrientation, getTime());
}
void Observer::computeCenterCOParameters(const Selection& destination,
JourneyParams& jparams,
double centerTime)
{
jparams.duration = centerTime;
jparams.startTime = realTime;
jparams.traj = CircularOrbit;
jparams.centerObject = frame->getRefObject();
jparams.expFactor = 0.5;
Vector3d v = destination.getPosition(getTime()).offsetFromKm(getPosition()).normalized();
Vector3d w = getOrientation().conjugate() * -Vector3d::UnitZ();
Selection centerObj = frame->getRefObject();
UniversalCoord centerPos = centerObj.getPosition(getTime());
//UniversalCoord targetPosition = destination.getPosition(getTime());
Quaterniond q;
q.setFromTwoVectors(v, w);
jparams.from = getPosition();
jparams.to = centerPos.offsetKm(q.conjugate() * getPosition().offsetFromKm(centerPos));
jparams.initialOrientation = getOrientation();
jparams.finalOrientation = getOrientation() * q;
jparams.startInterpolation = 0.0;
jparams.endInterpolation = 1.0;
jparams.rotation1 = q;
// Convert to frame coordinates
jparams.from = frame->convertFromUniversal(jparams.from, getTime());
jparams.initialOrientation = frame->convertFromUniversal(jparams.initialOrientation, getTime());
jparams.to = frame->convertFromUniversal(jparams.to, getTime());
jparams.finalOrientation = frame->convertFromUniversal(jparams.finalOrientation, getTime());
}
/*! Center the selection by moving on a circular orbit arround
* the primary body (refObject).
*/
void Observer::centerSelectionCO(const Selection& selection, double centerTime)
{
if (!selection.empty() && !frame->getRefObject().empty())
{
computeCenterCOParameters(selection, journey, centerTime);
observerMode = Travelling;
}
}
Observer::ObserverMode Observer::getMode() const
{
return observerMode;
}
void Observer::setMode(Observer::ObserverMode mode)
{
observerMode = mode;
}
// Private method to convert coordinates when a new observer frame is set.
// Universal coordinates remain the same. All frame coordinates get updated, including
// the goto parameters.
void Observer::convertFrameCoordinates(const ObserverFrame::SharedConstPtr &newFrame)
{
double now = getTime();
// Universal coordinates don't change.
// Convert frame coordinates to the new frame.
position = newFrame->convertFromUniversal(positionUniv, now);
orientation = newFrame->convertFromUniversal(orientationUniv, now);
// Convert goto parameters to the new frame
journey.from = ObserverFrame::convert(frame, newFrame, journey.from, now);
journey.initialOrientation = ObserverFrame::convert(frame, newFrame, journey.initialOrientation, now);
journey.to = ObserverFrame::convert(frame, newFrame, journey.to, now);
journey.finalOrientation = ObserverFrame::convert(frame, newFrame, journey.finalOrientation, now);
}
/*! Set the observer's reference frame. The position of the observer in
* universal coordinates will not change.
*/
void Observer::setFrame(ObserverFrame::CoordinateSystem cs, const Selection& refObj, const Selection& targetObj)
{
auto newFrame = shared_ptr<ObserverFrame>(new ObserverFrame(cs, refObj, targetObj));
convertFrameCoordinates(newFrame);
frame = newFrame;
}
/*! Set the observer's reference frame. The position of the observer in
* universal coordinates will not change.
*/
void Observer::setFrame(ObserverFrame::CoordinateSystem cs, const Selection& refObj)
{
setFrame(cs, refObj, Selection());
}
/*! Set the observer's reference frame. The position of the observer in
* universal coordinates will not change.
*/
void Observer::setFrame(const ObserverFrame::SharedConstPtr& f)
{
if (frame != f)
{
if (frame)
{
convertFrameCoordinates(f);
}
frame = f;
}
}
/*! Get the current reference frame for the observer.
*/
const ObserverFrame::SharedConstPtr& Observer::getFrame() const
{
return frame;
}
/*! Rotate the observer about its center.
*/
void Observer::rotate(const Quaternionf& q)
{
orientation = q.cast<double>() * orientation;
updateUniversal();
}
/*! Orbit around the reference object (if there is one.) This involves changing
* both the observer's position and orientation. If there is no current center
* object, the specified selection will be used as the center of rotation, and
* the observer reference frame will be modified.
*/
void Observer::orbit(const Selection& selection, const Quaternionf& q)
{
Selection center = frame->getRefObject();
if (center.empty() && !selection.empty())
{
// Automatically set the center of the reference frame
center = selection;
setFrame(frame->getCoordinateSystem(), center);
}
if (!center.empty())
{
// Get the focus position (center of rotation) in frame
// coordinates; in order to make this function work in all
// frames of reference, it's important to work in frame
// coordinates.
UniversalCoord focusPosition = center.getPosition(getTime());
//focusPosition = frame.fromUniversal(RigidTransform(focusPosition), getTime()).translation;
focusPosition = frame->convertFromUniversal(focusPosition, getTime());
// v = the vector from the observer's position to the focus
//Vec3d v = situation.translation - focusPosition;
Vector3d v = position.offsetFromKm(focusPosition);
Quaterniond qd = q.cast<double>();
// To give the right feel for rotation, we want to premultiply
// the current orientation by q. However, because of the order in
// which we apply transformations later on, we can't pre-multiply.
// To get around this, we compute a rotation q2 such
// that q1 * r = r * q2.
Quaterniond qd2 = orientation.conjugate() * qd * orientation;
qd2.normalize();
// Roundoff errors will accumulate and cause the distance between
// viewer and focus to drift unless we take steps to keep the
// length of v constant.
double distance = v.norm();
v = qd2.conjugate() * v;
v = v.normalized() * distance;
orientation = orientation * qd2;
position = focusPosition.offsetKm(v);
updateUniversal();
}
}
/*! Exponential camera dolly--move toward or away from the selected object
* at a rate dependent on the observer's distance from the object.
*/
void Observer::changeOrbitDistance(const Selection& selection, float d)
{
Selection center = frame->getRefObject();
if (center.empty() && !selection.empty())
{
center = selection;
setFrame(frame->getCoordinateSystem(), center);
}
if (!center.empty())
{
UniversalCoord focusPosition = center.getPosition(getTime());
double size = center.radius();
// Somewhat arbitrary parameters to chosen to give the camera movement
// a nice feel. They should probably be function parameters.
double minOrbitDistance = size;
double naturalOrbitDistance = 4.0 * size;
// Determine distance and direction to the selected object
Vector3d v = getPosition().offsetFromKm(focusPosition);
double currentDistance = v.norm();
if (currentDistance < minOrbitDistance)
minOrbitDistance = currentDistance * 0.5;
if (currentDistance >= minOrbitDistance && naturalOrbitDistance != 0)
{
double r = (currentDistance - minOrbitDistance) / naturalOrbitDistance;
double newDistance = minOrbitDistance + naturalOrbitDistance * exp(log(r) + d);
v = v * (newDistance / currentDistance);
position = frame->convertFromUniversal(focusPosition.offsetKm(v), getTime());
updateUniversal();
}
}
}
void Observer::setTargetSpeed(float s)
{
targetSpeed = s;
Vector3d v;
if (reverseFlag)
s = -s;
if (trackObject.empty())
{
trackingOrientation = getOrientation();
// Generate vector for velocity using current orientation
// and specified speed.
v = getOrientation().conjugate() * Vector3d(0, 0, -s);
}
else
{
// Use tracking orientation vector to generate target velocity
v = trackingOrientation.conjugate() * Vector3d(0, 0, -s);
}
targetVelocity = v;
initialVelocity = getVelocity();
beginAccelTime = realTime;
}
float Observer::getTargetSpeed()
{
return (float) targetSpeed;
}
void Observer::gotoJourney(const JourneyParams& params)
{
journey = params;
double distance = journey.from.offsetFromKm(journey.to).norm() / 2.0;
pair<double, double> sol = solve_bisection(TravelExpFunc(distance, journey.accelTime),
0.0001, 100.0,
1e-10);
journey.expFactor = sol.first;
journey.startTime = realTime;
observerMode = Travelling;
}
void Observer::gotoSelection(const Selection& selection,
double gotoTime,
const Vector3f& up,
ObserverFrame::CoordinateSystem upFrame)
{
gotoSelection(selection, gotoTime, 0.0, 0.5, up, upFrame);
}
// Return the preferred distance (in kilometers) for viewing an object
static double getPreferredDistance(const Selection& selection)
{
switch (selection.getType())
{
case Selection::Type_Body:
// Handle reference points (i.e. invisible objects) specially, since the
// actual radius of the point is meaningless. Instead, use the size of
// bounding sphere of all child objects. This is useful for system
// barycenters--the normal goto command will place the observer at
// a viewpoint in which the entire system can be seen.
if (selection.body()->getClassification() == Body::Invisible)
{
double r = selection.body()->getRadius();
if (selection.body()->getFrameTree() != nullptr)
r = selection.body()->getFrameTree()->boundingSphereRadius();
return min(astro::lightYearsToKilometers(0.1), r * 5.0);
}
else
{
return 5.0 * selection.radius();
}
case Selection::Type_DeepSky:
return 5.0 * selection.radius();
case Selection::Type_Star:
if (selection.star()->getVisibility())
{
return 100.0 * selection.radius();
}
else