libMesh
Public Member Functions | Friends | List of all members
libMesh::BoundingBox Class Reference

Defines a Cartesian bounding box by the two corner extremum. More...

#include <bounding_box.h>

Inheritance diagram for libMesh::BoundingBox:
[legend]

Public Member Functions

 BoundingBox (const Point &new_min, const Point &new_max)
 
 BoundingBox (const std::pair< Point, Point > &bbox)
 
 BoundingBox ()
 Default constructor sets invalid bounds. More...
 
void invalidate ()
 Sets the bounding box to be "inverted". More...
 
const Pointmin () const
 
Pointmin ()
 
const Pointmax () const
 
Pointmax ()
 
bool contains (const BoundingBox &) const
 
bool intersects (const BoundingBox &) const
 
bool intersects (const BoundingBox &, Real abstol) const
 
bool contains_point (const Point &) const
 
bool contains_point (const Point &p, Real abs_tol, Real rel_tol) const
 
void intersect_with (const BoundingBox &)
 Sets this bounding box to be the intersection with the other bounding box. More...
 
void union_with (const Point &p)
 Enlarges this bounding box to include the given point. More...
 
void union_with (const BoundingBox &)
 Sets this bounding box to be the union with the other bounding box. More...
 
Real signed_distance (const Point &p) const
 Computes the signed distance, d, from a given Point p to this BoundingBox. More...
 
void scale (const Real factor)
 Scales each dimension of the bounding box by factor. More...
 
Real max_size () const
 Returns the maximum size of a finite box extent. More...
 
void print (std::ostream &os=libMesh::out) const
 Formatted print, by default to libMesh::out. More...
 

Friends

std::ostream & operator<< (std::ostream &os, const BoundingBox &b)
 Formatted print as above but supports the syntax: More...
 

Detailed Description

Defines a Cartesian bounding box by the two corner extremum.

Definition at line 40 of file bounding_box.h.

Constructor & Destructor Documentation

◆ BoundingBox() [1/3]

libMesh::BoundingBox::BoundingBox ( const Point new_min,
const Point new_max 
)
inline

Definition at line 44 of file bounding_box.h.

45  :
46  std::pair<Point, Point>(new_min, new_max)
47  {}

◆ BoundingBox() [2/3]

libMesh::BoundingBox::BoundingBox ( const std::pair< Point, Point > &  bbox)
inline

Definition at line 49 of file bounding_box.h.

49  :
50  std::pair<Point, Point> (bbox)
51  {}

◆ BoundingBox() [3/3]

libMesh::BoundingBox::BoundingBox ( )
inline

Default constructor sets invalid bounds.

Definition at line 56 of file bounding_box.h.

References invalidate().

57  {
58  this->invalidate();
59  }
void invalidate()
Sets the bounding box to be "inverted".
Definition: bounding_box.h:65

Member Function Documentation

◆ contains()

bool libMesh::BoundingBox::contains ( const BoundingBox other_box) const
inline
Returns
true if the other bounding box is contained in this bounding box. Exact floating point <= comparisons are performed.

Definition at line 209 of file bounding_box.h.

Referenced by libMesh::NetGenMeshInterface::triangulate().

210 {
211  const libMesh::Point & my_lower = this->first;
212  const libMesh::Point & my_upper = this->second;
213 
214  const libMesh::Point & other_lower = other_box.first;
215  const libMesh::Point & other_upper = other_box.second;
216 
217  for (unsigned int dir=0; dir<LIBMESH_DIM; ++dir)
218  if (my_lower(dir) > other_lower(dir) ||
219  other_upper(dir) > my_upper(dir))
220  return false;
221 
222  return true;
223 }
A Point defines a location in LIBMESH_DIM dimensional Real space.
Definition: point.h:39

◆ contains_point() [1/2]

bool libMesh::BoundingBox::contains_point ( const Point p) const
Returns
true if the bounding box contains the given point.

