libacfutils
A general purpose library of utility functions designed to make it easier to develop addons for the X-Plane flight simulator.
Loading...
Searching...
No Matches
Data Structures | Macros | Functions
math.h File Reference
#include "assert.h"
#include "math_core.h"
#include "geom.h"
Include dependency graph for math.h:

Go to the source code of this file.

Data Structures

struct  pn_interp_t
 

Macros

#define POW4(x)   ((x) * (x) * (x) * (x))
 
#define POW3(x)   ((x) * (x) * (x))
 
#define POW2(x)   ((x) * (x))
 
#define ABS(x)   ((x) > 0 ? (x) : -(x))
 
#define wavg(x, y, w)   wavg_impl((x), (y), (w), __FILE__, __LINE__)
 
#define MAX_PN_INTERP_ORDER   64
 
#define HROUND2(oldval, newval, step, hyst_rng)
 
#define HROUND(oldval, newval, step)    HROUND2((oldval), (newval), (step), 0.35)
 

Functions

unsigned quadratic_solve (double a, double b, double c, double x[2])
 
double fx_lin (double x, double x1, double y1, double x2, double y2)
 
double fx_lin_multi (double x, const struct vect2_s *points, bool_t extrapolate)
 
double fx_lin_multi2 (double x, const struct vect2_s *points, size_t n_points, bool_t extrapolate)
 
double * fx_lin_multi_inv (double y, const struct vect2_s *points, size_t *num_out)
 
double * fx_lin_multi_inv2 (double y, const struct vect2_s *points, bool_t extrapolate, size_t *num_out)
 
double * fx_lin_multi_inv3 (double y, const struct vect2_s *points, size_t n_points, bool_t extrapolate, size_t *num_out)
 
static double wavg_impl (double x, double y, double w, const char *file, int line)
 
static double wavg2 (double x, double y, double w)
 
static double iter_fract (double x, double min_val, double max_val, bool_t clamp_output)
 
void pn_interp_init (pn_interp_t *interp, const vect2_t *points, unsigned npts)
 
static double pn_interp_run (double x, const pn_interp_t *interp)
 
static double smoothstep (double x, double edge0, double edge1)
 
static double smoothstep_inv (double x)
 

Macro Definition Documentation

◆ ABS

#define ABS (   x)    ((x) > 0 ? (x) : -(x))
Returns
The absolute value of x.

Definition at line 39 of file math.h.

◆ HROUND

#define HROUND (   oldval,
  newval,
  step 
)     HROUND2((oldval), (newval), (step), 0.35)

Same as HROUND2(), but uses a default hysterhesis range value of 0.35. That means the new value will only influence oldval if it is at least step * (0.5 + 0.35) = step * 0.85 or 85% of the way to the nearest step value away from oldval.

Definition at line 237 of file math.h.

◆ HROUND2

#define HROUND2 (   oldval,
  newval,
  step,
  hyst_rng 
)
Value:
do { \
double tmpval = round((newval) / (step)) * (step); \
if (isnan((oldval))) { \
(oldval) = tmpval; \
} else if ( \
(newval) > (oldval) + (step) * (0.5 + (hyst_rng)) || \
(newval) < (oldval) - (step) * (0.5 + (hyst_rng))) { \
(oldval) = tmpval; \
} \
} while (0)

Hysterhesis-rounding macro. Given an old value, new value, rounding step and hysterhesis range, performs the following:

  • first rounds newval to the nearest multiple of step
  • if oldval is NAN, it simply adops the new rounded value
  • otherwise, if the new rounded value differs from oldval by at least half step plus the hysterhesis range fraction, then oldval is set to the new rounded value, otherwise it stays put.

The purpose of this macro is to make oldval change in fixed steps, but avoid oscillation if the new value is right in the middle between two step sizes:

oldval nearly halfway to next step will cause
| +--newval - oldval to start oscillating rapidly
| | between X and X+1
| |
V V
======+=========+=========+=========+=====
X-1 X X+1 X+2
| |
|<-step-->|

