Skip to content

Commit

Permalink
Fix my assumption that wagyu can work on multiple outer rings at once
Browse files Browse the repository at this point in the history
  • Loading branch information
e-n-f committed Oct 27, 2023
1 parent 0c4b5d0 commit 5afc80d
Showing 1 changed file with 48 additions and 45 deletions.
93 changes: 48 additions & 45 deletions earcut.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,34 +5,7 @@ using Coord = long long;
using N = size_t;
using Point = std::array<Coord, 2>;

drawvec fix_by_triangulation(drawvec const &dv, int z, int detail) {
std::vector<std::vector<Point>> polygon;
drawvec out;
double scale = 1LL << (32 - z - detail);

for (size_t i = 0; i < dv.size(); i++) {
if (dv[i].op == VT_MOVETO) {
size_t j;
for (j = i + 1; j < dv.size(); j++) {
if (dv[j].op != VT_LINETO) {
break;
}
}

std::vector<Point> ring;
// j - 1 because earcut docs indicate that it doesn't expect
// a duplicate last point in each ring
for (size_t k = i; k < j - 1; k++) {
Point p = {(long long) dv[k].x, (long long) dv[k].y};
ring.push_back(p);
out.push_back(dv[k]);
}
polygon.push_back(ring);

i = j - 1;
}
}

drawvec reinforce(drawvec const &pts, std::vector<std::vector<Point>> polygon, double scale) {
std::vector<N> indices = mapbox::earcut<N>(polygon);

drawvec out2;
Expand All @@ -46,40 +19,70 @@ drawvec fix_by_triangulation(drawvec const &dv, int z, int detail) {

double px, py;

if (distance_from_line(out[indices[v1]].x, out[indices[v1]].y, // the point
out[indices[v2]].x, out[indices[v2]].y, // start of opposite side
out[indices[v3]].x, out[indices[v3]].y, // end of opposite side
&px, &py) < 2 * scale) {
double ang = atan2(out[indices[v1]].y - py, out[indices[v1]].x - px);
if (distance_from_line(pts[indices[v1]].x, pts[indices[v1]].y, // the point
pts[indices[v2]].x, pts[indices[v2]].y, // start of opposite side
pts[indices[v3]].x, pts[indices[v3]].y, // end of opposite side
&px, &py) < scale) {
double ang = atan2(pts[indices[v1]].y - py, pts[indices[v1]].x - px);

// make a new triangle that is not so flat
out2.push_back(draw(VT_MOVETO, out[indices[v2]].x, out[indices[v2]].y));
out2.push_back(draw(VT_LINETO, out[indices[v3]].x, out[indices[v3]].y));
out2.push_back(draw(VT_LINETO, px + 2 * scale * cos(ang), py + 2 * scale * sin(ang)));
out2.push_back(draw(VT_LINETO, out[indices[v2]].x, out[indices[v2]].y));
out2.push_back(draw(VT_MOVETO, pts[indices[v2]].x, pts[indices[v2]].y));
out2.push_back(draw(VT_LINETO, pts[indices[v3]].x, pts[indices[v3]].y));
out2.push_back(draw(VT_LINETO, px + scale * cos(ang), py + scale * sin(ang)));
out2.push_back(draw(VT_LINETO, pts[indices[v2]].x, pts[indices[v2]].y));
}
}
}

// re-close the rings from which we removed the last points earlier
return out2;
}

drawvec fix_by_triangulation(drawvec const &dv, int z, int detail) {
std::vector<std::vector<Point>> polygon;
drawvec out, out2;
double scale = 1LL << (32 - z - detail);

for (size_t i = 0; i < out.size(); i++) {
if (out[i].op == VT_MOVETO) {
for (size_t i = 0; i < dv.size(); i++) {
if (dv[i].op == VT_MOVETO) {
size_t j;
for (j = i + 1; j < out.size(); j++) {
if (out[j].op != VT_LINETO) {
for (j = i + 1; j < dv.size(); j++) {
if (dv[j].op != VT_LINETO) {
break;
}
}

for (size_t k = i; k < j; k++) {
out2.push_back(out[k]);
if (get_area(dv, i, j) > 0) {
// outer ring, so process whatever we have so far
drawvec additional = reinforce(out, polygon, scale);
for (auto const &d : additional) {
out2.push_back(d);
}

polygon.clear();
out.clear();
}

std::vector<Point> ring;
// j - 1 because earcut docs indicate that it doesn't expect
// a duplicate last point in each ring
for (size_t k = i; k < j - 1; k++) {
Point p = {(long long) dv[k].x, (long long) dv[k].y};
ring.push_back(p);
out.push_back(dv[k]);
}
polygon.push_back(ring);

out2.push_back(draw(VT_LINETO, out[i].x, out[i].y));
i = j - 1;
}
}

drawvec additional = reinforce(out, polygon, scale);
for (auto const &d : additional) {
out2.push_back(d);
}
for (auto const &d : dv) {
out2.push_back(d);
}

return out2;
}

0 comments on commit 5afc80d

Please sign in to comment.