summaryrefslogtreecommitdiff
path: root/graphics/pgf/contrib/pgfplots/tex/pgfplotsmeshplotimage.code.tex
blob: b5a51cd3b2a32e001bc03a45d9df3ac19ae7259a (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
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
%--------------------------------------------
%
% 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 2007-2015 by Christian Feuersänger.
%
% 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 file essentially belongs to the 'mesh' plot handler.
% It implements the (rather involved) feature 'mesh input=image'.
%
% 'mesh input=image' is implemented during the visualization phase
% and, partially, during the survey phase. The survey phase merely
% updates the data limits. The visualization phase has the complete
% knowledge about the input matrix size and can compute the cells.

\def\pgfplotsplothandlersurveystart@mesh@image{%
	\let\pgfplotsplothandlermesh@image@lastA=\pgfutil@empty
	\let\pgfplotsplothandlermesh@image@lastlastA=\pgfutil@empty
	\let\pgfplotsplothandlermesh@image@lastB=\pgfutil@empty
	\def\b@pgfplotsplothandlermesh@image@updatedlimits@A@next{1}%
	\def\b@pgfplotsplothandlermesh@image@updatedlimits@B@next{0}%
	\def\c@pgfplotsplothandlermesh@image@numscanlines{0}%
	%
	\if\pgfplots@plot@mesh@ordering0%
		% ordering = x varies= rowwise -> scanline is cols!
		\def\pgfplotsplothandlermesh@image@A@dir{x}%
		\def\pgfplotsplothandlermesh@image@B@dir{y}%
		\def\pgfplotsplothandlermesh@image@updatelimits@AB##1##2##3{%
			\pgfplotsaxisupdatelimitsforcoordinate{##1}{##2}{##3}%
		}%
	\else
		% ordering = y varies = colwise: scanline is rows!
		\def\pgfplotsplothandlermesh@image@A@dir{y}%
		\def\pgfplotsplothandlermesh@image@B@dir{x}%
		\def\pgfplotsplothandlermesh@image@updatelimits@AB##1##2##3{%
			\pgfplotsaxisupdatelimitsforcoordinate{##2}{##1}{##3}%
		}%
	\fi
}%
\def\pgfplotsplothandlersurveyend@mesh@image{%
	\if1\pgfplotsaxisplothasjumps
		% the approach to interpolate cell corners using the adjacent cell midpoints becomes extremely complicated once 
		% we allow all kinds of holes in the input matrix.
		% If you want to make invisible cells, you can add "nan" as color data
		\pgfplots@error{Sorry, 'matrix plot' supports no holes (jumps) in the input data. Please implement jumps using an unbounded point meta value (i.e. inf or nan)}%
	\fi
	%
}%
\def\pgfplotsplothandlersurveypoint@mesh@image{%
	% already called:
	%\pgfplotsplothandlersurveypoint@default
	%
	% The 'update limits' routine for 'mesh input=image' is a
	% HEURISTICS.
	%
	% It works well for "real" image plots, i.e. for those which are
	% not skewed or changed parametrically.
	%
	% The main challenge is that we have to update the matrix limits
	% while we pass through the scanlines.
	%
	% The idea is:
	% - update cell midpoint limits using the standard PGFPlots
	%   methods. We would like to have the cell limits instead,
	%   though... to this end:
	% - update the lower x cell limit for the first encountered X in every
	%   scanline
	% - update the upper x cell limit for the last encountered X in every
	%   scanline
	% - update the lower AND upper y cell limit for the first encountered Y
	%   in every scanline.
	%
	% That means: 
	% - we do NOT update X limits for mid points
	% - we do NOT update Y limits for the second, third, ..., nth
	%   point on scanlines. Only for the first.
	%
	% For parametric image plots, these limits will be inaccurate. I
	% suppose these are rather exotic anyway, so I accept that to
	% simplify the implementation.
	%
	\expandafter\let\expandafter\pgfplotsplothandlermesh@cur\csname pgfplots@current@point@\pgfplotsplothandlermesh@image@A@dir\endcsname
	\expandafter\let\expandafter\pgfplotsplothandlermesh@cur@B\csname pgfplots@current@point@\pgfplotsplothandlermesh@image@B@dir\endcsname
	%
	\if1\b@pgfplotsplothandlermesh@image@updatedlimits@A@next
		\ifx\pgfplotsplothandlermesh@cur\pgfutil@empty
		\else
			\ifx\pgfplotsplothandlermesh@image@lastA\pgfutil@empty
			\else
				\pgfplotscoordmath{x}{parse}{\pgfplotsplothandlermesh@image@lastA - 0.5*(\pgfplotsplothandlermesh@cur - \pgfplotsplothandlermesh@image@lastA)}%
				\let\pgfplots@current@point@xh=\pgfmathresult
				\pgfplotsplothandlermesh@image@updatelimits@AB\pgfplots@current@point@xh\pgfplotsplothandlermesh@cur@B\pgfplots@current@point@z
				%
				\def\b@pgfplotsplothandlermesh@image@updatedlimits@A@next{0}%
			\fi
		\fi
	\fi
	%
	\if1\b@pgfplotsplothandlermesh@image@updatedlimits@B@next
		\ifx\pgfplotsplothandlermesh@cur@B\pgfutil@empty
		\else
			\ifx\pgfplotsplothandlermesh@image@lastB\pgfutil@empty
			\else
				\pgfplotscoordmath{y}{parse}{\pgfplotsplothandlermesh@image@lastB - 0.5*(\pgfplotsplothandlermesh@cur@B - \pgfplotsplothandlermesh@image@lastB)}%
				\let\pgfplots@current@point@yh=\pgfmathresult
				\pgfplotsplothandlermesh@image@updatelimits@AB\pgfplotsplothandlermesh@cur\pgfplots@current@point@yh\pgfplots@current@point@z
				%
				%
				\pgfplotscoordmath{y}{parse}{\pgfplotsplothandlermesh@cur@B + 0.5*(\pgfplotsplothandlermesh@cur@B - \pgfplotsplothandlermesh@image@lastB)}%
				\let\pgfplots@current@point@yh=\pgfmathresult
				\pgfplotsplothandlermesh@image@updatelimits@AB\pgfplotsplothandlermesh@cur\pgfplots@current@point@yh\pgfplots@current@point@z
				%
				\def\b@pgfplotsplothandlermesh@image@updatedlimits@B@next{0}%
			\fi
		\fi
	\fi
	\let\pgfplotsplothandlermesh@image@lastlastA=\pgfplotsplothandlermesh@image@lastA
	\let\pgfplotsplothandlermesh@image@lastA=\pgfplotsplothandlermesh@cur
	%
	\let\pgfplotsplothandlermesh@image@lastB=\pgfplotsplothandlermesh@cur@B
}%

\def\pgfplotsplothandlersurveyend@mesh@image@scanlinecomplete{%
	\ifx\pgfplotsplothandlermesh@image@lastlastA\pgfutil@empty
	\else
		\ifx\pgfplotsplothandlermesh@image@lastA\pgfutil@empty
		\else
			\expandafter\let\expandafter\pgfplotsplothandlermesh@cur@B\csname pgfplots@current@point@\pgfplotsplothandlermesh@image@B@dir\endcsname
			\pgfplotscoordmath{x}{parse}{\pgfplotsplothandlermesh@image@lastA + 0.5*(\pgfplotsplothandlermesh@image@lastA - \pgfplotsplothandlermesh@image@lastlastA)}%
			\let\pgfplots@current@point@xh=\pgfmathresult
			\pgfplotsplothandlermesh@image@updatelimits@AB\pgfplots@current@point@xh\pgfplotsplothandlermesh@cur@B\pgfplots@current@point@z
		\fi
	\fi
	%
	\def\b@pgfplotsplothandlermesh@image@updatedlimits@B@next{1}%
	%
	\let\pgfplotsplothandlermesh@image@lastlastA=\pgfutil@empty
	\let\pgfplotsplothandlermesh@image@lastA=\pgfutil@empty
	\def\b@pgfplotsplothandlermesh@image@updatedlimits@A@next{1}%
	%
	\pgfplotsutil@advancestringcounter\c@pgfplotsplothandlermesh@image@numscanlines
}%

%
% The 'mesh' plot handler has a "visualization pipeline" as part of
% its visualization:
%
% if it is active, the coordinate stream used for the visualization
% (\pgfplotsplothandlermesh@stream) invokes
% \pgfplotsplothandlermesh@PIPE@DECODE as first item. This, in turn,
% fires other elements of the mesh plot pipeline, but its most
% important task is to assemble patches while it goes. 
%
% That's where 'mesh input=image' hooks in: it simply generates special patches.

% this is part of the implementation of mesh input=image
% the pgf@plotstreampoint routine for the first scan line:
\def\pgfplotsplothandlermesh@PIPE@DECODE@image@fillscanline#1{%
	\pgfplotsplothandlermesh@PIPE@DECODE@matrix@fillscanline@common{#1}%
	\ifnum\c@pgfplots@scanlineindex=\pgfplotsplothandlermesh@scanlinelength\relax
		% second line!
		\let\pgfplotsplothandlermesh@PIPE@DECODE=\pgfplotsplothandlermesh@PIPE@DECODE@image@secondscanline%%
		\c@pgfplots@scanlineindex=0
		\def\c@pgfplotsplothandlermesh@image@nextrownum{2}%
	\fi
}%

% IDEA: 
%
% 1. The first scanline is merely buffered and collected into the deque 'lastscanline' (just as any mesh plot).
% 2. The second scanline 
%  - generates cells for the first scanline _and_
%  - appends the computed intermediate vertices to the deque 'lastinterpscanline'.
%  - during the sweep, a single cell of the first scanline is
%  generated
%  - only the _last_ element of the scanline generates two cells
% 3. All scanlines after the second 
%  - pop the intermediate vertices from the deque 
%  - use them to construct cells for the previous scanline
%  - append their computed intermediate vertices to the deque 'lastinterpscanline'.
%  - during the sweep, a single cell of the first scanline is
%  generated
%  - only the _last_ element of the scanline generates two cells (just as in step 2)
% 4. the last scanline is special in that it generates cells for the
% previous scanline _and_ the last scanline.
%
% The "computed intermediate vertices" are mean values and
% extrapolation points. My idea is to make this special cell-based
% mesh more flexible than a simple imagesc, hoping that this pays off.
\def\pgfplotsplothandlermesh@PIPE@DECODE@image@secondscanline#1{%
	\pgfplotsplothandlermesh@PIPE@DECODE@matrix@collect@and@callback{#1}{%
		%
		% create cell:
		\pgfplotsplothandlermesh@PIPE@DECODE@image@sweep@create@cell{1}%
		%
		\advance\c@pgfplots@scanlineindex by1
		\ifnum\c@pgfplots@scanlineindex=\pgfplotsplothandlermesh@scanlinelength\relax
			% ok, we have to extrapolate the last column.
			\pgfplotsplothandlermesh@PIPE@DECODE@image@sweep@create@cell@lastcolumn{1}%
		\fi
		\advance\c@pgfplots@scanlineindex by-1
	}{%
		% if scanline is completed, switch states:
		\let\pgfplotsplothandlermesh@PIPE@DECODE=\pgfplotsplothandlermesh@PIPE@DECODE@image@after@second%%
		\c@pgfplots@scanlineindex=0
		\pgfplotsutil@advancestringcounter\c@pgfplotsplothandlermesh@image@nextrownum
	}%
}%
\def\pgfplotsplothandlermesh@PIPE@DECODE@image@after@second#1{%
	\pgfplotsplothandlermesh@PIPE@DECODE@matrix@collect@and@callback{#1}{%
		\pgfplotsplothandlermesh@PIPE@DECODE@image@sweep@create@cell{0}%
		%
		\advance\c@pgfplots@scanlineindex by1
		\ifnum\c@pgfplots@scanlineindex=\pgfplotsplothandlermesh@scanlinelength\relax
			% ok, we have to extrapolate the last column.
			\pgfplotsplothandlermesh@PIPE@DECODE@image@sweep@create@cell@lastcolumn{0}%
		\fi
		\advance\c@pgfplots@scanlineindex by-1
	}{%
		% if scanline is completed: advance to next
		\c@pgfplots@scanlineindex=0
		\pgfplotsutil@advancestringcounter\c@pgfplotsplothandlermesh@image@nextrownum
	}%
}%

