1 /*=========================================================================
2 
3   Program:   Visualization Toolkit
4   Module:    vtkBiDimensionalRepresentation.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 "vtkBiDimensionalRepresentation.h"
16 #include "vtkActor2D.h"
17 #include "vtkCellArray.h"
18 #include "vtkCommand.h"
19 #include "vtkCoordinate.h"
20 #include "vtkHandleRepresentation.h"
21 #include "vtkInteractorObserver.h"
22 #include "vtkLine.h"
23 #include "vtkMath.h"
24 #include "vtkObjectFactory.h"
25 #include "vtkPointHandleRepresentation2D.h"
26 #include "vtkPoints.h"
27 #include "vtkPolyData.h"
28 #include "vtkPolyDataMapper2D.h"
29 #include "vtkProperty2D.h"
30 #include "vtkRenderer.h"
31 #include "vtkTextMapper.h"
32 #include "vtkTextProperty.h"
33 #include "vtkWindow.h"
34 
35 #include <sstream>
36 
37 //------------------------------------------------------------------------------
vtkBiDimensionalRepresentation()38 vtkBiDimensionalRepresentation::vtkBiDimensionalRepresentation()
39 {
40   // By default, use one of these handles
41   this->HandleRepresentation = vtkPointHandleRepresentation2D::New();
42   this->Point1Representation = nullptr;
43   this->Point2Representation = nullptr;
44   this->Point3Representation = nullptr;
45   this->Point4Representation = nullptr;
46   this->InstantiateHandleRepresentation();
47 
48   this->Modifier = 0;
49   this->Tolerance = 5;
50   this->Placed = 0;
51 
52   this->Line1Visibility = 1;
53   this->Line2Visibility = 1;
54 
55   this->LabelFormat = new char[6];
56   snprintf(this->LabelFormat, 6, "%s", "%0.3g");
57 
58   this->ID = VTK_ID_MAX;
59   this->IDInitialized = 0;
60 
61   this->ShowLabelAboveWidget = 1;
62 }
63 
64 //------------------------------------------------------------------------------
~vtkBiDimensionalRepresentation()65 vtkBiDimensionalRepresentation::~vtkBiDimensionalRepresentation()
66 {
67   if (this->HandleRepresentation)
68   {
69     this->HandleRepresentation->Delete();
70   }
71   if (this->Point1Representation)
72   {
73     this->Point1Representation->Delete();
74   }
75   if (this->Point2Representation)
76   {
77     this->Point2Representation->Delete();
78   }
79   if (this->Point3Representation)
80   {
81     this->Point3Representation->Delete();
82   }
83   if (this->Point4Representation)
84   {
85     this->Point4Representation->Delete();
86   }
87 
88   this->SetLabelFormat(nullptr);
89 }
90 
91 //------------------------------------------------------------------------------
SetHandleRepresentation(vtkHandleRepresentation * handle)92 void vtkBiDimensionalRepresentation ::SetHandleRepresentation(vtkHandleRepresentation* handle)
93 {
94   if (handle == nullptr || handle == this->HandleRepresentation)
95   {
96     return;
97   }
98 
99   this->Modified();
100   this->HandleRepresentation->Delete();
101   this->HandleRepresentation = handle;
102   this->HandleRepresentation->Register(this);
103 
104   this->Point1Representation->Delete();
105   this->Point2Representation->Delete();
106   this->Point3Representation->Delete();
107   this->Point4Representation->Delete();
108 
109   this->Point1Representation = nullptr;
110   this->Point2Representation = nullptr;
111   this->Point3Representation = nullptr;
112   this->Point4Representation = nullptr;
113 
114   this->InstantiateHandleRepresentation();
115 }
116 
117 //------------------------------------------------------------------------------
GetPoint1WorldPosition(double pos[3])118 void vtkBiDimensionalRepresentation::GetPoint1WorldPosition(double pos[3])
119 {
120   this->Point1Representation->GetWorldPosition(pos);
121 }
122 
123 //------------------------------------------------------------------------------
GetPoint2WorldPosition(double pos[3])124 void vtkBiDimensionalRepresentation::GetPoint2WorldPosition(double pos[3])
125 {
126   this->Point2Representation->GetWorldPosition(pos);
127 }
128 
129 //------------------------------------------------------------------------------
GetPoint3WorldPosition(double pos[3])130 void vtkBiDimensionalRepresentation::GetPoint3WorldPosition(double pos[3])
131 {
132   this->Point3Representation->GetWorldPosition(pos);
133 }
134 
135 //------------------------------------------------------------------------------
GetPoint4WorldPosition(double pos[3])136 void vtkBiDimensionalRepresentation::GetPoint4WorldPosition(double pos[3])
137 {
138   this->Point4Representation->GetWorldPosition(pos);
139 }
140 
141 //------------------------------------------------------------------------------
SetPoint1DisplayPosition(double x[3])142 void vtkBiDimensionalRepresentation::SetPoint1DisplayPosition(double x[3])
143 {
144   this->Point1Representation->SetDisplayPosition(x);
145   double p[3];
146   this->Point1Representation->GetWorldPosition(p);
147   this->Point1Representation->SetWorldPosition(p);
148 }
149 
150 //------------------------------------------------------------------------------
SetPoint2DisplayPosition(double x[3])151 void vtkBiDimensionalRepresentation::SetPoint2DisplayPosition(double x[3])
152 {
153   this->Point2Representation->SetDisplayPosition(x);
154   double p[3];
155   this->Point2Representation->GetWorldPosition(p);
156   this->Point2Representation->SetWorldPosition(p);
157 }
158 
159 //------------------------------------------------------------------------------
SetPoint3DisplayPosition(double x[3])160 void vtkBiDimensionalRepresentation::SetPoint3DisplayPosition(double x[3])
161 {
162   this->Point3Representation->SetDisplayPosition(x);
163   double p[3];
164   this->Point3Representation->GetWorldPosition(p);
165   this->Point3Representation->SetWorldPosition(p);
166 }
167 
168 //------------------------------------------------------------------------------
SetPoint4DisplayPosition(double x[3])169 void vtkBiDimensionalRepresentation::SetPoint4DisplayPosition(double x[3])
170 {
171   this->Point4Representation->SetDisplayPosition(x);
172   double p[3];
173   this->Point4Representation->GetWorldPosition(p);
174   this->Point4Representation->SetWorldPosition(p);
175 }
176 
177 //------------------------------------------------------------------------------
SetPoint1WorldPosition(double x[3])178 void vtkBiDimensionalRepresentation::SetPoint1WorldPosition(double x[3])
179 {
180   this->Point1Representation->SetWorldPosition(x);
181 }
182 
183 //------------------------------------------------------------------------------
SetPoint2WorldPosition(double x[3])184 void vtkBiDimensionalRepresentation::SetPoint2WorldPosition(double x[3])
185 {
186   this->Point2Representation->SetWorldPosition(x);
187 }
188 
189 //------------------------------------------------------------------------------
SetPoint3WorldPosition(double x[3])190 void vtkBiDimensionalRepresentation::SetPoint3WorldPosition(double x[3])
191 {
192   this->Point3Representation->SetWorldPosition(x);
193 }
194 
195 //------------------------------------------------------------------------------
SetPoint4WorldPosition(double x[3])196 void vtkBiDimensionalRepresentation::SetPoint4WorldPosition(double x[3])
197 {
198   this->Point4Representation->SetWorldPosition(x);
199 }
200 
201 //------------------------------------------------------------------------------
GetPoint1DisplayPosition(double pos[3])202 void vtkBiDimensionalRepresentation::GetPoint1DisplayPosition(double pos[3])
203 {
204   this->Point1Representation->GetDisplayPosition(pos);
205   pos[2] = 0.0;
206 }
207 
208 //------------------------------------------------------------------------------
GetPoint2DisplayPosition(double pos[3])209 void vtkBiDimensionalRepresentation::GetPoint2DisplayPosition(double pos[3])
210 {
211   this->Point2Representation->GetDisplayPosition(pos);
212   pos[2] = 0.0;
213 }
214 
215 //------------------------------------------------------------------------------
GetPoint3DisplayPosition(double pos[3])216 void vtkBiDimensionalRepresentation::GetPoint3DisplayPosition(double pos[3])
217 {
218   this->Point3Representation->GetDisplayPosition(pos);
219   pos[2] = 0.0;
220 }
221 
222 //------------------------------------------------------------------------------
GetPoint4DisplayPosition(double pos[3])223 void vtkBiDimensionalRepresentation::GetPoint4DisplayPosition(double pos[3])
224 {
225   this->Point4Representation->GetDisplayPosition(pos);
226   pos[2] = 0.0;
227 }
228 
229 //------------------------------------------------------------------------------
InstantiateHandleRepresentation()230 void vtkBiDimensionalRepresentation::InstantiateHandleRepresentation()
231 {
232   if (!this->Point1Representation)
233   {
234     this->Point1Representation = this->HandleRepresentation->NewInstance();
235     this->Point1Representation->ShallowCopy(this->HandleRepresentation);
236   }
237 
238   if (!this->Point2Representation)
239   {
240     this->Point2Representation = this->HandleRepresentation->NewInstance();
241     this->Point2Representation->ShallowCopy(this->HandleRepresentation);
242   }
243 
244   if (!this->Point3Representation)
245   {
246     this->Point3Representation = this->HandleRepresentation->NewInstance();
247     this->Point3Representation->ShallowCopy(this->HandleRepresentation);
248   }
249 
250   if (!this->Point4Representation)
251   {
252     this->Point4Representation = this->HandleRepresentation->NewInstance();
253     this->Point4Representation->ShallowCopy(this->HandleRepresentation);
254   }
255 }
256 
257 //------------------------------------------------------------------------------
GetLength1()258 double vtkBiDimensionalRepresentation::GetLength1()
259 {
260   double x1[3], x2[3];
261 
262   this->GetPoint1WorldPosition(x1);
263   this->GetPoint2WorldPosition(x2);
264 
265   return sqrt(vtkMath::Distance2BetweenPoints(x1, x2));
266 }
267 
268 //------------------------------------------------------------------------------
GetLength2()269 double vtkBiDimensionalRepresentation::GetLength2()
270 {
271   double x3[3], x4[3];
272 
273   this->GetPoint3WorldPosition(x3);
274   this->GetPoint4WorldPosition(x4);
275 
276   return sqrt(vtkMath::Distance2BetweenPoints(x3, x4));
277 }
278 
279 //------------------------------------------------------------------------------
SetID(vtkIdType id)280 void vtkBiDimensionalRepresentation::SetID(vtkIdType id)
281 {
282   if (id == this->ID)
283   {
284     return;
285   }
286 
287   this->ID = id;
288   this->IDInitialized = 1;
289   this->Modified();
290 }
291 
292 //------------------------------------------------------------------------------
PrintSelf(ostream & os,vtkIndent indent)293 void vtkBiDimensionalRepresentation::PrintSelf(ostream& os, vtkIndent indent)
294 {
295   // Superclass typedef defined in vtkTypeMacro() found in vtkSetGet.h
296   this->Superclass::PrintSelf(os, indent);
297 
298   os << indent << "Tolerance: " << this->Tolerance << "\n";
299 
300   os << indent << "Length1: " << this->GetLength1() << "\n";
301   os << indent << "Length2: " << this->GetLength2() << "\n";
302 
303   os << indent << "Line1 Visibility: " << (this->Line1Visibility ? "On\n" : "Off\n");
304   os << indent << "Line2 Visibility: " << (this->Line2Visibility ? "On\n" : "Off\n");
305 
306   os << indent << "Handle Representation: " << this->HandleRepresentation << "\n";
307 
308   os << indent << "ID: " << this->ID << "\n";
309 
310   double labelPosition[3] = { 0.0, 0.0, 0.0 };
311   this->GetLabelPosition(labelPosition);
312   os << indent << "Label Position: (" << labelPosition[0] << ", " << labelPosition[1] << ","
313      << labelPosition[2] << ")\n";
314 
315   if (this->Renderer)
316   {
317     double worldLabelPosition[3] = { 0.0, 0.0, 0.0 };
318     this->GetWorldLabelPosition(worldLabelPosition);
319     os << indent << "World Label Position: (" << worldLabelPosition[0] << ", "
320        << worldLabelPosition[1] << "," << worldLabelPosition[2] << ")\n";
321   }
322 
323   os << indent << "Label Text: " << this->GetLabelText() << "\n";
324   os << indent << "Label Format: ";
325   if (this->LabelFormat)
326   {
327     os << this->LabelFormat << "\n";
328   }
329   else
330   {
331     os << "(null))\n";
332   }
333 
334   os << indent << "Point1 Representation\n";
335   this->Point1Representation->PrintSelf(os, indent.GetNextIndent());
336 
337   os << indent << "Point2 Representation\n";
338   this->Point2Representation->PrintSelf(os, indent.GetNextIndent());
339 
340   os << indent << "Point3 Representation\n";
341   this->Point3Representation->PrintSelf(os, indent.GetNextIndent());
342 
343   os << indent << "Point4 Representation\n";
344   this->Point4Representation->PrintSelf(os, indent.GetNextIndent());
345 
346   os << indent << "Show Label Above Widget: " << (this->ShowLabelAboveWidget ? "On\n" : "Off\n");
347 }
348