test_2d2d_intersection.cc 7.37 KB
 Dennis Gläser committed Jun 17, 2019 1 2 3 4 5 6 7 8 ``````#include #include #include #include #include `````` Ned Coltman committed Jul 29, 2020 9 ``````#include `````` Dennis Gläser committed Jun 17, 2019 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 `````` #ifndef DOXYGEN template Dune::FieldVector 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 Dune::MultiLinearGeometry makeTriangle( Dune::FieldVector&& a, Dune::FieldVector&& b, Dune::FieldVector&& c ) { using CornerStorage = std::vector>; return { Dune::GeometryTypes::triangle, CornerStorage({std::move(a), std::move(b), std::move(c)}) }; } template Dune::MultiLinearGeometry makeQuadrilateral( Dune::FieldVector&& a, Dune::FieldVector&& b, Dune::FieldVector&& c, Dune::FieldVector&& d ) { using CornerStorage = std::vector>; return { Dune::GeometryTypes::quadrilateral, CornerStorage({std::move(a), std::move(b), std::move(c), std::move(d)}) }; } template bool testPolygonIntersection(const Polygon1& pol1, const Polygon2& pol2, bool expectIntersection) { using Policy = Dumux::IntersectionPolicy::PolygonPolicy; using Algorithm = Dumux::GeometryIntersection; `````` Dennis Gläser committed Mar 14, 2020 55 `````` typename Algorithm::Intersection intersection; `````` Dennis Gläser committed Jun 17, 2019 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 `````` 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 void testPolygonIntersections(std::vector& returns) { for (auto scaling : {1.0, 1e3, 1e12, 1e-12}) { std::cout << "Test with scaling " << scaling << std::endl; const auto tria1 = makeTriangle( makePosition(0.0, 0.0), makePosition(0.0, 1.0*scaling), makePosition(1.0*scaling, 1.0*scaling) ); const auto tria2 = makeTriangle( makePosition(1.0*scaling, 0.0), makePosition(0.0, 0.0), makePosition(0.0, 1.0*scaling) ); const auto tria3 = makeTriangle( makePosition(1.0*scaling, 0.0), makePosition(0.0, 0.5*scaling), makePosition(1.0*scaling, 1.0*scaling) ); const auto tria4 = makeTriangle( makePosition(1.0*scaling, 0.0), makePosition(1.0*scaling, 1.0*scaling), makePosition(2.0*scaling, 1.0*scaling) ); const auto tria5 = makeTriangle( makePosition(1.0*scaling, 0.0), makePosition(0.5*scaling, 0.5*scaling), makePosition(2.0*scaling, 1.0*scaling) ); const auto tria6 = makeTriangle( makePosition(1.0*scaling, 0.0), makePosition(0.501*scaling, 0.501*scaling), makePosition(2.0*scaling, 1.0*scaling) ); const auto tria7 = makeTriangle( makePosition(1.0*scaling, 0.0), makePosition(0.499*scaling, 0.501*scaling), makePosition(2.0*scaling, 1.0*scaling) ); returns.push_back(testPolygonIntersection(tria1, tria2, true)); returns.push_back(testPolygonIntersection(tria1, tria3, true)); returns.push_back(testPolygonIntersection(tria1, tria4, false)); // touches in point returns.push_back(testPolygonIntersection(tria1, tria5, false)); // touches in edge returns.push_back(testPolygonIntersection(tria1, tria6, false)); // little bit outside triangle returns.push_back(testPolygonIntersection(tria1, tria7, true)); // little bit inside triangle // test some quadrilaterals const auto quad1 = makeQuadrilateral( makePosition(0.0, 0.0), makePosition(1.0*scaling, 0.0), makePosition(0.0, 1.0*scaling), makePosition(1.0*scaling, 1.0*scaling) ); const auto quad2 = makeQuadrilateral( makePosition(1.0*scaling, 0.0), makePosition(2.0*scaling, 0.0), makePosition(1.0*scaling, 2.0*scaling), makePosition(2.0*scaling, 2.0*scaling) ); const auto quad3 = makeQuadrilateral( makePosition(-1.0*scaling, 0.0), makePosition(0.0, 0.0), makePosition(-1.0*scaling, 1.0*scaling), makePosition(0.0, 1.0*scaling) ); const auto quad4 = makeQuadrilateral( makePosition(0.5*scaling, 0.0), makePosition(0.5*scaling, 0.501*scaling), makePosition(1.0*scaling, 0.0), makePosition(1.0*scaling, 1.0*scaling) ); returns.push_back(testPolygonIntersection(tria1, quad1, true)); returns.push_back(testPolygonIntersection(tria1, quad2, false)); // touches in point returns.push_back(testPolygonIntersection(tria1, quad3, false)); // touches in edge returns.push_back(testPolygonIntersection(tria1, quad4, true)); // small overlap region std::cout << std::endl; } } #endif int main(int argc, char* argv[]) try { std::vector 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; }``````