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