-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtinyspline.h
1590 lines (1471 loc) · 57 KB
/
tinyspline.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/** @file */
#ifndef TINYSPLINE_H
#define TINYSPLINE_H
#include <stddef.h>
#ifdef __cplusplus
extern "C" {
#endif
/******************************************************************************
* *
* :: Predefined Constants *
* *
* The following constants should only be changed with caution. Otherwise, the *
* internal consistency can not be guaranteed anymore. The predefined values *
* should be suitable for almost all environments, though. *
* *
******************************************************************************/
/**
* The maximum number of knots of a spline.
*/
#define TS_MAX_NUM_KNOTS 10000
/**
* The minimum knot value of a spline.
*/
#define TS_MIN_KNOT_VALUE 0.0f
/**
* The maximum knot value of a spline.
*/
#define TS_MAX_KNOT_VALUE 1.0f
/**
* Used to check whether two knots are equal.
*/
#define TS_KNOT_EPSILON 1e-4
/******************************************************************************
* *
* :: System Dependent Configuration *
* *
* The following configuration values must be adapted to your system. Some of *
* them may be configured with preprocessor definitions. The default values *
* should be fine for most modern hardware, such as x86, x86_64, and arm. *
* *
******************************************************************************/
#ifdef TINYSPLINE_FLOAT_PRECISION
typedef float tsReal;
#else
typedef double tsReal;
#endif
/******************************************************************************
* *
* :: Error Handling *
* *
* The following section defines enums, structs, and macros that are used to *
* handle different types of errors. The following listing shows an example: *
* *
* tsStatus status; *
* TS_TRY(any_label, status.code, &status) *
* // Use TS_CALL when calling functions of this library. *
* TS_CALL(any_label, status.code, ts_bspline_to_beziers( *
* &spline, &beziers, &status)) *
* if (...) *
* // Use one of the TS_THROW macros to raise an error. *
* TS_THROW_0(any_label, &status, TS_MALLOC, "out of memory") *
* TS_CATCH(status.code) *
* // Executed on error. *
* printf(status.message); *
* TS_FINALLY *
* // Executed in any case. *
* TS_END_TRY *
* *
* Although it is always advisable to properly handle errors, embedding your *
* code into TS_TRY/TS_END_TRY as well as passing a pointer to a tsStatus *
* object is entirely optional, as shown by the following example: *
* *
* ts_bspline_to_beziers(&spline, &beziers, NULL); *
* *
* Yet, you may check if a particular function failed, albeit you don't have *
* access to the error message: *
* *
* if (ts_bspline_to_beziers(&spline, &beziers, NULL)) *
* // an error occurred *
* *
******************************************************************************/
/**
* Defines different error codes.
*/
typedef enum {
TS_SUCCESS = 0, /**< No error. */
TS_MALLOC = -1, /**< Unable to allocate memory. */
TS_DIM_ZERO = -2, /**< The dimension of the control points are 0. */
TS_DEG_GE_NCTRLP = -3, /**< degree >= num_control_points. */
TS_U_UNDEFINED = -4, /**< Undefined knot value. */
TS_MULTIPLICITY = -5, /**< s(u) > order */
TS_KNOTS_DECR = -6, /**< Decreasing knot vector. */
TS_NUM_KNOTS = -7, /**< Unexpected number of knots. */
TS_UNDERIVABLE = -8, /**< Spline is not derivable. */
TS_LCTRLP_DIM_MISMATCH = -10, /**< len_control_points % dim != 0. */
TS_IO_ERROR = -11, /**< Error while reading/writing a file. */
TS_PARSE_ERROR = -12, /**< Error while parsing a serialized entity. */
TS_INDEX_ERROR = -13, /**< Index does not exist. */
TS_NO_RESULT = -14 /**< Function returns without result. */
} tsError;
/**
* Stores an error code (::tsError) as well as a corresponding error message.
*/
typedef struct {
tsError code; /**< The error code. */
char message[100]; /**< The corresponding error message. */
} tsStatus;
#define TS_TRY(label, error, status) \
{ \
(error) = TS_SUCCESS; \
if ((status) != NULL) { \
(status)->code = TS_SUCCESS; \
(status)->message[0] = '\0'; \
} \
__ ## label ## __: \
if (!(error)) {
#define TS_CALL(label, error, call) \
(error) = (call); \
if ((error)) goto __ ## label ## __;
#define TS_CATCH(error) \
} if ((error)) {
#define TS_FINALLY \
} {
#define TS_END_TRY \
} \
}
#define TS_END_TRY_RETURN(error) \
TS_END_TRY return (error);
#define TS_CALL_ROE(error, call) \
{ \
(error) = (call); \
if ((error)) return error; \
}
#define TS_RETURN_SUCCESS(status) \
{ \
if ((status) != NULL) { \
(status)->code = TS_SUCCESS; \
(status)->message[0] = '\0'; \
} \
return TS_SUCCESS; \
}
#define TS_RETURN_0(status, error, msg) \
{ \
if ((status) != NULL) { \
(status)->code = error; \
sprintf((status)->message, msg); \
} \
return error; \
}
#define TS_RETURN_1(status, error, msg, arg1) \
{ \
if ((status) != NULL) { \
(status)->code = error; \
sprintf((status)->message, msg, arg1); \
} \
return error; \
}
#define TS_RETURN_2(status, error, msg, arg1, arg2) \
{ \
if ((status) != NULL) { \
(status)->code = error; \
sprintf((status)->message, msg, arg1, arg2); \
} \
return error; \
}
#define TS_RETURN_3(status, error, msg, arg1, arg2, arg3) \
{ \
if ((status) != NULL) { \
(status)->code = error; \
sprintf((status)->message, msg, arg1, arg2, arg3); \
} \
return error; \
}
#define TS_THROW_0(label, error, status, val, msg) \
{ \
(error) = val; \
if ((status) != NULL) { \
(status)->code = val; \
sprintf((status)->message, msg); \
} \
goto __ ## label ## __; \
}
#define TS_THROW_1(label, error, status, val, msg, arg1) \
{ \
(error) = val; \
if ((status) != NULL) { \
(status)->code = val; \
sprintf((status)->message, msg, arg1); \
} \
goto __ ## label ## __; \
}
#define TS_THROW_2(label, error, status, val, msg, arg1, arg2) \
{ \
(error) = val; \
if ((status) != NULL) { \
(status)->code = val; \
sprintf((status)->message, msg, arg1, arg2); \
} \
goto __ ## label ## __; \
}
#define TS_THROW_3(label, error, status, val, msg, arg1, arg2, arg3) \
{ \
(error) = val; \
if ((status) != NULL) { \
(status)->code = val; \
sprintf((status)->message, msg, arg1, arg2, arg3); \
} \
goto __ ## label ## __; \
}
/******************************************************************************
* *
* :: Data Types *
* *
* The following section defines all available data types. *
* *
******************************************************************************/
/**
* Describes the structure of the knot vector of a NURBS/B-Spline. For more
* details, see:
*
* www.cs.mtu.edu/~shene/COURSES/cs3621/NOTES/spline/B-spline/bspline-curve.html
*/
typedef enum
{
/* Uniformly spaced knot vector. */
TS_OPENED = 0,
/* Uniformly spaced knot vector with clamped end knots. */
TS_CLAMPED = 1,
/* Uniformly spaced knot vector with s(u) = order of spline. */
TS_BEZIERS = 2
} tsBSplineType;
/**
* Represents a B-Spline which may also be used for NURBS, Bezier curves,
* lines, and points. NURBS are represented by homogeneous coordinates where
* the last component of a control point stores the weight of this control
* point. Bezier curves are B-Splines with 'num_control_points == order' and a
* clamped knot vector, therefore passing through their first and last control
* point (a property which does not necessarily apply to B-Splines and NURBS).
* If a Bezier curve consists of two control points only, we call it a line.
* Points, ultimately, are just very short lines consisting of a single control
* point.
*
* Two dimensional control points are stored as follows:
*
* [x_0, y_0, x_1, y_1, ..., x_n-1, y_n-1]
*
* Tree dimensional control points are stored as follows:
*
* [x_0, y_0, z_0, x_1, y_1, z_1, ..., x_n-1, y_n-1, z_n-1]
*
* ... and so on. NURBS are represented by homogeneous coordinates. For
* example, let's say we have a NURBS in 2D consisting of 11 control points
* where 'w_i' is the weight of the i'th control point. Then, the corresponding
* control points are stored as follows:
*
* [x_0*w_0, y_0*w_0, w_0, x_1*w_1, y_1*w_1, w_1, ..., x_10*w_10, y_10*w_10, w_10]
*/
typedef struct
{
struct tsBSplineImpl *pImpl; /**< The actual implementation. */
} tsBSpline;
/**
* Represents the output of De Boor's algorithm. De Boor's algorithm is used to
* evaluate a spline at given knot value 'u' by iteratively computing a net of
* intermediate values until the result is available:
*
* https://en.wikipedia.org/wiki/De_Boor%27s_algorithm
* https://www.cs.mtu.edu/~shene/COURSES/cs3621/NOTES/spline/de-Boor.html
*
* All points of a net are stored in 'points'. The resulting point is the last
* point in 'points' and, for the sake of convenience, may be accessed with
* 'result':
*
* tsDeBoorNet net = ... // evaluate an arbitrary spline and store
* // the resulting net of points in 'net'
*
* ts_deboornet_result(...) // use 'result' to access the resulting point
*
* Two dimensional points are stored as follows:
*
* [x_0, y_0, x_1, y_1, ..., x_n-1, y_n-1]
*
* Tree dimensional points are stored as follows:
*
* [x_0, y_0, z_0, x_1, y_1, z_1, ..., x_n-1, y_n-1, z_n-1]
*
* ... and so on. The output also supports homogeneous coordinates described
* above.
*
* There is a special case in which the evaluation of a knot value 'u' returns
* two results instead of one. It occurs when the multiplicity of 'u' ( s(u) )
* is equals to a spline's order, indicating that the spline is discontinuous
* at 'u'. This is common practice for B-Splines (and NURBS) consisting of
* connected Bezier curves where the endpoint of curve 'c_i' is equals to the
* start point of curve 'c_i+1'. The end point of 'c_i' and the start point of
* 'c_i+1' may still be completely different though, yielding to a spline
* having a (real and visible) gap at 'u'. Consequently, De Boor's algorithm
* must return two results if 's(u) == order' in order to give access to the
* desired points. In such case, 'points' stores only the two resulting points
* (there is no net to calculate) and 'result' points to the *first* point in
* 'points'. Since having (real) gaps in splines is unusual, both points in
* 'points' are generally equals, making it easy to handle this special case by
* accessing 'result' as already shown above for regular cases:
*
* tsDeBoorNet net = ... // evaluate a spline which is discontinuous at
* // the given knot value, yielding to a net with
* // two results
*
* ts_deboornet_result(...) // use 'result' to access the resulting point
*
* However, you can access both points if necessary:
*
* tsDeBoorNet net = ... // evaluate a spline which is discontinuous at
* // the given knot value, yielding to a net with
* // two results
*
* ts_deboornet_result(...)[0] ... // stores the first component of
* // the first point
*
* ts_deboornet_result(...)[dim(spline)] // stores the first component of
* // the second point
*
* As if this wasn't complicated enough, there is an exception for this special
* case, yielding to exactly one result (just like the regular case) even if
* 's(u) == order'. It occurs when 'u' is the lower or upper bound of a
* spline's domain. For instance, if 'b' is a spline with domain [0, 1] and is
* evaluated at 'u = 0' or 'u = 1' then 'result' is *always* a single point
* regardless of the multiplicity of 'u'. Hence, handling this exception is
* straightforward:
*
* tsDeBoorNet net = ... // evaluate a spline at the lower or upper
* // bound of its domain, for instance, 0 or 1
*
* ts_deboornet_result(...) // use 'result' to access the resulting point
*
* In summary, we have three different types of evaluation. 1) The regular case
* returning all points of the net we used to calculate the resulting point. 2)
* The special case returning exactly two points which is required for splines
* with (real) gaps. 3) The exception of 2) returning exactly one point even if
* 's(u) == order'. All in all this looks quite complex (and actually it is)
* but for most applications you do not have to deal with it. Just use 'result'
* to access the outcome of De Boor's algorithm.
*/
typedef struct
{
struct tsDeBoorNetImpl *pImpl; /**< The actual implementation. */
} tsDeBoorNet;
/******************************************************************************
* *
* :: Field Access Functions *
* *
* The following section contains getter and setter functions for the internal *
* state of the structs listed above. *
* *
******************************************************************************/
/**
* Returns the degree of \p spline.
*
* @param[in] spline
* The spline whose degree is read.
* @return
* The degree of \p spline.
*/
size_t ts_bspline_degree(const tsBSpline *spline);
/**
* Sets the degree of \p spline.
*
* @param[out] spline
* The spline whose degree is set.
* @param[in] deg
* The degree to be set.
* @param[out] status
* The status of this function. May be NULL.
* @return TS_SUCCESS
* On success.
* @return TS_DEG_GE_NCTRLP
* If \p degree >= ts_bspline_get_control_points(spline).
*/
tsError ts_bspline_set_degree(tsBSpline *spline, size_t deg, tsStatus *status);
/**
* Returns the order (degree + 1) of \p spline.
*
* @param[in] spline
* The spline whose order is read.
* @return
* The order of \p spline.
*/
size_t ts_bspline_order(const tsBSpline *spline);
/**
* Sets the order (degree + 1) of \p spline.
*
* @param[out] spline
* The spline whose order is set.
* @param[in] order
* The order to be set.
* @param[out] status
* The status of this function. May be NULL.
* @return TS_SUCCESS
* On success.
* @return TS_DEG_GE_NCTRLP
* If \p order > ts_bspline_get_control_points(spline) or if \p order == 0
* ( due to the underflow resulting from: order - 1 => 0 - 1 => INT_MAX
* which always is >= ts_bspline_get_control_points(spline) ).
*/
tsError ts_bspline_set_order(tsBSpline *spline, size_t order,
tsStatus *status);
/**
* Returns the dimension of \p spline. The dimension of a spline describes the
* number of components for each point in ts_bspline_get_control_points(spline).
* One-dimensional splines are possible, albeit their benefit might be
* questionable.
*
* @param[in] spline
* The spline whose dimension is read.
* @return
* The dimension of \p spline.
*/
size_t ts_bspline_dimension(const tsBSpline *spline);
/**
* Sets the dimension of \p spline. The following conditions must be satisfied:
*
* (1) dim >= 1
* (2) len_control_points(spline) % dim == 0
*
* with _len_control_points_ being the length of the control point array of \p
* spline. The dimension of a spline describes the number of components for
* each point in ts_bspline_get_control_points(spline). One-dimensional splines
* are possible, albeit their benefit might be questionable.
*
* @param[out] spline
* The spline whose dimension is set.
* @param[in] dim
* The dimension to be set.
* @param[out] status
* The status of this function. May be NULL.
* @return TS_SUCCESS
* On success.
* @return TS_DIM_ZERO
* If \p dimension == 0.
* @return TS_LCTRLP_DIM_MISMATCH
* If len_control_points(spline) % \p dim != 0
*/
tsError ts_bspline_set_dimension(tsBSpline *spline, size_t dim,
tsStatus *status);
/**
* Returns the length of the control point array of \p spline.
*
* @param[in] spline
* The spline with its control point array whose length is read.
* @return
* The length of the control point array of \p spline.
*/
size_t ts_bspline_len_control_points(const tsBSpline *spline);
/**
* Returns the number of control points of \p spline.
*
* @param[in] spline
* The spline whose number of control points is read.
* @return
* The number of control points of \p spline.
*/
size_t ts_bspline_num_control_points(const tsBSpline *spline);
/**
* Returns the size of the control point array of \p spline. This function may
* be useful when copying control points using memcpy or memmove.
*
* @param[in] spline
* The spline with its control point array whose size is read.
* @return
* The size of the control point array of \p spline.
*/
size_t ts_bspline_sof_control_points(const tsBSpline *spline);
/**
* Returns a deep copy of the control points of \p spline.
*
* @param[in] spline
* The spline whose control points are read.
* @param[out] ctrlp
* The output array.
* @param[out] status
* The status of this function. May be NULL.
* @return TS_SUCCESS
* On success.
* @return TS_MALLOC
* If allocating memory failed.
*/
tsError ts_bspline_control_points(const tsBSpline *spline, tsReal **ctrlp,
tsStatus *status);
/**
* Returns a deep copy of the control point of \p spline at \p index (index 0
* is the first control points, index 1 is the second control point, and so on).
*
* @param[in] spline
* The spline whose control point is read.
* @param[in] index
* The zero based index of the requested control point.
* @param[out] ctrlp
* The output array.
* @param[out] status
* The status of this function. May be NULL.
* @return TS_SUCCESS
* On success.
* @return TS_INDEX_ERROR
* If there is no control point at \p index.
* @return TS_MALLOC
* If allocating memory failed.
*/
tsError ts_bspline_control_point_at(const tsBSpline *spline, size_t index,
tsReal **ctrlp, tsStatus *status);
/**
* Sets the control points of \p spline. Creates a deep copy of \p ctrlp.
*
* @param[out] spline
* The spline whose control points are set.
* @param[in] ctrlp
* The values to deep copy.
* @param[out] status
* The status of this function. May be NULL.
* @return TS_SUCCESS
* On success.
*/
tsError ts_bspline_set_control_points(tsBSpline *spline, const tsReal *ctrlp,
tsStatus *status);
/**
* Sets the control point of \p spline at \p index. Creates a deep copy of
* \p ctrlp.
*
* @param[out] spline
* The spline whose control point is set.
* @param[in] index
* The zero based index of the control point to set.
* @param[in] ctrlp
* The values to deep copy.
* @param[out] status
* The status of this function. May be NULL.
* @return TS_SUCCESS
* On success.
* @return TS_INDEX_ERROR
* If there is no control point at \p index.
*/
tsError ts_bspline_set_control_point_at(tsBSpline *spline, size_t index,
const tsReal *ctrlp, tsStatus *status);
/**
* Returns the number of knots of \p spline.
*
* @param[in] spline
* The spline whose number of knots is read.
* @return
* The number of knots of \p spline.
*/
size_t ts_bspline_num_knots(const tsBSpline *spline);
/**
* Returns the size of the knot array of \p spline. This function may be useful
* when copying knots using memcpy or memmove.
*
* @param[in] spline
* The spline with its knot array whose size is read.
* @return TS_SUCCESS
* The size of the knot array of \p spline.
*/
size_t ts_bspline_sof_knots(const tsBSpline *spline);
/**
* Returns a deep copy of the knots of \p spline.
*
* @param[in] spline
* The spline whose knots are read.
* @param[out] knots
* The output array.
* @param[out] status
* The status of this function. May be NULL.
* @return TS_SUCCESS
* On success.
* @return TS_MALLOC
* If allocating memory failed.
*/
tsError ts_bspline_knots(const tsBSpline *spline, tsReal **knots,
tsStatus *status);
/**
* Sets the knots of \p spline. Creates a deep copy of \p knots and scales it's
* values to [TS_MIN_KNOT_VALUE, TS_MAX_KNOT_VALUE].
*
* @param[out] spline
* The spline whose knots are set.
* @param[in] knots
* The values to deep copy and scale.
* @param[out] status
* The status of this function. May be NULL.
* @return TS_SUCCESS
* On success.
* @return TS_KNOTS_DECR
* If the knot vector is decreasing.
* @return TS_MULTIPLICITY
* If there is a knot with multiplicity > order
*/
tsError ts_bspline_set_knots(tsBSpline *spline, const tsReal *knots,
tsStatus *status);
/* ------------------------------------------------------------------------- */
/**
* Returns the knot (sometimes also called 'u' or 't') of \p net.
*
* @param[in] net
* The net whose knot is read.
* @return
* The knot of \p net.
*/
tsReal ts_deboornet_knot(const tsDeBoorNet *net);
/**
* Returns the index [u_k, u_k+1) with u being the knot of \p net.
*
* @param[in] net
* The net whose index is read.
* @return
* The index [u_k, u_k+1) with u being the knot of \p net.
*/
size_t ts_deboornet_index(const tsDeBoorNet *net);
/**
* Returns the multiplicity of the knot of \p net.
*
* @param[in] net
* The net whose multiplicity is read.
* @return
* The multiplicity of the knot of \p net.
*/
size_t ts_deboornet_multiplicity(const tsDeBoorNet *net);
/**
* Returns the number of insertion that were necessary to evaluate the knot of
* \p net.
*
* @param[in] net
* The net with its knot whose number of insertions is read.
* @return
* The number of insertions that were necessary to evaluate the knot of \p
* net.
*/
size_t ts_deboornet_num_insertions(const tsDeBoorNet *net);
/**
* Returns the dimension of \p net. The dimension of a net describes the number
* of components for each point in ts_bspline_get_points(spline).
* One-dimensional nets are possible, albeit their benefit might be
* questionable.
*
* @param[in] net
* The net whose dimension is read.
* @return
* The dimension of \p net.
*/
size_t ts_deboornet_dimension(const tsDeBoorNet *net);
/**
* Returns the length of the point array of \p net.
*
* @param[in] net
* The net with its point array whose length is read.
* @return
* The length of the point array of \p net.
*/
size_t ts_deboornet_len_points(const tsDeBoorNet *net);
/**
* Returns the number of points of \p net.
*
* @param[in] net
* The net whose number of points is read.
* @return
* The number of points of \p net.
*/
size_t ts_deboornet_num_points(const tsDeBoorNet *net);
/**
* Returns the size of the point array of \p net. This function may be useful
* when copying points using memcpy or memmove.
*
* @param[in] net
* The net with its point array whose size is read.
* @return
* The size of the point array of \p net.
*/
size_t ts_deboornet_sof_points(const tsDeBoorNet *net);
/**
* Returns a deep copy of the points of \p net.
*
* @param[in] net
* The net whose points is read.
* @param[out] points
* The output array.
* @param[out] status
* The status of this function. May be NULL.
* @return TS_SUCCESS
* On success.
* @return TS_MALLOC
* If allocating memory failed.
*/
tsError ts_deboornet_points(const tsDeBoorNet *net, tsReal **points,
tsStatus *status);
/**
* Returns the length of the result array of \p net.
*
* @param[in] net
* The net with its result array whose length is read.
* @return
* The length of the result array of \p net.
*/
size_t ts_deboornet_len_result(const tsDeBoorNet *net);
/**
* Returns the number of points in the result array of \p net
* (1 <= num_result <= 2).
*
* @param[in] net
* The net with its result array whose number of points is read.
* @return
* The number of points in the result array of \p net.
*/
size_t ts_deboornet_num_result(const tsDeBoorNet *net);
/**
* Returns the size of the result array of \p net. This function may be useful
* when copying results using memcpy or memmove.
*
* @param[in] net
* The net with its result array whose size is read.
* @return TS_SUCCESS
* The size of the result array of \p net.
*/
size_t ts_deboornet_sof_result(const tsDeBoorNet *net);
/**
* Returns a deep copy of the result of \p net.
*
* @param[in] net
* The net whose result is read.
* @param[out] result
* The output array.
* @param[out] status
* The status of this function. May be NULL.
* @return TS_SUCCESS
* On success.
* @return TS_MALLOC
* If allocating memory failed.
*/
tsError ts_deboornet_result(const tsDeBoorNet *net, tsReal **result,
tsStatus *status);
/******************************************************************************
* *
* :: Constructors, Destructors, Copy, and Move Functions *
* *
* The following section contains functions to create and delete instances of *
* the data types listed above. Additionally, each data type has a copy and *
* move function. *
* *
******************************************************************************/
/**
* Creates a new spline whose data points to NULL.
*
* @return
* A new spline whose data points to NULL.
*/
tsBSpline ts_bspline_init();
/**
* Creates a new spline and stores the result in \p spline.
*
* @param[in] num_control_points
* The number of control points of \p spline.
* @param[in] dimension
* The dimension of each control point of \p spline.
* @param[in] degree
* The degree of \p spline.
* @param[in] type
* How to setup the knot vector of \p spline.
* @param[out] spline
* The output spline.
* @param[out] status
* The status of this function. May be NULL.
* @return TS_SUCCESS
* On success.
* @return TS_DIM_ZERO
* If \p dimension == 0.
* @return TS_DEG_GE_NCTRLP
* If \p degree >= \p num_control_points.
* @return TS_NUM_KNOTS
* If \p type == ::TS_BEZIERS and
* (\p num_control_points % \p degree + 1) != 0.
* @return TS_MALLOC
* If allocating memory failed.
*/
tsError ts_bspline_new(size_t num_control_points, size_t dimension,
size_t degree, tsBSplineType type, tsBSpline *spline,
tsStatus *status);
/**
* Creates a deep copy of \p src and stores the copied values in \p dest. Does
* nothing, if \p src == \p dest.
*
* @param[in] src
* The spline to deep copy.
* @param[out] dest
* The output spline.
* @param[out] status
* The status of this function. May be NULL.
* @return TS_SUCCESS
* On success.
* @return TS_MALLOC
* If allocating memory failed.
*/
tsError ts_bspline_copy(const tsBSpline *src, tsBSpline *dest,
tsStatus *status);
/**
* Moves the ownership of the data of \p src to \p dest. After calling this
* function, the data of \p src points to NULL. Does not free the data of
* \p dest. Does nothing, if \p src == \p dest.
*
* @param[out] src
* The spline whose values are moved to \p dest.
* @param[out] dest
* The spline that receives the values of \p src.
*/
void ts_bspline_move(tsBSpline *src, tsBSpline *dest);
/**
* Frees the data of \p spline. After calling this function, the data of
* \p spline points to NULL.
*
* @param[out] spline
* The spline to free.
*/
void ts_bspline_free(tsBSpline *spline);
/* ------------------------------------------------------------------------- */
/**
* Creates a new net whose data points to NULL.
*
* @return
* A new net whose data points to NULL.
*/
tsDeBoorNet ts_deboornet_init();
/**
* Creates a deep copy of \p src and stores the copied values in \p dest. Does
* nothing, if \p src == \p dest.
*
* @param[in] src
* The net to deep copy.
* @param[out] dest
* The output net.
* @param[out] status
* The status of this function. May be NULL.
* @return TS_SUCCESS
* On success.
* @return TS_MALLOC
* If allocating memory failed.
*/
tsError ts_deboornet_copy(const tsDeBoorNet *src, tsDeBoorNet *dest,
tsStatus *status);
/**
* Moves the ownership of the data of \p src to \p dest. After calling this
* function, the data of \p src points to NULL. Does not free the data of
* \p dest. Does nothing, if \p src == \p dest.
*
* @param[out] src
* The net whose values are moved to \p dest.
* @param[out] dest
* The net that receives the values of \p src.
*/
void ts_deboornet_move(tsDeBoorNet *src, tsDeBoorNet *dest);
/**
* Frees the data of \p net. After calling this function, the data of \p net
* points to NULL.
*
* @param[out] net
* The net to free.
*/
void ts_deboornet_free(tsDeBoorNet *net);
/******************************************************************************
* *
* :: Interpolation and Approximation Functions *
* *
* The following section contains functions to interpolate and approximate *
* arbitrary splines. *
* *
******************************************************************************/
/**
* Performs a cubic spline interpolation using the thomas algorithm, see:
*
* https://en.wikipedia.org/wiki/Tridiagonal_matrix_algorithm
* http://www.math.ucla.edu/~baker/149.1.02w/handouts/dd_splines.pdf
* http://www.bakoma-tex.com/doc/generic/pst-bspline/pst-bspline-doc.pdf
*
* The resulting spline is a sequence of bezier curves connecting each point
* in \p points. Each bezier curve _b_ is of degree 3 with \p dim being the
* dimension of the each control point in _b_. The total number of control
* points is (\p n - 1) * 4.
*
* On error, all values of \p \_spline\_ are set to 0/NULL.
*
* Note: \p n is the number of points in \p points and not the length of
* \p points. For instance, the follwing point vector yields to \p n = 4 and
* \p dim = 2:
*
* [x0, y0, x1, y1, x2, y2, x3, y3]
*
* @param points
* The points to interpolate.
* @param n
* The number of points in \p points.
* @param dim
* The dimension of each control point in \p \_spline\_.
* @param \_spline\_
* The output parameter storing the result of this function.
* @param status
* Output parameter. Store the returned error code and a descriptive error
* message. May be NULL.
* @return TS_SUCCESS
* On success.
* @return TS_DIM_ZERO
* If \p dim == 0.
* @return TS_DEG_GE_NCTRLP
* If \p n < 2.
* @return TS_MALLOC
* If allocating memory failed.
*/
tsError ts_bspline_interpolate_cubic(const tsReal *points, size_t n,
size_t dim, tsBSpline *_spline_, tsStatus *status);