The HROUND2() macro provides an additional "buffer" zone around the midpoint of the step, to avoid this oscillation:

oldval
| newval newval must now cross beyond this
| | _point before it will cause oldval
| | / to change from X to X+1
V V V
=+==============+=======|====|==+====
X-1 X |<-->| X+1
| hyst_rng |
| |
|<----step----->|

Definition at line 219 of file math.h.

◆ MAX_PN_INTERP_ORDER

#define MAX_PN_INTERP_ORDER   64

Definition at line 119 of file math.h.

◆ POW2

#define POW2 (   x)    ((x) * (x))
Returns
The 2th power of x.

Definition at line 36 of file math.h.

◆ POW3

#define POW3 (   x)    ((x) * (x) * (x))
Returns
The 3th power of x.

Definition at line 34 of file math.h.

◆ POW4

#define POW4 (   x)    ((x) * (x) * (x) * (x))
Returns
The 4th power of x.

Definition at line 32 of file math.h.

◆ wavg

#define wavg (   x,
  y,
 
)    wavg_impl((x), (y), (w), __FILE__, __LINE__)

Weighted average, w is weight fraction from 0.0 = all of x to 1.0 = all of y. The w argument MUST be within the 0.0-1.0 range, otherwise an assertion failure is triggered.

Definition at line 77 of file math.h.

Function Documentation

◆ fx_lin()

double fx_lin ( double  x,
double  x1,
double  y1,
double  x2,
double  y2 
)

Interpolates a linear function defined by two points.

Parameters
xPoint who's 'y' value we're looking for on the function.
x1First reference point's x coordinate.
y1First reference point's y coordinate.
x2Second reference point's x coordinate.
y2Second reference point's y coordinate.

Definition at line 69 of file math.c.

◆ fx_lin_multi()

double fx_lin_multi ( double  x,
const struct vect2_s *  points,
bool_t  extrapolate 
)

Multi-segment version of fx_lin. The segments are defined as a series of x-y coordinate points (list terminated with a NULL_VECT2). The list must contain AT LEAST 2 points. The value of 'x' is then computed using the fx_lin function from the appropriate segment. If 'x' falls outside of the curve range, the ‘extrapolate’ argument controls behavior. If extrapolate is B_TRUE, the nearest segment is extrapolated to the value of 'x'. Otherwise the function returns NAN.

Definition at line 97 of file math.c.

◆ fx_lin_multi2()

double fx_lin_multi2 ( double  x,
const struct vect2_s *  points,
size_t  n_points,
bool_t  extrapolate 
)

Multi-segment version of fx_lin. The segments are defined as a series of x-y coordinate points. The list must contain AT LEAST 2 points. The value of 'x' is then computed using the fx_lin function from the appropriate segment. If 'x' falls outside of the curve range, the extrapolate argument controls behavior. If extrapolate is B_TRUE, the nearest segment is extrapolated to the value of 'x'. Otherwise the function returns NAN.

Definition at line 113 of file math.c.

◆ fx_lin_multi_inv()

double * fx_lin_multi_inv ( double  y,
const struct vect2_s *  points,
size_t *  num_out 
)

The inverse of fx_lin_multi(). This attempts to invert the output of a function defined by points (terminated by a NULL_VECT2 element) into the possible input x elements. If the function isn't monotonic, this function may need to return multiple points. If the output Y value isn't found anywhere on the input function, this function returns NULL and num_out is set to zero. Use fx_lin_multi_inv2() for explicit extrapolation control.

Parameters
yThe output Y value of the original function for which to perform the inverse lookup.
pointsSequence of at least 2 points, which define the function. The list must be terminated by a NULL_VECT2 point.
num_outReturn argument, which will be filled with the number of returned points.
Returns
A malloc'd series of input X values which would produce the Y value passed in the first argument. The number of points in this array is returned in num_out. You MUST free this list when you're done with it by calling lacf_free().

Definition at line 167 of file math.c.

◆ fx_lin_multi_inv2()