% To be used within the DECODE "loop" only! It assembles temporary
% variables for the next iteration (and consumes it).
%
% #1: '1' if topmost row is to be extrapolated, '0' if it should be
% acquired from the datastructure 'lastinterpscanline'
\def\pgfplotsplothandlermesh@PIPE@DECODE@image@sweep@create@cell#1{%
		\let\pgfplotsplothandlermesh@patchclass\pgfplotsplothandlermesh@patchclass@input
		%
		% we have to compute the four corners by means of
		% averaging/extrapolation.
		%
		% We place the rectangle around (i-1,j-1), i.e. imjm
		% Notation: the four corners are aa, ab, ba, and bb.
		%  aa -----  ab
		%  |         |
		%  |         |
		%  |   imjm  |          
		%  |         |     
		%  |         |
		%  ba ------ bb
		%
		% bb is the average of the four cell mid points of our
		% scanline, the others are acquired from previous iterations
		% or from extrapolation schemes.
		%
		% compute bb:
		\pgfplotspatchvertexaccumstart
		\pgfplotspatchvertexaccum@configure@ignore@unboundedmetadata
		\expandafter\pgfplotspatchvertexadd\pgfplotsplothandlermesh@im@jm\times{0.25}%
		\expandafter\pgfplotspatchvertexadd\pgfplotsplothandlermesh@im@j\times{0.25}%
		\expandafter\pgfplotspatchvertexadd\pgfplotsplothandlermesh@i@j\times{0.25}%
		\expandafter\pgfplotspatchvertexadd\pgfplotsplothandlermesh@i@jm\times{0.25}%
		\pgfplotspatchvertexfinish\pgfplotsplothandlermesh@bb
		\pgfplotspatchvertexcopymetaifbounded\pgfplotsplothandlermesh@i@j\to\pgfplotsplothandlermesh@bb
		%
		%
		\if1#1%
			% Ah -- this here is the very first row! That means we
			% have to extrapolate the topmost coordinates.
			%
			% Apply extrapolation for ab:
			%
			% compute ab = bb + 2* ( 1/2 (imj + imjm) - bb)
			% which resolves to the following:
			\pgfplotspatchvertexaccumstart
			\pgfplotspatchvertexaccum@configure@ignore@unboundedmetadata
			\expandafter\pgfplotspatchvertexadd\pgfplotsplothandlermesh@bb\times{-1.0}%
			\expandafter\pgfplotspatchvertexadd\pgfplotsplothandlermesh@im@j\times{1.0}%
			\expandafter\pgfplotspatchvertexadd\pgfplotsplothandlermesh@im@jm\times{1.0}%
			\pgfplotspatchvertexfinish\pgfplotsplothandlermesh@ab
			\pgfplotspatchvertexcopymetaifbounded\pgfplotsplothandlermesh@im@j\to\pgfplotsplothandlermesh@ab
		\else
			\ifnum\c@pgfplots@scanlineindex=1
				\pgfplotsdequepopfront{lastinterpscanline}\to\pgfplotsplothandlermesh@aa
			\fi
			\pgfplotsdequepopfront{lastinterpscanline}\to\pgfplotsplothandlermesh@ab
		\fi
		%
		\ifnum\c@pgfplots@scanlineindex=1
			% ah, we have to apply special treatment for the leftmost
			% cell.
			%
			%
			% compute ba = bb + 2* ( 1/2 (imjm + ijm) - bb)
			% which resolves to the following:
			\pgfplotspatchvertexaccumstart
			\pgfplotspatchvertexaccum@configure@ignore@unboundedmetadata
			\expandafter\pgfplotspatchvertexadd\pgfplotsplothandlermesh@bb\times{-1.0}%
			\expandafter\pgfplotspatchvertexadd\pgfplotsplothandlermesh@i@jm\times{1.0}%
			\expandafter\pgfplotspatchvertexadd\pgfplotsplothandlermesh@im@jm\times{1.0}%
			\pgfplotspatchvertexfinish\pgfplotsplothandlermesh@ba
			\pgfplotspatchvertexcopymetaifbounded\pgfplotsplothandlermesh@i@jm\to\pgfplotsplothandlermesh@ba
			%
			\if1#1%
				% ok, extrapolate 'aa' as well:
				%
				% compute aa = ba + (ab - bb) 
				% which resolves to the following:
				\pgfplotspatchvertexaccumstart
				\pgfplotspatchvertexaccum@configure@ignore@unboundedmetadata
				\expandafter\pgfplotspatchvertexadd\pgfplotsplothandlermesh@bb\times{-1.0}%
				\expandafter\pgfplotspatchvertexadd\pgfplotsplothandlermesh@ba\times{1.0}%
				\expandafter\pgfplotspatchvertexadd\pgfplotsplothandlermesh@ab\times{1.0}%
				\pgfplotspatchvertexfinish\pgfplotsplothandlermesh@aa
				\pgfplotspatchvertexcopymetaifbounded\pgfplotsplothandlermesh@im@jm\to\pgfplotsplothandlermesh@aa
			\else
				% in this case, 'aa' has been fetched from the deque
				% (from the previous scanline)
			\fi
			%
			% as soon as we process the next scanline, we need to know the
			% interpolated values -- they will become the top row of the
			% next scanline's cells.
			\expandafter\pgfplotsdequepushback\pgfplotsplothandlermesh@ba\to{lastinterpscanline}%
		\else
			% ok, we take the left edge (ba and aa) from the previous
			% iteration (see the end of this code where the previous
			% loop iteration prepared them).
		\fi
		%
		\pgfplotsplothandlermesh@image@set@first@color@to\pgfplotsplothandlermesh@im@jm\to\pgfplotsplothandlermesh@aa
		\expandafter\pgfplotsplothandlermesh@setnextvertex\expandafter{\pgfplotsretval}%
		\expandafter\pgfplotsplothandlermesh@setnextvertex\expandafter{\pgfplotsplothandlermesh@ab}%
		\expandafter\pgfplotsplothandlermesh@setnextvertex\expandafter{\pgfplotsplothandlermesh@bb}%
		\expandafter\pgfplotsplothandlermesh@setnextvertex\expandafter{\pgfplotsplothandlermesh@ba}%
		%
		% ... and communicate this as well to the next scanline (see
		% \pgfplotsplothandlermesh@PIPE@DECODE@image@after@second)
		\expandafter\pgfplotsdequepushback\pgfplotsplothandlermesh@bb\to{lastinterpscanline}%
		%
		\ifnum\c@pgfplotsplothandlermesh@image@nextrownum=\pgfplotsplothandlermesh@numscanlines\relax
			\pgfplotsplothandlermesh@PIPE@DECODE@image@sweep@create@cell@lastrow
		\fi
		%
		% merely shift the current points such that the next loop
		% iteration can use them without modifications:
		\let\pgfplotsplothandlermesh@ba=\pgfplotsplothandlermesh@bb
		\let\pgfplotsplothandlermesh@aa=\pgfplotsplothandlermesh@ab
		%
}%

