test_2d2d_intersection.cc 7.38 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
#include <config.h>

#include <iostream>

#include <dune/common/exceptions.hh>
#include <dune/common/fvector.hh>
#include <dune/geometry/multilineargeometry.hh>

#include <dumux/common/geometry/geometryintersection.hh>

#ifndef DOXYGEN
template<int dimWorld>
Dune::FieldVector<double, dimWorld>
makePosition( double x, double y )
{
    if (dimWorld == 2)
        return {x, y};
    else if (dimWorld == 3)
        return {x, y, 1.0};
    else
        DUNE_THROW(Dune::InvalidStateException, "Invalid dimworld");
}

template<int dimWorld>
Dune::MultiLinearGeometry<double, 2, dimWorld>
makeTriangle( Dune::FieldVector<double, dimWorld>&& a,
              Dune::FieldVector<double, dimWorld>&& b,
              Dune::FieldVector<double, dimWorld>&& c )
{
    using CornerStorage = std::vector<Dune::FieldVector<double, dimWorld>>;
    return { Dune::GeometryTypes::triangle,
             CornerStorage({std::move(a), std::move(b), std::move(c)}) };
}

template<int dimWorld>
Dune::MultiLinearGeometry<double, 2, dimWorld>
makeQuadrilateral( Dune::FieldVector<double, dimWorld>&& a,
                   Dune::FieldVector<double, dimWorld>&& b,
                   Dune::FieldVector<double, dimWorld>&& c,
                   Dune::FieldVector<double, dimWorld>&& d )
{
    using CornerStorage = std::vector<Dune::FieldVector<double, dimWorld>>;
    return { Dune::GeometryTypes::quadrilateral,
             CornerStorage({std::move(a), std::move(b), std::move(c), std::move(d)}) };
}

template<int dimworld, class Polygon1, class Polygon2>
bool testPolygonIntersection(const Polygon1& pol1,
                             const Polygon2& pol2,
                             bool expectIntersection)
{
    using Policy = Dumux::IntersectionPolicy::PolygonPolicy<double, dimworld>;
    using Algorithm = Dumux::GeometryIntersection<Polygon1, Polygon2, Policy>;

    typename Algorithm::IntersectionType intersection;
    const bool found = Algorithm::intersection(pol1, pol2, intersection);
    if (found && !expectIntersection)
        std::cout << "Found false positive!" << std::endl;
    else if (!found && expectIntersection)
        std::cout << "Could not find intersection!" << std::endl;
    else if (found && expectIntersection)
        std::cout << "Found intersection" << std::endl;
    else
        std::cout << "No intersection" << std::endl;

    return (found == expectIntersection);
}

