14 #include <vtkImageData.h> 30 DataMetric(uid, name, dataManager, spaceProvider), mShowDistanceMarkers(false), mDistanceMarkerVisibility(50)
32 mArguments.reset(
new MetricReferenceArgumentList(QStringList() <<
"position" <<
"direction"));
33 mArguments->setValidArgumentTypes(QStringList() <<
"pointMetric" <<
"frameMetric");
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;
157 DataPtr model = this->getModel();
159 std::vector<Transform3D> pos = this->calculateOrientations();
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);
217 int reps = this->getRepeatCount();
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);
290 if (!this->getTextureFollowTool() || !model->hasTexture())
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 323 if (!mScaleToP1 || !this->getModel() || this->getModel()->
getType() ==
"image")
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();
535 if(this->modelIsImage())
537 DataPtr model = this->getModel();
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;
551 DataPtr model = this->getModel();
553 return (model && model->getType() ==
"image");
559 bool directionAlongUp =
similar(
dot(vup, dir.normal()), 1.0);
560 if (!directionAlongUp)
571 if(this->modelIsImage())
boost::shared_ptr< class SpaceProvider > SpaceProviderPtr
QString qstring_cast(const T &val)
void setOffsetFromP1(double val)
DoubleBoundingBox3D transform(const Transform3D &m, const DoubleBoundingBox3D &bb)
Transform3D createTransformRotateY(const double angle)
virtual Transform3D get_rMd() const
Vector3D topRight() const
void addXml(QDomNode &dataNode)
adds xml information about the data and its variabels
void setScaleToP1(bool val)
bool getTranslationOnly() const
Transform3D createTransformScale(const Vector3D &scale_)
Transform3D Transform3D
Transform3D is a representation of an affine 3D transform.
void propertiesChanged()
emitted when one of the metadata properties (uid, name etc) changes
Vector3D getZeroPosition() const
void transformChanged()
emitted when transform is changed
boost::shared_ptr< class Image > ImagePtr
void setDefineVectorUpMethod(QString defineVectorUpMethod)
Vector3D bottomLeft() const
void setDistanceMarkerVisibility(double val)
void updateTexture(MeshPtr model, Transform3D rMrr)
virtual void parseXml(QDomNode &dataNode)
Use a XML node to load data.
PatientModelServicePtr mDataManager
static CoordinateSystem reference()
virtual vtkImageDataPtr getBaseVtkImageData()
SpaceProviderPtr mSpaceProvider
static DoubleBoundingBox3D fromCloud(std::vector< Vector3D > cloud)
Vector3D cross(const Vector3D &a, const Vector3D &b)
compute cross product of a and b.
std::vector< Transform3D > calculateOrientations() const
void setTranslationOnly(bool val)
CustomMetric::DefineVectorUpMethods getDefineVectorUpMethods() const
boost::shared_ptr< class Data > DataPtr
QString getDefineVectorUpMethod() const
Transform3D createTransformIJC(const Vector3D &ivec, const Vector3D &jvec, const Vector3D ¢er)
virtual QString getType() const
bool getShowDistanceMarkers() const
void parseXml(QDomNode &dataNode)
Use a XML node to load data.
double getRepeatDistance() const
Data class that represents a custom.
void setOffsetFromP0(double val)
boost::shared_ptr< class PatientModelService > PatientModelServicePtr
virtual Vector3D getRefCoord() const
static CustomMetricPtr create(QString uid, QString name, PatientModelServicePtr dataManager, SpaceProviderPtr spaceProvider)
Transform3D createTransformTranslate(const Vector3D &translation)
csTOOL_OFFSET
the tool space t with a virtual offset added along the z axis. (to)
double dot(const Vector3D &a, const Vector3D &b)
compute inner product (or dot product) of a and b.
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.
bool getTextureFollowTool() const
Eigen::Vector3d Vector3D
Vector3D is a representation of a point or vector in 3D.
bool similar(const CameraInfo &lhs, const CameraInfo &rhs, double tol)
void setModelUid(QString val)
RealScalar length() const
std::vector< Vector3D > getPointCloud() const
virtual DoubleBoundingBox3D boundingBox() const
double getOffsetFromP0() const
void setTextureFollowTool(bool val)
int getRepeatCount() const
void setShowDistanceMarkers(bool show)
Transform3D createTransformRotateX(const double angle)
bool modelIsImage() const
boost::shared_ptr< class Mesh > MeshPtr
double getOffsetFromP1() const
double getDistanceMarkerVisibility() const
void setRepeatDistance(double val)
boost::shared_ptr< class CustomMetric > CustomMetricPtr
virtual bool isValid() const
virtual void addXml(QDomNode &dataNode)
adds xml information about the data and its variabels
bool getScaleToP1() const
QString getModelUid() const
Namespace for all CustusX production code.