-
Notifications
You must be signed in to change notification settings - Fork 0
/
monotone_decomposition.cpp
67 lines (58 loc) · 2.65 KB
/
monotone_decomposition.cpp
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
#include "monotone_decomposition.hpp"
#include <algorithm>
std::vector<monotone_decomposition::monotone_subpath> monotone_decomposition::get_monotone_subpaths(const poly_line& line) const
{
const auto& path = line.coordinates;
BOOST_ASSERT(path.size() >= 2);
std::vector<monotone_subpath> subpaths;
unsigned last_pos = 0;
geometry::monoticity mono = geometry::monoticity::CONSTANT;
for (unsigned i = 0; i < path.size()-1; i++)
{
// monoticity of current edge
geometry::monoticity current_mono = geometry::monoticity::CONSTANT;
// not increasing
if (path[i].y > path[i+1].y)
{
current_mono = static_cast<geometry::monoticity>(static_cast<unsigned char>(current_mono) & ~static_cast<unsigned char>(geometry::monoticity::INCREASING_Y));
}
if (path[i].x > path[i+1].x)
{
current_mono = static_cast<geometry::monoticity>(static_cast<unsigned char>(current_mono) & ~static_cast<unsigned char>(geometry::monoticity::INCREASING_X));
}
// not decreasing
if (path[i].y < path[i+1].y)
{
current_mono = static_cast<geometry::monoticity>(static_cast<unsigned char>(current_mono) & ~static_cast<unsigned char>(geometry::monoticity::DECREASING_Y));
}
if (path[i].x < path[i+1].x)
{
current_mono = static_cast<geometry::monoticity>(static_cast<unsigned char>(current_mono) & ~static_cast<unsigned char>(geometry::monoticity::DECREASING_X));
}
// monoticity of subpath last_pos .. i+1
geometry::monoticity next_mono = static_cast<geometry::monoticity>(static_cast<unsigned char>(mono) & static_cast<unsigned char>(current_mono));
if (next_mono == geometry::monoticity::INVALID)
{
subpaths.emplace_back(monotone_decomposition::monotone_subpath {
poly_line {line.id, std::vector<coordinate>(path.begin() + last_pos, path.begin() + (i+1))},
last_pos,
i+1,
mono
});
last_pos = i;
mono = current_mono;
}
else
{
mono = next_mono;
}
}
// finish last subpath
subpaths.emplace_back(monotone_decomposition::monotone_subpath {
poly_line {line.id, std::vector<coordinate>(path.begin() + last_pos, path.end())},
last_pos,
static_cast<unsigned>(path.size()),
mono
});
return subpaths;
}