template<int dimWorld>
void testPolygonIntersections(std::vector<bool>& returns)
{
    for (auto scaling : {1.0, 1e3, 1e12, 1e-12})
    {
        std::cout << "Test with scaling " << scaling << std::endl;
        const auto tria1 = makeTriangle( makePosition<dimWorld>(0.0, 0.0),
                                         makePosition<dimWorld>(0.0, 1.0*scaling),
                                         makePosition<dimWorld>(1.0*scaling, 1.0*scaling) );
        const auto tria2 = makeTriangle( makePosition<dimWorld>(1.0*scaling, 0.0),
                                         makePosition<dimWorld>(0.0, 0.0),
                                         makePosition<dimWorld>(0.0, 1.0*scaling) );
        const auto tria3 = makeTriangle( makePosition<dimWorld>(1.0*scaling, 0.0),
                                         makePosition<dimWorld>(0.0, 0.5*scaling),
                                         makePosition<dimWorld>(1.0*scaling, 1.0*scaling) );
        const auto tria4 = makeTriangle( makePosition<dimWorld>(1.0*scaling, 0.0),
                                         makePosition<dimWorld>(1.0*scaling, 1.0*scaling),
                                         makePosition<dimWorld>(2.0*scaling, 1.0*scaling) );
        const auto tria5 = makeTriangle( makePosition<dimWorld>(1.0*scaling, 0.0),
                                         makePosition<dimWorld>(0.5*scaling, 0.5*scaling),
                                         makePosition<dimWorld>(2.0*scaling, 1.0*scaling) );
        const auto tria6 = makeTriangle( makePosition<dimWorld>(1.0*scaling, 0.0),
                                         makePosition<dimWorld>(0.501*scaling, 0.501*scaling),
                                         makePosition<dimWorld>(2.0*scaling, 1.0*scaling) );
        const auto tria7 = makeTriangle( makePosition<dimWorld>(1.0*scaling, 0.0),
                                         makePosition<dimWorld>(0.499*scaling, 0.501*scaling),
                                         makePosition<dimWorld>(2.0*scaling, 1.0*scaling) );

        returns.push_back(testPolygonIntersection<dimWorld>(tria1, tria2, true));
        returns.push_back(testPolygonIntersection<dimWorld>(tria1, tria3, true));
        returns.push_back(testPolygonIntersection<dimWorld>(tria1, tria4, false)); // touches in point
        returns.push_back(testPolygonIntersection<dimWorld>(tria1, tria5, false)); // touches in edge
        returns.push_back(testPolygonIntersection<dimWorld>(tria1, tria6, false)); // little bit outside triangle
        returns.push_back(testPolygonIntersection<dimWorld>(tria1, tria7, true));  // little bit inside triangle

        // test some quadrilaterals
        const auto quad1 = makeQuadrilateral( makePosition<dimWorld>(0.0, 0.0),
                                              makePosition<dimWorld>(1.0*scaling, 0.0),
                                              makePosition<dimWorld>(0.0, 1.0*scaling),
                                              makePosition<dimWorld>(1.0*scaling, 1.0*scaling) );
        const auto quad2 = makeQuadrilateral( makePosition<dimWorld>(1.0*scaling, 0.0),
                                              makePosition<dimWorld>(2.0*scaling, 0.0),
                                              makePosition<dimWorld>(1.0*scaling, 2.0*scaling),
                                              makePosition<dimWorld>(2.0*scaling, 2.0*scaling) );
        const auto quad3 = makeQuadrilateral( makePosition<dimWorld>(-1.0*scaling, 0.0),
                                              makePosition<dimWorld>(0.0, 0.0),
                                              makePosition<dimWorld>(-1.0*scaling, 1.0*scaling),
                                              makePosition<dimWorld>(0.0, 1.0*scaling) );
        const auto quad4 = makeQuadrilateral( makePosition<dimWorld>(0.5*scaling, 0.0),
                                              makePosition<dimWorld>(0.5*scaling, 0.501*scaling),
                                              makePosition<dimWorld>(1.0*scaling, 0.0),
                                              makePosition<dimWorld>(1.0*scaling, 1.0*scaling) );

        returns.push_back(testPolygonIntersection<dimWorld>(tria1, quad1, true));
        returns.push_back(testPolygonIntersection<dimWorld>(tria1, quad2, false)); // touches in point
        returns.push_back(testPolygonIntersection<dimWorld>(tria1, quad3, false)); // touches in edge
        returns.push_back(testPolygonIntersection<dimWorld>(tria1, quad4, true)); // small overlap region

        std::cout << std::endl;
    }
}

#endif

int main(int argc, char* argv[]) try
{
    std::vector<bool> returns;
    testPolygonIntersections<2>(returns);

    // TODO: implement and test intersections in 3d
    // TODO: implement and test point and segment intersections

    // determine the exit code
    if (std::any_of(returns.begin(), returns.end(), [](bool i){ return !i; }))
        return 1;

    std::cout << "All tests passed!" << std::endl;

    return 0;
}
// //////////////////////////////////
//   Error handler
// /////////////////////////////////
catch (const Dune::Exception& e) {
    std::cout << e << std::endl;
    return 1;
}