12#ifndef DUMUX_GEOMETRY_INTERSECTING_ENTITIES_HH
13#define DUMUX_GEOMETRY_INTERSECTING_ENTITIES_HH
22#include <dune/common/fvector.hh>
38template<
int dimworld,
class CoordTypeA,
class CoordTypeB = CoordTypeA>
42 using ctype =
typename Dune::PromotionTraits<CoordTypeA, CoordTypeB>::PromotedType;
46 template<
class Corners>
50 , corners_(c.begin(), c.end())
62 const std::vector<GlobalPosition>&
corners()
const
69 bool cornersMatch(
const std::vector<GlobalPosition>& otherCorners)
const
71 if (otherCorners.size() != corners_.size())
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());
81 eps2 *= 1.5e-7*1.5e-7;
83 for (
int i = 0; i < corners_.size(); ++i)
85 if (std::none_of(otherCorners.begin(),
87 [&] (
const auto& other) { return (corners_[i] - other).two_norm2() < eps2; }))
95 std::vector<GlobalPosition> corners_;
102template<
class EntitySet,
class ctype,
int dimworld>
103inline std::vector<std::size_t>
106 bool isCartesianGrid =
false)
109 std::vector<std::size_t> entities;
119template<
class EntitySet,
class ctype,
int dimworld>
123 std::vector<std::size_t>& entities,
124 bool isCartesianGrid =
false)
134 else if (tree.
isLeaf(bBox, node))
136 const std::size_t entityIdx = bBox.child1;
137 using Geometry =
typename EntitySet::Entity::Geometry;
141 entities.push_back(entityIdx);
144 const auto& entity = tree.
entitySet().entity(entityIdx);
145 const auto& geometry = entity.geometry();
148 entities.push_back(entityIdx);
171template<
class EntitySet,
class ctype>
175 std::vector<std::size_t>& entities)
178 static constexpr int dimworld = EntitySet::dimensionworld;
184 if (tree.
isLeaf(bBox, node))
185 entities.push_back(bBox.child1);
201template<
class EntitySet,
class ctype>
202inline std::vector<std::size_t>
205 std::vector<std::size_t> entities;
218template<
class EntitySet0,
class EntitySet1>
221 std::size_t nodeA, std::size_t nodeB,
222 std::vector<std::pair<std::size_t, std::size_t>>& pairs)
225 static constexpr int dimworld = EntitySet0::dimensionworld;
232 const bool isLeafA = treeA.
isLeaf(bBoxA, nodeA);
233 const bool isLeafB = treeB.
isLeaf(bBoxB, nodeB);
236 if (isLeafA && isLeafB)
237 pairs.emplace_back(bBoxA.child1, bBoxB.child1);
250 else if (nodeA > nodeB)
267template<
class EntitySet0,
class EntitySet1>
268inline std::vector<std::pair<std::size_t, std::size_t>>
271 static_assert(int(EntitySet0::dimensionworld) == int(EntitySet1::dimensionworld),
272 "Can only intersect bounding box trees of same world dimension");
274 std::vector<std::pair<std::size_t, std::size_t>> pairs;
286template<
class Geometry,
class EntitySet>
287inline std::vector<IntersectionInfo<Geometry::coorddimension, typename Geometry::ctype, typename EntitySet::ctype>>
302template<
class Geometry,
class EntitySet,
class IntersectionPolicy>
303inline std::vector<IntersectionInfo<Geometry::coorddimension, typename Geometry::ctype, typename EntitySet::ctype>>
309 static_assert(int(Geometry::coorddimension) == int(EntitySet::dimensionworld),
310 "Can only intersect geometry and bounding box tree of same world dimension");
313 std::vector<IntersectionInfo<Geometry::coorddimension, typename Geometry::ctype, typename EntitySet::ctype>> intersections;
315 static constexpr int dimworld = Geometry::coorddimension;
318 std::array<ctype, 2*Geometry::coorddimension> bBox;
319 ctype* xMin = bBox.data(); ctype* xMax = xMin + Geometry::coorddimension;
322 auto corner = geometry.corner(0);
323 for (std::size_t dimIdx = 0; dimIdx < dimworld; ++dimIdx)
324 xMin[dimIdx] = xMax[dimIdx] = corner[dimIdx];
327 for (std::size_t cornerIdx = 1; cornerIdx < geometry.corners(); ++cornerIdx)
329 corner = geometry.corner(cornerIdx);
330 for (std::size_t dimIdx = 0; dimIdx < dimworld; ++dimIdx)
334 xMin[dimIdx] = min(xMin[dimIdx], corner[dimIdx]);
335 xMax[dimIdx] = max(xMax[dimIdx], corner[dimIdx]);
343 intersections, intersectionPolicy);
345 return intersections;
352template<
class Geometry,
class EntitySet>
355 const std::array<typename Geometry::ctype, 2*Geometry::coorddimension>& bBox,
358 typename Geometry::ctype,
359 typename EntitySet::ctype>>& intersections)
368template<
class Geometry,
class EntitySet,
class IntersectionPolicy>
371 const std::array<typename Geometry::ctype, 2*Geometry::coorddimension>& bBox,
374 typename Geometry::ctype,
375 typename EntitySet::ctype>>& intersections,
379 static constexpr int dimworld = Geometry::coorddimension;
387 if (tree.
isLeaf(bBoxNode, nodeIdx))
390 const auto eIdxA = 0;
391 const auto eIdxB = bBoxNode.child1;
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;
400 if (IntersectionAlgorithm::intersection(geometry, geometryTree, intersection))
402 static constexpr int dimIntersection = IntersectionPolicy::dimIntersection;
403 if constexpr (dimIntersection >= 2)
406 for (
unsigned int i = 0; i < triangulation.size(); ++i)
407 intersections.emplace_back(eIdxA, eIdxB, std::move(triangulation[i]));
409 else if constexpr (dimIntersection == 0)
410 intersections.emplace_back(eIdxA, eIdxB, std::array{intersection});
412 intersections.emplace_back(eIdxA, eIdxB, intersection);
430template<
class EntitySet0,
class EntitySet1>
431inline std::vector<IntersectionInfo<EntitySet0::dimensionworld, typename EntitySet0::ctype, typename EntitySet1::ctype>>
446template<
class EntitySet0,
class EntitySet1,
class IntersectionPolicy>
447inline std::vector<IntersectionInfo<EntitySet0::dimensionworld, typename EntitySet0::ctype, typename EntitySet1::ctype>>
453 static_assert(int(EntitySet0::dimensionworld) == int(EntitySet1::dimensionworld),
454 "Can only intersect bounding box trees of same world dimension");
457 std::vector<IntersectionInfo<EntitySet0::dimensionworld, typename EntitySet0::ctype, typename EntitySet1::ctype>> intersections;
464 intersections, intersectionPolicy);
466 return intersections;
473template<
class EntitySet0,
class EntitySet1>
476 std::size_t nodeA, std::size_t nodeB,
478 typename EntitySet0::ctype,
479 typename EntitySet1::ctype>>& intersections)
489template<
class EntitySet0,
class EntitySet1,
class IntersectionPolicy>
492 std::size_t nodeA, std::size_t nodeB,
494 typename EntitySet0::ctype,
495 typename EntitySet1::ctype>>& intersections,
503 static constexpr int dimworld = EntitySet0::dimensionworld;
509 const bool isLeafA = treeA.
isLeaf(bBoxA, nodeA);
510 const bool isLeafB = treeB.
isLeaf(bBoxB, nodeB);
513 if (isLeafA && isLeafB)
515 const auto eIdxA = bBoxA.child1;
516 const auto eIdxB = bBoxB.child1;
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();
523 using GeometryA = std::decay_t<
decltype(geometryA)>;
524 using GeometryB = std::decay_t<
decltype(geometryB)>;
526 using Intersection =
typename IntersectionAlgorithm::Intersection;
528 if (Intersection intersection; IntersectionAlgorithm::intersection(geometryA, geometryB, intersection))
530 static constexpr int dimIntersection = IntersectionPolicy::dimIntersection;
534 if constexpr (dimIntersection >= 2)
537 for (
unsigned int i = 0; i < triangulation.size(); ++i)
538 intersections.emplace_back(eIdxA, eIdxB, std::move(triangulation[i]));
540 else if constexpr (dimIntersection == 0)
541 intersections.emplace_back(eIdxA, eIdxB, std::array{intersection});
543 intersections.emplace_back(eIdxA, eIdxB, intersection);
563 else if (nodeA > nodeB)
583template<
class ctype,
int dimworld>
585 const Dune::FieldVector<ctype, dimworld>& min,
586 const Dune::FieldVector<ctype, dimworld>& max,
587 const std::array<
int, std::size_t(dimworld)>& cells)
589 std::size_t index = 0;
590 for (
int i = 0; i < dimworld; ++i)
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);
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
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.