-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathhobby_code.dtx
More file actions
3641 lines (3592 loc) · 113 KB
/
Copy pathhobby_code.dtx
File metadata and controls
3641 lines (3592 loc) · 113 KB
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
% \iffalse meta-comment
%<*internal>
\iffalse
%</internal>
%<*readme>
----------------------------------------------------------------
hobby --- a TikZ/PGF library for drawing smooth(ish) curves using
Hobby's algorithm (implemented in LaTeX3)
E-mail: loopspace@mathforge.org
Released under the LaTeX Project Public License v1.3c or later
See http://www.latex-project.org/lppl.txt
----------------------------------------------------------------
This package defines a path generation function for TikZ/PGF
which implements Hobby's algorithm for a path built out of Bezier
curves which passes through a given set of points.
The implementation is in LaTeX3. It can be used as as a TikZ
`to path`.
%</readme>
%<*internal>
\fi
\def\nameofplainTeX{plain}
\ifx\fmtname\nameofplainTeX\else
\expandafter\begingroup
\fi
%</internal>
%<*install>
\input docstrip.tex
\keepsilent
\askforoverwritefalse
\preamble
----------------------------------------------------------------
hobby --- a TikZ/PGF library for drawing smooth(ish) curves using
Hobby's algorithm (implemented in LaTeX3)
E-mail: loopspace@mathforge.org
Released under the LaTeX Project Public License v1.3c or later
See http://www.latex-project.org/lppl.txt
----------------------------------------------------------------
\endpreamble
\postamble
Copyright (C) 2012-2023 by Andrew Stacey <loopspace@mathforge.org>
This file may be distributed and/or modified under the conditions
of the LaTeX Project Public License, either version 1.3 of this
license or (at your option) any later version.
The latest version of this license is in:
http://www.latex-project.org/lppl.txt
and version 1.3 or later is part of all distributions of LaTeX
version 2005/12/01 or later.
This work is "maintained" (as per LPPL maintenance status) by
Andrew Stacey.
This work consists of the files hobby_code.dtx
hobby.tex
and the derived files hobby.code.tex
pgflibraryhobby.code.tex
tikzlibraryhobby.code.tex
pml3array.sty
hobby-l3draw.sty
hobby.ins
hobby.pdf
hobby_code.pdf
README.txt
\endpostamble
\usedir{tex/latex/hobby}
\generate{\file{tikzlibraryhobby.code.tex} {\from{hobby_code.dtx}{tikzlibrary}}}
\generate{\file{pgflibraryhobby.code.tex} {\from{hobby_code.dtx}{pgflibrary}}}
\generate{\file{hobby.code.tex} {\from{hobby_code.dtx}{hobby}}}
\generate{\file{pml3array.sty} {\from{hobby_code.dtx}{array}}}
\generate{\file{hobby-l3draw.sty} {\from{hobby_code.dtx}{l3hobby}}}
%</install>
%<install>\endbatchfile
%<*internal>
\usedir{source/latex/hobby}
\generate{
\file{\jobname.ins}{\from{\jobname.dtx}{install}}
}
\nopreamble\nopostamble
\generate{
\file{README.txt}{\from{\jobname.dtx}{readme}}
}
\ifx\fmtname\nameofplainTeX
\expandafter\endbatchfile
\else
\expandafter\endgroup
\fi
%</internal>
%<*driver>
\documentclass[full]{l3doc}
\usepackage[T1]{fontenc}
\usepackage{csquotes}
\usepackage{lmodern}
\usepackage{tikz}
\usepackage{amsmath}
\usetikzlibrary{hobby,decorations.pathreplacing}
\usepackage[margin=3cm]{geometry}
\EnableCrossrefs
\CodelineIndex
\RecordChanges
\addtolength{\hoffset}{.4in}
\begin{document}
\DocInput{\jobname.dtx}
\end{document}
%</driver>
% \fi
%
% \CheckSum{3427}
%
% \CharacterTable
% {Upper-case \A\B\C\D\E\F\G\H\I\J\K\L\M\N\O\P\Q\R\S\T\U\V\W\X\Y\Z
% Lower-case \a\b\c\d\e\f\g\h\i\j\k\l\m\n\o\p\q\r\s\t\u\v\w\x\y\z
% Digits \0\1\2\3\4\5\6\7\8\9
% Exclamation \! Double quote \" Hash (number) \#
% Dollar \$ Percent \% Ampersand \&
% Acute accent \' Left paren \( Right paren \)
% Asterisk \* Plus \+ Comma \,
% Minus \- Point \. Solidus \/
% Colon \: Semicolon \; Less than \<
% Equals \= Greater than \> Question mark \?
% Commercial at \@ Left bracket \[ Backslash \\
% Right bracket \] Circumflex \^ Underscore \_
% Grave accent \` Left brace \{ Vertical bar \|
% Right brace \} Tilde \~}
%
%
%
% \DoNotIndex{\newcommand,\newenvironment}
%
% \providecommand*{\url}{\texttt}
% \title{The \textsf{Hobby} package: code}
% \author{Andrew Stacey \\ \url{loopspace@mathforge.org}}
% \date{\hobbyVersion\ from\ \hobbyDate}
% \maketitle
%
%
% \StopEventually{\PrintChanges}
% \section{Implementation}
%
% \subsection{Main Code}
%
% \iffalse
%<*hobby>
% \fi
%
% We use \LaTeX3 syntax so need to load the requisite packages
% \begin{macrocode}
\RequirePackage{pml3array}
\ExplSyntaxOn
% \end{macrocode}
%
% \begin{macrocode}
\cs_generate_variant:Nn \fp_set:Nn {Nx}
\cs_generate_variant:Nn \tl_if_eq:nnTF {VnTF}
\cs_generate_variant:Nn \tl_if_eq:nnTF {xnTF}
% \end{macrocode}
%
% \subsubsection{Initialisation}
%
% We declare all our variables.
%
% Start with version and date, together with a check to see if we've been loaded twice (fail gracefully if so).
%
% \begin{macrocode}
\tl_clear:N \l_tmpa_tl
\tl_if_exist:NT \g__hobby_version
{
\tl_set:Nn \l_tmpa_tl {
\ExplSyntaxOff
\tl_clear:N \l_tmpa_tl
\endinput
}
}
\tl_use:N \l_tmpa_tl
\tl_new:N \g__hobby_version
\tl_new:N \g__hobby_date
\tl_gset:Nn \g__hobby_version {1.12}
\tl_gset:Nn \g__hobby_date {2023-09-01}
\DeclareDocumentCommand \hobbyVersion {}
{
\tl_use:N \g__hobby_version
}
\DeclareDocumentCommand \hobbyDate {}
{
\tl_use:N \g__hobby_date
}
% \end{macrocode}
%
% The function for computing the lengths of the control points depends on three parameters.
% These are set to \(a = \sqrt{2}\), \(b = 1/16\), and \(c = \frac{3 - \sqrt{5}}{2}\).
% \begin{macrocode}
\fp_new:N \g_hobby_parama_fp
\fp_new:N \g_hobby_paramb_fp
\fp_new:N \g_hobby_paramc_fp
\fp_gset:Nn \g_hobby_parama_fp {2^.5}
\fp_gset:Nn \g_hobby_paramb_fp {1/16}
\fp_gset:Nn \g_hobby_paramc_fp {(3-5^.5)/2}
% \end{macrocode}
%
% Now we define our objects for use in generating the path.
%
% \begin{macro}{\l_hobby_closed_bool}
% \Verb+\l_hobby_closed_bool+ is \Verb+true+ if the path is closed.
% \begin{macrocode}
\bool_new:N \l_hobby_closed_bool
% \end{macrocode}
% \end{macro}
%
% \begin{macro}{\l_hobby_disjoint_bool}
% \Verb+\l_hobby_disjoint_bool+ is \Verb+true+ if the path should start with a \Verb+moveto+ command.
% \begin{macrocode}
\bool_new:N \l_hobby_disjoint_bool
% \end{macrocode}
% \end{macro}
%
% \begin{macro}{\l_hobby_save_aux_bool}
% \Verb+\l_hobby_save_aux_bool+ is \Verb+true+ if when saving paths then they should be saved to the \Verb+aux+ file.
% \begin{macrocode}
\bool_new:N \l_hobby_save_aux_bool
\bool_set_true:N \l_hobby_save_aux_bool
\DeclareDocumentCommand \HobbyDisableAux {}
{
\bool_set_false:N \l_hobby_save_aux_bool
}
% \end{macrocode}
% \end{macro}
%
% \begin{macro}{\g__hobby_points_array}
% \Verb+\g__hobby_points_array+ is an array holding the specified points on the path.
% In the \LaTeX3 code, a ``point'' is a token list of the form \Verb+<number>, <number>+.
% This gives us the greatest flexibility in passing points back and forth between the \LaTeX3 code and any calling code.
% The array is indexed by integers beginning with \(0\).
% In the documentation, we will use the notation \(z_k\) to refer to the \(k\)th point.
% \begin{macrocode}
\array_new:N \g__hobby_points_array
% \end{macrocode}
% \end{macro}
%
% \begin{macro}{\g__hobby_points_x_array}
% \Verb+\g__hobby_points_x_array+ is an array holding the \(x\)--{}coordinates of the specified points.
% \begin{macrocode}
\array_new:N \g__hobby_points_x_array
% \end{macrocode}
% \end{macro}
%
% \begin{macro}{\l_hobby_points_y_array}
% \Verb+\g__hobby_points_y_array+ is an array holding the \(y\)--{}coordinates of the specified points.
% \begin{macrocode}
\array_new:N \g__hobby_points_y_array
% \end{macrocode}
% \end{macro}
%
% \begin{macro}{\g__hobby_actions_array}
% \Verb+\g__hobby_actions_array+ is an array holding the (encoded) action to be taken out on the segment of the path ending at that point.
% \begin{macrocode}
\array_new:N \g__hobby_actions_array
% \end{macrocode}
% \end{macro}
%
% \begin{macro}{\g__hobby_angles_array}
% \Verb+\g__hobby_angles_array+ is an array holding the angles of the lines between the points.
% Specifically, the angle indexed by \(k\) is the angle in radians of the line from \(z_k\) to \(z_{k+1}\).
% \begin{macrocode}
\array_new:N \g__hobby_angles_array
% \end{macrocode}
% \end{macro}
%
% \begin{macro}{\g__hobby_distances_array}
% \Verb+\g__hobby_distances_array+ is an array holding the distances between the points.
% Specifically, the distance indexed by \(k\), which we will write as \(d_k\), is the length of the line from \(z_k\) to \(z_{k+1}\).
% \begin{macrocode}
\array_new:N \g__hobby_distances_array
% \end{macrocode}
% \end{macro}
%
% \begin{macro}{\g__hobby_tension_out_array}
% \Verb+\g__hobby_tension_out_array+ is an array holding the tension for the path as it leaves each point.
% This is a parameter that controls how much the curve ``flexes'' as it leaves the point.
% In the following, this will be written \(\tau_k\).
% \begin{macrocode}
\array_new:N \g__hobby_tension_out_array
% \end{macrocode}
% \end{macro}
%
% \begin{macro}{\g__hobby_tension_in_array}
% \Verb+\g__hobby_tension_in_array+ is an array holding the tension for the path as it arrives at each point.
% This is a parameter that controls how much the curve ``flexes'' as it gets to the point.
% In the following, this will be written \(\overline{\tau}_k\).
% \begin{macrocode}
\array_new:N \g__hobby_tension_in_array
% \end{macrocode}
% \end{macro}
%
% \begin{macro}{\g__hobby_matrix_a_array}
% \Verb+\g__hobby_matrix_a_array+ is an array holding the subdiagonal of the linear system that has to be solved to find the angles of the control points.
% In the following, this will be denoted by \(A_i\).
% The first index is \(1\).
% \begin{macrocode}
\array_new:N \g__hobby_matrix_a_array
% \end{macrocode}
% \end{macro}
%
% \begin{macro}{\g__hobby_matrix_b_array}
% \Verb+\g__hobby_matrix_b_array+ is an array holding the diagonal of the linear system that has to be solved to find the angles of the control points.
% In the following, this will be denoted by \(B_i\).
% The first index is \(0\).
% \begin{macrocode}
\array_new:N \g__hobby_matrix_b_array
% \end{macrocode}
% \end{macro}
%
% \begin{macro}{\g__hobby_matrix_c_array}
% \Verb+\g__hobby_matrix_c_array+ is an array holding the superdiagonal of the linear system that has to be solved to find the angles of the control points.
% In the following, this will be denoted by \(C_i\).
% The first index is \(0\).
% \begin{macrocode}
\array_new:N \g__hobby_matrix_c_array
% \end{macrocode}
% \end{macro}
%
% \begin{macro}{\g__hobby_matrix_d_array}
% \Verb+\g__hobby_matrix_d_array+ is an array holding the target vector of the linear system that has to be solved to find the angles of the control points.
% In the following, this will be denoted by \(D_i\).
% The first index is \(1\).
% \begin{macrocode}
\array_new:N \g__hobby_matrix_d_array
% \end{macrocode}
% \end{macro}
%
% \begin{macro}{\g__hobby_vector_u_array}
% \Verb+\g__hobby_vector_u_array+ is an array holding the perturbation of the linear system for closed paths.
% The coefficient matrix for an \emph{open} path is tridiagonal and that means that Gaussian elimination runs faster than expected (\(O(n)\) instead of \(O(n^3)\)).
% The matrix for a closed path is not tridiagonal but is not far off.
% It can be solved by perturbing it to a tridiagonal matrix and then modifying the result.
% This array represents a utility vector in that perturbation.
% In the following, the vector will be denoted by \(u\).
% The first index is \(1\).
% \begin{macrocode}
\array_new:N \g__hobby_vector_u_array
% \end{macrocode}
% \end{macro}
%
% \begin{macro}{\g__hobby_excess_angle_array}
% \Verb+\g__hobby_excess_angle_array+ is an array that allows the user to say that the algorithm should add a multiple of \(2 \pi\) to the angle differences.
% This is because these angles are wrapped to the interval \((-\pi,\pi]\) but the wrapping might go wrong near the end points due to computation accuracy.
% The first index is \(1\).
% \begin{macrocode}
\array_new:N \g__hobby_excess_angle_array
% \end{macrocode}
% \end{macro}
%
% \begin{macro}{\g__hobby_psi_array}
% \Verb+\g__hobby_psi_array+ is an array holding the difference of the angles of the lines entering and exiting a point.
% That is, \(\psi_k\) is the angle between the lines joining \(z_k\) to \(z_{k-1}\) and \(z_{k+1}\).
% The first index is \(1\).
% \begin{macrocode}
\array_new:N \g__hobby_psi_array
% \end{macrocode}
% \end{macro}
%
% \begin{macro}{\g__hobby_theta_array}
% \Verb+\g__hobby_theta_array+ is an array holding the angles of the outgoing control points for the generated path.
% These are measured relative to the line joining the point to the next point on the path.
% The first index is \(0\).
% \begin{macrocode}
\array_new:N \g__hobby_theta_array
% \end{macrocode}
% \end{macro}
%
% \begin{macro}{\g__hobby_phi_array}
% \Verb+\g__hobby_phi_array+ is an array holding the angles of the incoming control points for the generated path.
% These are measured relative to the line joining the point to the previous point on the path.
% The first index is \(1\).
% \begin{macrocode}
\array_new:N \g__hobby_phi_array
% \end{macrocode}
% \end{macro}
%
% \begin{macro}{\g__hobby_sigma_array}
% \Verb+\g__hobby_sigma_array+ is an array holding the lengths of the outgoing control points for the generated path.
% The units are such that the length of the line to the next specified point is one unit.
% \begin{macrocode}
\array_new:N \g__hobby_sigma_array
% \end{macrocode}
% \end{macro}
%
% \begin{macro}{\g__hobby_rho_array}
% \Verb+\g__hobby_rho_array+ is an array holding the lengths of the incoming control points for the generated path.
% The units are such that the length of the line to the previous specified point is one unit.
% \begin{macrocode}
\array_new:N \g__hobby_rho_array
% \end{macrocode}
% \end{macro}
%
% \begin{macro}{\g__hobby_controla_array}
% \Verb+\g__hobby_controla_array+ is an array holding the coordinates of the first control points on the curves.
% The format is the same as for \Verb+\g__hobby_points_array+.
% \begin{macrocode}
\array_new:N \g__hobby_controla_array
% \end{macrocode}
% \end{macro}
%
% \begin{macro}{\g__hobby_controlb_array}
% \Verb+\g__hobby_controlb_array+ is an array holding the coordinates of the second control points on the curves.
% The format is the same as for \Verb+\g__hobby_points_array+.
% \begin{macrocode}
\array_new:N \g__hobby_controlb_array
% \end{macrocode}
% \end{macro}
%
% \begin{macro}{\l_hobby_matrix_v_fp}
% \Verb+\l_hobby_matrix_v_fp+ is a number which is used when doing the perturbation of the solution of the linear system for a closed curve.
% There is actually a vector, \(v\), that this corresponds to but that vector only has one component that needs computation.
% \begin{macrocode}
\fp_new:N \l_hobby_matrix_v_fp
% \end{macrocode}
% \end{macro}
%
% \begin{macro}{\l_hobby_tempa_tl}
% \Verb+\l_hobby_tempa_tl+ is a temporary variable of type \Verb+tl+.
% \begin{macrocode}
\fp_new:N \l_hobby_tempa_tl
% \end{macrocode}
% \end{macro}
%
% \begin{macro}{\l_hobby_tempb_tl}
% \Verb+\l_hobby_tempb_tl+ is a temporary variable of type \Verb+tl+.
% \begin{macrocode}
\fp_new:N \l_hobby_tempb_tl
% \end{macrocode}
% \end{macro}
%
% \begin{macro}{\l_hobby_tempa_fp}
% \Verb+\l_hobby_tempa_fp+ is a temporary variable of type \Verb+fp+.
% \begin{macrocode}
\fp_new:N \l_hobby_tempa_fp
% \end{macrocode}
% \end{macro}
%
% \begin{macro}{\l_hobby_tempb_fp}
% \Verb+\l_hobby_tempb_fp+ is a temporary variable of type \Verb+fp+.
% \begin{macrocode}
\fp_new:N \l_hobby_tempb_fp
% \end{macrocode}
% \end{macro}
%
% \begin{macro}{\l_hobby_tempc_fp}
% \Verb+\l_hobby_tempc_fp+ is a temporary variable of type \Verb+fp+.
% \begin{macrocode}
\fp_new:N \l_hobby_tempc_fp
% \end{macrocode}
% \end{macro}
%
% \begin{macro}{\l_hobby_tempd_fp}
% \Verb+\l_hobby_tempd_fp+ is a temporary variable of type \Verb+fp+.
% \begin{macrocode}
\fp_new:N \l_hobby_tempd_fp
% \end{macrocode}
% \end{macro}
%
% \begin{macro}{\l_hobby_temps_fp}
% \Verb+\l_hobby_temps_fp+ is a temporary variable of type \Verb+fp+.
% \begin{macrocode}
\fp_new:N \l_hobby_temps_fp
% \end{macrocode}
% \end{macro}
%
% \begin{macro}{\g__hobby_in_curl_fp}
% \Verb+\g__hobby_in_curl_fp+ is the ``curl'' at the end of an open path.
% This is used if the angle at the end is not specified.
% \begin{macrocode}
\fp_new:N \g__hobby_in_curl_fp
\fp_gset:Nn \g__hobby_in_curl_fp {1}
% \end{macrocode}
% \end{macro}
%
% \begin{macro}{\g__hobby_out_curl_fp}
% \Verb+\g__hobby_out_curl_fp+ is the ``curl'' at the start of an open path.
% This is used if the angle at the start is not specified.
% \begin{macrocode}
\fp_new:N \g__hobby_out_curl_fp
\fp_gset:Nn \g__hobby_out_curl_fp {1}
% \end{macrocode}
% \end{macro}
%
% \begin{macro}{\g__hobby_in_angle_fp}
% \Verb+\g__hobby_in_angle_fp+ is the angle at the end of an open path.
% If this is not specified, it will be computed automatically.
% It is set to \Verb+\c_inf_fp+ to allow easy detection of when it has been specified.
% \begin{macrocode}
\fp_new:N \g__hobby_in_angle_fp
\fp_gset_eq:NN \g__hobby_in_angle_fp \c_inf_fp
% \end{macrocode}
% \end{macro}
%
% \begin{macro}{\g__hobby_out_angle_fp}
% \Verb+\g__hobby_out_angle_fp+ is the angle at the start of an open path.
% If this is not specified, it will be computed automatically.
% It is set to \Verb+\c_inf_fp+ to allow easy detection of when it has been specified.
% \begin{macrocode}
\fp_new:N \g__hobby_out_angle_fp
\fp_gset_eq:NN \g__hobby_out_angle_fp \c_inf_fp
% \end{macrocode}
% \end{macro}
%
% \begin{macro}{\g__hobby_npoints_int}
% \Verb+\g__hobby_npoints_int+ is one less than the number of points on the curve.
% As our list of points starts at \(0\), this is the index of the last point.
% In the algorithm for a closed curve, some points are repeated whereupon this is incremented so that it is always the index of the last point.
% \begin{macrocode}
\int_new:N \g__hobby_npoints_int
% \end{macrocode}
% \end{macro}
%
% \begin{macro}{\g__hobby_draw_int}
% \begin{macrocode}
\int_new:N \g__hobby_draw_int
% \end{macrocode}
% \end{macro}
%
% A ``point'' is a key-value list setting the x-value, the y-value, and the tensions at that point.
% Using keys makes it easier to pass points from the algorithm code to the calling code and vice versa without either knowing too much about the other.
% \begin{macrocode}
\keys_define:nn {hobby / read in all} {
point .code:n = {
\fp_set:Nn \l_hobby_tempa_fp {\clist_item:nn {#1} {1}}
\fp_set:Nn \l_hobby_tempb_fp {\clist_item:nn {#1} {2}}
},
tension~out .fp_set:N = \l_hobby_tempc_fp,
tension~in .fp_set:N = \l_hobby_tempd_fp,
excess~angle .fp_set:N = \l_hobby_temps_fp,
break .tl_set:N = \l_tmpb_tl,
blank .tl_set:N = \l_tmpa_tl,
tension .meta:n = { tension~out=#1, tension~in=#1 },
break .default:n = false,
blank .default:n = false,
invert~soft~blanks .choice:,
invert~soft~blanks / true .code:n = {
\int_gset:Nn \g__hobby_draw_int {0}
},
invert~soft~blanks / false .code:n = {
\int_gset:Nn \g__hobby_draw_int {1}
},
invert~soft~blanks .default:n = true,
tension~out .default:n = 1,
tension~in .default:n = 1,
excess~angle .default:n = 0,
in~angle .fp_gset:N = \g__hobby_in_angle_fp,
out~angle .fp_gset:N = \g__hobby_out_angle_fp,
in~curl .fp_gset:N = \g__hobby_in_curl_fp,
out~curl .fp_gset:N = \g__hobby_out_curl_fp,
closed .bool_gset:N = \g__hobby_closed_bool,
closed .default:n = true,
disjoint .bool_gset:N = \g__hobby_disjoint_bool,
disjoint .default:n = true,
break~default .code:n = {
\keys_define:nn { hobby / read in all }
{
break .default:n = #1
}
},
blank~default .code:n = {
\keys_define:nn { hobby / read in all }
{
blank .default:n = #1
}
},
}
% \end{macrocode}
% There are certain other parameters that can be set for a given curve.
% \begin{macrocode}
\keys_define:nn { hobby / read in params} {
in~angle .fp_gset:N = \g__hobby_in_angle_fp,
out~angle .fp_gset:N = \g__hobby_out_angle_fp,
in~curl .fp_gset:N = \g__hobby_in_curl_fp,
out~curl .fp_gset:N = \g__hobby_out_curl_fp,
closed .bool_gset:N = \g__hobby_closed_bool,
closed .default:n = true,
disjoint .bool_gset:N = \g__hobby_disjoint_bool,
disjoint .default:n = true,
break~default .code:n = {
\keys_define:nn { hobby / read in all }
{
break .default:n = #1
}
},
blank~default .code:n = {
\keys_define:nn { hobby / read in all }
{
blank .default:n = #1
}
},
invert~soft~blanks .choice:,
invert~soft~blanks / true .code:n = {
\int_gset:Nn \g__hobby_draw_int {0}
},
invert~soft~blanks / false .code:n = {
\int_gset:Nn \g__hobby_draw_int {1}
},
invert~soft~blanks .default:n = true,
}
% \end{macrocode}
% \begin{macro}{\hobby_distangle:n}
% Computes the distance and angle between successive points.
% The argument given is the index of the current point.
% Assumptions: the points are in \Verb+\g__hobby_points_x_array+ and \Verb+\g__hobby_points_y_array+ and the index of the last point is \Verb+\g__hobby_npoints_int+.
% \begin{macrocode}
\cs_set:Nn \hobby_distangle:n {
\fp_set:Nn \l_hobby_tempa_fp {
(\array_get:Nn \g__hobby_points_x_array {#1 + 1})
- (\array_get:Nn \g__hobby_points_x_array {#1})}
\fp_set:Nn \l_hobby_tempb_fp {
(\array_get:Nn \g__hobby_points_y_array {#1 + 1})
- (\array_get:Nn \g__hobby_points_y_array {#1})}
\fp_set:Nn \l_hobby_tempc_fp { atan ( \l_hobby_tempb_fp, \l_hobby_tempa_fp ) }
\fp_veclen:NVV \l_hobby_tempd_fp \l_hobby_tempa_fp \l_hobby_tempb_fp
\array_gpush:Nx \g__hobby_angles_array {\fp_to_tl:N \l_hobby_tempc_fp}
\array_gpush:Nx \g__hobby_distances_array {\fp_to_tl:N \l_hobby_tempd_fp}
}
% \end{macrocode}
% \end{macro}
%
% \begin{macro}{\fp_veclen:NVV}
% Computes the length of the vector specified by the latter two arguments, storing the answer in the first.
% \begin{macrocode}
\cs_new:Nn \fp_veclen:Nnn {
\fp_set:Nn #1 {((#2)^2 + (#3)^2)^.5}
}
\cs_generate_variant:Nn \fp_veclen:Nnn {NVV}
% \end{macrocode}
% \end{macro}
%
% \begin{macro}{\hobby_ctrllen:Nnn}
% Computes the length of the control point vector from the two angles, storing the answer in the first argument given.
% \begin{macrocode}
\cs_new:Nn \hobby_ctrllen:Nnn {
\fp_set:Nn #1 {(2 - \g_hobby_parama_fp
* ( sin(#2) - \g_hobby_paramb_fp * sin(#3) )
* ( sin(#3) - \g_hobby_paramb_fp * sin(#2) )
* ( cos(#2) - cos(#3) ) )
/ ( 1 + (1 - \g_hobby_paramc_fp) * cos(#3) + \g_hobby_paramc_fp * cos(#2))}
}
\cs_generate_variant:Nn \hobby_ctrllen:Nnn {NVV}
% \end{macrocode}
% \end{macro}
%
% \begin{macro}{\hobby_append_point_copy:n}
% This function adds a copy of the point (numbered by its argument) to the end of the list of points, copying all the relevant data (coordinates, tension, etc.).
%
% Originally from Bruno Le Foch on TeX-SX.
% \begin{macrocode}
\cs_new_protected:Npn \hobby_append_point_copy:n #1
{
\hobby_append_point_copy_aux:Nn \g__hobby_points_array {#1}
\hobby_append_point_copy_aux:Nn \g__hobby_points_x_array {#1}
\hobby_append_point_copy_aux:Nn \g__hobby_points_y_array {#1}
\hobby_append_point_copy_aux:Nn \g__hobby_tension_in_array {#1}
\hobby_append_point_copy_aux:Nn \g__hobby_tension_out_array {#1}
\hobby_append_point_copy_aux:Nn \g__hobby_excess_angle_array {#1}
\hobby_append_point_copy_aux:Nn \g__hobby_actions_array {#1}
}
\cs_new_protected:Npn \hobby_append_point_copy_aux:Nn #1#2
{ \array_gpush:Nx #1 { \array_get:Nn #1 {#2} } }
% \end{macrocode}
% \end{macro}
%
% \begin{macro}{\hobby_gen_path:}
% This is the curve generation function.
% We assume at the start that we have an array containing all the points that the curve must go through, and the various curve parameters have been initialised.
% So these must be set up by a wrapper function which then calls this one.
% The list of required information is:
% \begin{enumerate}
% \item \Verb+\g__hobby_points_x_array+
% \item \Verb+\g__hobby_points_y_array+
% \item \Verb+\g__hobby_tension_out_array+
% \item \Verb+\g__hobby_tension_in_array+
% \item \Verb+\g__hobby_excess_angle_array+
% \item \Verb+\g__hobby_in_curl_fp+
% \item \Verb+\g__hobby_out_curl_fp+
% \item \Verb+\g__hobby_in_angle_fp+
% \item \Verb+\g__hobby_out_angle_fp+
% \item \Verb+\g__hobby_closed_bool+
% \item \Verb+\g__hobby_actions_array+
% \end{enumerate}
%
% \begin{macrocode}
\cs_new:Nn \hobby_gen_path:
{
% \end{macrocode}
% For much of the time, we can pretend that a closed path is the same as an open path.
% To do this, we need to make the end node an internal node by repeating the \(z_1\) node as the \(z_{n+1}\)th node.
% We also check that the last (\(z_n\)) and first (\(z_0\)) nodes are the same, otherwise we repeat the \(z_0\) node as well.
% \begin{macrocode}
\bool_if:NT \g__hobby_closed_bool {
% \end{macrocode}
% Are the \(x\)-values of the first and last points different?
% \begin{macrocode}
\fp_compare:nTF {(\array_get:Nn \g__hobby_points_x_array {0})
=
(\array_top:N \g__hobby_points_x_array)}
{
% \end{macrocode}
% No, so compare the \(y\)-values.
% Are the \(y\)-values of the first and last points different?
% \begin{macrocode}
\fp_compare:nF {
\array_get:Nn \g__hobby_points_y_array {0}
=
\array_top:N \g__hobby_points_y_array
}
{
% \end{macrocode}
% Yes, so we need to duplicate the first point, with all of its data.
% \begin{macrocode}
\hobby_append_point_copy:n {0}
}
}
{
% \end{macrocode}
% Yes, so we need to duplicate the first point, with all of its data.
% \begin{macrocode}
\hobby_append_point_copy:n {0}
}
% \end{macrocode}
% Now that we are sure that the first and last points are identical, we need to duplicate the first-but-one point (and all of its data).
% \begin{macrocode}
\hobby_append_point_copy:n {1}
}
% \end{macrocode}
%
% Set \Verb+\g__hobby_npoints_int+ to the number of points (minus one).
% \begin{macrocode}
\int_gset:Nn \g__hobby_npoints_int {\array_length:N \g__hobby_points_y_array}
% \end{macrocode}
% At this point, we need to decide what to do.
% This will depend on whether we have any intermediate points.
% \begin{macrocode}
\int_compare:nNnTF {\g__hobby_npoints_int} = {0} {
% \end{macrocode}
% Only one point, do nothing
% \begin{macrocode}
}
{
\int_compare:nNnTF {\g__hobby_npoints_int} = {1} {
% \end{macrocode}
% Only two points, skip processing.
% Just need to set the incoming and outgoing angles
% \begin{macrocode}
\hobby_distangle:n {0}
\fp_compare:nF { \g__hobby_out_angle_fp == \c_inf_fp }
{
\fp_set:Nn \l_hobby_tempa_fp { \g__hobby_out_angle_fp
- \array_get:Nn \g__hobby_angles_array {0}}
% \end{macrocode}
% We want to ensure that these angles lie in the range \((-\pi,\pi]\).
% So if the angle is bigger than \(\pi\), we subtract \(2 \pi\).
% (It shouldn't be that we can get bigger than \(3 \pi\) - check this)
% \begin{macrocode}
\fp_compare:nT {\l_hobby_tempa_fp > \c_pi_fp }
{
\fp_sub:Nn \l_hobby_tempa_fp {2 * \c_pi_fp}
}
% \end{macrocode}
% Similarly, we check to see if the angle is less than \(-\pi\).
% \begin{macrocode}
\fp_compare:nT {\l_hobby_tempa_fp < -\c_pi_fp }
{
\fp_add:Nn \l_hobby_tempa_fp {2 * \c_pi_fp}
}
\array_gput:Nnx \g__hobby_theta_array {0} {\fp_to_tl:N \l_hobby_tempa_fp}
\fp_compare:nT { \g__hobby_in_angle_fp == \c_inf_fp }
{
%^^A \fp_mul:Nn \l_hobby_tempa_fp {-1}
\array_gput:Nnx \g__hobby_phi_array {1}{ \fp_to_tl:N \l_hobby_tempa_fp}
}
}
\fp_compare:nTF { \g__hobby_in_angle_fp == \c_inf_fp }
{
\fp_compare:nT { \g__hobby_out_angle_fp == \c_inf_fp }
{
\array_gput:Nnx \g__hobby_phi_array {1} {0}
\array_gput:Nnx \g__hobby_theta_array {0} {0}
}
}
{
\fp_set:Nn \l_hobby_tempa_fp { - \g__hobby_in_angle_fp + \c_pi_fp
+ (\array_get:Nn \g__hobby_angles_array {0})}
\fp_compare:nT {\l_hobby_tempa_fp > \c_pi_fp }
{
\fp_sub:Nn \l_hobby_tempa_fp {2 * \c_pi_fp}
}
\fp_compare:nT {\l_hobby_tempa_fp < -\c_pi_fp }
{
\fp_add:Nn \l_hobby_tempa_fp {2 * \c_pi_fp}
}
\array_gput:Nnx \g__hobby_phi_array {1}
{\fp_to_tl:N \l_hobby_tempa_fp}
\fp_compare:nT { \g__hobby_out_angle_fp == \c_inf_fp }
{
%^^A \fp_mul:Nn \l_hobby_tempa_fp {-1}
\array_gput:Nnx \g__hobby_theta_array {0}{ \fp_to_tl:N \l_hobby_tempa_fp}
}
}
}
{
% \end{macrocode}
% Got enough points, go on with processing
% \begin{macrocode}
\hobby_compute_path:
}
\hobby_build_path:
}
}
% \end{macrocode}
% \end{macro}
%
%
% \begin{macro}{\hobby_compute_path:}
% This is the path builder where we have enough points to run the algorithm.
% \begin{macrocode}
\cs_new:Nn \hobby_compute_path:
{
% \end{macrocode}
% Our first step is to go through the list of points and compute the distances and angles between successive points.
% Thus \(d_i\) is the distance from \(z_i\) to \(z_{i+1}\) and the angle is the angle of the line from \(z_i\) to \(z_{i+1}\).
% \begin{macrocode}
\int_step_function:nnnN {0} {1} {\g__hobby_npoints_int - 1} \hobby_distangle:n
% \end{macrocode}
%
% For the majority of the code, we're only really interested in the differences of the angles.
% So for each internal point we compute the differences in the angles.
% \begin{macrocode}
\int_step_inline:nnnn {1} {1} {\g__hobby_npoints_int - 1} {
\fp_set:Nx \l_hobby_tempa_fp {
\array_get:Nn \g__hobby_angles_array {##1}
- \array_get:Nn \g__hobby_angles_array {##1 - 1}}
% \end{macrocode}
% We want to ensure that these angles lie in the range \((-\pi,\pi]\).
% So if the angle is bigger than \(\pi\), we subtract \(2 \pi\).
% (It shouldn't be that we can get bigger than \(3 \pi\) - check this.)
% \begin{macrocode}
\fp_compare:nTF {\l_hobby_tempa_fp > \c_pi_fp }
{
\fp_sub:Nn \l_hobby_tempa_fp {2 * \c_pi_fp}
}
{}
% \end{macrocode}
% Similarly, we check to see if the angle is less than \(-\pi\).
% \begin{macrocode}
\fp_compare:nTF {\l_hobby_tempa_fp <= -\c_pi_fp }
{
\fp_add:Nn \l_hobby_tempa_fp {2 * \c_pi_fp}
}
{}
% \end{macrocode}
% \url{https://github.com/loopspace/hobby/issues/11}
%
% If the angle is near \(\pm \pi\), check the previous turn to see whether we should pick \(-\pi\) or \(\pi\).
% \begin{macrocode}
\fp_compare:nT { abs(abs(\l_hobby_tempa_fp) - \c_pi_fp) < 1e-5 } {
\int_compare:nNnTF {##1} > {1} {
% \end{macrocode}
% Read back the turn direction of the previous segment, and use its sign to determine which of \(\pm \pi\) to pick
% \begin{macrocode}
\fp_set:Nx \l_hobby_tempb_fp { \array_get:Nn \g__hobby_psi_array {##1-1}}
\fp_compare:nTF { \l_hobby_tempb_fp >= 0 }
% \end{macrocode}
% Previous turn was left, force left
% \begin{macrocode}
{ \fp_set:Nn \l_hobby_tempa_fp { \c_pi_fp } }
% \end{macrocode}
% Previous turn was right, force right
% \begin{macrocode}
{ \fp_set:Nn \l_hobby_tempa_fp { -\c_pi_fp } }
} {
% \end{macrocode}
% There wasn't a previous turn, so force left
% \begin{macrocode}
\fp_set:Nn \l_hobby_tempa_fp { \c_pi_fp }
}
}
% \end{macrocode}
% The wrapping routine might not get it right at the edges so we add in the override.
% \begin{macrocode}
\array_get:NnNTF \g__hobby_excess_angle_array {##1} \l_tmpa_tl {
\fp_add:Nn \l_hobby_tempa_fp {2 * \c_pi_fp * \l_tmpa_tl}
}{}
% \end{macrocode}
% \begin{macrocode}
\array_gput:Nnx \g__hobby_psi_array {##1}{\fp_to_tl:N \l_hobby_tempa_fp}
}
% \end{macrocode}
%
% Next, we generate the matrix.
% We start with the subdiagonal.
% This is indexed from \(1\) to \(n-1\).
% \begin{macrocode}
\int_step_inline:nnnn {1} {1} {\g__hobby_npoints_int - 1} {
\array_gput:Nnx \g__hobby_matrix_a_array {##1} {\fp_to_tl:n {
\array_get:Nn \g__hobby_tension_in_array {##1}^2
* \array_get:Nn \g__hobby_distances_array {##1}
* \array_get:Nn \g__hobby_tension_in_array {##1 + 1}
}}
}
% \end{macrocode}
%
% Next, we attack main diagonal.
% We might need to adjust the first and last terms, but we'll do that in a minute.
% \begin{macrocode}
\int_step_inline:nnnn {1} {1} {\g__hobby_npoints_int - 1} {
\array_gput:Nnx \g__hobby_matrix_b_array {##1} {\fp_to_tl:n
{(3 * (\array_get:Nn \g__hobby_tension_in_array {##1 + 1}) - 1) *
(\array_get:Nn \g__hobby_tension_out_array {##1})^2 *
(\array_get:Nn \g__hobby_tension_out_array {##1 - 1})
* ( \array_get:Nn \g__hobby_distances_array {##1 - 1})
+
(3 * (\array_get:Nn \g__hobby_tension_out_array {##1 - 1}) - 1)
* (\array_get:Nn \g__hobby_tension_in_array {##1})^2
* (\array_get:Nn \g__hobby_tension_in_array {##1 + 1})
* (\array_get:Nn \g__hobby_distances_array {##1})}
}
}
% \end{macrocode}
%
% Next, the superdiagonal.
% \begin{macrocode}
\int_step_inline:nnnn {1} {1} {\g__hobby_npoints_int - 2} {
\array_gput:Nnx \g__hobby_matrix_c_array {##1} {\fp_to_tl:n
{(\array_get:Nn \g__hobby_tension_in_array {##1})^2
* (\array_get:Nn \g__hobby_tension_in_array {##1 - 1})
* (\array_get:Nn \g__hobby_distances_array {##1 - 1})
}}
}
% \end{macrocode}
%
% Lastly (before the adjustments), the target vector.
% \begin{macrocode}
\int_step_inline:nnnn {1} {1} {\g__hobby_npoints_int - 2} {
\array_gput:Nnx \g__hobby_matrix_d_array {##1} {\fp_to_tl:n
{
- (\array_get:Nn \g__hobby_psi_array {##1 + 1})
* (\array_get:Nn \g__hobby_tension_out_array {##1})^2
* (\array_get:Nn \g__hobby_tension_out_array {##1 - 1})
* (\array_get:Nn \g__hobby_distances_array {##1 - 1})
- (3 * (\array_get:Nn \g__hobby_tension_out_array {##1 - 1}) - 1)
* (\array_get:Nn \g__hobby_psi_array {##1})
* (\array_get:Nn \g__hobby_tension_in_array {##1})^2
* (\array_get:Nn \g__hobby_tension_in_array {##1 + 1})
* (\array_get:Nn \g__hobby_distances_array {##1})
}
}
}
% \end{macrocode}
%
% Next, there are some adjustments at the ends.
% These differ depending on whether the path is open or closed.
% \begin{macrocode}
\bool_if:NTF \g__hobby_closed_bool {
% \end{macrocode}
% Closed path
% \begin{macrocode}
\array_gput:Nnx \g__hobby_matrix_c_array {0} {\fp_to_tl:n {
- (\array_get:Nn \g__hobby_distances_array {\g__hobby_npoints_int - 2})
* (\array_get:Nn \g__hobby_tension_out_array {\g__hobby_npoints_int - 2})
* (\array_get:Nn \g__hobby_tension_out_array {\g__hobby_npoints_int - 1})^2
}}
\array_gput:Nnn \g__hobby_matrix_b_array {0} {1}
\array_gput:Nnn \g__hobby_matrix_d_array {0} {0}
\array_gput:Nnx \g__hobby_matrix_b_array {\g__hobby_npoints_int - 1} {\fp_to_tl:n {
(\array_get:Nn \g__hobby_matrix_b_array {\g__hobby_npoints_int - 1})
+ 1
}}
\array_gput:Nnx \g__hobby_matrix_d_array {\g__hobby_npoints_int - 1} {\fp_to_tl:n {
- (\array_get:Nn \g__hobby_psi_array {1})
* (\array_get:Nn \g__hobby_tension_out_array {\g__hobby_npoints_int -1})^2
* (\array_get:Nn \g__hobby_tension_out_array {\g__hobby_npoints_int -2})
* (\array_get:Nn \g__hobby_distances_array {\g__hobby_npoints_int - 2})
- (3 * (\array_get:Nn \g__hobby_tension_out_array {\g__hobby_npoints_int - 2}) - 1)
* (\array_get:Nn \g__hobby_psi_array {\g__hobby_npoints_int - 1})
* (\array_get:Nn \g__hobby_tension_in_array {\g__hobby_npoints_int - 1})^2