skytexture.cpp

Go to the documentation of this file.
00001 /* ============================================================================
00002 'skytexture.cpp' defines the interface that converts a skymap into a texture
00003 usable by the QGLViewer.
00004 
00005 Written by Nicholas Phillips.
00006 QT4 implementation.  Michael R. Greason, ADNET, 29 December 2006.
00007 ============================================================================ */
00008 /*
00009                         Fetch header files.
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 'SkyViewer' is the class constructor.
00022 
00023 Arguments:
00024         None.
00025 
00026 Written by Nicholas Phillips.
00027 QT4 implementation.  Michael R. Greason, ADNET, 28 August 2007.
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 '~SkyViewer' is the class destructor.
00041 
00042 Arguments:
00043         None.
00044 
00045 Written by Nicholas Phillips.
00046 QT4 implementation.  Michael R. Greason, ADNET, 28 August 2007.
00047 ---------------------------------------------------------------------------- */
00048 SkyTexture::~SkyTexture()
00049 {
00050         if( texture ) delete [] texture;
00051 
00052 }
00053 /* ----------------------------------------------------------------------------
00054 'buildLUT' builds the lookup table for a given map size and pixel ordering.
00055 
00056 Arguments:
00057         ns       - The desired nside.
00058         ordering - The pixel ordering.
00059 
00060 Returned:
00061         res      - Success?  true=yes.
00062 
00063 Written by Nicholas Phillips.
00064 QT4 implementation.  Michael R. Greason, ADNET, 28 August 2007.
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 'getLUT' returns the lookup table for a given map size and pixel ordering.  If
00095 the table isn't defined, create it.
00096 
00097 Arguments:
00098         nside_   - The desired nside.
00099         ordering - The pixel ordering.
00100 
00101 Returned:
00102         luti     - The lookup table.
00103 
00104 Written by Nicholas Phillips.
00105 QT4 implementation.  Michael R. Greason, ADNET, 28 August 2007.
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 'set' fills the texture from a skymap and the state of the range dialog.
00124 
00125 Arguments:
00126         None.
00127 
00128 Returned:
00129         Nothing.
00130 
00131 Written by Nicholas Phillips.
00132 QT4 implementation.  Michael R. Greason, ADNET, 28 August 2007.
00133 ---------------------------------------------------------------------------- */
00134 void SkyTexture::set(HealpixMap *skymap_in, RangeControl *rangedialog)
00135 {
00136 /*
00137                         If there is a painting going on, tell it to stop
00138                         and wait for it to finish
00139 */
00140         if( isRunning() ) {
00141                 restart = true;
00142                 wait();
00143         }
00144 /*
00145                         Retrieve the lookup table.  If it doesn't bomb then assume
00146                         things are fine.
00147 */
00148         skymap = skymap_in;
00149         order = skymap->pixordenum();
00150         lut = &(getLUT(skymap->nside(), skymap->pixordenum())->second);
00151 /*
00152                         Make sure texture buffer is correct size
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                         Retrieve the color table, the minimum and maximum, and the 
00162                         map field to display.
00163 */
00164         // get fields from RangeDialog
00165         ct = rangedialog->getColorTable();
00166         dpyfield = rangedialog->getField();
00167         minv = rangedialog->getMinimum();
00168         maxv = rangedialog->getMaximum();
00169 /*
00170                         Start the construction the color table.
00171 */
00172         restart = false;
00173         update = true;
00174         timer->start(); // no harm if already running
00175         start();
00176 
00177         return;
00178 }
00179 
00180 /* ----------------------------------------------------------------------------
00181 'run' fills the texture from a skymap, as a separate thread from the GUI
00182 
00183 
00184 Arguments:
00185         None.
00186 
00187 Returned:
00188         Nothing.
00189 
00190 Written by Nicholas Phillips.
00191 ---------------------------------------------------------------------------- */
00192 void SkyTexture::run()
00193 {
00194         //cout << "starting paint" << endl;
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                 switch (dpyfield)
00201                 {
00202                         case 4 :                        // N Obs
00203                                 v = (*skymap)[pix].Nobs();
00204                                 break;
00205                         case 3 :                        // P Polarization
00206                                 v = (*skymap)[pix].Pmag();
00207                                 break;
00208                         case 2 :                        // U Polarization
00209                                 v = (*skymap)[pix].U();
00210                                 break;
00211                         case 1 :                        // Q Polarization
00212                                 v = (*skymap)[pix].Q();
00213                                 break;
00214                         default :                       // Temperature
00215                                 v = (*skymap)[pix].T();
00216                                 break;
00217                 }
00218                 */
00219                 switch (dpyfield)
00220                 {
00221                         case Nobs :                     // N Obs
00222                                 v = (*skymap)[pix].Nobs();
00223                                 break;
00224                         case P :                        // P Polarization
00225                                 v = (*skymap)[pix].Pmag();
00226                                 break;
00227                         case U :                        // U Polarization
00228                                 v = (*skymap)[pix].U();
00229                                 break;
00230                         case Q :                        // Q Polarization
00231                                 v = (*skymap)[pix].Q();
00232                                 break;
00233                         case I :                        // Temperature
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                         //cout << "\tearly return" << endl;
00249                         return;
00250                 }
00251         }
00252         //glTexture();
00253         //cout << "\tcompleted" << endl;
00254         update = false;
00255         return;
00256 }
00257 /* ----------------------------------------------------------------------------
00258 'glTexture' assigns the texture to the OpenGL system.
00259 
00260 Arguments:
00261         None.
00262 
00263 Returned:
00264         Nothing.
00265 
00266 Written by Nicholas Phillips.
00267 ---------------------------------------------------------------------------- */
00268 void SkyTexture::reTexture()
00269 {
00270         glTexture();
00271         emit retextured();
00272         /*
00273         if( ! update )
00274                 timer->stop();
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 'highlite' highlights a pixel by setting its transparency.
00290 
00291 Arguments:
00292         pix   - The pixel to tweak.
00293         alpha - The opacity of the pixel.  1 indicates opaque; 0 indicates
00294                 transparent.
00295 
00296 Returned:
00297         Nothing.
00298 
00299 Written by Nicholas Phillips.
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 }

Generated on Fri Feb 6 15:32:42 2009 for Skyviewer by  doxygen 1.4.7