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
269Array is_non_zero(const Array &array);
270
280Array is_zero(const Array &array);
281
290Array lerp(const Array &array1, const Array &array2, const Array &t);
291Array lerp(const Array &array1, const Array &array2, float t);
292float lerp(float a, float b, float t);
293
300Array log10(const Array &array);
301
308Array pow(const Array &array, float exp);
309
326void radial_displacement_to_xy(const Array &dr,
327 Array &dx,
328 Array &dy,
329 float smoothing = 1.f,
330 Vec2<float> center = {0.5f, 0.5f},
331 Vec4<float> bbox = {0.f, 1.f, 0.f, 1.f});
332
347void rotate_displacement(const Array &delta, float angle, Array &dx, Array &dy);
348
373float sigmoid(float x,
374 float width = 1.f,
375 float vmin = 0.f,
376 float vmax = 1.f,
377 float x0 = 0.f);
378
404Array sigmoid(const Array &array,
405 float width = 1.f,
406 float vmin = 0.f,
407 float vmax = 1.f,
408 float x0 = 0.f);
409
416Array sin(const Array &array);
417
432Array smoothstep3(const Array &array, float vmin = 0.f, float vmax = 1.f);
433
440float smoothstep3(float x);
441
449float smoothstep3_lower(float x);
450Array smoothstep3_lower(const Array &x);
451
459float smoothstep3_upper(float x);
460Array smoothstep3_upper(const Array &x);
461
476Array smoothstep5(const Array &array, float vmin = 0.f, float vmax = 1.f);
477Array smoothstep5(const Array &array,
478 const Array &vmin,
479 const Array &vmax);
480
487float smoothstep5(float x);
488
496float smoothstep5_lower(float x);
497Array smoothstep5_lower(const Array &x);
498
506float smoothstep5_upper(float x);
507Array smoothstep5_upper(const Array &x);
508
515float smoothstep7(float x);
516Array smoothstep7(const Array &x);
517
524Array sqrt(const Array &array);
525
526Array sqrt_safe(const Array &array);
527
528} // 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:267
Array pow(const Array &array, float exp)
Return the array elements raised to the power 'exp'.
Definition math.cpp:205
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:444
std::function< float(float, float)> get_distance_function(DistanceFunction dist_fct)
Return the requested distance function.
Definition distance_function.cpp:12
Array is_non_zero(const Array &array)
Returns a binary mask of non-zero elements.
Definition math.cpp:158
Array is_zero(const Array &array)
Returns a binary mask of zero elements.
Definition math.cpp:168
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:277
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:304
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:434
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:195
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:215
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:334
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:398
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:346
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:383
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:249
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:242
float smoothstep7(float x)
Return the 7th order smoothstep function.
Definition math.cpp:413
float smoothstep3_upper(float x)
Return the 3rd order smoothstep function, with zero derivative only at 1.
Definition math.cpp:319