35 #include <boost/shared_ptr.hpp> 36 #include <vtkRenderer.h> 37 #include <vtkCamera.h> 38 #include <vtkImageActor.h> 39 #include <vtkSelectVisiblePoints.h> 59 CustomMetricRep::CustomMetricRep()
66 mMeshGeometry.clear();
67 for(
int i = 0; i < mImageGeometryProxy.size(); ++i)
69 this->
getRenderer()->RemoveActor(mImageGeometryProxy[i]->getActor());
71 mImageGeometryProxy.clear();
90 this->hideDistanceMetrics();
93 void CustomMetricRep::updateModel()
98 if (!this->
getView() || !custom)
101 DataPtr model = custom->getModel();
103 if(custom->modelIsImage())
104 this->updateImageModel(model);
106 this->updateMeshModel(model);
107 this->createDistanceMarkers();
110 void CustomMetricRep::updateImageModel(
DataPtr model)
112 ImagePtr imageModel = boost::dynamic_pointer_cast<
Image>(model);
114 if(!imageModel && !imageModel->is2D())
118 std::vector<Transform3D> pos = custom->calculateOrientations();
120 mImageGeometryProxy.resize(pos.size());
122 for(
unsigned i = 0; i < mImageGeometryProxy.size(); ++i)
124 if(!mImageGeometryProxy[i])
127 mImageGeometryProxy[i]->setImage(imageModel);
128 this->
getRenderer()->AddActor(mImageGeometryProxy[i]->getActor());
130 mImageGeometryProxy[i]->setTransformOffset(pos[i]);
134 void CustomMetricRep::updateMeshModel(
DataPtr model)
136 MeshPtr meshModel = boost::dynamic_pointer_cast<
Mesh>(model);
139 std::vector<Transform3D> pos = custom->calculateOrientations();
141 mMeshGeometry.resize(pos.size());
143 for (
unsigned i=0; i<mMeshGeometry.size(); ++i)
145 if (!mMeshGeometry[i])
148 mMeshGeometry[i]->setRenderer(this->
getRenderer());
151 mMeshGeometry[i]->setMesh(meshModel);
153 mMeshGeometry[i]->setTransformOffset(pos[i]);
155 custom->updateTexture(meshModel, pos[i]);
160 void CustomMetricRep::createDistanceMarkers()
162 mDistanceText.clear();
164 if(!custom->getModel() || !custom->getShowDistanceMarkers())
166 std::vector<Transform3D> pos = custom->calculateOrientations();
174 vtkSmartPointer<vtkPoints> points = vtkSmartPointer<vtkPoints>::New();
175 points->SetNumberOfPoints(pos.size());
177 mDistanceText.resize(pos.size());
178 Vector3D pos_0 = custom->getZeroPosition();
179 for(
unsigned i = 0; i < mDistanceText.size(); ++i)
182 double distance = (pos_i - pos_0).
length();
185 mDistanceText[i] = this->createDistanceText(pos[i].coord(textpos), distance);
187 Vector3D point = pos[i].coord(textpos);
188 vtkIdType pointId = i;
189 points->SetPoint(pointId, point.data());
196 text->setColor(
mMetric->getColor());
197 text->setText(QString(
"%1").arg(distance));
199 text->setPosition(pos);
200 text->placeAboveCenter();
207 void CustomMetricRep::hideDistanceMetrics()
209 if(mDistanceText.empty())
211 static vtkSmartPointer<vtkSelectVisiblePoints> visPts = vtkSmartPointer<vtkSelectVisiblePoints>::New();
213 float * zbuffer = visPts->Initialize(
true);
215 for(
unsigned i = 0; i < mDistanceText.size(); ++i)
217 Vector3D pos = mDistanceText[i]->getPosition();
218 bool visible = visPts->IsPointOccluded(pos.data(), zbuffer);
219 bool closeToCamera = this->isCloseToCamera(pos);
220 mDistanceText[i]->setVisibility(visible && closeToCamera);
225 bool CustomMetricRep::isCloseToCamera(
Vector3D pos)
227 double distanceThreshold = this->getCustomMetric()->getDistanceMarkerVisibility();
230 double distance = diff.norm();
231 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.