/usr/include/mapbox/geometry/wagyu/local_minimum_util.hpp is in libmapbox-wagyu-dev 0.4.3-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 | #pragma once
#include <mapbox/geometry/wagyu/edge.hpp>
#include <mapbox/geometry/wagyu/local_minimum.hpp>
#include <algorithm>
#ifdef DEBUG
#include <stdexcept>
#endif
namespace mapbox {
namespace geometry {
namespace wagyu {
template <typename T>
inline void reverse_horizontal(edge<T>& e) {
// swap horizontal edges' top and bottom x's so they follow the natural
// progression of the bounds - ie so their xbots will align with the
// adjoining lower edge. [Helpful in the process_horizontal() method.]
std::swap(e.top.x, e.bot.x);
}
// Make a list start on a local maximum by
// shifting all the points not on a local maximum to the
template <typename T>
void start_list_on_local_maximum(edge_list<T>& edges) {
if (edges.size() <= 2) {
return;
}
// Find the first local maximum going forward in the list
auto prev_edge = edges.end();
--prev_edge;
bool prev_edge_is_horizontal = is_horizontal(*prev_edge);
auto edge = edges.begin();
bool edge_is_horizontal;
bool y_decreasing_before_last_horizontal = false; // assume false at start
while (edge != edges.end()) {
edge_is_horizontal = is_horizontal(*edge);
if ((!prev_edge_is_horizontal && !edge_is_horizontal && edge->top == prev_edge->top)) {
break;
}
if (!edge_is_horizontal && prev_edge_is_horizontal) {
if (y_decreasing_before_last_horizontal &&
(edge->top == prev_edge->bot || edge->top == prev_edge->top)) {
break;
}
} else if (!y_decreasing_before_last_horizontal && !prev_edge_is_horizontal &&
edge_is_horizontal &&
(prev_edge->top == edge->top || prev_edge->top == edge->bot)) {
y_decreasing_before_last_horizontal = true;
}
prev_edge_is_horizontal = edge_is_horizontal;
prev_edge = edge;
++edge;
}
std::rotate(edges.begin(), edge, edges.end());
}
template <typename T>
bound<T> create_bound_towards_minimum(edge_list<T>& edges) {
if (edges.size() == 1) {
if (is_horizontal(edges.front())) {
reverse_horizontal(edges.front());
}
bound<T> bnd;
std::swap(bnd.edges, edges);
return bnd;
}
auto next_edge = edges.begin();
auto edge = next_edge;
++next_edge;
bool edge_is_horizontal = is_horizontal(*edge);
if (edge_is_horizontal) {
reverse_horizontal(*edge);
}
bool next_edge_is_horizontal;
bool y_increasing_before_last_horizontal = false; // assume false at start
while (next_edge != edges.end()) {
next_edge_is_horizontal = is_horizontal(*next_edge);
if ((!next_edge_is_horizontal && !edge_is_horizontal && edge->bot == next_edge->bot)) {
break;
}
if (!next_edge_is_horizontal && edge_is_horizontal) {
if (y_increasing_before_last_horizontal &&
(next_edge->bot == edge->bot || next_edge->bot == edge->top)) {
break;
}
} else if (!y_increasing_before_last_horizontal && !edge_is_horizontal &&
next_edge_is_horizontal &&
(edge->bot == next_edge->top || edge->bot == next_edge->bot)) {
y_increasing_before_last_horizontal = true;
}
edge_is_horizontal = next_edge_is_horizontal;
edge = next_edge;
if (edge_is_horizontal) {
reverse_horizontal(*edge);
}
++next_edge;
}
bound<T> bnd;
if (next_edge == edges.end()) {
std::swap(edges, bnd.edges);
} else {
bnd.edges.reserve(static_cast<std::size_t>(std::distance(edges.begin(), next_edge)));
std::move(edges.begin(), next_edge, std::back_inserter(bnd.edges));
edges.erase(edges.begin(), next_edge);
}
std::reverse(bnd.edges.begin(), bnd.edges.end());
return bnd;
}
template <typename T>
bound<T> create_bound_towards_maximum(edge_list<T>& edges) {
if (edges.size() == 1) {
bound<T> bnd;
std::swap(bnd.edges, edges);
return bnd;
}
auto next_edge = edges.begin();
auto edge = next_edge;
++next_edge;
bool edge_is_horizontal = is_horizontal(*edge);
bool next_edge_is_horizontal;
bool y_decreasing_before_last_horizontal = false; // assume false at start
while (next_edge != edges.end()) {
next_edge_is_horizontal = is_horizontal(*next_edge);
if ((!next_edge_is_horizontal && !edge_is_horizontal && edge->top == next_edge->top)) {
break;
}
if (!next_edge_is_horizontal && edge_is_horizontal) {
if (y_decreasing_before_last_horizontal &&
(next_edge->top == edge->bot || next_edge->top == edge->top)) {
break;
}
} else if (!y_decreasing_before_last_horizontal && !edge_is_horizontal &&
next_edge_is_horizontal &&
(edge->top == next_edge->top || edge->top == next_edge->bot)) {
y_decreasing_before_last_horizontal = true;
}
edge_is_horizontal = next_edge_is_horizontal;
edge = next_edge;
++next_edge;
}
bound<T> bnd;
if (next_edge == edges.end()) {
std::swap(bnd.edges, edges);
} else {
bnd.edges.reserve(static_cast<std::size_t>(std::distance(edges.begin(), next_edge)));
std::move(edges.begin(), next_edge, std::back_inserter(bnd.edges));
edges.erase(edges.begin(), next_edge);
}
return bnd;
}
template <typename T>
void fix_horizontals(bound<T>& bnd) {
auto edge_itr = bnd.edges.begin();
auto next_itr = std::next(edge_itr);
if (next_itr == bnd.edges.end()) {
return;
}
if (is_horizontal(*edge_itr) && next_itr->bot != edge_itr->top) {
reverse_horizontal(*edge_itr);
}
auto prev_itr = edge_itr++;
while (edge_itr != bnd.edges.end()) {
if (is_horizontal(*edge_itr) && prev_itr->top != edge_itr->bot) {
reverse_horizontal(*edge_itr);
}
prev_itr = edge_itr;
++edge_itr;
}
}
template <typename T>
void move_horizontals_on_left_to_right(bound<T>& left_bound, bound<T>& right_bound) {
// We want all the horizontal segments that are at the same Y as the minimum to be on the right
// bound
auto edge_itr = left_bound.edges.begin();
while (edge_itr != left_bound.edges.end()) {
if (!is_horizontal(*edge_itr)) {
break;
}
reverse_horizontal(*edge_itr);
++edge_itr;
}
if (edge_itr == left_bound.edges.begin()) {
return;
}
std::reverse(left_bound.edges.begin(), edge_itr);
auto dist = std::distance(left_bound.edges.begin(), edge_itr);
std::move(left_bound.edges.begin(), edge_itr, std::back_inserter(right_bound.edges));
left_bound.edges.erase(left_bound.edges.begin(), edge_itr);
std::rotate(right_bound.edges.begin(), std::prev(right_bound.edges.end(), dist),
right_bound.edges.end());
}
template <typename T>
void add_ring_to_local_minima_list(edge_list<T>& edges,
local_minimum_list<T>& minima_list,
polygon_type poly_type) {
if (edges.empty()) {
return;
}
// Adjust the order of the ring so we start on a local maximum
// therefore we start right away on a bound.
start_list_on_local_maximum(edges);
bound_ptr<T> first_minimum = nullptr;
bound_ptr<T> last_maximum = nullptr;
while (!edges.empty()) {
bool lm_minimum_has_horizontal = false;
auto to_minimum = create_bound_towards_minimum(edges);
if (edges.empty()) {
throw std::runtime_error("Edges is empty after only creating a single bound.");
}
auto to_maximum = create_bound_towards_maximum(edges);
fix_horizontals(to_minimum);
fix_horizontals(to_maximum);
auto to_max_first_non_horizontal = to_maximum.edges.begin();
auto to_min_first_non_horizontal = to_minimum.edges.begin();
bool minimum_is_left = true;
while (to_max_first_non_horizontal != to_maximum.edges.end() &&
is_horizontal(*to_max_first_non_horizontal)) {
lm_minimum_has_horizontal = true;
++to_max_first_non_horizontal;
}
while (to_min_first_non_horizontal != to_minimum.edges.end() &&
is_horizontal(*to_min_first_non_horizontal)) {
lm_minimum_has_horizontal = true;
++to_min_first_non_horizontal;
}
if (to_max_first_non_horizontal == to_maximum.edges.end() ||
to_min_first_non_horizontal == to_minimum.edges.end()) {
throw std::runtime_error("should not have a horizontal only bound for a ring");
}
if (lm_minimum_has_horizontal) {
if (to_max_first_non_horizontal->bot.x > to_min_first_non_horizontal->bot.x) {
minimum_is_left = true;
move_horizontals_on_left_to_right(to_minimum, to_maximum);
} else {
minimum_is_left = false;
move_horizontals_on_left_to_right(to_maximum, to_minimum);
}
} else {
if (to_max_first_non_horizontal->dx > to_min_first_non_horizontal->dx) {
minimum_is_left = false;
} else {
minimum_is_left = true;
}
}
assert(!to_minimum.edges.empty());
assert(!to_maximum.edges.empty());
auto const& min_front = to_minimum.edges.front();
if (last_maximum) {
to_minimum.maximum_bound = last_maximum;
}
to_minimum.poly_type = poly_type;
to_maximum.poly_type = poly_type;
if (!minimum_is_left) {
to_minimum.side = edge_right;
to_maximum.side = edge_left;
to_minimum.winding_delta = -1;
to_maximum.winding_delta = 1;
minima_list.emplace_back(std::move(to_maximum), std::move(to_minimum), min_front.bot.y,
lm_minimum_has_horizontal);
if (!last_maximum) {
first_minimum = &(minima_list.back().right_bound);
} else {
last_maximum->maximum_bound = &(minima_list.back().right_bound);
}
last_maximum = &(minima_list.back().left_bound);
} else {
to_minimum.side = edge_left;
to_maximum.side = edge_right;
to_minimum.winding_delta = -1;
to_maximum.winding_delta = 1;
minima_list.emplace_back(std::move(to_minimum), std::move(to_maximum), min_front.bot.y,
lm_minimum_has_horizontal);
if (!last_maximum) {
first_minimum = &(minima_list.back().left_bound);
} else {
last_maximum->maximum_bound = &(minima_list.back().left_bound);
}
last_maximum = &(minima_list.back().right_bound);
}
}
last_maximum->maximum_bound = first_minimum;
first_minimum->maximum_bound = last_maximum;
}
template <typename T>
void initialize_lm(local_minimum_ptr_list_itr<T>& lm) {
if (!(*lm)->left_bound.edges.empty()) {
(*lm)->left_bound.current_edge = (*lm)->left_bound.edges.begin();
(*lm)->left_bound.next_edge = std::next((*lm)->left_bound.current_edge);
(*lm)->left_bound.current_x = static_cast<double>((*lm)->left_bound.current_edge->bot.x);
(*lm)->left_bound.winding_count = 0;
(*lm)->left_bound.winding_count2 = 0;
(*lm)->left_bound.side = edge_left;
(*lm)->left_bound.ring = nullptr;
}
if (!(*lm)->right_bound.edges.empty()) {
(*lm)->right_bound.current_edge = (*lm)->right_bound.edges.begin();
(*lm)->right_bound.next_edge = std::next((*lm)->right_bound.current_edge);
(*lm)->right_bound.current_x = static_cast<double>((*lm)->right_bound.current_edge->bot.x);
(*lm)->right_bound.winding_count = 0;
(*lm)->right_bound.winding_count2 = 0;
(*lm)->right_bound.side = edge_right;
(*lm)->right_bound.ring = nullptr;
}
}
}
}
}
|