File: Patch.cpp

package info (click to toggle)
darkradiant 3.9.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 41,080 kB
  • sloc: cpp: 264,743; ansic: 10,659; python: 1,852; xml: 1,650; sh: 92; makefile: 21
file content (2593 lines) | stat: -rw-r--r-- 80,905 bytes parent folder | download | duplicates (2)
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
#include "Patch.h"

#include "i18n.h"
#include "ipatch.h"
#include "shaderlib.h"
#include "irenderable.h"
#include "itextstream.h"
#include "iselectiontest.h"

#include "registry/registry.h"
#include "math/Frustum.h"
#include "math/Ray.h"
#include "texturelib.h"
#include "brush/TextureProjection.h"
#include "brush/Winding.h"
#include "command/ExecutionFailure.h"
#include "selection/algorithm/Shader.h"
#include "selection/algorithm/Texturing.h"

#include "PatchSavedState.h"
#include "PatchNode.h"

// ====== Helper Functions ==================================================================

inline VertexPointer vertexpointer_Meshvertex(const MeshVertex* array) {
  return VertexPointer(&array->vertex, sizeof(MeshVertex));
}

inline const Colour4b colour_for_index(std::size_t i, std::size_t width)
{
	static const Vector3& cornerColourVec = GlobalPatchModule().getSettings().getVertexColour(patch::PatchEditVertexType::Corners);
	static const Vector3& insideColourVec = GlobalPatchModule().getSettings().getVertexColour(patch::PatchEditVertexType::Inside);
	const Colour4b colour_corner(int(cornerColourVec[0]*255), int(cornerColourVec[1]*255),
  								int(cornerColourVec[2]*255), 255);
	const Colour4b colour_inside(int(insideColourVec[0]*255), int(insideColourVec[1]*255),
  								int(insideColourVec[2]*255), 255);

	return (i%2 || (i/width)%2) ? colour_inside : colour_corner;
}

inline bool double_valid(double f) {
  return f == f;
}

// ====== Patch Implementation =========================================================================

// Constructor
Patch::Patch(PatchNode& node) :
    _node(node),
    _undoStateSaver(nullptr),
    _transformChanged(false),
    _tesselationChanged(true),
    _shader(texdef_name_default())
{
    construct();
}

// Copy constructor (create this patch from another patch)
Patch::Patch(const Patch& other, PatchNode& node) :
    IPatch(other),
    Bounded(other),
    Snappable(other),
    IUndoable(other),
    _node(node),
    _undoStateSaver(nullptr),
    _transformChanged(false),
    _tesselationChanged(true),
    _shader(other._shader.getMaterialName())
{
    // Initalise the default values
    construct();

    // Copy over the definitions from the <other> patch
    _patchDef3 = other._patchDef3;
    _subDivisions = other._subDivisions;
    setDims(other._width, other._height);
    copy_ctrl(_ctrl.begin(), other._ctrl.begin(), other._ctrl.begin()+(_width*_height));
    _shader.setMaterialName(other._shader.getMaterialName());
    controlPointsChanged();
}

void Patch::construct()
{
    _width = _height = 0;

    _patchDef3 = false;
    _subDivisions = Subdivisions(0, 0);

    // Check, if the shader name is correct
    check_shader();
}

// Get the current control point array
PatchControlArray& Patch::getControlPoints() {
    return _ctrl;
}

// Same as above, just for const arguments
const PatchControlArray& Patch::getControlPoints() const {
    return _ctrl;
}

// Get the (temporary) transformed control point array, not the saved ones
PatchControlArray& Patch::getControlPointsTransformed() {
    return _ctrlTransformed;
}

const PatchControlArray& Patch::getControlPointsTransformed() const {
    return _ctrlTransformed;
}

std::size_t Patch::getWidth() const {
    return _width;
}

std::size_t Patch::getHeight() const {
    return _height;
}

void Patch::setDims(std::size_t w, std::size_t h)
{
  if((w%2)==0)
    w -= 1;
  ASSERT_MESSAGE(w <= MAX_PATCH_WIDTH, "patch too wide");
  if(w > MAX_PATCH_WIDTH)
    w = MAX_PATCH_WIDTH;
  else if(w < MIN_PATCH_WIDTH)
    w = MIN_PATCH_WIDTH;

  if((h%2)==0)
    _height -= 1;
  ASSERT_MESSAGE(h <= MAX_PATCH_HEIGHT, "patch too tall");
  if(h > MAX_PATCH_HEIGHT)
    h = MAX_PATCH_HEIGHT;
  else if(h < MIN_PATCH_HEIGHT)
    h = MIN_PATCH_HEIGHT;

    _width = w;
    _height = h;

    if(_width * _height != _ctrl.size())
    {
        _ctrl.resize(_width * _height);
        _ctrlTransformed.resize(_ctrl.size());
        _node.updateSelectableControls();
    }
}

PatchNode& Patch::getPatchNode()
{
    return _node;
}

void Patch::connectUndoSystem(IUndoSystem& undoSystem)
{
    assert(!_undoStateSaver);

    // Acquire a new state saver
    _undoStateSaver = undoSystem.getStateSaver(*this);
}

// Remove the attached instance and decrease the counters
void Patch::disconnectUndoSystem(IUndoSystem& undoSystem)
{
    assert(_undoStateSaver);

    _undoStateSaver = nullptr;
    undoSystem.releaseStateSaver(*this);
}

// Return the interally stored AABB
const AABB& Patch::localAABB() const
{
    return _localAABB;
}

RenderSystemPtr Patch::getRenderSystem() const
{
    return _renderSystem.lock();
}

void Patch::setRenderSystem(const RenderSystemPtr& renderSystem)
{
    _renderSystem = renderSystem;
    _shader.setRenderSystem(renderSystem);
}

// Implementation of the abstract method of SelectionTestable
// Called to test if the patch can be selected by the mouse pointer
void Patch::testSelect(Selector& selector, SelectionTest& test)
{
    // ensure the tesselation is up to date
    updateTesselation();

    // The updateTesselation routine might have produced a degenerate patch, catch this
    if (_mesh.vertices.empty()) return;

    SelectionIntersection best;
    IndexPointer::index_type* pIndex = &_mesh.indices.front();

    for (std::size_t s=0; s<_mesh.numStrips; s++) {
        test.TestQuadStrip(vertexpointer_Meshvertex(&_mesh.vertices.front()), IndexPointer(pIndex, _mesh.lenStrips), best);
        pIndex += _mesh.lenStrips;
    }

    if (best.isValid()) {
        selector.addIntersection(best);
    }
}

// Transform this patch as defined by the transformation matrix <matrix>
void Patch::transform(const Matrix4& matrix)
{
    // Cycle through all the patch control vertices and transform the points
    for (PatchControlIter i = _ctrlTransformed.begin();
         i != _ctrlTransformed.end();
         ++i)
    {
        i->vertex = matrix.transformPoint(i->vertex);
    }

    // Check the handedness of the matrix and invert it if needed
    if(matrix.getHandedness() == Matrix4::LEFTHANDED)
    {
        PatchControlArray_invert(_ctrlTransformed, _width, _height);
    }

    // Mark this patch as changed
    transformChanged();
}

// Called if the patch has changed, so that the dirty flags are set
void Patch::transformChanged()
{
    _transformChanged = true;
    _tesselationChanged = true;
}

// Called to evaluate the transform
void Patch::evaluateTransform()
{
    // Only do something, if the patch really has changed
    if (_transformChanged)
    {
        _transformChanged = false;
        revertTransform();
        _node.evaluateTransform();
    }
}

// Revert the changes, fall back to the saved state in <_ctrl>
void Patch::revertTransform()
{
    _ctrlTransformed = _ctrl;
}

// Apply the transformed control array, save it into <_ctrl> and overwrite the old values
void Patch::freezeTransform()
{
    undoSave();

    // Save the transformed working set array over _ctrl
    _ctrl = _ctrlTransformed;

    // Don't call controlPointsChanged() here since that one will re-apply the
    // current transformation matrix, possible the second time.
    transformChanged();
    updateTesselation();

    for (Observers::iterator i = _observers.begin(); i != _observers.end();)
    {
        (*i++)->onPatchControlPointsChanged();
    }
}

// callback for changed control points
void Patch::controlPointsChanged()
{
    transformChanged();
    evaluateTransform();
    updateTesselation();
    _node.onControlPointsChanged();

    for (Observers::iterator i = _observers.begin(); i != _observers.end();)
    {
        (*i++)->onPatchControlPointsChanged();
    }
}

// Snaps the control points to the grid
void Patch::snapto(float snap)
{
    undoSave();

    for(PatchControlIter i = _ctrl.begin(); i != _ctrl.end(); ++i)
    {
        i->vertex.snap(snap);
    }

    controlPointsChanged();
}

const std::string& Patch::getShader() const
{
    return _shader.getMaterialName();
}

void Patch::setShader(const std::string& name)
{
    undoSave();

    _shader.setMaterialName(name);

    // Check if the shader is ok
    check_shader();
    // Call the callback functions
    textureChanged();
}

const SurfaceShader& Patch::getSurfaceShader() const
{
    return _shader;
}

SurfaceShader& Patch::getSurfaceShader()
{
    return _shader;
}

bool Patch::hasVisibleMaterial() const
{
    if (!_shader.getGLShader()) return false;

    const MaterialPtr& material = _shader.getGLShader()->getMaterial();
    return material && material->isVisible();
}

float Patch::getTextureAspectRatio() const
{
    return _shader.getTextureAspectRatio();
}

int Patch::getShaderFlags() const
{
    if (_shader.getGLShader() != 0)
    {
        return _shader.getGLShader()->getFlags();
    }

    return 0;
}

// Return a defined patch control vertex at <row>,<col>
PatchControl& Patch::ctrlAt(std::size_t row, std::size_t col) {
    return _ctrl[row*_width+col];
}

// The same as above just for const
const PatchControl& Patch::ctrlAt(std::size_t row, std::size_t col) const {
    return _ctrl[row*_width+col];
}

PatchControl& Patch::getTransformedCtrlAt(std::size_t row, std::size_t col)
{
    if (_ctrlTransformed.empty())
    {
        _ctrlTransformed = _ctrl;
    }

    return _ctrlTransformed[row * _width + col];
}

