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
73
80Array abs(const Array &array);
81
97Array abs_smooth(const Array &array, float mu, const Array &vshift);
98Array abs_smooth(const Array &array, float mu, float vshift);
99Array abs_smooth(const Array &array, float mu);
100float abs_smooth(float a, float mu);
101
112Array almost_unit_identity(const Array &array);
113float almost_unit_identity(float x);
114
122float almost_unit_identity_c2(float x);
123float almost_unit_identity_c2(const Array &array);
124
132inline float approx_hypot(float a, float b)
133{
134 a = std::abs(a);
135 b = std::abs(b);
136 if (a > b) std::swap(a, b);
137 return 0.414f * a + b;
138}
139
146inline float approx_rsqrt(float a)
147{
148 union
149 {
150 float f;
151 uint32_t i;
152 } conv = {.f = a};
153 conv.i = 0x5f3759df - (conv.i >> 1);
154 conv.f *= 1.5F - (a * 0.5F * conv.f * conv.f);
155 return conv.f;
156}
157
164Array atan(const Array &array);
165
180Array atan2(const Array &y, const Array &x);
181
188int ceil_div(int a, int b);
189
196Array cos(const Array &array);
197
204Array exp(const Array &array);
205
206float gain(float x, float factor);
207
221Array gaussian_decay(const Array &array, float sigma);
222
228std::function<float(float, float)> get_distance_function(
229 DistanceFunction dist_fct);
230
254std::function<float(float)> get_phasor_profile_function(
255 PhasorProfile phasor_profile,
256 float delta,
257 float *p_profile_avg = nullptr);
258
273std::function<float(float)> get_radial_profile_function(
274 RadialProfile radial_profile,
275 float delta);
276
285int highest_power_of_2(int n);
286
297Array hypot(const Array &array1, const Array &array2);
298
306Array is_equal(const Array &array, float value);
307
317Array is_non_zero(const Array &array);
318
328Array is_zero(const Array &array);
329
338Array lerp(const Array &array1, const Array &array2, const Array &t);
339Array lerp(const Array &array1, const Array &array2, float t);
340float lerp(float a, float b, float t);
341
348Array log10(const Array &array);
349
356Array pow(const Array &array, float exp);
357
374void radial_displacement_to_xy(const Array &dr,
375 Array &dx,
376 Array &dy,
377 float smoothing = 1.f,
378 glm::vec2 center = {0.5f, 0.5f},
379 glm::vec4 bbox = {0.f, 1.f, 0.f, 1.f});
380
395void rotate_displacement(const Array &delta, float angle, Array &dx, Array &dy);
396
421float sigmoid(float x,
422 float width = 1.f,
423 float vmin = 0.f,
424 float vmax = 1.f,
425 float x0 = 0.f);
426
452Array sigmoid(const Array &array,
453 float width = 1.f,
454 float vmin = 0.f,
455 float vmax = 1.f,
456 float x0 = 0.f);
457
464Array sin(const Array &array);
465
480Array smoothstep3(const Array &array, float vmin = 0.f, float vmax = 1.f);
481
488float smoothstep3(float x);
489
497float smoothstep3_lower(float x);
498Array smoothstep3_lower(const Array &x);
499
507float smoothstep3_upper(float x);
508Array smoothstep3_upper(const Array &x);
509
524Array smoothstep5(const Array &array, float vmin = 0.f, float vmax = 1.f);
525Array smoothstep5(const Array &array,
526 const Array &vmin,
527 const Array &vmax);
528
535float smoothstep5(float x);
536
544float smoothstep5_lower(float x);
545Array smoothstep5_lower(const Array &x);
546
554float smoothstep5_upper(float x);
555Array smoothstep5_upper(const Array &x);
556
563float smoothstep7(float x);
564Array smoothstep7(const Array &x);
565
572Array sqrt(const Array &array);
573
574Array sqrt_safe(const Array &array);
575
576} // 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:22
Array sin(const Array &array)
Return the sine of the array elements.
Definition math.cpp:297
Array pow(const Array &array, float exp)
Return the array elements raised to the power 'exp'.
Definition math.cpp:235
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:113
RadialProfile
Radial profile type.
Definition math.hpp:66
@ RP_POW
Definition math.hpp:69
@ RP_LINEAR
Definition math.hpp:68
@ RP_GAIN
Definition math.hpp:67
@ RP_SMOOTHSTEP_UPPER
Definition math.hpp:71
@ RP_SMOOTHSTEP
Definition math.hpp:70
Array almost_unit_identity(const Array &array)
Return the almost unit identity function.
Definition math.cpp:76
Array sqrt_safe(const Array &array)
Definition math.cpp:474
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:188
Array is_zero(const Array &array)
Returns a binary mask of zero elements.
Definition math.cpp:198
std::function< float(float)> get_phasor_profile_function(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 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:307
Array cos(const Array &array)
Return the cosine of the array elements.
Definition math.cpp:130
float smoothstep3_lower(float x)
Return the 3rd order smoothstep function, with zero derivative only at 0.
Definition math.cpp:334
Array abs(const Array &array)
Return the absolute value of the array elements.
Definition math.cpp:13
int ceil_div(int a, int b)
Integer ceiling division.
Definition math.cpp:125
std::function< float(float)> get_radial_profile_function(RadialProfile radial_profile, float delta)
Returns a normalized radial profile function.
Definition radial_profile_function.cpp:14
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:464
Array log10(const Array &array)
Return the log10 of the array elements.
Definition math.cpp:225
float approx_rsqrt(float a)
Return the approximate inverse square root of a number.
Definition math.hpp:146
float approx_hypot(float a, float b)
Return the approximate hypothenuse of two numbers.
Definition math.hpp:132
Array gaussian_decay(const Array &array, float sigma)
Return the Gaussian of the array elements.
Definition math.cpp:156
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:364
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:140
float smoothstep5_upper(float x)
Return the 5rd order smoothstep function, with zero derivative only at 1.
Definition math.cpp:428
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:86
void gain(Array &array, float factor, const Array *p_mask)
Apply a gain correction to the array elements.
Definition filters.cpp:189
Array is_equal(const Array &array, float value)
Returns a binary mask of elements equal to value.
Definition math.cpp:178
Array atan(const Array &array)
Return the arctan of the array elements.
Definition math.cpp:103
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:413
Array hypot(const Array &array1, const Array &array2)
Definition math.cpp:167
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:279
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:272
float smoothstep7(float x)
Return the 7th order smoothstep function.
Definition math.cpp:443
float smoothstep3_upper(float x)
Return the 3rd order smoothstep function, with zero derivative only at 1.
Definition math.cpp:349
void radial_displacement_to_xy(const Array &dr, Array &dx, Array &dy, float smoothing=1.f, glm::vec2 center={0.5f, 0.5f}, glm::vec4 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:245