]> Creatis software - creaMaracasVisu.git/blob - lib/maracasVisuLib/src/interface/wxWindows/widgets/include/vtkInteractorStyleCutter.cxx
Support #1768 CREATIS Licence insertion
[creaMaracasVisu.git] / lib / maracasVisuLib / src / interface / wxWindows / widgets / include / 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:15:18 $
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   //thanks
83   pd->Delete();
84   bboxMapper->Delete();
85 }
86
87 //----------------------------------------------------------------------------
88 vtkInteractorStyleCutter::~vtkInteractorStyleCutter()
89 {
90   this->Points->Delete();
91   this->BboxActor->Delete();
92   this->Lines->Delete();
93   this->LoopPoints->Delete();
94 }
95
96 //----------------------------------------------------------------------------
97 void vtkInteractorStyleCutter::OnMouseMove()
98 {
99   if (!this->Interactor || !this->Moving)
100     {
101     return;
102     }
103   
104   this->CurrentPosition[0] = this->Interactor->GetEventPosition()[0];
105   this->CurrentPosition[1] = this->Interactor->GetEventPosition()[1];  
106   
107   //mouse move event
108   this->Points->SetPoint(this->PointID, this->CurrentPosition[0], 
109     this->CurrentPosition[1], 0);
110
111   this->Interactor->Render();
112 }
113
114 //----------------------------------------------------------------------------
115 void vtkInteractorStyleCutter::OnLeftButtonDown()
116 {
117   if (!this->Interactor)
118     {
119     return;
120     }
121   
122   this->CurrentPosition[0] = this->Interactor->GetEventPosition()[0];
123   this->CurrentPosition[1] = this->Interactor->GetEventPosition()[1];  
124
125   if(!this->Moving)
126     {
127     this->Initialize();
128
129     //Call this before accessing CurrentRenderer
130     this->FindPokedRenderer(this->CurrentPosition[0], this->CurrentPosition[1]);
131     this->CurrentRenderer->AddProp( BboxActor );
132     }
133
134   this->Moving = 1;
135
136   this->Points->SetPoint(this->PointID, this->CurrentPosition[0],
137     this->CurrentPosition[1], 0);
138   this->PointID = this->Points->InsertNextPoint( this->CurrentPosition[0], 
139     this->CurrentPosition[1], 0);
140
141   this->Lines->InsertCellPoint( this->PointID );
142   this->Lines->UpdateCellCount( this->PointID + 1 );
143   this->BboxActor->VisibilityOn();
144
145   this->Interactor->Render();
146 }
147
148 //----------------------------------------------------------------------------
149 void vtkInteractorStyleCutter::OnRightButtonDown()
150 {
151   if (!this->Interactor || !this->Moving)
152     {
153     return;
154     }
155   
156   double xyz[3];
157   this->Points->GetPoint( 0, xyz );
158   this->Points->SetPoint(this->PointID, xyz);
159
160   //Save current state
161   this->EndLoop();
162
163   this->Interactor->Render();
164   this->Moving = 0;
165 }
166
167 //----------------------------------------------------------------------------
168 void vtkInteractorStyleCutter::Initialize()
169 {
170   this->Points->Reset();
171   this->Lines->Reset();
172
173   this->PointID = this->Points->InsertNextPoint( 0, 0, 0);
174   this->Lines->InsertNextCell( 1 );
175   this->Lines->InsertCellPoint( 0 );
176 }
177 //----------------------------------------------------------------------------
178 void vtkInteractorStyleCutter::EndLoop()
179 {
180   double pi[3],fpi[4];
181   int numPts = this->Points->GetNumberOfPoints()-1;
182   this->LoopPoints->SetNumberOfPoints( numPts );
183   vtkCamera *camera = this->CurrentRenderer->GetActiveCamera();
184   int state = camera->GetParallelProjection ();
185   camera->ParallelProjectionOn();
186
187   for (int i=0; i < numPts; i++)
188   {
189     this->Points->GetPoint(i, pi);
190     this->CurrentRenderer->SetDisplayPoint(pi[0], pi[1], 0);
191     this->CurrentRenderer->DisplayToWorld();
192
193     this->CurrentRenderer->GetWorldPoint( fpi );
194     if ( fpi[3] )
195     {
196       fpi[0] /= fpi[3];
197       fpi[1] /= fpi[3];
198       fpi[2] /= fpi[3];
199     }
200     this->LoopPoints->SetPoint( i, fpi[0], fpi[1], fpi[2] );
201   }
202
203   //Set direction of extrusion, should save this state before camera moves
204   camera->GetDirectionOfProjection( this->Direction );
205   //camera->SetParallelProjection( state );
206 }
207 //----------------------------------------------------------------------------
208 //Just a quick hack:
209 void vtkInteractorStyleCutter::VisibilityOff()
210 {
211   this->BboxActor->VisibilityOff();
212 }
213 //----------------------------------------------------------------------------
214 void vtkInteractorStyleCutter::PrintSelf(ostream& os, vtkIndent indent)
215 {
216   this->Superclass::PrintSelf(os, indent);
217 }