annotate src/dmcurves.c @ 978:fecf3967abee

Move lerp functions from dmlerp.c to dmcurves.c, too.
author Matti Hamalainen <ccr@tnsp.org>
date Fri, 27 Feb 2015 16:20:24 +0200
parents 1b439304ff3c
children 0ba1ec9b8608
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
976
1b439304ff3c Add new module dmcurves, that will contain functions for spline generation
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1 /*
1b439304ff3c Add new module dmcurves, that will contain functions for spline generation
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2 * DMLib
1b439304ff3c Add new module dmcurves, that will contain functions for spline generation
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
3 * -- Curve and spline functions
1b439304ff3c Add new module dmcurves, that will contain functions for spline generation
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
4 * Programmed and designed by Matti 'ccr' Hamalainen
1b439304ff3c Add new module dmcurves, that will contain functions for spline generation
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
5 * (C) Copyright 2015 Tecnic Software productions (TNSP)
1b439304ff3c Add new module dmcurves, that will contain functions for spline generation
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
6 */
1b439304ff3c Add new module dmcurves, that will contain functions for spline generation
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
7 #include "dmcurves.h"
1b439304ff3c Add new module dmcurves, that will contain functions for spline generation
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
8
1b439304ff3c Add new module dmcurves, that will contain functions for spline generation
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
9
978
fecf3967abee Move lerp functions from dmlerp.c to dmcurves.c, too.
Matti Hamalainen <ccr@tnsp.org>
parents: 976
diff changeset
10 void dmLerpInit(DMLerpContext *ctx, DMFloat start, DMFloat end, DMFloat nsteps)
fecf3967abee Move lerp functions from dmlerp.c to dmcurves.c, too.
Matti Hamalainen <ccr@tnsp.org>
parents: 976
diff changeset
11 {
fecf3967abee Move lerp functions from dmlerp.c to dmcurves.c, too.
Matti Hamalainen <ccr@tnsp.org>
parents: 976
diff changeset
12 ctx->start = start;
fecf3967abee Move lerp functions from dmlerp.c to dmcurves.c, too.
Matti Hamalainen <ccr@tnsp.org>
parents: 976
diff changeset
13 ctx->end = end;
fecf3967abee Move lerp functions from dmlerp.c to dmcurves.c, too.
Matti Hamalainen <ccr@tnsp.org>
parents: 976
diff changeset
14 ctx->nsteps = nsteps;
fecf3967abee Move lerp functions from dmlerp.c to dmcurves.c, too.
Matti Hamalainen <ccr@tnsp.org>
parents: 976
diff changeset
15 }
fecf3967abee Move lerp functions from dmlerp.c to dmcurves.c, too.
Matti Hamalainen <ccr@tnsp.org>
parents: 976
diff changeset
16
fecf3967abee Move lerp functions from dmlerp.c to dmcurves.c, too.
Matti Hamalainen <ccr@tnsp.org>
parents: 976
diff changeset
17
fecf3967abee Move lerp functions from dmlerp.c to dmcurves.c, too.
Matti Hamalainen <ccr@tnsp.org>
parents: 976
diff changeset
18 DMFloat dmCatmullRom(const DMFloat t, const DMFloat p0, const DMFloat p1, const DMFloat p2, const DMFloat p3)
fecf3967abee Move lerp functions from dmlerp.c to dmcurves.c, too.
Matti Hamalainen <ccr@tnsp.org>
parents: 976
diff changeset
19 {
fecf3967abee Move lerp functions from dmlerp.c to dmcurves.c, too.
Matti Hamalainen <ccr@tnsp.org>
parents: 976
diff changeset
20 const DMFloat q = t * t;
fecf3967abee Move lerp functions from dmlerp.c to dmcurves.c, too.
Matti Hamalainen <ccr@tnsp.org>
parents: 976
diff changeset
21 return (
fecf3967abee Move lerp functions from dmlerp.c to dmcurves.c, too.
Matti Hamalainen <ccr@tnsp.org>
parents: 976
diff changeset
22 (2 * p1) +
fecf3967abee Move lerp functions from dmlerp.c to dmcurves.c, too.
Matti Hamalainen <ccr@tnsp.org>
parents: 976
diff changeset
23 (-p0 + p2) * t +
fecf3967abee Move lerp functions from dmlerp.c to dmcurves.c, too.
Matti Hamalainen <ccr@tnsp.org>
parents: 976
diff changeset
24 (2 * p0 - 5 * p1 + 4 * p2 - p3) * q +
fecf3967abee Move lerp functions from dmlerp.c to dmcurves.c, too.
Matti Hamalainen <ccr@tnsp.org>
parents: 976
diff changeset
25 ( -p0 + 3 * p1 - 3 * p2 + p3) * q * t
fecf3967abee Move lerp functions from dmlerp.c to dmcurves.c, too.
Matti Hamalainen <ccr@tnsp.org>
parents: 976
diff changeset
26 ) * 0.5f;
fecf3967abee Move lerp functions from dmlerp.c to dmcurves.c, too.
Matti Hamalainen <ccr@tnsp.org>
parents: 976
diff changeset
27 }
fecf3967abee Move lerp functions from dmlerp.c to dmcurves.c, too.
Matti Hamalainen <ccr@tnsp.org>
parents: 976
diff changeset
28
fecf3967abee Move lerp functions from dmlerp.c to dmcurves.c, too.
Matti Hamalainen <ccr@tnsp.org>
parents: 976
diff changeset
29
976
1b439304ff3c Add new module dmcurves, that will contain functions for spline generation
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
30 static inline const DMVector *dmSplineGetPoint(const DMVector *points, const int npoints, const int n)
1b439304ff3c Add new module dmcurves, that will contain functions for spline generation
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
31 {
1b439304ff3c Add new module dmcurves, that will contain functions for spline generation
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
32 return (n < 0) ? &points[0] : ((n < npoints) ? &points[n] : &points[npoints - 1]);
1b439304ff3c Add new module dmcurves, that will contain functions for spline generation
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
33 }
1b439304ff3c Add new module dmcurves, that will contain functions for spline generation
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
34
1b439304ff3c Add new module dmcurves, that will contain functions for spline generation
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
35
1b439304ff3c Add new module dmcurves, that will contain functions for spline generation
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
36 int dmBSplineGenerate(DMVector *dst, const int ndst, const DMVector *points, const int npoints, const int slod)
1b439304ff3c Add new module dmcurves, that will contain functions for spline generation
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
37 {
1b439304ff3c Add new module dmcurves, that will contain functions for spline generation
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
38 int cv, j;
1b439304ff3c Add new module dmcurves, that will contain functions for spline generation
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
39 DMVector *curr = dst;
1b439304ff3c Add new module dmcurves, that will contain functions for spline generation
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
40
1b439304ff3c Add new module dmcurves, that will contain functions for spline generation
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
41 for (cv = -3, j = 0; j != npoints + 1 && j < ndst; j++, cv++, curr++)
1b439304ff3c Add new module dmcurves, that will contain functions for spline generation
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
42 {
1b439304ff3c Add new module dmcurves, that will contain functions for spline generation
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
43 // for each section of curve, draw 'lod' number of divisions
1b439304ff3c Add new module dmcurves, that will contain functions for spline generation
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
44 int i;
1b439304ff3c Add new module dmcurves, that will contain functions for spline generation
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
45 for (i = 0; i != slod; i++)
1b439304ff3c Add new module dmcurves, that will contain functions for spline generation
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
46 {
1b439304ff3c Add new module dmcurves, that will contain functions for spline generation
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
47 // use the parametric time value 0.0 to 1.0 for this curve segment
1b439304ff3c Add new module dmcurves, that will contain functions for spline generation
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
48 const float t = (float) i / slod;
1b439304ff3c Add new module dmcurves, that will contain functions for spline generation
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
49
1b439304ff3c Add new module dmcurves, that will contain functions for spline generation
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
50 // inverse value of 't'
1b439304ff3c Add new module dmcurves, that will contain functions for spline generation
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
51 const float it = 1.0f - t;
1b439304ff3c Add new module dmcurves, that will contain functions for spline generation
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
52
1b439304ff3c Add new module dmcurves, that will contain functions for spline generation
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
53 // calculate blending functions for cubic bspline
1b439304ff3c Add new module dmcurves, that will contain functions for spline generation
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
54 const float t2 = t * t;
1b439304ff3c Add new module dmcurves, that will contain functions for spline generation
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
55 const float t3 = t2 * t;
1b439304ff3c Add new module dmcurves, that will contain functions for spline generation
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
56 const float b0 = it * it * it / 6.0f;
1b439304ff3c Add new module dmcurves, that will contain functions for spline generation
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
57 const float b1 = ( 3*t3 - 6*t2 + 4) / 6.0f;
1b439304ff3c Add new module dmcurves, that will contain functions for spline generation
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
58 const float b2 = (-3*t3 + 3*t2 + 3*t + 1) / 6.0f;
1b439304ff3c Add new module dmcurves, that will contain functions for spline generation
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
59 const float b3 = t3 / 6.0f;
1b439304ff3c Add new module dmcurves, that will contain functions for spline generation
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
60
1b439304ff3c Add new module dmcurves, that will contain functions for spline generation
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
61 // calculate the x,y and z of the curve point
1b439304ff3c Add new module dmcurves, that will contain functions for spline generation
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
62 curr->x = b0 * dmSplineGetPoint(points, npoints, cv + 0)->x +
1b439304ff3c Add new module dmcurves, that will contain functions for spline generation
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
63 b1 * dmSplineGetPoint(points, npoints, cv + 1)->x +
1b439304ff3c Add new module dmcurves, that will contain functions for spline generation
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
64 b2 * dmSplineGetPoint(points, npoints, cv + 2)->x +
1b439304ff3c Add new module dmcurves, that will contain functions for spline generation
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
65 b3 * dmSplineGetPoint(points, npoints, cv + 3)->x;
1b439304ff3c Add new module dmcurves, that will contain functions for spline generation
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
66
1b439304ff3c Add new module dmcurves, that will contain functions for spline generation
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
67 curr->y = b0 * dmSplineGetPoint(points, npoints, cv + 0)->y +
1b439304ff3c Add new module dmcurves, that will contain functions for spline generation
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
68 b1 * dmSplineGetPoint(points, npoints, cv + 1)->y +
1b439304ff3c Add new module dmcurves, that will contain functions for spline generation
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
69 b2 * dmSplineGetPoint(points, npoints, cv + 2)->y +
1b439304ff3c Add new module dmcurves, that will contain functions for spline generation
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
70 b3 * dmSplineGetPoint(points, npoints, cv + 3)->y;
1b439304ff3c Add new module dmcurves, that will contain functions for spline generation
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
71
1b439304ff3c Add new module dmcurves, that will contain functions for spline generation
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
72 curr->z = b0 * dmSplineGetPoint(points, npoints, cv + 0)->z +
1b439304ff3c Add new module dmcurves, that will contain functions for spline generation
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
73 b1 * dmSplineGetPoint(points, npoints, cv + 1)->z +
1b439304ff3c Add new module dmcurves, that will contain functions for spline generation
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
74 b2 * dmSplineGetPoint(points, npoints, cv + 2)->z +
1b439304ff3c Add new module dmcurves, that will contain functions for spline generation
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
75 b3 * dmSplineGetPoint(points, npoints, cv + 3)->z;
1b439304ff3c Add new module dmcurves, that will contain functions for spline generation
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
76 }
1b439304ff3c Add new module dmcurves, that will contain functions for spline generation
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
77 }
1b439304ff3c Add new module dmcurves, that will contain functions for spline generation
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
78
1b439304ff3c Add new module dmcurves, that will contain functions for spline generation
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
79 return (j == ndst) ? DMERR_OK : DMERR_BOUNDS;
1b439304ff3c Add new module dmcurves, that will contain functions for spline generation
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
80 }
1b439304ff3c Add new module dmcurves, that will contain functions for spline generation
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
81
1b439304ff3c Add new module dmcurves, that will contain functions for spline generation
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
82
1b439304ff3c Add new module dmcurves, that will contain functions for spline generation
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
83 int dmBSplineGenerateAlloc(DMVector **dst, int *ndst, const DMVector *points, const int npoints, const int slod)
1b439304ff3c Add new module dmcurves, that will contain functions for spline generation
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
84 {
1b439304ff3c Add new module dmcurves, that will contain functions for spline generation
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
85 if (ndst == NULL || dst == NULL)
1b439304ff3c Add new module dmcurves, that will contain functions for spline generation
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
86 return DMERR_NULLPTR;
1b439304ff3c Add new module dmcurves, that will contain functions for spline generation
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
87
1b439304ff3c Add new module dmcurves, that will contain functions for spline generation
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
88 if ((*ndst = dmBSplineGetNPoints(npoints, slod)) <= 0)
1b439304ff3c Add new module dmcurves, that will contain functions for spline generation
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
89 return DMERR_INVALID_DATA;
1b439304ff3c Add new module dmcurves, that will contain functions for spline generation
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
90
1b439304ff3c Add new module dmcurves, that will contain functions for spline generation
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
91 if ((*dst = dmCalloc(*ndst, sizeof(DMVector))) == NULL)
1b439304ff3c Add new module dmcurves, that will contain functions for spline generation
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
92 return DMERR_MALLOC;
1b439304ff3c Add new module dmcurves, that will contain functions for spline generation
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
93
1b439304ff3c Add new module dmcurves, that will contain functions for spline generation
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
94 return dmBSplineGenerate(*dst, *ndst, points, npoints, slod);
1b439304ff3c Add new module dmcurves, that will contain functions for spline generation
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
95 }