libMesh
bounding_box.h
Go to the documentation of this file.
1 // The libMesh Finite Element Library.
2 // Copyright (C) 2002-2024 Benjamin S. Kirk, John W. Peterson, Roy H. Stogner
3 
4 // This library is free software; you can redistribute it and/or
5 // modify it under the terms of the GNU Lesser General Public
6 // License as published by the Free Software Foundation; either
7 // version 2.1 of the License, or (at your option) any later version.
8 
9 // This library is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 // Lesser General Public License for more details.
13 
14 // You should have received a copy of the GNU Lesser General Public
15 // License along with this library; if not, write to the Free Software
16 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 
18 
19 
20 #ifndef LIBMESH_BOUNDING_BOX_H
21 #define LIBMESH_BOUNDING_BOX_H
22 
23 // Local Includes
24 #include "libmesh/libmesh.h"
25 #include "libmesh/point.h" // some compilers want the full definition - I think so they can do
26 // return-value-optimization for BoundingBox'es - BSK
27 
28 // C++ Includes
29 #include <vector>
30 #include <set>
31 #include <limits>
32 
33 namespace libMesh
34 {
35 
40 class BoundingBox : public std::pair<Point, Point>
41 {
42 public:
43 
44  BoundingBox (const Point & new_min,
45  const Point & new_max) :
46  std::pair<Point, Point>(new_min, new_max)
47  {}
48 
49  BoundingBox (const std::pair<Point, Point> & bbox) :
50  std::pair<Point, Point> (bbox)
51  {}
52 
57  {
58  this->invalidate();
59  }
60 
64  void invalidate ()
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  }
72 
76  const Point & min() const
77  { return this->first; }
78 
79  Point & min()
80  { return this->first; }
81 
85  const Point & max() const
86  { return this->second; }
87 
88  Point & max()
89  { return this->second; }
90 
96  bool intersects (const BoundingBox &) const;
97 
112  bool intersects (const BoundingBox &, Real abstol) const;
113 
117  bool contains_point (const Point &) const;
118 
123  void intersect_with (const BoundingBox &);
124 
128  void union_with (const Point & p);
129 
134  void union_with (const BoundingBox &);
135 
142  Real signed_distance(const Point & p) const;
143 
153  void scale(const Real factor);
154 };
155 
156 
157 
158 // ------------------------------------------------------------
159 // BoundingBox class member functions
160 
161 // BoundingBox::intersects() is about 30% faster when inlined, so its definition
162 // is here instead of in the source file.
163 inline
164 bool
165 BoundingBox::intersects(const BoundingBox & other_box) const
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 }
199 
200 inline
201 bool
203  Real abstol) const
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 }
217 
218 inline
219 void
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 }
228 
229 } // namespace libMesh
230 
231 
232 #endif // LIBMESH_BOUNDING_BOX_H
BoundingBox()
Default constructor sets invalid bounds.
Definition: bounding_box.h:56
bool contains_point(const Point &) const
Definition: bounding_box.C:35
BoundingBox(const std::pair< Point, Point > &bbox)
Definition: bounding_box.h:49
void intersect_with(const BoundingBox &)
Sets this bounding box to be the intersection with the other bounding box.
Definition: bounding_box.C:65
bool intersects(const BoundingBox &) const
Definition: bounding_box.h:165
void invalidate()
Sets the bounding box to encompass the universe.
Definition: bounding_box.h:64
The libMesh namespace provides an interface to certain functionality in the library.
Real signed_distance(const Point &p) const
Computes the signed distance, d, from a given Point p to this BoundingBox.
Definition: bounding_box.C:100
const Point & min() const
Definition: bounding_box.h:76
libmesh_assert(ctx)
BoundingBox(const Point &new_min, const Point &new_max)
Definition: bounding_box.h:44
Defines a Cartesian bounding box by the two corner extremum.
Definition: bounding_box.h:40
DIE A HORRIBLE DEATH HERE typedef LIBMESH_DEFAULT_SCALAR_TYPE Real
void union_with(const Point &p)
Enlarges this bounding box to include the given point.
Definition: bounding_box.h:220
const Point & max() const
Definition: bounding_box.h:85
void scale(const Real factor)
Scales each dimension of the bounding box by factor.
Definition: bounding_box.C:137
A Point defines a location in LIBMESH_DIM dimensional Real space.
Definition: point.h:39