Fraxinus  2023.01.05-dev+develop.0da12
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 
96 {
97  return LandmarksPtr(new Landmarks());
98 }
99 
101 {
102  return mLandmarks;
103 }
104 
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 
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 qstring_cast(const T &val)
bool operator<(const Landmark &lhs, const Landmark &rhs)
Definition: cxLandmark.cpp:73
QString getUid() const
Definition: cxLandmark.cpp:33
LandmarkMap getLandmarks()
Definition: cxLandmark.cpp:100
One landmark, or fiducial, coordinate.
Definition: cxLandmark.h:40
QString getUid() const
Definition: cxLandmark.cpp:174
QDateTime getTimestamp() const
Definition: cxLandmark.cpp:42
void removeLandmark(QString uid)
Definition: cxLandmark.cpp:111
QString timestampSecondsFormat()
Definition: cxTime.cpp:18
LandmarkProperty(const QString &uid="", const QString &name="", bool active=true)
Definition: cxLandmark.cpp:157
boost::shared_ptr< class Landmarks > LandmarksPtr
Definition: cxData.h:41
void addXml(QDomNode &dataNode)
Definition: cxLandmark.cpp:189
Landmark(QString uid="", Vector3D coord=Vector3D(0, 0, 0))
Definition: cxLandmark.cpp:23
void addXml(QDomNode &dataNode) const
Definition: cxLandmark.cpp:47
void addXml(QDomNode dataNode) const
Definition: cxLandmark.cpp:123
void setLandmark(Landmark landmark)
Definition: cxLandmark.cpp:105
void parseXml(QDomNode &dataNode)
Definition: cxLandmark.cpp:195
Vector3D getCoord() const
Definition: cxLandmark.cpp:38
static LandmarksPtr create()
Definition: cxLandmark.cpp:95
Eigen::Vector3d Vector3D
Vector3D is a representation of a point or vector in 3D.
Definition: cxVector3D.h:42
std::map< QString, class Landmark > LandmarkMap
QString getName() const
Definition: cxLandmark.cpp:184
void setName(const QString &name)
Definition: cxLandmark.cpp:164
void setActive(bool active)
Definition: cxLandmark.cpp:169
void parseXml(QDomNode &dataNode)
Definition: cxLandmark.cpp:61
bool getActive() const
Definition: cxLandmark.cpp:179
void parseXml(QDomNode dataNode)
Definition: cxLandmark.cpp:137
Namespace for all CustusX production code.