// called just before an action to save the undo state
void Patch::undoSave()
{
    // Notify the undo observer to save this patch state
    if (_undoStateSaver != NULL)
    {
        _undoStateSaver->saveState();
    }
}

// Save the current patch state into a new UndoMemento instance (allocated on heap) and return it to the undo observer
IUndoMementoPtr Patch::exportState() const
{
    return IUndoMementoPtr(new SavedState(_width, _height, _ctrl, _patchDef3, _subDivisions.x(), _subDivisions.y(), _shader.getMaterialName()));
}

// Revert the state of this patch to the one that has been saved in the UndoMemento
void Patch::importState(const IUndoMementoPtr& state)
{
    undoSave();

    const SavedState& other = *(std::static_pointer_cast<SavedState>(state));

    // begin duplicate of SavedState copy constructor, needs refactoring

    // copy construct
    {
        _width = other.m_width;
        _height = other.m_height;
        _ctrl = other.m_ctrl;
        _ctrlTransformed = _ctrl;
        _node.updateSelectableControls();
        _patchDef3 = other.m_patchDef3;
        _subDivisions = Subdivisions(other.m_subdivisions_x, other.m_subdivisions_y);
        _shader.setMaterialName(other._materialName);
    }

    // end duplicate code

    // Notify that this patch has changed
    textureChanged();
    controlPointsChanged();
}

void Patch::check_shader()
{
    if (!shader_valid(getShader().c_str()))
    {
        rError() << "patch has invalid texture name: '" << getShader() << "'\n";
    }
}

// Patch Destructor
Patch::~Patch()
{
    for (Observers::iterator i = _observers.begin(); i != _observers.end();)
    {
        (*i++)->onPatchDestruction();
    }
}

bool Patch::isValid() const
{
  if(!_width || !_height)
  {
    return false;
  }

  for(PatchControlConstIter i = _ctrl.begin(); i != _ctrl.end(); ++i)
  {
    if(!double_valid((*i).vertex.x())
      || !double_valid((*i).vertex.y())
      || !double_valid((*i).vertex.z())
      || !double_valid((*i).texcoord.x())
      || !double_valid((*i).texcoord.y()))
    {
      rError() << "patch has invalid control points\n";
      return false;
    }
  }
  return true;
}

bool Patch::isDegenerate() const {

    if (!isValid()) {
        // Invalid patches are also "degenerate"
        return true;
    }

    Vector3 prev(0,0,0);

    // Compare each control's 3D coordinates with the previous one and break out
    // on the first non-equal one
    for (PatchControlConstIter i = _ctrl.begin(); i != _ctrl.end(); ++i) {

        // Skip the first comparison
        if (i != _ctrl.begin() && !math::isNear(i->vertex, prev, 0.0001)) {
            return false;
        }

        // Remember the coords of this vertex
        prev = i->vertex;
    }

    // The loop went through, all vertices the same
    return true;
}

void Patch::updateTesselation(bool force)
{
    // Only do something if the tesselation has actually changed
    if (!_tesselationChanged && !force) return;

    _tesselationChanged = false;

    if (!isValid())
    {
        _mesh.clear();
        _localAABB = AABB();
        return;
    }

    // Run the tesselation code
    _mesh.generate(_width, _height, _ctrlTransformed, subdivisionsFixed(), getSubdivisions(), _node.getRenderEntity());

    updateAABB();

    _node.onTesselationChanged();
}

void Patch::invertMatrix()
{
  undoSave();

  PatchControlArray_invert(_ctrl, _width, _height);

  controlPointsChanged();
}

void Patch::transposeMatrix()
{
    undoSave();

    // greebo: create a new temporary control array to hold the "old" matrix
    PatchControlArray tmp = _ctrl;

    std::size_t i = 0;

    for (std::size_t w = 0; w < _width; ++w)
    {
        for (std::size_t h = 0; h < _height; ++h)
        {
            // Copy elements such that the columns end up as rows
            _ctrl[i++] = tmp[h*_width + w];
        }
    }

    std::swap(_width, _height);

    controlPointsChanged();
}

void Patch::Redisperse(EMatrixMajor mt)
{
  std::size_t w, h, width, height, row_stride, col_stride;
  PatchControlIter p1, p2, p3;

  undoSave();

  switch(mt)
  {
  case COL:
    width = (_width-1)>>1;
    height = _height;
    col_stride = 1;
    row_stride = _width;
    break;
  case ROW:
    width = (_height-1)>>1;
    height = _width;
    col_stride = _width;
    row_stride = 1;
    break;
  default:
    ERROR_MESSAGE("neither row-major nor column-major");
    return;
  }

  for(h=0;h<height;h++)
  {
    p1 = _ctrl.begin()+(h*row_stride);
    for(w=0;w<width;w++)
    {
      p2 = p1+col_stride;
      p3 = p2+col_stride;
      p2->vertex = math::midPoint(p1->vertex, p3->vertex);
      p1 = p3;
    }
  }

  controlPointsChanged();
}

void Patch::redisperseRows()
{
	Redisperse(ROW);
}

void Patch::redisperseColumns()
{
	Redisperse(COL);
}

void Patch::insertRemove(bool insert, bool column, bool first)
{
    undoSave();

	try
	{
		if (insert)
		{
			// Decide whether we should insert rows or columns
			if (column) {
				// The insert point is 1 for "beginning" and width-2 for "end"
				insertColumns(first ? 1 : _width-2);
			}
			else {
				// The insert point is 1 for "beginning" and height-2 for "end"
				insertRows(first ? 1 : _height-2);
			}
		}
		else {
			// Col/Row Removal
			if (column) {
				// Column removal, pass TRUE
				removePoints(true, first ? 2 : _width - 3);
			}
			else {
				// Row removal, pass FALSE
				removePoints(false, first ? 2 : _height - 3);
			}
		}
	}
	catch (const GenericPatchException& g) {
		rError() << "Error manipulating patch dimensions: " << g.what() << "\n";
	}

    controlPointsChanged();
}

void Patch::appendPoints(bool columns, bool beginning) {
    bool rows = !columns; // Shortcut for readability

    if ((columns && _width + 2 > MAX_PATCH_WIDTH) ||
        (rows && _height + 2 > MAX_PATCH_HEIGHT))
    {
        rError() << "Patch::appendPoints() error: " <<
                               "Cannot make patch any larger.\n";
        return;
    }

    // Sanity check passed, now start the action
    undoSave();

    // Create a backup of the old control vertices
    PatchControlArray oldCtrl = _ctrl;
    std::size_t oldHeight = _height;
    std::size_t oldWidth = _width;

    // Resize this patch
    setDims(columns ? oldWidth+2 : oldWidth, rows ? oldHeight+2 : oldHeight);

    // Specify the target row to copy the values to
    std::size_t targetColStart = (columns && beginning) ? 2 : 0;
    std::size_t targetRowStart = (rows && beginning) ? 2 : 0;

    // We're copying the old patch matrix into a sub-matrix of the new patch
    // Fill in the control vertex values into the target area using this loop
    for (std::size_t newRow = targetRowStart, oldRow = 0;
         newRow < _height && oldRow < oldHeight;
         newRow++, oldRow++)
    {
        for (std::size_t newCol = targetColStart, oldCol = 0;
             oldCol < oldWidth && newCol < _width;
             oldCol++, newCol++)
        {
            // Copy the control vertex from the old patch to the new patch
            ctrlAt(newRow, newCol).vertex = oldCtrl[oldRow*oldWidth + oldCol].vertex;
            ctrlAt(newRow, newCol).texcoord = oldCtrl[oldRow*oldWidth + oldCol].texcoord;
        }
    }

    if (columns) {
        // Extrapolate the vertex attributes of the columns

        // These are the indices of the new columns
        std::size_t newCol1 = beginning ? 0 : _width - 1; // The outermost column
        std::size_t newCol2 = beginning ? 1 : _width - 2; // The nearest column

        // This indicates the direction we are taking the base values from
        // If we start at the beginning, we have to take the values on
        // the "right", hence the +1 index
        int neighbour = beginning ? +1 : -1;

        for (std::size_t row = 0; row < _height; row++) {
            // The distance of the two neighbouring columns,
            // this is taken as extrapolation value
            Vector3 vertexDiff = ctrlAt(row, newCol2 + neighbour).vertex -
                                 ctrlAt(row, newCol2 + 2*neighbour).vertex;
            Vector2 texDiff = ctrlAt(row, newCol2 + neighbour).texcoord -
                              ctrlAt(row, newCol2 + 2*neighbour).texcoord;

            // Extrapolate the values of the nearest column
            ctrlAt(row, newCol2).vertex = ctrlAt(row, newCol2 + neighbour).vertex + vertexDiff;
            ctrlAt(row, newCol2).texcoord = ctrlAt(row, newCol2 + neighbour).texcoord + texDiff;

            // Extrapolate once again linearly from the nearest column to the outermost column
            ctrlAt(row, newCol1).vertex = ctrlAt(row, newCol2).vertex + vertexDiff;
            ctrlAt(row, newCol1).texcoord = ctrlAt(row, newCol2).texcoord + texDiff;
        }
    }
    else {
        // Extrapolate the vertex attributes of the rows

        // These are the indices of the new rows
        std::size_t newRow1 = beginning ? 0 : _height - 1; // The outermost row
        std::size_t newRow2 = beginning ? 1 : _height - 2; // The nearest row

        // This indicates the direction we are taking the base values from
        // If we start at the beginning, we have to take the values on
        // the "right", hence the +1 index
        int neighbour = beginning ? +1 : -1;

        for (std::size_t col = 0; col < _width; col++) {
            // The distance of the two neighbouring rows,
            // this is taken as extrapolation value
            Vector3 vertexDiff = ctrlAt(newRow2 + neighbour, col).vertex -
                                 ctrlAt(newRow2 + 2*neighbour, col).vertex;
            Vector2 texDiff = ctrlAt(newRow2 + neighbour, col).texcoord -
                              ctrlAt(newRow2 + 2*neighbour, col).texcoord;

            // Extrapolate the values of the nearest row
            ctrlAt(newRow2, col).vertex = ctrlAt(newRow2 + neighbour, col).vertex + vertexDiff;
            ctrlAt(newRow2, col).texcoord = ctrlAt(newRow2 + neighbour, col).texcoord + texDiff;

            // Extrapolate once again linearly from the nearest row to the outermost row
            ctrlAt(newRow1, col).vertex = ctrlAt(newRow2, col).vertex + vertexDiff;
            ctrlAt(newRow1, col).texcoord = ctrlAt(newRow2, col).texcoord + texDiff;
        }
    }

    controlPointsChanged();
}

