]> Creatis software - creaMaracasVisu.git/blob - lib/maracasVisuLib/src/interface/wxWindows/widgets/include/vtkInteractorStyleCutter.cxx
DFCH
[creaMaracasVisu.git] / lib / maracasVisuLib / src / interface / wxWindows / widgets / include / vtkInteractorStyleCutter.cxx
1 /*=========================================================================
2
3   Program:   Visualization Toolkit
4   Module:    $RCSfile: vtkInteractorStyleCutter.cxx,v $
5   Language:  C++
6   Date:      $Date: 2009/05/14 13:54:57 $
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   //thanks
58   pd->Delete();
59   bboxMapper->Delete();
60 }
61
62 //----------------------------------------------------------------------------
63 vtkInteractorStyleCutter::~vtkInteractorStyleCutter()
64 {
65   this->Points->Delete();
66   this->BboxActor->Delete();
67   this->Lines->Delete();
68   this->LoopPoints->Delete();
69 }
70
71 //----------------------------------------------------------------------------
72 void vtkInteractorStyleCutter::OnMouseMove()
73 {
74   if (!this->Interactor || !this->Moving)
75     {
76     return;
77     }
78   
79   this->CurrentPosition[0] = this->Interactor->GetEventPosition()[0];
80   this->CurrentPosition[1] = this->Interactor->GetEventPosition()[1];  
81   
82   //mouse move event
83   this->Points->SetPoint(this->PointID, this->CurrentPosition[0], 
84     this->CurrentPosition[1], 0);
85
86   this->Interactor->Render();
87 }
88
89 //----------------------------------------------------------------------------
90 void vtkInteractorStyleCutter::OnLeftButtonDown()
91 {
92   if (!this->Interactor)
93     {
94     return;
95     }
96   
97   this->CurrentPosition[0] = this->Interactor->GetEventPosition()[0];
98   this->CurrentPosition[1] = this->Interactor->GetEventPosition()[1];  
99
100   if(!this->Moving)
101     {
102     this->Initialize();
103
104     //Call this before accessing CurrentRenderer
105     this->FindPokedRenderer(this->CurrentPosition[0], this->CurrentPosition[1]);
106     this->CurrentRenderer->AddProp( BboxActor );
107     }
108
109   this->Moving = 1;
110
111   this->Points->SetPoint(this->PointID, this->CurrentPosition[0],
112     this->CurrentPosition[1], 0);
113   this->PointID = this->Points->InsertNextPoint( this->CurrentPosition[0], 
114     this->CurrentPosition[1], 0);
115
116   this->Lines->InsertCellPoint( this->PointID );
117   this->Lines->UpdateCellCount( this->PointID + 1 );
118   this->BboxActor->VisibilityOn();
119
120   this->Interactor->Render();
121 }
122
123 //----------------------------------------------------------------------------
124 void vtkInteractorStyleCutter::OnRightButtonDown()
125 {
126   if (!this->Interactor || !this->Moving)
127     {
128     return;
129     }
130   
131   double xyz[3];
132   this->Points->GetPoint( 0, xyz );
133   this->Points->SetPoint(this->PointID, xyz);
134
135   //Save current state
136   this->EndLoop();
137
138   this->Interactor->Render();
139   this->Moving = 0;
140 }
141
142 //----------------------------------------------------------------------------
143 void vtkInteractorStyleCutter::Initialize()
144 {
145   this->Points->Reset();
146   this->Lines->Reset();
147
148   this->PointID = this->Points->InsertNextPoint( 0, 0, 0);
149   this->Lines->InsertNextCell( 1 );
150   this->Lines->InsertCellPoint( 0 );
151 }
152 //----------------------------------------------------------------------------
153 void vtkInteractorStyleCutter::EndLoop()
154 {
155   double pi[3],fpi[4];
156   int numPts = this->Points->GetNumberOfPoints()-1;
157   this->LoopPoints->SetNumberOfPoints( numPts );
158   vtkCamera *camera = this->CurrentRenderer->GetActiveCamera();
159   int state = camera->GetParallelProjection ();
160   camera->ParallelProjectionOn();
161
162   for (int i=0; i < numPts; i++)
163   {
164     this->Points->GetPoint(i, pi);
165     this->CurrentRenderer->SetDisplayPoint(pi[0], pi[1], 0);
166     this->CurrentRenderer->DisplayToWorld();
167
168     this->CurrentRenderer->GetWorldPoint( fpi );
169     if ( fpi[3] )
170     {
171       fpi[0] /= fpi[3];
172       fpi[1] /= fpi[3];
173       fpi[2] /= fpi[3];
174     }
175     this->LoopPoints->SetPoint( i, fpi[0], fpi[1], fpi[2] );
176   }
177
178   //Set direction of extrusion, should save this state before camera moves
179   camera->GetDirectionOfProjection( this->Direction );
180   //camera->SetParallelProjection( state );
181 }
182 //----------------------------------------------------------------------------
183 //Just a quick hack:
184 void vtkInteractorStyleCutter::VisibilityOff()
185 {
186   this->BboxActor->VisibilityOff();
187 }
188 //----------------------------------------------------------------------------
189 void vtkInteractorStyleCutter::PrintSelf(ostream& os, vtkIndent indent)
190 {
191   this->Superclass::PrintSelf(os, indent);
192 }