forked from CelestiaProject/Celestia
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathparseobject.cpp
More file actions
1906 lines (1654 loc) · 57.2 KB
/
parseobject.cpp
File metadata and controls
1906 lines (1654 loc) · 57.2 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
// parseobject.cpp
//
// Copyright (C) 2004-2009, the Celestia Development Team
// Original version by Chris Laurel <claurel@gmail.com>
//
// Functions for parsing objects common to star, solar system, and
// deep sky catalogs.
//
// 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 "parseobject.h"
#include "frame.h"
#include "trajmanager.h"
#include "rotationmanager.h"
#include "universe.h"
#include <celephem/customorbit.h>
#include <celephem/customrotation.h>
#ifdef USE_SPICE
#include <celephem/spiceorbit.h>
#include <celephem/spicerotation.h>
#endif
#include <celephem/scriptorbit.h>
#include <celephem/scriptrotation.h>
#include <celmath/geomutil.h>
#include <celutil/debug.h>
#include <cassert>
using namespace Eigen;
using namespace std;
using namespace celmath;
/**
* Returns the default units scale for orbits.
*
* If the usePlanetUnits flag is set, this returns a distance scale of AU and a
* time scale of years. Otherwise the distace scale is kilometers and the time
* scale is days.
*
* @param[in] usePlanetUnits Controls whether to return planet units or satellite units.
* @param[out] distanceScale The default distance scale in kilometers.
* @param[out] timeScale The default time scale in days.
*/
static void
GetDefaultUnits(bool usePlanetUnits, double& distanceScale, double& timeScale)
{
if(usePlanetUnits)
{
distanceScale = KM_PER_AU;
timeScale = DAYS_PER_YEAR;
}
else
{
distanceScale = 1.0;
timeScale = 1.0;
}
}
/**
* Returns the default distance scale for orbits.
*
* If the usePlanetUnits flag is set, this returns AU, otherwise it returns
* kilometers.
*
* @param[in] usePlanetUnits Controls whether to return planet units or satellite units.
* @param[out] distanceScale The default distance scale in kilometers.
*/
static void
GetDefaultUnits(bool usePlanetUnits, double& distanceScale)
{
distanceScale = (usePlanetUnits) ? KM_PER_AU : 1.0;
}
bool
ParseDate(Hash* hash, const string& name, double& jd)
{
// Check first for a number value representing a Julian date
if (hash->getNumber(name, jd))
return true;
string dateString;
if (hash->getString(name, dateString))
{
astro::Date date(1, 1, 1);
if (astro::parseDate(dateString, date))
{
jd = (double) date;
return true;
}
}
return false;
}
/*!
* Create a new Keplerian orbit from an ssc property table:
*
* \code EllipticalOrbit
* {
* # One of the following is required to specify orbit size:
* SemiMajorAxis <number>
* PericenterDistance <number>
*
* # Required
* Period <number>
*
* Eccentricity <number> (default: 0.0)
* Inclination <degrees> (default: 0.0)
* AscendingNode <degrees> (default: 0.0)
*
* # One or none of the following:
* ArgOfPericenter <degrees> (default: 0.0)
* LongOfPericenter <degrees> (default: 0.0)
*
* Epoch <date> (default J2000.0)
*
* # One or none of the following:
* MeanAnomaly <degrees> (default: 0.0)
* MeanLongitude <degrees> (default: 0.0)
* } \endcode
*
* If usePlanetUnits is true:
* Period is in Julian years
* SemiMajorAxis or PericenterDistance is in AU
* Otherwise:
* Period is in Julian days
* SemiMajorAxis or PericenterDistance is in kilometers.
*/
static EllipticalOrbit*
CreateEllipticalOrbit(Hash* orbitData,
bool usePlanetUnits)
{
// default units for planets are AU and years, otherwise km and days
double distanceScale;
double timeScale;
GetDefaultUnits(usePlanetUnits, distanceScale, timeScale);
// SemiMajorAxis and Period are absolutely required; everything
// else has a reasonable default.
double pericenterDistance = 0.0;
double semiMajorAxis = 0.0;
if (!orbitData->getLength("SemiMajorAxis", semiMajorAxis, 1.0, distanceScale))
{
if (!orbitData->getLength("PericenterDistance", pericenterDistance, 1.0, distanceScale))
{
clog << "SemiMajorAxis/PericenterDistance missing! Skipping planet . . .\n";
return nullptr;
}
}
double period = 0.0;
if (!orbitData->getTime("Period", period, 1.0, timeScale))
{
clog << "Period missing! Skipping planet . . .\n";
return nullptr;
}
double eccentricity = 0.0;
orbitData->getNumber("Eccentricity", eccentricity);
double inclination = 0.0;
orbitData->getAngle("Inclination", inclination);
double ascendingNode = 0.0;
orbitData->getAngle("AscendingNode", ascendingNode);
double argOfPericenter = 0.0;
if (!orbitData->getAngle("ArgOfPericenter", argOfPericenter))
{
double longOfPericenter = 0.0;
if (orbitData->getAngle("LongOfPericenter", longOfPericenter))
{
argOfPericenter = longOfPericenter - ascendingNode;
}
}
double epoch = astro::J2000;
ParseDate(orbitData, "Epoch", epoch);
// Accept either the mean anomaly or mean longitude--use mean anomaly
// if both are specified.
double anomalyAtEpoch = 0.0;
if (!orbitData->getAngle("MeanAnomaly", anomalyAtEpoch))
{
double longAtEpoch = 0.0;
if (orbitData->getAngle("MeanLongitude", longAtEpoch))
{
anomalyAtEpoch = longAtEpoch - (argOfPericenter + ascendingNode);
}
}
// If we read the semi-major axis, use it to compute the pericenter
// distance.
if (semiMajorAxis != 0.0)
pericenterDistance = semiMajorAxis * (1.0 - eccentricity);
return new EllipticalOrbit(pericenterDistance,
eccentricity,
degToRad(inclination),
degToRad(ascendingNode),
degToRad(argOfPericenter),
degToRad(anomalyAtEpoch),
period,
epoch);
}
/*!
* Create a new sampled orbit from an ssc property table:
*
* \code SampledTrajectory
* {
* Source <string>
* Interpolation "Cubic" | "Linear"
* DoublePrecision <boolean>
* } \endcode
*
* Source is the only required field. Interpolation defaults to cubic, and
* DoublePrecision defaults to true.
*/
static Orbit*
CreateSampledTrajectory(Hash* trajData, const fs::path& path)
{
string sourceName;
if (!trajData->getString("Source", sourceName))
{
clog << "SampledTrajectory is missing a source.\n";
return nullptr;
}
// Read interpolation type; string value must be either "Linear" or "Cubic"
// Default interpolation type is cubic.
string interpolationString;
TrajectoryInterpolation interpolation = TrajectoryInterpolationCubic;
if (trajData->getString("Interpolation", interpolationString))
{
if (!compareIgnoringCase(interpolationString, "linear"))
interpolation = TrajectoryInterpolationLinear;
else if (!compareIgnoringCase(interpolationString, "cubic"))
interpolation = TrajectoryInterpolationCubic;
else
clog << "Unknown interpolation type " << interpolationString << endl; // non-fatal error
}
// Double precision is true by default
bool useDoublePrecision = true;
trajData->getBoolean("DoublePrecision", useDoublePrecision);
TrajectoryPrecision precision = useDoublePrecision ? TrajectoryPrecisionDouble : TrajectoryPrecisionSingle;
DPRINTF(LOG_LEVEL_INFO, "Attempting to load sampled trajectory from source '%s'\n", sourceName.c_str());
ResourceHandle orbitHandle = GetTrajectoryManager()->getHandle(TrajectoryInfo(sourceName, path, interpolation, precision));
Orbit* orbit = GetTrajectoryManager()->find(orbitHandle);
if (orbit == nullptr)
{
clog << "Could not load sampled trajectory from '" << sourceName << "'\n";
}
return orbit;
}
/** Create a new FixedPosition trajectory.
*
* A FixedPosition is a property list with one of the following 3-vector properties:
*
* - \c Rectangular
* - \c Planetographic
* - \c Planetocentric
*
* Planetographic and planetocentric coordinates are given in the order longitude,
* latitude, altitude. Units of altitude are kilometers. Planetographic and
* and planetocentric coordinates are only practical when the coordinate system
* is BodyFixed.
*/
static Orbit*
CreateFixedPosition(Hash* trajData, const Selection& centralObject, bool usePlanetUnits)
{
double distanceScale;
GetDefaultUnits(usePlanetUnits, distanceScale);
Vector3d position = Vector3d::Zero();
Vector3d v = Vector3d::Zero();
if (trajData->getLengthVector("Rectangular", v, 1.0, distanceScale))
{
// Convert to Celestia's coordinate system
position = Vector3d(v.x(), v.z(), -v.y());
}
else if (trajData->getSphericalTuple("Planetographic", v))
{
if (centralObject.getType() != Selection::Type_Body)
{
clog << "FixedPosition planetographic coordinates aren't valid for stars.\n";
return nullptr;
}
// TODO: Need function to calculate planetographic coordinates
// TODO: Change planetocentricToCartesian so that 180 degree offset isn't required
position = centralObject.body()->planetocentricToCartesian(180.0 + v.x(), v.y(), v.z());
}
else if (trajData->getSphericalTuple("Planetocentric", v))
{
if (centralObject.getType() != Selection::Type_Body)
{
clog << "FixedPosition planetocentric coordinates aren't valid for stars.\n";
return nullptr;
}
// TODO: Change planetocentricToCartesian so that 180 degree offset isn't required
position = centralObject.body()->planetocentricToCartesian(180.0 + v.x(), v.y(), v.z());
}
else
{
clog << "Missing coordinates for FixedPosition\n";
return nullptr;
}
return new FixedOrbit(position);
}
#ifdef USE_SPICE
/**
* Parse a string list--either a single string or an array of strings is permitted.
*/
static bool
ParseStringList(Hash* table,
const string& propertyName,
list<string>& stringList)
{
Value* v = table->getValue(propertyName);
if (v == nullptr)
return false;
// Check for a single string first.
if (v->getType() == Value::StringType)
{
stringList.push_back(v->getString());
return true;
}
if (v->getType() == Value::ArrayType)
{
ValueArray* array = v->getArray();
ValueArray::const_iterator iter;
// Verify that all array entries are strings
for (iter = array->begin(); iter != array->end(); iter++)
{
if ((*iter)->getType() != Value::StringType)
return false;
}
// Add strings to stringList
for (iter = array->begin(); iter != array->end(); iter++)
stringList.push_back((*iter)->getString());
return true;
}
else
{
return false;
}
}
/*! Create a new SPICE orbit. This is just a Celestia wrapper for a trajectory specified
* in a SPICE SPK file.
*
* \code SpiceOrbit
* {
* Kernel <string|string array> # optional
* Target <string>
* Origin <string>
* BoundingRadius <number>
* Period <number> # optional
* Beginning <number> # optional
* Ending <number> # optional
* } \endcode
*
* The Kernel property specifies one or more SPK files that must be loaded. Any
* already loaded kernels will also be used if they contain trajectories for
* the target or origin.
* Target and origin are strings that give NAIF IDs for the target and origin
* objects. Either names or integer IDs are valid, but integer IDs still must
* be quoted.
* BoundingRadius gives a conservative estimate of the maximum distance between
* the target and origin objects. It is required by Celestia for visibility
* culling when rendering.
* Beginning and Ending specify the valid time range of the SPICE orbit. It is
* an error to specify Beginning without Ending, and vice versa. If neither is
* specified, the valid range is computed from the coverage window in the SPICE
* kernel pool. If the coverage window is noncontiguous, the first interval is
* used.
*/
static SpiceOrbit*
CreateSpiceOrbit(Hash* orbitData,
const fs::path& path,
bool usePlanetUnits)
{
string targetBodyName;
string originName;
list<string> kernelList;
double distanceScale;
double timeScale;
GetDefaultUnits(usePlanetUnits, distanceScale, timeScale);
if (orbitData->getValue("Kernel") != nullptr)
{
// Kernel list is optional; a SPICE orbit may rely on kernels already loaded into
// the kernel pool.
if (!ParseStringList(orbitData, "Kernel", kernelList))
{
clog << "Kernel list for SPICE orbit is neither a string nor array of strings\n";
return nullptr;
}
}
if (!orbitData->getString("Target", targetBodyName))
{
clog << "Target name missing from SPICE orbit\n";
return nullptr;
}
if (!orbitData->getString("Origin", originName))
{
clog << "Origin name missing from SPICE orbit\n";
return nullptr;
}
// A bounding radius for culling is required for SPICE orbits
double boundingRadius = 0.0;
if (!orbitData->getLength("BoundingRadius", boundingRadius, 1.0, distanceScale))
{
clog << "Bounding Radius missing from SPICE orbit\n";
return nullptr;
}
// The period of the orbit may be specified if appropriate; a value
// of zero for the period (the default), means that the orbit will
// be considered aperiodic.
double period = 0.0;
orbitData->getTime("Period", period, 1.0, timeScale);
// Either a complete time interval must be specified with Beginning/Ending, or
// else neither field can be present.
Value* beginningDate = orbitData->getValue("Beginning");
Value* endingDate = orbitData->getValue("Ending");
if (beginningDate != nullptr && endingDate == nullptr)
{
clog << "Beginning specified for SPICE orbit, but ending is missing.\n";
return nullptr;
}
if (endingDate != nullptr && beginningDate == nullptr)
{
clog << "Ending specified for SPICE orbit, but beginning is missing.\n";
return nullptr;
}
SpiceOrbit* orbit = nullptr;
if (beginningDate != nullptr && endingDate != nullptr)
{
double beginningTDBJD = 0.0;
if (!ParseDate(orbitData, "Beginning", beginningTDBJD))
{
clog << "Invalid beginning date specified for SPICE orbit.\n";
return nullptr;
}
double endingTDBJD = 0.0;
if (!ParseDate(orbitData, "Ending", endingTDBJD))
{
clog << "Invalid ending date specified for SPICE orbit.\n";
return nullptr;
}
orbit = new SpiceOrbit(targetBodyName,
originName,
period,
boundingRadius,
beginningTDBJD,
endingTDBJD);
}
else
{
// No time interval given; we'll use whatever coverage window is given
// in the SPICE kernel.
orbit = new SpiceOrbit(targetBodyName,
originName,
period,
boundingRadius);
}
if (!orbit->init(path, &kernelList))
{
// Error using SPICE library; destroy the orbit; hopefully a
// fallback is defined in the SSC file.
delete orbit;
orbit = nullptr;
}
return orbit;
}
/*! Create a new rotation model based on a SPICE frame.
*
* \code SpiceRotation
* {
* Kernel <string|string array> # optional
* Frame <string>
* BaseFrame <string> # optional (defaults to ecliptic)
* Period <number> # optional (units are hours)
* Beginning <number> # optional
* Ending <number> # optional
* } \endcode
*
* The Kernel property specifies one or more SPICE kernel files that must be
* loaded in order for the frame to be defined over the required range. Any
* already loaded kernels will be used if they contain information relevant
* for defining the frame.
* Frame and base name are strings that give SPICE names for the frames. The
* orientation of the SpiceRotation is the orientation of the frame relative to
* the base frame. By default, the base frame is eclipj2000.
* Beginning and Ending specify the valid time range of the SPICE rotation.
* If the Beginning and Ending are omitted, the rotation model is assumed to
* be valid at any time. It is an error to specify Beginning without Ending,
* and vice versa.
* Period specifies the principal rotation period; it defaults to 0 indicating
* that the rotation is aperiodic. It is not essential to provide the rotation
* period; it is only used by Celestia for displaying object information such
* as sidereal day length.
*/
static SpiceRotation*
CreateSpiceRotation(Hash* rotationData,
const fs::path& path)
{
string frameName;
string baseFrameName = "eclipj2000";
list<string> kernelList;
if (rotationData->getValue("Kernel") != nullptr)
{
// Kernel list is optional; a SPICE rotation may rely on kernels already loaded into
// the kernel pool.
if (!ParseStringList(rotationData, "Kernel", kernelList))
{
clog << "Kernel list for SPICE rotation is neither a string nor array of strings\n";
return nullptr;
}
}
if (!rotationData->getString("Frame", frameName))
{
clog << "Frame name missing from SPICE rotation\n";
return nullptr;
}
rotationData->getString("BaseFrame", baseFrameName);
// The period of the rotation may be specified if appropriate; a value
// of zero for the period (the default), means that the rotation will
// be considered aperiodic.
double period = 0.0;
rotationData->getTime("Period", period, 1.0, 1.0 / HOURS_PER_DAY);
// Either a complete time interval must be specified with Beginning/Ending, or
// else neither field can be present.
Value* beginningDate = rotationData->getValue("Beginning");
Value* endingDate = rotationData->getValue("Ending");
if (beginningDate != nullptr && endingDate == nullptr)
{
clog << "Beginning specified for SPICE rotation, but ending is missing.\n";
return nullptr;
}
if (endingDate != nullptr && beginningDate == nullptr)
{
clog << "Ending specified for SPICE rotation, but beginning is missing.\n";
return nullptr;
}
SpiceRotation* rotation = nullptr;
if (beginningDate != nullptr && endingDate != nullptr)
{
double beginningTDBJD = 0.0;
if (!ParseDate(rotationData, "Beginning", beginningTDBJD))
{
clog << "Invalid beginning date specified for SPICE rotation.\n";
return nullptr;
}
double endingTDBJD = 0.0;
if (!ParseDate(rotationData, "Ending", endingTDBJD))
{
clog << "Invalid ending date specified for SPICE rotation.\n";
return nullptr;
}
rotation = new SpiceRotation(frameName,
baseFrameName,
period,
beginningTDBJD,
endingTDBJD);
}
else
{
// No time interval given; rotation is valid at any time.
rotation = new SpiceRotation(frameName,
baseFrameName,
period);
}
if (!rotation->init(path, &kernelList))
{
// Error using SPICE library; destroy the rotation.
delete rotation;
rotation = nullptr;
}
return rotation;
}
#endif
static ScriptedOrbit*
CreateScriptedOrbit(Hash* orbitData,
const fs::path& path)
{
#if !defined(CELX)
clog << "ScriptedOrbit not usable without scripting support.\n";
return nullptr;
#else
// Function name is required
string funcName;
if (!orbitData->getString("Function", funcName))
{
clog << "Function name missing from script orbit definition.\n";
return nullptr;
}
// Module name is optional
string moduleName;
orbitData->getString("Module", moduleName);
Value* pathValue = new Value(path.string());
orbitData->addValue("AddonPath", *pathValue);
ScriptedOrbit* scriptedOrbit = new ScriptedOrbit();
if (!scriptedOrbit->initialize(moduleName, funcName, orbitData))
{
delete scriptedOrbit;
scriptedOrbit = nullptr;
}
return scriptedOrbit;
#endif
}
Orbit*
CreateOrbit(const Selection& centralObject,
Hash* planetData,
const fs::path& path,
bool usePlanetUnits)
{
Orbit* orbit = nullptr;
string customOrbitName;
if (planetData->getString("CustomOrbit", customOrbitName))
{
orbit = GetCustomOrbit(customOrbitName);
if (orbit != nullptr)
{
return orbit;
}
clog << "Could not find custom orbit named '" << customOrbitName <<
"'\n";
}
#ifdef USE_SPICE
Value* spiceOrbitDataValue = planetData->getValue("SpiceOrbit");
if (spiceOrbitDataValue != nullptr)
{
if (spiceOrbitDataValue->getType() != Value::HashType)
{
clog << "Object has incorrect spice orbit syntax.\n";
return nullptr;
}
else
{
orbit = CreateSpiceOrbit(spiceOrbitDataValue->getHash(), path, usePlanetUnits);
if (orbit != nullptr)
{
return orbit;
}
clog << "Bad spice orbit\n";
DPRINTF(LOG_LEVEL_ERROR, "Could not load SPICE orbit\n");
}
}
#endif
// Trajectory calculated by Lua script
Value* scriptedOrbitValue = planetData->getValue("ScriptedOrbit");
if (scriptedOrbitValue != nullptr)
{
if (scriptedOrbitValue->getType() != Value::HashType)
{
clog << "Object has incorrect scripted orbit syntax.\n";
return nullptr;
}
orbit = CreateScriptedOrbit(scriptedOrbitValue->getHash(), path);
if (orbit != nullptr)
return orbit;
}
// New 1.5.0 style for sampled trajectories. Permits specification of
// precision and interpolation type.
Value* sampledTrajDataValue = planetData->getValue("SampledTrajectory");
if (sampledTrajDataValue != nullptr)
{
if (sampledTrajDataValue->getType() != Value::HashType)
{
clog << "Object has incorrect syntax for SampledTrajectory.\n";
return nullptr;
}
return CreateSampledTrajectory(sampledTrajDataValue->getHash(), path);
}
// Old style for sampled trajectories. Assumes cubic interpolation and
// single precision.
string sampOrbitFile;
if (planetData->getString("SampledOrbit", sampOrbitFile))
{
DPRINTF(LOG_LEVEL_INFO, "Attempting to load sampled orbit file '%s'\n",
sampOrbitFile.c_str());
ResourceHandle orbitHandle =
GetTrajectoryManager()->getHandle(TrajectoryInfo(sampOrbitFile,
path,
TrajectoryInterpolationCubic,
TrajectoryPrecisionSingle));
orbit = GetTrajectoryManager()->find(orbitHandle);
if (orbit != nullptr)
{
return orbit;
}
clog << "Could not load sampled orbit file '" << sampOrbitFile << "'\n";
}
Value* orbitDataValue = planetData->getValue("EllipticalOrbit");
if (orbitDataValue != nullptr)
{
if (orbitDataValue->getType() != Value::HashType)
{
clog << "Object has incorrect elliptical orbit syntax.\n";
return nullptr;
}
return CreateEllipticalOrbit(orbitDataValue->getHash(),
usePlanetUnits);
}
// Create an 'orbit' that places the object at a fixed point in its
// reference frame. There are two forms for FixedPosition: a simple
// form with an 3-vector value, and complex form with a properlist
// value. The simple form:
//
// FixedPosition [ x y z ]
//
// is a shorthand for:
//
// FixedPosition { Rectangular [ x y z ] }
//
// In addition to Rectangular, other coordinate types for fixed position are
// Planetographic and Planetocentric.
Value* fixedPositionValue = planetData->getValue("FixedPosition");
if (fixedPositionValue != nullptr)
{
Vector3d fixedPosition = Vector3d::Zero();
double distanceScale;
GetDefaultUnits(usePlanetUnits, distanceScale);
if (planetData->getLengthVector("FixedPosition", fixedPosition, 1.0, distanceScale))
{
// Convert to Celestia's coordinate system
fixedPosition = Vector3d(fixedPosition.x(),
fixedPosition.z(),
-fixedPosition.y());
return new FixedOrbit(fixedPosition);
}
if (fixedPositionValue->getType() == Value::HashType)
{
return CreateFixedPosition(fixedPositionValue->getHash(), centralObject, usePlanetUnits);
}
clog << "Object has incorrect FixedPosition syntax.\n";
}
// LongLat will make an object fixed relative to the surface of its center
// object. This is done by creating an orbit with a period equal to the
// rotation rate of the parent object. A body-fixed reference frame is a
// much better way to accomplish this.
Vector3d longlat = Vector3d::Zero();
if (planetData->getSphericalTuple("LongLat", longlat))
{
Body* centralBody = centralObject.body();
if (centralBody != nullptr)
{
Vector3d pos = centralBody->planetocentricToCartesian(longlat.x(), longlat.y(), longlat.z());
return new SynchronousOrbit(*centralBody, pos);
}
// TODO: Allow fixing objects to the surface of stars.
return nullptr;
}
return nullptr;
}
static ConstantOrientation*
CreateFixedRotationModel(double offset,
double inclination,
double ascendingNode)
{
Quaterniond q = YRotation(-PI - offset) *
XRotation(-inclination) *
YRotation(-ascendingNode);
return new ConstantOrientation(q);
}
static RotationModel*
CreateUniformRotationModel(Hash* rotationData,
double syncRotationPeriod)
{
// Default to synchronous rotation
double period = syncRotationPeriod;
rotationData->getTime("Period", period, 1.0, 1.0 / HOURS_PER_DAY);
float offset = 0.0f;
if (rotationData->getAngle("MeridianAngle", offset))
{
offset = degToRad(offset);
}
double epoch = astro::J2000;
ParseDate(rotationData, "Epoch", epoch);
float inclination = 0.0f;
if (rotationData->getAngle("Inclination", inclination))
{
inclination = degToRad(inclination);
}
float ascendingNode = 0.0f;
if (rotationData->getAngle("AscendingNode", ascendingNode))
{
ascendingNode = degToRad(ascendingNode);
}
// No period was specified, and the default synchronous
// rotation period is zero, indicating that the object
// doesn't have a periodic orbit. Default to a constant
// orientation instead.
if (period == 0.0)
{
return CreateFixedRotationModel(offset, inclination, ascendingNode);
}
else
{
return new UniformRotationModel(period,
offset,
epoch,
inclination,
ascendingNode);
}
}
static ConstantOrientation*
CreateFixedRotationModel(Hash* rotationData)
{
double offset = 0.0;
if (rotationData->getAngle("MeridianAngle", offset))
{
offset = degToRad(offset);
}
double inclination = 0.0;
if (rotationData->getAngle("Inclination", inclination))
{
inclination = degToRad(inclination);
}
double ascendingNode = 0.0;
if (rotationData->getAngle("AscendingNode", ascendingNode))
{
ascendingNode = degToRad(ascendingNode);
}
Quaterniond q = YRotation(-PI - offset) *
XRotation(-inclination) *
YRotation(-ascendingNode);
return new ConstantOrientation(q);
}
static ConstantOrientation*
CreateFixedAttitudeRotationModel(Hash* rotationData)
{
double heading = 0.0;
if (rotationData->getAngle("Heading", heading))
{
heading = degToRad(heading);
}
double tilt = 0.0;
if (rotationData->getAngle("Tilt", tilt))
{
tilt = degToRad(tilt);
}
double roll = 0.0;
if (rotationData->getAngle("Roll", roll))
{
roll = degToRad(roll);
}
Quaterniond q = YRotation(-PI - heading) *
XRotation(-tilt) *
ZRotation(-roll);
return new ConstantOrientation(q);
}
static RotationModel*
CreatePrecessingRotationModel(Hash* rotationData,
double syncRotationPeriod)
{
// Default to synchronous rotation
double period = syncRotationPeriod;
rotationData->getTime("Period", period, 1.0, 1.0 / HOURS_PER_DAY);
float offset = 0.0f;
if (rotationData->getAngle("MeridianAngle", offset))
{
offset = degToRad(offset);
}
double epoch = astro::J2000;
ParseDate(rotationData, "Epoch", epoch);
float inclination = 0.0f;
if (rotationData->getAngle("Inclination", inclination))
{
inclination = degToRad(inclination);
}
float ascendingNode = 0.0f;
if (rotationData->getAngle("AscendingNode", ascendingNode))
{
ascendingNode = degToRad(ascendingNode);
}
// The default value of 0 is handled specially, interpreted to indicate
// that there's no precession.
double precessionPeriod = 0.0;
rotationData->getTime("PrecessionPeriod", precessionPeriod, 1.0, DAYS_PER_YEAR);
// No period was specified, and the default synchronous
// rotation period is zero, indicating that the object
// doesn't have a periodic orbit. Default to a constant
// orientation instead.
if (period == 0.0)
{
return CreateFixedRotationModel(offset, inclination, ascendingNode);
}
else
{
return new PrecessingRotationModel(period,
offset,
epoch,
inclination,
ascendingNode,
precessionPeriod);