summaryrefslogtreecommitdiff
path: root/support/dktools/dk4xsp.c
blob: d8f32255fa6543488069b1e6baf0d0a4a88ef126 (plain)
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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
/*
Copyright (C) 2018-2020, Dirk Krause
SPDX-License-Identifier: BSD-3-Clause
*/

/*
	WARNING: This file was generated by the dkct program (see
	http://dktools.sourceforge.net/ for details).
	Changes you make here will be lost if dkct is run again!
	You should modify the original source and run dkct on it.
	Original source: dk4xsp.ctr
*/

/**	@file dk4xsp.c The dk4xsp module.
*/


#line 436 "dk4xsp.ctr"


#include "dk4xsp.h"



#ifndef	DK4MEM_H_INCLUDED
#include "dk4mem.h"
#endif

#ifndef	DK4MATH_H_INCLUDED
#include "dk4math.h"
#endif

#if DK4_HAVE_ASSERT_H
#ifndef	ASSERT_H_INCLUDED
#include <assert.h>
#define	ASSERT_H_INCLUDED 1
#endif
#endif




#line 459 "dk4xsp.ctr"



/**	Approximation function.
*/
#define	FCT_F(u,p) \
u*u*u*(u*(u*(6.0-p)+2.0*p-15.0)+10.0-p)

/**	Interpolation function for 0<=u<=1.
*/
#define	FCT_G(u,p,q) \
u*(u*(u*(u*(u*(6.0-5.0*q-p)+2.0*p+14.0*q-15.0)+10.0-12.0*q-p)+2.0*q)+q)

/**	Interpolation function for -1<=u<0.
*/
#define	FCT_H(u,q) \
u*(u*(u*u*(-2.0*q-q*u)+2.0*q)+q)

/**	First derivative of approximation function.
*/
#define	FCT_DFDU(u,p) \
u*u*(u*(u*(30.0-5.0*p)+8.0*p-60.0)+30.0-3.0*p)

/**	First derivative of interpolation function for 0<=u<=1.
*/
#define	FCT_DGDU(u,p,q) \
u*(u*(u*(u*(30.0-25.0*q-5.0*p)+8.0*p+56.0*q-60.0)+30.0-36.0*q-3.0*p)+4.0*q)+q

/**	First derivative of interpolation function for -1<=u<0.
*/
#define	FCT_DHDU(u,q) \
u*(4.0*q-u*u*(8.0*q+5.0*q*u))+q



#if	TRACE_DEBUG
/**	Show one array contents.
	@param	ptr	Array start address.
*/
static
void
dk4xsp_show_array4(double const *ptr)
{
	size_t	i;
	for (i = 0; i < 4; i++) {
		

#line 505 "dk4xsp.ctr"
	}
}
#endif



/**	Calculate approximation blending function value.
	@param	u	Parameter u.
	@param	p	Constant value p derived from shape factor.
	@return	Calculation result.
*/

static
double fct_f(double u, double p)
{
#if	TRACE_DEBUG
	double	back	= 0.0;
	back = FCT_F(u,p);
	

#line 524 "dk4xsp.ctr"
	return back;
#else
	return ( FCT_F(u,p) );
#endif
}



/**	Calculate interpolation blending function value for positive u arguments.
	@param	u	Parameter u.
	@param	p	Constant value p derived from shape factor.
	@param	q	Constant value q derived from shape factor.
	@return	Calculation result.
*/

static
double fct_g(double u, double p, double q)
{
#if	TRACE_DEBUG
	double	back	= 0.0;
	back = FCT_G(u,p,q);
	

#line 546 "dk4xsp.ctr"
	return back;
#else
	return ( FCT_G(u,p,q) );
#endif
}



/**	Calculate interpolation blending function value for negative u arguments.
	@param	u	Parameter u.
	@param	q	Constant value q derived from shape factor.
	@return	Calculation result.
*/

static
double fct_h(double u, double q)
{
#if	TRACE_DEBUG
	double	back	= 0.0;
	back = FCT_H(u,q);
	

#line 567 "dk4xsp.ctr"
	return back;
#else
	return ( FCT_H(u,q) );
#endif
}



/**	Calculate approximation blending function derivative value.
	@param	u	Parameter u.
	@param	p	Constant value p derived from shape factor.
	@return	Calculation result.
*/

