Implement simple hole stencil to resolve holes in GDS polygons.

This commit is contained in:
2022-04-19 22:14:52 +02:00
parent a6899a3f7c
commit 01613d1977
2 changed files with 108 additions and 7 deletions

View File

@@ -4,6 +4,74 @@
//#include <gds-render/gds-utils/gds-types.h>
#include <vector>
class Hole {
private:
std::vector <p2t::Point *> hole_vertices;
public:
const std::vector <p2t::Point *> &get_vertices()
{
return hole_vertices;
}
void add_vertex(const p2t::Point &point)
{
auto pt = new p2t::Point(point);
hole_vertices.push_back(pt);
}
Hole(std::vector<p2t::Point *>::iterator start, std::vector<p2t::Point *>::iterator end)
{
hole_vertices = std::vector<p2t::Point *>(start, end);
}
};
static std::vector<Hole> resolve_holes_in_polyline(std::vector<p2t::Point *> &polyline)
{
bool found = false;
std::vector<Hole> holes;
std::vector<std::pair<
std::vector<p2t::Point *>::iterator,
std::vector<p2t::Point *>::iterator>
> points_to_remove;
for (auto start_it = polyline.begin(); start_it != polyline.end(); start_it++) {
for (auto check_it = std::next(start_it); check_it != polyline.end(); check_it++) {
/* Check if points are equal */
if (**start_it == **check_it) {
if (**std::prev(check_it) == **std::next(start_it)) {
/* Found a hole. Everything between start_it and check_it can be removed */
auto hole = Hole(std::next(start_it)+1, check_it);
holes.push_back(hole);
auto tbd = std::make_pair(start_it, check_it);
points_to_remove.push_back(tbd);
found = true;
break;
}
}
}
if (found)
break;
}
/* Remove form polyline */
for (auto it_pair_it = points_to_remove.begin(); it_pair_it != points_to_remove.end(); it_pair_it++) {
auto it_pair = *it_pair_it;
delete *it_pair.first;
delete *std::next(it_pair.first);
polyline.erase(it_pair.first, it_pair.second);
}
if (found) {
auto new_holes = resolve_holes_in_polyline(polyline);
holes.insert(holes.end(), new_holes.begin(), new_holes.end());
}
return holes;
}
int tesselator_triangulate_polygon(const struct gds_graphics *gfx, double scale, float** output_vertices, size_t *vertex_count)
{
GList *vertex_iter;
@@ -18,8 +86,14 @@ int tesselator_triangulate_polygon(const struct gds_graphics *gfx, double scale,
polyline.push_back(pt);
}
auto holes = resolve_holes_in_polyline(polyline);
auto my_cdt = p2t::CDT(polyline);
for (auto hole_it = holes.begin(); hole_it != holes.end(); hole_it++) {
my_cdt.AddHole((*hole_it).get_vertices());
}
my_cdt.Triangulate();
auto triangles = my_cdt.GetTriangles();