summaryrefslogtreecommitdiff
path: root/graphics/pgf/contrib/pgfplots/tex/meshplothandler.lua
blob: dba97aaf35096bebf5efb4cd018cf3bb6254c95b (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
-- This file has dependencies to BOTH, the TeX part of pgfplots and the LUA part.
-- It is the only LUA component with this property.
--
-- Its purpose is to encapsulate the communication between TeX and LUA in a central LUA file

local pgfplotsmath = pgfplots.pgfplotsmath
local error=error
local table=table
local string=string
local tostring=tostring
local type=type
local io=io

do
-- all globals will be read from/defined in pgfplots:
local _ENV = pgfplots


-------------------------------------------------------
-- A patch type.
-- @see \pgfplotsdeclarepatchclass

PatchType = newClass()

function PatchType:constructor(name, numVertices)
	self.name = name
	self.numVertices = numVertices
end

function PatchType:__tostring()
	return self.name
end

function PatchType:newPatch(coords)
	return Patch.new(self,coords)
end

LinePatchType = newClassExtends(PatchType)

function LinePatchType:constructor()
	PatchType.constructor(self, "line", 2)
end

TrianglePatchType = newClassExtends(PatchType)

function TrianglePatchType:constructor()
	PatchType.constructor(self, "triangle", 3)
end


RectanglePatchType = newClassExtends(PatchType)

function RectanglePatchType:constructor()
	PatchType.constructor(self, "rectangle", 4)
end

-------------------------------------------------------
--
-- a single patch.
-- @see \pgfplotsdeclarepatchclass

Patch = newClass()

function Patch:constructor(patchtype, coords)
	if not patchtype or not coords then error("arguments must not be nil") end
	if #coords ~= patchtype.numVertices then error("Unexpected number of coordinates provided; expected " .. tostring(patchtype.numVertices) .. " but got " .. tostring(#coords)) end

	self.patchtype = patchtype
	self.coords = coords
end

-------------------------------------------------------
-- Replicates \pgfplotsplothandlermesh (to some extend)
MeshPlothandler = newClassExtends(Plothandler)

function MeshPlothandler:constructor(axis, pointmetainputhandler)
    Plothandler.constructor(self,"mesh", axis, pointmetainputhandler)
end

-- see \pgfplot@apply@zbuffer
function MeshPlothandler:reverseScanline(scanLineLength)
    local coords = self.coords
    local tmp
    local scanlineOff
    local numScanLines = #coords / scanLineLength
    for scanline = 0,numScanLines-1,1 do
        scanlineOff = scanline * scanLineLength
        local reverseindex = scanlineOff + scanLineLength
        for i = 0,scanLineLength/2-1,1 do
            tmp = coords[1+scanlineOff+i]
            coords[1+scanlineOff+i] = coords[reverseindex]
            coords[reverseindex] = tmp

            reverseindex = reverseindex-1
        end
    end
end

-- see \pgfplot@apply@zbuffer
function MeshPlothandler:reverseTransposed(scanLineLength)
    local coords = self.coords
    local tmp
    local scanlineOff
    local numScanLines = #coords / scanLineLength
    local reverseScanline = numScanLines-1
    for scanline = 0,numScanLines/2-1,1 do
        scanlineOff = 1+scanline * scanLineLength
        reverseScanlineOff = 1+reverseScanline * scanLineLength
        for i = 0,scanLineLength-1 do
            tmp = coords[scanlineOff+i]
            coords[scanlineOff+i] = coords[reverseScanlineOff+i]
            coords[reverseScanlineOff+i] = tmp
        end

        reverseScanline = reverseScanline-1
    end
end

-- see \pgfplot@apply@zbuffer
function MeshPlothandler:reverseStream()
    local coords = self.coords
    local tmp
    local reverseindex = #coords
    for i = 1,#coords/2 do
        tmp = coords[i]
        coords[i] = coords[reverseindex]
        coords[reverseindex] = tmp
        reverseindex = reverseindex-1
    end
end



-------------------------------------------------------
--
-- The (LUA!) visualizer for patch plots. It prepares stuff such that TeX only needs to work with lowlevel driver (PGF) streams.
--

MeshVisualizer = newClassExtends(PlotVisualizer)

local COORDINATE_VALUE_OF_JUMPS = -16000
local meshVisualizerTagEmptyCoordinates = function(pt)
	pt.pgfXY= { COORDINATE_VALUE_OF_JUMPS, COORDINATE_VALUE_OF_JUMPS }
	pt.x = { COORDINATE_VALUE_OF_JUMPS, COORDINATE_VALUE_OF_JUMPS, COORDINATE_VALUE_OF_JUMPS }
end

function MeshVisualizer:constructor(sourcePlotHandler, patchType, rows, cols, isXVariesOrdering, isMatrixInput, isMatrixOutput, isZBufferSort)
	PlotVisualizer.constructor(self,sourcePlotHandler)
	self.patchType = patchType
	self.isMatrixInput = isMatrixInput
	self.isMatrixOutput = isMatrixOutput
	self.isZBufferSort = isZBufferSort
	self.rows = rows
	self.cols = cols
	self.isXVariesOrdering =isXVariesOrdering

	self.isOneDimMode= false
	self.scanLineLength =-1
	if isMatrixInput then
		-- isOneDimMode is ONLY interesting for matrix input
		if cols <= 1 or rows <=1 then
			self.isOneDimMode = true
			self.patchType = LinePatchType.new()
			-- this is not yet implemented (and cannot happen since the TeX call does catch this)
			error("UNSUPPORTED OPERATION EXCEPTION")
		end

		if isXVariesOrdering then
			-- x varies (=rowwise)
			self.scanLineLength = cols
		else
			-- y varies (=colwise)
			self.scanLineLength = rows
		end

		self.notifyJump = meshVisualizerTagEmptyCoordinates
	else
		-- disable any special handling
		self.isXVariesOrdering = true
	end

	-- log("initialized MeshVisualizer with " .. tostring(sourcePlotHandler) .. ", " .. tostring(patchType) .. ", isMatrixInput = " .. tostring(isMatrixInput) .. ", isMatrixOutput = " .. tostring(isMatrixOutput) .. ", isZBufferSort = " .. tostring(isZBufferSort) .. " rows = " ..tostring(rows) .. " cols = " ..tostring(cols) .. " is x varies=" .. tostring(isXVariesOrdering))
end

function MeshVisualizer:getVisualizationOutput()
	local result = PlotVisualizer.getVisualizationOutput(self)

	if self.isMatrixInput and not self.isMatrixOutput then
		result = self:decodeIntoPatches(result)
	end

	if self.isZBufferSort then
		result = self:applyZBufferSort(result)
	end

	return result
end

-- @param coords an array of Coord
function MeshVisualizer:applyZBufferSort(coords)
	-- in order to sort this thing, we need to compute the sort key (view depth) for each coord.
	-- furthermore, each list entry must be single patch... that means we need a (huge?) temporary table.

	local patchType = self.patchType
	local numVertices = patchType.numVertices

	if (#coords % numVertices) ~= 0 then error("Got an unexpected number of input coordinates: each patch has " .. tostring(numVertices) .. " vertices, but the number of coords " .. tostring(#coords) .. " is no multiple of this number") end
	local numPatches = #coords / numVertices

	-- STEP 1: compute an array of patches.
	local patches = {}
	local off=1
	for i = 1,numPatches do
		local patchCoords = {}
		for j = 1,numVertices do
			local pt = coords[off]
			off = off+1
			patchCoords[j] = pt
		end
		local patch = patchType:newPatch(patchCoords)
		patches[i] = patch
	end
	if off ~= 1+#coords then error("Internal error: not all coordinates are part of patches (got " .. tostring(off) .. "/" .. tostring(#coords) ..")") end

	-- STEP 2: assign the sort key: the "element depth".
	--
	-- the "element depth" is defined to be the MEAN of all
	-- vertex depths.
	-- And since the mean is 1/n * sum_{i=1}^n V_i, we can
	-- directly omit the 1/n --- it is the same for every
	-- vertex anyway, and we only want to compare the depth
	-- values.
	local axis = self.axis
	local getVertexDepth = axis.getVertexDepth
	for i=1,numPatches do
		local patch = patches[i]
		local patchcoords = patch.coords

		local sumOfVertexDepth = 0
		for j = 1,numVertices do
			local vertex = patchcoords[j]

			local vertexDepth = getVertexDepth(axis,vertex)

			sumOfVertexDepth = sumOfVertexDepth + vertexDepth
		end
		patch.elementDepth = sumOfVertexDepth
	end

	-- STEP 3: SORT.
	local comparator = function(patchA, patchB)
		return patchA.elementDepth > patchB.elementDepth
	end
	table.sort(patches, comparator)

	-- STEP 4: convert back into a list (in-place).
	local off = 1
	for i=1,numPatches do
		local patch = patches[i]
		local patchcoords = patch.coords
		for j = 1,numVertices do
			coords[off] = patchcoords[j]
			off = off+1
		end
	end
	if off ~= 1+#coords then error("Internal error: not all coordinates are part of patches (got " .. tostring(off) .. "/" .. tostring(#coords) ..")") end

	return coords
end

function MeshVisualizer:decodeIntoPatches(coords)
	local result = {}

	local scanLineLength = self.scanLineLength
	local length = #coords

	local i = scanLineLength
	while i < length do
		local im = i-scanLineLength

		for j = 2,scanLineLength do
			table.insert(result, coords[im+j-1]) -- (i-1,j-1)
			table.insert(result, coords[im+j])   -- (i-1,j  )
			table.insert(result, coords[i+j])    -- (i  ,j  )
			table.insert(result, coords[i+j-1])  -- (i  ,j-1)
		end

		i = i + scanLineLength
	end

	return result
end

end