NorMIT-nav  22.09
An IGT application
cxLandmark.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) SINTEF Department of Medical Technology.
5 All rights reserved.
6 
7 CustusX is released under a BSD 3-Clause license.
8 
9 See Lisence.txt (https://github.com/SINTEFMedtek/CustusX/blob/master/License.txt) for details.
10 =========================================================================*/
11 
12 
13 #include "cxLandmark.h"
14 
15 #include <QDomNode>
16 
17 #include "cxTypeConversions.h"
18 #include "cxTime.h"
19 
20 namespace cx
21 {
22 
23 Landmark::Landmark(QString uid, Vector3D coord) :
24  mUid(uid), mCoord(coord)
25 {
26  mTimestamp = QDateTime::currentDateTime();
27 }
28 
30 {
31 }
32 
33 QString Landmark::getUid() const
34 {
35  return mUid;
36 }
37 
39 {
40  return mCoord;
41 }
42 QDateTime Landmark::getTimestamp() const
43 {
44  return mTimestamp;
45 }
46 
47 void Landmark::addXml(QDomNode& dataNode) const
48 {
49  QDomDocument doc = dataNode.ownerDocument();
50 
51  dataNode.toElement().setAttribute("uid", qstring_cast(mUid));
52 
53  QDomElement coordNode = doc.createElement("coord");
54  coordNode.appendChild(doc.createTextNode(qstring_cast(mCoord)));
55  dataNode.appendChild(coordNode);
56 
57  QDomElement timestampNode = doc.createElement("timestamp");
58  timestampNode.appendChild(doc.createTextNode(mTimestamp.toString(timestampSecondsFormat())));
59  dataNode.appendChild(timestampNode);
60 }
61 void Landmark::parseXml(QDomNode& dataNode)
62 {
63  if (dataNode.isNull())
64  return;
65 
66  QDomElement base = dataNode.toElement();
67 
68  mUid = base.attribute("uid");
69  mCoord = Vector3D::fromString(dataNode.namedItem("coord").toElement().text());
70  mTimestamp = QDateTime::fromString(dataNode.namedItem("timestamp").toElement().text(), timestampSecondsFormat());
71 }
72 
73 bool operator<(const Landmark& lhs, const Landmark& rhs)
74 {
75  // attempts an integer comparison; otherwise revert to lexiographical
76  bool ok = true;
77  int i_lhs = lhs.getUid().toInt(&ok);
78  int i_rhs = rhs.getUid().toInt(&ok);
79  if (ok)
80  return i_lhs < i_rhs;
81 
82  return lhs.getUid() < rhs.getUid();
83 }
84 
85 
89 
90 Landmarks::Landmarks() : QObject(NULL)
91 {
92 
93 }
94 
95 LandmarksPtr Landmarks::create()
96 {
97  return LandmarksPtr(new Landmarks());
98 }
99 
100 LandmarkMap Landmarks::getLandmarks()
101 {
102  return mLandmarks;
103 }
104 
105 void Landmarks::setLandmark(Landmark landmark)
106 {
107  mLandmarks[landmark.getUid()] = landmark;
108  emit landmarkAdded(landmark.getUid());
109 }
110 
111 void Landmarks::removeLandmark(QString uid)
112 {
113  mLandmarks.erase(uid);
114  emit landmarkRemoved(uid);
115 }
116 
117 void Landmarks::clear()
118 {
119  while (!mLandmarks.empty())
120  this->removeLandmark(mLandmarks.begin()->first);
121 }
122 
123 void Landmarks::addXml(QDomNode dataNode) const
124 {
125  QDomElement landmarksNode = dataNode.toElement();
126  QDomDocument doc = dataNode.ownerDocument();
127 
128  LandmarkMap::const_iterator it = mLandmarks.begin();
129  for (; it != mLandmarks.end(); ++it)
130  {
131  QDomElement landmarkNode = doc.createElement("landmark");
132  it->second.addXml(landmarkNode);
133  landmarksNode.appendChild(landmarkNode);
134  }
135 }
136 
137 void Landmarks::parseXml(QDomNode dataNode)
138 {
139  QDomElement landmarksNode = dataNode.toElement();
140 
141  if (dataNode.isNull())
142  return;
143 
144  QDomElement landmarkNode = landmarksNode.firstChildElement("landmark");
145  for (; !landmarkNode.isNull(); landmarkNode = landmarkNode.nextSiblingElement("landmark"))
146  {
147  Landmark landmark;
148  landmark.parseXml(landmarkNode);
149  this->setLandmark(landmark);
150  }
151 }
152 
156 
157 LandmarkProperty::LandmarkProperty(const QString& uid, const QString& name, bool active) :
158  mUid(uid), mName(name), mActive(active)
159 {
160  if (mName.isEmpty())
161  mName = mUid;
162 }
163 
164 void LandmarkProperty::setName(const QString& name)
165 {
166  mName = name;
167 }
168 
170 {
171  mActive = active;
172 }
173 
175 {
176  return mUid;
177 }
178 
180 {
181  return mActive;
182 }
183 
185 {
186  return mName;
187 }
188 
189 void LandmarkProperty::addXml(QDomNode& dataNode)
190 {
191  dataNode.toElement().setAttribute("uid", qstring_cast(mUid));
192  dataNode.toElement().setAttribute("active", qstring_cast(mActive));
193  dataNode.toElement().setAttribute("name", qstring_cast(mName));
194 }
195 void LandmarkProperty::parseXml(QDomNode& dataNode)
196 {
197  if (dataNode.isNull())
198  return;
199 
200  QDomElement base = dataNode.toElement();
201  mUid = base.attribute("uid");
202  mActive = base.attribute("active").toInt();
203  mName = base.attribute("name");
204 }
205 
206 } // namespace cx
qstring_cast
QString qstring_cast(const T &val)
Definition: cxTypeConversions.h:46
cx::LandmarkProperty::parseXml
void parseXml(QDomNode &dataNode)
Definition: cxLandmark.cpp:195
cx::Landmark::getUid
QString getUid() const
Definition: cxLandmark.cpp:33
cx
Namespace for all CustusX production code.
Definition: cx_dev_group_definitions.h:13
cx::LandmarkProperty::getName
QString getName() const
Definition: cxLandmark.cpp:184
cx::operator<
bool operator<(const Landmark &lhs, const Landmark &rhs)
Definition: cxLandmark.cpp:73
cx::LandmarkMap
std::map< QString, class Landmark > LandmarkMap
Definition: cxLandmarkRegistrationWidget.h:31
cx::Landmark::addXml
void addXml(QDomNode &dataNode) const
Definition: cxLandmark.cpp:47
cx::Landmark::Landmark
Landmark(QString uid="", Vector3D coord=Vector3D(0, 0, 0))
Definition: cxLandmark.cpp:23
cx::LandmarkProperty::getUid
QString getUid() const
Definition: cxLandmark.cpp:174
cx::Landmark::~Landmark
~Landmark()
Definition: cxLandmark.cpp:29
cx::Landmark
One landmark, or fiducial, coordinate.
Definition: cxLandmark.h:40
cx::Landmark::getTimestamp
QDateTime getTimestamp() const
Definition: cxLandmark.cpp:42
cxTypeConversions.h
cx::Landmark::parseXml
void parseXml(QDomNode &dataNode)
Definition: cxLandmark.cpp:61
cx::timestampSecondsFormat
QString timestampSecondsFormat()
Definition: cxTime.cpp:18
cx::LandmarkProperty::addXml
void addXml(QDomNode &dataNode)
Definition: cxLandmark.cpp:189
cx::LandmarkProperty::setActive
void setActive(bool active)
Definition: cxLandmark.cpp:169
cx::Landmarks
Definition: cxLandmark.h:65
cx::LandmarksPtr
boost::shared_ptr< class Landmarks > LandmarksPtr
Definition: cxData.h:41
cx::Landmark::getCoord
Vector3D getCoord() const
Definition: cxLandmark.cpp:38
cxTime.h
cx::LandmarkProperty::setName
void setName(const QString &name)
Definition: cxLandmark.cpp:164
cx::Vector3D
Eigen::Vector3d Vector3D
Vector3D is a representation of a point or vector in 3D.
Definition: cxVector3D.h:42
cxLandmark.h
cx::LandmarkProperty::getActive
bool getActive() const
Definition: cxLandmark.cpp:179