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();
55 return boost::dynamic_pointer_cast<CustomMetric>(
mMetric);
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])
126 mMeshGeometry[i].reset(
new GraphicalGeometric);
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();
150 DoubleBoundingBox3D bounds = custom->getModel()->boundingBox();
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();
163 textpos[2] = bounds.bottomLeft()[2];
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)