Reference documentation for deal.II version Git 932f7faded 2020-11-28 20:02:43 +0100
\(\newcommand{\dealvcentcolon}{\mathrel{\mathop{:}}}\) \(\newcommand{\dealcoloneq}{\dealvcentcolon\mathrel{\mkern-1.2mu}=}\) \(\newcommand{\jump}[1]{\left[\!\left[ #1 \right]\!\right]}\) \(\newcommand{\average}[1]{\left\{\!\left\{ #1 \right\}\!\right\}}\)
bounding_box.cc
Go to the documentation of this file.
1 // ---------------------------------------------------------------------
2 //
3 // Copyright (C) 2017 - 2020 by the deal.II authors
4 //
5 // This file is part of the deal.II library.
6 //
7 // The deal.II library is free software; you can use it, redistribute
8 // it, and/or modify it under the terms of the GNU Lesser General
9 // Public License as published by the Free Software Foundation; either
10 // version 2.1 of the License, or (at your option) any later version.
11 // The full text of the license can be found in the file LICENSE.md at
12 // the top level directory of deal.II.
13 //
14 // ---------------------------------------------------------------------
17 
19 
20 template <int spacedim, typename Number>
21 bool
23  const double tolerance) const
24 {
25  for (unsigned int i = 0; i < spacedim; ++i)
26  {
27  // Bottom left-top right convention: the point is outside if it's smaller
28  // than the first or bigger than the second boundary point The bounding
29  // box is defined as a closed set
30  if ((p[i] < this->boundary_points.first[i] -
31  tolerance * std::abs(this->boundary_points.second[i] -
32  this->boundary_points.first[i])) ||
33  (p[i] > this->boundary_points.second[i] +
34  tolerance * std::abs(this->boundary_points.second[i] -
35  this->boundary_points.first[i])))
36  return false;
37  }
38  return true;
39 }
40 
41 template <int spacedim, typename Number>
42 void
44  const BoundingBox<spacedim, Number> &other_bbox)
45 {
46  for (unsigned int i = 0; i < spacedim; ++i)
47  {
48  this->boundary_points.first[i] =
49  std::min(this->boundary_points.first[i],
50  other_bbox.boundary_points.first[i]);
51  this->boundary_points.second[i] =
52  std::max(this->boundary_points.second[i],
53  other_bbox.boundary_points.second[i]);
54  }
55 }
56 
57 template <int spacedim, typename Number>
60  const BoundingBox<spacedim, Number> &other_bbox) const
61 {
62  if (spacedim == 1)
63  {
64  // In dimension 1 if the two bounding box are neighbors
65  // we can merge them
66  if (this->point_inside(other_bbox.boundary_points.first) ||
67  this->point_inside(other_bbox.boundary_points.second))
70  }
71  else
72  {
73  const std::array<Point<spacedim, Number>, 2> bbox1 = {
74  {this->get_boundary_points().first,
75  this->get_boundary_points().second}};
76  const std::array<Point<spacedim, Number>, 2> bbox2 = {
77  {other_bbox.get_boundary_points().first,
78  other_bbox.get_boundary_points().second}};
79 
80  // Step 1: testing if the boxes are close enough to intersect
81  for (unsigned int d = 0; d < spacedim; ++d)
82  if (bbox1[0][d] * (1 - std::numeric_limits<Number>::epsilon()) >
83  bbox2[1][d] ||
84  bbox2[0][d] * (1 - std::numeric_limits<Number>::epsilon()) >
85  bbox1[1][d])
87 
88  // The boxes intersect: we need to understand now how they intersect.
89  // We begin by computing the intersection:
90  std::array<double, spacedim> intersect_bbox_min;
91  std::array<double, spacedim> intersect_bbox_max;
92  for (unsigned int d = 0; d < spacedim; ++d)
93  {
94  intersect_bbox_min[d] = std::max(bbox1[0][d], bbox2[0][d]);
95  intersect_bbox_max[d] = std::min(bbox1[1][d], bbox2[1][d]);
96  }
97 
98  // Finding the intersection's dimension
99  int intersect_dim = spacedim;
100  for (unsigned int d = 0; d < spacedim; ++d)
101  if (std::abs(intersect_bbox_min[d] - intersect_bbox_max[d]) <=
103  (std::abs(intersect_bbox_min[d]) +
104  std::abs(intersect_bbox_max[d])))
105  --intersect_dim;
106 
107  if (intersect_dim == 0 || intersect_dim == spacedim - 2)
109 
110  // Checking the two mergeable cases: first if the boxes are aligned so
111  // that they can be merged
112  unsigned int not_align_1 = 0, not_align_2 = 0;
113  bool same_direction = true;
114  for (unsigned int d = 0; d < spacedim; ++d)
115  {
116  if (std::abs(bbox2[0][d] - bbox1[0][d]) >
118  (std::abs(bbox2[0][d]) + std::abs(bbox1[0][d])))
119  ++not_align_1;
120  if (std::abs(bbox1[1][d] - bbox2[1][d]) >
122  (std::abs(bbox1[1][d]) + std::abs(bbox2[1][d])))
123  ++not_align_2;
124  if (not_align_1 != not_align_2)
125  {
126  same_direction = false;
127  break;
128  }
129  }
130 
131  if (not_align_1 <= 1 && not_align_2 <= 1 && same_direction)
133 
134  // Second: one box is contained/equal to the other
135  if ((this->point_inside(bbox2[0]) && this->point_inside(bbox2[1])) ||
136  (other_bbox.point_inside(bbox1[0]) &&
137  other_bbox.point_inside(bbox1[1])))
139 
140  // Degenerate and mergeable cases have been found, it remains:
142  }
143 }
144 
145 
146 
147 template <int spacedim, typename Number>
148 std::pair<Point<spacedim, Number>, Point<spacedim, Number>> &
150 {
151  return this->boundary_points;
152 }
153 
154 
155 
156 template <int spacedim, typename Number>
157 const std::pair<Point<spacedim, Number>, Point<spacedim, Number>> &
159 {
160  return this->boundary_points;
161 }
162 
163 
164 
165 template <int spacedim, typename Number>
166 double
168 {
169  double vol = 1.0;
170  for (unsigned int i = 0; i < spacedim; ++i)
171  vol *= (this->boundary_points.second[i] - this->boundary_points.first[i]);
172  return vol;
173 }
174 
175 
176 
177 template <int spacedim, typename Number>
178 Number
179 BoundingBox<spacedim, Number>::lower_bound(const unsigned int direction) const
180 {
181  AssertIndexRange(direction, spacedim);
182 
183  return boundary_points.first[direction];
184 }
185 
186 
187 
188 template <int spacedim, typename Number>
189 Number
190 BoundingBox<spacedim, Number>::upper_bound(const unsigned int direction) const
191 {
192  AssertIndexRange(direction, spacedim);
193 
194  return boundary_points.second[direction];
195 }
196 
197 
198 
199 template <int spacedim, typename Number>
202 {
204  for (unsigned int i = 0; i < spacedim; ++i)
205  point[i] = .5 * (boundary_points.first[i] + boundary_points.second[i]);
206 
207  return point;
208 }
209 
210 
211 
212 template <int spacedim, typename Number>
214 BoundingBox<spacedim, Number>::bounds(const unsigned int direction) const
215 {
216  AssertIndexRange(direction, spacedim);
217 
218  std::pair<Point<1, Number>, Point<1, Number>> lower_upper_bounds;
219  lower_upper_bounds.first[0] = lower_bound(direction);
220  lower_upper_bounds.second[0] = upper_bound(direction);
221 
222  return BoundingBox<1, Number>(lower_upper_bounds);
223 }
224 
225 
226 
227 template <int spacedim, typename Number>
228 Number
229 BoundingBox<spacedim, Number>::side_length(const unsigned int direction) const
230 {
231  AssertIndexRange(direction, spacedim);
232 
233  return boundary_points.second[direction] - boundary_points.first[direction];
234 }
235 
236 
237 
238 template <int spacedim, typename Number>
240 BoundingBox<spacedim, Number>::vertex(const unsigned int index) const
241 {
243 
244  const Point<spacedim> unit_cell_vertex =
246 
248  for (unsigned int i = 0; i < spacedim; ++i)
249  point[i] = boundary_points.first[i] + side_length(i) * unit_cell_vertex[i];
250 
251  return point;
252 }
253 
254 
255 
256 template <int spacedim, typename Number>
258 BoundingBox<spacedim, Number>::child(const unsigned int index) const
259 {
261 
262  // Vertex closest to child.
263  const Point<spacedim, Number> parent_vertex = vertex(index);
264  const Point<spacedim, Number> parent_center = center();
265 
266  const Point<spacedim> upper_corner_unit_cell =
269 
270  const Point<spacedim> lower_corner_unit_cell =
272 
273  std::pair<Point<spacedim, Number>, Point<spacedim, Number>>
274  child_lower_upper_corner;
275  for (unsigned int i = 0; i < spacedim; ++i)
276  {
277  const double child_side_length = side_length(i) / 2;
278 
279  const double child_center = (parent_center[i] + parent_vertex[i]) / 2;
280 
281  child_lower_upper_corner.first[i] =
282  child_center + child_side_length * (lower_corner_unit_cell[i] - .5);
283  child_lower_upper_corner.second[i] =
284  child_center + child_side_length * (upper_corner_unit_cell[i] - .5);
285  }
286 
287  return BoundingBox<spacedim, Number>(child_lower_upper_corner);
288 }
289 
290 
291 
292 template <int spacedim, typename Number>
293 BoundingBox<spacedim - 1, Number>
294 BoundingBox<spacedim, Number>::cross_section(const unsigned int direction) const
295 {
296  AssertIndexRange(direction, spacedim);
297 
298  std::pair<Point<spacedim - 1, Number>, Point<spacedim - 1, Number>>
299  cross_section_lower_upper_corner;
300  for (unsigned int d = 0; d < spacedim - 1; ++d)
301  {
302  const int index_to_write_from =
303  internal::coordinate_to_one_dim_higher<spacedim - 1>(direction, d);
304 
305  cross_section_lower_upper_corner.first[d] =
306  boundary_points.first[index_to_write_from];
307 
308  cross_section_lower_upper_corner.second[d] =
309  boundary_points.second[index_to_write_from];
310  }
311 
312  return BoundingBox<spacedim - 1, Number>(cross_section_lower_upper_corner);
313 }
314 
315 
316 
317 template <int spacedim, typename Number>
320  const Point<spacedim, Number> &point) const
321 {
322  auto unit = point;
323  const auto diag = boundary_points.second - boundary_points.first;
324  unit -= boundary_points.first;
325  for (unsigned int d = 0; d < spacedim; ++d)
326  unit[d] /= diag[d];
327  return unit;
328 }
329 
330 
331 
332 template <int spacedim, typename Number>
335  const Point<spacedim, Number> &point) const
336 {
337  auto real = boundary_points.first;
338  const auto diag = boundary_points.second - boundary_points.first;
339  for (unsigned int d = 0; d < spacedim; ++d)
340  real[d] += diag[d] * point[d];
341  return real;
342 }
343 
344 
345 
346 template <int dim, typename Number>
349 {
350  std::pair<Point<dim, Number>, Point<dim, Number>> lower_upper_corner;
351  for (unsigned int i = 0; i < dim; ++i)
352  {
353  lower_upper_corner.second[i] = 1;
354  }
355  return BoundingBox<dim, Number>(lower_upper_corner);
356 }
357 
358 
359 #include "bounding_box.inst"
Point< spacedim, Number > vertex(const unsigned int index) const
Iterator lower_bound(Iterator first, Iterator last, const T &val)
Definition: utilities.h:1075
NeighborType
Definition: bounding_box.h:37
#define AssertIndexRange(index, range)
Definition: exceptions.h:1691
static Point< dim > unit_cell_vertex(const unsigned int vertex)
void merge_with(const BoundingBox< spacedim, Number > &other_bbox)
Definition: bounding_box.cc:43
Number lower_bound(const unsigned int direction) const
Definition: point.h:110
SymmetricTensor< 2, dim, Number > epsilon(const Tensor< 2, dim, Number > &Grad_u)
Number upper_bound(const unsigned int direction) const
Point< spacedim, Number > unit_to_real(const Point< spacedim, Number > &point) const
BoundingBox< spacedim, Number > child(const unsigned int index) const
Point< spacedim, Number > center() const
#define DEAL_II_NAMESPACE_CLOSE
Definition: config.h:372
std::pair< Point< spacedim, Number >, Point< spacedim, Number > > boundary_points
Definition: bounding_box.h:296
BoundingBox< spacedim - 1, Number > cross_section(const unsigned int direction) const
Point< spacedim > point(const gp_Pnt &p, const double tolerance=1e-10)
Definition: utilities.cc:188
SymmetricTensor< 2, dim, Number > d(const Tensor< 2, dim, Number > &F, const Tensor< 2, dim, Number > &dF_dt)
NeighborType get_neighbor_type(const BoundingBox< spacedim, Number > &other_bbox) const
Definition: bounding_box.cc:59
Number side_length(const unsigned int direction) const
#define DEAL_II_NAMESPACE_OPEN
Definition: config.h:371
T min(const T &t, const MPI_Comm &mpi_communicator)
Point< spacedim, Number > real_to_unit(const Point< spacedim, Number > &point) const
Point< 3 > center
std::pair< Point< spacedim, Number >, Point< spacedim, Number > > & get_boundary_points()
BoundingBox< 1, Number > bounds(const unsigned int direction) const
BoundingBox< dim, Number > create_unit_bounding_box()
double volume() const
T max(const T &t, const MPI_Comm &mpi_communicator)
bool point_inside(const Point< spacedim, Number > &p, const double tolerance=std::numeric_limits< Number >::epsilon()) const
Definition: bounding_box.cc:22