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
math.h
Go to the documentation of this file.
1/*
2 * CDDL HEADER START
3 *
4 * This file and its contents are supplied under the terms of the
5 * Common Development and Distribution License ("CDDL"), version 1.0.
6 * You may only use this file in accordance with the terms of version
7 * 1.0 of the CDDL.
8 *
9 * A full copy of the text of the CDDL should have accompanied this
10 * source. A copy of the CDDL is also available via the Internet at
11 * http://www.illumos.org/license/CDDL.
12 *
13 * CDDL HEADER END
14 */
15/*
16 * Copyright 2023 Saso Kiselkov. All rights reserved.
17 */
20#ifndef _ACF_UTILS_MATH_H_
21#define _ACF_UTILS_MATH_H_
22
23#include "assert.h"
24#include "math_core.h"
25#include "geom.h"
26
27#ifdef __cplusplus
28extern "C" {
29#endif
30
32#define POW4(x) ((x) * (x) * (x) * (x))
34#define POW3(x) ((x) * (x) * (x))
36#define POW2(x) ((x) * (x))
37#ifndef ABS
39#define ABS(x) ((x) > 0 ? (x) : -(x))
40#endif
41
42struct vect2_s;
43
44API_EXPORT unsigned quadratic_solve(double a, double b, double c, double x[2]);
45API_EXPORT double fx_lin(double x, double x1, double y1, double x2, double y2)
46 PURE_ATTR;
47API_EXPORT double fx_lin_multi(double x, const struct vect2_s *points,
48 bool_t extrapolate) PURE_ATTR;
49API_EXPORT double fx_lin_multi2(double x, const struct vect2_s *points,
50 size_t n_points, bool_t extrapolate) PURE_ATTR;
51API_EXPORT double *fx_lin_multi_inv(double y, const struct vect2_s *points,
52 size_t *num_out);
53API_EXPORT double *fx_lin_multi_inv2(double y, const struct vect2_s *points,
54 bool_t extrapolate, size_t *num_out);
55API_EXPORT double *fx_lin_multi_inv3(double y, const struct vect2_s *points,
56 size_t n_points, bool_t extrapolate, size_t *num_out);
57
62static inline double
63wavg_impl(double x, double y, double w, const char *file, int line)
64{
65 LACF_UNUSED(file);
66 LACF_UNUSED(line);
67 ASSERT_MSG(!isnan(w), "%f is NAN: called from: %s:%d", w, file, line);
68 ASSERT_MSG(w >= 0.0, "%f < 0.0: called from: %s:%d", w, file, line);
69 ASSERT_MSG(w <= 1.0, "%f > 1.0: called from: %s:%d", w, file, line);
70 return (x + (y - x) * w);
71}
77#define wavg(x, y, w) wavg_impl((x), (y), (w), __FILE__, __LINE__)
78
83static inline double
84wavg2(double x, double y, double w)
85{
86 return (x + (y - x) * w);
87}
88
109static inline double
110iter_fract(double x, double min_val, double max_val, bool_t clamp_output)
111{
112 ASSERT3F(min_val, !=, max_val);
113 x = (x - min_val) / (max_val - min_val);
114 if (clamp_output)
115 x = clamp(x, 0, 1);
116 return (x);
117}
118
119#define MAX_PN_INTERP_ORDER 64
129typedef struct {
130 unsigned order;
131 double coeff[MAX_PN_INTERP_ORDER];
132} pn_interp_t;
133
134API_EXPORT void pn_interp_init(pn_interp_t *interp, const vect2_t *points,
135 unsigned npts);
136
143static inline double
144pn_interp_run(double x, const pn_interp_t *interp)
145{
146 double y = 0, power = 1;
147
148 ASSERT(interp != NULL);
149 ASSERT(interp->order != 0);
150 for (unsigned i = 0; i < interp->order; i++) {
151 y += interp->coeff[i] * power;
152 power *= x;
153 }
154
155 return (y);
156}
157
162static inline double
163smoothstep(double x, double edge0, double edge1)
164{
165 ASSERT(!isnan(x));
166 ASSERT3F(edge1, >, edge0);
167 x = clamp((x - edge0) / (edge1 - edge0), 0, 1);
168 return (POW2(x) * (3 - 2 * x));
169}
170
174static inline double
175smoothstep_inv(double x)
176{
177 ASSERT(!isnan(x));
178 return (0.5 - sin(asin(1 - 2 * x) / 3));
179}
180
219#define HROUND2(oldval, newval, step, hyst_rng) \
220 do { \
221 double tmpval = round((newval) / (step)) * (step); \
222 if (isnan((oldval))) { \
223 (oldval) = tmpval; \
224 } else if ( \
225 (newval) > (oldval) + (step) * (0.5 + (hyst_rng)) || \
226 (newval) < (oldval) - (step) * (0.5 + (hyst_rng))) { \
227 (oldval) = tmpval; \
228 } \
229 } while (0)
230
237#define HROUND(oldval, newval, step) \
238 HROUND2((oldval), (newval), (step), 0.35)
239
240#ifdef __cplusplus
241}
242#endif
243
244#endif /* _ACF_UTILS_GEOM_H_ */
#define ASSERT_MSG(x, fmt,...)
Definition assert.h:214
double * fx_lin_multi_inv(double y, const struct vect2_s *points, size_t *num_out)
Definition math.c:167
double * fx_lin_multi_inv3(double y, const struct vect2_s *points, size_t n_points, bool_t extrapolate, size_t *num_out)
Definition math.c:198
double fx_lin(double x, double x1, double y1, double x2, double y2)
Definition math.c:69
static double wavg_impl(double x, double y, double w, const char *file, int line)
Definition math.h:63
unsigned quadratic_solve(double a, double b, double c, double x[2])
Definition math.c:33
double fx_lin_multi2(double x, const struct vect2_s *points, size_t n_points, bool_t extrapolate)
Definition math.c:113
double fx_lin_multi(double x, const struct vect2_s *points, bool_t extrapolate)
Definition math.c:97
double * fx_lin_multi_inv2(double y, const struct vect2_s *points, bool_t extrapolate, size_t *num_out)
Definition math.c:185