1 /*=========================================================================
2
3 Program: Visualization Toolkit
4 Module: vtkDistanceRepresentation.cxx
5
6 Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen
7 All rights reserved.
8 See Copyright.txt or http://www.kitware.com/Copyright.htm for details.
9
10 This software is distributed WITHOUT ANY WARRANTY; without even
11 the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
12 PURPOSE. See the above copyright notice for more information.
13
14 =========================================================================*/
15 #include "vtkDistanceRepresentation.h"
16 #include "vtkBox.h"
17 #include "vtkCoordinate.h"
18 #include "vtkEventData.h"
19 #include "vtkHandleRepresentation.h"
20 #include "vtkInteractorObserver.h"
21 #include "vtkMath.h"
22 #include "vtkObjectFactory.h"
23 #include "vtkRenderWindowInteractor.h"
24 #include "vtkRenderer.h"
25 #include "vtkWindow.h"
26
27 vtkCxxSetObjectMacro(vtkDistanceRepresentation, HandleRepresentation, vtkHandleRepresentation);
28
29 //------------------------------------------------------------------------------
vtkDistanceRepresentation()30 vtkDistanceRepresentation::vtkDistanceRepresentation()
31 {
32 this->HandleRepresentation = nullptr;
33 this->Point1Representation = nullptr;
34 this->Point2Representation = nullptr;
35
36 this->Tolerance = 5;
37 this->Placed = 0;
38
39 this->LabelFormat = new char[8];
40 snprintf(this->LabelFormat, 8, "%s", "%-#6.3g");
41
42 this->Scale = 1.0;
43 this->RulerMode = 0;
44 this->RulerDistance = 1.0;
45 this->NumberOfRulerTicks = 5;
46 }
47
48 //------------------------------------------------------------------------------
~vtkDistanceRepresentation()49 vtkDistanceRepresentation::~vtkDistanceRepresentation()
50 {
51 if (this->HandleRepresentation)
52 {
53 this->HandleRepresentation->Delete();
54 }
55 if (this->Point1Representation)
56 {
57 this->Point1Representation->Delete();
58 }
59 if (this->Point2Representation)
60 {
61 this->Point2Representation->Delete();
62 }
63
64 delete[] this->LabelFormat;
65 this->LabelFormat = nullptr;
66 }
67
68 //------------------------------------------------------------------------------
InstantiateHandleRepresentation()69 void vtkDistanceRepresentation::InstantiateHandleRepresentation()
70 {
71 if (!this->Point1Representation)
72 {
73 this->Point1Representation = this->HandleRepresentation->NewInstance();
74 this->Point1Representation->ShallowCopy(this->HandleRepresentation);
75 }
76
77 if (!this->Point2Representation)
78 {
79 this->Point2Representation = this->HandleRepresentation->NewInstance();
80 this->Point2Representation->ShallowCopy(this->HandleRepresentation);
81 }
82 }
83
84 //------------------------------------------------------------------------------
GetPoint1WorldPosition(double pos[3])85 void vtkDistanceRepresentation::GetPoint1WorldPosition(double pos[3])
86 {
87 if (this->Point1Representation)
88 {
89 this->Point1Representation->GetWorldPosition(pos);
90 }
91 }
92
93 //------------------------------------------------------------------------------
GetPoint2WorldPosition(double pos[3])94 void vtkDistanceRepresentation::GetPoint2WorldPosition(double pos[3])
95 {
96 if (this->Point2Representation)
97 {
98 this->Point2Representation->GetWorldPosition(pos);
99 }
100 }
101
102 //------------------------------------------------------------------------------
ComputeInteractionState(int vtkNotUsed (X),int vtkNotUsed (Y),int vtkNotUsed (modify))103 int vtkDistanceRepresentation::ComputeInteractionState(
104 int vtkNotUsed(X), int vtkNotUsed(Y), int vtkNotUsed(modify))
105 {
106 if (this->Point1Representation == nullptr || this->Point2Representation == nullptr)
107 {
108 this->InteractionState = vtkDistanceRepresentation::Outside;
109 return this->InteractionState;
110 }
111
112 int h1State = this->Point1Representation->GetInteractionState();
113 int h2State = this->Point2Representation->GetInteractionState();
114 if (h1State == vtkHandleRepresentation::Nearby)
115 {
116 this->InteractionState = vtkDistanceRepresentation::NearP1;
117 }
118 else if (h2State == vtkHandleRepresentation::Nearby)
119 {
120 this->InteractionState = vtkDistanceRepresentation::NearP2;
121 }
122 else
123 {
124 this->InteractionState = vtkDistanceRepresentation::Outside;
125 }
126
127 return this->InteractionState;
128 }
129
ComputeComplexInteractionState(vtkRenderWindowInteractor *,vtkAbstractWidget *,unsigned long,void *,int)130 int vtkDistanceRepresentation::ComputeComplexInteractionState(
131 vtkRenderWindowInteractor*, vtkAbstractWidget*, unsigned long, void*, int)
132 {
133 if (this->Point1Representation == nullptr || this->Point2Representation == nullptr)
134 {
135 this->InteractionState = vtkDistanceRepresentation::Outside;
136 return this->InteractionState;
137 }
138
139 int h1State = this->Point1Representation->GetInteractionState();
140 int h2State = this->Point2Representation->GetInteractionState();
141 if (h1State == vtkHandleRepresentation::Nearby)
142 {
143 this->InteractionState = vtkDistanceRepresentation::NearP1;
144 }
145 else if (h2State == vtkHandleRepresentation::Nearby)
146 {
147 this->InteractionState = vtkDistanceRepresentation::NearP2;
148 }
149 else
150 {
151 this->InteractionState = vtkDistanceRepresentation::Outside;
152 }
153
154 return this->InteractionState;
155 }
156
157 //------------------------------------------------------------------------------
StartWidgetInteraction(double e[2])158 void vtkDistanceRepresentation::StartWidgetInteraction(double e[2])
159 {
160 double pos[3];
161 pos[0] = e[0];
162 pos[1] = e[1];
163 pos[2] = 0.0;
164 this->SetPoint1DisplayPosition(pos);
165 this->SetPoint2DisplayPosition(pos);
166 }
167
StartComplexInteraction(vtkRenderWindowInteractor *,vtkAbstractWidget *,unsigned long,void * calldata)168 void vtkDistanceRepresentation::StartComplexInteraction(
169 vtkRenderWindowInteractor*, vtkAbstractWidget*, unsigned long, void* calldata)
170 {
171 vtkEventData* edata = static_cast<vtkEventData*>(calldata);
172 vtkEventDataDevice3D* edd = edata->GetAsEventDataDevice3D();
173 if (edd)
174 {
175 double pos[3];
176 edd->GetWorldPosition(pos);
177 this->SetPoint1WorldPosition(pos);
178 this->SetPoint2WorldPosition(pos);
179 }
180 }
181
182 //------------------------------------------------------------------------------
WidgetInteraction(double e[2])183 void vtkDistanceRepresentation::WidgetInteraction(double e[2])
184 {
185 double pos[3];
186 pos[0] = e[0];
187 pos[1] = e[1];
188 pos[2] = 0.0;
189 this->SetPoint2DisplayPosition(pos);
190 }
ComplexInteraction(vtkRenderWindowInteractor *,vtkAbstractWidget *,unsigned long,void * calldata)191 void vtkDistanceRepresentation::ComplexInteraction(
192 vtkRenderWindowInteractor*, vtkAbstractWidget*, unsigned long, void* calldata)
193 {
194 vtkEventData* edata = static_cast<vtkEventData*>(calldata);
195 vtkEventDataDevice3D* edd = edata->GetAsEventDataDevice3D();
196 if (edd)
197 {
198 double pos[3];
199 edd->GetWorldPosition(pos);
200 this->SetPoint2WorldPosition(pos);
201 }
202 }
203
204 //------------------------------------------------------------------------------
BuildRepresentation()205 void vtkDistanceRepresentation::BuildRepresentation()
206 {
207 // Make sure that tolerance is consistent between handles and this representation
208 if (this->Point1Representation)
209 {
210 this->Point1Representation->SetTolerance(this->Tolerance);
211 }
212 if (this->Point2Representation)
213 {
214 this->Point2Representation->SetTolerance(this->Tolerance);
215 }
216 }
217
218 //------------------------------------------------------------------------------
PrintSelf(ostream & os,vtkIndent indent)219 void vtkDistanceRepresentation::PrintSelf(ostream& os, vtkIndent indent)
220 {
221 // Superclass typedef defined in vtkTypeMacro() found in vtkSetGet.h
222 this->Superclass::PrintSelf(os, indent);
223
224 os << indent << "Distance: " << this->GetDistance() << "\n";
225 os << indent << "Tolerance: " << this->Tolerance << "\n";
226 os << indent << "Handle Representation: " << this->HandleRepresentation << "\n";
227
228 os << indent << "Label Format: ";
229 if (this->LabelFormat)
230 {
231 os << this->LabelFormat << "\n";
232 }
233 else
234 {
235 os << "(none)\n";
236 }
237
238 os << indent << "Scale: " << this->GetScale() << "\n";
239 os << indent << "Ruler Mode: " << (this->RulerMode ? "On" : "Off") << "\n";
240 os << indent << "Ruler Distance: " << this->GetRulerDistance() << "\n";
241 os << indent << "Number of Ruler Ticks: " << this->GetNumberOfRulerTicks() << "\n";
242
243 os << indent << "Point1 Representation: ";
244 if (this->Point1Representation)
245 {
246 this->Point1Representation->PrintSelf(os, indent.GetNextIndent());
247 }
248 else
249 {
250 os << "(none)\n";
251 }
252
253 os << indent << "Point2 Representation: ";
254 if (this->Point2Representation)
255 {
256 this->Point2Representation->PrintSelf(os, indent.GetNextIndent());
257 }
258 else
259 {
260 os << "(none)\n";
261 }
262 }
263