forked from miguelfranca/BHRaytracer
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSkyMap.cpp
More file actions
248 lines (203 loc) · 5.69 KB
/
SkyMap.cpp
File metadata and controls
248 lines (203 loc) · 5.69 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
#include "SkyMap.hpp"
#include "Tools.hpp"
#include <cassert>
SkyMap::SkyMap(const Spacetime& a_st, bool ep)
: st(a_st), do_elliptic_parametrization(ep)
{
}
VecD SkyMap::getCoordinates(double theta, double phi)
{
theta -= M_PI / 2;
while (phi > M_PI)
phi -= 2. * M_PI;
while (phi <= -M_PI)
phi += 2. * M_PI;
double x, y;
if (do_elliptic_parametrization) {
// code for elliptic parametrization of sky map
double phi2 = phi * phi;
double PI2 = M_PI * M_PI;
double theta2 = theta * theta;
x = (phi < 0 ? -1 : 1) * M_PI *
std::sqrt(phi2 * (PI2 - 4. * theta2) /
(PI2 * PI2 - 4. * theta2 * phi2));
y = (theta < 0 ? -1 : 1) * M_PI *
std::sqrt(theta2 * (PI2 - phi2) / (PI2 * PI2 - 4. * theta2 * phi2));
}
else {
x = phi;
y = theta;
}
return {x, y};
}
sf::Image SkyMap::getSkyView(const Matrix<VecD>& mat)
{
sf::Image view;
view.create(mat.getNC(), mat.getNL());
for (unsigned l = 0; l < mat.getNL(); ++l) {
for (unsigned c = 0; c < mat.getNC(); ++c) {
auto vec = mat[l][c];
sf::Color color;
double r = vec[1];
if (r < st.BH_radius() || std::isnan(r)) {
color = BLACK;
}
else {
double theta = vec[2];
double phi = vec[3];
VecD coords = getCoordinates(theta, phi);
color = getPixelColor(coords[0], coords[1]);
color = redshiftColor(color, vec[7]);
}
view.setPixel(c, l, color);
}
}
return view;
}
ColorSkyMap::ColorSkyMap(const Spacetime& a_st,
bool do_elliptic_parametrization, double a_grid_angle,
double a_thickness, double a_color_ring)
: SkyMap(a_st, do_elliptic_parametrization), grid_angle(a_grid_angle),
thickness(a_thickness), color_ring(a_color_ring)
{
}
sf::Color ColorSkyMap::getPixelColor(double x, double y)
{
sf::Color color;
static double pid2 = M_PI / 2.;
if (x > 0 && y > 0) {
if (x < pid2)
color = sf::Color(204, 0, 0); // dark red
else
color = sf::Color(255, 102, 102); // light red
}
else if (x < 0 && y > 0) {
if (x > -pid2)
color = sf::Color(255, 153, 51); // light orange
else
color = sf::Color(255, 255, 51); // yellow
}
else if (x > 0 && y < 0) {
if (x < pid2)
color = sf::Color(0, 0, 255); // dark blue
else
color = sf::Color(102, 178, 255); // light blue
}
else if (x < 0 && y < 0) {
if (x > -pid2)
color = sf::Color(0, 153, 0); // dark green
else
color = sf::Color(102, 255, 102); // light green
}
if (std::abs(std::remainder(x, grid_angle)) <= thickness)
color = BLACK;
if (std::abs(std::remainder(y, grid_angle)) <= thickness)
color = BLACK;
// spot of light
double r = sqrt(x * x + y * y);
if (r <= color_ring) {
auto color_2 = WHITE;
double factor = (color_ring - r) / color_ring;
factor = pow(factor, 0.3);
color.r = (1. - factor) * color.r + factor * color_2.r;
color.g = (1. - factor) * color.g + factor * color_2.g;
color.b = (1. - factor) * color.b + factor * color_2.b;
color.a = (1. - factor) * color.a + factor * color_2.a;
}
return color;
}
ImageSkyMap::ImageSkyMap(const Spacetime& a_st, const std::string& filename,
bool do_elliptic_parametrization)
: SkyMap(a_st, do_elliptic_parametrization)
{
if (!fileExists(filename))
errorMsg("Could not load file " + filename);
sky.loadFromFile(filename);
}
sf::Color ImageSkyMap::getPixelColor(double x, double y)
{
sf::Vector2u size = sky.getSize();
y = (y + M_PI / 2.) / M_PI * size.y;
x = (x + M_PI) / (2. * M_PI) * size.x;
// flip y because SFML grows y downwards
y = size.y - y;
sf::Color color;
color = sky.getPixel(std::round(x), std::round(y));
int offset = 5;
color = sf::Color(std::min((unsigned)(color.r + offset), (unsigned)(255)),
std::min((unsigned)(color.g + offset), (unsigned)(255)),
std::min((unsigned)(color.b + offset), (unsigned)(255)));
return color;
}
sf::Color redshiftColor(sf::Color color, double factor)
{
static double red = 700.;
static double green = 510.;
static double blue = 440.;
sf::Color red_new = wavelengthToColor(red * factor);
sf::Color green_new = wavelengthToColor(green * factor);
sf::Color blue_new = wavelengthToColor(blue * factor);
double R, G, B;
R = color.r / 255.;
G = color.g / 255.;
B = color.b / 255.;
assert(color.a == 255);
sf::Color out = sf::Color(std::min(255., red_new.r * R + green_new.r * G + blue_new.r * B),
std::min(255., red_new.g * R + green_new.g * G + blue_new.g * B),
std::min(255., red_new.b * R + green_new.b * G + blue_new.b * B),
255.);
return out;
}
sf::Color wavelengthToColor(double wavelength)
{
double R, G, B, A;
if (wavelength >= 380. && wavelength < 440.) {
R = -1 * (wavelength - 440.) / (440. - 380.);
G = 0.;
B = 1.;
}
else if (wavelength >= 440. && wavelength < 490.) {
R = 0.;
G = (wavelength - 440.) / (490. - 440.);
B = 1.;
}
else if (wavelength >= 490. && wavelength < 510.) {
R = 0.;
G = 1.;
B = -1 * (wavelength - 510.) / (510. - 490.);
}
else if (wavelength >= 510. && wavelength < 580.) {
R = (wavelength - 510.) / (580. - 510.);
G = 1.;
B = 0.;
}
else if (wavelength >= 580. && wavelength < 645.) {
R = 1.;
G = -1 * (wavelength - 645.) / (645. - 580.);
B = 0.;
}
else if (wavelength >= 645. && wavelength <= 780.) {
R = 1.;
G = 0.;
B = 0.;
}
else {
R = 0.;
G = 0.;
B = 0.;
}
// intensty is lower at the edges of the visible spectrum.
if (wavelength > 780. || wavelength < 380.) {
A = 0.;
}
else if (wavelength > 700.) {
A = (780. - wavelength) / (780. - 700.);
}
else if (wavelength < 420.) {
A = (wavelength - 380.) / (420. - 380.);
}
else {
A = 1.;
}
return sf::Color(R * 255 * A, G * 255 * A, B * 255 * A, 255);
}