fw4spl
MarkedSphereHandleRepresentation.cpp
1 /* ***** BEGIN LICENSE BLOCK *****
2  * FW4SPL - Copyright (C) IRCAD, 2009-2015.
3  * Distributed under the terms of the GNU Lesser General Public License (LGPL) as
4  * published by the Free Software Foundation.
5  * ****** END LICENSE BLOCK ****** */
6 
7 #ifndef ANDROID
8 
9 #include "vtkAssemblyPath.h"
10 #include "vtkCellPicker.h"
11 #include "vtkCamera.h"
12 #include "vtkCleanPolyData.h"
13 #include "vtkCoordinate.h"
14 #include "vtkCylinderSource.h"
15 #include "vtkFollower.h"
16 #include "vtkInteractorObserver.h"
17 #include "vtkLine.h"
18 #include "vtkMath.h"
19 #include "vtkObjectFactory.h"
20 #include "vtkPolyDataMapper.h"
21 #include "vtkPolyDataNormals.h"
22 #include "vtkProperty.h"
23 #include "vtkRenderer.h"
24 #include "vtkRenderWindow.h"
25 
26 
27 #include "fwRenderVTK/vtk/MarkedSphereHandleRepresentation.hpp"
28 
29 namespace fwRenderVTK
30 {
31 
32 namespace vtk
33 {
34 
35 vtkStandardNewMacro(MarkedSphereHandleRepresentation);
36 
37 //----------------------------------------------------------------------
38 MarkedSphereHandleRepresentation::MarkedSphereHandleRepresentation() : vtkSphereHandleRepresentation()
39 {
40  this->Marker = vtkCylinderSource::New();
41  this->Marker->SetCenter(0.,-1.,0.);
42  this->Marker->SetResolution(64);
43  this->Marker->SetHeight(0.);
44 
45  this->CleanPolyData = vtkCleanPolyData::New();
46  this->CleanPolyData->PointMergingOn();
47  this->CleanPolyData->CreateDefaultLocator();
48  this->CleanPolyData->SetInputConnection(0, this->Marker->GetOutputPort(0));
49 
50  vtkPolyDataNormals *MarkerNormals = vtkPolyDataNormals::New();
51  MarkerNormals->SetInputConnection( 0, this->CleanPolyData->GetOutputPort(0) );
52 
53  this->MarkerMapper = vtkPolyDataMapper::New();
54  this->MarkerMapper->SetInputConnection( MarkerNormals->GetOutputPort() );
55  MarkerNormals->Delete();
56 
57  this->Follower = vtkFollower::New();
58  this->Follower->SetMapper(this->MarkerMapper);
59  this->Follower->RotateX(90);
60 
61  // Set up the initial properties, parent's one is called in parent's constructor
62  this->CreateDefaultProperties();
63 
64  this->MarkerRadiusFactor = 1.5;
65  this->SetMarkerProperty(this->MarkerProperty);
66  this->Marker->SetRadius(this->MarkerRadiusFactor * this->Sphere->GetRadius());
67 }
68 
69 //----------------------------------------------------------------------
70 MarkedSphereHandleRepresentation::~MarkedSphereHandleRepresentation()
71 {
72  this->Marker->Delete();
73  this->CleanPolyData->Delete();
74  this->MarkerMapper->Delete();
75  this->Follower->Delete();
76  this->MarkerProperty->Delete();
77 }
78 
79 //-------------------------------------------------------------------------
80 void MarkedSphereHandleRepresentation::SetWorldPosition(double p[3])
81 {
82  this->vtkSphereHandleRepresentation::SetWorldPosition(p);
83  this->Follower->SetPosition(this->GetWorldPosition());// p may have been clamped
84 }
85 
86 //----------------------------------------------------------------------
87 void MarkedSphereHandleRepresentation::CreateDefaultProperties()
88 {
89  this->MarkerProperty = vtkProperty::New();
90  this->MarkerProperty->SetColor(1., 1., 0.);
91  this->MarkerProperty->SetOpacity(0.5);
92 }
93 
94 //----------------------------------------------------------------------
95 void MarkedSphereHandleRepresentation::BuildRepresentation()
96 {
97  // The net effect is to resize the handle
98 // if ( this->GetMTime() > this->BuildTime ||
99 // (this->Renderer && this->Renderer->GetVTKWindow() &&
100 // this->Renderer->GetVTKWindow()->GetMTime() > this->BuildTime) )
101 // {
102  if ( !this->Placed )
103  {
104  this->ValidPick = 1;
105  this->Placed = 1;
106  }
107 
108  this->SizeBounds();
109  this->Sphere->Update();
110 
111  this->Follower->SetCamera( this->GetRenderer()->GetActiveCamera() );
112  this->Marker->SetRadius(this->MarkerRadiusFactor * this->Sphere->GetRadius() );
113  this->Marker->Update();
114  this->BuildTime.Modified();
115 // }
116 
117 }
118 //
119 //----------------------------------------------------------------------
120 void MarkedSphereHandleRepresentation::GetActors(vtkPropCollection *pc)
121 {
122  this->Actor->GetActors(pc);
123  this->Follower->GetActors(pc);
124 }
125 
126 //----------------------------------------------------------------------
127 void MarkedSphereHandleRepresentation::ReleaseGraphicsResources(vtkWindow *win)
128 {
129  this->Actor->ReleaseGraphicsResources(win);
130  this->Follower->ReleaseGraphicsResources(win);
131 }
132 
133 //----------------------------------------------------------------------
134 int MarkedSphereHandleRepresentation::RenderOpaqueGeometry(vtkViewport *viewport)
135 {
136  this->BuildRepresentation();
137  int ret = 0;
138  if (this->GetRenderer()->GetActiveCamera()->GetParallelProjection())
139  {
140  ret = this->Follower->RenderOpaqueGeometry(viewport);
141  }
142  return this->Actor->RenderOpaqueGeometry(viewport) + ret;
143 }
144 
145 //----------------------------------------------------------------------
146 int MarkedSphereHandleRepresentation::RenderTranslucentPolygonalGeometry(vtkViewport *viewport)
147 {
148  this->BuildRepresentation();
149  int ret = 0;
150  if (this->GetRenderer()->GetActiveCamera()->GetParallelProjection())
151  {
152  ret = this->Follower->RenderTranslucentPolygonalGeometry(viewport);
153  }
154  return this->Actor->RenderTranslucentPolygonalGeometry(viewport) + ret;
155 }
156 
157 //----------------------------------------------------------------------
158 int MarkedSphereHandleRepresentation::HasTranslucentPolygonalGeometry()
159 {
160  return 1;
161 }
162 
163 //----------------------------------------------------------------------
164 void MarkedSphereHandleRepresentation::SetMarkerProperty(vtkProperty * p)
165 {
166  vtkSetObjectBodyMacro(MarkerProperty, vtkProperty, p);
167  if (p)
168  {
169  this->Follower->SetProperty( p );
170  }
171 }
172 
173 //----------------------------------------------------------------------
174 void MarkedSphereHandleRepresentation::PrintSelf(ostream& os, vtkIndent indent)
175 {
176  this->vtkSphereHandleRepresentation::PrintSelf(os,indent);
177 
178  if ( this->MarkerProperty )
179  {
180  os << indent << "Selected Property: " << this->MarkerProperty << "\n";
181  }
182  else
183  {
184  os << indent << "Marker Property: (none)\n";
185  }
186 
187  this->Sphere->PrintSelf(os,indent.GetNextIndent());
188 }
189 
190 } // namespace vtk
191 
192 } // namespace fwRenderVTK
193 
194 #endif //ANDROID
The namespace fwRenderVTK contains classes for rendering with VTK.