summaryrefslogtreecommitdiff
path: root/Master/texmf-dist/tex/generic/pgfplots/pgfcontrib/tikzlibraryfillbetween.code.tex
blob: 6446027318f960b0571730ba8735a1aed7516421 (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
%--------------------------------------------
%
% Package pgfplots
%
% Provides a user-friendly interface to create function plots (normal
% plots, semi-logplots and double-logplots).
% 
% It is based on Till Tantau's PGF package.
%
% Copyright 2013 by Christian Feuersaenger
%
% This program is free software: you can redistribute it and/or modify
% it under the terms of the GNU General Public License as published by
% the Free Software Foundation, either version 3 of the License, or
% (at your option) any later version.
% 
% This program is distributed in the hope that it will be useful,
% but WITHOUT ANY WARRANTY; without even the implied warranty of
% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
% GNU General Public License for more details.
% 
% You should have received a copy of the GNU General Public License
% along with this program.  If not, see <http://www.gnu.org/licenses/>.
%
%--------------------------------------------


% This library adds support for high-level instructions for "fill area
% between two arbitrary plots of functions".
%
% It activates the syntax \pgfpathfillbetween
% where A and B are two plots named by 'name path='.
%
% In fact, this here is not much more than a low-level invocation of
% \pgfpathfillbetween
% and a couple of styles. It could become a TikZ library because it
% actually works on any two named paths, but it has its restrictions
% regarding the supported input paths: both need to be plots of
% functions (non-intersecting, should have at most one function value
% for each canvas X coord)

\pgfutil@IfUndefined{pgfplotsset}{%
	\pgferror{Please load pgfplots before pgfplots.fillbetween.}%
	\endinput
}{}%

\usetikzlibrary{intersections}
\usetikzlibrary{decorations.softclip}

% COMPATIBILITY WITH PGF RELEASE:
\pgfutil@IfUndefined{pgfintersectiongetsolutiontimes}{%
	\pgfplotsusecompatibilityfile{pgflibraryintersections.code.tex}%
}{}%
\def\tikz@key@name@path@wrong#1#2{%
    \tikz@addmode{%
      \pgfsyssoftpath@getcurrentpath\tikz@intersect@temppath@round%
      \pgfprocessround\tikz@intersect@temppath@round\tikz@intersect@temppath%
      \ifx\tikz@intersect@namedpaths\pgfutil@empty%
      \else%
        \tikz@intersect@namedpaths%
        \pgfutil@ifundefined{tikz@intersect@path@name@#1}{}%
        {%
          \expandafter\expandafter\expandafter\def\expandafter\expandafter\expandafter\tikz@intersect@@temppath%
          \expandafter\expandafter\expandafter{\csname tikz@intersect@path@name@#1\endcsname}%
          \expandafter\expandafter\expandafter\def\expandafter\expandafter\expandafter\tikz@intersect@temppath%
          \expandafter\expandafter\expandafter{\expandafter\tikz@intersect@temppath\tikz@intersect@temppath}%
        }%
      \fi%
      \tikz@intersect@addto@path@names{#1}{#2}%
    }%
}%

\def\tikz@key@name@path@new#1#2{%
    \tikz@addmode{%
      \pgfsyssoftpath@getcurrentpath\tikz@intersect@temppath@round%
      \pgfprocessround\tikz@intersect@temppath@round\tikz@intersect@temppath%
      \ifx\tikz@intersect@namedpaths\pgfutil@empty%
      \else%
        \tikz@intersect@namedpaths%
      \fi%
      \tikz@intersect@addto@path@names{#1}{#2}%
    }%
}%

\ifx\tikz@key@name@path@wrong\tikz@key@name@path
	\immediate\write16{Package pgfplots: loading complementary 'name path' implementation for your pgf version...}
	\let\tikz@key@name@path=\tikz@key@name@path@new
\fi
% ------------------------------------------------------------


\pgfkeys{%
	/tikz/fill between/of/.code=\tikzlibraryfillbetween@parse#1\pgf@stop,
	%
	/tikz/fill between/on layer/.initial=pre main,
	/tikz/fill between/every segment/.style={},
	/tikz/fill between/every odd segment/.style={},
	/tikz/fill between/every even segment/.style={},
	%
	% soft clip={(axis cs:0,0) rectangle (axis cs:1,1)}
	/tikz/fill between/soft clip/.style={
		/tikz/fill between/soft clip first={#1},%
		/tikz/fill between/soft clip second={#1},%
	},%
	/tikz/fill between/soft clip first/.initial=,%
	/tikz/fill between/soft clip second/.initial=,
	%
	% #1: drawing options.
	/tikz/fill between/@draw style/.style={
		/pgf/fill between/result stream/begin/.code={%
			\gdef\tikzsegmentindex{0}%
		},%
		/pgf/fill between/result stream/next ready/.code={%
			\let\pgflibraryfill@path=\pgfretval
			\def\pgfplots@loc@TMPa{%
				/tikz/fill between/every segment,
				#1,%
				/tikz/fill between/every segment no \tikzsegmentindex/.try,
			}%
			\ifodd\tikzsegmentindex\relax
				\expandafter\def\expandafter\pgfplots@loc@TMPa\expandafter{\pgfplots@loc@TMPa
					/tikz/fill between/every odd segment,
				}%
			\else
				\expandafter\def\expandafter\pgfplots@loc@TMPa\expandafter{\pgfplots@loc@TMPa
					/tikz/fill between/every even segment,
				}%
			\fi
			%
			\expandafter\fill\expandafter[\pgfplots@loc@TMPa]
				\pgfextra \pgfsetpathandBB{\pgflibraryfill@path}\endpgfextra;%
			\pgfplotsutil@advancestringcounter@global\tikzsegmentindex
		},%
		/pgf/fill between/result stream/end/.code=,%
	},
	/tikz/fill between/.search also={/pgf/fill between,/pgfplots},
	%
	%
	%
	%--------------------------------------------------
	% /pgfplots/execute at begin axis@@/.add={%
	% 	\def\b@pgfplotslibraryfill@added
	% }{%
	% 	
	% },%
	%-------------------------------------------------- 
}

\def\tikzlibraryfillbetween@parse#1 and #2\pgf@stop{%
	\def\tikz@fillbetween@a{#1}%
	\def\tikz@fillbetween@b{#2}%
}%

% \tikzfillbetween[<options>]{<draw style>}
%
% <options> must contain 'of=<A> and <B>' and may configure how the
% area is computed.
%
% <draw style> affects every drawn region.
%
\def\tikzfillbetween{\pgfutil@ifnextchar[{\tikzfillbetween@opt}{\tikzfillbetween@opt[]}}
\def\tikzfillbetween@opt[#1]#2{%
	\begingroup
	\pgfqkeys{/tikz/fill between}{% 
		% prepare the low-level path generation instruction(s):
		/tikz/fill between/@draw style={#2},%
		%
		% set up input options:
		#1,%
	}%
	% automagically try to use the correct layer:
	\pgfkeysgetvalue{/tikz/fill between/on layer}\tikzlibraryfillbetween@path@layer@name
	\tikzlibraryfillbetween@determine@layer
	%
	\ifx\tikzlibraryfillbetween@path@layer@name\pgfutil@empty \else
		\pgfonlayer{\tikzlibraryfillbetween@path@layer@name}%
	\fi
	%
		\tikzlibraryfillbetween@path@generatepath
	%
	\ifx\tikzlibraryfillbetween@path@layer@name\pgfutil@empty \else
		\endpgfonlayer%
	\fi
	%
	\endgroup
}%

% Defines \tikzlibraryfillbetween@path@layer@name
\def\tikzlibraryfillbetween@determine@layer{%
	\ifx\tikzlibraryfillbetween@path@layer@name\pgfutil@empty
	\else
		\pgfutil@IfUndefined{pgf@layerlist}{%
			% hm. No layers active!? A pity...
			\tikzlibraryfillbetween@path@warn@layer
			\let\tikzlibraryfillbetween@path@layer@name\pgfutil@empty%
		}{%
			\edef\pgfplots@loc@TMPa{\noexpand\pgfutil@in@{\tikzlibraryfillbetween@path@layer@name}}%
			\expandafter\pgfplots@loc@TMPa\expandafter{\pgf@layerlist}%
			\ifpgfutil@in@
			\else
				\tikzlibraryfillbetween@path@warn@layer
				\let\tikzlibraryfillbetween@path@layer@name\pgfutil@empty%
			\fi
		}%
	\fi
}%

\def\tikzlibraryfillbetween@path@warn@layer{%
	\pgfplots@warning{'fill between': Could not activate graphics layer '\tikzlibraryfillbetween@path@layer@name'. Filled path will be on top of the other ones. Please ensure that '\tikzlibraryfillbetween@path@layer@name' is somewhere in the layer list (or set '/tikz/fill between/on layer=').}%
}%

\def\tikzlibraryfillbetween@parse@softclip{%
	\pgfkeysgetvalue{/tikz/fill between/soft clip first}\pgf@temp
	\pgfkeysgetvalue{/tikz/fill between/soft clip second}\pgf@tempb
	\ifx\pgf@temp\pgf@tempb
		% Ah - both have the same value!
		\ifx\pgf@temp\pgfutil@empty
			% ... and both are empty.
			\pgffillbetweensetsoftclippath{\pgfutil@empty}%
		\else
			% ... and both contain some path! Process it (once):
			\def\tikz@marshal{\tikzlibsoftclip@setkey{\pgffillbetweensetsoftclippath}}%
			\expandafter\tikz@marshal\pgf@temp\pgf@stop
		\fi
	\else
		% handle 'soft clip first':
		\ifx\pgf@temp\pgfutil@empty
			\pgffillbetweensetsoftclippathfirst{\pgfutil@empty}%
		\else
			\def\tikz@marshal{\tikzlibsoftclip@setkey{\pgffillbetweensetsoftclippathfirst}}%
			\expandafter\tikz@marshal\pgf@temp\pgf@stop
		\fi
		%
		% handle 'soft clip second':
		\pgfkeysgetvalue{/tikz/fill between/soft clip second}\pgf@tempb
		\ifx\pgf@tempb\pgfutil@empty
			\pgffillbetweensetsoftclippathsecond{\pgfutil@empty}%
		\else
			\def\tikz@marshal{\tikzlibsoftclip@setkey{\pgffillbetweensetsoftclippathsecond}}%
			\expandafter\tikz@marshal\pgf@tempb\pgf@stop
		\fi
	\fi
}%

\def\tikzlibraryfillbetween@path@generatepath{%
	%
	\tikzlibraryfillbetween@parse@softclip
	%
	\tikzlibraryfillbetween@path@check
	\expandafter\let\expandafter\tikz@fillbetween@a@path
		\csname tikz@intersect@path@name@\tikz@fillbetween@a\endcsname
	\expandafter\let\expandafter\tikz@fillbetween@b@path
		\csname tikz@intersect@path@name@\tikz@fillbetween@b\endcsname
	%
	\pgfpathfillbetween{\tikz@fillbetween@a@path}{\tikz@fillbetween@b@path}%
}%

\def\tikzlibraryfillbetween@path@check{%
	\pgfutil@IfUndefined{tikz@intersect@path@name@\tikz@fillbetween@a}{%
		\pgferror
		{fill between: the mandatory argument 'of=<name path A> and <name path B> is missing or has empty arguments. Please ensure that the option has been set and that both path names have been assigned (perhaps you need 'name path global=\tikz@fillbetween@a' somewhere?)}%
	}{}%
	\pgfutil@IfUndefined{tikz@intersect@path@name@\tikz@fillbetween@b}{%
		\pgferror
		{fill between: the mandatory argument 'of=<name path A> and <name path B> is missing or has empty arguments. Please ensure that the option has been set and that both path names have been assigned (perhaps you need 'name path global=\tikz@fillbetween@b' somewhere?)}%
	}{}%
}


%-----------------------------------------
%
% Utilities to work with path segments
%
%-----------------------------------------

% Defines \pgfretval such that it contains the named path '#1'
\def\tikzgetnamedpath#1{%
	\pgfutil@IfUndefined{tikz@intersect@path@name@#1}{%
		\pgferror{There is no named path called '#1'. Perhaps you misspelled it?}%
	}{%
		\expandafter\let\expandafter\pgfretval
			\csname tikz@intersect@path@name@#1\endcsname
	}%
}%

\def\tikznamecurrentpath#1{%
	\pgfgetpath\pgf@temp
	\pgfprocessround\pgf@temp\pgf@tempb%
	\expandafter\global\expandafter\let\csname tikz@intersect@path@name@#1\endcsname=\pgf@tempb
}%

% ---------------------------------------------------------------------------------------

\newif\iftikzpathsegments@reverse
\def\tikzlibrarysegments@parse#1 and #2\pgf@stop{%
	\def\tikz@path@segments@a{#1}%
	\def\tikz@path@segments@b{#2}%
}%


\pgfkeys{
	% allows to append intersection segments to the current path by
	% writing
	% \path[intersection segments={of=first and second, sequence=A0 -- B1 -- B3 A3[reverse] -- A1}];
	/tikz/intersection segments/.code={%
		\tikzpathintersectionsegments[#1]%
	},
	/tikz/segments/of/.code=\tikzlibrarysegments@parse#1\pgf@stop,
	/tikz/segments/of={} and {},
	/tikz/segments/sequence/.initial=A0 -- B1,
	/tikz/segments/reverse/.is if=tikzpathsegments@reverse,
	/tikz/segments/reverse/.default=true,
}

% Programmatic method to do the same as 'intersection
% segments={<options>}
%
% #1: <options>
\def\tikzpathintersectionsegments[#1]{%
	\begingroup
	\pgfqkeys{/tikz/segments}{#1}%
	\tikzgetnamedpath{\tikz@path@segments@a}%
	\let\tikz@path@segments@A=\pgfretval
	\tikzgetnamedpath{\tikz@path@segments@b}%
	\let\tikz@path@segments@B=\pgfretval
	%
	% this macro will be called whenever we need an intersection
	% segment. Perhaps we do not need it at all
	\def\tikz@ensurehascomputedintersection{%
		% compute intersections using the PGF intersection lib...
		\pgfintersectionofpaths{\pgfsetpath\tikz@path@segments@A}{\pgfsetpath\tikz@path@segments@B}%
		%
		% ... and compute the intersection *segments* for both input
		% paths...
		\pgfcomputeintersectionsegments1%
		\pgfcomputeintersectionsegments2%
		\let\tikz@ensurehascomputedintersection=\relax
	}%
	%
	\pgfkeysgetvalue{/tikz/segments/sequence}\tikz@sequence
	%
	\def\b@tikz@isect@nextismoveto{1}%
	\expandafter\tikzpathintersectionsegments@parse@loop\tikz@sequence\pgf@stop
	\endgroup
}%

\def\tikzpathintersectionsegments@parse@loop{%
	\pgfutil@ifnextchar-{%
		\tikz@isect@p@lineto
	}{%
		\pgfutil@ifnextchar A{%
			\tikz@isect@p@next
		}{%
			\pgfutil@ifnextchar B{%
				\tikz@isect@p@next
			}{%
				\pgfutil@ifnextchar\pgf@stop{%
					\tikz@isect@finish
				}{%
					\tikz@isect@p@error
				}%
			}%
		}%
	}%
}%

\def\tikz@isect@finish\pgf@stop{}%

\def\tikz@isect@p@lineto--{%
	\def\b@tikz@isect@nextismoveto{0}%
	\tikzpathintersectionsegments@parse@loop
}%

\def\tikz@isect@p@error#1\pgf@stop{%
	\pgferror{The argument of 'sequence' has an unexpected format near '#1'. Please write something like A0 -- B1 -- A1}%
}

\def\tikz@isect@p@next#1#2{%
	\pgfutil@ifnextchar[{%
		\tikz@isect@p@next@opt{#1}{#2}%
	}{%
		\tikz@isect@p@next@opt{#1}{#2}[]%
	}%
}%
\def\tikz@isect@p@next@opt#1#2[#3]{%
	\begingroup
	%
	% set keys (if any):
	\def\tikz@temp{#3}%
	\ifx\tikz@temp\pgfutil@empty
	\else
		\pgfqkeys{/tikz/segments}{#3}%
	\fi
	%
	% parse arguments:
	\edef\pgfmathresult{#2}%
	\def\tikz@temp{*}%
	\ifx\pgfmathresult\tikz@temp
		\def\b@tikz@select@all{1}%
	\else
		\def\b@tikz@select@all{0}%
		\tikz@ensurehascomputedintersection
		\pgfmathparse{round(#2)}%
		\let\tikz@index=\pgfmathresult
	\fi
	%
	\if A#1%
		\def\tikz@path{1}%
	\else
		\if B#1%
			\def\tikz@path{2}%
		\else
			\def\tikz@path{}%
			\pgferror{The argument of 'sequence' has an unexpected format near '#1#2': expected A#2 or B#2}%
		\fi
	\fi
	%
	%
	% PROCESS IT:
	\ifx\tikz@path\pgfutil@empty
	\else
		\if1\b@tikz@select@all%
			% ok... select the *entire* path. 
			% #1 = A|B :
			\expandafter\let\expandafter\pgfretval\csname tikz@path@segments@#1\endcsname
		\else
			\pgfgetintersectionsegmentpath{\tikz@path}{\tikz@index}%
		\fi
		\iftikzpathsegments@reverse
			\pgf@reverse@path\pgfretval
		\fi
		\if0\b@tikz@isect@nextismoveto
			\pgfpathreplacefirstmoveto\pgfretval
		\fi
		\pgfaddpathandBB\pgfretval
	\fi
	\endgroup
	\def\b@tikz@isect@nextismoveto{1}%
	\tikzpathintersectionsegments@parse@loop
}%

\endinput