-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathtypes.h
167 lines (138 loc) · 3.52 KB
/
types.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
/*
* stair-step-detector
* Copyright (c) 2021 Peter Nebe (mail@peter-nebe.dev)
*
* This file is part of stair-step-detector.
*
* stair-step-detector is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* stair-step-detector is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with stair-step-detector. If not, see <https://www.gnu.org/licenses/>.
*/
#ifndef TYPES_H_
#define TYPES_H_
#include <vector>
#include <array>
namespace stairs
{
template<class T>
struct Point2_
{
T x, y;
};
using Point2i = Point2_<int>;
using Point2f = Point2_<float>;
struct Point3f
{
float x, y, z;
};
using Coordinate_t = double;
template<int Dim>
struct Point_;
using Point2 = Point_<2>;
using Point3 = Point_<3>;
template<>
struct Point_<2>
{
Coordinate_t x, y;
Point_();
Point_(Coordinate_t x, Coordinate_t y);
Point_(const Point3 &p);
bool operator!=(const Point2 &other) const;
};
template<>
struct Point_<3>
{
Coordinate_t x, y, z;
Point_();
Point_(Coordinate_t x, Coordinate_t y, Coordinate_t z);
Point_(const Point3f &p);
Point_(const Point2 &p, Coordinate_t z);
};
inline Point2::Point_()
: Point_(0, 0) {}
inline Point2::Point_(Coordinate_t _x, Coordinate_t _y)
: x(_x), y(_y) {}
inline Point2::Point_(const Point3 &p)
: Point_(p.x, p.y) {}
inline bool Point2::operator!=(const Point2 &other) const
{
return x != other.x
|| y != other.y;
}
inline Point3::Point_()
: Point_(0, 0, 0) {}
inline Point3::Point_(Coordinate_t _x, Coordinate_t _y, Coordinate_t _z)
: x(_x), y(_y), z(_z) {}
inline Point3::Point_(const Point3f &p)
: Point_(p.x, p.y, p.z) {}
inline Point3::Point_(const Point2 &p, Coordinate_t _z)
: Point_(p.x, p.y, _z) {}
struct Rect2i
{
int x, y, width, height;
};
struct Size2i
{
int width, height;
};
using Points2i_t = std::vector<Point2i>;
using Contour_t = Points2i_t;
template<typename PointType>
using Quadrilateral_ = std::array<PointType, 4>;
using Quadrilateral_t = Quadrilateral_<Point2>;
using Quadrilateralf_t = Quadrilateral_<Point2f>;
template<typename CoordinateType>
class LineCoordinates
{
protected:
const CoordinateType _a, _b, _c;
LineCoordinates(CoordinateType a,
CoordinateType b,
CoordinateType c)
: _a(a),
_b(b),
_c(c)
{
}
CoordinateType det(const LineCoordinates<CoordinateType> &other) const
{
return _a * other._b - other._a * _b;
}
CoordinateType detx(const LineCoordinates<CoordinateType> &other) const
{
return _b * other._c - other._b * _c;
}
CoordinateType dety(const LineCoordinates<CoordinateType> &other) const
{
return other._a * _c - _a * other._c;
}
private:
LineCoordinates(CoordinateType x1,
CoordinateType y1,
CoordinateType x2,
CoordinateType y2)
: LineCoordinates(y2 - y1,
x1 - x2,
x2 * y1 - x1 * y2)
{
}
public:
template<typename PointType>
LineCoordinates(const PointType &p,
const PointType &q)
: LineCoordinates(p.x, p.y,
q.x, q.y)
{
}
}; // class LineCoordinates
} // namespace stairs
#endif // TYPES_H_