]> Creatis software - creaMaracasVisu.git/blob - lib/maracasVisuLib/src/interface/tcl/marTclInterface.h
Support #1768 CREATIS Licence insertion
[creaMaracasVisu.git] / lib / maracasVisuLib / src / interface / tcl / marTclInterface.h
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 #ifndef MAR__INTERFACE__TCL__HXX
27 #define MAR__INTERFACE__TCL__HXX
28
29 #include "../kernel/marInterface.h"
30 #include <kgfo/volstat.hxx>
31 #include <tcl.h>
32 #include <tk.h>
33 #include <wx/list.h>
34 #include <wx/string.h>
35 #include <vtkCellArray.h>
36 #include <vtkCellLocator.h>
37 #include <vtkImageData.h>
38 #include <vtkMath.h>
39 #include <vtkPoints.h>
40 #include <vtkPolyData.h>
41 #include <vtkTclUtil.h>
42
43 // ----------------------------------------------------------------------------
44 static marInterface MyMaracasData;
45 static kVolume* _ucharVol = NULL;
46
47 // ----------------------------------------------------------------------------
48 void freeAllMaracasInterfaces_dll( )
49 {
50         MyMaracasData.reset( );
51         if( _ucharVol ) delete _ucharVol;
52         _ucharVol = NULL;
53 }
54
55 // ----------------------------------------------------------------------------
56 wxStringList params_dll( )
57 {
58     return( MyMaracasData._parameters->getRelationalArray( ) );
59
60 }
61
62 // ----------------------------------------------------------------------------
63 void setParams_dll( wxStringList& raParams )
64 {
65     MyMaracasData._parameters->setRelationalArray( raParams );
66
67 }
68
69 // ----------------------------------------------------------------------------
70 bool saveParams_dll( std::string& fName )
71 {
72     return( MyMaracasData.saveParameters( std::string( fName ) ) );
73
74 }
75
76 // ----------------------------------------------------------------------------
77 bool loadParams_dll( std::string& fName )
78 {
79     return( MyMaracasData.loadParameters( std::string( fName ) ) );
80
81 }
82
83 // ----------------------------------------------------------------------------
84 void SetROIStep_dll( double vWidth )
85 {
86     MyMaracasData._parameters->setROIStep( vWidth );
87
88 }
89
90 // ----------------------------------------------------------------------------
91 double GetActualVoxelSize_dll( ) {
92
93     return( MyMaracasData._parameters->getVoxelSize( ) );
94
95 }
96
97 // ----------------------------------------------------------------------------
98 wxStringList studies_dll( )
99 {
100     return( MyMaracasData._dicom->getStudies( ) );
101 }
102
103 // ----------------------------------------------------------------------------
104 wxStringList studyData_dll( std::string& study )
105 {
106     MyMaracasData._dicom->setActualStudy( study );
107     return( MyMaracasData._dicom->getRelationalArrayStudyData( ) );
108
109 }
110
111 // ----------------------------------------------------------------------------
112 wxStringList series_dll( std::string& study )
113 {
114     MyMaracasData._dicom->setActualStudy( study );
115     return( MyMaracasData._dicom->getSeries( ) );
116
117 }
118
119 // ----------------------------------------------------------------------------
120 wxStringList serieData_dll( std::string& study, std::string& serie )
121 {
122     MyMaracasData._dicom->setActualStudy( study );
123     MyMaracasData._dicom->setActualSerie( serie );
124     return( MyMaracasData._dicom->getRelationalArraySerieData( ) );
125
126 }
127
128 // ----------------------------------------------------------------------------
129 void SubtractSeries_dll( std::string& sl, std::string& sr, std::string& ss, std::string& description )
130 {/*
131    marDicom::SubtractSeries(
132    MyMaracasData._parameters,
133    MyMaracasData._dicom->getActualStudy( ),
134    sl, sr,
135    ss, description
136    );
137  */
138 }
139
140 // ----------------------------------------------------------------------------
141 bool LoadImages_dll( )
142 {
143     MyMaracasData._dicom->loadActualSerie( );
144     return( true );
145
146 }
147
148 // ----------------------------------------------------------------------------
149 void CastVolume_dummy( )
150 {
151         if( _ucharVol ) delete _ucharVol;
152         _ucharVol = new kVolume( *( MyMaracasData._dicom->getVolume( ) ) );
153
154         _ucharVol->convertScale( kVolume::UCHAR, 0, 255 );
155
156 }
157
158 // ----------------------------------------------------------------------------
159 void LoadTkImage_dummy( uchar** image, int w, int h,
160                                                 std::string& tkName, Tcl_Interp* interp )
161 {
162     Tk_PhotoHandle hnd;
163     Tk_PhotoImageBlock bl;
164
165     hnd = Tk_FindPhoto( interp, ( char* )tkName.c_str( ) );
166     if( hnd != NULL ) {
167
168         // Image size
169         bl.width  = w;
170         bl.height = h;
171
172         // One pixel is represented with one unsigned short value
173         bl.pixelSize = sizeof( uchar );
174
175         // We're working on intensity images i.e. no RGB
176         bl.offset[ 0 ] = 0;
177         bl.offset[ 1 ] = 0;
178         bl.offset[ 2 ] = 0;
179         bl.offset[ 3 ] = 0;
180
181         // To go to next in vertical value
182         bl.pitch = bl.pixelSize * bl.width;
183
184         // Menory block
185         bl.pixelPtr = ( uchar* )image[ 0 ];
186
187         // Ok, lets do it!
188         Tk_PhotoPutBlock( hnd, &bl, 0, 0, bl.width, bl.height );
189
190     } // fi
191
192 }
193
194 // ----------------------------------------------------------------------------
195 void LoadTkImages_dll( wxStringList& tkNames, Tcl_Interp* interp )
196 {
197     int k, w, h, d;
198         std::string tmp;
199
200     w = MyMaracasData._dicom->getVolume( )->getXdim( );
201     h = MyMaracasData._dicom->getVolume( )->getYdim( );
202     d = MyMaracasData._dicom->getVolume( )->getZdim( );
203
204     CastVolume_dummy( );
205         uchar*** vol = ( uchar*** )( _ucharVol->getData3D( ) );
206     for( k = 0; k < d; k++ ) {
207
208                 tmp = tkNames[ k ];
209         LoadTkImage_dummy( vol[ k ],
210                                                    w, h, tmp, interp );
211
212     } // rof
213
214 }
215
216 // ----------------------------------------------------------------------------
217 int GetNumberOfImages_dll( )
218 {
219     return( MyMaracasData._dicom->getVolume( )->getZdim( ) );
220
221 }
222
223 // ----------------------------------------------------------------------------
224 wxStringList GetImagesNumbers_dll( )
225 {
226     return( MyMaracasData._dicom->getImageNumbers( ) );
227
228 }
229
230 // ----------------------------------------------------------------------------
231 double GetImageIntensity_dll( int x, int y, int z )
232 {
233     return( MyMaracasData._dicom->getVolume( )->getPixel( x, y, z ) );
234
235 }
236
237 // ----------------------------------------------------------------------------
238 ushort* GetProfilFromTotalVolume_dll( int xO, int yO, int zO,
239                                                                                  int xF, int yF, int zF )
240 {
241     int i;
242         kVolumeStat* stats = new kVolumeStat( MyMaracasData._dicom->
243                                                                                   getVolume( ) );
244
245         stats->profile( xO, yO, zO, xF, yF, zF );
246
247     ushort* r = new ushort[ stats->getSize( ) + 6 ];
248
249     r[ 0 ] = stats->getSize( ) + 6;
250     r[ 1 ] = ( ushort )stats->getMin( );
251     r[ 2 ] = ( ushort )stats->getMax( );
252     r[ 3 ] = ( ushort )stats->getAvg( );
253     r[ 4 ] = ( ushort )stats->getSD( );
254     r[ 5 ] = ( ushort )stats->getSize( );
255     for( i = 0; i < stats->getSize( ); i++ )
256                 r[ i + 6 ] = ( ushort )( stats->getValues( ) )[ i ];
257     return( r );
258
259 }
260
261 // ----------------------------------------------------------------------------
262 void GetAreaValuesFromTotalVolume_dll( int xO, int yO, int zO,
263                                                                            int xF, int yF, int zF,
264                                                                            int* min, int* max, int* avg,
265                                                                            int* sd, int* size )
266 {
267         kVolumeStat* stats = new kVolumeStat( MyMaracasData._dicom->getVolume( ) );
268
269         stats->stats( xO, yO, zO, xF, yF, zF );
270
271     *min = ( int )stats->getMin( );
272     *max = ( int )stats->getMax( );
273     *avg = ( int )stats->getAvg( );
274     *sd = ( int )stats->getSD( );
275     *size = stats->getSize( );
276
277 }
278
279 // ----------------------------------------------------------------------------
280 bool SaveExperiment_dll ( std::string& fName )
281 {
282     return( MyMaracasData.saveExperiment( fName ) );
283
284 }
285
286 // ----------------------------------------------------------------------------
287 bool LoadExperiment_dll ( std::string& fName )
288 {
289     return( MyMaracasData.loadExperiment( fName ) );
290
291 }
292
293 // ----------------------------------------------------------------------------
294 void InitExperiment_dll ( int* voi )
295 {
296     MyMaracasData._experiment->setVOI( voi );
297     MyMaracasData._experiment->initExperiment( MyMaracasData._dicom->
298                                                                                            getVolume( ) );
299         //if( _ucharVol ) delete _ucharVol;
300         //_ucharVol = NULL;
301         MyMaracasData._dicom->freeVolume( );
302 }
303
304 // ----------------------------------------------------------------------------
305 void SetStartPoint_dll( int x, int y, int z )
306 {
307     MyMaracasData._experiment->setStartPoint( x, y, z );
308 }
309
310 // ----------------------------------------------------------------------------
311 void ExtractAxes_dll( )
312 {
313     MyMaracasData._experiment->extractVascularTree( );
314 }
315
316 // ----------------------------------------------------------------------------
317 int GetNumberOfAxes_dll( )
318 {
319     return( MyMaracasData._experiment->getNumberOfAxes( ) );
320 }
321
322 // ----------------------------------------------------------------------------
323 vtkPolyData* GetAllAxes_dll( )
324 {
325     int i, n, j;
326     double p[ marAxis::INDX_count ];
327     marAxis* tmp;
328
329     vtkPoints* allPoints = vtkPoints::New( );
330     vtkCellArray* allTopology = vtkCellArray::New( );
331
332     j = 0;
333     for( n = 0; n < MyMaracasData._experiment->getNumberOfAxes( ); n++ ) {
334
335         tmp = MyMaracasData._experiment->getAxis( );
336         allTopology->InsertNextCell( tmp->getNumberOfControlPoints( ) );
337         for( i = 0; i < tmp->getNumberOfControlPoints( ); i++ ) {
338
339             tmp->getControlPoint( i, p, p + 3 );
340             allPoints->InsertNextPoint( p[ 0 ], p[ 1 ], p[ 2 ] );
341             allTopology->InsertCellPoint( j );
342                         j++;
343
344         } // rof
345
346     } // rof
347
348     vtkPolyData* allData = vtkPolyData::New( );
349     allData->SetPoints( allPoints );
350     allData->SetLines( allTopology );
351     
352     allPoints->Delete();
353     allTopology->Delete();
354
355     return( allData );
356
357 }
358
359 // ----------------------------------------------------------------------------
360 vtkPolyData* GetActualAxis_dll( )
361 {
362     int i, j, h;
363     double p[ marAxis::INDX_count ];
364     marAxis* tmp;
365
366     vtkPoints* points = vtkPoints::New( );
367     vtkCellArray* topology = vtkCellArray::New( );
368
369     j = 0;
370     h = MyMaracasData._experiment->getDynData( )->getVolume( )->getYdim( );
371     tmp = MyMaracasData._experiment->getAxis( );
372     topology->InsertNextCell( tmp->getNumberOfControlPoints( ) );
373     for( i = 0; i < tmp->getNumberOfControlPoints( ); i++ ) {
374
375                 tmp->getControlPoint( i, p, p + 3 );
376                 topology->InsertCellPoint( j++ );
377                 points->InsertNextPoint( p[ 0 ], h - 1 - p[ 1 ], p[ 2 ] );
378
379     } // rof
380
381     vtkPolyData* data = vtkPolyData::New( );
382     data->SetPoints( points );
383     data->SetLines( topology );
384     
385     points->Delete();
386     topology->Delete();
387
388     return( data );
389 }
390
391 // ----------------------------------------------------------------------------
392 int GetActualAxisNumberOfPoints_dll( )
393 {
394     return( MyMaracasData._experiment->getAxis( )->getNumberOfControlPoints( ) );
395
396 }
397
398 // ----------------------------------------------------------------------------
399 double* GetActualAxisPoint_dll( int i )
400 {
401     double* ret = new double[ marAxis::INDX_count + 1 ];
402
403     ret[ 0 ] = marAxis::INDX_count;
404     MyMaracasData._experiment->getAxis( )->getControlPoint( i, ret + 1, ret + 4 );
405     return( ret );
406 }
407
408 // ----------------------------------------------------------------------------
409 vtkImageData* GetActualAxisSlice_dll( int i )
410 {
411         //    return( MyMaracasData._experiment->GetSliceImage( i ) );
412         return( NULL );
413
414 }
415
416 // ----------------------------------------------------------------------------
417 void PrepareQuantification_dll( int i )
418 {
419         /*
420           MyMaracasData._experiment->SetAxis( i );
421           MyMaracasData._experiment->PrepareQuantification( );
422         */
423 }
424
425 // ----------------------------------------------------------------------------
426 double GetAxisLength_dll( )
427 {
428     return( MyMaracasData._experiment->getAxis( )->length( ) );
429
430 }
431
432 // ----------------------------------------------------------------------------
433 void GetImageRange_dll( int* min, int* max )
434 {
435     double m, M;
436
437     MyMaracasData._experiment->getDynData( )->getVolume( )->getMinMax( m, M );
438     *min = ( int )m;
439     *max = ( int )M;
440
441 }
442
443 // ----------------------------------------------------------------------------
444 vtkImageData* GetVTKVolume_dll( )
445 {
446     return( MyMaracasData._experiment->getDynData( )->getVolume( )->castVtk( ) );
447
448 }
449
450 // ----------------------------------------------------------------------------
451 int IntersectWithLine_dll(
452                                                   vtkCellLocator* iCellLocator,
453                                                   double x1, double y1, double z1, 
454                                                   double x2, double y2, double z2,
455                                                   double tol, double *xI, double *yI, double *zI
456                                                   )
457 {
458     float a0[ 3 ], a1[ 3 ], x[ 3 ], t, pcoords[ 3 ];
459     int subId; 
460
461     int returnVal;
462  
463     iCellLocator->Update( );
464
465     a0[ 0 ] = x1; a0[ 1 ] = y1; a0[ 2 ] = z1;
466     a1[ 0 ] = x2; a1[ 1 ] = y2; a1[ 2 ] = z2;
467
468     returnVal = iCellLocator->IntersectWithLine( a0, a1, tol, t, x, pcoords, subId );
469     ( *xI ) = x[ 0 ]; ( *yI ) = x[ 1 ]; ( *zI ) = x[ 2 ];
470
471     return( returnVal );
472 }
473
474 // ----------------------------------------------------------------------------
475 void TclPerpendiculars_dll(
476                                                    double xN, double yN, double zN, double angle,
477                                                    double *xP1, double *yP1, double *zP1,
478                                                    double *xP2, double *yP2, double *zP2
479                                                    )
480 {
481     double x[ 3 ], y[ 3 ], z[ 3 ];
482     vtkMath* math;
483
484     x[ 0 ] = xN; x[ 1 ] = yN; x[ 2 ] = zN;
485     math = vtkMath::New( );
486     math->Perpendiculars( x, y, z,angle );
487     ( *xP1 ) = y[ 0 ]; ( *yP1 ) = y[ 1 ]; ( *zP1 ) = y[ 2 ];
488     ( *xP2 ) = z[ 0 ]; ( *yP2 ) = z[ 1 ]; ( *zP2 ) = z[ 2 ];
489     math->Delete( );
490 }
491
492 #endif // MAR__INTERFACE__TCL__HXX
493
494 // EOF - interfaceTCL.hxx