void Patch::flipTexture(int nAxis)
{
    selection::algorithm::TextureFlipper::FlipPatch(*this, nAxis);
}

/** greebo: Helper function that shifts all control points in
 * texture space about <s,t>
 */
void Patch::translateTexCoords(const Vector2& translation)
{
	// Cycle through all control points and shift them in texture space
	for (PatchControlIter i = _ctrl.begin(); i != _ctrl.end(); ++i)
	{
    	i->texcoord += translation;
  	}
}

void Patch::translateTexture(float s, float t)
{
    undoSave();

    s = -1 * s / _shader.getWidth();
    t = t / _shader.getHeight();

    translateTexCoords(Vector2(s,t));

    controlPointsChanged();
}

void Patch::scaleTexture(float s, float t)
{
    selection::algorithm::TextureScaler::ScalePatch(*this, { s, t });
}

void Patch::rotateTexture(float angle)
{
    selection::algorithm::TextureRotator::RotatePatch(*this, degrees_to_radians(angle));
}

void Patch::fitTexture(float s, float t)
{
    // Save the current patch state to the undoMemento
    undoSave();

    /* greebo: Calculate the texture width and height increment per control point.
     * If we have a 4x4 patch and want to tile it 3x3, the distance
     * from one control point to the next one has to cover 3/4 of a full texture,
     * hence texture_x_repeat/patch_width and texture_y_repeat/patch_height.*/
    float sIncr = s / static_cast<float>(_width - 1);
    float tIncr = t / static_cast<float>(_height - 1);

    // Set the pointer to the first control point
    PatchControlIter pDest = _ctrl.begin();

    float tc = 0;

    // Cycle through the patch matrix (row per row)
    // Increment the <tc> counter by <tIncr> increment
    for (std::size_t h=0; h < _height; h++, tc += tIncr)
    {
        float sc = 0;

        // Cycle through the row points: reset sc to zero
        // and increment it by sIncr at each step.
        for (std::size_t w = 0; w < _width; w++, sc += sIncr)
        {
            // Set the texture coordinates
            pDest->texcoord[0] = sc;
            pDest->texcoord[1] = tc;
            // Set the pointer to the next control point
            pDest++;
        }
    }

    // Notify the patch
    controlPointsChanged();
}

void Patch::scaleTextureNaturally()
{
    // Save the undo memento
    undoSave();

    // Retrieve the default scale from the registry
    auto defaultScale = registry::getValue<float>("user/ui/textures/defaultTextureScale");

    // Cycles through all the patch columns and assigns s/t coordinates.
    // During each column or row cycle, the highest world distance between columns or rows
    // determines the distance in UV space (longest distance is taken).
    // World distances are scaled to UV space with the actual texture width/height,
    // scaled by the value in the registry.

    auto horizScale = 1.0f / (static_cast<float>(_shader.getWidth()) * defaultScale);

    double texcoordX = 0;

    // Cycle through the patch width,
    for (std::size_t w = 0; w < _width; w++)
    {
        // Apply the currently active <tex> value to the control point texture coordinates.
        for (std::size_t h = 0; h < _height; h++)
        {
            // Set the x-coord (or better s-coord?) of the texture to tex.
            // For the first width cycle this is tex=0, so the texture is not shifted at the first vertex
            ctrlAt(h, w).texcoord[0] = texcoordX;
        }

        // If we reached the last row (_width - 1) we are finished (all coordinates are applied)
        if (w + 1 == _width) break;

        // Determine the texcoord of the next column
        double highestNextTexCoord = 0;

        // Determine the longest distance to the next column.
        // Again, cycle through the current column
        for (std::size_t h = 0; h < _height; h++)
        {
            // v is the vector pointing from one control point to the next neighbour
            auto worldDistance = ctrlAt(h, w).vertex - ctrlAt(h, w + 1).vertex;

            // Scale the distance in world coordinates into texture coords
            double nextTexcoordX = texcoordX + worldDistance.getLength() * horizScale;

            // Use the farthest extrapolated texture cooord
            highestNextTexCoord = std::max(highestNextTexCoord, nextTexcoordX);
        }

        // Remember the highest found texcoord, assign it to the next column
        texcoordX = highestNextTexCoord;
    }

    // Now the same goes for the texture height, cycle through all the rows
    // and calculate the longest distances, convert them to texture coordinates
    // and apply them to the according texture coordinates.
    auto vertScale = 1.0f / (static_cast<float>(_shader.getHeight()) * defaultScale);

    double texcoordY = 0;

    // Each row is visited once
    for (std::size_t h = 0; h < _height; h++)
    {
        // Visit every vertex in this row, assigning the current texCoordY
        for (std::size_t w = 0; w < _width; w++)
        {
            ctrlAt(h, w).texcoord[1] = -texcoordY;
        }

        if (h + 1 == _height) break;

        double highestNextTexCoord = 0;

        for (std::size_t w = 0; w < _width; w++)
        {
            auto worldDistance = ctrlAt(h, w).vertex - ctrlAt(h + 1, w).vertex;

            double nextTexcoordY = texcoordY + worldDistance.getLength() * vertScale;

            highestNextTexCoord = std::max(highestNextTexCoord, nextTexcoordY);
        }

        texcoordY = highestNextTexCoord;
    }

    // Notify the patch that it control points got changed
    controlPointsChanged();
}

void Patch::updateAABB()
{
    AABB aabb;

    for(PatchControlIter i = _ctrlTransformed.begin(); i != _ctrlTransformed.end(); ++i)
    {
        aabb.includePoint(i->vertex);
    }

    // greebo: Only trigger the callbacks if the bounds actually changed
    if (_localAABB != aabb)
    {
        _localAABB = aabb;

        _node.boundsChanged();
    }
}

// Inserts two columns before and after the column having the index <colIndex>
void Patch::insertColumns(std::size_t colIndex) {
    if (colIndex == 0 || colIndex == _width) {
        throw GenericPatchException("Patch::insertColumns: can't insert at this index.");
    }

    if (_width + 2 > MAX_PATCH_WIDTH) {
        throw GenericPatchException("Patch::insertColumns: patch has too many columns.");
    }

    // Create a backup of the old control vertices
    PatchControlArray oldCtrl = _ctrl;
    std::size_t oldHeight = _height;
    std::size_t oldWidth = _width;

    // Resize this patch
    setDims(oldWidth + 2, oldHeight);

    // Now fill in the control vertex values and interpolate
    // before and after the insert point.
    for (std::size_t row = 0; row < _height; row++) {

        for (std::size_t newCol = 0, oldCol = 0;
             newCol < _width && oldCol < oldWidth;
             newCol++, oldCol++)
        {
            // Is this the insert point?
            if (oldCol == colIndex) {
                // Left column (to be interpolated)
                ctrlAt(row, newCol).vertex = float_mid(
                    oldCtrl[row*oldWidth + oldCol - 1].vertex,
                    oldCtrl[row*oldWidth + oldCol].vertex
                );
                ctrlAt(row, newCol).texcoord = float_mid(
                    oldCtrl[row*oldWidth + oldCol - 1].texcoord,
                    oldCtrl[row*oldWidth + oldCol].texcoord
                );

                // Set the newCol counter to the middle column
                newCol++;
                ctrlAt(row, newCol).vertex = oldCtrl[row*oldWidth + oldCol].vertex;
                ctrlAt(row, newCol).texcoord = oldCtrl[row*oldWidth + oldCol].texcoord;

                // Set newCol to the right column (to be interpolated)
                newCol++;
                ctrlAt(row, newCol).vertex = float_mid(
                    oldCtrl[row*oldWidth + oldCol].vertex,
                    oldCtrl[row*oldWidth + oldCol + 1].vertex
                );
                ctrlAt(row, newCol).texcoord = float_mid(
                    oldCtrl[row*oldWidth + oldCol].texcoord,
                    oldCtrl[row*oldWidth + oldCol + 1].texcoord
                );
            }
            else {
                // No special column, just copy the control vertex
                ctrlAt(row, newCol).vertex = oldCtrl[row*oldWidth + oldCol].vertex;
                ctrlAt(row, newCol).texcoord = oldCtrl[row*oldWidth + oldCol].texcoord;
            }
        }
    }
}

// Inserts two rows before and after the column having the index <colIndex>
void Patch::insertRows(std::size_t rowIndex) {
    if (rowIndex == 0 || rowIndex == _height) {
        throw GenericPatchException("Patch::insertRows: can't insert at this index.");
    }

    if (_height + 2 > MAX_PATCH_HEIGHT) {
        throw GenericPatchException("Patch::insertRows: patch has too many rows.");
    }

    // Create a backup of the old control vertices
    PatchControlArray oldCtrl = _ctrl;
    std::size_t oldHeight = _height;
    std::size_t oldWidth = _width;

    // Resize this patch
    setDims(oldWidth, oldHeight + 2);

    // Now fill in the control vertex values and interpolate
    // before and after the insert point.
    for (std::size_t col = 0; col < _width; col++) {

        for (std::size_t newRow = 0, oldRow = 0;
             newRow < _height && oldRow < oldHeight;
             newRow++, oldRow++)
        {
            // Is this the insert point?
            if (oldRow == rowIndex) {
                // the column above the insert point (to be interpolated)
                ctrlAt(newRow, col).vertex = float_mid(
                    oldCtrl[(oldRow-1)*oldWidth + col].vertex,
                    oldCtrl[oldRow*oldWidth + col].vertex
                );
                ctrlAt(newRow, col).texcoord = float_mid(
                    oldCtrl[(oldRow-1)*oldWidth + col].texcoord,
                    oldCtrl[oldRow*oldWidth + col].texcoord
                );

                // Set the newRow counter to the middle row
                newRow++;
                ctrlAt(newRow, col).vertex = oldCtrl[oldRow*oldWidth + col].vertex;
                ctrlAt(newRow, col).texcoord = oldCtrl[oldRow*oldWidth + col].texcoord;

                // Set newRow to the lower column (to be interpolated)
                newRow++;
                ctrlAt(newRow, col).vertex = float_mid(
                    oldCtrl[oldRow*oldWidth + col].vertex,
                    oldCtrl[(oldRow+1)*oldWidth + col].vertex
                );
                ctrlAt(newRow, col).texcoord = float_mid(
                    oldCtrl[oldRow*oldWidth + col].texcoord,
                    oldCtrl[(oldRow+1)*oldWidth + col].texcoord
                );
            }
            else {
                // No special column, just copy the control vertex
                ctrlAt(newRow, col).vertex = oldCtrl[oldRow*oldWidth + col].vertex;
                ctrlAt(newRow, col).texcoord = oldCtrl[oldRow*oldWidth + col].texcoord;
            }
        }
    }
}

