view 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
line wrap: on
line source

/*
 * DMLib
 * -- Wav file writing
 * Programmed and designed by Matti 'ccr' Hamalainen
 * (C) Copyright 2012 Tecnic Software productions (TNSP)
 */
#ifndef DMCURVES_H
#define DMCURVES_H

#include "dmlib.h"
#include "dmvecmat.h"

#ifdef __cplusplus
extern "C" {
#endif

/* Generic parameter interpolation
 */
#define DMM_S_CURVE(t)     ((t) * (t) * (3.0f - 2.0f * (t)))
#define DMM_LERP(t, a, b)  ((a) + (t) * ((b) - (a)))

typedef struct
{
    DMFloat start, end, nsteps;
} DMLerpContext;


void       dmLerpInit(DMLerpContext *ctx, DMFloat start, DMFloat end, DMFloat nsteps);
DMFloat    dmCatmullRom(const DMFloat t, const DMFloat p0, const DMFloat p1, const DMFloat p2, const DMFloat p3);


static inline DMFloat dmClamp10(const DMFloat a)
{
    return (a < 0.0f ? 0.0f : (a > 1.0f ? 1.0f : a));
}


static inline int dmClamp(const int v, const int min, const int max)
{
    return (v < min ? min : (v > max ? max : v));
}


static inline DMFloat dmLerpSCurve(DMLerpContext *ctx, const DMFloat step)
{
    const DMFloat n = step / ctx->nsteps;
    const DMFloat v = DMM_S_CURVE(n);
    return DMM_LERP(v, ctx->start, ctx->end);
}


static inline DMFloat dmLerpSCurveClamp(DMLerpContext *ctx, const DMFloat step)
{
    const DMFloat n = dmClamp10(step / ctx->nsteps);
    const DMFloat v = DMM_S_CURVE(n);
    return DMM_LERP(v, ctx->start, ctx->end);
}


static inline DMFloat dmLerp1(DMLerpContext *ctx, const DMFloat step)
{
    const DMFloat v = step / ctx->nsteps;
    return DMM_LERP(v, ctx->start, ctx->end);
}


static inline DMFloat dmLerp1Clamp(DMLerpContext *ctx, const DMFloat step)
{
    const DMFloat v = dmClamp10(step / ctx->nsteps);
    return DMM_LERP(v, ctx->start, ctx->end);
}


static inline DMFloat dmCatmullRomClamp(const DMFloat t, const DMFloat p0, const DMFloat p1, const DMFloat p2, const DMFloat p3)
{
    return dmCatmullRom(dmClamp10(t), p0, p1, p2, p3);
}


static inline int dmBSplineGetNPoints(const int npoints, const int slod)
{
    return npoints * slod;
}

int dmBSplineGenerateAlloc(DMVector **dst, int *ndst, const DMVector *points, const int npoints, const int slod);
int dmBSplineGenerate(DMVector *dst, const int ndst, const DMVector *points, const int npoints, const int slod);


#ifdef __cplusplus
}
#endif

#endif // DMCURVES_H