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 = NULL;
72 //-------------------------------------------------------------------
73 vtkPlane2DView::~vtkPlane2DView()
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();
85 //-------------------------------------------------------------------
86 void vtkPlane2DView::ResetBack()
97 //-------------------------------------------------------------------
98 void vtkPlane2DView::ResetPlane()
100 if (_pSource !=NULL) { _pSource -> Delete(); }
101 if (_3Dslices !=NULL) { _3Dslices -> Delete(); }
102 if (_stPoints !=NULL) { _stPoints -> Delete(); }
103 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()
161 double x = GetVtkmprbasedata()->GetX();
162 double y = GetVtkmprbasedata()->GetY();
163 double z = GetVtkmprbasedata()->GetZ();
166 vtkImageData *imagedata = GetVtkmprbasedata()->GetImageData();
167 imagedata->GetSpacing(spc);
172 vtkTransform *transform = GetVtkmprbasedata()->GetTransformOrientation();
173 double orientation[4];
174 transform->GetOrientationWXYZ(orientation);
176 bool okOrientation=true;
177 if ((orientation[0]!=_backOrient[0]) || (orientation[1]!=_backOrient[1]) ||
178 (orientation[2]!=_backOrient[2]) || (orientation[3]!=_backOrient[3]))
183 bool okPosicion=true;
184 if ( (x!=_backX) || (y!=_backY) || (z!=_backZ) )
189 if ((okPosicion==false) || (okOrientation==false) ) {
194 if ( _mip_visualization==true )
196 if (_interactorstyleplane2D!=NULL)
198 if (_interactorstyleplane2D->GetStateRotate()==false )
208 Extract_MIP_PlaneVTK();
210 Extract_One_PlaneVTK();
217 _backOrient[0]=orientation[0];
218 _backOrient[1]=orientation[1];
219 _backOrient[2]=orientation[2];
220 _backOrient[3]=orientation[3];
221 } //okPosition okOrientation
224 //-------------------------------------------------------------------
225 void vtkPlane2DView::Extract_One_PlaneVTK()
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();
244 //-------------------------------------------------------------------
245 void vtkPlane2DView::Extract_MIP_PlaneVTK( /*double heightDefinition*/ )
249 int sizeWidth = (_mip_width*2)+1 ;
251 int iWidth,itmp,tmpSizeWith;
253 vtkImageData *imagedata = GetVtkmprbasedata()->GetImageData();
254 SetPSource(_sizeIma);
255 imagedata->GetSpacing(spc);
256 bool heightDefinition=false;
259 heightDefinition=true;
261 if (heightDefinition==true)
263 mipWidth = _mip_width;
268 tmpSizeWith = (mipWidth*2) + 1;
269 sp = (spc[0]*sizeWidth)/tmpSizeWith;
270 sizeWidth = tmpSizeWith;
273 std::vector< vtkProbeFilter* > slicesLST;
274 _pSource->Push( -mipWidth * sp );
276 for ( iWidth=0 ; iWidth<sizeWidth ; iWidth++ )
278 vtkProbeFilter *slice = vtkProbeFilter::New();
279 slice -> SetInput( ( vtkDataSet* )_pSource->GetOutput( ) );
280 slice -> SetSource( imagedata );
282 slicesLST.push_back( slice );
283 _pSource->Push( sp );
285 if (_imageResult ==NULL)
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();
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)
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++)
307 pTemp = (unsigned short*)slicesLST[iWidth]->GetOutput()->GetPointData()->GetScalars()->GetVoidPointer(0);
309 if (pResult[iPixels]< pTemp[iPixels])
311 pResult[iPixels] = pTemp[iPixels];
316 for (itmp=1;itmp<deltaPixel;itmp++)
318 pResult[iPixels+itmp] = pResult[iPixels];
322 for (iWidth=0;iWidth<sizeWidth;iWidth++)
324 slicesLST[iWidth]->Delete();
326 _imageResult->Modified();
327 _imageViewer2XYZ->GetVtkImageViewer2()->SetInput ( _imageResult );
330 //-------------------------------------------------------------------
331 void vtkPlane2DView::Configure( )
333 wxVtk2DBaseView::Configure(false);
339 _pSource = vtkPlaneSource::New( );
340 _3Dslices = vtkProbeFilter::New( ) ;
341 _stPoints = vtkStructuredPoints::New( );
342 _change = vtkImageChangeInformation::New();
344 wxVTKRenderWindowInteractor *iren = GetWxVTKRenderWindowInteractor();
347 _imageViewer2XYZ -> GetVtkImageViewer2() -> SetupInteractor ( iren );
350 //RaC 04-2010 Look for the description of new changes in vtkInteractorStyleBaseView2D constructor.
353 // SetInteractorStyleImage( vtkInteractorStyleBaseView2D::New() );
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 );
366 _interactorstyleplane2D = new vtkInteractorStylePlane2D();
367 ((vtkInteractorStyleBaseView*)GetInteractorStyleBaseView())->AddInteractorStyleMaracas( _interactorstyleplane2D );
369 vtkCamera *camera =_imageViewer2XYZ->GetVtkImageViewer2()->GetRenderer()->GetActiveCamera();
371 camera->SetViewUp ( 0 , 1 , 0 );
372 camera->SetPosition ((0+_sizeIma)/2 , (0+_sizeIma)/2 , 10000 );
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 );
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);
389 //-------------------------------------------------------------------
390 void vtkPlane2DView::HorizontalLine()
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 );
417 //-------------------------------------------------------------------
418 void vtkPlane2DView::RotationStart()
420 vtkMPRBaseData *mprbasedata = (vtkMPRBaseData*)this->GetVtkBaseData();
421 vtkTransform *transform = mprbasedata->GetTransformOrientation();
422 _transform1->SetMatrix( transform->GetMatrix() );
425 //-------------------------------------------------------------------
426 void vtkPlane2DView::RotationDrag(double vx, double vy, bool ok_v, bool ok_ang)
430 _ang =sqrt( vx*vx + vy*vy ) / 1.5;
437 _transform2->Identity();
438 _transform2->RotateWXYZ(_ang,0,_vxb,_vyb);
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);
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();
454 //-------------------------------------------------------------------
455 void vtkPlane2DView::Refresh( )
458 UpdateColorWindowLevel();
459 wxVtkBaseView::Refresh();
461 //-------------------------------------------------------------------
462 void vtkPlane2DView::SetImgSize( int imgSize )
466 //-------------------------------------------------------------------
467 int vtkPlane2DView::GetImgSize()
471 //-------------------------------------------------------------------
472 int vtkPlane2DView::GetActualSlice() // virtual
474 _cx = GetVtkmprbasedata()->GetX();
475 _cy = GetVtkmprbasedata()->GetY();
476 _cz = GetVtkmprbasedata()->GetZ();
480 //-------------------------------------------------------------------
482 bool vtkPlane2DView::GetMipVisualization()
484 return _mip_visualization;
487 //-------------------------------------------------------------------
488 int vtkPlane2DView::GetMipWidth()
493 //-------------------------------------------------------------------
494 void vtkPlane2DView::SetActualSlice(int slice) // Virtual
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) );
501 //---------------------------------------------------------------------------
502 vtkInteractorStylePlane2D *vtkPlane2DView::GetInteractorstyleplane2D()
504 return this->_interactorstyleplane2D;
506 //---------------------------------------------------------------------------
507 void vtkPlane2DView::SetActive(bool active)
512 //---------------------------------------------------------------------------
514 void vtkPlane2DView::SetMipVisualization(bool ok)
516 _mip_visualization=ok;
519 //---------------------------------------------------------------------------
521 void vtkPlane2DView::SetMipWidth(int value)
526 //---------------------------------------------------------------------------
527 void vtkPlane2DView::SetVisibleLine(bool ok)
536 _lineActor->GetProperty()->SetOpacity(opacity);
539 // ----------------------------------------------------------------------------
540 void vtkPlane2DView::TransfromCoordViewWorld2(double &X, double &Y, double &Z)
543 GetVtkmprbasedata()->GetImageData()->GetSpacing(spc);
547 TransFromCoordScreenToWorld(xx,yy,zz);
548 vtkTransform *transf1 = vtkTransform::New();
550 vtkTransform *transf2 = GetVtkmprbasedata()->GetTransformOrientation();
551 transf1->Concatenate(transf2->GetMatrix());
552 double in[4], out[4];
553 double center = GetImgSize() / 2;
558 transf1->MultiplyPoint(in,out);
560 X = out[0] + GetVtkmprbasedata()->GetX() ;
561 Y = out[1] + GetVtkmprbasedata()->GetY() ;
562 Z = out[2] + GetVtkmprbasedata()->GetZ() ;