Definition at line 35 of file bounding_box.C.

References libMesh::is_between(), and libMesh::Real.

Referenced by OutputAssembly::interior_assembly(), main(), libMesh::GmshIO::read_mesh(), signed_distance(), and ElemTest< elem_type >::test_bounding_box().

36 {
37  // Make local variables first to make things more clear in a moment
38  Real my_min_x = this->first(0);
39  Real my_max_x = this->second(0);
40  bool x_int = is_between(my_min_x, p(0), my_max_x);
41 
42  bool intersection_true = x_int;
43 
44 #if LIBMESH_DIM > 1
45  Real my_min_y = this->first(1);
46  Real my_max_y = this->second(1);
47  bool y_int = is_between(my_min_y, p(1), my_max_y);
48 
49  intersection_true = intersection_true && y_int;
50 #endif
51 
52 
53 #if LIBMESH_DIM > 2
54  Real my_min_z = this->first(2);
55  Real my_max_z = this->second(2);
56  bool z_int = is_between(my_min_z, p(2), my_max_z);
57 
58  intersection_true = intersection_true && z_int;
59 #endif
60 
61  return intersection_true;
62 }
bool is_between(Real min, Real check, Real max)
Definition: bounding_box.C:30
DIE A HORRIBLE DEATH HERE typedef LIBMESH_DEFAULT_SCALAR_TYPE Real

◆ contains_point() [2/2]

bool libMesh::BoundingBox::contains_point ( const Point p,
Real  abs_tol,
Real  rel_tol 
) const
Returns
true if the bounding box contains the given point, to within at least one of the given tolerance(s). At least one tolerance should be set to greater than zero; otherwise use the other contains_point overload for efficiency.

Relative tolerances are computed relative to the maximum finite extent of the bounding box, max_size()

Definition at line 83 of file bounding_box.C.

References libMesh::is_between(), and libMesh::Real.

84 {
85  libmesh_assert_greater_equal(abs_tol, Real(0));
86  libmesh_assert_greater_equal(rel_tol, Real(0));
87 
88  // Just use the other contains_point overload
89  libmesh_assert_greater(rel_tol+abs_tol, Real(0));
90 
91  // Find absolute tolerance from relative tolerance
92  const Real tol = std::max(abs_tol, this->max_size()*rel_tol);
93 
94  // Make local variables first to make things more clear in a moment
95  const Real my_min_x = this->first(0) - tol;
96  const Real my_max_x = this->second(0) + tol;
97  const bool x_int = is_between(my_min_x, p(0), my_max_x);
98 
99  bool intersection_true = x_int;
100 
101 #if LIBMESH_DIM > 1
102  const Real my_min_y = this->first(1) - tol;
103  const Real my_max_y = this->second(1) + tol;
104  const bool y_int = is_between(my_min_y, p(1), my_max_y);
105 
106  intersection_true = intersection_true && y_int;
107 #endif
108 
109 
110 #if LIBMESH_DIM > 2
111  const Real my_min_z = this->first(2) - tol;
112  const Real my_max_z = this->second(2) + tol;
113  const bool z_int = is_between(my_min_z, p(2), my_max_z);
114 
115  intersection_true = intersection_true && z_int;
116 #endif
117 
118  return intersection_true;
119 }
Real max_size() const
Returns the maximum size of a finite box extent.
Definition: bounding_box.C:66
bool is_between(Real min, Real check, Real max)
Definition: bounding_box.C:30
DIE A HORRIBLE DEATH HERE typedef LIBMESH_DEFAULT_SCALAR_TYPE Real

◆ intersect_with()

void libMesh::BoundingBox::intersect_with ( const BoundingBox other_box)

Sets this bounding box to be the intersection with the other bounding box.

Definition at line 123 of file bounding_box.C.

