ACloudViewer  3.9.4
A Modern Library for 3D Data Processing
BoundingBox.h
Go to the documentation of this file.
1 // ----------------------------------------------------------------------------
2 // - CloudViewer: www.cloudViewer.org -
3 // ----------------------------------------------------------------------------
4 // Copyright (c) 2018-2024 www.cloudViewer.org
5 // SPDX-License-Identifier: MIT
6 // ----------------------------------------------------------------------------
7 
8 #pragma once
9 
10 // Local
11 #include "SquareMatrix.h"
12 
13 // EIGEN
14 #include <Eigen/Eigenvalues>
15 
16 // STL
17 #include <algorithm>
18 #include <cstdint>
19 #include <numeric>
20 
21 namespace cloudViewer {
22 
24 template <typename T>
26 public:
29  : m_bbMin(0, 0, 0), m_bbMax(0, 0, 0), color_(0, 0, 0), m_valid(false) {}
30 
33  const Vector3Tpl<T>& maxCorner)
34  : m_bbMin(minCorner),
36  color_(0, 0, 0),
37  m_valid(true) {}
38 
41  if (!m_valid) return bbox;
42  if (!bbox.isValid()) return *this;
43 
44  BoundingBoxTpl<T> tempBox;
45  {
46  tempBox.m_bbMin.x = std::min(m_bbMin.x, bbox.m_bbMin.x);
47  tempBox.m_bbMin.y = std::min(m_bbMin.y, bbox.m_bbMin.y);
48  tempBox.m_bbMin.z = std::min(m_bbMin.z, bbox.m_bbMin.z);
49  tempBox.m_bbMax.x = std::max(m_bbMax.x, bbox.m_bbMax.x);
50  tempBox.m_bbMax.y = std::max(m_bbMax.y, bbox.m_bbMax.y);
51  tempBox.m_bbMax.z = std::max(m_bbMax.z, bbox.m_bbMax.z);
52  tempBox.setValidity(true);
53  }
54 
55  return tempBox;
56  }
57 
60  if (bbox.isValid()) {
61  add(bbox.minCorner());
62  add(bbox.maxCorner());
63  }
64 
65  return *this;
66  }
67 
69  virtual const BoundingBoxTpl<T>& operator+=(const Vector3Tpl<T>& V) {
70  if (m_valid) {
71  m_bbMin += V;
72  m_bbMax += V;
73  }
74 
75  return *this;
76  }
77 
79  virtual const BoundingBoxTpl<T>& operator-=(const Vector3Tpl<T>& V) {
80  if (m_valid) {
81  m_bbMin -= V;
82  m_bbMax -= V;
83  }
84 
85  return *this;
86  }
87 
89  virtual const BoundingBoxTpl<T>& operator*=(T scaleFactor) {
90  if (m_valid) {
91  m_bbMin *= scaleFactor;
92  m_bbMax *= scaleFactor;
93  }
94 
95  return *this;
96  }
97 
99  virtual const BoundingBoxTpl<T>& operator*=(const SquareMatrixTpl<T>& mat) {
100  if (m_valid) {
101  Vector3Tpl<T> boxCorners[8];
102 
103  boxCorners[0] = m_bbMin;
104  boxCorners[1] = Vector3Tpl<T>(m_bbMin.x, m_bbMin.y, m_bbMax.z);
105  boxCorners[2] = Vector3Tpl<T>(m_bbMin.x, m_bbMax.y, m_bbMin.z);
106  boxCorners[3] = Vector3Tpl<T>(m_bbMax.x, m_bbMin.y, m_bbMin.z);
107  boxCorners[4] = m_bbMax;
108  boxCorners[5] = Vector3Tpl<T>(m_bbMin.x, m_bbMax.y, m_bbMax.z);
109  boxCorners[6] = Vector3Tpl<T>(m_bbMax.x, m_bbMax.y, m_bbMin.z);
110  boxCorners[7] = Vector3Tpl<T>(m_bbMax.x, m_bbMin.y, m_bbMax.z);
111 
112  clear();
113 
114  for (int i = 0; i < 8; ++i) {
115  add(mat * boxCorners[i]);
116  }
117  }
118 
119  return *this;
120  }
121 
123 
125  void clear() {
126  m_bbMin = m_bbMax = Vector3Tpl<T>(0, 0, 0);
127  m_valid = false;
128  }
129 
131  void add(const Vector3Tpl<T>& P) {
132  if (m_valid) {
133  if (P.x < m_bbMin.x)
134  m_bbMin.x = P.x;
135  else if (P.x > m_bbMax.x)
136  m_bbMax.x = P.x;
137 
138  if (P.y < m_bbMin.y)
139  m_bbMin.y = P.y;
140  else if (P.y > m_bbMax.y)
141  m_bbMax.y = P.y;
142 
143  if (P.z < m_bbMin.z)
144  m_bbMin.z = P.z;
145  else if (P.z > m_bbMax.z)
146  m_bbMax.z = P.z;
147  } else {
148  m_bbMax = m_bbMin = P;
149  m_valid = true;
150  }
151  }
152 
154  inline const Vector3Tpl<T>& minCorner() const { return m_bbMin; }
156  inline const Vector3Tpl<T>& maxCorner() const { return m_bbMax; }
157 
159  inline Vector3Tpl<T>& minCorner() { return m_bbMin; }
161  inline Vector3Tpl<T>& maxCorner() { return m_bbMax; }
162 
165  return (m_bbMax + m_bbMin) * static_cast<T>(0.5);
166  }
167 
169  Vector3Tpl<T> getDiagVec() const { return (m_bbMax - m_bbMin); }
170 
172  inline T getDiagNorm() const { return getDiagVec().norm(); }
173 
175  double getDiagNormd() const { return getDiagVec().normd(); }
176 
178  T getMinBoxDim() const {
180 
181  return std::min(V.x, std::min(V.y, V.z));
182  }
183 
185  T getMaxBoxDim() const {
187 
188  return std::max(V.x, std::max(V.y, V.z));
189  }
190 
192  double computeVolume() const {
194 
195  return static_cast<double>(V.x) * static_cast<double>(V.y) *
196  static_cast<double>(V.z);
197  }
198 
200  inline void setValidity(bool state) { m_valid = state; }
201 
203  inline bool isValid() const { return m_valid; }
204 
207 
209  T minDistTo(const BoundingBoxTpl<T>& bbox) const {
210  if (m_valid && bbox.isValid()) {
211  Vector3Tpl<T> d(0, 0, 0);
212 
213  for (uint8_t dim = 0; dim < 3; ++dim) {
214  // if the boxes overlap in one dimension, the distance is zero
215  // (in this dimension)
216  if (bbox.m_bbMin.u[dim] > m_bbMax.u[dim])
217  d.u[dim] = bbox.m_bbMin.u[dim] - m_bbMax.u[dim];
218  else if (bbox.m_bbMax.u[dim] < m_bbMin.u[dim])
219  d.u[dim] = m_bbMin.u[dim] - bbox.m_bbMax.u[dim];
220  }
221 
222  return d.norm();
223  } else {
224  return std::numeric_limits<T>::quiet_NaN();
225  }
226  }
227 
229 
231  inline bool contains(const Vector3Tpl<T>& P) const {
232  return (P.x >= m_bbMin.x && P.x <= m_bbMax.x && P.y >= m_bbMin.y &&
233  P.y <= m_bbMax.y && P.z >= m_bbMin.z && P.z <= m_bbMax.z);
234  }
235 
236  inline bool containsEigen(const Eigen::Vector3d& point) const {
237  return (point(0) >= m_bbMin.x && point(0) <= m_bbMax.x &&
238  point(1) >= m_bbMin.y && point(1) <= m_bbMax.y &&
239  point(2) >= m_bbMin.z && point(2) <= m_bbMax.z);
240  }
241 
242  std::vector<std::size_t> GetPointIndicesWithinBoundingBox(
243  const std::vector<Eigen::Vector3d>& points) const {
246  }
247 
248  std::vector<std::size_t> GetPointIndicesWithinBoundingBox(
249  const std::vector<Vector3Tpl<T>>& points) const {
250  std::vector<size_t> indices;
251  for (std::size_t idx = 0; idx < points.size(); idx++) {
252  const auto& point = points[idx];
253  if (contains(point)) {
254  indices.push_back(idx);
255  }
256  }
257  return indices;
258  }
259 
260  inline void addEigen(const Eigen::Vector3d& point) { add(point); }
261 
262  inline double volume() const { return getDiagVec().prod(); }
263 
265  inline void SetColor(const Eigen::Vector3d& color) { color_ = color; }
267  inline Eigen::Vector3d GetColor() const {
269  }
270 
271  double getXPercentage(double x) const {
272  return (x - m_bbMin(0)) / (m_bbMax(0) - m_bbMin(0));
273  }
274 
275  double getYPercentage(double y) const {
276  return (y - m_bbMin(1)) / (m_bbMax(1) - m_bbMin(1));
277  }
278 
279  double getZPercentage(double z) const {
280  return (z - m_bbMin(2)) / (m_bbMax(2) - m_bbMin(2));
281  }
282 
283  inline void getBounds(double bounds[6]) const {
284  bounds[0] = minCorner().x;
285  bounds[1] = maxCorner().x;
286  bounds[2] = minCorner().y;
287  bounds[3] = maxCorner().y;
288  bounds[4] = minCorner().z;
289  bounds[5] = maxCorner().z;
290  }
291 
292 protected:
300  bool m_valid;
301 };
302 
305 
306 } // namespace cloudViewer
int points
math::float4 color
Type y
Definition: CVGeom.h:137
Type u[3]
Definition: CVGeom.h:139
Type x
Definition: CVGeom.h:137
Type z
Definition: CVGeom.h:137
double normd() const
Returns vector norm (forces double precision output)
Definition: CVGeom.h:426
Type prod() const
x,y,z product
Definition: CVGeom.h:406
Type norm() const
Returns vector norm.
Definition: CVGeom.h:424
static Vector3Tpl fromArray(const int a[3])
Constructor from an int array.
Definition: CVGeom.h:268
static std::vector< Eigen::Matrix< double, 3, 1 > > fromArrayContainer(const std::vector< Vector3Tpl< PointCoordinateType >> &container)
Definition: CVGeom.h:301
Bounding box structure.
Definition: BoundingBox.h:25
Vector3Tpl< T > getDiagVec() const
Returns diagonal vector.
Definition: BoundingBox.h:169
BoundingBoxTpl(const Vector3Tpl< T > &minCorner, const Vector3Tpl< T > &maxCorner)
Constructor from two vectors (lower min. and upper max. corners)
Definition: BoundingBox.h:32
BoundingBoxTpl()
Default constructor.
Definition: BoundingBox.h:28
double getDiagNormd() const
Returns diagonal length (double precision)
Definition: BoundingBox.h:175
Vector3Tpl< T > & minCorner()
Returns min corner.
Definition: BoundingBox.h:159
CCVector3d color_
The color of the bounding box in RGB.
Definition: BoundingBox.h:298
Vector3Tpl< T > m_bbMin
Lower min. corner.
Definition: BoundingBox.h:294
BoundingBoxTpl< T > operator+(const BoundingBoxTpl< T > &bbox) const
Returns the 'sum' of this bounding-box and another one.
Definition: BoundingBox.h:40
Vector3Tpl< T > getCenter() const
Returns center.
Definition: BoundingBox.h:164
std::vector< std::size_t > GetPointIndicesWithinBoundingBox(const std::vector< Vector3Tpl< T >> &points) const
Definition: BoundingBox.h:248
double getZPercentage(double z) const
Definition: BoundingBox.h:279
Eigen::Vector3d GetColor() const
Gets the bounding box color.
Definition: BoundingBox.h:267
std::vector< std::size_t > GetPointIndicesWithinBoundingBox(const std::vector< Eigen::Vector3d > &points) const
Definition: BoundingBox.h:242
bool contains(const Vector3Tpl< T > &P) const
Returns whether a points is inside the box or not.
Definition: BoundingBox.h:231
T minDistTo(const BoundingBoxTpl< T > &bbox) const
Definition: BoundingBox.h:209
const Vector3Tpl< T > & maxCorner() const
Returns max corner (const)
Definition: BoundingBox.h:156
T getDiagNorm() const
Returns diagonal length.
Definition: BoundingBox.h:172
T getMinBoxDim() const
Returns minimal box dimension.
Definition: BoundingBox.h:178
void setValidity(bool state)
Sets bonding box validity.
Definition: BoundingBox.h:200
double computeVolume() const
Returns the bounding-box volume.
Definition: BoundingBox.h:192
virtual const BoundingBoxTpl< T > & operator*=(T scaleFactor)
Scales the bounding box.
Definition: BoundingBox.h:89
void getBounds(double bounds[6]) const
Definition: BoundingBox.h:283
T getMaxBoxDim() const
Returns maximal box dimension.
Definition: BoundingBox.h:185
void clear()
Resets the bounding box.
Definition: BoundingBox.h:125
bool containsEigen(const Eigen::Vector3d &point) const
Definition: BoundingBox.h:236
const Vector3Tpl< T > & minCorner() const
Returns min corner (const)
Definition: BoundingBox.h:154
virtual const BoundingBoxTpl< T > & operator*=(const SquareMatrixTpl< T > &mat)
Rotates the bounding box.
Definition: BoundingBox.h:99
Vector3Tpl< T > m_bbMax
Upper max. corner.
Definition: BoundingBox.h:296
const BoundingBoxTpl< T > & operator+=(const BoundingBoxTpl< T > &bbox)
In place 'sum' of this bounding-box with another one.
Definition: BoundingBox.h:59
double getXPercentage(double x) const
Definition: BoundingBox.h:271
virtual const BoundingBoxTpl< T > & operator+=(const Vector3Tpl< T > &V)
Shifts the bounding box with a vector.
Definition: BoundingBox.h:69
virtual const BoundingBoxTpl< T > & operator-=(const Vector3Tpl< T > &V)
Shifts the bounding box with a vector.
Definition: BoundingBox.h:79
void addEigen(const Eigen::Vector3d &point)
Definition: BoundingBox.h:260
void SetColor(const Eigen::Vector3d &color)
Sets the bounding box color.
Definition: BoundingBox.h:265
Vector3Tpl< T > & maxCorner()
Returns max corner.
Definition: BoundingBox.h:161
bool isValid() const
Returns whether bounding box is valid or not.
Definition: BoundingBox.h:203
double getYPercentage(double y) const
Definition: BoundingBox.h:275
void add(const Vector3Tpl< T > &P)
'Enlarges' the bounding box with a point
Definition: BoundingBox.h:131
int min(int a, int b)
Definition: cutil_math.h:53
int max(int a, int b)
Definition: cutil_math.h:48
Generic file read and write utility for python interface.
Definition: lsd.c:149