]> Creatis software - creaMaracasVisu.git/blob - lib/maracasVisuLib/src/interface/wxWindows/widgets/vtkPlane2DView.cxx
.
[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         SetInteractorStyleImage( vtkInteractorStyleBaseView2D::New() );
348
349         _interactorstyleplane2D = new vtkInteractorStylePlane2D();
350         ((vtkInteractorStyleBaseView*)GetInteractorStyleBaseView())->AddInteractorStyleMaracas( _interactorstyleplane2D );
351
352         vtkCamera *camera =_imageViewer2XYZ->GetVtkImageViewer2()->GetRenderer()->GetActiveCamera();
353         
354         camera->SetViewUp               (       0                       ,       1                               ,       0       );
355         camera->SetPosition             ((0+_sizeIma)/2 , (0+_sizeIma)/2        , 10000 ); 
356         
357         camera->SetFocalPoint   ((0+_sizeIma)/2 , (0+_sizeIma)/2        ,       0       ); 
358         camera->SetClippingRange(0.01, 100000);
359         camera->ComputeViewPlaneNormal();
360         camera->SetParallelScale( _sizeIma/3.0 );
361
362         // text information over the graphic window
363         _vtkInfoTextImage                                               = new vtkInfoTextImage();
364         _vtkInfoTextImageInteractorPlane2D              = new vtkInfoTextImageInteractorPlane2D();
365         _vtkInfoTextImage->SetWxVtk2DBaseView(this);
366         _vtkInfoTextImage->SetMarImageData(  GetVtkmprbasedata()->GetMarImageData() );
367         _vtkInfoTextImageInteractorPlane2D->SetModelVtkInfoTextImage(_vtkInfoTextImage);        
368         _vtkInfoTextImage->Configure();
369         ((vtkInteractorStyleBaseView*)this->GetInteractorStyleBaseView())->AddInteractorStyleMaracas(_vtkInfoTextImageInteractorPlane2D);
370
371
372 }
373
374 //-------------------------------------------------------------------
375 void vtkPlane2DView::HorizontalLine()
376 {
377 // Axe Horizontal
378         _pts = vtkPoints::New();
379         _pts->SetNumberOfPoints(2);
380         _pts->SetPoint(0, -1000 , -1000 , -1000 );
381         _pts->SetPoint(1,  1000 ,  1000 ,  1000 );
382         vtkCellArray *lines = vtkCellArray::New();
383         lines->InsertNextCell(2);
384         lines->InsertCellPoint(0);
385         lines->InsertCellPoint(1);
386         _pd = vtkPolyData::New();
387     _pd->SetPoints( _pts );
388     _pd->SetLines( lines );
389         lines->Delete();  //do not delete lines ??
390         _lineActor                                      =       vtkActor::New();
391     _lineMapper                                 =       vtkPolyDataMapper::New();
392         _lineMapper->SetInput(_pd);
393         _lineMapper->ImmediateModeRenderingOn();
394         _lineActor->SetMapper(_lineMapper);
395         _lineActor->GetProperty()->BackfaceCullingOn();
396         _lineActor->GetProperty()->SetDiffuseColor(0,0,1);
397         _lineActor->GetProperty()->SetLineWidth(2);
398         _lineActor->GetProperty()->SetOpacity(0);
399     _imageViewer2XYZ->GetVtkImageViewer2()->GetRenderer()->AddActor( _lineActor );
400 }
401
402 //-------------------------------------------------------------------
403 void vtkPlane2DView::RotationStart()
404 {
405         vtkMPRBaseData  *mprbasedata = (vtkMPRBaseData*)this->GetVtkBaseData();
406         vtkTransform    *transform       = mprbasedata->GetTransformOrientation();
407         _transform1->SetMatrix( transform->GetMatrix() );
408 }
409 //-------------------------------------------------------------------
410 void vtkPlane2DView::RotationDrag(double vx, double vy, bool ok_v, bool ok_ang)
411
412         if (ok_ang==false)
413         {
414                 _ang =sqrt( vx*vx + vy*vy ) / 1.5;
415         }
416
417         if (ok_v==false){
418                 _vxb=-vy;
419                 _vyb=vx;
420         }
421         _transform2->Identity();
422         _transform2->RotateWXYZ(_ang,0,_vxb,_vyb);
423
424         vtkMPRBaseData  *mprbasedata = (vtkMPRBaseData*)this->GetVtkBaseData();
425         vtkTransform *transform          =      vtkTransform::New();
426         transform->Identity();
427         transform->Concatenate(_transform1);
428         transform->Concatenate(_transform2);
429         mprbasedata->InitTransformOrientation(transform);
430         transform->Delete();
431
432         // Refresh Horizontal Line
433         _pts->SetPoint( 0 , (_sizeIma/2) - _vxb*2       , (_sizeIma/2) - _vyb*2 , 1 );
434         _pts->SetPoint( 1 , (_sizeIma/2) + _vxb*2       , (_sizeIma/2) + _vyb*2 , 1 );
435 //      RefreshCircleLine();
436 }
437
438 //-------------------------------------------------------------------
439 void vtkPlane2DView::Refresh(  )
440 {
441         ExtractPlane();
442         wxVtkBaseView::Refresh();
443 }
444 //-------------------------------------------------------------------
445 void vtkPlane2DView::SetImgSize( int imgSize )
446 {
447         _sizeIma = imgSize;
448 }
449 //-------------------------------------------------------------------
450 int vtkPlane2DView::GetImgSize()
451 {
452         return _sizeIma;
453 }
454 //-------------------------------------------------------------------
455 int     vtkPlane2DView::GetActualSlice()  // virtual 
456 {
457         _cx = GetVtkmprbasedata()->GetX();
458         _cy = GetVtkmprbasedata()->GetY();
459         _cz = GetVtkmprbasedata()->GetZ();
460         return 0;
461 }
462
463 //-------------------------------------------------------------------
464
465 bool vtkPlane2DView::GetMipVisualization()
466 {
467         return _mip_visualization;
468 }
469
470 //-------------------------------------------------------------------
471 int vtkPlane2DView::GetMipWidth()
472 {
473         return _mip_width;
474 }
475
476 //-------------------------------------------------------------------
477 void vtkPlane2DView::SetActualSlice(int slice)  // Virtual
478 {
479         double dir=(double)slice/3.0;
480         GetVtkmprbasedata()->SetX( _cx + (_n[0]*dir) );
481         GetVtkmprbasedata()->SetY( _cy + (_n[1]*dir) );
482         GetVtkmprbasedata()->SetZ( _cz + (_n[2]*dir) );
483 }
484 //---------------------------------------------------------------------------
485 vtkInteractorStylePlane2D       *vtkPlane2DView::GetInteractorstyleplane2D()
486 {
487         return this->_interactorstyleplane2D;
488 }
489 //---------------------------------------------------------------------------
490 void vtkPlane2DView::SetActive(bool active)
491 {
492         _active = active;
493 }
494
495 //---------------------------------------------------------------------------
496
497 void vtkPlane2DView::SetMipVisualization(bool ok)
498 {
499         _mip_visualization=ok;
500 }
501
502 //---------------------------------------------------------------------------
503
504 void vtkPlane2DView::SetMipWidth(int value)
505 {
506         _mip_width=value;
507 }
508
509 //---------------------------------------------------------------------------
510 void vtkPlane2DView::SetVisibleLine(bool ok)
511 {
512         double opacity;
513         if (ok==true)
514         {
515                 opacity=1;
516         } else {
517                 opacity=0;
518         }
519         _lineActor->GetProperty()->SetOpacity(opacity);
520 }
521 // ----------------------------------------------------------------------------
522 void vtkPlane2DView::TransfromeCoordViewWorld2(double &X, double &Y, double &Z)
523 {
524         double spc[3];
525         GetVtkmprbasedata()->GetImageData()->GetSpacing(spc);
526
527         double xx = X;
528         double yy = Y;
529         double zz = 0;
530
531         TransFromeCoordScreenToWorld(xx,yy,zz);
532
533
534         vtkTransform *transf1 = vtkTransform::New();
535         transf1->Identity();
536         vtkTransform *transf2 = GetVtkmprbasedata()->GetTransformOrientation();
537         transf1->Concatenate(transf2->GetMatrix());
538         double in[4], out[4];
539         double center = GetImgSize() / 2;
540         in[0] = 0;
541         in[1] = xx - center;
542         in[2] = yy - center;
543         in[3] = 0;
544
545         transf1->MultiplyPoint(in,out);
546         transf1->Delete();
547
548         X = out[0] + GetVtkmprbasedata()->GetX() ;
549         Y = out[1] + GetVtkmprbasedata()->GetY() ;
550         Z = out[2] + GetVtkmprbasedata()->GetZ() ;
551
552 }
553