PlusLib  2.9.0
Software library for tracked ultrasound image acquisition, calibration, and processing.
vtkPlusToolAxesActor.cxx
Go to the documentation of this file.
1 /*=Plus=header=begin======================================================
2 Program: Plus
3 Copyright (c) Laboratory for Percutaneous Surgery. All rights reserved.
4 See License.txt for details.
5 =========================================================Plus=header=end*/
6 
7 #include "PlusConfigure.h"
8 
9 #include "vtkPlusToolAxesActor.h"
10 
11 #include "vtkSmartPointer.h"
12 #include "vtkActor.h"
13 #include "vtkCaptionActor2D.h"
14 #include "vtkConeSource.h"
15 #include "vtkLineSource.h"
16 #include "vtkObject.h"
17 #include "vtkObjectFactory.h"
18 #include "vtkPolyData.h"
19 #include "vtkPolyDataMapper.h"
20 #include "vtkPropCollection.h"
21 #include "vtkProperty.h"
22 #include "vtkRenderer.h"
23 #include "vtkRenderWindow.h"
24 #include "vtkTextProperty.h"
25 #include "vtkTransform.h"
26 #include "vtkTextActor.h"
27 #include "vtkTubeFilter.h"
28 #include "vtkAppendPolyData.h"
29 
31 
32 //----------------------------------------------------------------------------
34 {
35  // Shaft
36  this->ArrowShaftActor=vtkActor::New();
37  this->ArrowShaftLineSource=vtkLineSource::New();
38  vtkSmartPointer<vtkPolyDataMapper> mapper=vtkSmartPointer<vtkPolyDataMapper>::New();
39  this->ArrowShaftActor->SetMapper(mapper);
40  this->TubeFilter = vtkTubeFilter::New();
41  this->TubeFilter->SetInputConnection(this->ArrowShaftLineSource->GetOutputPort());
42  this->TubeFilter->CappingOn();
43  this->TubeFilter->SetNumberOfSides(32);
44  this->TubeFilter->SetRadius(0.03);
45 
46  this->ArrowTipConeSource = vtkConeSource::New();
47  this->ArrowTipConeSource->SetResolution(32);
48  this->ArrowTipConeSource->SetRadius(0.06);
49  this->ArrowTipConeSource->SetHeight(0.1);
50 
51  vtkSmartPointer<vtkAppendPolyData> appendFilter = vtkSmartPointer<vtkAppendPolyData>::New();
52  appendFilter->AddInputConnection(this->TubeFilter->GetOutputPort());
53  appendFilter->AddInputConnection(this->ArrowTipConeSource->GetOutputPort());
54 
55  mapper->SetInputConnection(appendFilter->GetOutputPort());
56 
57  // Label
58  this->LabelActor=vtkCaptionActor2D::New();
59  this->LabelActor->SetHeight(0.02);
60  this->LabelActor->LeaderOff();
61  this->LabelActor->SetPadding(0);
62  this->LabelActor->BorderOff();
63  vtkTextProperty* textprop = this->LabelActor->GetTextActor()->GetTextProperty();
64  textprop->ItalicOff();
65  textprop->SetJustificationToLeft();
66  textprop->SetVerticalJustificationToCentered();
67 }
68 
69 //----------------------------------------------------------------------------
71 {
72  if (this->ArrowShaftActor)
73  {
74  this->ArrowShaftActor->Delete();
75  this->ArrowShaftActor=NULL;
76  }
77 
78  if (this->TubeFilter)
79  {
80  this->TubeFilter->Delete();
81  this->TubeFilter=NULL;
82  }
83 
84  if (this->ArrowShaftLineSource)
85  {
86  this->ArrowShaftLineSource->Delete();
87  this->ArrowShaftLineSource=NULL;
88  }
89 
90  if (this->ArrowTipConeSource)
91  {
92  this->ArrowTipConeSource->Delete();
93  this->ArrowTipConeSource=NULL;
94  }
95 
96  if (this->LabelActor)
97  {
98  this->LabelActor->Delete();
99  this->LabelActor=NULL;
100  }
101 }
102 
103 //----------------------------------------------------------------------------
105 {
106  this->ArrowShaftActor->ReleaseGraphicsResources( win );
107  this->LabelActor->ReleaseGraphicsResources( win );
108 }
109 
110 
111 //----------------------------------------------------------------------------
112 //----------------------------------------------------------------------------
114 {
115  this->Axes[0].LabelActor->SetCaption("X");
116  this->Axes[0].ArrowShaftActor->GetProperty()->SetColor(1, 0, 0);
117  this->Axes[1].LabelActor->SetCaption("Y");
118  this->Axes[1].ArrowShaftActor->GetProperty()->SetColor(0, 1, 0);
119  this->Axes[2].LabelActor->SetCaption("Z");
120  this->Axes[2].ArrowShaftActor->GetProperty()->SetColor(0, 0, 1);
121 
122  this->NameLabelActor=vtkCaptionActor2D::New();
123  this->NameLabelActor->SetHeight(0.03);
124  this->NameLabelActor->BorderOff();
125  this->NameLabelActor->LeaderOff();
126  this->NameLabelActor->SetPadding(0);
127  vtkTextProperty* textprop = this->NameLabelActor->GetTextActor()->GetTextProperty();
128  textprop->ItalicOff();
129  textprop->SetJustificationToLeft();
130  textprop->SetVerticalJustificationToCentered();
131 
132  this->ShaftLength = 0.0;
133  this->SetShaftLength(100.0);
134 
135  this->ShowLabelsOn();
136  this->ShowNameOff();
137 
138  this->UpdateProps();
139 }
140 
141 //----------------------------------------------------------------------------
143 {
144  this->NameLabelActor->Delete();
145  this->NameLabelActor=NULL;
146 }
147 
148 //----------------------------------------------------------------------------
149 void vtkPlusToolAxesActor::GetActors(vtkPropCollection *ac)
150 {
151  ac->AddItem(this->Axes[0].ArrowShaftActor);
152  ac->AddItem(this->Axes[1].ArrowShaftActor);
153  ac->AddItem(this->Axes[2].ArrowShaftActor);
154 
155  if (this->ShowLabels)
156  {
157  ac->AddItem(this->Axes[0].LabelActor);
158  ac->AddItem(this->Axes[1].LabelActor);
159  ac->AddItem(this->Axes[2].LabelActor);
160  }
161 
162  if (this->ShowName)
163  {
164  ac->AddItem(this->NameLabelActor);
165  }
166 }
167 
168 //----------------------------------------------------------------------------
170 {
171  int renderedSomething = 0;
172 
173  this->UpdateProps();
174 
175  renderedSomething += this->Axes[0].ArrowShaftActor->RenderOpaqueGeometry( vp );
176  renderedSomething += this->Axes[1].ArrowShaftActor->RenderOpaqueGeometry( vp );
177  renderedSomething += this->Axes[2].ArrowShaftActor->RenderOpaqueGeometry( vp );
178 
179  if (this->ShowLabels)
180  {
181  renderedSomething += this->Axes[0].LabelActor->RenderOpaqueGeometry( vp );
182  renderedSomething += this->Axes[1].LabelActor->RenderOpaqueGeometry( vp );
183  renderedSomething += this->Axes[2].LabelActor->RenderOpaqueGeometry( vp );
184  }
185 
186  if (this->ShowName)
187  {
188  renderedSomething += this->NameLabelActor->RenderOpaqueGeometry( vp );
189  }
190 
191  renderedSomething = (renderedSomething > 0)?(1):(0);
192  return renderedSomething;
193 }
194 
195 //-----------------------------------------------------------------------------
197 {
198  int renderedSomething = 0;
199 
200  this->UpdateProps();
201 
202  renderedSomething += this->Axes[0].ArrowShaftActor->RenderTranslucentPolygonalGeometry( vp );
203  renderedSomething += this->Axes[1].ArrowShaftActor->RenderTranslucentPolygonalGeometry( vp );
204  renderedSomething += this->Axes[2].ArrowShaftActor->RenderTranslucentPolygonalGeometry( vp );
205 
206  if (this->ShowLabels)
207  {
208  renderedSomething += this->Axes[0].LabelActor->RenderTranslucentPolygonalGeometry( vp );
209  renderedSomething += this->Axes[1].LabelActor->RenderTranslucentPolygonalGeometry( vp );
210  renderedSomething += this->Axes[2].LabelActor->RenderTranslucentPolygonalGeometry( vp );
211  }
212 
213  if (this->ShowName)
214  {
215  renderedSomething += this->NameLabelActor->RenderTranslucentPolygonalGeometry( vp );
216  }
217 
218  renderedSomething = (renderedSomething > 0)?(1):(0);
219  return renderedSomething;
220 }
221 
222 //-----------------------------------------------------------------------------
223 // Description:
224 // Does this prop have some translucent polygonal geometry?
226 {
227  int result = 0;
228 
229  this->UpdateProps();
230 
231  result |= this->Axes[0].ArrowShaftActor->HasTranslucentPolygonalGeometry();
232  result |= this->Axes[1].ArrowShaftActor->HasTranslucentPolygonalGeometry();
233  result |= this->Axes[2].ArrowShaftActor->HasTranslucentPolygonalGeometry();
234 
235  if (this->ShowLabels)
236  {
237  result |= this->Axes[0].LabelActor->HasTranslucentPolygonalGeometry();
238  result |= this->Axes[1].LabelActor->HasTranslucentPolygonalGeometry();
239  result |= this->Axes[2].LabelActor->HasTranslucentPolygonalGeometry();
240  }
241 
242  if (this->ShowName)
243  {
244  result |= this->NameLabelActor->HasTranslucentPolygonalGeometry();
245  }
246 
247  return result;
248 }
249 
250 //-----------------------------------------------------------------------------
252 {
253  int renderedSomething = 0;
254 
255  this->UpdateProps();
256 
257  if (this->ShowLabels)
258  {
259  renderedSomething += this->Axes[0].LabelActor->RenderOverlay( vp );
260  renderedSomething += this->Axes[1].LabelActor->RenderOverlay( vp );
261  renderedSomething += this->Axes[2].LabelActor->RenderOverlay( vp );
262  }
263 
264  if (this->ShowName)
265  {
266  renderedSomething += this->NameLabelActor->RenderOverlay( vp );
267  }
268 
269  return (renderedSomething > 0)?(1):(0);
270 }
271 
272 //----------------------------------------------------------------------------
274 {
275  this->Axes[0].ReleaseGraphicsResources( win );
276  this->Axes[1].ReleaseGraphicsResources( win );
277  this->Axes[2].ReleaseGraphicsResources( win );
278 
279  this->NameLabelActor->ReleaseGraphicsResources( win );
280 }
281 
282 //----------------------------------------------------------------------------
283 void vtkPlusToolAxesActor::GetBounds(double bounds[6])
284 {
285  double *bds = this->GetBounds();
286  bounds[0] = bds[0];
287  bounds[1] = bds[1];
288  bounds[2] = bds[2];
289  bounds[3] = bds[3];
290  bounds[4] = bds[4];
291  bounds[5] = bds[5];
292 }
293 
294 //----------------------------------------------------------------------------
295 void vtkPlusToolAxesActor::ExtendMaxBounds(vtkActor *actor, double* bounds)
296 {
297  double thisActorsBounds[6]={0};
298  actor->GetBounds(thisActorsBounds);
299  for ( int i = 0; i < 3; ++i )
300  {
301  if (thisActorsBounds[2*i]<bounds[2*i])
302  {
303  bounds[2*i] = thisActorsBounds[2*i];
304  }
305  if (thisActorsBounds[2*i+1]>bounds[2*i+1])
306  {
307  bounds[2*i+1] = thisActorsBounds[2*i+1];
308  }
309  }
310 }
311 
312 //----------------------------------------------------------------------------
313 // Get the bounds for this Actor as (Xmin,Xmax,Ymin,Ymax,Zmin,Zmax).
315 {
316  this->Axes[0].ArrowShaftActor->GetBounds(this->Bounds);
317  ExtendMaxBounds(this->Axes[1].ArrowShaftActor, this->Bounds);
318  ExtendMaxBounds(this->Axes[2].ArrowShaftActor, this->Bounds);
319 
320  return this->Bounds;
321 }
322 
323 //----------------------------------------------------------------------------
325 {
326  vtkMTimeType mTime = this->Superclass::GetMTime();
327  return mTime;
328 }
329 
330 //----------------------------------------------------------------------------
332 {
333  vtkMTimeType mTime = this->GetMTime();
334  return mTime;
335 }
336 
337 //----------------------------------------------------------------------------
339 {
340  // Get the Tool to World transform
341  if ( vtkTransform::SafeDownCast(this->GetUserTransform())==NULL )
342  {
343  vtkSmartPointer<vtkTransform> transform = vtkSmartPointer<vtkTransform>::New();
344  this->SetUserTransform(transform);
345  }
346  vtkTransform* toolToWorldTransform=vtkTransform::SafeDownCast(this->GetUserTransform());
347  if (toolToWorldTransform==NULL)
348  {
349  LOG_ERROR("vtkPlusToolAxesActor UserTransform is invalid");
350  return;
351  }
352 
353  // Compute the shaft origin and tip positions
354  double origin_Tool[4]={0,0,0,1}; // Tool coordinate system origin position (in the Tool coordinate system)
355  double origin_World[4]={0,0,0,1}; // Tool coordinate system origin position (in the World coordinate system)
356  toolToWorldTransform->MultiplyPoint(origin_Tool,origin_World);
357 
358  double shaftTip0_Tool[4]={this->ShaftLength,0,0,1}; // tip of the X shaft (in the Tool coordinate system)
359  double shaftTip0_World[4]={0,0,0,1}; // tip of the X shaft (in the World coordinate system)
360  toolToWorldTransform->MultiplyPoint(shaftTip0_Tool,shaftTip0_World);
361 
362  double shaftTip1_Tool[4]={0,this->ShaftLength,0,1}; // tip of the Y shaft (in the Tool coordinate system)
363  double shaftTip1_World[4]={0,0,0,1}; // tip of the Y shaft (in the World coordinate system)
364  toolToWorldTransform->MultiplyPoint(shaftTip1_Tool,shaftTip1_World);
365 
366  double shaftTip2_Tool[4]={0,0,this->ShaftLength,1}; // tip of the Z shaft (in the Tool coordinate system)
367  double shaftTip2_World[4]={0,0,0,1}; // tip of the Z shaft (in the World coordinate system)
368  toolToWorldTransform->MultiplyPoint(shaftTip2_Tool,shaftTip2_World);
369 
370  // Set axes endpoints
371  this->Axes[0].ArrowShaftLineSource->SetPoint1(origin_World);
372  this->Axes[1].ArrowShaftLineSource->SetPoint1(origin_World);
373  this->Axes[2].ArrowShaftLineSource->SetPoint1(origin_World);
374  this->Axes[0].ArrowShaftLineSource->SetPoint2(shaftTip0_World);
375  this->Axes[1].ArrowShaftLineSource->SetPoint2(shaftTip1_World);
376  this->Axes[2].ArrowShaftLineSource->SetPoint2(shaftTip2_World);
377 
378  // Position arrows
379  this->Axes[0].ArrowTipConeSource->SetCenter(shaftTip0_World[0], shaftTip0_World[1], shaftTip0_World[2]);
380  this->Axes[1].ArrowTipConeSource->SetCenter(shaftTip1_World[0], shaftTip1_World[1], shaftTip1_World[2]);
381  this->Axes[2].ArrowTipConeSource->SetCenter(shaftTip2_World[0], shaftTip2_World[1], shaftTip2_World[2]);
382  this->Axes[0].ArrowTipConeSource->SetDirection(shaftTip0_World[0]-origin_World[0], shaftTip0_World[1]-origin_World[1], shaftTip0_World[2]-origin_World[2]);
383  this->Axes[1].ArrowTipConeSource->SetDirection(shaftTip1_World[0]-origin_World[0], shaftTip1_World[1]-origin_World[1], shaftTip1_World[2]-origin_World[2]);
384  this->Axes[2].ArrowTipConeSource->SetDirection(shaftTip2_World[0]-origin_World[0], shaftTip2_World[1]-origin_World[1], shaftTip2_World[2]-origin_World[2]);
385 
386  // Draw axis labels
387  if (this->ShowLabels)
388  {
389  this->Axes[0].LabelActor->SetAttachmentPoint(shaftTip0_World);
390  this->Axes[1].LabelActor->SetAttachmentPoint(shaftTip1_World);
391  this->Axes[2].LabelActor->SetAttachmentPoint(shaftTip2_World);
392  }
393 
394  if (this->ShowName)
395  {
396  this->NameLabelActor->SetAttachmentPoint(origin_World);
397  }
398 }
399 
400 
401 //----------------------------------------------------------------------------
402 void vtkPlusToolAxesActor::PrintSelf(ostream& os, vtkIndent indent)
403 {
404  this->Superclass::PrintSelf(os,indent);
405 
406  os << indent << "Name: " << this->NameLabelActor->GetCaption() << endl;
407  os << indent << "ShaftLength: " << this->ShaftLength << endl;
408 }
409 
410 //----------------------------------------------------------------------------
411 void vtkPlusToolAxesActor::SetName(const std::string& name)
412 {
413  this->NameLabelActor->SetCaption(name.c_str());
414 }
415 
416 //----------------------------------------------------------------------------
417 void vtkPlusToolAxesActor::SetShaftLength(double shaftLength)
418 {
419  this->ShaftLength = shaftLength;
420 
421  this->Axes[0].TubeFilter->SetRadius(this->ShaftLength * 0.03);
422  this->Axes[1].TubeFilter->SetRadius(this->ShaftLength * 0.03);
423  this->Axes[2].TubeFilter->SetRadius(this->ShaftLength * 0.03);
424 
425  this->Axes[0].ArrowTipConeSource->SetRadius(this->ShaftLength * 0.06);
426  this->Axes[1].ArrowTipConeSource->SetRadius(this->ShaftLength * 0.06);
427  this->Axes[2].ArrowTipConeSource->SetRadius(this->ShaftLength * 0.06);
428 
429  this->Axes[0].ArrowTipConeSource->SetHeight(this->ShaftLength * 0.12);
430  this->Axes[1].ArrowTipConeSource->SetHeight(this->ShaftLength * 0.12);
431  this->Axes[2].ArrowTipConeSource->SetHeight(this->ShaftLength * 0.12);
432 }
virtual int RenderOpaqueGeometry(vtkViewport *viewport)
vtkStandardNewMacro(vtkPlusToolAxesActor)
virtual int RenderOverlay(vtkViewport *viewport)
virtual int RenderTranslucentPolygonalGeometry(vtkViewport *viewport)
for i
Actor for displaying coordinate system axes.
virtual void ShowNameOff()
void SetName(const std::string &name)
void ReleaseGraphicsResources(vtkWindow *)
void ExtendMaxBounds(vtkActor *actor, double *bounds)
virtual void ShowLabelsOn()
virtual int HasTranslucentPolygonalGeometry()
void SetShaftLength(double shaftLength)
virtual void GetActors(vtkPropCollection *)
void ReleaseGraphicsResources(vtkWindow *win)
vtkCaptionActor2D * NameLabelActor
virtual void PrintSelf(ostream &os, vtkIndent indent) VTK_OVERRIDE