124 {
125  this->first(0) = std::max(this->first(0), other_box.first(0));
126  this->second(0) = std::min(this->second(0), other_box.second(0));
127 
128 #if LIBMESH_DIM > 1
129  this->first(1) = std::max(this->first(1), other_box.first(1));
130  this->second(1) = std::min(this->second(1), other_box.second(1));
131 #endif
132 
133 #if LIBMESH_DIM > 2
134  this->first(2) = std::max(this->first(2), other_box.first(2));
135  this->second(2) = std::min(this->second(2), other_box.second(2));
136 #endif
137 }

◆ intersects() [1/2]

bool libMesh::BoundingBox::intersects ( const BoundingBox other_box) const
inline
Returns
true if the other bounding box has a non-empty intersection with this bounding box. Exact floating point <= comparisons are performed.

Definition at line 231 of file bounding_box.h.

Referenced by libMesh::TreeNode< N >::insert(), intersects(), BBoxTest::test_no_degenerate(), BBoxTest::test_one_degenerate(), and BBoxTest::test_two_degenerate().

232 {
233  const libMesh::Point & my_lower = this->first;
234  const libMesh::Point & my_upper = this->second;
235 
236  const libMesh::Point & other_lower = other_box.first;
237  const libMesh::Point & other_upper = other_box.second;
238 
239  // Since boxes are tensor products of line intervals it suffices to check
240  // that the line segments for each coordinate axis overlap.
241  for (unsigned int dir=0; dir<LIBMESH_DIM; ++dir)
242  {
243  // Line segments can intersect in two ways:
244  // 1. They can overlap.
245  // 2. One can be inside the other.
246  //
247  // In the first case we want to see if either end point of the second
248  // line segment lies within the first. In the second case we can simply
249  // check that one end point of the first line segment lies in the second
250  // line segment. Note that we don't need, in the second case, to do two
251  // checks since that case is already covered by the first.
252  if (!((my_lower(dir) <= other_lower(dir) &&
253  other_lower(dir) <= my_upper(dir)) ||
254  (my_lower(dir) <= other_upper(dir) &&
255  other_upper(dir) <= my_upper(dir))) &&
256  !((other_lower(dir) <= my_lower(dir) &&
257  my_lower(dir) <= other_upper(dir))))
258  {
259  return false;
260  }
261  }
262 
263  return true;
264 }
A Point defines a location in LIBMESH_DIM dimensional Real space.
Definition: point.h:39

◆ intersects() [2/2]

bool libMesh::BoundingBox::intersects ( const BoundingBox other_box,
Real  abstol 
) const
inline
Returns
true if the other bounding box has a non-empty intersection with this bounding box. abstol is an absolute tolerance used to make "fuzzy" comparisons. abstol must be strictly > 0.0, and both BBoxes being compared are "inflated" by abstol in each direction, i.e. (xmin, ymin, zmin) -> (xmin - abstol, ymin - abstol, zmin - abstol) (xmax, ymax, zmax) -> (xmax + abstol, ymax + abstol, zmax + abstol) before the intersection comparisons are made. This approach can be helpful for detecting intersections between two degenerate (planar) bounding boxes that lie in nearly (to within abstol) the same plane and in certain situations should be considered intersecting.

Definition at line 268 of file bounding_box.h.

References intersects(), and libMesh::libmesh_assert().

270 {
271  // If you want to use abstol==0, you need to call the "exact"
272  // comparison version of the intersects() function.
273  libmesh_assert(abstol > 0.);
274 
275  BoundingBox expanded_my_box = *this;
276  for (unsigned int dir=0; dir<LIBMESH_DIM; ++dir)
277  {
278  expanded_my_box.first(dir) -= abstol;
279  expanded_my_box.second(dir) += abstol;
280  }
281  return expanded_my_box.intersects(other_box);
282 }
BoundingBox()
Default constructor sets invalid bounds.
Definition: bounding_box.h:56
libmesh_assert(ctx)

◆ invalidate()

void libMesh::BoundingBox::invalidate ( )
inline

Sets the bounding box to be "inverted".

