Fraxinus  17.12-rc3
An IGT application
cxRegionOfInterestMetric.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 
34 
35 #include "cxBoundingBox3D.h"
36 #include "cxTypeConversions.h"
37 #include "cxPlaneMetric.h"
38 #include "cxPointMetric.h"
39 #include "cxXMLNodeWrapper.h"
40 #include "cxSpaceProvider.h"
41 #include "cxPatientModelService.h"
42 #include "cxSpaceListener.h"
43 #include "cxLogger.h"
44 
45 namespace cx
46 {
47 
49 {
50 
51 }
52 
54 {
55  DoubleBoundingBox3D bb = this->generateROIFromPointsAndMargin(this->transform(mPoints, qMd), mMargin);
56 
57  DoubleBoundingBox3D bb_max = this->generateROIFromPointsAndMargin(this->transform(mMaxBoundsPoints, qMd), mMargin);
58 
59  if (bb_max!=DoubleBoundingBox3D::zero())
60  bb = intersection(bb, bb_max);
61 
62  return bb;
63 }
64 
65 std::vector<Vector3D> RegionOfInterest::transform(const std::vector<Vector3D>& points, Transform3D M) const
66 {
67  std::vector<Vector3D> retval;
68  for (unsigned i=0; i<points.size(); ++i)
69  retval.push_back(M.coord(points[i]));
70  return retval;
71 }
72 
73 DoubleBoundingBox3D RegionOfInterest::generateROIFromPointsAndMargin(const std::vector<Vector3D>& points, double margin) const
74 {
75  if (points.empty())
77 
79  Vector3D vmargin(margin,margin, margin);
80  Vector3D bl = bb.bottomLeft() - vmargin;
81  Vector3D tr = bb.topRight() + vmargin;
82  bb = DoubleBoundingBox3D(bl, tr);
83 
84  return bb;
85 }
86 
87 //---------------------------------------------------------
88 //---------------------------------------------------------
89 //---------------------------------------------------------
90 
91 RegionOfInterestMetric::RegionOfInterestMetric(const QString& uid, const QString& name, PatientModelServicePtr dataManager, SpaceProviderPtr spaceProvider) :
92  DataMetric(uid, name, dataManager, spaceProvider)
93 {
94  mUseActiveTooltip = false;
95  mMargin = 20;
96 }
97 
99 {
100  return RegionOfInterestMetricPtr(new RegionOfInterestMetric(uid, name, dataManager, spaceProvider));
101 }
102 
104 {
105 }
106 
108 {
109  return this->boundingBox().center();
110 }
111 
113 {
114  return this->getROI().getBox() != DoubleBoundingBox3D::zero();
115 }
116 
117 void RegionOfInterestMetric::addXml(QDomNode& dataNode)
118 {
119  DataMetric::addXml(dataNode);
120 
121  XMLNodeAdder adder(dataNode);
122  for (unsigned i=0; i<mContainedData.size(); ++i)
123  adder.addTextToElement("content", mContainedData[i]);
124 
125  adder.addTextToElement("useActiveTooltip", QString::number(mUseActiveTooltip));
126  adder.addTextToElement("margin", QString::number(mMargin));
127  adder.addTextToElement("maxBoundsData", mMaxBoundsData);
128 }
129 
130 void RegionOfInterestMetric::parseXml(QDomNode& dataNode)
131 {
132  DataMetric::parseXml(dataNode);
133 
134  XMLNodeParser parser(dataNode);
135  mContainedData = parser.parseTextFromDuplicateElements("content");
136  mUseActiveTooltip = parser.parseTextFromElement("useActiveTooltip").toInt();
137  mMargin = parser.parseTextFromElement("margin").toDouble();
138  mMaxBoundsData = parser.parseTextFromElement("maxBoundsData");
139 
140  this->onContentChanged();
141 }
142 
144 {
145  return "bb";
146 }
147 
149 {
150  mContainedData = val;
151  this->onContentChanged();
152 }
153 
155 {
156  mUseActiveTooltip = val;
157 
158  this->onContentChanged();
159 }
160 
162 {
163  mMargin = val;
164  this->onContentChanged();
165 }
166 
168 {
169  mMaxBoundsData = val;
170  this->onContentChanged();
171 }
172 
173 void RegionOfInterestMetric::onContentChanged()
174 {
175  for (unsigned i=0; i<mListeners.size(); ++i)
176  {
177  disconnect(mListeners[i].get(), &SpaceListener::changed, this, &RegionOfInterestMetric::onContentTransformsChanged);
178  }
179  mListeners.clear();
180 
181  this->listenTo(CoordinateSystem(csTOOL_OFFSET, "active"));
182  for (unsigned i=0; i<mContainedData.size(); ++i)
183  {
184  this->listenTo(CoordinateSystem(csDATA, mContainedData[i]));
185  }
186  this->onContentTransformsChanged();
187 }
188 
189 void RegionOfInterestMetric::listenTo(CoordinateSystem space)
190 {
191  SpaceListenerPtr listener = mSpaceProvider->createListener();
192  listener->setSpace(space);
193  connect(listener.get(), &SpaceListener::changed, this, &RegionOfInterestMetric::onContentTransformsChanged);
194  mListeners.push_back(listener);
195 }
196 
197 void RegionOfInterestMetric::onContentTransformsChanged()
198 {
199  emit transformChanged();
200 }
201 
203 {
204  return this->getROI().getBox(this->get_rMd().inv());
205 }
206 
208 {
209  return "bb";
210 }
211 
212 template<class ITER>
214 {
215  for ( ; begin!=end; ++begin)
216  *begin = M.coord(*begin);
217 }
218 
220 {
221  RegionOfInterest retval;
222 
223  retval.mMargin = mMargin;
224 
225  if (mUseActiveTooltip)
226  {
227  retval.mPoints.push_back(this->getToolTip_r());
228  }
229 
230  std::map<QString, DataPtr> alldata = mDataManager->getDatas();
231  for (std::map<QString, DataPtr>::const_iterator i=alldata.begin(); i!=alldata.end(); ++i)
232  {
233  if (boost::dynamic_pointer_cast<RegionOfInterestMetric>(i->second))
234  continue;
235 
236  if (mContainedData.contains(i->first))
237  {
238  std::vector<Vector3D> c = i->second->getPointCloud();
239  transform_coord_range(c.begin(), c.end(), i->second->get_rMd());
240  std::copy(c.begin(), c.end(), back_inserter(retval.mPoints));
241  }
242 
243  if (mMaxBoundsData == i->first)
244  {
245  std::vector<Vector3D> c = i->second->getPointCloud();
246  transform_coord_range(c.begin(), c.end(), i->second->get_rMd());
247  retval.mMaxBoundsPoints = c;
248  }
249  }
250 
251  return retval;
252 }
253 
254 Vector3D RegionOfInterestMetric::getToolTip_r() const
255 {
256  Transform3D rMto = mSpaceProvider->get_toMfrom(CoordinateSystem(csTOOL_OFFSET, "active"),
258  Vector3D tp = rMto.coord(Vector3D(0, 0, 0));
259  return tp;
260 }
261 
262 }
boost::shared_ptr< class SpaceProvider > SpaceProviderPtr
Scalar * end()
virtual void parseXml(QDomNode &dataNode)
Use a XML node to load data.
Vector3D topRight() const
virtual QString getAsSingleLineString() const
void addXml(QDomNode &dataNode)
adds xml information about the data and its variabels
QStringList parseTextFromDuplicateElements(QString name)
Scalar * begin()
Transform3D Transform3D
Transform3D is a representation of an affine 3D transform.
Vector3D bottomLeft() const
virtual Vector3D getRefCoord() const
static CoordinateSystem reference()
DoubleBoundingBox3D getBox(Transform3D qMd=Transform3D::Identity())
virtual QString getValueAsString() const
csDATA
a datas space (d)
static DoubleBoundingBox3D fromCloud(std::vector< Vector3D > cloud)
virtual void addXml(QDomNode &dataNode)
adds xml information about the data and its variabels
void parseXml(QDomNode &dataNode)
Use a XML node to load data.
static DoubleBoundingBox3D zero()
static RegionOfInterestMetricPtr create(QString uid, QString name, PatientModelServicePtr dataManager, SpaceProviderPtr spaceProvider)
boost::shared_ptr< class PatientModelService > PatientModelServicePtr
QDomElement addTextToElement(QString name, QString text)
virtual DoubleBoundingBox3D boundingBox() const
Identification of a Coordinate system.
csTOOL_OFFSET
the tool space t with a virtual offset added along the z axis. (to)
boost::shared_ptr< class RegionOfInterestMetric > RegionOfInterestMetricPtr
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.
std::vector< Vector3D > mPoints
Eigen::Vector3d Vector3D
Vector3D is a representation of a point or vector in 3D.
Definition: cxVector3D.h:63
RegionOfInterest getROI() const
QString parseTextFromElement(QString name)
DoubleBoundingBox3D intersection(DoubleBoundingBox3D a, DoubleBoundingBox3D b)
boost::shared_ptr< class SpaceListener > SpaceListenerPtr
Base class for all Data Metrics.
Definition: cxDataMetric.h:64
void transform_coord_range(ITER begin, ITER end, Transform3D M)
std::vector< Vector3D > mMaxBoundsPoints
Namespace for all CustusX production code.