Debian Codegraph @

texmf-dist/tex/generic/pgf/libraries/pgflibrarycurvilinear.code.tex

raw
1 % Copyright 2019 by Till Tantau
2 %
3 % This file may be distributed and/or modified
4 %
5 % 1. under the LaTeX Project Public License and/or
6 % 2. under the GNU Public License.
7 %
8 % See the file doc/generic/pgf/licenses/LICENSE for more details.
9
10 \ProvidesFileRCS{pgflibrarycurvilinear.code.tex}
11
12 %
13 % This file defines commands for computing points in curvilinear
14 % coordinate systems.
15 %
16
17
18 %
19 % Curvilinear coordinate systems defined in terms of a Bezier curve.
20 %
21 % For the following coordinate systems, you must first provide a
22 % Bezier curve (using, as always, four points), which will be set for
23 % the current scope. When the curve is "installed" some expensive
24 % precomputations are done; subsequent calls to
25 % \pgfpointcurvilinearxxx based on this Bezier curve will be
26 % relatively quick.
27 %
28
29
30
31 % Install a Bezier curve
32 %
33 % #1 = start point
34 % #2 = first control point
35 % #3 = second control point
36 % #4 = end point
37 %
38 % Description:
39 %
40 % Subsequent calls to functions like
41 % \pgfpointcurvilinearbezierorthogonal will be relative to the curve
42 % installed using this command. The main job of this macro is a
43 % precomputation for computing length along the curve.
44 % All lengths along the Bezier curve are approximated using a lookup table
45 % of four time/length points. For this, an approximate time for length
46 % 1pt is computed first, and then the length for twice this time, four
47 % times this time, and eight times this time are approximated. A
48 % distance-to-time conversion is then done by a linear interpolation
49 % of distance-to-time for these four points. Note that all of these
50 % computations are not particularly precise, but a compromise trading
51 % speed against precision. Also note that the results will only be
52 % best near the start of the curve and may be far off near the end if
53 % that end is degenerate (second control point very near to end
54 % point).
55 %
56 % Example:
57 %
58 % \pgfsetcurvilinearbeziercurve
59 % {\pgfpoint{0mm}{10mm}}
60 % {\pgfpoint{5.5mm}{10mm}}
61 % {\pgfpoint{10mm}{5.5mm}}
62 % {\pgfpoint{10mm}{0mm}} % nearly a quarter circle
63 % \pgfpointcurvilinearbezierorthogonal{5mm}{5mm}
64 % % should be 5mm along the circle, put at
65 % % distance 15mm from the origin (5mm from the circle line).
66
67 \def\pgfsetcurvilinearbeziercurve#1#2#3#4{%
68 \pgf@process{#1}%
69 \edef\pgf@curvilinear@line@a{\noexpand\pgfqpoint{\the\pgf@x}{\the\pgf@y}}%
70 \pgf@xa=-\pgf@x%
71 \pgf@ya=-\pgf@y%
72 \pgf@process{#2}%
73 \edef\pgf@curvilinear@line@b{\noexpand\pgfqpoint{\the\pgf@x}{\the\pgf@y}}%
74 \pgf@xb=-\pgf@x%
75 \pgf@yb=-\pgf@y%
76 \advance\pgf@x by\pgf@xa%
77 \advance\pgf@y by\pgf@ya%
78 \pgfmathveclen@{\pgf@sys@tonumber\pgf@x}{\pgf@sys@tonumber\pgf@y}%
79 \let\pgf@curvilinear@lenab\pgfmathresult%
80 \pgf@process{#3}%
81 \edef\pgf@curvilinear@line@c{\noexpand\pgfqpoint{\the\pgf@x}{\the\pgf@y}}%
82 \pgf@xc=-\pgf@x%
83 \pgf@yc=-\pgf@y%
84 \advance\pgf@x by\pgf@xb%
85 \advance\pgf@y by\pgf@yb%
86 \pgfmathveclen@{\pgf@sys@tonumber\pgf@x}{\pgf@sys@tonumber\pgf@y}%
87 \let\pgf@curvilinear@lenbc\pgfmathresult%
88 \pgf@process{#4}%
89 \edef\pgf@curvilinear@line@d{\noexpand\pgfqpoint{\the\pgf@x}{\the\pgf@y}}%
90 \advance\pgf@x by\pgf@xc%
91 \advance\pgf@y by\pgf@yc%
92 \pgfmathveclen@{\pgf@sys@tonumber\pgf@x}{\pgf@sys@tonumber\pgf@y}%
93 \let\pgf@curvilinear@lencd\pgfmathresult
94 %
95 \pgf@x=\pgf@curvilinear@lenab pt%
96 \advance\pgf@x by\pgf@curvilinear@lenbc pt%
97 \advance\pgf@x by\pgf@curvilinear@lencd pt%
98 \pgfmathreciprocal@{\pgf@sys@tonumber\pgf@x}%
99 \pgf@curvilinear@time@a\pgfmathresult pt%
100 \pgf@process{\pgfpointcurveattime{\pgf@curvilinear@time@a}{\pgf@curvilinear@line@a}{\pgf@curvilinear@line@b}{\pgf@curvilinear@line@c}{\pgf@curvilinear@line@d}}%
101 \pgf@xb=-\pgf@x%
102 \pgf@yb=-\pgf@y%
103 \advance\pgf@x by\pgf@xa%
104 \advance\pgf@y by\pgf@ya%
105 \pgfmathveclen@{\pgf@sys@tonumber\pgf@x}{\pgf@sys@tonumber\pgf@y}%
106 \pgf@curvilinear@length@a\pgfmathresult pt%
107 \ifdim\pgf@curvilinear@length@a>1pt\relax%
108 % Ok, too large, let us make this smaller
109 \pgfmathdivide@{\pgf@sys@tonumber\pgf@curvilinear@time@a}{\pgf@sys@tonumber\pgf@curvilinear@length@a}%
110 \pgf@curvilinear@time@a\pgfmathresult pt%
111 \pgf@process{\pgfpointcurveattime{\pgf@curvilinear@time@a}{\pgf@curvilinear@line@a}{\pgf@curvilinear@line@b}{\pgf@curvilinear@line@c}{\pgf@curvilinear@line@d}}
112 \pgf@xb=-\pgf@x%
113 \pgf@yb=-\pgf@y%
114 \advance\pgf@x by\pgf@xa%
115 \advance\pgf@y by\pgf@ya%
116 \pgfmathveclen@{\pgf@sys@tonumber\pgf@x}{\pgf@sys@tonumber\pgf@y}%
117 \pgf@curvilinear@length@a\pgfmathresult pt%
118 \fi%
119 % Compute three positions:
120 \pgf@process{\pgfpointcurveattime{2\pgf@curvilinear@time@a}{\pgf@curvilinear@line@a}{\pgf@curvilinear@line@b}{\pgf@curvilinear@line@c}{\pgf@curvilinear@line@d}}
121 \pgf@xa=-\pgf@x%
122 \pgf@ya=-\pgf@y%
123 \advance\pgf@x by\pgf@xb%
124 \advance\pgf@y by\pgf@yb%
125 \pgfmathveclen@{\pgf@sys@tonumber\pgf@x}{\pgf@sys@tonumber\pgf@y}%
126 \pgf@curvilinear@length@b\pgfmathresult pt%
127 \advance\pgf@curvilinear@length@b by\pgf@curvilinear@length@a%
128 \pgf@process{\pgfpointcurveattime{4\pgf@curvilinear@time@a}{\pgf@curvilinear@line@a}{\pgf@curvilinear@line@b}{\pgf@curvilinear@line@c}{\pgf@curvilinear@line@d}}
129 \pgf@xb=-\pgf@x%
130 \pgf@yb=-\pgf@y%
131 \advance\pgf@x by\pgf@xa%
132 \advance\pgf@y by\pgf@ya%
133 \pgfmathveclen@{\pgf@sys@tonumber\pgf@x}{\pgf@sys@tonumber\pgf@y}%
134 \pgf@curvilinear@length@c\pgfmathresult pt%
135 \advance\pgf@curvilinear@length@c by\pgf@curvilinear@length@b%
136 \pgf@process{\pgfpointcurveattime{8\pgf@curvilinear@time@a}{\pgf@curvilinear@line@a}{\pgf@curvilinear@line@b}{\pgf@curvilinear@line@c}{\pgf@curvilinear@line@d}}
137 \advance\pgf@x by\pgf@xb%
138 \advance\pgf@y by\pgf@yb%
139 \pgfmathveclen@{\pgf@sys@tonumber\pgf@x}{\pgf@sys@tonumber\pgf@y}%
140 \pgf@curvilinear@length@d\pgfmathresult pt%
141 \advance\pgf@curvilinear@length@d by\pgf@curvilinear@length@c%
142 \let\pgf@curvilinear@comp@a\pgf@curvilinear@comp@a@initial%
143 \let\pgf@curvilinear@comp@b\pgf@curvilinear@comp@b@initial%
144 \let\pgf@curvilinear@comp@c\pgf@curvilinear@comp@c@initial%
145 \let\pgf@curvilinear@comp@d\pgf@curvilinear@comp@d@initial%
146 \let\pgf@curvilinear@comp@e\pgf@curvilinear@comp@e@initial%
147 \let\pgf@curvilinear@point\pgf@curvilinear@curve@point%
148 }%
149 \newdimen\pgf@curvilinear@time@a
150 \newdimen\pgf@curvilinear@length@a
151 \newdimen\pgf@curvilinear@length@b
152 \newdimen\pgf@curvilinear@length@c
153 \newdimen\pgf@curvilinear@length@d
154
155
156 \def\pgf@curvilinear@comp@a@initial{%
157 \pgfmathdivide@{\pgf@sys@tonumber\pgf@curvilinear@time@a}{\pgf@sys@tonumber\pgf@curvilinear@length@a}%
158 \let\pgf@curvilinear@quot@a\pgfmathresult%
159 \let\pgf@curvilinear@comp@a\pgf@curvilinear@comp@a@cont%
160 \pgf@curvilinear@comp@a@cont%
161 }%
162 \def\pgf@curvilinear@comp@a@cont{%
163 \pgf@x\pgf@curvilinear@quot@a\pgf@x%
164 }%
165
166 \def\pgf@curvilinear@comp@b@initial{%
167 \pgf@y=\pgf@curvilinear@length@b%
168 \advance\pgf@y by-\pgf@curvilinear@length@a%
169 \pgfmathdivide@{\pgf@sys@tonumber\pgf@curvilinear@time@a}{\pgf@sys@tonumber\pgf@y}%
170 \let\pgf@curvilinear@quot@b\pgfmathresult%
171 \pgf@y\pgfmathresult\pgf@curvilinear@length@a%
172 \pgf@y-\pgf@y%
173 \advance\pgf@y by\pgf@curvilinear@time@a%
174 \edef\pgf@curvilinear@correct@b{\pgf@sys@tonumber\pgf@y}%
175 \let\pgf@curvilinear@comp@b\pgf@curvilinear@comp@b@cont%
176 \pgf@curvilinear@comp@b@cont%
177 }%
178 \def\pgf@curvilinear@comp@b@cont{%
179 \pgf@x\pgf@curvilinear@quot@b\pgf@x%
180 \advance\pgf@x by\pgf@curvilinear@correct@b pt%
181 }%
182
183 \def\pgf@curvilinear@comp@c@initial{%
184 \pgf@y=\pgf@curvilinear@length@c%
185 \advance\pgf@y by-\pgf@curvilinear@length@b%
186 \[email protected]\pgf@y%
187 \pgfmathdivide@{\pgf@sys@tonumber\pgf@curvilinear@time@a}{\pgf@sys@tonumber\pgf@y}%
188 \let\pgf@curvilinear@quot@c\pgfmathresult%
189 \pgf@y\pgf@curvilinear@quot@c\pgf@curvilinear@length@b%
190 \pgf@y-\pgf@y%
191 \advance\pgf@y by2\pgf@curvilinear@time@a%
192 \edef\pgf@curvilinear@correct@c{\pgf@sys@tonumber\pgf@y}%
193 \let\pgf@curvilinear@comp@c\pgf@curvilinear@comp@c@cont%
194 \pgf@curvilinear@comp@c@cont%
195 }%
196 \def\pgf@curvilinear@comp@c@cont{%
197 \pgf@x\pgf@curvilinear@quot@c\pgf@x%
198 \advance\pgf@x by\pgf@curvilinear@correct@c pt%
199 }%
200
201 \def\pgf@curvilinear@comp@d@initial{%
202 \pgf@y=\pgf@curvilinear@length@d%
203 \advance\pgf@y by-\pgf@curvilinear@length@c%
204 \[email protected]\pgf@y%
205 \pgfmathdivide@{\pgf@sys@tonumber\pgf@curvilinear@time@a}{\pgf@sys@tonumber\pgf@y}%
206 \let\pgf@curvilinear@quot@d\pgfmathresult%
207 \pgf@y\pgf@curvilinear@quot@d\pgf@curvilinear@length@c%
208 \pgf@y-\pgf@y%
209 \advance\pgf@y by4\pgf@curvilinear@time@a%
210 \edef\pgf@curvilinear@correct@d{\pgf@sys@tonumber\pgf@y}%
211 \let\pgf@curvilinear@comp@d\pgf@curvilinear@comp@d@cont%
212 \pgf@curvilinear@comp@d@cont%
213 }%
214 \def\pgf@curvilinear@comp@d@cont{%
215 \pgf@x\pgf@curvilinear@quot@d\pgf@x%
216 \advance\pgf@x by\pgf@curvilinear@correct@d pt%
217 }%
218
219 \def\pgf@curvilinear@comp@e@initial{%
220 \pgfmathmultiply@{8}{\pgf@sys@tonumber\pgf@curvilinear@time@a}%
221 \expandafter\pgfmathdivide@\expandafter{\pgfmathresult}{\pgf@sys@tonumber\pgf@curvilinear@length@d}%
222 \let\pgf@curvilinear@quot@e\pgfmathresult%
223 \let\pgf@curvilinear@comp@e\pgf@curvilinear@comp@e@cont%
224 \pgf@curvilinear@comp@e@cont%
225 }%
226 \def\pgf@curvilinear@comp@e@cont{%
227 \pgf@x\pgf@curvilinear@quot@e\pgf@x%
228 }%
229
230
231 % Convert a distance into a time
232 %
233 % #1 = a distance
234 %
235 % Description:
236 %
237 % After having called \pgfsetcurvilinearbeziercurve, you can use this
238 % macro to convert a distance into a time along the curve set in that
239 % command. The result will be stored in \pgf@x. It will only be
240 % reasonably precise for small nonnegative #1 (in particular, #1
241 % should not be more than about half the length of the curve).
242
243 \def\pgfcurvilineardistancetotime#1{%
244 \pgfmathsetlength{\pgf@x}{#1}%
245 \ifdim\pgf@x<\pgf@curvilinear@length@c\relax%
246 \ifdim\pgf@x<\pgf@curvilinear@length@a\relax%
247 \pgf@curvilinear@comp@a%
248 \else\ifdim\pgf@x<\pgf@curvilinear@length@b\relax%
249 \pgf@curvilinear@comp@b%
250 \else%
251 \pgf@curvilinear@comp@c%
252 \fi\fi%
253 \else\ifdim\pgf@x<\pgf@curvilinear@length@d\relax%
254 \pgf@curvilinear@comp@d%
255 \else%
256 \pgf@curvilinear@comp@e%
257 \fi\fi%
258 }%
259
260
261
262 % Compute a "Bezier-orthogonal" point for use in a nonlinear transformation.
263 %
264 % #1 = x
265 % #2 = y
266 %
267 % Description:
268 %
269 % In this coordinate system, the x-axis "goes along" the Bezier curve
270 % installed using \pgfsetcurvilinearbeziercurve
271 % and the y-axis is always perpendicular to the (Bezier) curve at the
272 % given x-coordinate. Formally, given a pair (x,y), let B(x) be the point on
273 % the Bezier curve B at distance x from the start of the curve. Let
274 % T(x) be the tangent of B at B(x) and let P(x) be the (normalized)
275 % vector perpendicular to T(x). Then (x,y) would be mapped to B(x) +
276 % y*P(x). As an example, if B is a circle, then the corresponding
277 % curvilinear coordinate system is (essentially, except for an offset in
278 % the y value) the polar coordinate system.
279 %
280 % In addition to setting \pgf@x and \pgf@y, \pgf@xa/ya will be set to
281 % a tangent along the curve at the given point and \pgf@xb/yb to a
282 % tangent orthogonal to the curve.
283
284 \def\pgfpointcurvilinearbezierorthogonal#1#2{%
285 \pgfmathsetmacro\pgf@curvilinear@yfactor{#2}%
286 \pgfcurvilineardistancetotime{#1}%
287 \pgfpointcurveattime{\pgf@x}{\pgf@curvilinear@line@a}{\pgf@curvilinear@line@b}{\pgf@curvilinear@line@c}{\pgf@curvilinear@line@d}
288 \pgf@xc\pgf@x% save
289 \pgf@yc\pgf@y% save
290 % compute normal:
291 \advance\pgf@xb by-\pgf@xa%
292 \advance\pgf@yb by-\pgf@ya%
293 \ifdim\pgf@xb<0.0001pt\ifdim\pgf@xb>-0.0001pt\ifdim\pgf@yb<0.0001pt\ifdim\pgf@yb>-0.0001pt\pgf@diff@curvi@ac\fi\fi\fi\fi
294 \pgf@process{\pgfpointnormalised{\pgf@x=\pgf@yb\pgf@y=-\pgf@xb}}
295 \pgf@x\pgf@curvilinear@yfactor\pgf@x%
296 \pgf@y\pgf@curvilinear@yfactor\pgf@y%
297 \advance\pgf@x by\pgf@xc%
298 \advance\pgf@y by\pgf@yc%
299 }%
300
301 \def\pgf@diff@curvi@ac{%
302 \pgf@curvilinear@line@a%
303 \pgf@xa\pgf@x\pgf@ya\pgf@y%
304 \pgf@curvilinear@line@c%
305 \pgf@xb\pgf@x\pgf@yb\pgf@y%
306 \advance\pgf@xb by-\pgf@xa%
307 \advance\pgf@yb by-\pgf@ya%
308 \ifdim\pgf@xb<0.0001pt\ifdim\pgf@xb>-0.0001pt\ifdim\pgf@yb<0.0001pt\ifdim\pgf@yb>-0.0001pt% still degenerate!
309 \pgf@curvilinear@line@d%
310 \pgf@xb\pgf@x\pgf@yb\pgf@y%
311 \advance\pgf@xb by-\pgf@xa%
312 \advance\pgf@yb by-\pgf@ya%
313 \fi\fi\fi\fi%
314 \pgf@xb-\pgf@xb%
315 \pgf@yb-\pgf@yb%
316 }%
317
318
319
320 % Compute a "Bezier-polar" point.
321 %
322 % #1 = x
323 % #2 = y
324 %
325 % Description:
326 %
327 % Let (r:d) be the point (x,y) in polar coordinates, that is, r is the
328 % angle and d the distance of (x,y) to the origin. The point returned
329 % by \pgfpointcurvilinearbezierpolar is now defined as follows: First,
330 % we compute that point at distance d along the Bezier curve B. Let
331 % B(d) be this point. Then, we rotate this point around the start of
332 % the curve (B(0)) by r degrees.
333 %
334 % As an example, consider a triangle with one tip at the origin and
335 % the other tips as (4cm,3cm) and (4cm,-3cm). Then this triangle would be
336 % transformed as follows: We take the first 5cm of the Bezier curve
337 % and rotate it by roughly 37 degrees to the left and by 37 degrees to
338 % the right.
339 %
340 % Note that this command is pretty expensive.
341
342 \def\pgfpointcurvilinearbezierpolar#1#2{%
343 \pgfmathsetlength\pgfutil@tempdima{#1}%
344 \pgfmathsetlength\pgfutil@tempdimb{#2}%
345 % Compute angle:
346 \pgfpointnormalised{\pgfqpoint{\pgfutil@tempdima}{\pgfutil@tempdimb}}%
347 \ifdim\pgfutil@tempdimb=0pt\relax\ifdim\pgfutil@tempdima=0pt\pgf@x1pt\pgf@y0pt\fi\fi%
348 \ifdim\pgfutil@tempdima<0pt%
349 \pgf@x-\pgf@x%
350 \pgf@y-\pgf@y%
351 \fi%
352 \pgf@ya=-\pgf@y%
353 \pgfsettransformentries%
354 {\pgf@sys@tonumber\pgf@x}{\pgf@sys@tonumber\pgf@y}%
355 {\pgf@sys@tonumber\pgf@ya}{\pgf@sys@tonumber\pgf@x}{0pt}{0pt}%
356 \pgftransformshift{\pgfpointscale{-1}{\pgf@curvilinear@line@a}}%
357 \pgfmathveclen@{\pgf@sys@tonumber\pgfutil@tempdima}{\pgf@sys@tonumber\pgfutil@tempdimb}%
358 \ifdim\pgfutil@tempdima<0pt%
359 \edef\pgfmathresult{-\pgfmathresult}%
360 \fi%
361 \pgfcurvilineardistancetotime{\pgfmathresult}%
362 % Now, transform:
363 \pgf@process{%
364 \pgfpointadd{%
365 \pgfpointtransformed{%
366 \pgfpointcurveattime%
367 {\pgf@x}%
368 {\pgf@curvilinear@line@a}%
369 {\pgf@curvilinear@line@b}%
370 {\pgf@curvilinear@line@c}%
371 {\pgf@curvilinear@line@d}%
372 }%
373 }%
374 \pgf@curvilinear@line@a%
375 }%
376 }%
377
378
379 \endinput