// Removes the two rows before and after the column/row having the index <index>
void Patch::removePoints(bool columns, std::size_t index) {
    bool rows = !columns; // readability shortcut ;)

    if ((columns && _width<5) || (!columns && _height < 5))
    {
        throw GenericPatchException("Patch::removePoints: can't remove any more rows/columns.");
    }

    // Check column index bounds
    if (columns && (index < 2 || index > _width - 3)) {
        throw GenericPatchException("Patch::removePoints: can't remove columns at this index.");
    }

    // Check row index bounds
    if (rows && (index < 2 || index > _height - 3)) {
        throw GenericPatchException("Patch::removePoints: can't remove rows at this index.");
    }

    // Create a backup of the old control vertices
    PatchControlArray oldCtrl = _ctrl;
    std::size_t oldHeight = _height;
    std::size_t oldWidth = _width;

    // Resize this patch
    setDims(columns ? oldWidth - 2 : oldWidth, rows ? oldHeight - 2 : oldHeight);

    // Now fill in the control vertex values and skip
    // the rows/cols before and after the remove point.
    for (std::size_t newRow = 0, oldRow = 0;
         newRow < _height && oldRow < oldHeight;
         newRow++, oldRow++)
    {
        // Skip the row before and after the removal point
        if (rows && (oldRow == index - 1 || oldRow == index + 1)) {
            // Increase the old row pointer by 1
            oldRow++;
        }

        for (std::size_t oldCol = 0, newCol = 0;
             oldCol < oldWidth && newCol < _width;
             oldCol++, newCol++)
        {
            // Skip the column before and after the removal point
            if (columns && (oldCol == index - 1 || oldCol == index + 1)) {
                // Increase the old row pointer by 1
                oldCol++;
            }

            // Copy the control vertex from the old patch to the new patch
            ctrlAt(newRow, newCol).vertex = oldCtrl[oldRow*oldWidth + oldCol].vertex;
            ctrlAt(newRow, newCol).texcoord = oldCtrl[oldRow*oldWidth + oldCol].texcoord;
        }
    }
}

void Patch::constructSeam(patch::CapType eType, std::vector<Vector3>& points, std::size_t width)
{
  switch(eType)
  {
  case patch::CapType::InvertedBevel:
    {
      setDims(3, 3);
      _ctrl[0].vertex = points[0];
      _ctrl[1].vertex = points[1];
      _ctrl[2].vertex = points[1];
      _ctrl[3].vertex = points[1];
      _ctrl[4].vertex = points[1];
      _ctrl[5].vertex = points[1];
      _ctrl[6].vertex = points[2];
      _ctrl[7].vertex = points[1];
      _ctrl[8].vertex = points[1];
    }
    break;
  case patch::CapType::Bevel:
    {
      setDims(3, 3);
      Vector3 p3(points[2] + (points[0] - points[1]));
      _ctrl[0].vertex = p3;
      _ctrl[1].vertex = p3;
      _ctrl[2].vertex = points[2];
      _ctrl[3].vertex = p3;
      _ctrl[4].vertex = p3;
      _ctrl[5].vertex = points[1];
      _ctrl[6].vertex = p3;
      _ctrl[7].vertex = p3;
      _ctrl[8].vertex = points[0];
    }
    break;
  case patch::CapType::EndCap:
    {
      Vector3 p5(math::midPoint(points[0], points[4]));

      setDims(3, 3);
      _ctrl[0].vertex = points[0];
      _ctrl[1].vertex = p5;
      _ctrl[2].vertex = points[4];
      _ctrl[3].vertex = points[1];
      _ctrl[4].vertex = points[2];
      _ctrl[5].vertex = points[3];
      _ctrl[6].vertex = points[2];
      _ctrl[7].vertex = points[2];
      _ctrl[8].vertex = points[2];
    }
    break;
  case patch::CapType::InvertedEndCap:
    {
      setDims(5, 3);
      _ctrl[0].vertex = points[4];
      _ctrl[1].vertex = points[3];
      _ctrl[2].vertex = points[2];
      _ctrl[3].vertex = points[1];
      _ctrl[4].vertex = points[0];
      _ctrl[5].vertex = points[3];
      _ctrl[6].vertex = points[3];
      _ctrl[7].vertex = points[2];
      _ctrl[8].vertex = points[1];
      _ctrl[9].vertex = points[1];
      _ctrl[10].vertex = points[3];
      _ctrl[11].vertex = points[3];
      _ctrl[12].vertex = points[2];
      _ctrl[13].vertex = points[1];
      _ctrl[14].vertex = points[1];
    }
    break;
  case patch::CapType::Cylinder:
    {
      std::size_t mid = (width - 1) >> 1;

      bool degenerate = (mid % 2) != 0;

      std::size_t newHeight = mid + (degenerate ? 2 : 1);

      setDims(3, newHeight);

      if(degenerate)
      {
        ++mid;
        for(std::size_t i = width; i != width + 2; ++i)
        {
          points[i] = points[width - 1];
        }
      }

      {
        PatchControlIter pCtrl = _ctrl.begin();
        for(std::size_t i = 0; i != _height; ++i, pCtrl += _width)
        {
          pCtrl->vertex = points[i];
        }
      }
      {
        PatchControlIter pCtrl = _ctrl.begin() + 2;
        std::size_t h = _height - 1;
        for(std::size_t i = 0; i != _height; ++i, pCtrl += _width)
        {
          pCtrl->vertex = points[h + (h - i)];

          if (i == _height - 1) break; // prevent iterator from being incremented post bounds
        }
      }

      Redisperse(COL);
    }
    break;
  default:
    ERROR_MESSAGE("invalid patch-cap type");
    return;
  }
  controlPointsChanged();
}

// greebo: Calculates the nearest patch CORNER vertex from the given <point>
// Note: if this routine returns end(), something's rotten with the patch
PatchControlIter Patch::getClosestPatchControlToPoint(const Vector3& point) {

    PatchControlIter pBest = end();

    // Initialise with an illegal distance value
    double closestDist = -1.0;

    PatchControlIter corners[4] = {
        _ctrl.begin(),
        _ctrl.begin() + (_width-1),
        _ctrl.begin() + (_width*(_height-1)),
        _ctrl.begin() + (_width*_height - 1)
    };

    // Cycle through all the control points with an iterator
    //for (PatchControlIter i = _ctrl.begin(); i != _ctrl.end(); ++i) {
    for (unsigned int i = 0; i < 4; i++) {

        // Calculate the distance of the current vertex
        double candidateDist = (corners[i]->vertex - point).getLength();

        // Compare the distance to the currently closest one
        if (candidateDist < closestDist || pBest == end()) {
            // Store this distance as best value so far
            closestDist = candidateDist;

            // Store the pointer in <best>
            pBest = corners[i];
        }
    }

    return pBest;
}

/* greebo: This calculates the nearest patch control to the given brush <face>
 *
 * @returns: a pointer to the nearest patch face. (Can technically be end(), but really should not happen).*/
PatchControlIter Patch::getClosestPatchControlToPatch(const Patch& patch) {

    // A pointer to the patch vertex closest to the patch
    PatchControlIter pBest = end();

    // Initialise the best distance with an illegal value
    double closestDist = -1.0;

    // Cycle through the winding vertices and calculate the distance to each patch vertex
    for (PatchControlConstIter i = patch.begin(); i != patch.end(); ++i)
    {
        // Retrieve the vertex
        const Vector3& patchVertex = i->vertex;

        // Get the nearest control point to the current otherpatch vertex
        PatchControlIter candidate = getClosestPatchControlToPoint(patchVertex);

        if (candidate != end())
        {
            double candidateDist = (patchVertex - candidate->vertex).getLength();

            // If we haven't found a best patch control so far or
            // the candidate distance is even better, save it!
            if (pBest == end() || candidateDist < closestDist)
            {
                // Memorise this patch control
                pBest = candidate;
                closestDist =  candidateDist;
            }
        }
    } // end for

    return pBest;
}

/* greebo: This calculates the nearest patch control to the given brush <face>
 *
 * @returns: a pointer to the nearest patch face. (Can technically be end(), but really should not happen).*/
PatchControlIter Patch::getClosestPatchControlToFace(const Face* face)
{
    // A pointer to the patch vertex closest to the face
    PatchControlIter pBest = end();

    // Initialise the best distance with an illegal value
    double closestDist = -1.0;

    // Check for NULL pointer, just to make sure
    if (face != NULL)
    {
        // Retrieve the winding from the brush face
        const Winding& winding = face->getWinding();

        // Cycle through the winding vertices and calculate the distance to each patch vertex
        for (Winding::const_iterator i = winding.begin(); i != winding.end(); ++i) {
            // Retrieve the vertex
            const Vector3& faceVertex = i->vertex;

            // Get the nearest control point to the current face vertex
            PatchControlIter candidate = getClosestPatchControlToPoint(faceVertex);

            if (candidate != end())
            {
                double candidateDist = (faceVertex - candidate->vertex).getLength();

                // If we haven't found a best patch control so far or
                // the candidate distance is even better, save it!
                if (pBest == end() || candidateDist < closestDist)
                {
                    // Memorise this patch control
                    pBest = candidate;
                    closestDist =  candidateDist;
                }
            }
        } // end for
    }

    return pBest;
}

Vector2 Patch::getPatchControlArrayIndices(const PatchControlIter& control)
{
    std::size_t count = 0;

    // Go through the patch column per column and find the control vertex
    for (PatchControlIter p = _ctrl.begin(); p != _ctrl.end(); ++p, ++count)
    {
        // Compare the iterators to check if we have found the control
        if (p == control)
        {
            int row = static_cast<int>(floor(static_cast<float>(count) / _width));
            int col = static_cast<int>(count % _width);

            return Vector2(col, row);
        }
    }

    return Vector2(0,0);
}

