summaryrefslogtreecommitdiff
path: root/support/dktools/dk3xsp.c
blob: 8d731aeb32e355730635ababcb64a0299ea2f1c8 (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
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
/*
	WARNING: This file was generated by dkct.
	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: dk3xsp.ctr
*/

/*
Copyright (C) 2012-2017, Dirk Krause

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:

* Redistributions of source code must retain the above copyright notice,
  this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above opyright notice,
  this list of conditions and the following disclaimer in the documentation
  and/or other materials provided with the distribution.
* Neither the name of the author nor the names of contributors may be used
  to endorse or promote products derived from this software without specific
  prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS ``AS IS''
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
ARE DISCLAIMED.
IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY
DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/

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


#line 260 "dk3xsp.ctr"

#include "dk3all.h"
#include "dk3fig.h"
#include "dk3xsp.h"



#line 266 "dk3xsp.ctr"



/* ************************************************************************ */
/* *                                                                      * */
/* * Basic X-spline functions.                                            * */
/* *                                                                      * */
/* ************************************************************************ */


#line 272 "dk3xsp.ctr"





#line 276 "dk3xsp.ctr"



/**	Initialize spline point (set all components to 0.0).
	@param	ppt	Point to initialize.
*/
static
void
dk3xsp_init_spline_point(dk3_fig_spline_point_t *ppt)
{
  ppt->x = 0.0; ppt->y = 0.0; ppt->s = 0.0;
}



/**	Copy spline point or initialize a destination point.
	@param	pd	Destination pointer.
	@param	ps	Source pointer, may be NULL.
*/
static
void
dk3xsp_copy_spline_point(
  dk3_fig_spline_point_t *pd, dk3_fig_spline_point_t const *ps
)
{
  if(pd) {
    if(ps) {
      pd->x = ps->x; pd->y = ps->y; pd->s = ps->s;
    } else {
      dk3xsp_init_spline_point(pd);
    }
  }
}



void
dk3xsp_reset(dk3_xspline_segment_t *pseg)
{
  if(pseg) {
    dk3mem_res((void *)pseg, sizeof(dk3_xspline_segment_t));
    dk3xsp_init_spline_point(&(pseg->a));
    dk3xsp_init_spline_point(&(pseg->b));
    dk3xsp_init_spline_point(&(pseg->c));
    dk3xsp_init_spline_point(&(pseg->d));
    pseg->pa = pseg->pb = pseg->pc = pseg->pd = 2.0;
    pseg->qa = pseg->qb = pseg->qc = pseg->qd = 0.0;
    pseg->x = pseg->y = pseg->dxdt = pseg->dydt = 0.0;
    pseg->duadt = pseg->dubdt = pseg->ducdt = pseg->duddt = 0.0;
    pseg->cb = 0;
  }
}



void
dk3xsp_set_cb(dk3_xspline_segment_t *pseg, int v)
{
  if(pseg) {
    pseg->cb = ((v) ? 1 : 0);
  }
}



/**	The f(u) function.
	@param	u	The u argument.
	@param	p	The p parameter.
	@return	Function result.
*/
static
double
dk3xsp_f(double u, double p)
{
  double back;
  back = u * u * u *(10.0 - p + u * (2.0 * p - 15.0 + u * (6.0 - p)));
  

#line 353 "dk3xsp.ctr"
  return back;
}



/**	The f(u) function derived.
	@param	u	The u argument.
	@param	p	The p parameter.
	@return	Function result.
*/
static
double
dk3xsp_dfdu(double u, double p)
{
  double back;
  back = u * u * (30.0 - 3.0*p + u * (8.0*p - 60.0 + u * (30.0 - 5.0*p)));
  

#line 370 "dk3xsp.ctr"
  return back;
}



/**	The g(u) function.
	@param	u	Argument u.
	@param	p	Parameter p.
	@param	q	Parameter q.
	@return	Function result.
*/
static
double
dk3xsp_g(double u, double p, double q)
{
  double	back;
  back = u*(q+u*(2.0*q+u*(10.0-12.0*q-p+u*(2.0*p+14.0*q-15+u*(6.0-5.0*q-p)))));
  

#line 388 "dk3xsp.ctr"
  return back;
}



/**	The g(u) function derived.
	@param	u	Argument u.
	@param	p	Parameter p.
	@param	q	Parameter q.
	@return	Function result.
*/
static
double
dk3xsp_dgdu(double u, double p, double q)
{
  double back;
  back =
  q+u*(4.0*q+u*(30.0-3.0*p-36.0*q+u*(56.0*q+8.0*p-60.0+u*(30.0-5.0*p-25.0*q))));
  

#line 407 "dk3xsp.ctr"
  return back;
}



/**	The h(u) function.
	@param	u	Argument u.
	@param	q	Parameter q.
	@return	Function result.
*/
static
double
dk3xsp_h(double u, double q)
{
  double back;
  back = u*(q+u*(2.0*q-u*u*(q*(u+2.0))));
  

#line 424 "dk3xsp.ctr"
  return back;
}



/**	The h(u) function derived.
	@param	u	Argument u.
	@param	q	Parameter q.
	@return	Function result.
*/
static
double
dk3xsp_dhdu(double u, double q)
{
  double back;
  back = q+u*(4.0*q-u*u*(q*(8.0+5.0*u)));
  

#line 441 "dk3xsp.ctr"
  return back;
}



double
dk3xsp_get_x(dk3_xspline_segment_t const *pseg)
{
  double back = 0.0;
  if(pseg) {
    back = pseg->x;
  }
  return back;
}



double
dk3xsp_get_y(dk3_xspline_segment_t const *pseg)
{
  double back = 0.0;
  if(pseg) {
    back = pseg->y;
  }
  return back;
}



double
dk3xsp_get_dxdt(dk3_xspline_segment_t const *pseg)
{
  double back = 0.0;
  if(pseg) {
    back = pseg->dxdt;
  }
  return back;
}



double
dk3xsp_get_dydt(dk3_xspline_segment_t const *pseg)
{
  double back = 0.0;
  if(pseg) {
    back = pseg->dydt;
  }
  return back;
}



void
dk3xsp_set(
  dk3_xspline_segment_t		*pseg,
  dk3_fig_spline_point_t const	*a,
  dk3_fig_spline_point_t const	*b,
  dk3_fig_spline_point_t const	*c,
  dk3_fig_spline_point_t const	*d
)
{
  

#line 504 "dk3xsp.ctr"
  if(pseg) {
    pseg->ha = ((a) ? 1 : 0);
    pseg->hd = ((d) ? 1 : 0);
    dk3xsp_copy_spline_point(&(pseg->a), a);
    dk3xsp_copy_spline_point(&(pseg->b), b);
    dk3xsp_copy_spline_point(&(pseg->c), c);
    dk3xsp_copy_spline_point(&(pseg->d), d);
    pseg->pa = pseg->pb = pseg->pc = pseg->pd = 0.0;
    pseg->qa = pseg->qb = pseg->qc = pseg->qd = 0.0;
    pseg->duadt = pseg->dubdt = pseg->ducdt = pseg->duddt = 0.0;
    if((pseg->b).s < 0.0) {
      if(pseg->cb) {
        pseg->qa = pseg->qc = 0.0 - ((pseg->b).s);
      } else {
        pseg->qa = pseg->qc = -0.5 * ((pseg->b).s);
      }
      pseg->duadt = -1.0; pseg->ducdt = 1.0;
    } else {
      pseg->pa = pseg->pc = 2.0 * (1.0 + (pseg->b).s) * (1.0 + (pseg->b).s);
      pseg->ducdt = 1.0 / (1.0 + (pseg->b).s);
      pseg->duadt = -1.0 * pseg->ducdt;
    }
    if((pseg->c).s < 0.0) {
      if(pseg->cb) {
        pseg->qb = pseg->qd = 0.0 - ((pseg->c).s);
      } else {
        pseg->qb = pseg->qd = -0.5 * ((pseg->c).s);
      }
      pseg->dubdt = -1.0; pseg->duddt = 1.0;
    } else {
      pseg->pb = pseg->pd = 2.0 * (1.0 + (pseg->c).s) * (1.0 + (pseg->c).s);
      pseg->duddt = 1.0 / (1.0 + (pseg->c).s);
      pseg->dubdt = -1.0 * pseg->duddt;
    }
    

#line 539 "dk3xsp.ctr"
    

#line 540 "dk3xsp.ctr"
    

#line 541 "dk3xsp.ctr"
    

#line 542 "dk3xsp.ctr"
  } 

#line 543 "dk3xsp.ctr"
}



int
dk3xsp_calculate(
  dk3_xspline_segment_t		*pseg,
  double			 t,
  int				 fder
)
{
  double ua	= 0.0;	/* Argument u for blending function from point A. */
  double ub	= 0.0;	/* = B. */
  double uc	= 0.0;	/* = C. */
  double ud	= 0.0;	/* = D. */
  double fa	= 0.0;	/* Result from blending function from point A. */
  double fb	= 0.0;	/* = B. */
  double fc	= 0.0;	/* = C. */
  double fd	= 0.0;	/* = D. */
  double zx	= 0.0;	/* Counter in fraction to calculate x. */
  double zy	= 0.0;	/* Counter in fraction to calculate y. */
  double n	= 0.0;	/* Denominator in both fractions. */
  double dfadt	= 0.0;	/* Derivative result for point A blending function. */
  double dfbdt	= 0.0;	/* = B. */
  double dfcdt	= 0.0;	/* = C. */
  double dfddt	= 0.0;	/* = D. */
  double nder	= 0.0;	/* Derivative of fraction denominator. */
  double zxder	= 0.0;	/* Derivative of x fraction counter. */
  double zyder	= 0.0;	/* Derivative of y fraction counter. */
  double nsq	= 0.0;	/* Denominator square value. */
  int	 ec	= 0;	/* Error code for mathematical operations. */
  int	 back	= 0;
  

#line 576 "dk3xsp.ctr"
  if(pseg) {
    back = 1;
    /*
    	Initialize variables.
    */
#if VERSION_BEFORE_20150821
    ua = ub = uc = ud = fa = fb = fc = fd = zx = zy = n = 0.0;
#endif
    /*
    	Set ua, ub, uc, ud.
    */
    if((pseg->b).s < 0.0) {
      ua = -1.0 * t;
      uc = t;
    } else {
      ua = ((pseg->b).s - t) / (1.0 + (pseg->b).s);
      uc = ((pseg->b).s + t) / (1.0 + (pseg->b).s);
    }
    if((pseg->c).s < 0.0) {
      ub = 1.0 - t;
      ud = t - 1.0;
    } else {
      ub = (1.0 + (pseg->c).s - t) / (1.0 + (pseg->c).s);
      ud = (t + (pseg->c).s - 1.0) / (1.0 + (pseg->c).s);
    }
    

#line 602 "dk3xsp.ctr"
    

#line 603 "dk3xsp.ctr"
    

#line 604 "dk3xsp.ctr"
    

#line 605 "dk3xsp.ctr"
    /*
    	Calculate fa, fb, fc, fd.
    */
    if((pseg->b).s < 0.0) {
      fa = dk3xsp_h(ua, pseg->qa);
      fc = dk3xsp_g(uc, pseg->pc, pseg->qc);
    } else {
      if(t < (pseg->b).s) {
        fa = dk3xsp_f(ua, pseg->pa);
      }
      fc = dk3xsp_f(uc, pseg->pc);
    }
    if((pseg->c).s < 0.0) {
      fb = dk3xsp_g(ub, pseg->pb, pseg->qb);
      fd = dk3xsp_h(ud, pseg->qd);
    } else {
      fb = dk3xsp_f(ub, pseg->pb);
      if(t > (1.0 - (pseg->c).s)) {
        fd =  dk3xsp_f(ud, pseg->pd);
      }
    }
    

#line 627 "dk3xsp.ctr"
    

#line 628 "dk3xsp.ctr"
    

#line 629 "dk3xsp.ctr"
    

#line 630 "dk3xsp.ctr"
    /*
    	Calculate counters and denominator.
    */
    zx = dk3ma_d_add_ok(
      dk3ma_d_add_ok(
        ((pseg->ha) ? (fa * (pseg->a).x) : 0.0),
	(fb * (pseg->b).x),
	&ec
      ),
      dk3ma_d_add_ok(
        (fc * (pseg->c).x),
	((pseg->hd) ? (fd * (pseg->d).x) : 0.0),
	&ec
      ),
      &ec
    );
    zy = dk3ma_d_add_ok(
      dk3ma_d_add_ok(
        ((pseg->ha) ? (fa * (pseg->a).y) : 0.0),
	(fb * (pseg->b).y),
	&ec
      ),
      dk3ma_d_add_ok(
        (fc * (pseg->c).y),
	((pseg->hd) ? (fd * (pseg->d).y) : 0.0),
	&ec
      ),
      &ec
    );
    n  = dk3ma_d_add_ok(
      dk3ma_d_add_ok(((pseg->ha) ? fa : 0.0), fb, &ec),
      dk3ma_d_add_ok(fc, ((pseg->hd) ? fd : 0.0), &ec),
      &ec
    );
    

#line 665 "dk3xsp.ctr"
    

#line 666 "dk3xsp.ctr"
    

#line 667 "dk3xsp.ctr"
    /*
    	Calculate x and y value.
    */
    pseg->x = dk3ma_d_div_ok(zx, n, &ec);
    pseg->y = dk3ma_d_div_ok(zy, n, &ec);
    

#line 673 "dk3xsp.ctr"
    

#line 674 "dk3xsp.ctr"
    /*
    	Calculate derivatives if required.
    */
    if(fder) {
      /*
      	Initialize variables.
      */
      dfadt = dfbdt = dfcdt = dfddt = nder = zxder = zyder = 0.0;
      /*
      	Calculate dfadt, dfbdt, dfcdt, dfddt.
      */
      if((pseg->b).s < 0.0) {
        dfadt = dk3ma_d_mul_ok(dk3xsp_dhdu(ua, pseg->qa), pseg->duadt, &ec);
	dfcdt = dk3ma_d_mul_ok(
	  dk3xsp_dgdu(uc, pseg->pc, pseg->qc),
	  pseg->ducdt,
	  &ec
	);
      } else {
        if(t < (pseg->b).s) {
          dfadt = dk3ma_d_mul_ok(dk3xsp_dfdu(ua, pseg->pa), pseg->duadt, &ec);
	}
	dfcdt = dk3ma_d_mul_ok(dk3xsp_dfdu(uc, pseg->pc), pseg->ducdt, &ec);
      }
      if((pseg->c).s < 0.0) {
        dfbdt = dk3ma_d_mul_ok(
	  dk3xsp_dgdu(ub, pseg->pb, pseg->qb),
	  pseg->dubdt,
	  &ec
	);
	dfddt = dk3ma_d_mul_ok(dk3xsp_dhdu(ud, pseg->qd), pseg->duddt, &ec);
      } else {
        dfbdt = dk3ma_d_mul_ok(dk3xsp_dfdu(ub, pseg->pb), pseg->dubdt, &ec);
	if(t > (1.0 - (pseg->c).s)) {
	  dfddt = dk3ma_d_mul_ok(dk3xsp_dfdu(ud, pseg->pd), pseg->duddt, &ec);
	}
      }
      

#line 712 "dk3xsp.ctr"
      

#line 713 "dk3xsp.ctr"
      

#line 714 "dk3xsp.ctr"
      

#line 715 "dk3xsp.ctr"
      /*
      	Calculate counters and denominator.
      */
      zxder = dk3ma_d_add_ok(
        dk3ma_d_add_ok(
	  ((pseg->ha) ? dk3ma_d_mul_ok(dfadt, (pseg->a).x, &ec) : 0.0),
	  dk3ma_d_mul_ok(dfbdt, (pseg->b).x, &ec),
	  &ec
	),
	dk3ma_d_add_ok(
	  dk3ma_d_mul_ok(dfcdt, (pseg->c).x, &ec),
	  ((pseg->hd) ? dk3ma_d_mul_ok(dfddt, (pseg->d).x, &ec) : 0.0),
	  &ec
	),
	&ec
      );
      zyder = dk3ma_d_add_ok(
        dk3ma_d_add_ok(
	  ((pseg->ha) ? dk3ma_d_mul_ok(dfadt, (pseg->a).y, &ec) : 0.0),
	  dk3ma_d_mul_ok(dfbdt, (pseg->b).y, &ec),
	  &ec
	),
	dk3ma_d_add_ok(
	  dk3ma_d_mul_ok(dfcdt, (pseg->c).y, &ec),
	  ((pseg->hd) ? dk3ma_d_mul_ok(dfddt, (pseg->d).y, &ec) : 0.0),
	  &ec
	),
	&ec
      );
      nder  = dk3ma_d_add_ok(
        dk3ma_d_add_ok(((pseg->ha) ? dfadt : 0.0), dfbdt, &ec),
	dk3ma_d_add_ok(dfcdt, ((pseg->hd) ? dfddt : 0.0), &ec),
	&ec
      );
      nsq   = n * n;
      

#line 751 "dk3xsp.ctr"
      

#line 752 "dk3xsp.ctr"
      

#line 753 "dk3xsp.ctr"
      

#line 754 "dk3xsp.ctr"
      pseg->dxdt = dk3ma_d_div_ok(
        dk3ma_d_sub_ok(
	  dk3ma_d_mul_ok(n, zxder, &ec),
	  dk3ma_d_mul_ok(zx, nder, &ec),
	  &ec
	),
	nsq,
	&ec
      );
      pseg->dydt = dk3ma_d_div_ok(
        dk3ma_d_sub_ok(
	  dk3ma_d_mul_ok(n, zyder, &ec),
	  dk3ma_d_mul_ok(zy, nder, &ec),
	  &ec
	),
	nsq,
	&ec
      );
      

#line 773 "dk3xsp.ctr"
      

#line 774 "dk3xsp.ctr"
    }
    if(ec) {
      back = 0;
    }
  } 

#line 779 "dk3xsp.ctr"
  return back;
}



/* ************************************************************************ */
/* *                                                                      * */
/* * Length calculations for single segments.                             * */
/* *                                                                      * */
/* ************************************************************************ */


#line 787 "dk3xsp.ctr"



int
dk3xsp_is_null(double s)
{
  int		 back = 0;
  if(fabs(s) < 1.0e-6) {
    back = 1;
  }
  return back;
}





#line 803 "dk3xsp.ctr"



/**	Convert long to double.
	@param	l	Long value to convert.
	@return	Conversion result.
*/
static
double
dk3xsp_l_to_d(long l)
{
  return ((double)l);
}

double
dk3xsp_segment_length(
  dk3_xspline_segment_t 	*pseg,
  double			 prec,
  int				*ec
)
{
  double		 back = -1.0;
  double		 lastpassres;		/* Result of previous pass. */
  double		 passres;		/* Result of current pass. */
  double		 deltax;		/* Delta x between points. */
  double		 deltay;		/* Delta y between points. */
  double		 lastx;			/* Previous x. */
  double		 lasty;			/* Previous y. */
  double		 x = 0.0;		/* Current x. */
  double		 y = 0.0;		/* Current y. */
  double		 t;			/* Current t. */
  double		 dsegs;			/* Double value of segs. */
  long			 segs = 64L;		/* Number of segments. */
  long			 i;			/* Current segment. */
  int			 mec = 0;		/* Mathematical error code. */
  int			 mc = 1;		/* Flag: Must continue. */
  int			 cc = 1;		/* Flag: Can continue. */
  

#line 841 "dk3xsp.ctr"
  if((pseg) && (prec > 0.0)) {
    if(dk3xsp_is_null((pseg->b).s) && dk3xsp_is_null((pseg->c).s)) {
      /*
      	Simple line.
      */
      deltax = dk3ma_d_sub_ok((pseg->c).x, (pseg->b).x, &mec);
      deltay = dk3ma_d_sub_ok((pseg->c).y, (pseg->b).y, &mec);
      back = sqrt(
        dk3ma_d_add_ok(
	  dk3ma_d_mul_ok(deltax, deltax, &mec),
	  dk3ma_d_mul_ok(deltay, deltay, &mec),
	  &mec
	)
      );	

#line 855 "dk3xsp.ctr"
    } else {	

#line 856 "dk3xsp.ctr"
      /*
      	Spline calculation really necessary.
      */
      lastpassres = 0.0 - 2.0 * prec;
      if(lastpassres > -1.0) { lastpassres = -1.0; }
      while((cc) && (mc) && (!(mec))) {		

#line 862 "dk3xsp.ctr"
        passres = 0.0;
	if(dk3xsp_calculate(pseg, 0.0, 0)) {
	  lastx = dk3xsp_get_x(pseg);
	  lasty = dk3xsp_get_y(pseg);
	  dsegs = dk3xsp_l_to_d(segs);
	  for(i = 1L; i <= segs; i++) {
	    if(i == segs) {
	      t = 1.0;
	    } else {
	      t = dk3xsp_l_to_d(i) / dsegs;
	    }
	    if(dk3xsp_calculate(pseg, t, 0)) {
	      x = dk3xsp_get_x(pseg);
	      y = dk3xsp_get_y(pseg);
	    } else {
	      cc = 0;
	      mec = DK3_ERROR_MATH_OVERFLOW;
	    }
	    if(cc) {
	      deltax = dk3ma_d_sub_ok(x, lastx, &mec);
	      deltay = dk3ma_d_sub_ok(y, lasty, &mec);
	      passres = dk3ma_d_add_ok(
	        passres,
	        sqrt(
	          dk3ma_d_add_ok(
		    dk3ma_d_mul_ok(deltax, deltax, &mec),
		    dk3ma_d_mul_ok(deltay, deltay, &mec),
		    &mec
		  )
	        ),
	        &mec
	      );
	      lastx = x; lasty = y;
	    }
	  }			

#line 897 "dk3xsp.ctr"
	  if(fabs(passres - lastpassres) < prec) {	

#line 898 "dk3xsp.ctr"
	    mc = 0;		/* Finished successfully. */
	    back = passres;
	  } else {	

#line 901 "dk3xsp.ctr"
	    lastpassres = passres;
	    segs = segs * 2L;
	    if(segs > DK3XSP_MAX_ITERATION_SEGMENTS) {	

#line 904 "dk3xsp.ctr"
	      cc = 0;
	      if(ec) { *ec = DK3_ERROR_ITERATION; }
	    }
	  }
	} else {
	  cc = 0;
	  mec = DK3_ERROR_MATH_OVERFLOW;
	}
      }
    }
    if(mec) { back = -1.0; if(ec) { *ec = mec; } }
  } else {
    if(ec) { *ec = DK3_ERROR_INVALID_ARGS; }
  } 

#line 918 "dk3xsp.ctr"
  return back;
}



double
dk3xsp_segment_partial_length(
  dk3_xspline_segment_t		*pseg,
  double			 tstart,
  int				 dir,
  double			 prec,
  int				*ec
)
{
  double		 back = -1.0;
  double		 lastpassres;		/* Result of previous pass. */
  double		 passres;		/* Result of current pass. */
  double		 deltax;		/* Delta x between points. */
  double		 deltay;		/* Delta y between points. */
  double		 lastx;			/* Previous x. */
  double		 lasty;			/* Previous y. */
  double		 x = 0.0;		/* Current x. */
  double		 y = 0.0;		/* Current y. */
  double		 t;			/* Current t. */
  double		 dsegs;			/* Double value of segs. */
  double		 tdiff;			/* 1.0 - tstart. */
  long			 segs = 64L;		/* Number of segments. */
  long			 i;			/* Current segment. */
  int			 mec = 0;		/* Mathematical error code. */
  int			 mc = 1;		/* Flag: Must continue. */
  int			 cc = 1;		/* Flag: Can continue. */
  

#line 950 "dk3xsp.ctr"
  if((pseg) && (prec > 0.0) && (tstart >= 0.0) && (tstart<= 1.0)) { 

#line 951 "dk3xsp.ctr"
    if(dk3xsp_is_null((pseg->b).s) && dk3xsp_is_null((pseg->c).s)) {
      /*
      	Simple line.
      */
      

#line 956 "dk3xsp.ctr"
      if(dk3xsp_calculate(pseg, tstart, 0)) {
        x = dk3xsp_get_x(pseg);
	y = dk3xsp_get_y(pseg);
      } else {
#if VERSION_BEFORE_20150821
        cc = 0;
#endif
	mec = DK3_ERROR_MATH_OVERFLOW;
      }
      if(dir) {
        deltax = dk3ma_d_sub_ok((pseg->b).x, x, &mec);
	deltay = dk3ma_d_sub_ok((pseg->b).y, y, &mec);
      } else {
        deltax = dk3ma_d_sub_ok((pseg->c).x, x, &mec);
	deltay = dk3ma_d_sub_ok((pseg->c).y, y, &mec);
      }
      back = sqrt(
        dk3ma_d_add_ok(
	  dk3ma_d_mul_ok(deltax, deltax, &mec),
	  dk3ma_d_mul_ok(deltay, deltay, &mec),
	  &mec
	)
      );
    } else {
      /*
      	Spline calculations really necessary.
      */
      

#line 984 "dk3xsp.ctr"
      if(dir) {			

#line 985 "dk3xsp.ctr"
        lastpassres = 0.0 - 2.0 * prec;
	if(lastpassres > -1.0) { lastpassres = -1.0; }
	while((cc) && (mc) && (!(mec))) {
	  passres = 0.0;
	  dsegs = dk3xsp_l_to_d(segs);
	  if(dk3xsp_calculate(pseg, 0.0, 0)) {
	    lastx = dk3xsp_get_x(pseg);
	    lasty = dk3xsp_get_y(pseg);
	    for(i = 1L; i <= segs; i++) {
	      if(i == segs) {
	        t = tstart;
	      } else {
	        t = (tstart * dk3xsp_l_to_d(i)) / dsegs;
	      } 

#line 999 "dk3xsp.ctr"
	      if(dk3xsp_calculate(pseg, t, 0)) {
	        

#line 1001 "dk3xsp.ctr"
	        

#line 1002 "dk3xsp.ctr"
	        x = dk3xsp_get_x(pseg); 

#line 1003 "dk3xsp.ctr"
	        y = dk3xsp_get_y(pseg); 

#line 1004 "dk3xsp.ctr"
	        deltax = dk3ma_d_sub_ok(x, lastx, &mec); 

#line 1005 "dk3xsp.ctr"
	        deltay = dk3ma_d_sub_ok(y, lasty, &mec); 

#line 1006 "dk3xsp.ctr"
	        

#line 1007 "dk3xsp.ctr"
	        passres = dk3ma_d_add_ok(
	          passres,
		  sqrt(
		    dk3ma_d_add_ok(
		      dk3ma_d_mul_ok(deltax, deltax, &mec),
		      dk3ma_d_mul_ok(deltay, deltay, &mec),
		      &mec
		    )
		  ),
		  &mec
	        ); 

#line 1018 "dk3xsp.ctr"
	        lastx = x; lasty = y;
	      } else {
	        cc = 0;
	        mec = DK3_ERROR_MATH_OVERFLOW;
	      }
	    }
	    if(fabs(passres - lastpassres) < prec) {
	      mc = 0;
	      back = passres;
	    } else {
	      lastpassres = passres;
	      segs = segs * 2L;
	      if(segs > DK3XSP_MAX_ITERATION_SEGMENTS) {
	        cc = 0;
	        if(ec) { *ec = DK3_ERROR_ITERATION; }
	      }
	    }
	  } else {
	    cc = 0;
	    mec = DK3_ERROR_MATH_OVERFLOW;
	  }
	}
      } else {			

#line 1041 "dk3xsp.ctr"
        tdiff = 1.0 - tstart;
	lastpassres = 0.0 - 2.0 * prec;
	if(lastpassres > -1.0) { lastpassres = -1.0; }
	while((cc) && (mc) && (!(mec))) {
	  passres = 0.0;
	  dsegs = dk3xsp_l_to_d(segs);
	  if(dk3xsp_calculate(pseg, tstart, 0)) {
	    lastx = dk3xsp_get_x(pseg);
	    lasty = dk3xsp_get_y(pseg);
	    for(i = 1L; i <= segs; i++) {
	      if(i == segs) {	

#line 1052 "dk3xsp.ctr"
		t = 1.0;
	      } else {	
	        t = tstart + ((tdiff * dk3xsp_l_to_d(i)) / dsegs);
	      }
	      

#line 1057 "dk3xsp.ctr"
	      if(dk3xsp_calculate(pseg, t, 0)) {
		x = dk3xsp_get_x(pseg);
		y = dk3xsp_get_y(pseg);
	      } else {		

#line 1061 "dk3xsp.ctr"
		cc = 0;
		mec = DK3_ERROR_MATH_OVERFLOW;
	      }
	      if(cc) {
	        

#line 1066 "dk3xsp.ctr"
		

#line 1067 "dk3xsp.ctr"
		

#line 1068 "dk3xsp.ctr"
		

#line 1069 "dk3xsp.ctr"
	        deltax = dk3ma_d_sub_ok(x, lastx, &mec);
		deltay = dk3ma_d_sub_ok(y, lasty, &mec);
		

#line 1072 "dk3xsp.ctr"
		

#line 1073 "dk3xsp.ctr"
		

#line 1074 "dk3xsp.ctr"
		passres = dk3ma_d_add_ok(
		  passres,
		  sqrt(
		    dk3ma_d_add_ok(
		      dk3ma_d_mul_ok(deltax, deltax, &mec),
		      dk3ma_d_mul_ok(deltay, deltay, &mec),
		      &mec
		    )
		  ),
		  &mec
		); 

#line 1085 "dk3xsp.ctr"
		lastx = x; lasty = y;
	      }
	    }
	  } else {
	    cc = 0;
	    mec = DK3_ERROR_MATH_OVERFLOW;
	  }
	  if(fabs(passres - lastpassres) < prec) {
	    mc = 0;
	    back = passres;
	  } else {
	    lastpassres = passres;
	    segs = segs * 2L;
	    if(segs > DK3XSP_MAX_ITERATION_SEGMENTS) {
	      cc = 0;
	      if(ec) { *ec = DK3_ERROR_ITERATION; }
	    }
	  }
	}
      }
    }
    if(mec) { back = -1.0; if(ec) { *ec = mec; } }
  } else {
    if(ec) { *ec = DK3_ERROR_INVALID_ARGS; }
  }
  

#line 1111 "dk3xsp.ctr"
  return back;
}



/* ************************************************************************ */
/* *                                                                      * */
/* * Length calculations for entire splines.                              * */
/* *                                                                      * */
/* ************************************************************************ */


#line 1119 "dk3xsp.ctr"



double
dk3xsp_open_spline_length(
  dk3_fig_spline_point_t const	*points,
  size_t			 np,
  double			 prec,
  double			*fslc,
  int				 cb,
  int				*ec
)
{
  dk3_xspline_segment_t		 seg;
  double			 val;
  double			 back = 0.0;
  size_t			 i;
  int				 mec = 0;
  

#line 1138 "dk3xsp.ctr"
  if((points) && (1 < np) && (prec > 0.0)) {
    dk3xsp_reset(&seg);
    if(cb) { dk3xsp_set_cb(&seg, 1); }
    for(i = 0; ((i < (np - 1)) && (back > -1.0)); i++) {
      

#line 1143 "dk3xsp.ctr"
      val = -1.0;
      if(fslc) {
        val = fslc[i];
      }
      if(val < -0.5) {
        dk3xsp_set(
          &seg,
	  ((i > 1) ? (&(points[i-1])) : NULL),
	  &(points[i]),
	  &(points[i+1]),
	  ((i < (np - 2)) ? &(points[i+2]) : NULL)
        );
        val = dk3xsp_segment_length(&seg, (prec / (double)(np - 1)), ec);
	if(val >= 0.0) {
	  if(fslc) {
	    fslc[i] = val;
	  }
	}
      }
      if(val < 0.0) {
          back = -1.0; i = np - 1;
      } else {
        back = dk3ma_d_add_ok(back, val, &mec);
	if(mec) {
	  back = -1.0; i = np - 1;
	  if(ec) { *ec = mec; }
	}
      }
    }
  } else {
    if(ec) { *ec = DK3_ERROR_INVALID_ARGS; }
  } 

#line 1175 "dk3xsp.ctr"
  return back;
}



/**	Find Index of segment start point.
	@param	tstart	Parameter t.
	@param	ec	Pointer to error code variable, required.
	@return	Start segment index.
*/
static
size_t
dk3xsp_find_segment_start(double tstart, int *ec)
{
#if VERSION_BEFORE_20140809
  unsigned long		ul;
  size_t		back = 0;
  int			mec = 0;
  

#line 1194 "dk3xsp.ctr"
  ul = dk3ma_d_to_ul_ok(floor(tstart), &mec);
  if(mec) {
    *ec = mec;
  } else {
    back = (size_t)ul;
    if((unsigned long)back != ul) {
      *ec = DK3_ERROR_MATH_OVERFLOW;
    }
  } 

#line 1203 "dk3xsp.ctr"
  return back;
#else
  size_t		back	= 0;
  int			mec	= 0;
  

#line 1208 "dk3xsp.ctr"
  back = dk3ma_d_to_sz_ok(floor(tstart), &mec);
  if (mec) { if (ec) { *ec = mec; } }
  

#line 1211 "dk3xsp.ctr"
  return back;
#endif
}



int
dk3xsp_calculate_position(
  dk3_xspline_segment_t		*pseg,
  dk3_fig_spline_point_t const	*points,
  size_t			 np,
  double			 t,
  int				 cb,
  int				 der,
  int				*ec
)
{
  size_t	 i = 0;
  int		 back = 0;
  int		 mec = 0;
  if((pseg) && (points) && (np > 1) && ((t >= 0.0) && (t <= (double)(np - 1)))) 
  {
    i = dk3xsp_find_segment_start(t, &mec);
    if(!(mec)) {
      dk3xsp_reset(pseg);
      dk3xsp_set_cb(pseg, cb);
      if(i <= (np - 2)) {
        /* Not final point */
        dk3xsp_set(
	  pseg,
	  ((i > 0) ? &(points[i - 1]) : NULL),
	  &(points[i]),
	  &(points[i+1]),
	  ((i < (np - 2)) ? &(points[i + 2]) : NULL)
	);
	back = dk3xsp_calculate(pseg, (t - (double)i), der);
      } else {
        /* In final point */
	dk3xsp_set(
	  pseg,
	  ((np > 2) ? &(points[np - 3]): NULL),
	  &(points[np - 2]),
	  &(points[np - 1]),
	  NULL
	);
	back = dk3xsp_calculate(pseg, 1.0, der);
      }
    } else {
      if(ec) { *ec = mec; }
    }
  }
  else
  {
    if(ec) { *ec = DK3_ERROR_INVALID_ARGS;}
  }
  return back;
}



double
dk3xsp_open_spline_partial_length(
  dk3_fig_spline_point_t const	*points,
  size_t			 np,
  double			 prec,
  double			 tstart,
  double			*fslc,
  int				 dir,
  int				 cb,
  int				*ec
)
{
  dk3_xspline_segment_t		 seg;
  double			 back = -1.0;
  double			 val;
  size_t			 startseg;
  size_t			 i;
  int				 mec = 0;
  

#line 1290 "dk3xsp.ctr"
  if((points) && (1 < np) && (prec > 0.0)) {
    if((tstart >= 0.0) && (tstart <= ((double)(np - 1)))) {
      dk3xsp_reset(&seg);
      if(cb) { dk3xsp_set_cb(&seg, 1); }
      startseg = dk3xsp_find_segment_start(tstart, &mec);
      if(mec) {				

#line 1296 "dk3xsp.ctr"
        if(ec) { *ec = mec; }
      } else {				

#line 1298 "dk3xsp.ctr"
        if(startseg == (np - 1)) {	

#line 1299 "dk3xsp.ctr"
	  /*
	  	tstart points to the final point.
		So we have 0 for forward length or the full
		spline length for backward length.
	  */
	  if(dir) {	

#line 1305 "dk3xsp.ctr"
	    back = dk3xsp_open_spline_length(points, np, prec, fslc, cb, ec);
	  } else {	

#line 1307 "dk3xsp.ctr"
	    back = 0.0;
	  }
	} else {			

#line 1310 "dk3xsp.ctr"
	  if(tstart > 0.0) {		

#line 1311 "dk3xsp.ctr"
	    /*
	    	tstart points to somewhere in the spline.
		We have to calculate one segment partially, all other
		segments completely.
	    */
	    dk3xsp_set(
	      &seg,
	      ((startseg > 0) ? &(points[startseg-1]) : NULL),
	      &(points[startseg]),
	      &(points[startseg+1]),
	      ((startseg < (np - 2)) ? &(points[startseg+2]) : NULL)
	    );
	    val = dk3xsp_segment_partial_length(
	      &seg,(tstart - (double)startseg),dir,(prec / (double)(np - 1)),ec
	    );
	    if(val >= 0.0) {	

#line 1327 "dk3xsp.ctr"
	      back = val;
	      /*
	      	Partial segment calculated successfully.
		Now calculate other segments.
	      */
	      if(dir) {		

#line 1333 "dk3xsp.ctr"
	        /*
			Calculate all segments before the partial one.
		*/
	        for(i = 0; ((i < startseg) && (back > -1.0)); i++) {
		  val = -1.0;	

#line 1338 "dk3xsp.ctr"
		  if(fslc) {
		    val = fslc[i];
		  }
		  if(val < -0.5) {
		    dk3xsp_set(
		      &seg,
		      ((i > 0) ? &(points[i-1]) : NULL),
		      &(points[i]),
		      &(points[i+1]),
		      ((i < (np - 2)) ? &(points[i+2]) : NULL)
		    );
		    val = dk3xsp_segment_length(
		      &seg, (prec / (double)(np - 1)), ec
		    );
		    if(val >= 0.0) {
		      if(fslc) {
		        fslc[i] = val;
		      }
		    }
		  }
		  if(val >= 0.0) { 

#line 1359 "dk3xsp.ctr"
		    back = dk3ma_d_add_ok(back, val, &mec);
		    if(mec) {
		      back = -1.0; i = np;
		      if(ec) { *ec = mec; }
		    }
		  } else {
		    back = -1.0; i = np;
		  }
		}
	      } else {		

#line 1369 "dk3xsp.ctr"
	        /*
			Calculate all segments after the partial one.
		*/
	        for(i = (startseg + 1); ((i < (np - 1)) && (back > -1)); i++) {
		  val = -1.0;	

#line 1374 "dk3xsp.ctr"
		  if(fslc) {
		    val = fslc[i];
		  }
		  if(val < -0.5) {
		    dk3xsp_set(
		      &seg,
		      ((i > 0) ? &(points[i-1]) : NULL),
		      &(points[i]),
		      &(points[i+1]),
		      ((i < (np - 2)) ? &(points[i+2]) : NULL)
		    );
		    val = dk3xsp_segment_length(
		      &seg, (prec / (double)(np - 1)), ec
		    );
		    if(val >= 0.0) {
		      if(fslc) {
		        fslc[i] = val;
		      }
		    }
		  }
		  if(val >= 0.0) { 

#line 1395 "dk3xsp.ctr"
		    back = dk3ma_d_add_ok(back, val, &mec);
		    if(mec) {
		      back = -1.0; i = np;
		      if(ec) { *ec = mec; }
		    }
		  } else {			

#line 1401 "dk3xsp.ctr"
		    back = -1.0; i = np;
		  }
		}
	      }	

#line 1405 "dk3xsp.ctr"
	    } else {				

#line 1406 "dk3xsp.ctr"
	    }
	  } else {				

#line 1408 "dk3xsp.ctr"
	    /*
	    	tstart points to the start point.
		So we have the full spline length for forward length,
		0 for backward length.
	    */
	    if(0 == dir) {	

#line 1414 "dk3xsp.ctr"
	      back = dk3xsp_open_spline_length(points, np, prec, fslc, cb, ec);
	    } else {		

#line 1416 "dk3xsp.ctr"
	      back = 0.0;
	    }
	  }
	}
      }
    } else {
      if(ec) { *ec = DK3_ERROR_INVALID_ARGS; }
    }
  } else {
    if(ec) { *ec = DK3_ERROR_INVALID_ARGS; }
  } 

#line 1427 "dk3xsp.ctr"
  return back;
}



double
dk3xsp_get_t_for_distance(
  dk3_fig_spline_point_t const	*points,
  size_t			 np,
  double			 dist,
  double			 prec,
  double			*fslc,
  int				 dir,
  int				 cb,
  int				*ec
)
{
  double		 back = -1.0;
  double		 xl;		/* Left x. */
  double		 xr;		/* Right x. */
  double		 xc;		/* Center x. */
  double		 yl;		/* Left y. */
  double		 yr;		/* Right y. */
  double		 yc;		/* Center y. */
  double		 l;		/* Spline length. */
  double		 fprec;		/* factor * precision. */
  double		 ayl = 0.0;	/* abs yl. */
  double		 ayr = 0.0;	/* abs yr. */
  unsigned long		 passno = 0UL;	/* Current pass. */
  int			 mec = 0;	/* Error code variable. */
  int			 cc  = 1;	/* Flag: Can continue. */
  int			 mc  = 1;	/* Flag: Must continue. */
  

#line 1460 "dk3xsp.ctr"
  fprec = 0.2 * prec;
  if((points) && (1 < np) && (prec > 0.0) && (dist >= 0.0)) {
    l = dk3xsp_open_spline_length(points, np, fprec, fslc, cb, &mec);
    

#line 1464 "dk3xsp.ctr"
    if(l > dist) {
      xl = 0.0;
      xr = (double)(np - 1);
      if(dir) {
        yl = 0.0 - dist;
	yr = dk3ma_d_sub_ok(l, dist, &mec);	

#line 1470 "dk3xsp.ctr"
      } else {
        yl = dk3ma_d_sub_ok(l, dist, &mec);	

#line 1472 "dk3xsp.ctr"
	yr = 0.0 - dist;
      }
      while((cc) && (mc) && (!(mec))) {
        xc = 0.5 * dk3ma_d_add_ok(xl, xr, &mec);
	

#line 1477 "dk3xsp.ctr"
	yc = dk3ma_d_sub_ok(
	  dk3xsp_open_spline_partial_length(
	    points, np, fprec, xc, fslc, dir, cb, ec
	  ),
	  dist,
	  &mec
	);	

#line 1484 "dk3xsp.ctr"
	

#line 1485 "dk3xsp.ctr"
	

#line 1486 "dk3xsp.ctr"
	

#line 1487 "dk3xsp.ctr"
	if(dir) {
	  if(yc > 0.0) {
	    xr = xc; yr = yc;
	  } else {
	    xl = xc; yl = yc;
	  }
	} else {
	  if(yc > 0.0) {
	    xl = xc; yl = yc;
	  } else {
	    xr = xc; yr = yc;
	  }
	}
	ayl = fabs(yl);
	ayr = fabs(yr);
	if((ayl < prec) && (ayr < prec)) {
	  mc = 0;
	} else {
	  passno++;	

#line 1506 "dk3xsp.ctr"
	  if(passno > 1000UL) {		

#line 1507 "dk3xsp.ctr"
	    mec = DK3_ERROR_ITERATION;
	  } else {
	  }
	}
      }
      if((cc) && (!(mec))) {	

#line 1513 "dk3xsp.ctr"
	back = xl + ((xr - xl) * ayl)/(ayl + ayr);
      }
      if(mec) {			

#line 1516 "dk3xsp.ctr"
        back = -1.0;
	if(ec) { *ec = mec; }
      }
    } else {
      if(ec) { *ec = DK3_ERROR_INVALID_ARGS; }
    }
  } else {
    if(ec) { *ec = DK3_ERROR_INVALID_ARGS; }
  } 

#line 1525 "dk3xsp.ctr"
  return back;
}



double
dk3xsp_direction(
  dk3_fig_spline_point_t const	*points,
  size_t			 np,
  double			 t,
  int				 cb,
  int				*ec
)
{
  dk3_xspline_segment_t	 seg;
  double		 back = -10.0 * M_PI;
  double		 dxdt;
  double		 dydt;
  double		 delta;
  double		 tleft;
  double		 tright;
  double		 xleft;
  double		 xright;
  double		 yleft;
  double		 yright;
  int			 mec = 0;
  

#line 1552 "dk3xsp.ctr"
  if((points) && (1 < np)) {
    if((t >= 0.0) && (t <= ((double)(np - 1)))) {
      dk3xsp_reset(&seg);
      dk3xsp_set_cb(&seg, ((cb) ? 1 : 0));
      if(dk3xsp_calculate_position(&seg, points, np, t, cb, 1, &mec)) {
        dxdt = dk3xsp_get_dxdt(&seg);
	dydt = dk3xsp_get_dydt(&seg);
	if((fabs(dxdt) > 1.0e-6) || (fabs(dydt) > 1.0e-6)) {
	  back = dk3ma_d_atan2(dydt, dxdt);
	} else {			

#line 1562 "dk3xsp.ctr"
	  delta = 0.001;
	  tleft = tright = t;
	  if(t > delta) { tleft = t - delta; }
	  if(t < (((double)(np - 1)) - delta)) { tright = t + delta; }
	  if(dk3xsp_calculate_position(&seg, points, np, tleft, cb, 0, &mec)) {
	  xleft = dk3xsp_get_x(&seg);
	  yleft = dk3xsp_get_y(&seg);
	  if(dk3xsp_calculate_position(&seg, points, np, tright, cb, 0, &mec)) {
	    xright = dk3xsp_get_x(&seg);
	    yright = dk3xsp_get_y(&seg);
	    dxdt = dk3ma_d_sub_ok(xright, xleft, &mec);
	    dydt = dk3ma_d_sub_ok(yright, yleft, &mec);
	    back = dk3ma_d_atan2(dydt, dxdt);
	  } else {		

#line 1576 "dk3xsp.ctr"
	  }
	  } else {		

#line 1578 "dk3xsp.ctr"
	  }
	}
      } else {				

#line 1581 "dk3xsp.ctr"
      }
      if(mec) { if(ec) { *ec = mec; } }
    } else {
      if(ec) { *ec = DK3_ERROR_INVALID_ARGS; }
    }
  } else {
    if(ec) { *ec = DK3_ERROR_INVALID_ARGS; }
  } 

#line 1589 "dk3xsp.ctr"
  return back;
}



double
dk3xsp_direction_and_position(
  double			*xpos,
  double			*ypos,
  dk3_fig_spline_point_t const	*points,
  size_t			 np,
  double			 t,
  int				 cb,
  int				*ec
)
{
  dk3_xspline_segment_t	 seg;
  double		 back = -10.0 * M_PI;
  double		 dxdt;
  double		 dydt;
  double		 delta;
  double		 tleft;
  double		 tright;
  double		 xleft;
  double		 xright;
  double		 yleft;
  double		 yright;
  int			 mec = 0;
  

#line 1618 "dk3xsp.ctr"
  if((points) && (1 < np) && (xpos) && (ypos)) {
    if((t >= 0.0) && (t <= ((double)(np - 1)))) {
      dk3xsp_reset(&seg);
      dk3xsp_set_cb(&seg, ((cb) ? 1 : 0));
      if(dk3xsp_calculate_position(&seg, points, np, t, cb, 1, &mec)) {
	*xpos = dk3xsp_get_x(&seg);
	*ypos = dk3xsp_get_y(&seg);
        dxdt = dk3xsp_get_dxdt(&seg);
	dydt = dk3xsp_get_dydt(&seg);	

#line 1627 "dk3xsp.ctr"
	if((fabs(dxdt) > 1.0e-6) || (fabs(dydt) > 1.0e-6)) {
	  back = dk3ma_d_atan2(dydt, dxdt);	

#line 1629 "dk3xsp.ctr"
	} else {			

#line 1630 "dk3xsp.ctr"
	  delta = 0.001;
	  tleft = tright = t;
	  if(t > delta) { tleft = t - delta; }
	  if(t < (((double)(np - 1)) - delta)) { tright = t + delta; }
	  if(dk3xsp_calculate_position(&seg, points, np, tleft, cb, 0, &mec)) {
	  xleft = dk3xsp_get_x(&seg);
	  yleft = dk3xsp_get_y(&seg);
	  if(dk3xsp_calculate_position(&seg, points, np, tright, cb, 0, &mec)) {
	    xright = dk3xsp_get_x(&seg);
	    yright = dk3xsp_get_y(&seg);
	    dxdt = dk3ma_d_sub_ok(xright, xleft, &mec);
	    dydt = dk3ma_d_sub_ok(yright, yleft, &mec);
	    back = dk3ma_d_atan2(dydt, dxdt);
	  } else {		

#line 1644 "dk3xsp.ctr"
	  }
	  } else {		

#line 1646 "dk3xsp.ctr"
	  }
	}
      } else {				

#line 1649 "dk3xsp.ctr"
      }
      if(mec) { if(ec) { *ec = mec; } }
    } else {
      if(ec) { *ec = DK3_ERROR_INVALID_ARGS; }
    }
  } else {
    if(ec) { *ec = DK3_ERROR_INVALID_ARGS; }
  } 

#line 1657 "dk3xsp.ctr"
  return back;
}