File: TextureTool.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 (2305 lines) | stat: -rw-r--r-- 94,012 bytes parent folder | download | duplicates (3)
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
#include "RadiantTest.h"

#include <set>
#include <sigc++/connection.h>
#include "imap.h"
#include "ipatch.h"
#include "igrid.h"
#include "iselectable.h"
#include "itexturetoolmodel.h"
#include "icommandsystem.h"
#include "iselection.h"
#include "scenelib.h"
#include "algorithm/Primitives.h"
#include "render/TextureToolView.h"
#include "algorithm/View.h"
#include "selection/SelectionVolume.h"
#include "Rectangle.h"

namespace test
{

using TextureToolTest = RadiantTest;

inline std::size_t getTextureToolNodeCount()
{
    std::size_t count = 0;

    GlobalTextureToolSceneGraph().foreachNode([&](const textool::INode::Ptr& node)
    {
        ++count;
        return true;
    });

    return count;
}

inline textool::INode::Ptr getFirstTextureToolNode()
{
    textool::INode::Ptr returnValue;

    GlobalTextureToolSceneGraph().foreachNode([&](const textool::INode::Ptr& node)
    {
        returnValue = node;
        return false;
    });

    return returnValue;
}

inline std::vector<textool::INode::Ptr> getAllTextureToolNodes()
{
    std::vector<textool::INode::Ptr> list;

    GlobalTextureToolSceneGraph().foreachNode([&](const textool::INode::Ptr& node)
    {
        list.push_back(node);
        return true;
    });

    return list;
}

std::vector<textool::INode::Ptr> getAllSelectedTextoolNodes()
{
    std::vector<textool::INode::Ptr> selectedNodes;

    GlobalTextureToolSelectionSystem().foreachSelectedNode([&](const textool::INode::Ptr& node)
    {
        selectedNodes.push_back(node);
        return true;
    });

    return selectedNodes;
}

std::vector<textool::INode::Ptr> getAllSelectedComponentNodes()
{
    std::vector<textool::INode::Ptr> selectedNodes;

    GlobalTextureToolSelectionSystem().foreachSelectedComponentNode([&](const textool::INode::Ptr& node)
    {
        selectedNodes.push_back(node);
        return true;
    });

    return selectedNodes;
}

class SelectionChangedCatcher
{
private:
    bool _signalFired;
    sigc::connection _connection;

public:
    SelectionChangedCatcher() :
        _signalFired(false)
    {
        _connection = GlobalTextureToolSelectionSystem().signal_selectionChanged().connect([this]
        {
            _signalFired = true;
        });
    }

    bool signalHasFired() const
    {
        return _signalFired;
    }

    void reset()
    {
        _signalFired = false;
    }