/* Project the vertex onto the given plane and transform it into the texture space using the worldToTexture matrix
 */
Vector2 getProjectedTextureCoords(const Vector3& vertex, const Plane3& plane, const Matrix4& worldToTexture) {
    // Project the patch vertex onto the brush face plane
    Vector3 projection = plane.getProjection(vertex);

    // Transform the projection coordinates into texture space
    Vector3 texcoord = worldToTexture.transformPoint(projection);

    // Return the texture coordinates
    return Vector2(texcoord[0], texcoord[1]);
}

/* greebo: This routine can be used to create seamless texture transitions from brushes to patches.
 *
 * The idea is to flatten out the patch so that the distances between the patch control vertices
 * are preserved, but all of them lie flat in a plane. These points can then be projected onto
 * the brush faceplane and this way the texture coordinates can be easily calculated via the
 * world-to-texture-space transformations (the matrix is retrieved from the TexDef class).
 *
 * The main problem that has to be tackled is not the "natural" texturing itself (the method
 * "natural" already takes care of that), but the goal that the patch/brush texture transition
 * is seamless.
 *
 * The starting point of the "flattening" is the nearest control vertex of the patch (the closest
 * patch vertex to any of the brush winding vertices. Once this point has been found, the patch is
 * systematically flattened into a "virtual" patch plane. From there the points are projected into
 * the texture space and you're already there.
 *
 * Note: This took me quite a bit and it's entirely possible that there is a more clever solution
 * to this, but for this weekend I'm done with this (and it works ;)).
 *
 * Note: The angle between patch and brush can also be 90 degrees, the algorithm catches this case
 * and calculates its own virtual patch directions.
 */
void Patch::pasteTextureNatural(const Face* face)
{
    // Check for NULL pointers
    if (face == nullptr) return;

    // Convert the size_t stuff into int, because we need it for signed comparisons
    int patchHeight = static_cast<int>(_height);
    int patchWidth = static_cast<int>(_width);

    // Get the plane and its normalised normal vector of the face
    Plane3 plane = face->getPlane().getPlane().getNormalised();
    Vector3 faceNormal = plane.normal();

    // Get the conversion matrix from the FaceTextureDef, the local2World argument is the identity matrix
    Matrix4 worldToTexture = face->getProjection().getWorldToTexture(faceNormal, Matrix4::getIdentity());

    // Calculate the nearest corner vertex of this patch (to the face's winding vertices)
    PatchControlIter nearestControl = getClosestPatchControlToFace(face);

    // Determine the control array indices of the nearest control vertex
    Vector2 indices = getPatchControlArrayIndices(nearestControl);

    // this is the point from where the patch is virtually flattened
    int wStart = static_cast<int>(indices.x());
    int hStart = static_cast<int>(indices.y());

    // Calculate the increments in the patch array, needed for the loops
    int wIncr = (wStart == patchWidth-1) ? -1 : 1;
    int wEnd = (wIncr<0) ? -1 : patchWidth;

    int hIncr = (hStart == patchHeight-1) ? -1 : 1;
    int hEnd = (hIncr<0) ? -1 : patchHeight;

    PatchControl* startControl = &_ctrl[(patchWidth*hStart) + wStart];

    // Calculate the base directions that are used to "flatten" the patch
    // These have to be orthogonal to the facePlane normal, so that the texture coordinates
    // can be retrieved by projection onto the facePlane.

    // Get the control points of the next column and the next row
    PatchControl& nextColumn = _ctrl[(patchWidth*(hStart + hIncr)) + wStart];
    PatchControl& nextRow = _ctrl[(patchWidth*hStart) + (wStart + wIncr)];

    // Calculate the world direction of these control points and extract a base
    Vector3 widthVector = (nextRow.vertex - startControl->vertex);
    Vector3 heightVector = (nextColumn.vertex - startControl->vertex);

    if (widthVector.getLength() == 0.0f || heightVector.getLength() == 0.0f)
    {
		throw cmd::ExecutionFailure(
			_("Sorry. Patch is not suitable for this kind of operation.")
	    );
	}

    // Save the undo memento
    undoSave();

    // Calculate the base vectors of the virtual plane the patch is flattened in
    Vector3 widthBase, heightBase;
    getVirtualPatchBase(widthVector, heightVector, faceNormal, widthBase, heightBase);

    // Now cycle (systematically) through all the patch vertices, flatten them out by
    // calculating the 3D distances of each vertex and projecting them onto the facePlane.

    // Initialise the starting point
    PatchControl* prevColumn = startControl;
    Vector3 prevColumnVirtualVertex = prevColumn->vertex;

    for (int w = wStart; w != wEnd; w += wIncr) {

        // The first control in this row, calculate its virtual coords
        PatchControl* curColumn = &_ctrl[(patchWidth*hStart) + w];

        // The distance between the last column and this column
        double xyzColDist = (curColumn->vertex - prevColumn->vertex).getLength();

        // The vector pointing to the next control point, if it *was* a completely planar patch
        Vector3 curColumnVirtualVertex = prevColumnVirtualVertex + widthBase * xyzColDist;

        // Store this value for the upcoming column cycle
        PatchControl* prevRow = curColumn;
        Vector3 prevRowVirtualVertex = curColumnVirtualVertex;

        // Cycle through all the columns
        for (int h = hStart; h != hEnd; h += hIncr) {

            // The current control
            PatchControl* control = &_ctrl[(patchWidth*h) + w];

            // The distance between the last and the current vertex
            double xyzRowDist = (control->vertex - prevRow->vertex).getLength();

            // The vector pointing to the next control point, if it *was* a completely planar patch
            Vector3 virtualControlVertex = prevRowVirtualVertex + heightBase * xyzRowDist;

            // Project the virtual vertex onto the brush faceplane and transform it into texture space
            control->texcoord = getProjectedTextureCoords(virtualControlVertex, plane, worldToTexture);

            // Update the variables for the next loop
            prevRow = control;
            prevRowVirtualVertex = virtualControlVertex;
        }

        // Set the prevColumn control vertex to this one
        prevColumn = curColumn;
        prevColumnVirtualVertex = curColumnVirtualVertex;
    }

    // Notify the patch about the change
    controlPointsChanged();
}

void Patch::pasteTextureNatural(Patch& sourcePatch) {
    // Save the undo memento
    undoSave();

    // Convert the size_t stuff into int, because we need it for signed comparisons
    int patchHeight = static_cast<int>(_height);
    int patchWidth = static_cast<int>(_width);

    // Calculate the nearest corner vertex of this patch (to the sourcepatch vertices)
    PatchControlIter nearestControl = getClosestPatchControlToPatch(sourcePatch);

    PatchControlIter refControl = sourcePatch.getClosestPatchControlToPatch(*this);

    Vector2 texDiff = refControl->texcoord - nearestControl->texcoord;

    for (int col = 0; col < patchWidth; col++) {
        for (int row = 0; row < patchHeight; row++) {
            // Substract the texture coord difference from each control vertex
            ctrlAt(row, col).texcoord += texDiff;
        }
    }

    // Notify the patch about the change
    controlPointsChanged();
}

void Patch::pasteTextureProjected(const Face* face) {
    // Save the undo memento
    undoSave();

    /* greebo: If there is a face pointer being passed to this method,
     * the algorithm takes each vertex of the patch, projects it onto
     * the plane (defined by the brush face) and transforms the coordinates
     * into the texture space. */

    if (face != NULL) {
        // Get the normal vector of the face
        Plane3 plane = face->getPlane().getPlane().getNormalised();

        // Get the (already normalised) facePlane normal
        Vector3 faceNormal = plane.normal();

        // Get the conversion matrix from the FaceTextureDef, the local2World argument is the identity matrix
        Matrix4 worldToTexture = face->getProjection().getWorldToTexture(faceNormal, Matrix4::getIdentity());

        // Cycle through all the control points with an iterator
        for (PatchControlIter i = _ctrl.begin(); i != _ctrl.end(); ++i) {
            // Project the vertex onto the face plane and transform it into texture space
            i->texcoord = getProjectedTextureCoords(i->vertex, plane, worldToTexture);
        }

        // Notify the patch about the change
        controlPointsChanged();
    }
}

/* This clones the texture u/v coordinates from the <other> patch onto this one
 * Note: the patch dimensions must match exactly for this function to be performed.
 */
void Patch::pasteTextureCoordinates(const Patch* otherPatch) {
    undoSave();

    if (otherPatch != NULL) {

        if (otherPatch->getWidth() == _width && otherPatch->getHeight() == _height) {

            PatchControlConstIter other;
            PatchControlIter self;

            // Clone the texture coordinates one by one
            for (other = otherPatch->begin(), self = _ctrl.begin();
                 other != otherPatch->end();
                 ++other, ++self)
            {
                self->texcoord = other->texcoord;
            }

            // Notify the patch about the change
            controlPointsChanged();
        }
        else {
            rMessage() << "Error: Cannot copy texture coordinates, patch dimensions must match!\n";
        }
    }
}

