1 #include "vtkPlane2DView.h"
3 #include "vtkCellArray.h"
5 #include "vtkInteractorStyleBaseView2D.h"
6 #include "vtkPointData.h"
9 vtkPlane2DView::vtkPlane2DView( wxWindow *parent)
10 : wxVtk2DBaseView(parent)
17 _backOrient[0] = -99999;
18 _backOrient[1] = -99999;
19 _backOrient[2] = -99999;
20 _backOrient[3] = -99999;
23 _mip_visualization = true;
26 _transform1 = vtkTransform::New();
27 _transform2 = vtkTransform::New();
28 _transform1->Identity();
29 _transform2->Identity();
45 _interactorstyleplane2D = false;
47 //-------------------------------------------------------------------
48 vtkPlane2DView::~vtkPlane2DView()
53 if (_pts != NULL) { _pts -> Delete(); }
54 if (_lineActor != NULL) { _lineActor -> Delete(); }
55 if (_lineMapper != NULL) { _lineMapper -> Delete(); }
56 if (_pd != NULL) { _pd -> Delete(); }
58 _transform1 -> Delete();
59 _transform2 -> Delete();
62 //-------------------------------------------------------------------
63 void vtkPlane2DView::ResetBack()
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(); }
81 //-------------------------------------------------------------------
82 vtkMPRBaseData *vtkPlane2DView::GetVtkmprbasedata()
84 return (vtkMPRBaseData*)GetVtkBaseData();
87 //-------------------------------------------------------------------
88 void vtkPlane2DView::SetPSource(int sizeIma){
91 double x = GetVtkmprbasedata()->GetX();
92 double y = GetVtkmprbasedata()->GetY();
93 double z = GetVtkmprbasedata()->GetZ();
96 vtkImageData *imagedata = GetVtkmprbasedata()->GetImageData();
97 imagedata->GetSpacing(spc);
103 vtkMPRBaseData *mprbasedata = (vtkMPRBaseData*)this->GetVtkBaseData();
104 vtkTransform *transform = mprbasedata->GetTransformOrientation();
106 double in[3]; // temp
109 in[0]=1; in[1]=0; in[2]=0;
110 transform->TransformPoint(in,_n);
112 in[0]=0; in[1]=dimIma-1; in[2] = 0;
113 transform->TransformPoint(in,pA);
115 in[0]=0; in[1]=0; in[2]=dimIma-1;
116 transform->TransformPoint(in,pB);
118 _pSource -> SetPoint1( pA );
119 _pSource -> SetPoint2( pB );
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( );
129 // transform->Delete();
133 //-------------------------------------------------------------------
134 void vtkPlane2DView::ExtractPlane()
137 double x = GetVtkmprbasedata()->GetX();
138 double y = GetVtkmprbasedata()->GetY();
139 double z = GetVtkmprbasedata()->GetZ();
142 vtkImageData *imagedata = GetVtkmprbasedata()->GetImageData();
143 imagedata->GetSpacing(spc);
148 vtkTransform *transform = GetVtkmprbasedata()->GetTransformOrientation();
149 double orientation[4];
150 transform->GetOrientationWXYZ(orientation);
152 bool okOrientation=true;
153 if ((orientation[0]!=_backOrient[0]) || (orientation[1]!=_backOrient[1]) ||
154 (orientation[2]!=_backOrient[2]) || (orientation[3]!=_backOrient[3]))
159 bool okPosicion=true;
160 if ( (x!=_backX) || (y!=_backY) || (z!=_backZ) )
165 if ((okPosicion==false) || (okOrientation==false) ) {
170 if ( _mip_visualization==true )
172 if (_interactorstyleplane2D!=NULL)
174 if (_interactorstyleplane2D->GetStateRotate()==false )
184 Extract_MIP_PlaneVTK();
186 Extract_One_PlaneVTK();
193 _backOrient[0]=orientation[0];
194 _backOrient[1]=orientation[1];
195 _backOrient[2]=orientation[2];
196 _backOrient[3]=orientation[3];
197 } //okPosition okOrientation
200 //-------------------------------------------------------------------
202 void vtkPlane2DView::Extract_One_PlaneVTK()
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();
221 //-------------------------------------------------------------------
223 void vtkPlane2DView::Extract_MIP_PlaneVTK( /*double heightDefinition*/ )
228 int sizeWidth = (_mip_width*2)+1 ;
230 int iWidth,itmp,tmpSizeWith;
233 vtkImageData *imagedata = GetVtkmprbasedata()->GetImageData();
234 SetPSource(_sizeIma);
235 imagedata->GetSpacing(spc);
237 bool heightDefinition=false;
240 heightDefinition=true;
243 if (heightDefinition==true)
245 mipWidth = _mip_width;
250 tmpSizeWith = (mipWidth*2) + 1;
251 sp = (spc[0]*sizeWidth)/tmpSizeWith;
252 sizeWidth = tmpSizeWith;
258 std::vector< vtkProbeFilter* > slicesLST;
260 _pSource->Push( -mipWidth * sp );
263 for ( iWidth=0 ; iWidth<sizeWidth ; iWidth++ )
265 vtkProbeFilter *slice = vtkProbeFilter::New();
266 slice -> SetInput( ( vtkDataSet* )_pSource->GetOutput( ) );
267 slice -> SetSource( imagedata );
269 slicesLST.push_back( slice );
270 _pSource->Push( sp );
273 if (_imageResult ==NULL)
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();
285 unsigned short *pTemp;
286 unsigned short *pResult;
287 pResult = (unsigned short*)_imageResult->GetScalarPointer( 0 , 0 , 0 );
289 int iPixels , sizePixels = _sizeIma*_sizeIma;
290 for(iPixels=0 ; iPixels<sizePixels ; iPixels=iPixels+deltaPixel)
293 pTemp = (unsigned short*)slicesLST[0]->GetOutput()->GetPointData()->GetScalars()->GetVoidPointer(0);
295 pResult = (unsigned short*)_imageResult->GetScalarPointer( 0 , 0 , 0 );
296 pResult[iPixels] = pTemp[iPixels];
298 for (iWidth=1;iWidth<sizeWidth;iWidth++)
300 pTemp = (unsigned short*)slicesLST[iWidth]->GetOutput()->GetPointData()->GetScalars()->GetVoidPointer(0);
302 if (pResult[iPixels]< pTemp[iPixels])
304 pResult[iPixels] = pTemp[iPixels];
310 for (itmp=1;itmp<deltaPixel;itmp++)
312 pResult[iPixels+itmp] = pResult[iPixels];
318 for (iWidth=0;iWidth<sizeWidth;iWidth++)
320 slicesLST[iWidth]->Delete();
323 _imageResult->Modified();
324 _imageViewer2XYZ->GetVtkImageViewer2()->SetInput ( _imageResult );
328 //-------------------------------------------------------------------
329 void vtkPlane2DView::Configure( )
331 wxVtk2DBaseView::Configure(false);
337 _pSource = vtkPlaneSource::New( );
338 _3Dslices = vtkProbeFilter::New( ) ;
339 _stPoints = vtkStructuredPoints::New( );
340 _change = vtkImageChangeInformation::New();
342 wxVTKRenderWindowInteractor *iren = GetWxVTKRenderWindowInteractor();
345 _imageViewer2XYZ -> GetVtkImageViewer2() -> SetupInteractor ( iren );
347 SetInteractorStyleImage( vtkInteractorStyleBaseView2D::New() );
349 _interactorstyleplane2D = new vtkInteractorStylePlane2D();
350 ((vtkInteractorStyleBaseView*)GetInteractorStyleBaseView())->AddInteractorStyleMaracas( _interactorstyleplane2D );
352 vtkCamera *camera =_imageViewer2XYZ->GetVtkImageViewer2()->GetRenderer()->GetActiveCamera();
354 camera->SetViewUp ( 0 , 1 , 0 );
355 camera->SetPosition ((0+_sizeIma)/2 , (0+_sizeIma)/2 , 10000 );
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 );
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);
374 //-------------------------------------------------------------------
375 void vtkPlane2DView::HorizontalLine()
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 );
402 //-------------------------------------------------------------------
403 void vtkPlane2DView::RotationStart()
405 vtkMPRBaseData *mprbasedata = (vtkMPRBaseData*)this->GetVtkBaseData();
406 vtkTransform *transform = mprbasedata->GetTransformOrientation();
407 _transform1->SetMatrix( transform->GetMatrix() );
409 //-------------------------------------------------------------------
410 void vtkPlane2DView::RotationDrag(double vx, double vy, bool ok_v, bool ok_ang)
414 _ang =sqrt( vx*vx + vy*vy ) / 1.5;
421 _transform2->Identity();
422 _transform2->RotateWXYZ(_ang,0,_vxb,_vyb);
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);
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();
438 //-------------------------------------------------------------------
439 void vtkPlane2DView::Refresh( )
442 wxVtkBaseView::Refresh();
444 //-------------------------------------------------------------------
445 void vtkPlane2DView::SetImgSize( int imgSize )
449 //-------------------------------------------------------------------
450 int vtkPlane2DView::GetImgSize()
454 //-------------------------------------------------------------------
455 int vtkPlane2DView::GetActualSlice() // virtual
457 _cx = GetVtkmprbasedata()->GetX();
458 _cy = GetVtkmprbasedata()->GetY();
459 _cz = GetVtkmprbasedata()->GetZ();
463 //-------------------------------------------------------------------
465 bool vtkPlane2DView::GetMipVisualization()
467 return _mip_visualization;
470 //-------------------------------------------------------------------
471 int vtkPlane2DView::GetMipWidth()
476 //-------------------------------------------------------------------
477 void vtkPlane2DView::SetActualSlice(int slice) // Virtual
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) );
484 //---------------------------------------------------------------------------
485 vtkInteractorStylePlane2D *vtkPlane2DView::GetInteractorstyleplane2D()
487 return this->_interactorstyleplane2D;
489 //---------------------------------------------------------------------------
490 void vtkPlane2DView::SetActive(bool active)
495 //---------------------------------------------------------------------------
497 void vtkPlane2DView::SetMipVisualization(bool ok)
499 _mip_visualization=ok;
502 //---------------------------------------------------------------------------
504 void vtkPlane2DView::SetMipWidth(int value)
509 //---------------------------------------------------------------------------
510 void vtkPlane2DView::SetVisibleLine(bool ok)
519 _lineActor->GetProperty()->SetOpacity(opacity);
521 // ----------------------------------------------------------------------------
522 void vtkPlane2DView::TransfromeCoordViewWorld2(double &X, double &Y, double &Z)
525 GetVtkmprbasedata()->GetImageData()->GetSpacing(spc);
531 TransfromeCoordScreenToWorld(xx,yy,zz);
534 vtkTransform *transf1 = vtkTransform::New();
536 vtkTransform *transf2 = GetVtkmprbasedata()->GetTransformOrientation();
537 transf1->Concatenate(transf2->GetMatrix());
538 double in[4], out[4];
539 double center = GetImgSize() / 2;
545 transf1->MultiplyPoint(in,out);
548 X = out[0] + GetVtkmprbasedata()->GetX() ;
549 Y = out[1] + GetVtkmprbasedata()->GetY() ;
550 Z = out[2] + GetVtkmprbasedata()->GetZ() ;