Main Page | Namespace List | Class Hierarchy | Alphabetical List | Class List | Directories | File List | Namespace Members | Class Members | File Members | Related Pages

vuBasicUtility.cpp

Go to the documentation of this file.
00001 #include <wx/wx.h>
00002 #include <iostream.h>
00003 #include "vuBasicUtility.h"
00004 #include "../vuMainWindow.h"
00005 #include "../wxUIElements/vuKeyFramerDialog/vuKeyFramerDialog.h"
00006 #include "../wxUIElements/vuTransferDialog.h"
00007 #include "vuArcBall.h"
00008 
00009 //----------------------------------------------------------------------------
00010 //------------------------- The vuBasicGLCanvas implementation ---------------
00011 //----------------------------------------------------------------------------
00012 
00013 BEGIN_EVENT_TABLE(vuBasicGLCanvas, vuGLCanvas)
00014     EVT_MOUSE_EVENTS(vuBasicGLCanvas::OnMouse)
00015     EVT_CHAR(vuBasicGLCanvas::OnChar)
00016 END_EVENT_TABLE();
00017 
00018 
00019 vuBasicGLCanvas::vuBasicGLCanvas(vuBasicUtility *parent)
00020     : vuGLCanvas(parent), m_Parent(parent)
00021 
00022 {
00023 //      m_camera = parent->get_camera ();
00024 };
00025 
00026 void vuBasicUtility::notifyDataChanged ()
00027 
00028 {
00029 }
00030 
00031 bool vuBasicGLCanvas::glInit()
00032 {
00033     return m_Parent->glInit();
00034 }
00035 
00036 void vuBasicGLCanvas::render()
00037 {
00038   m_Parent->glRender();
00039 };
00040 
00041 void vuBasicGLCanvas::resize()
00042 {
00043     m_Parent->glResize();
00044 };
00045 
00046 void vuBasicGLCanvas::OnMouse(wxMouseEvent &ev)
00047 {
00048     m_Parent->glOnMouse(ev);
00049 };
00050 
00051 void vuBasicGLCanvas::OnChar(wxKeyEvent& event)
00052 {
00053   m_Parent->OnChar(event);
00054 }
00055 
00056 //----------------------------------------------------------------------------
00057 //------------------------- public: vuBasicUtility ---------------------------
00058 //----------------------------------------------------------------------------
00059 
00060 vuBasicUtility::vuBasicUtility() : m_helpPanel(this)
00061 {
00062     m_Main        = 0;
00063     m_glCanvas    = NULL;
00064     m_camera      = NULL;
00065     m_DrawPreview = true;
00066     m_keyframer   = NULL;
00067 }
00068 
00069 //----------------------------------------------------------------------------
00070 //------------------------- public: ~vuBasicUtility --------------------------
00071 //----------------------------------------------------------------------------
00072 
00073 vuBasicUtility::~vuBasicUtility()
00074 {
00075         //If the main window did not close the utility, then notify
00076         //that it is closed.  This has to be called from the destructor
00077         //because the OnClose event for the child window does not work
00078         //properly (it sets the this pointer to the wxMDIChild and not
00079         //to the vuBasicWindow, so you can't access data).
00080     if (m_Main!=0) m_Main->notifyClosed(this);
00081 }
00082 /*
00083 void vuBasicGLCanvas::set_camera (vuCamera *cam)
00084 
00085 {
00086         m_camera = cam;
00087 }
00088 
00089 vuCamera* vuBasicGLCanvas::get_camera ()
00090 
00091 {
00092         return m_camera;
00093 }
00094 
00095 */
00096 //----------------------------------------------------------------------------
00097 //------------------------- public: close() ----------------------------------
00098 //----------------------------------------------------------------------------
00099 
00100 void vuBasicUtility::close()
00101 {
00102         //indicate that the main window should not be called anymore
00103     m_Main = NULL;
00104         //Call the regular window close function
00105     Close();
00106 }
00107 
00108 /*
00109 void vuBasicUtility::DrawAgain ()
00110 
00111 {
00112         cerr << "vuBasicUtility:;DrawAgain" << endl;
00113         glRender ();
00114 }
00115 
00116 void vuBasicUtility::setCamera (vuCamera *cam)
00117 
00118 {
00119         m_camera = cam;
00120 }
00121 
00122 vuCamera* vuBasicUtility::getCamera ()
00123 
00124 {
00125         return m_camera;
00126 }
00127 */
00128 
00129 
00130 const vuBasicGLCanvas *vuBasicUtility::getCanvas()
00131 {
00132   return m_glCanvas;
00133 }
00134 
00135 //----------------------------------------------------------------------------
00136 //------------------------- public: init() -----------------------------------
00137 //----------------------------------------------------------------------------
00138 
00139 bool vuBasicUtility::init(vuMainWindow *main,const char* DataFile)
00140 {
00141     m_Main=main;
00142 
00143     //Create the window and show it
00144     //The parent must be explicitly cast as the proper wxWindow class so
00145     //that the Create() method can distinguish between MDI and non-MDI.
00146     bool success = Create((wxPARENTWINDOW*)m_Main,-1,"Utility",
00147                           wxPoint(-1,-1),wxSize(-1,-1),wxDEFAULT_FRAME_STYLE
00148                           |wxRESIZE_BORDER,"Utility");
00149 
00150     if (!success) return false;
00151     Show(false);
00152 
00153     //Create the OpenGL canvas.
00154     //It doesn't have to be deleted later because ownership is
00155     //passed to wxWindows
00156     m_glCanvas = new vuBasicGLCanvas(this);
00157 
00158     //create the window here
00159     //First the sizers.
00160     wxBoxSizer *mainSizer = new wxBoxSizer(wxVERTICAL);
00161     wxBoxSizer *topSizer = new wxBoxSizer(wxHORIZONTAL);
00162     wxBoxSizer *midSizer = new wxBoxSizer(wxHORIZONTAL);
00163     wxBoxSizer *botSizer = new wxBoxSizer(wxHORIZONTAL);
00164     wxBoxSizer *lefSizer = new wxBoxSizer(wxVERTICAL);
00165     wxBoxSizer *cenSizer = new wxBoxSizer(wxVERTICAL);
00166     wxBoxSizer *rigSizer = new wxBoxSizer(wxVERTICAL);
00167     //Add the glCanvas.
00168     m_glCanvas->SetSize(-1,-1,512,512);
00169     cenSizer->Add(m_glCanvas,1,wxEXPAND);
00170     //Add any derived window controls
00171     addTop(topSizer);
00172     addBottom(botSizer);
00173     addLeft(lefSizer);
00174     addRight(rigSizer);
00175     //Construct the window architecture.
00176     /*
00177     midSizer->Add(lefSizer,0,wxEXPAND);
00178     midSizer->Add(cenSizer,1,wxEXPAND);
00179     midSizer->Add(rigSizer,0,wxEXPAND);
00180     mainSizer->Add(topSizer);
00181     mainSizer->Add(midSizer,1,wxEXPAND);
00182     mainSizer->Add(botSizer);
00183     */
00184     midSizer->Add(lefSizer,0,wxGROW);
00185     midSizer->Add(cenSizer,1,wxEXPAND);
00186     midSizer->Add(rigSizer,0,wxGROW);
00187     mainSizer->Add(topSizer,0,wxGROW);
00188     mainSizer->Add(midSizer,1,wxEXPAND);
00189     mainSizer->Add(botSizer,0,wxGROW);
00190     //Set the window up to use the sizers
00191     SetSizer(mainSizer);
00192     SetAutoLayout(true);
00193     mainSizer->SetSizeHints( this );
00194 
00195     //Call the utility initialization function
00196     success = init(DataFile);
00197     if (success) Show(true);
00198     return success;
00199 };
00200 
00201 void vuBasicUtility::useOpenGL(bool yesorno)
00202 {
00203   if(m_glCanvas)
00204     if(yesorno)
00205       m_glCanvas->enableOpenGL();
00206     else
00207       m_glCanvas->disableOpenGL();
00208 }
00209 
00210 bool vuBasicUtility::IsReRendering ()
00211 
00212 {
00213         return false;
00214 }
00215 
00216 void vuBasicUtility::setIsReRendering (bool isit)
00217 {
00218 }
00219 
00220 
00221 /* ------------------------------------------------------------------------- */
00222 /* ------------------------- MOUSE handlers -------------------------------- */
00223 /* ------------------------------------------------------------------------- */
00224 
00237 void vuBasicUtility::glOnMouse(wxMouseEvent &event)
00238 {
00239   if (event.LeftDown() || event.RightDown())
00240     storeMousePosition(event);
00241   else if (event.LeftIsDown() && event.Moving())
00242     onMouseLeftMoving(event);
00243   else if (event.RightIsDown() && event.Moving())
00244     onMouseRightMoving(event);
00245   //currently cannot invoke transfer function dialog on left
00246   //double click because each group of algorithms can have their
00247   //own type of transfer function, even though only one type
00248   //of transfer function is currently used in vuVolume. See further
00249   //comments for volume::m_TFunc (which is commented out right now).
00250 
00251   //else if (event.LeftDClick())
00252     //invoke the transfer function dialog
00253     //onLeftDoubleClick(event);
00254   else
00255     onMouse(event);
00256 }
00257 
00258 
00260 void vuBasicUtility::onMouse(wxMouseEvent &event)
00261 {
00262   // this is supposed to be overwritten by subclasses
00263 }
00264 
00265 //----------------------------------------------------------------------------
00266 
00267 //The following function can only be implemented if volume::m_TFunc
00268 //is to be uncommented during a later architectural revision. Please
00269 //read comments within vuBasicUtility::onMouse() and for volume::m_TFunc.
00270 /*
00271 void vuBasicUtility::onLeftDoubleClick(wxMouseEvent &event)
00272 {
00273   vu1* volume = this->getVolume(); //invokes child class function.
00274   //polymorphically gets the volume data from the utility window
00275   //that inherits from vuBasicUtility.
00276 
00277   vuTransferDialog dlg(this,this->m_TFunc);
00278 
00279   if (dlg.ShowModal() == wxID_OK)
00280     {
00281       m_TFunc = dlg.getTransferFunc();
00282       volume->setTransferFunc(m_TFunc);
00283       m_glCanvas->redraw();
00284     }
00285 
00286   //Store the click position. Not quite sure why, but
00287   //the other mouse handlers do this, so I just copied.
00288   storeMousePosition(event);
00289 }
00290 */
00291 
00292 //----------------------------------------------------------------------------
00293 
00297 void vuBasicUtility::onMouseRightMoving(wxMouseEvent &event)
00298 {
00299   vu1 *volume = getVolume();
00300 
00301   if (volume != NULL) {
00302     vuCamera *camera = volume->getCameraPtr();
00303 
00304     if (camera) {
00305       float s = ((float)event.GetY() - m_MouseY)/1.0f;
00306       camera->translateXYZ(0.0f, 0.0f, -s);
00307       glResize();
00308       volume->setIsReRendering(true);
00309       m_glCanvas->redraw();
00310     }
00311     else
00312       cerr << "vuBasicUtility::onMouseRightMoving: camera is not set" << endl;
00313   }
00314   //Store the click position.
00315   storeMousePosition(event);
00316 }
00317 
00321 void vuBasicUtility::onMouseLeftMoving(wxMouseEvent &event)
00322 {
00323   vu1 *volume = getVolume();
00324 
00325   if (volume) {
00326     vuCamera *camera = volume->getCameraPtr();
00327 
00328     if (camera) {
00329       vuVector t = camera->getPosition() - volume->getCenter();
00330       float d = t.norm();
00331       camera->translateXYZ(0.0f, 0.0f, d);
00332       t = camera->getPosition();
00333       //use the arc ball
00334       vuArcBall ball;
00335       ball.attachCamera(volume->getCamera());
00336       ball.setWinSize(int (camera->getWidth()), int (camera->getHeight()));
00337       ball.turn(m_MouseX, m_MouseY, event.GetX(), event.GetY());
00338       camera->translateXYZ(0.0f, 0.0f, -d);
00339       camera->init();
00340       volume->setIsReRendering(true);
00341       m_glCanvas->redraw();
00342     }
00343     else
00344       cerr << "vuBasicUtility::onMouseLeftMoving: camera is not set" << endl;
00345   }
00346   //Store the click position.
00347   storeMousePosition(event);
00348 }
00349 
00351 void vuBasicUtility::storeMousePosition(wxMouseEvent &event)
00352 {
00353   m_MouseX = (int) event.GetX();
00354   m_MouseY = (int) event.GetY();
00355 }
00356 
00357 
00358 
00359 /* ------------------------------------------------------------------------- */
00360 /* ----------------------- KEYBOARD handlers ------------------------------- */
00361 /* ------------------------------------------------------------------------- */
00362 
00376 void vuBasicUtility::OnChar(wxKeyEvent& event)
00377 {
00378   switch(event.GetKeyCode()) {
00379     case '?':
00380       onKeyboardHelp(event);
00381       break;
00382     case 'k':
00383       onKeyboardKeyframer(event);
00384       break;
00385     case 13: {
00386       SetStatusText("Rendering...");
00387       Refresh();
00388       vu1 *volume = getVolume();
00389       if (volume) volume->setIsReRendering(true);
00390       m_DrawPreview = false;
00391       m_glCanvas->redraw();
00392       break;
00393     }
00394     case 316: // left 
00395     case 317: // up
00396     case 318: // right 
00397     case 319: // down 
00398       onKeyboardRotate(event);
00399       break;
00400     case '-':
00401     case '+':
00402       onKeyboardZoom(event);
00403       break;
00404     default:
00405       return onKeyboard(event);
00406   }
00407 }
00408 
00410 void vuBasicUtility::onKeyboard(wxKeyEvent &event)
00411 {
00412   // this is supposed to be overwritten by subclasses
00413 }
00414 
00416 void vuBasicUtility::onKeyboardHelp(wxKeyEvent& event)
00417 {
00418   m_helpPanel.setTitle("Help");
00419   m_helpPanel.setHelpText(helpText());
00420   m_helpPanel.Show(true);
00421 }
00422 
00424 void vuBasicUtility::onKeyboardKeyframer(wxKeyEvent& event)
00425 {
00426   vu1 *volume = getVolume();
00427 
00428   if (volume != NULL) {
00429     vuCamera *camera = volume->getCameraPtr();
00430 
00431     if (camera != NULL) {
00432       if (m_keyframer == NULL) {
00433         m_keyframer = new vuKeyFramerDialog(this, camera, this);
00434         m_keyframer->setup(camera, this);
00435       }
00436       m_keyframer->Show(true);
00437     }
00438   }
00439   else {
00440     cerr << "Warning! No camera defined. Can't start the keyframer." << endl;
00441   }
00442 }
00443 
00447 void vuBasicUtility::onKeyboardZoom(wxKeyEvent& event)
00448 {
00449   vu1 *volume = getVolume();
00450 
00451   if (volume != NULL) {
00452     vuCamera *camera = volume->getCameraPtr();
00453 
00454     if (camera) {
00455       float speed = (event.GetKeyCode() == '+') ?  10 : -10;
00456       camera->translateXYZ(0.0f, 0.0f, speed);
00457       glResize();
00458       m_glCanvas->redraw();
00459     }
00460   }
00461 }
00462 
00466 void vuBasicUtility::onKeyboardRotate(wxKeyEvent& event)
00467 {
00468   vu1 *volume = getVolume();
00469 
00470   if (volume) {
00471     vuCamera *camera = volume->getCameraPtr();
00472 
00473     if (camera) {
00474       vuVector t = camera->getPosition() - volume->getCenter();
00475       float d = t.norm();
00476       camera->translateXYZ(0.0f, 0.0f, d);
00477       t = camera->getPosition();
00478 
00479       float speed = (event.m_shiftDown) ? 0.2 : 2;
00480 
00481       switch (event.GetKeyCode()) {
00482       case 316:
00483         camera->rotateAboutUp(speed);
00484         break;
00485       case 317:
00486         camera->rotateAboutRight(speed);
00487         break;
00488       case 318:
00489         camera->rotateAboutUp(-speed);
00490         break;
00491       case 319:
00492         camera->rotateAboutRight(-speed);
00493         break;
00494       }
00495 
00496       volume->setIsReRendering();
00497       camera->translateXYZ(0.0f, 0.0f, -d);
00498       camera->init();
00499       m_glCanvas->redraw();
00500     }
00501   }  
00502 }
00503 
00504 /* ------------------------------------------------------------------------- */
00505 /* ----------------------- rendering handlers ------------------------------ */
00506 /* ------------------------------------------------------------------------- */
00507 
00508 void vuBasicUtility::glRender()
00509 {
00510   vu1 *volume = getVolume();
00511 
00512   if (volume == NULL) {
00513     cerr << "Warning: Can't draw preview. ";
00514     cerr << "Probably the utility's getVolume() was not overwritten. ";
00515     cerr << endl;
00516     onRender();
00517   }
00518   else if(m_DrawPreview) {
00519     volume->preview();
00520     SetStatusText(wxString("Press '?' for Help!"));
00521   }
00522   else {
00523     onRender();
00524     m_DrawPreview = true;
00525   }
00526 };
00527 
00528 void vuBasicUtility::onRender()
00529 {
00530   // this is the place for your rendering code. If you don't want to have
00531   // the default preview behaviour, overwrite glRender() instead of
00532   // onRender() -ms-
00533 }
00534 
00535 /* ------------------------------------------------------------------------- */
00536 /* -------------------------- help text ------------------------------------ */
00537 /* ------------------------------------------------------------------------- */
00538 
00539 wxString vuBasicUtility::helpText()
00540 {
00541   wxString str("");
00542 
00543   str += 
00544     "\n  Keyboard bindings:\n\n"
00545     "\t ?\t\t\t this help window\n"
00546     "\t k\t\t\t Keyframer\n"
00547     "\t +\t\t\t zoom in\n"
00548     "\t -\t\t\t zoom out\n"
00549     "\t enter\t\t\t render volume\n"
00550     "\t right\t\t\t rotate right\n"
00551     "\t left\t\t\t rotate left\n"
00552     "\t up\t\t rotate up\n"
00553     "\t down\t\t rotate down\n"
00554     "\t shift-right\t rotate slowly right\n"
00555     "\t shift-left\t\t rotate slowly left\n"
00556     "\t shift-up\t\t rotate slowly up\n"
00557     "\t shift-down\t rotate slowly down\n";
00558   return str;
00559 }
00560 
00561 /* ------------------------------------------------------------------------- */
00562 /* -------------------------- help text ------------------------------------ */
00563 /* ------------------------------------------------------------------------- */
00564 
00565 vu1 *vuBasicUtility::getVolume()
00566 {
00567   cerr << "Warning: the utility's getVolume() was not overwritten. " << endl;
00568   return NULL;
00569 }

Generated on Wed Dec 15 21:20:33 2004 for vuVolume by  doxygen 1.3.9.1