void Patch::alignTexture(AlignEdge align)
{
    if (isDegenerate()) return;

    // A 5x3 patch has (5-1)x2 + (3-1)x2 edges at the border

    // The edges in texture space, sorted the same as in the winding
    std::vector<Vector2> texEdges;
    std::vector<Vector2> texCoords;

    // Calculate all edges in texture space
    for (std::size_t h = 0; h < _height-1; ++h)
    {
        for (std::size_t w = 0; w < _width-1; ++w)
        {
            texEdges.push_back(ctrlAt(0, w).texcoord - ctrlAt(0, w+1).texcoord);
            texCoords.push_back(ctrlAt(0,w).texcoord);

            texEdges.push_back(ctrlAt(_height-1, w+1).texcoord - ctrlAt(_height-1, w).texcoord);
            texCoords.push_back(ctrlAt(_height-1, w+1).texcoord);
        }

        texEdges.push_back(ctrlAt(h, 0).texcoord - ctrlAt(h+1, 0).texcoord);
        texCoords.push_back(ctrlAt(h, 0).texcoord);

        texEdges.push_back(ctrlAt(h+1, _width-1).texcoord - ctrlAt(h, _width-1).texcoord);
        texCoords.push_back(ctrlAt(h+1, _width-1).texcoord);
    }

    // Find the edge which is nearest to the s,t base vector, to classify them as "top" or "left"
    std::size_t bottomEdge = findBestEdgeForDirection(Vector2(1,0), texEdges);
    std::size_t leftEdge = findBestEdgeForDirection(Vector2(0,1), texEdges);
    std::size_t rightEdge = findBestEdgeForDirection(Vector2(0,-1), texEdges);
    std::size_t topEdge = findBestEdgeForDirection(Vector2(-1,0), texEdges);

    // The bottom edge is the one with the larger T texture coordinate
    if (texCoords[topEdge].y() > texCoords[bottomEdge].y())
    {
        std::swap(topEdge, bottomEdge);
    }

    // The right edge is the one with the larger S texture coordinate
    if (texCoords[rightEdge].x() < texCoords[leftEdge].x())
    {
        std::swap(rightEdge, leftEdge);
    }

    // Find the winding vertex index we're calculating the delta for
    std::size_t coordIndex = 0;
    // The dimension to move (1 for top/bottom, 0 for left right)
    std::size_t dim = 0;

	switch (align)
	{
	case IPatch::AlignEdge::Top:
		coordIndex = topEdge;
		dim = 1;
		break;
	case IPatch::AlignEdge::Bottom:
		coordIndex = bottomEdge;
		dim = 1;
		break;
	case IPatch::AlignEdge::Left:
		coordIndex = leftEdge;
		dim = 0;
		break;
	case IPatch::AlignEdge::Right:
		coordIndex = rightEdge;
		dim = 0;
		break;
	};

    Vector2 snapped = texCoords[coordIndex];

    // Snap the dimension we're going to change only (s for left/right, t for top/bottom)
    snapped[dim] = float_snapped(snapped[dim], 1.0);

    Vector2 delta = snapped - texCoords[coordIndex];

    // Shift the texture such that we hit the snapped coordinate
    translateTexCoords(delta);

    controlPointsChanged();
}

PatchTesselation& Patch::getTesselation()
{
    // Ensure the tesselation is up to date
    updateTesselation();

    return _mesh;
}

PatchRenderIndices Patch::getRenderIndices() const
{
	// Ensure the tesselation is up to date
	const_cast<Patch&>(*this).updateTesselation();

	PatchRenderIndices info;

	info.indices = _mesh.indices;
	info.lenStrips = _mesh.lenStrips;
	info.numStrips = _mesh.numStrips;

	return info;
}

PatchMesh Patch::getTesselatedPatchMesh() const
{
    // Ensure the tesselation is up to date
    const_cast<Patch&>(*this).updateTesselation();

    PatchMesh mesh;

    mesh.width = _mesh.width;
    mesh.height = _mesh.height;

    for (std::vector<MeshVertex>::const_iterator i = _mesh.vertices.begin();
        i != _mesh.vertices.end(); ++i)
    {
        VertexNT v;

        v.vertex = i->vertex;
        v.texcoord = i->texcoord;
        v.normal = i->normal;

        mesh.vertices.push_back(v);
    }

    return mesh;
}

void Patch::constructPlane(const AABB& aabb, int axis, std::size_t width, std::size_t height)
{
  setDims(width, height);

  int x, y, z;
  switch(axis)
  {
  case 2: x=0; y=1; z=2; break;
  case 1: x=0; y=2; z=1; break;
  case 0: x=1; y=2; z=0; break;
  default:
    ERROR_MESSAGE("invalid view-type");
    return;
  }

  if(_width < MIN_PATCH_WIDTH || _width > MAX_PATCH_WIDTH) _width = 3;
  if(_height < MIN_PATCH_HEIGHT || _height > MAX_PATCH_HEIGHT) _height = 3;

  Vector3 vStart;
  vStart[x] = aabb.origin[x] - aabb.extents[x];
  vStart[y] = aabb.origin[y] - aabb.extents[y];
  vStart[z] = aabb.origin[z];

  auto xAdj = std::abs((vStart[x] - (aabb.origin[x] + aabb.extents[x])) / static_cast<Vector3::ElementType>(_width - 1));
  auto yAdj = std::abs((vStart[y] - (aabb.origin[y] + aabb.extents[y])) / static_cast<Vector3::ElementType>(_height - 1));

  Vector3 vTmp;
  vTmp[z] = vStart[z];
  PatchControlIter pCtrl = _ctrl.begin();

  vTmp[y]=vStart[y];
  for (std::size_t h=0; h<_height; h++)
  {
    vTmp[x]=vStart[x];
    for (std::size_t w=0; w<_width; w++, ++pCtrl)
    {
      pCtrl->vertex = vTmp;
      vTmp[x]+=xAdj;
    }
    vTmp[y]+=yAdj;
  }

  scaleTextureNaturally();
}

// Returns the dimension for the given viewtype, used by the patch prefab routines
// constDim will be the dimension which is held constant for each patch row,
// matching to the view vector, e.g. Z for the XY viewtype
// It is ensured that dim1 < dim2
inline void assignDimsForViewType(OrthoOrientation viewType, std::size_t& dim1, std::size_t& dim2, std::size_t& constDim)
{
    switch (viewType)
    {
        case OrthoOrientation::XY: constDim = 2; break; // z coordinate is incremented each patch row
        case OrthoOrientation::YZ: constDim = 0; break; // x coordinate is incremented each patch row
        case OrthoOrientation::XZ: constDim = 1; break; // y coordinate is incremented each patch row
    };

    // Calculate the other two dimensions, such that colDim1 < colDim2
    dim1 = (constDim + 1) % 3;
    dim2 = (constDim + 2) % 3;

    if (dim2 < dim1)
    {
        std::swap(dim1, dim2);
    }
}

void Patch::constructBevel(const AABB& aabb, OrthoOrientation viewType)
{
    Vector3 vPos[3] =
    {
        aabb.origin - aabb.extents,
        aabb.origin,
        aabb.origin + aabb.extents
    };

    std::size_t dim1 = 0, dim2 = 0, constDim = 0;
    assignDimsForViewType(viewType, dim1, dim2, constDim);

    std::size_t lowlowhigh[3] = { 0, 0, 2 };
    std::size_t lowhighhigh[3] = { 0, 2, 2 };

    setDims(3, 3);

    PatchControlIter ctrl = _ctrl.begin();

    for (std::size_t h = 0; h < 3; ++h)
    {
        for (std::size_t w = 0; w < 3; ++w, ++ctrl)
        {
            // One of the dimensions stays constant per row
            ctrl->vertex[constDim] = vPos[h][constDim];

            // One dimension goes like "low", "low", "high" in a row
            ctrl->vertex[dim1] = vPos[ lowlowhigh[w] ][dim1];

            // One dimension goes like "low", "high", "high" in a row
            ctrl->vertex[dim2] = vPos[ lowhighhigh[w] ][dim2];
        }
    }

	if (viewType == OrthoOrientation::XZ)
	{
		invertMatrix();
	}
}

void Patch::constructEndcap(const AABB& aabb, OrthoOrientation viewType)
{
    Vector3 vPos[3] =
    {
        aabb.origin - aabb.extents,
        aabb.origin,
        aabb.origin + aabb.extents
    };

    std::size_t pEndIndex[] =
    {
        2, 0,
        2, 2,
        1, 2,
        0, 2,
        0, 0,
    };

    // Define the "row" dimension, e.g. z for an XY-oriented patch
    std::size_t dim1 = 0, dim2 = 0, constDim = 0;
    assignDimsForViewType(viewType, dim1, dim2, constDim);

    setDims(5, 3);

    PatchControlIter pCtrl = _ctrl.begin();

    for (std::size_t h = 0; h < 3; ++h)
    {
        std::size_t* pIndex = pEndIndex;

        for (std::size_t w = 0; w < 5; ++w, pIndex += 2, ++pCtrl)
        {
            pCtrl->vertex[dim1] = vPos[pIndex[0]][dim1];
            pCtrl->vertex[dim2] = vPos[pIndex[1]][dim2];
            pCtrl->vertex[constDim] = vPos[h][constDim];
        }
    }

	if (viewType != OrthoOrientation::XZ)
	{
		invertMatrix();
	}
}

