13#ifndef DUMUX_GEOMETRY_INTERSECTION_HH
14#define DUMUX_GEOMETRY_INTERSECTION_HH
20#include <dune/common/exceptions.hh>
21#include <dune/common/promotiontraits.hh>
22#include <dune/common/reservedvector.hh>
23#include <dune/geometry/multilineargeometry.hh>
24#include <dune/geometry/referenceelements.hh>
35template<
class ct,
int dw>
39 using Point = Dune::FieldVector<ctype, dw>;
47template<
class ct,
int dw>
51 using Point = Dune::FieldVector<ctype, dw>;
60template<
class ct,
int dw>
64 using Point = Dune::FieldVector<ctype, dw>;
73template<
class ct,
int dw>
77 using Point = Dune::FieldVector<ctype, dw>;
86template<
class Geometry1,
class Geometry2>
89 static constexpr int dimworld = Geometry1::coorddimension;
90 static constexpr int isDim = std::min(
int(Geometry1::mydimension),
int(Geometry2::mydimension) );
91 static_assert(dimworld == int(Geometry2::coorddimension),
92 "Geometries must have the same coordinate dimension!");
93 static_assert(int(Geometry1::mydimension) <= 3 && int(Geometry2::mydimension) <= 3,
94 "Geometries must have dimension 3 or less.");
95 using ctype =
typename Dune::PromotionTraits<typename Geometry1::ctype, typename Geometry2::ctype>::PromotedType;
97 using DefaultPolicies = std::tuple<PointPolicy<ctype, dimworld>,
102 using type = std::tuple_element_t<isDim, DefaultPolicies>;
106template<
class Geometry1,
class Geometry2>
126template<
class Geo1,
class Geo2,
class ctype,
127 class GetFacetCornerIndices,
class ComputeNormalFunction >
129 ctype& tfirst, ctype& tlast,
130 const GetFacetCornerIndices& getFacetCornerIndices,
131 const ComputeNormalFunction& computeNormal)
133 static_assert(int(Geo2::mydimension) == 1,
"Geometry2 must be a segment");
134 static_assert(int(Geo1::mydimension) > int(Geo2::mydimension),
135 "Dimension of Geometry1 must be higher than that of Geometry2");
137 const auto a = geo2.corner(0);
138 const auto b = geo2.corner(1);
139 const auto d = b - a;
147 const auto facets = getFacetCornerIndices(geo1);
148 for (
const auto& f : facets)
150 const auto n = computeNormal(f);
152 const auto c0 = geo1.corner(f[0]);
153 const ctype denom = n*d;
154 const ctype dist = n*(a-c0);
157 const auto edge1 = geo1.corner(f[1]) - geo1.corner(f[0]);
158 const ctype eps = baseEps*edge1.two_norm();
163 if (abs(denom) < eps)
170 const ctype t = -dist / denom;
186 if (tfirst > tlast - baseEps)
203 template<
int mydim,
int cdim>
209 static const bool v =
true;
210 static const unsigned int topologyId = Dune::GeometryTypes::line.id();
218 template<
int mydim,
int cdim>
234<
class Geometry1,
class Geometry2,
236 int dimworld = Geometry1::coorddimension,
237 int dim1 = Geometry1::mydimension,
238 int dim2 = Geometry2::mydimension>
241 static constexpr int dimWorld = Policy::dimWorld;
244 using ctype =
typename Policy::ctype;
245 using Point =
typename Policy::Point;
251 static_assert(dimworld == Geometry2::coorddimension,
"Can only intersect geometries of same coordinate dimension");
252 DUNE_THROW(Dune::NotImplemented,
"Geometry intersection detection with intersection computation not implemented for dimworld = "
253 << dimworld <<
", dim1 = " << dim1 <<
", dim2 = " << dim2);
261template <
class Geometry1,
class Geometry2,
class Policy>
264 enum { dimworld = 2 };
269 static constexpr typename Policy::ctype eps_ = 1.5e-7;
272 using ctype =
typename Policy::ctype;
273 using Point =
typename Policy::Point;
282 template<
class P = Policy, std::enable_if_t<P::dimIntersection == 0,
int> = 0>
285 static_assert(int(dimworld) == int(Geometry2::coorddimension),
"Can only collide geometries of same coordinate dimension");
287 const auto v1 = geo1.corner(1) - geo1.corner(0);
288 const auto v2 = geo2.corner(1) - geo2.corner(0);
289 const auto ac = geo2.corner(0) - geo1.corner(0);
291 auto n2 =
Point({-1.0*v2[1], v2[0]});
295 const auto dist12 = n2*ac;
301 const auto v1Norm2 = v1.two_norm2();
302 const auto eps = eps_*sqrt(v1Norm2);
303 const auto eps2 = eps_*v1Norm2;
305 const auto sp = n2*v1;
308 if (abs(dist12) > eps)
314 if ( ac.two_norm2() < eps2 )
317 if ( (geo2.corner(1) - geo1.corner(1)).two_norm2() < eps2 )
322 if ( (geo2.corner(1) - geo1.corner(0)).two_norm2() < eps2 )
325 if ( (geo2.corner(0) - geo1.corner(1)).two_norm2() < eps2 )
334 const auto t1 = dist12 / sp;
337 if (t1 < -1.0*eps_ || t1 > 1.0 + eps_)
341 auto isPoint = geo1.global(t1);
344 const auto c = geo2.corner(0);
345 const auto d = geo2.corner(1);
347 using std::min;
using std::max;
348 std::array<ctype, 4> bBox({ min(c[0], d[0]), min(c[1], d[1]), max(c[0], d[0]), max(c[1], d[1]) });
365 template<
class P = Policy, std::enable_if_t<P::dimIntersection == 1,
int> = 0>
368 static_assert(int(dimworld) == int(Geometry2::coorddimension),
"Can only collide geometries of same coordinate dimension");
370 const auto& a = geo1.corner(0);
371 const auto& b = geo1.corner(1);
372 const auto ab = b - a;
374 const auto& p = geo2.corner(0);
375 const auto& q = geo2.corner(1);
376 const auto pq = q - p;
377 Dune::FieldVector<ctype, dimworld> n2{-pq[1], pq[0]};
380 const auto abNorm2 = ab.two_norm2();
381 const auto pqNorm2 = pq.two_norm2();
382 const auto eps2 = eps_*max(abNorm2, pqNorm2);
386 if (abs(n2*ab) > eps2)
390 const auto ap = p - a;
391 if (abs(ap*n2) > eps2)
396 auto t2 = ab*(q - a);
403 t1 = clamp(t1, 0.0, abNorm2);
404 t2 = clamp(t2, 0.0, abNorm2);
406 if (abs(t2-t1) < eps2)
418template <
class Geometry1,
class Geometry2,
class Policy>
421 enum { dimworld = 2 };
426 using ctype =
typename Policy::ctype;
427 using Point =
typename Policy::Point;
431 static constexpr ctype eps_ = 1.5e-7;
441 template<
class P = Policy, std::enable_if_t<P::dimIntersection == 1,
int> = 0>
444 static_assert(int(dimworld) == int(Geometry2::coorddimension),
"Can only collide geometries of same coordinate dimension");
447 if (intersect_(geo1, geo2, tfirst, tlast))
465 template<
class P = Policy, std::enable_if_t<P::dimIntersection == 0,
int> = 0>
468 static_assert(int(dimworld) == int(Geometry2::coorddimension),
"Can only collide geometries of same coordinate dimension");
474 if (!intersect_(geo1, geo2, tfirst, tlast) && abs(tfirst - tlast) < eps_)
476 const auto point = geo2.global(tfirst);
491 static bool intersect_(
const Geometry1& geo1,
const Geometry2& geo2,
ctype& tfirst,
ctype& tlast)
494 auto getFacetCorners = [] (
const Geometry1& geo1)
496 Dune::ReservedVector<std::array<int, 2>, 4> facetCorners;
497 switch (geo1.corners())
500 facetCorners = {{0, 2}, {3, 1}, {1, 0}, {2, 3}};
503 facetCorners = {{1, 0}, {0, 2}, {2, 1}};
506 DUNE_THROW(Dune::NotImplemented,
"Collision of segment and geometry of type "
507 << geo1.type() <<
" with "<< geo1.corners() <<
" corners.");
514 const auto center1 = geo1.center();
515 auto computeNormal = [&geo1, ¢er1] (
const std::array<int, 2>& facetCorners)
517 const auto c0 = geo1.corner(facetCorners[0]);
518 const auto c1 = geo1.corner(facetCorners[1]);
519 const auto edge = c1 - c0;
521 Dune::FieldVector<ctype, dimworld> n({-1.0*edge[1], edge[0]});
526 if ( n*(center1-c0) > 0.0 )
540template <
class Geometry1,
class Geometry2,
class Policy>
554 template<
class P = Policy>
565template <
class Geometry1,
class Geometry2,
class Policy>
568 enum { dimworld = 2 };
573 using ctype =
typename Policy::ctype;
574 using Point =
typename Policy::Point;
578 static constexpr ctype eps_ = 1.5e-7;
592 template<
class P = Policy, std::enable_if_t<P::dimIntersection == 2,
int> = 0>
595 static_assert(int(dimworld) == int(Geometry2::coorddimension),
"Can only collide geometries of same coordinate dimension");
598 std::vector<Point> points; points.reserve(6);
601 for (
int i = 0; i < geo1.corners(); ++i)
603 points.emplace_back(geo1.corner(i));
605 const auto numPoints1 = points.size();
606 if (numPoints1 != geo1.corners())
609 for (
int i = 0; i < geo2.corners(); ++i)
611 points.emplace_back(geo2.corner(i));
613 if (points.size() - numPoints1 != geo2.corners())
615 const auto refElement1 = referenceElement(geo1);
616 const auto refElement2 = referenceElement(geo2);
619 using SegGeometry = Dune::MultiLinearGeometry<ctype, 1, dimworld, Detail::SegmentGeometryTraits<ctype>>;
621 for (
int i = 0; i < refElement1.size(dim1-1); ++i)
623 const auto localEdgeGeom1 = refElement1.template geometry<dim1-1>(i);
624 const auto edge1 = SegGeometry( Dune::GeometryTypes::line,
625 std::array<Point, 2>{ {geo1.global(localEdgeGeom1.corner(0)),
626 geo1.global(localEdgeGeom1.corner(1))} });
628 for (
int j = 0; j < refElement2.size(dim2-1); ++j)
630 const auto localEdgeGeom2 = refElement2.template geometry<dim2-1>(j);
631 const auto edge2 = SegGeometry( Dune::GeometryTypes::line,
632 std::array<Point, 2>{ {geo2.global(localEdgeGeom2.corner(0)),
633 geo2.global(localEdgeGeom2.corner(1))} });
636 typename EdgeTest::Intersection edgeIntersection;
637 if (EdgeTest::intersection(edge1, edge2, edgeIntersection))
638 points.emplace_back(edgeIntersection);
648 const auto eps = (geo1.corner(0) - geo1.corner(1)).two_norm()*eps_;
649 std::sort(points.begin(), points.end(), [&eps](
const auto& a,
const auto& b) ->
bool
652 return (abs(a[0]-b[0]) > eps ? a[0] < b[0] : a[1] < b[1]);
655 auto removeIt = std::unique(points.begin(), points.end(), [&eps](
const auto& a,
const auto&b)
657 return (b-a).two_norm() < eps;
660 points.erase(removeIt, points.end());
663 if (points.size() < 3)
679 template<
class P = Policy, std::enable_if_t<P::dimIntersection == 1,
int> = 0>
682 static_assert(int(dimworld) == int(Geometry2::coorddimension),
"Can only collide geometries of same coordinate dimension");
683 DUNE_THROW(Dune::NotImplemented,
"Polygon-polygon intersection detection for segment-like intersections");
693 template<
class P = Policy, std::enable_if_t<P::dimIntersection == 0,
int> = 0>
696 static_assert(int(dimworld) == int(Geometry2::coorddimension),
"Can only collide geometries of same coordinate dimension");
697 DUNE_THROW(Dune::NotImplemented,
"Polygon-polygon intersection detection for touching points");
705template <
class Geometry1,
class Geometry2,
class Policy>
708 enum { dimworld = 3 };
713 using ctype =
typename Policy::ctype;
714 using Point =
typename Policy::Point;
718 static constexpr ctype eps_ = 1.5e-7;
732 template<
class P = Policy, std::enable_if_t<P::dimIntersection == 1,
int> = 0>
735 static_assert(int(dimworld) == int(Geometry2::coorddimension),
"Can only collide geometries of same coordinate dimension");
738 if (intersect_(geo1, geo2, tfirst, tlast))
760 template<
class P = Policy, std::enable_if_t<P::dimIntersection == 0,
int> = 0>
763 static_assert(int(dimworld) == int(Geometry2::coorddimension),
"Can only collide geometries of same coordinate dimension");
769 if (!intersect_(geo1, geo2, tfirst, tlast) && abs(tfirst - tlast) < eps_)
771 const auto point = geo2.global(tfirst);
786 static bool intersect_(
const Geometry1& geo1,
const Geometry2& geo2,
ctype& tfirst,
ctype& tlast)
789 auto getFacetCorners = [] (
const Geometry1& geo1)
791 Dune::ReservedVector<Dune::ReservedVector<int, 4>, 6> facetCorners;
793 switch (geo1.corners())
797 facetCorners = {{2, 0, 6, 4}, {1, 3, 5, 7}, {0, 1, 4, 5},
798 {3, 2, 7, 6}, {1, 0, 3, 2}, {4, 5, 6, 7}};
801 facetCorners = {{0, 2, 1}, {3, 4, 5}, {0, 1, 3, 4}, {0, 3, 2, 5}, {1, 2, 4, 5}};
804 facetCorners = {{0, 2, 1, 3}, {0, 1, 4}, {1, 3, 4}, {2, 4, 3}, {0, 4, 2}};
807 facetCorners = {{1, 0, 2}, {0, 1, 3}, {0, 3, 2}, {1, 2, 3}};
810 DUNE_THROW(Dune::NotImplemented,
"Collision of segment and geometry of type "
811 << geo1.type() <<
", "<< geo1.corners() <<
" corners.");
818 const auto center1 = geo1.center();
819 auto computeNormal = [&geo1, ¢er1] (
const Dune::ReservedVector<int, 4>& facetCorners)
821 const auto c0 = geo1.corner(facetCorners[0]);
822 const auto v0 = geo1.corner(facetCorners[1]) - c0;
823 const auto v1 = geo1.corner(facetCorners[2]) - c0;
829 if (n*(center1 - c0) > 0.0)
843template <
class Geometry1,
class Geometry2,
class Policy>
856 template<
class P = Policy>
867template <
class Geometry1,
class Geometry2,
class Policy>
870 enum { dimworld = 3 };
875 using ctype =
typename Policy::ctype;
876 using Point =
typename Policy::Point;
880 static constexpr ctype eps_ = 1.5e-7;
894 template<
class P = Policy, std::enable_if_t<P::dimIntersection == 2,
int> = 0>
897 static_assert(int(dimworld) == int(Geometry2::coorddimension),
"Can only collide geometries of same coordinate dimension");
900 const auto a1 = geo1.corner(1) - geo1.corner(0);
901 const auto b1 = geo1.corner(2) - geo1.corner(0);
904 const auto a2 = geo2.corner(1) - geo2.corner(0);
905 const auto b2 = geo2.corner(2) - geo2.corner(0);
909 const auto a1Norm2 = a1.two_norm2();
910 const auto b1Norm2 = b1.two_norm2();
913 const auto maxNorm2 = max(a1Norm2, b1Norm2);
914 const auto eps2 = maxNorm2*eps_;
922 auto d1 = geo2.corner(0) - geo1.corner(0);
923 if (d1.two_norm2() < eps2)
924 d1 = geo2.corner(1) - geo1.corner(0);
927 const auto maxNorm = sqrt(maxNorm2);
928 const auto eps3 = maxNorm*eps2;
930 if (abs(d1*n2) > eps3)
934 std::vector<Point> points; points.reserve(8);
937 for (
int i = 0; i < geo1.corners(); ++i)
939 points.emplace_back(geo1.corner(i));
941 const auto numPoints1 = points.size();
942 const bool resultIsGeo1 = numPoints1 == geo1.corners();
946 for (
int i = 0; i < geo2.corners(); ++i)
948 points.emplace_back(geo2.corner(i));
950 const bool resultIsGeo2 = (points.size() - numPoints1) == geo2.corners();
953 const auto referenceElement1 = referenceElement(geo1);
954 const auto referenceElement2 = referenceElement(geo2);
957 using SegGeometry = Dune::MultiLinearGeometry<ctype, 1, dimworld, Detail::SegmentGeometryTraits<ctype>>;
959 for (
int i = 0; i < referenceElement1.size(dim1-1); ++i)
961 const auto localEdgeGeom1 = referenceElement1.template geometry<dim1-1>(i);
962 const auto edge1 = SegGeometry(
963 Dune::GeometryTypes::line,
964 std::array<Point, 2>{
965 geo1.global(localEdgeGeom1.corner(0)),
966 geo1.global(localEdgeGeom1.corner(1))
970 for (
int j = 0; j < referenceElement2.size(dim2-1); ++j)
972 const auto localEdgeGeom2 = referenceElement2.template geometry<dim2-1>(j);
973 const auto edge2 = SegGeometry(
974 Dune::GeometryTypes::line,
975 std::array<Point, 2>{
976 geo2.global(localEdgeGeom2.corner(0)),
977 geo2.global(localEdgeGeom2.corner(1))
982 typename EdgeTest::Intersection edgeIntersection;
983 if (EdgeTest::intersection(edge1, edge2, edgeIntersection))
984 points.emplace_back(edgeIntersection);
994 const auto eps = maxNorm*eps_;
995 std::sort(points.begin(), points.end(), [eps] (
const auto& a,
const auto& b) ->
bool
998 return (abs(a[0]-b[0]) > eps ? a[0] < b[0]
999 : (abs(a[1]-b[1]) > eps ? a[1] < b[1]
1003 const auto squaredEps = eps*eps;
1004 points.erase(std::unique(
1005 points.begin(), points.end(),
1006 [squaredEps] (
const auto& a,
const auto&b) { return (b-a).two_norm2() < squaredEps; }),
1011 if (points.size() < 3)
1027 template<
class P = Policy, std::enable_if_t<P::dimIntersection == 1,
int> = 0>
1030 static_assert(int(dimworld) == int(Geometry2::coorddimension),
"Can only collide geometries of same coordinate dimension");
1031 DUNE_THROW(Dune::NotImplemented,
"Polygon-polygon intersection detection for segment-like intersections");
1041 template<
class P = Policy, std::enable_if_t<P::dimIntersection == 0,
int> = 0>
1044 static_assert(int(dimworld) == int(Geometry2::coorddimension),
"Can only collide geometries of same coordinate dimension");
1045 DUNE_THROW(Dune::NotImplemented,
"Polygon-polygon intersection detection for touching points");
1053template <
class Geometry1,
class Geometry2,
class Policy>
1056 enum { dimworld = 3 };
1066 static constexpr ctype eps_ = 1.5e-7;
1075 template<
class P = Policy, std::enable_if_t<P::dimIntersection == 2,
int> = 0>
1078 static_assert(int(dimworld) == int(Geometry2::coorddimension),
"Can only collide geometries of same coordinate dimension");
1081 std::vector<Point> points; points.reserve(10);
1084 for (
int i = 0; i < geo1.corners(); ++i)
1086 points.emplace_back(geo1.corner(i));
1089 for (
int i = 0; i < geo2.corners(); ++i)
1091 points.emplace_back(geo2.corner(i));
1094 using PolyhedronFaceGeometry = Dune::MultiLinearGeometry<ctype, 2, dimworld, Detail::PolygonGeometryTraits<ctype>>;
1095 using SegGeometry = Dune::MultiLinearGeometry<ctype, 1, dimworld, Detail::SegmentGeometryTraits<ctype>>;
1097 const auto refElement1 = referenceElement(geo1);
1098 const auto refElement2 = referenceElement(geo2);
1104 for (
int i = 0; i < refElement1.size(dim1-1); ++i)
1106 const auto localEdgeGeom = refElement1.template geometry<dim1-1>(i);
1107 const auto p = geo1.global(localEdgeGeom.corner(0));
1108 const auto q = geo1.global(localEdgeGeom.corner(1));
1109 const auto segGeo = SegGeometry(Dune::GeometryTypes::line, std::array<Point, 2>{p, q});
1112 typename PolySegTest::Intersection polySegIntersection;
1113 if (PolySegTest::intersection(geo2, segGeo, polySegIntersection))
1114 points.emplace_back(std::move(polySegIntersection));
1118 for (
int i = 0; i < refElement1.size(1); ++i)
1120 const auto faceGeo = [&]()
1122 const auto localFaceGeo = refElement1.template geometry<1>(i);
1123 if (localFaceGeo.corners() == 4)
1125 const auto a = geo1.global(localFaceGeo.corner(0));
1126 const auto b = geo1.global(localFaceGeo.corner(1));
1127 const auto c = geo1.global(localFaceGeo.corner(2));
1128 const auto d = geo1.global(localFaceGeo.corner(3));
1130 return PolyhedronFaceGeometry(Dune::GeometryTypes::cube(2), Dune::ReservedVector<Point, 4>{a, b, c, d});
1134 const auto a = geo1.global(localFaceGeo.corner(0));
1135 const auto b = geo1.global(localFaceGeo.corner(1));
1136 const auto c = geo1.global(localFaceGeo.corner(2));
1138 return PolyhedronFaceGeometry(Dune::GeometryTypes::simplex(2), Dune::ReservedVector<Point, 4>{a, b, c});
1142 for (
int j = 0; j < refElement2.size(1); ++j)
1144 const auto localEdgeGeom = refElement2.template geometry<1>(j);
1145 const auto p = geo2.global(localEdgeGeom.corner(0));
1146 const auto q = geo2.global(localEdgeGeom.corner(1));
1148 const auto segGeo = SegGeometry(Dune::GeometryTypes::line, std::array<Point, 2>{p, q});
1151 typename PolySegTest::Intersection polySegIntersection;
1152 if (PolySegTest::intersection(faceGeo, segGeo, polySegIntersection))
1153 points.emplace_back(std::move(polySegIntersection));
1158 if (points.empty())
return false;
1161 const auto eps = (geo1.corner(0) - geo1.corner(1)).two_norm()*eps_;
1162 const auto notEqual = [eps] (
auto a,
auto b) {
using std::abs;
return abs(b-a) > eps; };
1163 std::sort(points.begin(), points.end(), [notEqual](
const auto& a,
const auto& b) ->
bool
1165 return (notEqual(a[0], b[0]) ? a[0] < b[0]
1166 : (notEqual(a[1], b[1]) ? a[1] < b[1]
1170 const auto squaredEps = eps*eps;
1171 points.erase(std::unique(
1172 points.begin(), points.end(),
1173 [squaredEps] (
const auto& a,
const auto&b) { return (b-a).two_norm2() < squaredEps; }),
1178 if (points.size() < 3)
return false;
1193 template<
class P = Policy, std::enable_if_t<P::dimIntersection != 2,
int> = 0>
1196 static_assert(int(dimworld) == int(Geometry2::coorddimension),
"Can only collide geometries of same coordinate dimension");
1197 DUNE_THROW(Dune::NotImplemented,
"Polyhedron-polygon intersection detection only implemented for polygon-like intersections");
1205template <
class Geometry1,
class Geometry2,
class Policy>
1218 template<
class P = Policy>
1229template <
class Geometry1,
class Geometry2,
class Policy>
1232 enum { dimworld = 3 };
1242 static constexpr ctype eps_ = 1.5e-7;
1252 template<
class P = Policy, std::enable_if_t<P::dimIntersection == 3,
int> = 0>
1256 int(dimworld) == int(Geometry2::coorddimension),
1257 "Can only collide geometries of same coordinate dimension"
1260 const auto refElement1 = referenceElement(geo1);
1261 const auto refElement2 = referenceElement(geo2);
1264 std::vector<Point> points;
1265 points.reserve(refElement1.size(2) + refElement2.size(2));
1268 const auto addPointIntersections = [&](
const auto& g1,
const auto& g2)
1270 for (
int i = 0; i < g1.corners(); ++i)
1272 points.emplace_back(c);
1275 addPointIntersections(geo1, geo2);
1276 addPointIntersections(geo2, geo1);
1279 using PolyhedronFaceGeometry = Dune::MultiLinearGeometry<ctype, 2, dimworld, Detail::PolygonGeometryTraits<ctype>>;
1280 using SegGeometry = Dune::MultiLinearGeometry<ctype, 1, dimworld, Detail::SegmentGeometryTraits<ctype>>;
1286 const auto addEdgeIntersections = [&](
const auto& g1,
const auto& g2,
const auto& ref1,
const auto& ref2)
1288 for (
int i = 0; i < ref1.size(1); ++i)
1290 const auto faceGeo = [&]()
1292 const auto localFaceGeo = ref1.template geometry<1>(i);
1293 if (localFaceGeo.corners() == 4)
1295 const auto a = g1.global(localFaceGeo.corner(0));
1296 const auto b = g1.global(localFaceGeo.corner(1));
1297 const auto c = g1.global(localFaceGeo.corner(2));
1298 const auto d = g1.global(localFaceGeo.corner(3));
1300 return PolyhedronFaceGeometry(
1301 Dune::GeometryTypes::cube(2), Dune::ReservedVector<Point, 4>{a, b, c, d}
1306 const auto a = g1.global(localFaceGeo.corner(0));
1307 const auto b = g1.global(localFaceGeo.corner(1));
1308 const auto c = g1.global(localFaceGeo.corner(2));
1310 return PolyhedronFaceGeometry(
1311 Dune::GeometryTypes::simplex(2), Dune::ReservedVector<Point, 4>{a, b, c}
1316 for (
int j = 0; j < ref2.size(2); ++j)
1318 const auto localEdgeGeom = ref2.template geometry<2>(j);
1319 const auto p = g2.global(localEdgeGeom.corner(0));
1320 const auto q = g2.global(localEdgeGeom.corner(1));
1322 const auto segGeo = SegGeometry(Dune::GeometryTypes::line, std::array<Point, 2>{p, q});
1325 typename PolySegTest::Intersection polySegIntersection;
1326 if (PolySegTest::intersection(faceGeo, segGeo, polySegIntersection))
1327 points.emplace_back(std::move(polySegIntersection));
1332 addEdgeIntersections(geo1, geo2, refElement1, refElement2);
1333 addEdgeIntersections(geo2, geo1, refElement2, refElement1);
1340 const auto norm = (geo1.corner(0) - geo1.corner(1)).two_norm();
1341 const auto eps = norm*eps_;
1342 const auto notEqual = [eps] (
auto a,
auto b) {
using std::abs;
return abs(b-a) > eps; };
1343 std::sort(points.begin(), points.end(), [notEqual](
const auto& a,
const auto& b) ->
bool
1345 return (notEqual(a[0], b[0]) ? a[0] < b[0]
1346 : (notEqual(a[1], b[1]) ? a[1] < b[1]
1350 const auto squaredEps = eps*eps;
1351 points.erase(std::unique(
1352 points.begin(), points.end(),
1353 [squaredEps] (
const auto& a,
const auto&b) { return (b-a).two_norm2() < squaredEps; }),
1358 if (points.size() < 4)
1362 const bool coplanar = [&]
1364 const auto p0 = points[0];
1368 const auto epsCoplanar =
normal.two_norm()*norm*eps_;
1369 for (
int i = 3; i < points.size(); ++i)
1371 const auto ad = points[i] - p0;
1373 if (abs(
normal*ad) > epsCoplanar)
1395 template<
class P = Policy, std::enable_if_t<P::dimIntersection != 3,
int> = 0>
1398 static_assert(int(dimworld) == int(Geometry2::coorddimension),
"Can only collide geometries of same coordinate dimension");
1399 DUNE_THROW(Dune::NotImplemented,
"Polyhedron-polyhedron intersection detection only implemented for polyhedron-like intersections");
1407template <
class Geometry1,
class Geometry2,
class Policy>
1410 enum { dimworld = 3 };
1420 static constexpr ctype eps_ = 1.5e-7;
1432 template<
class P = Policy, std::enable_if_t<P::dimIntersection == 1,
int> = 0>
1435 static_assert(int(dimworld) == int(Geometry2::coorddimension),
"Can only collide geometries of same coordinate dimension");
1437 ctype tfirst, tlast;
1438 if (intersect_(geo1, geo2, tfirst, tlast))
1458 template<
class P = Policy, std::enable_if_t<P::dimIntersection == 0,
int> = 0>
1461 static_assert(int(dimworld) == int(Geometry2::coorddimension),
"Can only collide geometries of same coordinate dimension");
1463 const auto p = geo2.corner(0);
1464 const auto q = geo2.corner(1);
1466 const auto a = geo1.corner(0);
1467 const auto b = geo1.corner(1);
1468 const auto c = geo1.corner(2);
1470 if (geo1.corners() == 3)
1471 return intersect_<Policy>(a, b, c, p, q, is);
1473 else if (geo1.corners() == 4)
1476 bool hasSegment1, hasSegment2;
1478 const auto d = geo1.corner(3);
1479 const bool intersects1 = intersect_<Policy>(a, b, d, p, q, is1, hasSegment1);
1480 const bool intersects2 = intersect_<Policy>(a, d, c, p, q, is2, hasSegment2);
1482 if (hasSegment1 || hasSegment2)
1485 if (intersects1) { is = is1;
return true; }
1486 if (intersects2) { is = is2;
return true; }
1492 DUNE_THROW(Dune::NotImplemented,
"Collision of segment and geometry of type "
1493 << geo1.type() <<
", "<< geo1.corners() <<
" corners.");
1500 template<
class P = Policy, std::enable_if_t<P::dimIntersection == 1,
int> = 0>
1501 static bool intersect_(
const Geometry1& geo1,
const Geometry2& geo2,
ctype& tfirst,
ctype& tlast)
1504 auto getFacetCorners = [] (
const Geometry1& geo1)
1506 Dune::ReservedVector<std::array<int, 2>, 4> facetCorners;
1507 switch (geo1.corners())
1510 facetCorners = {{0, 2}, {3, 1}, {1, 0}, {2, 3}};
1513 facetCorners = {{1, 0}, {0, 2}, {2, 1}};
1516 DUNE_THROW(Dune::NotImplemented,
"Collision of segment and geometry of type "
1517 << geo1.type() <<
" with "<< geo1.corners() <<
" corners.");
1520 return facetCorners;
1523 const auto center1 = geo1.center();
1524 const auto normal1 =
crossProduct(geo1.corner(1) - geo1.corner(0),
1525 geo1.corner(2) - geo1.corner(0));
1528 auto computeNormal = [¢er1, &normal1, &geo1] (
const std::array<int, 2>& facetCorners)
1530 const auto c0 = geo1.corner(facetCorners[0]);
1531 const auto c1 = geo1.corner(facetCorners[1]);
1532 const auto edge = c1 - c0;
1534 Dune::FieldVector<ctype, dimworld> n =
crossProduct(edge, normal1);
1539 if ( n*(center1-c0) > 0.0 )
1551 template<
class P = Policy, std::enable_if_t<P::dimIntersection == 0,
int> = 0>
1552 static bool intersect_(
const Point& a,
const Point& b,
const Point& c,
1557 return intersect_(a, b, c, p, q, is, hasSegment);
1564 template<
class P = Policy, std::enable_if_t<P::dimIntersection == 0,
int> = 0>
1565 static bool intersect_(
const Point& a,
const Point& b,
const Point& c,
1569 hasSegmentIs =
false;
1571 const auto ab = b - a;
1572 const auto ac = c - a;
1573 const auto qp = p - q;
1580 const auto denom =
normal*qp;
1581 const auto abnorm2 = ab.two_norm2();
1582 const auto eps = eps_*abnorm2*qp.two_norm();
1584 if (abs(denom) < eps)
1586 const auto pa = a - p;
1587 const auto denom2 =
normal*pa;
1588 if (abs(denom2) > eps_*pa.two_norm()*abnorm2)
1593 using SegmentPolicy =
typename IntersectionPolicy::SegmentPolicy<ctype, dimworld>;
1594 using Triangle = Dune::AffineGeometry<ctype, 2, dimworld>;
1595 using Segment = Dune::AffineGeometry<ctype, 1, dimworld>;
1596 using SegmentIntersectionAlgorithm = GeometryIntersection<Triangle, Segment, SegmentPolicy>;
1597 using SegmentIntersectionType =
typename SegmentIntersectionAlgorithm::Intersection;
1598 SegmentIntersectionType segmentIs;
1600 Triangle triangle(Dune::GeometryTypes::simplex(2), std::array<Point, 3>({a, b, c}));
1601 Segment segment(Dune::GeometryTypes::simplex(1), std::array<Point, 2>({p, q}));
1602 if (SegmentIntersectionAlgorithm::intersection(triangle, segment, segmentIs))
1604 hasSegmentIs =
true;
1610 for (
const auto& ip : {p, q})
1626 const auto ap = p - a;
1627 const auto t = (ap*
normal)/denom;
1628 if (t < 0.0 - eps_)
return false;
1629 if (t > 1.0 + eps_)
return false;
1634 const auto v = (ac*e)/denom;
1635 if (v < -eps_ || v > 1.0 + eps_)
return false;
1636 const auto w = -(ab*e)/denom;
1641 const auto u = 1.0 - v -
w;
1656template <
class Geometry1,
class Geometry2,
class Policy>
1669 template<
class P = Policy>
1680template <
class Geometry1,
class Geometry2,
class Policy>
1683 enum { dimworld = 3 };
1693 static constexpr ctype eps_ = 1.5e-7;
1702 template<
class P = Policy, std::enable_if_t<P::dimIntersection == 0,
int> = 0>
1705 static_assert(int(dimworld) == int(Geometry2::coorddimension),
"Can only collide geometries of same coordinate dimension");
1707 const auto v1 = geo1.corner(1) - geo1.corner(0);
1708 const auto v2 = geo2.corner(1) - geo2.corner(0);
1709 const auto ac = geo2.corner(0) - geo1.corner(0);
1711 const auto v1Norm2 = v1.two_norm2();
1712 const auto eps2 = eps_*v1Norm2;
1718 if ( n.two_norm2() < eps2*v1Norm2 )
1726 const auto sp = v1*v2;
1729 if ( ac.two_norm2() < eps2 )
1732 if ( (geo2.corner(1) - geo1.corner(1)).two_norm2() < eps2 )
1737 if ( (geo2.corner(1) - geo1.corner(0)).two_norm2() < eps2 )
1740 if ( (geo2.corner(0) - geo1.corner(1)).two_norm2() < eps2 )
1752 const auto t2 = - 1.0*(ac*v1Normal) / (v2*v1Normal);
1755 if (t2 < -1.0*eps_ || t2 > 1.0 + eps_)
1771 template<
class P = Policy, std::enable_if_t<P::dimIntersection == 1,
int> = 0>
1774 static_assert(int(dimworld) == int(Geometry2::coorddimension),
"Can only collide geometries of same coordinate dimension");
1776 const auto& a = geo1.corner(0);
1777 const auto& b = geo1.corner(1);
1778 const auto ab = b-a;
1780 const auto& p = geo2.corner(0);
1781 const auto& q = geo2.corner(1);
1782 const auto pq = q-p;
1784 const auto abNorm2 = ab.two_norm2();
1785 const auto pqNorm2 = pq.two_norm2();
1788 const auto eps2 = eps_*max(abNorm2, pqNorm2);
1795 const auto ap = (p-a);
1796 const auto aq = (q-a);
1814 tp = clamp(tp, 0.0, abNorm2);
1815 tq = clamp(tq, 0.0, abNorm2);
1817 if (abs(tp-tq) < eps2)
An axis-aligned bounding box volume hierarchy for dune grids.
typename Policy::Intersection Intersection
Definition geometryintersection.hh:274
typename Policy::ctype ctype
Definition geometryintersection.hh:272
typename Policy::Point Point
Definition geometryintersection.hh:273
static bool intersection(const Geometry1 &geo1, const Geometry2 &geo2, Intersection &intersection)
Colliding two segments.
Definition geometryintersection.hh:283
static bool intersection(const Geometry1 &geo1, const Geometry2 &geo2, typename Base::Intersection &intersection)
Colliding segment and convex polygon.
Definition geometryintersection.hh:555
static bool intersection(const Geometry1 &geo1, const Geometry2 &geo2, Intersection &intersection)
Colliding segment and convex polygon.
Definition geometryintersection.hh:442
typename Policy::ctype ctype
Definition geometryintersection.hh:426
typename Policy::Intersection Intersection
Definition geometryintersection.hh:428
static bool intersection(const Geometry1 &geo1, const Geometry2 &geo2, typename P::Intersection &intersection)
Colliding segment and convex polygon.
Definition geometryintersection.hh:466
typename Policy::Point Point
Definition geometryintersection.hh:427
typename Policy::Intersection Intersection
Definition geometryintersection.hh:575
typename Policy::ctype ctype
Definition geometryintersection.hh:573
static bool intersection(const Geometry1 &geo1, const Geometry2 &geo2, Intersection &intersection)
Colliding two polygons.
Definition geometryintersection.hh:593
typename Policy::Point Point
Definition geometryintersection.hh:574
typename Policy::Intersection Intersection
Definition geometryintersection.hh:1690
typename Policy::Point Point
Definition geometryintersection.hh:1689
static bool intersection(const Geometry1 &geo1, const Geometry2 &geo2, Intersection &intersection)
Colliding two segments.
Definition geometryintersection.hh:1703
typename Policy::ctype ctype
Definition geometryintersection.hh:1688
static bool intersection(const Geometry1 &geo1, const Geometry2 &geo2, typename Base::Intersection &intersection)
Colliding segment and convex polygon.
Definition geometryintersection.hh:1670
static bool intersection(const Geometry1 &geo1, const Geometry2 &geo2, typename Base::Intersection &intersection)
Colliding segment and convex polyhedron.
Definition geometryintersection.hh:857
typename Policy::Intersection Intersection
Definition geometryintersection.hh:1417
typename Policy::Point Point
Definition geometryintersection.hh:1416
typename Policy::ctype ctype
Definition geometryintersection.hh:1415
static bool intersection(const Geometry1 &geo1, const Geometry2 &geo2, Intersection &intersection)
Colliding segment and convex polygon.
Definition geometryintersection.hh:1433
static bool intersection(const Geometry1 &geo1, const Geometry2 &geo2, Intersection &is)
Colliding segment and convex polygon.
Definition geometryintersection.hh:1459
typename Policy::Point Point
Definition geometryintersection.hh:876
typename Policy::ctype ctype
Definition geometryintersection.hh:875
typename Policy::Intersection Intersection
Definition geometryintersection.hh:877
static bool intersection(const Geometry1 &geo1, const Geometry2 &geo2, Intersection &intersection)
Colliding two polygons.
Definition geometryintersection.hh:895
static bool intersection(const Geometry1 &geo1, const Geometry2 &geo2, typename Base::Intersection &intersection)
Colliding polygon and convex polyhedron.
Definition geometryintersection.hh:1219
typename Policy::Intersection Intersection
Definition geometryintersection.hh:715
typename Policy::Point Point
Definition geometryintersection.hh:714
typename Policy::ctype ctype
Definition geometryintersection.hh:713
static bool intersection(const Geometry1 &geo1, const Geometry2 &geo2, Intersection &intersection)
Colliding segment and convex polyhedron.
Definition geometryintersection.hh:733
typename Policy::Intersection Intersection
Definition geometryintersection.hh:1063
static bool intersection(const Geometry1 &geo1, const Geometry2 &geo2, Intersection &intersection)
Colliding polygon and convex polyhedron.
Definition geometryintersection.hh:1076
typename Policy::Point Point
Definition geometryintersection.hh:1062
typename Policy::ctype ctype
Definition geometryintersection.hh:1061
typename Policy::Point Point
Definition geometryintersection.hh:1238
typename Policy::Intersection Intersection
Definition geometryintersection.hh:1239
typename Policy::ctype ctype
Definition geometryintersection.hh:1237
static bool intersection(const Geometry1 &geo1, const Geometry2 &geo2, Intersection &intersection)
Colliding two convex polyhedra.
Definition geometryintersection.hh:1253
A class for geometry collision detection and intersection calculation.
Definition geometryintersection.hh:240
typename Policy::ctype ctype
Definition geometryintersection.hh:244
typename Policy::Intersection Intersection
Definition geometryintersection.hh:246
static bool intersection(const Geometry1 &geo1, const Geometry2 &geo2, Intersection &intersection)
Determine if the two geometries intersect and compute the intersection geometry.
Definition geometryintersection.hh:249
typename Policy::Point Point
Definition geometryintersection.hh:245
default policy chooser class
Definition geometryintersection.hh:88
std::tuple_element_t< isDim, DefaultPolicies > type
Definition geometryintersection.hh:102
A function to compute the convex hull of a point cloud.
Dune::FieldVector< Scalar, 3 > crossProduct(const Dune::FieldVector< Scalar, 3 > &vec1, const Dune::FieldVector< Scalar, 3 > &vec2)
Cross product of two vectors in three-dimensional Euclidean space.
Definition math.hh:671
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
Vector normal(const Vector &v)
Create a vector normal to the given one (v is expected to be non-zero).
Definition normal.hh:26
std::vector< Dune::FieldVector< ctype, dimWorld > > grahamConvexHull(std::vector< Dune::FieldVector< ctype, dimWorld > > &points)
Compute the points making up the convex hull around the given set of unordered points.
Definition grahamconvexhull.hh:186
bool intersectsPointSimplex(const Dune::FieldVector< ctype, dimworld > &point, const Dune::FieldVector< ctype, dimworld > &p0, const Dune::FieldVector< ctype, dimworld > &p1, const Dune::FieldVector< ctype, dimworld > &p2, const Dune::FieldVector< ctype, dimworld > &p3)
Find out whether a point is inside the tetrahedron (p0, p1, p2, p3) (dimworld is 3).
Definition intersectspointsimplex.hh:26
bool computeSegmentIntersection(const Geo1 &geo1, const Geo2 &geo2, ctype baseEps, ctype &tfirst, ctype &tlast, const GetFacetCornerIndices &getFacetCornerIndices, const ComputeNormalFunction &computeNormal)
Algorithm to find segment-like intersections of a polygon/polyhedron with a segment....
Definition geometryintersection.hh:128
Detect if a point intersects a geometry.
Define some often used mathematical functions.
Definition cvfelocalresidual.hh:25
Definition geometryintersection.hh:32
typename DefaultPolicyChooser< Geometry1, Geometry2 >::type DefaultPolicy
Helper alias to define the default intersection policy.
Definition geometryintersection.hh:107
const Scalar PengRobinsonMixture< Scalar, StaticParameters >::w
Definition pengrobinsonmixture.hh:140
const Scalar PengRobinsonMixture< Scalar, StaticParameters >::u
Definition pengrobinsonmixture.hh:138
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
Definition geometryintersection.hh:219
Dune::ReservedVector< Dune::FieldVector< ctype, cdim >, 4 > Type
Definition geometryintersection.hh:219
Triangle or quadrilateral geometries with corners stored in a Dune::ReservedVector.
Definition geometryintersection.hh:217
Definition geometryintersection.hh:204
std::array< Dune::FieldVector< ctype, cdim >, 2 > Type
Definition geometryintersection.hh:204
Definition geometryintersection.hh:208
static const bool v
Definition geometryintersection.hh:209
static const unsigned int topologyId
Definition geometryintersection.hh:210
Segment geometries with corners stored in a std::array.
Definition geometryintersection.hh:202
Policy structure for point-like intersections.
Definition geometryintersection.hh:37
static constexpr int dimIntersection
Definition geometryintersection.hh:42
Point Intersection
Definition geometryintersection.hh:43
ct ctype
Definition geometryintersection.hh:38
Dune::FieldVector< ctype, dw > Point
Definition geometryintersection.hh:39
static constexpr int dimWorld
Definition geometryintersection.hh:41
Policy structure for polygon-like intersections.
Definition geometryintersection.hh:62
static constexpr int dimWorld
Definition geometryintersection.hh:67
static constexpr int dimIntersection
Definition geometryintersection.hh:68
ct ctype
Definition geometryintersection.hh:63
Polygon Intersection
Definition geometryintersection.hh:69
Dune::FieldVector< ctype, dw > Point
Definition geometryintersection.hh:64
std::vector< Point > Polygon
Definition geometryintersection.hh:65
Policy structure for polyhedron-like intersections.
Definition geometryintersection.hh:75
std::vector< Point > Polyhedron
Definition geometryintersection.hh:78
Dune::FieldVector< ctype, dw > Point
Definition geometryintersection.hh:77
Polyhedron Intersection
Definition geometryintersection.hh:82
static constexpr int dimIntersection
Definition geometryintersection.hh:81
static constexpr int dimWorld
Definition geometryintersection.hh:80
ct ctype
Definition geometryintersection.hh:76
Policy structure for segment-like intersections.
Definition geometryintersection.hh:49
Segment Intersection
Definition geometryintersection.hh:56
static constexpr int dimIntersection
Definition geometryintersection.hh:55
Dune::FieldVector< ctype, dw > Point
Definition geometryintersection.hh:51
static constexpr int dimWorld
Definition geometryintersection.hh:54
std::array< Point, 2 > Segment
Definition geometryintersection.hh:52
ct ctype
Definition geometryintersection.hh:50