14 #include <vtkImageData.h>
30 DataMetric(uid, name, dataManager, spaceProvider), mShowDistanceMarkers(false), mDistanceMarkerVisibility(50)
32 mArguments.reset(
new MetricReferenceArgumentList(QStringList() <<
"position" <<
"direction"));
34 connect(mArguments.get(), SIGNAL(argumentsChanged()),
this, SIGNAL(
transformChanged()));
36 mDefineVectorUpMethod = mDefineVectorUpMethods.table;
41 mRepeatDistance = 0.0;
42 mTranslationOnly =
false;
43 mTextureFollowTool =
false;
46 bool CustomMetric::needForToolListenerHasChanged()
const
48 bool toolDefinesUp = mDefineVectorUpMethod == mDefineVectorUpMethods.tool;
49 bool toolListenerDefined = mToolListener.get()!=NULL;
51 if (mTextureFollowTool != toolListenerDefined || toolDefinesUp != toolListenerDefined)
58 void CustomMetric::createOrDestroyToolListener()
60 if (this->needForToolListenerHasChanged())
62 bool toolDefinesUp = mDefineVectorUpMethod == mDefineVectorUpMethods.tool;
63 if (mTextureFollowTool || toolDefinesUp)
66 mToolListener->setSpace(CoordinateSystem(
csTOOL_OFFSET,
"active"));
72 mToolListener.reset();
77 void CustomMetric::onPropertiesChanged()
79 this->createOrDestroyToolListener();
84 return mDefineVectorUpMethods;
101 mArguments->addXml(dataNode);
103 QDomElement elem = dataNode.toElement();
104 elem.setAttribute(
"definevectorup", mDefineVectorUpMethod);
105 elem.setAttribute(
"meshUid", mModelUid);
107 elem.setAttribute(
"scaleToP1", mScaleToP1);
108 elem.setAttribute(
"offsetFromP0", mOffsetFromP0);
109 elem.setAttribute(
"offsetFromP1", mOffsetFromP1);
110 elem.setAttribute(
"repeatDistance", mRepeatDistance);
111 elem.setAttribute(
"showDistance", mShowDistanceMarkers);
112 elem.setAttribute(
"distanceMarkerVisibility", mDistanceMarkerVisibility);
113 elem.setAttribute(
"translationOnly", mTranslationOnly);
114 elem.setAttribute(
"textureFollowTool", mTextureFollowTool);
121 mArguments->parseXml(dataNode,
mDataManager->getDatas());
123 QDomElement elem = dataNode.toElement();
124 mDefineVectorUpMethod = elem.attribute(
"definevectorup",
qstring_cast(mDefineVectorUpMethod));
125 mModelUid = elem.attribute(
"meshUid",
qstring_cast(mModelUid));
126 mScaleToP1 = elem.attribute(
"scaleToP1", QString::number(mScaleToP1)).toInt();
127 mOffsetFromP0 = elem.attribute(
"offsetFromP0", QString::number(mOffsetFromP0)).toDouble();
128 mOffsetFromP1 = elem.attribute(
"offsetFromP1", QString::number(mOffsetFromP1)).toDouble();
129 mRepeatDistance = elem.attribute(
"repeatDistance", QString::number(mRepeatDistance)).toDouble();
130 mShowDistanceMarkers = elem.attribute(
"showDistance", QString::number(mShowDistanceMarkers)).toInt();
131 mDistanceMarkerVisibility = elem.attribute(
"distanceMarkerVisibility", QString::number(mDistanceMarkerVisibility)).toDouble();
132 mTranslationOnly = elem.attribute(
"translationOnly", QString::number(mTranslationOnly)).toInt();
133 mTextureFollowTool = elem.attribute(
"textureFollowTool", QString::number(mTextureFollowTool)).toInt();
135 this->onPropertiesChanged();
140 return !mArguments->getRefCoords().empty();
145 return mArguments->getRefCoords().front();
155 std::vector<Vector3D> retval;
160 std::vector<Vector3D> cloud;
165 rrMd = model->get_rMd();
166 cloud = model->getPointCloud();
170 cloud.push_back(Vector3D::Zero());
171 rrMd = Transform3D::Identity();
174 for (
unsigned i=0; i<pos.size(); ++i)
178 for (
unsigned j=0; j<cloud.size(); ++j)
181 retval.push_back(p_r);
191 std::vector<Vector3D> coords = mArguments->getRefCoords();
196 Vector3D dir = this->getDirection();
200 zeroPos = this->calculateOrientation(p0, dir, vup, scale).coord(
Vector3D(0,0,0));
205 std::vector<Vector3D> CustomMetric::getPositions()
const
207 std::vector<Vector3D> retval;
208 std::vector<Vector3D> coords = mArguments->getRefCoords();
209 if (coords.size() < 2)
215 double fullDist =
dot(dir, p1-p0);
218 for (
int i=0; i<reps; ++i)
220 double dist = mOffsetFromP0 + mRepeatDistance*i;
230 std::vector<Vector3D> coords = mArguments->getRefCoords();
231 if (coords.size() < 2)
239 if (!
similar(mRepeatDistance, 0.0))
240 reps = (
dot(p1-p0, dir)-mOffsetFromP0-mOffsetFromP1)/mRepeatDistance + 1;
241 reps = std::min(100, reps);
242 reps = std::max(reps, 1);
248 Vector3D CustomMetric::getDirection()
const
250 std::vector<Vector3D> coords = mArguments->getRefCoords();
252 return Vector3D::UnitZ();
253 Vector3D diff = (coords[1]-coords[0]);
254 if (
similar(diff.length(), 0.0))
256 return diff.normal();
259 Vector3D CustomMetric::getVectorUp()
const
261 if(mDefineVectorUpMethod == mDefineVectorUpMethods.connectedFrameInP1)
263 std::vector<Transform3D> transforms = mArguments->getRefFrames();
264 if (transforms.size()<2)
265 return Vector3D::UnitZ();
272 else if (mDefineVectorUpMethod == mDefineVectorUpMethods.tool)
275 Vector3D toolUp = -Vector3D::UnitX();
276 return rMt.vector(toolUp);
299 Vector3D t_r = rMt.coord(Vector3D::Zero());
300 Vector3D td_r = rMt.vector(Vector3D::UnitZ());
306 Vector3D x_min_r(c[0], bl[1], c[2]);
307 Vector3D x_max_r(c[0], tr[1], c[2]);
308 x_min_r = rMd.coord(x_min_r);
309 x_max_r = rMd.coord(x_max_r);
311 double t_x =
dot(t_r, td_r);
312 double bbmin_x =
dot(x_min_r, td_r);
313 double bbmax_x =
dot(x_max_r, td_r);
314 double range = bbmax_x-bbmin_x;
317 double s = (t_x-bbmin_x)/range;
318 model->getTextureData().getPositionY()->setValue(s);
321 Vector3D CustomMetric::getScale()
const
324 return Vector3D::Ones();
329 std::vector<Vector3D> coords = mArguments->getRefCoords();
330 double height = (coords[1] - coords[0]).
length();
332 Vector3D dir = this->getDirection();
333 double p0 =
dot(coords[0], dir);
334 double p1 =
dot(coords[1], dir);
337 height -= (mOffsetFromP0 + mOffsetFromP1);
340 height/bounds.
range()[1],
347 return mDefineVectorUpMethod;
352 mDefineVectorUpMethod = defineVectorUpMethod;
373 if (mScaleToP1 == val)
386 if (mOffsetFromP0 == val)
394 return mOffsetFromP0;
399 if (mOffsetFromP1 == val)
407 return mOffsetFromP1;
412 if (mRepeatDistance == val)
414 mRepeatDistance = val;
420 return mRepeatDistance;
425 if(mShowDistanceMarkers == show)
427 mShowDistanceMarkers = show;
433 return mShowDistanceMarkers;
437 if (mTranslationOnly == val)
439 mTranslationOnly = val;
445 if (mDistanceMarkerVisibility == val)
447 mDistanceMarkerVisibility = val;
453 return mDistanceMarkerVisibility;
458 return mTranslationOnly;
463 if (mTextureFollowTool == val)
465 mTextureFollowTool = val;
471 return mTextureFollowTool;
474 QStringList CustomMetric::DefineVectorUpMethods::getAvailableDefineVectorUpMethods()
const
478 retval << connectedFrameInP1;
483 std::map<QString, QString> CustomMetric::DefineVectorUpMethods::getAvailableDefineVectorUpMethodsDisplayNames()
const
485 std::map<QString, QString> names;
486 names[table] =
"The operating table";
487 names[connectedFrameInP1] =
"The connected frame in p1";
488 names[tool] =
"The active tool";
495 std::vector<Vector3D> pos = this->getPositions();
496 Vector3D dir = this->getDirection();
500 std::vector<Transform3D> retval(pos.size());
501 for (
unsigned i=0; i<retval.size(); ++i)
503 if (mTranslationOnly)
509 retval[i] = this->calculateOrientation(pos[i], dir, vup, scale);
524 Transform3D center2DImage = this->calculateTransformTo2DImageCenter();
532 Transform3D CustomMetric::calculateTransformTo2DImageCenter()
const
534 Transform3D position2DImage = Transform3D::Identity();
538 ImagePtr imageModel = boost::dynamic_pointer_cast<Image>(model);
540 int xSize = vtkImage->GetExtent()[1] - vtkImage->GetExtent()[0];
541 int ySize = vtkImage->GetExtent()[3] - vtkImage->GetExtent()[2];
542 Eigen::Array3d spacing(vtkImage->GetSpacing());
546 return position2DImage;
559 bool directionAlongUp =
similar(
dot(vup, dir.normal()), 1.0);
560 if (!directionAlongUp)