]> Creatis software - creaMaracasVisu.git/blob - lib/maracasVisuLib/src/CutModule/interface/vtkInteractorStyleCutter.cxx
#3109 creaMaracasVisu Bug New Normal - branch vtk7itk4 compilation with vtk7
[creaMaracasVisu.git] / lib / maracasVisuLib / src / CutModule / interface / vtkInteractorStyleCutter.cxx
1 /*# ---------------------------------------------------------------------
2 #
3 # Copyright (c) CREATIS (Centre de Recherche en Acquisition et Traitement de l'Image
4 #                        pour la Sant�)
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
8 #
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.
15 #
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
20 #  liability.
21 #
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 # ------------------------------------------------------------------------ */
25
26 /*=========================================================================
27
28   Program:   Visualization Toolkit
29   Module:    $RCSfile: vtkInteractorStyleCutter.cxx,v $
30   Language:  C++
31   Date:      $Date: 2012/11/15 14:16:20 $
32   Version:   $Revision: 1.2 $
33
34   Copyright (c) 1993-2002 Ken Martin, Will Schroeder, Bill Lorensen 
35   All rights reserved.
36   See Copyright.txt or http://www.kitware.com/Copyright.htm for details.
37
38      This software is distributed WITHOUT ANY WARRANTY; without even 
39      the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR 
40      PURPOSE.  See the above copyright notice for more information.
41
42 =========================================================================*/
43 #include "vtkInteractorStyleCutter.h"
44
45 #include <vtkPoints.h>
46 #include <vtkActor2D.h>
47 #include <vtkObjectFactory.h>
48 #include <vtkRenderer.h>
49 #include <vtkRenderWindowInteractor.h>
50 #include <vtkCellArray.h>
51 #include <vtkPolyData.h>
52 #include <vtkPolyDataMapper2D.h>
53 #include <vtkProperty2D.h>
54 #include <vtkCamera.h>
55
56 //EED 2017-01-01 Migration VTK7
57 #if VTK_MAJOR_VERSION <= 5
58 vtkCxxRevisionMacro(vtkInteractorStyleCutter, "$Revision: 1.2 $");
59 #else
60 // ..
61 #endif
62 vtkStandardNewMacro(vtkInteractorStyleCutter);
63
64 //----------------------------------------------------------------------------
65 vtkInteractorStyleCutter::vtkInteractorStyleCutter()
66 {
67   this->CurrentPosition[0] = this->CurrentPosition[1] = 0;
68   this->Direction[0] = this->Direction[1] = this->Direction[2] = 0.;
69   this->Moving = 0;
70
71   this->Points = vtkPoints::New();
72   this->Lines = vtkCellArray::New();
73   this->LoopPoints = vtkPoints::New();
74
75   vtkPolyData *pd = vtkPolyData::New();
76   pd->SetPoints( Points );
77   pd->SetLines( Lines );
78    
79   vtkPolyDataMapper2D *bboxMapper = vtkPolyDataMapper2D::New();
80 //EED 2017-01-01 Migration VTK7
81 #if VTK_MAJOR_VERSION <= 5
82   bboxMapper->SetInput( pd );
83 #else
84   bboxMapper->SetInputData( pd );
85 #endif
86    
87   this->BboxActor = vtkActor2D::New();
88   this->BboxActor->SetMapper( bboxMapper );
89   this->BboxActor->GetProperty()->SetColor(1, 0, 0);
90   this->BboxActor->VisibilityOff();
91
92   finished = false;
93
94   //thanks
95   pd->Delete();
96   bboxMapper->Delete();
97 }
98
99 //----------------------------------------------------------------------------
100 vtkInteractorStyleCutter::~vtkInteractorStyleCutter()
101 {
102   this->Points->Delete();
103   this->BboxActor->Delete();
104   this->Lines->Delete();
105   this->LoopPoints->Delete();
106 }
107
108 //----------------------------------------------------------------------------
109 void vtkInteractorStyleCutter::OnMouseMove()
110 {
111   if (!this->Interactor || !this->Moving)
112     {
113     return;
114     }
115   
116   this->CurrentPosition[0] = this->Interactor->GetEventPosition()[0];
117   this->CurrentPosition[1] = this->Interactor->GetEventPosition()[1];  
118   
119   //mouse move event
120   this->Points->SetPoint(this->PointID, this->CurrentPosition[0], 
121     this->CurrentPosition[1], 0);
122
123   this->Interactor->Render();
124 }
125
126 //----------------------------------------------------------------------------
127 void vtkInteractorStyleCutter::OnLeftButtonDown()
128 {
129   if (!this->Interactor)
130     {
131     return;
132     }
133
134         finished = false;
135   
136   this->CurrentPosition[0] = this->Interactor->GetEventPosition()[0];
137   this->CurrentPosition[1] = this->Interactor->GetEventPosition()[1];  
138
139   if(!this->Moving)
140     {
141     this->Initialize();
142
143     //Call this before accessing CurrentRenderer
144     this->FindPokedRenderer(this->CurrentPosition[0], this->CurrentPosition[1]);
145         this->CurrentRenderer->AddViewProp(BboxActor);
146     }
147
148   this->Moving = 1;
149
150   this->Points->SetPoint(this->PointID, this->CurrentPosition[0],
151     this->CurrentPosition[1], 0);
152   this->PointID = this->Points->InsertNextPoint( this->CurrentPosition[0], 
153     this->CurrentPosition[1], 0);
154
155   this->Lines->InsertCellPoint( this->PointID );
156   this->Lines->UpdateCellCount( this->PointID + 1 );
157   this->BboxActor->VisibilityOn();
158
159   this->Interactor->Render();
160 }
161
162 //----------------------------------------------------------------------------
163 bool vtkInteractorStyleCutter::Finished()
164 {
165     return finished;
166 }
167
168 //----------------------------------------------------------------------------
169 void vtkInteractorStyleCutter::OnRightButtonDown()
170 {
171   if (!this->Interactor || !this->Moving)
172     {
173     return;
174     }
175   
176   double xyz[3];
177   this->Points->GetPoint( 0, xyz );
178   this->Points->SetPoint(this->PointID, xyz);
179
180   //Save current state
181   this->EndLoop();
182
183   this->Interactor->Render();
184   this->Moving = 0;
185   finished = true;
186 }
187
188 //----------------------------------------------------------------------------
189 void vtkInteractorStyleCutter::Initialize()
190 {
191   this->Points->Reset();
192   this->Lines->Reset();
193
194   this->PointID = this->Points->InsertNextPoint( 0, 0, 0);
195   this->Lines->InsertNextCell( 1 );
196   this->Lines->InsertCellPoint( 0 );
197 }
198 //----------------------------------------------------------------------------
199 void vtkInteractorStyleCutter::EndLoop()
200 {
201   double pi[3],fpi[4];
202   int numPts = this->Points->GetNumberOfPoints()-1;
203   this->LoopPoints->SetNumberOfPoints( numPts );
204   vtkCamera *camera = this->CurrentRenderer->GetActiveCamera();
205   //int state = camera->GetParallelProjection ();
206   camera->ParallelProjectionOn();
207
208   for (int i=0; i < numPts; i++)
209   {
210     this->Points->GetPoint(i, pi);
211     this->CurrentRenderer->SetDisplayPoint(pi[0], pi[1], 0);
212     this->CurrentRenderer->DisplayToWorld();
213
214     this->CurrentRenderer->GetWorldPoint( fpi );
215     if ( fpi[3] )
216     {
217       fpi[0] /= fpi[3];
218       fpi[1] /= fpi[3];
219       fpi[2] /= fpi[3];
220     }
221     this->LoopPoints->SetPoint( i, fpi[0], fpi[1], fpi[2] );
222   }
223
224   //Set direction of extrusion, should save this state before camera moves
225   camera->GetDirectionOfProjection( this->Direction );
226   //camera->SetParallelProjection( state );
227 }
228 //----------------------------------------------------------------------------
229 //Just a quick hack:
230 void vtkInteractorStyleCutter::VisibilityOff()
231 {
232   this->BboxActor->VisibilityOff();
233 }
234 //----------------------------------------------------------------------------
235 void vtkInteractorStyleCutter::PrintSelf(ostream& os, vtkIndent indent)
236 {
237   this->Superclass::PrintSelf(os, indent);
238 }
239
240