-
Notifications
You must be signed in to change notification settings - Fork 0
/
polygon.h
93 lines (81 loc) · 2.15 KB
/
polygon.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
#pragma once
#include <complex>
#include <vector>
#include "base/base.h"
#include "boost/multiprecision/gmp.hpp"
typedef boost::multiprecision::mpz_int Z;
typedef boost::multiprecision::number<boost::multiprecision::gmp_rational,
boost::multiprecision::et_off> Q;
namespace std {
// Dummy functions for std::complex
bool isnan(const Q& q) { return false; }
bool isinf(const Q& q) { return false; }
Q copysign(const Q& x, const Q& y) { return x.sign() != y.sign() ? -x : x; }
}
struct Vertex {
Q x;
Q y;
};
typedef vector<Vertex> Polygon;
template <typename T>
void ResolveIndexReference(const vector<int>& index, const vector<T>& ref,
vector<T>* out) {
for (int i : index) {
out->emplace_back(ref[i]);
}
}
bool is_ccw(const Polygon& p) {
Q area;
for (int i = 1; i < p.size(); ++i) {
area += (p[i].x - p[0].x) * (p[i + 1].y - p[0].y) -
(p[i].y - p[0].y) * (p[i + 1].x - p[0].x);
}
LOG_IF(ERROR, area == 0) << "Unexpected zero area";
return area < 0;
}
Q consume_rational(std::istream& is) {
while (is.good() && isspace(is.peek())) {
is.get();
}
string buf;
while (is.good()) {
int c = is.peek();
if (isdigit(c) || c == '-' || c == '/') {
buf += c;
is.get();
} else {
break;
}
}
CHECK(!buf.empty());
return Q(buf);
}
std::istream& operator>>(std::istream& is, Vertex& v) {
v.x = consume_rational(is);
while (is.good() && isspace(is.peek())) is.get();
int c = is.peek();
LOG_IF(ERROR, c != ',' && !isdigit(c)) << "Expected comma but was " << (char)c
<< ":" << c;
if (c == ',') is.get();
v.y = consume_rational(is);
return is;
}
std::ostream& operator<<(std::ostream& os, const Vertex& v) {
return os << v.x << ',' << v.y;
}
std::istream& operator>>(std::istream& is, Polygon& p) {
int n_verts;
CHECK(is >> n_verts);
p.resize(n_verts);
for (int i = 0; i < n_verts; ++i) {
CHECK(is >> p[i]);
}
return is;
}
std::ostream& operator<<(std::ostream& os, const Polygon& p) {
os << p.size() << '\n';
for (const auto& v : p) {
os << v << '\n';
}
return os;
}