31#ifndef DUMUX_GEOMETRY_BOUNDINGBOXTREE_HH
32#define DUMUX_GEOMETRY_BOUNDINGBOXTREE_HH
42#include <dune/common/promotiontraits.hh>
43#include <dune/common/timer.hh>
44#include <dune/common/fvector.hh>
66template <
class GeometricEntitySet>
69 enum { dimworld = GeometricEntitySet::dimensionworld };
70 using ctype =
typename GeometricEntitySet::ctype;
77 struct BoundingBoxNode
95 void build(std::shared_ptr<const GeometricEntitySet> set)
101 boundingBoxNodes_.clear();
102 boundingBoxCoordinates_.clear();
108 const auto numLeaves = set->size();
111 const auto numNodes = 2*numLeaves - 1;
112 boundingBoxNodes_.reserve(numNodes);
113 boundingBoxCoordinates_.reserve(numNodes*2*dimworld);
116 std::vector<ctype> leafBoxes(2*dimworld*numLeaves);
118 for (
const auto& geometricEntity : *set)
119 computeEntityBoundingBox_(leafBoxes.data() + 2*dimworld*set->index(geometricEntity), geometricEntity);
122 std::vector<std::size_t> leafPartition(numLeaves);
123 std::iota(leafPartition.begin(), leafPartition.end(), 0);
126 build_(leafBoxes, leafPartition.begin(), leafPartition.end());
130 <<
" nodes for " << numLeaves <<
" grid entities in "
131 << timer.stop() <<
" seconds." << std::endl;
136 {
return *entitySet_; }
144 {
return boundingBoxNodes_[nodeIdx]; }
148 {
return boundingBoxCoordinates_.data() + 2*dimworld*nodeIdx; }
152 {
return boundingBoxNodes_.size(); }
156 bool isLeaf(
const BoundingBoxNode& node, std::size_t nodeIdx)
const
157 {
return node.child0 == nodeIdx; }
162 std::vector<BoundingBoxNode> boundingBoxNodes_;
165 std::vector<ctype> boundingBoxCoordinates_;
168 std::shared_ptr<const EntitySet> entitySet_;
171 template <
class Entity>
172 void computeEntityBoundingBox_(ctype* b,
const Entity& entity)
const
176 ctype* xMax = b + dimworld;
179 auto geometry = entity.geometry();
182 auto corner = geometry.corner(0);
183 for (std::size_t dimIdx = 0; dimIdx < dimworld; ++dimIdx)
184 xMin[dimIdx] = xMax[dimIdx] = corner[dimIdx];
187 for (std::size_t cornerIdx = 1; cornerIdx < geometry.corners(); ++cornerIdx)
189 corner = geometry.corner(cornerIdx);
190 for (std::size_t dimIdx = 0; dimIdx < dimworld; ++dimIdx)
194 xMin[dimIdx] = min(xMin[dimIdx], corner[dimIdx]);
195 xMax[dimIdx] = max(xMax[dimIdx], corner[dimIdx]);
201 std::size_t build_(
const std::vector<ctype>& leafBoxes,
202 const std::vector<std::size_t>::iterator& begin,
203 const std::vector<std::size_t>::iterator& end)
208 if (end - begin == 1)
211 const std::size_t leafNodeIdx = *begin;
212 const auto beginCoords = leafBoxes.begin() + 2*dimworld*leafNodeIdx;
213 const auto endCoords = beginCoords + 2*dimworld;
218 return addBoundingBox_(BoundingBoxNode{
numBoundingBoxes(), leafNodeIdx}, beginCoords, endCoords);
222 const auto bCoords = computeBBoxOfBBoxes_(leafBoxes, begin, end);
225 const auto axis = computeLongestAxis_(bCoords);
229 auto middle = begin + (end - begin)/2;
230 std::nth_element(begin, middle, end, [&leafBoxes, axis](std::size_t i, std::size_t j)
232 const ctype* bi = leafBoxes.data() + 2*dimworld*i;
233 const ctype* bj = leafBoxes.data() + 2*dimworld*j;
234 return bi[axis] + bi[axis + dimworld] < bj[axis] + bj[axis + dimworld];
239 return addBoundingBox_(BoundingBoxNode{build_(leafBoxes, begin, middle), build_(leafBoxes, middle, end)},
240 bCoords.begin(), bCoords.end());
244 template <
class Iterator>
245 std::size_t addBoundingBox_(BoundingBoxNode&& node,
246 const Iterator& coordBegin,
247 const Iterator& coordEnd)
250 boundingBoxNodes_.emplace_back(node);
253 boundingBoxCoordinates_.insert(boundingBoxCoordinates_.end(), coordBegin, coordEnd);
256 return boundingBoxNodes_.size() - 1;
260 std::array<ctype, 2*dimworld>
261 computeBBoxOfBBoxes_(
const std::vector<ctype>& leafBoxes,
262 const std::vector<std::size_t>::iterator& begin,
263 const std::vector<std::size_t>::iterator& end)
265 std::array<ctype, 2*dimworld> bBoxCoords;
269 const auto* bFirst = leafBoxes.data() + 2*dimworld*(*it);
271 for (
int coordIdx = 0; coordIdx < 2*dimworld; ++coordIdx)
272 bBoxCoords[coordIdx] = bFirst[coordIdx];
275 for (; it != end; ++it)
277 const auto* b = leafBoxes.data() + 2*dimworld*(*it);
278 for (
int coordIdx = 0; coordIdx < dimworld; ++coordIdx)
279 if (b[coordIdx] < bBoxCoords[coordIdx])
280 bBoxCoords[coordIdx] = b[coordIdx];
281 for (
int coordIdx = dimworld; coordIdx < 2*dimworld; ++coordIdx)
282 if (b[coordIdx] > bBoxCoords[coordIdx])
283 bBoxCoords[coordIdx] = b[coordIdx];
290 std::size_t computeLongestAxis_(
const std::array<ctype, 2*dimworld>& bCoords)
292 std::array<ctype, dimworld> axisLength;
293 for (
int coordIdx = 0; coordIdx < dimworld; ++coordIdx)
294 axisLength[coordIdx] = bCoords[dimworld + coordIdx] - bCoords[coordIdx];
296 return std::distance(axisLength.begin(), std::max_element(axisLength.begin(), axisLength.end()));
305template<
class ctype,
int dimworld,
typename std::enable_if_t<dimworld == 3,
int> = 0>
308 static constexpr ctype eps_ = 1.0e-7;
311 const auto dx = b[3] - b[0];
312 const auto dy = b[4] - b[1];
313 const auto dz = b[5] - b[2];
314 const ctype eps = max({dx, dy, dz})*eps_;
315 return (b[0] - eps <= point[0] && point[0] <= b[3] + eps &&
316 b[1] - eps <= point[1] && point[1] <= b[4] + eps &&
317 b[2] - eps <= point[2] && point[2] <= b[5] + eps);
325template<
class ctype,
int dimworld,
typename std::enable_if_t<dimworld == 2,
int> = 0>
328 static constexpr ctype eps_ = 1.0e-7;
331 const auto dx = b[2] - b[0];
332 const auto dy = b[3] - b[1];
333 const ctype eps = max(dx, dy)*eps_;
334 return (b[0] - eps <= point[0] && point[0] <= b[2] + eps &&
335 b[1] - eps <= point[1] && point[1] <= b[3] + eps);
343template<
class ctype,
int dimworld,
typename std::enable_if_t<dimworld == 1,
int> = 0>
346 static constexpr ctype eps_ = 1.0e-7;
347 const ctype eps0 = eps_*(b[1] - b[0]);
348 return b[0] - eps0 <= point[0] && point[0] <= b[1] + eps0;
356template<
class ctype,
int dimworld>
358 const Dune::FieldVector<ctype, dimworld>& min,
359 const Dune::FieldVector<ctype, dimworld>& max)
361 std::array<ctype, 2*dimworld> bBox;
362 std::copy(min.begin(), min.end(), bBox.begin());
363 std::copy(max.begin(), max.end(), bBox.begin()+dimworld);
372template<
int dimworld,
class ctypea,
class ctypeb,
typename std::enable_if_t<dimworld == 3,
int> = 0>
375 using ctype =
typename Dune::PromotionTraits<ctypea, ctypeb>::PromotedType;
376 static constexpr ctype eps_ = 1.0e-7;
377 const ctype eps0 = eps_*std::max(b[3]-b[0], a[3]-a[0]);
378 const ctype eps1 = eps_*std::max(b[4]-b[1], a[4]-a[1]);
379 const ctype eps2 = eps_*std::max(b[5]-b[2], a[5]-a[2]);
380 return (b[0] - eps0 <= a[3] && a[0] <= b[3] + eps0 &&
381 b[1] - eps1 <= a[4] && a[1] <= b[4] + eps1 &&
382 b[2] - eps2 <= a[5] && a[2] <= b[5] + eps2);
391template<
int dimworld,
class ctypea,
class ctypeb,
typename std::enable_if_t<dimworld == 2,
int> = 0>
394 using ctype =
typename Dune::PromotionTraits<ctypea, ctypeb>::PromotedType;
395 static constexpr ctype eps_ = 1.0e-7;
396 const ctype eps0 = eps_*std::max(b[2]-b[0], a[2]-a[0]);
397 const ctype eps1 = eps_*std::max(b[3]-b[1], a[3]-a[1]);
398 return (b[0] - eps0 <= a[2] && a[0] <= b[2] + eps0 &&
399 b[1] - eps1 <= a[3] && a[1] <= b[3] + eps1);
407template<
int dimworld,
class ctypea,
class ctypeb,
typename std::enable_if_t<dimworld == 1,
int> = 0>
410 using ctype =
typename Dune::PromotionTraits<ctypea, ctypeb>::PromotedType;
411 static constexpr ctype eps_ = 1.0e-7;
412 const ctype eps0 = eps_*std::max(b[1]-b[0], a[1]-a[0]);
413 return b[0] - eps0 <= a[1] && a[0] <= b[1] + eps0;
421template<
int dimworld,
class ctype>
425 for (
int d = 0; d < dimworld; ++d)
429 if (point[d] > b[d+dimworld])
430 squaredDistance += (point[d] - b[d+dimworld])*(point[d] - b[d+dimworld]);
static ctype distance(const Dune::FieldVector< ctype, dimWorld > &a, const Dune::FieldVector< ctype, dimWorld > &b)
Compute the shortest distance between two points.
Definition: distance.hh:294
static ctype squaredDistance(const Dune::FieldVector< ctype, dimWorld > &a, const Dune::FieldVector< ctype, dimWorld > &b)
Compute the shortest squared distance between two points.
Definition: distance.hh:303
Adaption of the non-isothermal two-phase two-component flow model to problems with CO2.
Definition: adapt.hh:29
bool intersectsBoundingBoxBoundingBox(const ctypea *a, const ctypeb *b)
Check whether a bounding box is intersecting another bounding box (dimworld == 3)
Definition: boundingboxtree.hh:373
ctype squaredDistancePointBoundingBox(const Dune::FieldVector< ctype, dimworld > &point, const ctype *b)
Compute squared distance between point and bounding box.
Definition: boundingboxtree.hh:422
bool intersectsPointBoundingBox(const Dune::FieldVector< ctype, dimworld > &point, const ctype *b)
Check whether a point is intersectin a bounding box (dimworld == 3)
Definition: boundingboxtree.hh:306
An axis-aligned bounding box volume tree implementation.
Definition: boundingboxtree.hh:68
BoundingBoxTree()=default
Default Constructor.
const ctype * getBoundingBoxCoordinates(std::size_t nodeIdx) const
Get an existing bounding box for a given node.
Definition: boundingboxtree.hh:147
bool isLeaf(const BoundingBoxNode &node, std::size_t nodeIdx) const
Definition: boundingboxtree.hh:156
const EntitySet & entitySet() const
the entity set this tree was built with
Definition: boundingboxtree.hh:135
GeometricEntitySet EntitySet
the type of entity set this tree was built with
Definition: boundingboxtree.hh:85
void build(std::shared_ptr< const GeometricEntitySet > set)
Build up bounding box tree for a grid with leafGridView.
Definition: boundingboxtree.hh:95
BoundingBoxTree(std::shared_ptr< const GeometricEntitySet > set)
Constructor with gridView.
Definition: boundingboxtree.hh:91
std::size_t numBoundingBoxes() const
Get the number of bounding boxes currently in the tree.
Definition: boundingboxtree.hh:151
const BoundingBoxNode & getBoundingBoxNode(std::size_t nodeIdx) const
Interface to be used by other bounding box trees.
Definition: boundingboxtree.hh:143