mirror of
https://github.com/felt/tippecanoe.git
synced 2026-10-05 18:05:42 +02:00
Fix more overflows
This commit is contained in:
@@ -184,19 +184,21 @@ int clip(long long *x0, long long *y0, long long *x1, long long *y1, long long x
|
|||||||
|
|
||||||
// XXX truncating division
|
// XXX truncating division
|
||||||
|
|
||||||
|
long long shift = 1LL << (GLOBAL_DETAIL - 32);
|
||||||
|
|
||||||
// Now find the intersection point;
|
// Now find the intersection point;
|
||||||
// use formulas y = y0 + slope * (x - x0), x = x0 + (1 / slope) * (y - y0)
|
// use formulas y = y0 + slope * (x - x0), x = x0 + (1 / slope) * (y - y0)
|
||||||
if (outcodeOut & TOP) { // point is above the clip rectangle
|
if (outcodeOut & TOP) { // point is above the clip rectangle
|
||||||
x = *x0 + (*x1 - *x0) * (ymax - *y0) / (*y1 - *y0);
|
x = *x0 + ((*x1 - *x0) / shift) * ((ymax - *y0) / shift) / (*y1 - *y0) * shift * shift;
|
||||||
y = ymax;
|
y = ymax;
|
||||||
} else if (outcodeOut & BOTTOM) { // point is below the clip rectangle
|
} else if (outcodeOut & BOTTOM) { // point is below the clip rectangle
|
||||||
x = *x0 + (*x1 - *x0) * (ymin - *y0) / (*y1 - *y0);
|
x = *x0 + ((*x1 - *x0) / shift) * ((ymin - *y0) / shift) / (*y1 - *y0) * shift * shift;
|
||||||
y = ymin;
|
y = ymin;
|
||||||
} else if (outcodeOut & RIGHT) { // point is to the right of clip rectangle
|
} else if (outcodeOut & RIGHT) { // point is to the right of clip rectangle
|
||||||
y = *y0 + (*y1 - *y0) * (xmax - *x0) / (*x1 - *x0);
|
y = *y0 + ((*y1 - *y0) / shift) * ((xmax - *x0) / shift) / (*x1 - *x0) * shift * shift;
|
||||||
x = xmax;
|
x = xmax;
|
||||||
} else if (outcodeOut & LEFT) { // point is to the left of clip rectangle
|
} else if (outcodeOut & LEFT) { // point is to the left of clip rectangle
|
||||||
y = *y0 + (*y1 - *y0) * (xmin - *x0) / (*x1 - *x0);
|
y = *y0 + ((*y1 - *y0) / shift) * ((xmin - *x0) / shift) / (*x1 - *x0) * shift * shift;
|
||||||
x = xmin;
|
x = xmin;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
+12
-7
@@ -307,11 +307,14 @@ double distance_from_line(long long point_x, long long point_y, long long segA_x
|
|||||||
// These calculations must be made in integers instead of floating point
|
// These calculations must be made in integers instead of floating point
|
||||||
// to make them consistent between x86 and arm floating point implementations.
|
// to make them consistent between x86 and arm floating point implementations.
|
||||||
//
|
//
|
||||||
// Coordinates may be up to 34 bits, so their product is up to 68 bits,
|
// In a 32-bit world, coordinates may be up to 34 bits, so their product is up to 68 bits,
|
||||||
// making their sum up to 69 bits. Downshift before multiplying to keep them in range.
|
// making their sum up to 69 bits. Downshift before multiplying to keep them in range.
|
||||||
double something = ((p2x / 4) * (p2x / 8) + (p2y / 4) * (p2y / 8)) * 32.0;
|
//
|
||||||
|
// If the world is bigger than 32 bits, scale down to 32 bits.
|
||||||
|
long long shift = 1LL << (GLOBAL_DETAIL - 32);
|
||||||
|
double something = ((p2x / 4 / shift) * (p2x / 8 / shift) + (p2y / 4 / shift) * (p2y / 8 / shift)) * 32.0 * shift * shift;
|
||||||
// likewise
|
// likewise
|
||||||
double u = (0 == something) ? 0 : ((point_x - segA_x) / 4 * (p2x / 8) + (point_y - segA_y) / 4 * (p2y / 8)) * 32.0 / (something);
|
double u = (0 == something) ? 0 : ((point_x - segA_x) / 4 / shift * (p2x / 8 / shift) + (point_y - segA_y) / 4 / shift * (p2y / 8 / shift)) * 32.0 * shift * shift / (something);
|
||||||
|
|
||||||
if (u >= 1) {
|
if (u >= 1) {
|
||||||
u = 1;
|
u = 1;
|
||||||
@@ -682,13 +685,15 @@ drawvec fix_polygon(const drawvec &geom) {
|
|||||||
xtotal /= count;
|
xtotal /= count;
|
||||||
ytotal /= count;
|
ytotal /= count;
|
||||||
|
|
||||||
|
long long shift = 1LL << (GLOBAL_DETAIL - 32);
|
||||||
|
|
||||||
// figure out which point is furthest from the centroid
|
// figure out which point is furthest from the centroid
|
||||||
long long dist2 = 0;
|
long long dist2 = 0;
|
||||||
long long furthest = 0;
|
long long furthest = 0;
|
||||||
for (size_t a = 0; a + 1 < ring.size(); a++) {
|
for (size_t a = 0; a + 1 < ring.size(); a++) {
|
||||||
// division by 16 because these are z0 coordinates and we need to avoid overflow
|
// division by 16 because these are z0 coordinates and we need to avoid overflow
|
||||||
long long xd = (ring[a].x - xtotal) / 16;
|
long long xd = (ring[a].x - xtotal) / 16 / shift;
|
||||||
long long yd = (ring[a].y - ytotal) / 16;
|
long long yd = (ring[a].y - ytotal) / 16 / shift;
|
||||||
long long d2 = xd * xd + yd * yd;
|
long long d2 = xd * xd + yd * yd;
|
||||||
if (d2 > dist2 || (d2 == dist2 && ring[a] < ring[furthest])) {
|
if (d2 > dist2 || (d2 == dist2 && ring[a] < ring[furthest])) {
|
||||||
dist2 = d2;
|
dist2 = d2;
|
||||||
@@ -703,8 +708,8 @@ drawvec fix_polygon(const drawvec &geom) {
|
|||||||
long long furthestb = 0;
|
long long furthestb = 0;
|
||||||
for (size_t a = 0; a + 1 < ring.size(); a++) {
|
for (size_t a = 0; a + 1 < ring.size(); a++) {
|
||||||
// division by 16 because these are z0 coordinates and we need to avoid overflow
|
// division by 16 because these are z0 coordinates and we need to avoid overflow
|
||||||
long long xd = (ring[a].x - ring[furthest].x) / 16;
|
long long xd = (ring[a].x - ring[furthest].x) / 16 / shift;
|
||||||
long long yd = (ring[a].y - ring[furthest].y) / 16;
|
long long yd = (ring[a].y - ring[furthest].y) / 16 / shift;
|
||||||
long long d2 = xd * xd + yd * yd;
|
long long d2 = xd * xd + yd * yd;
|
||||||
if (d2 > dist2b || (d2 == dist2b && ring[a] < ring[furthestb])) {
|
if (d2 > dist2b || (d2 == dist2b && ring[a] < ring[furthestb])) {
|
||||||
dist2b = d2;
|
dist2b = d2;
|
||||||
|
|||||||
+2
-2
@@ -385,8 +385,8 @@ serial_feature parse_feature(json_pull *jp, int z, unsigned x, unsigned y, std::
|
|||||||
for (size_t i = 0; i < dv.size(); i++) {
|
for (size_t i = 0; i < dv.size(); i++) {
|
||||||
unsigned sx = 0, sy = 0;
|
unsigned sx = 0, sy = 0;
|
||||||
if (z != 0) {
|
if (z != 0) {
|
||||||
sx = x << (GLOBAL_DETAIL - z);
|
sx = (long long) x << (GLOBAL_DETAIL - z);
|
||||||
sy = y << (GLOBAL_DETAIL - z);
|
sy = (long long) y << (GLOBAL_DETAIL - z);
|
||||||
}
|
}
|
||||||
dv[i].x = std::round(dv[i].x / scale) * scale - sx;
|
dv[i].x = std::round(dv[i].x / scale) * scale - sx;
|
||||||
dv[i].y = std::round(dv[i].y / scale) * scale - sy;
|
dv[i].y = std::round(dv[i].y / scale) * scale - sy;
|
||||||
|
|||||||
@@ -1265,8 +1265,8 @@ void *run_prefilter(void *v) {
|
|||||||
// Offset from tile coordinates back to world coordinates
|
// Offset from tile coordinates back to world coordinates
|
||||||
unsigned sx = 0, sy = 0;
|
unsigned sx = 0, sy = 0;
|
||||||
if (rpa->z != 0) {
|
if (rpa->z != 0) {
|
||||||
sx = rpa->tx << (GLOBAL_DETAIL - rpa->z);
|
sx = (long long) rpa->tx << (GLOBAL_DETAIL - rpa->z);
|
||||||
sy = rpa->ty << (GLOBAL_DETAIL - rpa->z);
|
sy = (long long) rpa->ty << (GLOBAL_DETAIL - rpa->z);
|
||||||
}
|
}
|
||||||
for (size_t i = 0; i < tmp_feature.geometry.size(); i++) {
|
for (size_t i = 0; i < tmp_feature.geometry.size(); i++) {
|
||||||
tmp_feature.geometry[i].x += sx;
|
tmp_feature.geometry[i].x += sx;
|
||||||
|
|||||||
Reference in New Issue
Block a user