zxing-cpp
v3.1
Toggle main menu visibility
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
12
namespace
ZXing
{
13
19
template
<
typename
T>
20
struct
PointT
21
{
22
using
value_t
= T;
23
T
x
= 0,
y
= 0;
24
25
constexpr
PointT
() =
default
;
26
constexpr
PointT
(T xx, T yy) :
x
(xx),
y
(yy) {}
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>
33
PointT
&
operator+=
(
const
PointT<U>
& b)
34
{
35
x
+= b.
x
;
36
y
+= b.
y
;
37
return
*
this
;
38
}
39
};
40
41
using
PointI
=
PointT<int>
;
42
using
PointF
=
PointT<double>
;
43
45
46
template
<
typename
T>
47
bool
operator==(
const
PointT<T>
& a,
const
PointT<T>
& b)
48
{
49
return
a.
x
== b.
x
&& a.
y
== b.
y
;
50
}
51
52
template
<
typename
T>
53
bool
operator!=(
const
PointT<T>& a,
const
PointT<T>& b)
54
{
55
return
!(a == b);
56
}
57
58
template
<
typename
T>
59
auto
operator-(
const
PointT<T>
& a) ->
PointT<T>
60
{
61
return
{-a.x, -a.y};
62
}
63
64
template
<
typename
T,
typename
U>
65
auto
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
70
template
<
typename
T,
typename
U>
71
auto
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
76
template
<
typename
T,
typename
U>
77
auto
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
82
template
<
typename
T,
typename
U>
83
PointT<T>
operator*(U s,
const
PointT<T>
& a)
84
{
85
return
{s * a.x, s * a.y};
86
}
87
88
template
<
typename
T,
typename
U>
89
PointT<T>
operator/(
const
PointT<T>
& a, U d)
90
{
91
return
{a.x / d, a.y / d};
92
}
93
94
template
<
typename
T,
typename
U>
95
auto
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
100
template
<
typename
T>
101
auto
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
107
template
<
typename
T>
108
T sumAbsComponent(
PointT<T>
p)
109
{
110
return
std::abs(p.x) + std::abs(p.y);
111
}
112
114
template
<
typename
T>
115
auto
length(
PointT<T>
p) ->
decltype
(std::sqrt(dot(p, p)))
116
{
117
return
std::sqrt(dot(p, p));
118
}
119
121
template
<
typename
T>
122
T maxAbsComponent(
PointT<T>
p)
123
{
124
return
std::max(std::abs(p.x), std::abs(p.y));
125
}
126
127
template
<
typename
T>
128
auto
distance(
PointT<T>
a,
PointT<T>
b) ->
decltype
(length(a - b))
129
{
130
return
length(a - b);
131
}
132
133
template
<
typename
T>
134
PointF
normalized(
PointT<T>
d)
135
{
136
return
PointF
(d) / length(
PointF
(d));
137
}
138
139
template
<
typename
T>
140
PointT<T>
bresenhamDirection(
PointT<T>
d)
141
{
142
return
d / maxAbsComponent(d);
143
}
144
145
template
<
typename
T>
146
PointT<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
151
template
<
typename
T>
152
PointT<T>
right(
PointT<T>
d)
153
{
154
return
{-d.y, d.x};
155
}
156
157
template
<
typename
T>
158
PointT<T>
left(
PointT<T>
d)
159
{
160
return
{d.y, -d.x};
161
}
162
166
inline
PointF
centered(
PointI
p)
167
{
168
return
p +
PointF
(0.5f, 0.5f);
169
}
170
171
inline
PointF
centered(
PointF
p)
172
{
173
return
{std::floor(p.x) + 0.5f, std::floor(p.y) + 0.5f};
174
}
175
177
178
template
<
typename
T>
179
std::string
ToString
(
const
PointT<T>
& p,
bool
swap =
false
,
char
delim =
'x'
)
180
{
181
return
std::to_string(swap ? p.
y
: p.
x
) + delim + std::to_string(swap ? p.
x
: p.
y
);
182
}
183
184
}
// ZXing
185
ZXing
Definition
Barcode.h:26
ZXing::PointF
PointT< double > PointF
Definition
Point.h:42
ZXing::PointI
PointT< int > PointI
Definition
Point.h:41
ZXing::ToString
std::string ToString(BarcodeFormat format)
ZXing::PointT
A simple 2D point class template.
Definition
Point.h:21
ZXing::PointT::PointT
constexpr PointT(T xx, T yy)
Definition
Point.h:26
ZXing::PointT::value_t
T value_t
Definition
Point.h:22
ZXing::PointT< int >::y
int y
Definition
Point.h:23
ZXing::PointT::PointT
constexpr PointT(const PointT< U > &p)
Definition
Point.h:29
ZXing::PointT::operator+=
PointT & operator+=(const PointT< U > &b)
Definition
Point.h:33
ZXing::PointT::PointT
constexpr PointT()=default
ZXing::PointT< int >::x
int x
Definition
Point.h:23
Point.h
Generated by
1.17.0