Project
Loading...
Searching...
No Matches
Contour.h
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#ifndef O2_MCH_CONTOUR_H
16#define O2_MCH_CONTOUR_H
17
18#include "Polygon.h"
19#include <vector>
20#include <cassert>
21#include <initializer_list>
22
23namespace o2
24{
25namespace mch
26{
27namespace contour
28{
29
30template <typename T>
31std::vector<o2::mch::contour::Vertex<T>> getVertices(const std::vector<o2::mch::contour::Polygon<T>>& polygons)
32{
33 std::vector<o2::mch::contour::Vertex<T>> vertices;
34
35 for (const auto& p : polygons) {
36 auto pv = getVertices(p);
37 vertices.insert(vertices.end(), pv.begin(), p.isClosed() ? pv.end() - 1 : pv.end());
38 }
39
40 return vertices;
41}
42
43template <typename T>
44std::vector<o2::mch::contour::Vertex<T>> getSortedVertices(const std::vector<o2::mch::contour::Polygon<T>>& polygons)
45{
46 std::vector<Vertex<T>> vertices{getVertices(polygons)};
47 std::sort(vertices.begin(), vertices.end());
48 return vertices;
49}
50
51template <typename T>
52bool isCounterClockwiseOriented(const std::vector<T>& polygons)
53{
54 for (const auto& p : polygons) {
55 if (!p.isCounterClockwiseOriented()) {
56 return false;
57 }
58 }
59 return true;
60}
61
62template <typename T>
64{
65 public:
66 using size_type = typename std::vector<o2::mch::contour::Polygon<T>>::size_type;
67
68 Contour() = default;
69
70 Contour(std::initializer_list<o2::mch::contour::Polygon<T>> args) : mPolygons(args) {}
71
72 size_type size() const { return mPolygons.size(); }
73
74 o2::mch::contour::Polygon<T> operator[](int i) const { return mPolygons[i]; }
75
76 bool empty() const { return size() == 0; }
77
79 {
80 mPolygons.push_back(polygon);
81 return *this;
82 }
83
84 bool contains(T x, T y) const
85 {
86 for (const auto& p : mPolygons) {
87 if (p.contains(x, y)) {
88 return true;
89 }
90 }
91 return false;
92 }
93
94 bool isClosed() const
95 {
96 for (const auto& p : mPolygons) {
97 if (!p.isClosed()) {
98 return false;
99 }
100 }
101 return true;
102 }
103
105
106 std::vector<o2::mch::contour::Vertex<T>> getVertices() const { return o2::mch::contour::getVertices(mPolygons); }
107
108 std::vector<o2::mch::contour::Vertex<T>> getSortedVertices() const
109 {
110 return o2::mch::contour::getSortedVertices(mPolygons);
111 }
112
113 std::vector<o2::mch::contour::Polygon<T>> getPolygons() const { return mPolygons; }
114
115 friend std::ostream& operator<<(std::ostream& os, const Contour<T>& contour)
116 {
117 os << contour.mPolygons;
118 return os;
119 }
120
121 private:
122 std::vector<o2::mch::contour::Polygon<T>> mPolygons;
123};
124
125template <typename T>
126bool operator!=(const Contour<T>& lhs, const Contour<T>& rhs)
127{
128 return !(rhs == lhs);
129}
130
135template <typename T>
136bool operator==(const Contour<T>& lhs, const Contour<T>& rhs)
137{
138 std::vector<Vertex<T>> vl{lhs.getSortedVertices()};
139 std::vector<Vertex<T>> vr{rhs.getSortedVertices()};
140
141 if (vl.size() != vr.size()) {
142 return false;
143 }
144 for (auto i = 0; i < vl.size(); ++i) {
145 if (vl[i] != vr[i]) {
146 return false;
147 }
148 }
149 return true;
150}
151
152template <typename T>
153std::ostream& operator<<(std::ostream& os, const std::vector<o2::mch::contour::Polygon<T>>& polygons)
154{
155 os << "MULTIPOLYGON(";
156
157 for (auto j = 0; j < polygons.size(); ++j) {
158 const Polygon<T>& p{polygons[j]};
159 os << '(';
160 for (auto i = 0; i < p.size(); ++i) {
161 os << p[i].x << " " << p[i].y;
162 if (i < p.size() - 1) {
163 os << ',';
164 }
165 }
166 os << ')';
167 if (j < polygons.size() - 1) {
168 os << ',';
169 }
170 }
171 os << ')';
172 return os;
173}
174
175template <typename T>
177{
178 return getBBox(contour.getVertices());
179}
180} // namespace contour
181} // namespace mch
182} // namespace o2
183
184#endif
int32_t i
uint32_t j
Definition RawData.h:0
bool isClosed() const
Definition Contour.h:94
bool contains(T x, T y) const
Definition Contour.h:84
typename std::vector< o2::mch::contour::Polygon< T > >::size_type size_type
Definition Contour.h:66
Contour< T > & addPolygon(const Polygon< T > &polygon)
Definition Contour.h:78
friend std::ostream & operator<<(std::ostream &os, const Contour< T > &contour)
Definition Contour.h:115
bool isCounterClockwiseOriented() const
Definition Contour.h:104
size_type size() const
Definition Contour.h:72
std::vector< o2::mch::contour::Polygon< T > > getPolygons() const
Definition Contour.h:113
std::vector< o2::mch::contour::Vertex< T > > getVertices() const
Definition Contour.h:106
o2::mch::contour::Polygon< T > operator[](int i) const
Definition Contour.h:74
Contour(std::initializer_list< o2::mch::contour::Polygon< T > > args)
Definition Contour.h:70
std::vector< o2::mch::contour::Vertex< T > > getSortedVertices() const
Definition Contour.h:108
GLint GLenum GLint x
Definition glcorearb.h:403
std::vector< o2::mch::contour::Vertex< T > > getVertices(const std::vector< o2::mch::contour::Polygon< T > > &polygons)
Definition Contour.h:31
bool isCounterClockwiseOriented(const std::vector< T > &polygons)
Definition Contour.h:52
bool operator!=(const Contour< T > &lhs, const Contour< T > &rhs)
Definition Contour.h:126
BBox< T > getBBox(const Contour< T > &contour)
Definition Contour.h:176
bool operator==(const Contour< T > &lhs, const Contour< T > &rhs)
Definition Contour.h:136
std::vector< o2::mch::contour::Vertex< T > > getSortedVertices(const std::vector< o2::mch::contour::Polygon< T > > &polygons)
Definition Contour.h:44
std::ostream & operator<<(std::ostream &os, const std::vector< o2::mch::contour::Polygon< T > > &polygons)
Definition Contour.h:153
a couple of static helper functions to create timestamp values for CCDB queries or override obsolete ...