Fraxinus  16.5.0-fx-rc6
An IGT application
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
cxBoundingBox3D.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 #include "cxBoundingBox3D.h"
34 #include "cxUtilHelpers.h"
35 #include "cxTypeConversions.h"
36 
37 // --------------------------------------------------------
38 namespace cx
39 {
40 //namespace utils
41 //{
43 
44 
45 // --------------------------------------------------------
46 // IntBoundingBox3D
48 {
49 }
50 IntBoundingBox3D::IntBoundingBox3D(int x0, int x1, int y0, int y1, int z0, int z1)
51 {
52  elems[0] = x0;
53  elems[1] = x1;
54  elems[2] = y0;
55  elems[3] = y1;
56  elems[4] = z0;
57  elems[5] = z1;
58 }
62 {
63  Vector3D bl, tr;
64  for (unsigned i = 0; i < a.size(); ++i)
65  {
66  bl[i] = std::min(a[i], b[i]);
67  tr[i] = std::max(a[i], b[i]);
68  }
69 
70  elems[0] = static_cast<int> (bl[0]);
71  elems[1] = static_cast<int> (tr[0]);
72  elems[2] = static_cast<int> (bl[1]);
73  elems[3] = static_cast<int> (tr[1]);
74  elems[4] = static_cast<int> (bl[2]);
75  elems[5] = static_cast<int> (tr[2]);
76 }
77 
78 IntBoundingBox3D::IntBoundingBox3D(const Eigen::Vector3i& a, const Eigen::Vector3i& b)
79 {
80  Eigen::Vector3i bl, tr;
81  for (unsigned i = 0; i < a.size(); ++i)
82  {
83  bl[i] = std::min(a[i], b[i]);
84  tr[i] = std::max(a[i], b[i]);
85  }
86 
87  elems[0] = static_cast<int> (bl[0]);
88  elems[1] = static_cast<int> (tr[0]);
89  elems[2] = static_cast<int> (bl[1]);
90  elems[3] = static_cast<int> (tr[1]);
91  elems[4] = static_cast<int> (bl[2]);
92  elems[5] = static_cast<int> (tr[2]);
93 }
94 
96 {
97  for (unsigned i = 0; i < size(); ++i)
98  elems[i] = static_cast<int> (data[i]);
99  // std::copy(data, data+size(), elems);
100 }
102 {
103  std::copy(data, data + size(), elems);
104 }
105 // --------------------------------------------------------
106 Eigen::Vector3i IntBoundingBox3D::bottomLeft() const
107 {
108  return Eigen::Vector3i(elems[0], elems[2], elems[4]);
109 }
110 Eigen::Vector3i IntBoundingBox3D::topRight() const
111 {
112  return Eigen::Vector3i(elems[1], elems[3], elems[5]);
113 }
114 Eigen::Vector3i IntBoundingBox3D::center() const
115 {
116  return (bottomLeft() + topRight()) / 2.0;
117 }
118 Eigen::Vector3i IntBoundingBox3D::range() const
119 {
120  return topRight() - bottomLeft();
121 }
126 Eigen::Vector3i IntBoundingBox3D::corner(int x, int y, int z) const
127 {
128  Eigen::Vector3i c;
129  c[0] = x ? elems[1] : elems[0];
130  c[1] = y ? elems[3] : elems[2];
131  c[2] = z ? elems[5] : elems[4];
132  return c;
133 }
134 // --------------------------------------------------------
135 
138 bool IntBoundingBox3D::contains(const Eigen::Vector3i& p) const
139 {
140  bool inside = true;
141  for (unsigned i = 0; i < 3; ++i)
142  inside &= ((elems[2 * i] <= p[i]) && (p[i] <= elems[2 * i + 1]));
143  return inside;
144 }
145 
146 std::ostream& operator<<(std::ostream& s, const IntBoundingBox3D& data)
147 {
148  return stream_range(s, data.begin(), data.end());
149 }
150 // --------------------------------------------------------
151 
152 // --------------------------------------------------------
153 // DoubleBoundingBox3D
155 {
156 }
157 DoubleBoundingBox3D::DoubleBoundingBox3D(double x0, double x1, double y0, double y1, double z0, double z1)
158 {
159  elems[0] = x0;
160  elems[1] = x1;
161  elems[2] = y0;
162  elems[3] = y1;
163  elems[4] = z0;
164  elems[5] = z1;
165 }
169 {
170  Vector3D bl, tr;
171  for (unsigned i = 0; i < a.size(); ++i)
172  {
173  bl[i] = std::min(a[i], b[i]);
174  tr[i] = std::max(a[i], b[i]);
175  }
176 
177  elems[0] = bl[0];
178  elems[1] = tr[0];
179  elems[2] = bl[1];
180  elems[3] = tr[1];
181  elems[4] = bl[2];
182  elems[5] = tr[2];
183 }
185 {
186  std::copy(data, data + size(), elems);
187 }
189 {
190  std::copy(data, data + size(), elems);
191 }
193 {
194  for (unsigned i = 0; i < size(); ++i)
195  {
196  elems[i] = bb.elems[i];
197  }
198 }
199 // --------------------------------------------------------
200 
204 {
205  return DoubleBoundingBox3D(data[0], data[2], data[1], data[3], 0, 0);
206 }
207 // --------------------------------------------------------
208 
210 {
211  return Vector3D(elems[0], elems[2], elems[4]);
212 }
214 {
215  return Vector3D(elems[1], elems[3], elems[5]);
216 }
218 {
219  return (bottomLeft() + topRight()) / 2.0;
220 }
222 {
223  return topRight() - bottomLeft();
224 }
229 Vector3D DoubleBoundingBox3D::corner(int x, int y, int z) const
230 {
231  Vector3D c;
232  c[0] = x ? elems[1] : elems[0];
233  c[1] = y ? elems[3] : elems[2];
234  c[2] = z ? elems[5] : elems[4];
235  return c;
236 }
237 // --------------------------------------------------------
238 
242 {
243  bool inside = true;
244  for (unsigned i = 0; i < 3; ++i)
245  inside &= ((elems[2 * i] <= p[i]) && (p[i] <= elems[2 * i + 1]));
246  return inside;
247 }
248 
249 bool similar(const DoubleBoundingBox3D& a, const DoubleBoundingBox3D& b, double tol)
250 {
251  return similar(a.bottomLeft(), b.bottomLeft(), tol) && similar(a.topRight(), b.topRight(), tol);
252 }
253 // --------------------------------------------------------
254 std::ostream& operator<<(std::ostream& s, const DoubleBoundingBox3D& data)
255 {
256  return stream_range(s, data.begin(), data.end());
257 }
258 // --------------------------------------------------------
259 
261 {
262  if (cloud.empty())
263  return DoubleBoundingBox3D(0, 0, 0, 0, 0, 0);
264 
265  Vector3D a = cloud[0]; // min
266  Vector3D b = cloud[0]; // max
267 
268  for (unsigned int i = 0; i < cloud.size(); ++i)
269  {
270  for (unsigned int j = 0; j < 3; ++j)
271  {
272  a[j] = std::min(a[j], cloud[i][j]);
273  b[j] = std::max(b[j], cloud[i][j]);
274  }
275  }
276  return DoubleBoundingBox3D(a, b);
277 }
278 
280 {
281  std::vector<double> raw = convertQString2DoubleVector(text);
282  if (raw.size() != 6)
283  return DoubleBoundingBox3D(0, 1, 0, 1, 0, 1);
284  return DoubleBoundingBox3D((double*) &(*raw.begin()));
285 }
286 
288 {
289  std::vector<Vector3D> cloud;
290  cloud.push_back(corner(0,0,0));
291  cloud.push_back(other.corner(0,0,0));
292  cloud.push_back(corner(0,0,1));
293  cloud.push_back(other.corner(0,0,1));
294  cloud.push_back(corner(0,1,0));
295  cloud.push_back(other.corner(0,1,0));
296  cloud.push_back(corner(0,1,1));
297  cloud.push_back(other.corner(0,1,1));
298  cloud.push_back(corner(1,0,0));
299  cloud.push_back(other.corner(1,0,0));
300  cloud.push_back(corner(1,0,1));
301  cloud.push_back(other.corner(1,0,1));
302  cloud.push_back(corner(1,1,0));
303  cloud.push_back(other.corner(1,1,0));
304  cloud.push_back(corner(1,1,1));
305  cloud.push_back(other.corner(1,1,1));
306  return fromCloud(cloud);
307 }
308 
309 // --------------------------------------------------------
310 //} // namespace utils
311 } // namespace cx
312 // --------------------------------------------------------
DoubleBoundingBox3D unionWith(const DoubleBoundingBox3D &other) const
Vector3D topRight() const
Vector3D corner(int x, int y, int z) const
static DoubleBoundingBox3D fromViewport(const double *data)
Vector3D bottomLeft() const
static DoubleBoundingBox3D fromString(const QString &text)
construct a bb from a string containing 6 whitespace-separated numbers
std::ostream & stream_range(std::ostream &s, ITER begin, ITER end, char separator=' ')
Definition: cxUtilHelpers.h:53
static DoubleBoundingBox3D fromCloud(std::vector< Vector3D > cloud)
bool similar(const DoubleBoundingBox3D &a, const DoubleBoundingBox3D &b, double tol)
Eigen::Vector3i topRight() const
Eigen::Vector3i bottomLeft() const
Eigen::Vector3i range() const
Representation of an integer bounding box in 3D. The data are stored as {xmin,xmax,ymin,ymax,zmin,zmax}, in order to simplify communication with vtk.
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::ostream & operator<<(std::ostream &s, const IntBoundingBox3D &data)
bool contains(const Eigen::Vector3i &p) const
Eigen::Vector3d Vector3D
Vector3D is a representation of a point or vector in 3D.
Definition: cxVector3D.h:63
Eigen::Vector3i corner(int x, int y, int z) const
Vector3D center() const
Eigen::Vector3i center() const
std::vector< double > convertQString2DoubleVector(const QString &input, bool *ok)
bool contains(const Vector3D &p) const