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