EBGeometry
Compact, header-only C++ library for fast evaluation of signed distance functions
Loading...
Searching...
No Matches
EBGeometry_Math.hpp
Go to the documentation of this file.
1// SPDX-FileCopyrightText: 2026 Robert Marskar <robert.marskar@sintef.no>
2//
3// SPDX-License-Identifier: GPL-3.0-or-later
4
16#ifndef EBGEOMETRY_MATH_HPP
17#define EBGEOMETRY_MATH_HPP
18
19#include <cfloat>
20#include <climits>
21#include <cmath>
22
23#include "EBGeometry_GPU.hpp"
24
25namespace EBGeometry {
26
30namespace Math {
31
41template <class T>
42[[nodiscard]] EBGEOMETRY_HOST_DEVICE
43constexpr T
44min(const T& a_a, const T& a_b) noexcept
45{
46 return (a_b < a_a) ? a_b : a_a;
47}
48
58template <class T>
59[[nodiscard]] EBGEOMETRY_HOST_DEVICE
60constexpr T
61max(const T& a_a, const T& a_b) noexcept
62{
63 return (a_a < a_b) ? a_b : a_a;
64}
65
75template <class T>
76[[nodiscard]] EBGEOMETRY_HOST_DEVICE
77constexpr T
78clamp(const T& a_value, const T& a_lo, const T& a_hi) noexcept
79{
80 return (a_value < a_lo) ? a_lo : ((a_hi < a_value) ? a_hi : a_value);
81}
82
90template <class T>
91struct Limits;
92
96template <>
97struct Limits<float>
98{
103 [[nodiscard]] EBGEOMETRY_HOST_DEVICE
104 static constexpr float
105 max() noexcept
106 {
107 return FLT_MAX;
108 }
109
114 [[nodiscard]] EBGEOMETRY_HOST_DEVICE
115 static constexpr float
116 lowest() noexcept
117 {
118 return -FLT_MAX;
119 }
120
125 [[nodiscard]] EBGEOMETRY_HOST_DEVICE
126 static constexpr float
127 min() noexcept
128 {
129 return FLT_MIN;
130 }
131
136 [[nodiscard]] EBGEOMETRY_HOST_DEVICE
137 static constexpr float
138 epsilon() noexcept
139 {
140 return FLT_EPSILON;
141 }
142
147 [[nodiscard]] EBGEOMETRY_HOST_DEVICE
148 static constexpr float
149 infinity() noexcept
150 {
151 return HUGE_VALF;
152 }
153};
154
158template <>
159struct Limits<double>
160{
165 [[nodiscard]] EBGEOMETRY_HOST_DEVICE
166 static constexpr double
167 max() noexcept
168 {
169 return DBL_MAX;
170 }
171
176 [[nodiscard]] EBGEOMETRY_HOST_DEVICE
177 static constexpr double
178 lowest() noexcept
179 {
180 return -DBL_MAX;
181 }
182
187 [[nodiscard]] EBGEOMETRY_HOST_DEVICE
188 static constexpr double
189 min() noexcept
190 {
191 return DBL_MIN;
192 }
193
198 [[nodiscard]] EBGEOMETRY_HOST_DEVICE
199 static constexpr double
200 epsilon() noexcept
201 {
202 return DBL_EPSILON;
203 }
204
209 [[nodiscard]] EBGEOMETRY_HOST_DEVICE
210 static constexpr double
211 infinity() noexcept
212 {
213 return HUGE_VAL;
214 }
215};
216
223template <class T, T Lowest, T Max>
225{
230 [[nodiscard]] EBGEOMETRY_HOST_DEVICE
231 static constexpr T
232 max() noexcept
233 {
234 return Max;
235 }
236
241 [[nodiscard]] EBGEOMETRY_HOST_DEVICE
242 static constexpr T
243 lowest() noexcept
244 {
245 return Lowest;
246 }
247
252 [[nodiscard]] EBGEOMETRY_HOST_DEVICE
253 static constexpr T
254 min() noexcept
255 {
256 return Lowest;
257 }
258};
259
261template <>
262struct Limits<int> : IntegerLimits<int, INT_MIN, INT_MAX>
263{
264};
265
267template <>
268struct Limits<unsigned int> : IntegerLimits<unsigned int, 0U, UINT_MAX>
269{
270};
271
273template <>
274struct Limits<long> : IntegerLimits<long, LONG_MIN, LONG_MAX>
275{
276};
277
279template <>
280struct Limits<unsigned long> : IntegerLimits<unsigned long, 0UL, ULONG_MAX>
281{
282};
283
285template <>
286struct Limits<long long> : IntegerLimits<long long, LLONG_MIN, LLONG_MAX>
287{
288};
289
291template <>
292struct Limits<unsigned long long> : IntegerLimits<unsigned long long, 0ULL, ULLONG_MAX>
293{
294};
295
296} // namespace Math
297
298} // namespace EBGeometry
299
300#endif
GPU portability layer: host/device decoration macros and backend detection.
#define EBGEOMETRY_HOST_DEVICE
Mark a function as callable from both host and device code (no-op on host compilers).
Definition EBGeometry_GPU.hpp:80
constexpr T clamp(const T &a_value, const T &a_lo, const T &a_hi) noexcept
A value limited to [lo, hi].
Definition EBGeometry_Math.hpp:78
constexpr T max(const T &a_a, const T &a_b) noexcept
The larger of two values.
Definition EBGeometry_Math.hpp:61
constexpr T min(const T &a_a, const T &a_b) noexcept
The smaller of two values.
Definition EBGeometry_Math.hpp:44
Namespace containing all of EBGeometry's functionality.
Definition EBGeometry_Affine.hpp:26
Numeric limits of an integer type, from its climits constants.
Definition EBGeometry_Math.hpp:225
static constexpr T min() noexcept
Smallest value (for an integer type, the same as lowest()).
Definition EBGeometry_Math.hpp:254
static constexpr T max() noexcept
Largest value.
Definition EBGeometry_Math.hpp:232
static constexpr T lowest() noexcept
Smallest value.
Definition EBGeometry_Math.hpp:243
static constexpr double infinity() noexcept
Positive infinity.
Definition EBGeometry_Math.hpp:211
static constexpr double epsilon() noexcept
Difference between 1 and the next representable value.
Definition EBGeometry_Math.hpp:200
static constexpr double min() noexcept
Smallest positive normal value.
Definition EBGeometry_Math.hpp:189
static constexpr double lowest() noexcept
Most negative finite value.
Definition EBGeometry_Math.hpp:178
static constexpr double max() noexcept
Largest finite value.
Definition EBGeometry_Math.hpp:167
static constexpr float max() noexcept
Largest finite value.
Definition EBGeometry_Math.hpp:105
static constexpr float min() noexcept
Smallest positive normal value.
Definition EBGeometry_Math.hpp:127
static constexpr float epsilon() noexcept
Difference between 1 and the next representable value.
Definition EBGeometry_Math.hpp:138
static constexpr float lowest() noexcept
Most negative finite value.
Definition EBGeometry_Math.hpp:116
static constexpr float infinity() noexcept
Positive infinity.
Definition EBGeometry_Math.hpp:149
Numeric limits, with the meanings of the std::numeric_limits members of the same names.
Definition EBGeometry_Math.hpp:91