mirror of
https://github.com/felt/tippecanoe.git
synced 2026-10-02 16:35:40 +02:00
Use 128-bit arithmetic to avoid overflow
This commit is contained in:
+17
-27
@@ -300,21 +300,15 @@ bool point_within_tile(long long x, long long y, int z) {
|
||||
return x >= 0 && y >= 0 && x < area && y < area;
|
||||
}
|
||||
|
||||
double distance_from_line(long long point_x, long long point_y, long long segA_x, long long segA_y, long long segB_x, long long segB_y) {
|
||||
long long p2x = segB_x - segA_x;
|
||||
long long p2y = segB_y - segA_y;
|
||||
double distance_from_line(__int128 point_x, __int128 point_y, long long segA_x, long long segA_y, long long segB_x, long long segB_y) {
|
||||
__int128 p2x = segB_x - segA_x;
|
||||
__int128 p2y = segB_y - segA_y;
|
||||
|
||||
// These calculations must be made in integers instead of floating point
|
||||
// to make them consistent between x86 and arm floating point implementations.
|
||||
//
|
||||
// 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.
|
||||
//
|
||||
// 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;
|
||||
double something = (p2x) * (p2x) + (p2y) * (p2y);
|
||||
// likewise
|
||||
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);
|
||||
double u = (0 == something) ? 0 : ((point_x - segA_x) * (p2x) + (point_y - segA_y) * (p2y)) / (something);
|
||||
|
||||
if (u >= 1) {
|
||||
u = 1;
|
||||
@@ -674,8 +668,8 @@ drawvec fix_polygon(const drawvec &geom) {
|
||||
|
||||
// calculate centroid
|
||||
// a + 1 < size() because point 0 is duplicated at the end
|
||||
long long xtotal = 0;
|
||||
long long ytotal = 0;
|
||||
__int128 xtotal = 0;
|
||||
__int128 ytotal = 0;
|
||||
long long count = 0;
|
||||
for (size_t a = 0; a + 1 < ring.size(); a++) {
|
||||
xtotal += ring[a].x;
|
||||
@@ -685,16 +679,13 @@ drawvec fix_polygon(const drawvec &geom) {
|
||||
xtotal /= count;
|
||||
ytotal /= count;
|
||||
|
||||
long long shift = 1LL << (GLOBAL_DETAIL - 32);
|
||||
|
||||
// figure out which point is furthest from the centroid
|
||||
long long dist2 = 0;
|
||||
long long furthest = 0;
|
||||
__int128 dist2 = 0;
|
||||
size_t furthest = 0;
|
||||
for (size_t a = 0; a + 1 < ring.size(); a++) {
|
||||
// division by 16 because these are z0 coordinates and we need to avoid overflow
|
||||
long long xd = (ring[a].x - xtotal) / 16 / shift;
|
||||
long long yd = (ring[a].y - ytotal) / 16 / shift;
|
||||
long long d2 = xd * xd + yd * yd;
|
||||
__int128 xd = (ring[a].x - xtotal);
|
||||
__int128 yd = (ring[a].y - ytotal);
|
||||
__int128 d2 = xd * xd + yd * yd;
|
||||
if (d2 > dist2 || (d2 == dist2 && ring[a] < ring[furthest])) {
|
||||
dist2 = d2;
|
||||
furthest = a;
|
||||
@@ -704,13 +695,12 @@ drawvec fix_polygon(const drawvec &geom) {
|
||||
// then figure out which point is furthest from *that*,
|
||||
// which will hopefully be a good origin point since it should be
|
||||
// at a far edge of the shape.
|
||||
long long dist2b = 0;
|
||||
long long furthestb = 0;
|
||||
__int128 dist2b = 0;
|
||||
size_t furthestb = 0;
|
||||
for (size_t a = 0; a + 1 < ring.size(); a++) {
|
||||
// division by 16 because these are z0 coordinates and we need to avoid overflow
|
||||
long long xd = (ring[a].x - ring[furthest].x) / 16 / shift;
|
||||
long long yd = (ring[a].y - ring[furthest].y) / 16 / shift;
|
||||
long long d2 = xd * xd + yd * yd;
|
||||
__int128 xd = (ring[a].x - ring[furthest].x);
|
||||
__int128 yd = (ring[a].y - ring[furthest].y);
|
||||
__int128 d2 = xd * xd + yd * yd;
|
||||
if (d2 > dist2b || (d2 == dist2b && ring[a] < ring[furthestb])) {
|
||||
dist2b = d2;
|
||||
furthestb = a;
|
||||
|
||||
Reference in New Issue
Block a user