skyviewer.cpp

Go to the documentation of this file.
00001 /* ============================================================================
00002 'skyviewer.cpp' extends the QGLViewer class.
00003 ============================================================================ */
00004 /*
00005                         Fetch header files.
00006 */
00007 #include <iostream>
00008 #include <QFile>
00009 #include <QTextStream>
00010 #include "skyviewer.h"
00011 #include "mainwindow.h"
00012 #include "heal.h"
00013 #include "debug.h"
00014 
00015 using namespace qglviewer;
00016 using namespace std;
00017 /*
00018                         Constants.
00019 */
00020 static const char stateFile[] = {".skyviewer.xml"};
00021 /* ============================================================================
00022 'SkyViewer' extends the QGLViewer class.
00023 ============================================================================ */
00024 /* ----------------------------------------------------------------------------
00025 'SkyViewer' is the class constructor.
00026 
00027 Arguments:
00028         parent - The parent widget.
00029         name   - The name of this widget.
00030 
00031 Written by Nicholas Phillips.
00032 QT4 implementation.  Michael R. Greason, ADNET, 28 August 2007.
00033 ---------------------------------------------------------------------------- */
00034 SkyViewer::SkyViewer (QWidget *parent, mainWindow *mw) : 
00035         QGLViewer(parent), whitecolor(255,255,255,255), blackcolor(0,0,0,255)
00036 {
00037         pulseflg  = true;
00038         mollview  = false;
00039         mwin      = mw;
00040         texture   = NULL;
00041         rigging   = NULL;
00042         whiterig  = NULL;
00043         polar     = NULL;
00044         hlite     =  1.0;
00045         delhlite  = -0.1;
00046         setStateFileName(stateFile);
00047 }
00048 /* ----------------------------------------------------------------------------
00049 '~SkyViewer' is the class destructor.  It does NOT delete the rigging, texture,
00050 or polar lines members.
00051 
00052 Arguments:
00053         None.
00054 
00055 Written by Nicholas Phillips.
00056 QT4 implementation.  Michael R. Greason, ADNET, 28 August 2007.
00057 ---------------------------------------------------------------------------- */
00058 SkyViewer::~SkyViewer()
00059 {
00060         saveStateToFile();
00061         texture  = NULL;
00062         rigging  = NULL;
00063         whiterig = NULL;
00064         polar    = NULL;
00065         if (constraint != NULL) delete constraint;
00066         constraint = NULL;
00067 }
00068 /* ----------------------------------------------------------------------------
00069 'setTexture' assigns the texture to be displayed.
00070 
00071 Arguments:
00072         t - The new texture.
00073 
00074 Returned:
00075         None.
00076 
00077 Written by Nicholas Phillips.
00078 QT4 implementation.  Michael R. Greason, ADNET, 28 August 2007.
00079 ---------------------------------------------------------------------------- */
00080 void SkyViewer::setTexture(SkyTexture *t)
00081 {
00082         if( texture )
00083                 disconnect( texture, SIGNAL(retextured()), this, SLOT( updateGL() ) );
00084         
00085         texture = t;
00086         connect( texture, SIGNAL(retextured()), this, SLOT( updateGL() ) );
00087 }
00088 /* ----------------------------------------------------------------------------
00089 'init' is called the first time this widget is displayed, initializing the
00090 widget.
00091 
00092 Arguments:
00093         None.
00094 
00095 Returned:
00096         None.
00097 
00098 Written by Nicholas Phillips.
00099 QT4 implementation.  Michael R. Greason, ADNET, 28 August 2007.
00100 ---------------------------------------------------------------------------- */
00101 void SkyViewer::init(void)
00102 {
00103 /*
00104                         General configuration.
00105 */
00106         setBackgroundColor(QColor(0,0,0));      // Black background.
00107         glEnable(GL_BLEND);
00108         glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
00109         //setFPSIsDisplayed(true);
00110 /*
00111                         Define the lighting.
00112 */
00113         GLfloat light_ambient[] = { 1.0, 1.0, 1.0, 1.0 };
00114         GLfloat light_zero[] = { 0.0, 0.0, 0.0, 1.0 };
00115         glEnable(GL_LIGHT0);
00116         glLightfv(GL_LIGHT0, GL_AMBIENT, light_ambient);
00117         glLightfv(GL_LIGHT0, GL_DIFFUSE, light_zero);
00118         glLightfv(GL_LIGHT0, GL_SPECULAR, light_zero);
00119 /*
00120                         Enable GL textures.
00121 */
00122         glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST );
00123         glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST );
00124         glEnable( GL_TEXTURE_2D );
00125                         // Nice texture coordinate interpolation
00126         glHint( GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST );
00127 /*
00128 
00129                         Configure the camera.
00130 */
00131         camera()->setPosition( Vec(3.0,0,0) );
00132         camera()->lookAt( Vec(0,0,0) );
00133         camera()->setUpVector(Vec(0,0,1));
00134         constraint = new CameraConstraint(camera());
00135         constraint->setRotationConstraintType(AxisPlaneConstraint::FREE);
00136         camera()->frame()->setConstraint(constraint);
00137 /*
00138                         Configure animation.
00139 */
00140         setAnimationPeriod(200);
00141 /*
00142                         Recover the state from a prior run.
00143 */
00144         restoreStateFromFile(); 
00145 }
00146 /* ----------------------------------------------------------------------------
00147 'draw' is called the whenever the widget needs to be drawn on the screen.
00148 
00149 Arguments:
00150         None.
00151 
00152 Returned:
00153         None.
00154 
00155 Written by Nicholas Phillips.
00156 QT4 implementation.  Michael R. Greason, ADNET, 28 August 2007.
00157 ---------------------------------------------------------------------------- */
00158 void SkyViewer::draw(void)
00159 {
00160         if (showrigging) {
00161                 glDisable( GL_TEXTURE_2D );
00162                 rigging->draw();
00163         }
00164         if (showtex) {
00165                 bool sr = showrigging;
00166                 showrigging = false;
00167                 if (whiterig != NULL)
00168                 {
00169                         glDisable(GL_TEXTURE_2D);
00170                         if (pulseflg)
00171                         {
00172                                 glColor4f(whitecolor.redF(), whitecolor.greenF(), whitecolor.blueF(), 1.);
00173                         }
00174                         else
00175                         {
00176                                 glColor4f(blackcolor.redF(), blackcolor.greenF(), blackcolor.blueF(), 1.);
00177                         }
00178                         whiterig->draw();
00179                 }
00180                 if (rigging != NULL)
00181                 {
00182                         glColor4f(1., 1., 1., 1.);
00183                         if (texture != NULL) glEnable( GL_TEXTURE_2D );
00184                         rigging->draw();
00185                 }
00186                 showrigging = sr;
00187         }
00188         if ((polar != NULL) && (polar->isOn()) && showpolar) {
00189                 glDisable( GL_TEXTURE_2D );
00190                 polar->draw();
00191         }
00192 }
00193 /* ----------------------------------------------------------------------------
00194 'animate' is called to perform the next step in animating the viewer.
00195 
00196 Arguments:
00197         None.
00198 
00199 Returned:
00200         None.
00201 
00202 Written by Nicholas Phillips.
00203 QT4 implementation.  Michael R. Greason, ADNET, 28 August 2007.
00204 ---------------------------------------------------------------------------- */
00205 void SkyViewer::animate (void)
00206 {
00207         const double llim = 0.3, ulim = 0.9;
00208         hlite += delhlite;
00209         if (hlite <= llim)
00210         {
00211                 hlite    = llim;
00212                 delhlite = -delhlite;
00213         }
00214         if (hlite >= ulim)
00215         {
00216                 hlite    = ulim;
00217                 delhlite = -delhlite;
00218                 pulseflg = pulseflg ? false : true;
00219         }
00220         mwin->highlightPixels(hlite);
00221 }
00222 /* ----------------------------------------------------------------------------
00223 'postSelection' is called at the end of the selection process by the QGLViewer
00224 library.
00225 
00226 Arguments:
00227         pt - The point in the widget where the selection took place.
00228 
00229 Returned:
00230         None.
00231 
00232 Written by Nicholas Phillips.
00233 QT4 implementation.  Michael R. Greason, ADNET, 30 August 2007.
00234 ---------------------------------------------------------------------------- */
00235 void SkyViewer::postSelection (const QPoint &pt)
00236 {
00237         Vec orig, dir;
00238         double lambda, phi;
00239         camera()->convertClickToLine(pt, orig, dir);
00240         if (! rigging->projectSelection(orig, dir, phi, lambda)) return;
00241         mwin->selectPixel(phi, lambda);
00242 }
00243 /* ----------------------------------------------------------------------------
00244 'constrainMollweide' constrains the camera if entering a Mollweide projection.
00245 
00246 Arguments:
00247         b - Entering Mollweide?.
00248 
00249 Returned:
00250         None.
00251 
00252 Written by Nicholas Phillips.
00253 QT4 implementation.  Michael R. Greason, ADNET, 31 August 2007.
00254 ---------------------------------------------------------------------------- */
00255 void SkyViewer::constrainMollweide (bool b)
00256 {
00257 /*
00258                         Mollweide.
00259 */
00260         mollview = b;
00261         if( mollview )
00262         {
00263                 camera()->setPosition( Vec(-3.0,0,0) );
00264                 camera()->lookAt( Vec(0,0,0) );
00265                 camera()->setUpVector(Vec(0,0,1));
00266                 constraint->setRotationConstraintType(AxisPlaneConstraint::FORBIDDEN);
00267                 camera()->frame()->setConstraint(constraint);
00268         }
00269 /*
00270                         3D sphere.
00271 */
00272         else
00273         {
00274                 constraint->setRotationConstraintType(AxisPlaneConstraint::FREE);
00275                 constraint->setTranslationConstraintType(AxisPlaneConstraint::FREE);
00276                 camera()->frame()->setConstraint(constraint);
00277         }
00278 }
00279 /* ----------------------------------------------------------------------------
00280 'helpString' defines the general help string for the application.  Used by
00281 QGLViewer to generate the help window.
00282 
00283 Arguments:
00284         None.
00285 
00286 Returned:
00287         None.
00288 
00289 Written by Nicholas Phillips.
00290 QT4 implementation.  Michael R. Greason, ADNET, 30 August 2007.
00291 ---------------------------------------------------------------------------- */
00292 QString SkyViewer::helpString () const
00293 {
00294         QString line, contents;
00295 /*
00296                         Read the file.
00297 */
00298         QFile qf(":/general.txt");
00299         qf.open(QIODevice::ReadOnly | QIODevice::Text);
00300         QTextStream in(&qf);
00301         while (!in.atEnd())
00302         {
00303                 line = in.readLine();
00304                 contents += line;
00305         }
00306         qf.close();
00307 /*
00308                         Write its contents.
00309 */
00310         return contents;
00311 }
00312 /* ----------------------------------------------------------------------------
00313 'recenterAt' recenters the current view to be centered on the passed
00314         pixel coordinates. 
00315 
00316 Arguments:
00317         x,y,z: coords of selected pixel
00318 Returned:
00319         None.
00320 
00321 Written by Nicholas Phillips, 10/14/08
00322 ---------------------------------------------------------------------------- */
00323 void SkyViewer::recenterAt(const double x_, const double y_, const double z_)
00324 {
00325         cout << "SkyViewer::recenterAt" << endl;
00326 
00327         cout << "pos:  " << camera()->position();
00328         cout << "\tlookat:  " << camera()->viewDirection();
00329         cout << "\tup:  " << camera()->upVector() << endl;
00330 
00331         Vec p(x_,y_,z_);
00332         Vec z(0,0,1);
00333         Vec d = z-(p*z)*p;
00334 
00335 
00336         cout << "new pos: " << p;
00337         p *= camera()->position().norm();
00338         cout << " -> " << p << endl;
00339 
00340         camera()->setPosition( p );
00341         camera()->setViewDirection(-p);
00342         camera()->setUpVector( d );
00343         
00344         cout << "pos:  " << camera()->position();
00345         cout << "\tlookat:  " << camera()->viewDirection();
00346         cout << "\tup:  " << camera()->upVector() << endl;
00347 
00348         cout << "d: " << d << ", d.p: "<< d*p << ",  d.z:  " << d*z << endl;
00349         cout << endl;
00350 
00351         return;
00352 }
00353 /* ----------------------------------------------------------------------------
00354 'recenterAt' recenters the current view to be centered on the passed
00355         pixel coordinates. 
00356 
00357 Arguments:
00358         x,y,z: coords of selected pixel
00359 Returned:
00360         None.
00361 
00362 Written by Nicholas Phillips, 10/14/08
00363 ---------------------------------------------------------------------------- */
00364 void SkyViewer::recenterAt(const double theta, const double phi)
00365 {
00366         Vec pos;        // Camera position
00367         Vec view;       // View direction
00368         Vec up;         // Up direction for camera
00369         
00370         if( mollview ) {
00371                 double phi = asin(cos(theta));
00372                 double lambda = atan2(sin(phi)*sin(theta),cos(phi)*sin(theta));
00373                 double x,y;
00374 		::toMollweide(phi, lambda, x, y);
00375                 //x *= -1;
00376 		::toMollweide(phi,theta,x,y);
00377                 x /= sqrt(2.0);
00378                 y /= sqrt(2.0);
00379                 cout << camera()->position() << "\t" << x << ", "<< y <<endl;
00380                 pos = Vec(camera()->position()[0],x,y);
00381                 view = Vec(-1,0,0);
00382                 up = Vec(0,0,1);
00383         }
00384         else {
00385                 Vec z(0,0,1);
00386                 pos = Vec(cos(phi)*sin(theta),sin(phi)*sin(theta),cos(theta));
00387                 up = z-(pos*z)*pos;     // Up direction
00388                 view = -pos;
00389                 pos *= camera()->position().norm();     // Same zoom as before
00390         }
00391         camera()->setPosition( pos );
00392         camera()->setViewDirection( view );
00393         camera()->setUpVector( up );
00394 
00395         return;
00396 }

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