CustusX  15.4.0-beta
An IGT application
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
cxPointMetric.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 "cxPointMetric.h"
35 #include "cxBoundingBox3D.h"
36 #include "cxTool.h"
37 #include "cxTypeConversions.h"
38 #include "cxSpaceProvider.h"
39 #include "cxSpaceListener.h"
41 
42 namespace cx
43 {
44 
45 PointMetric::PointMetric(const QString& uid, const QString& name, PatientModelServicePtr dataManager, SpaceProviderPtr spaceProvider) :
46  DataMetric(uid, name, dataManager, spaceProvider),
47  mCoordinate(0,0,0),
48  mSpace(CoordinateSystem::reference())
49 {
50  mSpaceListener = mSpaceProvider->createListener();
51  mSpaceListener->setSpace(mSpace);
52  connect(mSpaceListener.get(), SIGNAL(changed()), this, SLOT(resetCachedValues()));
53  connect(mSpaceListener.get(), SIGNAL(changed()), this, SIGNAL(transformChanged()));
54 }
55 
56 PointMetricPtr PointMetric::create(QString uid, QString name, PatientModelServicePtr dataManager, SpaceProviderPtr spaceProvider)
57 {
58  return PointMetricPtr(new PointMetric(uid, name, dataManager, spaceProvider));
59 }
60 
62 {
63 }
64 
66 {
67  if (p == mCoordinate)
68  return;
69 
70  mCoordinate = p;
71  this->resetCachedValues();
72  emit transformChanged();
73 }
74 
76 {
77  return mCoordinate;
78 }
79 
81 {
82  if (space == mSpace)
83  return;
84 
85  // keep the absolute position (in ref) constant when changing space.
86  Transform3D new_M_old = mSpaceProvider->get_toMfrom(this->getSpace(), space);
87  mCoordinate = new_M_old.coord(mCoordinate);
88 
89  mSpace = space;
90  this->resetCachedValues();
91  mSpaceListener->setSpace(space);
92 }
93 
95 {
96  return mSpace;
97 }
98 
99 void PointMetric::addXml(QDomNode& dataNode)
100 {
101  DataMetric::addXml(dataNode);
102 
103  dataNode.toElement().setAttribute("space", mSpace.toString());
104  dataNode.toElement().setAttribute("coord", qstring_cast(mCoordinate));
105 }
106 
107 void PointMetric::parseXml(QDomNode& dataNode)
108 {
109  DataMetric::parseXml(dataNode);
110 
111  this->setSpace(CoordinateSystem::fromString(dataNode.toElement().attribute("space", mSpace.toString())));
112  this->setCoordinate(Vector3D::fromString(dataNode.toElement().attribute("coord", qstring_cast(mCoordinate))));
113 }
114 
116 {
117  // convert both inputs to r space
118  Vector3D p0_r = this->getRefCoord();
119 
120  return DoubleBoundingBox3D(p0_r, p0_r);
121 }
122 
123 void PointMetric::resetCachedValues()
124 {
125 // std::cout << " ** PointMetric::resetCachedValues" << std::endl;
126 
127  mCachedRefCoord.reset();
128 }
129 
134 {
135 // std::cout << " ** PointMetric::getRefCoord" << std::endl;
136 
137 // return Vector3D(1,2,3);
138  if (!mCachedRefCoord.isValid())
139  {
140 // std::cout << " ** PointMetric::getRefCoord FILL CACHE" << std::endl;
141  Transform3D rM1 = mSpaceProvider->get_toMfrom(this->getSpace(), CoordinateSystem(csREF));
142  Vector3D val = rM1.coord(this->getCoordinate());
143  mCachedRefCoord.set(val);
144  }
145  return mCachedRefCoord.get();
146 }
147 
149 {
150  return prettyFormat(this->getRefCoord(), 1, 3);
151 }
152 
154 {
155  return QString("%1 \"%2\" %3")
156  .arg(this->getSingleLineHeader())
157  .arg(mSpace.toString())
158  .arg(qstring_cast(this->getCoordinate()));
159 }
160 
161 }
boost::shared_ptr< class SpaceProvider > SpaceProviderPtr
QString qstring_cast(const T &val)
void setSpace(CoordinateSystem space)
bool isValid() const
void addXml(QDomNode &dataNode)
adds xml information about the data and its variabels
Transform3D Transform3D
Transform3D is a representation of an affine 3D transform.
void transformChanged()
emitted when transform is changed
void setCoordinate(const Vector3D &p)
QString prettyFormat(Vector3D val, int decimals, int fieldWidth)
Definition: cxVector3D.cpp:119
csREF
the data reference space (r)
SpaceProviderPtr mSpaceProvider
Definition: cxDataMetric.h:87
virtual void parseXml(QDomNode &dataNode)
Use a XML node to load data.
void parseXml(QDomNode &dataNode)
Use a XML node to load data.
void set(const T &val)
virtual ~PointMetric()
Vector3D getCoordinate() const
boost::shared_ptr< class PatientModelService > PatientModelServicePtr
Identification of a Coordinate system.
QString getSingleLineHeader() const
virtual DoubleBoundingBox3D boundingBox() 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.
CoordinateSystem getSpace() const
cxLogicManager_EXPORT SpaceProviderPtr spaceProvider()
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 QString getValueAsString() const
virtual Vector3D getRefCoord() const
virtual void addXml(QDomNode &dataNode)
adds xml information about the data and its variabels
virtual QString getAsSingleLineString() const
static PointMetricPtr create(QString uid, QString name, PatientModelServicePtr dataManager, SpaceProviderPtr spaceProvider)
const T & get() const
static CoordinateSystem fromString(QString text)
boost::shared_ptr< class PointMetric > PointMetricPtr