| 1 |
% Copyright 2018 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{pgflibrarydatavisualization.formats.functions.code.tex} |
| 11 |
|
| 12 |
\usepgfmodule{datavisualization}% |
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
% Function evaluator |
| 18 |
% |
| 19 |
% (This uses TeX evaluation and is slow. *You have been warned*.) |
| 20 |
% |
| 21 |
% Description: |
| 22 |
% |
| 23 |
% The data contains the description of variables and functions. |
| 24 |
% |
| 25 |
% The format for the variables is |
| 26 |
% |
| 27 |
% var <name> : interval "["<start>:<end>"]" [ step <steps> | samples <samples>]; |
| 28 |
% |
| 29 |
% (The first two [ and ] are "real", the second pair means "optional", |
| 30 |
% see the examples.) |
| 31 |
% |
| 32 |
% |
| 33 |
% The format for the functions is |
| 34 |
% |
| 35 |
% func <name> = <body>; |
| 36 |
% |
| 37 |
% Additionally, the variables and functions stored in the keys |
| 38 |
% /pgf/data/vars and /pgf/data/funcs will also be used, the format for |
| 39 |
% them is the same as above. |
| 40 |
% |
| 41 |
% The <name> is always the name of a (data point) attribute. Inside |
| 42 |
% the <body> it can be accessed using the \value macro, which is |
| 43 |
% defined locally, here. |
| 44 |
% |
| 45 |
% Newlines are not important, everything is gathered into a single |
| 46 |
% line, the semicolons are used to detect the beginnings and ends of |
| 47 |
% declarations. |
| 48 |
% |
| 49 |
% With everything setup in this way, for each variable, the variable |
| 50 |
% ranges through the values between start and end, either with the |
| 51 |
% given step or the given number of samples. If neither is given, the |
| 52 |
% current value of /pgf/data visualization/samples is used. If there |
| 53 |
% are multiple variables, they all range over their respective |
| 54 |
% intervals independently. Thus, "var x = [0,1]; var y = [0:1];", |
| 55 |
% assuming samples=25, gives you 625 data points. |
| 56 |
% |
| 57 |
% As the variables range over their respective values, the functions |
| 58 |
% are evaluated using the function stored in /pgf/data/evaluator. By |
| 59 |
% default this is set to \pgfmathparse, but |
| 60 |
% you can use a different parser, if you like. |
| 61 |
% |
| 62 |
% Example: |
| 63 |
% |
| 64 |
% data [format=function] { |
| 65 |
% |
| 66 |
% % This is a parametric plot with the parameter called t. |
| 67 |
% var t : interval [0:360] samples 60; |
| 68 |
% |
| 69 |
% % Let's draw a circle: |
| 70 |
% func x = sin(\value t); |
| 71 |
% func y = cos(\value t); |
| 72 |
% } |
| 73 |
% |
| 74 |
% data [format=function] { |
| 75 |
% |
| 76 |
% % This a surface plot data... |
| 77 |
% var x : interval [0:1]; |
| 78 |
% var y : interval [0:1]; |
| 79 |
% |
| 80 |
% % Let's draw a circle: |
| 81 |
% func height = \value x*\value x + \value y*\value y; |
| 82 |
% } |
| 83 |
|
| 84 |
\pgfdeclaredataformat{function} |
| 85 |
{ % no catcode code |
| 86 |
} |
| 87 |
{ % Startup |
| 88 |
% Init collectors |
| 89 |
\let\value=\relax% avoid expansion |
| 90 |
\pgfkeysgetvalue{/pgf/data/vars}\pgf@lib@dv@collector |
| 91 |
} |
| 92 |
{% Line arguments |
| 93 |
#1% |
| 94 |
} |
| 95 |
{ % Line action |
| 96 |
\edef\pgf@lib@dv@collector{\pgf@lib@dv@collector\space #1}% |
| 97 |
} |
| 98 |
{% Ignore empty lines |
| 99 |
} |
| 100 |
{% |
| 101 |
\edef\pgf@lib@dv@collector{\pgf@lib@dv@collector\space \pgfkeysvalueof{/pgf/data/funcs}}% |
| 102 |
\let\pgf@lib@dv@parsed\pgfutil@empty% |
| 103 |
% launch parser on the result |
| 104 |
\expandafter\pgf@lib@dv@func@parse \pgf@lib@dv@collector\pgf@stop% |
| 105 |
\let\value\pgf@lib@dv@getval |
| 106 |
\pgf@lib@dv@parsed\pgfdatapoint\pgfutil@gobble\pgf@stop% |
| 107 |
}% |
| 108 |
|
| 109 |
\pgfkeys{ |
| 110 |
/pgf/data/vars/.initial=, |
| 111 |
/pgf/data/funcs/.initial=, |
| 112 |
/pgf/data/evaluator/.initial=\pgfmathparse, |
| 113 |
/pgf/data/samples/.initial=25}% |
| 114 |
|
| 115 |
\def\pgf@lib@dv@getval#1{\pgfkeysvalueof{/data point/#1}}% |
| 116 |
|
| 117 |
\def\pgf@lib@dv@func@parse{% |
| 118 |
\pgfutil@ifnextchar\pgf@stop{% done |
| 119 |
\pgfutil@gobble% |
| 120 |
} |
| 121 |
{% |
| 122 |
\pgfutil@ifnextchar v{% variable |
| 123 |
\pgf@lib@dv@var% |
| 124 |
}{% |
| 125 |
\pgf@lib@dv@func% |
| 126 |
}% |
| 127 |
}% |
| 128 |
}% |
| 129 |
|
| 130 |
\def\pgf@lib@dv@func func #1 =#2;{% |
| 131 |
\expandafter\def\expandafter\pgf@lib@dv@parsed\expandafter{\pgf@lib@dv@parsed\pgf@lib@dv@eval@func{#1}{#2}} |
| 132 |
\pgf@lib@dv@func@parse% |
| 133 |
}% |
| 134 |
\def\pgf@lib@dv@var var #1 :{\pgfutil@ifnextchar\bgroup{\pgf@lib@dv@var@group{#1}}{\pgf@lib@dv@var@int{#1}}}% |
| 135 |
\def\pgf@lib@dv@var@group#1#2;{% |
| 136 |
\expandafter\def\expandafter\pgf@lib@dv@parsed\expandafter{\pgf@lib@dv@parsed\pgf@lib@dv@group@var{#1}{#2}}% |
| 137 |
\pgf@lib@dv@func@parse% |
| 138 |
}% |
| 139 |
\def\pgf@lib@dv@var@int#1#2[#3:#4]{% |
| 140 |
\pgfutil@ifnextchar s{% |
| 141 |
\pgf@lib@dv@samplesorsteps{#1}{#3}{#4}% |
| 142 |
}{% |
| 143 |
\expandafter\def\expandafter\pgf@lib@dv@parsed\expandafter{% |
| 144 |
\pgf@lib@dv@parsed\pgf@lib@dv@samples@var{#1}{#3}{#4}{\pgfkeysvalueof{/pgf/data/samples}}}% |
| 145 |
\expandafter\pgf@lib@dv@func@parse\pgfutil@gobble% |
| 146 |
} |
| 147 |
}% |
| 148 |
\def\pgf@lib@dv@samplesorsteps#1#2#3s{% |
| 149 |
\pgfutil@ifnextchar t{% step |
| 150 |
\pgf@lib@dv@step{#1}{#2}{#3}% |
| 151 |
}{% sample |
| 152 |
\pgf@lib@dv@samples{#1}{#2}{#3}% |
| 153 |
}% |
| 154 |
}% |
| 155 |
\def\pgf@lib@dv@step#1#2#3tep#4;{% |
| 156 |
\expandafter\def\expandafter\pgf@lib@dv@parsed\expandafter{\pgf@lib@dv@parsed\pgf@lib@dv@step@var{#1}{#2}{#3}{#4}}% |
| 157 |
\pgf@lib@dv@func@parse% |
| 158 |
}% |
| 159 |
\def\pgf@lib@dv@samples#1#2#3amples#4;{% |
| 160 |
\expandafter\def\expandafter\pgf@lib@dv@parsed\expandafter{\pgf@lib@dv@parsed\pgf@lib@dv@samples@var{#1}{#2}{#3}{#4}}% |
| 161 |
\pgf@lib@dv@func@parse% |
| 162 |
}% |
| 163 |
|
| 164 |
% |
| 165 |
% Executer... |
| 166 |
% |
| 167 |
\def\pgf@lib@dv@samples@var#1#2#3#4{% |
| 168 |
\pgfmathparse{#3}% |
| 169 |
\let\pgf@lib@dv@temp=\pgfmathresult% |
| 170 |
\pgfmathparse{#2}% |
| 171 |
\pgfmathparse{\pgf@lib@dv@temp-\pgfmathresult}% |
| 172 |
\pgfmathparse{\pgfmathresult/(#4-1)}% |
| 173 |
\let\pgf@lib@dv@temp=\pgfmathresult% |
| 174 |
\pgf@lib@dv@step@var{#1}{#2}{#3}{\pgf@lib@dv@temp}% |
| 175 |
}% |
| 176 |
|
| 177 |
\def\pgf@lib@dv@step@var#1#2#3#4#5\pgf@stop{% |
| 178 |
{% |
| 179 |
\def\pgf@loop@body{#5}% |
| 180 |
\pgfmathparse{#2}% |
| 181 |
\pgfkeyslet{/data point/#1}{\pgfmathresult}% |
| 182 |
\pgfmathparse{#3}% |
| 183 |
\let\pgf@loop@end\pgfmathresult% |
| 184 |
\pgfmathparse{#4}% |
| 185 |
\let\pgf@loop@step\pgfmathresult% |
| 186 |
\pgfmathloop% |
| 187 |
\pgfmathlessthan{\pgf@loop@end}{\pgfkeysvalueof{/data point/#1}} |
| 188 |
\ifdim\pgfmathresult pt<1pt\relax% |
| 189 |
\pgf@loop@body\pgf@stop% |
| 190 |
\pgfmathparse{\pgfkeysvalueof{/data point/#1}+\pgf@loop@step}% |
| 191 |
\pgfkeyslet{/data point/#1}{\pgfmathresult}% |
| 192 |
\repeatpgfmathloop%% |
| 193 |
}% |
| 194 |
}% |
| 195 |
|
| 196 |
\def\pgf@lib@dv@group@var#1#2#3\pgf@stop{% |
| 197 |
\foreach \pgf@lib@dv@iterator in {#2} {% |
| 198 |
\pgfkeyslet{/data point/#1}{\pgf@lib@dv@iterator}% |
| 199 |
#3\pgf@stop% |
| 200 |
}% |
| 201 |
}% |
| 202 |
|
| 203 |
\def\pgf@lib@dv@eval@func#1#2{% |
| 204 |
\pgfkeysvalueof{/pgf/data/evaluator}{#2}% |
| 205 |
\pgfkeyslet{/data point/#1}{\pgfmathresult}% |
| 206 |
}% |
| 207 |
|
| 208 |
|
| 209 |
\endinput |