Project
Loading...
Searching...
No Matches
Polygon.cxx
Go to the documentation of this file.
1// Copyright 2019-2020 CERN and copyright holders of ALICE O2.
2// See https://alice-o2.web.cern.ch/copyright for details of the copyright holders.
3// All rights not expressly granted are reserved.
4//
5// This software is distributed under the terms of the GNU General Public
6// License v3 (GPL Version 3), copied verbatim in the file "COPYING".
7//
8// In applying this license CERN does not waive the privileges and immunities
9// granted to it by virtue of its status as an Intergovernmental Organization
10// or submit itself to any jurisdiction.
11
14
15#define BOOST_TEST_MODULE Test MCHContour Polygon
16#define BOOST_TEST_MAIN
17#define BOOST_TEST_DYN_LINK
18
19#include <boost/test/unit_test.hpp>
20#include <iostream>
21#include "../include/MCHContour/Polygon.h"
22#include "../include/MCHContour/BBox.h"
23
24using namespace o2::mch::contour;
25
26struct POLYGONS {
28 : testPads{{{{0.0, 0.0}, {1.0, 0.0}, {1.0, 1.0}, {0.0, 1.0}, {0.0, 0.0}}},
29 {{{1.0, 3.0}, {2.0, 3.0}, {2.0, 4.0}, {1.0, 4.0}, {1.0, 3.0}}},
30 {{{1.0, 0.0}, {2.0, 0.0}, {2.0, 1.0}, {1.0, 1.0}, {1.0, 0.0}}},
31 {{{0.0, 1.0}, {1.0, 1.0}, {1.0, 2.0}, {0.0, 2.0}, {0.0, 1.0}}},
32 {{{1.0, 1.0}, {2.0, 1.0}, {2.0, 2.0}, {1.0, 2.0}, {1.0, 1.0}}},
33 {{{1.0, 2.0}, {2.0, 2.0}, {2.0, 3.0}, {1.0, 3.0}, {1.0, 2.0}}}}
34 {
35 }
36
37 std::vector<Polygon<double>> testPads;
39 // clang-format off
40 Polygon<double> testPolygon{{{0.1, 0.1},
41 {1.1, 0.1},
42 {1.1, 1.1},
43 {2.1, 1.1},
44 {2.1, 3.1},
45 {1.1, 3.1},
46 {1.1, 2.1},
47 {0.1, 2.1},
48 {0.1, 0.1}}};
49 // clang-format on
50 Polygon<int> counterClockwisePolygon{{0, 0}, {1, 0}, {1, 1}, {0, 1}, {0, 0}};
51 Polygon<int> clockwisePolygon{{0, 0}, {0, 1}, {1, 1}, {1, 0}, {0, 0}};
52 Polygon<double> clockwisePolygonDouble{{0, 0}, {0, 1}, {1, 1}, {1, 0}, {0, 0}};
53 Polygon<double> testPolygon2{
54 {{-5.0, 10.0}, {-5.0, -2.0}, {0.0, -2.0}, {0.0, -10.0}, {5.0, -10.0}, {5.0, 10.0}, {-5.0, 10.0}}};
55};
56
57BOOST_AUTO_TEST_SUITE(o2_mch_contour)
58
59BOOST_FIXTURE_TEST_SUITE(polygon, POLYGONS)
60
61BOOST_AUTO_TEST_CASE(CreateCounterClockwiseOrientedPolygon)
62{
63 BOOST_CHECK(counterClockwisePolygon.isCounterClockwiseOriented());
64}
65
66BOOST_AUTO_TEST_CASE(CreateClockwiseOrientedPolygon) { BOOST_CHECK(!clockwisePolygon.isCounterClockwiseOriented()); }
67
68BOOST_AUTO_TEST_CASE(SignedArea) { BOOST_CHECK_CLOSE(testPolygon.signedArea(), 4.0, 0.1); }
69
70BOOST_AUTO_TEST_CASE(AClosePolygonIsAPolygonWhereLastVertexIsTheSameAsFirstOne)
71{
72 Polygon<int> p{{0, 0}, {0, 1}, {1, 1}, {1, 0}, {0, 0}};
73 BOOST_CHECK(p.isClosed());
74}
75
76BOOST_AUTO_TEST_CASE(ClosingAClosedPolygonIsANop) { BOOST_CHECK(testPolygon == close(testPolygon)); }
77
79{
80 Polygon<int> opened{{0, 0}, {1, 0}, {1, 1}, {0, 1}};
81 Polygon<int> expected{{0, 0}, {1, 0}, {1, 1}, {0, 1}, {0, 0}};
82 auto closed = close(opened);
83 BOOST_TEST(expected == closed);
84}
85
86BOOST_AUTO_TEST_CASE(ThrowIfClosingAPolygonResultInANonManhattanPolygon)
87{
88 Polygon<int> triangle{{0, 0}, {1, 0}, {1, 1}};
89
90 BOOST_CHECK_THROW(close(triangle), std::logic_error);
91}
92
93BOOST_AUTO_TEST_CASE(AnOpenedPolygonCannotBeEqualToAClosedOneEvenWithSameSetOfVertices)
94{
95 Polygon<double> opened{
96 {0, 2},
97 {0, 0},
98 {2, 0},
99 {2, 4},
100 {1, 4},
101 {1, 2},
102 };
103
104 auto closed{close(opened)};
105
106 BOOST_CHECK(closed != opened);
107}
108
109BOOST_AUTO_TEST_CASE(PolygonAreEqualAsLongAsTheyContainTheSameVerticesIrrespectiveOfOrder)
110{
111 Polygon<double> a{{0, 2}, {0, 0}, {2, 0}, {2, 4}, {1, 4}, {1, 2}, {0, 2}};
112
113 Polygon<double> b{{2, 4}, {2, 0}, {1, 4}, {1, 2}, {0, 2}, {0, 0}, {2, 4}};
114
115 Polygon<double> c{{2, 4}, {2, 0}, {1, 4}, {1, 2}, {0, 2}, {1, 1}};
116
117 BOOST_CHECK(a == b);
118 BOOST_CHECK(a != c);
119}
120
121BOOST_AUTO_TEST_CASE(ContainsThrowsIfCalledOnNonClosedPolygon)
122{
123 Polygon<double> opened{{0, 0}, {1, 0}, {1, 1}, {0, 1}};
124 BOOST_CHECK_THROW(opened.contains(0, 0), std::invalid_argument);
125};
126
127BOOST_AUTO_TEST_CASE(ContainsReturnsTrueIfPointIsInsidePolygon)
128{
129 BOOST_CHECK_EQUAL(testPolygon2.contains(0, 0), true);
130 BOOST_CHECK_EQUAL(testPolygon2.contains(-4.999, -1.999), true);
131}
132
133BOOST_AUTO_TEST_CASE(ContainsReturnsFalseIfPointIsExactlyOnAPolygonEdge)
134{
135 BOOST_CHECK_EQUAL(testPolygon2.contains(-2.5, -2), false);
136}
137
139{
140 BBox<double> expected{-5.0, -10.0, 5.0, 10.0};
141 BOOST_TEST(getBBox(testPolygon2) == expected);
142}
143
145{
146 Polygon<double> p{{-80, -20}, {-70, -20}, {-70, -19.5}, {-80, -19.5}, {-80, -20}};
147
148 auto box = getBBox(p);
149 std::cout << box << "\n";
150 BOOST_CHECK_EQUAL(box.xcenter(), -75.0);
151 BOOST_CHECK_EQUAL(box.ycenter(), -19.75);
152}
153
154BOOST_AUTO_TEST_CASE(ConstructionByVectorIterators)
155{
156 std::vector<Vertex<int>> vertices{{0, 0}, {1, 0}, {1, 1}, {0, 1}, {0, 0}};
157
158 Polygon<int> p(vertices.begin(), vertices.end());
159
160 BOOST_CHECK_EQUAL(p, counterClockwisePolygon);
161}
162
163BOOST_AUTO_TEST_CASE(PointOutsidePolygonDistanceToPolygonClosestToOneSegment)
164{
165 BOOST_CHECK_EQUAL(squaredDistancePointToPolygon(Vertex<double>{-1.0, -6.0}, testPolygon2), 1.0);
166 BOOST_CHECK_EQUAL(squaredDistancePointToPolygon(Vertex<double>{3.0, -14.0}, testPolygon2), 16.0);
167}
168
169BOOST_AUTO_TEST_CASE(PointOutsidePolygonDistanceToPolygonClosestToOneSegmentEndPoint)
170{
171 BOOST_CHECK_EQUAL(squaredDistancePointToPolygon(Vertex<double>{-1.0, -14.0}, testPolygon2), 17.0);
172 BOOST_CHECK_EQUAL(squaredDistancePointToPolygon(Vertex<double>{7.0, -14.0}, testPolygon2), 20.0);
173}
174
175BOOST_AUTO_TEST_SUITE_END()
176BOOST_AUTO_TEST_SUITE_END()
BOOST_AUTO_TEST_CASE(CreateCounterClockwiseOrientedPolygon)
Definition Polygon.cxx:61
uint32_t c
Definition RawData.h:2
GLboolean GLboolean GLboolean b
Definition glcorearb.h:1233
GLboolean GLboolean GLboolean GLboolean a
Definition glcorearb.h:1233
GLsizei const GLint * box
Definition glcorearb.h:4697
auto squaredDistancePointToPolygon(const Vertex< T > &point, const Polygon< T > &polygon) -> decltype(point.x *point.x)
Definition Polygon.h:278
BBox< T > getBBox(const Contour< T > &contour)
Definition Contour.h:176
Polygon< T > close(Polygon< T > polygon)
Definition Polygon.h:126
std::vector< Polygon< double > > testPads
Definition Polygon.cxx:37
Polygon< double > polygon
Definition Polygon.cxx:38
std::map< std::string, ID > expected
BOOST_CHECK(tree)
BOOST_TEST(digits==digitsD, boost::test_tools::per_element())
BOOST_CHECK_EQUAL(triggersD.size(), triggers.size())