-
-
Notifications
You must be signed in to change notification settings - Fork 210
/
Copy pathtinyspline.h
3117 lines (2929 loc) · 104 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>
/*! @name Deprecation
*
* The macro \c TS_DEPRECATED can be used to mark functions as
* deprecated.
*
* @{
*/
#if defined(__GNUC__) || defined(__clang__)
#define TS_DEPRECATED __attribute__((deprecated))
#elif defined(_MSC_VER)
#define TS_DEPRECATED __declspec(deprecated)
#elif defined(SWIG)
#define TS_DEPRECATED
#else
#warning "WARNING: TS_DEPRECATED is not supported by the compiler"
#define TS_DEPRECATED
#endif
/*! @} */
/*! @name Library Export/Import
*
* If TinySpline is built for Windows, the macros \c TINYSPLINE_SHARED_EXPORT
* and \c TINYSPLINE_SHARED_IMPORT define the Microsoft specific directives \c
* __declspec(dllexport) and \c __declspec(dllimport), respectively. More
* information on these directives can be found at:
*
* https://docs.microsoft.com/en-us/cpp/cpp/dllexport-dllimport
*
* If TinySpline is built to the ELF (most Unix like environments) or Mach (OS
* X) object format, \c TINYSPLINE_SHARED_EXPORT defines the directive
* <tt>__attribute__ ((visibility ("default")))</tt> which, in combination with
* \c -fvisibility=hidden, behaves similar to \c __declspec(dllexport). \c
* TINYSPLINE_SHARED_IMPORT is set empty (i.e., it defines nothing).
*
* If none of the above applies, \c TINYSPLINE_SHARED_EXPORT and \c
* TINYSPLINE_SHARED_IMPORT are set empty (i.e., they define nothing).
*
* Depending on whether TinySpline is compiled <em>as shared library</em> or
* linked against <em>as shared library</em>, \c TINYSPLINE_API points to \c
* TINYSPLINE_SHARED_EXPORT (compiled) or \c TINYSPLINE_SHARED_IMPORT (linked
* against). All elements of TinySpline that needs to be exported/imported are
* annotated with \c TINYSPLINE_API. This eliminates the need for a
* module-definition (.def) file. If TinySpline is compiled or linked against
* <em>as static library</em>, \c TINYSPLINE_API is set empty (i.e., it defines
* nothing).
*
* If you consume TinySpline as shared library built for Windows, all you need
* is to define \c TINYSPLINE_SHARED. This will automatically import all
* required symbols. When compiling TinySpline, the build system should set all
* necessary defines.
*
* @{
*/
#if !defined(TINYSPLINE_API)
#if defined(_WIN32) || defined(__CYGWIN__)
#define TINYSPLINE_SHARED_EXPORT __declspec(dllexport)
#define TINYSPLINE_SHARED_IMPORT __declspec(dllimport)
#elif defined(__ELF__) || defined(__MACH__)
#define TINYSPLINE_SHARED_EXPORT __attribute__ ((visibility ("default")))
#define TINYSPLINE_SHARED_IMPORT
#else
#define TINYSPLINE_SHARED_EXPORT
#define TINYSPLINE_SHARED_IMPORT
#endif
#ifdef TINYSPLINE_SHARED
#ifdef TINYSPLINE_EXPORT
#define TINYSPLINE_API TINYSPLINE_SHARED_EXPORT
#else
#define TINYSPLINE_API TINYSPLINE_SHARED_IMPORT
#endif
#else
#define TINYSPLINE_API
#endif
#endif
#ifdef __cplusplus
extern "C" {
#endif
/*! @} */
/*! @name Predefined Constants
*
* The following constants have been adjusted to maintain internal consistency
* and should only be changed with great caution! The values chosen should be
* suitable for most environments and can be used with float (single) and
* double precision (see ::tsReal). If changes are necessary, please read the
* documentation of the constants in advance.
*
* @{
*/
/**
* The mathematical constant pi.
*/
#define TS_PI 3.14159265358979323846
/**
* The maximum number of knots a spline can have. This constant is strongly
* related to ::TS_KNOT_EPSILON in that the larger ::TS_MAX_NUM_KNOTS is, the
* less precise ::TS_KNOT_EPSILON has to be (i.e., knots with greater distance
* are considered equal). Likewise, the more precise ::TS_KNOT_EPSILON is
* (i.e., knots with smaller distance are considered equal), the less
* ::TS_MAX_NUM_KNOTS has to be. By default, the relation between
* ::TS_MAX_NUM_KNOTS and ::TS_KNOT_EPSILON is as follows:
*
* TS_MAX_NUM_KNOTS = 1 / TS_KNOTS_EPSILON
*/
#define TS_MAX_NUM_KNOTS 10000
/**
* The minimum of the domain of newly created splines. Must be less than
* ::TS_DOMAIN_DEFAULT_MAX. This constant is used only when creating new
* splines. After creation, the domain of a spline can be adjusted as needed.
*/
#define TS_DOMAIN_DEFAULT_MIN 0.0f
/**
* The maximum of the domain of newly created splines. Must be greater than
* ::TS_DOMAIN_DEFAULT_MIN. This constant is used only when creating new
* splines. After creation, the domain of a spline can be adjusted as needed.
*/
#define TS_DOMAIN_DEFAULT_MAX 1.0f
/**
* If the distance between two knots falls below this threshold, they are
* considered equal. Must be positive ( > 0 ). This constant is strongly
* related to ::TS_MAX_NUM_KNOTS in that the more precise ::TS_KNOT_EPSILON is
* (i.e., knots with smaller distance are considered equal), the less
* ::TS_MAX_NUM_KNOTS has to be. Likewise, the larger ::TS_MAX_NUM_KNOTS is,
* the less precise ::TS_KNOT_EPSILON has to be (i.e., knots with greater
* distance are considered equal). By default, the relation between
* ::TS_KNOT_EPSILON and ::TS_MAX_NUM_KNOTS is as follows:
*
* TS_KNOT_EPSILON = 1 / TS_MAX_NUM_KNOTS
*
* It is recommended that ::TS_KNOT_EPSILON is aligned to the span of
* ::TS_DOMAIN_DEFAULT_MIN and ::TS_DOMAIN_DEFAULT_MAX. That is, adjacent
* floating point values in the domain [::TS_DOMAIN_DEFAULT_MIN,
* ::TS_DOMAIN_DEFAULT_MAX] should not be equal according to
* ::TS_KNOT_EPSILON. This is in particular recommended when ::TS_KNOT_EPSILON
* and ::TS_MAX_NUM_KNOTS are related to each other as described above.
*/
#define TS_KNOT_EPSILON 1e-4f
/**
* If the distance between two (control) points is less than or equal to this
* threshold, they are considered equal. This constant is not used directly by
* the C interface. Rather, it serves as a viable default value for functions
* requiring an epsilon environment to decide whether two (control) points are
* equal or not. The C++ interface, for example, uses this as default value for
* optional parameters.
*/
#ifdef TINYSPLINE_FLOAT_PRECISION
#define TS_POINT_EPSILON 1e-3f
#else
#define TS_POINT_EPSILON 1e-5f
#endif
/**
* If the length of an element (e.g., a vector) is less than this threshold,
* the length is considered \c 0. Must be positive ( > 0 ).
*/
#ifdef TINYSPLINE_FLOAT_PRECISION
#define TS_LENGTH_ZERO 1e-3f
#else
#define TS_LENGTH_ZERO 1e-4f
#endif
/*! @} */
/*! @name API Configuration
*
* In the following section, different aspects of TinySpline's API can be
* configured (compile-time). It is recommended to configure the API by
* supplying the corresponding preprocessor definition(s). That said, there is
* nothing wrong with editing the source code directly.
*
* @{
*/
/**
* TinySpline uses its own typedef for floating point numbers. Supported are
* floats (single precision) and doubles (double precision). By default,
* doubles are used. Note that this typedef affects the entire API (i.e., types
* are not mixed; all structs and functions rely only on tsReal). Float
* precision is primarily used in combination with GLUT because GLUT's API
* doesn't support doubles:
*
* https://www.glprogramming.com/red/chapter12.html
*
* Generally, double precision is the right choice. Floats are mainly supported
* for legacy reasons. Yet, floats are not considered deprecated! If necessary,
* tsReal can also be typedefed to any other floating point representation. In
* this case, make sure to adjust TS_MAX_NUM_KNOTS and TS_KNOT_EPSILON
* (cf. Section "Predefined Constants").
*/
#ifdef TINYSPLINE_FLOAT_PRECISION
typedef float tsReal;
#else
typedef double tsReal;
#endif
/*! @} */
/*! @name Error Handling
*
* There are three types of error handling in TinySpline.
*
* 1. Return value: Functions that can fail return a special error code
* (::tsError). If the error code is not \c 0 (::TS_SUCCESS), an error occurred
* during execution. For example:
*
* if ( ts_bspline_to_beziers(&spline, &beziers, NULL) ) {
* ... An error occurred ...
* }
*
* It is of course possible to check the actual type of error:
*
* tsError error = ts_bspline_to_beziers(&spline, &beziers, NULL);
* if (error == TS_MALLOC) {
* ... Out of memory ...
* } else if (error == ...
*
* This type of error handling is used in many C programs. The disadvantage
* is that there is no additional error message besides the error code (with
* which the cause of an error could be specified in more detail). Some
* libraries make do with global variables in which error messages are stored
* for later purpose (e.g., \a errno and \a strerror). Unfortunately,
* however, this approach (by design) is often not thread-safe. The second
* error handling option solves this issue.
*
* 2. ::tsStatus objects: Functions that can fail do not only return an error
* code, but also take a pointer to a ::tsStatus object as an optional
* parameter. In the event of an error, and if the supplied pointer is not
* NULL, the error message is stored in tsStatus#message and can be accessed by
* the caller. Using a ::tsStatus object, the example given in 1. can be
* modified as follows:
*
* tsStatus status;
* if ( ts_bspline_to_beziers(&spline, &beziers, &status) ) {
* status.code; // error code
* status.message; // error message
* }
*
* Note that ::tsStatus objects can be reused:
*
* tsStatus status;
* if ( ts_bspline_to_beziers(&spline, &beziers, &status) ) {
* ...
* }
* ...
* if ( ts_bspline_derive(&beziers, 1, 0.001, &beziers, &status) ) {
* ...
* }
*
* If you would like to use this type of error handling in your own functions
* (in particular the optional ::tsStatus parameter), you may wonder whether
* there is an easy way to return error codes and format error messages. This
* is where the macros ::TS_RETURN_0 -- ::TS_RETURN_4 come into play. They
* can, for example, be used as follows:
*
* tsError my_function(..., tsStatus *status, ...)
* {
* ...
* tsReal *points = (tsReal *) malloc(len * sizeof(tsReal));
* if (!points)
* TS_RETURN_0(status, TS_MALLOC, "out of memory")
* ...
* }
*
* The \c N in \c TS_RETURN_<N> denotes the number of format specifier in the
* supplied format string (cf. sprintf(char *, const char *, ... )).
*
* 3. Try-catch-finally blocks: TinySpline provides a set of macros that can be
* used when a complex control flow is necessary. The macros create a structure
* that is similar to the exception handling mechanisms of high-level languages
* such as C++. The basic structure is as follows:
*
* TS_TRY(try, error, status) // `status' may be NULL
* ...
* TS_CALL( try, error, ts_bspline_to_beziers(
* &spline, &beziers, status) )
* ...
* TS_CATCH(error)
* ... Executed in case of an error ...
* ... `error' (tsError) indicates the type of error.
* ... `status' (tsStatus) contains the error code and message ...
* TS_FINALLY
* ... Executed in any case ...
* TS_END_TRY
*
* ::TS_TRY and ::TS_END_TRY mark the boundaries of a try-catch-finally
* block. Every block has an identifier (name) that must be unique within a
* scope. The name of a block is set via the first argument of ::TS_TRY (\c
* try in the example listed above). The control flow of a try-catch-finally
* block is directed via the second and third argument of ::TS_TRY (\c error
* and \c status in the example listed above) and the utility macro
* ::TS_CALL. The second argument of ::TS_TRY, a ::tsError, is mandatory. The
* third argument of ::TS_TRY, a ::tsStatus object, is optional, that is, it
* may be \c NULL. ::TS_CALL serves as a wrapper for functions with return
* type ::tsError. If the called functions fails (more on that later),
* ::TS_CALL immediately jumps into the ::TS_CATCH section where \c error and
* \c status can be evaluated as needed (note that \c status may be \c
* NULL). The ::TS_FINALLY section is executed in any case and is in
* particularly helpful when resources (such as heap-memory, file-handles
* etc.) must be released.
*
* While ::TS_CALL can be used to wrap functions with return type ::tsError,
* sooner or later it will be necessary to delegate the failure of other
* kinds of functions (i.e., functions outside of TinySpline; e.g.,
* malloc(size_t)). This is the purpose of the ::TS_THROW_0 -- ::TS_THROW_4
* macros. It is not by coincidence that the signature of the \c TS_THROW_<N>
* macros is quite similar to that of the \c TS_RETURN_<N> macros. Both
* "macro groups" are used to report errors. The difference between \c
* TS_RETURN_<N> and TS_THROW_<N>, however, is that the former exits a
* function (i.e., a \c return statement is inserted by these macros) while
* the latter jumps into a catch block (the catch block to jump into is set
* via the first argument of \c TS_THROW_<N>):
*
* tsBSpline spline = ts_bspline_init();
* tsReal *points = NULL;
* TS_TRY(try, error, status)
* ...
* tsReal *points = (tsReal *) malloc(len * sizeof(tsReal));
* if (!points)
* TS_THROW_0(try, status, TS_MALLOC, "out of memory")
* ...
* TS_CALL( try, error, ts_bspline_interpolate_cubic_natural(
* points, len / dim, dim, &spline, status) )
* ...
* TS_CATCH(error)
* ... Log error message ...
* TS_FINALLY
* ts_bspline_free(&spline);
* if (points)
* free(points);
* TS_END_TRY
*
* In all likelihood, you are already familiar with this kind error
* handling. Actually, there are a plethora of examples available online
* showing how exception-like error handling can be implemented in C. What
* most of these examples have in common is that they suggest to wrap the
* functions \c setjmp and \c longjmp (see setjmp.h) with macros. While this
* undoubtedly is a clever trick, \c setjmp and \c longjmp have no viable
* (i.e, thread-safe) solution for propagating the cause of an error (in the
* form of a human-readable error message) back to the client of a
* library. Therefore, TinySpline implements try-catch-finally blocks with \c
* if statements, labels, and \c goto statements (TS_THROW_<N>).
*
* ::TS_TRY is flexible enough to be used in functions that are in turn
* embedded into TinySpline's error handling system:
*
* tsError my_function(..., tsStatus *status, ...)
* {
* tsError error;
* TS_TRY(try, error, status)
* ...
* TS_END_TRY
* return error;
* }
*
* as well as functions forming the root of a call stack that uses
* TinySpline's error handling system:
*
* tsStatus status;
* TS_TRY(try, status.code, &status)
* ...
* TS_END_TRY
*
* There is some utility macros that might be useful when dealing with
* try-catch-finally blocks:
*
* - ::TS_END_TRY_RETURN: Returns the supplied error code immediately after
* completing the corresponding block. Can be used as follows:
*
* tsError my_function(..., tsStatus *status, ...)
* {
* tsError error;
* TS_TRY(try, error, status)
* ...
* TS_END_TRY_RETURN(error)
* }
*
* - ::TS_END_TRY_ROE: Like ::TS_END_TRY_RETURN but returns the supplied
* error code, \c e, if \c e is not ::TS_SUCCESS (\c ROE means
* <b>R</b>eturn <b>O</b>n <b>E</b>rror). Can be used as follows:
*
* tsError my_function(..., tsStatus *status, ...)
* {
* tsError error;
* TS_TRY(try, error, status)
* ...
* TS_END_TRY_ROE(error)
* ... Additional code. The code is executed only if `error' is
* TS_SUCCESS, that is, if no error occurred in the try block
* above ...
* }
*
* - ::TS_CALL_ROE: Calls the supplied function and returns its error code,
* \c e, if \c e is not ::TS_SUCCESS. This macro can be seen as a \e
* minified try block (a dedicated try block is not needed).
*
* - ::TS_RETURN_SUCCESS: Shortcut for ::TS_RETURN_0 with error code
* ::TS_SUCCESS and an empty error message.
*
* @{
*/
/**
* Defines different error codes.
*/
typedef enum
{
/** No error. */
TS_SUCCESS = 0,
/** Memory cannot be allocated (malloc, realloc etc.). */
TS_MALLOC = -1,
/** Points have dimensionality 0. */
TS_DIM_ZERO = -2,
/** degree >= num(control_points). */
TS_DEG_GE_NCTRLP = -3,
/** Knot is not within the domain. */
TS_U_UNDEFINED = -4,
/** multiplicity(knot) > order */
TS_MULTIPLICITY = -5,
/** Decreasing knot vector. */
TS_KNOTS_DECR = -6,
/** Unexpected number of knots. */
TS_NUM_KNOTS = -7,
/** Spline is not derivable. */
TS_UNDERIVABLE = -8,
/** len(control_points) % dimension != 0. */
TS_LCTRLP_DIM_MISMATCH = -10,
/** Error while reading/writing a file. */
TS_IO_ERROR = -11,
/** Error while parsing a serialized entity. */
TS_PARSE_ERROR = -12,
/** Index does not exist (e.g., when accessing an array). */
TS_INDEX_ERROR = -13,
/** Function returns without result (e.g., approximations). */
TS_NO_RESULT = -14,
/** Unexpected number of points. */
TS_NUM_POINTS = -15
} tsError;
/**
* Stores an error code (see ::tsError) with corresponding message.
*/
typedef struct
{
/** The error code. */
tsError code;
/**
* The corresponding error message (encoded as C string). Memory is
* allocated on stack so as to be able to provide a meaningful message
* in the event of memory issues (cf. ::TS_MALLOC).
*/
char message[100];
} 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_END_TRY_ROE(error) \
TS_END_TRY if ((error)) 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_RETURN_4(status, error, msg, arg1, arg2, arg3, arg4) \
{ \
if ((status) != NULL) { \
(status)->code = error; \
sprintf((status)->message, msg, arg1, arg2, arg3, arg4); \
} \
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 ## __; \
}
#define TS_THROW_4(label, error, status, val, msg, arg1, arg2, arg3, arg4) \
{ \
(error) = val; \
if ((status) != NULL) { \
(status)->code = val; \
sprintf((status)->message, msg, arg1, arg2, arg3, arg4); \
} \
goto __ ## label ## __; \
}
/*! @} */
/*! @name Utility Structs and Enums
*
* @{
*/
/**
* Describes the structure of the knot vector. More details can be found at:
*
* www.cs.mtu.edu/~shene/COURSES/cs3621/NOTES/spline/B-spline/bspline-curve.html
*/
typedef enum
{
/** Uniformly spaced knot vector with opened end knots. */
TS_OPENED = 0,
/** Uniformly spaced knot vector with clamped end knots. */
TS_CLAMPED = 1,
/**
* Uniformly spaced knot vector where the multiplicity of each knot is
* equal to the order of the spline.
*/
TS_BEZIERS = 2
} tsBSplineType;
/**
* A three-dimensional TNB-vector with position. More details can be found at:
*
* https://en.wikipedia.org/wiki/Frenet-Serret_formulas
* https://www.math.tamu.edu/~tkiffe/calc3/tnb/tnb.html
*
* TNB stands for \e tangent, \e normal, and \e binormal.
*/
typedef struct
{
/** Position of the TNB-vector. */
tsReal position[3];
/** Tangent of the TNB-vector. */
tsReal tangent[3];
/** Normal of the TNB-vector. */
tsReal normal[3];
/** Binormal of the TNB-vector. */
tsReal binormal[3];
} tsFrame;
/*! @} */
/*! @name B-Spline Data
*
* The internal state of ::tsBSpline is protected using the PIMPL design
* pattern (see https://en.cppreference.com/w/cpp/language/pimpl for more
* details). The data of an instance can be accessed with the functions listed
* in this section.
*
* @{
*/
/**
* Represents a B-Spline, which may also be used for NURBS, Bezier curves,
* lines, and points. NURBS use homogeneous coordinates to store their control
* points (i.e. the last component of a control point stores the weight).
* Bezier curves are B-Splines with 'num_control_points == order' and a
* clamped knot vector, which lets them pass through their first and last
* control point (a property which does not necessarily apply to B-Splines and
* NURBS). Lines and points, on that basis, are Bezier curves of degree 1
* (lines) and 0 (points).
*
* 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. As already mentioned, NURBS use homogeneous coordinates to
* store their control points. For example, a NURBS in 2D stores its control
* points as follows:
*
* [x_0*w_0, y_0*w_0, w_0, x_1*w_1, y_1*w_1, w_1, ...]
*
* where 'w_i' is the weight of the i'th control point.
*/
typedef struct
{
struct tsBSplineImpl *pImpl; /**< The actual implementation. */
} tsBSpline;
/**
* Returns the degree of \p spline.
*
* @param[in] spline
* The spline whose degree is read.
* @return
* The degree of \p spline.
*/
size_t TINYSPLINE_API
ts_bspline_degree(const tsBSpline *spline);
/**
* 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 TINYSPLINE_API
ts_bspline_order(const tsBSpline *spline);
/**
* Returns the dimensionality of \p spline, that is, the number of components
* of its control points (::ts_bspline_control_points). 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 (>= 1).
*/
size_t TINYSPLINE_API
ts_bspline_dimension(const tsBSpline *spline);
/**
* Returns the length of the control point array of \p spline.
*
* @param[in] spline
* The spline whose length of the control point array is read.
* @return
* The length of the control point array of \p spline.
*/
size_t TINYSPLINE_API
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 TINYSPLINE_API
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 \e memcpy or \e memmove.
*
* @param[in] spline
* The spline whose size of the control point array is read.
* @return
* The size of the control point array of \p spline.
*/
size_t TINYSPLINE_API
ts_bspline_sof_control_points(const tsBSpline *spline);
/**
* Returns the pointer to the control point array of \p spline. Note that the
* return type of this function is \c const for a reason. Clients should only
* read the returned array. When suppressing the constness and writing to the
* array against better knowledge, the client is on its own with regard to the
* consistency of the internal state of \p spline. If the control points of a
* spline need to be changed, use ::ts_bspline_control_points to obtain a copy
* of the control point array and ::ts_bspline_set_control_points to copy the
* changed values back to the spline.
*
* @param[in] spline
* The spline whose pointer to the control point array is returned.
* @return
* Pointer to the control point array of \p spline.
*/
const tsReal TINYSPLINE_API *
ts_bspline_control_points_ptr(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. \b Note: It is the responsibility of the client to
* release the allocated memory after use.
* @param[out] status
* The status of this function. May be NULL.
* @return TS_SUCCESS
* On success.
* @return TS_MALLOC
* If allocating memory failed.
*/
tsError TINYSPLINE_API
ts_bspline_control_points(const tsBSpline *spline,
tsReal **ctrlp,
tsStatus *status);
/**
* Returns the pointer to the control point of \p spline at \p index. Note that
* the type of the out parameter \p ctrlp is \c const for a reason. Clients
* should only read the returned array. When suppressing the constness of \p
* ctrlp and writing to the array against better knowledge, the client is on
* its own with regard to the consistency of the internal state of \p
* spline. If one of the control points of a spline needs to be changed, use
* ::ts_bspline_set_control_points to copy the new control point to the spline.
*
* @param[in] spline
* The spline whose pointer to the control point at \p index is returned.
* @param[in] index
* Zero-based index of the control point to be returned.
* @param[out] ctrlp
* Pointer to the control point of \p spline at \p index.
* @param[out] status
* The status of this function. May be NULL.
* @return TS_SUCCESS
* On success.
* @return TS_INDEX_ERROR
* If \p index is out of range.
*/
tsError TINYSPLINE_API
ts_bspline_control_point_at_ptr(const tsBSpline *spline,
size_t index,
const tsReal **ctrlp,
tsStatus *status);
/**
* Sets the control points of \p spline. Creates a deep copy of \p ctrlp.
*
* @pre
* \p ctrlp has length ::ts_bspline_len_control_points.
* @param[out] spline
* The spline whose control points are set.
* @param[in] ctrlp
* The value.
* @param[out] status
* The status of this function. May be NULL.
* @return TS_SUCCESS
* On success.
*/
tsError TINYSPLINE_API
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.
*
* @pre
* \p ctrlp has length ::ts_bspline_dimension.
* @param[out] spline
* The spline whose control point is set at \p index.
* @param[in] index
* Zero-based index of the control point to be set.
* @param[in] ctrlp
* The value.
* @param[out] status
* The status of this function. May be NULL.
* @return TS_SUCCESS
* On success.
* @return TS_INDEX_ERROR
* If \p index is out of range.
*/
tsError TINYSPLINE_API
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 TINYSPLINE_API
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 \e memcpy or \e memmove.
*
* @param[in] spline
* The spline whose size of the knot array is read.
* @return TS_SUCCESS
* The size of the knot array of \p spline.
*/
size_t TINYSPLINE_API
ts_bspline_sof_knots(const tsBSpline *spline);
/**
* Returns the pointer to the knot vector of \p spline. Note that the return
* type of this function is \c const for a reason. Clients should only read the
* returned array. When suppressing the constness and writing to the array
* against better knowledge, the client is on its own with regard to the
* consistency of the internal state of \p spline. If the knot vector of a
* spline needs to be changed, use ::ts_bspline_knots to obtain a copy of the
* knot vector and ::ts_bspline_set_knots to copy the changed values back to
* the spline.
*
* @param[in] spline
* The spline whose pointer to the knot vector is returned.
* @return
* Pointer to the knot vector of \p spline.
*/
const tsReal TINYSPLINE_API *
ts_bspline_knots_ptr(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. \b Note: It is the responsibility of the client to
* release the allocated memory after use.
* @param[out] status
* The status of this function. May be NULL.
* @return TS_SUCCESS
* On success.
* @return TS_MALLOC
* If allocating memory failed.
*/
tsError TINYSPLINE_API
ts_bspline_knots(const tsBSpline *spline,
tsReal **knots,
tsStatus *status);
/**
* Returns the knot of \p spline at \p index.
*
* @param[in] spline
* The spline whose knot is read at \p index.
* @param[in] index
* Zero-based index of the knot to be read.
* @param[out] knot
* The output value.
* @param[out] status
* The status of this function. May be NULL.
* @return TS_SUCCESS
* On success.
* @return TS_INDEX_ERROR
* If \p index is out of range.
*/
tsError TINYSPLINE_API
ts_bspline_knot_at(const tsBSpline *spline,
size_t index,
tsReal *knot,
tsStatus *status);
/**
* Sets the knots of \p spline. Creates a deep copy of \p knots.
*
* @pre
* \p knots has length ::ts_bspline_num_knots.
* @param[out] spline
* The spline whose knots are set.
* @param[in] knots
* The value.
* @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