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