This is a useful starting point for future union_with operations.

Definition at line 65 of file bounding_box.h.

Referenced by BoundingBox().

66  {
67  for (unsigned int i=0; i<LIBMESH_DIM; i++)
68  {
69  this->first(i) = std::numeric_limits<Real>::max();
70  this->second(i) = -std::numeric_limits<Real>::max();
71  }
72  }

◆ max() [1/2]

const Point& libMesh::BoundingBox::max ( ) const
inline
Returns
A point at the maximum x,y,z coordinates of the box.

Definition at line 86 of file bounding_box.h.

Referenced by OutputAssembly::interior_assembly(), main(), print(), libMesh::GmshIO::read_mesh(), MeshGenerationTest::testBuildCube(), MeshGenerationTest::testBuildSquare(), and union_with().

87  { return this->second; }

◆ max() [2/2]

Point& libMesh::BoundingBox::max ( )
inline

Definition at line 89 of file bounding_box.h.

90  { return this->second; }

◆ max_size()

Real libMesh::BoundingBox::max_size ( ) const

Returns the maximum size of a finite box extent.

If the bounding box is infinite (or effectively so, e.g. using numeric_limits<Real>::max()) in some dimension, then that dimension is ignored.

Definition at line 66 of file bounding_box.C.

References libMesh::libmesh_isinf(), and libMesh::Real.

67 {
68  Real size = 0;
69  for (unsigned int d = 0; d != LIBMESH_DIM; ++d)
70  {
71  Real sized = this->second(d) - this->first(d);
72  if (!libmesh_isinf(sized) &&
73  this->second(d) != std::numeric_limits<Real>::max() &&
74  this->first(d) != -std::numeric_limits<Real>::max())
75  size = std::max(size, sized);
76  }
77  return size;
78 }
bool libmesh_isinf(T x)
DIE A HORRIBLE DEATH HERE typedef LIBMESH_DEFAULT_SCALAR_TYPE Real

◆ min() [1/2]

const Point& libMesh::BoundingBox::min ( ) const
inline
Returns
A point at the minimum x,y,z coordinates of the box.

Definition at line 77 of file bounding_box.h.

Referenced by OutputAssembly::interior_assembly(), main(), print(), libMesh::GmshIO::read_mesh(), MeshGenerationTest::testBuildCube(), MeshGenerationTest::testBuildSquare(), and union_with().

78  { return this->first; }

◆ min() [2/2]

Point& libMesh::BoundingBox::min ( )
inline

Definition at line 80 of file bounding_box.h.

81  { return this->first; }

◆ print()

void libMesh::BoundingBox::print ( std::ostream &  os = libMesh::out) const

Formatted print, by default to libMesh::out.

Definition at line 212 of file bounding_box.C.

References max(), and min().

213 {
214  os << "(min=" << this->min() << ", max=" << this->max() << ")";
215 }
const Point & min() const
Definition: bounding_box.h:77
const Point & max() const
Definition: bounding_box.h:86

◆ scale()

void libMesh::BoundingBox::scale ( const Real  factor)

Scales each dimension of the bounding box by factor.

Has no effect for dimensions in which either min(dim) == std::numeric_limits<Real>::max() or max(dim) == -std::numeric_limits<Real>::max(), which is the "invalid" state set by the default constructor and by invalidate().

Definition at line 195 of file bounding_box.C.

References dim, and libMesh::Real.

196 {
197  Real append;
198  for (unsigned int dim = 0; dim != LIBMESH_DIM; ++dim)
199  {
200  if (this->first(dim) != std::numeric_limits<Real>::max() &&
201  this->second(dim) != -std::numeric_limits<Real>::max())
202  {
203  libmesh_assert_greater_equal(this->second(dim), this->first(dim));
204  append = (this->second(dim) - this->first(dim)) * factor;
205  this->first(dim) -= append;
206  this->second(dim) += append;
207  }
208  }
209 }
unsigned int dim
DIE A HORRIBLE DEATH HERE typedef LIBMESH_DEFAULT_SCALAR_TYPE Real

