comparison src/dmcurves.h @ 976:1b439304ff3c

Add new module dmcurves, that will contain functions for spline generation etc and the LERP functions. Move LERP functions from dmlib.h to dmcurves.h.
author Matti Hamalainen <ccr@tnsp.org>
date Fri, 27 Feb 2015 16:16:59 +0200
parents
children 43594ac98f91
comparison
equal deleted inserted replaced
975:139b801f8d1c 976:1b439304ff3c
1 /*
2 * DMLib
3 * -- Wav file writing
4 * Programmed and designed by Matti 'ccr' Hamalainen
5 * (C) Copyright 2012 Tecnic Software productions (TNSP)
6 */
7 #ifndef DMCURVES_H
8 #define DMCURVES_H
9
10 #include "dmlib.h"
11 #include "dmvecmat.h"
12
13 #ifdef __cplusplus
14 extern "C" {
15 #endif
16
17 /* Generic parameter interpolation
18 */
19 #define DMM_S_CURVE(t) ((t) * (t) * (3.0f - 2.0f * (t)))
20 #define DMM_LERP(t, a, b) ((a) + (t) * ((b) - (a)))
21
22 typedef struct
23 {
24 DMFloat start, end, nsteps;
25 } DMLerpContext;
26
27
28 void dmLerpInit(DMLerpContext *ctx, DMFloat start, DMFloat end, DMFloat nsteps);
29 DMFloat dmCatmullRom(const DMFloat t, const DMFloat p0, const DMFloat p1, const DMFloat p2, const DMFloat p3);
30
31
32 static inline DMFloat dmClamp10(const DMFloat a)
33 {
34 return (a < 0.0f ? 0.0f : (a > 1.0f ? 1.0f : a));
35 }
36
37
38 static inline int dmClamp(const int v, const int min, const int max)
39 {
40 return (v < min ? min : (v > max ? max : v));
41 }
42
43
44 static inline DMFloat dmLerpSCurve(DMLerpContext *ctx, const DMFloat step)
45 {
46 const DMFloat n = step / ctx->nsteps;
47 const DMFloat v = DMM_S_CURVE(n);
48 return DMM_LERP(v, ctx->start, ctx->end);
49 }
50
51
52 static inline DMFloat dmLerpSCurveClamp(DMLerpContext *ctx, const DMFloat step)
53 {
54 const DMFloat n = dmClamp10(step / ctx->nsteps);
55 const DMFloat v = DMM_S_CURVE(n);
56 return DMM_LERP(v, ctx->start, ctx->end);
57 }
58
59
60 static inline DMFloat dmLerp1(DMLerpContext *ctx, const DMFloat step)
61 {
62 const DMFloat v = step / ctx->nsteps;
63 return DMM_LERP(v, ctx->start, ctx->end);
64 }
65
66
67 static inline DMFloat dmLerp1Clamp(DMLerpContext *ctx, const DMFloat step)
68 {
69 const DMFloat v = dmClamp10(step / ctx->nsteps);
70 return DMM_LERP(v, ctx->start, ctx->end);
71 }
72
73
74 static inline DMFloat dmCatmullRomClamp(const DMFloat t, const DMFloat p0, const DMFloat p1, const DMFloat p2, const DMFloat p3)
75 {
76 return dmCatmullRom(dmClamp10(t), p0, p1, p2, p3);
77 }
78
79
80 static inline int dmBSplineGetNPoints(const int npoints, const int slod)
81 {
82 return npoints * slod;
83 }
84
85 int dmBSplineGenerateAlloc(DMVector **dst, int *ndst, const DMVector *points, const int npoints, const int slod);
86 int dmBSplineGenerate(DMVector *dst, const int ndst, const DMVector *points, const int npoints, const int slod);
87
88
89 #ifdef __cplusplus
90 }
91 #endif
92
93 #endif // DMCURVES_H