Fraxinus  17.12
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) 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 "cxLandmark.h"
35 
36 #include <QDomNode>
37 
38 #include "cxTypeConversions.h"
39 #include "cxTime.h"
40 
41 namespace cx
42 {
43 
44 Landmark::Landmark(QString uid, Vector3D coord) :
45  mUid(uid), mCoord(coord)
46 {
47  mTimestamp = QDateTime::currentDateTime();
48 }
49 
51 {
52 }
53 
54 QString Landmark::getUid() const
55 {
56  return mUid;
57 }
58 
60 {
61  return mCoord;
62 }
63 QDateTime Landmark::getTimestamp() const
64 {
65  return mTimestamp;
66 }
67 
68 void Landmark::addXml(QDomNode& dataNode) const
69 {
70  QDomDocument doc = dataNode.ownerDocument();
71 
72  dataNode.toElement().setAttribute("uid", qstring_cast(mUid));
73 
74  QDomElement coordNode = doc.createElement("coord");
75  coordNode.appendChild(doc.createTextNode(qstring_cast(mCoord)));
76  dataNode.appendChild(coordNode);
77 
78  QDomElement timestampNode = doc.createElement("timestamp");
79  timestampNode.appendChild(doc.createTextNode(mTimestamp.toString(timestampSecondsFormat())));
80  dataNode.appendChild(timestampNode);
81 }
82 void Landmark::parseXml(QDomNode& dataNode)
83 {
84  if (dataNode.isNull())
85  return;
86 
87  QDomElement base = dataNode.toElement();
88 
89  mUid = base.attribute("uid");
90  mCoord = Vector3D::fromString(dataNode.namedItem("coord").toElement().text());
91  mTimestamp = QDateTime::fromString(dataNode.namedItem("timestamp").toElement().text(), timestampSecondsFormat());
92 }
93 
94 bool operator<(const Landmark& lhs, const Landmark& rhs)
95 {
96  // attempts an integer comparison; otherwise revert to lexiographical
97  bool ok = true;
98  int i_lhs = lhs.getUid().toInt(&ok);
99  int i_rhs = rhs.getUid().toInt(&ok);
100  if (ok)
101  return i_lhs < i_rhs;
102 
103  return lhs.getUid() < rhs.getUid();
104 }
105 
106 
110 
111 Landmarks::Landmarks() : QObject(NULL)
112 {
113 
114 }
115 
117 {
118  return LandmarksPtr(new Landmarks());
119 }
120 
122 {
123  return mLandmarks;
124 }
125 
127 {
128  mLandmarks[landmark.getUid()] = landmark;
129  emit landmarkAdded(landmark.getUid());
130 }
131 
132 void Landmarks::removeLandmark(QString uid)
133 {
134  mLandmarks.erase(uid);
135  emit landmarkRemoved(uid);
136 }
137 
139 {
140  while (!mLandmarks.empty())
141  this->removeLandmark(mLandmarks.begin()->first);
142 }
143 
144 void Landmarks::addXml(QDomNode dataNode) const
145 {
146  QDomElement landmarksNode = dataNode.toElement();
147  QDomDocument doc = dataNode.ownerDocument();
148 
149  LandmarkMap::const_iterator it = mLandmarks.begin();
150  for (; it != mLandmarks.end(); ++it)
151  {
152  QDomElement landmarkNode = doc.createElement("landmark");
153  it->second.addXml(landmarkNode);
154  landmarksNode.appendChild(landmarkNode);
155  }
156 }
157 
158 void Landmarks::parseXml(QDomNode dataNode)
159 {
160  QDomElement landmarksNode = dataNode.toElement();
161 
162  if (dataNode.isNull())
163  return;
164 
165  QDomElement landmarkNode = landmarksNode.firstChildElement("landmark");
166  for (; !landmarkNode.isNull(); landmarkNode = landmarkNode.nextSiblingElement("landmark"))
167  {
168  Landmark landmark;
169  landmark.parseXml(landmarkNode);
170  this->setLandmark(landmark);
171  }
172 }
173 
177 
178 LandmarkProperty::LandmarkProperty(const QString& uid, const QString& name, bool active) :
179  mUid(uid), mName(name), mActive(active)
180 {
181  if (mName.isEmpty())
182  mName = mUid;
183 }
184 
185 void LandmarkProperty::setName(const QString& name)
186 {
187  mName = name;
188 }
189 
191 {
192  mActive = active;
193 }
194 
196 {
197  return mUid;
198 }
199 
201 {
202  return mActive;
203 }
204 
206 {
207  return mName;
208 }
209 
210 void LandmarkProperty::addXml(QDomNode& dataNode)
211 {
212  dataNode.toElement().setAttribute("uid", qstring_cast(mUid));
213  dataNode.toElement().setAttribute("active", qstring_cast(mActive));
214  dataNode.toElement().setAttribute("name", qstring_cast(mName));
215 }
216 void LandmarkProperty::parseXml(QDomNode& dataNode)
217 {
218  if (dataNode.isNull())
219  return;
220 
221  QDomElement base = dataNode.toElement();
222  mUid = base.attribute("uid");
223  mActive = base.attribute("active").toInt();
224  mName = base.attribute("name");
225 }
226 
227 } // namespace cx
QString qstring_cast(const T &val)
bool operator<(const Landmark &lhs, const Landmark &rhs)
Definition: cxLandmark.cpp:94
QString getUid() const
Definition: cxLandmark.cpp:54
LandmarkMap getLandmarks()
Definition: cxLandmark.cpp:121
One landmark, or fiducial, coordinate.
Definition: cxLandmark.h:61
QString getUid() const
Definition: cxLandmark.cpp:195
QDateTime getTimestamp() const
Definition: cxLandmark.cpp:63
void removeLandmark(QString uid)
Definition: cxLandmark.cpp:132
QString timestampSecondsFormat()
Definition: cxTime.cpp:39
LandmarkProperty(const QString &uid="", const QString &name="", bool active=true)
Definition: cxLandmark.cpp:178
boost::shared_ptr< class Landmarks > LandmarksPtr
Definition: cxData.h:61
void addXml(QDomNode &dataNode)
Definition: cxLandmark.cpp:210
Landmark(QString uid="", Vector3D coord=Vector3D(0, 0, 0))
Definition: cxLandmark.cpp:44
void addXml(QDomNode &dataNode) const
Definition: cxLandmark.cpp:68
void addXml(QDomNode dataNode) const
Definition: cxLandmark.cpp:144
void setLandmark(Landmark landmark)
Definition: cxLandmark.cpp:126
void parseXml(QDomNode &dataNode)
Definition: cxLandmark.cpp:216
Vector3D getCoord() const
Definition: cxLandmark.cpp:59
static LandmarksPtr create()
Definition: cxLandmark.cpp:116
Eigen::Vector3d Vector3D
Vector3D is a representation of a point or vector in 3D.
Definition: cxVector3D.h:63
std::map< QString, class Landmark > LandmarkMap
QString getName() const
Definition: cxLandmark.cpp:205
void setName(const QString &name)
Definition: cxLandmark.cpp:185
void setActive(bool active)
Definition: cxLandmark.cpp:190
void parseXml(QDomNode &dataNode)
Definition: cxLandmark.cpp:82
bool getActive() const
Definition: cxLandmark.cpp:200
void parseXml(QDomNode dataNode)
Definition: cxLandmark.cpp:158
Namespace for all CustusX production code.