% The color handling of 'matrix input=image' is quite involved.
% There are the following requirements:
% 1. the point meta of the cell's first vertex _must_ be equal to the point meta of the cell mid point, i.e. equal to the user input. 
% 2. All other vertices should be interpolated "as smart as possible". This interpolation scheme should work even if adjacent cell midpoints have _no_ bounded meta data!
% 3. The routines which glue adjacent cells together have to ensure that they do not accidentally intermix (1) and (2). This is complicated since the sweep loop intermingles both responsibilities (sigh).
%
% This helper function ensures (1): it copies the point meta of the \pgfplotspatchvertex #1 to the values of of #2 and assigns the result to \pgfplotsretval.
% #1 a valid \pgfplotspatchvertex
% #2 a valid \pgfplotspatchvertex
%
% POSTCONDITION: \pgfplotsretval contains the result, i.e. a copy of '#2' which contains the point meta of '#1'.
\def\pgfplotsplothandlermesh@image@set@first@color@to#1\to#2{%
	\let\pgfplotsretval=#2%
	\pgfplotspatchvertexcopymeta#1\to\pgfplotsretval
}%

\def\pgfplotsplothandlermesh@PIPE@DECODE@image@sweep@create@cell@lastrow{%
	%
	\let\pgfplotsplothandlermesh@last@aa=\pgfplotsplothandlermesh@ba
	\let\pgfplotsplothandlermesh@last@ab=\pgfplotsplothandlermesh@bb
	%
	% ASSUMPTION: we want to compute Laa, Lab, Lba and Lbb in the following setup:
	%
	%  aa -----  ab
	%  |         |
	%  |         |
	%  |   imjm  |          
	%  |         |     
	%  |         |
	%  ba ------ bb
	% =Laa       =Lab
	%  |         |
	%  |         |
	%  |   ijm   |          
	%  |         |     
	%  |         |
	%  Lba------ Lbb
	%
	% compute Lbb = bb + 2 * (1/2 ( ijm + ij) - bb ). We assume that
	% we can still access 'bb', the coordinate of the current point.
	% This resolves to the following:
	\pgfplotspatchvertexaccumstart
	\pgfplotspatchvertexaccum@configure@ignore@unboundedmetadata
	\expandafter\pgfplotspatchvertexadd\pgfplotsplothandlermesh@bb\times{-1.0}%
	\expandafter\pgfplotspatchvertexadd\pgfplotsplothandlermesh@i@jm\times{1.0}%
	\expandafter\pgfplotspatchvertexadd\pgfplotsplothandlermesh@i@j\times{1.0}%
	\pgfplotspatchvertexfinish\pgfplotsplothandlermesh@last@bb
	% FIXME : which is the correctly extrapolated cdata!?
	%\pgfplotspatchvertexcopymetaifbounded\pgfplotsplothandlermesh@i@j\to\pgfplotsplothandlermesh@last@bb
	%
	\ifnum\c@pgfplots@scanlineindex=1
		% compute Lba = Lbb + Laa - Lab
		\pgfplotspatchvertexaccumstart
		\pgfplotspatchvertexaccum@configure@ignore@unboundedmetadata
		\expandafter\pgfplotspatchvertexadd\pgfplotsplothandlermesh@last@bb\times{1.0}%
		\expandafter\pgfplotspatchvertexadd\pgfplotsplothandlermesh@last@aa\times{1.0}%
		\expandafter\pgfplotspatchvertexadd\pgfplotsplothandlermesh@last@ab\times{-1.0}%
		\pgfplotspatchvertexfinish\pgfplotsplothandlermesh@last@ba
		% FIXME : which is the correctly extrapolated cdata!?
		%\pgfplotspatchvertexcopymetaifbounded\pgfplotsplothandlermesh@i@jm\to\pgfplotsplothandlermesh@last@ba
	\else
		% transported from previous iteration.
	\fi
	%
	\pgfplotsplothandlermesh@image@set@first@color@to\pgfplotsplothandlermesh@i@jm\to\pgfplotsplothandlermesh@last@aa
	\expandafter\pgfplotsplothandlermesh@setnextvertex\expandafter{\pgfplotsretval}%
	\expandafter\pgfplotsplothandlermesh@setnextvertex\expandafter{\pgfplotsplothandlermesh@last@ab}%
	\expandafter\pgfplotsplothandlermesh@setnextvertex\expandafter{\pgfplotsplothandlermesh@last@bb}%
	\expandafter\pgfplotsplothandlermesh@setnextvertex\expandafter{\pgfplotsplothandlermesh@last@ba}%
	%
	\let\pgfplotsplothandlermesh@last@ba=\pgfplotsplothandlermesh@last@bb
}%

