Project
Loading...
Searching...
No Matches
ContourCreator.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 ContourCreator
16#define BOOST_TEST_MAIN
17#define BOOST_TEST_DYN_LINK
18
19#include <boost/test/unit_test.hpp>
20#include <chrono>
21#include <iostream>
22#include "../include/MCHContour/ContourCreator.h"
23
24using namespace o2::mch::contour;
25using namespace o2::mch::contour::impl;
26
29 : testPads{{{{0.0, 0.0}, {1.0, 0.0}, {1.0, 1.0}, {0.0, 1.0}, {0.0, 0.0}}},
30 {{{1.0, 3.0}, {2.0, 3.0}, {2.0, 4.0}, {1.0, 4.0}, {1.0, 3.0}}},
31 {{{1.0, 0.0}, {2.0, 0.0}, {2.0, 1.0}, {1.0, 1.0}, {1.0, 0.0}}},
32 {{{0.0, 1.0}, {1.0, 1.0}, {1.0, 2.0}, {0.0, 2.0}, {0.0, 1.0}}},
33 {{{1.0, 1.0}, {2.0, 1.0}, {2.0, 2.0}, {1.0, 2.0}, {1.0, 1.0}}},
34 {{{1.0, 2.0}, {2.0, 2.0}, {2.0, 3.0}, {1.0, 3.0}, {1.0, 2.0}}}}
35 {
36 }
37
38 std::vector<Polygon<double>> testPads;
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 Polygon<int> counterClockwisePolygon{{0, 0}, {1, 0}, {1, 1}, {0, 1}, {0, 0}};
50 Polygon<int> clockwisePolygon{{0, 0}, {0, 1}, {1, 1}, {1, 0}, {0, 0}};
51 Polygon<double> clockwisePolygonDouble{{0, 0}, {0, 1}, {1, 1}, {1, 0}, {0, 0}};
52};
53
54BOOST_AUTO_TEST_SUITE(o2_mch_contour)
55
56BOOST_FIXTURE_TEST_SUITE(contourCreator, ContourCreatorPolygons)
57
58BOOST_AUTO_TEST_CASE(ContourCreationGeneratesEmptyContourForEmptyInput)
59{
60 std::vector<Polygon<double>> list;
61 auto contour = createContour(list);
62 BOOST_CHECK(contour.empty());
63}
64
65BOOST_AUTO_TEST_CASE(ContourCreationThrowsIfInputPolygonsAreNotCounterClockwiseOriented)
66{
67 std::vector<Polygon<double>> list;
68 list.push_back(clockwisePolygonDouble);
69 BOOST_CHECK_THROW(createContour(list), std::invalid_argument);
70}
71
72BOOST_AUTO_TEST_CASE(ContourCreationReturnsInputIfInputIsASinglePolygon)
73{
74 std::vector<Polygon<double>> list;
75 Polygon<double> onePolygon{{0, 0}, {1, 0}, {1, 1}, {0, 1}, {0, 0}};
76 list.push_back(onePolygon);
77 auto contour = createContour(list);
78 BOOST_REQUIRE(contour.size() == 1);
79 BOOST_CHECK_EQUAL(contour[0], onePolygon);
80}
81
82BOOST_AUTO_TEST_CASE(VerticalEdgeSortingMustSortSameAbcissaPointsLeftEdgeFirst)
83{
84 std::vector<VerticalEdge<double>> edges;
85 constexpr double sameX{42};
86 VerticalEdge<double> lastEdge{sameX + 1, 2, 0};
87 VerticalEdge<double> leftEdgeBottom{sameX, 2, 0};
88 VerticalEdge<double> leftEdgeTop{sameX, 10, 5};
89 VerticalEdge<double> rightEdge{sameX, 0, 2};
90
91 edges.push_back(lastEdge);
92 edges.push_back(rightEdge);
93 edges.push_back(leftEdgeTop);
94 edges.push_back(leftEdgeBottom);
95
96 sortVerticalEdges(edges);
97
98 BOOST_CHECK_EQUAL(edges[0], leftEdgeBottom);
99 BOOST_CHECK_EQUAL(edges[1], leftEdgeTop);
100 BOOST_CHECK_EQUAL(edges[2], rightEdge);
101 BOOST_CHECK_EQUAL(edges[3], lastEdge);
102}
103
104BOOST_AUTO_TEST_CASE(VerticalsToHorizontals)
105{
106 // clang-format off
107 std::vector<VerticalEdge<double>> testVerticals{{0.0, 7.0, 1.0}, {1.0, 1.0, 0.0}, {3.0, 0.0, 1.0},
108 {5.0, 1.0, 0.0}, {6.0, 0.0, 7.0}, {2.0, 5.0, 3.0},
109 {4.0, 3.0, 5.0}};
110 // clang-format on
111 std::vector<HorizontalEdge<double>> he{verticalsToHorizontals(testVerticals)};
112
113 std::vector<HorizontalEdge<double>> expected{{1, 0, 1}, {0, 1, 3}, {1, 3, 5}, {0, 5, 6}, {7, 6, 0}, {3, 2, 4}, {5, 4, 2}};
114
115 BOOST_CHECK(he == expected);
116}
117
118BOOST_AUTO_TEST_CASE(FinalizeContourThrowsIfNumberOfVerticalsDifferFromNumberOfHorizontals)
119{
120 std::vector<VerticalEdge<double>> v{{0, 1, 0}, {1, 0, 1}};
121 std::vector<HorizontalEdge<double>> h{{0, 0, 1}};
122 BOOST_CHECK_THROW(finalizeContour(v, h), std::invalid_argument);
123}
124
125BOOST_AUTO_TEST_CASE(FinalizeContourThrowsIfEndOfVerticalsDoNotMatchBeginOfHorizontals)
126{
127 std::vector<VerticalEdge<double>> v{{0, 7, 1}};
128 std::vector<HorizontalEdge<double>> wrong{{1, 2, 3}};
129 BOOST_CHECK_THROW(finalizeContour(v, wrong), std::invalid_argument);
130}
131
132BOOST_AUTO_TEST_CASE(FinalizeContourIEEEExample)
133{
134 // clang-format off
135 std::vector<VerticalEdge<double>> testVerticals{{0.0, 7.0, 1.0}, {1.0, 1.0, 0.0}, {3.0, 0.0, 1.0},
136 {5.0, 1.0, 0.0}, {6.0, 0.0, 7.0}, {2.0, 5.0, 3.0},
137 {4.0, 3.0, 5.0}};
138 // clang-format on
139 auto he{verticalsToHorizontals(testVerticals)};
140
141 auto contour = finalizeContour(testVerticals, he);
142
144 {{0, 7}, {0, 1}, {1, 1}, {1, 0}, {3, 0}, {3, 1}, {5, 1}, {5, 0}, {6, 0}, {6, 7}, {0, 7}},
145 {{2, 5}, {2, 3}, {4, 3}, {4, 5}, {2, 5}}};
146
147 BOOST_TEST(contour == expected);
148}
149
150BOOST_AUTO_TEST_CASE(FinalizeContourWithOneCommonVertex)
151{
152 std::vector<VerticalEdge<double>> ve{{0, 2, 0}, {1, 0, 2}, {1, 4, 2}, {2, 2, 4}};
153
154 auto he{verticalsToHorizontals(ve)};
155
156 auto contour = finalizeContour(ve, he);
157
158 Contour<double> expected{{{0, 2}, {0, 0}, {1, 0}, {1, 2}, {0, 2}},
159 {{1, 4}, {1, 2}, {2, 2}, {2, 4}, {1, 4}}};
160
161 BOOST_TEST(contour == expected);
162}
163
164BOOST_AUTO_TEST_CASE(CreateContourWithOneCommonVertex)
165{
166 std::vector<Polygon<double>> input{{{0, 0}, {1, 0}, {1, 1}, {0, 1}, {0, 0}},
167 {{0, 1}, {1, 1}, {1, 2}, {0, 2}, {0, 1}},
168 {{1, 2}, {2, 2}, {2, 3}, {1, 3}, {1, 2}},
169 {{1, 3}, {2, 3}, {2, 4}, {1, 4}, {1, 3}}};
170
171 auto contour = createContour(input);
172
173 Contour<double> expected{{{0, 2}, {0, 0}, {1, 0}, {1, 2}, {0, 2}},
174 {{1, 4}, {1, 2}, {2, 2}, {2, 4}, {1, 4}}};
175
176 BOOST_CHECK(contour == expected);
177}
178
179BOOST_AUTO_TEST_SUITE_END()
180
181BOOST_AUTO_TEST_SUITE_END()
BOOST_AUTO_TEST_CASE(ContourCreationGeneratesEmptyContourForEmptyInput)
Class for time synchronization of RawReader instances.
const GLdouble * v
Definition glcorearb.h:832
std::vector< HorizontalEdge< T > > verticalsToHorizontals(const std::vector< VerticalEdge< T > > &verticals)
Contour< T > finalizeContour(const std::vector< VerticalEdge< T > > &verticals, const std::vector< HorizontalEdge< T > > &horizontals)
void sortVerticalEdges(std::vector< VerticalEdge< T > > &edges)
Contour< T > createContour(const std::vector< Polygon< T > > &polygons)
Polygon< double > polygon
std::vector< Polygon< double > > testPads
Definition list.h:40
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())