◆ signed_distance()

Real libMesh::BoundingBox::signed_distance ( const Point p) const

Computes the signed distance, d, from a given Point p to this BoundingBox.

The sign convention is: d > 0 if the point is outside the BoundingBox d <= 0 if the point is inside the Bounding Box

Definition at line 158 of file bounding_box.C.

References contains_point(), and libMesh::Real.

Referenced by BBoxTest::test_signed_distance().

159 {
160  if (contains_point(p))
161  {
162  // Sign convention: if Point is inside the bbox, the distance is
163  // negative. We then find the smallest distance to the different
164  // sides of the box and return that.
165  Real min_dist = std::numeric_limits<Real>::max();
166 
167  for (unsigned int dir=0; dir<LIBMESH_DIM; ++dir)
168  {
169  min_dist = std::min(min_dist, std::abs(p(dir) - second(dir)));
170  min_dist = std::min(min_dist, std::abs(p(dir) - first(dir)));
171  }
172 
173  return -min_dist;
174  }
175  else // p is outside the box
176  {
177  Real dx[3] = {0., 0., 0.};
178 
179  // Compute distance "above"/"below" the box in each
180  // direction. If the point is somewhere in between the (min,
181  // max) values of the box, dx is 0.
182  for (unsigned int dir=0; dir<LIBMESH_DIM; ++dir)
183  {
184  if (p(dir) > second(dir))
185  dx[dir] = p(dir) - second(dir);
186  else if (p(dir) < first(dir))
187  dx[dir] = p(dir) - first(dir);
188  }
189 
190  return std::sqrt(dx[0]*dx[0] + dx[1]*dx[1] + dx[2]*dx[2]);
191  }
192 }
bool contains_point(const Point &) const
Definition: bounding_box.C:35
DIE A HORRIBLE DEATH HERE typedef LIBMESH_DEFAULT_SCALAR_TYPE Real

◆ union_with() [1/2]

void libMesh::BoundingBox::union_with ( const Point p)
inline

Enlarges this bounding box to include the given point.

Definition at line 286 of file bounding_box.h.

References max(), and min().

Referenced by libMesh::MeshBase::get_info(), libMesh::Cell::loose_bounding_box(), and libMesh::MeshTetInterface::volume_to_surface_mesh().

287 {
288  for (unsigned int i=0; i<LIBMESH_DIM; i++)
289  {
290  min()(i) = std::min(min()(i), p(i));
291  max()(i) = std::max(max()(i), p(i));
292  }
293 }
const Point & min() const
Definition: bounding_box.h:77
const Point & max() const
Definition: bounding_box.h:86

◆ union_with() [2/2]

void libMesh::BoundingBox::union_with ( const BoundingBox other_box)

Sets this bounding box to be the union with the other bounding box.

Definition at line 140 of file bounding_box.C.

141 {
142  this->first(0) = std::min(this->first(0), other_box.first(0));
143  this->second(0) = std::max(this->second(0), other_box.second(0));
144 
145 #if LIBMESH_DIM > 1
146  this->first(1) = std::min(this->first(1), other_box.first(1));
147  this->second(1) = std::max(this->second(1), other_box.second(1));
148 #endif
149 
150 #if LIBMESH_DIM > 2
151  this->first(2) = std::min(this->first(2), other_box.first(2));
152  this->second(2) = std::max(this->second(2), other_box.second(2));
153 #endif
154 }

Friends And Related Function Documentation

◆ operator<<

std::ostream& operator<< ( std::ostream &  os,
const BoundingBox b 
)
friend

Formatted print as above but supports the syntax:

std::cout << b << std::endl;

Definition at line 195 of file bounding_box.h.

196  {
197  b.print(os);
198  return os;
199  }

The documentation for this class was generated from the following files: