-
-
Notifications
You must be signed in to change notification settings - Fork 200
/
tinyspline.c
3480 lines (3159 loc) · 99.6 KB
/
tinyspline.c
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
#define TINYSPLINE_EXPORT
#include "tinyspline.h"
#include "parson.h" /* serialization */
#include <stdlib.h> /* malloc, free */
#include <math.h> /* fabs, sqrt, acos */
#include <string.h> /* memcpy, memmove */
#include <stdio.h> /* FILE, fopen */
#include <stdarg.h> /* varargs */
/* Suppress some useless MSVC warnings. */
#ifdef _MSC_VER
#pragma warning(push)
/* address of dllimport */
#pragma warning(disable:4232)
/* function not inlined */
#pragma warning(disable:4710)
/* byte padding */
#pragma warning(disable:4820)
/* meaningless deprecation */
#pragma warning(disable:4996)
/* Spectre mitigation */
#pragma warning(disable:5045)
#endif
#define INIT_OUT_BSPLINE(in, out) \
if ((in) != (out)) \
ts_int_bspline_init(out);
/*! @name Internal Structs and Functions
*
* Internal functions are prefixed with \e ts_int (int for internal).
*
* @{
*/
/**
* Stores the private data of ::tsBSpline.
*/
struct tsBSplineImpl
{
size_t deg; /**< Degree of B-Spline basis function. */
size_t dim; /**< Dimensionality of the control points (2D => x, y). */
size_t n_ctrlp; /**< Number of control points. */
size_t n_knots; /**< Number of knots (n_ctrlp + deg + 1). */
};
/**
* Stores the private data of ::tsDeBoorNet.
*/
struct tsDeBoorNetImpl
{
tsReal u; /**< The evaluated knot. */
size_t k; /**< The index [u_k, u_k+1) */
size_t s; /**< Multiplicity of u_k. */
size_t h; /**< Number of insertions required to obtain result. */
size_t dim; /**< Dimensionality of the points (2D => x, y). */
size_t n_points; /** Number of points in `points'. */
};
void
ts_int_bspline_init(tsBSpline *spline)
{
spline->pImpl = NULL;
}
size_t
ts_int_bspline_sof_state(const tsBSpline *spline)
{
return sizeof(struct tsBSplineImpl) +
ts_bspline_sof_control_points(spline) +
ts_bspline_sof_knots(spline);
}
tsReal *
ts_int_bspline_access_ctrlp(const tsBSpline *spline)
{
return (tsReal *) (& spline->pImpl[1]);
}
tsReal *
ts_int_bspline_access_knots(const tsBSpline *spline)
{
return ts_int_bspline_access_ctrlp(spline) +
ts_bspline_len_control_points(spline);
}
tsError
ts_int_bspline_access_ctrlp_at(const tsBSpline *spline,
size_t index,
tsReal **ctrlp,
tsStatus *status)
{
const size_t num = ts_bspline_num_control_points(spline);
if (index >= num) {
TS_RETURN_2(status, TS_INDEX_ERROR,
"index (%lu) >= num(control_points) (%lu)",
(unsigned long) index,
(unsigned long) num)
}
*ctrlp = ts_int_bspline_access_ctrlp(spline) +
index * ts_bspline_dimension(spline);
TS_RETURN_SUCCESS(status)
}
tsError
ts_int_bspline_access_knot_at(const tsBSpline *spline,
size_t index,
tsReal *knot,
tsStatus *status)
{
const size_t num = ts_bspline_num_knots(spline);
if (index >= num) {
TS_RETURN_2(status, TS_INDEX_ERROR,
"index (%lu) >= num(knots) (%lu)",
(unsigned long) index,
(unsigned long) num)
}
*knot = ts_int_bspline_access_knots(spline)[index];
TS_RETURN_SUCCESS(status)
}
void
ts_int_deboornet_init(tsDeBoorNet *net)
{
net->pImpl = NULL;
}
size_t
ts_int_deboornet_sof_state(const tsDeBoorNet *net)
{
return sizeof(struct tsDeBoorNetImpl) +
ts_deboornet_sof_points(net) +
ts_deboornet_sof_result(net);
}
tsReal *
ts_int_deboornet_access_points(const tsDeBoorNet *net)
{
return (tsReal *) (& net->pImpl[1]);
}
tsReal *
ts_int_deboornet_access_result(const tsDeBoorNet *net)
{
if (ts_deboornet_num_result(net) == 2) {
return ts_int_deboornet_access_points(net);
} else {
return ts_int_deboornet_access_points(net) +
/* Last point in `points`. */
(ts_deboornet_len_points(net) -
ts_deboornet_dimension(net));
}
}
/*! @} */
/*! @name B-Spline Data
*
* @{
*/
size_t
ts_bspline_degree(const tsBSpline *spline)
{
return spline->pImpl->deg;
}
size_t
ts_bspline_order(const tsBSpline *spline)
{
return ts_bspline_degree(spline) + 1;
}
size_t
ts_bspline_dimension(const tsBSpline *spline)
{
return spline->pImpl->dim;
}
size_t
ts_bspline_len_control_points(const tsBSpline *spline)
{
return ts_bspline_num_control_points(spline) *
ts_bspline_dimension(spline);
}
size_t
ts_bspline_num_control_points(const tsBSpline *spline)
{
return spline->pImpl->n_ctrlp;
}
size_t
ts_bspline_sof_control_points(const tsBSpline *spline)
{
return ts_bspline_len_control_points(spline) * sizeof(tsReal);
}
const tsReal *
ts_bspline_control_points_ptr(const tsBSpline *spline)
{
return ts_int_bspline_access_ctrlp(spline);
}
tsError
ts_bspline_control_points(const tsBSpline *spline,
tsReal **ctrlp,
tsStatus *status)
{
const size_t size = ts_bspline_sof_control_points(spline);
*ctrlp = (tsReal*) malloc(size);
if (!*ctrlp) TS_RETURN_0(status, TS_MALLOC, "out of memory")
memcpy(*ctrlp, ts_int_bspline_access_ctrlp(spline), size);
TS_RETURN_SUCCESS(status)
}
tsError
ts_bspline_control_point_at_ptr(const tsBSpline *spline,
size_t index,
const tsReal **ctrlp,
tsStatus *status)
{
tsReal *vals;
tsError err;
TS_TRY(try, err, status)
TS_CALL(try, err, ts_int_bspline_access_ctrlp_at(
spline, index, &vals, status))
*ctrlp = vals;
TS_CATCH(err)
*ctrlp = NULL;
TS_END_TRY_RETURN(err)
}
tsError
ts_bspline_set_control_points(tsBSpline *spline,
const tsReal *ctrlp,
tsStatus *status)
{
const size_t size = ts_bspline_sof_control_points(spline);
memmove(ts_int_bspline_access_ctrlp(spline), ctrlp, size);
TS_RETURN_SUCCESS(status)
}
tsError
ts_bspline_set_control_point_at(tsBSpline *spline,
size_t index,
const tsReal *ctrlp,
tsStatus *status)
{
tsReal *to;
size_t size;
tsError err;
TS_TRY(try, err, status)
TS_CALL(try, err, ts_int_bspline_access_ctrlp_at(
spline, index, &to, status))
size = ts_bspline_dimension(spline) * sizeof(tsReal);
memcpy(to, ctrlp, size);
TS_END_TRY_RETURN(err)
}
size_t
ts_bspline_num_knots(const tsBSpline *spline)
{
return spline->pImpl->n_knots;
}
size_t
ts_bspline_sof_knots(const tsBSpline *spline)
{
return ts_bspline_num_knots(spline) * sizeof(tsReal);
}
const tsReal *
ts_bspline_knots_ptr(const tsBSpline *spline)
{
return ts_int_bspline_access_knots(spline);
}
tsError
ts_bspline_knots(const tsBSpline *spline,
tsReal **knots,
tsStatus *status)
{
const size_t size = ts_bspline_sof_knots(spline);
*knots = (tsReal*) malloc(size);
if (!*knots) TS_RETURN_0(status, TS_MALLOC, "out of memory")
memcpy(*knots, ts_int_bspline_access_knots(spline), size);
TS_RETURN_SUCCESS(status)
}
tsError
ts_bspline_knot_at(const tsBSpline *spline,
size_t index,
tsReal *knot,
tsStatus *status)
{
return ts_int_bspline_access_knot_at(spline, index, knot, status);
}
tsError
ts_bspline_set_knots(tsBSpline *spline,
const tsReal *knots,
tsStatus *status)
{
const size_t size = ts_bspline_sof_knots(spline);
const size_t num_knots = ts_bspline_num_knots(spline);
const size_t order = ts_bspline_order(spline);
size_t idx, mult;
tsReal lst_knot, knot;
lst_knot = knots[0];
mult = 1;
for (idx = 1; idx < num_knots; idx++) {
knot = knots[idx];
if (ts_knots_equal(lst_knot, knot)) {
mult++;
} else if (lst_knot > knot) {
TS_RETURN_1(status, TS_KNOTS_DECR,
"decreasing knot vector at index: %lu",
(unsigned long) idx)
} else {
mult = 0;
}
if (mult > order) {
TS_RETURN_3(status, TS_MULTIPLICITY,
"mult(%f) (%lu) > order (%lu)",
knot, (unsigned long) mult,
(unsigned long) order)
}
lst_knot = knot;
}
memmove(ts_int_bspline_access_knots(spline), knots, size);
TS_RETURN_SUCCESS(status)
}
tsError
ts_bspline_set_knots_varargs(tsBSpline *spline,
tsStatus *status,
tsReal knot0,
double knot1,
...)
{
tsReal *values = NULL;
va_list argp;
size_t idx;
tsError err;
TS_TRY(try, err, status)
TS_CALL(try, err, ts_bspline_knots(
spline, &values, status))
values[0] = knot0;
values[1] = (tsReal) knot1;
va_start(argp, knot1);
for (idx = 2; idx < ts_bspline_num_knots(spline); idx++)
values[idx] = (tsReal) va_arg(argp, double);
va_end(argp);
TS_CALL(try, err, ts_bspline_set_knots(
spline, values, status))
TS_FINALLY
if (values) free(values);
TS_END_TRY_RETURN(err)
}
tsError
ts_bspline_set_knot_at(tsBSpline *spline,
size_t index,
tsReal knot,
tsStatus *status)
{
tsError err;
tsReal *knots = NULL;
/* This is only for initialization. */
tsReal oldKnot = ts_int_bspline_access_knots(spline)[0];
TS_TRY(try, err, status)
TS_CALL(try, err, ts_int_bspline_access_knot_at(
spline, index, &oldKnot, status))
/* knots must be set after reading oldKnot because the catch
* block assumes that oldKnot contains the correct value if
* knots is not NULL. */
knots = ts_int_bspline_access_knots(spline);
knots[index] = knot;
TS_CALL(try, err, ts_bspline_set_knots(
spline, knots, status))
TS_CATCH(err)
/* If knots is not NULL, oldKnot contains the correct value. */
if (knots) knots[index] = oldKnot;
TS_END_TRY_RETURN(err)
}
/*! @} */
/*! @name B-Spline Initialization
*
* @{
*/
tsBSpline
ts_bspline_init(void)
{
tsBSpline spline;
ts_int_bspline_init(&spline);
return spline;
}
tsError
ts_int_bspline_generate_knots(const tsBSpline *spline,
tsBSplineType type,
tsStatus *status)
{
const size_t n_knots = ts_bspline_num_knots(spline);
const size_t deg = ts_bspline_degree(spline);
const size_t order = ts_bspline_order(spline);
tsReal fac; /**< Factor used to calculate the knot values. */
size_t i; /**< Used in for loops. */
tsReal *knots; /**< Pointer to the knots of \p _result_. */
/* order >= 1 implies 2*order >= 2 implies n_knots >= 2 */
if (type == TS_BEZIERS && n_knots % order != 0) {
TS_RETURN_2(status, TS_NUM_KNOTS,
"num(knots) (%lu) %% order (%lu) != 0",
(unsigned long) n_knots, (unsigned long) order)
}
knots = ts_int_bspline_access_knots(spline);
if (type == TS_OPENED) {
knots[0] = TS_DOMAIN_DEFAULT_MIN; /* n_knots >= 2 */
fac = (TS_DOMAIN_DEFAULT_MAX - TS_DOMAIN_DEFAULT_MIN)
/ (n_knots - 1); /* n_knots >= 2 */
for (i = 1; i < n_knots-1; i++)
knots[i] = TS_DOMAIN_DEFAULT_MIN + i*fac;
knots[i] = TS_DOMAIN_DEFAULT_MAX; /* n_knots >= 2 */
} else if (type == TS_CLAMPED) {
/* n_knots >= 2*order == 2*(deg+1) == 2*deg + 2 > 2*deg - 1 */
fac = (TS_DOMAIN_DEFAULT_MAX - TS_DOMAIN_DEFAULT_MIN)
/ (n_knots - 2*deg - 1);
ts_arr_fill(knots, order, TS_DOMAIN_DEFAULT_MIN);
for (i = order ;i < n_knots-order; i++)
knots[i] = TS_DOMAIN_DEFAULT_MIN + (i-deg)*fac;
ts_arr_fill(knots + i, order, TS_DOMAIN_DEFAULT_MAX);
} else if (type == TS_BEZIERS) {
/* n_knots >= 2*order implies n_knots/order >= 2 */
fac = (TS_DOMAIN_DEFAULT_MAX - TS_DOMAIN_DEFAULT_MIN)
/ (n_knots/order - 1);
ts_arr_fill(knots, order, TS_DOMAIN_DEFAULT_MIN);
for (i = order; i < n_knots-order; i += order)
ts_arr_fill(knots + i,
order,
TS_DOMAIN_DEFAULT_MIN + (i/order)*fac);
ts_arr_fill(knots + i, order, TS_DOMAIN_DEFAULT_MAX);
}
TS_RETURN_SUCCESS(status)
}
tsError
ts_bspline_new(size_t num_control_points,
size_t dimension,
size_t degree,
tsBSplineType type,
tsBSpline *spline,
tsStatus *status)
{
const size_t order = degree + 1;
const size_t num_knots = num_control_points + order;
const size_t len_ctrlp = num_control_points * dimension;
const size_t sof_real = sizeof(tsReal);
const size_t sof_impl = sizeof(struct tsBSplineImpl);
const size_t sof_ctrlp_vec = len_ctrlp * sof_real;
const size_t sof_knots_vec = num_knots * sof_real;
const size_t sof_spline = sof_impl + sof_ctrlp_vec + sof_knots_vec;
tsError err;
ts_int_bspline_init(spline);
if (dimension < 1) {
TS_RETURN_0(status, TS_DIM_ZERO, "unsupported dimension: 0")
}
if (num_knots > TS_MAX_NUM_KNOTS) {
TS_RETURN_2(status, TS_NUM_KNOTS,
"unsupported number of knots: %lu > %i",
(unsigned long) num_knots, TS_MAX_NUM_KNOTS)
}
if (degree >= num_control_points) {
TS_RETURN_2(status, TS_DEG_GE_NCTRLP,
"degree (%lu) >= num(control_points) (%lu)",
(unsigned long) degree,
(unsigned long) num_control_points)
}
spline->pImpl = (struct tsBSplineImpl *) malloc(sof_spline);
if (!spline->pImpl) TS_RETURN_0(status, TS_MALLOC, "out of memory")
spline->pImpl->deg = degree;
spline->pImpl->dim = dimension;
spline->pImpl->n_ctrlp = num_control_points;
spline->pImpl->n_knots = num_knots;
TS_TRY(try, err, status)
TS_CALL(try, err, ts_int_bspline_generate_knots(
spline, type, status))
TS_CATCH(err)
ts_bspline_free(spline);
TS_END_TRY_RETURN(err)
}
tsError
ts_bspline_new_with_control_points(size_t num_control_points,
size_t dimension,
size_t degree,
tsBSplineType type,
tsBSpline *spline,
tsStatus *status,
double first,
...)
{
tsReal *ctrlp = NULL;
va_list argp;
size_t i;
tsError err;
TS_TRY(try, err, status)
TS_CALL(try, err, ts_bspline_new(
num_control_points, dimension,
degree, type, spline, status))
TS_CATCH(err)
ts_bspline_free(spline);
TS_END_TRY_ROE(err)
ctrlp = ts_int_bspline_access_ctrlp(spline);
ctrlp[0] = (tsReal) first;
va_start(argp, first);
for (i = 1; i < ts_bspline_len_control_points(spline); i++)
ctrlp[i] = (tsReal) va_arg(argp, double);
va_end(argp);
TS_RETURN_SUCCESS(status)
}
tsError
ts_bspline_copy(const tsBSpline *src,
tsBSpline *dest,
tsStatus *status)
{
size_t size;
if (src == dest) TS_RETURN_SUCCESS(status)
ts_int_bspline_init(dest);
size = ts_int_bspline_sof_state(src);
dest->pImpl = (struct tsBSplineImpl *) malloc(size);
if (!dest->pImpl) TS_RETURN_0(status, TS_MALLOC, "out of memory")
memcpy(dest->pImpl, src->pImpl, size);
TS_RETURN_SUCCESS(status)
}
void
ts_bspline_move(tsBSpline *src,
tsBSpline *dest)
{
if (src == dest) return;
dest->pImpl = src->pImpl;
ts_int_bspline_init(src);
}
void
ts_bspline_free(tsBSpline *spline)
{
if (spline->pImpl) free(spline->pImpl);
ts_int_bspline_init(spline);
}
/*! @} */
/*! @name De Boor Net Data
*
* @{
*/
tsReal
ts_deboornet_knot(const tsDeBoorNet *net)
{
return net->pImpl->u;
}
size_t
ts_deboornet_index(const tsDeBoorNet *net)
{
return net->pImpl->k;
}
size_t
ts_deboornet_multiplicity(const tsDeBoorNet *net)
{
return net->pImpl->s;
}
size_t
ts_deboornet_num_insertions(const tsDeBoorNet *net)
{
return net->pImpl->h;
}
size_t
ts_deboornet_dimension(const tsDeBoorNet *net)
{
return net->pImpl->dim;
}
size_t
ts_deboornet_len_points(const tsDeBoorNet *net)
{
return ts_deboornet_num_points(net) *
ts_deboornet_dimension(net);
}
size_t
ts_deboornet_num_points(const tsDeBoorNet *net)
{
return net->pImpl->n_points;
}
size_t
ts_deboornet_sof_points(const tsDeBoorNet *net)
{
return ts_deboornet_len_points(net) * sizeof(tsReal);
}
const tsReal *
ts_deboornet_points_ptr(const tsDeBoorNet *net)
{
return ts_int_deboornet_access_points(net);
}
tsError
ts_deboornet_points(const tsDeBoorNet *net,
tsReal **points,
tsStatus *status)
{
const size_t size = ts_deboornet_sof_points(net);
*points = (tsReal*) malloc(size);
if (!*points) TS_RETURN_0(status, TS_MALLOC, "out of memory")
memcpy(*points, ts_int_deboornet_access_points(net), size);
TS_RETURN_SUCCESS(status)
}
size_t
ts_deboornet_len_result(const tsDeBoorNet *net)
{
return ts_deboornet_num_result(net) *
ts_deboornet_dimension(net);
}
size_t
ts_deboornet_num_result(const tsDeBoorNet *net)
{
return ts_deboornet_num_points(net) == 2 ? 2 : 1;
}
size_t
ts_deboornet_sof_result(const tsDeBoorNet *net)
{
return ts_deboornet_len_result(net) * sizeof(tsReal);
}
const tsReal *
ts_deboornet_result_ptr(const tsDeBoorNet *net)
{
return ts_int_deboornet_access_result(net);
}
tsError
ts_deboornet_result(const tsDeBoorNet *net,
tsReal **result,
tsStatus *status)
{
const size_t size = ts_deboornet_sof_result(net);
*result = (tsReal*) malloc(size);
if (!*result) TS_RETURN_0(status, TS_MALLOC, "out of memory")
memcpy(*result, ts_int_deboornet_access_result(net), size);
TS_RETURN_SUCCESS(status)
}
/*! @} */
/*! @name De Boor Net Initialization
*
* @{
*/
tsDeBoorNet
ts_deboornet_init(void)
{
tsDeBoorNet net;
ts_int_deboornet_init(&net);
return net;
}
tsError
ts_int_deboornet_new(const tsBSpline *spline,
tsDeBoorNet *net,
tsStatus *status)
{
const size_t dim = ts_bspline_dimension(spline);
const size_t deg = ts_bspline_degree(spline);
const size_t order = ts_bspline_order(spline);
const size_t num_points = (size_t)(order * (order+1) * 0.5f);
/* Handle `order == 1' which generates too few points. */
const size_t fixed_num_points = num_points < 2 ? 2 : num_points;
const size_t sof_real = sizeof(tsReal);
const size_t sof_impl = sizeof(struct tsDeBoorNetImpl);
const size_t sof_points_vec = fixed_num_points * dim * sof_real;
const size_t sof_net = sof_impl + sof_points_vec;
net->pImpl = (struct tsDeBoorNetImpl *) malloc(sof_net);
if (!net->pImpl) TS_RETURN_0(status, TS_MALLOC, "out of memory")
net->pImpl->u = 0.f;
net->pImpl->k = 0;
net->pImpl->s = 0;
net->pImpl->h = deg;
net->pImpl->dim = dim;
net->pImpl->n_points = fixed_num_points;
TS_RETURN_SUCCESS(status)
}
void
ts_deboornet_free(tsDeBoorNet *net)
{
if (net->pImpl) free(net->pImpl);
ts_int_deboornet_init(net);
}
tsError
ts_deboornet_copy(const tsDeBoorNet *src,
tsDeBoorNet *dest,
tsStatus *status)
{
size_t size;
if (src == dest) TS_RETURN_SUCCESS(status)
ts_int_deboornet_init(dest);
size = ts_int_deboornet_sof_state(src);
dest->pImpl = (struct tsDeBoorNetImpl *) malloc(size);
if (!dest->pImpl) TS_RETURN_0(status, TS_MALLOC, "out of memory")
memcpy(dest->pImpl, src->pImpl, size);
TS_RETURN_SUCCESS(status)
}
void
ts_deboornet_move(tsDeBoorNet *src,
tsDeBoorNet *dest)
{
if (src == dest) return;
dest->pImpl = src->pImpl;
ts_int_deboornet_init(src);
}
/*! @} */
/*! @name Interpolation and Approximation Functions
*
* @{
*/
tsError
ts_int_cubic_point(const tsReal *point,
size_t dim,
tsBSpline *spline,
tsStatus *status)
{
const size_t size = dim * sizeof(tsReal);
tsReal *ctrlp = NULL;
size_t i;
tsError err;
TS_CALL_ROE(err, ts_bspline_new(
4, dim, 3,
TS_CLAMPED, spline, status))
ctrlp = ts_int_bspline_access_ctrlp(spline);
for (i = 0; i < 4; i++) {
memcpy(ctrlp + i*dim,
point,
size);
}
TS_RETURN_SUCCESS(status)
}
tsError
ts_int_thomas_algorithm(const tsReal *a,
const tsReal *b,
const tsReal *c,
size_t num,
size_t dim,
tsReal *d,
tsStatus *status)
{
size_t i, j, k, l;
tsReal m, *cc = NULL;
tsError err;
if (dim == 0) {
TS_RETURN_0(status, TS_DIM_ZERO,
"unsupported dimension: 0")
}
if (num <= 1) {
TS_RETURN_1(status, TS_NUM_POINTS,
"num(points) (%lu) <= 1",
(unsigned long) num)
}
cc = (tsReal *) malloc(num * sizeof(tsReal));
if (!cc) TS_RETURN_0(status, TS_MALLOC, "out of memory")
TS_TRY(try, err, status)
/* Forward sweep. */
if (fabs(b[0]) <= fabs(c[0])) {
TS_THROW_2(try, err, status, TS_NO_RESULT,
"error: |%f| <= |%f|", b[0], c[0])
}
/* |b[i]| > |c[i]| implies that |b[i]| > 0. Thus, the following
* statements cannot evaluate to division by zero.*/
cc[0] = c[0] / b[0];
for (i = 0; i < dim; i++)
d[i] = d[i] / b[0];
for (i = 1; i < num; i++) {
if (fabs(b[i]) <= fabs(a[i]) + fabs(c[i])) {
TS_THROW_3(try, err, status, TS_NO_RESULT,
"error: |%f| <= |%f| + |%f|",
b[i], a[i], c[i])
}
/* |a[i]| < |b[i]| and cc[i - 1] < 1. Therefore, the
* following statement cannot evaluate to division by
* zero. */
m = 1.f / (b[i] - a[i] * cc[i - 1]);
/* |b[i]| > |a[i]| + |c[i]| implies that there must be
* an eps > 0 such that |b[i]| = |a[i]| + |c[i]| + eps.
* Even if |a[i]| is 0 (by which the result of the
* following statement becomes maximum), |c[i]| is less
* than |b[i]| by an amount of eps. By substituting the
* previous and the following statements (under the
* assumption that |a[i]| is 0), we obtain c[i] / b[i],
* which must be less than 1. */
cc[i] = c[i] * m;
for (j = 0; j < dim; j++) {
k = i * dim + j;
l = (i-1) * dim + j;
d[k] = (d[k] - a[i] * d[l]) * m;
}
}
/* Back substitution. */
for (i = num-1; i > 0; i--) {
for (j = 0; j < dim; j++) {
k = (i-1) * dim + j;
l = i * dim + j;
d[k] -= cc[i-1] * d[l];
}
}
TS_FINALLY
free(cc);
TS_END_TRY_RETURN(err)
}
tsError
ts_int_relaxed_uniform_cubic_bspline(const tsReal *points,
size_t n,
size_t dim,
tsBSpline *spline,
tsStatus *status)
{
const size_t order = 4; /**< Order of spline to interpolate. */
const tsReal as = 1.f/6.f; /**< The value 'a sixth'. */
const tsReal at = 1.f/3.f; /**< The value 'a third'. */
const tsReal tt = 2.f/3.f; /**< The value 'two third'. */
size_t sof_ctrlp; /**< Size of a single control point. */
const tsReal* b = points; /**< Array of the b values. */
tsReal* s; /**< Array of the s values. */
size_t i, d; /**< Used in for loops */
size_t j, k, l; /**< Used as temporary indices. */
tsReal *ctrlp; /**< Pointer to the control points of \p _spline_. */
tsError err;
/* input validation */
if (dim == 0)
TS_RETURN_0(status, TS_DIM_ZERO, "unsupported dimension: 0")
if (n <= 1) {
TS_RETURN_1(status, TS_NUM_POINTS,
"num(points) (%lu) <= 1",
(unsigned long) n)
}
/* in the following n >= 2 applies */
sof_ctrlp = dim * sizeof(tsReal); /* dim > 0 implies sof_ctrlp > 0 */
s = NULL;
TS_TRY(try, err, status)
/* n >= 2 implies n-1 >= 1 implies (n-1)*4 >= 4 */
TS_CALL(try, err, ts_bspline_new(
(n-1) * 4, dim, order - 1,
TS_BEZIERS, spline, status))
ctrlp = ts_int_bspline_access_ctrlp(spline);
s = (tsReal*) malloc(n * sof_ctrlp);
if (!s) {
TS_THROW_0(try, err, status, TS_MALLOC,
"out of memory")
}
/* set s_0 to b_0 and s_n = b_n */
memcpy(s, b, sof_ctrlp);
memcpy(s + (n-1)*dim, b + (n-1)*dim, sof_ctrlp);
/* set s_i = 1/6*b_{i-1} + 2/3*b_{i} + 1/6*b_{i+1}*/
for (i = 1; i < n-1; i++) {
for (d = 0; d < dim; d++) {
j = (i-1)*dim+d;
k = i*dim+d;
l = (i+1)*dim+d;
s[k] = as * b[j];
s[k] += tt * b[k];
s[k] += as * b[l];
}
}
/* create beziers from b and s */
for (i = 0; i < n-1; i++) {
for (d = 0; d < dim; d++) {
j = i*dim+d;
k = i*4*dim+d;
l = (i+1)*dim+d;
ctrlp[k] = s[j];
ctrlp[k+dim] = tt*b[j] + at*b[l];
ctrlp[k+2*dim] = at*b[j] + tt*b[l];
ctrlp[k+3*dim] = s[l];
}
}
TS_CATCH(err)
ts_bspline_free(spline);
TS_FINALLY
if (s)
free(s);
TS_END_TRY_RETURN(err)
}
tsError
ts_bspline_interpolate_cubic_natural(const tsReal *points,
size_t num_points,
size_t dimension,
tsBSpline *spline,
tsStatus *status)
{
const size_t sof_ctrlp = dimension * sizeof(tsReal);
const size_t len_points = num_points * dimension;
const size_t num_int_points = num_points - 2;
const size_t len_int_points = num_int_points * dimension;
tsReal *buffer, *a, *b, *c, *d;
size_t i, j, k, l;
tsError err;
ts_int_bspline_init(spline);
if (num_points == 0)
TS_RETURN_0(status, TS_NUM_POINTS, "num(points) == 0")
if (num_points == 1) {
TS_CALL_ROE(err, ts_int_cubic_point(
points, dimension, spline, status))
TS_RETURN_SUCCESS(status)
}
if (num_points == 2) {
return ts_int_relaxed_uniform_cubic_bspline(
points, num_points, dimension, spline, status);
}
/* `num_points` >= 3 */
buffer = NULL;
TS_TRY(try, err, status)
buffer = (tsReal *) malloc(
/* `a', `b', `c' (note that `c' is equal to `a') */
2 * num_int_points * sizeof(tsReal) +
/* At first: `d' Afterwards: The result of the thomas
* algorithm including the first and last point to be
* interpolated. */
num_points * dimension * sizeof(tsReal));
if (!buffer) {
TS_THROW_0(try, err, status, TS_MALLOC,
"out of memory")
}
/* The system of linear equations is taken from:
* http://www.bakoma-tex.com/doc/generic/pst-bspline/
* pst-bspline-doc.pdf */
a = c = buffer;
ts_arr_fill(a, num_int_points, 1);
b = a + num_int_points;
ts_arr_fill(b, num_int_points, 4);
d = b + num_int_points /* shift to the beginning of `d' */
+ dimension; /* make space for the first point */
/* 6 * S_{i+1} */
for (i = 0; i < num_int_points; i++) {
for (j = 0; j < dimension; j++) {
k = i * dimension + j;
l = (i+1) * dimension + j;
d[k] = 6 * points[l];