-
Notifications
You must be signed in to change notification settings - Fork 0
/
make_problem.cc
122 lines (109 loc) · 3.54 KB
/
make_problem.cc
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
#include <complex>
#include <vector>
#include <fstream>
#include <sstream>
#include "base/base.h"
#include "boost/rational.hpp"
#include "polygon.h"
#include "problem.h"
#include "solution.h"
#include <boost/geometry.hpp>
#include <boost/geometry/algorithms/area.hpp>
#include <boost/geometry/algorithms/union.hpp>
#include <boost/geometry/geometries/geometries.hpp>
#include <boost/geometry/geometries/polygon.hpp>
#include <boost/geometry/geometries/point_xy.hpp>
#include <boost/multiprecision/gmp.hpp>
DEFINE_string(input, "/dev/stdin", "input solution file");
namespace bg = boost::geometry;
using boost::rational;
using gPoint = bg::model::d2::point_xy<Q>;
using ccwRing = bg::model::ring<gPoint, false, false>;
using ccwPolygon = bg::model::polygon<gPoint, false, false>;
using ccwMultiPolygon = bg::model::multi_polygon<ccwPolygon>;
using LineString = bg::model::linestring<gPoint>;
using MultiLineString = bg::model::multi_linestring<LineString>;
using namespace std;
Vertex ConvertToVertex(const gPoint& p) {
Vertex v;
v.x = p.x();
v.y = p.y();
return v;
}
template <typename Ring>
void ReduceRingAnchorPoints(Ring* ring) {
for (int i = 0; i < ring->size(); ++i) {
auto a = (*ring)[i];
auto b = (*ring)[(i + 1) % ring->size()];
auto c = (*ring)[(i + 2) % ring->size()];
bg::subtract_point(c, b);
bg::subtract_point(b, a);
if (b.x() * c.y() - b.y() * c.x() == 0) {
ring->erase(ring->begin() + (i + 1) % ring->size());
i--;
}
}
}
int main(int argc, char** argv) {
ParseCommandLineFlags(&argc, &argv);
// Input
Solution solution;
std::ifstream solution_ifs(FLAGS_input);
ReadSolution(solution_ifs, &solution);
// Combine
ccwMultiPolygon mpoly, mpoly_tmp;
MultiLineString mls, mls_tmp;
for (const auto& facet : solution.facets) {
ccwRing ring;
for (const auto& i : facet) {
bg::append(ring,
gPoint(solution.dst_verts[i].x, solution.dst_verts[i].y));
}
bg::correct(ring);
LOG(INFO) << "union ring: " << bg::wkt(ring);
bg::union_(mpoly, ring, mpoly_tmp);
mpoly.swap(mpoly_tmp);
bg::clear(mpoly_tmp);
LOG(INFO) << "polygon: " << bg::wkt(mpoly);
LineString ls;
for (const auto& i : facet) {
bg::append(ls, gPoint(solution.dst_verts[i].x, solution.dst_verts[i].y));
}
bg::append(ls, gPoint(solution.dst_verts[facet[0]].x,
solution.dst_verts[facet[0]].y));
LOG(INFO) << "union line: " << bg::wkt(ls);
bg::union_(mls, ls, mls_tmp);
mls.swap(mls_tmp);
bg::clear(mls_tmp);
LOG(INFO) << "lines: " << bg::wkt(mls);
}
// Optimize
for (auto& poly : mpoly) {
ReduceRingAnchorPoints(&bg::exterior_ring(poly));
for (auto& iring : bg::interior_rings(poly)) {
ReduceRingAnchorPoints(&iring);
}
}
// Output
Problem problem;
for (const auto& poly : mpoly) {
const auto& xring = bg::exterior_ring(poly);
problem.polygons.emplace_back(xring.size());
std::transform(xring.begin(), xring.end(), problem.polygons.back().begin(),
&ConvertToVertex);
const auto& irings = bg::interior_rings(poly);
for (const auto& iring : irings) {
problem.polygons.emplace_back(iring.size());
std::transform(iring.begin(), iring.end(),
problem.polygons.back().begin(), &ConvertToVertex);
}
}
for (const auto& ls : mls) {
for (int i = 0; i < ls.size() - 1; ++i) {
problem.skelton.emplace_back(ConvertToVertex(ls[i]),
ConvertToVertex(ls[i + 1]));
}
}
WriteProblem(problem, cout);
return 0;
}