]> Creatis software - creaMaracasVisu.git/blob - lib/maracasVisuLib/src/CutModule/interface/vtkInteractorStyleCutter.cxx
Support #1768 CREATIS Licence insertion
[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 vtkCxxRevisionMacro(vtkInteractorStyleCutter, "$Revision: 1.2 $");
57 vtkStandardNewMacro(vtkInteractorStyleCutter);
58
59 //----------------------------------------------------------------------------
60 vtkInteractorStyleCutter::vtkInteractorStyleCutter()
61 {
62   this->CurrentPosition[0] = this->CurrentPosition[1] = 0;
63   this->Direction[0] = this->Direction[1] = this->Direction[2] = 0.;
64   this->Moving = 0;
65
66   this->Points = vtkPoints::New();
67   this->Lines = vtkCellArray::New();
68   this->LoopPoints = vtkPoints::New();
69
70   vtkPolyData *pd = vtkPolyData::New();
71   pd->SetPoints( Points );
72   pd->SetLines( Lines );
73    
74   vtkPolyDataMapper2D *bboxMapper = vtkPolyDataMapper2D::New();
75   bboxMapper->SetInput( pd );
76    
77   this->BboxActor = vtkActor2D::New();
78   this->BboxActor->SetMapper( bboxMapper );
79   this->BboxActor->GetProperty()->SetColor(1, 0, 0);
80   this->BboxActor->VisibilityOff();
81
82   finished = false;
83
84   //thanks
85   pd->Delete();
86   bboxMapper->Delete();
87 }
88
89 //----------------------------------------------------------------------------
90 vtkInteractorStyleCutter::~vtkInteractorStyleCutter()
91 {
92   this->Points->Delete();
93   this->BboxActor->Delete();
94   this->Lines->Delete();
95   this->LoopPoints->Delete();
96 }
97
98 //----------------------------------------------------------------------------
99 void vtkInteractorStyleCutter::OnMouseMove()
100 {
101   if (!this->Interactor || !this->Moving)
102     {
103     return;
104     }
105   
106   this->CurrentPosition[0] = this->Interactor->GetEventPosition()[0];
107   this->CurrentPosition[1] = this->Interactor->GetEventPosition()[1];  
108   
109   //mouse move event
110   this->Points->SetPoint(this->PointID, this->CurrentPosition[0], 
111     this->CurrentPosition[1], 0);
112
113   this->Interactor->Render();
114 }
115
116 //----------------------------------------------------------------------------
117 void vtkInteractorStyleCutter::OnLeftButtonDown()
118 {
119   if (!this->Interactor)
120     {
121     return;
122     }
123
124         finished = false;
125   
126   this->CurrentPosition[0] = this->Interactor->GetEventPosition()[0];
127   this->CurrentPosition[1] = this->Interactor->GetEventPosition()[1];  
128
129   if(!this->Moving)
130     {
131     this->Initialize();
132
133     //Call this before accessing CurrentRenderer
134     this->FindPokedRenderer(this->CurrentPosition[0], this->CurrentPosition[1]);
135         this->CurrentRenderer->AddViewProp(BboxActor);
136     }
137
138   this->Moving = 1;
139
140   this->Points->SetPoint(this->PointID, this->CurrentPosition[0],
141     this->CurrentPosition[1], 0);
142   this->PointID = this->Points->InsertNextPoint( this->CurrentPosition[0], 
143     this->CurrentPosition[1], 0);
144
145   this->Lines->InsertCellPoint( this->PointID );
146   this->Lines->UpdateCellCount( this->PointID + 1 );
147   this->BboxActor->VisibilityOn();
148
149   this->Interactor->Render();
150 }
151
152 //----------------------------------------------------------------------------
153 bool vtkInteractorStyleCutter::Finished()
154 {
155     return finished;
156 }
157
158 //----------------------------------------------------------------------------
159 void vtkInteractorStyleCutter::OnRightButtonDown()
160 {
161   if (!this->Interactor || !this->Moving)
162     {
163     return;
164     }
165   
166   double xyz[3];
167   this->Points->GetPoint( 0, xyz );
168   this->Points->SetPoint(this->PointID, xyz);
169
170   //Save current state
171   this->EndLoop();
172
173   this->Interactor->Render();
174   this->Moving = 0;
175   finished = true;
176 }
177
178 //----------------------------------------------------------------------------
179 void vtkInteractorStyleCutter::Initialize()
180 {
181   this->Points->Reset();
182   this->Lines->Reset();
183
184   this->PointID = this->Points->InsertNextPoint( 0, 0, 0);
185   this->Lines->InsertNextCell( 1 );
186   this->Lines->InsertCellPoint( 0 );
187 }
188 //----------------------------------------------------------------------------
189 void vtkInteractorStyleCutter::EndLoop()
190 {
191   double pi[3],fpi[4];
192   int numPts = this->Points->GetNumberOfPoints()-1;
193   this->LoopPoints->SetNumberOfPoints( numPts );
194   vtkCamera *camera = this->CurrentRenderer->GetActiveCamera();
195   //int state = camera->GetParallelProjection ();
196   camera->ParallelProjectionOn();
197
198   for (int i=0; i < numPts; i++)
199   {
200     this->Points->GetPoint(i, pi);
201     this->CurrentRenderer->SetDisplayPoint(pi[0], pi[1], 0);
202     this->CurrentRenderer->DisplayToWorld();
203
204     this->CurrentRenderer->GetWorldPoint( fpi );
205     if ( fpi[3] )
206     {
207       fpi[0] /= fpi[3];
208       fpi[1] /= fpi[3];
209       fpi[2] /= fpi[3];
210     }
211     this->LoopPoints->SetPoint( i, fpi[0], fpi[1], fpi[2] );
212   }
213
214   //Set direction of extrusion, should save this state before camera moves
215   camera->GetDirectionOfProjection( this->Direction );
216   //camera->SetParallelProjection( state );
217 }
218 //----------------------------------------------------------------------------
219 //Just a quick hack:
220 void vtkInteractorStyleCutter::VisibilityOff()
221 {
222   this->BboxActor->VisibilityOff();
223 }
224 //----------------------------------------------------------------------------
225 void vtkInteractorStyleCutter::PrintSelf(ostream& os, vtkIndent indent)
226 {
227   this->Superclass::PrintSelf(os, indent);
228 }
229
230