]> Creatis software - creaMaracasVisu.git/blob - lib/maracasVisuLib/src/kernel/include/marExperiment.cpp
BUG macOs
[creaMaracasVisu.git] / lib / maracasVisuLib / src / kernel / include / marExperiment.cpp
1 /*=========================================================================
2
3   Program:   wxMaracas
4   Module:    $RCSfile: marExperiment.cpp,v $
5   Language:  C++
6   Date:      $Date: 2009/05/14 13:55:07 $
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 "matrix.h"
19 #include "marExperiment.h"
20 #include "FonctionsGrales.h"
21 #include "ExtractionAxe.h"
22
23 // ----------------------------------------------------------------------------
24 marExperiment::marExperiment( marParameters* p ) : marObject( p ),
25                               _dynData( NULL ), _description( _T("NO DESCRIPTION") )
26 {
27   _voi[ 0 ] = _voi[ 1 ] = _voi[ 2 ] = 0;
28   _voi[ 3 ] = _voi[ 4 ] = _voi[ 5 ] = 0;
29   _startPoint[ 0 ] = _startPoint[ 1 ] = _startPoint[ 2 ] = 0;
30   _axes.push_back( NULL );
31
32 }
33
34 marExperiment::~marExperiment( ) { 
35         reset( ); 
36 }
37
38 // ----------------------------------------------------------------------------
39 void marExperiment::initExperiment( kVolume* volume )
40 {
41   {
42     reset( );
43     _dynData = new marDynData( getParameters( ) );
44
45      _dynData->loadData( volume, _voi );
46
47 /* EED Borrame
48 #ifndef DXMM
49      _dynData->loadData( volume, _voi );
50 #else
51     _dynData->loadDataDXMM( volume, _voi );
52 #endif
53 */
54   }
55 }
56
57 // ----------------------------------------------------------------------------
58 void marExperiment::setStartPoint( int sx, int sy, int sz )
59 {
60 // EED 22 sep 2006
61 //  _startPoint[ 0 ] = ( int )( ( double )sx * getParameters( )->getVoxelSize( ) );
62 //  _startPoint[ 1 ] = ( int )( ( double )sy * getParameters( )->getVoxelSize( ) );
63 //  _startPoint[ 2 ] = ( int )( ( double )sz * getParameters( )->getVoxelSize( ) );
64
65   _startPoint[ 0 ] = sx;
66   _startPoint[ 1 ] = sy;
67   _startPoint[ 2 ] = sz;
68
69 }
70
71 // ----------------------------------------------------------------------------
72 void marExperiment::extractVascularTree( int sens , double *vit, int cleanLevel)
73 {
74
75   marAxis* tmpAxis;
76   marAxis* tmpAxis2;
77
78 #ifdef __LFV__GET__VASCULAR__TREE__
79     PPPOINTAXE pts = NULL;
80     PARBREVASCULAIRE res = NULL;
81 #else
82     PPPOINTAXE res = NULL;
83     double pt[ marAxis::INDX_count ];
84 #endif
85     int n, i;
86     res = ExtraireArbre(
87       _startPoint[ 0 ],
88       _startPoint[ 1 ],
89       _startPoint[ 2 ],
90       ( PPPVOLUME_USHORT )( _dynData->getVolume( )->castIdo( ) ),
91       1, 1, 1,
92       _dynData->getVolume( )->getXdim( ) - 1,
93       _dynData->getVolume( )->getYdim( ) - 1,
94       _dynData->getVolume( )->getZdim( ) - 1,
95       ( double )( getParameters( )->getIntParam( marParameters::e_roi_dimension ) ),
96       ( double )( getParameters( )->getIntParam( marParameters::e_step ) ),
97       getParameters( )->getDoubleParam( marParameters::e_gravity_distance ),
98       getParameters( )->getDoubleParam( marParameters::e_distance_to_maximum_intensity ),
99       getParameters( )->getDoubleParam( marParameters::e_flexion_coeficient ),
100       getParameters( )->getDoubleParam( marParameters::e_tension_coeficient ),
101       getParameters( )->getIntParam( marParameters::e_mass_power ),
102 #ifdef __LFV__GET__VASCULAR__TREE__
103       pts,
104 #else
105       res,
106 #endif
107       &n , sens , vit);
108
109     // Data treatment
110 #ifdef __LFV__GET__VASCULAR__TREE__
111     for( n = 0; n < res->nomAxes; n++ ) {
112       tmp = new marAxis( getParameters( ) );
113       tmp->set_points_disc( res->lstAxes[ n ] );
114       for( i = 0; i < res->lstAxes[ n ]->nomPoints; i++ )
115         tmp->AddAxisPoint( *( res->lstAxes[ n ]->lstPoints[ i ] ) );
116       tmp->doSpline( );
117       tmp->calculateSignal( _dynData->getVolume( ) );
118       _axes.push_back( tmp );
119     } // rof
120 #else
121         if (n>2){
122                 //
123                 double voxSize = this->getParameters( )->getVoxelSize( );
124                 tmpAxis = new marAxis( this->getParameters( ) );
125                 std::string desc = "Axis N.";
126                 desc += _axes.size( );
127                 tmpAxis->setDescription( desc );
128                 tmpAxis->set_points_disc( res );
129                 for( i = 0; i < n; i++ ) {
130                         memcpy( pt, res[ i ], sizeof( POINTAXE ) );
131                         pt[ 0 ] *= voxSize;
132                         pt[ 1 ] *= voxSize;
133                         pt[ 2 ] *= voxSize;
134                         tmpAxis->addAxisPoint( pt );
135                 }        // rof
136                 tmpAxis->doSpline( );
137                 tmpAxis->calculateSignal( _dynData->getVolume( ) );
138
139                 //
140                 int jCount=0;
141                 tmpAxis2 = new marAxis( this->getParameters( ) );
142                 tmpAxis2->setDescription( desc );
143                 tmpAxis2->set_points_disc( res );       
144                 for( i = 0; i < n; i++ ) {
145                         if (tmpAxis->getSignal(i)>=cleanLevel ){
146                                 jCount++;
147                                 memcpy( pt, res[ i ], sizeof( POINTAXE ) );
148                                 pt[ 0 ] *= voxSize;
149                                 pt[ 1 ] *= voxSize;
150                                 pt[ 2 ] *= voxSize;
151                                 tmpAxis2->addAxisPoint( pt );
152                         } else {
153                                 i=n;
154                         }
155                 } // for 
156                 delete tmpAxis;
157
158                 if (jCount>2){
159                         tmpAxis2->doSpline( );
160                         tmpAxis2->calculateSignal( _dynData->getVolume( ) );
161                         _axes.push_back( tmpAxis2 );
162                         _axes[ 0 ] = tmpAxis2;
163                 } else {
164                         delete tmpAxis2;
165                 }
166     }
167 #endif // __LFV__GET__VASCULAR__TREE__
168
169 }
170
171
172 //marContour* marExperiment::generateContour( int slice, int x, int y, std::vector< double* >* points) 
173 vtkPolyData* marExperiment::generateContour( int slice, int x, int y, std::vector< double* >* points)
174 {
175 /*  vtkImageData* imagedata;
176
177   imagedata = getSliceImage( slice );
178   vtkKitwareContourFilter* cntVTK = vtkKitwareContourFilter::New( );
179   cntVTK->SetInput( imagedata );
180   cntVTK->SetNumberOfContours( 1 );
181   //cntVTK->SetValue( 0, vmin );
182   cntVTK->SetValue( 0, 20 );
183   //cntVTK->SetValue( 1, vmax );
184   cntVTK->Update( );
185
186     return cntVTK;*/
187
188 //  return(_axes[ 0 ]->setContour( slice, x, y, points));
189
190         return NULL;
191         
192   //return( _axes[ 0 ]->getContour( slice ) );
193 }
194
195
196 // ----------------------------------------------------------------------------
197 void marExperiment::applyChangeResolution( )
198 {
199   int i;
200
201   for( i = 1; i < _axes.size( ); i++ )
202     _axes[ i ]->changeAxisResolution( );
203 }
204
205 // ----------------------------------------------------------------------------
206 void marExperiment::prepareQuantification( )
207 {
208
209 // EED borrame
210 //  _axes[0]->sliceVolumeAxis( _dynData->getVolume( ), true);
211
212   if( _axes[ 0 ] )
213     _axes[ 0 ]->createEmptyVectors();
214
215 }
216
217 // ----------------------------------------------------------------------------
218 void marExperiment::backQuant( )
219 {
220 //Cette méthode n'existe plus: dommage 
221 //    if ( _axes[0]->getQuantOn() )
222   {
223     _axes[0]->setFinishQuant( _axes[0]->getFinishQuant() - 1);
224     if ( _axes[0]->getFinishQuant() < 0 )
225       _axes[0]->setFinishQuant(0);
226   }
227 }
228
229
230 // ----------------------------------------------------------------------------
231 void marExperiment::reset( )
232 {
233   int i;
234
235   _description = _T("");
236   if( _dynData != NULL ) delete _dynData;
237     
238  
239   _dynData = NULL;
240
241   for( i = 1; i < _axes.size( ); i++ ){
242     if (_axes[ i ]!=NULL) delete _axes[ i ];
243   }
244
245   _axes.clear( );
246   _axes.push_back( NULL );
247 // PS ->   // PS Et les images ???? _quantificationImages
248 }
249
250 // ----------------------------------------------------------------------------
251 void marExperiment::calculateAxesSignal( )
252 {
253   int i;
254   for( i = 1; i < _axes.size( ); i++ )
255     _axes[ i ]->calculateSignal( _dynData->getVolume( ) );
256 }
257
258 // ----------------------------------------------------------------------------
259 void marExperiment::prepareContourImages( )
260 {
261   if( _axes[ 0 ] )
262     _axes[ 0 ]->sliceVolumeAxis( _dynData->getVolume( ) );
263 }
264
265 // ----------------------------------------------------------------------------
266 void marExperiment::copyFrom( const marObject& from )
267 { // TODO
268 }
269
270 // ----------------------------------------------------------------------------
271 bool marExperiment::save( std::ofstream& os )
272 {
273   int i;
274   size_t st;
275
276   st = _description.length( );
277   os.write( ( const char* )&st, sizeof( size_t ) );
278   os.write( ( const char* )_description.c_str( ), sizeof( char ) * st );
279   os.write( ( const char* )_startPoint, 3 * sizeof( int ) );
280   os.write( ( const char* )_voi, 6 * sizeof( int ) );
281   _dynData->save( os );
282   i = _axes.size( ) - 1;
283   os.write( ( const char* )&i, sizeof( int ) );
284   for( i = 1; i < _axes.size( ); i++ )
285     _axes[ i ]->save( os );
286   return( true );
287 }
288
289 // ----------------------------------------------------------------------------
290 bool marExperiment::load(  std::ifstream& is )
291 {
292
293   int i, n;
294   char ttmp[ 5000 ];
295   size_t st;
296   marAxis* ax;
297
298   reset( );
299
300   is.read( ( char* )&st, sizeof( size_t ) );
301   is.read( ( char* )ttmp, sizeof( char ) * st );
302   ttmp[ st ] = '\0';
303
304 // EED 05 join 2007
305 // _description  = ttmp;
306   _description   = wxString(ttmp, wxConvUTF8);
307
308   is.read( ( char* )_startPoint, 3 * sizeof( int ) );
309   is.read( ( char* )_voi, 6 * sizeof( int ) );
310
311   _dynData = new marDynData( getParameters( ) );
312   _dynData->load( is );
313
314   is.read( ( char* )&n, sizeof( int ) );
315   for( i = 1; i <= n; i++ ) {
316     ax = new marAxis( getParameters( ) );
317     ax->load( is );
318     _axes.push_back( ax );
319     _axes[ 0 ] = ax;
320   } // rof
321   return( true );
322 }
323
324 // ----------------------------------------------------------------------------
325 void marExperiment::DeleteAxis( unsigned int index )
326 {
327         /* Very naughty bug, when doing a - 1, where a is uint result is still
328         uint, imaging when a=0 :) need to cast to int*/
329   
330   int axesize=_axes.size( )-1;
331   index=index+1;
332   delete _axes[ index ];
333   for( int i = index; i < axesize; i++ ) 
334      _axes[ i ] = _axes[ i + 1 ];
335   _axes.pop_back( );
336
337   if (index<axesize) {
338         _axes[ 0 ]=_axes[ index ];
339   } else {
340         _axes[ 0 ]=NULL;
341   }
342
343 /*  
344   _axes[index]->Delete();
345   if(_axes[ 0 ]) 
346           _axes[ 0 ]->Delete( );
347   _axes.erase(_axes.begin() + index);
348 */
349
350 // PS ->   // PS : traitement identique à la methode std::vector::erase(iterator)
351 /*
352   if(_axes[ 0 ]) 
353           _axes[ 0 ]->Delete( );
354   delete _axes[ index ];
355 */
356 }
357 // ----------------------------------------------------------------------------
358 void marExperiment::RegenerateSignal(){
359         marAxis *maraxis = getAxis(); 
360         if (maraxis!=NULL) {
361                 maraxis->calculateSignal( _dynData->getVolume( ) );
362                 maraxis->eraseContourVectorsContent();                   
363         }
364 }
365 // ----------------------------------------------------------------------------
366 void marExperiment::ClearContours(){
367         marAxis *maraxis = getAxis(); 
368         if (maraxis!=NULL) maraxis->eraseContourVectorsContent();                        
369 }
370 // ----------------------------------------------------------------------------
371 void marExperiment::RegenerateAxis(){
372         if (getAxis()!=NULL){
373                 int actualquant=getAxis()->getActualQuant();
374                 DeleteAxis(0);                  
375                 extractVascularTree();
376                 setAxis(0);     
377                 getAxis()->setActualQuant(actualquant);
378                 getAxis()->createEmptyVectors();                         
379         }
380 }
381 // ----------------------------------------------------------------------------
382 void marExperiment::RecalculateAxis(){
383         marAxis *maraxis = getAxis(); 
384         if (maraxis!=NULL){
385                 maraxis->doSpline();     
386                 int nCnts = ( int ) maraxis->getNumberOfSplinePoints( );
387                 getAxis()->setActualQuant(nCnts/2);
388                 getAxis()->createEmptyVectors();                         
389         }
390 }
391
392 // eof - experiment.cxx