14 #include <boost/shared_ptr.hpp> 15 #include <vtkRenderer.h> 16 #include <vtkCamera.h> 17 #include <vtkImageActor.h> 18 #include <vtkSelectVisiblePoints.h> 38 CustomMetricRep::CustomMetricRep()
45 mMeshGeometry.clear();
46 for(
int i = 0; i < mImageGeometryProxy.size(); ++i)
48 this->
getRenderer()->RemoveActor(mImageGeometryProxy[i]->getActor());
50 mImageGeometryProxy.clear();
69 this->hideDistanceMetrics();
72 void CustomMetricRep::updateModel()
77 if (!this->
getView() || !custom)
80 DataPtr model = custom->getModel();
82 if(custom->modelIsImage())
83 this->updateImageModel(model);
85 this->updateMeshModel(model);
86 this->createDistanceMarkers();
89 void CustomMetricRep::updateImageModel(
DataPtr model)
91 ImagePtr imageModel = boost::dynamic_pointer_cast<
Image>(model);
93 if(!imageModel && !imageModel->is2D())
97 std::vector<Transform3D> pos = custom->calculateOrientations();
99 mImageGeometryProxy.resize(pos.size());
101 for(
unsigned i = 0; i < mImageGeometryProxy.size(); ++i)
103 if(!mImageGeometryProxy[i])
106 mImageGeometryProxy[i]->setImage(imageModel);
107 this->
getRenderer()->AddActor(mImageGeometryProxy[i]->getActor());
109 mImageGeometryProxy[i]->setTransformOffset(pos[i]);
113 void CustomMetricRep::updateMeshModel(
DataPtr model)
115 MeshPtr meshModel = boost::dynamic_pointer_cast<
Mesh>(model);
118 std::vector<Transform3D> pos = custom->calculateOrientations();
120 mMeshGeometry.resize(pos.size());
122 for (
unsigned i=0; i<mMeshGeometry.size(); ++i)
124 if (!mMeshGeometry[i])
127 mMeshGeometry[i]->setRenderer(this->
getRenderer());
130 mMeshGeometry[i]->setMesh(meshModel);
132 mMeshGeometry[i]->setTransformOffset(pos[i]);
134 custom->updateTexture(meshModel, pos[i]);
139 void CustomMetricRep::createDistanceMarkers()
141 mDistanceText.clear();
143 if(!custom->getModel() || !custom->getShowDistanceMarkers())
145 std::vector<Transform3D> pos = custom->calculateOrientations();
153 vtkSmartPointer<vtkPoints> points = vtkSmartPointer<vtkPoints>::New();
154 points->SetNumberOfPoints(pos.size());
156 mDistanceText.resize(pos.size());
157 Vector3D pos_0 = custom->getZeroPosition();
158 for(
unsigned i = 0; i < mDistanceText.size(); ++i)
161 double distance = (pos_i - pos_0).
length();
164 mDistanceText[i] = this->createDistanceText(pos[i].coord(textpos), distance);
166 Vector3D point = pos[i].coord(textpos);
167 vtkIdType pointId = i;
168 points->SetPoint(pointId, point.data());
175 text->setColor(
mMetric->getColor());
176 text->setText(QString(
"%1").arg(distance));
178 text->setPosition(pos);
179 text->placeAboveCenter();
186 void CustomMetricRep::hideDistanceMetrics()
188 if(mDistanceText.empty())
190 static vtkSmartPointer<vtkSelectVisiblePoints> visPts = vtkSmartPointer<vtkSelectVisiblePoints>::New();
192 float * zbuffer = visPts->Initialize(
true);
194 for(
unsigned i = 0; i < mDistanceText.size(); ++i)
196 Vector3D pos = mDistanceText[i]->getPosition();
197 bool visible = visPts->IsPointOccluded(pos.data(), zbuffer);
198 bool closeToCamera = this->isCloseToCamera(pos);
199 mDistanceText[i]->setVisibility(visible && closeToCamera);
204 bool CustomMetricRep::isCloseToCamera(
Vector3D pos)
206 double distanceThreshold = this->getCustomMetric()->getDistanceMarkerVisibility();
209 double distance = diff.norm();
210 if(distance < distanceThreshold)
vtkRendererPtr getRenderer()
virtual void onModifiedStartRender()
virtual void onEveryRender()
boost::shared_ptr< class Image > ImagePtr
Vector3D bottomLeft() const
static boost::shared_ptr< REP > wrap_new(REP *object, QString uid)
boost::shared_ptr< class Data > DataPtr
Data class that represents a custom.
boost::shared_ptr< class CustomMetricRep > CustomMetricRepPtr
Helper for rendering 3D text that faces the camera and has a constant viewed size, always on top.
Representation of a floating-point bounding box in 3D. The data are stored as {xmin,xmax,ymin,ymax,zmin,zmax}, in order to simplify communication with vtk.
Eigen::Vector3d Vector3D
Vector3D is a representation of a point or vector in 3D.
RealScalar length() const
boost::shared_ptr< class Mesh > MeshPtr
boost::shared_ptr< CaptionText3D > CaptionText3DPtr
static Image2DProxyPtr New()
boost::shared_ptr< class CustomMetric > CustomMetricPtr
static CustomMetricRepPtr New(const QString &uid="")
Namespace for all CustusX production code.