From 74a2e0ab153d43684f30bc2bdd73addc49f869b2 Mon Sep 17 00:00:00 2001 From: Eric Fischer Date: Tue, 12 Jun 2018 11:32:40 -0700 Subject: [PATCH] More informative return value from clipping --- clip.hpp | 18 ++++++++++++------ geometry.cpp | 12 ++++++------ 2 files changed, 18 insertions(+), 12 deletions(-) diff --git a/clip.hpp b/clip.hpp index 1a40fb1..1bc345e 100644 --- a/clip.hpp +++ b/clip.hpp @@ -23,11 +23,17 @@ static int computeOutCode(double x, double y, double xmin, double ymin, double x return code; } +#define CLIP_OK 0 +#define CLIP_ELIMINATED 1 +#define CLIP_CHANGED_FIRST 2 +#define CLIP_CHANGED_SECOND 4 +#define CLIP_CHANGED (CLIP_CHANGED_FIRST | CLIP_CHANGED_SECOND) + static int clip(double *x0, double *y0, double *x1, double *y1, double xmin, double ymin, double xmax, double ymax) { int outcode0 = computeOutCode(*x0, *y0, xmin, ymin, xmax, ymax); int outcode1 = computeOutCode(*x1, *y1, xmin, ymin, xmax, ymax); + int changed = CLIP_OK; int accept = 0; - int changed = 0; while (1) { if (!(outcode0 | outcode1)) { // Bitwise OR is 0. Trivially accept and get out of loop @@ -65,19 +71,19 @@ static int clip(double *x0, double *y0, double *x1, double *y1, double xmin, dou *x0 = x; *y0 = y; outcode0 = computeOutCode(*x0, *y0, xmin, ymin, xmax, ymax); - changed = 1; + changed |= CLIP_CHANGED_FIRST; } else { *x1 = x; *y1 = y; outcode1 = computeOutCode(*x1, *y1, xmin, ymin, xmax, ymax); - changed = 1; + changed |= CLIP_CHANGED_SECOND; } } } - if (accept == 0) { - return 0; + if (accept) { + return changed; } else { - return changed + 1; + return CLIP_ELIMINATED; } } diff --git a/geometry.cpp b/geometry.cpp index c83e889..404de37 100644 --- a/geometry.cpp +++ b/geometry.cpp @@ -663,14 +663,14 @@ drawvec clip_lines(drawvec &geom, int z, long long buffer) { int c = clip(&x1, &y1, &x2, &y2, min, min, area, area); - if (c > 1) { // clipped + if (c == CLIP_ELIMINATED) { + out.push_back(draw(VT_MOVETO, geom[i].x, geom[i].y)); + } else if (c == CLIP_OK) { + out.push_back(geom[i]); + } else { out.push_back(draw(VT_MOVETO, x1, y1)); out.push_back(draw(VT_LINETO, x2, y2)); out.push_back(draw(VT_MOVETO, geom[i].x, geom[i].y)); - } else if (c == 1) { // unchanged - out.push_back(geom[i]); - } else { // clipped away entirely - out.push_back(draw(VT_MOVETO, geom[i].x, geom[i].y)); } } else { out.push_back(geom[i]); @@ -776,7 +776,7 @@ drawvec impose_tile_boundaries(drawvec &geom, long long extent) { int c = clip(&x1, &y1, &x2, &y2, 0, 0, extent, extent); - if (c > 1) { // clipped + if (c & CLIP_CHANGED) { if (x1 != geom[i - 1].x || y1 != geom[i - 1].y) { out.push_back(draw(VT_LINETO, x1, y1)); out[out.size() - 1].necessary = 1;