18 #include "../DataManagement/mitkBoundingShapeUtil.h"
20 #include <vtkAppendPolyData.h>
21 #include <vtkCamera.h>
22 #include <vtkCubeSource.h>
23 #include <vtkDataSetMapper.h>
25 #include <vtkPolyData.h>
26 #include <vtkPolyDataMapper.h>
27 #include <vtkSphereSource.h>
28 #include <vtkTransformFilter.h>
32 class BoundingShapeVtkMapper3D::Impl
34 class LocalStorage :
public Mapper::BaseLocalStorage
40 LocalStorage(
const LocalStorage &) =
delete;
41 LocalStorage &operator=(
const LocalStorage &) =
delete;
43 std::vector<vtkSmartPointer<vtkSphereSource>> Handles;
44 vtkSmartPointer<vtkActor> Actor;
45 vtkSmartPointer<vtkActor> HandleActor;
46 vtkSmartPointer<vtkActor> SelectedHandleActor;
47 vtkSmartPointer<vtkPropAssembly> PropAssembly;
51 Impl() : DistanceFromCam(1.0)
56 for (
int i = 0; i < 6; ++i)
60 double DistanceFromCam;
61 std::vector<Handle> HandlePropertyList;
66 mitk::BoundingShapeVtkMapper3D::Impl::LocalStorage::LocalStorage()
67 : Actor(vtkSmartPointer<vtkActor>::
New()),
68 HandleActor(vtkSmartPointer<vtkActor>::
New()),
69 SelectedHandleActor(vtkSmartPointer<vtkActor>::
New()),
70 PropAssembly(vtkSmartPointer<vtkPropAssembly>::
New())
72 for (
int i = 0; i < 6; i++)
76 mitk::BoundingShapeVtkMapper3D::Impl::LocalStorage::~LocalStorage()
82 Superclass::SetDefaultProperties(node, renderer, overwrite);
85 mitk::BoundingShapeVtkMapper3D::BoundingShapeVtkMapper3D() :
m_Impl(new Impl)
89 mitk::BoundingShapeVtkMapper3D::~BoundingShapeVtkMapper3D()
101 if (actor ==
nullptr)
104 auto dataNode = this->GetDataNode();
106 if (dataNode ==
nullptr)
109 bool isVisible =
false;
110 dataNode->GetBoolProperty(
"Bounding Shape.3D Rendering", isVisible, renderer);
112 actor->SetVisibility(isVisible);
114 float lineWidth = 1.0f;
115 dataNode->GetFloatProperty(
"Bounding Shape.Line.Width", lineWidth, renderer);
117 auto property = actor->GetProperty();
118 property->SetLineWidth(lineWidth);
123 auto dataNode = this->GetDataNode();
125 if (dataNode ==
nullptr)
128 vtkCamera *camera = renderer->
GetVtkRenderer()->GetActiveCamera();
130 auto localStorage =
m_Impl->LocalStorageHandler.GetLocalStorage(renderer);
131 bool needGenerateData = localStorage->GetLastGenerateDataTime() < dataNode->GetMTime();
132 double distance = camera->GetDistance();
136 m_Impl->DistanceFromCam = distance;
137 needGenerateData =
true;
140 if (needGenerateData)
142 bool isVisible =
true;
143 dataNode->GetVisibility(isVisible, renderer);
147 localStorage->Actor->VisibilityOff();
153 if (geometryData ==
nullptr)
169 sqrt((p0[0] - p4[0]) * (p0[0] - p4[0]) + (p0[1] - p4[1]) * (p0[1] - p4[1]) + (p0[2] - p4[2]) * (p0[2] - p4[2]));
171 sqrt((p0[0] - p2[0]) * (p0[0] - p2[0]) + (p0[1] - p2[1]) * (p0[1] - p2[1]) + (p0[2] - p2[2]) * (p0[2] - p2[2]));
173 sqrt((p0[0] - p1[0]) * (p0[0] - p1[0]) + (p0[1] - p1[1]) * (p0[1] - p1[1]) + (p0[2] - p1[2]) * (p0[2] - p1[2]));
178 if (
m_Impl->HandlePropertyList.size() == 6)
188 m_Impl->HandlePropertyList[0].SetPosition(pointLeft);
189 m_Impl->HandlePropertyList[1].SetPosition(pointRight);
190 m_Impl->HandlePropertyList[2].SetPosition(pointTop);
191 m_Impl->HandlePropertyList[3].SetPosition(pointBottom);
192 m_Impl->HandlePropertyList[4].SetPosition(pointFront);
193 m_Impl->HandlePropertyList[5].SetPosition(pointBack);
197 cube->SetXLength(extent[0] / spacing[0]);
198 cube->SetYLength(extent[1] / spacing[1]);
199 cube->SetZLength(extent[2] / spacing[2]);
202 vtkSmartPointer<vtkMatrix4x4> imageTransform = geometry->GetVtkTransform()->GetMatrix();
204 translation->Translate(center[0] - imageTransform->GetElement(0, 3),
205 center[1] - imageTransform->GetElement(1, 3),
206 center[2] - imageTransform->GetElement(2, 3));
209 transform->SetMatrix(imageTransform);
210 transform->PostMultiply();
211 transform->Concatenate(translation);
216 transformFilter->SetInputData(cube->GetOutput());
217 transformFilter->SetTransform(transform);
218 transformFilter->Update();
221 vtkSmartPointer<vtkPolyData> polydata = transformFilter->GetPolyDataOutput();
222 if (polydata ==
nullptr)
224 localStorage->Actor->VisibilityOff();
229 dynamic_cast<mitk::DoubleProperty *
>(this->GetDataNode()->GetProperty(
"Bounding Shape.Handle Size Factor"));
232 if (handleSizeProperty !=
nullptr)
233 initialHandleSize = handleSizeProperty->GetValue();
235 initialHandleSize = 1.0 / 40.0;
238 ((camera->GetDistance() * std::tan(vtkMath::RadiansFromDegrees(camera->GetViewAngle()))) / 2.0) *
241 if (localStorage->PropAssembly->GetParts()->IsItemPresent(localStorage->HandleActor))
242 localStorage->PropAssembly->RemovePart(localStorage->HandleActor);
243 if (localStorage->PropAssembly->GetParts()->IsItemPresent(localStorage->Actor))
244 localStorage->PropAssembly->RemovePart(localStorage->Actor);
250 dynamic_cast<mitk::IntProperty *
>(dataNode->GetProperty(
"Bounding Shape.Active Handle ID"));
253 for (
auto &handle : localStorage->Handles)
255 Point3D handlecenter =
m_Impl->HandlePropertyList[i].GetPosition();
256 handle->SetCenter(handlecenter[0], handlecenter[1], handlecenter[2]);
257 handle->SetRadius(handlesize);
259 if (activeHandleId ==
nullptr)
261 appendPoly->AddInputConnection(handle->GetOutputPort());
265 if (activeHandleId->GetValue() !=
m_Impl->HandlePropertyList[i].GetIndex())
267 appendPoly->AddInputConnection(handle->GetOutputPort());
271 selectedhandlemapper->SetInputData(handle->GetOutput());
272 localStorage->SelectedHandleActor->SetMapper(selectedhandlemapper);
273 localStorage->SelectedHandleActor->GetProperty()->SetColor(0, 1, 0);
274 localStorage->SelectedHandleActor->GetMapper()->SetInputDataObject(handle->GetOutput());
275 localStorage->PropAssembly->AddPart(localStorage->SelectedHandleActor);
280 appendPoly->Update();
283 mapper->SetInputData(polydata);
286 handlemapper->SetInputData(appendPoly->GetOutput());
288 localStorage->Actor->SetMapper(mapper);
289 localStorage->Actor->GetMapper()->SetInputDataObject(polydata);
290 localStorage->Actor->GetProperty()->SetOpacity(0.3);
293 if (selectedColor !=
nullptr)
296 localStorage->Actor->GetProperty()->SetColor(color[0], color[1], color[2]);
299 localStorage->HandleActor->SetMapper(handlemapper);
300 if (activeHandleId ==
nullptr)
302 localStorage->HandleActor->GetProperty()->SetColor(1, 1, 1);
306 localStorage->HandleActor->GetProperty()->SetColor(1, 0, 0);
309 localStorage->HandleActor->GetMapper()->SetInputDataObject(appendPoly->GetOutput());
311 this->ApplyColorAndOpacityProperties(renderer, localStorage->Actor);
312 this->ApplyBoundingShapeProperties(renderer, localStorage->Actor);
314 this->ApplyColorAndOpacityProperties(renderer, localStorage->HandleActor);
315 this->ApplyBoundingShapeProperties(renderer, localStorage->HandleActor);
317 this->ApplyColorAndOpacityProperties(renderer, localStorage->SelectedHandleActor);
318 this->ApplyBoundingShapeProperties(renderer, localStorage->SelectedHandleActor);
325 localStorage->Actor->VisibilityOn();
326 localStorage->HandleActor->VisibilityOn();
327 localStorage->SelectedHandleActor->VisibilityOn();
329 localStorage->PropAssembly->AddPart(localStorage->Actor);
330 localStorage->PropAssembly->AddPart(localStorage->HandleActor);
331 localStorage->PropAssembly->VisibilityOn();
333 localStorage->UpdateGenerateDataTime();
338 return m_Impl->LocalStorageHandler.GetLocalStorage(renderer)->PropAssembly;
void ApplyColorAndOpacityProperties(BaseRenderer *, vtkActor *) override
Apply color and opacity properties read from the PropertyList. Called by mapper subclasses.
vtkProp * GetVtkProp(BaseRenderer *renderer) override
Organizes the rendering process.
DataCollection - Class to facilitate loading/accessing structured data.
std::vector< mitk::Point3D > GetCornerPoints(mitk::BaseGeometry::Pointer geometry, bool visualizationOffset)
helper function for calculating corner points of the bounding object from a given geometry ...
std::vector< int > GetHandleIndices(int index)
The ColorProperty class RGB color property.
void ApplyBoundingShapeProperties(BaseRenderer *renderer, vtkActor *)
Data class only having a BaseGeometry but not containing any specific data.
Point< ScalarType, 3 > Point3D
itk::RGBPixel< float > Color
Color Standard RGB color typedef (float)
void GenerateDataForRenderer(BaseRenderer *renderer) override
Generate the data needed for rendering into renderer.
const float selectedColor[]
MITKCORE_EXPORT const ScalarType eps
static void SetDefaultProperties(DataNode *node, BaseRenderer *renderer=nullptr, bool overwrite=false)
vtkRenderer * GetVtkRenderer() const
mitk::BaseGeometry * GetGeometry(int t=0) const
Return the geometry, which is a TimeGeometry, of the data as non-const pointer.
mitk::Point3D CalcAvgPoint(mitk::Point3D a, mitk::Point3D b)
helper function for calculating the average of two points
Class for nodes of the DataTree.
static itkEventMacro(BoundingShapeInteractionEvent, itk::AnyEvent) class MITKBOUNDINGSHAPE_EXPORT BoundingShapeInteractor Pointer New()
Basic interaction methods for mitk::GeometryData.