annotate src/dmcurves.c @ 984:0ba1ec9b8608

Comment cleanups.
author Matti Hamalainen <ccr@tnsp.org>
date Fri, 27 Feb 2015 18:12:20 +0200
parents fecf3967abee
children 7df95aefb9c6
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 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
48 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
49
984
0ba1ec9b8608 Comment cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 978
diff changeset
50 // Calculate blending functions for cubic bspline
976
1b439304ff3c Add new module dmcurves, that will contain functions for spline generation
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
51 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
52 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
53 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
54 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
55 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
56 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
57
984
0ba1ec9b8608 Comment cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 978
diff changeset
58 // Calculate coordinates of the curve point
976
1b439304ff3c Add new module dmcurves, that will contain functions for spline generation
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
59 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
60 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
61 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
62 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
63
1b439304ff3c Add new module dmcurves, that will contain functions for spline generation
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
64 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
65 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
66 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
67 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
68
1b439304ff3c Add new module dmcurves, that will contain functions for spline generation
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
69 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
70 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
71 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
72 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
73 }
1b439304ff3c Add new module dmcurves, that will contain functions for spline generation
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
74 }
1b439304ff3c Add new module dmcurves, that will contain functions for spline generation
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
75
1b439304ff3c Add new module dmcurves, that will contain functions for spline generation
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
76 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
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
1b439304ff3c Add new module dmcurves, that will contain functions for spline generation
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
80 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
81 {
1b439304ff3c Add new module dmcurves, that will contain functions for spline generation
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
82 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
83 return DMERR_NULLPTR;
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 = dmBSplineGetNPoints(npoints, slod)) <= 0)
1b439304ff3c Add new module dmcurves, that will contain functions for spline generation
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
86 return DMERR_INVALID_DATA;
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 ((*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
89 return DMERR_MALLOC;
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 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
92 }