Skip to content

Commit

Permalink
To improve compatibility, implement custom clamp() method.
Browse files Browse the repository at this point in the history
  • Loading branch information
harrism committed Nov 26, 2024
1 parent 7ebe1d3 commit c9b7072
Showing 1 changed file with 13 additions and 5 deletions.
18 changes: 13 additions & 5 deletions cpp/cuproj/include/cuproj/operation/clamp_angular_coordinates.cuh
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
#include <cuproj/operation/operation.cuh>
#include <cuproj/projection_parameters.hpp>

#include <cuda/std/__algorithm/clamp.h>
#include <thrust/iterator/transform_iterator.h>

namespace cuproj {
Expand Down Expand Up @@ -61,7 +60,8 @@ class clamp_angular_coordinates : operation<Coordinate> {
* @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);
Expand All @@ -80,7 +80,7 @@ class clamp_angular_coordinates : operation<Coordinate> {
* @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;
Expand All @@ -91,7 +91,7 @@ class clamp_angular_coordinates : operation<Coordinate> {

/* Clamp latitude to -pi/2..pi/2 degree range */
auto half_pi = static_cast<T>(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_;
Expand All @@ -111,7 +111,7 @@ class clamp_angular_coordinates : operation<Coordinate> {
* @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;

Expand All @@ -124,6 +124,14 @@ class clamp_angular_coordinates : operation<Coordinate> {
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_{};
};
Expand Down

0 comments on commit c9b7072

Please sign in to comment.