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