| 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{pgfmodulenonlineartransformations.code.tex} |
| 11 |
|
| 12 |
|
| 13 |
% |
| 14 |
% This file defines commands for nonlinear coordinate systems. This is |
| 15 |
% needed, for instance, for bend arrow heads. |
| 16 |
% |
| 17 |
% |
| 18 |
% Non-linear coordinate systems transform coordinates, in addition to |
| 19 |
% the normal linear transformations, in a, well, non-linear way. An |
| 20 |
% example are polar coordinates. |
| 21 |
% |
| 22 |
% Like the linear transformations, non-linear transformations are |
| 23 |
% applied on a low level and they are integrated into the whole path |
| 24 |
% construction mechanism. So, while \pgfpointpolar also "does |
| 25 |
% something nonlinear", it just provides a way of computing a |
| 26 |
% coordinate. In contrast, installing a polar transformation will |
| 27 |
% transform everything being drawn using the \pgfpath... commands an |
| 28 |
% will even turn a straight line into a circle. |
| 29 |
% |
| 30 |
% Since computing non-linear transformations is expensive, they are |
| 31 |
% only applied "if really necessary". |
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
% Return a transformed point |
| 37 |
% |
| 38 |
% #1 = a point |
| 39 |
% |
| 40 |
% Description: |
| 41 |
% |
| 42 |
% Applies the current transformation and the current nonlinear |
| 43 |
% transformation to the given point and returns the result in \pgf@x/y. |
| 44 |
|
| 45 |
\def\pgfpointtransformednonlinear#1{% |
| 46 |
\pgf@process{% |
| 47 |
#1% |
| 48 |
\pgf@pos@transform@glob% |
| 49 |
\pgf@nlt@list% |
| 50 |
} |
| 51 |
}% |
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
% Approximate the nonlinear translation locally |
| 57 |
% |
| 58 |
% Description: |
| 59 |
% |
| 60 |
% Does a "local synchronization" of the nonlinear transformation with |
| 61 |
% the linear transformation regarding the translation part of the |
| 62 |
% current nonlinear transformation. The *linear* transformation will be |
| 63 |
% updated so that it translates things to the point to which the |
| 64 |
% nonlinear transformation transforms the origin. The nonlinear |
| 65 |
% transformation will be reset. |
| 66 |
% |
| 67 |
% The net effect is that you can now draw things near the origin as |
| 68 |
% before, but with just a linear transformation in force. Note that |
| 69 |
% the coordinate system will not be rotated, only a shift is done (use |
| 70 |
% the "real" local sync for a full sync). |
| 71 |
|
| 72 |
\def\pgfapproximatenonlineartranslation{% |
| 73 |
\ifx\pgf@nlt@list\pgfutil@empty% |
| 74 |
\else% |
| 75 |
\pgfpointtransformednonlinear{\pgfpointorigin}% |
| 76 |
\pgf@pt@x=\pgf@x% |
| 77 |
\pgf@pt@y=\pgf@y% |
| 78 |
\let\pgf@nlt@list\pgfutil@empty% |
| 79 |
\let\pgf@nlt@moveto\pgf@lt@moveto% |
| 80 |
\let\pgf@nlt@lineto\pgf@lt@lineto% |
| 81 |
\let\pgf@nlt@curveto\pgf@lt@curveto% |
| 82 |
\let\pgf@nlt@closepath\pgf@lt@closepath% |
| 83 |
\fi% |
| 84 |
}% |
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
% Local sync translation |
| 89 |
% |
| 90 |
% Description: |
| 91 |
% |
| 92 |
% Does a "complete local synchronization" of the nonlinear |
| 93 |
% transformation with the linear transformation. The *linear* |
| 94 |
% transformation will be updated so that it transforms things to the |
| 95 |
% point to which the nonlinear transformation transforms the |
| 96 |
% origin. The nonlinear transformation will be reset. |
| 97 |
% |
| 98 |
% The net effect is that you can now draw things near the origin as |
| 99 |
% before, but with just a linear transformation in force. |
| 100 |
|
| 101 |
\def\pgfapproximatenonlineartransformation{% |
| 102 |
\ifx\pgf@nlt@list\pgfutil@empty% |
| 103 |
\else% |
| 104 |
\pgfpointtransformednonlinear{\pgfpoint{1pt}{0pt}}% |
| 105 |
\pgf@xa\pgf@x |
| 106 |
\pgf@ya\pgf@y |
| 107 |
\pgfpointtransformednonlinear{\pgfpoint{0pt}{1pt}}% |
| 108 |
\pgf@xb\pgf@x |
| 109 |
\pgf@yb\pgf@y |
| 110 |
\pgfpointtransformednonlinear{\pgfpointorigin}% |
| 111 |
\advance\pgf@xa by-\pgf@x |
| 112 |
\advance\pgf@xb by-\pgf@x |
| 113 |
\advance\pgf@ya by-\pgf@y |
| 114 |
\advance\pgf@yb by-\pgf@y |
| 115 |
\pgfsettransformentries{\pgf@sys@tonumber\pgf@xa}{\pgf@sys@tonumber\pgf@ya}{\pgf@sys@tonumber\pgf@xb}{\pgf@sys@tonumber\pgf@yb}{\pgf@x}{\pgf@y}% |
| 116 |
\let\pgf@nlt@list\pgfutil@empty% |
| 117 |
\let\pgf@nlt@moveto\pgf@lt@moveto% |
| 118 |
\let\pgf@nlt@lineto\pgf@lt@lineto% |
| 119 |
\let\pgf@nlt@curveto\pgf@lt@curveto% |
| 120 |
\let\pgf@nlt@closepath\pgf@lt@closepath% |
| 121 |
\fi% |
| 122 |
}% |
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
% Adds a non-linear transformation to the current list of |
| 127 |
% transformations. |
| 128 |
% |
| 129 |
% #1 = code |
| 130 |
% |
| 131 |
% Description: |
| 132 |
% |
| 133 |
% When the code #1 is called, \pgf@x and \pgf@y will be set to some |
| 134 |
% values, let us call this the point p. The nonlinear transformation |
| 135 |
% will now map this point to a new point f(p). The coordinates of this |
| 136 |
% point should be returned in \pgf@x and \pgf@y. Furthermore, consider |
| 137 |
% the two "unit tangents" (going in $x$-direction and in |
| 138 |
% $y$-direction) at point p. The nonlinear transformation will map |
| 139 |
% these to new tangents, t_x and t_y. The (not necessarily normalized) |
| 140 |
% vectors t_x and t_y should be returned in pgf@xa and -ya and pgf@xb |
| 141 |
% and pgf@yb. The macro should not do any global assignments except to |
| 142 |
% \pgf@x and \pgf@y, but may do arbitrary local assignments. |
| 143 |
% |
| 144 |
% The effect of installing a new transformation is the following: As |
| 145 |
% long as there is at least one nonlinear transformation installed, |
| 146 |
% whenever the path construction commands add a curve or line to the |
| 147 |
% softpath, the line or curve will be "bend" by applying all of the |
| 148 |
% nonlinear transformation installed. In particular, straight lines |
| 149 |
% will be replaced by curves. |
| 150 |
% |
| 151 |
% Note that the normal linear transformation are always applied before |
| 152 |
% any nonlinear transformations are applied. |
| 153 |
|
| 154 |
\def\pgftransformnonlinear#1{% |
| 155 |
\expandafter\def\expandafter\pgf@nlt@list\expandafter{\pgf@nlt@list#1}% |
| 156 |
\let\pgf@nlt@moveto\pgf@nlt@moveto@nlt |
| 157 |
\let\pgf@nlt@lineto\pgf@nlt@lineto@nlt |
| 158 |
\let\pgf@nlt@curveto\pgf@nlt@curveto@nlt |
| 159 |
\let\pgf@nlt@closepath\pgf@nlt@closepath@nlt |
| 160 |
}% |
| 161 |
|
| 162 |
\def\pgf@nlt@moveto@nlt#1#2{% |
| 163 |
{% |
| 164 |
\pgfutil@tempdima#1% |
| 165 |
\pgfutil@tempdimb#2% |
| 166 |
\pgf@x\pgfutil@tempdima% |
| 167 |
\pgf@y\pgfutil@tempdimb% |
| 168 |
\xdef\pgf@nlt@last@moveto@orig{{\the\pgf@x}{\the\pgf@y}}% |
| 169 |
\pgf@nlt@list% |
| 170 |
\xdef\pgf@nlt@last@moveto@trans{{\the\pgf@x}{\the\pgf@y}}% |
| 171 |
\xdef\pgf@nlt@last@moveto@xaxis{{\the\pgf@xa}{\the\pgf@ya}}% |
| 172 |
\xdef\pgf@nlt@last@moveto@yaxis{{\the\pgf@xb}{\the\pgf@yb}}% |
| 173 |
\pgf@protocolsizes{\pgf@x}{\pgf@y}% |
| 174 |
\pgfsyssoftpath@moveto{\the\pgf@x}{\the\pgf@y}% |
| 175 |
\global\let\pgf@nlt@last@coord@orig\pgf@nlt@last@moveto@orig% |
| 176 |
\global\let\pgf@nlt@last@coord@trans\pgf@nlt@last@moveto@trans% |
| 177 |
\global\let\pgf@nlt@last@coord@xaxis\pgf@nlt@last@moveto@xaxis% |
| 178 |
\global\let\pgf@nlt@last@coord@yaxis\pgf@nlt@last@moveto@yaxis% |
| 179 |
}% |
| 180 |
}% |
| 181 |
|
| 182 |
|
| 183 |
\def\pgf@nlt@lineto@nlt#1#2{% |
| 184 |
{% |
| 185 |
\edef\pgf@temp{\pgf@xc\the#1\pgf@yc\the#2}% |
| 186 |
\pgf@temp% |
| 187 |
\expandafter\pgfqpoint\pgf@nlt@last@coord@orig% |
| 188 |
\expandafter\pgf@nlt@set@temps\pgf@nlt@last@coord@trans% |
| 189 |
% Test, whether the points are quite near: |
| 190 |
\pgf@xa\pgf@x\advance\pgf@xa by-\pgf@xc% |
| 191 |
\pgf@ya\pgf@y\advance\pgf@ya by-\pgf@yc% |
| 192 |
\pgfutil@tempswafalse% |
| 193 |
\ifdim\pgf@xa<0.1pt\relax\ifdim\pgf@xa>-0.1pt\relax\ifdim\pgf@ya<0.1pt\relax\ifdim\pgf@ya>-0.1pt\relax\pgfutil@tempswatrue\fi\fi\fi\fi% |
| 194 |
\ifpgfutil@tempswa% |
| 195 |
\pgf@x\pgf@xc\pgf@y\pgf@yc% |
| 196 |
\xdef\pgf@nlt@last@coord@orig{{\the\pgf@x}{\the\pgf@y}}% |
| 197 |
\pgf@process{\pgf@nlt@list}% |
| 198 |
\xdef\pgf@nlt@last@coord@trans{{\the\pgf@x}{\the\pgf@y}}% |
| 199 |
\xdef\pgf@nlt@last@coord@xaxis{{\the\pgf@xa}{\the\pgf@ya}}% |
| 200 |
\xdef\pgf@nlt@last@coord@yaxis{{\the\pgf@xb}{\the\pgf@yb}}% |
| 201 |
\pgf@protocolsizes{\pgf@x}{\pgf@y}% |
| 202 |
\pgfsyssoftpath@lineto{\the\pgf@x}{\the\pgf@y}% |
| 203 |
\else% |
| 204 |
% Compute support points |
| 205 |
\pgf@xb=.333333\pgf@x% |
| 206 |
\advance\pgf@xb by.666666\pgf@xc% |
| 207 |
\pgf@yb=.333333\pgf@y% |
| 208 |
\advance\pgf@yb by.666666\pgf@yc% |
| 209 |
\pgf@xa=.333333\pgf@xc% |
| 210 |
\advance\pgf@xa by.666666\pgf@x% |
| 211 |
\pgf@ya=.333333\pgf@yc% |
| 212 |
\advance\pgf@ya by.666666\pgf@y% |
| 213 |
\pgf@nlt@inner@curve% |
| 214 |
\fi% |
| 215 |
}% |
| 216 |
}% |
| 217 |
|
| 218 |
\def\pgf@nlt@set@temps#1#2{% |
| 219 |
\pgfutil@tempdima#1\pgfutil@tempdimb#2% |
| 220 |
}% |
| 221 |
|
| 222 |
\def\pgf@nlt@curveto@nlt#1#2#3#4#5#6{% |
| 223 |
{% |
| 224 |
\edef\pgf@temp{\pgf@xa\the#1\pgf@ya\the#2\pgf@xb\the#3\pgf@yb\the#4\pgf@xc\the#5\pgf@yc\the#6}% |
| 225 |
\pgf@temp% |
| 226 |
\pgf@nlt@inner@curve% |
| 227 |
}% |
| 228 |
}% |
| 229 |
|
| 230 |
\newdimen\pgftransformnonlinearflatness |
| 231 |
\pgftransformnonlinearflatness=5pt |
| 232 |
\def\pgfsettransformnonlinearflatness#1{\pgfmathsetlength\pgftransformnonlinearflatness{#1}}% |
| 233 |
|
| 234 |
|
| 235 |
|
| 236 |
% Compute a curve from \pgf@nlt@last@coord@orig to \pgf@xc/\pgf@yc via the |
| 237 |
% controls \pgf@xa/\pgf@ya and \pgf@xb/\pgf@yb. |
| 238 |
\def\pgf@nlt@inner@curve{% |
| 239 |
\expandafter\pgfqpoint\pgf@nlt@last@coord@orig% |
| 240 |
% Save delta of supports: |
| 241 |
\pgfutil@tempdima\pgf@xa\advance\pgfutil@tempdima by-\pgf@xb |
| 242 |
\pgfutil@tempdimb\pgf@ya\advance\pgfutil@tempdimb by-\pgf@yb |
| 243 |
\begingroup |
| 244 |
% Replace supports by relative supports |
| 245 |
\advance\pgf@xa by-\pgf@x% |
| 246 |
\advance\pgf@ya by-\pgf@y% |
| 247 |
\advance\pgf@xb by-\pgf@xc% |
| 248 |
\advance\pgf@yb by-\pgf@yc% |
| 249 |
% |
| 250 |
% Now, test whether the flatness is satisfied: |
| 251 |
% |
| 252 |
\pgfutil@tempswafalse |
| 253 |
\ifdim\pgfutil@tempdima>\pgftransformnonlinearflatness\pgfutil@tempswatrue\fi% |
| 254 |
\ifdim\pgfutil@tempdima<-\pgftransformnonlinearflatness\pgfutil@tempswatrue\fi% |
| 255 |
\ifdim\pgfutil@tempdimb>\pgftransformnonlinearflatness\pgfutil@tempswatrue\fi% |
| 256 |
\ifdim\pgfutil@tempdimb<-\pgftransformnonlinearflatness\pgfutil@tempswatrue\fi% |
| 257 |
\ifdim\pgf@xa>\pgftransformnonlinearflatness\pgfutil@tempswatrue\fi% |
| 258 |
\ifdim\pgf@xa<-\pgftransformnonlinearflatness\pgfutil@tempswatrue\fi% |
| 259 |
\ifdim\pgf@ya>\pgftransformnonlinearflatness\pgfutil@tempswatrue\fi% |
| 260 |
\ifdim\pgf@ya<-\pgftransformnonlinearflatness\pgfutil@tempswatrue\fi% |
| 261 |
\ifdim\pgf@xb>\pgftransformnonlinearflatness\pgfutil@tempswatrue\fi% |
| 262 |
\ifdim\pgf@xb<-\pgftransformnonlinearflatness\pgfutil@tempswatrue\fi% |
| 263 |
\ifdim\pgf@yb>\pgftransformnonlinearflatness\pgfutil@tempswatrue\fi% |
| 264 |
\ifdim\pgf@yb<-\pgftransformnonlinearflatness\pgfutil@tempswatrue\fi% |
| 265 |
\ifpgfutil@tempswa% |
| 266 |
\endgroup% Undo the adjustments... |
| 267 |
{% |
| 268 |
\edef\pgf@orig@xya{\pgf@xa\the\pgf@xa\pgf@ya\the\pgf@ya} |
| 269 |
\edef\pgf@orig@xyb{\pgf@xb\the\pgf@xb\pgf@yb\the\pgf@yb} |
| 270 |
\edef\pgf@orig@xyc{\pgf@xc\the\pgf@xc\pgf@yc\the\pgf@yc} |
| 271 |
\pgfpointcurveattime{.5}% |
| 272 |
{\expandafter\pgfqpoint\pgf@nlt@last@coord@orig}% |
| 273 |
{\pgf@x\pgf@xa\pgf@y\pgf@ya}% |
| 274 |
{\pgf@x\pgf@xb\pgf@y\pgf@yb}% |
| 275 |
{\pgf@x\pgf@xc\pgf@y\pgf@yc}% |
| 276 |
% First new curve: |
| 277 |
% Start is at last@coord@orig, |
| 278 |
% first support is at last@coord@orig*t + s*(original xa/ya) |
| 279 |
% second support is at xb/yb |
| 280 |
% target is at x/y |
| 281 |
{% |
| 282 |
% Target: |
| 283 |
\pgf@xc\pgf@x\pgf@yc\pgf@y |
| 284 |
% First support: |
| 285 |
\expandafter\pgfqpoint\pgf@nlt@last@coord@orig% |
| 286 |
\pgf@orig@xya% |
| 287 |
\pgf@xa\pgf@time@s\pgf@xa\advance\pgf@xa by\pgf@time@t\pgf@x% |
| 288 |
\pgf@ya\pgf@time@s\pgf@ya\advance\pgf@ya by\pgf@time@t\pgf@y% |
| 289 |
\pgf@nlt@inner@curve% |
| 290 |
}% |
| 291 |
% Second new curve: |
| 292 |
% Start is at (new) last@coord@orig |
| 293 |
% first support is at xa/ya |
| 294 |
% second support is at t*(original xb/yb) + s*(original xc/yc) |
| 295 |
% target is at original xc/yc |
| 296 |
{% |
| 297 |
\pgf@orig@xyb% |
| 298 |
\pgf@orig@xyc% |
| 299 |
\pgf@xb\pgf@time@t\pgf@xb\advance\pgf@xb by\pgf@time@s\pgf@xc% |
| 300 |
\pgf@yb\pgf@time@t\pgf@yb\advance\pgf@yb by\pgf@time@s\pgf@yc% |
| 301 |
\pgf@nlt@inner@curve% |
| 302 |
}% |
| 303 |
}% |
| 304 |
\else% |
| 305 |
\endgroup% Snap back... |
| 306 |
\pgf@nlt@do@inner@curve% |
| 307 |
\fi% |
| 308 |
}% |
| 309 |
|
| 310 |
\def\pgf@nlt@do@inner@curve{% |
| 311 |
\pgf@process{\pgf@x\pgf@xa\pgf@y\pgf@ya\pgf@nlt@list}% |
| 312 |
\pgf@xa\pgf@x\pgf@ya\pgf@y% |
| 313 |
\pgf@process{\pgf@x\pgf@xb\pgf@y\pgf@yb\pgf@nlt@list}% |
| 314 |
\pgf@xb\pgf@x\pgf@yb\pgf@y% |
| 315 |
\xdef\pgf@nlt@last@coord@orig{{\the\pgf@xc}{\the\pgf@yc}}% |
| 316 |
\pgf@process{\pgf@x\pgf@xc\pgf@y\pgf@yc\pgf@nlt@list}% |
| 317 |
\pgf@xc\pgf@x\pgf@yc\pgf@y% |
| 318 |
\xdef\pgf@nlt@last@coord@trans{{\the\pgf@xc}{\the\pgf@yc}}% |
| 319 |
\pgf@protocolsizes{\pgf@xa}{\pgf@ya}% |
| 320 |
\pgf@protocolsizes{\pgf@xb}{\pgf@yb}% |
| 321 |
\pgf@protocolsizes{\pgf@xc}{\pgf@yc}% |
| 322 |
\pgfsyssoftpath@curveto{\the\pgf@xa}{\the\pgf@ya}{\the\pgf@xb}{\the\pgf@yb}{\the\pgf@xc}{\the\pgf@yc}% |
| 323 |
}% |
| 324 |
|
| 325 |
|
| 326 |
\def\pgf@nlt@closepath@nlt{% |
| 327 |
{% |
| 328 |
\expandafter\pgfqpoint\pgf@nlt@last@moveto@orig% |
| 329 |
\expandafter\pgf@nlt@set@temps\pgf@nlt@last@coord@orig% |
| 330 |
\advance\pgfutil@tempdima by-\pgf@x% |
| 331 |
\advance\pgfutil@tempdimb by-\pgf@y% |
| 332 |
\pgfutil@tempswatrue% |
| 333 |
\ifdim\pgfutil@tempdima<0.01pt\relax\ifdim\pgfutil@tempdima>-0.01pt\relax\ifdim\pgfutil@tempdimb<0.01pt\relax\ifdim\pgfutil@tempdimb>-0.01pt\relax\pgfutil@tempswafalse\fi\fi\fi\fi% |
| 334 |
\ifpgfutil@tempswa% |
| 335 |
\pgf@nlt@lineto@nlt{\pgf@x}{\pgf@y}% |
| 336 |
\fi% |
| 337 |
\pgfsyssoftpath@closepath% |
| 338 |
}% |
| 339 |
}% |
| 340 |
|
| 341 |
|
| 342 |
\endinput |