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

vuRaycast.cpp

Go to the documentation of this file.
00001 #include "vuRaycast.h"
00002 
00003 #include <fstream.h>
00004 
00005 #include "vuColourRGBa.h"
00006 #include "vuMatrix.h"
00007 #include "vuArcBall.h"
00008 #include "vuPreviewWin.h"
00009 #include "vuCamera.h"
00010 
00011 #include "../../wxUIElements/vuTransferDialog.h"
00012 #include <wx/button.h>
00013 
00014 //----------------------------------------------------------------------------
00015 //------------------------- The vuRaycast event table --------------------------
00016 //----------------------------------------------------------------------------
00017 
00018 enum
00019 {
00020   idCANVAS,
00021   idRENDER,
00022   idPREDRAW,
00023   idDOSPEC,
00024   idSMPDIST,
00025   idTFUNC
00026 };
00027 
00028 BEGIN_EVENT_TABLE(vuRaycast, vuBasicUtility)
00029     EVT_BUTTON  (idRENDER, vuRaycast::OnButtonRender)
00030     EVT_CHECKBOX(idPREDRAW, vuRaycast::OnCheckBoxPreDraw)
00031     EVT_CHECKBOX(idDOSPEC, vuRaycast::OnCheckBoxDoSpec)
00032     EVT_TEXT_ENTER(idSMPDIST, vuRaycast::OnChangeSampDist)
00033     EVT_LISTBOX(idTFUNC, vuRaycast::OnSelectTFunc) 
00034 END_EVENT_TABLE();
00035 
00036 //----------------------------------------------------------------------------
00037 //------------------------- The constructor ----------------------------------
00038 //----------------------------------------------------------------------------
00039 
00040 vuRaycast::vuRaycast() : m_TFunc(4,256), m_TFuncDlg(this, m_TFunc)
00041 {
00042     m_Data = NULL;
00043 }
00044 
00045 //----------------------------------------------------------------------------
00046 //------------------------- The destructor -----------------------------------
00047 //----------------------------------------------------------------------------
00048 
00049 vuRaycast::~vuRaycast()
00050 {
00051     if (m_Data != 0) delete m_Data;
00052 }
00053 
00054 //----------------------------------------------------------------------------
00055 //------------------------- public: getFileType() ----------------------------
00056 //----------------------------------------------------------------------------
00057 
00058 const char* vuRaycast::getFileType()
00059 {
00060     return "11121";
00061 }
00062 
00063 //----------------------------------------------------------------------------
00064 //------------------------- public: init() -----------------------------------
00065 //----------------------------------------------------------------------------
00066 
00067 bool vuRaycast::init(const char* DataFile)
00068 {
00069     SetEvtHandlerEnabled(true);
00070 
00071     //Set up the window
00072     SetTitle("RGB Raycaster");
00073     CreateStatusBar();
00074 
00075 
00076     //Create a volume data instance.
00077     m_Data = new vu1112113;
00078     m_Data->setFileName(DataFile);
00079 
00080     //Set the transfer function for the data.
00081     m_TFunc.addOpacity(1,0.01);
00082     m_TFunc.addOpacity(11,0.01);
00083     m_TFunc.addOpacity(12,1);
00084     m_TFunc.addOpacity(32,1);
00085     m_TFunc.addColour(1,vuColourRGBa(0.f));
00086     m_TFunc.addColour(11,vuColourRGBa(1.f));
00087     m_TFunc.addColour(32,vuColourRGBa(0.f,0.f,0.6f));
00088     m_TFunc.setOpacitySmoothing(0);
00089     m_TFunc.setColourSmoothing(0);
00090 
00091     m_TFunc.generateFunction();
00092     m_Data->setTransferFunc(m_TFunc);
00093     m_Data->doPreviewDraw(true);
00094     m_Data->getCamera().translateXYZ(0.0f, 0.0f, 64);
00095 
00096     //Read in the data.
00097     bool success = m_Data->read();
00098     if (success)
00099     {
00100         m_glCanvas->SetSize(512,512);
00101         Fit();
00102         m_Preview->attachCamera(&m_Data->getCamera());
00103         m_Preview->setCubeSize(m_Data->getDim1Size(),m_Data->getDim2Size(),m_Data->getDim3Size());
00104     }
00105     else
00106     {
00107         wxMessageDialog dlg(this,m_Data->getErrorMessage(),"vuRaycastVR",wxOK);
00108         dlg.ShowModal();
00109     }
00110     return success;
00111 }
00112 
00113 vuCamera* vuRaycast::getCamera ()
00114 
00115 {
00116         return m_Data->getCameraPtr ();
00117 }
00118 
00119 vuImage* vuRaycast::getCurrentImage ()
00120 
00121 {
00122         return m_Data->getImage ();
00123 //      return NULL;
00124 }
00125 
00126 /*
00127 void vuRaycast::setCamera (vuCamera* cam)
00128 
00129 {
00130 
00131 }*/
00132 
00133 //----------------------------------------------------------------------------
00134 //------------------------- public: addRight() -------------------------------
00135 //----------------------------------------------------------------------------
00136 
00137 void vuRaycast::addRight(wxSizer *sizer)
00138 {
00139   //Add some control elements
00140   sizer->Add( new wxButton(this, idRENDER, "Render"),
00141                  0,           // make horizontally unstretchable
00142                  wxALL,       // make border all around (implicit top alignment)
00143                  10 );        // set border width to 10
00144   wxSize size(35,-1);
00145   m_TCsmpDist = new wxTextCtrl(this,idSMPDIST,"1.00", wxDefaultPosition,size,
00146                                wxTE_PROCESS_ENTER|wxTE_PROCESS_TAB,
00147                                wxTextValidator(wxFILTER_NUMERIC));
00148   m_CBdoSpec = new wxCheckBox(this,idDOSPEC,"do specular");
00149   m_CBdoSpec->SetValue(false);
00150   m_CBdrawPrev = new wxCheckBox(this,idPREDRAW,"draw preview");
00151   m_CBdrawPrev->SetValue(true);
00152   m_TFuncList = new wxListBox(this, idTFUNC, wxDefaultPosition,wxSize(150,70),0,
00153                               NULL,wxLB_SINGLE);
00154   m_TFuncList->Append("per sample");
00155   m_TFuncList->Append("per segment");
00156   m_TFuncList->Append("alpha weighted");
00157   
00158   sizer->Add( m_CBdrawPrev,0,wxALL|wxALIGN_LEFT,1);
00159   sizer->Add( m_CBdoSpec,0,wxALL|wxALIGN_LEFT,1);
00160   sizer->Add(new wxStaticText(this,-1,"sampling distance:"),0,wxALL|wxALIGN_LEFT,1);
00161   sizer->Add( m_TCsmpDist,0,wxALL|wxALIGN_LEFT,1);
00162   sizer->Add(new wxStaticText(this,-1,"transfer function"),0,wxALL|wxALIGN_LEFT,1);
00163   sizer->Add( m_TFuncList,0,wxALL|wxALIGN_LEFT,1);
00164   
00165   m_Preview = new vuPreviewWin(this,200,200);
00166   sizer->Add( m_Preview, 
00167               1,           // make horizontally stretchable
00168               wxALL,       // make border all around (implicit top alignment)
00169               10 );        // set border width to 10
00170 
00171 }
00172 
00173 void vuRaycast::DrawAgain()
00174 
00175 {
00176         m_Data->doRefresh();
00177         m_glCanvas->redraw();
00178 }
00179 
00180 void vuRaycast::DrawFromImage ()
00181 
00182 {
00183 //      m_Data->displayFromImage ();
00184 //      m_Data->render ();
00185         m_glCanvas->redraw();
00186 }
00187 
00188 //----------------------------------------------------------------------------
00189 //------------------------- public: OnButtonRender() -------------------------
00190 //----------------------------------------------------------------------------
00191 
00192 void vuRaycast::OnButtonRender( wxCommandEvent& event)
00193 {
00194   m_DrawPreview = false;
00195   m_Data->doRefresh();
00196   m_glCanvas->redraw();
00197 }
00198 
00199 #if wxMINOR_VERSION < 5
00200 void vuRaycast::OnCheckBoxPreDraw()
00201 #else
00202 void vuRaycast::OnCheckBoxPreDraw(wxCommandEvent&)
00203 #endif
00204 {
00205   m_Data->doPreviewDraw(m_CBdrawPrev->GetValue());
00206 }
00207 
00208 #if wxMINOR_VERSION < 5
00209 void vuRaycast::OnCheckBoxDoSpec()
00210 #else
00211 void vuRaycast::OnCheckBoxDoSpec(wxCommandEvent&)
00212 #endif
00213 {
00214   m_Data->doSpecular(m_CBdoSpec->GetValue());
00215 }
00216 
00217 #if wxMINOR_VERSION < 5
00218 void vuRaycast::OnChangeSampDist()
00219 #else
00220 void vuRaycast::OnChangeSampDist(wxCommandEvent&)
00221 #endif
00222 {
00223   double val;
00224   m_TCsmpDist->GetValue().ToDouble(&val);
00225   m_Data->setSamplingDistance(val);
00226   m_TCsmpDist->SetValue(wxString::Format("%.2f",val));
00227 }
00228 
00229 #if wxMINOR_VERSION < 5
00230 void vuRaycast::OnSelectTFunc()
00231 #else
00232 void vuRaycast::OnSelectTFunc(wxCommandEvent&)
00233 #endif
00234 {
00235     wxArrayInt sel;
00236     if(m_TFuncList->GetSelections(sel))
00237     {
00238         m_Data->setTFuncMethod(sel[0]);
00239     }
00240 }
00241 
00242 //----------------------------------------------------------------------------
00243 //------------------------- protected: glInit() ------------------------------
00244 //----------------------------------------------------------------------------
00245 
00246 bool vuRaycast::glInit(void)
00247 {
00248     if (m_Data == 0) return false;
00249   /*
00250     glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
00251 
00252     glEnable(GL_LIGHTING);
00253     glEnable(GL_LIGHT0);
00254     glLightModeli(GL_LIGHT_MODEL_TWO_SIDE, GL_TRUE);
00255   */
00256     glEnable(GL_DEPTH_TEST);
00257     glDepthFunc(GL_LESS);
00258     m_Data->initOpenGL();
00259     return true;
00260 };
00261 
00262 //----------------------------------------------------------------------------
00263 //------------------------- protected: onRender() ----------------------------
00264 //----------------------------------------------------------------------------
00265 
00266 void vuRaycast::onRender()
00267 {
00268   m_Data->setTransferFunc(m_TFunc);     // better once too often :-|
00269   glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
00270   glMatrixMode(GL_PROJECTION);
00271   glLoadIdentity();
00272   glOrtho(0,m_Data->getCamera().getWidth(),
00273           0,m_Data->getCamera().getHeight(), -1, 1);
00274   glMatrixMode(GL_MODELVIEW);
00275   glLoadIdentity();
00276 
00277   wxStopWatch watch;
00278   watch.Start();
00279   m_Data->render();
00280   m_Data->displayFromImage();
00281   watch.Pause();
00282   SetStatusText(wxString("Render Time: ") + vuString(watch.Time()).c_str() + "ms");
00283 };
00284 
00285 //----------------------------------------------------------------------------
00286 //------------------------- protected: glResize() ----------------------------
00287 //----------------------------------------------------------------------------
00288 
00289 void vuRaycast::glResize()
00290 {
00291     glViewport(0, 0, (GLint)m_glCanvas->getWidth(),(GLint)m_glCanvas->getHeight());
00292     m_Data->setImageSize(m_glCanvas->getWidth(),m_glCanvas->getHeight());
00293   
00294         //Set the viewport.
00295         //glViewport(0, m_glCanvas->getHeight()-m_Data->getCamera().getHeight(), 
00296         //                 (GLint)m_Data->getCamera().getWidth(), (GLint)m_Data->getCamera().getHeight());
00297 
00298     //Set the viewport.
00299     
00300 /*
00301     //Find the largest dimension of the data.
00302     dword max = m_Data->getDim1Size();
00303     if (m_Data->getDim2Size() > max)
00304         max = m_Data->getDim2Size();
00305     if (m_Data->getDim3Size() > max)
00306         max = m_Data->getDim3Size();
00307 
00308     //Set the opengl projection matrix.
00309     glMatrixMode(GL_PROJECTION);
00310     glLoadIdentity();
00311     glOrtho((float)max/(-1.0f*m_ViewScale), (float)max/m_ViewScale,
00312             (float)max/(-1.0f*m_ViewScale), (float)max/m_ViewScale,
00313             10000.0f, -10000.0f);
00314     glMatrixMode(GL_MODELVIEW);
00315     glLoadIdentity();
00316     
00317     //Set the opengl light info.
00318     float lpos[4] = {0.0, 0.0, 1024.0, 1.0};m_Data->m_Camera
00319     glLightfv(GL_LIGHT0, GL_POSITION, lpos);
00320   */
00321 }
00322 
00323 //----------------------------------------------------------------------------
00324 //------------------------- protected: onMouse() ---------------------------
00325 //----------------------------------------------------------------------------
00326 
00327 void vuRaycast::onMouse(wxMouseEvent &ev)
00328 {
00329   // here is the place for additional mouse bevaviour.
00330   if(!m_Data) return;
00331 
00332   if (ev.LeftDClick()) {
00333     m_TFuncDlg.Show(true);
00334   }
00335 }
00336 
00337 
00338 //--------------------------------------------------------------------------------------
00340 void vuRaycast::onKeyboard(wxKeyEvent& event)
00341 {
00342   static char *filename = "viewpoint.def";
00343 
00344   switch(event.GetKeyCode()) {
00345   case 'v' :
00346     {
00347       //load viewpoint definition
00348       ifstream file(filename);
00349       if(file) {
00350         vuCamera& cam = m_Data->getCamera();
00351         vuVector v;
00352         file>>v[0]; file>>v[1]; file>>v[2];
00353         cam.setPosition(v);
00354         file>>v[0]; file>>v[1]; file>>v[2];
00355         cam.setLookAtVector(v);
00356         file>>v[0]; file>>v[1]; file>>v[2];
00357         cam.setUpVector(v);
00358         cout<< "read "<<filename<<endl;
00359       }
00360     }
00361     break;
00362   case 'V' :
00363     {
00364       //save viewpoint definition
00365       ofstream file(filename);
00366       if(file) {
00367         vuCamera& cam = m_Data->getCamera();
00368         vuVector v = cam.getPosition();
00369         file<<v[0]<<' '; file<<v[1]<<' '; file<<v[2]<<endl;
00370         v = cam.getLookAtVector();
00371         file<<v[0]<<' '; file<<v[1]<<' '; file<<v[2]<<endl;
00372         v = cam.getUpVector();
00373         file<<v[0]<<' '; file<<v[1]<<' '; file<<v[2]<<endl;
00374         cout<< "wrote "<<filename<<endl;
00375       }
00376     }
00377     break;
00378   }
00379 }
00380 
00381 vu1 *vuRaycast::getVolume()
00382 {
00383   return (vu1 *)m_Data;
00384 }

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