void Patch::ConstructPrefab(const AABB& aabb, EPatchPrefab eType, OrthoOrientation viewType, std::size_t width, std::size_t height)
{
    if (eType == ePlane)
    {
        constructPlane(aabb, static_cast<int>(viewType), width, height);
    }
    else if (eType == eBevel)
    {
        constructBevel(aabb, viewType);
    }
    else if (eType == eEndCap)
    {
        constructEndcap(aabb, viewType);
    }
    else if (eType == eSqCylinder || eType == eCylinder ||
             eType == eDenseCylinder || eType == eVeryDenseCylinder ||
             eType == eCone || eType == eSphere)
    {
        Vector3 vPos[3] =
        {
            aabb.origin - aabb.extents,
            aabb.origin,
            aabb.origin + aabb.extents,
        };

        PatchControlIter pStart;

        switch(eType)
        {
        case eSqCylinder:
            setDims(9, 3);
            pStart = _ctrl.begin();
            break;
        case eDenseCylinder:
        case eVeryDenseCylinder:
        case eCylinder:
            setDims(9, 3);
            pStart = _ctrl.begin() + 1;
            break;
        case eCone: setDims(9, 3);
            pStart = _ctrl.begin() + 1;
            break;
        case eSphere:
            setDims(9, 5);
            pStart = _ctrl.begin() + (9+1);
            break;
        default:
            ERROR_MESSAGE("this should be unreachable");
            return;
        }

        // greebo: Determine which dimensions are assigned, depending on the view type

        // Define the "row" dimension, e.g. z for an XY-oriented cylinder
        std::size_t colDim1 = 0, colDim2 = 0, rowDim = 0;
        assignDimsForViewType(viewType, colDim1, colDim2, rowDim);

        // As first measure, assign a closed, axis-aligned loop of vertices for each patch row
        // Depending on the prefab type, further actions are performed in the switch statement below
        {
            // greebo: the other "column" dimensions are using the same pattern for each view
            // 0 = min, 1 = mid, 2 = max
            std::size_t pCylIndex[] =
            {
                0, 0,
                1, 0,
                2, 0,
                2, 1,
                2, 2,
                1, 2,
                0, 2,
                0, 1,
                0, 0,
            };

            for (std::size_t h = 0; h < 3; ++h)
            {
                std::size_t* pIndex = pCylIndex;

                PatchControlIter pCtrl = pStart;

                for (std::size_t w = 0; w < 8; ++w, ++pCtrl)
                {
                    // For the "row" dimension, we use the patch height 0..2
                    pCtrl->vertex[rowDim] = vPos[h][rowDim];

                    // Assign the other two "column" dimensions
                    pCtrl->vertex[colDim1] = vPos[pIndex[0]][colDim1];
                    pCtrl->vertex[colDim2] = vPos[pIndex[1]][colDim2];

                    pIndex += 2;
                }

                // Go to the next line, but only do that if we're not at the last one already
                // to not increment the pStart iterator beyond the end of the container
                if (h < 2) pStart += 9;
            }
        }

        switch(eType)
        {
        case eSqCylinder:
            {
                PatchControlIter pCtrl = _ctrl.begin();

                for (std::size_t h = 0; h < 3; ++h)
                {
                    pCtrl[8].vertex = pCtrl[0].vertex;

                    // Go to the next line
                    if (h < 2) pCtrl+=9;
                }
            }
            break;

        case eDenseCylinder:
        case eVeryDenseCylinder:
        case eCylinder:
            {
                // Regular cylinders get the first column snapped to the last one
                // to form a closed loop
                PatchControlIter pCtrl = _ctrl.begin();

                for (std::size_t h = 0; h < 3; ++h)
                {
                    pCtrl[0].vertex = pCtrl[8].vertex;

                    // Go to the next line
                    if (h < 2) pCtrl+=9;
                }
            }
            break;
        case eCone:
            // Close the control vertex loop of cones
            {
                PatchControlIter pCtrl = _ctrl.begin();

                for (std::size_t h = 0; h < 2; ++h)
                {
                    pCtrl[0].vertex = pCtrl[8].vertex;
                    // Go to the next line
                    if (h < 1) pCtrl+=9;
                }
            }
            // And "merge" the vertices of the last row into one single point
            {
                PatchControlIter pCtrl = _ctrl.begin() + 9*2;

                for (std::size_t w = 0; w < 9; ++w, ++pCtrl)
                {
                    pCtrl->vertex[colDim1] = vPos[1][colDim1];
                    pCtrl->vertex[colDim2] = vPos[1][colDim2];
                    pCtrl->vertex[rowDim] = vPos[2][rowDim];
                }
            }
            break;
        case eSphere:
            // Close the vertex loop for spheres too (middle row)
            {
                PatchControlIter pCtrl = _ctrl.begin() + 9;

                for (std::size_t h = 0; h < 3; ++h)
                {
                    pCtrl[0].vertex = pCtrl[8].vertex;

                    // Go to the next line
                    if (h < 2) pCtrl+=9;
                }
            }
            // Merge the first and last row vertices into one single point
            {
                PatchControlIter pCtrl = _ctrl.begin();

                for (std::size_t w = 0; w < 9; ++w, ++pCtrl)
                {
                    pCtrl->vertex[colDim1] = vPos[1][colDim1];
                    pCtrl->vertex[colDim2] = vPos[1][colDim2];
                    pCtrl->vertex[rowDim] = vPos[0][rowDim];
                }
            }
            {
                PatchControlIter pCtrl = _ctrl.begin() + (9*4);

                for (std::size_t w = 0; w < 9; ++w, ++pCtrl)
                {
                    pCtrl->vertex[colDim1] = vPos[1][colDim1];
                    pCtrl->vertex[colDim2] = vPos[1][colDim2];
                    pCtrl->vertex[rowDim] = vPos[2][rowDim];
                }
            }
            break;
        default:
            ERROR_MESSAGE("this should be unreachable");
            return;
        }

		if (eType == eDenseCylinder)
		{
			insertRemove(true, false, true);
		}

		if (eType == eVeryDenseCylinder)
		{
			insertRemove(true, false, false);
			insertRemove(true, false, true);
		}

		if (viewType == OrthoOrientation::XZ)
		{
			invertMatrix();
		}
	}

	scaleTextureNaturally();
}

namespace
{

Vector3 getAverageNormal(const Vector3& normal1, const Vector3& normal2, double thickness)
{
    // Beware of normals with 0 length
    if (normal1.getLengthSquared() == 0) return normal2;
    if (normal2.getLengthSquared() == 0) return normal1;

    // Both normals have length > 0
    Vector3 n1 = normal1.getNormalised();
    Vector3 n2 = normal2.getNormalised();

    // Get the angle bisector
    Vector3 normal = (n1 + n2).getNormalised();

    // Now calculate the length correction out of the angle
    // of the two normals
    auto factor = cos(n1.angle(n2) * 0.5);

    // Stretch the normal to fit the required thickness
    normal *= thickness;

    // Check for div by zero (if the normals are antiparallel)
    // and stretch the resulting normal, if necessary
    if (factor != 0)
    {
        normal /= factor;
    }

    return normal;
}

inline void calculateColTangentForCtrl(const Patch& sourcePatch, std::size_t row, std::size_t col, Vector3 colTangent[2])
{
    const auto& curCtrl = sourcePatch.ctrlAt(row, col);
    auto sourceWidth = sourcePatch.getWidth();

    // Are we at the beginning/end of the column?
    if (col == 0 || col == sourceWidth - 1)
    {
        // Get the next row index
        std::size_t nextCol = (col == sourceWidth - 1) ? (col - 1) : (col + 1);

        const PatchControl& colNeighbour = sourcePatch.ctrlAt(row, nextCol);

        // One available tangent
        colTangent[0] = colNeighbour.vertex - curCtrl.vertex;
        // Reverse it if we're at the end of the column
        colTangent[0] *= (col == sourceWidth - 1) ? -1 : +1;
    }
    // We are in between, two tangents can be calculated
    else
    {
        // Take two neighbouring vertices that should form a line segment
        const PatchControl& neighbour1 = sourcePatch.ctrlAt(row, col + 1);
        const PatchControl& neighbour2 = sourcePatch.ctrlAt(row, col - 1);

        // Calculate both available tangents
        colTangent[0] = neighbour1.vertex - curCtrl.vertex;
        colTangent[1] = neighbour2.vertex - curCtrl.vertex;

        // Reverse the second one
        colTangent[1] *= -1;

        // Cull redundant tangents
        if (math::isParallel(colTangent[1], colTangent[0]))
        {
            colTangent[1] = Vector3(0, 0, 0);
        }
    }
}

inline void calculateRowTangentForCtrl(const Patch& sourcePatch, std::size_t row, std::size_t col, Vector3 rowTangent[2])
{
    const auto& curCtrl = sourcePatch.ctrlAt(row, col);
    auto sourceHeight = sourcePatch.getHeight();

    // Are we at the beginning or the end?
    if (row == 0 || row == sourceHeight - 1)
    {
        // Yes, only calculate one row tangent
        // Get the next row index
        std::size_t nextRow = (row == sourceHeight - 1) ? (row - 1) : (row + 1);

        const PatchControl& rowNeighbour = sourcePatch.ctrlAt(nextRow, col);

        // First tangent
        rowTangent[0] = rowNeighbour.vertex - curCtrl.vertex;
        // Reverse it accordingly
        rowTangent[0] *= (row == sourceHeight - 1) ? -1 : +1;
    }
    else
    {
        // Two tangents to calculate
        const PatchControl& rowNeighbour1 = sourcePatch.ctrlAt(row + 1, col);
        const PatchControl& rowNeighbour2 = sourcePatch.ctrlAt(row - 1, col);

        // First tangent
        rowTangent[0] = rowNeighbour1.vertex - curCtrl.vertex;
        rowTangent[1] = rowNeighbour2.vertex - curCtrl.vertex;

        // Reverse the second one
        rowTangent[1] *= -1;

        // Cull redundant tangents
        if (math::isParallel(rowTangent[1], rowTangent[0]))
        {
            rowTangent[1] = Vector3(0, 0, 0);
        }
    }
}

Vector3 calculateNormalForTangents(Vector3 colTangent[2], Vector3 rowTangent[2], const float thickness)
{
    Vector3 normal;

    // If two column tangents are available, take the length-corrected average
    if (colTangent[1].getLengthSquared() > 0)
    {
        // Two column normals to calculate
        Vector3 normal1 = rowTangent[0].cross(colTangent[0]);
        Vector3 normal2 = rowTangent[0].cross(colTangent[1]);

        if (normal1.getLengthSquared() > 0)
        {
            normal1.normalise();
        }

        if (normal2.getLengthSquared() > 0)
        {
            normal2.normalise();
        }

        normal = getAverageNormal(normal1, normal2, thickness);

        // Scale the normal down, as it is multiplied with thickness later on
        normal /= thickness;
    }
    else
    {
        // One column tangent available, maybe we have a second rowtangent?
        if (rowTangent[1].getLengthSquared() > 0)
        {
            // Two row normals to calculate
            Vector3 normal1 = rowTangent[0].cross(colTangent[0]);
            Vector3 normal2 = rowTangent[1].cross(colTangent[0]);

            if (normal1.getLengthSquared() > 0)
            {
                normal1.normalise();
            }

            if (normal2.getLengthSquared() > 0)
            {
                normal2.normalise();
            }

            normal = getAverageNormal(normal1, normal2, thickness);

            // Scale the normal down, as it is multiplied with thickness later on
            normal /= thickness;
        }
        else
        {
            normal = rowTangent[0].cross(colTangent[0]);

            if (normal.getLengthSquared() > 0)
            {
                normal.normalise();
            }
        }
    }

    return normal;
}

}