double * fx_lin_multi_inv2 ( double  y,
const struct vect2_s *  points,
bool_t  extrapolate,
size_t *  num_out 
)

Same as fx_lin_multi_inv(), but allows you to control extrapolation of the function. If extrapolate=B_TRUE, then the edges of the function definition in points are extrapolated, to see if they might intersect the Y value. Please note that this doesn't guarantee that you'll get a non-zero number of X points out of the function (e.g. if the function lies entirely on one side of a boundary value and reverses away from the boundary condition on both sides, solving for a Y past the boundary value will still return no results).

See also
fx_lin_multi_inv()

Definition at line 185 of file math.c.

◆ fx_lin_multi_inv3()

double * fx_lin_multi_inv3 ( double  y,
const struct vect2_s *  points,
size_t  n_points,
bool_t  extrapolate,
size_t *  num_out 
)

Same as fx_lin_multi_inv2(), but instead of relying on passing a NULL_VECT2-terminated in points, this function takes an explicit number of points in the n_points argument. This MUST be at least 2.

Definition at line 198 of file math.c.

◆ iter_fract()

static double iter_fract ( double  x,
double  min_val,
double  max_val,
bool_t  clamp_output 
)
static

Given two values min_val and max_val, returns how far between min_val and max_val a third value 'x' lies. If ‘clamp_output’ is true, 'x' is clamped such that it always lies between min_val and max_val. In essence, this function computes:

^
|
1 -------------------+
| / |
| / |
f(x) ------------+ |
| / | |
| / | |
0 ------+ | |
| | | |
+-----|-----|------|----->
min_val x max_val

Definition at line 110 of file math.h.

◆ pn_interp_init()

void pn_interp_init ( pn_interp_t interp,
const vect2_t points,
unsigned  numpts 
)

Given a series of X-Y coordinates, this function initializes a polynomial interpolator that smoothly passes through all the input points. When you are done with the interpolator, you DON'T have to free it. The pn_interp_t structure is entirely self-contained.

Algorithm credit: https://en.wikibooks.org/wiki/\ Algorithm_Implementation/Mathematics/Polynomial_interpolation

Parameters
interpInterpolator that needs to be initialized.
pointsInput points that the interpolator needs to pass through.
nptsNumber points in ‘points’. This must be GREATER than 0.

Definition at line 268 of file math.c.

◆ pn_interp_run()

static double pn_interp_run ( double  x,
const pn_interp_t interp 
)
static

Given an initialized pn_interp_t, calculates the Y value at a given point.

Parameters
xThe X point for which to calculate the interpolated Y value.
interpAn initialized interpolator (see pn_interp_init()).
See also
pn_interp_init()

Definition at line 144 of file math.h.

◆ quadratic_solve()

unsigned quadratic_solve ( double  a,
double  b,
double  c,
double  x[2] 
)

Solves quadratic equation ax^2 + bx + c = 0. Solutions are placed in 'x'.

Returns
The number of solutions (0, 1 or 2).

Definition at line 33 of file math.c.

◆ smoothstep()

static double smoothstep ( double  x,
double  edge0,
double  edge1 
)
static

Implements the smoothstep function from GLSL. See Wikipedia for more info: https://en.wikipedia.org/wiki/Smoothstep

Definition at line 163 of file math.h.

◆ smoothstep_inv()

static double smoothstep_inv ( double  x)
static

Inverse of smoothstep(). The returned value is always in the range of 0-1.

Definition at line 175 of file math.h.

◆ wavg2()

static double wavg2 ( double  x,
double  y,
double  w 
)
static

Similar to wavg(), but performs no bounds checks. For values of w which are outside of 0.0-1.0, the value is extrapolated beyond the bounds.

Definition at line 84 of file math.h.

◆ wavg_impl()

static double wavg_impl ( double  x,
double  y,
double  w,
const char *  file,
int  line 
)
static
Note
Internall. Do not call directly. Use wavg() instead.
See also
wavg()

Definition at line 63 of file math.h.