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
|
-- Contains coordinate streamers, i.e. classes which generate coordinates and stream them to some output stream
local math=math
local pgfplotsmath = pgfplots.pgfplotsmath
local type=type
local tostring=tostring
local error=error
local table=table
do
-- all globals will be read from/defined in pgfplots:
local _ENV = pgfplots
-----------------------------------
CoordOutputStream = newClass()
function CoordOutputStream:constructor()
end
-- @param pt an instance of Coord
function CoordOutputStream:coord(pt)
end
-----------------------------------
SurveyCoordOutputStream = newClassExtends(CoordOutputStream)
function SurveyCoordOutputStream:constructor(targetPlothandler)
if not targetPlothandler then error("arguments must not be nil") end
self.plothandler= targetPlothandler
end
function SurveyCoordOutputStream:coord(pt)
self.plothandler:surveypoint(pt)
end
-------------
-- This is a partial reimplementation of \addplot expression: it samples points -- but entirely in LUA. Only the results are serialized back to TeX.
AddplotExpressionCoordinateGenerator = newClass()
function AddplotExpressionCoordinateGenerator:constructor(coordoutputstream, expressionsByDimension, domainMin, domainMax, samples, variableNames)
if not coordoutputstream or not expressionsByDimension or not domainMin or not domainMax or not samples or not variableNames then error("arguments must not be nil") end
if #variableNames ~= 2 then error("Expected 2 variableNames") end
self.coordoutputstream = coordoutputstream
self.is3d = #expressionsByDimension == 3
self.expressions = expressionsByDimension
self.domainMin = domainMin
self.domainMax = domainMax
self.samples = samples
self.variableNames = variableNames
-- log("initialized " .. tostring(self) .. "\n")
end
-- @return true on success or false if the operation cannot be carried out.
-- this method is a replicate of \pgfplots@addplotimpl@expression@@
function AddplotExpressionCoordinateGenerator:generateCoords()
local stringToFunctionMap = pgfluamathfunctions.stringToFunctionMap
-- create a backup of the 'x' and 'y' math expressions which
-- have been defined in \pgfplots@coord@stream@start:
local old_global_function_x = stringToFunctionMap["x"]
local old_global_function_y = stringToFunctionMap["y"]
local coordoutputstream = self.coordoutputstream
local is3d = self.is3d
local expressions = self.expressions
local xExpr = expressions[1]
local yExpr = expressions[2]
local zExpr = expressions[3]
local domainMin = self.domainMin
local domainMax = self.domainMax
local samples = self.samples
local h = {}
for i = 1,#domainMin do
h[i] = (domainMax[i] - domainMin[i]) / (samples[i]-1)
end
local variableNames = self.variableNames
local x,y
local sampleLine = #samples==1
local function pseudoconstantx() return x end
local pseudoconstanty
if sampleLine then
if yExpr ~= variableNames[2] then
-- suppress the warning - we want to allow (x,y,x^2) in this case.
pseudoconstanty = function() return 0 end
else
local didWarn = false
pseudoconstanty = function()
if not didWarn then
log("Sorry, you can't use 'y' in this context. PGFPlots expected to sample a line, not a mesh. Please use the [mesh] option combined with [samples y>0] and [domain y!=0:0] to indicate a twodimensional input domain\n")
didWarn = true
end
return 0
end
end
else
pseudoconstanty = function() return y end
end
local pgfmathparse = pgfluamathparser.pgfmathparse
local prepareX
if xExpr == variableNames[1] then
prepareX = function() return x end
else
prepareX = function() return pgfmathparse(xExpr) end
end
local prepareY
if yExpr == variableNames[2] then
prepareY = function() return y end
else
prepareY = function() return pgfmathparse(yExpr) end
end
local function computeXYZ()
stringToFunctionMap[variableNames[1]] = pseudoconstantx
stringToFunctionMap[variableNames[2]] = pseudoconstanty
local X = prepareX()
local Y = prepareY()
local Z = nil
if is3d then
Z = pgfmathparse(zExpr)
else
-- FIXME BUG: we need to do something else with 'Z' if this is a 2d plot inside of a 3d axis!
end
local pt = Coord.new()
pt.x = { X, Y, Z}
-- restore 'x' and 'y'
-- FIXME : defining the resulting x/y coordinates as 'x' and 'y' constants was a really really bad idea in the first place :-(
stringToFunctionMap["x"] = old_global_function_x
stringToFunctionMap["y"] = old_global_function_y
coordoutputstream:coord(pt)
end
if not sampleLine then
local xmin = domainMin[1]
local ymin = domainMin[2]
local hx = h[1]
local hy = h[2]
local max_i = samples[1]-1
local max_j = samples[2]-1
-- samples twodimensionally (a lattice):
for j = 0,max_j do
-- FIXME : pgfplots@plot@data@notify@next@y
y = ymin + j*hy
-- log("" .. j .. "\n")
for i = 0,max_i do
-- FIXME : pgfplots@plot@data@notify@next@x
x = xmin + i*hx
computeXYZ()
end
-- FIXME : \pgfplotsplothandlernotifyscanlinecomplete
end
else
local xmin = domainMin[1]
local hx = h[1]
local max_i = samples[1]-1
for i = 0,max_i do
-- FIXME : pgfplots@plot@data@notify@next@x
x = xmin + i*hx
computeXYZ()
end
end
stringToFunctionMap[variableNames[1]] = nil
stringToFunctionMap[variableNames[2]] = nil
return true
end
function AddplotExpressionCoordinateGenerator:__tostring()
local result = "AddplotExpressionCoordinateGenerator[\n"
result = result .. "\n variable(s)=" .. self.variableNames[1] .. " " .. self.variableNames[2]
result = result .. "\n expressions="
for i = 1,#self.expressions do
result = result .. self.expressions[i] ..", "
end
result = result .. "\n domain=" .. self.domainMin[1] .. ":" .. self.domainMax[1]
result = result .. "\n samples=" .. self.samples[1]
if #self.domainMin == 2 then
result = result .. "\n domain y=" .. self.domainMin[2] .. ":" .. self.domainMax[2]
result = result .. "\n samples y=" .. self.samples[2]
end
result = result .. "]"
return result
end
end
|