void Patch::createThickenedOpposite(const Patch& sourcePatch,
                                    const float thickness,
                                    const int axis)
{
    // Clone the dimensions from the other patch
    setDims(sourcePatch.getWidth(), sourcePatch.getHeight());

    // Also inherit the tesselation from the source patch
    setFixedSubdivisions(sourcePatch.subdivisionsFixed(), sourcePatch.getSubdivisions());

    // Copy the shader from the source patch
    setShader(sourcePatch.getShader());

    // if extrudeAxis == 0,0,0 the patch is extruded along its vertex normals
    Vector3 extrudeAxis(0,0,0);

    switch (axis) {
        case 0: // X-Axis
            extrudeAxis = Vector3(1,0,0);
            break;
        case 1: // Y-Axis
            extrudeAxis = Vector3(0,1,0);
            break;
        case 2: // Z-Axis
            extrudeAxis = Vector3(0,0,1);
            break;
        default:
            // Default value already set during initialisation
            break;
    }

    for (std::size_t col = 0; col < _width; col++)
    {
        for (std::size_t row = 0; row < _height; row++)
        {
            // The current control vertex on the other patch
            const PatchControl& curCtrl = sourcePatch.ctrlAt(row, col);

            Vector3 normal;

            // Are we extruding along vertex normals (i.e. extrudeAxis == 0,0,0)?
            if (extrudeAxis == Vector3(0,0,0))
            {
                // The col tangents (empty if 0,0,0)
                Vector3 colTangent[2] = { Vector3(0,0,0), Vector3(0,0,0) };

                calculateColTangentForCtrl(sourcePatch, row, col, colTangent);

                // Calculate the tangent vectors to the next row
                Vector3 rowTangent[2] = { Vector3(0,0,0), Vector3(0,0,0) };

                calculateRowTangentForCtrl(sourcePatch, row, col, rowTangent);

                normal = calculateNormalForTangents(colTangent, rowTangent, thickness);
            }
            else
            {
                // Take the predefined extrude direction instead
                normal = extrudeAxis;
            }

            // Store the new coordinates into this patch at the current coords
            ctrlAt(row, col).vertex = curCtrl.vertex + normal*thickness;

            // Clone the texture cooordinates of the source patch
            ctrlAt(row, col).texcoord = curCtrl.texcoord;
        }
    }

    // Notify the patch about the change
    controlPointsChanged();
}

void Patch::createThickenedWall(const Patch& sourcePatch,
                                const Patch& targetPatch,
                                const int wallIndex)
{
    // Copy the shader from the source patch
    setShader(sourcePatch.getShader());

    // The start and end control vertex indices
    int start = 0;
    int end = 0;
    // The increment (incr = 1 for the "long" edge, incr = width for the "short" edge)
    int incr = 1;

    // These are the target dimensions of this wall
    // The width is depending on which edge is "seamed".
    int cols = 0;
    int rows = 3;

    int sourceWidth = static_cast<int>(sourcePatch.getWidth());
    int sourceHeight = static_cast<int>(sourcePatch.getHeight());

    bool sourceTesselationFixed = sourcePatch.subdivisionsFixed();
    Subdivisions sourceTesselationX(sourcePatch.getSubdivisions().x(), 1);
    Subdivisions sourceTesselationY(sourcePatch.getSubdivisions().y(), 1);

    // Determine which of the four edges have to be connected
    // and calculate the start, end & stepsize for the following loop
    switch (wallIndex) {
        case 0:
            cols = sourceWidth;
            start = 0;
            end = sourceWidth - 1;
            incr = 1;
            setFixedSubdivisions(sourceTesselationFixed, sourceTesselationX);
            break;
        case 1:
            cols = sourceWidth;
            start = sourceWidth * (sourceHeight-1);
            end = sourceWidth*sourceHeight - 1;
            incr = 1;
            setFixedSubdivisions(sourceTesselationFixed, sourceTesselationX);
            break;
        case 2:
            cols = sourceHeight;
            start = 0;
            end = sourceWidth*(sourceHeight-1);
            incr = sourceWidth;
            setFixedSubdivisions(sourceTesselationFixed, sourceTesselationY);
            break;
        case 3:
            cols = sourceHeight;
            start = sourceWidth - 1;
            end = sourceWidth*sourceHeight - 1;
            incr = sourceWidth;
            setFixedSubdivisions(sourceTesselationFixed, sourceTesselationY);
            break;
    }

    setDims(cols, rows);

    const PatchControlArray& sourceCtrl = sourcePatch.getControlPoints();
    const PatchControlArray& targetCtrl = targetPatch.getControlPoints();

    int col = 0;
    // Now go through the control vertices with these calculated stepsize
    for (int idx = start; idx <= end; idx += incr, col++) {
        Vector3 sourceCoord = sourceCtrl[idx].vertex;
        Vector3 targetCoord = targetCtrl[idx].vertex;
        Vector3 middleCoord = (sourceCoord + targetCoord) / 2;

        // Now assign the vertex coordinates
        ctrlAt(0, col).vertex = sourceCoord;
        ctrlAt(1, col).vertex = middleCoord;
        ctrlAt(2, col).vertex = targetCoord;
    }

	if (wallIndex == 0 || wallIndex == 3) {
		invertMatrix();
	}

    // Notify the patch about the change
    controlPointsChanged();
}

void Patch::stitchTextureFrom(Patch& sourcePatch) {
    // Save the undo memento
    undoSave();

    // Convert the size_t stuff into int, because we need it for signed comparisons
    int patchHeight = static_cast<int>(_height);
    int patchWidth = static_cast<int>(_width);

    // Calculate the nearest corner vertex of this patch (to the sourcepatch vertices)
    PatchControlIter nearestControl = getClosestPatchControlToPatch(sourcePatch);

    PatchControlIter refControl = sourcePatch.getClosestPatchControlToPatch(*this);

    // Get the distance in texture space
    Vector2 texDiff = refControl->texcoord - nearestControl->texcoord;

    // The floored values
    Vector2 floored(floor(fabs(texDiff[0])), floor(fabs(texDiff[1])));

    // Compute the shift applicable to all vertices
    Vector2 shift;
    shift[0] = (fabs(texDiff[0])>1.0E-4) ? -floored[0] * texDiff[0]/fabs(texDiff[0]) : 0.0f;
    shift[1] = (fabs(texDiff[1])>1.0E-4) ? -floored[1] * texDiff[1]/fabs(texDiff[1]) : 0.0f;

    // Now shift all the texture vertices in the right direction, so that this patch
    // is getting as close as possible to the origin in texture space.
    for (PatchControlIter i = _ctrl.begin(); i != _ctrl.end(); ++i) {
        i->texcoord += shift;
    }

    int sourceHeight = static_cast<int>(sourcePatch.getHeight());
    int sourceWidth = static_cast<int>(sourcePatch.getWidth());

    // Go through all the 3D vertices and see if they are shared by the other patch
    for (int col = 0; col < patchWidth; col++) {
        for (int row = 0; row < patchHeight; row++) {

            // The control vertex that is to be manipulated
            PatchControl& self = ctrlAt(row, col);

            // Check all the other patch controls for spatial coincidences
            for (int srcCol = 0; srcCol < sourceWidth; srcCol++) {
                for (int srcRow = 0; srcRow < sourceHeight; srcRow++) {
                    // Get the other control
                    const PatchControl& other = sourcePatch.ctrlAt(srcRow, srcCol);

                    auto dist = (other.vertex - self.vertex).getLength();

                    // Allow the coords to be a _bit_ distant
                    if (fabs(dist) < 0.005) {
                        // Assimilate the texture coordinates
                        self.texcoord = other.texcoord;
                    }
                }
            }
        }
    }

    // Notify the patch about the change
    controlPointsChanged();
}

void Patch::normaliseTexture()
{
    selection::algorithm::TextureNormaliser::NormalisePatch(*this);
}

const Subdivisions& Patch::getSubdivisions() const
{
    return _subDivisions;
}

void Patch::setFixedSubdivisions(bool isFixed, const Subdivisions& divisions)
{
    undoSave();

    _patchDef3 = isFixed;
    _subDivisions = divisions;

	if (_subDivisions.x() == 0)
	{
		_subDivisions.x() = 4;
	}

	if (_subDivisions.y() == 0)
	{
		_subDivisions.y() = 4;
	}

    SceneChangeNotify();
    textureChanged();
    controlPointsChanged();
}

bool Patch::subdivisionsFixed() const
{
    return _patchDef3;
}

bool Patch::getIntersection(const Ray& ray, Vector3& intersection)
{
    std::vector<RenderIndex>::const_iterator stripStartIndex = _mesh.indices.begin();

    // Go over each quad strip and intersect the ray with its triangles
    for (std::size_t strip = 0; strip < _mesh.numStrips; ++strip)
    {
        // Iterate over the indices. The +2 increment will lead up to the next quad
        for (std::vector<RenderIndex>::const_iterator indexIter = stripStartIndex;
            indexIter + 2 < stripStartIndex + _mesh.lenStrips; indexIter += 2)
        {
            Vector3 triangleIntersection;

            // Run a selection test against the quad's triangles
            {
                const Vector3& p1 = _mesh.vertices[*indexIter].vertex;
                const Vector3& p2 = _mesh.vertices[*(indexIter + 1)].vertex;
                const Vector3& p3 = _mesh.vertices[*(indexIter + 2)].vertex;

                if (ray.intersectTriangle(p1, p2, p3, triangleIntersection) == Ray::POINT)
                {
                    intersection = triangleIntersection;
                    return true;
                }
            }

            {
                const Vector3& p1 = _mesh.vertices[*(indexIter + 2)].vertex;
                const Vector3& p2 = _mesh.vertices[*(indexIter + 1)].vertex;
                const Vector3& p3 = _mesh.vertices[*(indexIter + 3)].vertex;

                if (ray.intersectTriangle(p1, p2, p3, triangleIntersection) == Ray::POINT)
                {
                    intersection = triangleIntersection;
                    return true;
                }
            }
        }

        stripStartIndex += _mesh.lenStrips;
    }

    return false;
}

void Patch::textureChanged()
{
    _node.onMaterialChanged();

    for (auto i = _observers.begin(); i != _observers.end();)
    {
        (*i++)->onPatchTextureChanged();
    }

    signal_patchTextureChanged().emit();
}

void Patch::attachObserver(Observer* observer)
{
    _observers.insert(observer);
}

void Patch::detachObserver(Observer* observer)
{
    _observers.erase(observer);
}

sigc::signal<void>& Patch::signal_patchTextureChanged()
{
    static sigc::signal<void> _sigPatchTextureChanged;
    return _sigPatchTextureChanged;
}

void Patch::queueTesselationUpdate()
{
    _tesselationChanged = true;
}