libMesh
bounding_box.C
Go to the documentation of this file.
1 // The libMesh Finite Element Library.
2 // Copyright (C) 2002-2017 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 // Local includes
21 #include "libmesh/bounding_box.h"
22 
23 
24 
25 namespace libMesh
26 {
27 // Small helper function to make intersects() more readable.
28 bool is_between(Real min, Real check, Real max)
29 {
30  return min <= check && check <= max;
31 }
32 
33 bool BoundingBox::intersects (const BoundingBox & other_box) const
34 {
35  // Make local variables first to make things more clear in a moment
36  const Real & my_min_x = this->first(0);
37  const Real & my_max_x = this->second(0);
38  const Real & other_min_x = other_box.first(0);
39  const Real & other_max_x = other_box.second(0);
40 
41  const bool x_int = is_between(my_min_x, other_min_x, my_max_x) || is_between(my_min_x, other_max_x, my_max_x) ||
42  is_between(other_min_x, my_min_x, other_max_x) || is_between(other_min_x, my_max_x, other_max_x);
43 
44  bool intersection_true = x_int;
45 
46 #if LIBMESH_DIM > 1
47  const Real & my_min_y = this->first(1);
48  const Real & my_max_y = this->second(1);
49  const Real & other_min_y = other_box.first(1);
50  const Real & other_max_y = other_box.second(1);
51 
52  const bool y_int = is_between(my_min_y, other_min_y, my_max_y) || is_between(my_min_y, other_max_y, my_max_y) ||
53  is_between(other_min_y, my_min_y, other_max_y) || is_between(other_min_y, my_max_y, other_max_y);
54 
55  intersection_true = intersection_true && y_int;
56 #endif
57 
58 #if LIBMESH_DIM > 2
59  const Real & my_min_z = this->first(2);
60  const Real & my_max_z = this->second(2);
61  const Real & other_min_z = other_box.first(2);
62  const Real & other_max_z = other_box.second(2);
63 
64  const bool z_int = is_between(my_min_z, other_min_z, my_max_z) || is_between(my_min_z, other_max_z, my_max_z) ||
65  is_between(other_min_z, my_min_z, other_max_z) || is_between(other_min_z, my_max_z, other_max_z);
66 
67  intersection_true = intersection_true && z_int;
68 #endif
69 
70  return intersection_true;
71 }
72 
73 bool BoundingBox::contains_point (const Point & p) const
74 {
75  // Make local variables first to make things more clear in a moment
76  Real my_min_x = this->first(0);
77  Real my_max_x = this->second(0);
78  bool x_int = is_between(my_min_x, p(0), my_max_x);
79 
80  bool intersection_true = x_int;
81 
82 #if LIBMESH_DIM > 1
83  Real my_min_y = this->first(1);
84  Real my_max_y = this->second(1);
85  bool y_int = is_between(my_min_y, p(1), my_max_y);
86 
87  intersection_true = intersection_true && y_int;
88 #endif
89 
90 
91 #if LIBMESH_DIM > 2
92  Real my_min_z = this->first(2);
93  Real my_max_z = this->second(2);
94  bool z_int = is_between(my_min_z, p(2), my_max_z);
95 
96  intersection_true = intersection_true && z_int;
97 #endif
98 
99  return intersection_true;
100 }
101 
102 
103 void BoundingBox::intersect_with (const BoundingBox & other_box)
104 {
105  this->first(0) = std::max(this->first(0), other_box.first(0));
106  this->second(0) = std::min(this->second(0), other_box.second(0));
107 
108 #if LIBMESH_DIM > 1
109  this->first(1) = std::max(this->first(1), other_box.first(1));
110  this->second(1) = std::min(this->second(1), other_box.second(1));
111 #endif
112 
113 #if LIBMESH_DIM > 2
114  this->first(2) = std::max(this->first(2), other_box.first(2));
115  this->second(2) = std::min(this->second(2), other_box.second(2));
116 #endif
117 }
118 
119 
120 void BoundingBox::union_with (const BoundingBox & other_box)
121 {
122  this->first(0) = std::min(this->first(0), other_box.first(0));
123  this->second(0) = std::max(this->second(0), other_box.second(0));
124 
125 #if LIBMESH_DIM > 1
126  this->first(1) = std::min(this->first(1), other_box.first(1));
127  this->second(1) = std::max(this->second(1), other_box.second(1));
128 #endif
129 
130 #if LIBMESH_DIM > 2
131  this->first(2) = std::min(this->first(2), other_box.first(2));
132  this->second(2) = std::max(this->second(2), other_box.second(2));
133 #endif
134 }
135 
136 
137 
138 } // namespace libMesh
bool contains_point(const Point &) const
Definition: bounding_box.C:73
bool intersects(const BoundingBox &) const
Definition: bounding_box.C:33
void intersect_with(const BoundingBox &)
Definition: bounding_box.C:103
The libMesh namespace provides an interface to certain functionality in the library.
long double max(long double a, double b)
bool is_between(Real min, Real check, Real max)
Definition: bounding_box.C:28
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)
Definition: bounding_box.h:140
long double min(long double a, double b)
A Point defines a location in LIBMESH_DIM dimensional Real space.
Definition: point.h:38