summaryrefslogtreecommitdiff
path: root/graphics/pgf/base/tex/pgflibrarydatavisualization.formats.functions.code.tex
blob: d1e87cb1f7f34ab5726ea4c33eb51bd075714c29 (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
% Copyright 2018 by Till Tantau
%
% This file may be distributed and/or modified
%
% 1. under the LaTeX Project Public License and/or
% 2. under the GNU Public License.
%
% See the file doc/generic/pgf/licenses/LICENSE for more details.

\ProvidesFileRCS{pgflibrarydatavisualization.formats.functions.code.tex}

\usepgfmodule{datavisualization}%




% Function evaluator
%
% (This uses TeX evaluation and is slow. *You have been warned*.)
%
% Description:
%
% The data contains the description of variables and functions.
%
% The format for the variables is
%
%   var <name> : interval "["<start>:<end>"]" [ step <steps> | samples <samples>];
%
% (The first two [ and ] are "real", the second pair means "optional",
% see the examples.)
%
%
% The format for the functions is
%
%   func <name> = <body>;
%
% Additionally, the variables and functions stored in the keys
% /pgf/data/vars and /pgf/data/funcs will also be used, the format for
% them is the same as above.
%
% The <name> is always the name of a (data point) attribute. Inside
% the <body> it can be accessed using the \value macro, which is
% defined locally, here.
%
% Newlines are not important, everything is gathered into a single
% line, the semicolons are used to detect the beginnings and ends of
% declarations.
%
% With everything setup in this way, for each variable, the variable
% ranges through the values between start and end, either with the
% given step or the given number of samples. If neither is given, the
% current value of /pgf/data visualization/samples is used. If there
% are multiple variables, they all range over their respective
% intervals independently. Thus, "var x = [0,1]; var y = [0:1];",
% assuming samples=25, gives you 625 data points.
%
% As the variables range over their respective values, the functions
% are evaluated using the function stored in /pgf/data/evaluator. By
% default this is set to \pgfmathparse, but
% you can use a different parser, if you like.
%
% Example:
%
% data [format=function] {
%
%   % This is a parametric plot with the parameter called t.
%   var t : interval [0:360] samples 60;
%
%   % Let's draw a circle:
%   func x = sin(\value t);
%   func y = cos(\value t);
% }
%
% data [format=function] {
%
%   % This a surface plot data...
%   var x : interval [0:1];
%   var y : interval [0:1];
%
%   % Let's draw a circle:
%   func height = \value x*\value x + \value y*\value y;
% }

\pgfdeclaredataformat{function}
{ % no catcode code
}
{ % Startup
  % Init collectors
  \let\value=\relax% avoid expansion
  \pgfkeysgetvalue{/pgf/data/vars}\pgf@lib@dv@collector
}
{% Line arguments
  #1%
}
{ % Line action
  \edef\pgf@lib@dv@collector{\pgf@lib@dv@collector\space #1}%
}
{% Ignore empty lines
}
{%
  \edef\pgf@lib@dv@collector{\pgf@lib@dv@collector\space \pgfkeysvalueof{/pgf/data/funcs}}%
  \let\pgf@lib@dv@parsed\pgfutil@empty%
  % launch parser on the result
  \expandafter\pgf@lib@dv@func@parse \pgf@lib@dv@collector\pgf@stop%
  \let\value\pgf@lib@dv@getval
  \pgf@lib@dv@parsed\pgfdatapoint\pgfutil@gobble\pgf@stop%
}%

\pgfkeys{
  /pgf/data/vars/.initial=,
  /pgf/data/funcs/.initial=,
  /pgf/data/evaluator/.initial=\pgfmathparse,
  /pgf/data/samples/.initial=25}%

\def\pgf@lib@dv@getval#1{\pgfkeysvalueof{/data point/#1}}%

\def\pgf@lib@dv@func@parse{%
  \pgfutil@ifnextchar\pgf@stop{% done
    \pgfutil@gobble%
  }
  {%
    \pgfutil@ifnextchar v{% variable
      \pgf@lib@dv@var%
    }{%
      \pgf@lib@dv@func%
    }%
  }%
}%

\def\pgf@lib@dv@func func #1 =#2;{%
  \expandafter\def\expandafter\pgf@lib@dv@parsed\expandafter{\pgf@lib@dv@parsed\pgf@lib@dv@eval@func{#1}{#2}}
  \pgf@lib@dv@func@parse%
}%
\def\pgf@lib@dv@var var #1 :{\pgfutil@ifnextchar\bgroup{\pgf@lib@dv@var@group{#1}}{\pgf@lib@dv@var@int{#1}}}%
\def\pgf@lib@dv@var@group#1#2;{%
  \expandafter\def\expandafter\pgf@lib@dv@parsed\expandafter{\pgf@lib@dv@parsed\pgf@lib@dv@group@var{#1}{#2}}%
  \pgf@lib@dv@func@parse%
}%
\def\pgf@lib@dv@var@int#1#2[#3:#4]{%
  \pgfutil@ifnextchar s{%
    \pgf@lib@dv@samplesorsteps{#1}{#3}{#4}%
  }{%
    \expandafter\def\expandafter\pgf@lib@dv@parsed\expandafter{%
      \pgf@lib@dv@parsed\pgf@lib@dv@samples@var{#1}{#3}{#4}{\pgfkeysvalueof{/pgf/data/samples}}}%
    \expandafter\pgf@lib@dv@func@parse\pgfutil@gobble%
  }
}%
\def\pgf@lib@dv@samplesorsteps#1#2#3s{%
  \pgfutil@ifnextchar t{% step
    \pgf@lib@dv@step{#1}{#2}{#3}%
  }{% sample
    \pgf@lib@dv@samples{#1}{#2}{#3}%
  }%
}%
\def\pgf@lib@dv@step#1#2#3tep#4;{%
  \expandafter\def\expandafter\pgf@lib@dv@parsed\expandafter{\pgf@lib@dv@parsed\pgf@lib@dv@step@var{#1}{#2}{#3}{#4}}%
  \pgf@lib@dv@func@parse%
}%
\def\pgf@lib@dv@samples#1#2#3amples#4;{%
  \expandafter\def\expandafter\pgf@lib@dv@parsed\expandafter{\pgf@lib@dv@parsed\pgf@lib@dv@samples@var{#1}{#2}{#3}{#4}}%
  \pgf@lib@dv@func@parse%
}%

%
% Executer...
%
\def\pgf@lib@dv@samples@var#1#2#3#4{%
  \pgfmathparse{#3}%
  \let\pgf@lib@dv@temp=\pgfmathresult%
  \pgfmathparse{#2}%
  \pgfmathparse{\pgf@lib@dv@temp-\pgfmathresult}%
  \pgfmathparse{\pgfmathresult/(#4-1)}%
  \let\pgf@lib@dv@temp=\pgfmathresult%
  \pgf@lib@dv@step@var{#1}{#2}{#3}{\pgf@lib@dv@temp}%
}%

\def\pgf@lib@dv@step@var#1#2#3#4#5\pgf@stop{%
  {%
    \def\pgf@loop@body{#5}%
    \pgfmathparse{#2}%
    \pgfkeyslet{/data point/#1}{\pgfmathresult}%
    \pgfmathparse{#3}%
    \let\pgf@loop@end\pgfmathresult%
    \pgfmathparse{#4}%
    \let\pgf@loop@step\pgfmathresult%
    \pgfmathloop%
      \pgfmathlessthan{\pgf@loop@end}{\pgfkeysvalueof{/data point/#1}}
    \ifdim\pgfmathresult pt<1pt\relax%
      \pgf@loop@body\pgf@stop%
      \pgfmathparse{\pgfkeysvalueof{/data point/#1}+\pgf@loop@step}%
      \pgfkeyslet{/data point/#1}{\pgfmathresult}%
    \repeatpgfmathloop%%
  }%
}%

\def\pgf@lib@dv@group@var#1#2#3\pgf@stop{%
  \foreach \pgf@lib@dv@iterator in {#2} {%
    \pgfkeyslet{/data point/#1}{\pgf@lib@dv@iterator}%
    #3\pgf@stop%
  }%
}%

\def\pgf@lib@dv@eval@func#1#2{%
  \pgfkeysvalueof{/pgf/data/evaluator}{#2}%
  \pgfkeyslet{/data point/#1}{\pgfmathresult}%
}%


\endinput