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