LibreCAD
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
tinyspline.h
Go to the documentation of this file.
1 #ifndef TINYSPLINE_H
2 #define TINYSPLINE_H
3 
4 #include <stddef.h>
5 
6 
7 #ifdef __cplusplus
8 extern "C" {
9 #endif
10 
11 /********************************************************
12 * *
13 * System dependent configuration *
14 * *
15 ********************************************************/
16 #ifdef TINYSPLINE_DOUBLE_PRECISION
17 typedef double tsRational;
18 #else
19 typedef float tsRational;
20 #endif
21 
22 
23 /********************************************************
24 * *
25 * Data *
26 * *
27 ********************************************************/
41 typedef enum
42 {
43  TS_SUCCESS = 0, /* no error */
44  TS_MALLOC = -1, /* malloc/realloc returned null */
45  TS_DIM_ZERO = -2, /* the dimension of the control points are 0 */
46  TS_DEG_GE_NCTRLP = -3, /* degree of spline >= number of control points */
47  TS_U_UNDEFINED = -4, /* spline is not defined at u */
48  TS_MULTIPLICITY = -5, /* the multiplicity of a knot is greater than
49  * the order of the spline */
50  TS_KNOTS_DECR = -6, /* decreasing knot vector */
51  TS_NUM_KNOTS = -7, /* unexpected number of knots */
52  TS_UNDERIVABLE = -8 /* spline is not derivable */
53 } tsError;
54 
61 typedef enum
62 {
63 /* setup knots as... */
64  TS_OPENED = 0, /* [uniformly spaced] */
65  TS_CLAMPED = 1, /* [u_1 = u_2 = ..., uniformly spaced, ... = u_n-1 = u_n] */
66  TS_BEZIERS = 2, /* uniformly spaced with s(u) = order for all u in knots */
67  TS_NONE = 3 /* do not setup the knots; they may have any values */
69 
70 typedef struct
71 {
72  tsRational u; /* the knot u */
73  size_t k; /* the index [u_k, u_k+1) */
74  size_t s; /* the multiplicity of u_k */
75  size_t h; /* how many times u must be inserted */
76  size_t dim; /* dimension of a control point */
77  size_t n_points; /* number of control points */
78  tsRational* points; /* the control points of the de Boor net */
79  tsRational* result; /* the result of the evaluation
80  * Technically it is a pointer to the last point in points. Any changes made
81  * here will modify points and vice versa. In case of a discontinuous B-Spline
82  * at u, points can be used to get access to the first point and result to get
83  * access to the second one. */
84 } tsDeBoorNet;
85 
86 typedef struct
87 {
88  size_t deg; /* degree of b-spline basis function */
89  size_t order; /* degree + 1
90  * This field is provided for convenience, because some libraries (e.g. OpenGL)
91  * implicitly describing a polynomial of degree n with n + 1. */
92  size_t dim; /* dimension of a control point */
93  size_t n_ctrlp; /* number of control points */
94  size_t n_knots; /* number of knots (n_ctrlp + deg + 1) */
95  tsRational* ctrlp; /* the control points of the spline */
96  tsRational* knots; /* the knot vector of the spline (ascending)
97  * Technically ctrlp and knots share the same array. The first elements of
98  * this array are the control points and the last elements are the knots. Keep
99  * in mind that you should never assign two different arrays to ctrlp and knots
100  * otherwise function provided by this library may result in undefined
101  * behaviour. */
102 } tsBSpline;
103 
104 
105 /********************************************************
106 * *
107 * Methods *
108 * *
109 ********************************************************/
115 void ts_deboornet_default(tsDeBoorNet* deBoorNet);
116 
123 void ts_deboornet_free(tsDeBoorNet* deBoorNet);
124 
130 void ts_bspline_default(tsBSpline* bspline);
131 
138 void ts_bspline_free(tsBSpline* bspline);
139 
146 void ts_bspline_move(tsBSpline* from, tsBSpline* to);
147 
164  const size_t deg, const size_t dim,
165  const size_t n_ctrlp, const tsBSplineType type,
166  tsBSpline* bspline
167 );
168 
193  const tsRational* points, const size_t n, const size_t dim,
194  tsBSpline* bspline
195 );
196 
228  const tsBSpline* original,
229  tsBSpline* derivative
230 );
231 
247  const tsDeBoorNet* original,
248  tsDeBoorNet* copy
249 );
250 
266  const tsBSpline* original,
267  tsBSpline* copy
268 );
269 
283  const tsBSpline* bspline, const tsRational* ctrlp,
284  tsBSpline* result
285 );
286 
300  const tsBSpline* bspline, const tsRational* knots,
301  tsBSpline* result
302 );
303 
327  const tsBSpline* original, const tsBSplineType type,
328  const tsRational min, const tsRational max,
329  tsBSpline* result
330 );
331 
347  const tsBSpline* bspline, const tsRational u,
348  tsDeBoorNet* deBoorNet
349 );
350 
352  const tsBSpline* bspline, const tsRational u, const size_t n,
353  tsBSpline* result, size_t* k
354 );
355 
383  const tsBSpline* bspline, const int n, const int back,
384  tsBSpline* resized
385 );
386 
388  const tsBSpline* bspline, const tsRational u,
389  tsBSpline* split, size_t* k
390 );
391 
416  const tsBSpline* original, const tsRational b,
417  tsBSpline* buckled
418 );
419 
421  const tsBSpline* bspline,
422  tsBSpline* beziers
423 );
424 
425 
426 /********************************************************
427 * *
428 * Utility *
429 * *
430 ********************************************************/
438 int ts_fequals(const tsRational x, const tsRational y);
439 
445 char* ts_enum_str(const tsError err);
446 
452 tsError ts_str_enum(const char* str);
453 
457 void ts_ffill(tsRational* arr, const size_t num, const tsRational val);
458 
459 
460 #ifdef __cplusplus
461 }
462 #endif
463 
464 #endif /* TINYSPLINE_H */
465 
size_t dim
Definition: tinyspline.h:76
tsError ts_bspline_interpolate(const tsRational *points, const size_t n, const size_t dim, tsBSpline *bspline)
tsError ts_bspline_set_knots(const tsBSpline *bspline, const tsRational *knots, tsBSpline *result)
float tsRational
Definition: tinyspline.h:19
tsError ts_bspline_derive(const tsBSpline *original, tsBSpline *derivative)
tsError ts_str_enum(const char *str)
tsError ts_bspline_set_ctrlp(const tsBSpline *bspline, const tsRational *ctrlp, tsBSpline *result)
tsRational * ctrlp
Definition: tinyspline.h:95
tsError ts_bspline_new(const size_t deg, const size_t dim, const size_t n_ctrlp, const tsBSplineType type, tsBSpline *bspline)
void ts_bspline_move(tsBSpline *from, tsBSpline *to)
tsRational * result
Definition: tinyspline.h:79
size_t dim
Definition: tinyspline.h:92
void ts_bspline_free(tsBSpline *bspline)
tsRational * points
Definition: tinyspline.h:78
tsError ts_bspline_setup_knots(const tsBSpline *original, const tsBSplineType type, const tsRational min, const tsRational max, tsBSpline *result)
size_t order
Definition: tinyspline.h:89
tsRational * knots
Definition: tinyspline.h:96
tsError ts_bspline_copy(const tsBSpline *original, tsBSpline *copy)
tsError ts_deboornet_copy(const tsDeBoorNet *original, tsDeBoorNet *copy)
size_t n_ctrlp
Definition: tinyspline.h:93
tsError ts_bspline_insert_knot(const tsBSpline *bspline, const tsRational u, const size_t n, tsBSpline *result, size_t *k)
void ts_ffill(tsRational *arr, const size_t num, const tsRational val)
tsError ts_bspline_buckle(const tsBSpline *original, const tsRational b, tsBSpline *buckled)
size_t k
Definition: tinyspline.h:73
int ts_fequals(const tsRational x, const tsRational y)
size_t h
Definition: tinyspline.h:75
size_t n_points
Definition: tinyspline.h:77
char * ts_enum_str(const tsError err)
size_t n_knots
Definition: tinyspline.h:94
tsError ts_bspline_split(const tsBSpline *bspline, const tsRational u, tsBSpline *split, size_t *k)
void ts_deboornet_default(tsDeBoorNet *deBoorNet)
size_t deg
Definition: tinyspline.h:88
tsError ts_bspline_resize(const tsBSpline *bspline, const int n, const int back, tsBSpline *resized)
void ts_bspline_default(tsBSpline *bspline)
tsError ts_bspline_to_beziers(const tsBSpline *bspline, tsBSpline *beziers)
void ts_deboornet_free(tsDeBoorNet *deBoorNet)
tsRational u
Definition: tinyspline.h:72
tsError
Definition: tinyspline.h:41
tsBSplineType
Definition: tinyspline.h:61
size_t s
Definition: tinyspline.h:74
tsError ts_bspline_evaluate(const tsBSpline *bspline, const tsRational u, tsDeBoorNet *deBoorNet)