Push out from the centroid; check whether it breaks topology

This commit is contained in:
Erica Fischer
2023-10-25 15:32:05 -07:00
parent 14e43cbfd3
commit a3a0e36b9d
+63 -16
View File
@@ -5,6 +5,45 @@ using Coord = long long;
using N = size_t;
using Point = std::array<Coord, 2>;
// Return false if the requested adjustment would give
// any of the polygons that contain vertex N a negative area
bool can_adjust(drawvec const &dv, std::vector<N> const &indices, size_t n, double *dx, double *dy) {
printf("looking for %zu\n", n);
bool again = true;
while (again) {
again = false;
for (size_t i = 0; i + 2 < indices.size(); i += 3) {
for (size_t j = 0; j < 3; j++) {
if (indices[i + j] == indices[n]) {
drawvec tri;
for (size_t k = 0; k < 3; k++) {
printf("found %zu %lld,%lld\n", i + j, dv[indices[i + k]].x, dv[indices[i + k]].y);
tri.push_back(dv[indices[i + k]]);
tri[k].op = VT_LINETO;
if (indices[i + k] == indices[n]) {
tri[k].x += *dx;
tri[k].y += *dy;
}
}
tri.push_back(tri[0]);
tri[0].op = VT_MOVETO;
printf("area %f\n", get_area(tri, 0, tri.size()));
if (get_area(tri, 0, tri.size()) < 0) {
*dx /= 2;
*dy /= 2;
again = true;
}
}
}
}
}
return true;
}
drawvec fix_by_triangulation(drawvec const &dv, int z, int detail) {
std::vector<std::vector<Point>> polygon;
drawvec out;
@@ -39,29 +78,37 @@ drawvec fix_by_triangulation(drawvec const &dv, int z, int detail) {
while (again) {
again = false;
for (size_t i = 0; i + 2 < indices.size(); i += 3) {
long long cx = 0, cy = 0;
for (size_t j = 0; j < 3; j++) {
cx += out[indices[i + j]].x;
cy += out[indices[i + j]].y;
}
cx /= 3;
cy /= 3;
for (size_t j = 0; j < 3; j++) {
size_t v1 = i + j;
size_t v2 = i + ((j + 1) % 3);
if (std::llround(out[indices[v1]].x / scale) == std::llround(out[indices[v2]].x / scale) &&
std::llround(out[indices[v1]].y / scale) == std::llround(out[indices[v2]].y / scale)) {
double ang = atan2(out[indices[v2]].y - out[indices[v1]].y, out[indices[v2]].x - out[indices[v1]].x);
double ang = atan2(out[indices[v1]].y - cy, out[indices[v1]].x - cx);
double dx = scale * cos(ang) * sqrt(2) * 20;
double dy = scale * sin(ang) * sqrt(2) * 20;
if (can_adjust(out, indices, v1, &dx, &dy)) {
out[indices[v1]].x += dx;
out[indices[v1]].y += dy;
again = true;
}
printf("at %zu/%zu: %zu and %zu: ", i, j, v1, v2);
printf("z%d: was %f,%f to %f,%f ",
z,
out[indices[v1]].x / scale, out[indices[v1]].y / scale,
out[indices[v2]].x / scale, out[indices[v2]].y / scale);
out[indices[v1]].x -= scale * cos(ang) * sqrt(2);
out[indices[v1]].y -= scale * sin(ang) * sqrt(2);
out[indices[v2]].x += scale * cos(ang) * sqrt(2);
out[indices[v2]].y += scale * sin(ang) * sqrt(2);
again = true;
printf("now %f,%f to %f,%f\n",
out[indices[v1]].x / scale, out[indices[v1]].y / scale,
out[indices[v2]].x / scale, out[indices[v2]].y / scale);
ang = atan2(out[indices[v2]].y - cy, out[indices[v2]].x - cx);
dx = scale * cos(ang) * sqrt(2) * 20;
dy = scale * sin(ang) * sqrt(2) * 20;
if (can_adjust(out, indices, v2, &dx, &dy)) {
out[indices[v2]].x += dx;
out[indices[v2]].y += dy;
again = true;
}
}
}
}