version 3.11-dev
Loading...
Searching...
No Matches
intersectingentities.hh
Go to the documentation of this file.
1// -*- mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
2// vi: set et ts=4 sw=4 sts=4:
3//
4// SPDX-FileCopyrightText: Copyright © DuMux Project contributors, see AUTHORS.md in root folder
5// SPDX-License-Identifier: GPL-3.0-or-later
6//
12#ifndef DUMUX_GEOMETRY_INTERSECTING_ENTITIES_HH
13#define DUMUX_GEOMETRY_INTERSECTING_ENTITIES_HH
14
15#include <array>
16#include <cmath>
17#include <type_traits>
18#include <vector>
19#include <algorithm>
20#include <limits>
21
22#include <dune/common/fvector.hh>
23
24#include <dumux/common/math.hh>
30
31namespace Dumux {
32
38template<int dimworld, class CoordTypeA, class CoordTypeB = CoordTypeA>
40{
41public:
42 using ctype = typename Dune::PromotionTraits<CoordTypeA, CoordTypeB>::PromotedType;
43 static constexpr int dimensionworld = dimworld;
44 using GlobalPosition = Dune::FieldVector<ctype, dimworld>;
45
46 template<class Corners>
47 explicit IntersectionInfo(std::size_t a, std::size_t b, Corners&& c)
48 : a_(a)
49 , b_(b)
50 , corners_(c.begin(), c.end())
51 {}
52
54 std::size_t first() const
55 { return a_; }
56
58 std::size_t second() const
59 { return b_; }
60
62 const std::vector<GlobalPosition>& corners() const
63 { return corners_; }
64
69 bool cornersMatch(const std::vector<GlobalPosition>& otherCorners) const
70 {
71 if (otherCorners.size() != corners_.size())
72 return false;
73
74 using std::max;
75 ctype eps2 = std::numeric_limits<ctype>::min();
76 for (int i = 1; i < corners_.size(); ++i)
77 eps2 = max(eps2, (corners_[i] - corners_[0]).two_norm2());
78
79 // We use a base epsilon of 1.5e-7 for comparisons of lengths.
80 // Since here we compare squared lengths, we multiply by its square.
81 eps2 *= 1.5e-7*1.5e-7;
82
83 for (int i = 0; i < corners_.size(); ++i)
84 // early return if none of the other corners are equal to this corner
85 if (std::none_of(otherCorners.begin(),
86 otherCorners.end(),
87 [&] (const auto& other) { return (corners_[i] - other).two_norm2() < eps2; }))
88 return false;
89
90 return true;
91 }
92
93private:
94 std::size_t a_, b_;
95 std::vector<GlobalPosition> corners_;
96};
97
102template<class EntitySet, class ctype, int dimworld>
103inline std::vector<std::size_t>
104intersectingEntities(const Dune::FieldVector<ctype, dimworld>& point,
105 const BoundingBoxTree<EntitySet>& tree,
106 bool isCartesianGrid = false)
107{
108 // Call the recursive find function to find candidates
109 std::vector<std::size_t> entities;
110 if (tree.numBoundingBoxes() > 0)
111 intersectingEntities(point, tree, tree.numBoundingBoxes() - 1, entities, isCartesianGrid);
112 return entities;
113}
114
119template<class EntitySet, class ctype, int dimworld>
120void intersectingEntities(const Dune::FieldVector<ctype, dimworld>& point,
121 const BoundingBoxTree<EntitySet>& tree,
122 std::size_t node,
123 std::vector<std::size_t>& entities,
124 bool isCartesianGrid = false)
125{
126 // Get the bounding box for the current node
127 const auto& bBox = tree.getBoundingBoxNode(node);
128
129 // if the point is not in the bounding box we can stop
130 if (!intersectsPointBoundingBox(point, tree.getBoundingBoxCoordinates(node))) return;
131
132 // now we know it's inside
133 // if the box is a leaf do the primitive test.
134 else if (tree.isLeaf(bBox, node))
135 {
136 const std::size_t entityIdx = bBox.child1;
137 using Geometry = typename EntitySet::Entity::Geometry;
138 // for structured cube grids or axis-aligned geometries the bounding box
139 // test is already exact, so we can skip the primitive test
140 if (isCartesianGrid || GeometryTraits<Geometry>::axisAligned)
141 entities.push_back(entityIdx);
142 else
143 {
144 const auto& entity = tree.entitySet().entity(entityIdx);
145 const auto& geometry = entity.geometry();
146 // if the primitive is positive it intersects the actual geometry, add the entity to the list
147 if (intersectsPointGeometry(point, geometry))
148 entities.push_back(entityIdx);
149 }
150 }
151
152 // No leaf. Check both children nodes.
153 else
154 {
155 intersectingEntities(point, tree, bBox.child0, entities, isCartesianGrid);
156 intersectingEntities(point, tree, bBox.child1, entities, isCartesianGrid);
157 }
158}
159
171template<class EntitySet, class ctype>
172void intersectingBoxes(const ctype* box,
173 const BoundingBoxTree<EntitySet>& tree,
174 std::size_t node,
175 std::vector<std::size_t>& entities)
176{
177 // if the query box doesn't overlap the node's box we can stop
178 static constexpr int dimworld = EntitySet::dimensionworld;
180 return;
181
182 // if the box is a leaf the bounding box overlap is the result
183 const auto& bBox = tree.getBoundingBoxNode(node);
184 if (tree.isLeaf(bBox, node))
185 entities.push_back(bBox.child1);
186
187 // no leaf, check both children nodes
188 else
189 {
190 intersectingBoxes(box, tree, bBox.child0, entities);
191 intersectingBoxes(box, tree, bBox.child1, entities);
192 }
193}
194
201template<class EntitySet, class ctype>
202inline std::vector<std::size_t>
203intersectingBoxes(const ctype* box, const BoundingBoxTree<EntitySet>& tree)
204{
205 std::vector<std::size_t> entities;
206 if (tree.numBoundingBoxes() > 0)
207 intersectingBoxes(box, tree, tree.numBoundingBoxes() - 1, entities);
208 return entities;
209}
210
218template<class EntitySet0, class EntitySet1>
220 const BoundingBoxTree<EntitySet1>& treeB,
221 std::size_t nodeA, std::size_t nodeB,
222 std::vector<std::pair<std::size_t, std::size_t>>& pairs)
223{
224 // if the two bounding boxes of the current nodes don't overlap we can stop searching
225 static constexpr int dimworld = EntitySet0::dimensionworld;
227 treeB.getBoundingBoxCoordinates(nodeB)))
228 return;
229
230 const auto& bBoxA = treeA.getBoundingBoxNode(nodeA);
231 const auto& bBoxB = treeB.getBoundingBoxNode(nodeB);
232 const bool isLeafA = treeA.isLeaf(bBoxA, nodeA);
233 const bool isLeafB = treeB.isLeaf(bBoxB, nodeB);
234
235 // both leaves: their boxes overlap, record the entity index pair
236 if (isLeafA && isLeafB)
237 pairs.emplace_back(bBoxA.child1, bBoxB.child1);
238
239 // descend into the tree that hasn't reached a leaf yet, larger tree (bigger node number) first
240 else if (isLeafA)
241 {
242 intersectingBoxes(treeA, treeB, nodeA, bBoxB.child0, pairs);
243 intersectingBoxes(treeA, treeB, nodeA, bBoxB.child1, pairs);
244 }
245 else if (isLeafB)
246 {
247 intersectingBoxes(treeA, treeB, bBoxA.child0, nodeB, pairs);
248 intersectingBoxes(treeA, treeB, bBoxA.child1, nodeB, pairs);
249 }
250 else if (nodeA > nodeB)
251 {
252 intersectingBoxes(treeA, treeB, bBoxA.child0, nodeB, pairs);
253 intersectingBoxes(treeA, treeB, bBoxA.child1, nodeB, pairs);
254 }
255 else
256 {
257 intersectingBoxes(treeA, treeB, nodeA, bBoxB.child0, pairs);
258 intersectingBoxes(treeA, treeB, nodeA, bBoxB.child1, pairs);
259 }
260}
261
267template<class EntitySet0, class EntitySet1>
268inline std::vector<std::pair<std::size_t, std::size_t>>
270{
271 static_assert(int(EntitySet0::dimensionworld) == int(EntitySet1::dimensionworld),
272 "Can only intersect bounding box trees of same world dimension");
273
274 std::vector<std::pair<std::size_t, std::size_t>> pairs;
275 if (treeA.numBoundingBoxes() > 0 && treeB.numBoundingBoxes() > 0)
276 intersectingBoxes(treeA, treeB, treeA.numBoundingBoxes() - 1, treeB.numBoundingBoxes() - 1, pairs);
277 return pairs;
278}
279
286template<class Geometry, class EntitySet>
287inline std::vector<IntersectionInfo<Geometry::coorddimension, typename Geometry::ctype, typename EntitySet::ctype>>
288intersectingEntities(const Geometry& geometry,
289 const BoundingBoxTree<EntitySet>& tree)
290{
292 return intersectingEntities(geometry, tree, IP{});
293}
294
302template<class Geometry, class EntitySet, class IntersectionPolicy>
303inline std::vector<IntersectionInfo<Geometry::coorddimension, typename Geometry::ctype, typename EntitySet::ctype>>
304intersectingEntities(const Geometry& geometry,
305 const BoundingBoxTree<EntitySet>& tree,
306 IntersectionPolicy intersectionPolicy)
307{
308 // check if the world dimensions match
309 static_assert(int(Geometry::coorddimension) == int(EntitySet::dimensionworld),
310 "Can only intersect geometry and bounding box tree of same world dimension");
311
312 // Create data structure for return type
313 std::vector<IntersectionInfo<Geometry::coorddimension, typename Geometry::ctype, typename EntitySet::ctype>> intersections;
315 static constexpr int dimworld = Geometry::coorddimension;
316
317 // compute the bounding box of the given geometry
318 std::array<ctype, 2*Geometry::coorddimension> bBox;
319 ctype* xMin = bBox.data(); ctype* xMax = xMin + Geometry::coorddimension;
320
321 // Get coordinates of first vertex
322 auto corner = geometry.corner(0);
323 for (std::size_t dimIdx = 0; dimIdx < dimworld; ++dimIdx)
324 xMin[dimIdx] = xMax[dimIdx] = corner[dimIdx];
325
326 // Compute the min and max over the remaining vertices
327 for (std::size_t cornerIdx = 1; cornerIdx < geometry.corners(); ++cornerIdx)
328 {
329 corner = geometry.corner(cornerIdx);
330 for (std::size_t dimIdx = 0; dimIdx < dimworld; ++dimIdx)
331 {
332 using std::max;
333 using std::min;
334 xMin[dimIdx] = min(xMin[dimIdx], corner[dimIdx]);
335 xMax[dimIdx] = max(xMax[dimIdx], corner[dimIdx]);
336 }
337 }
338
339 // Call the recursive find function to find candidates
340 if (tree.numBoundingBoxes() > 0)
341 intersectingEntities(geometry, tree,
342 bBox, tree.numBoundingBoxes() - 1,
343 intersections, intersectionPolicy);
344
345 return intersections;
346}
347
352template<class Geometry, class EntitySet>
353void intersectingEntities(const Geometry& geometry,
354 const BoundingBoxTree<EntitySet>& tree,
355 const std::array<typename Geometry::ctype, 2*Geometry::coorddimension>& bBox,
356 std::size_t nodeIdx,
357 std::vector<IntersectionInfo<Geometry::coorddimension,
358 typename Geometry::ctype,
359 typename EntitySet::ctype>>& intersections)
360{
362 intersectingEntities(geometry, tree, bBox, nodeIdx, intersections, IP{});
363}
364
368template<class Geometry, class EntitySet, class IntersectionPolicy>
369void intersectingEntities(const Geometry& geometry,
370 const BoundingBoxTree<EntitySet>& tree,
371 const std::array<typename Geometry::ctype, 2*Geometry::coorddimension>& bBox,
372 std::size_t nodeIdx,
373 std::vector<IntersectionInfo<Geometry::coorddimension,
374 typename Geometry::ctype,
375 typename EntitySet::ctype>>& intersections,
376 IntersectionPolicy intersectionPolicy)
377{
378 // if the two bounding boxes don't intersect we can stop searching
379 static constexpr int dimworld = Geometry::coorddimension;
381 return;
382
383 // get node info for current bounding box node
384 const auto& bBoxNode = tree.getBoundingBoxNode(nodeIdx);
385
386 // if the box is a leaf do the primitive test.
387 if (tree.isLeaf(bBoxNode, nodeIdx))
388 {
389 // eIdxA is always 0 since we intersect with exactly one geometry
390 const auto eIdxA = 0;
391 const auto eIdxB = bBoxNode.child1;
392
393 const auto& entityTree = tree.entitySet().entity(eIdxB);
394 const auto& geometryTree = entityTree.geometry();
395 using GeometryTree = std::decay_t<decltype(geometryTree)>;
397 using Intersection = typename IntersectionAlgorithm::Intersection;
398 Intersection intersection;
399
400 if (IntersectionAlgorithm::intersection(geometry, geometryTree, intersection))
401 {
402 static constexpr int dimIntersection = IntersectionPolicy::dimIntersection;
403 if constexpr (dimIntersection >= 2)
404 {
405 const auto triangulation = triangulate<dimIntersection, dimworld>(intersection);
406 for (unsigned int i = 0; i < triangulation.size(); ++i)
407 intersections.emplace_back(eIdxA, eIdxB, std::move(triangulation[i]));
408 }
409 else if constexpr (dimIntersection == 0)
410 intersections.emplace_back(eIdxA, eIdxB, std::array{intersection});
411 else
412 intersections.emplace_back(eIdxA, eIdxB, intersection);
413 }
414 }
415
416 // No leaf. Check both children nodes.
417 else
418 {
419 intersectingEntities(geometry, tree, bBox, bBoxNode.child0, intersections, intersectionPolicy);
420 intersectingEntities(geometry, tree, bBox, bBoxNode.child1, intersections, intersectionPolicy);
421 }
422}
423
430template<class EntitySet0, class EntitySet1>
431inline std::vector<IntersectionInfo<EntitySet0::dimensionworld, typename EntitySet0::ctype, typename EntitySet1::ctype>>
438
446template<class EntitySet0, class EntitySet1, class IntersectionPolicy>
447inline std::vector<IntersectionInfo<EntitySet0::dimensionworld, typename EntitySet0::ctype, typename EntitySet1::ctype>>
449 const BoundingBoxTree<EntitySet1>& treeB,
450 IntersectionPolicy intersectionPolicy)
451{
452 // check if the world dimensions match
453 static_assert(int(EntitySet0::dimensionworld) == int(EntitySet1::dimensionworld),
454 "Can only intersect bounding box trees of same world dimension");
455
456 // Create data structure for return type
457 std::vector<IntersectionInfo<EntitySet0::dimensionworld, typename EntitySet0::ctype, typename EntitySet1::ctype>> intersections;
458
459 // Call the recursive find function to find candidates
460 if (treeA.numBoundingBoxes() > 0 && treeB.numBoundingBoxes() > 0)
461 intersectingEntities(treeA, treeB,
462 treeA.numBoundingBoxes() - 1,
463 treeB.numBoundingBoxes() - 1,
464 intersections, intersectionPolicy);
465
466 return intersections;
467}
468
473template<class EntitySet0, class EntitySet1>
475 const BoundingBoxTree<EntitySet1>& treeB,
476 std::size_t nodeA, std::size_t nodeB,
477 std::vector<IntersectionInfo<EntitySet0::dimensionworld,
478 typename EntitySet0::ctype,
479 typename EntitySet1::ctype>>& intersections)
480{
482 intersectingEntities(treeA, treeB, nodeA, nodeB, intersections, IP{});
483}
484
489template<class EntitySet0, class EntitySet1, class IntersectionPolicy>
491 const BoundingBoxTree<EntitySet1>& treeB,
492 std::size_t nodeA, std::size_t nodeB,
493 std::vector<IntersectionInfo<EntitySet0::dimensionworld,
494 typename EntitySet0::ctype,
495 typename EntitySet1::ctype>>& intersections,
496 IntersectionPolicy intersectionPolicy)
497{
498 // Get the bounding box for the current node
499 const auto& bBoxA = treeA.getBoundingBoxNode(nodeA);
500 const auto& bBoxB = treeB.getBoundingBoxNode(nodeB);
501
502 // if the two bounding boxes of the current nodes don't intersect we can stop searching
503 static constexpr int dimworld = EntitySet0::dimensionworld;
505 treeB.getBoundingBoxCoordinates(nodeB)))
506 return;
507
508 // Check if we have a leaf in treeA or treeB
509 const bool isLeafA = treeA.isLeaf(bBoxA, nodeA);
510 const bool isLeafB = treeB.isLeaf(bBoxB, nodeB);
511
512 // If both boxes are leaves do the primitive test
513 if (isLeafA && isLeafB)
514 {
515 const auto eIdxA = bBoxA.child1;
516 const auto eIdxB = bBoxB.child1;
517
518 const auto& entityA = treeA.entitySet().entity(eIdxA);
519 const auto& entityB = treeB.entitySet().entity(eIdxB);
520 const auto& geometryA = entityA.geometry();
521 const auto& geometryB = entityB.geometry();
522
523 using GeometryA = std::decay_t<decltype(geometryA)>;
524 using GeometryB = std::decay_t<decltype(geometryB)>;
526 using Intersection = typename IntersectionAlgorithm::Intersection;
527
528 if (Intersection intersection; IntersectionAlgorithm::intersection(geometryA, geometryB, intersection))
529 {
530 static constexpr int dimIntersection = IntersectionPolicy::dimIntersection;
531
532 // intersection is returned as a point cloud for dim >= 2
533 // so we have to triangulate first
534 if constexpr (dimIntersection >= 2)
535 {
536 const auto triangulation = triangulate<dimIntersection, dimworld>(intersection);
537 for (unsigned int i = 0; i < triangulation.size(); ++i)
538 intersections.emplace_back(eIdxA, eIdxB, std::move(triangulation[i]));
539 }
540 else if constexpr (dimIntersection == 0)
541 intersections.emplace_back(eIdxA, eIdxB, std::array{intersection});
542 else
543 intersections.emplace_back(eIdxA, eIdxB, intersection);
544 }
545 }
546
547 // if we reached the leaf in treeA, just continue in treeB
548 else if (isLeafA)
549 {
550 intersectingEntities(treeA, treeB, nodeA, bBoxB.child0, intersections, intersectionPolicy);
551 intersectingEntities(treeA, treeB, nodeA, bBoxB.child1, intersections, intersectionPolicy);
552 }
553
554 // if we reached the leaf in treeB, just continue in treeA
555 else if (isLeafB)
556 {
557 intersectingEntities(treeA, treeB, bBoxA.child0, nodeB, intersections, intersectionPolicy);
558 intersectingEntities(treeA, treeB, bBoxA.child1, nodeB, intersections, intersectionPolicy);
559 }
560
561 // we know now that both trees didn't reach the leaf yet so
562 // we continue with the larger tree first (bigger node number)
563 else if (nodeA > nodeB)
564 {
565 intersectingEntities(treeA, treeB, bBoxA.child0, nodeB, intersections, intersectionPolicy);
566 intersectingEntities(treeA, treeB, bBoxA.child1, nodeB, intersections, intersectionPolicy);
567 }
568 else
569 {
570 intersectingEntities(treeA, treeB, nodeA, bBoxB.child0, intersections, intersectionPolicy);
571 intersectingEntities(treeA, treeB, nodeA, bBoxB.child1, intersections, intersectionPolicy);
572 }
573}
574
583template<class ctype, int dimworld>
584inline std::size_t intersectingEntityCartesianGrid(const Dune::FieldVector<ctype, dimworld>& point,
585 const Dune::FieldVector<ctype, dimworld>& min,
586 const Dune::FieldVector<ctype, dimworld>& max,
587 const std::array<int, std::size_t(dimworld)>& cells)
588{
589 std::size_t index = 0;
590 for (int i = 0; i < dimworld; ++i)
591 {
592 using std::clamp; using std::floor;
593 ctype dimOffset = clamp<ctype>(floor((point[i]-min[i])*cells[i]/(max[i]-min[i])), 0.0, cells[i]-1);
594 for (int j = 0; j < i; ++j)
595 dimOffset *= cells[j];
596 index += static_cast<std::size_t>(dimOffset);
597 }
598 return index;
599}
600
601} // end namespace Dumux
602
603#endif
An axis-aligned bounding box volume hierarchy for dune grids.
An axis-aligned bounding box volume tree implementation.
Definition boundingboxtree.hh:105
const ctype * getBoundingBoxCoordinates(std::size_t nodeIdx) const
Get the coordinates of the bounding box of a given node (minimum then maximum coordinates).
Definition boundingboxtree.hh:178
bool isLeaf(const BoundingBoxNode &node, std::size_t nodeIdx) const
Definition boundingboxtree.hh:187
const EntitySet & entitySet() const
the entity set this tree was built with
Definition boundingboxtree.hh:166
std::size_t numBoundingBoxes() const
Get the number of bounding boxes currently in the tree.
Definition boundingboxtree.hh:182
const BoundingBoxNode & getBoundingBoxNode(std::size_t nodeIdx) const
Interface to be used by other bounding box trees.
Definition boundingboxtree.hh:174
A class for geometry collision detection and intersection calculation.
Definition geometryintersection.hh:240
An intersection object resulting from the intersection of two primitives in an entity set.
Definition intersectingentities.hh:40
static constexpr int dimensionworld
Definition intersectingentities.hh:43
bool cornersMatch(const std::vector< GlobalPosition > &otherCorners) const
Check if the corners of this intersection match with the given corners.
Definition intersectingentities.hh:69
const std::vector< GlobalPosition > & corners() const
Get the corners of the intersection geometry.
Definition intersectingentities.hh:62
IntersectionInfo(std::size_t a, std::size_t b, Corners &&c)
Definition intersectingentities.hh:47
Dune::FieldVector< ctype, dimworld > GlobalPosition
Definition intersectingentities.hh:44
std::size_t second() const
Get the index of the intersecting entity belonging to the other grid.
Definition intersectingentities.hh:58
std::size_t first() const
Get the index of the intersecting entity belonging to this grid.
Definition intersectingentities.hh:54
typename Dune::PromotionTraits< CoordTypeA, CoordTypeB >::PromotedType ctype
Definition intersectingentities.hh:42
A class for collision detection of two geometries and computation of intersection corners.
Traits telling the bounding box tree algorithms about a leaf geometry.
std::size_t intersectingEntityCartesianGrid(const Dune::FieldVector< ctype, dimworld > &point, const Dune::FieldVector< ctype, dimworld > &min, const Dune::FieldVector< ctype, dimworld > &max, const std::array< int, std::size_t(dimworld)> &cells)
Compute the index of the intersecting element of a Cartesian grid with a point The grid is given by t...
Definition intersectingentities.hh:584
Triangulation< dim, dimWorld, typename RandomAccessContainer::value_type::value_type > triangulate(const RandomAccessContainer &points)
Triangulate area given points of a convex hull (1d).
Definition triangulation.hh:82
bool intersectsPointGeometry(const Dune::FieldVector< ctype, dimworld > &point, const Geometry &g)
Find out whether a point is inside a three-dimensional geometry.
Definition intersectspointgeometry.hh:28
void intersectingBoxes(const ctype *box, const BoundingBoxTree< EntitySet > &tree, std::size_t node, std::vector< std::size_t > &entities)
Compute all entities whose bounding box overlaps the given box.
Definition intersectingentities.hh:172
std::vector< std::pair< int, std::size_t > > intersectingEntities(const Dune::FieldVector< ctype, dimworld > &point, const DistributedBoundingBoxTree< EntitySet > &tree, bool isCartesianGrid=false, bool onlyOwned=true)
Compute all intersections between entities and a point on a distributed tree.
Definition distributedintersectingentities.hh:81
Detect if a point intersects a geometry.
Define some often used mathematical functions.
Definition geometryintersection.hh:32
typename DefaultPolicyChooser< Geometry1, Geometry2 >::type DefaultPolicy
Helper alias to define the default intersection policy.
Definition geometryintersection.hh:107
Definition adapt.hh:17
bool intersectsBoundingBoxBoundingBox(const ctypea *a, const ctypeb *b)
Check whether a bounding box is intersecting another bounding box (dimworld == 3).
Definition boundingboxtree.hh:374
bool intersectsPointBoundingBox(const Dune::FieldVector< ctype, dimworld > &point, const ctype *b)
Check whether a point is intersecting a bounding box (dimworld == 3).
Definition boundingboxtree.hh:307
static constexpr bool axisAligned
Definition geometrytraits.hh:31
Functionality to triangulate point clouds.