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