Fraxinus  16.5.0-fx-rc3
An IGT application
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
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->getData());
85  this->resetCachedValues();
86 }
87 
88 void DistanceMetric::resetCachedValues()
89 {
90  mCachedEndPoints.reset();
91 }
92 
93 std::vector<Vector3D> DistanceMetric::getEndpoints() const
94 {
95  if (!mCachedEndPoints.isValid())
96  {
97  mCachedEndPoints.set(this->getEndpointsUncached());
98  }
99  return mCachedEndPoints.get();
100 }
101 
102 std::vector<Vector3D> DistanceMetric::getEndpointsUncached() const
103 {
104  DataPtr a0 = mArguments->get(0);
105  DataPtr a1 = mArguments->get(1);
106 
107  if (!a0 || !a1)
108  return std::vector<Vector3D>();
109  std::vector<Vector3D> retval(2);
110 
111  // case I: point-point
112  // case II: point-plane
113  // case III: plane-plane (not implemented)
114 
115  if ((a0->getType() == "pointMetric") && (a1->getType() == "pointMetric"))
116  {
117  retval[0] = boost::dynamic_pointer_cast<PointMetric>(a0)->getRefCoord();
118  retval[1] = boost::dynamic_pointer_cast<PointMetric>(a1)->getRefCoord();
119  }
120  else if ((a0->getType() == "planeMetric") && (a1->getType() == "pointMetric"))
121  {
122  Plane3D plane = boost::dynamic_pointer_cast<PlaneMetric>(a0)->getRefPlane();
123  Vector3D p = boost::dynamic_pointer_cast<PointMetric>(a1)->getRefCoord();
124 
125  retval[0] = plane.projection(p);
126  retval[1] = p;
127  }
128  else if ((a0->getType() == "pointMetric") && (a1->getType() == "planeMetric"))
129  {
130  Plane3D plane = boost::dynamic_pointer_cast<PlaneMetric>(a1)->getRefPlane();
131  Vector3D p = boost::dynamic_pointer_cast<PointMetric>(a0)->getRefCoord();
132 
133  retval[1] = plane.projection(p);
134  retval[0] = p;
135  }
136  else
137  {
138  return std::vector<Vector3D>();
139  }
140 
141  return retval;
142 }
143 
145 {
146  std::vector<Vector3D> endpoints = this->getEndpoints();
147  if (endpoints.size() != 2)
148  return -1;
149 
150  return (endpoints[1] - endpoints[0]).length();
151 }
152 
154 {
155  return QString("%1 mm").arg(this->getDistance(), 0, 'f', 1);
156 }
157 
159 {
161 }
162 
164 {
165  return QString("%1 %2")
166  .arg(this->getSingleLineHeader())
167  .arg(qstring_cast(this->getDistance()));
168 }
169 
170 }
boost::shared_ptr< class SpaceProvider > SpaceProviderPtr
QString qstring_cast(const T &val)
bool isValid() const
static DistanceMetricPtr create(QString uid, QString name, PatientModelServicePtr dataManager, SpaceProviderPtr spaceProvider)
void addXml(QDomNode &dataNode)
adds xml information about the data and its variabels
virtual DoubleBoundingBox3D boundingBox() const
PatientModelServicePtr mDataManager
Definition: cxDataMetric.h:88
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.
void set(const T &val)
boost::shared_ptr< class PatientModelService > PatientModelServicePtr
virtual Vector3D getRefCoord() const
QString getSingleLineHeader() const
boost::shared_ptr< class DistanceMetric > DistanceMetricPtr
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.
Eigen::Hyperplane< double, 3 > Plane3D
Definition: cxPlaneMetric.h:47
cxLogicManager_EXPORT SpaceProviderPtr spaceProvider()
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.
Vector3D center() const
virtual void addXml(QDomNode &dataNode)
adds xml information about the data and its variabels
virtual QString getAsSingleLineString() const
const T & get() const
virtual bool isValid() const