static
double fct_dfdu(double u, double p)
{
#if	TRACE_DEBUG
	double back;
	back = FCT_DFDU(u,p);
	

#line 588 "dk4xsp.ctr"
	return back;
#else
	return ( FCT_DFDU(u,p) );
#endif
}



/**	Calculate interpolation blending function derivative value
	for positive u arguments.
	@param	u	Parameter u.
	@param	p	Constant value p derived from shape factor.
	@param	q	Constant value q derived from shape factor.
	@return	Calculation result.
*/

static
double fct_dgdu(double u, double p, double q)
{
#if	TRACE_DEBUG
	double	back;
	back = FCT_DGDU(u,p,q);
	

#line 611 "dk4xsp.ctr"
	return back;
#else
	return ( FCT_DGDU(u,p,q) );
#endif
}



/**	Calculate interpolation blending function derivative value
	for negative u arguments.
	@param	u	Parameter u.
	@param	q	Constant value q derived from shape factor.
	@return	Calculation result.
*/

static
double fct_dhdu(double u, double q)
{
#if	TRACE_DEBUG
	double back;
	back = FCT_DHDU(u,q);
	

#line 633 "dk4xsp.ctr"
	return back;
#else
	return ( FCT_DHDU(u,q) );
#endif
}



/**	Prepare a coordinates calculation, calculate blending functions
	values and optionally blending functions derivatives values.
	@param	xsp	Spline structure to use.
	@param	t	Parameter t in range 0 to 1.
	@param	d	Flag: Derivatives required.
*/

