29#ifndef DUMUX_GEOMETRY_BOUNDINGBOXTREE_HH
30#define DUMUX_GEOMETRY_BOUNDINGBOXTREE_HH
40#include <dune/common/promotiontraits.hh>
41#include <dune/common/timer.hh>
42#include <dune/common/fvector.hh>
64template <
class GeometricEntitySet>
67 enum { dimworld = GeometricEntitySet::dimensionworld };
68 using ctype =
typename GeometricEntitySet::ctype;
75 struct BoundingBoxNode
93 void build(std::shared_ptr<const GeometricEntitySet> set)
99 boundingBoxNodes_.clear();
100 boundingBoxCoordinates_.clear();
106 const auto numLeaves = set->size();
109 const auto numNodes = 2*numLeaves - 1;
110 boundingBoxNodes_.reserve(numNodes);
111 boundingBoxCoordinates_.reserve(numNodes*2*dimworld);
114 std::vector<ctype> leafBoxes(2*dimworld*numLeaves);
116 for (
const auto& geometricEntity : *set)
117 computeEntityBoundingBox_(leafBoxes.data() + 2*dimworld*set->index(geometricEntity), geometricEntity);
120 std::vector<std::size_t> leafPartition(numLeaves);
121 std::iota(leafPartition.begin(), leafPartition.end(), 0);
124 build_(leafBoxes, leafPartition.begin(), leafPartition.end());
128 <<
" nodes for " << numLeaves <<
" grid entites in "
129 << timer.stop() <<
" seconds." << std::endl;
134 {
return *entitySet_; }
142 {
return boundingBoxNodes_[nodeIdx]; }
146 {
return boundingBoxCoordinates_.data() + 2*dimworld*nodeIdx; }
150 {
return boundingBoxNodes_.size(); }
154 bool isLeaf(
const BoundingBoxNode& node, std::size_t nodeIdx)
const
155 {
return node.child0 == nodeIdx; }
160 std::vector<BoundingBoxNode> boundingBoxNodes_;
163 std::vector<ctype> boundingBoxCoordinates_;
166 std::shared_ptr<const EntitySet> entitySet_;
169 template <
class Entity>
170 void computeEntityBoundingBox_(ctype* b,
const Entity& entity)
const
174 ctype* xMax = b + dimworld;
177 auto geometry = entity.geometry();
180 auto corner = geometry.corner(0);
181 for (std::size_t dimIdx = 0; dimIdx < dimworld; ++dimIdx)
182 xMin[dimIdx] = xMax[dimIdx] = corner[dimIdx];
185 for (std::size_t cornerIdx = 1; cornerIdx < geometry.corners(); ++cornerIdx)
187 corner = geometry.corner(cornerIdx);
188 for (std::size_t dimIdx = 0; dimIdx < dimworld; ++dimIdx)
192 xMin[dimIdx] = min(xMin[dimIdx], corner[dimIdx]);
193 xMax[dimIdx] = max(xMax[dimIdx], corner[dimIdx]);
199 std::size_t build_(
const std::vector<ctype>& leafBoxes,
200 const std::vector<std::size_t>::iterator& begin,
201 const std::vector<std::size_t>::iterator& end)
206 if (end - begin == 1)
209 const std::size_t leafNodeIdx = *begin;
210 const auto beginCoords = leafBoxes.begin() + 2*dimworld*leafNodeIdx;
211 const auto endCoords = beginCoords + 2*dimworld;
216 return addBoundingBox_(BoundingBoxNode{
numBoundingBoxes(), leafNodeIdx}, beginCoords, endCoords);
220 const auto bCoords = computeBBoxOfBBoxes_(leafBoxes, begin, end);
223 const auto axis = computeLongestAxis_(bCoords);
227 auto middle = begin + (end - begin)/2;
228 std::nth_element(begin, middle, end, [&leafBoxes, axis](std::size_t i, std::size_t j)
230 const ctype* bi = leafBoxes.data() + 2*dimworld*i;
231 const ctype* bj = leafBoxes.data() + 2*dimworld*j;
232 return bi[axis] + bi[axis + dimworld] < bj[axis] + bj[axis + dimworld];
237 return addBoundingBox_(BoundingBoxNode{build_(leafBoxes, begin, middle), build_(leafBoxes, middle, end)},
238 bCoords.begin(), bCoords.end());
242 template <
class Iterator>
243 std::size_t addBoundingBox_(BoundingBoxNode&& node,
244 const Iterator& coordBegin,
245 const Iterator& coordEnd)
248 boundingBoxNodes_.emplace_back(node);
251 boundingBoxCoordinates_.insert(boundingBoxCoordinates_.end(), coordBegin, coordEnd);
254 return boundingBoxNodes_.size() - 1;
258 std::array<ctype, 2*dimworld>
259 computeBBoxOfBBoxes_(
const std::vector<ctype>& leafBoxes,
260 const std::vector<std::size_t>::iterator& begin,
261 const std::vector<std::size_t>::iterator& end)
263 std::array<ctype, 2*dimworld> bBoxCoords;
267 const auto* bFirst = leafBoxes.data() + 2*dimworld*(*it);
269 for (
int coordIdx = 0; coordIdx < 2*dimworld; ++coordIdx)
270 bBoxCoords[coordIdx] = bFirst[coordIdx];
273 for (; it != end; ++it)
275 const auto* b = leafBoxes.data() + 2*dimworld*(*it);
276 for (
int coordIdx = 0; coordIdx < dimworld; ++coordIdx)
277 if (b[coordIdx] < bBoxCoords[coordIdx])
278 bBoxCoords[coordIdx] = b[coordIdx];
279 for (
int coordIdx = dimworld; coordIdx < 2*dimworld; ++coordIdx)
280 if (b[coordIdx] > bBoxCoords[coordIdx])
281 bBoxCoords[coordIdx] = b[coordIdx];
288 std::size_t computeLongestAxis_(
const std::array<ctype, 2*dimworld>& bCoords)
290 std::array<ctype, dimworld> axisLength;
291 for (
int coordIdx = 0; coordIdx < dimworld; ++coordIdx)
292 axisLength[coordIdx] = bCoords[dimworld + coordIdx] - bCoords[coordIdx];
294 return std::distance(axisLength.begin(), std::max_element(axisLength.begin(), axisLength.end()));
303template<
class ctype,
int dimworld,
typename std::enable_if_t<dimworld == 3,
int> = 0>
306 static constexpr ctype eps_ = 1.0e-7;
309 const auto dx = b[3] - b[0];
310 const auto dy = b[4] - b[1];
311 const auto dz = b[5] - b[2];
312 const ctype eps = max({dx, dy, dz})*eps_;
313 return (b[0] - eps <= point[0] && point[0] <= b[3] + eps &&
314 b[1] - eps <= point[1] && point[1] <= b[4] + eps &&
315 b[2] - eps <= point[2] && point[2] <= b[5] + eps);
323template<
class ctype,
int dimworld,
typename std::enable_if_t<dimworld == 2,
int> = 0>
326 static constexpr ctype eps_ = 1.0e-7;
329 const auto dx = b[2] - b[0];
330 const auto dy = b[3] - b[1];
331 const ctype eps = max(dx, dy)*eps_;
332 return (b[0] - eps <= point[0] && point[0] <= b[2] + eps &&
333 b[1] - eps <= point[1] && point[1] <= b[3] + eps);
341template<
class ctype,
int dimworld,
typename std::enable_if_t<dimworld == 1,
int> = 0>
344 static constexpr ctype eps_ = 1.0e-7;
345 const ctype eps0 = eps_*(b[1] - b[0]);
346 return b[0] - eps0 <= point[0] && point[0] <= b[1] + eps0;
354template<
class ctype,
int dimworld>
356 const Dune::FieldVector<ctype, dimworld>& min,
357 const Dune::FieldVector<ctype, dimworld>& max)
359 std::array<ctype, 2*dimworld> bBox;
360 std::copy(min.begin(), min.end(), bBox.begin());
361 std::copy(max.begin(), max.end(), bBox.begin()+dimworld);
370template<
int dimworld,
class ctypea,
class ctypeb,
typename std::enable_if_t<dimworld == 3,
int> = 0>
373 using ctype =
typename Dune::PromotionTraits<ctypea, ctypeb>::PromotedType;
374 static constexpr ctype eps_ = 1.0e-7;
375 const ctype eps0 = eps_*std::max(b[3]-b[0], a[3]-a[0]);
376 const ctype eps1 = eps_*std::max(b[4]-b[1], a[4]-a[1]);
377 const ctype eps2 = eps_*std::max(b[5]-b[2], a[5]-a[2]);
378 return (b[0] - eps0 <= a[3] && a[0] <= b[3] + eps0 &&
379 b[1] - eps1 <= a[4] && a[1] <= b[4] + eps1 &&
380 b[2] - eps2 <= a[5] && a[2] <= b[5] + eps2);
389template<
int dimworld,
class ctypea,
class ctypeb,
typename std::enable_if_t<dimworld == 2,
int> = 0>
392 using ctype =
typename Dune::PromotionTraits<ctypea, ctypeb>::PromotedType;
393 static constexpr ctype eps_ = 1.0e-7;
394 const ctype eps0 = eps_*std::max(b[2]-b[0], a[2]-a[0]);
395 const ctype eps1 = eps_*std::max(b[3]-b[1], a[3]-a[1]);
396 return (b[0] - eps0 <= a[2] && a[0] <= b[2] + eps0 &&
397 b[1] - eps1 <= a[3] && a[1] <= b[3] + eps1);
405template<
int dimworld,
class ctypea,
class ctypeb,
typename std::enable_if_t<dimworld == 1,
int> = 0>
408 using ctype =
typename Dune::PromotionTraits<ctypea, ctypeb>::PromotedType;
409 static constexpr ctype eps_ = 1.0e-7;
410 const ctype eps0 = eps_*std::max(b[1]-b[0], a[1]-a[0]);
411 return b[0] - eps0 <= a[1] && a[0] <= b[1] + eps0;
ctype distance(const Dune::FieldVector< ctype, dimWorld > &a, const Dune::FieldVector< ctype, dimWorld > &b)
Compute the shortest distance between two points.
Definition: geometry/distance.hh:138
bool intersectsBoundingBoxBoundingBox(const ctypea *a, const ctypeb *b)
Check whether a bounding box is intersecting another bounding box (dimworld == 3)
Definition: geometry/boundingboxtree.hh:371
bool intersectsPointBoundingBox(const Dune::FieldVector< ctype, dimworld > &point, const ctype *b)
Check whether a point is intersectin a bounding box (dimworld == 3)
Definition: geometry/boundingboxtree.hh:304
An axis-aligned bounding box volume tree implementation.
Definition: geometry/boundingboxtree.hh:66
BoundingBoxTree()=default
Default Constructor.
const ctype * getBoundingBoxCoordinates(std::size_t nodeIdx) const
Get an existing bounding box for a given node.
Definition: geometry/boundingboxtree.hh:145
bool isLeaf(const BoundingBoxNode &node, std::size_t nodeIdx) const
Definition: geometry/boundingboxtree.hh:154
const EntitySet & entitySet() const
the entity set this tree was built with
Definition: geometry/boundingboxtree.hh:133
GeometricEntitySet EntitySet
the type of entity set this tree was built with
Definition: geometry/boundingboxtree.hh:83
void build(std::shared_ptr< const GeometricEntitySet > set)
Build up bounding box tree for a grid with leafGridView.
Definition: geometry/boundingboxtree.hh:93
BoundingBoxTree(std::shared_ptr< const GeometricEntitySet > set)
Constructor with gridView.
Definition: geometry/boundingboxtree.hh:89
std::size_t numBoundingBoxes() const
Get the number of bounding boxes currently in the tree.
Definition: geometry/boundingboxtree.hh:149
const BoundingBoxNode & getBoundingBoxNode(std::size_t nodeIdx) const
Interface to be used by other bounding box trees.
Definition: geometry/boundingboxtree.hh:141