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

vuBCCSheetSplatter.cpp

Go to the documentation of this file.
00001 #include "vuBCCSheetSplatter.h"
00002 #include <math.h>
00003 
00004 #include "vuMatrix.h"
00005 #include "vuArcBall.h"
00006 #include "vuPreviewWin.h"
00007 
00008 #include "../../wxUIElements/vuTransferDialog.h"
00009 #include <wx/button.h>
00010 
00011 //----------------------------------------------------------------------------
00012 //------------------------- The vuSpectral event table --------------------------
00013 //----------------------------------------------------------------------------
00014 
00015 enum
00016 {
00017   idCANVAS,
00018   idRENDER,
00019   idCHANGE_SLICEW,
00020   idCHANGE_SPLATS
00021 };
00022 
00023 BEGIN_EVENT_TABLE(vuBCCSheetSplatter, vuBasicUtility)
00024   EVT_BUTTON  (idRENDER, vuBCCSheetSplatter::OnButtonRender)
00025   EVT_TEXT_ENTER(idCHANGE_SLICEW, vuBCCSheetSplatter::OnChangeSliceWidth)
00026   EVT_TEXT_ENTER(idCHANGE_SPLATS, vuBCCSheetSplatter::OnChangeSplatSize)
00027 END_EVENT_TABLE()
00028 
00029 //----------------------------------------------------------------------------
00030 //------------------------- The constructor ----------------------------------
00031 //----------------------------------------------------------------------------
00032 
00033 vuBCCSheetSplatter::vuBCCSheetSplatter()
00034 {
00035     m_Data = NULL;
00036     m_DrawBBox = false;
00037 }
00038 
00039 //----------------------------------------------------------------------------
00040 //------------------------- The destructor -----------------------------------
00041 //----------------------------------------------------------------------------
00042 
00043 vuBCCSheetSplatter::~vuBCCSheetSplatter()
00044 {
00045     if (m_Data != 0) delete m_Data;
00046 }
00047 
00048 //----------------------------------------------------------------------------
00049 //------------------------- public: getFileType() ----------------------------
00050 //----------------------------------------------------------------------------
00051 
00052 const char* vuBCCSheetSplatter::getFileType()
00053 {
00054     return "15121";
00055 }
00056 
00057 //----------------------------------------------------------------------------
00058 //------------------------- public: init() -----------------------------------
00059 //----------------------------------------------------------------------------
00060 
00061 bool vuBCCSheetSplatter::init(const char* DataFile)
00062 {
00063     //Set up the window
00064     SetTitle("sheet-based BCC grid splatting");
00065     CreateStatusBar();
00066 
00067     //Create a volume data instance.
00068     m_Data = new vu1512112;
00069     m_Data->setFileName(DataFile);
00070 
00071     /*
00072     //Set the transfer function for the data.
00073     m_TFunc.addOpacity(0,0.00);
00074     m_TFunc.addOpacity(1,0.00);
00075     m_TFunc.addColour(1,0,0,0);
00076     
00077     //m_TFunc.addOpacity(50,.3);
00078     //m_TFunc.addColour(50,1,1,1);
00079     //m_TFunc.addOpacity(99,.3);
00080     //m_TFunc.addColour(99,1,1,1);
00081     
00082     m_TFunc.addOpacity(199,0.00);
00083     m_TFunc.addOpacity(200,1);
00084     m_TFunc.addColour(200,1,0,0);
00085     m_TFunc.addOpacity(255,1);
00086     m_TFunc.addColour(255,1,0,0);
00087     */
00088 
00089     //Set the transfer function for the data.
00090     m_TFunc.addOpacity(0,0.00);
00091     m_TFunc.addOpacity(1,0.00);
00092     m_TFunc.addColour(1,vuColourRGBa(0.0f));
00093 
00094     m_TFunc.addOpacity(4,0.06);
00095     m_TFunc.addOpacity(11,0.01);
00096     m_TFunc.addOpacity(12,1);
00097     m_TFunc.addOpacity(32,1);
00098     m_TFunc.addColour(1,vuColourRGBa(0.0f));
00099     m_TFunc.addColour(11,vuColourRGBa(1.0f));
00100     m_TFunc.addColour(32,vuColourRGBa(1.f,0.f,0.f,1.f));
00101  
00102     m_TFunc.addOpacity(255,1);
00103     m_TFunc.addColour(255,vuColourRGBa(1,0,0));
00104 
00105     m_TFunc.setOpacitySmoothing(0);
00106     m_TFunc.setColourSmoothing(0);
00107     m_TFunc.generateFunction();
00108     m_Data->setTransferFunc(m_TFunc);
00109 
00110     //Read in the data.
00111     bool success = m_Data->read();
00112     if (success)
00113     {
00114         m_glCanvas->SetSize(512,512);
00115         Fit();
00116         //m_Preview->attachCamera(&m_Data->getCamera());
00117         //m_Preview->setCubeSize(m_Data->getDim1Size(),m_Data->getDim2Size(),m_Data->getDim3Size());
00118     }
00119     else
00120     {
00121         wxMessageDialog dlg(this,m_Data->getErrorMessage(),"vuBCCSheetSplatterVR",wxOK);
00122         dlg.ShowModal();
00123     }
00124 
00125     return success;
00126 }
00127 
00128 void vuBCCSheetSplatter::DrawAgain ()
00129 {
00130 }
00131 
00132 void vuBCCSheetSplatter::DrawFromImage ()
00133 {
00134 }
00135 
00136 vuImage* vuBCCSheetSplatter::getCurrentImage ()
00137 
00138 {
00139         return NULL;
00140 }
00141 
00142 vuCamera* vuBCCSheetSplatter::getCamera ()
00143 
00144 {
00145         return NULL;
00146 }
00147 
00148 //----------------------------------------------------------------------------
00149 //------------------------- public: addRight() -------------------------------
00150 //----------------------------------------------------------------------------
00151 
00152 void vuBCCSheetSplatter::addRight(wxSizer *sizer)
00153 {
00154   //Add some control elements
00155   sizer->Add( new wxButton(this, idRENDER, "Render"),
00156                  0,           // make horizontally unstretchable
00157                  wxALL,       // make border all around (implicit top alignment)
00158                  10 );        // set border width to 10
00159   wxSize size(35,-1);
00160   sizer->Add(new wxStaticText(this,-1,"Splat radius:"),0,wxALL|wxALIGN_LEFT,1);
00161   m_Splats = new wxTextCtrl(this,idCHANGE_SPLATS,"", wxDefaultPosition,size,
00162                             wxTE_PROCESS_ENTER|wxTE_PROCESS_TAB,wxTextValidator(wxFILTER_NUMERIC));
00163   sizer->Add( m_Splats,1,wxALL|wxEXPAND,1 );
00164   sizer->Add(new wxStaticText(this,-1,"Sheet width:"),0,wxALL|wxALIGN_LEFT,1);
00165   m_Slicew = new wxTextCtrl(this,idCHANGE_SLICEW,"", wxDefaultPosition,wxDefaultSize,
00166                             wxTE_PROCESS_ENTER|wxTE_PROCESS_TAB,wxTextValidator(wxFILTER_NUMERIC));
00167   sizer->Add( m_Slicew,1,wxALL|wxEXPAND,1 );
00168   //m_Splats->SetValue(wxString::Format("%.3f",m_Data->getFootprintSize()));
00169   m_Splats->Enable(true);
00170   //m_Slicew->SetValue(wxString::Format("%.3f",m_Data->getSliceWidth()));
00171   m_Slicew->Enable(true);
00172   //m_Preview = new vuPreviewWin(this,200,200);
00173   /*  sizer->Add( m_Preview, 
00174               1,           // make horizontally stretchable
00175               wxALL,       // make border all around (implicit top alignment)
00176               10 );        // set border width to 10
00177   */
00178 }
00179 
00180 //----------------------------------------------------------------------------
00181 //------------------------- public: OnButtonRender() -------------------------
00182 //----------------------------------------------------------------------------
00183 
00184 void vuBCCSheetSplatter::OnButtonRender( wxCommandEvent& event)
00185 {
00186   m_Data->doRefresh();
00187   m_glCanvas->redraw();
00188 }
00189 
00190 
00191 //----------------------------------------------------------------------------
00192 //------------------------- protected: glInit() ------------------------------
00193 //----------------------------------------------------------------------------
00194 
00195 bool vuBCCSheetSplatter::glInit(void)
00196 {
00197     if (m_Data == 0) return false;
00198   /*
00199     glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
00200 
00201     glEnable(GL_LIGHTING);
00202     glEnable(GL_LIGHT0);
00203     glLightModeli(GL_LIGHT_MODEL_TWO_SIDE, GL_TRUE);
00204   */
00205     glEnable(GL_DEPTH_TEST);
00206     glDepthFunc(GL_LESS);
00207     m_Data->initOpenGL();
00208     
00209     useOpenGL(false);
00210 
00211     return true;
00212 };
00213 
00214 //----------------------------------------------------------------------------
00215 //------------------------- protected: glRender() ----------------------------
00216 //----------------------------------------------------------------------------
00217 
00218 void vuBCCSheetSplatter::glRender()
00219 {
00220     if( m_DrawBBox)
00221       {
00222         static int c = 0;
00223         if(c++<10) return;
00224 
00225         //Set the viewport.
00226         glViewport(0, int (m_glCanvas->getHeight() - m_Data->getCamera().getHeight()), (GLint)m_Data->getCamera().getWidth(), (GLint)m_Data->getCamera().getHeight());
00227 
00228         glEnable(GL_DEPTH_TEST);
00229         glDepthFunc(GL_LESS);
00230         //glEnable(GL_CULL_FACE);
00231         //glCullFace(GL_BACK);                  //what a shit
00232         //glClear(GL_COLOR_BUFFER_BIT);
00233         glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
00234 
00235         //Set the opengl projection matrix.
00236         glMatrixMode(GL_PROJECTION);
00237         glLoadIdentity();
00238         m_Data->getCamera().glInit();
00239         glMatrixMode(GL_MODELVIEW);
00240         glLoadIdentity();
00241         m_Data->getCamera().gluLookAt();
00242         //vuMatrix cam = m_Data->getCamera().getViewMat();
00243         //glTranslatef(m_Data->center[0],m_Data->center[1],m_Data->center[2]);
00244         //glMultMatrixf(cam.getData());
00245         //glTranslatef(-m_Data->center[0],-m_Data->center[1],-m_Data->center[2]);
00246         //draw an axis aligned box
00247         vuVector dsize = m_Data->getCenter();
00248         dsize*=2;
00249         drawBox(dsize[0],dsize[1],dsize[2]);
00250       } else
00251       {
00252         wxStopWatch watch;
00253         watch.Start();
00254 
00255         m_Data->render();
00256 
00257         watch.Pause();
00258         SetStatusText(wxString("Render Time: ") + vuString(watch.Time()).c_str() + "ms");
00259       }
00260 };
00261 
00262 #if wxMINOR_VERSION < 5
00263 void vuBCCSheetSplatter::OnChangeSliceWidth()
00264 #else
00265 void vuBCCSheetSplatter::OnChangeSliceWidth(wxCommandEvent&)
00266 #endif
00267 {
00268   double val;
00269   m_Slicew->GetValue().ToDouble(&val);
00270   m_Data->setSliceWidth(val);
00271   m_Slicew->SetValue(wxString::Format("%.2f",m_Data->getSliceWidth()));
00272 }
00273 
00274 #if wxMINOR_VERSION < 5
00275 void vuBCCSheetSplatter::OnChangeSplatSize()
00276 #else
00277 void vuBCCSheetSplatter::OnChangeSplatSize(wxCommandEvent&)
00278 #endif
00279 {
00280   double val;
00281   m_Splats->GetValue().ToDouble(&val);
00282   m_Data->setFootprintSize(val);
00283   m_Splats->SetValue(wxString::Format("%.2f",m_Data->getFootprintSize()));
00284 }
00285 
00286 //----------------------------------------------------------------------------
00287 //------------------------- protected: glResize() ----------------------------
00288 //----------------------------------------------------------------------------
00289 
00290 void vuBCCSheetSplatter::glResize()
00291 {
00292   //Find the largest dimension of the data.
00293   dword max = m_Data->getDim1Size();
00294   if (m_Data->getDim2Size() > max)
00295     max = m_Data->getDim2Size();
00296   if (m_Data->getDim3Size() > max)
00297     max = m_Data->getDim3Size();
00298   vuParallelCamera *camera = (vuParallelCamera *)m_Data->getCameraPtr();
00299 
00300   float scale = camera->getXRange()/m_glCanvas->getWidth();
00301   //camera->setXRange(scale*m_glCanvas->getWidth());
00302   camera->setYRange(scale*m_glCanvas->getHeight());
00303 
00304   m_Data->setImageSize(m_glCanvas->getWidth(),m_glCanvas->getHeight());
00305   /*
00306     //Set the viewport.
00307     glViewport(0, 0, (GLint)m_glCanvas->getWidth(),(GLint)m_glCanvas->getHeight());
00308 
00309     //Find the largest dimension of the data.
00310     dword max = m_Data->getDim1Size();
00311     if (m_Data->getDim2Size() > max)
00312         max = m_Data->getDim2Size();
00313     if (m_Data->getDim3Size() > max)
00314         max = m_Data->getDim3Size();
00315 
00316     //Set the opengl projection matrix.
00317     glMatrixMode(GL_PROJECTION);
00318     glLoadIdentity();
00319     glOrtho((float)max/(-1.0f*m_ViewScale), (float)max/m_ViewScale,
00320             (float)max/(-1.0f*m_ViewScale), (float)max/m_ViewScale,
00321             10000.0f, -10000.0f);
00322     glMatrixMode(GL_MODELVIEW);
00323     glLoadIdentity();
00324 
00325     //Set the opengl light info.
00326     float lpos[4] = {0.0, 0.0, 1024.0, 1.0};m_Data->m_Camera
00327     glLightfv(GL_LIGHT0, GL_POSITION, lpos);
00328   */
00329 }
00330 
00331 //----------------------------------------------------------------------------
00332 //------------------------- protected: glOnMouse() ---------------------------
00333 //----------------------------------------------------------------------------
00334 
00335 void vuBCCSheetSplatter::glOnMouse(wxMouseEvent &ev)
00336 {
00337     if(!m_Data) return;
00338     if (ev.LeftDown() || ev.RightDown())
00339     {
00340         //Store the click position.
00341         m_x = (int) ev.GetX();
00342         m_y = (int) ev.GetY();
00343     }
00344     else if (ev.LeftIsDown() && ev.Moving())
00345     {
00346         useOpenGL(true);
00347         m_DrawBBox = true;
00348 
00349         //Rotate the volume
00350         vuParallelCamera *camera = (vuParallelCamera *)m_Data->getCameraPtr();
00351 
00352         vuVector t = camera->getPosition()-m_Data->getCenter();
00353         float d = t.norm();
00354         camera->translateXYZ(0.0f, 0.0f, d);
00355         t = camera->getPosition();
00356         //use the arc ball
00357         vuArcBall ball;
00358         ball.attachCamera(m_Data->getCamera());
00359         ball.setWinSize(int (camera->getWidth()), int (camera->getHeight()));
00360         ball.turn(m_x, m_y, ev.GetX(), ev.GetY());
00361         //camera->rotateAboutUp(ev.GetX() - m_x);
00362         //camera->rotateAboutRight(ev.GetY() - m_y);
00363         camera->translateXYZ(0.0f, 0.0f, -d);
00364         camera->init();
00365         m_glCanvas->redraw();
00366 
00367         //Store the click position.
00368         m_x = (int) ev.GetX();
00369         m_y = (int) ev.GetY();
00370     }
00371     else if (ev.RightIsDown() && ev.Moving())
00372     {
00373       useOpenGL(true);
00374       m_DrawBBox = true;
00375 
00376         //distance from the volume.
00377         float s = ((float)ev.GetY() - m_y)/30.0f;
00378         if(fabs(s)>0.9) s=0;
00379         //float d = t.norm();
00380         vuParallelCamera *camera = (vuParallelCamera *)m_Data->getCameraPtr();
00381 
00382         camera->setXRange(camera->getXRange()*(1-s));
00383         camera->setYRange(camera->getYRange()*(1-s));
00384         //cout<<m_Data->getCamera().getXRange()<<endl;
00385         //glResize();
00386         m_glCanvas->redraw();
00387 
00388         //Store the click position.
00389         m_x = (int) ev.GetX();
00390         m_y = (int) ev.GetY();
00391     }
00392     else if (ev.LeftDClick())
00393     {
00394         //Pop up the transfer function editor
00395         vuTransferDialog dlg(this,m_TFunc);
00396 
00397         if (dlg.ShowModal() == wxID_OK)
00398         {
00399             m_TFunc = dlg.getTransferFunc();
00400             m_Data->setTransferFunc(m_TFunc);
00401             m_glCanvas->redraw();
00402         }
00403     } else 
00404       {
00405         if(m_DrawBBox)
00406           {
00407             useOpenGL(false);
00408             m_DrawBBox = false;
00409             glResize();
00410             m_glCanvas->redraw();
00411           }
00412       }
00413 }
00414 
00415 void vuBCCSheetSplatter::drawBox(float sx, float sy, float sz)
00416 {
00417   static GLfloat n[6][3] = {  /* Normals for the 6 faces of a cube. */
00418     {-1.0, 0.0, 0.0}, {0.0, 1.0, 0.0}, {1.0, 0.0, 0.0},
00419     {0.0, -1.0, 0.0}, {0.0, 0.0, -1.0}, {0.0, 0.0, 1.0} };
00420   static GLint faces[6][4] = {  /* Vertex indices for the 6 faces of a cube. */
00421     {0, 1, 2, 3}, {3, 2, 6, 7}, {7, 6, 5, 4},
00422     {4, 5, 1, 0}, {5, 6, 2, 1}, {7, 4, 0, 3} };
00423   static GLfloat v[8][3];  /* Will be filled in with X,Y,Z vertexes. */
00424   
00425   /* Setup cube vertex data. */
00426   v[0][0] = v[1][0] = v[2][0] = v[3][0] = 0;
00427   v[4][0] = v[5][0] = v[6][0] = v[7][0] = sx;
00428   v[0][1] = v[1][1] = v[4][1] = v[5][1] = 0;
00429   v[2][1] = v[3][1] = v[6][1] = v[7][1] = sy;
00430   v[0][2] = v[3][2] = v[4][2] = v[7][2] = sz;
00431   v[1][2] = v[2][2] = v[5][2] = v[6][2] = 0;
00432   
00433   int i;
00434   for (i = 0; i < 6; i++) {
00435     glBegin(GL_QUADS);
00436     glNormal3fv(&n[i][0]);
00437     glColor3fv(v[faces[i][0]]);
00438     glVertex3fv(&v[faces[i][0]][0]);
00439     glColor3fv(v[faces[i][1]]);
00440     glVertex3fv(&v[faces[i][1]][0]);
00441     glColor3fv(v[faces[i][2]]);
00442     glVertex3fv(&v[faces[i][2]][0]);
00443     glColor3fv(v[faces[i][3]]);
00444     glVertex3fv(&v[faces[i][3]][0]);
00445     glEnd();
00446   }
00447 }

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