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
75
82Array abs(const Array &array);
83
99Array abs_smooth(const Array &array, float mu, const Array &vshift);
100Array abs_smooth(const Array &array, float mu, float vshift);
101Array abs_smooth(const Array &array, float mu);
102float abs_smooth(float a, float mu);
103
114Array almost_unit_identity(const Array &array);
115float almost_unit_identity(float x);
116
124float almost_unit_identity_c2(float x);
125float almost_unit_identity_c2(const Array &array);
126
134inline float approx_hypot(float a, float b)
135{
136 a = std::abs(a);
137 b = std::abs(b);
138 if (a > b) std::swap(a, b);
139 return 0.414f * a + b;
140}
141
148inline float approx_rsqrt(float a)
149{
150 union
151 {
152 float f;
153 uint32_t i;
154 } conv = {.f = a};
155 conv.i = 0x5f3759df - (conv.i >> 1);
156 conv.f *= 1.5F - (a * 0.5F * conv.f * conv.f);
157 return conv.f;
158}
159
166Array atan(const Array &array);
167
182Array atan2(const Array &y, const Array &x);
183
190int ceil_div(int a, int b);
191
198Array cos(const Array &array);
199
206Array exp(const Array &array);
207
208float gain(float x, float factor);
209
223Array gaussian_decay(const Array &array, float sigma);
224
230std::function<float(float, float)> get_distance_function(
231 DistanceFunction dist_fct);
232
256std::function<float(float)> get_phasor_profile_function(
257 PhasorProfile phasor_profile,
258 float delta,
259 float *p_profile_avg = nullptr);
260
275std::function<float(float)> get_radial_profile_function(
276 RadialProfile radial_profile,
277 float delta);
278
287int highest_power_of_2(int n);
288
299Array hypot(const Array &array1, const Array &array2);
300
308Array is_equal(const Array &array, float value);
309
319Array is_non_zero(const Array &array);
320
330Array is_zero(const Array &array);
331
340Array lerp(const Array &array1, const Array &array2, const Array &t);
341Array lerp(const Array &array1, const Array &array2, float t);
342float lerp(float a, float b, float t);
343
350Array log10(const Array &array);
351
358Array pow(const Array &array, float exp);
359
376void radial_displacement_to_xy(const Array &dr,
377 Array &dx,
378 Array &dy,
379 float smoothing = 1.f,
380 glm::vec2 center = {0.5f, 0.5f},
381 glm::vec4 bbox = {0.f, 1.f, 0.f, 1.f});
382
397void rotate_displacement(const Array &delta, float angle, Array &dx, Array &dy);
398
423float sigmoid(float x,
424 float width = 1.f,
425 float vmin = 0.f,
426 float vmax = 1.f,
427 float x0 = 0.f);
428
454Array sigmoid(const Array &array,
455 float width = 1.f,
456 float vmin = 0.f,
457 float vmax = 1.f,
458 float x0 = 0.f);
459
466Array sin(const Array &array);
467
482Array smoothstep3(const Array &array, float vmin = 0.f, float vmax = 1.f);
483
490float smoothstep3(float x);
491
499float smoothstep3_lower(float x);
500Array smoothstep3_lower(const Array &x);
501
509float smoothstep3_upper(float x);
510Array smoothstep3_upper(const Array &x);
511
526Array smoothstep5(const Array &array, float vmin = 0.f, float vmax = 1.f);
527Array smoothstep5(const Array &array,
528 const Array &vmin,
529 const Array &vmax);
530
537float smoothstep5(float x);
538
546float smoothstep5_lower(float x);
547Array smoothstep5_lower(const Array &x);
548
556float smoothstep5_upper(float x);
557Array smoothstep5_upper(const Array &x);
558
565float smoothstep7(float x);
566Array smoothstep7(const Array &x);
567
574Array sqrt(const Array &array);
575
584Array sqrt_safe(const Array &array);
585
591float threshold(float x, float x0, float x1);
592
601Array threshold(const Array &x, float x0, float x1);
602
609float threshold_smooth(float x, float x0, float x1);
610
619Array threshold_smooth(const Array &x, float x0, float x1);
620
632float triangle(float x, float vmin, float vmax);
633
634} // 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:23
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
float threshold(float x, float x0, float x1)
Applies a linear threshold to a scalar value.
Definition math.cpp:498
RadialProfile
Radial profile type.
Definition math.hpp:66
@ RP_FLAT_BOTTOM
Definition math.hpp:72
@ 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
@ RP_SQRT
Definition math.hpp:73
Array almost_unit_identity(const Array &array)
Return the almost unit identity function.
Definition math.cpp:76
Array sqrt_safe(const Array &array)
Computes a safe square root element-wise on an array.
Definition math.cpp:488
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
@ PP_COSINE_PEAKY
Definition math.hpp:56
@ PP_COSINE_BULKY
Definition math.hpp:55
@ PP_TRIANGLE
Definition math.hpp:59
@ PP_COSINE_SQUARE
Definition math.hpp:57
@ PP_COSINE_STD
Definition math.hpp:58
Array sqrt(const Array &array)
Return the square root of the array elements.
Definition math.cpp:478
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:148
float approx_hypot(float a, float b)
Return the approximate hypothenuse of two numbers.
Definition math.hpp:134
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:378
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:442
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
float threshold_smooth(float x, float x0, float x1)
Applies a smooth threshold to a scalar value.
Definition math.cpp:515
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:215
float smoothstep5_lower(float x)
Return the 5rd order smoothstep function, with zero derivative only at 0.
Definition math.cpp:427
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 triangle(float x, float vmin, float vmax)
Triangle function between two bounds.
Definition math.cpp:532
float smoothstep7(float x)
Return the 7th order smoothstep function.
Definition math.cpp:457
float smoothstep3_upper(float x)
Return the 3rd order smoothstep function, with zero derivative only at 1.
Definition math.cpp:363
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