HighMap library (C++)
Loading...
Searching...
No Matches
math.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
16#pragma once
17
18#ifndef M_PI
19#define M_PI 3.14159265358979323846
20#endif
21
22#ifndef M_PI_2
23#define M_PI_2 1.57079632679489661923
24#endif
25
26#ifndef M_SQRT2
27#define M_SQRT2 1.41421356237309504880
28#endif
29
30#ifndef M_SQRT1_2
31#define M_SQRT1_2 0.707106781186547524401
32#endif
33
34#include "highmap/array.hpp"
35
36namespace hmap
37{
38
49
61
68Array abs(const Array &array);
69
85Array abs_smooth(const Array &array, float mu, const Array &vshift);
86Array abs_smooth(const Array &array, float mu, float vshift);
87Array abs_smooth(const Array &array, float mu);
88float abs_smooth(float a, float mu);
89
100Array almost_unit_identity(const Array &array);
101float almost_unit_identity(float x);
102
110float almost_unit_identity_c2(float x);
111
119inline float approx_hypot(float a, float b)
120{
121 a = std::abs(a);
122 b = std::abs(b);
123 if (a > b) std::swap(a, b);
124 return 0.414f * a + b;
125}
126
133inline float approx_rsqrt(float a)
134{
135 union
136 {
137 float f;
138 uint32_t i;
139 } conv = {.f = a};
140 conv.i = 0x5f3759df - (conv.i >> 1);
141 conv.f *= 1.5F - (a * 0.5F * conv.f * conv.f);
142 return conv.f;
143}
144
151Array atan(const Array &array);
152
167Array atan2(const Array &y, const Array &x);
168
175Array cos(const Array &array);
176
183Array exp(const Array &array);
184
185float gain(float x, float factor);
186
200Array gaussian_decay(const Array &array, float sigma);
201
207std::function<float(float, float)> get_distance_function(
208 DistanceFunction dist_fct);
209
233std::function<float(float)> get_phasor_profile_function(
234 const PhasorProfile &phasor_profile,
235 float delta,
236 float *p_profile_avg = nullptr);
237
246int highest_power_of_2(int n);
247
258Array hypot(const Array &array1, const Array &array2);
259
268Array lerp(const Array &array1, const Array &array2, const Array &t);
269Array lerp(const Array &array1, const Array &array2, float t);
270float lerp(float a, float b, float t);
271
278Array log10(const Array &array);
279
286Array pow(const Array &array, float exp);
287
304void radial_displacement_to_xy(const Array &dr,
305 Array &dx,
306 Array &dy,
307 float smoothing = 1.f,
308 Vec2<float> center = {0.5f, 0.5f},
309 Vec4<float> bbox = {0.f, 1.f, 0.f, 1.f});
310
325void rotate_displacement(const Array &delta, float angle, Array &dx, Array &dy);
326
351float sigmoid(float x,
352 float width = 1.f,
353 float vmin = 0.f,
354 float vmax = 1.f,
355 float x0 = 0.f);
356
382Array sigmoid(const Array &array,
383 float width = 1.f,
384 float vmin = 0.f,
385 float vmax = 1.f,
386 float x0 = 0.f);
387
394Array sin(const Array &array);
395
410Array smoothstep3(const Array &array, float vmin = 0.f, float vmax = 1.f);
411
418float smoothstep3(float x);
419
427float smoothstep3_lower(float x);
428Array smoothstep3_lower(const Array &x);
429
437float smoothstep3_upper(float x);
438Array smoothstep3_upper(const Array &x);
439
454Array smoothstep5(const Array &array, float vmin = 0.f, float vmax = 1.f);
455Array smoothstep5(const Array &array,
456 const Array &vmin,
457 const Array &vmax);
458
465float smoothstep5(float x);
466
474float smoothstep5_lower(float x);
475Array smoothstep5_lower(const Array &x);
476
484float smoothstep5_upper(float x);
485Array smoothstep5_upper(const Array &x);
486
493float smoothstep7(float x);
494Array smoothstep7(const Array &x);
495
502Array sqrt(const Array &array);
503
504Array sqrt_safe(const Array &array);
505
506} // namespace hmap
Declaration of the Array class for 2D floating-point arrays with various mathematical operations and ...
float f(int i, float gi)
Definition distance_transform.cpp:10
Definition algebra.hpp:28
Array sin(const Array &array)
Return the sine of the array elements.
Definition math.cpp:247
Array pow(const Array &array, float exp)
Return the array elements raised to the power 'exp'.
Definition math.cpp:185
Array atan2(const Array &y, const Array &x)
Computes the element-wise arctangent of two arrays, considering the signs of both inputs.
Definition math.cpp:98
Array almost_unit_identity(const Array &array)
Return the almost unit identity function.
Definition math.cpp:71
Array sqrt_safe(const Array &array)
Definition math.cpp:424
std::function< float(float, float)> get_distance_function(DistanceFunction dist_fct)
Return the requested distance function.
Definition distance_function.cpp:12
Array smoothstep3(const Array &array, float vmin=0.f, float vmax=1.f)
Return the 3rd order smoothstep function of the array elements.
Definition math.cpp:257
Array cos(const Array &array)
Return the cosine of the array elements.
Definition math.cpp:110
float smoothstep3_lower(float x)
Return the 3rd order smoothstep function, with zero derivative only at 0.
Definition math.cpp:284
Array abs(const Array &array)
Return the absolute value of the array elements.
Definition math.cpp:13
DistanceFunction
Distance function type.
Definition math.hpp:43
@ CHEBYSHEV
Chebyshev.
Definition math.hpp:44
@ EUCLISHEV
Euclidian and Chebyshev mix.
Definition math.hpp:46
@ EUCLIDIAN
Euclidian.
Definition math.hpp:45
@ MANHATTAN
Manhattan.
Definition math.hpp:47
PhasorProfile
Phasor angular profile type.
Definition math.hpp:54
@ COSINE_SQUARE
Definition math.hpp:57
@ TRIANGLE
Definition math.hpp:59
@ COSINE_STD
Definition math.hpp:58
@ COSINE_PEAKY
Definition math.hpp:56
@ COSINE_BULKY
Definition math.hpp:55
Array sqrt(const Array &array)
Return the square root of the array elements.
Definition math.cpp:414
std::function< float(float)> get_phasor_profile_function(const PhasorProfile &phasor_profile, float delta, float *p_profile_avg=nullptr)
Generates a function representing a phasor profile based on the specified type and parameters.
Definition phasor_profile_function.cpp:14
Array log10(const Array &array)
Return the log10 of the array elements.
Definition math.cpp:175
float approx_rsqrt(float a)
Return the approximate inverse square root of a number.
Definition math.hpp:133
float approx_hypot(float a, float b)
Return the approximate hypothenuse of two numbers.
Definition math.hpp:119
void radial_displacement_to_xy(const Array &dr, Array &dx, Array &dy, float smoothing=1.f, Vec2< float > center={0.5f, 0.5f}, Vec4< float > bbox={0.f, 1.f, 0.f, 1.f})
Interpret the input array dr as a radial displacement and convert it to a pair of displacements dx an...
Definition math.cpp:195
Array gaussian_decay(const Array &array, float sigma)
Return the Gaussian of the array elements.
Definition math.cpp:136
int highest_power_of_2(int n)
Computes the highest power of 2 less than or equal to the given number.
Definition pyramid_decomposition.cpp:193
Array smoothstep5(const Array &array, float vmin=0.f, float vmax=1.f)
Return the 5rd order smoothstep function of the array elements.
Definition math.cpp:314
Array abs_smooth(const Array &array, float mu, const Array &vshift)
Return the smooth absolute value of the array elements.
Definition math.cpp:49
float angle(const Point &p1, const Point &p2)
Computes the angle between two points relative to the x-axis.
Definition points.cpp:42
Array exp(const Array &array)
Return the exponantial of the array elements.
Definition math.cpp:120
float smoothstep5_upper(float x)
Return the 5rd order smoothstep function, with zero derivative only at 1.
Definition math.cpp:378
float almost_unit_identity_c2(float x)
Return the almost unit identity function (with a second-order derivative equals 0 at x = 1 also to av...
Definition math.cpp:81
void gain(Array &array, float factor, const Array *p_mask)
Apply a gain correction to the array elements.
Definition filters.cpp:347
Array atan(const Array &array)
Return the arctan of the array elements.
Definition math.cpp:88
Point lerp(const Point &p1, const Point &p2, float t)
Linearly interpolates between two points.
Definition points.cpp:185
float smoothstep5_lower(float x)
Return the 5rd order smoothstep function, with zero derivative only at 0.
Definition math.cpp:363
Array hypot(const Array &array1, const Array &array2)
Definition math.cpp:147
float sigmoid(float x, float width=1.f, float vmin=0.f, float vmax=1.f, float x0=0.f)
Computes the sigmoid function for a scalar value.
Definition math.cpp:229
void rotate_displacement(const Array &delta, float angle, Array &dx, Array &dy)
Rotates a scalar displacement field into directional X and Y components.
Definition math.cpp:222
float smoothstep7(float x)
Return the 7th order smoothstep function.
Definition math.cpp:393
float smoothstep3_upper(float x)
Return the 3rd order smoothstep function, with zero derivative only at 1.
Definition math.cpp:299