forked from CelestiaProject/Celestia
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdeepskyobj.cpp
More file actions
222 lines (190 loc) · 4.79 KB
/
deepskyobj.cpp
File metadata and controls
222 lines (190 loc) · 4.79 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
// deepskyobj.cpp
//
// Copyright (C) 2003-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 <algorithm>
#include <cstdio>
#include <config.h>
#include <cassert>
#include "astro.h"
#include "deepskyobj.h"
#include "galaxy.h"
#include "globular.h"
#include "nebula.h"
#include "opencluster.h"
#include <celengine/selection.h>
#include <celutil/util.h>
#include <celutil/debug.h>
#include <celmath/intersect.h>
using namespace Eigen;
using namespace std;
using namespace celmath;
Vector3d DeepSkyObject::getPosition() const
{
return position;
}
void DeepSkyObject::setPosition(const Vector3d& p)
{
position = p;
}
Quaternionf DeepSkyObject::getOrientation() const
{
return orientation;
}
void DeepSkyObject::setOrientation(const Quaternionf& q)
{
orientation = q;
}
void DeepSkyObject::setRadius(float r)
{
radius = r;
}
float DeepSkyObject::getAbsoluteMagnitude() const
{
return absMag;
}
void DeepSkyObject::setAbsoluteMagnitude(float _absMag)
{
absMag = _absMag;
}
string DeepSkyObject::getDescription() const
{
return "";
}
const string& DeepSkyObject::getInfoURL() const
{
return infoURL;
}
void DeepSkyObject::setInfoURL(const string& s)
{
infoURL = s;
}
bool DeepSkyObject::pick(const Ray3d& ray,
double& distanceToPicker,
double& cosAngleToBoundCenter) const
{
if (isVisible())
return testIntersection(ray, Sphered(position, (double) radius), distanceToPicker, cosAngleToBoundCenter);
else
return false;
}
void DeepSkyObject::hsv2rgb( float *r, float *g, float *b, float h, float s, float v )
{
// r,g,b values are from 0 to 1
// h = [0,360], s = [0,1], v = [0,1]
int i;
float f, p, q, t;
if( s == 0 )
{
// achromatic (grey)
*r = *g = *b = v;
return;
}
h /= 60; // sector 0 to 5
i = (int) floorf( h );
f = h - (float) i; // factorial part of h
p = v * ( 1 - s );
q = v * ( 1 - s * f );
t = v * ( 1 - s * ( 1 - f ) );
switch( i )
{
case 0:
*r = v;
*g = t;
*b = p;
break;
case 1:
*r = q;
*g = v;
*b = p;
break;
case 2:
*r = p;
*g = v;
*b = t;
break;
case 3:
*r = p;
*g = q;
*b = v;
break;
case 4:
*r = t;
*g = p;
*b = v;
break;
default:
*r = v;
*g = p;
*b = q;
break;
}
}
bool DeepSkyObject::load(AssociativeArray* params, const fs::path& resPath)
{
// Get position
Vector3d position(Vector3d::Zero());
if (params->getVector("Position", position))
{
setPosition(position);
}
else
{
double distance = 1.0;
double RA = 0.0;
double dec = 0.0;
params->getLength("Distance", distance, KM_PER_LY);
params->getAngle("RA", RA, DEG_PER_HRA);
params->getAngle("Dec", dec);
Vector3d p = astro::equatorialToCelestialCart(RA, dec, distance);
setPosition(p);
}
// Get orientation
Vector3d axis(Vector3d::UnitX());
double angle = 0.0;
params->getVector("Axis", axis);
params->getAngle("Angle", angle);
setOrientation(Quaternionf(AngleAxisf((float) degToRad(angle), axis.cast<float>().normalized())));
double radius = 1.0;
params->getLength("Radius", radius, KM_PER_LY);
setRadius((float) radius);
double absMag = 0.0;
if (params->getNumber("AbsMag", absMag))
setAbsoluteMagnitude((float) absMag);
string infoURL; // FIXME: infourl class
if (params->getString("InfoURL", infoURL))
{
if (infoURL.find(':') == string::npos)
{
// Relative URL, the base directory is the current one,
// not the main installation directory
if (resPath.c_str()[1] == ':')
// Absolute Windows path, file:/// is required
infoURL = "file:///" + resPath.string() + "/" + infoURL;
else if (!resPath.empty())
infoURL = resPath.string() + "/" + infoURL;
}
setInfoURL(infoURL);
}
bool visible = true;
if (params->getBoolean("Visible", visible))
{
setVisible(visible);
}
bool clickable = true;
if (params->getBoolean("Clickable", clickable))
{
setClickable(clickable);
}
return true;
}
Selection DeepSkyObject::toSelection()
{
// std::cout << "DeepSkyObject::toSelection()\n";
return Selection(this);
}