-
Notifications
You must be signed in to change notification settings - Fork 4
/
common.hpp
130 lines (101 loc) · 2.57 KB
/
common.hpp
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
#pragma once
#include <iostream>
#include <fstream>
#include <optional>
#include "vendor/json.hpp"
namespace FPCFilter
{
template <class T>
class PointXYZ {
public:
T x, y, z;
PointXYZ(T x, T y, T z) : x(x), y(y), z(z) {}
bool operator==(const PointXYZ &other) const {
return x == other.x && y == other.y && z == other.z;
}
bool operator!=(const PointXYZ &other) const {
return !(*this == other);
}
bool operator<(const PointXYZ &other) const {
return std::tie(x, y, z) < std::tie(other.x, other.y, other.z);
}
bool operator>(const PointXYZ &other) const {
return std::tie(x, y, z) > std::tie(other.x, other.y, other.z);
}
bool operator<=(const PointXYZ &other) const {
return std::tie(x, y, z) <= std::tie(other.x, other.y, other.z);
}
bool operator>=(const PointXYZ &other) const {
return std::tie(x, y, z) >= std::tie(other.x, other.y, other.z);
}
};
// Point class (x, y)
class PointXY
{
public:
float x;
float y;
PointXY(float x, float y) : x(x), y(y) {}
};
class Polygon
{
private:
std::vector<PointXY> points;
public:
Polygon() {}
Polygon(std::vector<PointXY> points) : points(points) {}
Polygon(std::vector<float> x, std::vector<float> y)
{
for (int i = 0; i < x.size(); i++)
{
this->points.push_back(PointXY(x[i], y[i]));
}
}
std::vector<PointXY> getPoints()
{
return this->points;
}
void addPoint(PointXY point)
{
this->points.push_back(point);
}
void addPoint(float x, float y)
{
this->points.push_back(PointXY(x, y));
}
bool inside(float x, float y) const
{
// ray-casting algorithm based on
// https://wrf.ecse.rpi.edu/Research/Short_Notes/pnpoly.html/pnpoly.html
auto inside = false;
size_t i;
size_t j;
for (i = 0, j = points.size() - 1; i < points.size(); j = i++)
{
const auto xi = points[i].x;
const auto yi = points[i].y;
const auto xj = points[j].x;
const auto yj = points[j].y;
const auto intersect = ((yi > y) != (yj > y)) && (x < (xj - xi)* (y - yi) / (yj - yi) + xi);
if (intersect)
inside = !inside;
}
return inside;
};
bool inside(const PointXY& point) const
{
return inside(point.x, point.y);
};
};
class NotImplementedException : public std::exception
{
public:
NotImplementedException(const char* message) : message(message) {}
const char* what() const noexcept
{
return message;
}
private:
const char* message;
};
}