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);
230 //EED 2017-01-01 Migration VTK7
231 #if VTK_MAJOR_VERSION <= 5
232 _3Dslices -> SetInput( ( vtkDataSet* )_pSource->GetOutput( ) );
233 _3Dslices -> SetSource( imagedata );
235 _3Dslices -> SetInputData( ( vtkDataSet* )_pSource->GetOutput( ) );
236 _3Dslices -> SetSourceData( imagedata );
239 _3Dslices -> Update( );
240 _stPoints -> GetPointData( )->SetScalars( _3Dslices->GetOutput()->GetPointData()->GetScalars() );
241 _stPoints -> SetDimensions( _sizeIma, _sizeIma, 1 );
243 //EED 2017-01-01 Migration VTK7
244 #if VTK_MAJOR_VERSION <= 5
245 _stPoints -> SetScalarType( imagedata->GetScalarType() );
246 _stPoints -> SetScalarTypeToShort();
247 _stPoints -> Update();
248 // _change -> SetInput( _stPoints );
249 // _change -> Update(); //important
250 _imageViewer2XYZ->GetVtkImageViewer2()->SetInput ( _stPoints );
252 printf("EED Warning! vtkPlane2DView::Extract_One_PlaneVTK VTK 5->7 ???? SetScalarTypeToShort \n");
253 printf("EED Warning! vtkPlane2DView::Extract_One_PlaneVTK VTK 5->7 ???? SetScalarTypeToShort \n");
254 printf("EED Warning! vtkPlane2DView::Extract_One_PlaneVTK VTK 5->7 ???? SetScalarTypeToShort \n");
255 printf("EED Warning! vtkPlane2DView::Extract_One_PlaneVTK VTK 5->7 ???? SetScalarTypeToShort \n");
256 printf("EED Warning! vtkPlane2DView::Extract_One_PlaneVTK VTK 5->7 ???? SetScalarTypeToShort \n");
257 vtkInformation* info=_stPoints->GetInformation();
258 vtkDataObject::SetPointDataActiveScalarInfo(info, VTK_SHORT, 1);
259 _imageViewer2XYZ->GetVtkImageViewer2()->SetInputData( _stPoints );
264 // _imageViewer2XYZ->GetVtkImageViewer2()->SetInput ( _change->GetOutput() );
265 // vtkImageActor *imageActor = _imageViewer2XYZ->GetVtkImageViewer2()->GetImageActor();
268 //-------------------------------------------------------------------
269 void vtkPlane2DView::Extract_MIP_PlaneVTK( /*double heightDefinition*/ )
273 int sizeWidth = (_mip_width*2)+1 ;
275 int iWidth,itmp,tmpSizeWith;
277 vtkImageData *imagedata = GetVtkmprbasedata()->GetImageData();
278 SetPSource(_sizeIma);
279 imagedata->GetSpacing(spc);
280 bool heightDefinition=false;
283 heightDefinition=true;
285 if (heightDefinition==true)
287 mipWidth = _mip_width;
292 tmpSizeWith = (mipWidth*2) + 1;
293 sp = (spc[0]*sizeWidth)/tmpSizeWith;
294 sizeWidth = tmpSizeWith;
297 std::vector< vtkProbeFilter* > slicesLST;
298 _pSource->Push( -mipWidth * sp );
300 for ( iWidth=0 ; iWidth<sizeWidth ; iWidth++ )
302 vtkProbeFilter *slice = vtkProbeFilter::New();
303 //EED 2017-01-01 Migration VTK7
304 #if VTK_MAJOR_VERSION <= 5
305 slice -> SetInput( ( vtkDataSet* )_pSource->GetOutput( ) );
306 slice -> SetSource( imagedata );
308 slice -> SetInputData( ( vtkDataSet* )_pSource->GetOutput( ) );
309 slice -> SetSourceData( imagedata );
312 slicesLST.push_back( slice );
313 _pSource->Push( sp );
315 if (_imageResult ==NULL)
317 _imageResult = vtkImageData::New();
318 _imageResult -> SetDimensions(_sizeIma,_sizeIma,1);
319 _imageResult -> SetSpacing(1,1,1);
320 _imageResult -> SetExtent(0,_sizeIma-1,0,_sizeIma-1,0,0);
321 //EED 2017-01-01 Migration VTK7
322 #if VTK_MAJOR_VERSION <= 5
323 _imageResult -> SetScalarType( imagedata->GetScalarType() );
324 _imageResult -> SetWholeExtent(0,_sizeIma-1,0,_sizeIma-1,0,0);
325 _imageResult -> AllocateScalars();
326 _imageResult -> Update();
328 _imageResult -> AllocateScalars(imagedata->GetScalarType(),1);
332 unsigned short *pTemp;
333 unsigned short *pResult;
334 pResult = (unsigned short*)_imageResult->GetScalarPointer( 0 , 0 , 0 );
335 int iPixels , sizePixels = _sizeIma*_sizeIma;
336 for(iPixels=0 ; iPixels<sizePixels ; iPixels=iPixels+deltaPixel)
338 pTemp = (unsigned short*)slicesLST[0]->GetOutput()->GetPointData()->GetScalars()->GetVoidPointer(0);
339 pResult = (unsigned short*)_imageResult->GetScalarPointer( 0 , 0 , 0 );
340 pResult[iPixels] = pTemp[iPixels];
341 for (iWidth=1;iWidth<sizeWidth;iWidth++)
343 pTemp = (unsigned short*)slicesLST[iWidth]->GetOutput()->GetPointData()->GetScalars()->GetVoidPointer(0);
345 if (pResult[iPixels]< pTemp[iPixels])
347 pResult[iPixels] = pTemp[iPixels];
352 for (itmp=1;itmp<deltaPixel;itmp++)
354 pResult[iPixels+itmp] = pResult[iPixels];
358 for (iWidth=0;iWidth<sizeWidth;iWidth++)
360 slicesLST[iWidth]->Delete();
362 _imageResult->Modified();
363 //EED 2017-01-01 Migration VTK7
364 #if VTK_MAJOR_VERSION <= 5
365 _imageViewer2XYZ->GetVtkImageViewer2()->SetInput( _imageResult );
367 _imageViewer2XYZ->GetVtkImageViewer2()->SetInputData( _imageResult );
371 //-------------------------------------------------------------------
372 void vtkPlane2DView::Configure( )
374 wxVtk2DBaseView::Configure(false);
380 _pSource = vtkPlaneSource::New( );
381 _3Dslices = vtkProbeFilter::New( ) ;
382 _stPoints = vtkStructuredPoints::New( );
383 _change = vtkImageChangeInformation::New();
385 crea::wxVTKRenderWindowInteractor *iren = GetWxVTKRenderWindowInteractor();
388 _imageViewer2XYZ -> GetVtkImageViewer2() -> SetupInteractor ( iren );
391 //RaC 04-2010 Look for the description of new changes in vtkInteractorStyleBaseView2D constructor.
394 // SetInteractorStyleImage( vtkInteractorStyleBaseView2D::New() );
397 vtkInteractorStyleBaseView2D *style2D = vtkInteractorStyleBaseView2D::New();
398 manualInteractorWindowLevel *_manualinteractorwindowlevel= new manualInteractorWindowLevel();
399 style2D->SetInteractorWindowLevel( _manualinteractorwindowlevel );
400 vtkInteractorScrollZ *_vtkInteractorScrollZ = new vtkInteractorScrollZ();
401 style2D->SetInteractorScrollZ(_vtkInteractorScrollZ);
402 SetInteractorStyleImage( style2D );
407 _interactorstyleplane2D = new vtkInteractorStylePlane2D();
408 ((vtkInteractorStyleBaseView*)GetInteractorStyleBaseView())->AddInteractorStyleMaracas( _interactorstyleplane2D );
410 vtkCamera *camera =_imageViewer2XYZ->GetVtkImageViewer2()->GetRenderer()->GetActiveCamera();
412 camera->SetViewUp ( 0 , 1 , 0 );
413 camera->SetPosition ((0+_sizeIma)/2 , (0+_sizeIma)/2 , 10000 );
415 camera->SetFocalPoint ((0+_sizeIma)/2 , (0+_sizeIma)/2 , 0 );
416 camera->SetClippingRange(0.01, 100000);
417 camera->ComputeViewPlaneNormal();
418 camera->SetParallelScale( _sizeIma/3.0 );
420 // text information over the graphic window
421 _vtkInfoTextImage = new vtkInfoTextImage();
422 _vtkInfoTextImageInteractorPlane2D = new vtkInfoTextImageInteractorPlane2D();
423 _vtkInfoTextImage->SetWxVtk2DBaseView(this);
424 _vtkInfoTextImage->SetMarImageData( GetVtkmprbasedata()->GetMarImageData() );
425 _vtkInfoTextImageInteractorPlane2D->SetModelVtkInfoTextImage(_vtkInfoTextImage);
426 _vtkInfoTextImage->Configure();
427 ((vtkInteractorStyleBaseView*)this->GetInteractorStyleBaseView())->AddInteractorStyleMaracas(_vtkInfoTextImageInteractorPlane2D);
430 //-------------------------------------------------------------------
431 void vtkPlane2DView::HorizontalLine()
434 _pts = vtkPoints::New();
435 _pts->SetNumberOfPoints(2);
436 _pts->SetPoint(0, -1000 , -1000 , -1000 );
437 _pts->SetPoint(1, 1000 , 1000 , 1000 );
438 vtkCellArray *lines = vtkCellArray::New();
439 lines->InsertNextCell(2);
440 lines->InsertCellPoint(0);
441 lines->InsertCellPoint(1);
442 _pd = vtkPolyData::New();
443 _pd->SetPoints( _pts );
444 _pd->SetLines( lines );
445 lines->Delete(); //do not delete lines ??
446 _lineActor = vtkActor::New();
447 _lineMapper = vtkPolyDataMapper::New();
448 //EED 2017-01-01 Migration VTK7
449 #if VTK_MAJOR_VERSION <= 5
450 _lineMapper->SetInput(_pd);
451 _lineMapper->ImmediateModeRenderingOn();
453 _lineMapper->SetInputData(_pd);
455 _lineActor->SetMapper(_lineMapper);
456 _lineActor->GetProperty()->BackfaceCullingOn();
457 _lineActor->GetProperty()->SetDiffuseColor(0,0,1);
458 _lineActor->GetProperty()->SetLineWidth(2);
459 _lineActor->GetProperty()->SetOpacity(0);
460 _imageViewer2XYZ->GetVtkImageViewer2()->GetRenderer()->AddActor( _lineActor );
463 //-------------------------------------------------------------------
464 void vtkPlane2DView::RotationStart()
466 vtkMPRBaseData *mprbasedata = (vtkMPRBaseData*)this->GetVtkBaseData();
467 vtkTransform *transform = mprbasedata->GetTransformOrientation();
468 _transform1->SetMatrix( transform->GetMatrix() );
471 //-------------------------------------------------------------------
472 void vtkPlane2DView::RotationDrag(double vx, double vy, bool ok_v, bool ok_ang)
476 _ang =sqrt( vx*vx + vy*vy ) / 1.5;
483 _transform2->Identity();
484 _transform2->RotateWXYZ(_ang,0,_vxb,_vyb);
486 vtkMPRBaseData *mprbasedata = (vtkMPRBaseData*)this->GetVtkBaseData();
487 vtkTransform *transform = vtkTransform::New();
488 transform->Identity();
489 transform->Concatenate(_transform1);
490 transform->Concatenate(_transform2);
491 mprbasedata->InitTransformOrientation(transform);
494 // Refresh Horizontal Line
495 _pts->SetPoint( 0 , (_sizeIma/2) - _vxb*2 , (_sizeIma/2) - _vyb*2 , 1 );
496 _pts->SetPoint( 1 , (_sizeIma/2) + _vxb*2 , (_sizeIma/2) + _vyb*2 , 1 );
497 // RefreshCircleLine();
500 //-------------------------------------------------------------------
501 void vtkPlane2DView::Refresh( )
504 UpdateColorWindowLevel();
505 wxVtkBaseView::Refresh();
507 //-------------------------------------------------------------------
508 void vtkPlane2DView::SetImgSize( int imgSize )
512 //-------------------------------------------------------------------
513 int vtkPlane2DView::GetImgSize()
517 //-------------------------------------------------------------------
518 int vtkPlane2DView::GetActualSlice() // virtual
520 _cx = GetVtkmprbasedata()->GetX();
521 _cy = GetVtkmprbasedata()->GetY();
522 _cz = GetVtkmprbasedata()->GetZ();
526 //-------------------------------------------------------------------
528 bool vtkPlane2DView::GetMipVisualization()
530 return _mip_visualization;
533 //-------------------------------------------------------------------
534 int vtkPlane2DView::GetMipWidth()
539 //-------------------------------------------------------------------
540 void vtkPlane2DView::SetActualSlice(int slice) // Virtual
542 double dir=(double)slice/3.0;
543 GetVtkmprbasedata()->SetX( _cx + (_n[0]*dir) );
544 GetVtkmprbasedata()->SetY( _cy + (_n[1]*dir) );
545 GetVtkmprbasedata()->SetZ( _cz + (_n[2]*dir) );
547 //---------------------------------------------------------------------------
548 vtkInteractorStylePlane2D *vtkPlane2DView::GetInteractorstyleplane2D()
550 return this->_interactorstyleplane2D;
552 //---------------------------------------------------------------------------
553 void vtkPlane2DView::SetActive(bool active)
558 //---------------------------------------------------------------------------
560 void vtkPlane2DView::SetMipVisualization(bool ok)
562 _mip_visualization=ok;
565 //---------------------------------------------------------------------------
567 void vtkPlane2DView::SetMipWidth(int value)
572 //---------------------------------------------------------------------------
573 void vtkPlane2DView::SetVisibleLine(bool ok)
582 _lineActor->GetProperty()->SetOpacity(opacity);
585 // ----------------------------------------------------------------------------
586 void vtkPlane2DView::TransfromCoordViewWorld2(double &X, double &Y, double &Z)
589 GetVtkmprbasedata()->GetImageData()->GetSpacing(spc);
593 TransFromCoordScreenToWorld(xx,yy,zz);
594 vtkTransform *transf1 = vtkTransform::New();
596 vtkTransform *transf2 = GetVtkmprbasedata()->GetTransformOrientation();
597 transf1->Concatenate(transf2->GetMatrix());
598 double in[4], out[4];
599 double center = GetImgSize() / 2;
604 transf1->MultiplyPoint(in,out);
606 X = out[0] + GetVtkmprbasedata()->GetX() ;
607 Y = out[1] + GetVtkmprbasedata()->GetY() ;
608 Z = out[2] + GetVtkmprbasedata()->GetZ() ;