version 3.11-dev
Loading...
Searching...
No Matches
geometryintersection.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//
13#ifndef DUMUX_GEOMETRY_INTERSECTION_HH
14#define DUMUX_GEOMETRY_INTERSECTION_HH
15
16#include <array>
17#include <tuple>
18#include <vector>
19
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>
25
26#include <dumux/common/math.hh>
30
31namespace Dumux {
33
35template<class ct, int dw>
37{
38 using ctype = ct;
39 using Point = Dune::FieldVector<ctype, dw>;
40
41 static constexpr int dimWorld = dw;
42 static constexpr int dimIntersection = 0;
44};
45
47template<class ct, int dw>
49{
50 using ctype = ct;
51 using Point = Dune::FieldVector<ctype, dw>;
52 using Segment = std::array<Point, 2>;
53
54 static constexpr int dimWorld = dw;
55 static constexpr int dimIntersection = 1;
57};
58
60template<class ct, int dw>
62{
63 using ctype = ct;
64 using Point = Dune::FieldVector<ctype, dw>;
65 using Polygon = std::vector<Point>;
66
67 static constexpr int dimWorld = dw;
68 static constexpr int dimIntersection = 2;
70};
71
73template<class ct, int dw>
75{
76 using ctype = ct;
77 using Point = Dune::FieldVector<ctype, dw>;
78 using Polyhedron = std::vector<Point>;
79
80 static constexpr int dimWorld = dw;
81 static constexpr int dimIntersection = 3;
83};
84
86template<class Geometry1, class Geometry2>
88{
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;
96
97 using DefaultPolicies = std::tuple<PointPolicy<ctype, dimworld>,
101public:
102 using type = std::tuple_element_t<isDim, DefaultPolicies>;
103};
104
106template<class Geometry1, class Geometry2>
108
109} // end namespace IntersectionPolicy
110
111namespace Detail {
112
126template< class Geo1, class Geo2, class ctype,
127 class GetFacetCornerIndices, class ComputeNormalFunction >
128bool computeSegmentIntersection(const Geo1& geo1, const Geo2& geo2, ctype baseEps,
129 ctype& tfirst, ctype& tlast,
130 const GetFacetCornerIndices& getFacetCornerIndices,
131 const ComputeNormalFunction& computeNormal)
132{
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");
136
137 const auto a = geo2.corner(0);
138 const auto b = geo2.corner(1);
139 const auto d = b - a;
140
141 // The initial interval is the whole segment.
142 // Afterwards we start clipping the interval
143 // at the intersections with the facets of geo1
144 tfirst = 0.0;
145 tlast = 1.0;
146
147 const auto facets = getFacetCornerIndices(geo1);
148 for (const auto& f : facets)
149 {
150 const auto n = computeNormal(f);
151
152 const auto c0 = geo1.corner(f[0]);
153 const ctype denom = n*d;
154 const ctype dist = n*(a-c0);
155
156 // use first edge of the facet to scale eps
157 const auto edge1 = geo1.corner(f[1]) - geo1.corner(f[0]);
158 const ctype eps = baseEps*edge1.two_norm();
159
160 // if denominator is zero the segment in parallel to the edge.
161 // If the distance is positive there is no intersection
162 using std::abs;
163 if (abs(denom) < eps)
164 {
165 if (dist > eps)
166 return false;
167 }
168 else // not parallel: compute line-line intersection
169 {
170 const ctype t = -dist / denom;
171 // if entering half space cut tfirst if t is larger
172 using std::signbit;
173 if (signbit(denom))
174 {
175 if (t > tfirst)
176 tfirst = t;
177 }
178 // if exiting half space cut tlast if t is smaller
179 else
180 {
181 if (t < tlast)
182 tlast = t;
183 }
184 // there is no intersection of the interval is empty
185 // use unscaled epsilon since t is in local coordinates
186 if (tfirst > tlast - baseEps)
187 return false;
188 }
189 }
190
191 // If we made it until here an intersection exists.
192 return true;
193}
194
195} // end namespace Detail
196
197namespace Detail {
198
200template<class ctype>
201struct SegmentGeometryTraits : public Dune::MultiLinearGeometryTraits<ctype>
202{
203 template<int mydim, int cdim>
204 struct CornerStorage { using Type = std::array<Dune::FieldVector<ctype, cdim>, 2>; };
205
206 template<int dim>
208 {
209 static const bool v = true;
210 static const unsigned int topologyId = Dune::GeometryTypes::line.id();
211 };
212};
213
215template<class ctype>
216struct PolygonGeometryTraits : public Dune::MultiLinearGeometryTraits<ctype>
217{
218 template<int mydim, int cdim>
219 struct CornerStorage { using Type = Dune::ReservedVector<Dune::FieldVector<ctype, cdim>, 4>; };
220};
221
222} // end namespace Detail
223
233template
234<class Geometry1, class Geometry2,
236 int dimworld = Geometry1::coorddimension,
237 int dim1 = Geometry1::mydimension,
238 int dim2 = Geometry2::mydimension>
240{
241 static constexpr int dimWorld = Policy::dimWorld;
242
243public:
244 using ctype = typename Policy::ctype;
245 using Point = typename Policy::Point;
246 using Intersection = typename Policy::Intersection;
247
249 static bool intersection(const Geometry1& geo1, const Geometry2& geo2, Intersection& intersection)
250 {
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);
254 }
255};
256
261template <class Geometry1, class Geometry2, class Policy>
262class GeometryIntersection<Geometry1, Geometry2, Policy, 2, 1, 1>
263{
264 enum { dimworld = 2 };
265 enum { dim1 = 1 };
266 enum { dim2 = 1 };
267
268 // base epsilon for floating point comparisons
269 static constexpr typename Policy::ctype eps_ = 1.5e-7;
270
271public:
272 using ctype = typename Policy::ctype;
273 using Point = typename Policy::Point;
274 using Intersection = typename Policy::Intersection;
275
282 template<class P = Policy, std::enable_if_t<P::dimIntersection == 0, int> = 0>
283 static bool intersection(const Geometry1& geo1, const Geometry2& geo2, Intersection& intersection)
284 {
285 static_assert(int(dimworld) == int(Geometry2::coorddimension), "Can only collide geometries of same coordinate dimension");
286
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);
290
291 auto n2 = Point({-1.0*v2[1], v2[0]});
292 n2 /= n2.two_norm();
293
294 // compute distance of first corner on geo2 to line1
295 const auto dist12 = n2*ac;
296
297 // first check parallel segments
298 using std::abs;
299 using std::sqrt;
300
301 const auto v1Norm2 = v1.two_norm2();
302 const auto eps = eps_*sqrt(v1Norm2);
303 const auto eps2 = eps_*v1Norm2;
304
305 const auto sp = n2*v1;
306 if (abs(sp) < eps)
307 {
308 if (abs(dist12) > eps)
309 return false;
310
311 // point intersection can only be one of the corners
312 if ( (v1*v2) < 0.0 )
313 {
314 if ( ac.two_norm2() < eps2 )
315 { intersection = geo2.corner(0); return true; }
316
317 if ( (geo2.corner(1) - geo1.corner(1)).two_norm2() < eps2 )
318 { intersection = geo2.corner(1); return true; }
319 }
320 else
321 {
322 if ( (geo2.corner(1) - geo1.corner(0)).two_norm2() < eps2 )
323 { intersection = geo2.corner(1); return true; }
324
325 if ( (geo2.corner(0) - geo1.corner(1)).two_norm2() < eps2 )
326 { intersection = geo2.corner(0); return true; }
327 }
328
329 // no intersection
330 return false;
331 }
332
333 // intersection point on v1 in local coords
334 const auto t1 = dist12 / sp;
335
336 // check if the local coords are valid
337 if (t1 < -1.0*eps_ || t1 > 1.0 + eps_)
338 return false;
339
340 // compute global coordinates
341 auto isPoint = geo1.global(t1);
342
343 // check if point is in bounding box of v2
344 const auto c = geo2.corner(0);
345 const auto d = geo2.corner(1);
346
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]) });
349
350 if ( intersectsPointBoundingBox(isPoint, bBox.data()) )
351 {
352 intersection = std::move(isPoint);
353 return true;
354 }
355
356 return false;
357 }
358
365 template<class P = Policy, std::enable_if_t<P::dimIntersection == 1, int> = 0>
366 static bool intersection(const Geometry1& geo1, const Geometry2& geo2, Intersection& intersection)
367 {
368 static_assert(int(dimworld) == int(Geometry2::coorddimension), "Can only collide geometries of same coordinate dimension");
369
370 const auto& a = geo1.corner(0);
371 const auto& b = geo1.corner(1);
372 const auto ab = b - a;
373
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]};
378
379 using std::max;
380 const auto abNorm2 = ab.two_norm2();
381 const auto pqNorm2 = pq.two_norm2();
382 const auto eps2 = eps_*max(abNorm2, pqNorm2);
383
384 // non-parallel segments do not intersect in a segment.
385 using std::abs;
386 if (abs(n2*ab) > eps2)
387 return false;
388
389 // check if the segments lie on the same line
390 const auto ap = p - a;
391 if (abs(ap*n2) > eps2)
392 return false;
393
394 // compute scaled local coordinates of corner 1/2 of segment2 on segment1
395 auto t1 = ab*ap;
396 auto t2 = ab*(q - a);
397
398 using std::swap;
399 if (t1 > t2)
400 swap(t1, t2);
401
402 using std::clamp;
403 t1 = clamp(t1, 0.0, abNorm2);
404 t2 = clamp(t2, 0.0, abNorm2);
405
406 if (abs(t2-t1) < eps2)
407 return false;
408
409 intersection = Intersection({geo1.global(t1/abNorm2), geo1.global(t2/abNorm2)});
410 return true;
411 }
412};
413
418template <class Geometry1, class Geometry2, class Policy>
419class GeometryIntersection<Geometry1, Geometry2, Policy, 2, 2, 1>
420{
421 enum { dimworld = 2 };
422 enum { dim1 = 2 };
423 enum { dim2 = 1 };
424
425public:
426 using ctype = typename Policy::ctype;
427 using Point = typename Policy::Point;
428 using Intersection = typename Policy::Intersection;
429
430private:
431 static constexpr ctype eps_ = 1.5e-7; // base epsilon for floating point comparisons
432
433public:
441 template<class P = Policy, std::enable_if_t<P::dimIntersection == 1, int> = 0>
442 static bool intersection(const Geometry1& geo1, const Geometry2& geo2, Intersection& intersection)
443 {
444 static_assert(int(dimworld) == int(Geometry2::coorddimension), "Can only collide geometries of same coordinate dimension");
445
446 ctype tfirst, tlast;
447 if (intersect_(geo1, geo2, tfirst, tlast))
448 {
449 // the intersection exists. Export the intersections geometry now:
450 // s(t) = a + t(b-a) in [tfirst, tlast]
451 intersection = Intersection({geo2.global(tfirst), geo2.global(tlast)});
452 return true;
453 }
454
455 return false;
456 }
457
465 template<class P = Policy, std::enable_if_t<P::dimIntersection == 0, int> = 0>
466 static bool intersection(const Geometry1& geo1, const Geometry2& geo2, typename P::Intersection& intersection)
467 {
468 static_assert(int(dimworld) == int(Geometry2::coorddimension), "Can only collide geometries of same coordinate dimension");
469
470 // intersect_ stops as soon as the interval [tfirst, tlast] is empty or a single point,
471 // and a single point is an intersection only if it lies in the polygon
472 ctype tfirst, tlast;
473 using std::abs;
474 if (!intersect_(geo1, geo2, tfirst, tlast) && abs(tfirst - tlast) < eps_)
475 {
476 const auto point = geo2.global(tfirst);
477 if (intersectsPointGeometry(point, geo1))
478 {
479 intersection = Intersection(point);
480 return true;
481 }
482 }
483
484 return false;
485 }
486
487private:
491 static bool intersect_(const Geometry1& geo1, const Geometry2& geo2, ctype& tfirst, ctype& tlast)
492 {
493 // lambda to obtain the facet corners on geo1
494 auto getFacetCorners = [] (const Geometry1& geo1)
495 {
496 Dune::ReservedVector<std::array<int, 2>, 4> facetCorners;
497 switch (geo1.corners())
498 {
499 case 4: // quadrilateral
500 facetCorners = {{0, 2}, {3, 1}, {1, 0}, {2, 3}};
501 break;
502 case 3: // triangle
503 facetCorners = {{1, 0}, {0, 2}, {2, 1}};
504 break;
505 default:
506 DUNE_THROW(Dune::NotImplemented, "Collision of segment and geometry of type "
507 << geo1.type() << " with "<< geo1.corners() << " corners.");
508 }
509
510 return facetCorners;
511 };
512
513 // lambda to obtain the normal on a facet
514 const auto center1 = geo1.center();
515 auto computeNormal = [&geo1, &center1] (const std::array<int, 2>& facetCorners)
516 {
517 const auto c0 = geo1.corner(facetCorners[0]);
518 const auto c1 = geo1.corner(facetCorners[1]);
519 const auto edge = c1 - c0;
520
521 Dune::FieldVector<ctype, dimworld> n({-1.0*edge[1], edge[0]});
522 n /= n.two_norm();
523
524 // orientation of the element is not known
525 // make sure normal points outwards of element
526 if ( n*(center1-c0) > 0.0 )
527 n *= -1.0;
528
529 return n;
530 };
531
532 return Detail::computeSegmentIntersection(geo1, geo2, eps_, tfirst, tlast, getFacetCorners, computeNormal);
533 }
534};
535
540template <class Geometry1, class Geometry2, class Policy>
541class GeometryIntersection<Geometry1, Geometry2, Policy, 2, 1, 2>
542: public GeometryIntersection<Geometry2, Geometry1, Policy, 2, 2, 1>
543{
545
546public:
554 template<class P = Policy>
555 static bool intersection(const Geometry1& geo1, const Geometry2& geo2, typename Base::Intersection& intersection)
556 {
557 return Base::intersection(geo2, geo1, intersection);
558 }
559};
560
565template <class Geometry1, class Geometry2, class Policy>
566class GeometryIntersection<Geometry1, Geometry2, Policy, 2, 2, 2>
567{
568 enum { dimworld = 2 };
569 enum { dim1 = 2 };
570 enum { dim2 = 2 };
571
572public:
573 using ctype = typename Policy::ctype;
574 using Point = typename Policy::Point;
575 using Intersection = typename Policy::Intersection;
576
577private:
578 static constexpr ctype eps_ = 1.5e-7; // base epsilon for floating point comparisons
579
580public:
592 template<class P = Policy, std::enable_if_t<P::dimIntersection == 2, int> = 0>
593 static bool intersection(const Geometry1& geo1, const Geometry2& geo2, Intersection& intersection)
594 {
595 static_assert(int(dimworld) == int(Geometry2::coorddimension), "Can only collide geometries of same coordinate dimension");
596
597 // the candidate intersection points
598 std::vector<Point> points; points.reserve(6);
599
600 // add polygon1 corners that are inside polygon2
601 for (int i = 0; i < geo1.corners(); ++i)
602 if (intersectsPointGeometry(geo1.corner(i), geo2))
603 points.emplace_back(geo1.corner(i));
604
605 const auto numPoints1 = points.size();
606 if (numPoints1 != geo1.corners())
607 {
608 // add polygon2 corners that are inside polygon1
609 for (int i = 0; i < geo2.corners(); ++i)
610 if (intersectsPointGeometry(geo2.corner(i), geo1))
611 points.emplace_back(geo2.corner(i));
612
613 if (points.size() - numPoints1 != geo2.corners())
614 {
615 const auto refElement1 = referenceElement(geo1);
616 const auto refElement2 = referenceElement(geo2);
617
618 // add intersections of edges
619 using SegGeometry = Dune::MultiLinearGeometry<ctype, 1, dimworld, Detail::SegmentGeometryTraits<ctype>>;
621 for (int i = 0; i < refElement1.size(dim1-1); ++i)
622 {
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))} });
627
628 for (int j = 0; j < refElement2.size(dim2-1); ++j)
629 {
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))} });
634
636 typename EdgeTest::Intersection edgeIntersection;
637 if (EdgeTest::intersection(edge1, edge2, edgeIntersection))
638 points.emplace_back(edgeIntersection);
639 }
640 }
641 }
642 }
643
644 if (points.empty())
645 return false;
646
647 // remove duplicates
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
650 {
651 using std::abs;
652 return (abs(a[0]-b[0]) > eps ? a[0] < b[0] : a[1] < b[1]);
653 });
654
655 auto removeIt = std::unique(points.begin(), points.end(), [&eps](const auto& a, const auto&b)
656 {
657 return (b-a).two_norm() < eps;
658 });
659
660 points.erase(removeIt, points.end());
661
662 // return false if we don't have at least three unique points
663 if (points.size() < 3)
664 return false;
665
666 // intersection polygon is convex hull of above points
668 assert(!intersection.empty());
669 return true;
670 }
671
679 template<class P = Policy, std::enable_if_t<P::dimIntersection == 1, int> = 0>
680 static bool intersection(const Geometry1& geo1, const Geometry2& geo2, Intersection& intersection)
681 {
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");
684 }
685
693 template<class P = Policy, std::enable_if_t<P::dimIntersection == 0, int> = 0>
694 static bool intersection(const Geometry1& geo1, const Geometry2& geo2, Intersection& intersection)
695 {
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");
698 }
699};
700
705template <class Geometry1, class Geometry2, class Policy>
706class GeometryIntersection<Geometry1, Geometry2, Policy, 3, 3, 1>
707{
708 enum { dimworld = 3 };
709 enum { dim1 = 3 };
710 enum { dim2 = 1 };
711
712public:
713 using ctype = typename Policy::ctype;
714 using Point = typename Policy::Point;
715 using Intersection = typename Policy::Intersection;
716
717private:
718 static constexpr ctype eps_ = 1.5e-7; // base epsilon for floating point comparisons
719
720public:
732 template<class P = Policy, std::enable_if_t<P::dimIntersection == 1, int> = 0>
733 static bool intersection(const Geometry1& geo1, const Geometry2& geo2, Intersection& intersection)
734 {
735 static_assert(int(dimworld) == int(Geometry2::coorddimension), "Can only collide geometries of same coordinate dimension");
736
737 ctype tfirst, tlast;
738 if (intersect_(geo1, geo2, tfirst, tlast))
739 {
740 // Intersection exists. Export the intersections geometry now:
741 // s(t) = a + t(b-a) in [tfirst, tlast]
742 intersection = Intersection({geo2.global(tfirst), geo2.global(tlast)});
743 return true;
744 }
745
746 return false;
747 }
748
760 template<class P = Policy, std::enable_if_t<P::dimIntersection == 0, int> = 0>
761 static bool intersection(const Geometry1& geo1, const Geometry2& geo2, Intersection& intersection)
762 {
763 static_assert(int(dimworld) == int(Geometry2::coorddimension), "Can only collide geometries of same coordinate dimension");
764
765 // intersect_ stops as soon as the interval [tfirst, tlast] is empty or a single point,
766 // and a single point is an intersection only if it lies in the polyhedron
767 ctype tfirst, tlast;
768 using std::abs;
769 if (!intersect_(geo1, geo2, tfirst, tlast) && abs(tfirst - tlast) < eps_)
770 {
771 const auto point = geo2.global(tfirst);
772 if (intersectsPointGeometry(point, geo1))
773 {
774 intersection = Intersection(point);
775 return true;
776 }
777 }
778
779 return false;
780 }
781
782private:
786 static bool intersect_(const Geometry1& geo1, const Geometry2& geo2, ctype& tfirst, ctype& tlast)
787 {
788 // lambda to obtain facet corners on geo1
789 auto getFacetCorners = [] (const Geometry1& geo1)
790 {
791 Dune::ReservedVector<Dune::ReservedVector<int, 4>, 6> facetCorners;
792 // sort facet corner so that normal n = (p1-p0)x(p2-p0) always points outwards
793 switch (geo1.corners())
794 {
795 // todo compute intersection geometries
796 case 8: // hexahedron
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}};
799 break;
800 case 6: // prism
801 facetCorners = {{0, 2, 1}, {3, 4, 5}, {0, 1, 3, 4}, {0, 3, 2, 5}, {1, 2, 4, 5}};
802 break;
803 case 5: // pyramid
804 facetCorners = {{0, 2, 1, 3}, {0, 1, 4}, {1, 3, 4}, {2, 4, 3}, {0, 4, 2}};
805 break;
806 case 4: // tetrahedron
807 facetCorners = {{1, 0, 2}, {0, 1, 3}, {0, 3, 2}, {1, 2, 3}};
808 break;
809 default:
810 DUNE_THROW(Dune::NotImplemented, "Collision of segment and geometry of type "
811 << geo1.type() << ", "<< geo1.corners() << " corners.");
812 }
813
814 return facetCorners;
815 };
816
817 // lambda to obtain the normal on a facet
818 const auto center1 = geo1.center();
819 auto computeNormal = [&geo1, &center1] (const Dune::ReservedVector<int, 4>& facetCorners)
820 {
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;
824
825 auto n = crossProduct(v0, v1);
826 n /= n.two_norm();
827
828 // the facet corners give outward normals for one orientation of the element only
829 if (n*(center1 - c0) > 0.0)
830 n *= -1.0;
831
832 return n;
833 };
834
835 return Detail::computeSegmentIntersection(geo1, geo2, eps_, tfirst, tlast, getFacetCorners, computeNormal);
836 }
837};
838
843template <class Geometry1, class Geometry2, class Policy>
844class GeometryIntersection<Geometry1, Geometry2, Policy, 3, 1, 3>
845: public GeometryIntersection<Geometry2, Geometry1, Policy, 3, 3, 1>
846{
848public:
856 template<class P = Policy>
857 static bool intersection(const Geometry1& geo1, const Geometry2& geo2, typename Base::Intersection& intersection)
858 {
859 return Base::intersection(geo2, geo1, intersection);
860 }
861};
862
867template <class Geometry1, class Geometry2, class Policy>
868class GeometryIntersection<Geometry1, Geometry2, Policy, 3, 2, 2>
869{
870 enum { dimworld = 3 };
871 enum { dim1 = 2 };
872 enum { dim2 = 2 };
873
874public:
875 using ctype = typename Policy::ctype;
876 using Point = typename Policy::Point;
877 using Intersection = typename Policy::Intersection;
878
879private:
880 static constexpr ctype eps_ = 1.5e-7; // base epsilon for floating point comparisons
881
882public:
894 template<class P = Policy, std::enable_if_t<P::dimIntersection == 2, int> = 0>
895 static bool intersection(const Geometry1& geo1, const Geometry2& geo2, Intersection& intersection)
896 {
897 static_assert(int(dimworld) == int(Geometry2::coorddimension), "Can only collide geometries of same coordinate dimension");
898
899 // if the geometries are not parallel, there cannot be a polygon intersection
900 const auto a1 = geo1.corner(1) - geo1.corner(0);
901 const auto b1 = geo1.corner(2) - geo1.corner(0);
902 const auto n1 = crossProduct(a1, b1);
903
904 const auto a2 = geo2.corner(1) - geo2.corner(0);
905 const auto b2 = geo2.corner(2) - geo2.corner(0);
906 const auto n2 = crossProduct(a2, b2);
907
908 // compute an epsilon scaled with larger edge length
909 const auto a1Norm2 = a1.two_norm2();
910 const auto b1Norm2 = b1.two_norm2();
911
912 using std::max;
913 const auto maxNorm2 = max(a1Norm2, b1Norm2);
914 const auto eps2 = maxNorm2*eps_;
915
916 // early exit if polygons are not parallel
917 using std::abs;
918 if (crossProduct(n1, n2).two_norm2() > eps2*maxNorm2)
919 return false;
920
921 // they are parallel, verify that they are on the same plane
922 auto d1 = geo2.corner(0) - geo1.corner(0);
923 if (d1.two_norm2() < eps2)
924 d1 = geo2.corner(1) - geo1.corner(0);
925
926 using std::sqrt;
927 const auto maxNorm = sqrt(maxNorm2);
928 const auto eps3 = maxNorm*eps2;
929
930 if (abs(d1*n2) > eps3)
931 return false;
932
933 // the candidate intersection points
934 std::vector<Point> points; points.reserve(8);
935
936 // add polygon1 corners that are inside polygon2
937 for (int i = 0; i < geo1.corners(); ++i)
938 if (intersectsPointGeometry(geo1.corner(i), geo2))
939 points.emplace_back(geo1.corner(i));
940
941 const auto numPoints1 = points.size();
942 const bool resultIsGeo1 = numPoints1 == geo1.corners();
943 if (!resultIsGeo1)
944 {
945 // add polygon2 corners that are inside polygon1
946 for (int i = 0; i < geo2.corners(); ++i)
947 if (intersectsPointGeometry(geo2.corner(i), geo1))
948 points.emplace_back(geo2.corner(i));
949
950 const bool resultIsGeo2 = (points.size() - numPoints1) == geo2.corners();
951 if (!resultIsGeo2)
952 {
953 const auto referenceElement1 = referenceElement(geo1);
954 const auto referenceElement2 = referenceElement(geo2);
955
956 // add intersections of edges
957 using SegGeometry = Dune::MultiLinearGeometry<ctype, 1, dimworld, Detail::SegmentGeometryTraits<ctype>>;
959 for (int i = 0; i < referenceElement1.size(dim1-1); ++i)
960 {
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))
967 }
968 );
969
970 for (int j = 0; j < referenceElement2.size(dim2-1); ++j)
971 {
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))
978 }
979 );
980
982 typename EdgeTest::Intersection edgeIntersection;
983 if (EdgeTest::intersection(edge1, edge2, edgeIntersection))
984 points.emplace_back(edgeIntersection);
985 }
986 }
987 }
988 }
989
990 if (points.empty())
991 return false;
992
993 // remove duplicates
994 const auto eps = maxNorm*eps_;
995 std::sort(points.begin(), points.end(), [eps] (const auto& a, const auto& b) -> bool
996 {
997 using std::abs;
998 return (abs(a[0]-b[0]) > eps ? a[0] < b[0]
999 : (abs(a[1]-b[1]) > eps ? a[1] < b[1]
1000 : (a[2] < b[2])));
1001 });
1002
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; }),
1007 points.end()
1008 );
1009
1010 // return false if we don't have at least three unique points
1011 if (points.size() < 3)
1012 return false;
1013
1014 // intersection polygon is convex hull of above points
1016 assert(!intersection.empty());
1017 return true;
1018 }
1019
1027 template<class P = Policy, std::enable_if_t<P::dimIntersection == 1, int> = 0>
1028 static bool intersection(const Geometry1& geo1, const Geometry2& geo2, Intersection& intersection)
1029 {
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");
1032 }
1033
1041 template<class P = Policy, std::enable_if_t<P::dimIntersection == 0, int> = 0>
1042 static bool intersection(const Geometry1& geo1, const Geometry2& geo2, Intersection& intersection)
1043 {
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");
1046 }
1047};
1048
1053template <class Geometry1, class Geometry2, class Policy>
1054class GeometryIntersection<Geometry1, Geometry2, Policy, 3, 3, 2>
1055{
1056 enum { dimworld = 3 };
1057 enum { dim1 = 3 };
1058 enum { dim2 = 2 };
1059
1060public:
1061 using ctype = typename Policy::ctype;
1062 using Point = typename Policy::Point;
1063 using Intersection = typename Policy::Intersection;
1064
1065private:
1066 static constexpr ctype eps_ = 1.5e-7; // base epsilon for floating point comparisons
1067
1068public:
1075 template<class P = Policy, std::enable_if_t<P::dimIntersection == 2, int> = 0>
1076 static bool intersection(const Geometry1& geo1, const Geometry2& geo2, Intersection& intersection)
1077 {
1078 static_assert(int(dimworld) == int(Geometry2::coorddimension), "Can only collide geometries of same coordinate dimension");
1079
1080 // the candidate intersection points
1081 std::vector<Point> points; points.reserve(10);
1082
1083 // add 3d geometry corners that are inside the 2d geometry
1084 for (int i = 0; i < geo1.corners(); ++i)
1085 if (intersectsPointGeometry(geo1.corner(i), geo2))
1086 points.emplace_back(geo1.corner(i));
1087
1088 // add 2d geometry corners that are inside the 3d geometry
1089 for (int i = 0; i < geo2.corners(); ++i)
1090 if (intersectsPointGeometry(geo2.corner(i), geo1))
1091 points.emplace_back(geo2.corner(i));
1092
1093 // get some geometry types
1094 using PolyhedronFaceGeometry = Dune::MultiLinearGeometry<ctype, 2, dimworld, Detail::PolygonGeometryTraits<ctype>>;
1095 using SegGeometry = Dune::MultiLinearGeometry<ctype, 1, dimworld, Detail::SegmentGeometryTraits<ctype>>;
1096
1097 const auto refElement1 = referenceElement(geo1);
1098 const auto refElement2 = referenceElement(geo2);
1099
1100 // intersection policy for point-like intersections (used later)
1102
1103 // add intersection points of all polyhedron edges (codim dim-1) with the polygon
1104 for (int i = 0; i < refElement1.size(dim1-1); ++i)
1105 {
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});
1110
1112 typename PolySegTest::Intersection polySegIntersection;
1113 if (PolySegTest::intersection(geo2, segGeo, polySegIntersection))
1114 points.emplace_back(std::move(polySegIntersection));
1115 }
1116
1117 // add intersection points of all polygon faces (codim 1) with the polyhedron faces
1118 for (int i = 0; i < refElement1.size(1); ++i)
1119 {
1120 const auto faceGeo = [&]()
1121 {
1122 const auto localFaceGeo = refElement1.template geometry<1>(i);
1123 if (localFaceGeo.corners() == 4)
1124 {
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));
1129
1130 return PolyhedronFaceGeometry(Dune::GeometryTypes::cube(2), Dune::ReservedVector<Point, 4>{a, b, c, d});
1131 }
1132 else
1133 {
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));
1137
1138 return PolyhedronFaceGeometry(Dune::GeometryTypes::simplex(2), Dune::ReservedVector<Point, 4>{a, b, c});
1139 }
1140 }();
1141
1142 for (int j = 0; j < refElement2.size(1); ++j)
1143 {
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));
1147
1148 const auto segGeo = SegGeometry(Dune::GeometryTypes::line, std::array<Point, 2>{p, q});
1149
1151 typename PolySegTest::Intersection polySegIntersection;
1152 if (PolySegTest::intersection(faceGeo, segGeo, polySegIntersection))
1153 points.emplace_back(std::move(polySegIntersection));
1154 }
1155 }
1156
1157 // return if no intersection points were found
1158 if (points.empty()) return false;
1159
1160 // remove duplicates
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
1164 {
1165 return (notEqual(a[0], b[0]) ? a[0] < b[0]
1166 : (notEqual(a[1], b[1]) ? a[1] < b[1]
1167 : (a[2] < b[2])));
1168 });
1169
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; }),
1174 points.end()
1175 );
1176
1177 // return false if we don't have more than three unique points
1178 if (points.size() < 3) return false;
1179
1180 // intersection polygon is convex hull of above points
1182 assert(!intersection.empty());
1183
1184 return true;
1185 }
1186
1193 template<class P = Policy, std::enable_if_t<P::dimIntersection != 2, int> = 0>
1194 static bool intersection(const Geometry1& geo1, const Geometry2& geo2, Intersection& intersection)
1195 {
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");
1198 }
1199};
1200
1205template <class Geometry1, class Geometry2, class Policy>
1206class GeometryIntersection<Geometry1, Geometry2, Policy, 3, 2, 3>
1207: public GeometryIntersection<Geometry2, Geometry1, Policy, 3, 3, 2>
1208{
1210public:
1218 template<class P = Policy>
1219 static bool intersection(const Geometry1& geo1, const Geometry2& geo2, typename Base::Intersection& intersection)
1220 {
1221 return Base::intersection(geo2, geo1, intersection);
1222 }
1223};
1224
1229template <class Geometry1, class Geometry2, class Policy>
1230class GeometryIntersection<Geometry1, Geometry2, Policy, 3, 3, 3>
1231{
1232 enum { dimworld = 3 };
1233 enum { dim1 = 3 };
1234 enum { dim2 = 3 };
1235
1236public:
1237 using ctype = typename Policy::ctype;
1238 using Point = typename Policy::Point;
1239 using Intersection = typename Policy::Intersection;
1240
1241private:
1242 static constexpr ctype eps_ = 1.5e-7; // base epsilon for floating point comparisons
1243
1244public:
1252 template<class P = Policy, std::enable_if_t<P::dimIntersection == 3, int> = 0>
1253 static bool intersection(const Geometry1& geo1, const Geometry2& geo2, Intersection& intersection)
1254 {
1255 static_assert(
1256 int(dimworld) == int(Geometry2::coorddimension),
1257 "Can only collide geometries of same coordinate dimension"
1258 );
1259
1260 const auto refElement1 = referenceElement(geo1);
1261 const auto refElement2 = referenceElement(geo2);
1262
1263 // the candidate intersection points
1264 std::vector<Point> points;
1265 points.reserve(refElement1.size(2) + refElement2.size(2));
1266
1267 // add corners inside the other geometry
1268 const auto addPointIntersections = [&](const auto& g1, const auto& g2)
1269 {
1270 for (int i = 0; i < g1.corners(); ++i)
1271 if (const auto& c = g1.corner(i); intersectsPointGeometry(c, g2))
1272 points.emplace_back(c);
1273 };
1274
1275 addPointIntersections(geo1, geo2);
1276 addPointIntersections(geo2, geo1);
1277
1278 // get geometry types for the facets
1279 using PolyhedronFaceGeometry = Dune::MultiLinearGeometry<ctype, 2, dimworld, Detail::PolygonGeometryTraits<ctype>>;
1280 using SegGeometry = Dune::MultiLinearGeometry<ctype, 1, dimworld, Detail::SegmentGeometryTraits<ctype>>;
1281
1282 // intersection policy for point-like intersections (used later)
1284
1285 // add intersection points of all edges with the faces of the other polyhedron
1286 const auto addEdgeIntersections = [&](const auto& g1, const auto& g2, const auto& ref1, const auto& ref2)
1287 {
1288 for (int i = 0; i < ref1.size(1); ++i)
1289 {
1290 const auto faceGeo = [&]()
1291 {
1292 const auto localFaceGeo = ref1.template geometry<1>(i);
1293 if (localFaceGeo.corners() == 4)
1294 {
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));
1299
1300 return PolyhedronFaceGeometry(
1301 Dune::GeometryTypes::cube(2), Dune::ReservedVector<Point, 4>{a, b, c, d}
1302 );
1303 }
1304 else
1305 {
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));
1309
1310 return PolyhedronFaceGeometry(
1311 Dune::GeometryTypes::simplex(2), Dune::ReservedVector<Point, 4>{a, b, c}
1312 );
1313 }
1314 }();
1315
1316 for (int j = 0; j < ref2.size(2); ++j)
1317 {
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));
1321
1322 const auto segGeo = SegGeometry(Dune::GeometryTypes::line, std::array<Point, 2>{p, q});
1323
1325 typename PolySegTest::Intersection polySegIntersection;
1326 if (PolySegTest::intersection(faceGeo, segGeo, polySegIntersection))
1327 points.emplace_back(std::move(polySegIntersection));
1328 }
1329 }
1330 };
1331
1332 addEdgeIntersections(geo1, geo2, refElement1, refElement2);
1333 addEdgeIntersections(geo2, geo1, refElement2, refElement1);
1334
1335 // return if no intersection points were found
1336 if (points.empty())
1337 return false;
1338
1339 // remove duplicates
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
1344 {
1345 return (notEqual(a[0], b[0]) ? a[0] < b[0]
1346 : (notEqual(a[1], b[1]) ? a[1] < b[1]
1347 : (a[2] < b[2])));
1348 });
1349
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; }),
1354 points.end()
1355 );
1356
1357 // return false if we don't have more than four unique points (dim+1)
1358 if (points.size() < 4)
1359 return false;
1360
1361 // check if the points are coplanar (then we don't have a 3-dimensional intersection)
1362 const bool coplanar = [&]
1363 {
1364 const auto p0 = points[0];
1365 const auto normal = crossProduct(points[1] - p0, points[2] - p0);
1366 // include the normal in eps (instead of norm*norm) since the normal can become very small
1367 // if the first three points are very close together
1368 const auto epsCoplanar = normal.two_norm()*norm*eps_;
1369 for (int i = 3; i < points.size(); ++i)
1370 {
1371 const auto ad = points[i] - p0;
1372 using std::abs;
1373 if (abs(normal*ad) > epsCoplanar)
1374 return false;
1375 }
1376
1377 return true;
1378 }();
1379
1380 if (coplanar)
1381 return false;
1382
1383 // we return the intersection as point cloud (that can be triangulated later)
1384 intersection = points;
1385
1386 return true;
1387 }
1388
1395 template<class P = Policy, std::enable_if_t<P::dimIntersection != 3, int> = 0>
1396 static bool intersection(const Geometry1& geo1, const Geometry2& geo2, Intersection& intersection)
1397 {
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");
1400 }
1401};
1402
1407template <class Geometry1, class Geometry2, class Policy>
1408class GeometryIntersection<Geometry1, Geometry2, Policy, 3, 2, 1>
1409{
1410 enum { dimworld = 3 };
1411 enum { dim1 = 2 };
1412 enum { dim2 = 1 };
1413
1414public:
1415 using ctype = typename Policy::ctype;
1416 using Point = typename Policy::Point;
1417 using Intersection = typename Policy::Intersection;
1418
1419private:
1420 static constexpr ctype eps_ = 1.5e-7; // base epsilon for floating point comparisons
1421
1422public:
1432 template<class P = Policy, std::enable_if_t<P::dimIntersection == 1, int> = 0>
1433 static bool intersection(const Geometry1& geo1, const Geometry2& geo2, Intersection& intersection)
1434 {
1435 static_assert(int(dimworld) == int(Geometry2::coorddimension), "Can only collide geometries of same coordinate dimension");
1436
1437 ctype tfirst, tlast;
1438 if (intersect_(geo1, geo2, tfirst, tlast))
1439 {
1440 // the intersection exists. Export the intersections geometry now:
1441 // s(t) = a + t(b-a) in [tfirst, tlast]
1442 intersection = Intersection({geo2.global(tfirst), geo2.global(tlast)});
1443 return true;
1444 }
1445
1446 return false;
1447 }
1448
1458 template<class P = Policy, std::enable_if_t<P::dimIntersection == 0, int> = 0>
1459 static bool intersection(const Geometry1& geo1, const Geometry2& geo2, Intersection& is)
1460 {
1461 static_assert(int(dimworld) == int(Geometry2::coorddimension), "Can only collide geometries of same coordinate dimension");
1462
1463 const auto p = geo2.corner(0);
1464 const auto q = geo2.corner(1);
1465
1466 const auto a = geo1.corner(0);
1467 const auto b = geo1.corner(1);
1468 const auto c = geo1.corner(2);
1469
1470 if (geo1.corners() == 3)
1471 return intersect_<Policy>(a, b, c, p, q, is);
1472
1473 else if (geo1.corners() == 4)
1474 {
1475 Intersection is1, is2;
1476 bool hasSegment1, hasSegment2;
1477
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);
1481
1482 if (hasSegment1 || hasSegment2)
1483 return false;
1484
1485 if (intersects1) { is = is1; return true; }
1486 if (intersects2) { is = is2; return true; }
1487
1488 return false;
1489 }
1490
1491 else
1492 DUNE_THROW(Dune::NotImplemented, "Collision of segment and geometry of type "
1493 << geo1.type() << ", "<< geo1.corners() << " corners.");
1494 }
1495
1496private:
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)
1502 {
1503 // lambda to obtain the facet corners on geo1
1504 auto getFacetCorners = [] (const Geometry1& geo1)
1505 {
1506 Dune::ReservedVector<std::array<int, 2>, 4> facetCorners;
1507 switch (geo1.corners())
1508 {
1509 case 4: // quadrilateral
1510 facetCorners = {{0, 2}, {3, 1}, {1, 0}, {2, 3}};
1511 break;
1512 case 3: // triangle
1513 facetCorners = {{1, 0}, {0, 2}, {2, 1}};
1514 break;
1515 default:
1516 DUNE_THROW(Dune::NotImplemented, "Collision of segment and geometry of type "
1517 << geo1.type() << " with "<< geo1.corners() << " corners.");
1518 }
1519
1520 return facetCorners;
1521 };
1522
1523 const auto center1 = geo1.center();
1524 const auto normal1 = crossProduct(geo1.corner(1) - geo1.corner(0),
1525 geo1.corner(2) - geo1.corner(0));
1526
1527 // lambda to obtain the normal on a facet
1528 auto computeNormal = [&center1, &normal1, &geo1] (const std::array<int, 2>& facetCorners)
1529 {
1530 const auto c0 = geo1.corner(facetCorners[0]);
1531 const auto c1 = geo1.corner(facetCorners[1]);
1532 const auto edge = c1 - c0;
1533
1534 Dune::FieldVector<ctype, dimworld> n = crossProduct(edge, normal1);
1535 n /= n.two_norm();
1536
1537 // orientation of the element is not known
1538 // make sure normal points outwards of element
1539 if ( n*(center1-c0) > 0.0 )
1540 n *= -1.0;
1541
1542 return n;
1543 };
1544
1545 return Detail::computeSegmentIntersection(geo1, geo2, eps_, tfirst, tlast, getFacetCorners, computeNormal);
1546 }
1547
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,
1553 const Point& p, const Point& q,
1554 Intersection& is)
1555 {
1556 bool hasSegment;
1557 return intersect_(a, b, c, p, q, is, hasSegment);
1558 }
1559
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,
1566 const Point& p, const Point& q,
1567 Intersection& is, bool& hasSegmentIs)
1568 {
1569 hasSegmentIs = false;
1570
1571 const auto ab = b - a;
1572 const auto ac = c - a;
1573 const auto qp = p - q;
1574
1575 // compute the triangle normal that defines the triangle plane
1576 const auto normal = crossProduct(ab, ac);
1577
1578 // compute the denominator
1579 // if denom is 0 the segment is parallel and we can return
1580 const auto denom = normal*qp;
1581 const auto abnorm2 = ab.two_norm2();
1582 const auto eps = eps_*abnorm2*qp.two_norm();
1583 using std::abs;
1584 if (abs(denom) < eps)
1585 {
1586 const auto pa = a - p;
1587 const auto denom2 = normal*pa;
1588 if (abs(denom2) > eps_*pa.two_norm()*abnorm2)
1589 return false;
1590
1591 // if we get here, we are in-plane. Check if a
1592 // segment-like intersection with geometry 1 exists.
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;
1599
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))
1603 {
1604 hasSegmentIs = true;
1605 return false;
1606 }
1607
1608 // We are in-plane. A point-like
1609 // intersection can only be on the edges
1610 for (const auto& ip : {p, q})
1611 {
1612 if ( intersectsPointSimplex(ip, a, b)
1613 || intersectsPointSimplex(ip, b, c)
1614 || intersectsPointSimplex(ip, c, a) )
1615 {
1616 is = ip;
1617 return true;
1618 }
1619 }
1620
1621 return false;
1622 }
1623
1624 // compute intersection t value of pq with plane of triangle.
1625 // a segment intersects if and only if 0 <= t <= 1.
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;
1630
1631 // compute the barycentric coordinates and check if the intersection point
1632 // is inside the bounds of the triangle
1633 const auto e = crossProduct(qp, ap);
1634 const auto v = (ac*e)/denom;
1635 if (v < -eps_ || v > 1.0 + eps_) return false;
1636 const auto w = -(ab*e)/denom;
1637 if (w < -eps_ || v + w > 1.0 + eps_) return false;
1638
1639 // Now we are sure there is an intersection points
1640 // Perform delayed division compute the last barycentric coordinate component
1641 const auto u = 1.0 - v - w;
1642
1643 Point ip(0.0);
1644 ip.axpy(u, a);
1645 ip.axpy(v, b);
1646 ip.axpy(w, c);
1647 is = ip;
1648 return true;
1649 }
1650};
1651
1656template <class Geometry1, class Geometry2, class Policy>
1657class GeometryIntersection<Geometry1, Geometry2, Policy, 3, 1, 2>
1658: public GeometryIntersection<Geometry2, Geometry1, Policy, 3, 2, 1>
1659{
1661public:
1669 template<class P = Policy>
1670 static bool intersection(const Geometry1& geo1, const Geometry2& geo2, typename Base::Intersection& intersection)
1671 {
1672 return Base::intersection(geo2, geo1, intersection);
1673 }
1674};
1675
1680template <class Geometry1, class Geometry2, class Policy>
1681class GeometryIntersection<Geometry1, Geometry2, Policy, 3, 1, 1>
1682{
1683 enum { dimworld = 3 };
1684 enum { dim1 = 1 };
1685 enum { dim2 = 1 };
1686
1687public:
1688 using ctype = typename Policy::ctype;
1689 using Point = typename Policy::Point;
1690 using Intersection = typename Policy::Intersection;
1691
1692private:
1693 static constexpr ctype eps_ = 1.5e-7; // base epsilon for floating point comparisons
1694
1695public:
1702 template<class P = Policy, std::enable_if_t<P::dimIntersection == 0, int> = 0>
1703 static bool intersection(const Geometry1& geo1, const Geometry2& geo2, Intersection& intersection)
1704 {
1705 static_assert(int(dimworld) == int(Geometry2::coorddimension), "Can only collide geometries of same coordinate dimension");
1706
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);
1710
1711 const auto v1Norm2 = v1.two_norm2();
1712 const auto eps2 = eps_*v1Norm2;
1713
1714 const auto n = crossProduct(v1, v2);
1715
1716 // first check if segments are parallel
1717 using std::abs;
1718 if ( n.two_norm2() < eps2*v1Norm2 )
1719 {
1720 // check if they lie on the same line
1721 if (crossProduct(v1, ac).two_norm2() > eps2)
1722 return false;
1723
1724 // they lie on the same line,
1725 // if so, point intersection can only be one of the corners
1726 const auto sp = v1*v2;
1727 if ( sp < 0.0 )
1728 {
1729 if ( ac.two_norm2() < eps2 )
1730 { intersection = geo2.corner(0); return true; }
1731
1732 if ( (geo2.corner(1) - geo1.corner(1)).two_norm2() < eps2 )
1733 { intersection = geo2.corner(1); return true; }
1734 }
1735 else
1736 {
1737 if ( (geo2.corner(1) - geo1.corner(0)).two_norm2() < eps2 )
1738 { intersection = geo2.corner(1); return true; }
1739
1740 if ( (geo2.corner(0) - geo1.corner(1)).two_norm2() < eps2 )
1741 { intersection = geo2.corner(0); return true; }
1742 }
1743
1744 // no intersection
1745 return false;
1746 }
1747
1748 // in-plane normal vector on v1
1749 const auto v1Normal = crossProduct(v1, n);
1750
1751 // intersection point on v2 in local coords
1752 const auto t2 = - 1.0*(ac*v1Normal) / (v2*v1Normal);
1753
1754 // check if the local coords are valid
1755 if (t2 < -1.0*eps_ || t2 > 1.0 + eps_)
1756 return false;
1757
1758 if (auto ip = geo2.global(t2); intersectsPointGeometry(ip, geo1))
1759 { intersection = std::move(ip); return true; }
1760
1761 return false;
1762 }
1763
1771 template<class P = Policy, std::enable_if_t<P::dimIntersection == 1, int> = 0>
1772 static bool intersection(const Geometry1& geo1, const Geometry2& geo2, Intersection& intersection)
1773 {
1774 static_assert(int(dimworld) == int(Geometry2::coorddimension), "Can only collide geometries of same coordinate dimension");
1775
1776 const auto& a = geo1.corner(0);
1777 const auto& b = geo1.corner(1);
1778 const auto ab = b-a;
1779
1780 const auto& p = geo2.corner(0);
1781 const auto& q = geo2.corner(1);
1782 const auto pq = q-p;
1783
1784 const auto abNorm2 = ab.two_norm2();
1785 const auto pqNorm2 = pq.two_norm2();
1786
1787 using std::max;
1788 const auto eps2 = eps_*max(abNorm2, pqNorm2);
1789
1790 // if the segment are not parallel there is no segment intersection
1791 using std::abs;
1792 if (crossProduct(ab, pq).two_norm2() > eps2*eps2)
1793 return false;
1794
1795 const auto ap = (p-a);
1796 const auto aq = (q-a);
1797
1798 // points have to be colinear
1799 if (crossProduct(ap, aq).two_norm2() > eps2*eps2)
1800 return false;
1801
1802 // scaled local coordinates
1803 // we save the division for the normalization for now
1804 // and do it in the very end if we find an intersection
1805 auto tp = ap*ab;
1806 auto tq = aq*ab;
1807
1808 // make sure they are sorted
1809 using std::swap;
1810 if (tp > tq)
1811 swap(tp, tq);
1812
1813 using std::clamp;
1814 tp = clamp(tp, 0.0, abNorm2);
1815 tq = clamp(tq, 0.0, abNorm2);
1816
1817 if (abs(tp-tq) < eps2)
1818 return false;
1819
1820 intersection = Intersection({geo1.global(tp/abNorm2), geo1.global(tq/abNorm2)});
1821 return true;
1822 }
1823};
1824
1825} // end namespace Dumux
1826
1827#endif
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
Definition adapt.hh:17
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
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