static
void
dk4xsp_i_prepare(
	dk4_xsp_t	*xsp,
	double		 t,
	int			 d
)
{
	size_t	i;
	

#line 658 "dk4xsp.ctr"
#if	DK4_USE_ASSERT
	assert(NULL != xsp);
#endif
	for (i = 0; i < 4; i++) { xsp->f[i] = xsp->u[i] = 0.0; }
	if (0.0 > xsp->s[1]) {		

#line 663 "dk4xsp.ctr"
		xsp->u[0] = 0.0 - t;
		xsp->u[2] = t;
		if ((uint8_t)0U != xsp->h[0]) {
			xsp->f[0] = fct_h(xsp->u[0], xsp->q[0]);
		}
		xsp->f[2] = fct_g(xsp->u[2], xsp->p[2], xsp->q[2]);
	}
	else {						

#line 671 "dk4xsp.ctr"
		xsp->u[0] = (xsp->s[1] - t) / (1.0 + xsp->s[1]);
		xsp->u[2] = (xsp->s[1] + t) / (1.0 + xsp->s[1]);
		if (t < xsp->s[1]) {	

#line 674 "dk4xsp.ctr"
			if ((uint8_t)0U != xsp->h[0]) {	

#line 675 "dk4xsp.ctr"
				xsp->f[0] = fct_f(xsp->u[0], xsp->p[0]);
			}
#if	TRACE_DEBUG
			else {							

#line 679 "dk4xsp.ctr"
			}
#endif
		}
#if	TRACE_DEBUG
		else {					

#line 684 "dk4xsp.ctr"
		}
#endif
		xsp->f[2] = fct_f(xsp->u[2], xsp->p[2]);
	}
	if (0.0 > xsp->s[2]) {		

#line 689 "dk4xsp.ctr"
		xsp->u[1] = 1.0 - t;
		xsp->u[3] = t - 1.0;
		xsp->f[1] = fct_g(xsp->u[1], xsp->p[1], xsp->q[1]);
		if ((uint8_t)0U != xsp->h[3]) {
			xsp->f[3] = fct_h(xsp->u[3], xsp->q[3]);
		}
	}
	else {						

#line 697 "dk4xsp.ctr"
		xsp->u[1] = (1.0 + xsp->s[2] - t) / (1.0 + xsp->s[2]);
		xsp->u[3] = (t + xsp->s[2] - 1.0) / (1.0 + xsp->s[2]);
		xsp->f[1] = fct_f(xsp->u[1], xsp->p[1]);
		if (t > (1.0 - xsp->s[2])) {		

#line 701 "dk4xsp.ctr"
			if ((uint8_t)0U != xsp->h[3]) {	

#line 702 "dk4xsp.ctr"
				xsp->f[3] = fct_f(xsp->u[3], xsp->p[3]);
			}
			else {							

#line 705 "dk4xsp.ctr"
			}
		}
		else {								

#line 708 "dk4xsp.ctr"
		}
	}

	if (0 == d) { goto finished; }

	for (i = 0; i < 4; i++) { xsp->dfdt[i] = 0.0; }
	if (0.0 > xsp->s[1]) {
		if ((uint8_t)0U != xsp->h[0]) {
			xsp->dfdt[0] = fct_dhdu(xsp->u[0], xsp->q[0]);
			xsp->dfdt[0] *= xsp->dudt[0];
		}
		xsp->dfdt[2] = fct_dgdu(xsp->u[2], xsp->p[2], xsp->q[2]);
		xsp->dfdt[2] *= xsp->dudt[2];
	}
	else {
		if (t < xsp->s[1]) {				

#line 724 "dk4xsp.ctr"
			if ((uint8_t)0U != xsp->h[0]) {	

#line 725 "dk4xsp.ctr"
				xsp->dfdt[0] = fct_dfdu(xsp->u[0], xsp->p[0]);
				xsp->dfdt[0] *= xsp->dudt[0];
			}
#if	TRACE_DEBUG
			else {							

#line 730 "dk4xsp.ctr"
			}
#endif
		}
#if	TRACE_DEBUG
		else {								

#line 735 "dk4xsp.ctr"
		}
#endif

		xsp->dfdt[2] = fct_dfdu(xsp->u[2], xsp->p[2]);
		xsp->dfdt[2] *= xsp->dudt[2];
	}

	if (0.0 > xsp->s[2]) {
		xsp->dfdt[1] = fct_dgdu(xsp->u[1], xsp->p[1], xsp->q[1]);
		xsp->dfdt[1] *= xsp->dudt[1];
		if ((uint8_t)0U != xsp->h[3]) {
			xsp->dfdt[3] = fct_dhdu(xsp->u[3], xsp->q[3]);
			xsp->dfdt[3] *= xsp->dudt[3];
		}
	}
	else {
		xsp->dfdt[1] = fct_dfdu(xsp->u[1], xsp->p[1]);
		xsp->dfdt[1] *= xsp->dudt[1];
		if (t > (1.0 - xsp->s[2])) {		

#line 754 "dk4xsp.ctr"
			if ((uint8_t)0U != xsp->h[3]) {	

#line 755 "dk4xsp.ctr"
				xsp->dfdt[3] = fct_dfdu(xsp->u[3], xsp->p[3]);
				xsp->dfdt[3] *= xsp->dudt[3];
			}
#if	TRACE_DEBUG
			else {							

#line 760 "dk4xsp.ctr"
			}
#endif
		}
#if	TRACE_DEBUG
		else {								

#line 765 "dk4xsp.ctr"
		}
#endif
	}

	finished:
	

#line 771 "dk4xsp.ctr"
	

#line 772 "dk4xsp.ctr"
	

#line 773 "dk4xsp.ctr"
	

#line 774 "dk4xsp.ctr"
	

#line 775 "dk4xsp.ctr"
	

#line 776 "dk4xsp.ctr"
	

#line 777 "dk4xsp.ctr"
	

#line 778 "dk4xsp.ctr"
	

#line 779 "dk4xsp.ctr"
	

#line 780 "dk4xsp.ctr"
	

#line 781 "dk4xsp.ctr"
	

#line 782 "dk4xsp.ctr"
	

#line 783 "dk4xsp.ctr"
	

#line 784 "dk4xsp.ctr"
	

#line 785 "dk4xsp.ctr"
	return;
}



/**	Calculate coordinates for one dimension.
	@param	rp	Address of result variable or result array.
				When calculating the derivative, rp must be a
				2 elements array.
	@param	xsp	Spline structure to use.
	@param	x	Array of 4 coordinates.
	@param	d	Flag: Calculate derivative.
	@param	erp	Error report, may be NULL.
	@return	1 on success, 0 on error.
*/

static
int
dk4xsp_i_calculate(
	double			*rp,
	dk4_xsp_t const	*xsp,
	double const	*x,
	int				 d,
	dk4_er_t		*erp
)
{
	double	 z;
	double	 n;
	double	 dzdt;
	double	 dndt;
	int		 back	=	1;
	

#line 817 "dk4xsp.ctr"

#if	DK4_USE_ASSERT
	assert(NULL != rp);
	assert(NULL != xsp);
#endif
	/*	Calculate coordinates
	*/
	z = x[1] * xsp->f[1] + x[2] * xsp->f[2];
	n = xsp->f[1] + xsp->f[2];
	if (DK4_UINT8_0 != xsp->h[0]) {
		z += x[0] * xsp->f[0];
		n += xsp->f[0];
	}
	if (DK4_UINT8_0 != xsp->h[3]) {
		z += x[3] * xsp->f[3];
		n += xsp->f[3];
	}
	*rp = z / n;
	if (!(dk4ma_is_finite(*rp))) {	

#line 836 "dk4xsp.ctr"
		back = 0;
		dk4error_set_simple_error_code(erp, DK4_E_MATH_OVERFLOW);
	}

	/*	Abort on errors or if no derivative needed
	*/
	if ((0 == d) || (0 == back)) { goto finished; }

	/*	Calculate derivative
	*/
	dzdt = x[1] * xsp->dfdt[1] + x[2] * xsp->dfdt[2];
	dndt = xsp->dfdt[1] + xsp->dfdt[2];
	if (DK4_UINT8_0 != xsp->h[0]) {
		dzdt += x[0] * xsp->dfdt[0];
		dndt += xsp->dfdt[0];
	}
	if (DK4_UINT8_0 != xsp->h[3]) {
		dzdt += x[3] * xsp->dfdt[3];
		dndt += xsp->dfdt[3];
	}
	rp[1] = (n * dzdt - z * dndt) / (n * n);
	if (!(dk4ma_is_finite(rp[1]))) {	

#line 858 "dk4xsp.ctr"
		back = 0;
		dk4error_set_simple_error_code(erp, DK4_E_MATH_OVERFLOW);
	}

	finished:
	

#line 864 "dk4xsp.ctr"
	

#line 865 "dk4xsp.ctr"
	

#line 866 "dk4xsp.ctr"
	

#line 867 "dk4xsp.ctr"
	

#line 868 "dk4xsp.ctr"
	return back;
}



void
dk4xsp_reset_data(dk4_xsp_t *xsp)
{
	size_t		i;		/* Traverse arrays */
	

#line 878 "dk4xsp.ctr"
#if	DK4_USE_ASSERT
	assert(NULL != xsp);
#endif
	if (NULL != xsp) {
		for (i = 0; i < 4; i++) {
			xsp->s[i] = xsp->q[i] = 0.0;
			xsp->p[i] = 2.0;
			xsp->h[i] = ((uint8_t)0U);
		}
		xsp->dudt[0] = xsp->dudt[1] = -1.0;
		xsp->dudt[2] = xsp->dudt[3] =  1.0;
	}
	

#line 891 "dk4xsp.ctr"
	

#line 892 "dk4xsp.ctr"
	

#line 893 "dk4xsp.ctr"
}



void
dk4xsp_reset(dk4_xsp_t *xsp)
{
	

#line 901 "dk4xsp.ctr"
#if	DK4_USE_ASSERT
	assert(NULL != xsp);
#endif
	if (NULL != xsp) {
		DK4_MEMRES(xsp,sizeof(dk4_xsp_t));
		dk4xsp_reset_data(xsp);
		xsp->xfig = ((uint8_t)0U);
	}
	

#line 910 "dk4xsp.ctr"
}



void
dk4xsp_set_xfig(dk4_xsp_t *xsp, uint8_t val)
{
	

#line 918 "dk4xsp.ctr"
#if	DK4_USE_ASSERT
	assert(NULL != xsp);
#endif
	if (NULL != xsp) {
		xsp->xfig = val;
	}
	

#line 925 "dk4xsp.ctr"
}



void
dk4xsp_set_point(
	dk4_xsp_t	*xsp,
	double		 s,
	size_t		 i
)
{
	

#line 937 "dk4xsp.ctr"
#if	DK4_USE_ASSERT
	assert(NULL != xsp);
#endif
	if ((NULL != xsp) && (4 > i)) {
		xsp->s[i] = s;
		xsp->h[i] = ((uint8_t)1U);
		switch ((int)i) {
			case 1: {	/* Start of segment */
				if (0.0 > s) {	/* Interpolation */
					if (((uint8_t)0U) != xsp->xfig) {
						xsp->q[0] = xsp->q[2] = 0.0 - s;
					}
					else {
						xsp->q[0] = xsp->q[2] = -0.5 * s;
					}
				}
				else {			/* Approximation */
					xsp->p[0] = xsp->p[2] = 2.0 * (1.0 + s) * (1.0 + s);
					xsp->dudt[2] = 1.0 / (1.0 + s);
					xsp->dudt[0] = 0.0 - xsp->dudt[2];
				}
			} break;
			case 2: {	/* End of segment */
				if (0.0 > s) {	/* Interpolation */
					if (((uint8_t)0U) != xsp->xfig) {
						xsp->q[1] = xsp->q[3] = 0.0 - s;
					}
					else {
						xsp->q[1] = xsp->q[3] = -0.5 * s;
					}
				}
				else {			/* Approximation */
					xsp->p[1] = xsp->p[3] = 2.0 * (1.0 + s) * (1.0 + s);
					xsp->dudt[3] = 1.0 / (1.0 + s);
					xsp->dudt[1] = 0.0 - xsp->dudt[3];
				}
			} break;
		}
	}
	

#line 977 "dk4xsp.ctr"
}



int
dk4xsp_prepare(
	dk4_xsp_t	*xsp,
	double		 t,
	int			 d,
	dk4_er_t	*erp
)
{
	int		 back	= 0;
	

#line 991 "dk4xsp.ctr"
#if	DK4_USE_ASSERT
	assert(NULL != xsp);
	assert(0.0 <= t);
	assert(1.0 >= t);
#endif
	if ( (NULL != xsp) && (0.0 <= t) && (1.0 >= t)) {
		if (((uint8_t)0U != xsp->h[1]) && ((uint8_t)0U != xsp->h[2])) {
			back = 1;
			dk4xsp_i_prepare(xsp, t, d);
		}
		else {
			dk4error_set_simple_error_code(erp, DK4_E_INVALID_ARGUMENTS);
		}
	}
	else {
		dk4error_set_simple_error_code(erp, DK4_E_INVALID_ARGUMENTS);
	}
	

#line 1009 "dk4xsp.ctr"
	return back;
}



int
dk4xsp_calculate(
	double			*rp,
	dk4_xsp_t const	*xsp,
	double const	*x,
	int				 d,
	dk4_er_t		*erp
)
{
	int		 back	= 0;
	

#line 1025 "dk4xsp.ctr"
#if	DK4_USE_ASSERT
	assert(NULL != xsp);
	assert(NULL != rp);
#endif
	if ((NULL != rp) && (NULL != xsp) && (NULL != x)) {
		if (((uint8_t)0U != xsp->h[1]) && ((uint8_t)0U != xsp->h[2])) {
			back = dk4xsp_i_calculate(rp, xsp, x, d, erp);
		}
		else {
			dk4error_set_simple_error_code(erp, DK4_E_INVALID_ARGUMENTS);
		}
	}
	else {
		dk4error_set_simple_error_code(erp, DK4_E_INVALID_ARGUMENTS);
	}
	

#line 1041 "dk4xsp.ctr"
	return back;
}



void
dk4xsp2d_reset(
	dk4_xsp_2d_t	*sp
)
{
	size_t	i;
	

#line 1053 "dk4xsp.ctr"
#if	DK4_USE_ASSERT
	assert(NULL != sp);
#endif
	if (NULL != sp) {
		DK4_MEMRES(sp,sizeof(dk4_xsp_2d_t));
		dk4xsp_reset(&(sp->xsp));
		for (i = 0; i < 4; i++) { sp->x[i] = sp->y[i] = 0.0; }
	}
	

#line 1062 "dk4xsp.ctr"
}



void
dk4xsp2d_set_xfig(
	dk4_xsp_2d_t	*sp,
	uint8_t			 val
)
{
	

#line 1073 "dk4xsp.ctr"
#if	DK4_USE_ASSERT
	assert(NULL != sp);
#endif
	if (NULL != sp) {
		dk4xsp_set_xfig(&(sp->xsp), val);
	}
	

#line 1080 "dk4xsp.ctr"
}



void
dk4xsp2d_reset_points(
	dk4_xsp_2d_t	*sp
)
{
	size_t	i;
	

#line 1091 "dk4xsp.ctr"
#if	DK4_USE_ASSERT
	assert(NULL != sp);
#endif
	if (NULL != sp) {
		dk4xsp_reset_data(&(sp->xsp));
		for (i = 0; i < 4; i++) { sp->x[i] = sp->y[i] = 0.0; }
	}
	

#line 1099 "dk4xsp.ctr"
}



void
dk4xsp2d_set_point(
	dk4_xsp_2d_t	*sp,
	double			 x,
	double			 y,
	double			 s,
	size_t			 i
)
{
	

#line 1113 "dk4xsp.ctr"
#if	DK4_USE_ASSERT
	assert(NULL != sp);
#endif
	if ((NULL != sp) && (4 > i)) {
		sp->x[i] = x;
		sp->y[i] = y;
		dk4xsp_set_point(&(sp->xsp), s, i);
	}
	

#line 1122 "dk4xsp.ctr"
}



int
dk4xsp2d_calculate_value(
	double			*rp,
	dk4_xsp_2d_t	*sp,
	double			 t,
	dk4_er_t		*erp
)
{
	int		 res;
	int		 back	= 0;
	

#line 1137 "dk4xsp.ctr"
#if	DK4_USE_ASSERT
	assert(NULL != sp);
	assert(NULL != rp);
	assert(0.0 <= t);
	assert(1.0 >= t);
#endif
	if ((NULL != rp) && (NULL != sp) && (0.0 <= t) && (1.0 >= t)) {
		if (((uint8_t)0U != sp->xsp.h[1]) && ((uint8_t)0U != sp->xsp.h[2])) {
			back = 1;
			dk4xsp_i_prepare(&(sp->xsp), t, 0);
			res = dk4xsp_i_calculate(&(rp[0]), &(sp->xsp), &(sp->x[0]), 0, erp);
			if (0 == res) {	

#line 1149 "dk4xsp.ctr"
				back = 0;
			}
			res = dk4xsp_i_calculate(&(rp[1]), &(sp->xsp), &(sp->y[0]), 0, erp);
			if (0 == res) {	

#line 1153 "dk4xsp.ctr"
				back = 0;
			}
			

#line 1156 "dk4xsp.ctr"
			

#line 1157 "dk4xsp.ctr"
		}
		else {				

#line 1159 "dk4xsp.ctr"
			dk4error_set_simple_error_code(erp, DK4_E_INVALID_ARGUMENTS);
		}
	}
	else {					

#line 1163 "dk4xsp.ctr"
		dk4error_set_simple_error_code(erp, DK4_E_INVALID_ARGUMENTS);
	}
	

#line 1166 "dk4xsp.ctr"
	return back;
}



int
dk4xsp2d_calculate_value_derivative(
	double			*rp,
	dk4_xsp_2d_t	*sp,
	double			 t,
	dk4_er_t		*erp
)
{
	int		 res;
	int		 back	 = 0;
	

#line 1182 "dk4xsp.ctr"
#if	DK4_USE_ASSERT
	assert(NULL != sp);
	assert(NULL != rp);
	assert(0.0 <= t);
	assert(1.0 >= t);
#endif
	if ((NULL != rp) && (NULL != sp) && (0.0 <= t) && (1.0 >= t)) {
		if (((uint8_t)0U != sp->xsp.h[1]) && ((uint8_t)0U != sp->xsp.h[2])) {
			back = 1;
			dk4xsp_i_prepare(&(sp->xsp), t, 1);
			res = dk4xsp_i_calculate(&(rp[0]), &(sp->xsp), &(sp->x[0]), 1, erp);
			if (0 == res) {	

#line 1194 "dk4xsp.ctr"
				back = 0;
			}
			res = dk4xsp_i_calculate(&(rp[2]), &(sp->xsp), &(sp->y[0]), 1, erp);
			if (0 == res) {	

#line 1198 "dk4xsp.ctr"
				back = 0;
			}
			

#line 1201 "dk4xsp.ctr"
			

#line 1202 "dk4xsp.ctr"
			

#line 1203 "dk4xsp.ctr"
			

#line 1204 "dk4xsp.ctr"
		}
		else {			

#line 1206 "dk4xsp.ctr"
			dk4error_set_simple_error_code(erp, DK4_E_INVALID_ARGUMENTS);
		}
	}
	else {				

#line 1210 "dk4xsp.ctr"
		dk4error_set_simple_error_code(erp, DK4_E_INVALID_ARGUMENTS);
	}
	

#line 1213 "dk4xsp.ctr"
	return back;
}



