Fraxinus  17.12
An IGT application
cxDistanceMetric.cpp
Go to the documentation of this file.
1 /*=========================================================================
2 This file is part of CustusX, an Image Guided Therapy Application.
3 
4 Copyright (c) 2008-2014, SINTEF Department of Medical Technology
5 All rights reserved.
6 
7 Redistribution and use in source and binary forms, with or without
8 modification, are permitted provided that the following conditions are met:
9 
10 1. Redistributions of source code must retain the above copyright notice,
11  this list of conditions and the following disclaimer.
12 
13 2. Redistributions in binary form must reproduce the above copyright notice,
14  this list of conditions and the following disclaimer in the documentation
15  and/or other materials provided with the distribution.
16 
17 3. Neither the name of the copyright holder nor the names of its contributors
18  may be used to endorse or promote products derived from this software
19  without specific prior written permission.
20 
21 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
22 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
24 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
25 FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
27 SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
28 CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
29 OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30 OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 =========================================================================*/
32 
33 
34 #include "cxDistanceMetric.h"
35 #include "cxBoundingBox3D.h"
36 #include "cxTypeConversions.h"
37 #include "cxPlaneMetric.h"
38 #include "cxPointMetric.h"
39 
40 #include "cxPatientModelService.h"
41 
42 namespace cx
43 {
44 
45 DistanceMetric::DistanceMetric(const QString& uid, const QString& name, PatientModelServicePtr dataManager, SpaceProviderPtr spaceProvider) :
46  DataMetric(uid, name, dataManager, spaceProvider)
47 {
48  mArguments.reset(new MetricReferenceArgumentList(QStringList() << "line endpoint 0" << "line endpoint 1"));
49  mArguments->setValidArgumentTypes(QStringList() << "pointMetric" << "planeMetric");
50 
51  connect(mArguments.get(), SIGNAL(argumentsChanged()), this, SLOT(resetCachedValues()));
52  connect(mArguments.get(), SIGNAL(argumentsChanged()), this, SIGNAL(transformChanged()));
53 }
54 
55 DistanceMetricPtr DistanceMetric::create(QString uid, QString name, PatientModelServicePtr dataManager, SpaceProviderPtr spaceProvider)
56 {
57  return DistanceMetricPtr(new DistanceMetric(uid, name, dataManager, spaceProvider));
58 }
59 
61 {
62 }
63 
65 {
66  return this->boundingBox().center();
67 }
68 
70 {
71  return this->getEndpoints().size()==2;
72 }
73 
74 void DistanceMetric::addXml(QDomNode& dataNode)
75 {
76  DataMetric::addXml(dataNode);
77  mArguments->addXml(dataNode);
78 }
79 
80 void DistanceMetric::parseXml(QDomNode& dataNode)
81 {
82  DataMetric::parseXml(dataNode);
83 
84  mArguments->parseXml(dataNode, mDataManager->getDatas());
85  this->resetCachedValues();
86 }
87 
88 void DistanceMetric::resetCachedValues()
89 {
90  mCachedEndPoints.reset();
91 }
92 
93 std::vector<Vector3D> DistanceMetric::getEndpoints() const
94 {
95  this->updateCache();
96  return mCachedEndPoints.get();
97 }
98 
99 void DistanceMetric::updateCache() const
100 {
101  if (!mCachedEndPoints.isValid())
102  {
103  std::vector<Vector3D> endpoints;
104  Vector3D direction;
105  this->getEndpointsUncached(&endpoints, &direction);
106  mCachedEndPoints.set(endpoints);
107  mCachedDirection.set(direction);
108  }
109 }
110 
111 void DistanceMetric::getEndpointsUncached(std::vector<Vector3D> *endpoints, Vector3D* direction) const
112 {
113  DataPtr a0 = mArguments->get(0);
114  DataPtr a1 = mArguments->get(1);
115 
116  if (!a0 || !a1)
117  {
118  endpoints->clear();
119  return;
120  }
121  std::vector<Vector3D> retval(2);
122  Vector3D dir;
123 
124  // case I: point-point
125  // case II: point-plane
126  // case III: plane-plane (not implemented)
127 
128  if ((a0->getType() == "pointMetric") && (a1->getType() == "pointMetric"))
129  {
130  retval[0] = boost::dynamic_pointer_cast<PointMetric>(a0)->getRefCoord();
131  retval[1] = boost::dynamic_pointer_cast<PointMetric>(a1)->getRefCoord();
132  dir = (retval[1] - retval[0]).normal();
133  }
134  else if ((a0->getType() == "planeMetric") && (a1->getType() == "pointMetric"))
135  {
136  Plane3D plane = boost::dynamic_pointer_cast<PlaneMetric>(a0)->getRefPlane();
137  Vector3D p = boost::dynamic_pointer_cast<PointMetric>(a1)->getRefCoord();
138 
139  retval[0] = plane.projection(p);
140  retval[1] = p;
141  dir = plane.normal();
142  }
143  else if ((a0->getType() == "pointMetric") && (a1->getType() == "planeMetric"))
144  {
145  Plane3D plane = boost::dynamic_pointer_cast<PlaneMetric>(a1)->getRefPlane();
146  Vector3D p = boost::dynamic_pointer_cast<PointMetric>(a0)->getRefCoord();
147 
148  retval[1] = plane.projection(p);
149  retval[0] = p;
150  dir = plane.normal();
151  }
152  else
153  {
154  endpoints->clear();
155  }
156 
157  *endpoints = retval;
158  *direction = dir;
159 }
160 
162 {
163  std::vector<Vector3D> endpoints = this->getEndpoints();
164  if (endpoints.size() != 2)
165  return -1;
166 
167  Vector3D dir = this->getDirection();
168 
169  return dot(endpoints[1] - endpoints[0], dir);
170 }
171 
173 {
174  this->updateCache();
175  return mCachedDirection.get();
176 }
177 
179 {
180  return QString("%1 mm").arg(this->getDistance(), 0, 'f', 1);
181 }
182 
184 {
185  return DoubleBoundingBox3D::fromCloud(this->getEndpoints());
186 }
187 
188 
189 }
boost::shared_ptr< class SpaceProvider > SpaceProviderPtr
Vector3D getDirection() const
boost::shared_ptr< class DistanceMetric > DistanceMetricPtr
static DistanceMetricPtr create(QString uid, QString name, PatientModelServicePtr dataManager, SpaceProviderPtr spaceProvider)
void addXml(QDomNode &dataNode)
adds xml information about the data and its variabels
PlainObject normal() const
virtual DoubleBoundingBox3D boundingBox() const
Data class representing a plane.
Definition: cxPlaneMetric.h:69
virtual QString getValueAsString() const
static DoubleBoundingBox3D fromCloud(std::vector< Vector3D > cloud)
boost::shared_ptr< class Data > DataPtr
void parseXml(QDomNode &dataNode)
Use a XML node to load data.
boost::shared_ptr< class PatientModelService > PatientModelServicePtr
virtual Vector3D getRefCoord() const
double dot(const Vector3D &a, const Vector3D &b)
compute inner product (or dot product) of a and b.
Definition: cxVector3D.cpp:67
Data class that represents a distance between two points, or a point and a plane. ...
double getDistance() const
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.
std::vector< Vector3D > getEndpoints() const
return the two endpoints in reference space. None if invalid.
Data class that represents a single point.
Definition: cxPointMetric.h:63
Eigen::Vector3d Vector3D
Vector3D is a representation of a point or vector in 3D.
Definition: cxVector3D.h:63
virtual void parseXml(QDomNode &dataNode)
Use a XML node to load data.
virtual void addXml(QDomNode &dataNode)
adds xml information about the data and its variabels
virtual bool isValid() const
Namespace for all CustusX production code.