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