\def\pgfplotsplothandlermesh@PIPE@DECODE@image@sweep@create@cell@lastrow@lastcol{%
	%
	% ASSUMPTION: we want to compute Laa, Lab, Lba and Lbb in the following setup:
	%
	%  aa -----  ab
	%  |         |
	%  |         |
	%  |   imj   |          
	%  |         |     
	%  |         |
	%  ba ------ bb
	% =Laa       =Lab
	%  |         |
	%  |         |
	%  |   ij    |          
	%  |         |     
	%  |         |
	%  Lba------ Lbb
	\let\pgfplotsplothandlermesh@last@aa=\pgfplotsplothandlermesh@ba
	\let\pgfplotsplothandlermesh@last@ab=\pgfplotsplothandlermesh@bb
	%
	% I mirrored the formula for lastrow.
	\pgfplotspatchvertexaccumstart
	\pgfplotspatchvertexaccum@configure@ignore@unboundedmetadata
	\expandafter\pgfplotspatchvertexadd\pgfplotsplothandlermesh@last@bb\times{1.0}%
	\expandafter\pgfplotspatchvertexadd\pgfplotsplothandlermesh@last@aa\times{-1.0}%
	\expandafter\pgfplotspatchvertexadd\pgfplotsplothandlermesh@last@ab\times{1.0}%
	\pgfplotspatchvertexfinish\pgfplotsplothandlermesh@last@bb
	\pgfplotspatchvertexcopymetaifbounded\pgfplotsplothandlermesh@i@j\to\pgfplotsplothandlermesh@last@bb
	%
	\pgfplotsplothandlermesh@image@set@first@color@to\pgfplotsplothandlermesh@i@j\to\pgfplotsplothandlermesh@last@aa
	\expandafter\pgfplotsplothandlermesh@setnextvertex\expandafter{\pgfplotsretval}%
	\expandafter\pgfplotsplothandlermesh@setnextvertex\expandafter{\pgfplotsplothandlermesh@last@ab}%
	\expandafter\pgfplotsplothandlermesh@setnextvertex\expandafter{\pgfplotsplothandlermesh@last@bb}%
	% this value is transported from previous iteration:
	\expandafter\pgfplotsplothandlermesh@setnextvertex\expandafter{\pgfplotsplothandlermesh@last@ba}%
}%

