diff options
author | Bruno de Oliveira Abinader <bruno@mapbox.com> | 2018-07-19 17:54:13 +0300 |
---|---|---|
committer | Bruno de Oliveira Abinader <bruno@mapbox.com> | 2018-07-30 15:34:45 +0300 |
commit | 8bd8b335f036880a2b7dc478c3bffb6897749a61 (patch) | |
tree | 472e1797916b15c0538b6bf52c8f6188e65bd1c5 | |
parent | ba411cf421e0254bfd89c60ee10384f5e19cff10 (diff) | |
download | qtlocation-mapboxgl-8bd8b335f036880a2b7dc478c3bffb6897749a61.tar.gz |
[core] Added floating point compare mbgl::util::isNull
-rw-r--r-- | cmake/core-files.cmake | 1 | ||||
-rw-r--r-- | include/mbgl/math/compare.hpp | 17 | ||||
-rw-r--r-- | src/mbgl/util/tile_cover.cpp | 11 |
3 files changed, 24 insertions, 5 deletions
diff --git a/cmake/core-files.cmake b/cmake/core-files.cmake index e9f70378ad..3f7a2855a7 100644 --- a/cmake/core-files.cmake +++ b/cmake/core-files.cmake @@ -121,6 +121,7 @@ set(MBGL_CORE_FILES # math include/mbgl/math/clamp.hpp + include/mbgl/math/compare.hpp include/mbgl/math/log2.hpp include/mbgl/math/minmax.hpp include/mbgl/math/wrap.hpp diff --git a/include/mbgl/math/compare.hpp b/include/mbgl/math/compare.hpp new file mode 100644 index 0000000000..c4149a4ed0 --- /dev/null +++ b/include/mbgl/math/compare.hpp @@ -0,0 +1,17 @@ +#pragma once + +#include <cmath> +#include <limits> +#include <type_traits> + +namespace mbgl { +namespace util { + +// https://en.cppreference.com/w/cpp/types/numeric_limits/epsilon +template <typename T, typename = std::enable_if<std::is_floating_point<T>::value>> +T clampedFloor(T a) { + return ::fabs(a) <= std::numeric_limits<T>::epsilon() ? 0 : ::floor(a); +} + +} // namespace util +} // namespace mbgl diff --git a/src/mbgl/util/tile_cover.cpp b/src/mbgl/util/tile_cover.cpp index 3f39e53d40..b8dceb157f 100644 --- a/src/mbgl/util/tile_cover.cpp +++ b/src/mbgl/util/tile_cover.cpp @@ -4,6 +4,7 @@ #include <mbgl/map/transform_state.hpp> #include <mbgl/util/tile_cover_impl.hpp> #include <mbgl/util/tile_coordinate.hpp> +#include <mbgl/math/compare.hpp> #include <functional> #include <list> @@ -34,8 +35,8 @@ struct edge { // scan-line conversion static void scanSpans(edge e0, edge e1, int32_t ymin, int32_t ymax, ScanLine scanLine) { - double y0 = ::fmax(ymin, std::floor(e1.y0)); - double y1 = ::fmin(ymax, std::ceil(e1.y1)); + double y0 = util::max(double(ymin), util::clampedFloor(e1.y0)); + double y1 = util::min(double(ymax), std::ceil(e1.y1)); // sort edges by x-coordinate if ((e0.x0 == e1.x0 && e0.y0 == e1.y0) ? @@ -50,9 +51,9 @@ static void scanSpans(edge e0, edge e1, int32_t ymin, int32_t ymax, ScanLine sca double d0 = e0.dx > 0; // use y + 1 to compute x0 double d1 = e1.dx < 0; // use y + 1 to compute x1 for (int32_t y = y0; y < y1; y++) { - double x0 = m0 * ::fmax(0, ::fmin(e0.dy, y + d0 - e0.y0)) + e0.x0; - double x1 = m1 * ::fmax(0, ::fmin(e1.dy, y + d1 - e1.y0)) + e1.x0; - scanLine(std::floor(x1), std::ceil(x0), y); + double x0 = m0 * util::max(0.0, util::min(e0.dy, y + d0 - e0.y0)) + e0.x0; + double x1 = m1 * util::max(0.0, util::min(e1.dy, y + d1 - e1.y0)) + e1.x0; + scanLine(util::clampedFloor(x1), std::ceil(x0), y); } } |