1 /*# ---------------------------------------------------------------------
3 # Copyright (c) CREATIS (Centre de Recherche en Acquisition et Traitement de l'Image
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
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.
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
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 # ------------------------------------------------------------------------ */
26 #include "vtkPlane2DView.h"
28 #include "vtkCellArray.h"
30 #include "vtkInteractorStyleBaseView2D.h"
31 #include "vtkPointData.h"
34 vtkPlane2DView::vtkPlane2DView( wxWindow *parent)
35 : wxVtk2DBaseView(parent)
42 _backOrient[0] = -99999;
43 _backOrient[1] = -99999;
44 _backOrient[2] = -99999;
45 _backOrient[3] = -99999;
48 _mip_visualization = true;
51 _transform1 = vtkTransform::New();
52 _transform2 = vtkTransform::New();
53 _transform1->Identity();
54 _transform2->Identity();
70 _interactorstyleplane2D = false;
72 //-------------------------------------------------------------------
73 vtkPlane2DView::~vtkPlane2DView()
78 if (_pts != NULL) { _pts -> Delete(); }
79 if (_lineActor != NULL) { _lineActor -> Delete(); }
80 if (_lineMapper != NULL) { _lineMapper -> Delete(); }
81 if (_pd != NULL) { _pd -> Delete(); }
83 _transform1 -> Delete();
84 _transform2 -> Delete();
87 //-------------------------------------------------------------------
88 void vtkPlane2DView::ResetBack()
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(); }
106 //-------------------------------------------------------------------
107 vtkMPRBaseData *vtkPlane2DView::GetVtkmprbasedata()
109 return (vtkMPRBaseData*)GetVtkBaseData();
112 //-------------------------------------------------------------------
113 void vtkPlane2DView::SetPSource(int sizeIma){
114 int dimIma = sizeIma;
116 double x = GetVtkmprbasedata()->GetX();
117 double y = GetVtkmprbasedata()->GetY();
118 double z = GetVtkmprbasedata()->GetZ();
121 vtkImageData *imagedata = GetVtkmprbasedata()->GetImageData();
122 imagedata->GetSpacing(spc);
128 vtkMPRBaseData *mprbasedata = (vtkMPRBaseData*)this->GetVtkBaseData();
129 vtkTransform *transform = mprbasedata->GetTransformOrientation();
131 double in[3]; // temp
134 in[0]=1; in[1]=0; in[2]=0;
135 transform->TransformPoint(in,_n);
137 in[0]=0; in[1]=dimIma-1; in[2] = 0;
138 transform->TransformPoint(in,pA);
140 in[0]=0; in[1]=0; in[2]=dimIma-1;
141 transform->TransformPoint(in,pB);
143 _pSource -> SetPoint1( pA );
144 _pSource -> SetPoint2( pB );
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( );
154 // transform->Delete();
158 //-------------------------------------------------------------------
159 void vtkPlane2DView::ExtractPlane()
162 double x = GetVtkmprbasedata()->GetX();
163 double y = GetVtkmprbasedata()->GetY();
164 double z = GetVtkmprbasedata()->GetZ();
167 vtkImageData *imagedata = GetVtkmprbasedata()->GetImageData();
168 imagedata->GetSpacing(spc);
173 vtkTransform *transform = GetVtkmprbasedata()->GetTransformOrientation();
174 double orientation[4];
175 transform->GetOrientationWXYZ(orientation);
177 bool okOrientation=true;
178 if ((orientation[0]!=_backOrient[0]) || (orientation[1]!=_backOrient[1]) ||
179 (orientation[2]!=_backOrient[2]) || (orientation[3]!=_backOrient[3]))
184 bool okPosicion=true;
185 if ( (x!=_backX) || (y!=_backY) || (z!=_backZ) )
190 if ((okPosicion==false) || (okOrientation==false) ) {
195 if ( _mip_visualization==true )
197 if (_interactorstyleplane2D!=NULL)
199 if (_interactorstyleplane2D->GetStateRotate()==false )
209 Extract_MIP_PlaneVTK();
211 Extract_One_PlaneVTK();
218 _backOrient[0]=orientation[0];
219 _backOrient[1]=orientation[1];
220 _backOrient[2]=orientation[2];
221 _backOrient[3]=orientation[3];
222 } //okPosition okOrientation
225 //-------------------------------------------------------------------
227 void vtkPlane2DView::Extract_One_PlaneVTK()
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();
246 //-------------------------------------------------------------------
248 void vtkPlane2DView::Extract_MIP_PlaneVTK( /*double heightDefinition*/ )
253 int sizeWidth = (_mip_width*2)+1 ;
255 int iWidth,itmp,tmpSizeWith;
258 vtkImageData *imagedata = GetVtkmprbasedata()->GetImageData();
259 SetPSource(_sizeIma);
260 imagedata->GetSpacing(spc);
262 bool heightDefinition=false;
265 heightDefinition=true;
268 if (heightDefinition==true)
270 mipWidth = _mip_width;
275 tmpSizeWith = (mipWidth*2) + 1;
276 sp = (spc[0]*sizeWidth)/tmpSizeWith;
277 sizeWidth = tmpSizeWith;
283 std::vector< vtkProbeFilter* > slicesLST;
285 _pSource->Push( -mipWidth * sp );
288 for ( iWidth=0 ; iWidth<sizeWidth ; iWidth++ )
290 vtkProbeFilter *slice = vtkProbeFilter::New();
291 slice -> SetInput( ( vtkDataSet* )_pSource->GetOutput( ) );
292 slice -> SetSource( imagedata );
294 slicesLST.push_back( slice );
295 _pSource->Push( sp );
298 if (_imageResult ==NULL)
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();
310 unsigned short *pTemp;
311 unsigned short *pResult;
312 pResult = (unsigned short*)_imageResult->GetScalarPointer( 0 , 0 , 0 );
314 int iPixels , sizePixels = _sizeIma*_sizeIma;
315 for(iPixels=0 ; iPixels<sizePixels ; iPixels=iPixels+deltaPixel)
318 pTemp = (unsigned short*)slicesLST[0]->GetOutput()->GetPointData()->GetScalars()->GetVoidPointer(0);
320 pResult = (unsigned short*)_imageResult->GetScalarPointer( 0 , 0 , 0 );
321 pResult[iPixels] = pTemp[iPixels];
323 for (iWidth=1;iWidth<sizeWidth;iWidth++)
325 pTemp = (unsigned short*)slicesLST[iWidth]->GetOutput()->GetPointData()->GetScalars()->GetVoidPointer(0);
327 if (pResult[iPixels]< pTemp[iPixels])
329 pResult[iPixels] = pTemp[iPixels];
335 for (itmp=1;itmp<deltaPixel;itmp++)
337 pResult[iPixels+itmp] = pResult[iPixels];
343 for (iWidth=0;iWidth<sizeWidth;iWidth++)
345 slicesLST[iWidth]->Delete();
348 _imageResult->Modified();
349 _imageViewer2XYZ->GetVtkImageViewer2()->SetInput ( _imageResult );
353 //-------------------------------------------------------------------
354 void vtkPlane2DView::Configure( )
356 wxVtk2DBaseView::Configure(false);
362 _pSource = vtkPlaneSource::New( );
363 _3Dslices = vtkProbeFilter::New( ) ;
364 _stPoints = vtkStructuredPoints::New( );
365 _change = vtkImageChangeInformation::New();
367 wxVTKRenderWindowInteractor *iren = GetWxVTKRenderWindowInteractor();
370 _imageViewer2XYZ -> GetVtkImageViewer2() -> SetupInteractor ( iren );
373 //RaC 04-2010 Look for the description of new changes in vtkInteractorStyleBaseView2D constructor.
376 // SetInteractorStyleImage( vtkInteractorStyleBaseView2D::New() );
379 vtkInteractorStyleBaseView2D *style2D = vtkInteractorStyleBaseView2D::New();
381 manualInteractorWindowLevel *_manualinteractorwindowlevel= new manualInteractorWindowLevel();
382 style2D->SetInteractorWindowLevel( _manualinteractorwindowlevel );
384 vtkInteractorScrollZ *_vtkInteractorScrollZ = new vtkInteractorScrollZ();
385 style2D->SetInteractorScrollZ(_vtkInteractorScrollZ);
387 SetInteractorStyleImage( style2D );
392 _interactorstyleplane2D = new vtkInteractorStylePlane2D();
393 ((vtkInteractorStyleBaseView*)GetInteractorStyleBaseView())->AddInteractorStyleMaracas( _interactorstyleplane2D );
395 vtkCamera *camera =_imageViewer2XYZ->GetVtkImageViewer2()->GetRenderer()->GetActiveCamera();
397 camera->SetViewUp ( 0 , 1 , 0 );
398 camera->SetPosition ((0+_sizeIma)/2 , (0+_sizeIma)/2 , 10000 );
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 );
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);
415 //-------------------------------------------------------------------
416 void vtkPlane2DView::HorizontalLine()
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 );
443 //-------------------------------------------------------------------
444 void vtkPlane2DView::RotationStart()
446 vtkMPRBaseData *mprbasedata = (vtkMPRBaseData*)this->GetVtkBaseData();
447 vtkTransform *transform = mprbasedata->GetTransformOrientation();
448 _transform1->SetMatrix( transform->GetMatrix() );
450 //-------------------------------------------------------------------
451 void vtkPlane2DView::RotationDrag(double vx, double vy, bool ok_v, bool ok_ang)
455 _ang =sqrt( vx*vx + vy*vy ) / 1.5;
462 _transform2->Identity();
463 _transform2->RotateWXYZ(_ang,0,_vxb,_vyb);
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);
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();
479 //-------------------------------------------------------------------
480 void vtkPlane2DView::Refresh( )
483 wxVtkBaseView::Refresh();
485 //-------------------------------------------------------------------
486 void vtkPlane2DView::SetImgSize( int imgSize )
490 //-------------------------------------------------------------------
491 int vtkPlane2DView::GetImgSize()
495 //-------------------------------------------------------------------
496 int vtkPlane2DView::GetActualSlice() // virtual
498 _cx = GetVtkmprbasedata()->GetX();
499 _cy = GetVtkmprbasedata()->GetY();
500 _cz = GetVtkmprbasedata()->GetZ();
504 //-------------------------------------------------------------------
506 bool vtkPlane2DView::GetMipVisualization()
508 return _mip_visualization;
511 //-------------------------------------------------------------------
512 int vtkPlane2DView::GetMipWidth()
517 //-------------------------------------------------------------------
518 void vtkPlane2DView::SetActualSlice(int slice) // Virtual
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) );
525 //---------------------------------------------------------------------------
526 vtkInteractorStylePlane2D *vtkPlane2DView::GetInteractorstyleplane2D()
528 return this->_interactorstyleplane2D;
530 //---------------------------------------------------------------------------
531 void vtkPlane2DView::SetActive(bool active)
536 //---------------------------------------------------------------------------
538 void vtkPlane2DView::SetMipVisualization(bool ok)
540 _mip_visualization=ok;
543 //---------------------------------------------------------------------------
545 void vtkPlane2DView::SetMipWidth(int value)
550 //---------------------------------------------------------------------------
551 void vtkPlane2DView::SetVisibleLine(bool ok)
560 _lineActor->GetProperty()->SetOpacity(opacity);
562 // ----------------------------------------------------------------------------
563 void vtkPlane2DView::TransfromCoordViewWorld2(double &X, double &Y, double &Z)
566 GetVtkmprbasedata()->GetImageData()->GetSpacing(spc);
572 TransFromCoordScreenToWorld(xx,yy,zz);
575 vtkTransform *transf1 = vtkTransform::New();
577 vtkTransform *transf2 = GetVtkmprbasedata()->GetTransformOrientation();
578 transf1->Concatenate(transf2->GetMatrix());
579 double in[4], out[4];
580 double center = GetImgSize() / 2;
586 transf1->MultiplyPoint(in,out);
589 X = out[0] + GetVtkmprbasedata()->GetX() ;
590 Y = out[1] + GetVtkmprbasedata()->GetY() ;
591 Z = out[2] + GetVtkmprbasedata()->GetZ() ;