zxing-cpp v3.2
Loading...
Searching...
No Matches
Quadrilateral.h
Go to the documentation of this file.
1/*
2* Copyright 2020 Axel Waggershauser
3*/
4// SPDX-License-Identifier: Apache-2.0
5
6#pragma once
7
8#include "Point.h"
9
10#ifdef ZXING_INTERNAL
11#include "ZXAlgorithms.h"
12#endif
13
14#include <array>
15#include <cmath>
16#include <string>
17
18namespace ZXing {
19
28template <typename T>
29class Quadrilateral : public std::array<T, 4>
30{
31 using Base = std::array<T, 4>;
32 using Base::at;
33public:
34 using Point = T;
35
36 Quadrilateral() = default;
37 Quadrilateral(T tl, T tr, T br, T bl) : Base{tl, tr, br, bl} {}
38 template <typename U>
40 : Quadrilateral(Point(tl), Point(tr), Point(br), Point(bl))
41 {}
42
43 constexpr Point topLeft() const noexcept { return at(0); }
44 constexpr Point topRight() const noexcept { return at(1); }
45 constexpr Point bottomRight() const noexcept { return at(2); }
46 constexpr Point bottomLeft() const noexcept { return at(3); }
47
50 double rotation() const
51 {
52 auto centerLine = (topRight() + bottomRight()) - (topLeft() + bottomLeft());
53 if (centerLine == Point{})
54 return 0.;
55 auto centerLineF = normalized(centerLine);
56 return std::atan2(centerLineF.y, centerLineF.x);
57 }
58
60 [[deprecated("Use rotation() instead")]] double orientation() const { return rotation(); } // deprecated in v3.2
62};
63
66
67template <typename T>
68std::string ToString(const Quadrilateral<PointT<T>>& points)
69{
70 std::string res;
71 for (const auto& p : points)
72 res += std::to_string(p.x) + "x" + std::to_string(p.y) + (&p == &points.back() ? "" : " ");
73 return res;
74}
75
76#ifdef ZXING_INTERNAL
77
78template <typename PointT = PointF>
79Quadrilateral<PointT> Rectangle(int width, int height, typename PointT::value_t margin = 0)
80{
81 return {
82 PointT{margin, margin}, {width - margin, margin}, {width - margin, height - margin}, {margin, height - margin}};
83}
84
85template <typename PointT = PointF>
86Quadrilateral<PointT> Rectangle(int x0, int x1, int y0, int y1, typename PointT::value_t o)
87{
88 return {PointT{x0 + o, y0 + o}, {x1 + o, y0 + o}, {x1 + o, y1 + o}, {x0 + o, y1 + o}};
89}
90
91template <typename PointT = PointF>
92Quadrilateral<PointT> Rectangle(int left, int top, int width, int height)
93{
94 int right = left + width - 1;
95 int bottom = top + height - 1;
96
97 return {PointT{left, top}, {right, top}, {right, bottom}, {left, bottom}};
98}
99
100template <typename PointT = PointF>
101Quadrilateral<PointT> CenteredSquare(int size)
102{
103 return Scale(Quadrilateral(PointT{-1, -1}, {1, -1}, {1, 1}, {-1, 1}), size / 2);
104}
105
106template <typename PointT = PointI>
107Quadrilateral<PointT> Line(int y, int xStart, int xStop)
108{
109 return {PointT{xStart, y}, {xStop, y}, {xStop, y}, {xStart, y}};
110}
111
112template <typename PointT>
113bool IsConvex(const Quadrilateral<PointT>& poly)
114{
115 const int N = Size(poly);
116 bool sign = false;
117
118 typename PointT::value_t m = INFINITY, M = 0;
119
120 for(int i = 0; i < N; i++)
121 {
122 auto d1 = poly[(i + 2) % N] - poly[(i + 1) % N];
123 auto d2 = poly[i] - poly[(i + 1) % N];
124 auto cp = cross(d1, d2);
125
126 // TODO: see if the isInside check for all boundary points in GridSampler is still required after fixing the wrong fabs()
127 // application in the following line
128 UpdateMinMax(m, M, std::fabs(cp));
129
130 if (i == 0)
131 sign = cp > 0;
132 else if (sign != (cp > 0))
133 return false;
134 }
135
136 // It turns out being convex is not enough to prevent a "numerical instability"
137 // that can cause the corners being projected inside the image boundaries but
138 // some points near the corners being projected outside. This has been observed
139 // where one corner is almost in line with two others. The M/m ratio is below 2
140 // for the complete existing sample set. For very "skewed" QRCodes a value of
141 // around 3 is realistic. A value of 14 has been observed to trigger the
142 // instability.
143 return M / m < 4.0;
144}
145
146template <typename PointT>
147Quadrilateral<PointT> Scale(const Quadrilateral<PointT>& q, int factor)
148{
149 return {factor * q[0], factor * q[1], factor * q[2], factor * q[3]};
150}
151
152template <typename PointT>
154{
155 return {q[0] + offset, q[1] + offset, q[2] + offset, q[3] + offset};
156}
157
158template <typename PointT>
159PointT Center(const Quadrilateral<PointT>& q)
160{
161 return Reduce(q) / Size(q);
162}
163
164template <typename PointT>
165Quadrilateral<PointT> RotatedCorners(const Quadrilateral<PointT>& q, int n = 1, bool mirror = false)
166{
168 std::rotate_copy(q.begin(), q.begin() + ((n + 4) % 4), q.end(), res.begin());
169 if (mirror)
170 std::swap(res[1], res[3]);
171 return res;
172}
173
174template <typename PointT>
175bool IsInside(const PointT& p, const Quadrilateral<PointT>& q)
176{
177 // Test if p is on the same side (right or left) of all polygon segments
178 int pos = 0, neg = 0;
179 for (int i = 0; i < Size(q); ++i)
180 (cross(p - q[i], q[(i + 1) % Size(q)] - q[i]) < 0 ? neg : pos)++;
181 return pos == 0 || neg == 0;
182}
183
184template <typename PointT>
186{
187 auto [minX, maxX] = std::minmax({q[0].x, q[1].x, q[2].x, q[3].x});
188 auto [minY, maxY] = std::minmax({q[0].y, q[1].y, q[2].y, q[3].y});
189 return {PointT{minX, minY}, {maxX, minY}, {maxX, maxY}, {minX, maxY}};
190}
191
192template <typename PointT>
193bool HaveIntersectingBoundingBoxes(const Quadrilateral<PointT>& a, const Quadrilateral<PointT>& b)
194{
195 auto bba = BoundingBox(a), bbb = BoundingBox(b);
196
197 bool x = bbb.topRight().x < bba.topLeft().x || bbb.topLeft().x > bba.topRight().x;
198 bool y = bbb.bottomLeft().y < bba.topLeft().y || bbb.topLeft().y > bba.bottomLeft().y;
199 return !(x || y);
200}
201
202template <typename PointT>
204{
205 auto dist2First = [r = a[0]](auto s, auto t) { return distance(s, r) < distance(t, r); };
206 // rotate points such that the the two topLeft points are closest to each other
207 auto offset = std::min_element(b.begin(), b.end(), dist2First) - b.begin();
208
210 for (int i = 0; i < 4; ++i)
211 res[i] = (a[i] + b[(i + offset) % 4]) / 2;
212
213 return res;
214}
215
216#endif
217
218} // ZXing
219
A simple class representing a quadrilateral defined by its four corner points.
Definition Quadrilateral.h:30
constexpr Point topRight() const noexcept
Definition Quadrilateral.h:44
T Point
Definition Quadrilateral.h:34
Quadrilateral(T tl, T tr, T br, T bl)
Definition Quadrilateral.h:37
constexpr Point bottomLeft() const noexcept
Definition Quadrilateral.h:46
double rotation() const
Definition Quadrilateral.h:50
constexpr Point bottomRight() const noexcept
Definition Quadrilateral.h:45
constexpr Point topLeft() const noexcept
Definition Quadrilateral.h:43
Quadrilateral(PointT< U > tl, PointT< U > tr, PointT< U > br, PointT< U > bl)
Definition Quadrilateral.h:39
Definition Barcode.h:26
Quadrilateral< PointI > QuadrilateralI
Definition Quadrilateral.h:65
Quadrilateral< PointF > QuadrilateralF
Definition Quadrilateral.h:64
std::string ToString(BarcodeFormat format)
A simple 2D point class template.
Definition Point.h:21
T value_t
Definition Point.h:22