zxing-cpp v3.0
Loading...
Searching...
No Matches
Point.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 <algorithm>
9#include <cmath>
10#include <string>
11
12namespace ZXing {
13
19template <typename T>
20struct PointT
21{
22 using value_t = T;
23 T x = 0, y = 0;
24
25 constexpr PointT() = default;
26 constexpr PointT(T x, T y) : x(x), y(y) {}
27
28 template <typename U>
29 constexpr explicit PointT(const PointT<U>& p) : x(static_cast<T>(p.x)), y(static_cast<T>(p.y))
30 {}
31
32 template <typename U>
34 {
35 x += b.x;
36 y += b.y;
37 return *this;
38 }
39};
40
43
45
46template <typename T>
47bool operator==(const PointT<T>& a, const PointT<T>& b)
48{
49 return a.x == b.x && a.y == b.y;
50}
51
52template <typename T>
53bool operator!=(const PointT<T>& a, const PointT<T>& b)
54{
55 return !(a == b);
56}
57
58template <typename T>
59auto operator-(const PointT<T>& a) -> PointT<T>
60{
61 return {-a.x, -a.y};
62}
63
64template <typename T, typename U>
65auto operator+(const PointT<T>& a, const PointT<U>& b) -> PointT<decltype(a.x + b.x)>
66{
67 return {a.x + b.x, a.y + b.y};
68}
69
70template <typename T, typename U>
71auto operator-(const PointT<T>& a, const PointT<U>& b) -> PointT<decltype(a.x - b.x)>
72{
73 return {a.x - b.x, a.y - b.y};
74}
75
76template <typename T, typename U>
77auto operator*(const PointT<T>& a, const PointT<U>& b) -> PointT<decltype(a.x * b.x)>
78{
79 return {a.x * b.x, a.y * b.y};
80}
81
82template <typename T, typename U>
83PointT<T> operator*(U s, const PointT<T>& a)
84{
85 return {s * a.x, s * a.y};
86}
87
88template <typename T, typename U>
89PointT<T> operator/(const PointT<T>& a, U d)
90{
91 return {a.x / d, a.y / d};
92}
93
94template <typename T, typename U>
95auto dot(const PointT<T>& a, const PointT<U>& b) -> decltype (a.x * b.x)
96{
97 return a.x * b.x + a.y * b.y;
98}
99
100template <typename T>
101auto cross(PointT<T> a, PointT<T> b) -> decltype(a.x * b.x)
102{
103 return a.x * b.y - b.x * a.y;
104}
105
107template <typename T>
108T sumAbsComponent(PointT<T> p)
109{
110 return std::abs(p.x) + std::abs(p.y);
111}
112
114template <typename T>
115auto length(PointT<T> p) -> decltype(std::sqrt(dot(p, p)))
116{
117 return std::sqrt(dot(p, p));
118}
119
121template <typename T>
122T maxAbsComponent(PointT<T> p)
123{
124 return std::max(std::abs(p.x), std::abs(p.y));
125}
126
127template <typename T>
128auto distance(PointT<T> a, PointT<T> b) -> decltype(length(a - b))
129{
130 return length(a - b);
131}
132
133template <typename T>
134PointF normalized(PointT<T> d)
135{
136 return PointF(d) / length(PointF(d));
137}
138
139template <typename T>
140PointT<T> bresenhamDirection(PointT<T> d)
141{
142 return d / maxAbsComponent(d);
143}
144
145template <typename T>
146PointT<T> mainDirection(PointT<T> d)
147{
148 return std::abs(d.x) > std::abs(d.y) ? PointT<T>(d.x, 0) : PointT<T>(0, d.y);
149}
150
154inline PointF centered(PointI p)
155{
156 return p + PointF(0.5f, 0.5f);
157}
158
159inline PointF centered(PointF p)
160{
161 return {std::floor(p.x) + 0.5f, std::floor(p.y) + 0.5f};
162}
163
165
166template <typename T>
167std::string ToString(const PointT<T>& p, bool swap = false, char delim = 'x')
168{
169 return std::to_string(swap ? p.y : p.x) + delim + std::to_string(swap ? p.x : p.y);
170}
171
172} // ZXing
173
Definition Barcode.h:26
PointT< double > PointF
Definition Point.h:42
PointT< int > PointI
Definition Point.h:41
std::string ToString(BarcodeFormat format)
A simple 2D point class template.
Definition Point.h:21
T value_t
Definition Point.h:22
int y
Definition Point.h:23
constexpr PointT(const PointT< U > &p)
Definition Point.h:29
PointT & operator+=(const PointT< U > &b)
Definition Point.h:33
constexpr PointT()=default
int x
Definition Point.h:23
constexpr PointT(T x, T y)
Definition Point.h:26