Debian Codegraph @

texmf-dist/tex/generic/pgfplots/pgfcontrib/tikzlibrarydecorations.softclip.code.tex

raw
1 %--------------------------------------------
2 %
3 % Package pgfplots
4 %
5 % Provides a user-friendly interface to create function plots (normal
6 % plots, semi-logplots and double-logplots).
7 %
8 % It is based on Till Tantau's PGF package.
9 %
10 % Copyright 2013 by Christian Feuersaenger
11 %
12 % This program is free software: you can redistribute it and/or modify
13 % it under the terms of the GNU General Public License as published by
14 % the Free Software Foundation, either version 3 of the License, or
15 % (at your option) any later version.
16 %
17 % This program is distributed in the hope that it will be useful,
18 % but WITHOUT ANY WARRANTY; without even the implied warranty of
19 % MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 % GNU General Public License for more details.
21 %
22 % You should have received a copy of the GNU General Public License
23 % along with this program. If not, see <http://www.gnu.org/licenses/>.
24 %
25 %--------------------------------------------
26 %
27 % This library adds support for a "soft clip" decoration. It applies
28 % clipping to an input path, but rather than simply instructing the
29 % display driver to clip the path, it computes a new clip path from
30 % the input.
31 %
32 % This library is (currently) on top of tikz.
33
34 \pgfutil@IfUndefined{pgfplotsset}{%
35 \pgferror{Please load pgfplots before pgfplots.fillbetween.}%
36 \endinput
37 }{}%
38
39 \usetikzlibrary{intersections}
40
41 \pgfplots@iffileexists{pgflibraryfillbetween.code.tex}{%
42 \usepgflibrary{fillbetween}
43 }{%
44 \pgfplotsusecompatibilityfile{pgflibraryfillbetween.code.tex}%
45 }%
46
47 \pgfutil@IfUndefined{pgfintersectiongetsolutiontimes}{%
48 \pgfplotsusecompatibilityfile{pgflibraryintersections.code.tex}%
49 }{}%
50
51
52 \pgfkeys{
53 % soft clip={(axis cs:0,0) rectangle (axis cs:1,1)}
54 % or
55 % soft clip={A}
56 % where 'A' is defined using 'name path=A' somewhere
57 /pgf/decoration/soft clip path/.code={%
58 % FIXME : I would rather NOT evaluate path arguments in this
59 % context! Who knows how people set keys!? But alas, we cannot
60 % evaluate late because the CM is reset while working on
61 % decorations ... (see below)
62 \tikzlibsoftclip@setkey{\tikzlibsoftclip@setkey@assign}#1\pgf@stop
63 },
64 /pgf/decoration/every soft clipped path/.style={},%
65 }
66
67 % #1 a macro containing a soft path
68 \def\tikzlibsoftclip@setkey@assign#1{%
69 \let\pgf@decoration@soft@clip=#1%
70 }%
71
72 \tikzlibsoftclip@setkey@assign{\pgfutil@empty}%
73
74 \pgfdeclaredecoration{soft clip}{replace}{
75
76 \state{replace}[width=\pgfdecoratedpathlength,
77 persistent precomputation={%
78 % This here is an earlier draft... but alas, the
79 % transformation matrix has been reset in this context,
80 % and we cannot define the clip path dynamically here.
81 % I left it to document that.
82 %
83 %\pgfkeysgetvalue{/pgf/decoration/soft clip path}\pgf@temp
84 %\def\tikz@marshal{\tikzlibsoftclip@setkey{\tikzlibsoftclip@setkey@assign}}%
85 %\expandafter\tikz@marshal\pgf@temp\pgf@stop
86 }
87 ]{%
88 \ifx\pgf@decoration@soft@clip\pgfutil@empty
89 \pgfplotsthrow{invalid argument}
90 {\pgf@decoration@soft@clip}%
91 {The mandatory argument 'soft clip path=(A) rectangle (B)' has not been set}%
92 \pgfeov
93 \else
94 \tikzset{/pgf/decoration/every soft clipped path}%
95 \pgfpathcomputesoftclippath{\pgfdecoratedpath}{\pgf@decoration@soft@clip}%
96 \pgfsetpathandBB{\pgfretval}%
97 \fi
98 }
99 }
100
101 % ---------------------------------------------------------------------------------
102 %
103 % SOFT-CLIPPING.
104 %
105 % "softclip" means to get rid of those parts of a path which are
106 % outside of a clip path.
107 %
108 % An example is to trim at the beginning and/or end of a
109 % path as part of fill-between ("poor-mans-clipping").
110 %
111 % The difference to "real" clipping is that it is applied to the
112 % path, not to the viewer -- the path can still be drawn with any
113 % decorations, line widths, etc.
114 %
115 % Another difference is that this feature is (considerably) less sophisticated.
116 %
117 % #1: the input path
118 % #2: the clip path (if it is empty, no clipping will be applied)
119 % OUTPUT:
120 % \pgfretval is #1 with modifications
121 \def\pgfpathcomputesoftclippath#1#2{%
122 \ifx#2\pgfutil@empty
123 \let\pgfretval=#1%
124 \else
125 \ifx#1\pgfutil@empty
126 \let\pgfretval=#1%
127 \else
128 \pgfpathcomputesoftclippath@{#1}{#2}%
129 \fi
130 \fi
131 }
132
133 \def\pgfpathcomputesoftclippath@#1#2{%
134 \begingroup
135 \pgfprocessround{#1}{#1}%
136 \pgfprocessround{#2}{#2}%
137 %
138 %
139 \pgfpathcomputesoftclippath@is@first@outside@of@path{#1}{#2}%
140 \let\b@pgffill@is@outside@clip=\pgfretval
141 %
142 %\message{computing soft clip path for ^^J\meaning#1 and ^^J\meaning#2^^J first point of input is outside of clip path=\b@pgffill@is@outside@clip^^J}%
143 %
144 % FIXME : it might be that I need to sort them ... !? but
145 % other tests indicate that I should not!?
146 %\pgfintersectionsortbyfirstpath
147 \pgf@intersect@sortfalse
148 \pgfintersectionofpaths%
149 {%
150 \pgfsetpath#1%
151 }%
152 {%
153 \pgfsetpath#2%
154 }%
155 %
156 %\message{... num intersections = \pgfintersectionsolutions^^J}%
157 %
158 \ifnum\pgfintersectionsolutions=0 %
159 \if1\b@pgffill@is@outside@clip
160 % entire path is outside of the clipped area.
161 \let\pgfretval=\pgfutil@empty
162 \else
163 \let\pgfretval=#1%
164 \fi
165 \else
166 % split the first involved path into the
167 % segments induced by the intersection points:
168 \pgfcomputeintersectionsegments{1}%
169 \let\pgfpathfilled@a@segments=\pgfretval
170 %
171 % Now, create a new path which contains only those
172 % segments which are INSIDE of the clip path.
173 %
174 % I assume that I can rely on "even/odd" matching: if the
175 % first is inside, the second is outside, the third
176 % inside, etc.
177 \pgfapplistnewempty{pgfretval@tmp}%
178 \def\c@pgfpathfilled@counter{0}%
179 \pgfmathloop
180 \ifnum\c@pgfpathfilled@counter<\pgfpathfilled@a@segments\relax
181 \if0\b@pgffill@is@outside@clip
182 \expandafter\let\expandafter\pgf@loc@path@a\csname pgf@intersect@path@split@a@\c@pgfpathfilled@counter\endcsname
183 \expandafter\pgfapplistpushback\pgf@loc@path@a\to{pgfretval@tmp}%
184 \fi
185 \pgfpathfillbetween@negate\b@pgffill@is@outside@clip
186 %
187 \pgfutil@advancestringcounter\c@pgfpathfilled@counter
188 \repeatpgfmathloop
189 \pgfapplistlet\pgfretval={pgfretval@tmp}%
190 \fi
191 %
192 \global\let\pgf@glob@TMPa=\pgfretval%
193 \endgroup
194 \let\pgfretval=\pgf@glob@TMPa
195 }
196
197 % #1: input path (non-empty)
198 % #2: soft clip path
199 % Defines "\def\pgfretval{1}" if (the first point of #1 is outside or on the path #2)
200 % Defines "\def\pgfretval{0}" if (the first point of #1 is inside of the path #2)
201 \def\pgfpathcomputesoftclippath@is@first@outside@of@path#1#2{%
202 % APPROACH: shoot a line starting at the first coordinate of the
203 % first path through "the middle of #2". Then make an even/odd
204 % check on the number of intersections.
205 %
206 % FIXME : for now, I only support (x,y) rectangle (X,Y) anyway --
207 % optimize for that case!? This here might be too complex...
208 \begingroup
209 \expandafter\pgfpathfillbetween@get@first@coord#1\pgf@stop
210 \let\pgfpathfilled@a@firstcoord=\pgfretval%
211 %
212 % Get some point "in the middle of #2":
213 \pgfpathcomputesoftclippath@accum@pseudo@mean#2%
214 \edef\pgfpathfilled@b@center{\noexpand\pgfqpoint\pgfretval}%
215 %
216 % We have to shoot *through* #2, not just into the middle of #2.
217 % Consequently, we need to know how big #2 is:
218 \pgfpathcomputesoftclippath@is@first@outside@of@path@getBB#2%
219 \let\pgf@size@hint=\pgfretval
220 %
221 % Now, compute a target point such that our shot goes through it:
222 \pgfqpointscale{%
223 \pgf@size@hint
224 }{%
225 \pgfpointnormalised{%
226 \pgfpointdiff%
227 {\expandafter\pgfqpoint\pgfpathfilled@a@firstcoord}%
228 {\pgfpathfilled@b@center}%
229 }%
230 }%
231 % collect intermediate results as \pgf@xa/\pgf@ya are overwritten:
232 \edef\pgf@direction@vector{\noexpand\pgfqpoint{\the\pgf@x}{\the\pgf@y}}%
233 \pgfpointadd{\pgfpathfilled@b@center}{\pgf@direction@vector}%
234 \edef\pgfpathfilled@shoot@line@trg{{\the\pgf@x}{\the\pgf@y}}%
235 %
236 % this here is our path!
237 \edef\pgf@direction@path{%
238 \noexpand\pgfsyssoftpath@movetotoken\pgfpathfilled@a@firstcoord
239 \noexpand\pgfsyssoftpath@linetotoken\pgfpathfilled@shoot@line@trg
240 }%
241 %
242 % ... ok, compute intersections. This should be fairly fast as
243 % our second path has just 1 segment, i.e. it will be O(N) where N
244 % is the number of segments in #2.
245 \pgf@intersect@sortfalse
246 \pgfintersectionofpaths%
247 {%
248 \pgfsetpath#2%
249 }%
250 {%
251 \pgfsetpath\pgf@direction@path%
252 }%
253 %
254 \ifnum\pgfintersectionsolutions=0 %
255 % this must not happen! We have taken great care such that we
256 % *have* at least 1 intersection points!
257 \pgferror{Illegal state encountered: the computation of a softpath failed. The failure occurred while computing whether the first input coordinate is inside of the clip path (found no intersections)}%
258 \fi
259 %
260 \pgfpathcomputesoftclippath@if@is@first@on@boundary{%
261 % AH! The first point is ON the boundary of the clip path.
262 % This is equivalent to the condition that the first
263 % intersection point equals the first point.
264 %
265 % In this case, we say that it is OUTSIDE of the clip path!
266 %
267 % This is actually related to
268 % 1. the fact that the input path is splitted into segments
269 % induced by the intersection points in order to apply
270 % soft-clipping.
271 % 2. the fact that the clipping is based on an even/odd
272 % matching (in/out).
273 %
274 % In that setup, we want to skip the segment until the first
275 % intersection point - after all, it is "empty" anyway.
276 %
277 % To this end, we have to say that this first (empty) segment
278 % is OUTSIDE:
279 \def\pgfretval{1}%
280 }{%
281 \ifodd\pgfintersectionsolutions\relax%
282 \def\pgfretval{0}%
283 \else
284 \def\pgfretval{1}%
285 \fi
286 }%
287 \pgfmath@smuggleone\pgfretval
288 \endgroup
289 }
290
291 \def\pgfpathcomputesoftclippath@if@is@first@on@boundary#1#2{%
292 \pgfpathcomputesoftclippath@compute@infty@norm{%
293 \pgfpointdiff%
294 {\expandafter\pgfqpoint\pgfpathfilled@a@firstcoord}%
295 {\pgfpointintersectionsolution{1}}%
296 }%
297 \ifdim\pgf@x<\pgfintersectiontolerance\relax
298 % YES: the first point IS on the boundary: the first
299 % intersection solution == first point.
300 #1%
301 \else
302 % No, it is NOT equal to the first intersection solution - and
303 % thus NOT on the boundary.
304 #2%
305 \fi
306 }%
307
308 % Defines \pgf@x to be the infty norm of vector #1
309 \def\pgfpathcomputesoftclippath@compute@infty@norm#1{%
310 \pgf@process{#1}%
311 \ifdim\pgf@x<0sp \global\pgf@x=-\pgf@x\fi
312 \ifdim\pgf@y<0sp \global\pgf@y=-\pgf@y\fi
313 \ifdim\pgf@x<\pgf@y
314 \global\pgf@x=\pgf@y
315 \fi
316 }
317
318 % Defines \pgfretval to be a *scalar* "size indicator" (1-norm) of the bounding
319 % box of #1.
320 %
321 % #1: a macro containing a softpath.
322 \def\pgfpathcomputesoftclippath@is@first@outside@of@path@getBB#1{%
323 \begingroup
324 \pgf@getpathsizes\pgf@interrupt@pathsizes
325 % we only need the path size here:
326 \pgf@relevantforpicturesizefalse
327 %
328 % FIXME : CODE CLEANUP NEEDED
329 \def\pgfsetpathBB@protocol@lastmoveto##1##2{}%
330 \expandafter\pgfsetpath@loop#1\pgf@stop
331 \pgfpointdiff
332 {\pgfqpoint\pgf@pathminx\pgf@pathminy}%
333 {\pgfqpoint\pgf@pathmaxx\pgf@pathmaxy}%
334 % compute |v|_1 = x + y (both components are non-negative anyway):
335 \pgf@xa=\pgf@x
336 \advance\pgf@xa by\pgf@y
337 \xdef\pgf@glob@TMPa{\pgf@sys@tonumber\pgf@xa}%
338 \pgf@setpathsizes\pgf@interrupt@pathsizes
339 \endgroup
340 \let\pgfretval=\pgf@glob@TMPa
341 }%
342
343 % Defines \pgfretval to be of a "pseudo" mean of path #1.
344 %
345 % Here, "pseudo" refers to the fact that the mean will only be
346 % accumulated over the "first couple of coordinates" to avoid numeric
347 % overflows in TeX's math engine.
348 \def\pgfpathcomputesoftclippath@accum@pseudo@mean#1{%
349 \begingroup
350 \let\pgfsyssoftpath@movetotoken\pgfpathcomputesoftclippath@accum@pseudo@mean@
351 \let\pgfsyssoftpath@linetotoken\pgfpathcomputesoftclippath@accum@pseudo@mean@
352 \let\pgfsyssoftpath@closepathtoken\pgfpathcomputesoftclippath@accum@pseudo@mean@
353 \let\pgfsyssoftpath@curvetotoken\pgfpathcomputesoftclippath@accum@pseudo@mean@
354 \let\pgfsyssoftpath@curvetosupportatoken\pgfpathcomputesoftclippath@accum@pseudo@mean@relax
355 \let\pgfsyssoftpath@curvetosupportbtoken\pgfpathcomputesoftclippath@accum@pseudo@mean@relax
356 \c@pgf@countc=0 %
357 \pgf@xa=0pt %
358 \pgf@ya=0pt %
359 #1%
360 \divide\pgf@xa by\c@pgf@countc
361 \divide\pgf@ya by\c@pgf@countc
362 \edef\pgfretval{{\the\pgf@xa}{\the\pgf@ya}}%
363 \pgfmath@smuggleone\pgfretval
364 \endgroup
365 }%
366 \def\pgfpathcomputesoftclippath@accum@pseudo@mean@relax#1#2{}
367 \def\pgfpathcomputesoftclippath@accum@pseudo@mean@#1#2{%
368 \ifnum\c@pgf@countc<4
369 % avoid overflows. 4 must be sufficient for now.
370 \advance\pgf@xa by#1\relax
371 \advance\pgf@ya by#2\relax
372 \advance\c@pgf@countc by1 %
373 \fi
374 }
375
376
377 % #1: of the form '{<x>}{<y>}'
378 % #2: of the form '{<x>}{<y>}'
379 \def\pgfpathfillbetween@check@x@less@than#1#2{%
380 \edef\pgf@temp{#1#2}%
381 \expandafter\pgfpathfillbetween@check@x@less@than@\pgf@temp
382 }%
383
384 % #1: x1
385 % #2: y1
386 % #3: x2
387 % #4: y2
388 \def\pgfpathfillbetween@check@x@less@than@#1#2#3#4{%
389 \ifdim#1>#3\relax
390 % <=
391 \def\pgfretval{0}%
392 \else
393 \def\pgfretval{1}%
394 \fi
395 }%
396
397 \def\pgfpathfillbetween@negate#1{%
398 \if0#1%
399 \def#1{1}%
400 \else
401 \def#1{0}%
402 \fi
403 }
404
405 \pgfkeys{
406 /tikz/soft clip assign/name/.code={\tikzgetnamedpath{#1}},
407 /tikz/soft clip assign/path/.code={\tikzlibsoftclip@setkey@@#1\pgf@stop},
408 }
409
410 %
411 % \tikzlibsoftclip@setkey{<\macro>} <path name>\pgf@stop
412 %
413 % OR
414 %
415 % \tikzlibsoftclip@setkey{<\macro>} (<A>) rectangle (<B>)\pgf@stop
416 %
417 % #1: a macro which is called with the resulting clip path as argument #1.
418 \def\tikzlibsoftclip@setkey#1#2\pgf@stop{%
419 \pgfutil@in@{=}{#2}%
420 \ifpgfutil@in@
421 \pgfqkeys{/tikz/soft clip assign}{#2}%
422 \else
423 \def\pgf@temp{#2}%
424 \pgfplots@command@to@string\pgf@temp\pgf@temp
425 %
426 \tikzifisnamedpath{\pgf@temp}{%
427 \pgfkeysalso{/tikz/soft clip assign/name={#2}}%
428 }{%
429 \pgfkeysalso{/tikz/soft clip assign/path={#2}}%
430 }%
431 \fi
432 #1{\pgfretval}%
433 }
434
435 \def\tikzlibsoftclip@setkey@@{%
436 \edef\tikzlibsoftclip@setkey@@reset{\noexpand\tikz@expandcount=\the\tikz@expandcount\relax}%
437 \tikz@expandcount=100 %
438 %
439 \tikz@scan@one@point\tikzlibsoftclip@setkey@bb@scan@a
440 }%
441
442 \def\tikzlibsoftclip@setkey@bb@scan@a#1{%
443 \def\tikzlibsoftclip@setkey@bb@a{#1}%
444 \pgfutil@ifnextchar r{%
445 \tikzlibsoftclip@setkey@bb@scan@rectangle
446 }{%
447 \tikzlibsoftclip@setkey@@error
448 }%
449 }%
450
451 \def\tikzlibsoftclip@setkey@bb@scan@rectangle rectangle{%
452 \tikz@scan@one@point\tikzlibsoftclip@setkey@bb@scan@b
453 }%
454
455 \def\tikz@gobble@until@stop#1\pgf@stop{}%
456
457 \def\tikzlibsoftclip@setkey@bb@scan@b#1{%
458 \def\tikzlibsoftclip@setkey@bb@b{#1}%
459 \pgfutil@ifnextchar \pgf@stop{%
460 \tikzlibsoftclip@setkey@@reset
461 \tikzlibsoftclip@setkey@@activate
462 \tikz@gobble@until@stop
463 }{%
464 \tikzlibsoftclip@setkey@@error
465 }%
466 }%
467
468 \def\tikzlibsoftclip@setkey@@error#1\pgf@stop{%
469 \def\pgfplots@loc@TMPa{#1}%
470 \pgfplots@command@to@string\pgfplots@loc@TMPa\pgfplots@loc@TMPb
471 \pgfplotsthrow{invalid argument}
472 {\pgfplots@loc@TMPa}%
473 {fill between: the argument of 'soft clip' has an unexpected format near '\pgfplots@loc@TMPb'; expected '(<pt>) rectangle (<pt>)'}%
474 \pgfeov
475 }%
476
477 % INPUT:
478 % - two PGF points \tikzlibsoftclip@setkey@bb@a and \tikzlibsoftclip@setkey@bb@b.
479 %
480 % POSTCONDITION: \pgfretval contains the resulting path.
481 \def\tikzlibsoftclip@setkey@@activate{%
482 % Expand points to {<x>}{<y>} ...
483 \pgf@process{\tikzlibsoftclip@setkey@bb@a}%
484 \edef\tikzlibsoftclip@setkey@bb@a{{\the\pgf@x}{\the\pgf@y}}%
485 \pgf@process{\tikzlibsoftclip@setkey@bb@b}%
486 \edef\tikzlibsoftclip@setkey@bb@b{{\the\pgf@x}{\the\pgf@y}}%
487 %
488 \pgfinterruptpath
489 \pgf@relevantforpicturesizefalse%
490 \expandafter\pgfqpoint\tikzlibsoftclip@setkey@bb@a
491 \pgf@xa=\pgf@x
492 \pgf@ya=\pgf@y
493 \expandafter\pgfqpoint\tikzlibsoftclip@setkey@bb@b
494 \pgf@xb=\pgf@x
495 \pgf@yb=\pgf@y
496 %
497 \pgfpathmoveto{\pgfqpoint{\pgf@xa}{\pgf@ya}}%
498 \pgfpathlineto{\pgfqpoint{\pgf@xa}{\pgf@yb}}%
499 \pgfpathlineto{\pgfqpoint{\pgf@xb}{\pgf@yb}}%
500 \pgfpathlineto{\pgfqpoint{\pgf@xb}{\pgf@ya}}%
501 \pgfpathclose
502 \pgfgetpath\pgfplots@loc@TMPa
503 \global\let\pgfplots@glob@TMPa=\pgfplots@loc@TMPa
504 \endpgfinterruptpath
505 %
506 \let\pgfretval=\pgfplots@glob@TMPa
507 }%
508 % ---------------------------------------------------------------------------------
509
510 % Executes #2 if #1 is a named path and #3 otherwise.
511 \def\tikzifisnamedpath#1#2#3{%
512 \pgfutil@IfUndefined{tikz@intersect@path@name@#1}{%
513 \def\tikz@next{#3}%
514 }{%
515 \def\tikz@next{#2}%
516 }%
517 \tikz@next
518 }%
519
520