Mercurial > hg > dmlib
annotate src/dmcurves.c @ 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 | fecf3967abee |
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 |
1b439304ff3c
Add new module dmcurves, that will contain functions for spline generation
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
10 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
|
11 { |
1b439304ff3c
Add new module dmcurves, that will contain functions for spline generation
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
12 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
|
13 } |
1b439304ff3c
Add new module dmcurves, that will contain functions for spline generation
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
14 |
1b439304ff3c
Add new module dmcurves, that will contain functions for spline generation
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
15 |
1b439304ff3c
Add new module dmcurves, that will contain functions for spline generation
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
16 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
|
17 { |
1b439304ff3c
Add new module dmcurves, that will contain functions for spline generation
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
18 int cv, j; |
1b439304ff3c
Add new module dmcurves, that will contain functions for spline generation
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
19 DMVector *curr = dst; |
1b439304ff3c
Add new module dmcurves, that will contain functions for spline generation
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
20 |
1b439304ff3c
Add new module dmcurves, that will contain functions for spline generation
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
21 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
|
22 { |
1b439304ff3c
Add new module dmcurves, that will contain functions for spline generation
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
23 // 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
|
24 int i; |
1b439304ff3c
Add new module dmcurves, that will contain functions for spline generation
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
25 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
|
26 { |
1b439304ff3c
Add new module dmcurves, that will contain functions for spline generation
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
27 // 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
|
28 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
|
29 |
1b439304ff3c
Add new module dmcurves, that will contain functions for spline generation
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
30 // inverse value of 't' |
1b439304ff3c
Add new module dmcurves, that will contain functions for spline generation
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
31 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
|
32 |
1b439304ff3c
Add new module dmcurves, that will contain functions for spline generation
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
33 // 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
|
34 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
|
35 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
|
36 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
|
37 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
|
38 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
|
39 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
|
40 |
1b439304ff3c
Add new module dmcurves, that will contain functions for spline generation
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
41 // 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
|
42 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
|
43 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
|
44 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
|
45 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
|
46 |
1b439304ff3c
Add new module dmcurves, that will contain functions for spline generation
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
47 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
|
48 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
|
49 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
|
50 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
|
51 |
1b439304ff3c
Add new module dmcurves, that will contain functions for spline generation
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
52 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
|
53 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
|
54 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
|
55 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
|
56 } |
1b439304ff3c
Add new module dmcurves, that will contain functions for spline generation
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
57 } |
1b439304ff3c
Add new module dmcurves, that will contain functions for spline generation
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
58 |
1b439304ff3c
Add new module dmcurves, that will contain functions for spline generation
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
59 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
|
60 } |
1b439304ff3c
Add new module dmcurves, that will contain functions for spline generation
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
61 |
1b439304ff3c
Add new module dmcurves, that will contain functions for spline generation
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
62 |
1b439304ff3c
Add new module dmcurves, that will contain functions for spline generation
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
63 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
|
64 { |
1b439304ff3c
Add new module dmcurves, that will contain functions for spline generation
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
65 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
|
66 return DMERR_NULLPTR; |
1b439304ff3c
Add new module dmcurves, that will contain functions for spline generation
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
67 |
1b439304ff3c
Add new module dmcurves, that will contain functions for spline generation
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
68 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
|
69 return DMERR_INVALID_DATA; |
1b439304ff3c
Add new module dmcurves, that will contain functions for spline generation
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
70 |
1b439304ff3c
Add new module dmcurves, that will contain functions for spline generation
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
71 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
|
72 return DMERR_MALLOC; |
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 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
|
75 } |