int
dk4xsp2d_is_line(dk4_xsp_2d_t const *sp)
{
	int		back	= 0;
	

#line 1223 "dk4xsp.ctr"
#if	DK4_USE_ASSERT
	assert(NULL != sp);
#endif
	if (NULL != sp) {
		if (1.0e-8 > fabs(sp->xsp.s[1])) {
			if (1.0e-8 > fabs(sp->xsp.s[2])) {
				back = 1;
			}
		}
	}
#if	TRACE_DEBUG
	else {		

#line 1235 "dk4xsp.ctr"
	}
#endif
	

#line 1238 "dk4xsp.ctr"
	return back;
}



int
dk4xsp2d_calculate_length(
	double			*rp,
	dk4_xsp_2d_t	*sp,
	double			 eps,
	dk4_er_t		*erp
)
{
	double		 v0[2];			/* Previous point */
	double		 v1[2];			/* Current point */
	double		 oldres;		/* Length result from previous pass */
	double		 res;			/* Length result from current pass */
	double		 dx;			/* X difference */
	double		 dy;			/* Y difference */
	double		 t;				/* t value at end of current sub segment */
	size_t		 nsegs;			/* Number of sub segments in current pass */
	size_t		 i;				/* Current sub segment */
	unsigned	 passno;		/* Pass number */
	int			 back	= 0;	/* Function result */
	

#line 1263 "dk4xsp.ctr"
#if	DK4_USE_ASSERT
	assert(NULL != sp);
	assert(NULL != rp);
	assert(0.0 < eps);
#endif
	if ((NULL != rp) && (NULL != sp) && (0.0 < eps)) {
		if (0 != dk4xsp2d_is_line(sp)) {		

#line 1270 "dk4xsp.ctr"
			dx = sp->x[2] - sp->x[1];
			dy = sp->y[2] - sp->y[1];
			res = sqrt(dx * dx + dy * dy);
			if (0 != dk4ma_is_finite(res)) {
				*rp = res;
				back = 1;
			}
			else {								

#line 1278 "dk4xsp.ctr"
				dk4error_set_simple_error_code(erp, DK4_E_MATH_OVERFLOW);
			}
		}
		else {									

#line 1282 "dk4xsp.ctr"
			nsegs = (size_t)256U;
			oldres = -1.0;
			for (passno = 0U; ((0 == back) && (passno < 16U)); passno++) {
				res = 0.0;
				if (0 != dk4xsp2d_calculate_value(v0, sp, 0.0, erp)) {
					for (i = (size_t)0U; i < nsegs; i++) {
						t = ((double)(i + (size_t)1U)) / ((double)nsegs);
						if (0 != dk4xsp2d_calculate_value(v1, sp, t, erp)) {
							dx = v1[0] - v0[0];
							dy = v1[1] - v0[1];
							res += sqrt(dx * dx + dy * dy);
						}
						else {			

#line 1295 "dk4xsp.ctr"
							back = -1;
							dk4error_set_simple_error_code(
								erp, DK4_E_MATH_OVERFLOW
							);
						}
						DK4_MEMCPY(v0,v1,sizeof(v0));
					}					

#line 1302 "dk4xsp.ctr"
					if (0 != dk4ma_is_finite(res)) {
#if	TRACE_DEBUG
						if (0U != passno) {
							

#line 1306 "dk4xsp.ctr"
						}
#endif
						if ((0U != passno) && (eps > fabs(res - oldres))) {
							back = 1;	

#line 1310 "dk4xsp.ctr"
							*rp = res;
						}
						else {
							oldres = res;
							nsegs *= (size_t)2U;
						}
					}
					else {				

#line 1318 "dk4xsp.ctr"
						back = -1;
						dk4error_set_simple_error_code(erp,DK4_E_MATH_OVERFLOW);
					}
				}
				else {					

#line 1323 "dk4xsp.ctr"
					back = -1;
					dk4error_set_simple_error_code(erp, DK4_E_MATH_OVERFLOW);
				}
			}
			if (0 == back) {			

#line 1328 "dk4xsp.ctr"
				dk4error_set_simple_error_code(erp, DK4_E_NOT_FOUND);
			}
		}
	}
	else {				

#line 1333 "dk4xsp.ctr"
		dk4error_set_simple_error_code(erp, DK4_E_INVALID_ARGUMENTS);
	}
	if (-1 == back) { back = 0; }
	

#line 1337 "dk4xsp.ctr"
	return back;
}



