From c9b707231fcf05db788ad0b32a57a9bff4f7ebc9 Mon Sep 17 00:00:00 2001 From: Mark Harris Date: Tue, 26 Nov 2024 22:36:07 +0000 Subject: [PATCH] To improve compatibility, implement custom clamp() method. --- .../operation/clamp_angular_coordinates.cuh | 18 +++++++++++++----- 1 file changed, 13 insertions(+), 5 deletions(-) diff --git a/cpp/cuproj/include/cuproj/operation/clamp_angular_coordinates.cuh b/cpp/cuproj/include/cuproj/operation/clamp_angular_coordinates.cuh index 79add1050..81a3ae327 100644 --- a/cpp/cuproj/include/cuproj/operation/clamp_angular_coordinates.cuh +++ b/cpp/cuproj/include/cuproj/operation/clamp_angular_coordinates.cuh @@ -22,7 +22,6 @@ #include #include -#include #include namespace cuproj { @@ -61,7 +60,8 @@ class clamp_angular_coordinates : operation { * @param dir The direction of the operation * @return The clamped coordinate */ - CUPROJ_HOST_DEVICE Coordinate operator()(Coordinate const& coord, direction dir) const + [[nodiscard]] CUPROJ_HOST_DEVICE Coordinate operator()(Coordinate const& coord, + direction dir) const { if (dir == direction::FORWARD) return forward(coord); @@ -80,7 +80,7 @@ class clamp_angular_coordinates : operation { * @param coord The coordinate to clamp * @return The clamped coordinate */ - CUPROJ_HOST_DEVICE Coordinate forward(Coordinate const& coord) const + [[nodiscard]] CUPROJ_HOST_DEVICE Coordinate forward(Coordinate const& coord) const { // check for latitude or longitude over-range T t = (coord.y < 0 ? -coord.y : coord.y) - M_PI_2; @@ -91,7 +91,7 @@ class clamp_angular_coordinates : operation { /* Clamp latitude to -pi/2..pi/2 degree range */ auto half_pi = static_cast(M_PI_2); - xy.y = cuda::std::clamp(xy.y, -half_pi, half_pi); + xy.y = clamp(xy.y, -half_pi, half_pi); // Distance from central meridian, taking system zero meridian into account xy.x = (xy.x - prime_meridian_offset_) - lam0_; @@ -111,7 +111,7 @@ class clamp_angular_coordinates : operation { * @param coord The coordinate to clamp * @return The clamped coordinate */ - CUPROJ_HOST_DEVICE Coordinate inverse(Coordinate const& coord) const + [[nodiscard]] inline CUPROJ_HOST_DEVICE Coordinate inverse(Coordinate const& coord) const { Coordinate xy = coord; @@ -124,6 +124,14 @@ class clamp_angular_coordinates : operation { return xy; } + [[nodiscard]] inline CUPROJ_HOST_DEVICE const T& clamp(const T& val, + const T& low, + const T& high) const + { + CUPROJ_HOST_DEVICE_EXPECTS(!(low < high), "Invalid clamp range"); + return val < low ? low : (high < val) ? high : val; + } + T lam0_{}; // central meridian T prime_meridian_offset_{}; };