]> Creatis software - creaMaracasVisu.git/blob - lib/maracasVisuLib/src/kernel/marParameters.cpp
avoid warnings / fix typo / reindent
[creaMaracasVisu.git] / lib / maracasVisuLib / src / kernel / marParameters.cpp
1 /*=========================================================================
2
3  Program:   wxMaracas
4  Module:    $RCSfile: marParameters.cpp,v $
5  Language:  C++
6  Date:      $Date: 2010/04/20 14:42:44 $
7  Version:   $Revision: 1.3 $
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 "marParameters.h"
19 #include <math.h>
20 #include <wx/log.h>
21
22 static const char* BoolParamNames[] = {
23         // PS ->     "e_debug_window_visible"  ,
24         // PS ->        "e_debug_console_visible"     ,
25         // PS ->        "e_calculate_mip_default"    ,
26         // PS ->        "e_choose_all_slices_default" ,
27         "e_debug_isocontour"          ,
28         "e_debug_diameters"           ,
29         "e_debug_fill_area"           ,
30         "e_invest_slice_order"           ,
31         // PS ->        "e_show_cell"                 ,
32         // PS ->        "e_show_int_cell"
33 };
34
35 static const char* DoubleParamNames[] = {
36     "e_gravity_distance"              ,
37         "e_axis_discret_step"             ,
38         "e_distance_to_maximum_intensity" ,
39         "e_cell_coeficient"               ,
40         "e_flexion_coeficient"            ,
41         "e_tension_coeficient"            ,
42         "e_voxel_x_dimension"            ,
43         "e_voxel_y_dimension"             ,
44         "e_voxel_z_dimension"             ,
45         "e_scale"                         ,
46         // PS ->        "e_sampling"                      ,
47         "e_threshold_isocontour"          ,
48         "e_threshold_snake_isocontour"    ,
49         "e_sigma"                         ,
50         // PS ->        "e_gradient_coeficient"           ,
51         // PS ->        "e_extern_coeficient"             ,
52         // PS ->        "e_ballon_coeficient"             ,
53         // PS ->        "e_back_color_3D_r"               ,
54         // PS ->        "e_back_color_3D_g"               ,
55         // PS ->        "e_back_color_3D_b"               ,
56         // PS ->        "e_axis_color_r"                  ,
57         // PS ->        "e_axis_color_g"                  ,
58         // PS ->        "e_axis_color_b"
59     "e_RescaleIntercept"          ,
60     "e_RescaleSlope"              ,
61 };
62
63 static const char* IntParamNames[] = {
64     "e_roi_dimension"                     ,
65         "e_step"                                  ,
66         "e_step_coeficient"                       ,
67         "e_mass_power"                            ,
68         "e_algorithm_type"                        ,
69         "e_mask_size"                             ,
70         "e_number_of_points_for_snake"            ,
71         "e_quantification_type"                   ,
72         "e_stenosis_type"                         ,
73         // PS ->        "e_number_of_iterations_snake_isocontour" ,
74         // PS ->        "e_number_of_iterations_snake_deriche"    ,
75         "e_voxel_type"                            ,
76         // PS ->        "e_coef_cell_max_int"
77 };
78
79 static const char* StringParamNames[] = {
80         "e_installation_directory" ,
81         "e_dicom_images_directory"
82 };
83
84 // -------------------------------------------------------------------------
85 double marParameters::getVoxelSize( )
86 {
87 /* OLD CODE:
88
89  double ret = 1.0;
90  
91   if( _intValues[ e_voxel_type ] == VOX_TYPE_MINIMUM ) {
92   
93    ret = _doubleValues[ e_voxel_x_dimension ];
94    ret = GTM_MIN( _doubleValues[ e_voxel_y_dimension ], ret );
95    ret = GTM_MIN( _doubleValues[ e_voxel_z_dimension ], ret );
96    
97     } // fi
98     return( ret );
99         */
100
101
102 //EEDx1 
103     double ret = 1.0;
104     //Juan Carrillo 03/06/2005
105         if( _intValues[ e_voxel_type ] == VOX_TYPE_MINIMUM ) {
106                 
107                 ret = _doubleValues[ e_voxel_x_dimension ];
108                 ret = ( _doubleValues[ e_voxel_y_dimension ] < ret )?
109                         _doubleValues[ e_voxel_y_dimension ]: ret;
110                 ret = ( _doubleValues[ e_voxel_z_dimension ] < ret )?
111                         _doubleValues[ e_voxel_z_dimension ]: ret;
112     } // fi
113     return( ret*1 );
114 }
115
116 // -------------------------------------------------------------------------
117 double marParameters::getDimIma( )
118 {
119         double scale = _doubleValues[ e_scale ];
120         double roiDim = ( double )( _intValues[ e_roi_dimension ] );
121     return( scale * roiDim );
122 }
123
124 // -------------------------------------------------------------------------
125 int marParameters::getSizeIma( )
126 {
127     //  TODO : There should be a formal way to calculate a good image size.
128     return( 128 ); 
129 }
130
131 // -------------------------------------------------------------------------
132 double marParameters::getImaRatio( )
133 {
134     return( getDimIma( ) / ( double )( getSizeIma( ) ) );
135 }
136
137 // -------------------------------------------------------------------------
138 void marParameters::setROIStep( double width )
139 {
140     _intValues[ e_roi_dimension ] =
141                 ( int )ceil( _doubleValues[ e_cell_coeficient ] * width );
142     _intValues[ e_step ] =
143                 ( int )floor( ( double )_intValues[ e_roi_dimension ] /
144                       ( double )_intValues[ e_step_coeficient ] );
145 }
146
147
148 // -------------------------------------------------------------------------
149 float marParameters::getRescaleSlope(){
150         return (float) _doubleValues[ e_RescaleSlope ];
151 }
152 // -------------------------------------------------------------------------
153 float marParameters::getRescaleIntercept(){
154         return (float) _doubleValues[ e_RescaleIntercept ];
155 }
156 // -------------------------------------------------------------------------
157 void marParameters::setRescaleSlope(float slope){
158         _doubleValues[ e_RescaleSlope ]= slope;
159 }
160 // -------------------------------------------------------------------------
161 void marParameters::setRescaleIntercept(float intercept){
162         _doubleValues[ e_RescaleIntercept ]= intercept;
163 }
164 // -------------------------------------------------------------------------
165 void marParameters::copyFrom( const marParameters& from )
166 {
167     int i;
168         
169     memcpy( _boolValues, from._boolValues, e_bool_cont * sizeof( bool ) );
170     memcpy( _intValues, from._intValues, e_int_cont * sizeof( int ) );
171     memcpy( _doubleValues,
172                 from._doubleValues,
173                 e_double_cont * sizeof( double ) );
174         
175      for( i = 0; i < e_string_cont; i++ )
176                 _stringValues[ i ] = from._stringValues[ i ];
177 }
178
179 // -------------------------------------------------------------------------
180 void marParameters::reset( )
181 {
182         // PS ->     _boolValues[ e_debug_window_visible ]      = false;
183         // PS ->     _boolValues[ e_debug_console_visible ]     = false;
184         // PS ->     _boolValues[ e_calculate_mip_default ]     = false;
185         // PS ->     _boolValues[ e_choose_all_slices_default ] = true;
186     _boolValues[ e_debug_isocontour ]          = true;
187     _boolValues[ e_debug_diameters ]           = true;
188     _boolValues[ e_debug_fill_area ]           = false;
189     _boolValues[ e_invest_slice_order ]            = false;
190         // PS ->     _boolValues[ e_show_cell ]                 = true;
191         // PS ->     _boolValues[ e_show_int_cell ]             = true;
192         
193     _doubleValues[ e_gravity_distance ]              = 1.0;
194     _doubleValues[ e_axis_discret_step ]             = 2.0;
195     _doubleValues[ e_distance_to_maximum_intensity ] = 0.0;
196     _doubleValues[ e_cell_coeficient ]               = 1.2;
197     _doubleValues[ e_flexion_coeficient ]            = 0.1;
198     _doubleValues[ e_tension_coeficient ]            = 0.3;
199     _doubleValues[ e_voxel_x_dimension ]             = 1.0;
200     _doubleValues[ e_voxel_y_dimension ]             = 1.0;
201     _doubleValues[ e_voxel_z_dimension ]             = 1.0;
202     _doubleValues[ e_scale ]                         = 4.0;
203         // PS ->     _doubleValues[ e_sampling ]         = 4.0;
204     _doubleValues[ e_threshold_isocontour ]          = 40.0;
205     _doubleValues[ e_threshold_snake_isocontour ]    = 40.0;
206     _doubleValues[ e_sigma ]                         = 0.1;
207     _doubleValues[ e_RescaleIntercept ]              = 0;
208     _doubleValues[ e_RescaleSlope ]                  = 1;
209         // PS ->     _doubleValues[ e_extern_coeficient ]             = 0.3;
210         // PS ->     _doubleValues[ e_gradient_coeficient ]           = 0.3;
211         // PS ->     _doubleValues[ e_ballon_coeficient ]             = 0.02;
212         // PS ->     _doubleValues[ e_back_color_3D_r ]               = 0.0;
213         // PS ->     _doubleValues[ e_back_color_3D_g ]               = 0.0;
214         // PS ->     _doubleValues[ e_back_color_3D_b ]               = 0.0;
215         // PS ->     _doubleValues[ e_axis_color_r ]                  = 1.0;
216         // PS ->     _doubleValues[ e_axis_color_g ]                  = 0.0;
217         // PS ->     _doubleValues[ e_axis_color_b ]                  = 0.0;
218         
219         
220     _intValues[ e_roi_dimension ]                         = 10;
221     _intValues[ e_step ]                                  = 3;
222     _intValues[ e_step_coeficient ]                       = 2;
223     _intValues[ e_mass_power ]                            = 2;
224     _intValues[ e_algorithm_type ]                        = ISOCONTOURS;
225     _intValues[ e_mask_size ]                             = 5;
226     _intValues[ e_number_of_points_for_snake ]            = 20;
227     _intValues[ e_quantification_type ]                   = TYPE_AREA;
228     _intValues[ e_stenosis_type ]                         = TYPE_AREA;
229         // PS ->     _intValues[ e_number_of_iterations_snake_isocontour ] = 5;
230         // PS ->     _intValues[ e_number_of_iterations_snake_deriche ]    = 700;
231
232
233     _intValues[ e_voxel_type ]                            = VOX_TYPE_NORMAL;
234
235
236         // PS ->     _intValues[ e_coef_cell_max_int ]                     = 2;
237         
238      _stringValues[ e_installation_directory ]  = _T("NO_DIRECTORY");
239      _stringValues[ e_dicom_images_directory ] = _T("NO_DIRECTORY");
240         
241          //MAZV CT compatibility
242         _contourThresh          = 10;
243         _lumenPercentage        = 86;
244         _calcPercentage         = 90;
245         _standardDeviation      = 3;
246         _radius                 = 3;
247         //end MAZV CT compatibility
248
249 }
250
251 // -------------------------------------------------------------------------
252 bool marParameters::save( std::ofstream& os )
253 {
254     int i, s;
255         try 
256         {
257                 os.write( ( const char* )_boolValues, e_bool_cont * sizeof( bool ) );
258                 os.write( ( char* )_intValues, e_int_cont * sizeof( int ) );
259                 os.write( ( const char* )_doubleValues, e_double_cont * sizeof( double ) );
260                 for( i = 0; i < e_string_cont; i++ ) {
261                         s = _stringValues[ i ].length( );
262                         os.write( ( const char* )&s, sizeof( int ) );
263                         os.write( ( const char* )_stringValues[ i ].c_str( ), s * sizeof( char ) );
264                 } // rof
265                 return( true );
266         }
267         catch (...)
268         {
269                 return(false);
270         }
271     
272 }
273
274 // -------------------------------------------------------------------------
275 bool marParameters::load( std::ifstream& is )
276 {
277         int i, s;
278         try
279         {
280                 is.read( ( char* )_boolValues, e_bool_cont * sizeof( bool ) );
281                 is.read( ( char* )_intValues, e_int_cont * sizeof( int ) );
282                 is.read( ( char* )_doubleValues, e_double_cont * sizeof( double ) );
283                 for( i = 0; i < e_string_cont; i++ )
284                 {
285                         is.read( ( char* )&s, sizeof( int ) );
286                         _stringValues[ i ].resize( s );
287                         is.read( ( char* )_stringValues[ i ].c_str( ), s * sizeof( char ) );
288                 } // rof
289                 return( true ); 
290         }
291         catch (...)
292         {
293                 
294                 return(false);
295         }
296         
297 }
298
299 // -------------------------------------------------------------------------
300 bool marParameters::save( wxString& nw )
301 {
302     std::ofstream os( (const char*)nw.mb_str( ) ,std::ios::out |std::ios::binary );
303         
304 //EEDxx2.4 DEBuG
305 //      wxLogDebug("Saving parameters to file " + nw);
306         
307     if( os==NULL ) 
308         {
309                 wxString errorMsg;
310                 errorMsg= _T("Error : Cannot open file ")+nw+_T(" to save parameters");
311 //EEDxx2.4 DEBuG
312 //              wxLogDebug(errorMsg);
313                 return( false );
314         }
315         if (save( os ))
316         {
317                 os.close( );
318 //EEDxx2.4 DEBuG
319 //              wxLogDebug("Parameters saved to file " + nw);
320                 return( true );
321         }
322         else
323         {
324                 os.close( );
325 //EEDxx2.4 DEBuG
326 //              wxLogDebug("Error : Cannot save parameters to file "+nw);
327                 return(false);
328         }
329 }
330
331 // -------------------------------------------------------------------------
332 bool marParameters::load( wxString& nr )
333 {
334     std::ifstream is( (const char*)nr.mb_str( ),std::ios::binary|std::ios::in);
335         
336 //EEDxx2.4 DEBuG
337 //      wxLogDebug("Laoding parameters from file " + nr);
338     if( is==NULL ) 
339         {
340                 wxString errorMsg;
341                 errorMsg= _T("Error : Cannot open file ")+nr+_T(" to load parameters");
342 //EEDxx2.4 DEBuG
343 //              wxLogDebug(errorMsg);
344                 return( false );
345         }
346         if (load( is ))
347         {
348                 is.close( );
349 //EEDxx2.4 DEBuG
350 //              wxLogDebug("Parameters loaded from file " + nr);
351                 return( true );
352         }
353         else
354         {
355                 is.close( );
356 //EEDxx2.4 DEBuG
357 //              wxLogDebug("Error : Cannot load parameters from file "+nr+": file may be corrupted");
358                 return(false);
359         }
360 }
361
362 // ----------------------------------------------------------------------------
363 wxArrayString marParameters::getRelationalArray( )
364 {
365     // Allocate memory for pairs of data plus one end NULL array
366         //    wxStringList ret;
367     wxArrayString ret;
368     wxString str;
369     int i, j;
370         
371     for( i = 0, j = 0; i < e_bool_cont; i++, j += 2 ) {
372                 
373                 ret.Add( wxString( BoolParamNames[ i ], wxConvUTF8)  );
374                 str.Printf( _T("%d"), ( _boolValues[ i ] )? 1: 0 );
375                 ret.Add( str );
376                 
377     } // rof
378     for( i = 0; i < e_double_cont; i++, j += 2 ) {
379                 
380                 ret.Add( wxString( DoubleParamNames[ i ], wxConvUTF8)  );
381                 str.Printf( _T("%f"), _doubleValues[ i ] );
382                 ret.Add( str );
383                 
384     } // rof
385     for( i = 0; i < e_int_cont; i++, j += 2 ) {
386                 
387                 ret.Add( wxString( IntParamNames[ i ], wxConvUTF8) );
388                 str.Printf( _T("%d"), _intValues[ i ] );
389                 ret.Add( str );
390                 
391     } // rof
392
393      for( i = 0; i < e_string_cont; i++, j += 2 ) {
394                 ret.Add( wxString( StringParamNames[ i ], wxConvUTF8)  );
395                 ret.Add( wxString( _stringValues[ i ].c_str( ), wxConvUTF8 ) );
396      } // rof
397
398
399     return( ret );
400         
401 }
402
403 // ----------------------------------------------------------------------------
404 //void marParameters::setRelationalArray( wxStringList& arr )
405 void marParameters::setRelationalArray( wxArrayString& arr )
406 {
407         
408     int i = 0, j;
409     bool cont;
410         
411         //    while( arr[ i ] != NULL ) {
412     while( !arr.IsEmpty() ) {
413                 
414                 cont = true;
415                 for( j = 0; j < e_string_cont && cont; j++ ) {
416                         if( strcmp( StringParamNames[ j ], (const char*)arr[ i ].mb_str() ) == 0 ) {
417                                 _stringValues[ j ] = ( wxString )arr[ i + 1 ];
418                                 cont = false;
419                         } // fi
420                 } // rof
421                 for( j = 0; j < e_bool_cont && cont; j++ ) {
422                         
423                         if( strcmp( BoolParamNames[ j ], (const char*)arr[ i ].mb_str() ) == 0 ) {
424                                 
425                                 _boolValues[ j ] = ( strcmp( (const char*)arr[ i + 1 ].mb_str(), "1" ) == 0 );
426                                 cont = false;
427                                 
428                         } // fi
429                         
430                 } // rof
431                 for( j = 0; j < e_int_cont && cont; j++ ) {
432                         
433                         if( strcmp( IntParamNames[ j ], (const char*)arr[ i ].mb_str() ) == 0 ) {
434                                 
435                                 _intValues[ j ] = atoi( (const char*)arr[ i + 1 ].mb_str() );
436                                 cont = false;
437                                 
438                         } // fi
439                         
440                 } // rof
441                 for( j = 0; j < e_double_cont && cont; j++ ) {
442                         
443                         if( strcmp( DoubleParamNames[ j ], (const char*)arr[ i ].mb_str() ) == 0 ) {
444                                 
445                                 ( ( wxString )arr[ i + 1 ] ).ToDouble( &_doubleValues[ j ] );
446                                 cont = false;
447                                 
448                         } // fi
449                         
450                 } // rof
451                 i += 2;
452                 
453     } // fwhile
454         
455 }
456
457
458 bool marParameters::getInvestSliceOrder(){
459   return _boolValues[ e_invest_slice_order ];
460 }
461
462 //MAZV - Added for CT compatibility
463 // ----------------------------------------------------------------------------
464 int marParameters::getContourThresh() {
465         return _contourThresh;
466 }
467
468 // ----------------------------------------------------------------------------
469 void marParameters::setContourThresh( int thresh ) {
470         _contourThresh = thresh;
471 }
472
473 // ----------------------------------------------------------------------------
474 double marParameters::getStandardDeviation() {
475         return _standardDeviation;
476 }
477
478 // ----------------------------------------------------------------------------
479 double marParameters::getRadius() {
480         return _radius;
481 }
482
483 // ----------------------------------------------------------------------------
484 void marParameters::setStandardDeviation( double dev ) {
485         _standardDeviation = dev;
486 }
487
488 // ----------------------------------------------------------------------------
489 void marParameters::setRadius( double rad ) {
490         _radius = rad;
491 }
492
493 // ----------------------------------------------------------------------------
494 void marParameters::setLumenPercentage(int percentage) {
495         _lumenPercentage = percentage;
496 }
497 // ----------------------------------------------------------------------------
498 void marParameters::setCalcPercentage(int percentage) {
499         _calcPercentage = percentage;
500 }
501
502 // ----------------------------------------------------------------------------
503 int      marParameters::getLumenPercentage() {
504         return _lumenPercentage;
505 }
506
507 // ----------------------------------------------------------------------------
508 int  marParameters::getCalcPercentage() {
509         return _calcPercentage;
510 }
511 //MAZV end of addition
512 // eof - parameters.cxx