libcuproj  24.02.00
Loading...
Searching...
No Matches
clamp_angular_coordinates.cuh
Go to the documentation of this file.
1/*
2 * Copyright (c) 2023, NVIDIA CORPORATION.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#pragma once
18
19#include <cuproj/detail/utility/cuda.hpp>
20#include <cuproj/detail/wrap_to_pi.hpp>
21#include <cuproj/error.hpp>
24
25#include <thrust/iterator/transform_iterator.h>
26
27#include <algorithm>
28
29namespace cuproj {
30
43template <typename Coordinate, typename T = typename Coordinate::value_type>
45 public:
51 CUPROJ_HOST_DEVICE clamp_angular_coordinates(projection_parameters<T> const& params)
52 : lam0_(params.lam0_), prime_meridian_offset_(params.prime_meridian_offset_)
53 {
54 }
55
56 // projection_parameters<T> setup(projection_parameters<T> const& params) { return params; }
57
65 CUPROJ_HOST_DEVICE Coordinate operator()(Coordinate const& coord, direction dir) const
66 {
67 if (dir == direction::FORWARD)
68 return forward(coord);
69 else
70 return inverse(coord);
71 }
72
73 private:
84 CUPROJ_HOST_DEVICE Coordinate forward(Coordinate const& coord) const
85 {
86 // check for latitude or longitude over-range
87 T t = (coord.y < 0 ? -coord.y : coord.y) - M_PI_2;
88 CUPROJ_HOST_DEVICE_EXPECTS(t <= EPSILON_RADIANS<T>, "Invalid latitude");
89 CUPROJ_HOST_DEVICE_EXPECTS(coord.x <= 10 || coord.x >= -10, "Invalid longitude");
90
91 Coordinate xy = coord;
92
93 /* Clamp latitude to -pi/2..pi/2 degree range */
94 auto half_pi = static_cast<T>(M_PI_2);
95 xy.y = std::clamp(xy.y, -half_pi, half_pi);
96
97 // Distance from central meridian, taking system zero meridian into account
98 xy.x = (xy.x - prime_meridian_offset_) - lam0_;
99
100 // Ensure longitude is in the -pi:pi range
101 xy.x = detail::wrap_to_pi(xy.x);
102
103 return xy;
104 }
105
115 CUPROJ_HOST_DEVICE Coordinate inverse(Coordinate const& coord) const
116 {
117 Coordinate xy = coord;
118
119 // Distance from central meridian, taking system zero meridian into account
120 xy.x += prime_meridian_offset_ + lam0_;
121
122 // Ensure longitude is in the -pi:pi range
123 xy.x = detail::wrap_to_pi(xy.x);
124
125 return xy;
126 }
127
128 T lam0_{}; // central meridian
129 T prime_meridian_offset_{};
130};
131
136} // namespace cuproj
Clamp angular coordinates to the valid range and offset by the central meridian (lam0) and an optiona...
CUPROJ_HOST_DEVICE clamp_angular_coordinates(projection_parameters< T > const &params)
Construct a new clamp angular coordinates object.
CUPROJ_HOST_DEVICE Coordinate operator()(Coordinate const &coord, direction dir) const
Clamp angular coordinate to the valid range.
Base class for all transform operations.
Definition operation.cuh:63
#define CUPROJ_HOST_DEVICE_EXPECTS(cond, reason)
Macro for checking (pre-)conditions that throws an exception when a condition is violated.
Definition error.hpp:100
direction
Enumerates the direction of a transform operation.
Definition operation.cuh:44