forked from CelestiaProject/Celestia
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhash.cpp
More file actions
650 lines (499 loc) · 15.7 KB
/
hash.cpp
File metadata and controls
650 lines (499 loc) · 15.7 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
// hash.cpp
//
// Copyright (C) 2001-2019, 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 <utility>
#include <celutil/color.h>
#include <celutil/fsutils.h>
#include <celutil/util.h>
#include <celmath/mathlib.h>
#include "astro.h"
#include "hash.h"
#include "value.h"
using namespace Eigen;
using namespace std;
using namespace celmath;
using namespace celestia::util;
AssociativeArray::~AssociativeArray()
{
for (const auto &iter : assoc)
delete iter.second;
}
Value* AssociativeArray::getValue(const string& key) const
{
map<string, Value*>::const_iterator iter = assoc.find(key);
if (iter == assoc.end())
return nullptr;
return iter->second;
}
void AssociativeArray::addValue(const string& key, Value& val)
{
assoc.insert(map<string, Value*>::value_type(key, &val));
}
bool AssociativeArray::getNumber(const string& key, double& val) const
{
Value* v = getValue(key);
if (v == nullptr || v->getType() != Value::NumberType)
return false;
val = v->getNumber();
return true;
}
bool AssociativeArray::getNumber(const string& key, float& val) const
{
double dval;
if (!getNumber(key, dval))
return false;
val = (float) dval;
return true;
}
bool AssociativeArray::getNumber(const string& key, int& val) const
{
double ival;
if (!getNumber(key, ival))
return false;
val = (int) ival;
return true;
}
bool AssociativeArray::getNumber(const string& key, uint32_t& val) const
{
double ival;
if (!getNumber(key, ival))
return false;
val = (uint32_t) ival;
return true;
}
bool AssociativeArray::getString(const string& key, string& val) const
{
Value* v = getValue(key);
if (v == nullptr || v->getType() != Value::StringType)
return false;
val = v->getString();
return true;
}
bool AssociativeArray::getPath(const string& key, fs::path& val) const
{
string v;
if (getString(key, v))
{
val = PathExp(v);
return true;
}
return false;
}
bool AssociativeArray::getBoolean(const string& key, bool& val) const
{
Value* v = getValue(key);
if (v == nullptr || v->getType() != Value::BooleanType)
return false;
val = v->getBoolean();
return true;
}
bool AssociativeArray::getVector(const string& key, Vector3d& val) const
{
Value* v = getValue(key);
if (v == nullptr || v->getType() != Value::ArrayType)
return false;
ValueArray* arr = v->getArray();
if (arr->size() != 3)
return false;
Value* x = (*arr)[0];
Value* y = (*arr)[1];
Value* z = (*arr)[2];
if (x->getType() != Value::NumberType ||
y->getType() != Value::NumberType ||
z->getType() != Value::NumberType)
return false;
val = Vector3d(x->getNumber(), y->getNumber(), z->getNumber());
return true;
}
bool AssociativeArray::getVector(const string& key, Vector3f& val) const
{
Vector3d vecVal;
if (!getVector(key, vecVal))
return false;
val = vecVal.cast<float>();
return true;
}
bool AssociativeArray::getVector(const string& key, Vector4d& val) const
{
Value* v = getValue(key);
if (v == nullptr || v->getType() != Value::ArrayType)
return false;
ValueArray* arr = v->getArray();
if (arr->size() != 4)
return false;
Value* x = (*arr)[0];
Value* y = (*arr)[1];
Value* z = (*arr)[2];
Value* w = (*arr)[3];
if (x->getType() != Value::NumberType ||
y->getType() != Value::NumberType ||
z->getType() != Value::NumberType ||
w->getType() != Value::NumberType)
return false;
val = Vector4d(x->getNumber(), y->getNumber(), z->getNumber(), w->getNumber());
return true;
}
bool AssociativeArray::getVector(const string& key, Vector4f& val) const
{
Vector4d vecVal;
if (!getVector(key, vecVal))
return false;
val = vecVal.cast<float>();
return true;
}
/**
* Retrieves a quaternion, scaled to an associated angle unit.
*
* The quaternion is specified in the catalog file in axis-angle format as follows:
* \verbatim {PropertyName} [ angle axisX axisY axisZ ] \endverbatim
*
* @param[in] key Hash key for the rotation.
* @param[out] val A quaternion representing the value if present, unaffected if not.
* @return True if the key exists in the hash, false otherwise.
*/
bool AssociativeArray::getRotation(const string& key, Eigen::Quaternionf& val) const
{
Value* v = getValue(key);
if (v == nullptr || v->getType() != Value::ArrayType)
return false;
ValueArray* arr = v->getArray();
if (arr->size() != 4)
return false;
Value* w = (*arr)[0];
Value* x = (*arr)[1];
Value* y = (*arr)[2];
Value* z = (*arr)[3];
if (w->getType() != Value::NumberType ||
x->getType() != Value::NumberType ||
y->getType() != Value::NumberType ||
z->getType() != Value::NumberType)
return false;
Vector3f axis((float) x->getNumber(),
(float) y->getNumber(),
(float) z->getNumber());
double ang = w->getNumber();
double angScale = 1.0;
getAngleScale(key, angScale);
float angle = degToRad((float) (ang * angScale));
val = Quaternionf(AngleAxisf(angle, axis.normalized()));
return true;
}
bool AssociativeArray::getColor(const string& key, Color& val) const
{
Vector4d vec4;
if (getVector(key, vec4))
{
Vector4f vec4f = vec4.cast<float>();
val = Color(vec4f);
return true;
}
Vector3d vec3;
if (getVector(key, vec3))
{
Vector3f vec3f = vec3.cast<float>();
val = Color(vec3f);
return true;
}
string rgba;
if (getString(key, rgba))
{
return Color::parse(rgba.c_str(), val);
}
return false;
}
/**
* Retrieves a numeric quantity scaled to an associated angle unit.
* @param[in] key Hash key for the quantity.
* @param[out] val The returned quantity if present, unaffected if not.
* @param[in] outputScale Returned value is scaled to this value.
* @param[in] defaultScale If no units are specified, use this scale. Defaults to outputScale.
* @return True if the key exists in the hash, false otherwise.
*/
bool
AssociativeArray::getAngle(const string& key, double& val, double outputScale, double defaultScale) const
{
if (!getNumber(key, val))
return false;
double angleScale;
if(getAngleScale(key, angleScale))
{
angleScale /= outputScale;
}
else
{
angleScale = (defaultScale == 0.0) ? 1.0 : defaultScale / outputScale;
}
val *= angleScale;
return true;
}
/** @copydoc AssociativeArray::getAngle() */
bool
AssociativeArray::getAngle(const string& key, float& val, double outputScale, double defaultScale) const
{
double dval;
if (!getAngle(key, dval, outputScale, defaultScale))
return false;
val = ((float) dval);
return true;
}
/**
* Retrieves a numeric quantity scaled to an associated length unit.
* @param[in] key Hash key for the quantity.
* @param[out] val The returned quantity if present, unaffected if not.
* @param[in] outputScale Returned value is scaled to this value.
* @param[in] defaultScale If no units are specified, use this scale. Defaults to outputScale.
* @return True if the key exists in the hash, false otherwise.
*/
bool
AssociativeArray::getLength(const string& key, double& val, double outputScale, double defaultScale) const
{
if(!getNumber(key, val))
return false;
double lengthScale;
if(getLengthScale(key, lengthScale))
{
lengthScale /= outputScale;
}
else
{
lengthScale = (defaultScale == 0.0) ? 1.0 : defaultScale / outputScale;
}
val *= lengthScale;
return true;
}
/** @copydoc AssociativeArray::getLength() */
bool AssociativeArray::getLength(const string& key, float& val, double outputScale, double defaultScale) const
{
double dval;
if (!getLength(key, dval, outputScale, defaultScale))
return false;
val = ((float) dval);
return true;
}
/**
* Retrieves a numeric quantity scaled to an associated time unit.
* @param[in] key Hash key for the quantity.
* @param[out] val The returned quantity if present, unaffected if not.
* @param[in] outputScale Returned value is scaled to this value.
* @param[in] defaultScale If no units are specified, use this scale. Defaults to outputScale.
* @return True if the key exists in the hash, false otherwise.
*/
bool AssociativeArray::getTime(const string& key, double& val, double outputScale, double defaultScale) const
{
if(!getNumber(key, val))
return false;
double timeScale;
if(getTimeScale(key, timeScale))
{
timeScale /= outputScale;
}
else
{
timeScale = (defaultScale == 0.0) ? 1.0 : defaultScale / outputScale;
}
val *= timeScale;
return true;
}
/** @copydoc AssociativeArray::getTime() */
bool AssociativeArray::getTime(const string& key, float& val, double outputScale, double defaultScale) const
{
double dval;
if(!getTime(key, dval, outputScale, defaultScale))
return false;
val = ((float) dval);
return true;
}
/**
* Retrieves a numeric quantity scaled to an associated mass unit.
* @param[in] key Hash key for the quantity.
* @param[out] val The returned quantity if present, unaffected if not.
* @param[in] outputScale Returned value is scaled to this value.
* @param[in] defaultScale If no units are specified, use this scale. Defaults to outputScale.
* @return True if the key exists in the hash, false otherwise.
*/
bool AssociativeArray::getMass(const string& key, double& val, double outputScale, double defaultScale) const
{
if(!getNumber(key, val))
return false;
double massScale;
if(getMassScale(key, massScale))
{
massScale /= outputScale;
}
else
{
massScale = (defaultScale == 0.0) ? 1.0 : defaultScale / outputScale;
}
val *= massScale;
return true;
}
/** @copydoc AssociativeArray::getMass() */
bool AssociativeArray::getMass(const string& key, float& val, double outputScale, double defaultScale) const
{
double dval;
if(!getMass(key, dval, outputScale, defaultScale))
return false;
val = ((float) dval);
return true;
}
/**
* Retrieves a vector quantity scaled to an associated length unit.
* @param[in] key Hash key for the quantity.
* @param[out] val The returned vector if present, unaffected if not.
* @param[in] outputScale Returned value is scaled to this value.
* @param[in] defaultScale If no units are specified, use this scale. Defaults to outputScale.
* @return True if the key exists in the hash, false otherwise.
*/
bool AssociativeArray::getLengthVector(const string& key, Eigen::Vector3d& val, double outputScale, double defaultScale) const
{
if(!getVector(key, val))
return false;
double lengthScale;
if(getLengthScale(key, lengthScale))
{
lengthScale /= outputScale;
}
else
{
lengthScale = (defaultScale == 0.0) ? 1.0 : defaultScale / outputScale;
}
val *= lengthScale;
return true;
}
/** @copydoc AssociativeArray::getLengthVector() */
bool AssociativeArray::getLengthVector(const string& key, Eigen::Vector3f& val, double outputScale, double defaultScale) const
{
Vector3d vecVal;
if(!getLengthVector(key, vecVal, outputScale, defaultScale))
return false;
val = vecVal.cast<float>();
return true;
}
/**
* Retrieves a spherical tuple \verbatim [longitude, latitude, altitude] \endverbatim scaled to associated angle and length units.
* @param[in] key Hash key for the quantity.
* @param[out] val The returned tuple in units of degrees and kilometers if present, unaffected if not.
* @return True if the key exists in the hash, false otherwise.
*/
bool AssociativeArray::getSphericalTuple(const string& key, Vector3d& val) const
{
if(!getVector(key, val))
return false;
double angleScale;
if(getAngleScale(key, angleScale))
{
val[0] *= angleScale;
val[1] *= angleScale;
}
double lengthScale = 1.0;
getLengthScale(key, lengthScale);
val[2] *= lengthScale;
return true;
}
/** @copydoc AssociativeArray::getSphericalTuple */
bool AssociativeArray::getSphericalTuple(const string& key, Vector3f& val) const
{
Vector3d vecVal;
if(!getSphericalTuple(key, vecVal))
return false;
val = vecVal.cast<float>();
return true;
}
/**
* Retrieves the angle unit associated with a given property.
* @param[in] key Hash key for the property.
* @param[out] scale The returned angle unit scaled to degrees if present, unaffected if not.
* @return True if an angle unit has been specified for the property, false otherwise.
*/
bool AssociativeArray::getAngleScale(const string& key, double& scale) const
{
string unitKey(key + "%Angle");
string unit;
if (!getString(unitKey, unit))
return false;
return astro::getAngleScale(unit, scale);
}
/** @copydoc AssociativeArray::getAngleScale() */
bool AssociativeArray::getAngleScale(const string& key, float& scale) const
{
double dscale;
if (!getAngleScale(key, dscale))
return false;
scale = ((float) dscale);
return true;
}
/**
* Retrieves the length unit associated with a given property.
* @param[in] key Hash key for the property.
* @param[out] scale The returned length unit scaled to kilometers if present, unaffected if not.
* @return True if a length unit has been specified for the property, false otherwise.
*/
bool AssociativeArray::getLengthScale(const string& key, double& scale) const
{
string unitKey(key + "%Length");
string unit;
if (!getString(unitKey, unit))
return false;
return astro::getLengthScale(unit, scale);
}
/** @copydoc AssociativeArray::getLengthScale() */
bool AssociativeArray::getLengthScale(const string& key, float& scale) const
{
double dscale;
if (!getLengthScale(key, dscale))
return false;
scale = ((float) dscale);
return true;
}
/**
* Retrieves the time unit associated with a given property.
* @param[in] key Hash key for the property.
* @param[out] scale The returned time unit scaled to days if present, unaffected if not.
* @return True if a time unit has been specified for the property, false otherwise.
*/
bool AssociativeArray::getTimeScale(const string& key, double& scale) const
{
string unitKey(key + "%Time");
string unit;
if (!getString(unitKey, unit))
return false;
return astro::getTimeScale(unit, scale);
}
/** @copydoc AssociativeArray::getTimeScale() */
bool AssociativeArray::getTimeScale(const string& key, float& scale) const
{
double dscale;
if (!getTimeScale(key, dscale))
return false;
scale = ((float) dscale);
return true;
}
/**
* Retrieves the mass unit associated with a given property.
* @param[in] key Hash key for the property.
* @param[out] scale The returned mass unit scaled to Earth mass if present, unaffected if not.
* @return True if a mass unit has been specified for the property, false otherwise.
*/
bool AssociativeArray::getMassScale(const string& key, double& scale) const
{
string unitKey(key + "%Mass");
string unit;
if (!getString(unitKey, unit))
return false;
return astro::getMassScale(unit, scale);
}
/** @copydoc AssociativeArray::getMassScale() */
bool AssociativeArray::getMassScale(const string& key, float& scale) const
{
double dscale;
if (!getMassScale(key, dscale))
return false;
scale = ((float) dscale);
return true;
}