\def\pgfplotsplothandlermesh@PIPE@DECODE@image@sweep@create@cell@lastcolumn#1{%
	\let\pgfplotsplothandlermesh@patchclass\pgfplotsplothandlermesh@patchclass@input
	%
	% we have to compute the four corners by means of
	% averaging/extrapolation.
	%
	% We place the rectangle around (i-1,j), i.e. imj.
	% This is in contrast to
	% \pgfplotsplothandlermesh@PIPE@DECODE@image@sweep@create@cell
	% where we placed the rectange around (i-1,j-1).
	%
	% Notation: the four corners are aa, ab, ba, and bb.
	%  aa -----  ab
	%  |         |
	%  |         |
	%  |   imj   |          
	%  |         |     
	%  |         |
	%  ba ------ bb
	%
	% We can simply copy aa and ba from the previous cell (as in
	% the standard case). Note that this has already been prepared
	% for us, i.e. the variables have already been set by the
	% previous iteration.
	%
	% compute ab and bb by means of extrapolation:
	%
	% compute bb:
	\pgfplotspatchvertexaccumstart
	\pgfplotspatchvertexaccum@configure@ignore@unboundedmetadata
	\expandafter\pgfplotspatchvertexadd\pgfplotsplothandlermesh@ba\times{-1.0}%
	\expandafter\pgfplotspatchvertexadd\pgfplotsplothandlermesh@im@j\times{1.0}%
	\expandafter\pgfplotspatchvertexadd\pgfplotsplothandlermesh@i@j\times{1.0}%
	\pgfplotspatchvertexfinish\pgfplotsplothandlermesh@bb
	% FIXME : which is the correctly extrapolated cdata!?
	%
	%
	\if1#1%
		% Ah -- this here is the very first row! That means we
		% have to extrapolate the topmost coordinates.
		%
		% Apply extrapolation for ab:
		%
		% compute ab = bb + (aa - ba)
		\pgfplotspatchvertexaccumstart
		\pgfplotspatchvertexaccum@configure@ignore@unboundedmetadata
		\expandafter\pgfplotspatchvertexadd\pgfplotsplothandlermesh@bb\times{1.0}%
		\expandafter\pgfplotspatchvertexadd\pgfplotsplothandlermesh@aa\times{1.0}%
		\expandafter\pgfplotspatchvertexadd\pgfplotsplothandlermesh@ba\times{-1.0}%
		\pgfplotspatchvertexfinish\pgfplotsplothandlermesh@ab
		% FIXME : which is the correctly extrapolated cdata!?
	\else
		\pgfplotsdequepopfront{lastinterpscanline}\to\pgfplotsplothandlermesh@ab
	\fi
	%
	\pgfplotsplothandlermesh@image@set@first@color@to\pgfplotsplothandlermesh@im@j\to\pgfplotsplothandlermesh@aa
	\expandafter\pgfplotsplothandlermesh@setnextvertex\expandafter{\pgfplotsretval}%
	\expandafter\pgfplotsplothandlermesh@setnextvertex\expandafter{\pgfplotsplothandlermesh@ab}%
	\expandafter\pgfplotsplothandlermesh@setnextvertex\expandafter{\pgfplotsplothandlermesh@bb}%
	\expandafter\pgfplotsplothandlermesh@setnextvertex\expandafter{\pgfplotsplothandlermesh@ba}%
	%
	% ... and communicate this as well to the next scanline (see
	% \pgfplotsplothandlermesh@PIPE@DECODE@image@after@second)
	\expandafter\pgfplotsdequepushback\pgfplotsplothandlermesh@bb\to{lastinterpscanline}%
	%
	\ifnum\c@pgfplotsplothandlermesh@image@nextrownum=\pgfplotsplothandlermesh@numscanlines\relax
		\pgfplotsplothandlermesh@PIPE@DECODE@image@sweep@create@cell@lastrow@lastcol
	\fi
}%