]> Creatis software - creaMaracasVisu.git/blob - lib/maracasVisuLib/src/interface/wxWindows/widgets/include/wxSurfaceSTLWidget.cxx
cf4d8589ce9b1897e5470736b306881442794c93
[creaMaracasVisu.git] / lib / maracasVisuLib / src / interface / wxWindows / widgets / include / wxSurfaceSTLWidget.cxx
1 /*=========================================================================
2
3   Program:   wxMaracas
4   Module:    $RCSfile: wxSurfaceSTLWidget.cxx,v $
5   Language:  C++
6   Date:      $Date: 2009/05/14 13:54:57 $
7   Version:   $Revision: 1.1 $
8
9   Copyright: (c) 2002, 2003
10   License:
11   
12      This software is distributed WITHOUT ANY WARRANTY; without even 
13      the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR 
14      PURPOSE.  See the above copyright notice for more information.
15
16 =========================================================================*/
17
18 #include "wxSurfaceSTLWidget.h"
19 #include <matrix.h>
20
21 #include <wx/dialog.h>
22 #include <wx/colordlg.h>
23
24 #ifndef __WXMSW__
25 #   include "../res/bitmaps/extract.xpm"
26 #   include "../res/bitmaps/del_axis.xpm"
27 #   include "../res/bitmaps/vessels.xpm"
28 #   include "../res/bitmaps/cutter.xpm"
29 #   include "../res/bitmaps/stl.xpm"
30 #endif
31
32 //----------------------------------------------------------------------------
33 BEGIN_EVENT_TABLE( wxSurfaceSTLWidget, wxPanel )
34
35     EVT_TOOL(ID_BUTTON_VESSELS_CONSTRUCTION, wxSurfaceSTLWidget::OnVesselConstruction )
36     EVT_TOOL(ID_BUTTON_3D_CUTTER, wxSurfaceSTLWidget::On3DCutter )
37     EVT_TOOL(ID_BUTTON_EXPORT_STL, wxSurfaceSTLWidget::OnExportAsSTL )
38
39     EVT_BUTTON(ID_BUTTON_CHOOSE_COLOR, wxSurfaceSTLWidget::OnChooseSurfaceColor )
40     EVT_CHECKBOX(ID_CHECKBOX_ISOVISIBLE, wxSurfaceSTLWidget::OnIsoVisible )
41     EVT_CHECKBOX(ID_CHECKBOX_STL_VISIBLE, wxSurfaceSTLWidget::OnSTLSurfaceVisible )
42
43     EVT_COMMAND_SCROLL(ID_SLIDER_OPACITY, wxSurfaceSTLWidget::OnSliderOpacityScrollThumbrelease)
44
45     EVT_COMMAND_SCROLL_THUMBRELEASE(ID_SLIDER_ISOVAL, wxSurfaceSTLWidget::OnSliderIsovalueScrollThumbrelease)
46     EVT_COMMAND_SCROLL_THUMBRELEASE(ID_SLIDER_STL_THRESH, wxSurfaceSTLWidget::OnSliderSTLSurfaceValueThumbrelease)
47
48     EVT_CHECKBOX(ID_CHECKBOX_INVERT_SLICE_ORDER, wxSurfaceSTLWidget::OnInvertSliceOrder )
49
50 END_EVENT_TABLE( );
51
52 //----------------------------------------------------------------------------
53 wxSurfaceSTLWidget::wxSurfaceSTLWidget(wxWindow* parent, wxWindowID id, const wxPoint& pos, const wxSize& size, long style,
54     const wxString& name) : wxPanel( parent, id, pos, size, style, name )
55 {
56     _mar = NULL;
57
58     _3DWorldSTL = new vtk3DSurfaceSTLWidget( this, ID_WORLD3D );
59
60     // Set up axis toolbar
61     wxBitmap axisToolBarBitmaps[ 2 ];
62     axisToolBarBitmaps[ 0 ] = wxBITMAP( axistoolbar_extract );
63     axisToolBarBitmaps[ 1 ] = wxBITMAP( axistoolbar_erase );
64
65
66     //By default the Iso value is visible.
67     _isoVisible = new wxCheckBox( this, ID_CHECKBOX_ISOVISIBLE, _T("Iso visible") );
68     _isoVisible->SetValue( true );
69     _isoValue = new wxSlider( this, ID_SLIDER_ISOVAL, 0, 0, 10000 , wxDefaultPosition, wxDefaultSize, wxSL_HORIZONTAL | wxSL_LABELS);
70     _opacity = new wxSlider( this, ID_SLIDER_OPACITY, 0, 0, 100 , wxDefaultPosition, wxDefaultSize, wxSL_HORIZONTAL | wxSL_LABELS);
71     _isoValue->SetLabel( _T("IsoValue") );
72     _opacity->SetLabel( _T("Surface opacity") );
73
74     //I can't get to brighten the button when mouse over:
75     //_surface_color = new wxButton(this, ID_BUTTON_CHOOSE_COLOR, "", wxPoint(0,0), wxSize(150,50), wxRAISED_BORDER);
76     //_surface_color->SetBackgroundColour( wxColour(250, 235, 214));
77     
78     // Set up vessels toolbar
79     wxBitmap vesselsToolBarBitmaps[ 3 ];
80     vesselsToolBarBitmaps[ 0 ] = wxBITMAP( vesselstoolbar_extract );
81     vesselsToolBarBitmaps[ 1 ] = wxBITMAP( vesselstoolbar_cutter );
82     vesselsToolBarBitmaps[ 2 ] = wxBITMAP( vesselstoolbar_stl );
83
84          _vessels_tb = new wxToolBar( this, -1);
85     _vessels_tb->SetMargins( 4, 4 );
86     _vessels_tb->SetToolBitmapSize( wxSize( vesselsToolBarBitmaps[ 0 ].GetWidth( ), vesselsToolBarBitmaps[ 0 ].GetHeight( ) ) );
87     _vessels_tb->AddTool(ID_BUTTON_VESSELS_CONSTRUCTION, _T("Construct Surface"), vesselsToolBarBitmaps[ 0 ],  _T("Construct Surface"));
88     _vessels_tb->AddSeparator( );
89     //_vessels_tb->AddTool(ID_BUTTON_3D_CUTTER, vesselsToolBarBitmaps[ 1 ], "Delete axis", "short help string");
90     _vessels_tb->AddCheckTool(ID_BUTTON_3D_CUTTER, _T("Delete axis"), vesselsToolBarBitmaps[ 1 ], wxNullBitmap, _T("Delete axis") ); //, "short help string"
91     _vessels_tb->AddSeparator( );
92     _vessels_tb->AddTool(ID_BUTTON_EXPORT_STL, _T("Export as STL"), vesselsToolBarBitmaps[ 2 ], _T("Export as STL"));
93     _vessels_tb->Realize( );
94     _vessels_tb->SetRows( 1 ); ///\todo : what that ? mat
95
96
97     //Set Properties:
98     _isoValue->SetSize(wxSize(64, 16));
99     //End set properties
100     
101     //Do the layout:
102     wxStaticBoxSizer* sizer_3 = new wxStaticBoxSizer(new wxStaticBox(this, -1, _T("Iso Surface values")), wxVERTICAL);
103     //Some borders are needed on wxGTK otherwise static text and slider labels collides.
104     sizer_3->Add(_isoVisible, 1, wxALL|wxEXPAND, 2);
105     sizer_3->Add(new wxStaticText(this, -1, _T("Isovalue")), 1, wxALL|wxEXPAND, 2);
106     sizer_3->Add(_isoValue, 1, wxALL|wxEXPAND, 2);
107     sizer_3->Add(new wxStaticText(this, -1, _T("Opacity (%)")), 1, wxALL|wxEXPAND, 2);
108     sizer_3->Add(_opacity, 1, wxALL|wxEXPAND, 2);
109
110     //By default the STL Iso value is visible.
111     _stlIntSurfaceVisible = new wxCheckBox( this, ID_CHECKBOX_STL_VISIBLE, _T("Internal STL Surface visible") );
112     _stlIntSurfaceVisible->SetValue( true );
113     _stlExtSurfaceVisible = new wxCheckBox( this, ID_CHECKBOX_STL_VISIBLE, _T("External STL Surface visible") );
114     _stlExtSurfaceVisible->SetValue( true );
115     _stlSurfaceValue = new wxSlider( this, ID_SLIDER_STL_THRESH, 0, 0, 100 , wxDefaultPosition, wxDefaultSize, wxSL_HORIZONTAL | wxSL_LABELS);
116     _stlSurfaceValue->SetLabel( _T("STL Surface Value") );
117     _stlSurfaceValue->SetRange(0, 100);
118     //hard coded value 'cause thie is still not reachable: (int)_3DWorldSTL->GetSTLThreshold()
119     _stlSurfaceValue->SetValue( 45 ); //100*.45 remember to multiply by 100
120
121
122     //Do the layout:
123     wxStaticBoxSizer* sizer_4 = new wxStaticBoxSizer(new wxStaticBox(this, -1, _T("STL Surface values")), wxVERTICAL);
124     sizer_4->Add(_stlIntSurfaceVisible, 1, wxALL|wxEXPAND, 2);
125     sizer_4->Add(_stlExtSurfaceVisible, 1, wxALL|wxEXPAND, 2);
126     sizer_4->Add(new wxStaticText(this, -1, _T("STL Isovalue")), 1, wxALL|wxEXPAND, 2);
127     sizer_4->Add(_stlSurfaceValue, 1, wxALL|wxEXPAND, 2);
128     
129     _stlIntSurfaceVisible->Disable();
130     _stlExtSurfaceVisible->Disable();
131     _stlSurfaceValue->Disable();
132
133     _chkInvertSliceOrder = new wxCheckBox(this, ID_CHECKBOX_INVERT_SLICE_ORDER, _T("Invert slice order")); 
134
135     wxBoxSizer* sizer_2 = new wxBoxSizer(wxVERTICAL);
136     sizer_2->Add(sizer_3, 1, wxEXPAND, 0);
137 // EED Borrame
138 //    sizer_2->Add(_axis_tb, 1, wxEXPAND|wxALL|wxALIGN_LEFT, 0);
139     sizer_2->Add(sizer_4, 1, wxEXPAND, 0);
140     sizer_2->Add(_vessels_tb, 1, wxEXPAND|wxALL|wxALIGN_LEFT, 0);
141     sizer_2->Add(_chkInvertSliceOrder, 1, wxEXPAND|wxALL|wxALIGN_LEFT, 0);
142
143     wxBoxSizer* sizer_1 = new wxBoxSizer(wxHORIZONTAL);
144     sizer_1->Add(_3DWorldSTL, 1, wxEXPAND, 0);
145     sizer_1->Add(sizer_2, 0, wxALL|wxALIGN_RIGHT, 5);
146
147     SetAutoLayout(true);
148     SetSizer(sizer_1);
149     Layout();
150
151 }
152
153 //----------------------------------------------------------------------------
154 wxSurfaceSTLWidget::~wxSurfaceSTLWidget( )
155 {
156         _3DWorldSTL->Delete();
157 }
158
159 //----------------------------------------------------------------------------
160 /*void wxSurfaceSTLWidget::ShowMARACASDataDebug( vtkImageData *ima )
161 {
162     _3DWorldSTL->ShowMARACASData2Debug( ima );
163        
164     //GUI stuff:
165         float minmax[2];
166         float min,max;
167         ima->GetScalarRange( minmax );
168         min = minmax[0];
169     max = minmax[1];
170     
171     
172      _isoValue->SetRange((int)min, (int)max);
173      _isoValue->SetValue( (int)(max/2) );
174
175 }*/
176 //----------------------------------------------------------------------------
177 void wxSurfaceSTLWidget::ShowMARACASData( marInterface* mar )
178 {
179 wxBusyCursor wait;
180
181
182 //    _width  = _mar->_experiment->getDynData( )->getVolume( )->getWidth( );
183 //    _height = _mar->_experiment->getDynData( )->getVolume( )->getHeight( );
184 //    _depth  = _mar->_experiment->getDynData( )->getVolume( )->getDepth( );
185     
186
187     double min, max; //backward compatibility
188
189     _mar = mar;
190     //_3DWorldSTL->_mar = mar;
191 //    _3DWorldSTL->ShowMARACASData( mar );
192     _3DWorldSTL->ShowMARACASDataAndAxe( mar );
193
194         
195     //GUI stuff:
196     _mar->_experiment->getDynData( )->getVolume( )->getMinMax( min, max );
197
198     _isoValue->SetRange((int)min, (int)max);
199     _isoValue->SetValue( (int)(max/4) );
200      
201     _opacity->SetValue( 50 );
202         _chkInvertSliceOrder->SetValue( _mar->_parameters->getBoolParam( marParameters::e_invest_slice_order ) );
203
204 }
205 //----------------------------------------------------------------------------
206 void wxSurfaceSTLWidget::OnChooseSurfaceColor(wxCommandEvent& WXUNUSED(event))
207 {
208     wxColourData data;
209     data.SetChooseFull( true );
210     for( int i = 0; i < 16; i++ )
211     {
212         wxColour colour( i * 16, i * 16, i * 16 );
213         data.SetCustomColour( i, colour );
214     } // rof
215
216     wxColourDialog dialog ( this, &data );
217     dialog.SetTitle( _T("choose color") );
218
219     if( dialog.ShowModal( ) == wxID_OK )
220     {
221         wxColourData retData = dialog.GetColourData( );
222         wxColour col = retData.GetColour( );
223       //_surface_color->SetBackgroundColour( col );
224
225       _3DWorldSTL->SetSurfaceColor(col.Red(), col.Green(), col.Blue() );
226
227     } // fi
228  
229 }
230
231
232 //----------------------------------------------------------------------------
233 void wxSurfaceSTLWidget::OnInvertSliceOrder(wxCommandEvent& WXUNUSED(event)){
234   wxMessageDialog *msgDlg = new wxMessageDialog(this, _T("You have to restart the application!"), _T("WARNING!!"), wxOK );  
235   msgDlg->ShowModal();
236   delete msgDlg;
237   _mar->_parameters->setBoolParam( marParameters::e_invest_slice_order, _chkInvertSliceOrder->GetValue( ) );
238   _mar->saveParameters( );
239 }
240 //----------------------------------------------------------------------------
241 void wxSurfaceSTLWidget::OnIsoVisible(wxCommandEvent& WXUNUSED(event))
242 {
243   _3DWorldSTL->SetSurfaceVisibility( _isoVisible->GetValue() );
244 }
245 //----------------------------------------------------------------------------
246 void wxSurfaceSTLWidget::OnSliderSTLSurfaceValueThumbrelease(wxScrollEvent& WXUNUSED(event))
247 {
248   wxBusyCursor wait;
249   double tmp=(double)_stlSurfaceValue->GetValue();
250   _3DWorldSTL->SetSTLThresholdRatio( tmp );
251 }
252 //----------------------------------------------------------------------------
253 void wxSurfaceSTLWidget::OnSTLSurfaceVisible(wxCommandEvent& WXUNUSED(event))
254 {
255   _3DWorldSTL->SetSTLSurfaceVisibility( _stlIntSurfaceVisible->GetValue(), _stlExtSurfaceVisible->GetValue() );
256 }
257 //----------------------------------------------------------------------------
258 void wxSurfaceSTLWidget::OnSliderIsovalueScrollThumbrelease(wxScrollEvent& event)
259 {
260   wxBusyCursor wait;
261   _3DWorldSTL->SetSurfaceIsoValue( event.GetPosition() );
262 }
263 //----------------------------------------------------------------------------
264 void wxSurfaceSTLWidget::OnSliderOpacityScrollThumbrelease(wxScrollEvent& event)
265 {
266   _3DWorldSTL->SetSurfaceOpacity( event.GetPosition() );
267 }
268
269 //----------------------------------------------------------------------------
270 void wxSurfaceSTLWidget::OnVesselConstruction(wxCommandEvent& WXUNUSED(event))
271 {
272         wxBusyCursor wait;
273   _3DWorldSTL->ConstructVessel( );
274   _vessels_tb->EnableTool(ID_BUTTON_3D_CUTTER, true);
275   _vessels_tb->EnableTool(ID_BUTTON_EXPORT_STL, true);
276
277   _stlIntSurfaceVisible->Enable();
278   _stlExtSurfaceVisible->Enable();
279   _stlSurfaceValue->Enable();
280   _vessels_tb->EnableTool(ID_BUTTON_VESSELS_CONSTRUCTION, false);
281 //  this->GetParent()->GetToolBar()->EnableTool(ID_TOOLBAR_IMAGE_BROWSER, false);
282 }
283 //----------------------------------------------------------------------------
284 void wxSurfaceSTLWidget::On3DCutter(wxCommandEvent& event)
285 {
286         wxBusyCursor wait;
287   _3DWorldSTL->SetCuttingMode( event.IsChecked() );
288 }
289
290 //----------------------------------------------------------------------------
291 void wxSurfaceSTLWidget::OnExportAsSTL(wxCommandEvent& WXUNUSED(event))
292 {
293         wxBusyCursor wait;
294   wxString dirSTL = _mar->_parameters->getStringParam( 
295     marParameters::e_installation_directory ); 
296   dirSTL = ( dirSTL == _T("NO_DIRECTORY") ) ? wxGetHomeDir( ) : dirSTL;
297   wxDirDialog dialog( this, _T("Choose a directory..."), ( !dirSTL.IsEmpty( ) )?
298   dirSTL: wxGetHomeDir( ) );
299
300   if( dialog.ShowModal( ) == wxID_OK ) 
301   {
302      wxString tmpSTR(dialog.GetPath( ) +  _T("/") + _mar->_dicom->GetPatientName() );
303       _3DWorldSTL->ExportSurfaceAsSTL( (const char*)(tmpSTR.mb_str())  );
304   }
305
306   //By default *always* update e_installation_directory:
307   _mar->_parameters->setStringParam( marParameters::e_installation_directory,
308     dialog.GetPath( ) ); 
309   _mar->saveParameters( );
310 }