int
dk4xsp2d_calculate_partial_length(
	double			*rp,
	dk4_xsp_2d_t	*sp,
	double			 tend,
	double			 eps,
	dk4_er_t		*erp
)
{
	double		 v0[2];			/* Previous point */
	double		 v1[2];			/* Current point */
	double		 oldres;		/* Length result from previous pass */
	double		 res;			/* Length result from current pass */
	double		 dx;			/* X difference */
	double		 dy;			/* Y difference */
	double		 t;				/* t value at end of current sub segment */
	size_t		 nsegs;			/* Number of sub segments in current pass */
	size_t		 i;				/* Current sub segment */
	unsigned	 passno;		/* Pass number */
	int			 back	= 0;	/* Function result */
	

#line 1363 "dk4xsp.ctr"
#if	DK4_USE_ASSERT
	assert(NULL != sp);
	assert(NULL != rp);
	assert(0.0 < eps);
	assert(0.0 <= tend);
	assert(1.0 >= tend);
#endif
	if (
		(NULL != rp) && (NULL != sp) && (0.0 < eps)
		&& (0.0 <= tend) && (1.0 >= tend)
	) {
		if (0.0 >= tend) {
			*rp = 0.0;
			back = 1;
		}
		else {
			if (1.0 <= tend) {
				back = dk4xsp2d_calculate_length(rp, sp, eps, erp);
			}
			else {
				if (0 != dk4xsp2d_is_line(sp)) {		

#line 1384 "dk4xsp.ctr"
					if (0 != dk4xsp2d_calculate_value(v0, sp, tend, erp)) {
						dx = v0[0] - sp->x[1];
						dy = v0[1] - sp->y[1];
						res = sqrt(dx * dx + dy * dy);
						if (0 != dk4ma_is_finite(res)) {
							*rp = res;
							back = 1;
						}
						else {
							dk4error_set_simple_error_code(
								erp, DK4_E_MATH_OVERFLOW
							);
						}
					}
					else {
						back = -1;
						dk4error_set_simple_error_code(
							erp, DK4_E_MATH_OVERFLOW
						);
					}
				}
				else {									

#line 1406 "dk4xsp.ctr"
					nsegs = (size_t)256U;
					oldres = -1.0;
					for (passno = 0U;((0 == back) && (passno < 16U));passno++) {
						res = 0.0;
						if (0 != dk4xsp2d_calculate_value(v0, sp, 0.0, erp)) {
							for (i = (size_t)0U; i < nsegs; i++) {
								t = (tend * ((double)(i + (size_t)1U)))
								  / ((double)nsegs);
								if (
									0 != dk4xsp2d_calculate_value(v1,sp,t,erp)
								) {
									dx = v1[0] - v0[0];
									dy = v1[1] - v0[1];
									res += sqrt(dx * dx + dy * dy);
								}
								else {			

#line 1422 "dk4xsp.ctr"
									back = -1;
									dk4error_set_simple_error_code(
										erp, DK4_E_MATH_OVERFLOW
									);
								}
								DK4_MEMCPY(v0,v1,sizeof(v0));
							}			

#line 1429 "dk4xsp.ctr"
							if (0 != dk4ma_is_finite(res)) {
#if	TRACE_DEBUG
								if (0U != passno) {
							

#line 1433 "dk4xsp.ctr"
								}
#endif
								if (
									(0U != passno) && (eps > fabs(res - oldres))
								) {
									back = 1;	

#line 1439 "dk4xsp.ctr"
									*rp = res;
								}
								else {
									oldres = res;
									nsegs *= (size_t)2U;
								}
							}
							else {				

#line 1447 "dk4xsp.ctr"
								back = -1;
								dk4error_set_simple_error_code(
									erp, DK4_E_MATH_OVERFLOW
								);
							}
						}
						else {					

#line 1454 "dk4xsp.ctr"
							back = -1;
							dk4error_set_simple_error_code(
								erp, DK4_E_MATH_OVERFLOW
							);
						}
					}
					if (0 == back) {			

#line 1461 "dk4xsp.ctr"
						dk4error_set_simple_error_code(erp, DK4_E_NOT_FOUND);
					}
				}
			}
		}
	}
	else {						

#line 1468 "dk4xsp.ctr"
		dk4error_set_simple_error_code(erp, DK4_E_INVALID_ARGUMENTS);
	}
	if (-1 == back) { back = 0; }
	

#line 1472 "dk4xsp.ctr"
	return back;
}



/* vim: set ai sw=4 ts=4 : */