HighMap library (C++)
Loading...
Searching...
No Matches
core.hpp
Go to the documentation of this file.
1/* Copyright (c) 2023 Otto Link. Distributed under the terms of the GNU General
2 Public License. The full license is in the file LICENSE, distributed with
3 this software. */
4
10#pragma once
11#include <cmath>
12#include <cstdint>
13
14namespace hmap
15{
16
23float abs_smooth(float a, float mu);
24
33float almost_unit_identity(float x);
34
42float almost_unit_identity_c2(float x);
43
51inline float approx_hypot(float a, float b)
52{
53 a = std::abs(a);
54 b = std::abs(b);
55 if (a > b) std::swap(a, b);
56 return 0.414f * a + b;
57}
58
65inline float approx_rsqrt(float a)
66{
67 union
68 {
69 float f;
70 uint32_t i;
71 } conv = {.f = a};
72
73 conv.i = 0x5f3759df - (conv.i >> 1);
74 conv.f *= 1.5F - (a * 0.5F * conv.f * conv.f);
75
76 return conv.f;
77}
78
86int ceil_div(int a, int b);
87
94float fast_exp(float x);
95
102float fast_log(float x);
103
110float gain(float x, float factor);
111
119
127float lerp(float a, float b, float t);
128
136float log_safe(float x, float eps = 1e-6f);
137
148float power_curve(float x, float a, float b);
149
165float r_min(float f1, float f2, float alpha = 0.f);
166
182float r_max(float f1, float f2, float alpha = 0.f);
183
200float sigmoid(float x,
201 float width = 1.f,
202 float vmin = 0.f,
203 float vmax = 1.f,
204 float x0 = 0.f);
205
211float smoothstep3(float x);
212
220float smoothstep3(float x, float clamp_min, float clamp_max);
221
227float smoothstep3_lower(float x);
228
234float smoothstep3_upper(float x);
235
241float smoothstep5(float x);
242
248float smoothstep5_lower(float x);
249
255float smoothstep5_upper(float x);
256
262float smoothstep7(float x);
263
272float threshold(float x, float x0, float x1);
273
281float threshold_smooth(float x, float x0, float x1);
282
294float trapeze(float x, float x0, float x1, float width);
295
304float trapeze_smooth(float x, float x0, float x1, float width);
305
314float triangle(float x, float vmin, float vmax);
315
316} // namespace hmap
float f(int i, float gi)
Definition distance_transform.cpp:14
Definition opencv.hpp:20
Array smoothstep3_upper(const Array &array)
Cubic smoothstep (zero derivative at 1 only).
Definition math.cpp:511
float trapeze_smooth(float x, float x0, float x1, float width)
Smooth trapezoidal pulse using cubic smoothstep interpolation.
Definition core.cpp:175
Array sigmoid(const Array &array, float width=1.f, float vmin=0.f, float vmax=1.f, float x0=0.f)
Sigmoid function.
Definition math.cpp:450
float fast_log(float x)
Fast natural logarithm approximation.
Definition core.cpp:47
Array smoothstep7(const Array &array)
Seventh-order smoothstep.
Definition math.cpp:604
Array smoothstep5_upper(const Array &array)
Quintic smoothstep (zero derivative at 1 only).
Definition math.cpp:592
void clamp_min(Array &array, float vmin)
Clamp array values lower than a given bound.
Definition range.cpp:155
Array almost_unit_identity(const Array &array)
Almost unit identity function.
Definition math.cpp:80
Array threshold(const Array &array, float x0, float x1)
Linear threshold.
Definition math.cpp:640
Array smoothstep3(const Array &array, float vmin=0.f, float vmax=1.f)
Cubic smoothstep.
Definition math.cpp:475
Array threshold_smooth(const Array &array, float x0, float x1)
Smooth threshold.
Definition math.cpp:652
float trapeze(float x, float x0, float x1, float width)
Computes a trapezoidal pulse function.
Definition core.cpp:162
int ceil_div(int a, int b)
Integer ceiling division.
Definition core.cpp:31
float approx_rsqrt(float a)
Fast inverse square root approximation.
Definition core.hpp:65
float approx_hypot(float a, float b)
Approximate hypotenuse.
Definition core.hpp:51
Array smoothstep3_lower(const Array &array)
Cubic smoothstep (zero derivative at 0 only).
Definition math.cpp:499
Array smoothstep5_lower(const Array &array)
Quintic smoothstep (zero derivative at 0 only).
Definition math.cpp:580
int highest_power_of_2(int n)
Highest power of 2 less than or equal to n.
Array smoothstep5(const Array &array, float vmin=0.f, float vmax=1.f)
Quintic smoothstep.
Definition math.cpp:523
Array r_max(const Array &array1, const Array &array2, float alpha=0.f)
Generalized Rvachev R-function for smooth maximum / union (exact zero-set).
Definition math.cpp:436
Array abs_smooth(const Array &array, float mu, const Array &vshift)
Smooth absolute value.
Definition math.cpp:61
void gain(Array &array, float factor, const Array *p_mask)
Apply a gain correction to the array elements.
Definition filters.cpp:251
Array log_safe(const Array &array, float eps=1e-6f)
Safe natural logarithm with lower clamp.
Definition math.cpp:390
float fast_exp(float x)
Fast exponential approximation.
Definition core.cpp:36
Array almost_unit_identity_c2(const Array &array)
Almost unit identity function (C2 variant).
Definition math.cpp:92
Point lerp(const Point &p1, const Point &p2, float t)
Linearly interpolates between two points.
Definition points.cpp:265
Array r_min(const Array &array1, const Array &array2, float alpha=0.f)
Generalized Rvachev R-function for smooth minimum / intersection (exact zero-set).
Definition math.cpp:422
float power_curve(float x, float a, float b)
Smooth asymmetric bell-shaped curve on [0,1].
Definition core.cpp:73
float triangle(float x, float vmin, float vmax)
Triangle function between bounds.
Definition core.cpp:180
void clamp_max(Array &array, float vmax)
Clamp array values larger than a given bound.
Definition range.cpp:87