]> Creatis software - creaMaracasVisu.git/blob - lib/maracasVisuLib/src/interface/wxWindows/widgets/vtkPlane2DView.cxx
f0cbc97744c4d841c05e21f5150e7aaf0ce4d67c
[creaMaracasVisu.git] / lib / maracasVisuLib / src / interface / wxWindows / widgets / vtkPlane2DView.cxx
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 #include "vtkPlane2DView.h"
27
28 #include "vtkCellArray.h"
29
30 #include "vtkInteractorStyleBaseView2D.h"
31 #include "vtkPointData.h"
32
33
34 vtkPlane2DView::vtkPlane2DView( wxWindow *parent)
35 : wxVtk2DBaseView(parent)
36 {
37
38         _backX                                  =       -99999;
39         _backY                                  =       -99999;
40         _backZ                                  =       -99999;
41
42         _backOrient[0]                  =       -99999;
43         _backOrient[1]                  =       -99999;
44         _backOrient[2]                  =       -99999;
45         _backOrient[3]                  =       -99999;
46
47         _active                                 =   true;
48         _mip_visualization              =       true;
49         _mip_width                              =       2;
50
51     _transform1                         =       vtkTransform::New();
52     _transform2                         =       vtkTransform::New();
53         _transform1->Identity();
54         _transform2->Identity();
55
56         _sizeIma                                =       200;
57
58         _pSource                                =       NULL;
59         _3Dslices                               =       NULL;
60         _stPoints                               =       NULL;
61         _change                                 =       NULL;
62         _imageResult                    =       NULL;
63
64         // line horizontal
65     _pts                        = NULL;
66     _lineActor          = NULL;
67         _lineMapper             = NULL;
68         _pd                             = NULL;
69
70         _interactorstyleplane2D =       false;
71 }
72 //-------------------------------------------------------------------
73 vtkPlane2DView::~vtkPlane2DView()
74 {
75         ResetPlane();
76
77         // Horizontal Line
78     if (_pts            != NULL)        {       _pts                    -> Delete(); }
79     if (_lineActor      != NULL)        {       _lineActor              -> Delete(); }
80     if (_lineMapper     != NULL)        {       _lineMapper             -> Delete(); }
81     if (_pd                     != NULL)        {       _pd                             -> Delete(); }
82
83         _transform1             -> Delete();
84         _transform2             -> Delete();
85 }
86
87 //-------------------------------------------------------------------
88 void vtkPlane2DView::ResetBack()
89 {
90                 _backX=-1;
91                 _backY=-1;
92                 _backZ=-1;
93                 _backOrient[0]=-1;
94                 _backOrient[1]=-1;
95                 _backOrient[2]=-1;
96                 _backOrient[3]=-1;
97 }
98
99 //-------------------------------------------------------------------
100 void vtkPlane2DView::ResetPlane(){
101         if (_pSource    !=NULL) { _pSource      -> Delete();    }
102         if (_3Dslices   !=NULL) { _3Dslices     -> Delete();    }
103         if (_stPoints   !=NULL) { _stPoints     -> Delete();    }
104         if (_change             !=NULL) { _change       -> Delete();    }
105 }
106 //-------------------------------------------------------------------
107 vtkMPRBaseData *vtkPlane2DView::GetVtkmprbasedata()
108 {
109         return (vtkMPRBaseData*)GetVtkBaseData();
110 }
111
112 //-------------------------------------------------------------------
113 void vtkPlane2DView::SetPSource(int sizeIma){
114         int dimIma      = sizeIma; 
115
116         double x = GetVtkmprbasedata()->GetX();
117         double y = GetVtkmprbasedata()->GetY();
118         double z = GetVtkmprbasedata()->GetZ();
119
120         double spc[3];
121         vtkImageData *imagedata = GetVtkmprbasedata()->GetImageData();
122         imagedata->GetSpacing(spc);
123         x=x*spc[0];
124         y=y*spc[1];
125         z=z*spc[2];
126
127
128         vtkMPRBaseData  *mprbasedata = (vtkMPRBaseData*)this->GetVtkBaseData();
129         vtkTransform    *transform       = mprbasedata->GetTransformOrientation();
130
131         double in[3];  // temp
132         double pA[3];
133         double pB[3];
134         in[0]=1;                in[1]=0;                        in[2]=0;
135         transform->TransformPoint(in,_n);
136
137         in[0]=0;        in[1]=dimIma-1;         in[2] = 0;
138         transform->TransformPoint(in,pA);
139
140         in[0]=0;        in[1]=0;                in[2]=dimIma-1;
141         transform->TransformPoint(in,pB);
142
143     _pSource -> SetPoint1( pA   );
144     _pSource -> SetPoint2( pB   );
145
146         _pSource -> SetOrigin( 0        , 0             , 0             );
147     _pSource -> SetResolution( sizeIma-1 , sizeIma -1 );
148         _pSource -> Update();
149         _pSource -> SetCenter( x, y, z );
150     _pSource -> SetNormal( _n );
151     _pSource -> Update( );
152
153 // EED Borrame
154 //      transform->Delete();
155
156 }
157
158 //-------------------------------------------------------------------
159 void vtkPlane2DView::ExtractPlane() 
160 {
161
162         double x = GetVtkmprbasedata()->GetX();
163         double y = GetVtkmprbasedata()->GetY();
164         double z = GetVtkmprbasedata()->GetZ();
165
166         double spc[3];
167         vtkImageData *imagedata = GetVtkmprbasedata()->GetImageData();
168         imagedata->GetSpacing(spc);
169         x=x*spc[0];
170         y=y*spc[1];
171         z=z*spc[2];
172
173         vtkTransform *transform = GetVtkmprbasedata()->GetTransformOrientation();
174         double orientation[4];
175         transform->GetOrientationWXYZ(orientation);
176
177         bool okOrientation=true;
178         if ((orientation[0]!=_backOrient[0]) || (orientation[1]!=_backOrient[1]) ||
179                 (orientation[2]!=_backOrient[2]) || (orientation[3]!=_backOrient[3]))
180         {
181                 okOrientation=false;
182         }
183
184         bool okPosicion=true;
185         if ( (x!=_backX) || (y!=_backY) || (z!=_backZ) )
186         {
187                 okPosicion=false;
188         }
189
190         if ((okPosicion==false) || (okOrientation==false) ) {
191                 if (_active==true){
192
193                         bool ok = false;
194
195                         if ( _mip_visualization==true )
196                         {
197                                 if (_interactorstyleplane2D!=NULL)
198                                 {
199                                         if (_interactorstyleplane2D->GetStateRotate()==false )
200                                         {
201                                                 ok=true;
202                                         }
203                                 }
204                         }
205 //                      ok=true;
206
207                         if (ok==true)
208                         {
209                                 Extract_MIP_PlaneVTK();
210                         } else {
211                                 Extract_One_PlaneVTK();
212                         } // ok
213
214                 } // active
215                 _backX=x;
216                 _backY=y;
217                 _backZ=z;
218                 _backOrient[0]=orientation[0];
219                 _backOrient[1]=orientation[1];
220                 _backOrient[2]=orientation[2];
221                 _backOrient[3]=orientation[3];
222         } //okPosition okOrientation
223 }
224
225 //-------------------------------------------------------------------
226
227 void vtkPlane2DView::Extract_One_PlaneVTK()
228 {
229         vtkImageData *imagedata = GetVtkmprbasedata()->GetImageData();
230         SetPSource(_sizeIma);
231         _3Dslices -> SetInput( ( vtkDataSet* )_pSource->GetOutput( ) );
232         _3Dslices -> SetSource( imagedata );
233         _3Dslices -> Update( );
234         _stPoints -> GetPointData( )->SetScalars(  _3Dslices->GetOutput()->GetPointData()->GetScalars()  );
235         _stPoints -> SetDimensions( _sizeIma, _sizeIma, 1 );
236         _stPoints -> SetScalarType( imagedata->GetScalarType() );
237         _stPoints -> SetScalarTypeToShort();
238         _stPoints -> Update();
239 //      _change   -> SetInput( _stPoints );  
240 //      _change   -> Update();    //important
241         _imageViewer2XYZ->GetVtkImageViewer2()->SetInput ( _stPoints );
242 //      _imageViewer2XYZ->GetVtkImageViewer2()->SetInput ( _change->GetOutput() );
243         //      vtkImageActor *imageActor = _imageViewer2XYZ->GetVtkImageViewer2()->GetImageActor();
244 }
245
246 //-------------------------------------------------------------------
247
248 void vtkPlane2DView::Extract_MIP_PlaneVTK( /*double heightDefinition*/ )
249 {
250
251         int mipWidth;
252         double sp;
253         int sizeWidth = (_mip_width*2)+1 ;
254         int deltaPixel;
255         int iWidth,itmp,tmpSizeWith;
256
257         double spc[3];
258         vtkImageData *imagedata = GetVtkmprbasedata()->GetImageData();
259         SetPSource(_sizeIma);
260         imagedata->GetSpacing(spc);
261
262         bool heightDefinition=false;
263         if (_mip_width<3)
264         {
265                 heightDefinition=true;
266         }
267
268         if (heightDefinition==true)
269         {
270                 mipWidth        =       _mip_width;
271                 sp                      =       spc[0];
272                 deltaPixel      =       1;
273         } else {
274                 mipWidth        =       2;
275                 tmpSizeWith =   (mipWidth*2) + 1;
276                 sp                      =       (spc[0]*sizeWidth)/tmpSizeWith;
277                 sizeWidth       =       tmpSizeWith;
278                 deltaPixel      =       4;
279         }
280
281
282
283         std::vector< vtkProbeFilter* > slicesLST;
284
285         _pSource->Push( -mipWidth * sp );
286         _pSource->Update();
287
288         for ( iWidth=0 ; iWidth<sizeWidth ; iWidth++ )
289         {       
290                 vtkProbeFilter *slice = vtkProbeFilter::New();
291                 slice -> SetInput( ( vtkDataSet* )_pSource->GetOutput( ) );
292                 slice -> SetSource( imagedata );
293                 slice -> Update( );
294                 slicesLST.push_back( slice );
295                 _pSource->Push( sp );
296         }
297         
298         if (_imageResult ==NULL)
299         {
300                 _imageResult = vtkImageData::New();
301                 _imageResult -> SetDimensions(_sizeIma,_sizeIma,1);
302                 _imageResult -> SetSpacing(1,1,1);
303                 _imageResult -> SetScalarType( imagedata->GetScalarType() );
304                 _imageResult -> SetExtent(0,_sizeIma-1,0,_sizeIma-1,0,0);
305                 _imageResult -> SetWholeExtent(0,_sizeIma-1,0,_sizeIma-1,0,0);
306                 _imageResult -> AllocateScalars();
307                 _imageResult -> Update();
308         }
309
310         unsigned short *pTemp;
311         unsigned short *pResult;
312         pResult = (unsigned short*)_imageResult->GetScalarPointer( 0 , 0 , 0 ); 
313
314         int iPixels , sizePixels = _sizeIma*_sizeIma;
315         for(iPixels=0 ; iPixels<sizePixels ; iPixels=iPixels+deltaPixel)
316         {
317
318                 pTemp = (unsigned short*)slicesLST[0]->GetOutput()->GetPointData()->GetScalars()->GetVoidPointer(0);
319
320                 pResult = (unsigned short*)_imageResult->GetScalarPointer( 0 , 0 , 0 ); 
321                 pResult[iPixels] = pTemp[iPixels];
322
323                 for (iWidth=1;iWidth<sizeWidth;iWidth++)
324                 {
325                         pTemp = (unsigned short*)slicesLST[iWidth]->GetOutput()->GetPointData()->GetScalars()->GetVoidPointer(0);
326
327                         if (pResult[iPixels]< pTemp[iPixels])
328                         {
329                                 pResult[iPixels] = pTemp[iPixels];
330                         }
331                 }
332
333                 if (deltaPixel!=1)
334                 {
335                         for (itmp=1;itmp<deltaPixel;itmp++)
336                         {
337                                 pResult[iPixels+itmp] = pResult[iPixels];
338                         }
339                 }
340
341         }
342
343         for (iWidth=0;iWidth<sizeWidth;iWidth++)
344         {       
345                 slicesLST[iWidth]->Delete();
346         }
347
348         _imageResult->Modified();
349         _imageViewer2XYZ->GetVtkImageViewer2()->SetInput ( _imageResult );
350
351 }
352
353 //-------------------------------------------------------------------
354 void vtkPlane2DView::Configure( )
355 {
356         wxVtk2DBaseView::Configure(false);
357
358         HorizontalLine();
359 // Borrame
360 //      CircleLine();
361
362         _pSource         = vtkPlaneSource::New( );
363     _3Dslices    = vtkProbeFilter::New( ) ;
364         _stPoints        = vtkStructuredPoints::New( );
365         _change          = vtkImageChangeInformation::New();
366
367         wxVTKRenderWindowInteractor *iren               = GetWxVTKRenderWindowInteractor();
368         ExtractPlane();
369 //      SetActive(false);
370         _imageViewer2XYZ -> GetVtkImageViewer2()        -> SetupInteractor ( iren );
371
372         //////////////
373         //RaC 04-2010 Look for the description of new changes in vtkInteractorStyleBaseView2D constructor.
374         //
375         // Previous version
376         // SetInteractorStyleImage( vtkInteractorStyleBaseView2D::New() );
377         //
378         // Actual version
379         vtkInteractorStyleBaseView2D *style2D = vtkInteractorStyleBaseView2D::New();
380
381         manualInteractorWindowLevel *_manualinteractorwindowlevel= new manualInteractorWindowLevel();
382         style2D->SetInteractorWindowLevel( _manualinteractorwindowlevel );
383
384         vtkInteractorScrollZ *_vtkInteractorScrollZ = new vtkInteractorScrollZ();
385         style2D->SetInteractorScrollZ(_vtkInteractorScrollZ);
386
387         SetInteractorStyleImage( style2D );
388
389         // RaC
390         //////////////
391
392         _interactorstyleplane2D = new vtkInteractorStylePlane2D();
393         ((vtkInteractorStyleBaseView*)GetInteractorStyleBaseView())->AddInteractorStyleMaracas( _interactorstyleplane2D );
394
395         vtkCamera *camera =_imageViewer2XYZ->GetVtkImageViewer2()->GetRenderer()->GetActiveCamera();
396         
397         camera->SetViewUp               (       0                       ,       1                               ,       0       );
398         camera->SetPosition             ((0+_sizeIma)/2 , (0+_sizeIma)/2        , 10000 ); 
399         
400         camera->SetFocalPoint   ((0+_sizeIma)/2 , (0+_sizeIma)/2        ,       0       ); 
401         camera->SetClippingRange(0.01, 100000);
402         camera->ComputeViewPlaneNormal();
403         camera->SetParallelScale( _sizeIma/3.0 );
404
405         // text information over the graphic window
406         _vtkInfoTextImage                                               = new vtkInfoTextImage();
407         _vtkInfoTextImageInteractorPlane2D              = new vtkInfoTextImageInteractorPlane2D();
408         _vtkInfoTextImage->SetWxVtk2DBaseView(this);
409         _vtkInfoTextImage->SetMarImageData(  GetVtkmprbasedata()->GetMarImageData() );
410         _vtkInfoTextImageInteractorPlane2D->SetModelVtkInfoTextImage(_vtkInfoTextImage);        
411         _vtkInfoTextImage->Configure();
412         ((vtkInteractorStyleBaseView*)this->GetInteractorStyleBaseView())->AddInteractorStyleMaracas(_vtkInfoTextImageInteractorPlane2D);
413 }
414
415 //-------------------------------------------------------------------
416 void vtkPlane2DView::HorizontalLine()
417 {
418 // Axe Horizontal
419         _pts = vtkPoints::New();
420         _pts->SetNumberOfPoints(2);
421         _pts->SetPoint(0, -1000 , -1000 , -1000 );
422         _pts->SetPoint(1,  1000 ,  1000 ,  1000 );
423         vtkCellArray *lines = vtkCellArray::New();
424         lines->InsertNextCell(2);
425         lines->InsertCellPoint(0);
426         lines->InsertCellPoint(1);
427         _pd = vtkPolyData::New();
428     _pd->SetPoints( _pts );
429     _pd->SetLines( lines );
430         lines->Delete();  //do not delete lines ??
431         _lineActor                                      =       vtkActor::New();
432     _lineMapper                                 =       vtkPolyDataMapper::New();
433         _lineMapper->SetInput(_pd);
434         _lineMapper->ImmediateModeRenderingOn();
435         _lineActor->SetMapper(_lineMapper);
436         _lineActor->GetProperty()->BackfaceCullingOn();
437         _lineActor->GetProperty()->SetDiffuseColor(0,0,1);
438         _lineActor->GetProperty()->SetLineWidth(2);
439         _lineActor->GetProperty()->SetOpacity(0);
440     _imageViewer2XYZ->GetVtkImageViewer2()->GetRenderer()->AddActor( _lineActor );
441 }
442
443 //-------------------------------------------------------------------
444 void vtkPlane2DView::RotationStart()
445 {
446         vtkMPRBaseData  *mprbasedata = (vtkMPRBaseData*)this->GetVtkBaseData();
447         vtkTransform    *transform       = mprbasedata->GetTransformOrientation();
448         _transform1->SetMatrix( transform->GetMatrix() );
449 }
450 //-------------------------------------------------------------------
451 void vtkPlane2DView::RotationDrag(double vx, double vy, bool ok_v, bool ok_ang)
452
453         if (ok_ang==false)
454         {
455                 _ang =sqrt( vx*vx + vy*vy ) / 1.5;
456         }
457
458         if (ok_v==false){
459                 _vxb=-vy;
460                 _vyb=vx;
461         }
462         _transform2->Identity();
463         _transform2->RotateWXYZ(_ang,0,_vxb,_vyb);
464
465         vtkMPRBaseData  *mprbasedata = (vtkMPRBaseData*)this->GetVtkBaseData();
466         vtkTransform *transform          =      vtkTransform::New();
467         transform->Identity();
468         transform->Concatenate(_transform1);
469         transform->Concatenate(_transform2);
470         mprbasedata->InitTransformOrientation(transform);
471         transform->Delete();
472
473         // Refresh Horizontal Line
474         _pts->SetPoint( 0 , (_sizeIma/2) - _vxb*2       , (_sizeIma/2) - _vyb*2 , 1 );
475         _pts->SetPoint( 1 , (_sizeIma/2) + _vxb*2       , (_sizeIma/2) + _vyb*2 , 1 );
476 //      RefreshCircleLine();
477 }
478
479 //-------------------------------------------------------------------
480 void vtkPlane2DView::Refresh(  )
481 {
482         ExtractPlane();
483         wxVtkBaseView::Refresh();
484 }
485 //-------------------------------------------------------------------
486 void vtkPlane2DView::SetImgSize( int imgSize )
487 {
488         _sizeIma = imgSize;
489 }
490 //-------------------------------------------------------------------
491 int vtkPlane2DView::GetImgSize()
492 {
493         return _sizeIma;
494 }
495 //-------------------------------------------------------------------
496 int     vtkPlane2DView::GetActualSlice()  // virtual 
497 {
498         _cx = GetVtkmprbasedata()->GetX();
499         _cy = GetVtkmprbasedata()->GetY();
500         _cz = GetVtkmprbasedata()->GetZ();
501         return 0;
502 }
503
504 //-------------------------------------------------------------------
505
506 bool vtkPlane2DView::GetMipVisualization()
507 {
508         return _mip_visualization;
509 }
510
511 //-------------------------------------------------------------------
512 int vtkPlane2DView::GetMipWidth()
513 {
514         return _mip_width;
515 }
516
517 //-------------------------------------------------------------------
518 void vtkPlane2DView::SetActualSlice(int slice)  // Virtual
519 {
520         double dir=(double)slice/3.0;
521         GetVtkmprbasedata()->SetX( _cx + (_n[0]*dir) );
522         GetVtkmprbasedata()->SetY( _cy + (_n[1]*dir) );
523         GetVtkmprbasedata()->SetZ( _cz + (_n[2]*dir) );
524 }
525 //---------------------------------------------------------------------------
526 vtkInteractorStylePlane2D       *vtkPlane2DView::GetInteractorstyleplane2D()
527 {
528         return this->_interactorstyleplane2D;
529 }
530 //---------------------------------------------------------------------------
531 void vtkPlane2DView::SetActive(bool active)
532 {
533         _active = active;
534 }
535
536 //---------------------------------------------------------------------------
537
538 void vtkPlane2DView::SetMipVisualization(bool ok)
539 {
540         _mip_visualization=ok;
541 }
542
543 //---------------------------------------------------------------------------
544
545 void vtkPlane2DView::SetMipWidth(int value)
546 {
547         _mip_width=value;
548 }
549
550 //---------------------------------------------------------------------------
551 void vtkPlane2DView::SetVisibleLine(bool ok)
552 {
553         double opacity;
554         if (ok==true)
555         {
556                 opacity=1;
557         } else {
558                 opacity=0;
559         }
560         _lineActor->GetProperty()->SetOpacity(opacity);
561 }
562 // ----------------------------------------------------------------------------
563 void vtkPlane2DView::TransfromCoordViewWorld2(double &X, double &Y, double &Z)
564 {
565         double spc[3];
566         GetVtkmprbasedata()->GetImageData()->GetSpacing(spc);
567
568         double xx = X;
569         double yy = Y;
570         double zz = 0;
571
572         TransFromCoordScreenToWorld(xx,yy,zz);
573
574
575         vtkTransform *transf1 = vtkTransform::New();
576         transf1->Identity();
577         vtkTransform *transf2 = GetVtkmprbasedata()->GetTransformOrientation();
578         transf1->Concatenate(transf2->GetMatrix());
579         double in[4], out[4];
580         double center = GetImgSize() / 2;
581         in[0] = 0;
582         in[1] = xx - center;
583         in[2] = yy - center;
584         in[3] = 0;
585
586         transf1->MultiplyPoint(in,out);
587         transf1->Delete();
588
589         X = out[0] + GetVtkmprbasedata()->GetX() ;
590         Y = out[1] + GetVtkmprbasedata()->GetY() ;
591         Z = out[2] + GetVtkmprbasedata()->GetZ() ;
592 }
593