    ~SelectionChangedCatcher()
    {
        _connection.disconnect();
    }
};

// Checks that changing the regular scene selection will have an effect on the tex tool scene
TEST_F(TextureToolTest, SceneGraphObservesSelection)
{
    std::string material = "textures/numbers/1";
    auto worldspawn = GlobalMapModule().findOrInsertWorldspawn();
    auto brush1 = algorithm::createCubicBrush(worldspawn, Vector3(0,0,0), material);
    auto brush2 = algorithm::createCubicBrush(worldspawn, Vector3(0,256,256), material);

    // Empty tex tool scenegraph on empty scene selection
    EXPECT_EQ(GlobalSelectionSystem().countSelected(), 0) << "Non-empty selection at startup";
    EXPECT_EQ(getTextureToolNodeCount(), 0) << "There shouldn't be any textool nodes when the scene is empty";
    EXPECT_EQ(GlobalTextureToolSceneGraph().getActiveMaterial(), "") << "Active material shoud be empty";

    Node_setSelected(brush1, true);
    EXPECT_EQ(GlobalSelectionSystem().countSelected(), 1) << "1 Brush must be selected";

    // We don't know how many tex tool nodes there are, but it should be more than 0
    auto nodeCount = getTextureToolNodeCount();
    EXPECT_GT(nodeCount, 0) << "There should be some tex tool nodes now";
    EXPECT_EQ(GlobalTextureToolSceneGraph().getActiveMaterial(), material) << "Active material mismatch";

    Node_setSelected(brush2, true);
    EXPECT_EQ(GlobalSelectionSystem().countSelected(), 2) << "2 Brushes must be selected";
    EXPECT_EQ(GlobalTextureToolSceneGraph().getActiveMaterial(), material) << "Active material mismatch";

    // Should be even more now
    auto nodeCount2 = getTextureToolNodeCount();
    EXPECT_GT(nodeCount2, nodeCount) << "There should be more tex tool nodes now";

    GlobalSelectionSystem().setSelectedAll(false);
    EXPECT_EQ(GlobalSelectionSystem().countSelected(), 0) << "Non-empty selection at shutdown";
    EXPECT_EQ(getTextureToolNodeCount(), 0) << "There shouldn't be any textool nodes when the scene is empty";
    EXPECT_EQ(GlobalTextureToolSceneGraph().getActiveMaterial(), "") << "Active material should be empty again";
}

TEST_F(TextureToolTest, SceneGraphNeedsUniqueShader)
{
    std::string material1 = "textures/numbers/1";
    std::string material2 = "textures/numbers/2";
    auto worldspawn = GlobalMapModule().findOrInsertWorldspawn();
    auto brush1 = algorithm::createCubicBrush(worldspawn, Vector3(0, 0, 0), material1);
    auto brush2 = algorithm::createCubicBrush(worldspawn, Vector3(0, 256, 256), material2);

    Node_setSelected(brush1, true);
    EXPECT_EQ(GlobalSelectionSystem().countSelected(), 1) << "1 Brush must be selected";
    EXPECT_EQ(GlobalTextureToolSceneGraph().getActiveMaterial(), material1) << "Active material mismatch";

    // We don't know how many tex tool nodes there are, but it should be more than 0
    EXPECT_GT(getTextureToolNodeCount(), 0) << "There should be some tex tool nodes now";

    Node_setSelected(brush2, true);
    EXPECT_EQ(GlobalSelectionSystem().countSelected(), 2) << "2 Brushes must be selected";

    EXPECT_EQ(getTextureToolNodeCount(), 0) << "There should be no nodes now, since the material is non unique";
    EXPECT_EQ(GlobalTextureToolSceneGraph().getActiveMaterial(), "") << "Active material mismatch";

    // Deselect brush 1, now only brush 2 is selected
    Node_setSelected(brush1, false);
    EXPECT_EQ(GlobalSelectionSystem().countSelected(), 1) << "1 Brush must be selected";
    EXPECT_GT(getTextureToolNodeCount(), 0) << "There should be some tex tool nodes again";
    EXPECT_EQ(GlobalTextureToolSceneGraph().getActiveMaterial(), material2) << "Active material mismatch";
}

TEST_F(TextureToolTest, SceneGraphRecognisesBrushes)
{
    auto worldspawn = GlobalMapModule().findOrInsertWorldspawn();
    auto brush1 = algorithm::createCubicBrush(worldspawn, Vector3(0, 0, 0), "textures/numbers/1");

    Node_setSelected(brush1, true);
    EXPECT_EQ(GlobalSelectionSystem().countSelected(), 1) << "1 Brush must be selected";

    // We don't know how many tex tool nodes there are, but it should be more than 0
    EXPECT_GT(getTextureToolNodeCount(), 0) << "There should be some tex tool nodes now";
}

TEST_F(TextureToolTest, SceneGraphRecognisesSingleFaces)
{
    auto worldspawn = GlobalMapModule().findOrInsertWorldspawn();
    auto brush1 = algorithm::createCubicBrush(worldspawn, Vector3(0, 0, 0), "textures/numbers/1");
    auto brush2 = algorithm::createCubicBrush(worldspawn, Vector3(100, 100, 0), "textures/numbers/1");

    auto faceUp = algorithm::findBrushFaceWithNormal(Node_getIBrush(brush1), Vector3(0, 0, 1));

    // Position the camera top-down, similar to what an XY view is seeing
    render::View viewFaceUp(true);
    algorithm::constructCameraView(viewFaceUp, brush1->localAABB(), { 0, 0, -1 }, { -90, 0, 0 });

    SelectionVolume testFaceUp(viewFaceUp);
    GlobalSelectionSystem().selectPoint(testFaceUp, selection::SelectionSystem::eToggle, true);

    EXPECT_EQ(GlobalSelectionSystem().countSelectedComponents(), 1) << "1 Face component should be selected";
    EXPECT_EQ(getTextureToolNodeCount(), 1) << "There should be exactly 1 tex tool node in the scene";

    Node_setSelected(brush2, true);

    EXPECT_EQ(GlobalSelectionSystem().countSelectedComponents(), 1) << "1 Face component should be selected";
    EXPECT_EQ(GlobalSelectionSystem().countSelected(), 1) << "1 Brush should be selected";
    
    EXPECT_EQ(getTextureToolNodeCount(), 7) << "There should be 7 tex tool nodes in the scene, 1 single face + 6 brush faces";
}

TEST_F(TextureToolTest, SceneGraphRecognisesPatches)
{
    auto worldspawn = GlobalMapModule().findOrInsertWorldspawn();
    auto patch = GlobalPatchModule().createPatch(patch::PatchDefType::Def2);
    scene::addNodeToContainer(patch, worldspawn);

    Node_setSelected(patch, true);
    EXPECT_EQ(GlobalSelectionSystem().countSelected(), 1) << "1 patch must be selected";

    // We don't know how many tex tool nodes there are, but it should be more than 0
    EXPECT_GT(getTextureToolNodeCount(), 0) << "There should be some tex tool nodes now";
}

TEST_F(TextureToolTest, SceneGraphNotifiedAboutFaceDestruction)
{
    auto worldspawn = GlobalMapModule().findOrInsertWorldspawn();
    auto brush1 = algorithm::createCubicBrush(worldspawn, Vector3(0, 0, 0), "textures/numbers/1");
    Node_setSelected(brush1, true);

    EXPECT_EQ(GlobalSelectionSystem().countSelected(), 1) << "1 Brush should be selected";
    EXPECT_EQ(getTextureToolNodeCount(), 6) << "There should be exactly 6 tex tool nodes in the scene";

    Node_getIBrush(brush1)->clear();

    EXPECT_EQ(GlobalSelectionSystem().countSelected(), 1) << "Technically, 1 Brush should still be selected";
    EXPECT_EQ(getTextureToolNodeCount(), 0) << "All faces have been cleared, tex tool scene should be empty now";
}

// Selecting an inhomogenously textured brush will result in an empty scene graph
TEST_F(TextureToolTest, SceneGraphSkipsInhomogeneousBrushes)
{
    auto worldspawn = GlobalMapModule().findOrInsertWorldspawn();
    auto brush1 = algorithm::createCubicBrush(worldspawn, Vector3(0, 0, 0), "textures/numbers/1");
    Node_getIBrush(brush1)->getFace(2).setShader("textures/numbers/2");

    Node_setSelected(brush1, true);
    EXPECT_EQ(GlobalSelectionSystem().countSelected(), 1) << "1 Brush must be selected";

    // No tex tool nodes should show up here
    EXPECT_EQ(getTextureToolNodeCount(), 0) << "There shouldn't be any tex tool nodes here";
}

// Harmonising the textures of all brush faces should make it show up in the texture tool
TEST_F(TextureToolTest, SceneGraphUpdatesOnBrushMaterialChange)
{
    auto worldspawn = GlobalMapModule().findOrInsertWorldspawn();
    auto brush1Node = algorithm::createCubicBrush(worldspawn, Vector3(0, 0, 0), "textures/numbers/1");
    auto brush1 = Node_getIBrush(brush1Node);
    brush1->getFace(2).setShader("textures/numbers/2");

    Node_setSelected(brush1Node, true);
    EXPECT_EQ(GlobalSelectionSystem().countSelected(), 1) << "1 Brush must be selected";

    // No tex tool nodes should show up here
    EXPECT_EQ(getTextureToolNodeCount(), 0) << "There shouldn't be any tex tool nodes here";

    // Now apply the same texture to all faces
    for (int i = 0; i < brush1->getNumFaces(); ++i)
    {
        brush1->getFace(i).setShader("textures/numbers/1");
    }

    EXPECT_EQ(getTextureToolNodeCount(), brush1->getNumFaces()) << "There shoud be 6 faces in the tex tool";
}

TEST_F(TextureToolTest, PatchNodeBounds)
{
    auto worldspawn = GlobalMapModule().findOrInsertWorldspawn();
    auto patchNode = GlobalPatchModule().createPatch(patch::PatchDefType::Def2);

    auto patch = Node_getIPatch(patchNode);
    patch->setDims(3, 3);

    auto origin = Vector2(5.4, -78.3);
    auto step = 0.45;
    // Accumulate the test bounds
    AABB checkedBounds;

    for (auto col = 0; col < 3; ++col)
    {
        for (auto row = 0; row < 3; ++row)
        {
            auto& ctrl = patch->getTransformedCtrlAt(row, col);

            ctrl.texcoord = Vector2(origin.x() + step * col, origin.y() + step * row);
            checkedBounds.includePoint({ ctrl.texcoord.x(), ctrl.texcoord.y(), 0 });
        }
    }

    patch->freezeTransform();

    scene::addNodeToContainer(patchNode, worldspawn);
    Node_setSelected(patchNode, true);

    auto node = getFirstTextureToolNode();
    EXPECT_TRUE(node) << "No texture tool node here";

    EXPECT_TRUE(math::isNear(node->localAABB().getOrigin(), checkedBounds.getOrigin(), 0.01)) << 
        "Bounds mismatch, got " << node->localAABB().getOrigin() << " instead of " << checkedBounds.getOrigin();
    EXPECT_TRUE(math::isNear(node->localAABB().getExtents(), checkedBounds.getExtents(), 0.01)) <<
        "Bounds mismatch, got " << node->localAABB().getExtents() << " instead of " << checkedBounds.getExtents();
}

TEST_F(TextureToolTest, ForeachSelectedNode)
{
    auto worldspawn = GlobalMapModule().findOrInsertWorldspawn();
    auto brush1 = algorithm::createCubicBrush(worldspawn, Vector3(0, 0, 0), "textures/numbers/1");
    auto brush2 = algorithm::createCubicBrush(worldspawn, Vector3(0, 256, 256), "textures/numbers/1");
    auto patchNode = GlobalPatchModule().createPatch(patch::PatchDefType::Def2);
    Node_getIPatch(patchNode)->setDims(3, 3);
    Node_getIPatch(patchNode)->setShader("textures/numbers/1");

    Node_setSelected(brush1, true);
    Node_setSelected(brush2, true);
    Node_setSelected(patchNode, true);
    EXPECT_EQ(GlobalSelectionSystem().countSelected(), 3) << "3 items must be selected";

    // We don't know how many tex tool nodes there are, but it should be more than 0
    EXPECT_GT(getTextureToolNodeCount(), 0) << "There should be some tex tool nodes now";

    std::set<textool::INode::Ptr> selectedNodes;
    std::size_t i = 0;

    // Selected every odd node
    GlobalTextureToolSceneGraph().foreachNode([&](const textool::INode::Ptr& node)
    {
        if (++i % 2 == 1)
        {
            node->setSelected(true);
            selectedNodes.emplace(node);
        }

        return true;
    });

    std::size_t selectedCount = 0;
    GlobalTextureToolSelectionSystem().foreachSelectedNode([&](const textool::INode::Ptr& node)
    {
        ++selectedCount;
        EXPECT_TRUE(selectedNodes.count(node) > 0) << "Node shouldn't be selected";
        return true;
    });

    EXPECT_EQ(selectedCount, selectedNodes.size()) << "Selection count didn't match";
}

constexpr int TEXTOOL_WIDTH = 500;
constexpr int TEXTOOL_HEIGHT = 400;

inline scene::INodePtr setupPatchNodeForTextureTool()
{
    auto worldspawn = GlobalMapModule().findOrInsertWorldspawn();
    auto patchNode = algorithm::createPatchFromBounds(worldspawn, AABB(Vector3(4, 50, 60), Vector3(64, 128, 256)), "textures/numbers/1");

    auto patch = Node_getIPatch(patchNode);
    patch->scaleTextureNaturally();
    patch->controlPointsChanged();

    // Select this node in the scene, to make it available in the texture tool
    Node_setSelected(patchNode, true);

    return patchNode;
}

inline scene::INodePtr setupBrushNodeForTextureTool(const std::string& material = "textures/numbers/1")
{
    auto worldspawn = GlobalMapModule().findOrInsertWorldspawn();
    auto brush = algorithm::createCubicBrush(worldspawn, Vector3(0, 256, 256), material);
    scene::addNodeToContainer(brush, worldspawn);

    // Put all faces into the tex tool scene
    Node_setSelected(brush, true);

    return brush;
}

inline textool::IFaceNode::Ptr findTexToolFaceWithNormal(const Vector3& normal)
{
    textool::IFaceNode::Ptr result;

    GlobalTextureToolSceneGraph().foreachNode([&](const textool::INode::Ptr& node)
    {
        auto faceNode = std::dynamic_pointer_cast<textool::IFaceNode>(node);

        if (faceNode && math::isNear(faceNode->getFace().getPlane3().normal(), normal, 0.01))
        {
            result = faceNode;
        }

        return result == nullptr;
    });

    return result;
}

// Default manipulator mode should be "Drag"
TEST_F(TextureToolTest, DefaultManipulatorMode)
{
    EXPECT_EQ(GlobalTextureToolSelectionSystem().getActiveManipulatorType(), selection::IManipulator::Drag);
    EXPECT_EQ(GlobalTextureToolSelectionSystem().getActiveManipulator()->getType(), selection::IManipulator::Drag);
}

TEST_F(TextureToolTest, DefaultSelectionMode)
{
    EXPECT_EQ(GlobalTextureToolSelectionSystem().getSelectionMode(), textool::SelectionMode::Surface);
}

TEST_F(TextureToolTest, ToggleManipulatorModesByCmd)
{
    // We're starting in "Drag" mode, so toggling the default mode should do nothing
    GlobalCommandSystem().executeCommand("ToggleTextureToolManipulatorMode", { "Drag" });
    EXPECT_EQ(GlobalTextureToolSelectionSystem().getActiveManipulatorType(), selection::IManipulator::Drag);

    // Toggle to Rotate
    GlobalCommandSystem().executeCommand("ToggleTextureToolManipulatorMode", { "Rotate" });
    EXPECT_EQ(GlobalTextureToolSelectionSystem().getActiveManipulatorType(), selection::IManipulator::Rotate);

    // Toggle from Rotate back to Drag
    GlobalCommandSystem().executeCommand("ToggleTextureToolManipulatorMode", { "Rotate" });
    EXPECT_EQ(GlobalTextureToolSelectionSystem().getActiveManipulatorType(), selection::IManipulator::Drag);

    // Toggle to Rotate again
    GlobalCommandSystem().executeCommand("ToggleTextureToolManipulatorMode", { "Rotate" });
    EXPECT_EQ(GlobalTextureToolSelectionSystem().getActiveManipulatorType(), selection::IManipulator::Rotate);

    // Toggle Drag explicitly
    GlobalCommandSystem().executeCommand("ToggleTextureToolManipulatorMode", { "Drag" });
    EXPECT_EQ(GlobalTextureToolSelectionSystem().getActiveManipulatorType(), selection::IManipulator::Drag);
}

TEST_F(TextureToolTest, ToggleManipulatorMode)
{
    // We're starting in "Drag" mode, so toggling the default mode should do nothing
    GlobalTextureToolSelectionSystem().toggleManipulatorMode(selection::IManipulator::Drag);
    EXPECT_EQ(GlobalTextureToolSelectionSystem().getActiveManipulatorType(), selection::IManipulator::Drag);

    // Toggle to Rotate
    GlobalTextureToolSelectionSystem().toggleManipulatorMode(selection::IManipulator::Rotate);
    EXPECT_EQ(GlobalTextureToolSelectionSystem().getActiveManipulatorType(), selection::IManipulator::Rotate);

    // Toggle from Rotate back to Drag
    GlobalTextureToolSelectionSystem().toggleManipulatorMode(selection::IManipulator::Rotate);
    EXPECT_EQ(GlobalTextureToolSelectionSystem().getActiveManipulatorType(), selection::IManipulator::Drag);

    // Toggle to Rotate again
    GlobalTextureToolSelectionSystem().toggleManipulatorMode(selection::IManipulator::Rotate);
    EXPECT_EQ(GlobalTextureToolSelectionSystem().getActiveManipulatorType(), selection::IManipulator::Rotate);

    // Toggle Drag explicitly
    GlobalTextureToolSelectionSystem().toggleManipulatorMode(selection::IManipulator::Drag);
    EXPECT_EQ(GlobalTextureToolSelectionSystem().getActiveManipulatorType(), selection::IManipulator::Drag);
}

TEST_F(TextureToolTest, ManipulatorModeChangedSignal)
{
    bool signalFired = false;
    selection::IManipulator::Type signalArgument;

    // Subscribe to the changed signal
    sigc::connection conn = GlobalTextureToolSelectionSystem().signal_activeManipulatorChanged().connect(
        [&](selection::IManipulator::Type type)
    {
        signalFired = true;
        signalArgument = type;
    });

    // We're starting in drag mode, so no changed expected
    GlobalCommandSystem().executeCommand("ToggleTextureToolManipulatorMode", { "Drag" });
    EXPECT_FALSE(signalFired) << "Signal shouldn't have fired";
    signalFired = false;

    // Changing to Rotate should fire the signal
    GlobalCommandSystem().executeCommand("ToggleTextureToolManipulatorMode", { "Rotate" });
    EXPECT_TRUE(signalFired) << "Signal should have fired";
    EXPECT_EQ(signalArgument, selection::IManipulator::Rotate) << "Signal communicated wrong mode";
    signalFired = false;

    // Toggle Rotate, should switch back to Drag
    GlobalCommandSystem().executeCommand("ToggleTextureToolManipulatorMode", { "Rotate" });
    EXPECT_TRUE(signalFired) << "Signal should have fired";
    EXPECT_EQ(signalArgument, selection::IManipulator::Drag) << "Signal communicated wrong mode";
    signalFired = false;

    // Changing to Rotate (again) should fire the signal
    GlobalCommandSystem().executeCommand("ToggleTextureToolManipulatorMode", { "Rotate" });
    EXPECT_TRUE(signalFired) << "Signal should have fired";
    EXPECT_EQ(signalArgument, selection::IManipulator::Rotate) << "Signal communicated wrong mode";
    signalFired = false;

    // Directly toggle to Drag, should fire
    GlobalCommandSystem().executeCommand("ToggleTextureToolManipulatorMode", { "Drag" });
    EXPECT_TRUE(signalFired) << "Signal should have fired";
    EXPECT_EQ(signalArgument, selection::IManipulator::Drag) << "Signal communicated wrong mode";
    signalFired = false;

    conn.disconnect();
}

TEST_F(TextureToolTest, ToggleSelectionModeByCmd)
{
    bool signalFired = false;
    textool::SelectionMode signalArgument;

    // Subscribe to the changed signal
    sigc::connection conn = GlobalTextureToolSelectionSystem().signal_selectionModeChanged().connect(
        [&](textool::SelectionMode mode)
    {
        signalFired = true;
        signalArgument = mode;
    });

    // We're starting in Surface mode, toggle to Surface again
    GlobalCommandSystem().executeCommand("ToggleTextureToolSelectionMode", { "Surface" });
    EXPECT_EQ(GlobalTextureToolSelectionSystem().getSelectionMode(), textool::SelectionMode::Surface);
    EXPECT_FALSE(signalFired) << "Signal shouldn't have fired";
    signalFired = false;

    // Switch to vertex mode
    GlobalCommandSystem().executeCommand("ToggleTextureToolSelectionMode", { "Vertex" });
    EXPECT_EQ(GlobalTextureToolSelectionSystem().getSelectionMode(), textool::SelectionMode::Vertex);
    EXPECT_TRUE(signalFired) << "Signal should have fired";
    signalFired = false;

    // Toggle vertex mode again => back to surface mode
    GlobalCommandSystem().executeCommand("ToggleTextureToolSelectionMode", { "Vertex" });
    EXPECT_EQ(GlobalTextureToolSelectionSystem().getSelectionMode(), textool::SelectionMode::Surface);
    EXPECT_TRUE(signalFired) << "Signal should have fired";
    signalFired = false;

    // Switch to vertex mode (again)
    GlobalCommandSystem().executeCommand("ToggleTextureToolSelectionMode", { "Vertex" });
    EXPECT_EQ(GlobalTextureToolSelectionSystem().getSelectionMode(), textool::SelectionMode::Vertex);
    EXPECT_TRUE(signalFired) << "Signal should have fired";
    signalFired = false;

    // Directly toggle surface mode
    GlobalCommandSystem().executeCommand("ToggleTextureToolSelectionMode", { "Surface" });
    EXPECT_EQ(GlobalTextureToolSelectionSystem().getSelectionMode(), textool::SelectionMode::Surface);
    EXPECT_TRUE(signalFired) << "Signal should have fired";
    signalFired = false;
}

TEST_F(TextureToolTest, ToggleSelectionMode)
{
    bool signalFired = false;
    textool::SelectionMode signalArgument;

    // Subscribe to the changed signal
    sigc::connection conn = GlobalTextureToolSelectionSystem().signal_selectionModeChanged().connect(
        [&](textool::SelectionMode mode)
    {
        signalFired = true;
        signalArgument = mode;
    });

    // We're starting in Surface mode, toggle to Surface again
    GlobalTextureToolSelectionSystem().toggleSelectionMode(textool::SelectionMode::Surface);
    EXPECT_EQ(GlobalTextureToolSelectionSystem().getSelectionMode(), textool::SelectionMode::Surface);
    EXPECT_FALSE(signalFired) << "Signal shouldn't have fired";
    signalFired = false;

    // Switch to vertex mode
    GlobalTextureToolSelectionSystem().toggleSelectionMode(textool::SelectionMode::Vertex);
    EXPECT_EQ(GlobalTextureToolSelectionSystem().getSelectionMode(), textool::SelectionMode::Vertex);
    EXPECT_TRUE(signalFired) << "Signal should have fired";
    signalFired = false;

    // Toggle vertex mode again => back to surface mode
    GlobalTextureToolSelectionSystem().toggleSelectionMode(textool::SelectionMode::Vertex);
    EXPECT_EQ(GlobalTextureToolSelectionSystem().getSelectionMode(), textool::SelectionMode::Surface);
    EXPECT_TRUE(signalFired) << "Signal should have fired";
    signalFired = false;

    // Switch to vertex mode (again)
    GlobalTextureToolSelectionSystem().toggleSelectionMode(textool::SelectionMode::Vertex);
    EXPECT_EQ(GlobalTextureToolSelectionSystem().getSelectionMode(), textool::SelectionMode::Vertex);
    EXPECT_TRUE(signalFired) << "Signal should have fired";
    signalFired = false;

    // Directly toggle surface mode
    GlobalTextureToolSelectionSystem().toggleSelectionMode(textool::SelectionMode::Surface);
    EXPECT_EQ(GlobalTextureToolSelectionSystem().getSelectionMode(), textool::SelectionMode::Surface);
    EXPECT_TRUE(signalFired) << "Signal should have fired";
    signalFired = false;
}

TEST_F(TextureToolTest, SelectionModeChangedSignal)
{
    bool signalFired = false;
    textool::SelectionMode signalArgument;

    // Subscribe to the changed signal
    sigc::connection conn = GlobalTextureToolSelectionSystem().signal_selectionModeChanged().connect(
        [&] (textool::SelectionMode mode)
        {
            signalFired = true;
            signalArgument = mode;
        });

    // We're starting in Surface mode, so no changed expected
    GlobalTextureToolSelectionSystem().setSelectionMode(textool::SelectionMode::Surface);
    EXPECT_FALSE(signalFired) << "Signal shouldn't have fired";
    signalFired = false;

    GlobalTextureToolSelectionSystem().setSelectionMode(textool::SelectionMode::Vertex);
    EXPECT_TRUE(signalFired) << "Signal should have fired";
    EXPECT_EQ(signalArgument, textool::SelectionMode::Vertex) << "Signal communicated wrong mode";
    signalFired = false;

    // Switch to the same mode again => no signal expected
    GlobalTextureToolSelectionSystem().setSelectionMode(textool::SelectionMode::Vertex);
    EXPECT_FALSE(signalFired) << "Signal shouldn't have fired";
    signalFired = false;

    // Back to surface mode
    GlobalTextureToolSelectionSystem().setSelectionMode(textool::SelectionMode::Surface);
    EXPECT_TRUE(signalFired) << "Signal should have fired";
    EXPECT_EQ(signalArgument, textool::SelectionMode::Surface) << "Signal communicated wrong mode";
    signalFired = false;

    conn.disconnect();
}

void performPointSelection(const Vector2& texcoord, const render::View& view, selection::SelectionSystem::EModifier mode = selection::SelectionSystem::eToggle)
{
    auto texcoordTransformed = view.GetViewProjection().transformPoint(Vector3(texcoord.x(), texcoord.y(), 0));
    Vector2 devicePoint(texcoordTransformed.x(), texcoordTransformed.y());

    // Use the device point we calculated for this vertex and use it to construct a selection test
    render::View scissored(view);
    ConstructSelectionTest(scissored, selection::Rectangle::ConstructFromPoint(devicePoint, Vector2(0.02f, 0.02f)));

    SelectionVolume test(scissored);
    GlobalTextureToolSelectionSystem().selectPoint(test, mode);
}

TEST_F(TextureToolTest, TestSelectPatchSurfaceByPoint)
{
    auto patchNode = setupPatchNodeForTextureTool();
    auto patch = Node_getIPatch(patchNode);

    // Get the texture space bounds of this patch
    auto bounds = algorithm::getTextureSpaceBounds(*patch);

    // Construct a view that includes the patch UV bounds
    bounds.extents *= 1.2f;

    render::TextureToolView view;
    view.constructFromTextureSpaceBounds(bounds, TEXTOOL_WIDTH, TEXTOOL_HEIGHT);

    SelectionChangedCatcher signalObserver;

    // Test-select in the middle of the patch bounds
    performPointSelection(Vector2(bounds.origin.x(), bounds.origin.y()), view);

    EXPECT_TRUE(signalObserver.signalHasFired()) << "No selection changed signal emitted";

    // Check if the node was selected
    auto selectedNodes = getAllSelectedTextoolNodes();
    EXPECT_EQ(selectedNodes.size(), 1) << "Only one patch should be selected";
    EXPECT_TRUE(std::dynamic_pointer_cast<textool::IPatchNode>(selectedNodes.front())) << "Couldn't cast to special type";
}

inline int getIndexOfSingleSelectedNode(const std::vector<textool::INode::Ptr>& nodes)
{
    int selectedIndex = -1;

    for (int i = 0; i < nodes.size(); ++i)
    {
        if (nodes[i]->isSelected())
        {
            EXPECT_EQ(selectedIndex, -1) << "More than one node selected";
            selectedIndex = i;
        }
    }

    return selectedIndex;
}

TEST_F(TextureToolTest, CycleSelectPatchSurface)
{
    std::vector<scene::INodePtr> patchNodes =
    {
        setupPatchNodeForTextureTool(),
        setupPatchNodeForTextureTool(),
        setupPatchNodeForTextureTool(),
    };

    // Get the texture space bounds of a single patch (the others are the same)
    auto bounds = algorithm::getTextureSpaceBounds(*Node_getIPatch(patchNodes[0]));
    bounds.extents *= 1.2f;

    render::TextureToolView view;
    view.constructFromTextureSpaceBounds(bounds, TEXTOOL_WIDTH, TEXTOOL_HEIGHT);

    auto allNodes = getAllTextureToolNodes();
    EXPECT_EQ(allNodes.size(), 3) << "We should have 3 nodes in the scene";

    // Test-select in the middle of the patch bounds
    performPointSelection(Vector2(bounds.origin.x(), bounds.origin.y()), view, selection::SelectionSystem::eReplace);

    auto curSelectedIndex = getIndexOfSingleSelectedNode(allNodes);
    EXPECT_NE(curSelectedIndex, -1) << "A single node should be selected";
    std::set<int> allVisitedIndices;

    allVisitedIndices.insert(curSelectedIndex);

    // Cycle through the selection (twice)
    for (int i = 0; i < allNodes.size()*2; ++i)
    {
        // Test-select in the middle of the patch bounds
        performPointSelection(Vector2(bounds.origin.x(), bounds.origin.y()), view, selection::SelectionSystem::eCycle);

        auto newIndex = getIndexOfSingleSelectedNode(allNodes);
        EXPECT_NE(newIndex, -1) << "A single node should be selected";

        EXPECT_NE(newIndex, curSelectedIndex) << "The selected node should have been switched to another one";

        allVisitedIndices.insert(newIndex);
        curSelectedIndex = newIndex;
    }

    EXPECT_EQ(allVisitedIndices.size(), allNodes.size()) << "All nodes should have been selected after cycling through them";
}

TEST_F(TextureToolTest, TestSelectPatchVertexByPoint)
{
    auto patchNode = setupPatchNodeForTextureTool();
    auto patch = Node_getIPatch(patchNode);

    // Get the texture space bounds of this patch
    auto bounds = algorithm::getTextureSpaceBounds(*patch);

    // Construct a view that includes the patch UV bounds
    bounds.extents *= 1.2f;

    render::TextureToolView view;
    view.constructFromTextureSpaceBounds(bounds, TEXTOOL_WIDTH, TEXTOOL_HEIGHT);

    // Switch to vertex selection mode
    GlobalTextureToolSelectionSystem().setSelectionMode(textool::SelectionMode::Vertex);

    // Get the texcoords of the first vertex
    auto firstVertex = patch->ctrlAt(2, 1).texcoord;
    auto secondVertex = patch->ctrlAt(2, 0).texcoord;

    SelectionChangedCatcher signalObserver;

    // Selecting something in the middle of two vertices should not do anything
    performPointSelection((firstVertex + secondVertex) / 2, view);
    EXPECT_TRUE(getAllSelectedComponentNodes().empty()) << "Test-selecting a patch in between vertices should not have succeeded";
    EXPECT_FALSE(signalObserver.signalHasFired()) << "Selection Changed Signal shouldn't have fired";
    signalObserver.reset();

    performPointSelection(firstVertex, view);

    // Hitting a vertex will select the patch componentselectable
    EXPECT_EQ(getAllSelectedComponentNodes().size(), 1) << "Only one patch should be selected";
    EXPECT_TRUE(signalObserver.signalHasFired()) << "Selection Changed Signal should have fired";
    signalObserver.reset();

    // Hitting another vertex should not de-select the componentselectable
    performPointSelection(secondVertex, view);
    EXPECT_EQ(getAllSelectedComponentNodes().size(), 1) << "Only one patch should still be selected";
    EXPECT_TRUE(signalObserver.signalHasFired()) << "Selection Changed Signal should have fired";
    signalObserver.reset();

    // De-selecting the first and the second vertex should release the patch
    performPointSelection(secondVertex, view);
    EXPECT_EQ(getAllSelectedComponentNodes().size(), 1) << "Only one patch should still be selected";
    EXPECT_TRUE(signalObserver.signalHasFired()) << "Selection Changed Signal should have fired";
    signalObserver.reset();

    performPointSelection(firstVertex, view);
    EXPECT_TRUE(getAllSelectedComponentNodes().empty()) << "Selection should be empty now";
    EXPECT_TRUE(signalObserver.signalHasFired()) << "Selection Changed Signal should have fired";
    signalObserver.reset();
}

TEST_F(TextureToolTest, TestSelectFaceSurfaceByPoint)
{
    auto brush = setupBrushNodeForTextureTool();
    auto faceUp = algorithm::findBrushFaceWithNormal(Node_getIBrush(brush), Vector3(0, 0, 1));

    // Check the face
    auto textoolFace = findTexToolFaceWithNormal(faceUp->getPlane3().normal());
    EXPECT_FALSE(textoolFace->isSelected()) << "Face should be unselected at start";

    // Get the texture space bounds of this face
    // Construct a view that includes the face UV bounds
    auto bounds = algorithm::getTextureSpaceBounds(*faceUp);
    bounds.extents *= 1.2f;

    render::TextureToolView view;
    view.constructFromTextureSpaceBounds(bounds, TEXTOOL_WIDTH, TEXTOOL_HEIGHT);

    SelectionChangedCatcher signalObserver;

    // Point-select in the middle of the face
    performPointSelection(algorithm::getFaceCentroid(faceUp), view);

    EXPECT_TRUE(signalObserver.signalHasFired()) << "Selection Changed Signal should have fired";

    // Check if the node was selected
    auto selectedNodes = getAllSelectedTextoolNodes();
    EXPECT_EQ(selectedNodes.size(), 1) << "Only one item should be selected";
    EXPECT_EQ(selectedNodes.front(), textoolFace) << "The face should be selected";
    EXPECT_TRUE(std::dynamic_pointer_cast<textool::IFaceNode>(selectedNodes.front())) << "Couldn't cast to special type";
}

TEST_F(TextureToolTest, TestSelectFaceVertexByPoint)
{
    auto brush = setupBrushNodeForTextureTool();
    auto faceUp = algorithm::findBrushFaceWithNormal(Node_getIBrush(brush), Vector3(0, 0, 1));

    // Get the texture space bounds of this face
    auto bounds = algorithm::getTextureSpaceBounds(*faceUp);

    // Construct a view that includes the patch UV bounds
    bounds.extents *= 1.2f;

    render::TextureToolView view;
    view.constructFromTextureSpaceBounds(bounds, TEXTOOL_WIDTH, TEXTOOL_HEIGHT);

    // Switch to vertex selection mode
    GlobalTextureToolSelectionSystem().setSelectionMode(textool::SelectionMode::Vertex);

    SelectionChangedCatcher signalObserver;

    // Get the texcoords of the first vertex
    auto firstVertex = faceUp->getWinding()[0].texcoord;
    auto secondVertex = faceUp->getWinding()[1].texcoord;

    // Selecting something in the middle of two vertices should not do anything
    performPointSelection((firstVertex + secondVertex) / 2, view);
    EXPECT_TRUE(getAllSelectedComponentNodes().empty()) << "Test-selecting a face in between vertices should not have succeeded";
    EXPECT_FALSE(signalObserver.signalHasFired()) << "Selection Changed Signal shouldn't have fired";
    signalObserver.reset();

    // Hitting a vertex will select the face
    performPointSelection(firstVertex, view);
    EXPECT_EQ(getAllSelectedComponentNodes().size(), 1) << "Only one face should be selected";
    EXPECT_TRUE(signalObserver.signalHasFired()) << "Selection Changed Signal should have fired";
    signalObserver.reset();

    // Hitting another vertex should not de-select the face
    performPointSelection(secondVertex, view);
    EXPECT_EQ(getAllSelectedComponentNodes().size(), 1) << "Only one face should still be selected";
    EXPECT_TRUE(signalObserver.signalHasFired()) << "Selection Changed Signal should have fired";
    signalObserver.reset();

    // De-selecting the first and the second vertex should release the face
    performPointSelection(secondVertex, view);
    EXPECT_EQ(getAllSelectedComponentNodes().size(), 1) << "Only one face should still be selected";
    EXPECT_TRUE(signalObserver.signalHasFired()) << "Selection Changed Signal should have fired";
    signalObserver.reset();

    performPointSelection(firstVertex, view);
    EXPECT_TRUE(getAllSelectedComponentNodes().empty()) << "Selection should be empty now";
    EXPECT_TRUE(signalObserver.signalHasFired()) << "Selection Changed Signal should have fired";
    signalObserver.reset();
}

TEST_F(TextureToolTest, TestSelectPatchByArea)
{
    auto patchNode = setupPatchNodeForTextureTool();
    auto patch = Node_getIPatch(patchNode);

    // Get the texture space bounds of this patch
    auto bounds = algorithm::getTextureSpaceBounds(*patch);

    // Construct a view that includes the patch UV bounds
    bounds.extents *= 1.2f;

    render::TextureToolView view;
    view.constructFromTextureSpaceBounds(bounds, TEXTOOL_WIDTH, TEXTOOL_HEIGHT);

    // Use the device point we calculated for this vertex and use it to construct a selection test
    ConstructSelectionTest(view, selection::Rectangle::ConstructFromArea(Vector2(-0.95f, -0.95f), Vector2(0.95f*2, 0.95f*2)));

    SelectionVolume test(view);
    SelectionChangedCatcher signalObserver;

    GlobalTextureToolSelectionSystem().selectArea(test, selection::SelectionSystem::eToggle);

    EXPECT_TRUE(signalObserver.signalHasFired()) << "Selection Changed Signal should have fired";

    // Check if the node was selected
    auto selectedNodes = getAllSelectedTextoolNodes();
    EXPECT_EQ(selectedNodes.size(), 1) << "Only one patch should be selected";
    EXPECT_TRUE(std::dynamic_pointer_cast<textool::IPatchNode>(selectedNodes.front())) << "Couldn't cast to special type";
}

TEST_F(TextureToolTest, ClearSelectionUsingCommand)
{
    auto worldspawn = GlobalMapModule().findOrInsertWorldspawn();
    auto brush1 = algorithm::createCubicBrush(worldspawn, Vector3(0, 0, 0), "textures/numbers/1");
    auto brush2 = algorithm::createCubicBrush(worldspawn, Vector3(0, 256, 256), "textures/numbers/1");
    auto patchNode = setupPatchNodeForTextureTool();
    Node_getIPatch(patchNode)->setShader("textures/numbers/1");

    Node_setSelected(brush1, true);
    Node_setSelected(brush2, true);
    EXPECT_EQ(GlobalSelectionSystem().countSelected(), 3) << "3 items must be selected";

    // We don't know how many tex tool nodes there are, but it should be more than 0
    EXPECT_GT(getTextureToolNodeCount(), 0) << "There should be some tex tool nodes now";

    std::set<textool::INode::Ptr> selectedNodes;
    std::size_t i = 0;

    // Select every single node
    GlobalTextureToolSceneGraph().foreachNode([&](const textool::INode::Ptr& node)
    {
        node->setSelected(true);
        selectedNodes.emplace(node);
        return true;
    });

    // We should have a non-empty selection
    EXPECT_GT(GlobalTextureToolSelectionSystem().countSelected(), 0) << "No nodes selected";

    // Switch to vertex mode
    GlobalTextureToolSelectionSystem().setSelectionMode(textool::SelectionMode::Vertex);

    // Get the texture space bounds of this patch
    render::TextureToolView view;
    auto bounds = algorithm::getTextureSpaceBounds(*Node_getIPatch(patchNode));
    bounds.extents *= 1.2f;
    view.constructFromTextureSpaceBounds(bounds, TEXTOOL_WIDTH, TEXTOOL_HEIGHT);

    // Select patch vertices
    algorithm::foreachPatchVertex(*Node_getIPatch(patchNode), [&](const PatchControl& control)
    {
        performPointSelection(control.texcoord, view);
    });

    // Select face vertices
    auto faceUp = algorithm::findBrushFaceWithNormal(Node_getIBrush(brush1), Vector3(0, 0, 1));

    // Get the texture space bounds of this face
    bounds = algorithm::getTextureSpaceBounds(*faceUp);
    bounds.extents *= 1.2f;
    view.constructFromTextureSpaceBounds(bounds, TEXTOOL_WIDTH, TEXTOOL_HEIGHT);

    for (const auto& vertex : faceUp->getWinding())
    {
        performPointSelection(vertex.texcoord, view);
    }

    // We should have two selected component nodes
    EXPECT_GT(GlobalTextureToolSelectionSystem().countSelectedComponentNodes(), 0) << "No components selected";
    EXPECT_GT(GlobalSelectionSystem().countSelected(), 0) << "Scene selection count should be > 0";

    SelectionChangedCatcher signalObserver;

    // Hitting ESC once will deselect the components
    GlobalCommandSystem().executeCommand("UnSelectSelection");

    EXPECT_EQ(GlobalTextureToolSelectionSystem().countSelectedComponentNodes(), 0) << "Component selection should be gone";
    EXPECT_GT(GlobalTextureToolSelectionSystem().countSelected(), 0) << "Surface selection should not have been touched";
    EXPECT_GT(GlobalSelectionSystem().countSelected(), 0) << "Scene selection count should still be > 0";
    EXPECT_EQ(GlobalTextureToolSelectionSystem().getSelectionMode(), textool::SelectionMode::Vertex) << "We should still be in vertex mode";
    EXPECT_TRUE(signalObserver.signalHasFired()) << "Selection Changed Signal should have fired";
    signalObserver.reset();

    // Next deselection will exit vertex mode
    GlobalCommandSystem().executeCommand("UnSelectSelection");
    EXPECT_EQ(GlobalTextureToolSelectionSystem().getSelectionMode(), textool::SelectionMode::Surface) << "We should be in Surface mode now";
    EXPECT_GT(GlobalTextureToolSelectionSystem().countSelected(), 0) << "Surface selection should not have been touched";
    EXPECT_GT(GlobalSelectionSystem().countSelected(), 0) << "Scene selection count should still be > 0";
    EXPECT_FALSE(signalObserver.signalHasFired()) << "Selection Changed Signal shouldn't have fired";
    signalObserver.reset();

    // Next will de-select the regular selection
    GlobalCommandSystem().executeCommand("UnSelectSelection");
    EXPECT_EQ(GlobalTextureToolSelectionSystem().countSelected(), 0) << "Surface selection should be gone now";
    EXPECT_GT(GlobalSelectionSystem().countSelected(), 0) << "Scene selection count should still be > 0";
    EXPECT_TRUE(signalObserver.signalHasFired()) << "Selection Changed Signal should have fired";
    signalObserver.reset();

    // Now that the tex tool selection is gone, we should affect the scene selection
    GlobalCommandSystem().executeCommand("UnSelectSelection");
    EXPECT_EQ(GlobalSelectionSystem().countSelected(), 0) << "Scene selection should be gone now";
    EXPECT_FALSE(signalObserver.signalHasFired()) << "Selection Changed Signal shouldn't have fired";
}

TEST_F(TextureToolTest, ClearSelection)
{
    auto worldspawn = GlobalMapModule().findOrInsertWorldspawn();
    auto brush1 = algorithm::createCubicBrush(worldspawn, Vector3(0, 0, 0), "textures/numbers/1");
    auto brush2 = algorithm::createCubicBrush(worldspawn, Vector3(0, 256, 256), "textures/numbers/1");
    auto patchNode = setupPatchNodeForTextureTool();
    Node_getIPatch(patchNode)->setShader("textures/numbers/1");

    Node_setSelected(brush1, true);
    Node_setSelected(brush2, true);
    EXPECT_EQ(GlobalSelectionSystem().countSelected(), 3) << "3 scene nodes must be selected";

    std::set<textool::INode::Ptr> selectedNodes;
    std::size_t i = 0;

    // Select every single node
    GlobalTextureToolSceneGraph().foreachNode([&](const textool::INode::Ptr& node)
    {
        node->setSelected(true);
        selectedNodes.emplace(node);
        return true;
    });

    // We should have a non-empty selection
    EXPECT_GT(GlobalTextureToolSelectionSystem().countSelected(), 0) << "No nodes selected";

    SelectionChangedCatcher signalObserver;

    // Deselect
    GlobalTextureToolSelectionSystem().clearSelection();

    EXPECT_TRUE(signalObserver.signalHasFired()) << "Selection Changed Signal should have fired";

    EXPECT_EQ(GlobalTextureToolSelectionSystem().countSelected(), 0) << "Surface selection should be gone now";
    EXPECT_EQ(GlobalSelectionSystem().countSelected(), 3) << "3 scene nodes must be selected";

    for (const auto& node : selectedNodes)
    {
        EXPECT_FALSE(node->isSelected()) << "Node should have been deselected";
    }
}

TEST_F(TextureToolTest, ClearComponentSelection)
{
    auto patchNode = setupPatchNodeForTextureTool();
    EXPECT_EQ(GlobalSelectionSystem().countSelected(), 1) << "1 scene node must be selected";

    // Switch to vertex mode
    GlobalTextureToolSelectionSystem().setSelectionMode(textool::SelectionMode::Vertex);

    // Get the texture space bounds of this patch
    render::TextureToolView view;
    auto bounds = algorithm::getTextureSpaceBounds(*Node_getIPatch(patchNode));
    bounds.extents *= 1.2f;
    view.constructFromTextureSpaceBounds(bounds, TEXTOOL_WIDTH, TEXTOOL_HEIGHT);

    // Select patch vertices
    algorithm::foreachPatchVertex(*Node_getIPatch(patchNode), [&](const PatchControl& control)
    {
        performPointSelection(control.texcoord, view);
    });

    EXPECT_EQ(GlobalTextureToolSelectionSystem().countSelectedComponentNodes(), 1) << "We should have 1 selected component node";

    SelectionChangedCatcher signalObserver;

    // Deselect all components
    GlobalTextureToolSelectionSystem().clearComponentSelection();

    EXPECT_TRUE(signalObserver.signalHasFired()) << "Selection Changed Signal should have fired";
    EXPECT_EQ(GlobalTextureToolSelectionSystem().countSelectedComponentNodes(), 0) << "Component selection should be gone now";
    EXPECT_EQ(GlobalTextureToolSelectionSystem().getSelectionMode(), textool::SelectionMode::Vertex) << "Should still be in vertex mode";
}

inline std::vector<Vector2> getTexcoords(const IFace* face)
{
    std::vector<Vector2> uvs;

    for (const auto& vertex : face->getWinding())
    {
        uvs.push_back(vertex.texcoord);
    }

    return uvs;
}

void dragManipulateSelectionTowardsLowerRight(const Vector2& startTexcoord, const render::View& view, bool cancelInsteadOfFinish = false)
{
    auto centroid = startTexcoord;
    auto centroidTransformed = view.GetViewProjection().transformPoint(Vector3(centroid.x(), centroid.y(), 0));
    Vector2 devicePoint(centroidTransformed.x(), centroidTransformed.y());

    GlobalTextureToolSelectionSystem().onManipulationStart();

    // Simulate a transformation by click-and-drag
    auto manipulator = GlobalTextureToolSelectionSystem().getActiveManipulator();
    EXPECT_EQ(manipulator->getType(), selection::IManipulator::Drag) << "Wrong manipulator";

    render::View scissored(view);
    ConstructSelectionTest(scissored, selection::Rectangle::ConstructFromPoint(devicePoint, Vector2(0.05, 0.05)));

    SelectionVolume test(scissored);
    manipulator->testSelect(test, GlobalTextureToolSelectionSystem().getPivot2World());

    auto manipComponent = manipulator->getActiveComponent();
    auto pivot2World = GlobalTextureToolSelectionSystem().getPivot2World();
    manipComponent->beginTransformation(pivot2World, scissored, devicePoint);

    // Move the device point a bit to the lower right
    auto secondDevicePoint = devicePoint + (Vector2(1, -1) - devicePoint) / 2;

    render::View scissored2(view);
    ConstructSelectionTest(scissored2, selection::Rectangle::ConstructFromPoint(secondDevicePoint, Vector2(0.05, 0.05)));

    manipComponent->transform(pivot2World, scissored2, secondDevicePoint, selection::IManipulator::Component::Constraint::Unconstrained);

    if (!cancelInsteadOfFinish)
    {
        GlobalTextureToolSelectionSystem().onManipulationFinished();
    }
    else
    {
        GlobalTextureToolSelectionSystem().onManipulationCancelled();
    }
}

TEST_F(TextureToolTest, DragManipulateFace)
{
    auto brush = setupBrushNodeForTextureTool();
    auto faceUp = algorithm::findBrushFaceWithNormal(Node_getIBrush(brush), Vector3(0, 0, 1));
    auto faceDown = algorithm::findBrushFaceWithNormal(Node_getIBrush(brush), Vector3(0, 0, -1));

    // Remember the texcoords of this face
    auto oldFaceUpUvs = getTexcoords(faceUp);
    auto oldFaceDownUvs = getTexcoords(faceDown);

    // Select the face
    auto textoolFace = findTexToolFaceWithNormal(faceUp->getPlane3().normal());
    textoolFace->setSelected(true);

    // Get the texture space bounds of this face
    // Construct a view that includes the patch UV bounds
    auto bounds = algorithm::getTextureSpaceBounds(*faceUp);
    bounds.extents *= 1.2f;

    render::TextureToolView view;
    view.constructFromTextureSpaceBounds(bounds, TEXTOOL_WIDTH, TEXTOOL_HEIGHT);

    // Check the device coords of the face centroid and manipulate from that point
    auto centroid = algorithm::getFaceCentroid(faceUp);
    dragManipulateSelectionTowardsLowerRight(centroid, view);

    // All the texcoords should have been moved to the lower right (U increased, V increased)
    auto oldUv = oldFaceUpUvs.begin();
    for (const auto& vertex : faceUp->getWinding())
    {
        EXPECT_LT(oldUv->x(), vertex.texcoord.x());
        EXPECT_LT(oldUv->y(), vertex.texcoord.y());
        ++oldUv;
    }

    // The texcoords of the other face should not have been changed
    oldUv = oldFaceDownUvs.begin();
    for (const auto& vertex : faceDown->getWinding())
    {
        EXPECT_EQ(oldUv->x(), vertex.texcoord.x());
        EXPECT_EQ(oldUv->y(), vertex.texcoord.y());
        ++oldUv;
    }
}

TEST_F(TextureToolTest, DragResizeFace)
{
    auto brush = setupBrushNodeForTextureTool();
    auto faceUp = algorithm::findBrushFaceWithNormal(Node_getIBrush(brush), Vector3(0, 0, 1));
    auto faceDown = algorithm::findBrushFaceWithNormal(Node_getIBrush(brush), Vector3(0, 0, -1));

    // Remember the texcoords of this face
    auto oldFaceUpUvs = getTexcoords(faceUp);
    auto oldFaceDownUvs = getTexcoords(faceDown);

    // Select the face
    auto textoolFace = findTexToolFaceWithNormal(faceUp->getPlane3().normal());
    textoolFace->setSelected(true);

    // Get the texture space bounds of this face
    auto oldBounds = algorithm::getTextureSpaceBounds(*faceUp);

    // Construct a view that includes the UV bounds
    auto bounds = oldBounds;
    bounds.extents *= 1.5f;

    render::TextureToolView view;
    view.constructFromTextureSpaceBounds(bounds, TEXTOOL_WIDTH, TEXTOOL_HEIGHT);

    // Check the device coords a point outside the face bounds
    auto startPoint = Vector2(oldBounds.getOrigin().x(), oldBounds.getOrigin().y()) + Vector2(oldBounds.getExtents().x() * 1.15, 0);
    dragManipulateSelectionTowardsLowerRight(startPoint, view);

    // The UV extents of that brush must have been increased
    auto newBounds = algorithm::getTextureSpaceBounds(*faceUp);

    EXPECT_GT(newBounds.getExtents().x(), oldBounds.getExtents().x()) << "Brush UV bounds X should have been increased";
    EXPECT_EQ(newBounds.getExtents().y(), oldBounds.getExtents().y()) << "Brush UV bounds Y should have stayed the same";

    // The texcoords of the other face should not have been changed
    auto oldUv = oldFaceDownUvs.begin();
    for (const auto& vertex : faceDown->getWinding())
    {
        EXPECT_EQ(oldUv->x(), vertex.texcoord.x());
        EXPECT_EQ(oldUv->y(), vertex.texcoord.y());
        ++oldUv;
    }
}

void performPatchManipulationTest(bool cancelOperation)
{
    auto patchNode = setupPatchNodeForTextureTool();
    auto patch = Node_getIPatch(patchNode);

    // Remember the texcoords before manipulation
    std::vector<Vector2> oldTexcoords;
    algorithm::foreachPatchVertex(*patch, [&](const PatchControl& control) { oldTexcoords.push_back(control.texcoord); });

    auto texToolPatch = getFirstTextureToolNode();
    texToolPatch->setSelected(true);

    // Get the texture space bounds of this patch
    auto bounds = algorithm::getTextureSpaceBounds(*patch);
    bounds.extents *= 1.2f;

    render::TextureToolView view;
    view.constructFromTextureSpaceBounds(bounds, TEXTOOL_WIDTH, TEXTOOL_HEIGHT);

    // Check the device coords of a point in the middle of the patch
    auto centroid = Vector2(bounds.origin.x(), bounds.origin.y());
    dragManipulateSelectionTowardsLowerRight(centroid, view, cancelOperation); // optionally cancel

    std::vector<Vector2> changedTexcoords;
    algorithm::foreachPatchVertex(*patch, [&](const PatchControl& control) { changedTexcoords.push_back(control.texcoord); });

    if (!cancelOperation)
    {
        // All the texcoords should have been moved to the lower right (U increased, V increased)
        for (auto i = 0; i < oldTexcoords.size(); ++i)
        {
            EXPECT_LT(oldTexcoords[i].x(), changedTexcoords[i].x());
            EXPECT_LT(oldTexcoords[i].y(), changedTexcoords[i].y());
        }
    }
    else
    {
        // All texcoords should remain unchanged
        for (auto i = 0; i < oldTexcoords.size(); ++i)
        {
            // should be unchanged
            EXPECT_NEAR(oldTexcoords[i].x(), changedTexcoords[i].x(), 0.01);
            EXPECT_NEAR(oldTexcoords[i].y(), changedTexcoords[i].y(), 0.01);
        }
    }
}

TEST_F(TextureToolTest, DragManipulatePatch)
{
    performPatchManipulationTest(false); // don't cancel
}

TEST_F(TextureToolTest, CancelDragManipulationOfPatch)
{
    performPatchManipulationTest(true); // cancel
}

void performPatchVertexManipulationTest(bool cancelOperation)
{
    auto patchNode = setupPatchNodeForTextureTool();
    auto patch = Node_getIPatch(patchNode);

    // Remember the texcoords before manipulation
    std::vector<Vector2> oldTexcoords;
    algorithm::foreachPatchVertex(*patch, [&](const PatchControl& control) { oldTexcoords.push_back(control.texcoord); });

    // Get the texture space bounds of this patch
    auto bounds = algorithm::getTextureSpaceBounds(*patch);
    bounds.extents *= 1.2f;

    render::TextureToolView view;
    view.constructFromTextureSpaceBounds(bounds, TEXTOOL_WIDTH, TEXTOOL_HEIGHT);

    GlobalTextureToolSelectionSystem().setSelectionMode(textool::SelectionMode::Vertex);

    // Select every odd vertex
    for (auto i = 1; i < oldTexcoords.size(); i += 2)
    {
        performPointSelection(oldTexcoords[i], view);
    }

    EXPECT_EQ(getAllSelectedComponentNodes().size(), 1) << "No component node selected";

    // Drag-manipulate the first odd vertex
    dragManipulateSelectionTowardsLowerRight(oldTexcoords[1], view, cancelOperation); // optionally cancel the operation

    std::vector<Vector2> changedTexcoords;
    algorithm::foreachPatchVertex(*patch, [&](const PatchControl& control) { changedTexcoords.push_back(control.texcoord); });

    if (!cancelOperation)
    {
        // All odd texcoords should have been moved to the lower right (U increased, V increased)
        for (auto i = 0; i < oldTexcoords.size(); ++i)
        {
            if (i % 2 == 1)
            {
                EXPECT_LT(oldTexcoords[i].x(), changedTexcoords[i].x());
                EXPECT_LT(oldTexcoords[i].y(), changedTexcoords[i].y());
            }
            else
            {
                // should be unchanged
                EXPECT_NEAR(oldTexcoords[i].x(), changedTexcoords[i].x(), 0.01);
                EXPECT_NEAR(oldTexcoords[i].y(), changedTexcoords[i].y(), 0.01);
            }
        }
    }
    else // operation cancelled
    {
        // All texcoords should remain unchanged
        for (auto i = 0; i < oldTexcoords.size(); ++i)
        {
            // should be unchanged
            EXPECT_NEAR(oldTexcoords[i].x(), changedTexcoords[i].x(), 0.01);
            EXPECT_NEAR(oldTexcoords[i].y(), changedTexcoords[i].y(), 0.01);
        }
    }
}

TEST_F(TextureToolTest, DragManipulatePatchVertices)
{
    performPatchVertexManipulationTest(false); // don't cancel
}

TEST_F(TextureToolTest, CancelDragManipulationOfPatchVertices)
{
    performPatchVertexManipulationTest(true); // cancel
}

// When switching from Vertex to Surface mode, the pivot should be recalculated
TEST_F(TextureToolTest, PivotIsRecalculatedWhenSwitchingModes)
{
    auto patchNode = setupPatchNodeForTextureTool();
    auto patch = Node_getIPatch(patchNode);

    // Get the texture space bounds of this patch
    auto bounds = algorithm::getTextureSpaceBounds(*patch);

    // Construct a view that includes the patch UV bounds
    bounds.extents *= 1.2f;

    render::TextureToolView view;
    view.constructFromTextureSpaceBounds(bounds, TEXTOOL_WIDTH, TEXTOOL_HEIGHT);

    // Get the texcoords of the first vertex
    auto firstVertex = patch->ctrlAt(2, 1).texcoord;
    auto secondVertex = patch->ctrlAt(2, 0).texcoord;

    // Select the patch itself
    performPointSelection(secondVertex, view);

    // Check the manipulation pivot
    auto pivot2world = GlobalTextureToolSelectionSystem().getPivot2World();

    // The pivot should be near the center of the patch
    auto boundsOrigin = algorithm::getTextureSpaceBounds(*patch).origin;
    EXPECT_TRUE(math::isNear(pivot2world.tCol().getVector3(), boundsOrigin, 0.01)) <<
        "Pivot should be at the center of the patch";

    // Switch to vertex selection mode and select two vertices
    GlobalTextureToolSelectionSystem().setSelectionMode(textool::SelectionMode::Vertex);

    performPointSelection(firstVertex, view);
    EXPECT_EQ(getAllSelectedComponentNodes().size(), 1) << "Only one patch should still be selected";

    // Pivot should be right at the first vertex
    auto componentPivot2world = GlobalTextureToolSelectionSystem().getPivot2World();
    EXPECT_TRUE(math::isNear(componentPivot2world.tCol().getVector3(),
        Vector3(firstVertex.x(), firstVertex.y(), 0), 0.01)) << "Pivot should be at the single selected vertex";

    // Selecting a second point, the pivot should move to the middle of the two
    performPointSelection(secondVertex, view);
    EXPECT_EQ(getAllSelectedComponentNodes().size(), 1) << "Only one patch should still be selected";

    componentPivot2world = GlobalTextureToolSelectionSystem().getPivot2World();

    // The pivot should now be in the middle of the two selected vertices
    auto midPoint = (firstVertex + secondVertex) * 0.5;
    EXPECT_TRUE(math::isNear(componentPivot2world.tCol().getVector3(), 
        Vector3(midPoint.x(), midPoint.y(), 0), 0.01)) << "Pivot should be in between the two selected vertices";

    // Switching back to surface selection mode, the pivot needs to move to the bounds origin again
    GlobalTextureToolSelectionSystem().setSelectionMode(textool::SelectionMode::Surface);
    EXPECT_TRUE(math::isNear(pivot2world.tCol().getVector3(), boundsOrigin, 0.01)) <<
        "Pivot should be at the center of the patch after switching back to surface mode";
}

void performFaceVertexManipulationTest(bool cancelOperation, std::vector<std::size_t> vertexIndicesToManipulate,
    std::function<void(IFace&, const std::vector<Vector2>&, const std::vector<Vector2>&)> assertionFunc)
{
    auto brush = setupBrushNodeForTextureTool();
    auto faceUp = algorithm::findBrushFaceWithNormal(Node_getIBrush(brush), Vector3(0, 0, 1));

    // Remember the texcoords before manipulation
    std::vector<Vector2> oldTexcoords;
    for (const auto& vertex : faceUp->getWinding()) { oldTexcoords.push_back(vertex.texcoord); }

    // Get the texture space bounds of this face
    auto bounds = algorithm::getTextureSpaceBounds(*faceUp);
    bounds.extents *= 1.2f;

    render::TextureToolView view;
    view.constructFromTextureSpaceBounds(bounds, TEXTOOL_WIDTH, TEXTOOL_HEIGHT);

    GlobalTextureToolSelectionSystem().setSelectionMode(textool::SelectionMode::Vertex);

    // Select a certain number of vertices
    for (auto index : vertexIndicesToManipulate)
    {
        performPointSelection(oldTexcoords[index], view);
    }

    EXPECT_EQ(getAllSelectedComponentNodes().size(), 1) << "No component node selected";

    // Drag-manipulate the first odd vertex
    auto firstIndex = vertexIndicesToManipulate.front();
    dragManipulateSelectionTowardsLowerRight(oldTexcoords[firstIndex], view, cancelOperation); // optionally cancel the operation

    std::vector<Vector2> changedTexcoords;
    for (const auto& vertex : faceUp->getWinding()) { changedTexcoords.push_back(vertex.texcoord); }

    assertionFunc(*faceUp, oldTexcoords, changedTexcoords);
}

inline void assertAllCoordsUnchanged(IFace&, const std::vector<Vector2>& oldTexcoords, const std::vector<Vector2>& changedTexcoords)
{
    // All texcoords should remain unchanged
    for (auto i = 0; i < oldTexcoords.size(); ++i)
    {
        // should be unchanged
        EXPECT_NEAR(oldTexcoords[i].x(), changedTexcoords[i].x(), 0.01);
        EXPECT_NEAR(oldTexcoords[i].y(), changedTexcoords[i].y(), 0.01);
    }
}

inline void assertAllCoordsMovedBySameAmount(IFace&, const std::vector<Vector2>& oldTexcoords, const std::vector<Vector2>& changedTexcoords)
{
    // All manipulated vertices should have been changed by the same amount
    auto draggedDistanceOfFirst = (changedTexcoords[0] - oldTexcoords[0]).getLengthSquared();

    EXPECT_GT(draggedDistanceOfFirst, 0) << "Vertex 0 hasn't been moved at all";

    for (int i = 1; i < changedTexcoords.size(); ++i)
    {
        auto draggedDistance = (changedTexcoords[i] - oldTexcoords[i]).getLengthSquared();
        EXPECT_NEAR(draggedDistance, draggedDistanceOfFirst, 0.01)
            << "The vertex " << i << " should have been moved by the same amount as vertex 0";
    }
}

inline std::size_t getFarthestIndex(const std::vector<Vector2>& texcoords, const Vector2& point, std::vector<std::size_t> fixedIndices)
{
    std::size_t farthestIndex = 1;
    double largestDistance = 0;

    for (std::size_t i = 0; i < texcoords.size(); ++i)
    {
        if (std::find(fixedIndices.begin(), fixedIndices.end(), i) != fixedIndices.end()) continue;

        auto candidateDistance = (texcoords[i] - point).getLengthSquared();

        if (candidateDistance > largestDistance)
        {
            farthestIndex = i;
            largestDistance = candidateDistance;
        }
    }

    return farthestIndex;
}

// When manipulating one vertex, the "opposite" vertex should remain the same as it is chosen as fixed point
TEST_F(TextureToolTest, DragManipulateSingleFaceVertex)
{
    performFaceVertexManipulationTest(false, { 0 }, [](IFace& face, 
        const std::vector<Vector2>& oldTexcoords, const std::vector<Vector2>& changedTexcoords)
    {
        // Find out which face vertex was the farthest away from the modified one
        auto farthestIndex = getFarthestIndex(oldTexcoords, oldTexcoords[0], { 0 });

        // The farthest vertex should remain unchanged
        EXPECT_NEAR(oldTexcoords[farthestIndex].x(), changedTexcoords[farthestIndex].x(), 0.01) << "Opposite vertex X should remain unchanged";
        EXPECT_NEAR(oldTexcoords[farthestIndex].y(), changedTexcoords[farthestIndex].y(), 0.01) << "Opposite vertex Y should remain unchanged";

        // The algorithm will pick a third vertex that should remain unchanged
        // it will be the farthest from the center of the first two vertices
        auto center = (oldTexcoords[farthestIndex] + oldTexcoords[0]) * 0.5;
        auto thirdIndex = getFarthestIndex(oldTexcoords, center, { 0, farthestIndex });

        // All the others will have changed in some way
        for (int i = 0; i < oldTexcoords.size(); ++i)
        {
            if (i == farthestIndex || i == thirdIndex) continue;

            EXPECT_FALSE(float_equal_epsilon(oldTexcoords[i].x(), changedTexcoords[i].x(), 0.05)) << "Vertex " << i << " x should have changed";
            EXPECT_FALSE(float_equal_epsilon(oldTexcoords[i].y(), changedTexcoords[i].y(), 0.05)) << "Vertex " << i << " y should have changed";;
        }
    }); 
}

// Dragging two selected vertices chooses the one vertex as anchor point which is farthest away from the clicked vertex
TEST_F(TextureToolTest, DragManipulateTwoFaceVertices)
{
    // Vertices 0 and 2 are opposite of each other
    std::size_t firstVertex = 0;
    std::size_t secondVertex = 2;
    performFaceVertexManipulationTest(false, { firstVertex, secondVertex }, [&](IFace& face,
        const std::vector<Vector2>& oldTexcoords, const std::vector<Vector2>& changedTexcoords)
    {
        auto center = (changedTexcoords[firstVertex] + changedTexcoords[secondVertex]) * 0.5;
        // Find out which face vertex was the farthest away from the bounds center of the selection
        std::size_t farthestIndex = 0;
        double largestDistanceSquared = 0;

        for (std::size_t i = 0; i < changedTexcoords.size(); ++i)
        {
            if (i == firstVertex || i == secondVertex) continue;

            auto candidateDistanceSquared = (changedTexcoords[i] - center).getLengthSquared();

            if (candidateDistanceSquared > largestDistanceSquared)
            {
                farthestIndex = i;
                largestDistanceSquared = candidateDistanceSquared;
            }
        }

        // The farthest vertex should remain unchanged
        EXPECT_NEAR(oldTexcoords[farthestIndex].x(), changedTexcoords[farthestIndex].x(), 0.01) << "Opposite vertex X should remain unchanged";
        EXPECT_NEAR(oldTexcoords[farthestIndex].y(), changedTexcoords[farthestIndex].y(), 0.01) << "Opposite vertex Y should remain unchanged";

        // The two manipulated vertices should have been changed by the same amount
        auto draggedDistanceOfFirst = changedTexcoords[firstVertex] - oldTexcoords[firstVertex];
        auto draggedDistanceOfSecond = changedTexcoords[secondVertex] - oldTexcoords[secondVertex];

        EXPECT_NEAR(draggedDistanceOfFirst.getLengthSquared(), draggedDistanceOfSecond.getLengthSquared(), 0.01);

        // All the other non-fixed vertices will have changed in some way
        for (int i = 0; i < oldTexcoords.size(); ++i)
        {
            if (i == firstVertex || i == secondVertex || i == farthestIndex) continue;

            EXPECT_FALSE(float_equal_epsilon(oldTexcoords[i].x(), changedTexcoords[i].x(), 0.05)) << "Vertex " << i << " x should have changed";
            EXPECT_FALSE(float_equal_epsilon(oldTexcoords[i].y(), changedTexcoords[i].y(), 0.05)) << "Vertex " << i << " y should have changed";;
        }
    });
}

// Dragging three (or more) selected vertices should move all of the face vertices by the same amount
TEST_F(TextureToolTest, DragManipulateThreeFaceVertices)
{
    // Select three vertices
    performFaceVertexManipulationTest(false, { 0, 1, 2 }, assertAllCoordsMovedBySameAmount);
}

// Dragging three (or more) selected vertices should move all of the face vertices by the same amount
TEST_F(TextureToolTest, DragManipulateFourFaceVertices)
{
    // Select four vertices
    performFaceVertexManipulationTest(false, { 0, 1, 2, 3 }, assertAllCoordsMovedBySameAmount);
}

TEST_F(TextureToolTest, CancelDragManipulationOfFaceVertices)
{
    performFaceVertexManipulationTest(true, { 0 }, assertAllCoordsUnchanged); // cancel
}

TEST_F(TextureToolTest, SelectRelatedOfPatchVertex)
{
    auto patchNode = setupPatchNodeForTextureTool();
    auto patch = Node_getIPatch(patchNode);

    // Get the texture space bounds of this patch
    auto bounds = algorithm::getTextureSpaceBounds(*patch);
    bounds.extents *= 1.2f;

    render::TextureToolView view;
    view.constructFromTextureSpaceBounds(bounds, TEXTOOL_WIDTH, TEXTOOL_HEIGHT);

    // Switch to vertex selection mode
    GlobalTextureToolSelectionSystem().setSelectionMode(textool::SelectionMode::Vertex);

    // Get the texcoords of the first vertex
    auto firstVertex = patch->ctrlAt(2, 1).texcoord;
    performPointSelection(firstVertex, view);

    // Hitting a vertex will select the patch componentselectable
    EXPECT_EQ(getAllSelectedComponentNodes().size(), 1) << "Only one patch should be selected";
    
    textool::IComponentSelectable::Ptr componentSelectable;
    GlobalTextureToolSelectionSystem().foreachSelectedComponentNode([&](const textool::INode::Ptr& node)
    {
        componentSelectable = std::dynamic_pointer_cast<textool::IComponentSelectable>(node);
        return false;
    });

    EXPECT_EQ(componentSelectable->getNumSelectedComponents(), 1) << "1 vertex should be selected";

    // Execute "Select Related"
    GlobalCommandSystem().executeCommand("TexToolSelectRelated");

    EXPECT_EQ(getAllSelectedComponentNodes().size(), 1) << "Only one patch should be selected";
    EXPECT_EQ(componentSelectable->getNumSelectedComponents(), 9) << "all 3x3 vertices should be selected";
}

TEST_F(TextureToolTest, SelectRelatedOfFaceVertex)
{
    auto brush = setupBrushNodeForTextureTool();
    auto faceUp = algorithm::findBrushFaceWithNormal(Node_getIBrush(brush), Vector3(0, 0, 1));

    // Get the texture space bounds of this face
    auto bounds = algorithm::getTextureSpaceBounds(*faceUp);
    bounds.extents *= 1.2f;

    render::TextureToolView view;
    view.constructFromTextureSpaceBounds(bounds, TEXTOOL_WIDTH, TEXTOOL_HEIGHT);

    // Switch to vertex selection mode
    GlobalTextureToolSelectionSystem().setSelectionMode(textool::SelectionMode::Vertex);

    // Get the texcoords of the first vertex
    auto firstVertex = faceUp->getWinding()[0].texcoord;
    performPointSelection(firstVertex, view);

    // Hitting a vertex will select the face componentselectable
    EXPECT_EQ(getAllSelectedComponentNodes().size(), 1) << "Only one face should be selected";

    textool::IComponentSelectable::Ptr componentSelectable;
    GlobalTextureToolSelectionSystem().foreachSelectedComponentNode([&](const textool::INode::Ptr& node)
    {
        componentSelectable = std::dynamic_pointer_cast<textool::IComponentSelectable>(node);
        return false;
    });

    EXPECT_EQ(componentSelectable->getNumSelectedComponents(), 1) << "1 vertex should be selected";

    // Execute "Select Related"
    GlobalCommandSystem().executeCommand("TexToolSelectRelated");

    EXPECT_EQ(getAllSelectedComponentNodes().size(), 1) << "Only one face should be selected";
    EXPECT_EQ(componentSelectable->getNumSelectedComponents(), faceUp->getWinding().size()) << "All winding vertices should be selected";
}

TEST_F(TextureToolTest, SelectRelatedOfFaceNode)
{
    auto brush = setupBrushNodeForTextureTool();
    auto faceUp = algorithm::findBrushFaceWithNormal(Node_getIBrush(brush), Vector3(0, 0, 1));

    EXPECT_EQ(getAllSelectedTextoolNodes().size(), 0) << "No item should be selected";

    // Get the texture space bounds of this face
    // Construct a view that includes the face UV bounds
    auto bounds = algorithm::getTextureSpaceBounds(*faceUp);
    bounds.extents *= 1.2f;

    render::TextureToolView view;
    view.constructFromTextureSpaceBounds(bounds, TEXTOOL_WIDTH, TEXTOOL_HEIGHT);

    // Point-select in the middle of the face
    performPointSelection(algorithm::getFaceCentroid(faceUp), view);

    EXPECT_EQ(getAllSelectedTextoolNodes().size(), 1) << "Only one face should be selected";

    // Execute "Select Related"
    GlobalCommandSystem().executeCommand("TexToolSelectRelated");

    EXPECT_EQ(getAllSelectedTextoolNodes().size(), 6) << "All 6 faces should be selected";
}

inline void assumeFaceVerticesGridSnapped(const IFace& face, bool shouldBeSnapped)
{
    auto gridSize = GlobalGrid().getGridSize(grid::Space::Texture);

    for (const auto& vertex : face.getWinding())
    {
        if (shouldBeSnapped)
        {
            EXPECT_EQ(vertex.texcoord.x(), float_snapped(vertex.texcoord.x(), gridSize)) << "U should be grid-snapped";
            EXPECT_EQ(vertex.texcoord.y(), float_snapped(vertex.texcoord.y(), gridSize)) << "V should be grid-snapped";
        }
        else
        {
            EXPECT_NE(vertex.texcoord.x(), float_snapped(vertex.texcoord.x(), gridSize)) << "U should not be grid-snapped";
            EXPECT_NE(vertex.texcoord.y(), float_snapped(vertex.texcoord.y(), gridSize)) << "V should not be grid-snapped";
        }
    }
}

inline void assumePatchVerticesGridSnapped(const IPatch& patch, bool shouldBeSnapped)
{
    auto gridSize = GlobalGrid().getGridSize(grid::Space::Texture);

    for (std::size_t col = 0; col < patch.getWidth(); ++col)
    {
        for (std::size_t row = 0; row < patch.getHeight(); ++row)
        {
            const auto& texcoord = patch.ctrlAt(row, col).texcoord;

            if (shouldBeSnapped)
            {
                EXPECT_EQ(texcoord.x(), float_snapped(texcoord.x(), gridSize)) << "U should be grid-snapped";
                EXPECT_EQ(texcoord.y(), float_snapped(texcoord.y(), gridSize)) << "V should be grid-snapped";
            }
            else
            {
                EXPECT_NE(texcoord.x(), float_snapped(texcoord.x(), gridSize)) << "U should not be grid-snapped";
                EXPECT_NE(texcoord.y(), float_snapped(texcoord.y(), gridSize)) << "V should not be grid-snapped";
            }
        }
    }
}

TEST_F(TextureToolTest, SnapFaceToGrid)
{
    auto brush = setupBrushNodeForTextureTool();
    auto faceUp = algorithm::findBrushFaceWithNormal(Node_getIBrush(brush), Vector3(0, 0, 1));
    auto faceRight = algorithm::findBrushFaceWithNormal(Node_getIBrush(brush), Vector3(1, 0, 0));

    // Assume some non-grid snapped texcoords on both faces
    faceUp->fitTexture(1, 1);
    faceUp->shiftTexdef(0.133f, 0.111f);

    faceRight->fitTexture(1, 1);
    faceRight->shiftTexdef(0.133f, 0.111f);

    EXPECT_EQ(getAllSelectedTextoolNodes().size(), 0) << "No item should be selected";

    // Get the texture space bounds of this face
    // Construct a view that includes the face UV bounds
    auto bounds = algorithm::getTextureSpaceBounds(*faceUp);
    bounds.extents *= 1.2f;

    render::TextureToolView view;
    view.constructFromTextureSpaceBounds(bounds, TEXTOOL_WIDTH, TEXTOOL_HEIGHT);

    // Point-select in the middle of the face
    performPointSelection(algorithm::getFaceCentroid(faceUp), view);

    GlobalGrid().setGridSize(GRID_8);
    auto gridSize = GlobalGrid().getGridSize(grid::Space::Texture);

    assumeFaceVerticesGridSnapped(*faceUp, false);
    assumeFaceVerticesGridSnapped(*faceRight, false);

    EXPECT_EQ(getAllSelectedTextoolNodes().size(), 1) << "Only one face should be selected";

    // Snap selection to grid
    GlobalCommandSystem().executeCommand("TexToolSnapToGrid");

    // The selected face should be grid-snapped
    assumeFaceVerticesGridSnapped(*faceUp, true);
    // The second face should stay unchanged
    assumeFaceVerticesGridSnapped(*faceRight, false);
}

TEST_F(TextureToolTest, SnapFaceVerticesToGrid)
{
    auto brush = setupBrushNodeForTextureTool();
    auto faceUp = algorithm::findBrushFaceWithNormal(Node_getIBrush(brush), Vector3(0, 0, 1));

    faceUp->fitTexture(1, 1);
    faceUp->shiftTexdef(0.133f, 0.111f);

    // Get the texture space bounds of this face
    auto bounds = algorithm::getTextureSpaceBounds(*faceUp);
    bounds.extents *= 1.2f;

    render::TextureToolView view;
    view.constructFromTextureSpaceBounds(bounds, TEXTOOL_WIDTH, TEXTOOL_HEIGHT);

    // Switch to vertex selection mode
    GlobalTextureToolSelectionSystem().setSelectionMode(textool::SelectionMode::Vertex);

    // Get the texcoords of the first vertex
    auto firstIndex = 0;
    auto secondIndex = 1;

    auto firstVertex = faceUp->getWinding()[firstIndex].texcoord;
    auto secondVertex = faceUp->getWinding()[secondIndex].texcoord;
    performPointSelection(firstVertex, view);
    performPointSelection(secondVertex, view);

    // Hitting a vertex will select the face componentselectable
    EXPECT_EQ(getAllSelectedComponentNodes().size(), 1) << "Only one face should be selected";

    GlobalGrid().setGridSize(GRID_8);
    auto gridSize = GlobalGrid().getGridSize(grid::Space::Texture);

    // All vertices should be off-grid
    assumeFaceVerticesGridSnapped(*faceUp, false);

    // Snap selection to grid
    GlobalCommandSystem().executeCommand("TexToolSnapToGrid");

    for (auto i = 0; i < faceUp->getWinding().size(); ++i)
    {
        const auto& vertex = faceUp->getWinding()[i];

        // At least the the two selected ones should have been grid-aligned, the rest is not checked
        if (i == firstIndex || i == secondIndex)
        {
            EXPECT_EQ(vertex.texcoord.x(), float_snapped(vertex.texcoord.x(), gridSize)) << "U should be grid-snapped";
            EXPECT_EQ(vertex.texcoord.y(), float_snapped(vertex.texcoord.y(), gridSize)) << "V should be grid-snapped";
        }
    }
}

TEST_F(TextureToolTest, SnapPatchToGrid)
{
    auto patchNode = setupPatchNodeForTextureTool();
    auto patch = Node_getIPatch(patchNode);

    patch->fitTexture(1, 1);
    patch->translateTexture(13, 11);

    // Get the texture space bounds of this patch
    auto bounds = algorithm::getTextureSpaceBounds(*patch);
    bounds.extents *= 1.2f;

    render::TextureToolView view;
    view.constructFromTextureSpaceBounds(bounds, TEXTOOL_WIDTH, TEXTOOL_HEIGHT);

    // Test-select in the middle of the patch bounds
    performPointSelection(Vector2(bounds.origin.x(), bounds.origin.y()), view);

    // Patch should be selected
    EXPECT_EQ(getAllSelectedTextoolNodes().size(), 1) << "Only one patch should be selected";

    GlobalGrid().setGridSize(GRID_8);
    assumePatchVerticesGridSnapped(*patch, false);

    GlobalCommandSystem().executeCommand("TexToolSnapToGrid");

    assumePatchVerticesGridSnapped(*patch, true);
}

TEST_F(TextureToolTest, SnapPatchVerticesToGrid)
{
    auto patchNode = setupPatchNodeForTextureTool();
    auto patch = Node_getIPatch(patchNode);

    patch->fitTexture(1, 1);
    patch->translateTexture(0.133f, 0.111f);

    // Get the texture space bounds of this patch
    auto bounds = algorithm::getTextureSpaceBounds(*patch);
    bounds.extents *= 1.2f;

    render::TextureToolView view;
    view.constructFromTextureSpaceBounds(bounds, TEXTOOL_WIDTH, TEXTOOL_HEIGHT);

    // Switch to vertex mode
    GlobalTextureToolSelectionSystem().setSelectionMode(textool::SelectionMode::Vertex);

    // Get the texcoords of the first vertex
    auto definedRow = 2;
    auto definedCol = 1;

    auto firstVertex = patch->ctrlAt(definedRow, definedCol).texcoord;
    performPointSelection(firstVertex, view);

    GlobalGrid().setGridSize(GRID_8);
    assumePatchVerticesGridSnapped(*patch, false);

    GlobalCommandSystem().executeCommand("TexToolSnapToGrid");

    auto gridSize = GlobalGrid().getGridSize(grid::Space::Texture);

    // Check the patch tex coords, only the selected vertex should have been snapped
    for (std::size_t col = 0; col < patch->getWidth(); ++col)
    {
        for (std::size_t row = 0; row < patch->getHeight(); ++row)
        {
            const auto& texcoord = patch->ctrlAt(row, col).texcoord;

            if (row == definedRow && col == definedCol)
            {
                EXPECT_EQ(texcoord.x(), float_snapped(texcoord.x(), gridSize)) << "U should be grid-snapped";
                EXPECT_EQ(texcoord.y(), float_snapped(texcoord.y(), gridSize)) << "V should be grid-snapped";
            }
            else
            {
                EXPECT_NE(texcoord.x(), float_snapped(texcoord.x(), gridSize)) << "U should not be grid-snapped";
                EXPECT_NE(texcoord.y(), float_snapped(texcoord.y(), gridSize)) << "V should not be grid-snapped";
            }
        }
    }
}

void performPatchVertexMergeTest(bool useBoundsOrigin)
{
    auto patchNode1 = setupPatchNodeForTextureTool();
    auto patchNode2 = setupPatchNodeForTextureTool();
    auto patch1 = Node_getIPatch(patchNode1);
    auto patch2 = Node_getIPatch(patchNode2);

    patch1->fitTexture(1, 1);
    patch2->fitTexture(1, 1);
    patch2->translateTexture(20, -20);

    // Get the texture space bounds of both patches
    auto bounds = algorithm::getTextureSpaceBounds(*patch1);
    bounds.includeAABB(algorithm::getTextureSpaceBounds(*patch2));
    bounds.extents *= 1.2f;

    render::TextureToolView view;
    view.constructFromTextureSpaceBounds(bounds, TEXTOOL_WIDTH, TEXTOOL_HEIGHT);

    // Switch to vertex mode
    GlobalTextureToolSelectionSystem().setSelectionMode(textool::SelectionMode::Vertex);

    // Get the texcoords of the first vertex
    auto definedRow = 2;
    auto definedCol = 1;
    const auto& vertex1 = patch1->ctrlAt(definedRow, definedCol).texcoord;
    const auto& vertex2 = patch2->ctrlAt(definedRow, definedCol).texcoord;

    performPointSelection(vertex1, view);
    performPointSelection(vertex2, view);

    EXPECT_EQ(getAllSelectedComponentNodes().size(), 2) << "2 patches should be selected";
    EXPECT_NE(vertex1.x(), vertex2.x()) << "Vertex 1 is alredy merged with Vertex 2";
    EXPECT_NE(vertex1.y(), vertex2.y()) << "Vertex 1 is alredy merged with Vertex 2";

    // We expect the vertices to be at the center if merge items is called with no arguments
    auto center = (vertex1 + vertex2) * 0.5;

    if (useBoundsOrigin)
    {
        // When using bounds origin, call the command without argument
        GlobalCommandSystem().executeCommand("TexToolMergeItems");
    }
    else
    {
        // Use an arbitrary position that is somewhere near other than center
        center += Vector2(-0.7, -0.3);
        GlobalCommandSystem().executeCommand("TexToolMergeItems", { cmd::Argument(center) });
    }

    EXPECT_EQ(vertex1.x(), center.x()) << "Vertex 1 should be at center " << center;
    EXPECT_EQ(vertex1.y(), center.y()) << "Vertex 1 should be at center " << center;
    EXPECT_EQ(vertex2.x(), center.x()) << "Vertex 2 should be at center " << center;
    EXPECT_EQ(vertex2.y(), center.y()) << "Vertex 2 should be at center " << center;
}

TEST_F(TextureToolTest, MergePatchVertices)
{
    // Perform the test using the bounds origin
    performPatchVertexMergeTest(true);
}

TEST_F(TextureToolTest, MergePatchVerticesAtCoords)
{
    // Perform the test using some other point
    performPatchVertexMergeTest(false);
}

TEST_F(TextureToolTest, MergeFaceVertices)
{
    auto brush1 = setupBrushNodeForTextureTool();
    auto brush2 = setupBrushNodeForTextureTool();
    auto face1 = algorithm::findBrushFaceWithNormal(Node_getIBrush(brush1), Vector3(0, 0, 1));
    auto face2 = algorithm::findBrushFaceWithNormal(Node_getIBrush(brush2), Vector3(0, 0, 1));

    face1->fitTexture(1, 1);

    face2->fitTexture(1, 1);
    face2->shiftTexdef(0.2f, -0.2f);

    // Get the texture space bounds
    auto bounds = algorithm::getTextureSpaceBounds(*face1);
    bounds.includeAABB(algorithm::getTextureSpaceBounds(*face2));
    bounds.extents *= 1.2f;

    render::TextureToolView view;
    view.constructFromTextureSpaceBounds(bounds, TEXTOOL_WIDTH, TEXTOOL_HEIGHT);

    // Switch to vertex mode
    GlobalTextureToolSelectionSystem().setSelectionMode(textool::SelectionMode::Vertex);

    // Get the texcoords of the first vertex
    const auto& vertex1 = face1->getWinding()[0].texcoord;
    const auto& vertex2 = face2->getWinding()[0].texcoord;

    performPointSelection(vertex1, view);
    performPointSelection(vertex2, view);

    EXPECT_EQ(getAllSelectedComponentNodes().size(), 2) << "2 faces should be selected";
    EXPECT_NE(vertex1.x(), vertex2.x()) << "Vertex 1 is alredy merged with Vertex 2";
    EXPECT_NE(vertex1.y(), vertex2.y()) << "Vertex 1 is alredy merged with Vertex 2";

    // We expect the vertices to be at the center if merge items is called with no arguments
    auto center = (vertex1 + vertex2) * 0.5;

    GlobalCommandSystem().executeCommand("TexToolMergeItems");

    EXPECT_EQ(vertex1.x(), center.x()) << "Vertex 1 should be at center " << center;
    EXPECT_EQ(vertex1.y(), center.y()) << "Vertex 1 should be at center " << center;
    EXPECT_EQ(vertex2.x(), center.x()) << "Vertex 2 should be at center " << center;
    EXPECT_EQ(vertex2.y(), center.y()) << "Vertex 2 should be at center " << center;
}

// Two selected face vertices will refuse to be merged since that produces an unusable tex def
TEST_F(TextureToolTest, MergeTwoVerticesOfSameFace)
{
    auto brush1 = setupBrushNodeForTextureTool();
    auto brush2 = setupBrushNodeForTextureTool();
    auto face1 = algorithm::findBrushFaceWithNormal(Node_getIBrush(brush1), Vector3(0, 0, 1));
    auto face2 = algorithm::findBrushFaceWithNormal(Node_getIBrush(brush2), Vector3(0, 0, 1));

    face1->fitTexture(1, 1);

    face2->fitTexture(1, 1);
    face2->shiftTexdef(0.2f, -0.2f);

    // Get the texture space bounds
    auto bounds = algorithm::getTextureSpaceBounds(*face1);
    bounds.includeAABB(algorithm::getTextureSpaceBounds(*face2));
    bounds.extents *= 1.2f;

    render::TextureToolView view;
    view.constructFromTextureSpaceBounds(bounds, TEXTOOL_WIDTH, TEXTOOL_HEIGHT);

    // Switch to vertex mode
    GlobalTextureToolSelectionSystem().setSelectionMode(textool::SelectionMode::Vertex);

    // Get the texcoords of two vertices
    const auto& vertex1 = face1->getWinding()[0].texcoord;
    const auto& vertex2 = face2->getWinding()[0].texcoord;

    // The third vertex is from the first face
    const auto& vertex3 = face1->getWinding()[1].texcoord;

    performPointSelection(vertex1, view);
    performPointSelection(vertex2, view);
    performPointSelection(vertex3, view);

    EXPECT_EQ(getAllSelectedComponentNodes().size(), 2) << "2 faces should be selected";
    EXPECT_NE(vertex1.x(), vertex2.x()) << "Vertex 1 is alredy merged with Vertex 2";
    EXPECT_NE(vertex1.y(), vertex2.y()) << "Vertex 1 is alredy merged with Vertex 2";

    AABB selectionBounds;

    GlobalTextureToolSelectionSystem().foreachSelectedComponentNode([&](const textool::INode::Ptr& node)
    {
        auto componentSelectable = std::dynamic_pointer_cast<textool::IComponentSelectable>(node);
        if (!componentSelectable) return true;

        selectionBounds.includeAABB(componentSelectable->getSelectedComponentBounds());

        return true;
    });

    GlobalCommandSystem().executeCommand("TexToolMergeItems");

    // Only one face vertex should be at the center
    auto center = selectionBounds.origin;
    EXPECT_TRUE((vertex1.x() == center.x() && vertex1.y() == center.y()) || 
                (vertex1.x() == center.x() && vertex3.y() == center.y())) 
        << "Vertex 1 or 3 should be at center " << center;
}

void performPatchFlipTest(int axis)
{
    auto patchNode = setupPatchNodeForTextureTool();
    auto patch = Node_getIPatch(patchNode);

    // Get the texture space bounds of this patch
    auto patchBounds = algorithm::getTextureSpaceBounds(*patch);
    auto bounds = patchBounds;
    bounds.extents *= 1.2f;

    render::TextureToolView view;
    view.constructFromTextureSpaceBounds(bounds, TEXTOOL_WIDTH, TEXTOOL_HEIGHT);

    // Test-select in the middle of the patch bounds
    performPointSelection(Vector2(bounds.origin.x(), bounds.origin.y()), view);

    // Patch should be selected
    EXPECT_EQ(getAllSelectedTextoolNodes().size(), 1) << "Only one patch should be selected";

    std::vector<Vector2> oldTexCoords;
    algorithm::foreachPatchVertex(*patch, [&](const PatchControl& ctrl) { oldTexCoords.push_back(ctrl.texcoord); });

    auto cmd = axis == 0 ? "TexToolFlipS" : "TexToolFlipT";
    GlobalCommandSystem().executeCommand(cmd);

    // Every changed vertex should have been flipped about the bounds origin
    algorithm::expectVerticesHaveBeenFlipped(axis, *patch, oldTexCoords, { patchBounds.origin.x(), patchBounds.origin.y() });
}

TEST_F(TextureToolTest, FlipSinglePatchS)
{
    performPatchFlipTest(0);
}

TEST_F(TextureToolTest, FlipSinglePatchT)
{
    performPatchFlipTest(1);
}

void performFlipTestWithTwoPatches(int axis)
{
    auto patchNode1 = setupPatchNodeForTextureTool();
    auto patchNode2 = setupPatchNodeForTextureTool();
    auto patch1 = Node_getIPatch(patchNode1);
    auto patch2 = Node_getIPatch(patchNode2);

    patch1->fitTexture(1, 1);
    patch2->fitTexture(1, 1);
    patch2->translateTexture(45, -40);

    // Get the texture space bounds of both patches
    auto patchBounds1 = algorithm::getTextureSpaceBounds(*patch1);
    auto patchBounds2 = algorithm::getTextureSpaceBounds(*patch2);

    auto bounds = patchBounds1;
    bounds.includeAABB(patchBounds2);
    bounds.extents *= 1.2f;

    render::TextureToolView view;
    view.constructFromTextureSpaceBounds(bounds, TEXTOOL_WIDTH, TEXTOOL_HEIGHT);

    // Test-select in the middle of the patch bounds
    performPointSelection(Vector2(patchBounds1.origin.x(), patchBounds1.origin.y()), view);
    performPointSelection(Vector2(patchBounds2.origin.x(), patchBounds2.origin.y()), view);

    EXPECT_EQ(getAllSelectedTextoolNodes().size(), 2) << "Both patches should be selected";

    std::vector<Vector2> oldTexCoords1;
    algorithm::foreachPatchVertex(*patch1, [&](const PatchControl& ctrl) { oldTexCoords1.push_back(ctrl.texcoord); });
    std::vector<Vector2> oldTexCoords2;
    algorithm::foreachPatchVertex(*patch2, [&](const PatchControl& ctrl) { oldTexCoords2.push_back(ctrl.texcoord); });

    Vector2 flipCenter(bounds.origin.x(), bounds.origin.y());

    auto cmd = axis == 0 ? "TexToolFlipS" : "TexToolFlipT";
    GlobalCommandSystem().executeCommand(cmd);

    // Every changed vertex should have been flipped about the common bounds origin
    // Check patch 1 and 2
    algorithm::expectVerticesHaveBeenFlipped(axis, *patch1, oldTexCoords1, flipCenter);
    algorithm::expectVerticesHaveBeenFlipped(axis, *patch2, oldTexCoords2, flipCenter);
}

TEST_F(TextureToolTest, FlipTwoPatchesS)
{
    performFlipTestWithTwoPatches(0);
}

TEST_F(TextureToolTest, FlipTwoPatchesT)
{
    performFlipTestWithTwoPatches(1);
}

void performFaceFlipTest(int axis)
{
    auto brush = setupBrushNodeForTextureTool();
    auto faceUp = algorithm::findBrushFaceWithNormal(Node_getIBrush(brush), Vector3(0, 0, 1));

    // Get the texture space bounds of this face
    auto bounds = algorithm::getTextureSpaceBounds(*faceUp);
    bounds.extents *= 1.2f;

    render::TextureToolView view;
    view.constructFromTextureSpaceBounds(bounds, TEXTOOL_WIDTH, TEXTOOL_HEIGHT);

    // Test-select in the middle of the face
    performPointSelection(Vector2(bounds.origin.x(), bounds.origin.y()), view);

    // Face should be selected
    EXPECT_EQ(getAllSelectedTextoolNodes().size(), 1) << "Only one face should be selected";

    std::vector<Vector2> oldTexCoords;
    for (const auto& vertex : faceUp->getWinding())
    {
        oldTexCoords.push_back(vertex.texcoord);
    }

    auto cmd = axis == 0 ? "TexToolFlipS" : "TexToolFlipT";
    GlobalCommandSystem().executeCommand(cmd);

    // Every face vertex should have been flipped about the bounds origin
    auto old = oldTexCoords.begin();
    for (const auto& vertex : faceUp->getWinding())
    {
        // Calculate the mirrored coordinate
        auto expectedTexcoord = *(old++);
        expectedTexcoord[axis] = 2 * bounds.origin[axis] - expectedTexcoord[axis];

        EXPECT_EQ(vertex.texcoord.x(), expectedTexcoord.x()) << "Mirrored vertex should be at " << expectedTexcoord;
        EXPECT_EQ(vertex.texcoord.y(), expectedTexcoord.y()) << "Mirrored vertex should be at " << expectedTexcoord;
    }
}

TEST_F(TextureToolTest, FlipSingleFaceS)
{
    performFaceFlipTest(0);
}

TEST_F(TextureToolTest, FlipSingleFaceT)
{
    performFaceFlipTest(1);
}

TEST_F(TextureToolTest, RotateSelectedPreservesPatchTexelScale)
{
    auto material = "textures/a_1024x512";
    auto worldspawn = GlobalMapModule().findOrInsertWorldspawn();
    auto patchNode = algorithm::createPatchFromBounds(worldspawn, AABB(Vector3(4, 50, 60), Vector3(64, 128, 256)), "textures/a_1024x512");
    auto patch = Node_getIPatch(patchNode);
    patch->fitTexture(1, 1);
    Node_setSelected(patchNode, true);

    // The texture bounds should be 1x1, since the texture is fitted
    auto bounds = algorithm::getTextureSpaceBounds(*patch);

    EXPECT_NEAR(bounds.extents.x() * 2, 1, 0.01) << "Patch UV bounds should be 1x1 before rotating";
    EXPECT_NEAR(bounds.extents.y() * 2, 1, 0.01) << "Patch UV bounds should be 1x1 before rotating";

    render::TextureToolView view;
    view.constructFromTextureSpaceBounds(bounds, TEXTOOL_WIDTH, TEXTOOL_HEIGHT);

    // Test-select in the middle of the bounds
    performPointSelection(Vector2(bounds.origin.x(), bounds.origin.y()), view);

    // Item should be selected
    EXPECT_EQ(getAllSelectedTextoolNodes().size(), 1) << "Only one patch should be selected";

    GlobalCommandSystem().executeCommand("TexToolRotateSelected", cmd::Argument(45));
    GlobalCommandSystem().executeCommand("TexToolRotateSelected", cmd::Argument(45));

    auto boundsAfter = algorithm::getTextureSpaceBounds(*patch);

    EXPECT_NEAR(boundsAfter.extents.x() * 2, 0.5, 0.01) << "Patch UV bounds should be 0.5x2 after rotating";
    EXPECT_NEAR(boundsAfter.extents.y() * 2, 2, 0.01) << "Patch UV bounds should be 0.5x2 after rotating";
}

TEST_F(TextureToolTest, RotateSelectedPreservesFaceTexelScale)
{
    auto material = "textures/a_1024x512";
    auto worldspawn = GlobalMapModule().findOrInsertWorldspawn();
    auto test1024x512Node = algorithm::createCuboidBrush(worldspawn, AABB({ 128, 32, 16 }, { 128, 32, 16 }), material);
    algorithm::foreachFace(*Node_getIBrush(test1024x512Node), [](IFace& face) 
    {
        // Shift all unrelated faces
        if (math::isNear(face.getPlane3().normal(), { 0, 0, 1 }, 0.01)) return;

        face.fitTexture(1, 1);
        face.shiftTexdef(3, 3);
    });
    Node_getIBrush(test1024x512Node)->evaluateBRep();
    Node_setSelected(test1024x512Node, true);

    auto& face = *algorithm::findBrushFaceWithNormal(Node_getIBrush(test1024x512Node), { 0, 0, 1 });
    face.fitTexture(1, 1);

    auto bounds = algorithm::getTextureSpaceBounds(face);

    EXPECT_NEAR(bounds.extents.x() * 2, 1, 0.01) << "Face UV bounds should be 1x1 before rotating";
    EXPECT_NEAR(bounds.extents.y() * 2, 1, 0.01) << "Face UV bounds should be 1x1 before rotating";

    render::TextureToolView view;
    view.constructFromTextureSpaceBounds(bounds, TEXTOOL_WIDTH, TEXTOOL_HEIGHT);

    // Test-select in the middle of the bounds
    performPointSelection(Vector2(bounds.origin.x(), bounds.origin.y()), view);

    // Item should be selected
    EXPECT_EQ(getAllSelectedTextoolNodes().size(), 1) << "Only one face should be selected";

    GlobalCommandSystem().executeCommand("TexToolRotateSelected", cmd::Argument(45));
    GlobalCommandSystem().executeCommand("TexToolRotateSelected", cmd::Argument(45));

    Node_getIBrush(test1024x512Node)->evaluateBRep();
    auto boundsAfter = algorithm::getTextureSpaceBounds(face);

    EXPECT_NEAR(boundsAfter.extents.x() * 2, 0.5, 0.01) << "Face UV bounds should be 0.5x2 after rotating";
    EXPECT_NEAR(boundsAfter.extents.y() * 2, 2, 0.01) << "Face UV bounds should be 0.5x2 after rotating";
}

}