libMesh
Public Member Functions | 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 encompass the universe. More...
 
const Pointmin () const
 
Pointmin ()
 
const Pointmax () const
 
Pointmax ()
 
bool intersects (const BoundingBox &) const
 
bool intersects (const BoundingBox &, Real abstol) const
 
bool contains_point (const Point &) 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...
 

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 encompass the universe.
Definition: bounding_box.h:64

Member Function Documentation

◆ contains_point()

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(), 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

◆ 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 65 of file bounding_box.C.

66 {
67  this->first(0) = std::max(this->first(0), other_box.first(0));
68  this->second(0) = std::min(this->second(0), other_box.second(0));
69 
70 #if LIBMESH_DIM > 1
71  this->first(1) = std::max(this->first(1), other_box.first(1));
72  this->second(1) = std::min(this->second(1), other_box.second(1));
73 #endif
74 
75 #if LIBMESH_DIM > 2
76  this->first(2) = std::max(this->first(2), other_box.first(2));
77  this->second(2) = std::min(this->second(2), other_box.second(2));
78 #endif
79 }

◆ 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 165 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().

166 {
167  const libMesh::Point & my_lower = this->first;
168  const libMesh::Point & my_upper = this->second;
169 
170  const libMesh::Point & other_lower = other_box.first;
171  const libMesh::Point & other_upper = other_box.second;
172 
173  // Since boxes are tensor products of line intervals it suffices to check
174  // that the line segments for each coordinate axis overlap.
175  for (unsigned int dir=0; dir<LIBMESH_DIM; ++dir)
176  {
177  // Line segments can intersect in two ways:
178  // 1. They can overlap.
179  // 2. One can be inside the other.
180  //
181  // In the first case we want to see if either end point of the second
182  // line segment lies within the first. In the second case we can simply
183  // check that one end point of the first line segment lies in the second
184  // line segment. Note that we don't need, in the second case, to do two
185  // checks since that case is already covered by the first.
186  if (!((my_lower(dir) <= other_lower(dir) &&
187  other_lower(dir) <= my_upper(dir)) ||
188  (my_lower(dir) <= other_upper(dir) &&
189  other_upper(dir) <= my_upper(dir))) &&
190  !((other_lower(dir) <= my_lower(dir) &&
191  my_lower(dir) <= other_upper(dir))))
192  {
193  return false;
194  }
195  }
196 
197  return true;
198 }
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 202 of file bounding_box.h.

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

204 {
205  // If you want to use abstol==0, you need to call the "exact"
206  // comparison version of the intersects() function.
207  libmesh_assert(abstol > 0.);
208 
209  BoundingBox expanded_my_box = *this;
210  for (unsigned int dir=0; dir<LIBMESH_DIM; ++dir)
211  {
212  expanded_my_box.first(dir) -= abstol;
213  expanded_my_box.second(dir) += abstol;
214  }
215  return expanded_my_box.intersects(other_box);
216 }
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 encompass the universe.

Definition at line 64 of file bounding_box.h.

Referenced by BoundingBox().

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

◆ 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 85 of file bounding_box.h.

Referenced by OutputAssembly::interior_assembly(), main(), MeshGenerationTest::testBuildCube(), MeshGenerationTest::testBuildSquare(), and union_with().

86  { return this->second; }

◆ max() [2/2]

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

Definition at line 88 of file bounding_box.h.

89  { return this->second; }

◆ 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 76 of file bounding_box.h.

Referenced by OutputAssembly::interior_assembly(), main(), MeshGenerationTest::testBuildCube(), MeshGenerationTest::testBuildSquare(), and union_with().

77  { return this->first; }

◆ min() [2/2]

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

Definition at line 79 of file bounding_box.h.

80  { return this->first; }

◆ 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 137 of file bounding_box.C.

References dim, and libMesh::Real.

138 {
139  Real append;
140  for (unsigned int dim = 0; dim != LIBMESH_DIM; ++dim)
141  {
142  if (this->first(dim) != std::numeric_limits<Real>::max() &&
143  this->second(dim) != -std::numeric_limits<Real>::max())
144  {
145  libmesh_assert_greater_equal(this->second(dim), this->first(dim));
146  append = (this->second(dim) - this->first(dim)) * factor;
147  this->first(dim) -= append;
148  this->second(dim) += append;
149  }
150  }
151 }
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 100 of file bounding_box.C.

References std::abs(), contains_point(), libMesh::Real, and std::sqrt().

Referenced by BBoxTest::test_signed_distance().

101 {
102  if (contains_point(p))
103  {
104  // Sign convention: if Point is inside the bbox, the distance is
105  // negative. We then find the smallest distance to the different
106  // sides of the box and return that.
107  Real min_dist = std::numeric_limits<Real>::max();
108 
109  for (unsigned int dir=0; dir<LIBMESH_DIM; ++dir)
110  {
111  min_dist = std::min(min_dist, std::abs(p(dir) - second(dir)));
112  min_dist = std::min(min_dist, std::abs(p(dir) - first(dir)));
113  }
114 
115  return -min_dist;
116  }
117  else // p is outside the box
118  {
119  Real dx[3] = {0., 0., 0.};
120 
121  // Compute distance "above"/"below" the box in each
122  // direction. If the point is somewhere in between the (min,
123  // max) values of the box, dx is 0.
124  for (unsigned int dir=0; dir<LIBMESH_DIM; ++dir)
125  {
126  if (p(dir) > second(dir))
127  dx[dir] = p(dir) - second(dir);
128  else if (p(dir) < first(dir))
129  dx[dir] = p(dir) - first(dir);
130  }
131 
132  return std::sqrt(dx[0]*dx[0] + dx[1]*dx[1] + dx[2]*dx[2]);
133  }
134 }
bool contains_point(const Point &) const
Definition: bounding_box.C:35
ADRealEigenVector< T, D, asd > sqrt(const ADRealEigenVector< T, D, asd > &)
Definition: type_vector.h:53
ADRealEigenVector< T, D, asd > abs(const ADRealEigenVector< T, D, asd > &)
Definition: type_vector.h:57
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 220 of file bounding_box.h.

References max(), and min().

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

221 {
222  for (unsigned int i=0; i<LIBMESH_DIM; i++)
223  {
224  min()(i) = std::min(min()(i), p(i));
225  max()(i) = std::max(max()(i), p(i));
226  }
227 }
const Point & min() const
Definition: bounding_box.h:76
const Point & max() const
Definition: bounding_box.h:85

◆ 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 82 of file bounding_box.C.

83 {
84  this->first(0) = std::min(this->first(0), other_box.first(0));
85  this->second(0) = std::max(this->second(0), other_box.second(0));
86 
87 #if LIBMESH_DIM > 1
88  this->first(1) = std::min(this->first(1), other_box.first(1));
89  this->second(1) = std::max(this->second(1), other_box.second(1));
90 #endif
91 
92 #if LIBMESH_DIM > 2
93  this->first(2) = std::min(this->first(2), other_box.first(2));
94  this->second(2) = std::max(this->second(2), other_box.second(2));
95 #endif
96 }

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