7 #include "fwRenderVTK/vtk/fwHandleRepresentation3D.hpp" 10 #include <vtkCleanPolyData.h> 11 #include <vtkCubeSource.h> 12 #include <vtkCylinderSource.h> 13 #include <vtkFollower.h> 14 #include <vtkInteractorObserver.h> 16 #include <vtkObjectFactory.h> 17 #include <vtkPolyDataMapper.h> 18 #include <vtkPolyDataNormals.h> 19 #include <vtkProperty.h> 20 #include <vtkRenderer.h> 21 #include <vtkSphereSource.h> 22 #include <vtkTransform.h> 23 #include <vtkTransformPolyDataFilter.h> 31 vtkStandardNewMacro(fwHandleRepresentation3D);
34 fwHandleRepresentation3D::fwHandleRepresentation3D()
37 this->CubeSource = vtkSmartPointer<vtkCubeSource>::New();
40 this->SphereSource = vtkSmartPointer<vtkSphereSource>::New();
41 this->SphereSource->SetThetaResolution(20);
42 this->SphereSource->SetPhiResolution(20);
43 this->SphereSource->Update();
44 this->SetHandle(this->SphereSource->GetOutput());
46 this->GetSelectedProperty()->SetColor(0., 1., 0.);
48 this->ShapeRepresentation = SPHERE;
50 auto cylinderSource = vtkSmartPointer<vtkCylinderSource>::New();
51 cylinderSource->SetCenter(0., -1., 0.);
52 cylinderSource->SetResolution(64);
53 cylinderSource->SetHeight(0.);
54 this->Marker = cylinderSource;
56 this->CleanPolyData = vtkSmartPointer<vtkCleanPolyData>::New();
57 this->CleanPolyData->PointMergingOn();
58 this->CleanPolyData->CreateDefaultLocator();
59 this->CleanPolyData->SetInputConnection(0, this->Marker->GetOutputPort(0));
61 auto MarkerNormals = vtkSmartPointer<vtkPolyDataNormals>::New();
62 MarkerNormals->SetInputConnection( 0, this->CleanPolyData->GetOutputPort(0) );
64 this->MarkerMapper = vtkSmartPointer<vtkPolyDataMapper>::New();
65 this->MarkerMapper->SetInputConnection( MarkerNormals->GetOutputPort() );
67 this->Follower = vtkSmartPointer<vtkFollower>::New();
68 this->Follower->SetMapper(this->MarkerMapper);
69 this->Follower->RotateX(90);
72 this->CreateDefaultProperties();
74 this->MarkerRadiusFactor = 1.2;
75 this->SetMarkerProperty(this->MarkerProperty);
76 cylinderSource->SetRadius(this->MarkerRadiusFactor * this->SphereSource->GetRadius());
81 fwHandleRepresentation3D::~fwHandleRepresentation3D()
88 void fwHandleRepresentation3D::PrintSelf(ostream& os, vtkIndent indent)
90 this->Superclass::PrintSelf(os, indent);
92 os << indent <<
"HandleSize: " << this->HandleSize << endl;
93 os << indent <<
"ShapeRepresentation: " << (this->ShapeRepresentation == SPHERE ?
"SPHERE" :
"CUBE") << endl;
94 if (this->SphereSource)
96 this->SphereSource->PrintSelf(os, indent.GetNextIndent());
100 this->CubeSource->PrintSelf(os, indent.GetNextIndent());
106 void fwHandleRepresentation3D::SetWorldPosition(
double p[3])
108 this->vtkPolygonalHandleRepresentation3D::SetWorldPosition(p);
109 this->Follower->SetPosition(this->GetWorldPosition());
116 void fwHandleRepresentation3D::CreateDefaultProperties()
118 this->MarkerProperty = vtkProperty::New();
119 this->MarkerProperty->SetColor(1., 1., 0.);
120 this->MarkerProperty->SetOpacity(1.);
125 void fwHandleRepresentation3D::SetShapeRepresentation( Shape shape)
127 this->ShapeRepresentation = shape;
128 if (this->ShapeRepresentation == SPHERE)
130 vtkSmartPointer<vtkCylinderSource> cynlinderSource = vtkCylinderSource::SafeDownCast(this->Marker);
131 if (!cynlinderSource)
133 cynlinderSource = vtkCylinderSource::New();
134 cynlinderSource->SetCenter(0., -1., 0.);
135 cynlinderSource->SetResolution(64);
136 cynlinderSource->SetHeight(0.);
137 cynlinderSource->SetRadius(this->MarkerRadiusFactor * this->SphereSource->GetRadius() );
138 this->Marker = cynlinderSource;
139 this->CleanPolyData->SetInputConnection(0, this->Marker->GetOutputPort(0));
142 this->SetHandle(this->SphereSource->GetOutput());
146 vtkSmartPointer<vtkCubeSource> cubeSource = vtkCubeSource::SafeDownCast(this->Marker);
149 cubeSource = vtkCubeSource::New();
150 cubeSource->SetCenter(0., -1., 0.);
151 cubeSource->SetXLength(this->MarkerRadiusFactor * this->CubeSource->GetXLength() );
152 cubeSource->SetYLength(0.);
153 cubeSource->SetZLength(this->MarkerRadiusFactor * this->CubeSource->GetXLength() );
154 this->Marker = cubeSource;
155 this->CleanPolyData->SetInputConnection(0, this->Marker->GetOutputPort(0));
158 this->SetHandle(this->CubeSource->GetOutput());
163 void fwHandleRepresentation3D::BuildRepresentation()
165 if (!this->GetRenderer() ||
166 !this->GetRenderer()->GetActiveCamera())
172 if (this->ShapeRepresentation == SPHERE)
174 currLength = this->SphereSource->GetRadius()*2;
178 currLength = this->CubeSource->GetXLength();
181 if (currLength != this->HandleSize)
184 if (this->ShapeRepresentation == SPHERE)
186 this->SphereSource->SetRadius(this->HandleSize / 2.);
187 this->SphereSource->Update();
189 vtkSmartPointer<vtkCylinderSource> cynlinderSource = vtkCylinderSource::SafeDownCast(this->Marker);
192 cynlinderSource->SetRadius(this->MarkerRadiusFactor * this->HandleSize / 2. );
195 this->SetHandle(this->SphereSource->GetOutput());
199 this->CubeSource->SetXLength(this->HandleSize);
200 this->CubeSource->SetYLength(this->HandleSize);
201 this->CubeSource->SetZLength(this->HandleSize);
202 this->CubeSource->Update();
204 vtkSmartPointer<vtkCubeSource> cubeSource = vtkCubeSource::SafeDownCast(this->Marker);
207 cubeSource->SetXLength(this->MarkerRadiusFactor * this->HandleSize );
208 cubeSource->SetYLength(0.);
209 cubeSource->SetZLength(this->MarkerRadiusFactor * this->HandleSize );
212 this->SetHandle(this->CubeSource->GetOutput());
215 this->Follower->SetCamera( this->GetRenderer()->GetActiveCamera() );
216 this->Marker->Update();
218 const double textScale = 10;
219 this->SetLabelTextScale(textScale, textScale, textScale);
224 this->BuildTime.Modified();
230 void fwHandleRepresentation3D::GetActors(vtkPropCollection* pc)
232 this->Actor->GetActors(pc);
233 this->LabelTextActor->GetActors(pc);
234 this->Follower->GetActors(pc);
239 void fwHandleRepresentation3D::ReleaseGraphicsResources(vtkWindow* win)
241 this->Actor->ReleaseGraphicsResources(win);
242 this->LabelTextActor->ReleaseGraphicsResources(win);
243 this->Follower->ReleaseGraphicsResources(win);
248 int fwHandleRepresentation3D::RenderOpaqueGeometry(vtkViewport* viewport)
250 this->BuildRepresentation();
252 if (this->GetRenderer()->GetActiveCamera()->GetParallelProjection())
254 ret = this->Follower->RenderOpaqueGeometry(viewport);
256 if (this->HandleVisibility)
258 ret += this->Actor->RenderOpaqueGeometry(viewport);
260 if (this->LabelVisibility)
262 ret += this->LabelTextActor->RenderOpaqueGeometry(viewport);
270 int fwHandleRepresentation3D::RenderTranslucentPolygonalGeometry(vtkViewport* viewport)
272 this->BuildRepresentation();
274 if (this->GetRenderer()->GetActiveCamera()->GetParallelProjection())
276 ret = this->Follower->RenderTranslucentPolygonalGeometry(viewport);
278 if (this->HandleVisibility)
280 ret += this->Actor->RenderTranslucentPolygonalGeometry(viewport);
282 if (this->LabelVisibility)
284 ret += this->LabelTextActor->RenderTranslucentPolygonalGeometry(viewport);
286 return this->Actor->RenderTranslucentPolygonalGeometry(viewport) + ret;
291 int fwHandleRepresentation3D::HasTranslucentPolygonalGeometry()
298 void fwHandleRepresentation3D::SetMarkerProperty(vtkProperty* p)
300 vtkSetObjectBodyMacro(MarkerProperty, vtkProperty, p);
303 this->Follower->SetProperty( p );
The namespace fwRenderVTK contains classes for rendering with VTK.