tippecanoe/mapbox/geometry/wagyu/build_result.hpp

73 lines
2.1 KiB
C++
Raw Normal View History

2016-12-07 14:14:56 -08:00
#pragma once
#include <mapbox/geometry/wagyu/ring.hpp>
#include <mapbox/geometry/wagyu/ring_util.hpp>
2018-07-31 17:32:27 -07:00
#include <mapbox/geometry/multi_polygon.hpp>
2016-12-07 14:14:56 -08:00
namespace mapbox {
namespace geometry {
namespace wagyu {
2018-07-31 17:32:27 -07:00
template <typename T1, typename T2>
void push_ring_to_polygon(mapbox::geometry::polygon<T2>& poly,
ring_ptr<T1> r,
bool reverse_output) {
mapbox::geometry::linear_ring<T2> lr;
lr.reserve(r->size() + 1);
2016-12-07 14:14:56 -08:00
auto firstPt = r->points;
auto ptIt = r->points;
if (reverse_output) {
do {
2018-07-31 17:32:27 -07:00
lr.emplace_back(static_cast<T2>(ptIt->x), static_cast<T2>(ptIt->y));
2016-12-07 14:14:56 -08:00
ptIt = ptIt->next;
} while (ptIt != firstPt);
} else {
do {
2018-07-31 17:32:27 -07:00
lr.emplace_back(static_cast<T2>(ptIt->x), static_cast<T2>(ptIt->y));
2016-12-07 14:14:56 -08:00
ptIt = ptIt->prev;
} while (ptIt != firstPt);
}
2017-01-04 16:33:43 -08:00
lr.emplace_back(firstPt->x, firstPt->y); // close the ring
2016-12-07 14:14:56 -08:00
poly.push_back(lr);
}
2018-07-31 17:32:27 -07:00
template <typename T1, typename T2>
void build_result_polygons(mapbox::geometry::multi_polygon<T2>& solution,
ring_vector<T1>const& rings,
2016-12-07 14:14:56 -08:00
bool reverse_output) {
2018-07-31 17:32:27 -07:00
for (auto r : rings) {
if (r == nullptr) {
2016-12-07 14:14:56 -08:00
continue;
}
2018-07-31 17:32:27 -07:00
assert(r->points);
2016-12-07 14:14:56 -08:00
solution.emplace_back();
push_ring_to_polygon(solution.back(), r, reverse_output);
2018-07-31 17:32:27 -07:00
for (auto c : r->children) {
if (c == nullptr) {
2016-12-07 14:14:56 -08:00
continue;
}
2018-07-31 17:32:27 -07:00
assert(c->points);
2016-12-07 14:14:56 -08:00
push_ring_to_polygon(solution.back(), c, reverse_output);
}
2018-07-31 17:32:27 -07:00
for (auto c : r->children) {
if (c == nullptr) {
continue;
}
2016-12-07 14:14:56 -08:00
if (!c->children.empty()) {
build_result_polygons(solution, c->children, reverse_output);
}
}
}
}
2018-07-31 17:32:27 -07:00
template <typename T1, typename T2>
void build_result(mapbox::geometry::multi_polygon<T2>& solution,
ring_manager<T1>const& rings,
2016-12-07 14:14:56 -08:00
bool reverse_output) {
build_result_polygons(solution, rings.children, reverse_output);
}
}
}
}