EBGeometry
Compact, header-only C++ library for fast evaluation of signed distance functions
Loading...
Searching...
No Matches
EBGeometry_Blend.hpp
Go to the documentation of this file.
1// SPDX-FileCopyrightText: 2022 Robert Marskar <robert.marskar@sintef.no>
2//
3// SPDX-License-Identifier: GPL-3.0-or-later
4
13#ifndef EBGEOMETRY_BLEND_HPP
14#define EBGEOMETRY_BLEND_HPP
15
16// Std includes
17#include <cmath>
18#include <type_traits>
19
20// Our includes
21#include "EBGeometry_GPU.hpp"
22#include "EBGeometry_Macros.hpp"
23#include "EBGeometry_Math.hpp"
24
25namespace EBGeometry {
26
37template <class T>
39{
40 static_assert(std::is_floating_point_v<T>, "ExpMinOp requires a floating-point type T");
41
49 [[nodiscard]] EBGEOMETRY_HOST_DEVICE
50 T
51 operator()(const T& a, const T& b, const T& s) const noexcept
52 {
53 EBGEOMETRY_EXPECT(s > T(0));
54
55 return Math::min(a, b) - s * std::log1p(std::exp(-std::abs(a - b) / s));
56 }
57};
58
67template <class T>
69{
70 static_assert(std::is_floating_point_v<T>, "ExpMaxOp requires a floating-point type T");
71
79 [[nodiscard]] EBGEOMETRY_HOST_DEVICE
80 T
81 operator()(const T& a, const T& b, const T& s) const noexcept
82 {
83 EBGEOMETRY_EXPECT(s > T(0));
84
85 return Math::max(a, b) + s * std::log1p(std::exp(-std::abs(a - b) / s));
86 }
87};
88
97template <class T>
99{
100 static_assert(std::is_floating_point_v<T>, "SmoothMinOp requires a floating-point type T");
101
109 [[nodiscard]] EBGEOMETRY_HOST_DEVICE
110 T
111 operator()(const T& a, const T& b, const T& s) const noexcept
112 {
113 EBGEOMETRY_EXPECT(s > T(0));
114
115 const T h = Math::max(s - std::abs(a - b), T(0)) / s;
116
117 return Math::min(a, b) - T(0.25) * h * h * s;
118 }
119};
120
128template <class T>
130{
131 static_assert(std::is_floating_point_v<T>, "SmoothMaxOp requires a floating-point type T");
132
140 [[nodiscard]] EBGEOMETRY_HOST_DEVICE
141 T
142 operator()(const T& a, const T& b, const T& s) const noexcept
143 {
144 EBGEOMETRY_EXPECT(s > T(0));
145
146 const T h = Math::max(s - std::abs(a - b), T(0)) / s;
147
148 return Math::max(a, b) + T(0.25) * h * h * s;
149 }
150};
151
157template <class T>
158inline constexpr ExpMinOp<T> ExpMin{};
159
165template <class T>
166inline constexpr ExpMaxOp<T> ExpMax{};
167
173template <class T>
174inline constexpr SmoothMinOp<T> SmoothMin{};
175
181template <class T>
182inline constexpr SmoothMaxOp<T> SmoothMax{};
183
184} // namespace EBGeometry
185
186#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
Utility macros for EBGeometry.
#define EBGEOMETRY_EXPECT(cond)
Runtime precondition assertion for EBGeometry.
Definition EBGeometry_Macros.hpp:155
Scalar helpers and numeric limits that compile for both host and device.
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
constexpr ExpMaxOp< T > ExpMax
The exponential smooth maximum, as a ready-made function object: ExpMax<T>(a, b, s).
Definition EBGeometry_Blend.hpp:166
constexpr ExpMinOp< T > ExpMin
The exponential smooth minimum, as a ready-made function object: ExpMin<T>(a, b, s).
Definition EBGeometry_Blend.hpp:158
constexpr SmoothMaxOp< T > SmoothMax
The polynomial smooth maximum, as a ready-made function object: SmoothMax<T>(a, b,...
Definition EBGeometry_Blend.hpp:182
constexpr SmoothMinOp< T > SmoothMin
The polynomial smooth minimum, as a ready-made function object: SmoothMin<T>(a, b,...
Definition EBGeometry_Blend.hpp:174
Exponential smooth maximum for blending two signed-distance values.
Definition EBGeometry_Blend.hpp:69
T operator()(const T &a, const T &b, const T &s) const noexcept
Evaluate the exponential smooth maximum.
Definition EBGeometry_Blend.hpp:81
Exponential smooth minimum for blending two signed-distance values.
Definition EBGeometry_Blend.hpp:39
T operator()(const T &a, const T &b, const T &s) const noexcept
Evaluate the exponential smooth minimum.
Definition EBGeometry_Blend.hpp:51
Quadratic polynomial smooth maximum for blending two signed-distance values.
Definition EBGeometry_Blend.hpp:130
T operator()(const T &a, const T &b, const T &s) const noexcept
Evaluate the polynomial smooth maximum.
Definition EBGeometry_Blend.hpp:142
Quadratic polynomial smooth minimum for blending two signed-distance values.
Definition EBGeometry_Blend.hpp:99
T operator()(const T &a, const T &b, const T &s) const noexcept
Evaluate the polynomial smooth minimum.
Definition EBGeometry_Blend.hpp:111