00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011 #include <iostream>
00012 #include "skytexture.h"
00013 #include "rangecontrol.h"
00014 #include "heal.h"
00015 #include "map_exception.h"
00016
00017 using namespace std;
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029 SkyTexture::SkyTexture() : texture(0), texture_res(0), nside(0)
00030 {
00031 hilite_level = 128;
00032 select_level = 64;
00033 order = HealpixMap::Undefined;
00034 timer = new QTimer(this);
00035 timer->setInterval(50);
00036 connect(timer, SIGNAL(timeout()), this, SLOT(reTexture()));
00037
00038 }
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048 SkyTexture::~SkyTexture()
00049 {
00050 if( texture ) delete [] texture;
00051
00052 }
00053
00054
00055
00056
00057
00058
00059
00060
00061
00062
00063
00064
00065
00066 bool SkyTexture::buildLUT(const int ns, HealpixMap::PixOrder ordering)
00067 {
00068 if (ordering == HealpixMap::Undefined) return false;
00069 PixLUTCache &lut_cache = (ordering == HealpixMap::Ring)
00070 ? lut_cache_ring : lut_cache_nest;
00071 PixLUT &lut = lut_cache[ns];
00072 lut.resize(12*ns*ns);
00073 int face = 0, x = 0, y = 0;
00074 long dy = 4*ns;
00075 long pix;
00076 long k = 0;
00077 for(face = 0; face < 12; face++) {
00078 long xo = ns*(face % 4);
00079 long yo = ns*(face / 4);
00080 long face_offset = face*ns*ns;
00081 for(y = 0; y < ns; y++) {
00082 for(x = 0; x < ns; x++) {
00083 k = x + xo + (y+yo)*dy;
00084 pix = xy2pix(x,y) + face_offset;
00085 if (ordering == HealpixMap::Ring)
00086 nest2ring(ns,pix,&pix);
00087 lut[pix] = 4*k;
00088 }
00089 }
00090 }
00091 return true;
00092 }
00093
00094
00095
00096
00097
00098
00099
00100
00101
00102
00103
00104
00105
00106
00107 PixLUTCache::iterator SkyTexture::getLUT(const int ns, HealpixMap::PixOrder ordering)
00108 {
00109 PixLUTCache::iterator luti;
00110 if (ordering == HealpixMap::Undefined) return luti;
00111 PixLUTCache &lut_cache = (ordering == HealpixMap::Ring)
00112 ? lut_cache_ring : lut_cache_nest;
00113
00114 luti = lut_cache.find(ns);
00115 if( luti == lut_cache.end() ) buildLUT(ns, ordering);
00116 luti = lut_cache.find(ns);
00117 if( luti == lut_cache.end() ) {
00118 throw MapException(MapException::Other, 0, "SkyTexture::getLUT:Failed, aborting.");
00119 }
00120 return luti;
00121 }
00122
00123
00124
00125
00126
00127
00128
00129
00130
00131
00132
00133
00134 void SkyTexture::set(HealpixMap *skymap_in, RangeControl *rangedialog)
00135 {
00136
00137
00138
00139
00140 if( isRunning() ) {
00141 restart = true;
00142 wait();
00143 }
00144
00145
00146
00147
00148 skymap = skymap_in;
00149 order = skymap->pixordenum();
00150 lut = &(getLUT(skymap->nside(), skymap->pixordenum())->second);
00151
00152
00153
00154 if( skymap->nside() != (unsigned int) nside ) {
00155 nside = skymap->nside();
00156 texture_res = 4*nside;
00157 if( texture) delete[] texture;
00158 texture = new unsigned char[texture_res*texture_res*4];
00159 }
00160
00161
00162
00163
00164
00165 ct = rangedialog->getColorTable();
00166 dpyfield = rangedialog->getField();
00167 minv = rangedialog->getMinimum();
00168 maxv = rangedialog->getMaximum();
00169
00170
00171
00172 restart = false;
00173 update = true;
00174 timer->start();
00175 start();
00176
00177 return;
00178 }
00179
00180
00181
00182
00183
00184
00185
00186
00187
00188
00189
00190
00191
00192 void SkyTexture::run()
00193 {
00194
00195 float v = 0.0;
00196 long texk = 0;
00197 QColor color, blank(255, 255, 255, 255);
00198 for(uint pix = 0; pix < skymap->size(); pix++) {
00199
00200
00201
00202
00203
00204
00205
00206
00207
00208
00209
00210
00211
00212
00213
00214
00215
00216
00217
00218
00219 switch (dpyfield)
00220 {
00221 case Nobs :
00222 v = (*skymap)[pix].Nobs();
00223 break;
00224 case P :
00225 v = (*skymap)[pix].Pmag();
00226 break;
00227 case U :
00228 v = (*skymap)[pix].U();
00229 break;
00230 case Q :
00231 v = (*skymap)[pix].Q();
00232 break;
00233 case I :
00234 v = (*skymap)[pix].T();
00235 break;
00236 }
00237
00238 if (v < minv) v = minv;
00239 if (v > maxv) v = maxv;
00240 v = (v-minv)/(maxv-minv);
00241 color = (*ct)(v);
00242 texk = (*lut)[pix];
00243 texture[texk++] = color.red();
00244 texture[texk++] = color.green();
00245 texture[texk++] = color.blue();
00246 texture[texk++] = 255;
00247 if( restart ) {
00248
00249 return;
00250 }
00251 }
00252
00253
00254 update = false;
00255 return;
00256 }
00257
00258
00259
00260
00261
00262
00263
00264
00265
00266
00267
00268 void SkyTexture::reTexture()
00269 {
00270 glTexture();
00271 emit retextured();
00272
00273
00274
00275
00276 return;
00277 }
00278 void SkyTexture::glTexture()
00279 {
00280 glTexImage2D(
00281 GL_TEXTURE_2D, 0, 4,
00282 texture_res, texture_res, 0,
00283 GL_RGBA, GL_UNSIGNED_BYTE,
00284 texture
00285 );
00286 return;
00287 }
00288
00289
00290
00291
00292
00293
00294
00295
00296
00297
00298
00299
00300
00301 void SkyTexture::highlite (const int pix, float alpha)
00302 {
00303 long texk = (*lut)[pix];
00304 if (alpha < 0.) alpha = 0.;
00305 if (alpha > 1.) alpha = 1.;
00306 texture[texk+3] = int(255. * alpha);
00307 glTexture();
00308 }