diff --git a/include/mapbox/earcut.hpp b/include/mapbox/earcut.hpp index 47707d0..a916c1d 100644 --- a/include/mapbox/earcut.hpp +++ b/include/mapbox/earcut.hpp @@ -84,7 +84,7 @@ class Earcut { bool hashing; double minX, maxX; double minY, maxY; - double size; + double inv_size = 0; template > class ObjectPool { @@ -135,7 +135,6 @@ void Earcut::operator()(const Polygon& points) { double x; double y; - size = 0; int threshold = 80; std::size_t len = 0; @@ -170,7 +169,8 @@ void Earcut::operator()(const Polygon& points) { } while (p != outerNode); // minX, minY and size are later used to transform coords into integers for z-order calculation - size = std::max(maxX - minX, maxY - minY); + inv_size = std::max(maxX - minX, maxY - minY); + inv_size = inv_size != .0 ? (1. / inv_size) : .0; } earcutLinked(outerNode); @@ -231,7 +231,7 @@ Earcut::filterPoints(Node* start, Node* end) { removeNode(p); p = end = p->prev; - if (p == p->next) return nullptr; + if (p == p->next) break; again = true; } else { @@ -603,8 +603,8 @@ Earcut::sortLinked(Node* list) { template int32_t Earcut::zOrder(const double x_, const double y_) { // coords are transformed into non-negative 15-bit integer range - int32_t x = static_cast(32767.0 * (x_ - minX) / size); - int32_t y = static_cast(32767.0 * (y_ - minY) / size); + int32_t x = static_cast(32767.0 * (x_ - minX) * inv_size); + int32_t y = static_cast(32767.0 * (y_ - minY) * inv_size); x = (x | (x << 8)) & 0x00FF00FF; x = (x | (x << 4)) & 0x0F0F0F0F; diff --git a/test/fixtures/issue83.cpp b/test/fixtures/issue83.cpp new file mode 100644 index 0000000..f36af7e --- /dev/null +++ b/test/fixtures/issue83.cpp @@ -0,0 +1,14 @@ + +#include "geometries.hpp" + +namespace mapbox { +namespace fixtures { + +static const Fixture issue83("issue83", 0, 1e-14, Infinity, { + {{0,0},{4000,0},{4000,4000},{0,4000}}, + {{0,0},{4000,0},{4000,4000},{0,4000}}, + {{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1}}, +}); + +} +}