Fraxinus  16.5.0-fx-rc1
An IGT application
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
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 
116 LandmarksPtr Landmarks::create()
117 {
118  return LandmarksPtr(new Landmarks());
119 }
120 
121 LandmarkMap Landmarks::getLandmarks()
122 {
123  return mLandmarks;
124 }
125 
126 void Landmarks::setLandmark(Landmark landmark)
127 {
128  //std::cout << "Image::setLandmark" << std::endl;
129  mLandmarks[landmark.getUid()] = landmark;
130  emit landmarkAdded(landmark.getUid());
131 }
132 
133 void Landmarks::removeLandmark(QString uid)
134 {
135  mLandmarks.erase(uid);
136  emit landmarkRemoved(uid);
137 }
138 
139 void Landmarks::clear()
140 {
141  while (!mLandmarks.empty())
142  this->removeLandmark(mLandmarks.begin()->first);
143 }
144 
145 void Landmarks::addXml(QDomNode dataNode) const
146 {
147  QDomElement landmarksNode = dataNode.toElement();
148  QDomDocument doc = dataNode.ownerDocument();
149 
150  LandmarkMap::const_iterator it = mLandmarks.begin();
151  for (; it != mLandmarks.end(); ++it)
152  {
153  QDomElement landmarkNode = doc.createElement("landmark");
154  it->second.addXml(landmarkNode);
155  landmarksNode.appendChild(landmarkNode);
156  }
157 }
158 
159 void Landmarks::parseXml(QDomNode dataNode)
160 {
161  QDomElement landmarksNode = dataNode.toElement();
162 
163  if (dataNode.isNull())
164  return;
165 
166  QDomElement landmarkNode = landmarksNode.firstChildElement("landmark");
167  for (; !landmarkNode.isNull(); landmarkNode = landmarkNode.nextSiblingElement("landmark"))
168  {
169  Landmark landmark;
170  landmark.parseXml(landmarkNode);
171  this->setLandmark(landmark);
172  }
173 }
174 
178 
179 LandmarkProperty::LandmarkProperty(const QString& uid, const QString& name, bool active) :
180  mUid(uid), mName(name), mActive(active)
181 {
182  if (mName.isEmpty())
183  mName = mUid;
184 }
185 
186 void LandmarkProperty::setName(const QString& name)
187 {
188  mName = name;
189 }
190 
192 {
193  mActive = active;
194 }
195 
197 {
198  return mUid;
199 }
200 
202 {
203  return mActive;
204 }
205 
207 {
208  return mName;
209 }
210 
211 void LandmarkProperty::addXml(QDomNode& dataNode)
212 {
213  dataNode.toElement().setAttribute("uid", qstring_cast(mUid));
214  dataNode.toElement().setAttribute("active", qstring_cast(mActive));
215  dataNode.toElement().setAttribute("name", qstring_cast(mName));
216 }
217 void LandmarkProperty::parseXml(QDomNode& dataNode)
218 {
219  if (dataNode.isNull())
220  return;
221 
222  QDomElement base = dataNode.toElement();
223  mUid = base.attribute("uid");
224  mActive = base.attribute("active").toInt();
225  mName = base.attribute("name");
226 }
227 
228 } // 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
One landmark, or fiducial, coordinate.
Definition: cxLandmark.h:61
QString getUid() const
Definition: cxLandmark.cpp:196
QDateTime getTimestamp() const
Definition: cxLandmark.cpp:63
QString timestampSecondsFormat()
Definition: cxTime.cpp:39
boost::shared_ptr< class Landmarks > LandmarksPtr
Definition: cxData.h:61
void addXml(QDomNode &dataNode)
Definition: cxLandmark.cpp:211
Landmark(QString uid="", Vector3D coord=Vector3D(0, 0, 0))
Definition: cxLandmark.cpp:44
void addXml(QDomNode &dataNode) const
Definition: cxLandmark.cpp:68
void parseXml(QDomNode &dataNode)
Definition: cxLandmark.cpp:217
Vector3D getCoord() const
Definition: cxLandmark.cpp:59
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:206
void setName(const QString &name)
Definition: cxLandmark.cpp:186
void setActive(bool active)
Definition: cxLandmark.cpp:191
void parseXml(QDomNode &dataNode)
Definition: cxLandmark.cpp:82
bool getActive() const
Definition: cxLandmark.cpp:201