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