1 #ifndef DUNE_POLYGONGRID_INTERSECTION_HH
2 #define DUNE_POLYGONGRID_INTERSECTION_HH
3 
4 #include <cassert>
5 #include <cstddef>
6 
7 #include <type_traits>
8 
9 #include <dune/common/exceptions.hh>
10 
11 #include <dune/geometry/type.hh>
12 
13 #include <dune/grid/common/intersection.hh>
14 #include <dune/grid/common/intersectioniterator.hh>
15 
16 #include <dune/polygongrid/declaration.hh>
17 #include <dune/polygongrid/entity.hh>
18 #include <dune/polygongrid/geometry.hh>
19 #include <dune/polygongrid/meshobjects.hh>
20 
21 namespace Dune
22 {
23 
24   namespace __PolygonGrid
25   {
26 
27     // Intersection
28     // ------------
29 
30     template< class Grid >
31     class Intersection
32     {
33       typedef Intersection< Grid > This;
34 
35       typedef HalfEdge< typename std::remove_const< Grid >::type::ctype > Item;
36 
37       typedef __PolygonGrid::Entity< 0, 2, Grid > EntityImpl;
38       typedef __PolygonGrid::Geometry< 1, 2, Grid > GeometryImpl;
39       typedef __PolygonGrid::LocalGeometry< 1, 2, Grid > LocalGeometryImpl;
40 
41     public:
42       static const int dimension = 2;
43       static const int codimension = 1;
44       static const int mydimension = dimension - codimension;
45 
46       typedef typename std::remove_const< Grid >::type::ctype ctype;
47 
48       typedef Dune::Entity< 0, 2, Grid, __PolygonGrid::Entity > Entity;
49       typedef Dune::Geometry< 1, 2, Grid, __PolygonGrid::Geometry > Geometry;
50       typedef Dune::Geometry< 1, 2, Grid, __PolygonGrid::LocalGeometry > LocalGeometry;
51       //typedef Geometry LocalGeometry;
52 
53       typedef typename Geometry::GlobalCoordinate GlobalCoordinate;
54       typedef typename Geometry::LocalCoordinate LocalCoordinate;
55 
56       Intersection () = default;
57 
Intersection(Item item)58       explicit Intersection ( Item item ) : item_( item ) {}
59 
equals(const This & other) const60       bool equals ( const This &other ) const { return (item_ == other.item_); }
61 
conforming() const62       bool conforming () const noexcept { return true; }
63 
boundary() const64       bool boundary () const noexcept { return !neighbor(); }
65 
neighbor() const66       bool neighbor () const noexcept { return item().neighbor().regular(); }
67 
boundaryId() const68       int boundaryId () const noexcept { return 1; }
69 
boundarySegmentIndex() const70       std::size_t boundarySegmentIndex () const noexcept { assert( boundary() ); return item().neighbor().boundaryIndex(); }
71 
inside() const72       Entity inside () const { return EntityImpl( item().cell() ); }
outside() const73       Entity outside () const { return EntityImpl( item().neighbor() ); }
74 
indexInInside() const75       int indexInInside () const { return item().indexInCell(); }
indexInOutside() const76       int indexInOutside () const { return item().indexInNeighbor(); }
77 
type() const78       GeometryType type () const noexcept { return GeometryTypes::cube( mydimension ); }
79 
geometry() const80       Geometry geometry () const { return Geometry( GeometryImpl( item() ) ); }
81 
geometryInInside() const82       LocalGeometry geometryInInside () const
83       {
84         const auto& geom = this->geometry();
85         const auto& elemGeo = this->inside().geometry();
86         const auto& c0 = elemGeo.local( geom.corner( 0 ) );
87         const auto& c1 = elemGeo.local( geom.corner( 1 ) );
88         return LocalGeometry( LocalGeometryImpl(c0 , c1) );
89         assert( false );
90         DUNE_THROW( InvalidStateException, "Intersection::geometryInOutside does not make for arbitrary polytopes." );
91       }
92 
geometryInOutside() const93       LocalGeometry geometryInOutside () const
94       {
95         const auto& geom = this->geometry();
96         const auto& elemGeo = this->outside().geometry();
97         const auto& c0 = elemGeo.local( geom.corner( 0 ) );
98         const auto& c1 = elemGeo.local( geom.corner( 1 ) );
99         return LocalGeometry( LocalGeometryImpl(c0 , c1) );
100         assert( false );
101         DUNE_THROW( InvalidStateException, "Intersection::geometryInOutside does not make for arbitrary polytopes." );
102       }
103 
integrationOuterNormal(const LocalCoordinate &) const104       GlobalCoordinate integrationOuterNormal ( const LocalCoordinate & ) const { return outerNormal(); }
outerNormal(const LocalCoordinate &) const105       GlobalCoordinate outerNormal ( const LocalCoordinate & ) const { return outerNormal(); }
106 
unitOuterNormal(const LocalCoordinate &) const107       GlobalCoordinate unitOuterNormal ( const LocalCoordinate & ) const { return centerUnitOuterNormal(); }
108 
centerUnitOuterNormal() const109       GlobalCoordinate centerUnitOuterNormal () const
110       {
111         GlobalCoordinate normal = outerNormal();
112         return normal *= ctype( 1 ) / normal.two_norm();
113       }
114 
item() const115       const Item &item () const { return item_; }
116 
117     private:
outerNormal() const118       GlobalCoordinate outerNormal () const
119       {
120         const GlobalCoordinate tangent = (item().target().position() - item().flip().target().position());
121         return GlobalCoordinate{ tangent[ 1 ], -tangent[ 0 ] };
122       }
123 
124       Item item_;
125     };
126 
127 
128 
129     // IntersectionIterator
130     // --------------------
131 
132     template< class Grid >
133     class IntersectionIterator
134     {
135       typedef IntersectionIterator< Grid > This;
136 
137       typedef __PolygonGrid::Intersection< Grid > IntersectionImpl;
138 
139     public:
140       typedef typename HalfEdges< typename std::remove_const< Grid >::type::ctype >::Iterator HalfEdgeIterator;
141 
142       typedef Dune::Intersection< const Grid, IntersectionImpl > Intersection;
143 
144       IntersectionIterator () = default;
145 
IntersectionIterator(const HalfEdgeIterator halfEdgeIterator)146       explicit IntersectionIterator ( const HalfEdgeIterator halfEdgeIterator ) : halfEdgeIterator_( halfEdgeIterator ) {}
147 
increment()148       void increment () { ++halfEdgeIterator_; }
149 
dereference() const150       Intersection dereference () const { return IntersectionImpl( *halfEdgeIterator_ ); }
151 
equals(const This & other) const152       bool equals ( const This &other ) const { return (halfEdgeIterator_ == other.halfEdgeIterator_); }
153 
154     private:
155       HalfEdgeIterator halfEdgeIterator_;
156     };
157 
158   } // namespace __PolygonGrid
159 
160 } // namespace Dune
161 
162 #endif // #ifndef DUNE_POLYGONGRID_INTERSECTION_HH
163