summaryrefslogtreecommitdiff
path: root/Master/texmf-dist/tex/generic/pgfplots/pgfplotsmeshplothandler.code.tex
blob: e1430c929cbb68dc059aad0367790fd2f3d114dc (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
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
%--------------------------------------------
%
% 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-2010 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/>.
%
%--------------------------------------------

% The low-level tikz plot handler for 3D mesh and surface plots.
%
% It expects 
% a) a coordinate stream which is a linearized matrix, iterated either
% rowwise or column wise,
% b) the current set of options in /pgfplots/mesh/.
%
%
% ATTENTION:
% 	this handler is NOT as general as the others. In particular, it
% 	can't be interrupted by TeX groups: every single statement,
% 	starting with streamstart and ending with stream end, must be in
% 	the same scope!
%
% 	As a consequence, you can't use this handler inside of (all) TikZ
% 	input streams. Try it out. It will work in pgfplots, however.
%
% Furthermore, it relies on a couple of pgfplots macros. This may
% change in future versions.
% It relies on:
% 	- \ifpgfplots@curplot@threedim
% 	- \pgfplots@current@point@meta
% 		-> it also invokes \pgfplots@perpointmeta@preparetrafo and its
% 		@APPLY counterpart which checks for
% 	- \pgfplots@metamax
% 		If you use it outside of pgfplots, use
% 		\global\let\pgfplots@metamax=\pgfutil@empty
% 		This is can be used without an initialised
% 		pgfplots axes - as long as \pgfplots@current@point@meta is in
% 		the range [0,1000].
% 	- for the case 'shader=interp', a bounding box must be established
% 	before the plot is finished. This must be done using 
%	\def\pgfplotspointbbupperright{\pgfqpointxyz{2}{2}{0.9}}
%	\def\pgfplotspointbblowerleft {\pgfqpointxyz{0}{0}{0.1}}
%	or something like that.
%
% Besides these internals, it also relies on all public /pgfplots
% configuration keys related to mesh/surface plots.
%
% Please note that the 'z buffer' feature will not work without
% pgfplots (especially the z buffer=sort feature).
%
% Here is an example which compiles without a pgfplots axes:
% ----------------------------------------------------------
% \begin{tikzpicture}
% 	\scope[
% 		/pgfplots/.cd,
% 		surf,
% 		shader=faceted,
% 		mesh/rows=4,
% 		mesh/cols=4,
% 		mesh/num points=,
% 		/tikz/x={(0.44237cm,-0.07439cm)}, 
% 		/tikz/y={(0.30942cm,0.23932cm)},
% 		/tikz/z={(0.0cm,1.5cm)},
% 	]
% \makeatletter
% 
% 	\global\let\pgfplots@metamax=\pgfutil@empty
% 	\pgfplots@curplot@threedimtrue
% 
% 	\pgfplotsplothandlermesh
% 	\pgfplotstreamstart
% 
% 	\def\rangea{0.21}%
% 	\def\rangeb{0.9}%
% 
% 	\pgfmathparse{1000/(\rangeb-\rangea)}
% 	\let\factor=\pgfmathresult
% 
% 	\def\simplecoordinate(#1,#2,#3){%
% 		\pgfmathparse{\factor*(#3 - \rangea)}%
% 		\let\pgfplots@current@point@meta=\pgfmathresult
% 		\pgfplotstreampoint{\pgfqpointxyz{#1}{#2}{#3}}%
% 	}%
% 	% for the case 'shaer=interp':
%	\def\pgfplotspointbbupperright{\pgfqpointxyz{2}{2}{0.9}}
%	\def\pgfplotspointbblowerleft {\pgfqpointxyz{0}{0}{0.1}}
% 
% \simplecoordinate(0,3,0.7)
% \simplecoordinate(1,3,0.5)
% \simplecoordinate(2,3,0.58)
% \simplecoordinate(3,3,0.9)
% 
% \simplecoordinate(0,2,0.68)
% \simplecoordinate(1,2,0.22)
% \simplecoordinate(2,2,0.25)
% \simplecoordinate(3,2,0.4)
% 
% \simplecoordinate(0,1,0.6)
% \simplecoordinate(1,1,0.3)
% \simplecoordinate(2,1,0.21)
% \simplecoordinate(3,1,0.3)
% 
% \simplecoordinate(0,0,0.8)
% \simplecoordinate(1,0,0.56)
% \simplecoordinate(2,0,0.5)
% \simplecoordinate(3,0,0.75)
% 
% 
% 	\pgfplotstreamend
% 	\pgfusepath{stroke}
% 
% 	\endscope
% \end{tikzpicture}
% ----------------------------------------------------------
%  END OF EXAMPLE
%  -> see also the legend image for mesh plots.
%
\def\pgfplotsplothandlermesh{%
	\pgfkeysgetvalue{/pgfplots/patch type}\pgfplotsplothandlermesh@patchclass
	\ifx\pgfplotsplothandlermesh@patchclass\pgfplotsplothandlermesh@patchclass@defaulttext
		\let\pgfplotsplothandlermesh@patchclass=\pgfutil@empty
	\fi
	% FIXME : detect 1d mode here!
	\ifx\pgfplotsplothandlermesh@patchclass\pgfutil@empty
		% assign default patch class.
		\if1\pgfplotsplothandlermesh@matrixinput
			\def\pgfplotsplothandlermesh@patchclass{rectangle}%
		\else
			\def\pgfplotsplothandlermesh@patchclass{triangle}%
		\fi
		\pgfkeyslet{/pgfplots/patch type}\pgfplotsplothandlermesh@patchclass
	\fi
	%
	\if0\pgfplotsplothandlermesh@matrixinput
		% mesh input=patches: simply skip empty input lines:
		\pgfkeyssetvalue{/pgfplots/empty line}{none}%
	\fi
	%
	\let\pgfplotsplothandlersurveystart=\pgfplotsplothandlersurveystart@mesh
	\let\pgfplotsplothandlersurveyend=\pgfplotsplothandlersurveyend@mesh
	\let\pgfplotsplothandlersurveyaftersetpointmeta=\pgfplotsplothandlersurveyaftersetpointmeta@mesh@cdatanormalize
	%
	\pgfkeysgetvalue{/pgfplots/patch table/value}\pgfplotsplothandlermesh@patchtable
	\ifx\pgfplotsplothandlermesh@patchtable\pgfutil@empty
	\else
		\def\pgfplotsplothandlermesh@matrixinput{0}% implies 'patch input=patches'
		\let\pgfplotsplothandlersurveystart=\pgfplotsplothandlersurveystart@mesh@patchtable
		\let\pgfplotsplothandlersurveypoint=\pgfplotsplothandlersurveypoint@mesh@patchtable
		\let\pgfplotsplothandlersurveyend=\pgfplotsplothandlersurveyend@mesh@patchtable
	\fi
	\if0\pgfplotsplothandlermesh@colorinput
		% mesh/color input=colormap
	\else
		% mesh/color input=explicit
		\if0\csname pgfpmeta@\pgfplotspointmetainputhandler @issymbolic\endcsname
			\pgfplotsthrow{invalid argument}{\pgfplots@loc@TMPa}{Sorry, color input!=colormap requires 'point meta=symbolic' or 'explicit symbolic'. Please adjust the format of your input point meta data}\pgfeov%
		\fi
	\fi
	%
	\def\pgf@plotstreamstart{%
%		\scope
		%
		% this shouldn't be necessary, but it doesn't hurt either.
		\pgfplots@perpointmeta@preparetrafo
		%
		\def\pgfplotsplothandlermesh@ONEDIMMODE{0}%
		\pgfplotsplothandlermesh@init@meshkeys
		%
		%
		\let\pgfplotsplothandlermesh@patchclass@input=\pgfplotsplothandlermesh@patchclass
		\let\pgfplotsplothandlermesh@patchclass@output=\pgfplotsplothandlermesh@patchclass@input
		\if0\pgfplotsplothandlermesh@triangulate
		\else
			\pgfplotspatchclass{\pgfplotsplothandlermesh@patchclass@input}{triangulate class}%
			\let\pgfplotsplothandlermesh@patchclass@output=\pgfplotsretval
		\fi
		%
		\pgfplotspatchclass{\pgfplotsplothandlermesh@patchclass@input}{init}%
		\ifx\pgfplotsplothandlermesh@patchclass@input\pgfplotsplothandlermesh@patchclass@output
		\else
			\pgfplotspatchclass{\pgfplotsplothandlermesh@patchclass@output}{init}%
		\fi
		%
		% assign boolean:
		\let\b@pgfplotsplothandlermesh@matrixoutput=\pgfplotsplothandlermesh@matrixinput
		\pgfplotspatchclass{\pgfplotsplothandlermesh@patchclass@output}{allow matrix}%
		\if0\pgfplotsretval
			\def\b@pgfplotsplothandlermesh@matrixoutput{0}%
		\fi
		\ifnum\pgfkeysvalueof{/pgfplots/patch refines}>0
			\def\b@pgfplotsplothandlermesh@matrixoutput{0}%
		\fi
		\if1\pgfplotsaxisplothasjumps
			\def\b@pgfplotsplothandlermesh@matrixoutput{0}%
		\fi
		\if\pgfplotsplothandlermesh@zbuffer@choice4% 'z buffer=sort':
			\def\b@pgfplotsplothandlermesh@matrixoutput{0}%
		\fi
		%
		\if2\pgfplotsplothandlermesh@shader
			% shader=interp
			%
			\pgfkeysgetvalue{/pgfplots/mesh/interior colormap name}\pgfplots@loc@TMPc
			\ifx\pgfplots@loc@TMPc\pgfutil@empty
			\else
				\pgfplotswarning{interior colormap inefficient}\pgfeov%
				\pgfplotsset{shader=faceted interp,faceted color=none}%
			\fi
		\fi
		\if2\pgfplotsplothandlermesh@shader
			% shader=interp
			\if0\pgfplotsplothandlermesh@triangulate
			 	% patch to triangles=false
			\else
				% no matrix output possible:
				\def\b@pgfplotsplothandlermesh@matrixoutput{0}%
			\fi
		\else
			% 'shader!=interp'
			\def\b@pgfplotsplothandlermesh@matrixoutput{0}%
		\fi
		\pgfplotspatchclass{\pgfplotsplothandlermesh@patchclass@output}{get pdf shading type}%
		\if5\pgfplotsretval
		\else
			\def\b@pgfplotsplothandlermesh@matrixoutput{0}%
		\fi
		%
		% special handling: check if Type=5 shadings need to be
		% converted to Type=4
		\if0\b@pgfplotsplothandlermesh@matrixoutput
			% no matrix output possible
			\if5\pgfplotsretval
				% pdf shading type=5 (matrix output)
				\if2\pgfplotsplothandlermesh@shader
					% shader=interp
					%
					% FIXME : I am unsure if this here is still
					% necessary as of 3fc734d7ac3020600c06258b1bc80b22da53edf3
					% -> perhaps the check can be skipped entirely.
					%
					% oh. shading type=5 is *essentially* the same as
					% matrix output, but matrix output is forbidden. 
					% In that case, triangulate the matrix and shade the
					% triangles:
					\def\pgfplotsplothandlermesh@triangulate{1}%
					\def\pgfplotsplothandlermesh@patchclass@output{triangle}%
					% Note shader=faceted interp  can be written as
					% matrix -- it shades individual elements anyway;
					% and one single patch can be written as matrix.
				\fi
			\fi
		\fi
		%
		\pgfplotsplothandlermesh@init@DECODE
		\pgfplotsplothandlermesh@init@REFINE
		%
		\pgfutil@IfUndefined{pgfplots@drawmodes}{%
			\let\pgfplots@drawmodes=\tikz@mode
			\let\pgfplots@drawoptions=\tikz@options
		}{}%
		%
		%
		% Prepare color data source:
		\ifx\pgfplotspointmetainputhandler\pgfutil@empty%
			% oh. There is no color data!? 
			\pgfplotsplothandlermesh@init@path@without@point@meta
			%
		\else
			\pgfplotsplothandlermesh@init@cdata
			\pgfplotsplothandlermesh@init@shader
			\pgfplotsplothandlermesh@init@flat@color
		\fi
		% PREPARE THE STREAM PROCESSING:
		% handling of jumps.
		\pgfplotsplothandlermesh@init@jumphandling
		%
		\ifpgfplotsplothandlermesh@shownormals
			\let\pgfplotsplothandlermesh@show@normals@if@configured=\pgfplotsplothandlermesh@show@normals
			% actually store 3d coordinates:
			\let\pgfplotsplothandlermesh@serialize@logical@coords=\pgfplotsplothandlermesh@serialize@logical@coords@doit
		\fi
		%
		% z buffering:
		\pgfplotsplothandlermesh@init@zbuffer
		%
		\pgfplotsplothandlermesh@init@mesh@stream
		%
		\c@pgfplots@scanlineindex=0
	}%
	%
	\let\pgfplotsplothandlerifcurrentpointcanbefirstlast=\pgfplotsplothandlerifcurrentpointcanbefirstlast@mesh
}%

\def\pgfplotsplothandlerifcurrentpointcanbefirstlast@mesh#1#2{%
	\pgfplotspatchclass{\pgfplotsplothandlermesh@patchclass@output}{if current point can be first last}{#1}{#2}%
}%

% Assigns default values to 'rows' and 'cols' in case
% their values are empty.
%
% PRECONDITION:
% 	The values of
% - '/pgfplots/mesh/rows' 
% - '/pgfplots/mesh/cols'
% - '/pgfplots/mesh/num points'
% - \pgfplotsscanlinelength
% - '/pgfplots/mesh/ordering'
%   are assigned properly.
%
% POSTCONDITION:
% 	'rows' and 'cols' now incorporates \pgfplotsscanlinelength if
% 	their values had been empty. However, there may still be empty
% 	values.
%
% @see \pgfplotsautocompletemeshkeys
\def\pgfplotspreparemeshkeydefaults{%
	\pgfplots@autocomplete@meshkeys0%
}%

% Completes and sanitizes 'mesh/rows' and 'mesh/cols' such that they
% have useful integer values afterwards.
%
% This method works similarly to \pgfplotspreparemeshkeydefaults.
% However, it really completes values and issues error messages if
% that proves to be impossible.
%
% PRECONDITION:
% 	The values of
% - '/pgfplots/mesh/rows' 
% - '/pgfplots/mesh/cols'
% - '/pgfplots/mesh/num points'
% - '/pgfplots/mesh/ordering'
%   are assigned properly.
%   If 
% - \pgfplotsscanlinelength
%   exists and is positive, it may also be used.
%
% POSTCONDITION:
% 	'rows' and 'cols' have useful integer values.
\def\pgfplotsautocompletemeshkeys{%
	\pgfplots@autocomplete@meshkeys1%
}%
\def\pgfplots@autocomplete@meshkeys#1{%
	% For #1 = 0, the values are only partially completed (which is
	% also compatible with patch plots)
	\pgfkeysgetvalue{/pgfplots/mesh/rows}\pgfplotsplothandlermesh@rows
	\pgfkeysgetvalue{/pgfplots/mesh/cols}\pgfplotsplothandlermesh@cols
	\pgfkeysgetvalue{/pgfplots/mesh/num points}\pgfplotsplothandlermesh@numpoints
	\edef\pgfplotsplothandlermesh@numpoints{\pgfplotsplothandlermesh@numpoints}%
	\edef\pgfplotsplothandlermesh@cols{\pgfplotsplothandlermesh@cols}%
	\edef\pgfplotsplothandlermesh@rows{\pgfplotsplothandlermesh@rows}%
	%
	\def\pgfplots@loc@TMPa{0}%
	\ifpgfplots@curplot@threedim
		\def\pgfplots@loc@TMPa{1}%
	\else
		\if s\pgfplots@meshmode
			% we have a 'surf' plot. That doesn't make sense unless
			% one has a matrix, even for a two--dimensional plot. Try it.
			\def\pgfplots@loc@TMPa{1}%
		\fi
	\fi
	\if1\pgfplots@loc@TMPa
		\ifx\pgfplotsplothandlermesh@rows\pgfutil@empty
			\ifx\pgfplotsplothandlermesh@cols\pgfutil@empty
				\pgfutil@ifundefined{pgfplotsscanlinelength}{\def\pgfplotsscanlinelength{-1}}{}%
				\ifpgfplots@scanline@verbose
					\ifnum\pgfplotsscanlinelength>0
						% we have scanlinelength set! Use it:
						\message{PGFPlots: Scanline length is \pgfplotsscanlinelength.}%
					\else
						\ifnum\pgfplotsscanlinelength=-2
							\message{PGFPlots: Scanline length could not be determined since multiple, different scan line lengths have been found.}%
						\else
							\message{PGFPlots: No end-of-scanline marker found in input stream.}%
						\fi
					\fi
				\fi
				\ifnum\pgfplotsscanlinelength>0
					% we have scanlinelength set! Use it:
					\if\pgfplots@plot@mesh@ordering0%
						% ordering = x varies= rowwise -> scanline is cols!
						\edef\pgfplotsplothandlermesh@cols{\pgfplotsscanlinelength}%
					\else
						% ordering = y varies = colwise: scanline is rows!
						\edef\pgfplotsplothandlermesh@rows{\pgfplotsscanlinelength}%
					\fi
				\else
					\if1#1%
						% try to get reasonable values:
						\ifx\pgfplotsplothandlermesh@numpoints\pgfutil@empty
							\pgfplots@error{Sorry, a mesh needs either '[mesh/rows=<N>]' or '[mesh/cols=<N>]' set - or at least '[mesh/num points=<N>]'. But it did not find any of them...}%
							\def\pgfplotsplothandlermesh@cols{5}%
							\def\pgfplotsplothandlermesh@rows{5}%
						\else
							% We don't have rows/cols. Well, try using
							% sqrt(num points) for both of them!
							\ifnum\pgfplotsplothandlermesh@numpoints<16380
								\pgfmath@basic@sqrt@{\pgfplotsplothandlermesh@numpoints}%
							\else
								\pgfmathfloatparsenumber{\pgfplotsplothandlermesh@numpoints}%
								\pgfmathfloatsqrt@{\pgfmathresult}%
								\pgfmathfloattofixed{\pgfmathresult}%
							\fi
							\begingroup
							% Try whether this rows*rows yields numpoints:
							\afterassignment\pgfplots@gobble@until@relax
							\c@pgf@counta=\pgfmathresult\relax
							\c@pgf@countb=\c@pgf@counta
							\multiply\c@pgf@counta by\c@pgf@counta
							\def\pgfplotsretval{1}%
							\ifnum\c@pgf@counta=\pgfplotsplothandlermesh@numpoints\relax
								% ok, rows*rows = numpoints.
								\edef\pgfplotsplothandlermesh@rows{\the\c@pgf@countb}%
							\else
								% oh. Did not work. Check for rounding errors:
								\c@pgf@counta=\c@pgf@countb
								\advance\c@pgf@counta by1
								\c@pgf@countb=\c@pgf@counta
								\multiply\c@pgf@counta by\c@pgf@counta
								\ifnum\c@pgf@counta=\pgfplotsplothandlermesh@numpoints
									% ok, (rows+1)*(rows+1) = numpoints.
									\edef\pgfplotsplothandlermesh@rows{\the\c@pgf@countb}%
								\else
									% no... did not work. num points doesn't
									% appear to be a squared number.
									\def\pgfplotsretval{0}%
									\edef\pgfplotsplothandlermesh@rows{5}%
								\fi
							\fi
							\if\pgfplotsretval1
								\pgfplotswarning{matrix delinearize uses sqrt}{\pgfplotsplothandlermesh@rows}{\pgfplotsplothandlermesh@cols}{\pgfplotsplothandlermesh@numpoints}{\pgfmathresult}\pgfeov%
							\else
								\pgfplots@error{Sorry, 'plot mesh' needs either '[mesh/rows=<N>]' or '[mesh/cols=<N>]' set,
									but it did not find any of them. 
									I also tried rows = sqrt(num points) = sqrt(\pgfplotsplothandlermesh@numpoints) = \pgfmathresult\space without success.}%
							\fi
							\pgfmath@smuggleone\pgfplotsplothandlermesh@rows
							\endgroup
							\let\pgfplotsplothandlermesh@cols=\pgfplotsplothandlermesh@rows
						\fi
					\else
						% #1=0 -> leave them empty.
					\fi
				\fi
			\fi
		\fi
		\if1#1%
			\ifx\pgfplotsplothandlermesh@rows\pgfutil@empty
				\ifx\pgfplotsplothandlermesh@cols\pgfutil@empty
				\else
					% Compute 'rows' out of 'num points' and 'cols':
					\ifx\pgfplotsplothandlermesh@numpoints\pgfutil@empty
						\pgfplots@error{Sorry, matrix input needs a valid '[mesh/num points=<N>]' variable if only one of '[mesh/rows=<N>]' or '[mesh/cols=<N>]' is given.}%
						\def\pgfplotsplothandlermesh@numpoints{100}%
					\fi
					\begingroup
					\c@pgf@counta=\pgfplotsplothandlermesh@numpoints\relax
					\divide\c@pgf@counta by\pgfplotsplothandlermesh@cols\relax
					\xdef\pgfplots@glob@TMPa{\the\c@pgf@counta}%
					\endgroup
					\let\pgfplotsplothandlermesh@rows=\pgfplots@glob@TMPa
				\fi
			\else
				\ifx\pgfplotsplothandlermesh@cols\pgfutil@empty
					% Compute 'cols' out of 'num points' and 'rows':
					\ifx\pgfplotsplothandlermesh@numpoints\pgfutil@empty
						\pgfplots@error{Sorry, matrix input needs a valid '[mesh/num points=<N>]' variable if only one of '[mesh/cols=<N>]' or '[mesh/rows=<N>]' is given.}%
						\def\pgfplotsplothandlermesh@numpoints{100}%
					\fi
					\begingroup
					\c@pgf@counta=\pgfplotsplothandlermesh@numpoints\relax
					\divide\c@pgf@counta by\pgfplotsplothandlermesh@rows\relax
					\xdef\pgfplots@glob@TMPa{\the\c@pgf@counta}%
					\endgroup
					\let\pgfplotsplothandlermesh@cols=\pgfplots@glob@TMPa
				\fi
			\fi
			\ifx\pgfplotsplothandlermesh@numpoints\pgfutil@empty
				\c@pgfplots@scanlineindex=\pgfplotsplothandlermesh@rows\relax
				\multiply\c@pgfplots@scanlineindex by \pgfplotsplothandlermesh@cols\relax
				\edef\pgfplotsplothandlermesh@numpoints{\the\c@pgfplots@scanlineindex}%
			\fi
			%
			\c@pgfplots@scanlineindex=\pgfplotsplothandlermesh@rows\relax
			\multiply\c@pgfplots@scanlineindex by\pgfplotsplothandlermesh@cols\relax
			\ifnum\c@pgfplots@scanlineindex=\pgfplotsplothandlermesh@numpoints\relax
			\else
				\ifcase\pgfplots@mesh@checkmode\relax
					% mesh/check=false
					\def\pgfplots@loc@TMPa##1{}%
				\or
					% mesh/check=warning
					\def\pgfplots@loc@TMPa##1{\pgfplotsthrow@warning{##1}}%
				\or
					% mesh/check=error
					\def\pgfplots@loc@TMPa##1{\pgfplots@error{##1}}%
				\fi
				\pgfplotswarning{matrix check}{\pgfplots@loc@TMPa}{\pgfplotsplothandlermesh@rows}{\pgfplotsplothandlermesh@cols}{\pgfplotsplothandlermesh@numpoints}{\the\c@pgfplots@scanlineindex}\pgfeov%
			\fi
		\fi
	\else
		% 2d visualization has no mesh:
		\edef\pgfplotsplothandlermesh@rows{\pgfplotsplothandlermesh@numpoints}%
		\def\pgfplotsplothandlermesh@cols{1}%
	\fi
	%
	\def\pgfplots@loc@TMPa{\pgfkeyssetvalue{/pgfplots/mesh/rows}}%
	\expandafter\pgfplots@loc@TMPa\expandafter{\pgfplotsplothandlermesh@rows}%
	\def\pgfplots@loc@TMPa{\pgfkeyssetvalue{/pgfplots/mesh/cols}}%
	\expandafter\pgfplots@loc@TMPa\expandafter{\pgfplotsplothandlermesh@cols}%
}%

%% 
%% Helper structs for patch plots (surfaces)

% #1: the canvas x coordinate
% #2: the canvas y coordinate
% #3: the point meta data (cdata)
% #4: the vertex'es depth (if required)
% #5: the 3d coordinates of the vertex (will only be stored if it is deemed necessary)
\def\pgfplotspatchvertex#1#2#3#4:#5\endvertex{%
	\def\pgfplotspatchvertexstruct{{#1}{#2}{#3}#4:#5}%
	\def\pgfplotspatchvertexx{#1}%
	\def\pgfplotspatchvertexy{#2}%
	\def\pgfplotspatchvertexmeta{#3}%
	\def\pgfplotspatchvertexdepth{#4}%
	\def\pgfplotspatchvertexcoords{#5}%
}%
\def\pgfplotspointpatchvertex#1#2#3#4:#5\endvertex{%
	\global\pgf@x=#1
	\global\pgf@y=#2
}%

% Allows to combined different vertices in the form
% \pgfplotspatchvertexaccumstart
% \expandafter\pgfplotspatchvertexadd\vertexA\times{0.25}%
% \expandafter\pgfplotspatchvertexadd\vertexB\times{0.5}%
% \expandafter\pgfplotspatchvertexadd\vertexC\times{0.75}%
% \pgfplotspatchvertexfinish\finalvertex
% --> now, \finalvertex contains the result vertex, 0.25*A + 0.5*B + 0.75*C .
%
% THERE MUST NOT BE MATH INBETWEEN.
% More precisely, this overwrites the macros
% \pgf@x,\pgf@y, \pgf@xa,\pgf@ya, \pgf@yb, \pgfmathresult and \pgfplotspatchvertexdepth
\def\pgfplotspatchvertexaccumstart{%
	\pgf@xa=0pt
	\pgf@ya=0pt
	\pgf@yb=0pt
	\if0\pgfplotsplothandlermesh@colorinput
		% mesh/color input=colormap
		\pgfplotscoordmath{meta}{zero}%
	\else
		% mesh/color input=explicit
		\begingroup
		\pgfplotscolorzero{\pgfplotsplothandlermesh@explicit@color@num@comps}%
		\let\pgfmathresult=\pgfplotsretval
		\pgfmath@smuggleone\pgfmathresult
		\endgroup
	\fi
}%

% #1: the patch's x coordinate
% #2: the patch's y coordinate
% #3: the patch's point meta value
% #4: depth (if required)
% #5: 3d coordinate of the vertex
\def\pgfplotspatchvertexadd#1#2#3#4:#5\times#6{%
	\global\pgf@x=#1 %
	\global\pgf@y=#2 %
	\advance\pgf@xa by#6\pgf@x
	\advance\pgf@ya by#6\pgf@y
	\let\pgfmathresult@=\pgfmathresult
	\if0\pgfplotsplothandlermesh@colorinput
		% mesh/color input=colormap
		\pgfplotscoordmath{meta}{parsenumber}{#6}%
		\pgfplotscoordmath{meta}{op}{multiply}{{#3}{\pgfmathresult}}%
		\pgfplotscoordmath{meta}{op}{add}{{\pgfmathresult@}{\pgfmathresult}}%
	\else
		% mesh/color input=explicit
		\begingroup
		\pgfplotscoloraddweighted{\pgfplotsplothandlermesh@explicit@color@num@comps}
			{\pgfmathresult@}{#6}{#3}%
		\let\pgfmathresult=\pgfplotsretval
		\pgfmath@smuggleone\pgfmathresult
		\endgroup
	\fi
	\def\pgfplotspatchvertexdepth{#4}%
	\ifx\pgfplotspatchvertexdepth\pgfutil@empty
	\else
		\global\pgf@y=#4pt %
		\advance\pgf@yb by#6\pgf@y
	\fi
}%
\def\pgfplotspatchvertexfinish#1{%
	\edef#1{%
		{\the\pgf@xa}%
		{\the\pgf@ya}%
		{\pgfmathresult}%
		\ifx\pgfplotspatchvertexdepth\pgfutil@empty\else\pgf@sys@tonumber\pgf@yb\fi%
		:%
	}%
}
% Same as \pgfplotspatchvertexadd, but it only adds the X and Y
% coordinates (ignoring the rest).
\def\pgfplotspatchvertexaddXY#1#2#3#4:#5\times#6{%
	\global\pgf@x=#1
	\global\pgf@y=#2
	\advance\pgf@xa by#6\pgf@x
	\advance\pgf@ya by#6\pgf@y
}%
% Same as \pgfplotspatchvertexfinish, but it only saves X and Y.
\def\pgfplotspatchvertexfinishXY#1{%
	\edef#1{{\the\pgf@xa}{\the\pgf@ya}{}:}%
}

% #1 class name
% #2 method
% <any method arguments follow>
\def\pgfplotspatchclass#1#2{%
	\pgfutil@ifundefined{pgfpptch@#1@#2}{%
		\pgfplotspatchclasserror{#1}{#2}%
	}{%
		\edef\pgfplotspatchclassname{#1}%
		\csname pgfpptch@#1@#2\endcsname
	}%
}%
\def\pgfplotspatchclasserror#1#2{%
	\pgfutil@ifundefined{pgfpptch@#1@}{%
		\pgfplots@error{Sorry, the 'patch type=#1' is not defined. Maybe you misspelled it? Or perhaps you need to load \string\usepgfplotslibrary{patchplots}?}%
	}{%
		\pgfplots@error{Sorry, the patch class '#1' has no method '#2' (or it is not yet implemented)}%
	}%
}%

% #1: will be expanded with \edef
\def\pgfplotsplothandlermesh@setnextvertex#1{%
	\pgfplotspatchclass{\pgfplotsplothandlermesh@patchclass}{set next vertex}{#1}%
}%

% Declares a new patch class named #1.
% 
% #1: a string name.
% #2: methods defining the class, provided as key values. See the
% predefined classes to adjust your own ones. Documentation about the
% available methods and their arguments can be found below in this
% file.
%
% @see \usepgfplotslibrary{patchplots}
\def\pgfplotsdeclarepatchclass#1#2{%
	\edef\pgfplotsdeclarepatchclass@{@#1@}%
	\pgfutil@ifundefined{pgfpptch@#1@}{%
		% Ok. This is the FIRST declaration of this patch class.
		% Define some default algorithms.
		%
		\expandafter\def\csname pgfpptch@#1@\endcsname{#1}%
		\pgfqkeys{/pgfplots/@declare patch}{%
			init=,
			allow matrix=\def\pgfplotsretval{0},
			init pdf shading=,
			stream to shader={%
%\message{\pgfplotspatchclassname: stream to shader: Starting  ^^J}%
				\pgfplotspatchclass{\pgfplotspatchclassname}{foreach vertex}{%
%\message{vertex (\pgfplotspatchvertexx,\pgfplotspatchvertexy[\pgfplotspatchvertexmeta]. ^^J}%
					\let\pgfplots@current@point@meta=\pgfplotspatchvertexmeta
					\pgfplotsaxisvisphasetransformpointmeta
					\pgfplotslibrarysurfstreamcoord{%
						\global\pgf@x=\pgfplotspatchvertexx\space
						\global\pgf@y=\pgfplotspatchvertexy\space
					}{\pgfplotspointmetatransformed}%
				}%
			},%
			fill path={%
				\def\b@pgfplotsmeshplothandler@fillpath@default@isfirst{1}%
				\pgfplotspatchclass{\pgfplotspatchclassname}{foreach vertex}{%
%\message{vertex (\pgfplotspatchvertexx,\pgfplotspatchvertexy[\pgfplotspatchvertexmeta]. ^^J}%
					\if1\b@pgfplotsmeshplothandler@fillpath@default@isfirst
						\expandafter\pgfpathmoveto
					\else
						\expandafter\pgfpathlineto
					\fi
					{\global\pgf@x=\pgfplotspatchvertexx\space\global\pgf@y=\pgfplotspatchvertexy\space}%
					\def\b@pgfplotsmeshplothandler@fillpath@default@isfirst{0}%
				}%
				\pgfpathclose
			},
			get num cdata vertices=\pgfplotspatchclass{\pgfplotspatchclassname}{get num vertices},
			get dimension=\def\pgfplotsretval{2},
			supports global path=\def\pgfplotsretval{0},
			set edge visible=\expandafter\edef\csname pgfppatchedge@##1\endcsname{##2},
			uses view depth=\def\pgfplotsretval{0},
			if current point can be first last=##1,
			serialize except vertices=\let\pgfplotsretval\pgfutil@empty,
			deserialize except vertices=##1,
			serialize={%
				% the default serialization is not necessarily the fastest
				% possible. But it should work.
				\pgfplotspatchclass{\pgfplotspatchclassname}{serialize except vertices}%
				\t@pgfplots@toka=\expandafter{\pgfplotsretval
					\let\pgfplotspatchready@defaultserialize=\pgfplotspatchready
					\let\pgfplotspatchready=\relax
				}%
				\edef\pgfplotsretval{%
					\noexpand\pgfplotspatchclass{\pgfplotspatchclassname}{new}%
					\the\t@pgfplots@toka
				}%
				\pgfplotspatchclass{\pgfplotspatchclassname}{foreach vertex}{%
					\t@pgfplots@toka=\expandafter{\pgfplotsretval}%
					\edef\pgfplotsretval{\the\t@pgfplots@toka
						\noexpand\pgfplotspatchclass{\pgfplotspatchclassname}{set next vertex}{\pgfplotspatchvertexstruct}%
					}%
				}%
				\expandafter\def\expandafter\pgfplotsretval\expandafter{\pgfplotsretval
					\let\pgfplotspatchready=\pgfplotspatchready@defaultserialize
				}%
			},
			deserialize=##1,
			triangulate class=\def\pgfplotsretval{triangle},
			triangulate={%
				\pgfplots@error{triangulate: not yet implemented for \pgfplotspatchclassname}%
			},%
			refine={%
				\let\pgfplotsrefinedpatchready=\pgfplotspatchready
				\pgfplotspatchclass@recursiverefine{##1}%
			},%
			recursive refine@={%
				\pgfplots@error{refine: not yet implemented for \pgfplotspatchclassname}%
			},%
			compute normal={%
				\pgfplotsplothandlermesh@compute@normal@default
			},%
			sample in unit cube={%
				\pgfplots@error{sample in unit cube: not implemented for \pgfplotspatchclassname}%
			},
		}%
	}{}%
	%
	%
	\pgfqkeys{/pgfplots/@declare patch}{#2}%
	%
	%
	\pgfutil@ifundefined{pgfpptch\pgfplotsdeclarepatchclass@ foreach cdata vertex}{%
		\pgfutil@namelet
			{pgfpptch\pgfplotsdeclarepatchclass@ foreach cdata vertex}
			{pgfpptch\pgfplotsdeclarepatchclass@ foreach vertex}%
	}{%
	}%
	\pgfutil@ifundefined{pgfpptch\pgfplotsdeclarepatchclass@ stroke path}{%
		\pgfutil@namelet
			{pgfpptch\pgfplotsdeclarepatchclass@ stroke path}
			{pgfpptch\pgfplotsdeclarepatchclass@ fill path}%
	}{%
	}%
}%
\pgfqkeys{/pgfplots/@declare patch}{%
	% may do any more or less global preparations. It is some sort (!)
	% of constructor.
	init/.code=
		{\expandafter\def\csname pgfpptch\pgfplotsdeclarepatchclass@ init\endcsname{#1}},%
	allow matrix/.code=
		{\expandafter\def\csname pgfpptch\pgfplotsdeclarepatchclass@ allow matrix\endcsname{#1}},%
	%
	% FIXME : this is NOT properly used in higher order patches!?
	% Uses view depth defines \pgfplotsretval to '1' if and only if it
	% does ALWAYS needs the view depth. Note that 'z buffer=sort' will
	% automatically provide the view depth. Use this key if view depth
	% is necessary even for 'z buffer!=sort'.
	uses view depth/.code=
		{\expandafter\def\csname pgfpptch\pgfplotsdeclarepatchclass@ uses view depth\endcsname{#1}},%
	%
	% creates a new patch. The vertices need to be added by 'set next
	% vertex'
	new/.code=
		{\expandafter\def\csname pgfpptch\pgfplotsdeclarepatchclass@ new\endcsname{#1}},%
	%
	% sets the next vertex value of the patch.
	%
	% if the patch has all expected vertices, \pgfplotspatchready is
	% invoked and the next call to 'set next vertex' will start a new
	% patch.
	%
	% #1: encodes the current vertex such that
	%   \pgfplotspatchvertex#1\endvertex
	% is accepted.
	set next vertex/.code=
		{\expandafter\def\csname pgfpptch\pgfplotsdeclarepatchclass@ set next vertex\endcsname##1{#1}},%
	%
	%
	% Creates a new patch by means of sampling. The sampling is
	% applied in the unit cube [0,1]x[0,1] (unit interval if this is an 1d patch
	% type).
	%
	% 'sample in unit cube={<code to invoke for each sampled point>}'
	% 
	% It will invoke <code to invoke for each sampled point> once for
	% every sampled vertex. The complete unit cube will be covered.
	% Depending on the patch type, either one or more patches are
	% required to fill the unit cube. As soon as one patch is ready,
	% \pgfplotspatchready will be invoked.
	%
	%
	% PRECONDITION: 
	%  -\pgfplotspatchready is defined. 
	%
	% Arguments:
	%  #1: code which will be invoked whenever a new coordinate is
	%  ready. Inside of #1, the macros \pgfplotspatchclassx and
	%  \pgfplotspatchclassy result in the x and y coordinate (inside
	%  of the unit cube), in fixed point representation.
	%
	% Example:
	%   \def\pgfplotspatchready{}
	%   \pgfplotspatchclass{biquadratic}{sample in unit cube}{%
	%       point ready at (\pgfplotspatchclassx, \pgfplotspatchclassy)
	%   }
	sample in unit cube/.code=
		{\expandafter\def\csname pgfpptch\pgfplotsdeclarepatchclass@ sample in unit cube\endcsname##1{#1}},%
	%
	% declares whether a single edge is visible or not.
	% #1: an integer denoting the edge, either 0,1,2,...,N-1 where N = num verts
	% #2: 0 if the edge is not to be stroked and 1 if it is stroked.
	%
	% This command doesn't need to be called; but if it is, it needs
	% to be called for every edge.
	%
	% ATTENTION: this is *only* used by 'stroke path'. You will never
	% need 'stroke path' unless some edges are invisible. If all edges
	% are visible, 'stroke path' and 'fill path' are the same.
	%
	% This is currently *only* used for triangle triangulations!
	set edge visible/.code=
		{\expandafter\def\csname pgfpptch\pgfplotsdeclarepatchclass@ set edge visible\endcsname##1##2{#1}},%
	%
	% Defines \pgfplotsretval to be a serialized representation of
	% this patch.
	serialize/.code=
		{\expandafter\def\csname pgfpptch\pgfplotsdeclarepatchclass@ serialize\endcsname{#1}},%
	% Defines \pgfplotsretval to be a serialized representation of
	% this patches extra data fields, excluding vertices.
	% This is often a NO-OP (unless 'set edge visible' has been used)
	serialize except vertices/.code=
		{\expandafter\def\csname pgfpptch\pgfplotsdeclarepatchclass@ serialize except vertices\endcsname{#1}},%
	%
	% Restores a serialized representation from #1.
	deserialize/.code=
		{\expandafter\def\csname pgfpptch\pgfplotsdeclarepatchclass@ deserialize\endcsname##1{#1}},%
	% Restores a serialized representation from #1. Only vertices are
	% not stored here.
	deserialize except vertices/.code=
		{\expandafter\def\csname pgfpptch\pgfplotsdeclarepatchclass@ deserialize except vertices\endcsname##1{#1}},%
	%
	% issues \pgfplotspatchvertex{...}\endvertex for the first vertex.
	first vertex/.code=
		{\expandafter\def\csname pgfpptch\pgfplotsdeclarepatchclass@ first vertex\endcsname{#1}},%
	%
	% invokes 
	% 	\pgfplotspatchvertex...\endvertex
	% 	#1
	% for the every vertex.
	foreach vertex/.code=
		{\expandafter\def\csname pgfpptch\pgfplotsdeclarepatchclass@ foreach vertex\endcsname##1{#1}},%
	%
	% invokes 
	% 	\pgfplotspatchvertex...\endvertex
	% 	#1
	% for every colored vertex.
	% See 'get num cdata vertices'
	foreach cdata vertex/.code=
		{\expandafter\def\csname pgfpptch\pgfplotsdeclarepatchclass@ foreach cdata vertex\endcsname##1{#1}},%
	%
	% generates a pgf path which is readily usable for filling.
	fill path/.code=
		{\expandafter\def\csname pgfpptch\pgfplotsdeclarepatchclass@ fill path\endcsname{#1}},%
	%
	% Similar to 'fill path', this here paints the edges. It is only
	% useful if 'set edge visible' is in use. In all other cases, it
	% is the same as 'fill path'
	%
	% This is currently *only* used for triangle triangulations!
	stroke path/.code=
		{\expandafter\def\csname pgfpptch\pgfplotsdeclarepatchclass@ stroke path\endcsname{#1}},%
	%
	init pdf shading/.code=
		{\expandafter\def\csname pgfpptch\pgfplotsdeclarepatchclass@ init pdf shading\endcsname{#1}},%
	stream to shader/.code=
		{\expandafter\def\csname pgfpptch\pgfplotsdeclarepatchclass@ stream to shader\endcsname{#1}},%
	%
	% defines \pgfplotsretval to contain the number of vertices.
	get num vertices/.code=
		{\expandafter\def\csname pgfpptch\pgfplotsdeclarepatchclass@ get num vertices\endcsname{#1}},%
	%
	% defines \pgfplotsretval to contain the number of vertices which have color data
	% attached. This is usually the same as 'get num vertices' (but
	% see coons patches).
	get num cdata vertices/.code=
		{\expandafter\def\csname pgfpptch\pgfplotsdeclarepatchclass@ get num cdata vertices\endcsname{#1}},%
	%
	% defines \pgfplotsretval to contain the dimension of this patch
	% (either 1 for lines or 2 for patches).
	get dimension/.code=
		{\expandafter\def\csname pgfpptch\pgfplotsdeclarepatchclass@ get dimension\endcsname{#1}},%
	%
	% defines \pgfplotsretval to be 1 if and only if the patch class
	% supports global paths.
	% A global path is one which is drawn as one huge, uninterrupted
	% PGF path (except for jumps perhaps). Its area can be filled,
	% i.e. it behaves exactly like the 'sharp' plot handler. This is
	% used implicitly if 'point meta=none' (if there is point meta
	% data, we always want individual path segments and \pgfusepath
	% them after each segment).
	supports global path/.code=
		{\expandafter\def\csname pgfpptch\pgfplotsdeclarepatchclass@ supports global path\endcsname{#1}},%
	%
	% Approximates the given patch by at least one triangle.
	% For every finished triangle, \pgfplotspatchready will be invoked
	% and \pgfplotsplothandlermesh@patchclass will be set to triangle.
	% More precisely, it is expected to be set to the return value of
	% 'triangulate class'.
	triangulate/.code=
		{\expandafter\def\csname pgfpptch\pgfplotsdeclarepatchclass@ triangulate\endcsname{#1}},%
	% Defines \pgfplotsretval to be the classname returned by
	% 'triangulate'. This is usually 'triangle'
	triangulate class/.code=
		{\expandafter\def\csname pgfpptch\pgfplotsdeclarepatchclass@ triangulate class\endcsname{#1}},%
	%
	% Refines the given patch #1 times.
	% For every finished patch, \pgfplotspatchready will be invoked.
	refine/.code=
		{\expandafter\def\csname pgfpptch\pgfplotsdeclarepatchclass@ refine\endcsname##1{#1}},%
	%
	% shared helper method which can be invoked recursively by refine.
	% the default implementation of 'refine' will invoke this one.
	%
	% #1: a serialized representation of the given patch. Use it to
	% restore the current patch in intermediate steps. Note that you
	% may not use \begingroup/\endgroup in this method; otherwise the
	% 'z buffer' algorithm will need to be done with \global (and I
	% don't want that. You probably neither).
	%
	% The refinement loop as such will be controlled by the default
	% implementation of 'refine'. This macro is ONLY to be called by the
	% default implementation of 'refine'! Overwriting it should
	% automatically yield refinement facilities.
	%
	% the macro \pgfplotspatchready should be called for each refined
	% element (happens automatically if you use 'set next vertex').
	%
	% POSTCONDITION:
	% 	the element has been refined once.
	recursive refine@/.code=
		{\expandafter\def\csname pgfpptch\pgfplotsdeclarepatchclass@ recursive refine@\endcsname##1{#1}},%
	%
	get pdf shading type/.style={get pdf shading type*={\def\pgfplotsretval{#1}}},
	get pdf shading type*/.code=
		{\expandafter\def\csname pgfpptch\pgfplotsdeclarepatchclass@ get pdf shading type\endcsname{#1}},%
	%
	% A method which defines \pgfplotsretval to be the normal of the current segment.
	% The values will be given in a format understood by
	% \pgfplotsmathvectortostring{\pgfplotsretval}{default}
	% 
	% This method REQUIRES that 3d coordinates are stored (see \pgfplotsplothandlermesh@serialize@logical@coords).
	compute normal/.code=
		{\expandafter\def\csname pgfpptch\pgfplotsdeclarepatchclass@ compute normal\endcsname{#1}},%
	%
	% executes '##1' if the current point can be the first or last
	% point of the patch. Executes ##2 if that is not the case.
	if current point can be first last/.code=
		{\expandafter\def\csname pgfpptch\pgfplotsdeclarepatchclass@ if current point can be first last\endcsname##1##2{#1}},%
}

% This implements 'compute normal' :
\def\pgfplotsplothandlermesh@compute@normal@default{%
	\begingroup
	\c@pgf@countd=0
	\pgfplotspatchclass{\pgfplotspatchclassname}{foreach vertex}{%
		\ifx\pgfplotspatchvertexcoords\pgfutil@empty
			\pgfplotsplothandlermesh@compute@normal@error
		\fi
		\pgfplotsmathvectorfromstring{\pgfplotspatchvertexcoords}{default}%
		\expandafter\let\csname point\the\c@pgf@countd\endcsname=\pgfplotsretval
		\let\pointlast=\pgfplotsretval
		\advance\c@pgf@countd by1 %
	}%
	\pgfplotsmathvectorsubtract{\csname point1\endcsname}{\csname point0\endcsname}{default}%
	\let\diffA=\pgfplotsretval
	% Hm. usually, I would use \point2 here... but it turned out that
	% the result looks ugly for rectangular patches -- so I used the
	% last point instead (which is, at least, consistent with the
	% patch boundaries)
	\pgfplotsmathvectorsubtract{\csname pointlast\endcsname}{\csname point0\endcsname}{default}%
	\let\diffB=\pgfplotsretval
	\pgfplotsmathvectorcrossprod{\diffA}{\diffB}{default}%
	\let\crossAB=\pgfplotsretval
	% this here is an experiment to use the MEAN of two normal
	% vectors for rectangular patches... but it did not work as good
	% as expected:
	% \pgfplotspatchclass{\pgfplotspatchclassname}{get num vertices}%
	% \ifnum\pgfplotsretval=4
	% 	\pgfplotsmathvectorsubtract{\csname point3\endcsname}{\csname point0\endcsname}{default}%
	% 	\let\diffC=\pgfplotsretval
	% 	\pgfplotsmathvectorcrossprod{\diffB}{\diffC}{default}%
	% 	\let\crossBC=\pgfplotsretval
	% 	\pgfplotsmathvectoradd{\crossAB}{\crossBC}{default}%
	% 	\pgfplotscoordmath{default}{parsenumber}{0.5}%
	% 	\pgfplotsmathvectorscale{\pgfplotsretval}{\pgfmathresult}{default}%
	% 	\let\normal=\pgfplotsretval
	% \else
		\let\normal=\crossAB
	%\fi
	%
	% specific choices of z buffering change the sequence of points.
	% If the number of reversals is off, we need to switch the
	% normal's sign:
	\def\pgfplots@switch@normal@dir{0}%
	\if1\pgfplotsplothandlermesh@zbuffer@choice
		% z buffer=reverse x seq
		\def\pgfplots@switch@normal@dir{1}%
	\else
		\if2\pgfplotsplothandlermesh@zbuffer@choice
			% z buffer=reverse y seq
			\def\pgfplots@switch@normal@dir{1}%
		\fi
	\fi
	\if1\pgfplots@switch@normal@dir
		\pgfplotscoordmath{default}{parsenumber}{-1}%
		\pgfplotsmathvectorscale{\normal}{\pgfmathresult}{default}%
	\fi
	\pgfmath@smuggleone\pgfplotsretval
	\endgroup
}%
\def\pgfplotsplothandlermesh@compute@normal@error{%
	\pgfplots@error{Cannot compute normal vectors: due to some reason, only canvas coordinates of the involved patch are available in this context, but the original input coordinates are required. This indicates an incompatible combination of variables (sorry). Please report this as a bug}%
}%

\def\pgfplotspatchclass@recursiverefine#1{%
	\c@pgf@counta=#1
	\ifnum\c@pgf@counta=1
		\let\pgfplotspatchready=\pgfplotsrefinedpatchready
	\else
		\advance\c@pgf@counta by-1
		\edef\pgfplotspatchready{\noexpand\pgfplotspatchclass@recursiverefine{\the\c@pgf@counta}}%
	\fi
	%
	\pgfplotspatchclass{\pgfplotspatchclassname}{serialize}%
	\t@pgfplots@toka=\expandafter{\pgfplotsretval}%
	\t@pgfplots@tokc=\expandafter{\pgfplotspatchready}%
	\edef\pgfplots@loc@TMPa{%
		\noexpand\pgfplotspatchclass{\pgfplotspatchclassname}{deserialize}{\the\t@pgfplots@toka}%
		\noexpand\def\noexpand\pgfplotspatchready{\the\t@pgfplots@tokc}%
		\noexpand\def\noexpand\pgfplotspatchclassname{\pgfplotspatchclassname}% % FIXME: is that correct?
	}%
	\expandafter\pgfplotspatchclass@recursiverefine@\expandafter{\pgfplots@loc@TMPa}%
}%
\def\pgfplotspatchclass@recursiverefine@{%
	\pgfplotspatchclass{\pgfplotspatchclassname}{recursive refine@}%
}%


% sequence macros preferred over integer indices.
% using characters avoids the extensive use of \csname ...\endcsname
% and \expandafter which would be necessary for integer indices.
\def\pgfpptchindexnext@A{B}% 0->1
\def\pgfpptchindexnext@B{C}% 1->2
\def\pgfpptchindexnext@C{D}% 2->3
\def\pgfpptchindexnext@D{E}% 3->4
\def\pgfpptchindexnext@E{F}% 4->5
\def\pgfpptchindexnext@F{G}% 5->6
\def\pgfpptchindexnext@G{H}% 6->7
\def\pgfpptchindexnext@H{I}% 7->8
\def\pgfpptchindexnext@I{J}% 8->9
\def\pgfpptchindexnext@J{K}% 9->10
\def\pgfpptchindexnext@K{L}% 10->11
\def\pgfpptchindexnext@L{M}% 11->12
\def\pgfpptchindexnext@M{N}% 12->13
\def\pgfpptchindexnext@N{O}% 13->14
\def\pgfpptchindexnext@O{P}% 14->15
\def\pgfpptchindexnext@P{Q}% 15->16
\def\pgfpptchindexnext@Q{R}% 16->17

\pgfplotsdeclarepatchclass{line}{%
	allow matrix=\def\pgfplotsretval{0},
	new=\def\pgfplotspatchclass@line@no{0},
	set next vertex={%
		\if0\pgfplotspatchclass@line@no\relax
			\edef\pgfplotspatchclass@line@A{#1}%
			\def\pgfplotspatchclass@line@no{1}%
		\else
			\edef\pgfplotspatchclass@line@B{#1}%
			\def\pgfplotspatchclass@line@no{0}%
			\pgfplotspatchready%
		\fi
	},
	sample in unit cube={%
		\def\pgfplotspatchclassx{0}%
		\def\pgfplotspatchclassy{0}%
		#1%
		\def\pgfplotspatchclassx{1}%
		\def\pgfplotspatchclassy{0}%
		#1%
		\pgfplotspatchready
	},%
	get dimension=\def\pgfplotsretval{1},
	supports global path=\def\pgfplotsretval{1},
	first vertex=\expandafter\pgfplotspatchvertex\pgfplotspatchclass@line@A\endvertex,
	foreach vertex={%
		\expandafter\pgfplotspatchvertex\pgfplotspatchclass@line@A\endvertex #1%
		\expandafter\pgfplotspatchvertex\pgfplotspatchclass@line@B\endvertex #1%
	},
	fill path={%
		\pgfplotsplothandlermesh@pathmoveto{\expandafter\pgfplotspointpatchvertex\pgfplotspatchclass@line@A\endvertex}%
		\pgfpathlineto{\expandafter\pgfplotspointpatchvertex\pgfplotspatchclass@line@B\endvertex}%
		\pgfplotsplothandlermesh@setlastpoint{\expandafter\pgfplotspointpatchvertex\pgfplotspatchclass@line@B\endvertex}%
	},
	serialize except vertices=\let\pgfplotsretval\pgfutil@empty,%
	deserialize except vertices=,
	get num vertices=\def\pgfplotsretval{2},
	get pdf shading type=0,
}%

\def\pgfplotsplothandlermesh@last@x{-16000pt}
\def\pgfplotsplothandlermesh@last@y{-16000pt}

\def\pgfplotsplothandlermesh@pathmoveto#1{%
	\pgf@process{#1}%
	\pgfpathmoveto{}%
}
\def\pgfplotsplothandlermesh@setlastpoint#1{%
}

% Applies a moveto to #1 - but only if #1 is NOT the last used path
% segment.
\def\pgfplotsplothandlermesh@pathmoveto@globalpath#1{%
	\pgf@process{#1}%
	\pgfplots@loc@tmptrue
	\ifdim\pgf@x=\pgfplotsplothandlermesh@last@x\relax
		\ifdim\pgf@y=\pgfplotsplothandlermesh@last@y\relax
			\pgfplots@loc@tmpfalse
		\fi
	\fi
	\ifpgfplots@loc@tmp
		\pgfpathmoveto{}%
	\fi
}%

\def\pgfplotsplothandlermesh@setlastpoint@globalpath#1{%
	\pgf@process{#1}%
	\xdef\pgfplotsplothandlermesh@last@x{\the\pgf@x}%
	\xdef\pgfplotsplothandlermesh@last@y{\the\pgf@y}%
}%

\pgfplotsdeclarepatchclass{triangle}{%
	allow matrix=\def\pgfplotsretval{0},
	new=\def\pgfplotspatchclass@tri@no{0}\let\pgfplotspatchclass@trie@AB\relax,
	set next vertex={%
		\ifcase\pgfplotspatchclass@tri@no\relax
			\edef\pgfplotspatchclass@tri@A{#1}%
			\def\pgfplotspatchclass@tri@no{1}%
		\or
			\edef\pgfplotspatchclass@tri@B{#1}%
			\def\pgfplotspatchclass@tri@no{2}%
		\or
			\edef\pgfplotspatchclass@tri@C{#1}%
			\def\pgfplotspatchclass@tri@no{0}%
			\pgfplotspatchready%
		\fi
	},
	sample in unit cube={%
		\def\pgfplotspatchclassx{0}%
		\def\pgfplotspatchclassy{0}%
		#1%
		\def\pgfplotspatchclassx{1}%
		\def\pgfplotspatchclassy{0}%
		#1%
		\def\pgfplotspatchclassx{0}%
		\def\pgfplotspatchclassy{1}%
		#1%
		\pgfplotspatchready
		%
		%
		\def\pgfplotspatchclassx{0}%
		\def\pgfplotspatchclassy{1}%
		#1%
		\def\pgfplotspatchclassx{1}%
		\def\pgfplotspatchclassy{0}%
		#1%
		\def\pgfplotspatchclassx{1}%
		\def\pgfplotspatchclassy{1}%
		#1%
		\pgfplotspatchready
	},%
	first vertex=\expandafter\pgfplotspatchvertex\pgfplotspatchclass@tri@A\endvertex,
	foreach vertex={%
		\expandafter\pgfplotspatchvertex\pgfplotspatchclass@tri@A\endvertex #1%
		\expandafter\pgfplotspatchvertex\pgfplotspatchclass@tri@B\endvertex #1%
		\expandafter\pgfplotspatchvertex\pgfplotspatchclass@tri@C\endvertex #1%
	},
	fill path={%
		\pgfpathmoveto{\expandafter\pgfplotspointpatchvertex\pgfplotspatchclass@tri@A\endvertex}%
		\pgfpathlineto{\expandafter\pgfplotspointpatchvertex\pgfplotspatchclass@tri@B\endvertex}%
		\pgfpathlineto{\expandafter\pgfplotspointpatchvertex\pgfplotspatchclass@tri@C\endvertex}%
		\pgfpathclose
	},
	stroke path={%
		\ifx\pgfplotspatchclass@trie@AB\relax
			\pgfpathmoveto{\expandafter\pgfplotspointpatchvertex\pgfplotspatchclass@tri@A\endvertex}%
			\pgfpathlineto{\expandafter\pgfplotspointpatchvertex\pgfplotspatchclass@tri@B\endvertex}%
			\pgfpathlineto{\expandafter\pgfplotspointpatchvertex\pgfplotspatchclass@tri@C\endvertex}%
			\pgfpathclose
		\else
			\if1\pgfplotspatchclass@trie@AB
				\pgfpathmoveto{\expandafter\pgfplotspointpatchvertex\pgfplotspatchclass@tri@A\endvertex}%
				\pgfpathlineto{\expandafter\pgfplotspointpatchvertex\pgfplotspatchclass@tri@B\endvertex}%
			\fi
			\if1\pgfplotspatchclass@trie@BC
				\pgfpathmoveto{\expandafter\pgfplotspointpatchvertex\pgfplotspatchclass@tri@B\endvertex}%
				\pgfpathlineto{\expandafter\pgfplotspointpatchvertex\pgfplotspatchclass@tri@C\endvertex}%
			\fi
			\if1\pgfplotspatchclass@trie@CA
				\pgfpathmoveto{\expandafter\pgfplotspointpatchvertex\pgfplotspatchclass@tri@C\endvertex}%
				\pgfpathlineto{\expandafter\pgfplotspointpatchvertex\pgfplotspatchclass@tri@A\endvertex}%
			\fi
		\fi
	},
	triangulate=\pgfplotspatchready,
	set edge visible={%
		\ifcase#1
			\edef\pgfplotspatchclass@trie@AB{#2}%
		\or
			\edef\pgfplotspatchclass@trie@BC{#2}%
		\or
			\edef\pgfplotspatchclass@trie@CA{#2}%
		\fi
	},
	serialize except vertices={%
		\ifx\pgfplotspatchclass@trie@AB\relax
			\let\pgfplotsretval\pgfutil@empty%
		\else
			\edef\pgfplotsretval{%
				\noexpand\def\noexpand\pgfplotspatchclass@trie@AB{\pgfplotspatchclass@trie@AB}%
				\noexpand\def\noexpand\pgfplotspatchclass@trie@BC{\pgfplotspatchclass@trie@BC}%
				\noexpand\def\noexpand\pgfplotspatchclass@trie@CA{\pgfplotspatchclass@trie@CA}%
			}%
		\fi
	},%
	recursive refine@={%
		%
		\pgfplotspatchclass{\pgfplotspatchclassname}{new}%
		%
		% Perform longest edge bisection.
		\pgfpointdiff
			{\expandafter\pgfplotspointpatchvertex\pgfplotspatchclass@tri@A\endvertex}%
			{\expandafter\pgfplotspointpatchvertex\pgfplotspatchclass@tri@B\endvertex}%
		\edef\pgfplots@loc@TMPa{{\pgf@sys@tonumber\pgf@x}{\pgf@sys@tonumber\pgf@y}}%
		\expandafter\pgfmath@basic@veclen@\pgfplots@loc@TMPa
		\pgf@xa=\pgfmathresult pt
		\pgf@yc=\pgf@xa % "max so far"
		\def\pgfplotspatch@tri@longestedgestart{A}%
		%
		\pgfpointdiff
			{\expandafter\pgfplotspointpatchvertex\pgfplotspatchclass@tri@B\endvertex}%
			{\expandafter\pgfplotspointpatchvertex\pgfplotspatchclass@tri@C\endvertex}%
		\edef\pgfplots@loc@TMPa{{\pgf@sys@tonumber\pgf@x}{\pgf@sys@tonumber\pgf@y}}%
		\expandafter\pgfmath@basic@veclen@\pgfplots@loc@TMPa
		\pgf@xa=\pgfmathresult pt
		\ifdim\pgf@xa>\pgf@yc
			\pgf@yc=\pgf@xa
			\def\pgfplotspatch@tri@longestedgestart{B}%
		\fi
		%
		\pgfpointdiff
			{\expandafter\pgfplotspointpatchvertex\pgfplotspatchclass@tri@C\endvertex}%
			{\expandafter\pgfplotspointpatchvertex\pgfplotspatchclass@tri@A\endvertex}%
		\edef\pgfplots@loc@TMPa{{\pgf@sys@tonumber\pgf@x}{\pgf@sys@tonumber\pgf@y}}%
		\expandafter\pgfmath@basic@veclen@\pgfplots@loc@TMPa
		\pgf@xa=\pgfmathresult pt
		\ifdim\pgf@xa>\pgf@yc
			\def\pgfplotspatch@tri@longestedgestart{C}%
		\fi
		% The ordering is not arbitrary
		% (unless z buffer=sort is in effect). Let's assume that the
		% AB edge is BACKGROUND and the CD edge is FOREGROUND. Then
		% this sequence here should have approximately the same depth
		% ordering:
		%
		\pgfplotspatchvertexaccumstart
		\if A\pgfplotspatch@tri@longestedgestart
			\expandafter\pgfplotspatchvertexadd\pgfplotspatchclass@tri@A\times{0.5}%
			\expandafter\pgfplotspatchvertexadd\pgfplotspatchclass@tri@B\times{0.5}%
				\def\pgfplots@loc@TMPa{ACM,BCM}%
		\else
			\if B\pgfplotspatch@tri@longestedgestart
				\expandafter\pgfplotspatchvertexadd\pgfplotspatchclass@tri@B\times{0.5}%
				\expandafter\pgfplotspatchvertexadd\pgfplotspatchclass@tri@C\times{0.5}%
				\def\pgfplots@loc@TMPa{ABM,ACM}%
			\else
				\expandafter\pgfplotspatchvertexadd\pgfplotspatchclass@tri@C\times{0.5}%
				\expandafter\pgfplotspatchvertexadd\pgfplotspatchclass@tri@A\times{0.5}%
				\def\pgfplots@loc@TMPa{ABM,BCM}%
			\fi
		\fi
		\pgfplotspatchvertexfinish\pgfplotspatchclass@tri@M
		\edef\pgfplots@loc@TMPa{\pgfplots@loc@TMPa;\pgfplotspatchclass@tri@M}%
		%
		% invoke helper method such that I don't need to restore or
		% recompute \pgfplotspatchclass@tri@M during recursion.
		\expandafter\pgfplotspatchclass@tri@recursiverefine@@\pgfplots@loc@TMPa\relax{#1}%
	},
	deserialize except vertices=%
		\let\pgfplotspatchclass@trie@AB\relax
		#1,
	get pdf shading type=4,
	get num vertices=\def\pgfplotsretval{3},
}%
\def\pgfplotspatchclass@tri@recursiverefine@@#1#2M,#3#4M;#5\relax#6{%
	\expandafter\let\expandafter\pgfplotspatchclass@tri@A@\csname pgfplotspatchclass@tri@#1\endcsname
	\expandafter\let\expandafter\pgfplotspatchclass@tri@B@\csname pgfplotspatchclass@tri@#2\endcsname
	\expandafter\pgfplotsplothandlermesh@setnextvertex\expandafter{\pgfplotspatchclass@tri@A@}%
	\expandafter\pgfplotsplothandlermesh@setnextvertex\expandafter{\pgfplotspatchclass@tri@B@}%
	\pgfplotsplothandlermesh@setnextvertex{#5}%
	#6%
	%
	\expandafter\let\expandafter\pgfplotspatchclass@tri@A@\csname pgfplotspatchclass@tri@#3\endcsname
	\expandafter\let\expandafter\pgfplotspatchclass@tri@B@\csname pgfplotspatchclass@tri@#4\endcsname
	\expandafter\pgfplotsplothandlermesh@setnextvertex\expandafter{\pgfplotspatchclass@tri@A@}%
	\expandafter\pgfplotsplothandlermesh@setnextvertex\expandafter{\pgfplotspatchclass@tri@B@}%
	\pgfplotsplothandlermesh@setnextvertex{#5}%
	#6%
}%


% A rectangle specified in the sequence
% D <-- C
% |     ^
% v     |
% A --> B 
%       
\pgfplotsdeclarepatchclass{rectangle}{%
	allow matrix=\def\pgfplotsretval{1},
	new=\def\pgfplotspatchclass@rect@no{0},
	set next vertex={%
		\ifcase\pgfplotspatchclass@rect@no\relax
			\edef\pgfplotspatchclass@rect@A{#1}%
			\def\pgfplotspatchclass@rect@no{1}%
		\or
			\edef\pgfplotspatchclass@rect@B{#1}%
			\def\pgfplotspatchclass@rect@no{2}%
		\or
			\edef\pgfplotspatchclass@rect@C{#1}%
			\def\pgfplotspatchclass@rect@no{3}%
		\or
			\edef\pgfplotspatchclass@rect@D{#1}%
			\def\pgfplotspatchclass@rect@no{0}%
			\pgfplotspatchready%
		\fi
	},
	sample in unit cube={%
		\def\pgfplotspatchclassx{0}%
		\def\pgfplotspatchclassy{0}%
		#1%
		\def\pgfplotspatchclassx{1}%
		\def\pgfplotspatchclassy{0}%
		#1%
		\def\pgfplotspatchclassx{1}%
		\def\pgfplotspatchclassy{1}%
		#1%
		\def\pgfplotspatchclassx{0}%
		\def\pgfplotspatchclassy{1}%
		#1%
		\pgfplotspatchready
	},%
	first vertex=\expandafter\pgfplotspatchvertex\pgfplotspatchclass@rect@A\endvertex,
	foreach vertex={%
		\expandafter\pgfplotspatchvertex\pgfplotspatchclass@rect@A\endvertex #1%
		\expandafter\pgfplotspatchvertex\pgfplotspatchclass@rect@B\endvertex #1%
		\expandafter\pgfplotspatchvertex\pgfplotspatchclass@rect@C\endvertex #1%
		\expandafter\pgfplotspatchvertex\pgfplotspatchclass@rect@D\endvertex #1%
	},
	stream to shader={%
		% a 'foreach vertex' with slightly different sequence:
		\def\pgfplots@foreachvertex@@{%
			\let\pgfplots@current@point@meta=\pgfplotspatchvertexmeta
			\pgfplotsaxisvisphasetransformpointmeta
			\pgfplotslibrarysurfstreamcoord{%
				\global\pgf@x=\pgfplotspatchvertexx\space
				\global\pgf@y=\pgfplotspatchvertexy\space
			}{\pgfplotspointmetatransformed}%
		}%
		\if1\b@pgfplotsplothandlermesh@matrixoutput
			\expandafter\pgfplotspatchvertex\pgfplotspatchclass@rect@A\endvertex\pgfplots@foreachvertex@@%
			\expandafter\pgfplotspatchvertex\pgfplotspatchclass@rect@B\endvertex\pgfplots@foreachvertex@@%
			\expandafter\pgfplotspatchvertex\pgfplotspatchclass@rect@D\endvertex\pgfplots@foreachvertex@@% EXCHANGED SEQUENCE
			\expandafter\pgfplotspatchvertex\pgfplotspatchclass@rect@C\endvertex\pgfplots@foreachvertex@@%
		\else
			\expandafter\pgfplotspatchvertex\pgfplotspatchclass@rect@A\endvertex\pgfplots@foreachvertex@@%
			\expandafter\pgfplotspatchvertex\pgfplotspatchclass@rect@B\endvertex\pgfplots@foreachvertex@@%
			\expandafter\pgfplotspatchvertex\pgfplotspatchclass@rect@D\endvertex\pgfplots@foreachvertex@@%
			\expandafter\pgfplotspatchvertex\pgfplotspatchclass@rect@B\endvertex\pgfplots@foreachvertex@@%
			\expandafter\pgfplotspatchvertex\pgfplotspatchclass@rect@C\endvertex\pgfplots@foreachvertex@@%
			\expandafter\pgfplotspatchvertex\pgfplotspatchclass@rect@D\endvertex\pgfplots@foreachvertex@@%
		\fi
	},%
	fill path={%
		\pgfpathmoveto{\expandafter\pgfplotspointpatchvertex\pgfplotspatchclass@rect@A\endvertex}%
		\pgfpathlineto{\expandafter\pgfplotspointpatchvertex\pgfplotspatchclass@rect@B\endvertex}%
		\pgfpathlineto{\expandafter\pgfplotspointpatchvertex\pgfplotspatchclass@rect@C\endvertex}%
		\pgfpathlineto{\expandafter\pgfplotspointpatchvertex\pgfplotspatchclass@rect@D\endvertex}%
		\pgfpathclose
	},
	init pdf shading={%
		\if0\b@pgfplotsplothandlermesh@matrixoutput
			\ifnum\pgfkeysvalueof{/pgfplots/surf shading/cols}=2
			\else
				\pgfplots@error{Sorry, (patch type=rectangle) and (rectangular patch shadings with more than one rectangle) is not supported (got \pgfkeysvalueof{/pgfplots/surf shading/cols} x * input points).
					It is supported if z buffer!=sort. Consider using 'patch to triangles=true' to combine it with z buffer=sort}%
			\fi
		\fi
	},
	recursive refine@={%
		\pgfplots@error{Please use \string\usepgfplotslibrary{patchplots} in order to use 'refine,patch type=\pgfplotspatchclassname'}%
	},
	triangulate={%
		\let\pgfplotsplothandlermesh@patchclass@=\pgfplotsplothandlermesh@patchclass
		\def\pgfplotsplothandlermesh@patchclass{triangle}%
		\pgfplotspatchclass{\pgfplotsplothandlermesh@patchclass}{new}%
		\pgfplotspatchclass{\pgfplotsplothandlermesh@patchclass}{set edge visible}01%
		\pgfplotspatchclass{\pgfplotsplothandlermesh@patchclass}{set edge visible}10%
		\pgfplotspatchclass{\pgfplotsplothandlermesh@patchclass}{set edge visible}21%
		\expandafter\pgfplotsplothandlermesh@setnextvertex\expandafter{\pgfplotspatchclass@rect@A}%
		\expandafter\pgfplotsplothandlermesh@setnextvertex\expandafter{\pgfplotspatchclass@rect@B}%
		\expandafter\pgfplotsplothandlermesh@setnextvertex\expandafter{\pgfplotspatchclass@rect@D}%
		%
		\pgfplotspatchclass{\pgfplotsplothandlermesh@patchclass}{set edge visible}01%
		\pgfplotspatchclass{\pgfplotsplothandlermesh@patchclass}{set edge visible}11%
		\pgfplotspatchclass{\pgfplotsplothandlermesh@patchclass}{set edge visible}20%
		\expandafter\pgfplotsplothandlermesh@setnextvertex\expandafter{\pgfplotspatchclass@rect@B}%
		\expandafter\pgfplotsplothandlermesh@setnextvertex\expandafter{\pgfplotspatchclass@rect@C}%
		\expandafter\pgfplotsplothandlermesh@setnextvertex\expandafter{\pgfplotspatchclass@rect@D}%
		\let\pgfplotsplothandlermesh@patchclass=\pgfplotsplothandlermesh@patchclass@
	},%
	serialize except vertices=\let\pgfplotsretval\pgfutil@empty,%
	deserialize except vertices=,
	get pdf shading type=5,
	get num vertices=\def\pgfplotsretval{4},
}%

\def\pgfplots@rect@next@A{B}
\def\pgfplots@rect@next@B{C}
\def\pgfplots@rect@next@C{D}
\def\pgfplots@rect@next@D{A}

\def\pgfplots@rect@rnext@A{D}
\def\pgfplots@rect@rnext@B{A}
\def\pgfplots@rect@rnext@C{B}
\def\pgfplots@rect@rnext@D{C}

% Ideas: make sure that the foreground edge is the 'v=1'
% edge of the coons patch, i.e. that it is drawn on top of
% the rest.
%
% Furthermore, for fixed v, the 'u=1' point should belong
% to the foreground (as it is drawn on top of u=0).
%
% In other words, if the coons patch looks like 
%
% BB-------CC   <--v=1
%  |        |
%  |        |
%  |        |
% AA-------DD   <--v=0
%  |        |
%  u=0      u=1
%
% where the AA,BB,CC,DD are the corners of the coons
% patch, The FOREGROUND POINT of our visualization here should be CC, 
% the second most foreground point BB (assuming that
% foreground point and second most foreground point are
% adjacent).
%
% Thus, if 'CC' is behind of 'BB' in the A,B,C,D ordering:
%    start the coons patch with AA := BB-1 in forward
%    direction.
% else
%    start the coons patch with AA := BB+1 in reverse
%    direction.
%
% #1: the vertex prefix for the rectangular shape.
% Thus, \csname #1A\endcsname is the first vertex, \csname #1B\endcsname the second
% and #1C, #1D the third and fourth.
%
% [output] #2: A macro name which will be filled with the FIRST point
% to stream to the shader. It will be defined to be either A,B,C or D.
% [output] #3: A macro name which indicates whether the stream shall
% be forward or backward. It will be defined to be either empty or 'r'. If
% it is empty, the coons patch shall be in the sequence
% #2,next(#2),next(next(#2),....
% If it is 'r', the coons patch shall be in the sequence
% #2,prev(#2),prev(prev(#2),...
%
% Note that this method works also for biquadratic patches since these
% patches also have #1A, #1B, #1C and #1D as their corners.
% 
\def\pgfplotspatchclass@rect@to@coonspatch@findshadingsequence@for@depth#1#2#3{%
	\def#2{A}%
	\def#3{}%
	%
	\expandafter\expandafter\expandafter\pgfplotspatchvertex\csname #1A\endcsname\endvertex
	\ifx\pgfplotspatchvertexdepth\pgfutil@empty
	\else
		% aaah -- we have z buffer information. Then we will use it!
		\edef\PA{\pgfplotspatchvertexdepth pt A}%
		\expandafter\expandafter\expandafter\pgfplotspatchvertex\csname #1B\endcsname\endvertex
		\edef\PB{\pgfplotspatchvertexdepth pt B}%
		\expandafter\expandafter\expandafter\pgfplotspatchvertex\csname #1C\endcsname\endvertex
		\edef\PC{\pgfplotspatchvertexdepth pt C}%
		\expandafter\expandafter\expandafter\pgfplotspatchvertex\csname #1D\endcsname\endvertex
		\edef\PD{\pgfplotspatchvertexdepth pt D}%
		\pgfplotsutilsortfour\PA\PB\PC\PD
		\def\toCHAR##1pt ##2{##2}% get original index
		%
		%
		\edef\PA{\expandafter\toCHAR\PA}%%
		\edef#2{\expandafter\toCHAR\PB}%%
		\edef\Pnext{\csname pgfplots@rect@next@#2\endcsname}% this here uses only A,B,C and D (also for biquadratic patches!)
		\if\Pnext\PA
			% ok, next(BB) = CC.
			% that means we have to advance forward.
			\def#3{}%
			\edef#2{\csname pgfplots@rect@rnext@#2\endcsname}%
		\else
			\edef#2{\csname pgfplots@rect@next@#2\endcsname}%
			\def#3{r}%
		\fi
%\message{^^JDEPTH SEQUENCE for rectangle: \PA, \PB, \PC, \PD^^J}%
%\message{COONS PATH ORDERING: starts with #2, direction: \ifx#3\pgfutil@empty forward\else backward\fi^^J}%
	\fi
}%

\def\pgfplotspatchclass@rect@to@coonspatch@findshadingsequence@for@depth@no@op#1#2#3{%
	\def#2{A}%
	\def#3{}%
}

\def\pgfplotsplothandlermesh@patchclass@defaulttext{default}%

% This is a VERY special case. The idea is to switch to
% "normal" path mode:
% \pgfpathmoveto \pgfpathlineto \pgfpathlineto
% \pgfpathlineto \pgfusepath.
%
% Note that the mesh handler's default is to issue
% \pgfusepath after *each* elementary patch.
%
% This special use-case might not cover all aspects of the
% mesh/surf/patch plot handler.
\def\pgfplotsplothandlermesh@init@path@without@point@meta{%
	%
	\pgfplotspatchclass{\pgfplotsplothandlermesh@patchclass@output}{supports global path}%
	\ifnum\pgfplotsretval=1 %
		\def\pgfplotsplothandlermesh@definecolor{%
			% no-op
		}%
		% FIXME : this does not handle separate edges, i.e.
		% \pgfplotsplothandlermesh@VISUALIZE@std@separate@fillstroke
		\def\pgfplotsplothandlermesh@VISUALIZE{\pgfplotsplothandlermesh@VISUALIZE@globalpath@fill@andor@stroke}%
		\let\pgfplotsplothandlermesh@pathmoveto=\pgfplotsplothandlermesh@pathmoveto@globalpath
		\let\pgfplotsplothandlermesh@setlastpoint=\pgfplotsplothandlermesh@setlastpoint@globalpath
		% 
		% This activates tikz colors:
		%\tikz@options
		\pgfplots@drawoptions
		% Acquire the values for \tikz@mode@* [used for faceted]
		\pgfplots@drawmodes
	\else
		\pgfplots@error{Please provide 'point meta' for your plot. Omitting point meta is only supported for 1d patch types.}%
	\fi
}

% implements 'show normals'. Keep in mind that this is more-or-less a
% debugging helper, not productive code.
\def\pgfplotsplothandlermesh@show@normals{%
	\pgfplotspatchclass{\pgfplotsplothandlermesh@patchclass}{compute normal}%
	\let\pgfplots@normal@vector=\pgfplotsretval
%\pgfplotsmathvectortostring\pgfplots@normal@vector{default}%
%\message{got normal \pgfplotsretval^^J}%
	\pgfplotsmathvectorlength{\pgfplots@normal@vector}{default}%
	\pgfplotscoordmath{default}{parsenumber}{\pgfkeysvalueof{/pgfplots/mesh/show normals length factor}}% FIXME
	\pgfplotscoordmath{default}{op}{divide}{{\pgfmathresult}{\pgfplotsretval}}%
	\pgfplotsmathvectorscale{\pgfplots@normal@vector}{\pgfmathresult}{default}%
	%\pgfplotsmathvectoradd{\pgfplotspatchvertexcoords}{\pgfplotsretval}{default}%
	\let\pgfplots@normal@vector=\pgfplotsretval
	%
	\draw[/pgfplots/every patch normal]
		\pgfextra{%
			\pgfpathmoveto
			{%
				\pgf@process{%
					\pgfplotspatchclass{\pgfplotsplothandlermesh@patchclass}{get num vertices}%
					\pgfmath@basic@reciprocal@{\pgfplotsretval}%
					\let\scale=\pgfmathresult
					\pgfplotspatchvertexaccumstart
					\pgfplotspatchclass{\pgfplotspatchclassname}{foreach vertex}{%
						\expandafter\pgfplotspatchvertexadd\pgfplotspatchvertexstruct\times{\scale}%
					}%
					\pgfplotspatchvertexfinishXY{\pgfplots@loc@TMPa}%
					\expandafter\pgfplotspointpatchvertex\pgfplots@loc@TMPa\endvertex%
				}%
			}%
			\edef\pgfplots@loc@TMPa{\pgf@x=\the\pgf@x\space\pgf@y=\the\pgf@y\space}%
			%
			\pgfpathlineto{%
				\pgfpointadd
					{\pgfplots@loc@TMPa}
					{\pgfplotspointfromcsvvector\pgfplots@normal@vector{default}}%
			}%
		}%
	;
}%

% Defines \pgfplotsretval to be either empty or the current point's
% depth according to z buffer information.
\def\pgfplotsplothandlermesh@compute@vertex@depth{\let\pgfplotsretval\pgfutil@empty}%

% If \pgfplotsplothandlermesh@compute@vertex@depth is set to this
% particular implementation, the \pgfplotspatchvertexdepth macro will
% contain the view depth.
%
\def\pgfplotsplothandlermesh@compute@vertex@depth@doit{%
	\ifx\pgfplots@current@point@x\pgfutil@empty
		\let\pgfplotsretval\pgfutil@empty
	\else
		\pgfplotsmathvectorfromstring{\pgfplots@current@point@x,\pgfplots@current@point@y,\pgfplots@current@point@z}{default}%
		\pgfplotsmathvectorviewdepth{\pgfplotsretval}%
		\pgfplotscoordmath{default}{tofixed}{\pgfplotsretval}%
		\edef\pgfplotsretval{\pgfmathresult}%
	\fi
}%

% A command which defines \pgfplotsretval to be either empty or to
% the (x,y,z) 3d coordinates of the data point. Usually, the data
% scaling transformation will already be applied to these points.
%
% Note that this is unnecessary for most applications of mesh plots.
%
% The resulting value will be available as \pgfplotspatchvertexcoords
\def\pgfplotsplothandlermesh@serialize@logical@coords{%
	% empty by default
	\let\pgfplotsretval=\pgfutil@empty
}%
\def\pgfplotsplothandlermesh@serialize@logical@coords@doit{%
	\ifx\pgfplots@current@point@x\pgfutil@empty
		\let\pgfplotsretval=\pgfutil@empty
	\else
		% note that these points are the result of the data scaling
		% trafo. That is OK because the view normal is also in the
		% same coordinate space.
		\pgfplotsmathvectorfromstring{\pgfplots@current@point@x,\pgfplots@current@point@y,\pgfplots@current@point@z}{default}%
	\fi
}

% the inverse for \pgfplotsplothandlermesh@serialize@logical@coords: it defines \pgfplots@current@point@[xyz].
% You should only call it if you know that there IS something to deserialize.
\def\pgfplotsplothandlermesh@deserialize@logical@coords#1{%
	\expandafter\pgfplotsplothandlermesh@deserialize@logical@coords@#1\relax
}%
\def\pgfplotsplothandlermesh@deserialize@logical@coords@#1,#2,#3\relax{%
	\def\pgfplots@current@point@x{#1}%
	\def\pgfplots@current@point@y{#2}%
	\def\pgfplots@current@point@z{#3}%
}%

% The patch visualization works like a PIPELINE:
%
% DECODE -> CHECK JUMP -> REFINE -> FINISH
%
% where FINISH is usually nothing but VISUALIZE; but it can also mean
% to collect patches, sort them according to view depth and visualize
% the result.
%
% Each single PIPE element can be exchanged by a separate "subclass".
% For example, DECODE can either DECODE from a matrix (a lattice) or a
% sequence of patch coords.
%
\def\pgfplotsplothandlermesh@stream#1{%
	\pgfplotsplothandlermesh@PIPE@DECODE{#1}%
}%

% accumulates a patch vertex-by-vertex.
% #1 the next vertex of a patch (#1 defines canvas coordinates \pgf@x
% and \pgf@y).
%
% @PRECONDITION \pgfplots@current@point@[xyz] and \pgfplots@current@point@meta
% need to be defined.
\def\pgfplotsplothandlermesh@PIPE@DECODE#1{%
	\let\pgfplotsplothandlermesh@patchclass\pgfplotsplothandlermesh@patchclass@input
	\pgfplotsplothandlermesh@compute@vertex@depth% <-- this modifies \pgfplotsretval
	\let\pgfplots@view@depth=\pgfplotsretval
	\pgf@process{#1}%
	% calls \pgfplotspatchready when ready
	\pgfplotsplothandlermesh@serialize@logical@coords
	\pgfplotspatchclass{\pgfplotsplothandlermesh@patchclass}{set next vertex}{{\the\pgf@x}{\the\pgf@y}{\pgfplots@current@point@meta}\pgfplots@view@depth:\pgfplotsretval}%%
}%
\def\pgfplotspatchready{%
	\pgfplotsplothandlermesh@PIPE@CHECKJUMP
}%
\let\pgfplotspatchready@orig=\pgfplotspatchready
\def\pgfplotsplothandlermesh@PIPE@CHECKJUMP{%
	\pgfplots@loc@tmptrue
	\pgfplotspatchclass{\pgfplotsplothandlermesh@patchclass}{foreach vertex}{%
		\pgfplotsplothandlermesh@check@unbounded%
	}%
	\ifpgfplots@loc@tmp
		\expandafter\pgfplotsplothandlermesh@PIPE@REFINE
	\fi
}%
\def\pgfplotsplothandlermesh@PIPE@REFINE@no@op{%
	\pgfplotsplothandlermesh@PIPE@FINISH
}%
\let\pgfplotsplothandlermesh@PIPE@REFINE=\pgfplotsplothandlermesh@PIPE@REFINE@no@op

% invoked when a patch is completely processed.
% This might be replaced by the z buffering algorithm
\def\pgfplotsplothandlermesh@PIPE@FINISH{%
	\pgfplotsplothandlermesh@PIPE@interior@exterior@styles
}%

\def\pgfplotsplothandlermesh@PIPE@interior@exterior@styles{%
	\pgfplotsplothandlermesh@interior@exterior@hook
	\pgfplotsplothandlermesh@VISUALIZE
}%

% Standard implementation for VISUALIZE
\def\pgfplotsplothandlermesh@VISUALIZE@std@fill@andor@stroke{%
	\pgfplotspatchclass{\pgfplotsplothandlermesh@patchclass}{fill path}%
	\pgfplotsplothandlermesh@definecolor
	\pgfplotsplothandlermesh@usepath
	\pgfplotsplothandlermesh@show@normals@if@configured
}%

% a special VISUALIZE implementation which allows to draw a CONTINUOS
% global path, i.e. one which is not "\pgfusepath"ed after each patch
% segment. This is only used if 'point meta=none'
\def\pgfplotsplothandlermesh@VISUALIZE@globalpath@fill@andor@stroke{%
	\pgfplotspatchclass{\pgfplotsplothandlermesh@patchclass}{fill path}%
}%

\def\pgfplotsplothandlermesh@VISUALIZE@std{\pgfplotsplothandlermesh@VISUALIZE@std@fill@andor@stroke}

\def\pgfplotsplothandlermesh@VISUALIZE{\pgfplotsplothandlermesh@VISUALIZE@std}

\let\pgfplotsplothandlermesh@show@normals@if@configured\relax


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\def\pgfplotsplothandlermesh@init@DECODE{%
	\if1\pgfplotsplothandlermesh@matrixinput
		% mesh input=lattice
		% Init memory:
		%
		% And now finally: initialise the conversion from 
		%  LINEARIZED COORD STREAM -> DATA MATRIX:
		\if\pgfplots@plot@mesh@ordering0%
			% x varies (=rowwise):
			\let\pgfplotsplothandlermesh@scanlinelength=\pgfplotsplothandlermesh@cols
		\else
			% y varies (=colwise):
			\let\pgfplotsplothandlermesh@scanlinelength=\pgfplotsplothandlermesh@rows
		\fi
		%
		\if1\pgfplotsplothandlermesh@ONEDIMMODE
%\message{initialised DECODE for 1d mode.^^J}%
			\let\pgfplotsplothandlermesh@PIPE@DECODE=\pgfplotsplothandlermesh@PIPE@DECODE@matrix@ONEDIMMODE
		\else
			\if0\b@pgfplotsplothandlermesh@matrixoutput
				% in fact, this does only remember the maximum capacity;
				% it doesn't fill the memory.
				\c@pgfplots@scanlineindex=\pgfplotsplothandlermesh@scanlinelength\relax
				\advance\c@pgfplots@scanlineindex by1
				\pgfplotsdequenewempty{lastscanline}\capacity{\the\c@pgfplots@scanlineindex}%
				\let\pgfplotsplothandlermesh@PIPE@DECODE=\pgfplotsplothandlermesh@PIPE@DECODE@matrix@fillscanline
%\message{initialised DECODE for patch output (from matrix).^^J}%
			\else
				% SPECIAL HANDLING!
				\let\pgfplotsplothandlermesh@PIPE@DECODE=\pgfplotsplothandlermesh@PIPE@DECODE@to@shader
%\message{initialised DECODE for direct matrix output (from matrix).^^J}%
			\fi
		\fi
	\else
%\message{initialised DECODE for patch output (from patch).^^J}%
	\fi
}%


\def\pgfplotsplothandlermesh@PIPE@REFINE@triangulate{%
	\let\pgfplotspatchready\pgfplotsplothandlermesh@PIPE@FINISH
	\pgfplotspatchclass{\pgfplotsplothandlermesh@patchclass}{triangulate}%
	\let\pgfplotspatchready\pgfplotspatchready@orig
}%
\def\pgfplotsplothandlermesh@PIPE@REFINE@recursively{%
	\let\pgfplotspatchready\pgfplotsplothandlermesh@PIPE@FINISH
	\pgfplotspatchclass{\pgfplotsplothandlermesh@patchclass}{refine}{\pgfplotsplothandlermesh@refinecount}%
	\let\pgfplotspatchready\pgfplotspatchready@orig
}%
\def\pgfplotsplothandlermesh@PIPE@REFINE@triangulate@recursively{%
	\let\pgfplotspatchready=\pgfplotsplothandlermesh@PIPE@REFINE@triangulate@recursively@patchready
	\pgfplotspatchclass{\pgfplotsplothandlermesh@patchclass}{refine}{\pgfplotsplothandlermesh@refinecount}%
	\let\pgfplotspatchready\pgfplotspatchready@orig
}%
\def\pgfplotsplothandlermesh@PIPE@REFINE@triangulate@recursively@patchready{%
	\let\pgfplotspatchready\pgfplotsplothandlermesh@PIPE@FINISH
	\pgfplotspatchclass{\pgfplotsplothandlermesh@patchclass}{triangulate}%
	\let\pgfplotspatchready\pgfplotsplothandlermesh@PIPE@REFINE@triangulate@recursively@patchready
}%
\def\pgfplotsplothandlermesh@init@REFINE{%
	\pgfkeysgetvalue{/pgfplots/patch refines}\pgfplotsplothandlermesh@refinecount
	\if0\pgfplotsplothandlermesh@triangulate
		\ifnum\pgfplotsplothandlermesh@refinecount>0
%\message{initiased REFINE to REFINE.}%
			\let\pgfplotsplothandlermesh@PIPE@REFINE=\pgfplotsplothandlermesh@PIPE@REFINE@recursively
		\else
%\message{initiased REFINE to NOP.}%
			\let\pgfplotsplothandlermesh@PIPE@REFINE=\pgfplotsplothandlermesh@PIPE@REFINE@no@op
		\fi
	\else
%\message{initiased REFINE to triangulate.}%
		\ifnum\pgfplotsplothandlermesh@refinecount>0
			\let\pgfplotsplothandlermesh@PIPE@REFINE=\pgfplotsplothandlermesh@PIPE@REFINE@triangulate@recursively
		\else
			\let\pgfplotsplothandlermesh@PIPE@REFINE=\pgfplotsplothandlermesh@PIPE@REFINE@triangulate
		\fi
	\fi
}%

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% MESH SURVEY
\def\pgfplotsplothandlersurveystart@mesh{%
	\pgfplotsplothandlersurveystart@default
	\if0\pgfplotsplothandlermesh@colorinput
		% mesh/color input=colormap
	\else
		% mesh/color input=explicit
		\if1\pgfplotsplothandlermesh@colorinput@mathparse
			\pgfkeysdef{/pgfplots/color sequence/every input component}{%
				\expandafter\pgfmathparse\expandafter{\pgfmathresult}%
				\ifpgfplots@usefpu
					\pgfmathfloattofixed\pgfmathresult
				\fi
			}%
		\else
			\pgfkeysdef{/pgfplots/color sequence/every input component}{}%
		\fi
		\pgfplotscolornormalizesequence[
			colorspace=\pgfkeysvalueof{/pgfplots/mesh/colorspace explicit color output},
			default input colorspace=\pgfkeysvalueof{/pgfplots/mesh/colorspace explicit color input},
			context message={point meta '\pgfplots@current@point@meta' of coord no \the\c@pgfplots@coordindex\space (\pgfplots@current@point@x,\pgfplots@current@point@y\ifpgfplots@curplot@threedim,\pgfplots@current@point@z\fi)},
		]%
	\fi
}%
\def\pgfplotsplothandlersurveyend@mesh{%
	\pgfplotsplothandlersurveyend@default
	\if0\pgfplotsplothandlermesh@colorinput
		% mesh/color input=colormap
	\else
		% mesh/color input=explicit
		\endpgfplotscolornormalizesequence
	\fi
}

\def\pgfplotsplothandlersurveyaftersetpointmeta@mesh@cdatanormalize{%	
	\pgfplotsplothandlersurveyaftersetpointmeta@default
	\if0\pgfplotsplothandlermesh@colorinput
		% mesh/color input=colormap
	\else
		% mesh/color input=explicit
		\pgfplotscolornormalizesequencenext{\pgfplots@current@point@meta}%
		\let\pgfplots@current@point@meta=\pgfplotsretval
	\fi
}%

\newif\ifpgfplotsplothandlermesh@patchtable@hascdata
\newif\ifpgfplotsplothandlermesh@patchtable@cdata@individual
\def\pgfplotsplothandlersurveystart@mesh@patchtable{%
	\pgfplotsplothandlersurveystart@mesh
	\pgfplotsarraynewempty\pgfplots@verts
}%
\def\pgfplotsplothandlersurveypoint@mesh@patchtable{%
	\pgfplotscoordmath{x}{parsenumber}{\pgfplots@current@point@x}%
	\let\pgfplots@current@point@x=\pgfmathresult
	%
	\pgfplotscoordmath{y}{parsenumber}{\pgfplots@current@point@y}%
	\let\pgfplots@current@point@y=\pgfmathresult
	%
	\ifpgfplots@curplot@threedim
		\pgfplotscoordmath{z}{parsenumber}{\pgfplots@current@point@z}%
		\let\pgfplots@current@point@z=\pgfmathresult
	\fi
	%
	\ifpgfplotsplothandlermesh@patchtable@hascdata
		% will be handled later, using data from within 'patch table':
		\let\pgfplots@current@point@meta=\pgfutil@empty
	\else
		\pgfplotsaxissurveysetpointmeta
	\fi
	%
	\def\pgfplots@loc@TMPa{1}%
	\ifx\pgfplots@current@point@x\pgfutil@empty
		\def\pgfplots@loc@TMPa{0}%
	\else
		\ifx\pgfplots@current@point@y\pgfutil@empty
			\def\pgfplots@loc@TMPa{0}%
		\else
			\ifx\pgfplots@current@point@z\pgfutil@empty
				\ifpgfplots@curplot@threedim
					\def\pgfplots@loc@TMPa{0}%
				\fi
			\fi
		\fi
	\fi
	\if1\pgfplots@loc@TMPa
		\edef\pgfplots@loc@TMPa{\pgfplots@current@point@x,\pgfplots@current@point@y,\pgfplots@current@point@z;\pgfplots@current@point@meta;}%
		\expandafter\pgfplotsarraypushback\pgfplots@loc@TMPa\to\pgfplots@verts
	\fi
}%
\def\pgfplotsplothandlersurveyend@mesh@patchtable{%
	\pgfplotsplothandlersurveyend@mesh
	\let\pgfplotsplothandlersurveystart=\pgfplotsplothandlersurveystart@default
	\let\pgfplotsplothandlersurveyend=\pgfplotsplothandlersurveyend@default
	\let\pgfplotsplothandlersurveypoint=\pgfplotsplothandlersurveypoint@default@noreset@of@pointmeta
	\pgfplotsplothandlersurveystart
	\expandafter\pgfplotstable@isloadedtable\expandafter{\pgfplotsplothandlermesh@patchtable}{%
		\pgfplots@error{Sorry, 'patch table={<loaded table>}' has not been implemented; it would probably be too slow :-(. Please provide a file name or inline table data}%
	}{%
		\expandafter\pgfplotstableread\expandafter*\expandafter{\pgfplotsplothandlermesh@patchtable} to listener\pgfplotsplothandlermesh@patchtable@rowlistener
	}%
	\pgfplotsplothandlersurveyend
}%

\def\pgfplotsplothandlermesh@patchtable@init{%
	\pgfplotspatchclass{\pgfplotsplothandlermesh@patchclass}{get num vertices}%
	\let\pgfplotsplothandlermesh@patchtable@numverts=\pgfplotsretval
	%
	\ifpgfplotsplothandlermesh@patchtable@hascdata
		\ifpgfplotsplothandlermesh@patchtable@cdata@individual
		\else
			\ifnum\pgfplotsplothandlermesh@patchtable@numverts<\pgfplotstablecols\relax
				% prepare a pointer to data column: use the column
				% after the last vertex index.
				% this thing constitutes the "default" cdata source for
				% 'point meta=explicit'.
				\pgfplotstablereadgetptrtocolindex
					{\pgfplotsplothandlermesh@patchtable@numverts}
					{\pgfplots@table@PTR@CDATA}%
			\else
				\let\pgfplots@table@PTR@CDATA=\relax
			\fi
		\fi
	\else
		\pgfplotsplothandlermesh@patchtable@cdata@individualfalse
	\fi
	%
	\c@pgf@countb=0
	\pgfutil@loop
	\ifnum\c@pgf@countb<\pgfplotsplothandlermesh@patchtable@numverts\relax
		\edef\pgfplots@loc@TMPa{%
			\noexpand\pgfplotstablereadgetptrtocolindex{\the\c@pgf@countb}{\expandafter\noexpand\csname pgfplots@table@PTR@\the\c@pgf@countb\endcsname}%
		}%
		\pgfplots@loc@TMPa
		\advance\c@pgf@countb by1
	\pgfutil@repeat
	\let\pgfplotsplothandlermesh@patchtable@init=\relax
}%
\def\pgfplotsplothandlermesh@patchtable@rowlistener@exceptionmsg{%
	patch table \pgfplotsplothandlermesh@patchtable\space line \pgfplotstablelineno: could not read column no \the\c@pgf@countb: expected '\pgfplots@current@vertexindex' to be a valid index into the vertex arrays (the \pgfplotsarraysizeof\pgfplots@verts\space input coordinates)%
}%
\def\pgfplotsplothandlermesh@patchtable@rowlistener{%
	\pgfplotsplothandlermesh@patchtable@init%
	%
	\ifpgfplotsplothandlermesh@patchtable@hascdata
		\ifpgfplotsplothandlermesh@patchtable@cdata@individual
		\else
			\ifx\pgfplots@table@PTR@CDATA\relax
				\let\pgfplots@current@point@meta=\pgfutil@empty
			\else
				\pgfplotstablereadevalptr\pgfplots@table@PTR@CDATA\pgfplots@current@point@meta
			\fi
		\fi
	\fi
	%
	\c@pgf@countb=0
	\pgfutil@loop
	\ifnum\c@pgf@countb<\pgfplotsplothandlermesh@patchtable@numverts\relax
		\expandafter\pgfplotstablereadevalptr\csname pgfplots@table@PTR@\the\c@pgf@countb\endcsname\pgfplots@current@vertexindex%
		%
		\ifx\pgfplots@current@vertexindex\pgfutil@empty
		\else
			\pgfmathfloatparsenumber{\pgfplots@current@vertexindex}%
			\pgfmathfloattoint\pgfmathresult
			\let\pgfplots@current@vertexindex=\pgfmathresult
			%
			\let\pgfplotsexceptionmsg=\pgfplotsplothandlermesh@patchtable@rowlistener@exceptionmsg
			\pgfplotsarrayselect\pgfplots@current@vertexindex\of\pgfplots@verts\to\pgfplots@stored@pt
			\let\pgfplotsexceptionmsg=\relax
			%
			\ifpgfplotsplothandlermesh@patchtable@cdata@individual
				\c@pgf@countc=\c@pgf@countb\relax
				\advance\c@pgf@countc by\pgfplotsplothandlermesh@patchtable@numverts\relax
				\expandafter\getthisrowno\expandafter{\the\c@pgf@countc}\pgfplots@current@point@meta
			\fi
			%
			\expandafter\pgfplotsplothandlermesh@patchtable@stream\pgfplots@stored@pt
		\fi
		\advance\c@pgf@countb by1
	\pgfutil@repeat
}
\def\pgfplotsplothandlermesh@patchtable@stream#1,#2,#3;#4;{%
	\def\pgfplots@current@point@x{#1}%
	\def\pgfplots@current@point@y{#2}%
	\def\pgfplots@current@point@z{#3}%
	\ifpgfplotsplothandlermesh@patchtable@hascdata
		\def\pgfplots@set@perpointmeta@done{0}%
		\pgfplotsaxissurveysetpointmeta
	\else
		\def\pgfplots@current@point@meta{#4}%
	\fi
	\pgfplotsplothandlersurveypoint
}%
% MESH SURVEY
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\def\pgfplotsplothandlermesh@init@meshkeys{%
	\if1\pgfplotsplothandlermesh@matrixinput
		% mesh input=lattice
		\pgfplotsautocompletemeshkeys%
		\pgfkeysgetvalue{/pgfplots/mesh/rows}\pgfplotsplothandlermesh@rows
		\pgfkeysgetvalue{/pgfplots/mesh/cols}\pgfplotsplothandlermesh@cols
		\pgfkeysgetvalue{/pgfplots/mesh/num points}\pgfplotsplothandlermesh@numpoints
		%
		% the \pgfplotsplothandlermesh@ONEDIMMODE macro is ONLY
		% interesting for matrix input.
		\ifnum\pgfplotsplothandlermesh@cols>1
		\else
			\def\pgfplotsplothandlermesh@ONEDIMMODE{1}%
		\fi
		\ifnum\pgfplotsplothandlermesh@rows>1
		\else
			\def\pgfplotsplothandlermesh@ONEDIMMODE{1}%
		\fi
		\if1\pgfplotsplothandlermesh@ONEDIMMODE
			\def\pgfplotsplothandlermesh@patchclass{line}%
			\pgfkeyslet{/pgfplots/patch type}\pgfplotsplothandlermesh@patchclass
		\fi
	\else
		% mesh input=patches
		\pgfkeys{/pgfplots/mesh/ordering/x varies}% disable any special handling.
	\fi
	%
	\pgfplotspatchclass{\pgfkeysvalueof{/pgfplots/patch type}}{get dimension}%
	\ifnum\pgfplotsretval=1 %
		\def\pgfplots@meshmode{m}% mesh.
	\fi
	%
}%

\let\pgfplotsplothandlermesh@interior@exterior@hook=\relax
\def\pgfplotsplothandlermesh@interior@exterior@hook@active@zerothresh{%
	\begingroup
	\pgfplotspatchclass{\pgfplotsplothandlermesh@patchclass}{compute normal}%
% \let\pgfplots@normal=\pgfplotsretval
% \pgfplotsmathvectorlength{\pgfplotsretval}{default}%
% \pgfplotscoordmath{default}{op}{reciprocal}{{\pgfplotsretval}}%
% \pgfplotsmathvectorscale{\pgfplots@normal}{\pgfmathresult}{default}%
	%
	\pgfplotsmathvectorscalarproduct{\pgfplots@view@dir@threedim}{\pgfplotsretval}{default}%
	\pgfplotscoordmath{default}{if is}{\pgfplotsretval}{-}{%
		% <0:
		\global\let\pgfplots@glob@TMPc\pgfplotsplothandlermesh@is@interior
	}{%
		% >=0:
		\global\let\pgfplots@glob@TMPc\pgfplotsplothandlermesh@is@exterior
	}%
	\endgroup
	\pgfplots@glob@TMPc
}%

\def\pgfplotsplothandlermesh@interior@exterior@hook@active{%
	\begingroup
	\pgfplotspatchclass{\pgfplotsplothandlermesh@patchclass}{compute normal}%
	\let\pgfplots@normal=\pgfplotsretval
	\pgfplotsmathvectorlength{\pgfplotsretval}{default}%
	\let\pgfplots@normal@len=\pgfplotsretval
	%
	\pgfplotsmathvectorscalarproduct{\pgfplots@view@dir@threedim}{\pgfplots@normal}{default}%
	\let\pgfplots@dot=\pgfplotsretval
	\pgfplotscoordmath{default}{op}{multiply}{{\pgfplots@view@dir@threedim@len}{\pgfplots@normal@len}}%
	\pgfplotscoordmath{default}{op}{divide}{{\pgfplots@dot}{\pgfmathresult}}%

	\pgfplotscoordmath{default}{if less than}{\pgfmathresult}{\pgfplotsplothandlermesh@interior@thresh}{%
		% <0:
		\global\let\pgfplots@glob@TMPc\pgfplotsplothandlermesh@is@interior
	}{%
		% >=0:
		\global\let\pgfplots@glob@TMPc\pgfplotsplothandlermesh@is@exterior
	}%
	\endgroup
	\pgfplots@glob@TMPc
}%


% only used if
% \pgfplotsplothandlermesh@interior@exterior@hook@active is
% active
\def\pgfplotsplothandlermesh@is@interior{%
%\def\pgfplotsplothandlermesh@usepath{\pgfusepath{discard}}%
	\pgfkeyslet{/pgfplots/colormap name}\pgfplotsplothandlermesh@colormapname@interior
	\pgfkeyslet{/pgfplots/surf shading/colormap}\pgfplotsplothandlermesh@shader@colormap@interior
}%
\def\pgfplotsplothandlermesh@is@exterior{%
%\let\pgfplotsplothandlermesh@usepath=\pgfusepathqfillstroke
	\pgfkeyslet{/pgfplots/colormap name}\pgfplotsplothandlermesh@colormapname
	\pgfkeyslet{/pgfplots/surf shading/colormap}\pgfplotsplothandlermesh@shader@colormap
}%

\def\pgfplotsplothandlermesh@init@cdata{%
	% Ok, we have color data.
	%
	\pgfkeysgetvalue{/pgfplots/mesh/colorspace explicit color output}\pgfplots@loc@TMPa
	\pgfplotscolorspacegetcomponents{\pgfplots@loc@TMPa}%
	\let\pgfplotsplothandlermesh@explicit@color@num@comps=\pgfplotsretval
	%
	\pgfkeysgetvalue{/pgfplots/colormap name}\pgfplotsplothandlermesh@colormapname
	%
	\pgfkeysgetvalue{/pgfplots/mesh/interior colormap name}\pgfplotsplothandlermesh@colormapname@interior
	\ifx\pgfplotsplothandlermesh@colormapname@interior\pgfutil@empty
	\else
		\pgfkeysgetvalue{/pgfplots/mesh/interior colormap thresh}\pgfplotsplothandlermesh@interior@thresh
		\pgfplotscoordmath{default}{parsenumber}{\pgfplotsplothandlermesh@interior@thresh}%
		\let\pgfplotsplothandlermesh@interior@thresh=\pgfmathresult
		\pgfplotscoordmath{default}{if is}{\pgfplotsplothandlermesh@interior@thresh}{0}{%
			\let\pgfplotsplothandlermesh@interior@exterior@hook=\pgfplotsplothandlermesh@interior@exterior@hook@active@zerothresh
		}{%
			\pgfplotsmathvectorlength{\pgfplots@view@dir@threedim}{default}% should be 1, but never mind
			\let\pgfplots@view@dir@threedim@len=\pgfplotsretval
			\let\pgfplotsplothandlermesh@interior@exterior@hook=\pgfplotsplothandlermesh@interior@exterior@hook@active
		}%
		% actually store 3d coordinates:
		\let\pgfplotsplothandlermesh@serialize@logical@coords=\pgfplotsplothandlermesh@serialize@logical@coords@doit
	\fi
	%
	% Prepare:
	\def\pgfplotsplothandlermesh@definecolor{%
		\pgfplotsplothandlermesh@get@flat@color
		\pgfplotsplothandlermesh@definemappedcolor{\pgfplotsretval}%
		\pgfplots@drawoptions
	}%
	%
	% Acquire the values for \tikz@mode@* [used for faceted]
	\pgfplots@drawmodes
}

\def\pgfplotsplothandlermesh@definemappedcolor#1{%
	\if0\pgfplotsplothandlermesh@colorinput
		% mesh/color input=colormap
		\pgfplotscolormapdefinemappedcolor{#1}%
	\else
		% mesh/color input=explicit
		%
		% we assume that the color *is* already normalized, 
		% i.e. it is {<comma-separate-components>} and to be
		% interpreted in the correct color space.
		\edef\pgfplots@loc@TMPb{%
			\noexpand\pgfutil@definecolor{mapped color}
				{\pgfkeysvalueof{/pgfplots/mesh/colorspace explicit color output}}
				{#1}%
		}%
		\pgfplots@loc@TMPb
	\fi
}%

\let\pgfsetstrokecolor@orig=\pgfsetstrokecolor
\def\pgfplotsplothandlermesh@VISUALIZE@std@separate@fillstroke@setstrokecolor#1{%
	\pgfsetstrokecolor@orig{.}%
}%
\def\pgfplotsplothandlermesh@VISUALIZE@std@separate@fillstroke{%
	\pgfplotsplothandlermesh@get@flat@color
	\pgfplotsplothandlermesh@definemappedcolor{\pgfplotsretval}%
	%
	\ifx\pgfplotsplothandlermesh@usepathfill\relax
	\else
		\let\pgfsetstrokecolor=\pgfplotsplothandlermesh@VISUALIZE@std@separate@fillstroke@setstrokecolor
		\pgfplots@drawoptions
		\pgfplotspatchclass{\pgfplotsplothandlermesh@patchclass}{fill path}%
		\pgfplotsplothandlermesh@usepathfill
	\fi
	\ifx\pgfplotsplothandlermesh@usepathstroke\relax
	\else
		\let\pgfsetstrokecolor=\pgfsetstrokecolor@orig
		\pgfplots@drawoptions
		\pgfplotspatchclass{\pgfplotsplothandlermesh@patchclass}{stroke path}%
		\pgfplotsplothandlermesh@usepathstroke
	\fi
}%

% This is one of two places where shadings are served with
% coordinates; the other one uses matrix output (which does not work
% with 'stream to shader')
\def\pgfplotsplothandlermesh@VISUALIZE@interp{%
	\pgfplotspatchclass{\pgfplotsplothandlermesh@patchclass}{stream to shader}%
	\ifpgf@relevantforpicturesize
		\pgfplotspatchclass{\pgfplotsplothandlermesh@patchclass}{foreach vertex}{%
			\global\pgf@x=\pgfplotspatchvertexx\space
			\global\pgf@y=\pgfplotspatchvertexy\space
			% compute the bounding box using the vertices:
			\pgf@protocolsizes{\pgf@x}{\pgf@y}%
		}%
	\fi
}%

\def\pgfplotsplothandlermesh@VISUALIZE@faceted@interp{%
	\pgfplotslibrarysurfstreamstart
	\pgfplotsplothandlermesh@VISUALIZE@interp
	\pgfplotslibrarysurfstreamend
	\pgfplotslibrarysurfdrawinpicture
	%
	\pgfplotsplothandlermesh@VISUALIZE@std
}%

\def\pgfplotsplothandlermesh@PIPE@CHECKJUMP@disable{%
	\def\pgfplotsplothandlermesh@PIPE@CHECKJUMP{\pgfplotsplothandlermesh@PIPE@REFINE}%
}%

\def\pgfplotsplothandlermesh@init@shader{%
	% 
	% Now, process the 'shader'  key here:
	\if\pgfplots@meshmode m% mesh
		% this is processed by the 'shader=flat' code below.
		\def\pgfplotsplothandlermesh@shader{0}% 'flat'
	\fi
	\ifnum\pgfplotsplothandlermesh@shader>1% 'shader=interp' and 'shader=faceted interp'
		\pgflibrarysurfshadingifactive
			{}%
			{%
				\pgfutil@ifundefined{pgfplotsplothandlermesh@shader@warning}{%
					\pgfplots@error{Sorry, surface shading (shader=interp) is NOT available for the selected driver `\pgfsysdriver'. I will use 'shader=flat' if you continue now}%
					\gdef\pgfplotsplothandlermesh@shader@warning{x}%
				}{}%
				\def\pgfplotsplothandlermesh@shader{0}%
			}%
	\fi
	%
	%
	\ifnum\pgfplotsplothandlermesh@shader>1
		% 'shader=2': 'interp':
		% very simple: defer work to surf library. Only
		% provide the mapped colordata.
		%
		% 'shader=3': 'faceted interp':
		% a mix of 'faceted' and 'interp'. (might be inefficient in
		% terms of pdf memory and rendering times)
		%
		% This won't be used for mesh plots, neither in one
		% nor in two dimensions.
		\pgfkeyssetvalue{/pgfplots/surf shading/anchor}{\pgfpointorigin}%
		%
 		% do NOT update the picture's BB using the streamed low-level coordinates 
		% (which are bezier control points in most cases). 
		% We do that on our own using the input vertices.
		\pgfkeys{/pgfplots/surf shading/update BB=false}%
		\if1\b@pgfplotsplothandlermesh@matrixoutput
			% this *implies* a compatible patch class, see the
			% definition of \b@pgfplotsplothandlermesh@matrixoutput
			\def\pgfplotsretval{5}%
		\else
			\pgfplotspatchclass{\pgfplotsplothandlermesh@patchclass@output}{get pdf shading type}%
			\if5\pgfplotsretval
				% we have no matrix output->
				% downgrade lattice triangular grid to individual
				% triangles:
				\def\pgfplotsretval{4}%
			\fi
		\fi
		\pgfkeyslet{/pgfplots/surf shading/shading type}\pgfplotsretval%
		%
		\pgfplotsplothandlermesh@set@shader@colormap
		\pgfkeysgetvalue{/pgfplots/mesh/interior colormap name}\pgfplots@loc@TMPa
		\ifx\pgfplots@loc@TMPa\pgfutil@empty
		\else
			\let\pgfplotsplothandlermesh@shader@colormap=\pgfplotsretval
			\begingroup
			\pgfkeyslet{/pgfplots/colormap name}\pgfplots@loc@TMPa
			\pgfplotsplothandlermesh@set@shader@colormap
			\pgfmath@smuggleone\pgfplotsretval
			\endgroup
			\let\pgfplotsplothandlermesh@shader@colormap@interior=\pgfplotsretval
		\fi
		%
		\if0\pgfplotsplothandlermesh@colorinput
			% mesh/color input=colormap
			\pgfkeysgetvalue{/pgfplots/colormap name}\pgfplots@loc@TMPa
			\pgfplotscolormapgetcolorspace\pgfplots@loc@TMPa
			\pgfkeyslet{/pgfplots/surf shading/colorspace}\pgfplotsretval
		\else
			% mesh/color input=explicit
			\pgfkeysgetvalue{/pgfplots/mesh/colorspace explicit color output}\pgfplots@loc@TMPa
			\edef\pgfplots@loc@TMPa{\pgfplots@loc@TMPa}%
			\pgfkeyslet{/pgfplots/surf shading/colorspace}\pgfplots@loc@TMPa
		\fi
		\if2\pgfplotsplothandlermesh@shader
			% shader=interp
			\let\pgfplotsplothandlermesh@definecolor=\relax
			\let\pgfplotsplothandlermesh@VISUALIZE=\pgfplotsplothandlermesh@VISUALIZE@interp%
			\pgfkeyslet{/pgfplots/surf shading/cols}\pgfplotsplothandlermesh@scanlinelength
			%\if1\b@pgfplotsplothandlermesh@matrixoutput
			\iffalse% FIXME : is this special handling necessary!?
				\if\pgfplots@plot@mesh@ordering0%
					% ordering=x varies:
					% nothing special to do
				\else
					% FIXME 
					% [this happens only for mesh input=lattice]
					% colwise:
					%
					% The low level shader NEEDS rowwise ordering. So, we need to transpose the data matrix.
					% As usual in TeX, we should save macros as much as possible. I guess we NEED at least one macro for each matrix row.
					% Idea:
					% while we iterate colwise through the input data, accumulate row vectors.
					% Then, process the row vectors.
					% Since vectors are expensive (in terms of number of macros), we use fast "append" lists instead of vectors.
					\let\pgfplotsplothandlermesh@shader@interp=\pgfplotsplothandlermesh@shader@interp@incremental@transpose
					%
					% init all required row vectors:
					\c@pgfplots@scanlineindex=0
					\pgfutil@loop
					\ifnum\c@pgfplots@scanlineindex<\pgfplotsplothandlermesh@rows\relax
						\expandafter\pgfplotsapplistXnewempty\csname pgfp@transprow@\the\c@pgfplots@scanlineindex\endcsname%
						\advance\c@pgfplots@scanlineindex by1
					\pgfutil@repeat
					%\pgfkeyslet{/pgfplots/surf shading/cols}\pgfplotsplothandlermesh@rows
					%\pgfplots@error{Sorry, the 'shader=interp' does not yet support 'mesh/ordering=colwise'... You can continue now, but the plot will be transposed.}%
				\fi
			\fi
			\pgfplotspatchclass{\pgfplotsplothandlermesh@patchclass@output}{init pdf shading}%
			\pgfplotslibrarysurfstreamstart
		\else
			% shader='faceted interp'
			\def\pgfplotsplothandlermesh@VISUALIZE{\pgfplotsplothandlermesh@VISUALIZE@faceted@interp}%
			\pgfkeyssetvalue{/pgfplots/surf shading/cols}{2}%
			\pgfplotspatchclass{\pgfplotsplothandlermesh@patchclass@output}{init pdf shading}%
		\fi
	\else
		\def\pgfplotsplothandlermesh@VISUALIZE{\pgfplotsplothandlermesh@VISUALIZE@std}%
	\fi
	%
	%%
	\ifpgfplotsplothandlermesh@internaldepth
	\else
		% disable internal sorting.
		\let\pgfplotspatchclass@rect@to@coonspatch@findshadingsequence@for@depth=\pgfplotspatchclass@rect@to@coonspatch@findshadingsequence@for@depth@no@op%
		\let\pgfplotspatchclass@tri@to@coonspatch@findshadingsequence@for@depth=\pgfplotspatchclass@tri@to@coonspatch@findshadingsequence@for@depth@no@op
	\fi
}%

\def\pgfplotsplothandlermesh@init@flat@color{%
	\ifcase\pgfplotsplothandlermesh@shader
		% 0 == 'flat'
		\def\pgfplotsplothandlermesh@needs@flat@color{1}%
	\or
		% 1 == 'faceted'
		\def\pgfplotsplothandlermesh@needs@flat@color{1}%
	\or
		% 2 == 'interp'
		\def\pgfplotsplothandlermesh@needs@flat@color{0}%
	\or
		% 3 == 'faceted interp'
		\def\pgfplotsplothandlermesh@needs@flat@color{1}%
	\fi
	\if1\pgfplotsplothandlermesh@needs@flat@color
		\pgfplotsplothandlermesh@init@flat@color@getter
		%
		\pgfplotsplothandlermesh@init@flat@color@usepath
		%
		%
		\pgfplotsplothandlermesh@init@flat@color@visualize@pipe@step
	\fi
}%

\def\pgfplotsplothandlermesh@init@flat@color@visualize@pipe@step{%
	\ifx\pgfplotsplothandlermesh@PIPE@REFINE\pgfplotsplothandlermesh@PIPE@REFINE@no@op
		\let\pgfplotsplothandlermesh@VISUALIZE@std=\pgfplotsplothandlermesh@VISUALIZE@std@fill@andor@stroke
	\else
		\ifpgfplotsplothandlermesh@hide@refined@edges
			% this is (at most) partially implemented.
			\let\pgfplotsplothandlermesh@VISUALIZE@std=\pgfplotsplothandlermesh@VISUALIZE@std@separate@fillstroke
		\else
			\let\pgfplotsplothandlermesh@VISUALIZE@std=\pgfplotsplothandlermesh@VISUALIZE@std@fill@andor@stroke
		\fi
		%
		% FIXME : that doesn't work.
		% filling *always* seems to need a stroke operation,
		% otherwise there will be translucent edges.
		% I need to adjust the stroke color :-(
		%
		% FIXME : even that doesn't work correctly. The problem
		% is: drawing adjacent patches on top of each other
		% OVERDRAWS edges: once with the correct stroke color;
		% once with the fill color.
		%
		\ifx\pgfplotsplothandlermesh@usepath\pgfusepathqstroke
			\let\pgfplotsplothandlermesh@usepathfill=\relax
			\let\pgfplotsplothandlermesh@usepathstroke=\pgfusepathqstroke
		\else
			\ifx\pgfplotsplothandlermesh@usepath\pgfusepathqfillstroke
				\let\pgfplotsplothandlermesh@usepathfill=\pgfusepathqfillstroke
				\let\pgfplotsplothandlermesh@usepathstroke=\pgfusepathqstroke
			\else
				\let\pgfplotsplothandlermesh@usepathfill=\relax
				\let\pgfplotsplothandlermesh@usepathstroke=\relax
			\fi
		\fi
	\fi
}%

\def\pgfplotsplothandlermesh@init@flat@color@usepath{%
	\if\pgfplots@meshmode m%
		% 'mesh' plot: stroke only and use the 'flat' code
		% here just to define the color for each mesh
		% segment.
		\let\pgfplotsplothandlermesh@usepath=\pgfusepathqstroke
	\else
		% 'surf' plot: 
		\let\pgfplotsplothandlermesh@usepath=\pgfusepathqfillstroke
		\if\pgfplotsplothandlermesh@shader3% faceted interp
			% it has an extra fill command (shading)
			\let\pgfplotsplothandlermesh@usepath=\pgfusepathqstroke
		\fi
		%
		% the distinction between 'faceted' and 'flat' is
		% technical: it is just the default draw color.
		%
		% 'flat,draw=black' is the same as 'faceted'.
		\pgfkeysgetvalue{/pgfplots/faceted color}\pgfplots@loc@TMPb
		\def\pgfplots@loc@TMPc{none}%
		\ifx\pgfplots@loc@TMPb\pgfplots@loc@TMPc
			\pgfkeyslet{/pgfplots/faceted color}\pgfutil@empty
			\def\pgfplots@loc@TMPb{}%
		\fi
		%
		\def\pgfplots@loc@TMPa{0}%
		\if\pgfplotsplothandlermesh@shader1% faceted
			\def\pgfplots@loc@TMPa{1}%
			\ifx\pgfplots@loc@TMPb\pgfutil@empty
				% oh. no faceted color. Then: fill only, use
				% whatever color is available.
				\def\pgfplots@loc@TMPa{0}%
				%\let\pgfplotsplothandlermesh@usepath=\pgfusepathqfill
			\fi
		\fi
		\if\pgfplotsplothandlermesh@shader3% faceted interp
			\def\pgfplots@loc@TMPa{1}%
			\ifx\pgfplots@loc@TMPb\pgfutil@empty
				% oh. no faceted color. Then: no-op!
				% This is the same as interp with more overhead
				% (but allows z buffering).
				\def\pgfplots@loc@TMPa{0}%
				\def\pgfplotsplothandlermesh@usepath{\pgfusepath{discard}}%
			\fi
		\fi
		\if1\pgfplots@loc@TMPa
			% make sure there is a the correct color.
			\expandafter\def\expandafter\pgfplots@drawoptions\expandafter{%
				\pgfplots@drawoptions
				\pgfsetstrokecolor{\pgfkeysvalueof{/pgfplots/faceted color}}%
			}%
		\fi
		%
	\fi
}

\def\pgfplotsplothandlermesh@init@flat@color@getter{%
	\if\pgfplotsplothandlermesh@flatmode c% 'flat corner':
		%
		% use colordata of ONE corner point. That's easy, not
		% much to do.
		\def\pgfplotsplothandlermesh@get@flat@color{%
			\message{pgfplotsplothandlermesh@get@flat@color^^J}%
			\pgfplotspatchclass{\pgfplotsplothandlermesh@patchclass@output}{first vertex}%
			\let\pgfplots@current@point@meta=\pgfplotspatchvertexmeta
			\pgfplotsaxisvisphasetransformpointmeta
			\let\pgfplotsretval=\pgfplotspointmetatransformed%
		}%
	\else
		% 'flat mean':
		%
		% use colordata as mean of all corners for shading.
		\if0\pgfplotsplothandlermesh@colorinput
			% mesh/color input=colormap
			\def\pgfplotsplothandlermesh@get@flat@color{%
				\global\pgfplots@tmpa=0pt
				\pgfplotspatchclass{\pgfplotsplothandlermesh@patchclass@output}{foreach cdata vertex}{%
					\let\pgfplots@current@point@meta=\pgfplotspatchvertexmeta
					\pgfplotsaxisvisphasetransformpointmeta
					\global\advance\pgfplots@tmpa by\pgfplotspointmetatransformed pt
				}%
				\pgfplotspatchclass{\pgfplotsplothandlermesh@patchclass@output}{get num cdata vertices}%
				\global\divide\pgfplots@tmpa by\pgfplotsretval\relax
				\edef\pgfplotsretval{\pgf@sys@tonumber\pgfplots@tmpa}%
				\global\pgfplots@tmpa=0pt
			}%
		\else
			% mesh/color input=explicit
			\pgfplotspatchclass{\pgfplotsplothandlermesh@patchclass@output}{get num cdata vertices}%
			\pgf@xa=1pt %
			\divide\pgf@xa by\pgfplotsretval\relax
			\edef\pgfplotsplothandlermesh@get@flat@color@@{\pgf@sys@tonumber\pgf@xa}%
			%
			\def\pgfplotsplothandlermesh@get@flat@color{%
				\pgfplotscolorzero{\pgfplotsplothandlermesh@explicit@color@num@comps}%
				\pgfplotspatchclass{\pgfplotsplothandlermesh@patchclass@output}{foreach cdata vertex}{%
					\pgfplotscoloraddweighted
						{\pgfplotsplothandlermesh@explicit@color@num@comps}%
						{\pgfplotsretval}
						{\pgfplotsplothandlermesh@get@flat@color@@}%
						{\pgfplotspatchvertexmeta}%
				}%
				\let\pgfplots@current@point@meta=\pgfplotspatchvertexmeta
				\pgfplotsaxisvisphasetransformpointmeta
			}%
		\fi
	\fi
}%

\def\pgfplotsplothandlermesh@init@jumphandling{%
	% For matrix input, a "jump" should result in something
	% visible although it must not destroy the matrix structure.
	%
	% try this here:
	\def\pgfplotsplothandlervisualizejump{%
		\pgfplotstreampoint{\global\pgf@x=-16000.0pt \global\pgf@y=-16000.0pt }%
	}%
	%
	\if0\pgfplotsaxisplothasjumps
		\pgfplotsplothandlermesh@PIPE@CHECKJUMP@disable
	\fi
}%


\def\pgfplotsplothandlermesh@init@zbuffer{%
	\ifpgfplots@curplot@threedim
		\pgfplotspatchclass{\pgfplotsplothandlermesh@patchclass@output}{uses view depth}%
		\if1\pgfplotsretval
			\let\pgfplotsplothandlermesh@compute@vertex@depth=\pgfplotsplothandlermesh@compute@vertex@depth@doit%
		\fi
		%
		\if\pgfplotsplothandlermesh@zbuffer@choice4%
			% z buffer=sort:
			% create a z buffer:
			% the z buffer contains elements {<view depth>}{{<pt1>}{<pt2>}{<pt3>}{<pt4>}}
			\pgfplotsarraynewempty\pgfplots@zbuffer@local
			%
			\let\pgfplotsplothandlermesh@compute@vertex@depth=\pgfplotsplothandlermesh@compute@vertex@depth@doit%
			%
			% 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.
			\let\pgfplotsplothandlermesh@accum@element@depth=\pgfmath@basic@add@
			%
			% and collect each point into the z buffer
			% (instead of drawing it):
			\def\pgfplotsplothandlermesh@PIPE@FINISH{%
				%
				% compute DEPTH(lattice element):= min{DEPTH(A),DEPTH(B),DEPTH(C),DEPTH(D)}
				\def\pgfmathresult{0}%
				\def\pgfplotsplothandlermesh@zbuffer@tmp{}%
				\pgfplotspatchclass{\pgfplotsplothandlermesh@patchclass@output}{foreach vertex}{%
					\pgfplotsplothandlermesh@accum@element@depth\pgfplotspatchvertexdepth\pgfmathresult
					\t@pgfplots@toka=\expandafter{\pgfplotsplothandlermesh@zbuffer@tmp}%
					\t@pgfplots@tokb=\expandafter{\pgfplotspatchvertexstruct}%
					\edef\pgfplotsplothandlermesh@zbuffer@tmp{%
						\the\t@pgfplots@toka
						{\the\t@pgfplots@tokb}%
					}%
				}%
				\pgfplotspatchclass{\pgfplotsplothandlermesh@patchclass@output}{serialize except vertices}%
				\t@pgfplots@toka=\expandafter{\pgfplotsretval}%
				\t@pgfplots@tokb=\expandafter{\pgfplotsplothandlermesh@zbuffer@tmp}%
				\edef\pgfplotsplothandlermesh@zbuffer@tmp{%
					{\the\t@pgfplots@toka}% first argument is meta data, then follow the vertices
					\the\t@pgfplots@tokb%
				}%
				%
				% assemble array element:
				\t@pgfplots@toka=\expandafter{\pgfplotsplothandlermesh@zbuffer@tmp}%
				\edef\pgfplots@loc@TMPa{%
					{\pgfmathresult}%
					{%
						\the\t@pgfplots@toka
					}%
				}%
				\expandafter\pgfplotsarraypushback\pgfplots@loc@TMPa\to\pgfplots@zbuffer@local
			}%
		\fi
	\fi
}%
\def\pgfplotsplothandlermesh@init@mesh@stream{%
	\global\let\pgf@plotstreamspecial=\pgfutil@gobble%
	\global\let\pgf@plotstreamend=\pgfplotsplothandlermesh@finish%
	\global\let\pgf@plotstreampoint=\pgfplotsplothandlermesh@stream
	\pgfplotspatchclass{\pgfplotsplothandlermesh@patchclass}{new}%
}

\def\pgfplotsplothandlermesh@set@shader@colormap{%
	\if0\pgfplotsplothandlermesh@colorinput
		% mesh/color input=colormap
		\pgfkeysgetvalue{/pgfplots/colormap name}\pgfplots@loc@TMPa
		\expandafter\pgfplotscolormaptopdffunction\expandafter{\pgfplots@loc@TMPa}%
		\pgfkeyslet{/pgfplots/surf shading/colormap}\pgfplotsretval
	\else
		% mesh/color input=explicit
		%
		% tell it to expect explicit colors:
		\pgfkeyslet{/pgfplots/surf shading/colormap}\pgfutil@empty
	\fi
}%

{
\dimen0=-16000.0pt
\xdef\pgfplotsplothandlermesh@check@unbounded@text{\the\dimen0}%
}

\def\pgfplotsplothandlermesh@check@unbounded{%
	\ifx\pgfplotspatchvertexx\pgfplotsplothandlermesh@check@unbounded@text
		\pgfplots@loc@tmpfalse
	\fi
}%

\long\def\pgfplotsplothandlermesh@defaultlegend@img#1{%
	\scope[%
		#1,
		/pgfplots/mesh/rows=3,
		/pgfplots/mesh/cols=3,
		/pgfplots/mesh/num points=,
		/tikz/x={(0.44237cm,-0.07439cm)}, 
		/tikz/y={(0.30942cm,0.23932cm)},
		/tikz/z={(0.0cm,1.5cm)},
		scale=0.4,
		yshift=-0.7cm,
	]
	\let\pgfplots@metamax=\pgfutil@empty
	\pgfplots@curplot@threedimtrue

	\pgfplotsplothandlermesh
	\pgfplotstreamstart

	\def\rangea{0.21}%
	\def\rangeb{0.7}%

	\pgfmathparse{1000/(\rangeb-\rangea)}
	\let\factor=\pgfmathresult

	\pgfutil@IfUndefined{pgfqpointxyz@orig}{%
		\let\pgfqpointxyz@orig=\pgfqpointxyz
	}{}%
	%
	\def\simplecoordinate(##1,##2,##3){%
		\pgfmathparse{\factor*(##3 - \rangea)}%
		\pgfmathfloatparsenumber\pgfmathresult
		\let\pgfplots@current@point@meta=\pgfmathresult
		% pgfplots changes \pgfqpointxyz to
		% \pgfplotspointdescriptionxy inside of legends, so use the
		% '@orig' point command:
		\pgfplotstreampoint{\pgfqpointxyz@orig{##1}{##2}{##3}}%
	}%
	% for the case 'shader=interp':
	\def\pgfplotspointbbupperright{\pgfqpointxyz@orig{2}{2}{0.9}}
	\def\pgfplotspointbblowerleft {\pgfqpointxyz@orig{0}{0}{0.1}}

	\simplecoordinate(0,2,0.7)
	\simplecoordinate(1,2,0.5)
	\simplecoordinate(2,2,0.43)

	\simplecoordinate(0,1,0.68)
	\simplecoordinate(1,1,0.4)
	\simplecoordinate(2,1,0.25)

	\simplecoordinate(0,0,0.6)
	\simplecoordinate(1,0,0.35)
	\simplecoordinate(2,0,0.21)

	\pgfplotstreamend
	\pgfusepath{stroke}
	\endscope
}%

\def\pgfplotsplothandlermesh@finish{%
	\if\pgfplotsplothandlermesh@zbuffer@choice4%
		% z buffer=sort:
		% process the z buffer:
		\pgfkeyslet{/pgfplots/iflessthan/.@cmd}\pgfplotsplothandlermesh@zbuffer@sort@iflessthen
		\pgfkeysdef{/pgfplots/array/unscope pre}{%
			\let\pgfplotsplothandlermesh@patchclass=\pgfplotsplothandlermesh@patchclass@output
			\def\pgfplotspatchready{\pgfplotsplothandlermesh@PIPE@interior@exterior@styles}%
			\pgfplotsarrayforeachungrouped\pgfplots@zbuffer@local\as\curelem{%
				\expandafter\pgfplotsplothandlermesh@draw@zbufferelement\curelem%
			}%
		}%
		\pgfkeysdef{/pgfplots/array/unscope post}{}%
		\pgfplotsarraysort\pgfplots@zbuffer@local
	\fi
	%
	\if2\pgfplotsplothandlermesh@shader
		% shader=interp:
		% FIXME : broken!
		%\ifx\pgfplotsplothandlermesh@shader@interp\pgfplotsplothandlermesh@shader@interp@incremental@transpose%
			% This is the case for 'mesh/ordering=colwise'. We need to finish the transpose procedure:
		%	\pgfplotsplothandlermesh@shader@interp@transpose@finish
		%\fi
		\pgfplotslibrarysurfstreamend
		\pgfplotslibrarysurfdrawinpicture
	\fi
}%


% This is one of two places where the shader is served with
% coordinates: it handles the special matrix-form shading which is
% incompatible with the patch-wise sequence of coordinates
\def\pgfplotsplothandlermesh@PIPE@DECODE@to@shader#1{%
	\pgfplotsaxisvisphasetransformpointmeta
	\ifpgf@relevantforpicturesize
		\pgf@process{#1}%
		\pgf@protocolsizes{\pgf@x}{\pgf@y}%
	\fi
	\pgfplotslibrarysurfstreamcoord
		{#1}%
		{\pgfplotspointmetatransformed}%
}%

\def\pgfplotsplothandlermesh@PIPE@DECODE@matrix@ONEDIMMODE#1{%
	\let\pgfplotsplothandlermesh@patchclass\pgfplotsplothandlermesh@patchclass@input
	\pgfplotsplothandlermesh@compute@vertex@depth% <-- this modifies \pgfplotsretval
	\let\pgfplots@view@depth=\pgfplotsretval
	\pgf@process{#1}%
	\pgfplotsplothandlermesh@serialize@logical@coords
	\edef\pgfplotsplothandlermesh@i{{\the\pgf@x}{\the\pgf@y}{\pgfplots@current@point@meta}\pgfplots@view@depth:\pgfplotsretval}%
	\ifnum\c@pgfplots@scanlineindex>0
		\expandafter\pgfplotsplothandlermesh@setnextvertex\expandafter{\pgfplotsplothandlermesh@im}%
		\expandafter\pgfplotsplothandlermesh@setnextvertex\expandafter{\pgfplotsplothandlermesh@i}%
	\else
		\advance\c@pgfplots@scanlineindex by1
	\fi
	\let\pgfplotsplothandlermesh@im=\pgfplotsplothandlermesh@i
}%

% the pgf@plotstreampoint routine for the first scan line:
\def\pgfplotsplothandlermesh@PIPE@DECODE@matrix@fillscanline#1{%
	\pgfplotsplothandlermesh@compute@vertex@depth% <-- this modifies \pgfplotsretval
	\let\pgfplots@view@depth=\pgfplotsretval
	\pgf@process{#1}%
	\pgfplotsplothandlermesh@serialize@logical@coords
	\edef\pgfplots@loc@TMPa{{\the\pgf@x}{\the\pgf@y}{\pgfplots@current@point@meta}\pgfplots@view@depth:\pgfplotsretval}%
	\expandafter\pgfplotsdequepushback\pgfplots@loc@TMPa\to{lastscanline}%
	\advance\c@pgfplots@scanlineindex by1
	\ifnum\c@pgfplots@scanlineindex=\pgfplotsplothandlermesh@scanlinelength\relax
		% second line!
		\let\pgfplotsplothandlermesh@PIPE@DECODE=\pgfplotsplothandlermesh@PIPE@DECODE@matrix%%
		\c@pgfplots@scanlineindex=0
	\fi
}%
% This is the pgf@plotstreampoint for all scan lines starting with the
% second.
%
% We perform
% // i == y
% // j == x
% for i = 1  to rows (note the 1! 0 would be the first row)
% 	for j = 0 to cols
% 		get (i-1, j) from front of deque // remember: i: rows, j : cols. We iterate rowwise.
% 		push (i,j) to back of deque
% 		if j > 0
% 			draw 
% 				(i-1,j-1) [from last iteration]  -- (i-1,j  ) [from deque]
% 			--  (  i,j  ) [from last iteration]  -- (i  ,j-1) [from #1]
% 			-- cycle
% 		endif
% 		let (i-1,j-1) := (i-1,j) // for next iteration
% 		let (  i,j-1) := (  i,j) // for next iteration
% 	end
% end
%
% This is a SWEEP through the data matrix.
\def\pgfplotsplothandlermesh@PIPE@DECODE@matrix#1{%
	% NOTATION: 
	% 	'im == (i-1)'
	% 	'jm == (j-1)'
	\pgfplotsplothandlermesh@compute@vertex@depth% <-- this modifies \pgfplotsretval
	\let\pgfplots@view@depth=\pgfplotsretval
	\pgf@process{#1}%
	\pgfplotsplothandlermesh@serialize@logical@coords
	\edef\pgfplotsplothandlermesh@i@j{{\the\pgf@x}{\the\pgf@y}{\pgfplots@current@point@meta}\pgfplots@view@depth:\pgfplotsretval}% FIXME relies on pgfplots macros
%\message{##\the\c@pgfplots@scanlineindex: prepare rectangle with corner \meaning\pgfplotsplothandlermesh@i@j.}%
	\pgfplotsdequepopfront{lastscanline}\to\pgfplotsplothandlermesh@im@j
	\expandafter\pgfplotsdequepushback\pgfplotsplothandlermesh@i@j\to{lastscanline}%
	\ifnum\c@pgfplots@scanlineindex>0
		\let\pgfplotsplothandlermesh@patchclass\pgfplotsplothandlermesh@patchclass@input
		\expandafter\pgfplotsplothandlermesh@setnextvertex\expandafter{\pgfplotsplothandlermesh@im@jm}%
		\expandafter\pgfplotsplothandlermesh@setnextvertex\expandafter{\pgfplotsplothandlermesh@im@j}%
		\expandafter\pgfplotsplothandlermesh@setnextvertex\expandafter{\pgfplotsplothandlermesh@i@j}%
		\expandafter\pgfplotsplothandlermesh@setnextvertex\expandafter{\pgfplotsplothandlermesh@i@jm}%
	\fi
	\let\pgfplotsplothandlermesh@im@jm=\pgfplotsplothandlermesh@im@j
	\let\pgfplotsplothandlermesh@i@jm=\pgfplotsplothandlermesh@i@j
	\advance\c@pgfplots@scanlineindex by1
	\ifnum\c@pgfplots@scanlineindex=\pgfplotsplothandlermesh@scanlinelength\relax
		\c@pgfplots@scanlineindex=0
	\fi
}%
\def\pgfplotsplothandlermesh@shader@interp@incremental@transpose#1{%
	\pgfplotsaxisvisphasetransformpointmeta
	\pgf@process{#1}%
	\edef\pgfplotsplothandlermesh@i{\noexpand\pgfplotsplothandlermesh@shader@interp@next{\the\pgf@x}{\the\pgf@y}{\pgfplotspointmetatransformed}}%
	%
	% Store the postprocessed point into the appropriate row vector:
	\def\pgfplots@loc@TMPa{\expandafter\pgfplotsapplistXpushback\expandafter{\pgfplotsplothandlermesh@i}\to}
	\expandafter\pgfplots@loc@TMPa\csname pgfp@transprow@\the\c@pgfplots@scanlineindex\endcsname%
	%
	\advance\c@pgfplots@scanlineindex by1
	\ifnum\c@pgfplots@scanlineindex=\pgfplotsplothandlermesh@scanlinelength\relax
		\c@pgfplots@scanlineindex=0
	\fi
}%
\def\pgfplotsplothandlermesh@shader@interp@transpose@finish{%
	% Now, "execute" each row vector:
	\def\pgfplotsplothandlermesh@shader@interp@next##1##2##3{%
		\pgfplotslibrarysurfstreamcoord{\global\pgf@x=##1 \global\pgf@y=##2 }{##3}%
	}%
	\c@pgfplots@scanlineindex=0
	\pgfutil@loop
	\ifnum\c@pgfplots@scanlineindex<\pgfplotsplothandlermesh@rows\relax
		\expandafter\pgfplotsapplistXlet\expandafter\pgfplots@loc@TMPd\expandafter=\csname pgfp@transprow@\the\c@pgfplots@scanlineindex\endcsname%
		\expandafter\pgfplotsapplistXnewempty\csname pgfp@transprow@\the\c@pgfplots@scanlineindex\endcsname% clear
		% execute the row:
		\pgfplots@loc@TMPd
		\advance\c@pgfplots@scanlineindex by1
	\pgfutil@repeat
	\let\pgfplots@loc@TMPd=\relax
}%

\def\pgfplotsplothandlermesh@draw@zbufferelement#1#2{%
	\pgfplotsplothandlermesh@draw@zbufferelement@#2\pgfplots@EOI
}
\def\pgfplotsplothandlermesh@draw@zbufferelement@#1{%
	\pgfplotspatchclass{\pgfplotsplothandlermesh@patchclass@output}{deserialize except vertices}{#1}%
	\pgfplotsplothandlermesh@draw@zbufferelement@@
}
\def\pgfplotsplothandlermesh@draw@zbufferelement@@#1{%
	\def\pgfplots@loc@TMPa{#1}%
	\ifx\pgfplots@loc@TMPa\pgfplots@EOI
	\else
		\pgfplotspatchclass{\pgfplotsplothandlermesh@patchclass@output}{set next vertex}{#1}%
		\expandafter\pgfplotsplothandlermesh@draw@zbufferelement@@
	\fi
}%


\def\pgfplotsplothandlermesh@zbuffer@sort@iflessthen#1#2#3#4\pgfeov{%
	\edef\pgfplots@loc@TMPa{#1}%
	\expandafter\pgfplotsplothandlermesh@zbuffer@sort@iflessthen@\pgfplots@loc@TMPa\relax
	\let\pgfplotsplothandlermesh@zbuffer@sort@iflessthen@a=\pgfmathresult
	\edef\pgfplots@loc@TMPb{#2}%
	\expandafter\pgfplotsplothandlermesh@zbuffer@sort@iflessthen@\pgfplots@loc@TMPb\relax
	\let\pgfplotsplothandlermesh@zbuffer@sort@iflessthen@b=\pgfmathresult
	\ifdim\pgfplotsplothandlermesh@zbuffer@sort@iflessthen@b pt<\pgfplotsplothandlermesh@zbuffer@sort@iflessthen@a pt%
		#3\relax
	\else
		#4\relax
	\fi
}%
\def\pgfplotsplothandlermesh@zbuffer@sort@iflessthen@#1#2{%
	\def\pgfmathresult{#1}%
}%
\def\pgfplotsplothandlermesh@unpack@zbuffer@pointdepth#1{%
	\expandafter\pgfplotsplothandlermesh@unpack@zbuffer@pointdepth@#1%
}%
\def\pgfplotsplothandlermesh@unpack@zbuffer@pointdepth@#1#2#3#4{%
	\def\pgfplotsretval{#4}%
}%