-
Notifications
You must be signed in to change notification settings - Fork 4
/
skytexture.cpp
283 lines (249 loc) · 7.28 KB
/
skytexture.cpp
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
/* ============================================================================
'skytexture.cpp' defines the interface that converts a skymap into a texture
usable by the QGLViewer.
Written by Nicholas Phillips.
QT4 implementation. Michael R. Greason, ADNET, 29 December 2006.
============================================================================ */
/*
Fetch header files.
*/
#include "skytexture.h"
#include "rangecontrol.h"
#include "heal.h"
#include "map_exception.h"
using namespace std;
/* ----------------------------------------------------------------------------
'SkyViewer' is the class constructor.
Arguments:
None.
Written by Nicholas Phillips.
QT4 implementation. Michael R. Greason, ADNET, 28 August 2007.
---------------------------------------------------------------------------- */
SkyTexture::SkyTexture() : texture(0), texture_res(0), nside(0)
{
hilite_level = 128;
select_level = 64;
order = HealpixMap::Undefined;
timer = new QTimer(this);
timer->setInterval(50);
connect(timer, &QTimer::timeout,
this, static_cast<void(SkyTexture::*)()>(&SkyTexture::reTexture));
}
/* ----------------------------------------------------------------------------
'~SkyViewer' is the class destructor.
Arguments:
None.
Written by Nicholas Phillips.
QT4 implementation. Michael R. Greason, ADNET, 28 August 2007.
---------------------------------------------------------------------------- */
SkyTexture::~SkyTexture()
{
if( texture ) delete [] texture;
}
/* ----------------------------------------------------------------------------
'buildLUT' builds the lookup table for a given map size and pixel ordering.
Arguments:
ns - The desired nside.
ordering - The pixel ordering.
Returned:
res - Success? true=yes.
Written by Nicholas Phillips.
QT4 implementation. Michael R. Greason, ADNET, 28 August 2007.
---------------------------------------------------------------------------- */
bool SkyTexture::buildLUT(const int ns, HealpixMap::PixOrder ordering)
{
if (ordering == HealpixMap::Undefined) return false;
PixLUTCache &lut_cache = (ordering == HealpixMap::Ring)
? lut_cache_ring : lut_cache_nest;
PixLUT &lut = lut_cache[ns];
lut.resize(12*ns*ns);
int face = 0, x = 0, y = 0;
long dy = 4*ns;
long pix;
long k = 0;
for(face = 0; face < 12; face++) {
long xo = ns*(face % 4);
long yo = ns*(face / 4);
long face_offset = face*ns*ns;
for(y = 0; y < ns; y++) {
for(x = 0; x < ns; x++) {
k = x + xo + (y+yo)*dy;
pix = xy2pix(x,y) + face_offset;
if (ordering == HealpixMap::Ring)
nest2ring(ns,pix,&pix);
lut[pix] = 4*k;
}
}
}
return true;
}
/* ----------------------------------------------------------------------------
'getLUT' returns the lookup table for a given map size and pixel ordering. If
the table isn't defined, create it.
Arguments:
nside_ - The desired nside.
ordering - The pixel ordering.
Returned:
luti - The lookup table.
Written by Nicholas Phillips.
QT4 implementation. Michael R. Greason, ADNET, 28 August 2007.
---------------------------------------------------------------------------- */
PixLUTCache::iterator SkyTexture::getLUT(const int ns, HealpixMap::PixOrder ordering)
{
PixLUTCache::iterator luti;
if (ordering == HealpixMap::Undefined) return luti;
PixLUTCache &lut_cache = (ordering == HealpixMap::Ring)
? lut_cache_ring : lut_cache_nest;
luti = lut_cache.find(ns);
if( luti == lut_cache.end() ) buildLUT(ns, ordering);
luti = lut_cache.find(ns);
if( luti == lut_cache.end() ) {
throw MapException(MapException::Other, 0, "SkyTexture::getLUT:Failed, aborting.");
}
return luti;
}
/* ----------------------------------------------------------------------------
'set' fills the texture from a skymap and the state of the range dialog.
Arguments:
None.
Returned:
Nothing.
Written by Nicholas Phillips.
QT4 implementation. Michael R. Greason, ADNET, 28 August 2007.
---------------------------------------------------------------------------- */
void SkyTexture::set(HealpixMap *skymap_in, RangeControl *rangedialog)
{
/*
If there is a painting going on, tell it to stop
and wait for it to finish
*/
if( isRunning() ) {
restart = true;
wait();
}
/*
Retrieve the lookup table. If it doesn't bomb then assume
things are fine.
*/
skymap = skymap_in;
order = skymap->pixordenum();
lut = &(getLUT(skymap->nside(), skymap->pixordenum())->second);
/*
Make sure texture buffer is correct size
*/
if( skymap->nside() != (unsigned int) nside ) {
nside = skymap->nside();
texture_res = 4*nside;
if( texture) delete[] texture;
texture = new unsigned char[texture_res*texture_res*4];
}
/*
Retrieve the color table, the minimum and maximum, and the
map field to display.
*/
// get fields from RangeDialog
ct = rangedialog->getColorTable();
dpyfield = rangedialog->getField();
minv = rangedialog->getMinimum();
maxv = rangedialog->getMaximum();
/*
Start the construction the color table.
*/
restart = false;
update = true;
timer->start(); // no harm if already running
start();
return;
}
/* ----------------------------------------------------------------------------
'run' fills the texture from a skymap, as a separate thread from the GUI
Arguments:
None.
Returned:
Nothing.
Written by Nicholas Phillips.
---------------------------------------------------------------------------- */
void SkyTexture::run()
{
float v = 0.0;
long texk = 0;
QColor color, blank(255, 255, 255, 255);
for(uint pix = 0; pix < skymap->size(); pix++) {
switch (dpyfield)
{
case Nobs : // N Obs
v = (*skymap)[pix].Nobs();
break;
case P : // P Polarization
v = (*skymap)[pix].Pmag();
break;
case U : // U Polarization
v = (*skymap)[pix].U();
break;
case Q : // Q Polarization
v = (*skymap)[pix].Q();
break;
case I : // Temperature
v = (*skymap)[pix].T();
break;
}
if (v < minv) v = minv;
if (v > maxv) v = maxv;
v = (v-minv)/(maxv-minv);
color = (*ct)(v);
texk = (*lut)[pix];
texture[texk++] = color.red();
texture[texk++] = color.green();
texture[texk++] = color.blue();
texture[texk++] = 255;
if( restart ) {return;}
}
update = false;
return;
}
/* ----------------------------------------------------------------------------
'glTexture' assigns the texture to the OpenGL system.
Arguments:
None.
Returned:
Nothing.
Written by Nicholas Phillips.
---------------------------------------------------------------------------- */
void SkyTexture::reTexture()
{
glTexture();
emit retextured();
/*
if( ! update )
timer->stop();
*/
return;
}
void SkyTexture::glTexture()
{
glTexImage2D(
GL_TEXTURE_2D, 0, 4,
texture_res, texture_res, 0,
GL_RGBA, GL_UNSIGNED_BYTE,
texture
);
return;
}
/* ----------------------------------------------------------------------------
'highlite' highlights a pixel by setting its transparency.
Arguments:
pix - The pixel to tweak.
alpha - The opacity of the pixel. 1 indicates opaque; 0 indicates
transparent.
Returned:
Nothing.
Written by Nicholas Phillips.
---------------------------------------------------------------------------- */
void SkyTexture::highlite (const int pix, float alpha)
{
long texk = (*lut)[pix];
if (alpha < 0.) alpha = 0.;
if (alpha > 1.) alpha = 1.;
texture[texk+3] = int(255. * alpha);
glTexture();
}