forked from CelestiaProject/Celestia
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbody.cpp
More file actions
1425 lines (1126 loc) · 34 KB
/
body.cpp
File metadata and controls
1425 lines (1126 loc) · 34 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
// body.cpp
//
// Copyright (C) 2001-2006 Chris Laurel <claurel@shatters.net>
//
// 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 <cstdlib>
#include <cassert>
#include <algorithm>
#include <celmath/mathlib.h>
#include <celutil/gettext.h>
#include <celutil/utf8.h>
#include "geometry.h"
#include "meshmanager.h"
#include "body.h"
#include "atmosphere.h"
#include "frame.h"
#include "timeline.h"
#include "timelinephase.h"
#include "frametree.h"
#include "referencemark.h"
#include "selection.h"
using namespace Eigen;
using namespace std;
using namespace celmath;
Body::Body(PlanetarySystem* _system, const string& _name) :
system(_system),
orbitVisibility(UseClassVisibility)
{
setName(_name);
recomputeCullingRadius();
system->addBody(this);
}
Body::~Body()
{
if (system)
system->removeBody(this);
// Remove from frame hierarchy
// Clean up the reference mark list
if (referenceMarks)
{
for (const auto r : *referenceMarks)
delete r;
delete referenceMarks;
}
delete timeline;
delete satellites;
delete frameTree;
if(altSurfaces)
{
for (const auto &s : *altSurfaces)
delete s.second;
delete altSurfaces;
}
delete locations;
}
/*! Reset body attributes to their default values. The object hierarchy is left untouched,
* i.e. child objects are not removed. Alternate surfaces and locations are not removed
* either.
*/
void Body::setDefaultProperties()
{
radius = 1.0f;
semiAxes = Vector3f::Ones();
mass = 0.0f;
density = 0.0f;
bondAlbedo = 0.5f;
geomAlbedo = 0.5f;
reflectivity = 0.5f;
temperature = 0.0f;
tempDiscrepancy = 0.0f;
geometryOrientation = Quaternionf::Identity();
geometry = InvalidResource;
surface = Surface(Color::White);
delete atmosphere;
atmosphere = nullptr;
delete rings;
rings = nullptr;
classification = Unknown;
visible = true;
clickable = true;
visibleAsPoint = true;
overrideOrbitColor = false;
orbitVisibility = UseClassVisibility;
recomputeCullingRadius();
}
/*! Return the list of all names (non-localized) by which this
* body is known.
*/
const vector<string>& Body::getNames() const
{
return names;
}
/*! Return the primary name for the body; if i18n, return the
* localized name of the body.
*/
string Body::getName(bool i18n) const
{
if (!i18n)
return names[0];
else
return names[localizedNameIndex];
}
/*! Get the localized name for the body. If no localized name
* has been set, the primary name is returned.
*/
string Body::getLocalizedName() const
{
return names[localizedNameIndex];
}
bool Body::hasLocalizedName() const
{
return localizedNameIndex != 0;
}
/*! Set the primary name of the body. The localized name is updated
* automatically as well.
* Note: setName() is private, and only called from the Body constructor.
* It shouldn't be called elsewhere.
*/
void Body::setName(const string& name)
{
names[0] = name;
string localizedName = _(name.c_str());
if (name == localizedName)
{
// No localized name; set the localized name index to zero to
// indicate this.
localizedNameIndex = 0;
}
else
{
names.push_back(localizedName);
localizedNameIndex = names.size() - 1;
}
}
/*! Add a new name for this body. Aliases are non localized.
*/
void Body::addAlias(const string& alias)
{
// Don't add an alias if it matches the primary name
if (alias != names[0])
{
names.push_back(alias);
system->addAlias(this, alias);
}
}
PlanetarySystem* Body::getSystem() const
{
return system;
}
FrameTree* Body::getFrameTree() const
{
return frameTree;
}
FrameTree* Body::getOrCreateFrameTree()
{
if (!frameTree)
frameTree = new FrameTree(this);
return frameTree;
}
const Timeline* Body::getTimeline() const
{
return timeline;
}
void Body::setTimeline(Timeline* newTimeline)
{
if (timeline != newTimeline)
{
delete timeline;
timeline = newTimeline;
markChanged();
}
}
void Body::markChanged()
{
if (timeline)
timeline->markChanged();
}
void Body::markUpdated()
{
if (frameTree)
frameTree->markUpdated();
}
const ReferenceFrame::SharedConstPtr& Body::getOrbitFrame(double tdb) const
{
return timeline->findPhase(tdb)->orbitFrame();
}
const Orbit* Body::getOrbit(double tdb) const
{
return timeline->findPhase(tdb)->orbit();
}
const ReferenceFrame::SharedConstPtr& Body::getBodyFrame(double tdb) const
{
return timeline->findPhase(tdb)->bodyFrame();
}
const RotationModel* Body::getRotationModel(double tdb) const
{
return timeline->findPhase(tdb)->rotationModel();
}
/*! Get the radius of a sphere large enough to contain the primary
* geometry of the object: either a mesh or an ellipsoid.
* For an irregular (mesh) object, the radius is defined to be
* the largest semi-axis of the axis-aligned bounding box. The
* radius of the smallest sphere containing the object is potentially
* larger by a factor of sqrt(3).
*
* This method does not consider additional object features
* such as rings, atmospheres, or reference marks; use
* getCullingRadius() for that.
*/
float Body::getBoundingRadius() const
{
if (geometry == InvalidResource)
return radius;
return radius * 1.7320508f; // sqrt(3)
}
/*! Return the radius of sphere large enough to contain any geometry
* associated with this object: the primary geometry, comet tail,
* rings, atmosphere shell, cloud layers, or reference marks.
*/
float Body::getCullingRadius() const
{
return cullingRadius;
}
float Body::getMass() const
{
return mass;
}
void Body::setMass(float _mass)
{
mass = _mass;
}
float Body::getDensity() const
{
if (density > 0)
return density;
if (radius == 0 || !isSphere())
return 0;
// assume that we have a spherical body
// @mass unit is mass of Earth
// @astro::EarthMass unit is kg
// @radius unit km
// so we divide density by 1e9 to have kg/m^3
double volume = 4.0 / 3.0 * PI * ::pow(radius, 3);
return (float) mass * astro::EarthMass / 1e9 / volume;
}
void Body::setDensity(float _density)
{
density = _density;
}
float Body::getAlbedo() const
{
return getGeomAlbedo();
}
void Body::setAlbedo(float _albedo)
{
setGeomAlbedo(_albedo);
}
float Body::getGeomAlbedo() const
{
return geomAlbedo;
}
void Body::setGeomAlbedo(float _geomAlbedo)
{
geomAlbedo = _geomAlbedo;
}
float Body::getBondAlbedo() const
{
return bondAlbedo;
}
void Body::setBondAlbedo(float _bondAlbedo)
{
bondAlbedo = _bondAlbedo;
}
float Body::getReflectivity() const
{
return reflectivity;
}
void Body::setReflectivity(float _reflectivity)
{
reflectivity = _reflectivity;
}
float Body::getTemperature(double time) const
{
if (temperature > 0)
return temperature;
const PlanetarySystem* system = getSystem();
if (system == nullptr)
return 0;
const Star* sun = system->getStar();
if (sun == nullptr)
return 0;
float temp = 0.0f;
if (sun->getVisibility()) // the sun is a star
{
float distFromSun = (float)getAstrocentricPosition(time).norm();
temp = sun->getTemperature() *
pow(1.0f - getBondAlbedo(), 0.25f) *
sqrt(sun->getRadius() / (2.0f * distFromSun));
}
else // the sun is a barycenter
{
if (sun->getOrbitingStars() == nullptr)
return 0;
const UniversalCoord bodyPos = getPosition(time);
float flux = 0.0;
for (const auto *s : *sun->getOrbitingStars())
{
float distFromSun = (float)s->getPosition(time).distanceFromKm(bodyPos);
float lum = square(s->getRadius()) * pow(s->getTemperature(), 4.0f);
flux += lum / square(distFromSun);
}
temp = (float) pow((1.0f - getBondAlbedo()) * flux, 0.25f) / sqrt(2.0f);
}
return getTempDiscrepancy() + temp;
}
void Body::setTemperature(float _temperature)
{
temperature = _temperature;
}
float Body::getTempDiscrepancy() const
{
return tempDiscrepancy;
}
void Body::setTempDiscrepancy(float _tempDiscrepancy)
{
tempDiscrepancy = _tempDiscrepancy;
}
Quaternionf Body::getGeometryOrientation() const
{
return geometryOrientation;
}
void Body::setGeometryOrientation(const Quaternionf& orientation)
{
geometryOrientation = orientation;
}
/*! Set the semiaxes of a body.
*/
void Body::setSemiAxes(const Vector3f& _semiAxes)
{
semiAxes = _semiAxes;
// Radius will always be the largest of the three semi axes
radius = semiAxes.maxCoeff();
recomputeCullingRadius();
}
/*! Retrieve the body's semiaxes
*/
Vector3f Body::getSemiAxes() const
{
return semiAxes;
}
/*! Get the radius of the body. For a spherical body, this is simply
* the sphere's radius. For an ellipsoidal body, the radius is the
* largest of the three semiaxes. For irregular bodies (with a shape
* represented by a mesh), the radius is the largest semiaxis of the
* mesh's axis aligned bounding axis. Note that this means some portions
* of the mesh may extend outside the sphere of the retrieved radius.
* To obtain the radius of a sphere that will definitely enclose the
* body, call getBoundingRadius() instead.
*/
float Body::getRadius() const
{
return radius;
}
/*! Return true if the body is a perfect sphere.
*/
bool Body::isSphere() const
{
return (geometry == InvalidResource) &&
(semiAxes.x() == semiAxes.y()) &&
(semiAxes.x() == semiAxes.z());
}
/*! Return true if the body is ellipsoidal, with geometry determined
* completely by its semiaxes rather than a triangle based model.
*/
bool Body::isEllipsoid() const
{
return geometry == InvalidResource;
}
const Surface& Body::getSurface() const
{
return surface;
}
Surface& Body::getSurface()
{
return surface;
}
void Body::setSurface(const Surface& surf)
{
surface = surf;
}
void Body::setGeometry(ResourceHandle _geometry)
{
geometry = _geometry;
}
/*! Set the scale factor for geometry; this is only used with unnormalized meshes.
* When a mesh is normalized, the effective scale factor is the radius.
*/
void Body::setGeometryScale(float scale)
{
geometryScale = scale;
}
PlanetarySystem* Body::getSatellites() const
{
return satellites;
}
void Body::setSatellites(PlanetarySystem* ssys)
{
satellites = ssys;
}
RingSystem* Body::getRings() const
{
return rings;
}
void Body::setRings(const RingSystem& _rings)
{
if (!rings)
rings = new RingSystem(_rings);
else
*rings = _rings;
recomputeCullingRadius();
}
const Atmosphere* Body::getAtmosphere() const
{
return atmosphere;
}
Atmosphere* Body::getAtmosphere()
{
return atmosphere;
}
void Body::setAtmosphere(const Atmosphere& _atmosphere)
{
if (!atmosphere)
atmosphere = new Atmosphere();
*atmosphere = _atmosphere;
recomputeCullingRadius();
}
// The following four functions are used to get the state of the body
// in universal coordinates:
// * getPosition
// * getOrientation
// * getVelocity
// * getAngularVelocity
/*! Get the position of the body in the universal coordinate system.
* This method uses high-precision coordinates and is thus
* slower relative to getAstrocentricPosition(), which works strictly
* with standard double precision. For most purposes,
* getAstrocentricPosition() should be used instead of the more
* general getPosition().
*/
UniversalCoord Body::getPosition(double tdb) const
{
Vector3d position = Vector3d::Zero();
auto phase = timeline->findPhase(tdb);
Vector3d p = phase->orbit()->positionAtTime(tdb);
auto frame = phase->orbitFrame();
while (frame->getCenter().getType() == Selection::Type_Body)
{
phase = frame->getCenter().body()->timeline->findPhase(tdb);
position += frame->getOrientation(tdb).conjugate() * p;
p = phase->orbit()->positionAtTime(tdb);
frame = phase->orbitFrame();
}
position += frame->getOrientation(tdb).conjugate() * p;
if (frame->getCenter().star())
return frame->getCenter().star()->getPosition(tdb).offsetKm(position);
else
return frame->getCenter().getPosition(tdb).offsetKm(position);
}
/*! Get the orientation of the body in the universal coordinate system.
*/
Quaterniond Body::getOrientation(double tdb) const
{
auto phase = timeline->findPhase(tdb);
return phase->rotationModel()->orientationAtTime(tdb) * phase->bodyFrame()->getOrientation(tdb);
}
/*! Get the velocity of the body in the universal frame.
*/
Vector3d Body::getVelocity(double tdb) const
{
auto phase = timeline->findPhase(tdb);
auto orbitFrame = phase->orbitFrame();
Vector3d v = phase->orbit()->velocityAtTime(tdb);
v = orbitFrame->getOrientation(tdb).conjugate() * v + orbitFrame->getCenter().getVelocity(tdb);
if (!orbitFrame->isInertial())
{
Vector3d r = Selection(const_cast<Body*>(this)).getPosition(tdb).offsetFromKm(orbitFrame->getCenter().getPosition(tdb));
v += orbitFrame->getAngularVelocity(tdb).cross(r);
}
return v;
}
/*! Get the angular velocity of the body in the universal frame.
*/
Vector3d Body::getAngularVelocity(double tdb) const
{
auto phase = timeline->findPhase(tdb);
Vector3d v = phase->rotationModel()->angularVelocityAtTime(tdb);
auto bodyFrame = phase->bodyFrame();
v = bodyFrame->getOrientation(tdb).conjugate() * v;
if (!bodyFrame->isInertial())
{
v += bodyFrame->getAngularVelocity(tdb);
}
return v;
}
/*! Get the transformation which converts body coordinates into
* astrocentric coordinates. Some clarification on the meaning
* of 'astrocentric': the position of every solar system body
* is ultimately defined with respect to some star or star
* system barycenter.
*/
Matrix4d Body::getLocalToAstrocentric(double tdb) const
{
auto phase = timeline->findPhase(tdb);
Vector3d p = phase->orbitFrame()->convertToAstrocentric(phase->orbit()->positionAtTime(tdb), tdb);
return Eigen::Transform<double, 3, Affine>(Translation3d(p)).matrix();
}
/*! Get the position of the center of the body in astrocentric ecliptic coordinates.
*/
Vector3d Body::getAstrocentricPosition(double tdb) const
{
// TODO: Switch the iterative method used in getPosition
auto phase = timeline->findPhase(tdb);
return phase->orbitFrame()->convertToAstrocentric(phase->orbit()->positionAtTime(tdb), tdb);
}
/*! Get a rotation that converts from the ecliptic frame to the body frame.
*/
Quaterniond Body::getEclipticToFrame(double tdb) const
{
auto phase = timeline->findPhase(tdb);
return phase->bodyFrame()->getOrientation(tdb);
}
/*! Get a rotation that converts from the ecliptic frame to the body's
* mean equatorial frame.
*/
Quaterniond Body::getEclipticToEquatorial(double tdb) const
{
auto phase = timeline->findPhase(tdb);
return phase->rotationModel()->equatorOrientationAtTime(tdb) * phase->bodyFrame()->getOrientation(tdb);
}
/*! Get a rotation that converts from the ecliptic frame to this
* objects's body fixed frame.
*/
Quaterniond Body::getEclipticToBodyFixed(double tdb) const
{
auto phase = timeline->findPhase(tdb);
return phase->rotationModel()->orientationAtTime(tdb) * phase->bodyFrame()->getOrientation(tdb);
}
// The body-fixed coordinate system has an origin at the center of the
// body, y-axis parallel to the rotation axis, x-axis through the prime
// meridian, and z-axis at a right angle the xy plane.
Quaterniond Body::getEquatorialToBodyFixed(double tdb) const
{
auto phase = timeline->findPhase(tdb);
return phase->rotationModel()->spin(tdb);
}
/*! Get a transformation to convert from the object's body fixed frame
* to the astrocentric ecliptic frame.
*/
Matrix4d Body::getBodyFixedToAstrocentric(double tdb) const
{
//return getEquatorialToBodyFixed(tdb).toMatrix4() * getLocalToAstrocentric(tdb);
Matrix4d m = Eigen::Transform<double, 3, Affine>(getEquatorialToBodyFixed(tdb)).matrix();
return m * getLocalToAstrocentric(tdb);
}
Vector3d Body::planetocentricToCartesian(double lon, double lat, double alt) const
{
double phi = -degToRad(lat) + PI / 2;
double theta = degToRad(lon) - PI;
Vector3d pos(cos(theta) * sin(phi),
cos(phi),
-sin(theta) * sin(phi));
return pos * (getRadius() + alt);
}
Vector3d Body::planetocentricToCartesian(const Vector3d& lonLatAlt) const
{
return planetocentricToCartesian(lonLatAlt.x(), lonLatAlt.y(), lonLatAlt.z());
}
/*! Convert cartesian body-fixed coordinates to spherical planetocentric
* coordinates.
*/
Vector3d Body::cartesianToPlanetocentric(const Vector3d& v) const
{
Vector3d w = v.normalized();
double lat = PI / 2.0 - acos(w.y());
double lon = atan2(w.z(), -w.x());
return Vector3d(lon, lat, v.norm() - getRadius());
}
/*! Convert body-centered ecliptic coordinates to spherical planetocentric
* coordinates.
*/
Vector3d Body::eclipticToPlanetocentric(const Vector3d& ecl, double tdb) const
{
Vector3d bf = getEclipticToBodyFixed(tdb) * ecl;
return cartesianToPlanetocentric(bf);
}
bool Body::extant(double t) const
{
return timeline->includes(t);
}
void Body::getLifespan(double& begin, double& end) const
{
begin = timeline->startTime();
end = timeline->endTime();
}
float Body::getLuminosity(const Star& sun,
float distanceFromSun) const
{
return getLuminosity(sun.getLuminosity(), distanceFromSun);
}
float Body::getLuminosity(float sunLuminosity,
float distanceFromSun) const
{
// Compute the total power of the star in Watts
double power = astro::SOLAR_POWER * sunLuminosity;
// Compute the irradiance at a distance of 1au from the star in W/m^2
// double irradiance = power / sphereArea(astro::AUtoKilometers(1.0) * 1000);
// Compute the irradiance at the body's distance from the star
double satIrradiance = power / sphereArea(distanceFromSun * 1000);
// Compute the total energy hitting the planet
double incidentEnergy = satIrradiance * circleArea(radius * 1000);
double reflectedEnergy = incidentEnergy * getReflectivity();
// Compute the luminosity (i.e. power relative to solar power)
return (float) (reflectedEnergy / astro::SOLAR_POWER);
}
/*! Get the apparent magnitude of the body, neglecting the phase (as if
* the body was at opposition.
*/
float Body::getApparentMagnitude(const Star& sun,
float distanceFromSun,
float distanceFromViewer) const
{
return astro::lumToAppMag(getLuminosity(sun, distanceFromSun),
astro::kilometersToLightYears(distanceFromViewer));
}
/*! Get the apparent magnitude of the body, neglecting the phase (as if
* the body was at opposition.
*/
float Body::getApparentMagnitude(float sunLuminosity,
float distanceFromSun,
float distanceFromViewer) const
{
return astro::lumToAppMag(getLuminosity(sunLuminosity, distanceFromSun),
astro::kilometersToLightYears(distanceFromViewer));
}
/*! Get the apparent magnitude of the body, corrected for its phase.
*/
float Body::getApparentMagnitude(const Star& sun,
const Vector3d& sunPosition,
const Vector3d& viewerPosition) const
{
return getApparentMagnitude(sun.getLuminosity(),
sunPosition,
viewerPosition);
}
/*! Get the apparent magnitude of the body, corrected for its phase.
*/
float Body::getApparentMagnitude(float sunLuminosity,
const Vector3d& sunPosition,
const Vector3d& viewerPosition) const
{
double distanceToViewer = viewerPosition.norm();
double distanceToSun = sunPosition.norm();
float illuminatedFraction = (float) (1.0 + (viewerPosition / distanceToViewer).dot(sunPosition / distanceToSun)) / 2.0f;
return astro::lumToAppMag(getLuminosity(sunLuminosity, (float) distanceToSun) * illuminatedFraction, (float) astro::kilometersToLightYears(distanceToViewer));
}
int Body::getClassification() const
{
return classification;
}
void Body::setClassification(int _classification)
{
classification = _classification;
recomputeCullingRadius();
markChanged();
}
/*! Return the effective classification of this body used when rendering
* orbits. Normally, this is just the classification of the object, but
* invisible objects are treated specially: they behave as if they have
* the classification of their child objects. This fixes annoyances when
* planets are defined with orbits relative to their system barycenters.
* For example, Pluto's orbit can seen in a solar system scale view, even
* though its orbit is defined relative to the Pluto-Charon barycenter
* and is this just a few hundred kilometers in size.
*/
int Body::getOrbitClassification() const
{
if (classification != Invisible || !frameTree)
return classification;
int orbitClass = frameTree->childClassMask();
if ((orbitClass & Planet) != 0)
return Planet;
if ((orbitClass & DwarfPlanet) != 0)
return DwarfPlanet;
if ((orbitClass & Asteroid) != 0)
return Asteroid;
if ((orbitClass & Moon) != 0)
return Moon;
if ((orbitClass & MinorMoon) != 0)
return MinorMoon;
if ((orbitClass & Spacecraft) != 0)
return Spacecraft;
return Invisible;
}
const string& Body::getInfoURL() const
{
return infoURL;
}
void Body::setInfoURL(const string& _infoURL)
{
infoURL = _infoURL;
}
Surface* Body::getAlternateSurface(const string& name) const
{
if (!altSurfaces)
return nullptr;
auto iter = altSurfaces->find(name);
if (iter == altSurfaces->end())
return nullptr;
return iter->second;
}
void Body::addAlternateSurface(const string& name, Surface* surface)
{
if (!altSurfaces)
altSurfaces = new AltSurfaceTable();
//altSurfaces->insert(AltSurfaceTable::value_type(name, surface));
(*altSurfaces)[name] = surface;
}
vector<string>* Body::getAlternateSurfaceNames() const
{
vector<string>* names = new vector<string>();
if (altSurfaces)
{
for (const auto& s : *altSurfaces)
names->push_back(s.first);
}
return names;
}
void Body::addLocation(Location* loc)
{
assert(loc != nullptr);
if (!loc)
return;
if (!locations)
locations = new vector<Location*>();
locations->push_back(loc);
loc->setParentBody(this);
}
vector<Location*>* Body::getLocations() const
{
return locations;
}
Location* Body::findLocation(const string& name, bool i18n) const
{
if (!locations)
return nullptr;
for (const auto location : *locations)
{
if (!UTF8StringCompare(name, location->getName(false)))
return location;
if (i18n && !UTF8StringCompare(name, location->getName(true)))
return location;
}
return nullptr;
}
// Compute the positions of locations on an irregular object using ray-mesh
// intersections. This is not automatically done when a location is added
// because it would force the loading of all meshes for objects with
// defined locations; on-demand (i.e. when the object becomes visible to
// a user) loading of meshes is preferred.
void Body::computeLocations()
{
if (locationsComputed)
return;
locationsComputed = true;
// No work to do if there's no mesh, or if the mesh cannot be loaded