CustusX  15.4.0-beta
An IGT application
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
cxProbeData.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 "cxProbeData.h"
35 #include <QDomNode>
36 #include "cxTypeConversions.h"
37 #include <iostream>
38 #include "cxEnumConverter.h"
39 #include "cxLogger.h"
40 
41 namespace cx
42 {
43 
44 namespace
45 {
46  double loadAttribute(QDomNode dataNode, QString name, double defVal)
47  {
48  QString text = dataNode.toElement().attribute(name);
49  bool ok;
50  double val = text.toDouble(&ok);
51  if (ok)
52  return val;
53  return defVal;
54  }
55 }
56 
57 // --------------------------------------------------------
58 // --------------------------------------------------------
59 // --------------------------------------------------------
60 
61 
63  mType(type), mDepthStart(0), mDepthEnd(0), mWidth(0),
64  mTemporalCalibration(0), mCenterOffset(0), mSoundSpeedCompensationFactor(1.0),
65  mUid("default"),
66  mOrigin_p(0, 0, 0), mSpacing(-1, -1, -1), mClipRect_p(0, 0, 0, 0), mSize(0, 0),
67  mDigital(false)
68 {}
69 
70 void ProbeDefinition::addXml(QDomNode dataNode) const
71 {
72  QDomElement elem = dataNode.toElement();
73  elem.setAttribute("type", qstring_cast(mType));
74  elem.setAttribute("depthStart", qstring_cast(mDepthStart));
75  elem.setAttribute("depthEnd", qstring_cast(mDepthEnd));
76  elem.setAttribute("width", qstring_cast(mWidth));
77 
78  elem.setAttribute("temporalCalibration", qstring_cast(mTemporalCalibration));
79  elem.setAttribute("centerOffset", qstring_cast(mCenterOffset));
80 
81  elem.setAttribute("uid", qstring_cast(mUid));
82 
83  QDomElement imageNode = dataNode.ownerDocument().createElement("image");
84  this->addImageXml(imageNode);
85  dataNode.appendChild(imageNode);
86 }
87 
88 void ProbeDefinition::parseXml(QDomNode dataNode)
89 {
90  QDomElement elem = dataNode.toElement();
91 
92  mType = static_cast<TYPE>(elem.attribute("type").toInt());
93  mDepthStart = loadAttribute(elem, "depthStart", 0);
94  mDepthEnd = loadAttribute(elem, "depthEnd", 0);
95  mWidth = loadAttribute(elem, "width", 0);
96 
97  mTemporalCalibration = loadAttribute(elem, "temporalCalibration", 0);
98  mCenterOffset = loadAttribute(elem, "centerOffset", 0);
99  mUid = elem.attribute("uid");
100 
101  QDomNode imageNode = dataNode.namedItem("image");
102  this->parseImageXml(imageNode);
103 }
104 
105 void ProbeDefinition::addImageXml(QDomNode dataNode) const
106 {
107  QDomElement elem = dataNode.toElement();
108 
109  elem.setAttribute("origin_p", qstring_cast(mOrigin_p));
110  elem.setAttribute("spacing", qstring_cast(mSpacing));
111  elem.setAttribute("clipRect_p", qstring_cast(mClipRect_p));
112  elem.setAttribute("width", qstring_cast(mSize.width()));
113  elem.setAttribute("height", qstring_cast(mSize.height()));
114 }
115 
116 void ProbeDefinition::parseImageXml(QDomNode dataNode)
117 {
118  QDomElement elem = dataNode.toElement();
119 
120  mOrigin_p = Vector3D::fromString(elem.attribute("origin_p"));
121  mSpacing = Vector3D::fromString(elem.attribute("spacing"));
122  mClipRect_p = DoubleBoundingBox3D::fromString(elem.attribute("clipRect_p"));
123  mSize.setWidth(loadAttribute(elem, "width", 0));
124  mSize.setHeight(loadAttribute(elem, "height", 0));
125 }
126 
128 {
129  mTemporalCalibration = value;
130 }
131 
133 {
134  mType = type;
135 }
136 
137 void ProbeDefinition::setSector(double depthStart, double depthEnd, double width, double centerOffset)
138 {
139  mDepthStart=depthStart;
140  mDepthEnd=depthEnd;
141  mWidth=width;
142  mCenterOffset=centerOffset;
143 }
144 
146 {
147  return mType;
148 }
149 
151 {
152  return mDepthStart;
153 }
154 
156 {
157  return mDepthEnd;
158 }
159 
161 {
162  return mWidth;
163 }
164 
166 {
167  return mTemporalCalibration;
168 }
169 
171 {
172  return mCenterOffset;
173 }
174 
175 void ProbeDefinition::resample(QSize newSize)
176 {
177  if (newSize==mSize)
178  return;
179 
180  Vector3D factor(double(newSize.width())/mSize.width(), double(newSize.height())/mSize.height(), 1);
181 
182  mOrigin_p = multiply_elems(mOrigin_p, factor);
183  mSpacing = divide_elems(mSpacing, factor);
184 
185  Vector3D cr0 = multiply_elems(mClipRect_p.corner(0,0,0), factor);
186  Vector3D cr1 = multiply_elems(mClipRect_p.corner(1,1,1), factor);
187  mClipRect_p = DoubleBoundingBox3D(cr0, cr1);
188 
189  mSize = newSize;
190 }
191 
192 QString ProbeDefinition::getUid() const
193 {
194  return mUid;
195 }
196 
197 void ProbeDefinition::setUid(QString uid)
198 {
199  mUid = uid;
200 }
201 
203 {
204  // cliprect and sector data are connected to linear probes:
205  if (mType==tLINEAR)
206  {
207  mClipRect_p[0] = mOrigin_p[0] - mWidth/2/mSpacing[0];
208  mClipRect_p[1] = mOrigin_p[0] + mWidth/2/mSpacing[0];
209 
210  mClipRect_p[2] = mOrigin_p[1] + mDepthStart/mSpacing[1];
211  mClipRect_p[3] = mOrigin_p[1] + mDepthEnd/mSpacing[1];
212  }
213 }
214 
216 {
217  // cliprect and sector data are connected to linear probes:
218  if (mType==tLINEAR)
219  {
220  mWidth = 2*std::max(fabs(mClipRect_p[0] - mOrigin_p[0]), fabs(mClipRect_p[1] - mOrigin_p[0])) * mSpacing[0];
221  mDepthStart = (mClipRect_p[2] - mOrigin_p[1]) * mSpacing[1];
222  mDepthEnd = (mClipRect_p[3] - mOrigin_p[1]) * mSpacing[1];
223  }
224 }
225 
227 {
228  mSpacing[1] = mSpacing[1] * factor / mSoundSpeedCompensationFactor;
229  mSoundSpeedCompensationFactor = factor;
230 
231  if (this->getType() != ProbeDefinition::tLINEAR)
232  reportWarning("Sound speed compensation is applied to spacing[1], i.e. it is correct for linear probes and approxomate for other probes. Factor: " + qstring_cast(factor));
233 }
234 
235 //Should be transform_uMv(p) (input in pixels, output in mm)
236 Vector3D ProbeDefinition::transform_p_to_u(const Vector3D& q_p) const
237 {
238  Vector3D c(q_p[0], double(mSize.height()) - q_p[1] - 1, -q_p[2]);
239  c = multiply_elems(c, mSpacing);
240  return c;
241 }
242 
244 {
245  return this->transform_p_to_u(mOrigin_p);
246 }
247 
249 {
250  Vector3D p0 = transform_p_to_u(mClipRect_p.corner(0,0,0));
251  Vector3D p1 = transform_p_to_u(mClipRect_p.corner(1,1,1));
252  return DoubleBoundingBox3D(p0,p1);
253 }
254 
256 {
257  return mOrigin_p;
258 }
259 
261 {
262  return mSpacing;
263 }
264 
266 {
267  return mClipRect_p;
268 }
269 
271 {
272  return mSize;
273 }
274 
276 {
277  mOrigin_p = origin_p;
278 }
279 
281 {
282  mSpacing = spacing;
283 }
284 
286 {
287  mClipRect_p = clipRect_p;
288 }
289 
290 void ProbeDefinition::setSize(QSize size)
291 {
292  mSize = size;
293 }
294 
296 {
297  mDigital = val;
298 }
299 
301 {
302  return mDigital;
303 }
304 
305 }
QString qstring_cast(const T &val)
double getWidth() const
void addXml(QDomNode dataNode) const
Definition: cxProbeData.cpp:70
void setType(TYPE type)
Vector3D divide_elems(const Vector3D &a, const Vector3D &b)
perform element-wise division of a and b.
Definition: cxVector3D.cpp:57
void setSpacing(Vector3D spacing)
double getCenterOffset() const
US beam is emitted straight forward.
Definition: cxProbeData.h:123
static DoubleBoundingBox3D fromString(const QString &text)
construct a bb from a string containing 6 whitespace-separated numbers
void resample(QSize mSize)
Set a new image size. Resample all other parameters to match this new image size, keeping sizes in mi...
QString getUid() const
void setSector(double depthStart, double depthEnd, double width, double centerOffset=0)
void updateSectorFromClipRect()
double getDepthStart() const
void parseXml(QDomNode dataNode)
Definition: cxProbeData.cpp:88
double getDepthEnd() const
Vector3D getOrigin_p() const
void updateClipRectFromSector()
void setOrigin_p(Vector3D origin_p)
TYPE getType() const
void reportWarning(QString msg)
Definition: cxLogger.cpp:91
void setTemporalCalibration(double value)
void setUid(QString uid)
void setUseDigitalVideo(bool val)
RTSource is digital (eg. US sector is set digitally, not read from .xml file)
void setClipRect_p(DoubleBoundingBox3D clipRect_p)
DoubleBoundingBox3D getClipRect_u() const
sector clipping rect in image space u. (lower-left corner origin)
void applySoundSpeedCompensationFactor(double factor)
Compensate for soundspeed for linear probes.
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.
ProbeDefinition(TYPE type=tNONE)
Definition: cxProbeData.cpp:62
Vector3D getOrigin_u() const
probe origin in image space u. (lower-left corner origin)
Eigen::Vector3d Vector3D
Vector3D is a representation of a point or vector in 3D.
Definition: cxVector3D.h:63
Vector3D multiply_elems(const Vector3D &a, const Vector3D &b)
perform element-wise multiplication of a and b.
Definition: cxVector3D.cpp:52
Vector3D getSpacing() const
double getTemporalCalibration() const
bool getUseDigitalVideo() const
DoubleBoundingBox3D getClipRect_p() const
QSize getSize() const
void setSize(QSize size)