mln-rs-renderer 0.0.13

Part of mln-rs, a Rust port of the MapLibre Native map rendering core. Depend on mln-rs instead.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
// The C++ class hierarchy `Placement` <- `StaticPlacement` <- `TilePlacement`
// is one struct; `PlacementKind` holds the subclass and its data, and the
// virtual methods dispatch on it.
//
// C++ `PlacementContext` keeps references to the bucket, the render tile and
// the transform state; here it owns what it needs (the shared bucket, the
// render tiles' owner) so that TilePlacement can keep it in its
// intersection list. Symbol instances are addressed by their index in the
// bucket's `symbol_instances`, which live behind the bucket's state lock
// (symbol_bucket.rs): placement locks it while it places a bucket, and never
// calls a bucket method that locks it again while it holds it.
//
// `collisionCircles` is keyed by the C++ `const CollisionFeature*`: the
// feature's bucket instance id, symbol instance index and whether it is the
// text (or icon) feature.
//
// Not ported: the MLN_SYMBOL_GUARDS checks (`check`, `checkIndexes`,
// `forceFail`; always true without guards). `TilePlacement::locationCache`
// is unused in C++ and left out.

use std::cell::{Cell, RefCell};
use std::collections::BTreeSet;
use std::num::NonZeroU32;
use std::rc::Rc;
use std::sync::{Arc, OnceLock};
use std::time::Duration;

use geometry::Point;
use glam::Vec2;
use mln_map_types::{MapDebugOptions, MapMode, ProjectionData, TileProjector, TransformState};
use mln_math::{fmaxf, fminf};
use mln_style::TransitionOptions;
use mln_style::layers::symbol_layer_properties::SymbolLayoutProperties;
use mln_style_types::{
    AlignmentType, IconTextFitType, SymbolAnchorType, SymbolPlacementType, SymbolZOrderType, TextJustifyType,
    TextRotationAlignmentType, TextWritingModeType,
};
use mln_text::glyph::WritingModeType;
use mln_text::{AnchorAlignment, get_anchor_justification};
use mln_tile_id::{OverscaledTileID, UnwrappedTileID};
use mln_util::constants::{DEFAULT_TRANSITION_DURATION, EXTENT, ONE_EM, TILE_SIZE_D};
use mln_util::grid_index::BBox;
use mln_util::hash::HashMap;
use mln_util::mat4::Mat4;
use mln_util::time::Instant;
use mln_vec_map::VecMap;

use crate::bucket::Bucket;
use crate::buckets::symbol_bucket::{
    Buffer, CollisionBoxDynamicVertex, PlacedSymbol, PlacedSymbolPools, SortKeyRange, SymbolBucket, SymbolBucketState,
    ZoomEvaluatedSize, get_sorted_symbols,
};
use crate::geometry::feature_index::{FeatureIndex, IndexedSubfeature};
use crate::layout::symbol_instance::{SymbolInstance, SymbolInstancePools};
use crate::layout::symbol_projection::{
    LabelPlaneProjector, add_dynamic_attributes, evaluate_size_for_feature, get_tile_skew_vectors, hide_glyphs,
    reproject_line_labels,
};
use crate::render_layer::{BucketPlacementData, RenderLayer, RenderLayerPtr, SymbolTranslate};
use crate::render_tile::RenderTile;
use crate::text::collision_feature::{CollisionBox, CollisionFeature, ProjectedCollisionBox};
use crate::text::collision_index::{
    CollisionBoundaries, CollisionIndex, IntersectStatus, OverlapMode, PlacedFeatureResult,
};
use crate::update_parameters::UpdateParameters;

#[derive(Clone, Copy, Debug, PartialEq)]
pub(crate) struct OpacityState {
    pub(crate) opacity: f32,
    pub(crate) placed: bool,
}

impl OpacityState {
    #[must_use]
    pub(crate) fn new(placed: bool, skip_fade: bool) -> Self {
        OpacityState { opacity: if skip_fade && placed { 1.0 } else { 0.0 }, placed }
    }

    #[must_use]
    pub(crate) fn from_prev(prev_state: OpacityState, increment: f32, placed: bool) -> Self {
        OpacityState {
            opacity: fmaxf(
                0.0,
                fminf(1.0, prev_state.opacity + if prev_state.placed { increment } else { -increment }),
            ),
            placed,
        }
    }

    #[must_use]
    pub(crate) fn is_hidden(self) -> bool {
        self.opacity == 0.0 && !self.placed
    }
}

#[derive(Clone, Copy, Debug, PartialEq)]
pub(crate) struct JointOpacityState {
    pub(crate) icon: OpacityState,
    pub(crate) text: OpacityState,
}

impl JointOpacityState {
    #[must_use]
    pub(crate) fn new(placed_text: bool, placed_icon: bool, skip_fade: bool) -> Self {
        JointOpacityState {
            icon: OpacityState::new(placed_icon, skip_fade),
            text: OpacityState::new(placed_text, skip_fade),
        }
    }

    #[must_use]
    pub(crate) fn from_prev(
        prev_opacity_state: &JointOpacityState,
        increment: f32,
        placed_text: bool,
        placed_icon: bool,
    ) -> Self {
        JointOpacityState {
            icon: OpacityState::from_prev(prev_opacity_state.icon, increment, placed_icon),
            text: OpacityState::from_prev(prev_opacity_state.text, increment, placed_text),
        }
    }

    #[must_use]
    pub(crate) fn is_hidden(&self) -> bool {
        self.icon.is_hidden() && self.text.is_hidden()
    }
}

#[derive(Clone, Copy, Debug, PartialEq)]
pub(crate) struct VariableOffset {
    pub(crate) offset: [f32; 2],
    pub(crate) width: f32,
    pub(crate) height: f32,
    pub(crate) anchor: SymbolAnchorType,
    pub(crate) text_box_scale: f32,
    pub(crate) prev_anchor: Option<SymbolAnchorType>,
}

#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub(crate) struct JointPlacement {
    pub(crate) text: bool,
    pub(crate) icon: bool,
    /// skipFade = outside viewport, but within `CollisionIndex::viewportPadding`
    /// px of the edge Because these symbols aren't onscreen yet, we can skip
    /// the "fade in" animation, and if a subsequent viewport change brings
    /// them into view, they'll be fully visible right away.
    pub(crate) skip_fade: bool,
}

impl JointPlacement {
    #[must_use]
    pub(crate) fn new(text: bool, icon: bool, skip_fade: bool) -> Self {
        JointPlacement { text, icon, skip_fade }
    }

    #[must_use]
    pub(crate) fn placed(self) -> bool {
        self.text || self.icon
    }
}

#[derive(Debug)]
pub(crate) struct RetainedQueryData {
    pub(crate) bucket_instance_id: u32,
    pub(crate) feature_index: Option<Arc<FeatureIndex>>,
    pub(crate) tile_id: OverscaledTileID,
    pub(crate) feature_sort_order: RefCell<Option<Arc<Vec<usize>>>>,
}

impl RetainedQueryData {
    #[must_use]
    pub(crate) fn new(
        bucket_instance_id: u32,
        feature_index: Option<Arc<FeatureIndex>>,
        tile_id: OverscaledTileID,
    ) -> Self {
        RetainedQueryData { bucket_instance_id, feature_index, tile_id, feature_sort_order: RefCell::new(None) }
    }
}

pub(crate) type CollisionGroupPredicate = Rc<dyn Fn(&IndexedSubfeature) -> bool>;
pub(crate) type CollisionGroup = (u16, Option<CollisionGroupPredicate>);

pub(crate) struct CollisionGroups {
    collision_groups: VecMap<String, CollisionGroup>,
    max_group_id: u16,
    cross_source_collisions: bool,
}

impl CollisionGroups {
    #[must_use]
    pub(crate) fn new(cross_source_collisions: bool) -> Self {
        CollisionGroups { collision_groups: VecMap::new(), max_group_id: 0, cross_source_collisions }
    }

    pub(crate) fn get(&mut self, source_id: &str) -> CollisionGroup {
        // The predicate/groupID mechanism allows for arbitrary grouping,
        // but the current interface defines one source == one group when
        // crossSourceCollisions == true.
        if self.cross_source_collisions {
            (0, None)
        } else {
            if !self.collision_groups.contains_key(source_id) {
                self.max_group_id = self.max_group_id.wrapping_add(1);
                let next_group_id = self.max_group_id;
                let predicate: CollisionGroupPredicate =
                    Rc::new(move |feature: &IndexedSubfeature| feature.collision_group_id == next_group_id);
                self.collision_groups.insert(source_id.to_owned(), (next_group_id, Some(predicate)));
            }
            self.collision_groups[source_id].clone()
        }
    }
}

#[derive(Clone, Debug, PartialEq)]
pub struct PlacedSymbolData {
    /// Contents of the label
    pub key: Vec<u16>,
    /// If symbol contains text, text collision box in viewport coordinates
    pub text_collision_box: Option<BBox>,
    /// If symbol contains icon, icon collision box in viewport coordinates
    pub icon_collision_box: Option<BBox>,
    /// Symbol text was placed
    pub text_placed: bool,
    /// Symbol icon was placed
    pub icon_placed: bool,
    /// Symbol text or icon collision box intersects tile borders
    pub intersects_tile_border: bool,
    /// Viewport padding ({viewportPadding, viewportPadding} is a coordinate
    /// of the tile's top-left corner)
    pub viewport_padding: f32,
    /// Layer id (leader of the symbol layout group)
    pub layer: String,
}

/// The `SymbolBucket` of a placement data item.
fn symbol_bucket(bucket: &Arc<dyn Bucket>) -> Option<&SymbolBucket> {
    (**bucket).downcast_ref::<SymbolBucket>()
}

/// A symbol layer's `*-translate` in the tile's units, as a point.
fn translation_point(
    tile_id: &UnwrappedTileID,
    translate: &SymbolTranslate,
    state: &TransformState,
    screen_space: bool,
) -> Vec2 {
    // A screen-space symbol takes its translate in the screen matrix.
    if screen_space {
        return Vec2::ZERO;
    }
    let translation = RenderTile::tile_unit_translation(tile_id, translate.offset, translate.anchor, state);
    Vec2::new(translation[0], translation[1])
}

/// The projection without the horizon plane, so nothing on it counts as
/// hidden behind the planet.
fn without_horizon(mut data: ProjectionData) -> ProjectionData {
    data.clipping_plane = [0.0, 0.0, 0.0, 0.0];
    data
}

/// The C++ context's `tileProjector`, `textLabelPlane` and `iconLabelPlane`.
/// They borrow a transform state, and placing a symbol borrows the
/// placement (and its collision index's state) mutably, so the port builds
/// them over a copy of the state for each pass over a context's symbols
/// (`PlacementContext::projectors`) instead of keeping them in it.
pub(crate) struct PlacementProjectors<'a> {
    pub(crate) tile_projector: TileProjector<'a>,
    pub(crate) text_label_plane: LabelPlaneProjector<'a>,
    pub(crate) icon_label_plane: LabelPlaneProjector<'a>,
}

pub(crate) struct PlacementContext {
    bucket: Arc<dyn Bucket>,
    tile_owner: Rc<Vec<RenderTile>>,
    tile_index: usize,
    bearing: f64,

    pub(crate) pixels_to_tile_units: f32,
    pub(crate) scale: f32,
    pub(crate) pixel_ratio: f32,

    pub(crate) rotate_text_with_map: bool,
    pub(crate) pitch_text_with_map: bool,
    pub(crate) rotate_icon_with_map: bool,
    pub(crate) pitch_icon_with_map: bool,
    pub(crate) placement_type: SymbolPlacementType,

    /// The tile's projection for `tileProjector`. Screen-space symbols are
    /// drawn over the view, so the planet does not hide them: no horizon
    /// plane.
    tile_projection: ProjectionData,
    // Labels collide where `*-translate` puts them, line labels' circles
    // included.
    pub(crate) text_translation: Vec2,
    pub(crate) icon_translation: Vec2,

    pub(crate) collision_group: CollisionGroup,
    pub(crate) partially_evaluated_text_size: ZoomEvaluatedSize,
    pub(crate) partially_evaluated_icon_size: ZoomEvaluatedSize,

    pub(crate) text_overlap_mode: OverlapMode,
    pub(crate) icon_overlap_mode: OverlapMode,
    // This logic is similar to the "defaultOpacityState" logic below in
    // updateBucketOpacities If we know a symbol is always supposed to show,
    // force it to be marked visible even if it wasn't placed into the collision
    // index (because some or all of it was outside the range of the collision
    // grid). There is a subtle edge case here we're accepting:
    //  Symbol A has text-overlap: always, icon-overlap: always,
    //  icon-optional: false A's icon is outside the grid, so doesn't get placed
    //  A's text would be inside grid, but doesn't get placed because of
    //  icon-optional: false We still show A because of the overlap
    //  settings. Symbol B has overlap: never, and gets placed where A's
    //  text would be On panning in, there is a short period when Symbol B and
    //  Symbol A will overlap This is the reverse of our normal policy of "fade
    //  in on pan", but should look like any other collision and hopefully not
    //  be too noticeable.
    // See https://github.com/mapbox/mapbox-gl-native/issues/12683
    pub(crate) always_show_text: bool,
    pub(crate) always_show_icon: bool,

    pub(crate) has_icon_text_fit: bool,

    pub(crate) avoid_edges: Option<CollisionBoundaries>,
}

impl PlacementContext {
    /// The context for placing `bucket`'s symbols, with the translations from
    /// `params`. Locks the bucket's state briefly; call it without holding the
    /// lock.
    pub(crate) fn new(
        params: &BucketPlacementData,
        state: &TransformState,
        placement_zoom: f32,
        collision_group: CollisionGroup,
        avoid_edges: Option<CollisionBoundaries>,
    ) -> Self {
        let bucket = symbol_bucket(&params.bucket).expect("PlacementContext: a symbol bucket");
        let render_tile = params.tile();
        let overscaled_id = *render_tile.get_overscaled_tile_id();
        let pixels_to_tile_units = render_tile.id.pixels_to_tile_units(1.0, placement_zoom);
        // `std::pow(2, placementZoom - overscaledZ)`: a double pow.
        let scale = 2f64.powf(f64::from(placement_zoom - f32::from(overscaled_id.overscaled_z))) as f32;
        let pixel_ratio = (TILE_SIZE_D * f64::from(overscaled_id.overscale_factor()) / f64::from(EXTENT)) as f32;

        let layout = &*bucket.layout;
        let rotate_text_with_map = *layout.text_rotation_alignment.uniform() == TextRotationAlignmentType::Map;
        let pitch_text_with_map = *layout.text_pitch_alignment.uniform() == AlignmentType::Map;
        let rotate_icon_with_map = *layout.icon_rotation_alignment.uniform() == AlignmentType::Map;
        let pitch_icon_with_map = *layout.icon_pitch_alignment.uniform() == AlignmentType::Map;
        let placement_type = &layout.symbol_placement;

        let screen_space = &layout.symbol_screen_space;
        let text_translation =
            translation_point(&render_tile.id, &params.text_translate, state, *screen_space.uniform());
        let icon_translation =
            translation_point(&render_tile.id, &params.icon_translate, state, *screen_space.uniform());
        let tile_projection =
            if *screen_space.uniform() { without_horizon(render_tile.projection) } else { render_tile.projection };

        let partially_evaluated_text_size = bucket.text_size_binder.evaluate_for_zoom(placement_zoom);
        let partially_evaluated_icon_size = bucket.icon_size_binder.evaluate_for_zoom(placement_zoom);

        let text_overlap_mode = OverlapMode::text(layout);
        let icon_overlap_mode = OverlapMode::icon(layout);
        let text_always_overlap = text_overlap_mode == OverlapMode::Always;
        let icon_always_overlap = icon_overlap_mode == OverlapMode::Always;
        let always_show_text = text_always_overlap
            && (icon_always_overlap
                || !(bucket.has_icon_data() || bucket.has_sdf_icon_data())
                || *layout.icon_optional.uniform());
        let always_show_icon =
            icon_always_overlap && (text_always_overlap || !bucket.has_text_data() || *layout.text_optional.uniform());

        let has_icon_text_fit = *layout.icon_text_fit.uniform() != IconTextFitType::None;

        PlacementContext {
            bucket: Arc::clone(&params.bucket),
            tile_owner: Rc::clone(&params.tile_owner),
            tile_index: params.tile_index,
            bearing: state.get_bearing(),
            pixels_to_tile_units,
            scale,
            pixel_ratio,
            rotate_text_with_map,
            pitch_text_with_map,
            rotate_icon_with_map,
            pitch_icon_with_map,
            placement_type: *placement_type.uniform(),
            tile_projection,
            text_translation,
            icon_translation,
            collision_group,
            partially_evaluated_text_size,
            partially_evaluated_icon_size,
            text_overlap_mode,
            icon_overlap_mode,
            always_show_text,
            always_show_icon,
            has_icon_text_fit,
            avoid_edges,
        }
    }

    /// The context's projectors through `state`, the collision index's.
    pub(crate) fn projectors<'a>(&self, state: &'a TransformState) -> PlacementProjectors<'a> {
        let tile_projector = TileProjector::with_data(state, &self.get_render_tile().id, self.tile_projection);
        PlacementProjectors {
            text_label_plane: LabelPlaneProjector::new(
                &tile_projector,
                self.pitch_text_with_map,
                self.rotate_text_with_map,
                self.pixels_to_tile_units,
                self.text_translation,
            ),
            icon_label_plane: LabelPlaneProjector::new(
                &tile_projector,
                self.pitch_icon_with_map,
                self.rotate_icon_with_map,
                self.pixels_to_tile_units,
                self.icon_translation,
            ),
            tile_projector,
        }
    }

    #[must_use]
    pub(crate) fn get_bucket(&self) -> &SymbolBucket {
        symbol_bucket(&self.bucket).expect("PlacementContext: a symbol bucket")
    }

    #[must_use]
    pub(crate) fn get_layout(&self) -> &SymbolLayoutProperties::PossiblyEvaluated {
        &self.get_bucket().layout
    }

    #[must_use]
    pub(crate) fn get_render_tile(&self) -> &RenderTile {
        &self.tile_owner[self.tile_index]
    }

    #[must_use]
    pub(crate) fn get_overscaled_id(&self) -> &OverscaledTileID {
        self.get_render_tile().get_overscaled_tile_id()
    }

    fn collision_group_predicate(&self) -> Option<&dyn Fn(&IndexedSubfeature) -> bool> {
        self.collision_group.1.as_deref()
    }
}

pub(crate) struct PlacementController {
    placement: Placement,
    stale: bool,
}

impl Default for PlacementController {
    fn default() -> Self {
        PlacementController::new()
    }
}

impl PlacementController {
    #[must_use]
    pub(crate) fn new() -> Self {
        PlacementController { placement: Placement::new_default(), stale: false }
    }

    pub(crate) fn set_placement(&mut self, placement: Placement) {
        self.placement = placement;
        self.stale = false;
    }

    /// Takes the current placement, for the next one to place against,
    /// leaving the empty placement.
    pub(crate) fn take_placement(&mut self) -> Placement {
        std::mem::replace(&mut self.placement, Placement::new_default())
    }

    /// Resets to the empty placement; returns whether a committed placement
    /// was discarded.
    pub(crate) fn reset(&mut self) -> bool {
        let changed = self.placement.update_parameters.is_some();
        if changed {
            self.placement = Placement::new_default();
        }
        self.stale = false;
        changed
    }

    #[must_use]
    pub(crate) fn get_placement(&self) -> &Placement {
        &self.placement
    }

    pub(crate) fn set_placement_stale(&mut self) {
        self.stale = true;
    }

    #[must_use]
    pub(crate) fn placement_is_recent(
        &self,
        now: Option<Instant>,
        zoom: f32,
        period_override: Option<Duration>,
    ) -> bool {
        if !self.placement.transitions_enabled() {
            return false;
        }

        let update_period = match period_override {
            Some(period) => period,
            None => self.placement.get_update_period(zoom),
        };

        elapsed(now, self.placement.get_commit_time()) < update_period
    }

    #[must_use]
    pub(crate) fn has_transitions(&self, now: Option<Instant>) -> bool {
        if !self.placement.transitions_enabled() {
            return false;
        }

        if self.stale {
            return true;
        }

        self.placement.has_transitions(now)
    }
}

/// `Intersection` (`TilePlacement`).
struct Intersection {
    ctx: Rc<PlacementContext>,
    /// Index of the symbol instance in the context's bucket.
    symbol_index: usize,
    /// The symbol's anchor point, for sorting.
    anchor_point: Vec2,
    status: IntersectStatus,
    /// less means more important
    priority: usize,
}

/// The data members of `TilePlacement`.
#[derive(Default)]
struct TilePlacementState {
    tile_borders: Option<CollisionBoundaries>,
    seen_cross_tile_ids: BTreeSet<NonZeroU32>,
    placed_symbols_data: Vec<PlacedSymbolData>,
    intersections: Vec<Intersection>,
    populate_intersections: bool,
    current_intersection_priority: usize,
    collect_data: bool,
}

/// The subclass of a `Placement`.
enum PlacementKind {
    /// `Placement` (continuous map mode).
    Continuous,
    /// Static map mode.
    Static,
    /// Tile map mode.
    Tile(Box<TilePlacementState>),
}

/// Key of `collisionCircles` (C++: the `CollisionFeature*`).
type CollisionFeatureKey = (u32, usize, bool);

/// How long after `since` `now` is: zero if it is before, and
/// `Duration::MAX` without either time, as for a time long ago.
fn elapsed(now: Option<Instant>, since: Option<Instant>) -> Duration {
    match (now, since) {
        (Some(now), Some(since)) => now.saturating_duration_since(since),
        _ => Duration::MAX,
    }
}

/// `Placement`.
pub(crate) struct Placement {
    kind: PlacementKind,

    update_parameters: Option<Arc<UpdateParameters>>,
    collision_index: CollisionIndex,

    transition_options: TransitionOptions,

    /// When the symbols last began to fade; `None` for long ago.
    fade_start_time: Option<Instant>,
    /// The time of the update that placed the symbols; `None` for long ago.
    commit_time: Option<Instant>,
    placement_zoom: f32,
    prev_zoom_adjustment: f32,

    placements: HashMap<NonZeroU32, JointPlacement>,
    opacities: HashMap<NonZeroU32, JointOpacityState>,
    variable_offsets: HashMap<NonZeroU32, VariableOffset>,
    placed_orientations: HashMap<NonZeroU32, TextWritingModeType>,

    retained_query_data: HashMap<u32, RetainedQueryData>,
    collision_groups: CollisionGroups,
    /// The placement this one replaces, which it reads while it places and
    /// drops when it commits.
    prev_placement: Option<Box<Placement>>,
    /// The collision index of the placement this one replaced, which its
    /// commit no longer reads; the next placement reuses its grids.
    spare_collision_index: Option<CollisionIndex>,
    show_collision_boxes: bool,

    // Cache being used by placeSymbol()
    text_boxes: Vec<ProjectedCollisionBox>,
    icon_boxes: Vec<ProjectedCollisionBox>,
    // Used for debug purposes.
    collision_circles: HashMap<CollisionFeatureKey, Vec<ProjectedCollisionBox>>,
}

/// The `calculateVariableLayoutOffset(anchor, width, height, offset,
/// textBoxScale)` overload: the shift along the label's own axes; collision
/// projects it the way the label is aligned.
fn calculate_variable_layout_offset_unrotated(
    anchor: SymbolAnchorType,
    width: f32,
    height: f32,
    offset: [f32; 2],
    text_box_scale: f32,
) -> Vec2 {
    let alignment = AnchorAlignment::get_anchor_alignment(anchor);
    let shift_x = -(alignment.horizontal_align - 0.5) * width;
    let shift_y = -(alignment.vertical_align - 0.5) * height;
    Vec2::new(shift_x + offset[0] * text_box_scale, shift_y + offset[1] * text_box_scale)
}

/// The variable anchor's shift as a screen-space offset, for the boxes that
/// are not projected.
#[expect(
    clippy::too_many_arguments,
    reason = "the anchor, the box's size, offset and scale, and the alignment and bearing that turn it to screen space"
)]
fn calculate_variable_layout_offset(
    anchor: SymbolAnchorType,
    width: f32,
    height: f32,
    offset: [f32; 2],
    text_box_scale: f32,
    rotate_with_map: bool,
    pitch_with_map: bool,
    bearing: f32,
) -> Vec2 {
    let shift = calculate_variable_layout_offset_unrotated(anchor, width, height, offset, text_box_scale);
    if rotate_with_map {
        Vec2::from_angle(if pitch_with_map { bearing } else { -bearing }).rotate(shift)
    } else {
        shift
    }
}

fn calculate_variable_render_shift(
    anchor: SymbolAnchorType,
    width: f32,
    height: f32,
    text_offset: [f32; 2],
    text_box_scale: f32,
    render_text_size: f32,
) -> Vec2 {
    let alignment = AnchorAlignment::get_anchor_alignment(anchor);
    let shift_x = -(alignment.horizontal_align - 0.5) * width;
    let shift_y = -(alignment.vertical_align - 0.5) * height;
    Vec2::new(
        (shift_x / text_box_scale + text_offset[0]) * render_text_size,
        (shift_y / text_box_scale + text_offset[1]) * render_text_size,
    )
}

/// Returns indices into the bucket's symbol instances in `sort_key_range`, in
/// the order to place them.
fn get_bucket_symbols(
    bucket: &SymbolBucket,
    state: &SymbolBucketState,
    sort_key_range: Option<&SortKeyRange>,
    bearing: f64,
) -> Vec<usize> {
    if *bucket.layout.symbol_z_order.uniform() == SymbolZOrderType::ViewportY {
        let mut sorted_symbols = get_sorted_symbols(&state.symbol_instances, bearing as f32);
        // Place in the reverse order than draw i.e., starting from the foreground elements.
        sorted_symbols.reverse();
        return sorted_symbols;
    }
    bucket.get_symbols(sort_key_range, state.symbol_instances.len()).collect()
}

fn has_text_data(state: &SymbolBucketState) -> bool {
    !state.text.segments.is_empty()
}
fn has_icon_data(state: &SymbolBucketState) -> bool {
    !state.icon.segments.is_empty()
}
fn has_sdf_icon_data(state: &SymbolBucketState) -> bool {
    !state.sdf_icon.segments.is_empty()
}
fn has_icon_collision_box_data(state: &SymbolBucketState) -> bool {
    state.icon_collision_box.as_ref().is_some_and(|b| !b.segments.is_empty())
}
fn has_icon_collision_circle_data(state: &SymbolBucketState) -> bool {
    state.icon_collision_circle.as_ref().is_some_and(|b| !b.segments.is_empty())
}
fn has_text_collision_box_data(state: &SymbolBucketState) -> bool {
    state.text_collision_box.as_ref().is_some_and(|b| !b.segments.is_empty())
}
fn has_text_collision_circle_data(state: &SymbolBucketState) -> bool {
    state.text_collision_circle.as_ref().is_some_and(|b| !b.segments.is_empty())
}

fn justification_to_index(
    justify: TextJustifyType,
    symbol_instance: &SymbolInstance,
    orientation: TextWritingModeType,
) -> Option<usize> {
    // Vertical symbol has just one justification, style::TextJustifyType::Left.
    if orientation == TextWritingModeType::Vertical {
        return symbol_instance.placed_vertical_text_index;
    }

    match justify {
        TextJustifyType::Right => symbol_instance.placed_right_text_index,
        TextJustifyType::Center => symbol_instance.placed_center_text_index,
        TextJustifyType::Left => symbol_instance.placed_left_text_index,
        TextJustifyType::Auto => {
            debug_assert!(false);
            None
        }
    }
}

const JUSTIFY_TYPES: [TextJustifyType; 3] = [TextJustifyType::Right, TextJustifyType::Center, TextJustifyType::Left];

fn mark_used_justification(
    text: &mut Buffer,
    placed_anchor: SymbolAnchorType,
    symbol_instance: &SymbolInstance,
    orientation: TextWritingModeType,
) {
    let anchor_justify = get_anchor_justification(placed_anchor);
    debug_assert_ne!(anchor_justify, TextJustifyType::Auto);
    let auto_index = justification_to_index(anchor_justify, symbol_instance, orientation);

    for justify in JUSTIFY_TYPES {
        if let Some(index) = justification_to_index(justify, symbol_instance, orientation) {
            debug_assert!(text.placed_symbols.len() > index);
            if auto_index.is_some_and(|auto_index| index != auto_index) {
                // There are multiple justifications and this one isn't it: shift offscreen
                text.placed_symbols[index].cross_tile_id = None;
            } else {
                // Either this is the chosen justification or the justification is hardwired: use this one
                text.placed_symbols[index].cross_tile_id = symbol_instance.cross_tile_id();
            }
        }
    }
}

fn mark_used_orientation(
    text: &mut Buffer,
    icon: &mut Buffer,
    sdf_icon: &mut Buffer,
    orientation: TextWritingModeType,
    symbol_instance: &SymbolInstance,
) {
    let horizontal = if orientation == TextWritingModeType::Horizontal { Some(orientation) } else { None };
    let vertical = if orientation == TextWritingModeType::Vertical { Some(orientation) } else { None };

    if let Some(i) = symbol_instance.placed_right_text_index {
        text.placed_symbols[i].placed_orientation = horizontal;
    }

    if let Some(i) = symbol_instance.placed_center_text_index
        && !symbol_instance.get_single_line()
    {
        text.placed_symbols[i].placed_orientation = horizontal;
    }

    if let Some(i) = symbol_instance.placed_left_text_index
        && !symbol_instance.get_single_line()
    {
        text.placed_symbols[i].placed_orientation = horizontal;
    }

    if let Some(i) = symbol_instance.placed_vertical_text_index {
        text.placed_symbols[i].placed_orientation = vertical;
    }

    let icon_buffer = if symbol_instance.has_sdf_icon() { sdf_icon } else { icon };
    if let Some(i) = symbol_instance.placed_icon_index {
        icon_buffer.placed_symbols[i].placed_orientation = horizontal;
    }

    if let Some(i) = symbol_instance.placed_vertical_icon_index {
        icon_buffer.placed_symbols[i].placed_orientation = vertical;
    }
}

/// Appends `n` copies of `value` to `v`.
fn extend<T: Copy>(v: &mut Vec<T>, n: usize, value: T) {
    v.extend(std::iter::repeat_n(value, n));
}

/// The text placement attempts of `placeSymbol`
/// (`placeTextForPlacementModes`).
fn place_text_for_placement_modes(
    this: &mut Placement,
    bucket: &SymbolBucket,
    symbol_instance: &SymbolInstance,
    placed: &mut PlacedFeatureResult,
    placed_vertical_text: &mut PlacedFeatureResult,
    place_horizontal_fn: &mut dyn FnMut(&mut Placement) -> PlacedFeatureResult,
    place_vertical_fn: &mut dyn FnMut(&mut Placement, PlacedFeatureResult) -> PlacedFeatureResult,
) {
    if bucket.allow_vertical_placement
        && (symbol_instance.get_writing_modes() & WritingModeType::Vertical) != WritingModeType::None
    {
        debug_assert_ne!(bucket.placement_modes.as_slice(), []);
        for placement_mode in &bucket.placement_modes {
            if *placement_mode == TextWritingModeType::Vertical {
                *placed = place_vertical_fn(this, *placed);
                *placed_vertical_text = *placed;
            } else {
                *placed = place_horizontal_fn(this);
            }

            if placed.placed {
                break;
            }
        }
    } else {
        *placed = place_horizontal_fn(this);
    }
}

impl Placement {
    /// The initial placement of a `PlacementController`.
    #[must_use]
    pub(crate) fn new_default() -> Self {
        Placement {
            kind: PlacementKind::Continuous,
            update_parameters: None,
            collision_index: CollisionIndex::new(&TransformState::default(), MapMode::Static),
            transition_options: TransitionOptions::default(),
            fade_start_time: None,
            commit_time: None,
            placement_zoom: 0.0,
            prev_zoom_adjustment: 0.0,
            placements: HashMap::default(),
            opacities: HashMap::default(),
            variable_offsets: HashMap::default(),
            placed_orientations: HashMap::default(),
            retained_query_data: HashMap::default(),
            collision_groups: CollisionGroups::new(true),
            prev_placement: None,
            spare_collision_index: None,
            show_collision_boxes: false,
            text_boxes: Vec::new(),
            icon_boxes: Vec::new(),
            collision_circles: HashMap::default(),
        }
    }

    fn new(update_parameters: Arc<UpdateParameters>, prev_placement: Option<Placement>, kind: PlacementKind) -> Self {
        let mut prev_placement = prev_placement;
        let previous_collision_index = prev_placement.as_mut().and_then(|prev| prev.spare_collision_index.take());
        Placement {
            kind,
            collision_index: CollisionIndex::reusing(
                &update_parameters.transform_state,
                update_parameters.mode,
                previous_collision_index,
            ),
            transition_options: update_parameters.style.get_transition_options(),
            fade_start_time: None,
            commit_time: update_parameters.time_point,
            placement_zoom: update_parameters.transform_state.get_zoom() as f32,
            prev_zoom_adjustment: 0.0,
            placements: HashMap::default(),
            opacities: HashMap::default(),
            variable_offsets: HashMap::default(),
            placed_orientations: HashMap::default(),
            retained_query_data: HashMap::default(),
            collision_groups: CollisionGroups::new(update_parameters.cross_source_collisions),
            prev_placement: prev_placement.map(Box::new),
            spare_collision_index: None,
            show_collision_boxes: update_parameters.debug_options.contains(MapDebugOptions::COLLISION),
            text_boxes: Vec::new(),
            icon_boxes: Vec::new(),
            collision_circles: HashMap::default(),
            update_parameters: Some(update_parameters),
        }
    }

    /// A new placement, from the given update parameters and the previous
    /// placement. In Continuous map mode, `prev_placement` must be provided.
    pub(crate) fn create(update_parameters: Arc<UpdateParameters>, prev_placement: Option<Placement>) -> Placement {
        match update_parameters.mode {
            MapMode::Continuous => {
                debug_assert!(prev_placement.is_some());
                Placement::new(update_parameters, prev_placement, PlacementKind::Continuous)
            }
            MapMode::Static => Placement::new(update_parameters, None, PlacementKind::Static),
            MapMode::Tile => Placement::new(update_parameters, None, PlacementKind::Tile(Box::default())),
        }
    }

    fn get_prev_placement(&self) -> Option<&Placement> {
        self.prev_placement.as_deref()
    }

    fn is_tilted_view(&self) -> bool {
        self.update_parameters.as_ref().expect("updateParameters").transform_state.get_pitch() != 0.0
    }

    pub(crate) fn get_collision_index(&self) -> &CollisionIndex {
        &self.collision_index
    }

    pub(crate) fn get_commit_time(&self) -> Option<Instant> {
        self.commit_time
    }

    pub(crate) fn place_layers(&mut self, layers: &[RenderLayerPtr]) {
        if matches!(self.kind, PlacementKind::Tile(_)) {
            self.tile_place_layers(layers);
            return;
        }
        for layer in layers.iter().rev() {
            let mut seen_cross_tile_ids = BTreeSet::new();
            self.place_layer(&*layer.borrow(), &mut seen_cross_tile_ids);
        }
        self.commit();
    }

    fn place_layer(&mut self, layer: &dyn RenderLayer, seen_cross_tile_ids: &mut BTreeSet<NonZeroU32>) {
        for data in layer.get_placement_data() {
            place_bucket(&*data.bucket, self, data, seen_cross_tile_ids);
        }
    }

    /// The virtual `placeSymbolBucket(params, seenCrossTileIDs)`.
    fn place_symbol_bucket(&mut self, params: &BucketPlacementData, seen_cross_tile_ids: &mut BTreeSet<NonZeroU32>) {
        if matches!(self.kind, PlacementKind::Tile(_)) {
            self.tile_place_symbol_bucket(params, seen_cross_tile_ids);
        } else {
            self.base_place_symbol_bucket(params, seen_cross_tile_ids);
        }
    }

    fn base_place_symbol_bucket(
        &mut self,
        params: &BucketPlacementData,
        seen_cross_tile_ids: &mut BTreeSet<NonZeroU32>,
    ) {
        debug_assert!(self.update_parameters.is_some());
        let Some(symbol_bucket) = symbol_bucket(&params.bucket) else {
            return;
        };
        let render_tile = params.tile();
        let collision_group = self.collision_groups.get(&params.source_id);
        let avoid_edges = self.get_avoid_edges(symbol_bucket, &render_tile.matrix);
        let ctx = PlacementContext::new(
            params,
            self.collision_index.get_transform_state(),
            self.placement_zoom,
            collision_group,
            avoid_edges,
        );
        let transform_state = self.collision_index.get_transform_state().clone();
        let projectors = ctx.projectors(&transform_state);
        let mut state = symbol_bucket.state();
        for symbol_index in self.get_sorted_symbols(params, symbol_bucket, &state, ctx.pixel_ratio) {
            let symbol = &state.symbol_instances[symbol_index];
            if symbol.cross_tile_id().is_some_and(|id| seen_cross_tile_ids.contains(&id)) {
                continue;
            }
            self.place_symbol(symbol_index, symbol, &state, &ctx, &projectors);

            // Prevent a flickering issue while zooming out.
            if let Some(id) = symbol.cross_tile_id()
                && !ctx.get_render_tile().hold_for_fade()
            {
                seen_cross_tile_ids.insert(id);
            }
        }

        // Prevent a flickering issue when a symbol is moved.
        state.just_reloaded = false;
        drop(state);

        // As long as this placement lives, we have to hold onto this bucket's
        // matching FeatureIndex/data for querying purposes
        self.retained_query_data.entry(symbol_bucket.bucket_instance_id).or_insert_with(|| {
            RetainedQueryData::new(
                symbol_bucket.bucket_instance_id,
                params.feature_index.clone(),
                *ctx.get_overscaled_id(),
            )
        });
    }

    /// Places a symbol instance; `projectors` are the context's (see
    /// `PlacementProjectors`).
    fn place_symbol(
        &mut self,
        symbol_index: usize,
        symbol_instance: &SymbolInstance,
        state: &SymbolBucketState,
        ctx: &PlacementContext,
        projectors: &PlacementProjectors<'_>,
    ) -> JointPlacement {
        let k_unplaced = JointPlacement::new(false, false, false);
        let Some(cross_tile_id) = symbol_instance.cross_tile_id() else {
            return k_unplaced;
        };

        if ctx.get_render_tile().hold_for_fade() {
            // Mark all symbols from this tile as "not placed", but don't add to
            // seenCrossTileIDs, because we don't know yet if we have a duplicate in
            // a parent tile that _should_ be placed.
            return k_unplaced;
        }
        let bucket = ctx.get_bucket();
        let pos_matrix = ctx.get_render_tile().matrix;
        let tile_projector = &projectors.tile_projector;
        let mut variable_text_anchors = symbol_instance.get_text_anchors();
        self.text_boxes.clear();
        self.icon_boxes.clear();

        let mut place_text = false;
        let mut place_icon = false;
        let mut offscreen = true;
        let mut icon_occluded = false;
        let mut placed = PlacedFeatureResult::default();
        let mut placed_vertical_text = PlacedFeatureResult::default();
        let mut placed_vertical_icon = PlacedFeatureResult::default();
        let shift: Cell<Vec2> = Cell::new(Vec2::ZERO);
        if let Some(horizontal_text_index) = symbol_instance.get_default_horizontal_placed_text_index() {
            let placed_symbol: &PlacedSymbol = &state.text.placed_symbols[horizontal_text_index];
            let font_size = evaluate_size_for_feature(&ctx.partially_evaluated_text_size, placed_symbol);

            let update_previous_orientation_if_not_placed = |this: &mut Placement, is_placed: bool| {
                if bucket.allow_vertical_placement
                    && !is_placed
                    && let Some(prev_orientation) =
                        this.get_prev_placement().and_then(|prev| prev.placed_orientations.get(&cross_tile_id)).copied()
                {
                    this.placed_orientations.insert(cross_tile_id, prev_orientation);
                }
            };

            // Line or point label placement
            if variable_text_anchors.is_empty() {
                let place_feature =
                    |this: &mut Placement, collision_feature: &CollisionFeature, orientation: TextWritingModeType| {
                        this.text_boxes.clear();
                        let placed_feature = this.collision_index.place_feature(
                            collision_feature,
                            &state.symbol_instance_pools,
                            None,
                            ctx.text_translation,
                            tile_projector,
                            &projectors.text_label_plane,
                            ctx.pixel_ratio,
                            placed_symbol,
                            &state.text.pools,
                            ctx.scale,
                            font_size,
                            ctx.text_overlap_mode,
                            ctx.pitch_text_with_map,
                            ctx.rotate_text_with_map,
                            this.show_collision_boxes,
                            ctx.avoid_edges.as_ref(),
                            ctx.collision_group_predicate(),
                            &mut this.text_boxes,
                        );
                        if placed_feature.placed {
                            this.placed_orientations.entry(cross_tile_id).or_insert(orientation);
                        }
                        placed_feature
                    };

                let mut place_horizontal = |this: &mut Placement| {
                    place_feature(this, symbol_instance.get_text_collision_feature(), TextWritingModeType::Horizontal)
                };

                let mut place_vertical = |this: &mut Placement, _placed: PlacedFeatureResult| {
                    if bucket.allow_vertical_placement
                        && let Some(vertical) = symbol_instance.get_vertical_text_collision_feature()
                    {
                        return place_feature(this, vertical, TextWritingModeType::Vertical);
                    }
                    PlacedFeatureResult::default()
                };

                place_text_for_placement_modes(
                    self,
                    bucket,
                    symbol_instance,
                    &mut placed,
                    &mut placed_vertical_text,
                    &mut place_horizontal,
                    &mut place_vertical,
                );
                update_previous_orientation_if_not_placed(self, placed.placed);

                place_text = placed.placed;
                offscreen &= placed.offscreen;
            } else if !symbol_instance.get_text_collision_feature().along_line
                && !symbol_instance.get_text_collision_feature().boxes.is_empty()
            {
                // If this symbol was in the last placement, shift the previously
                // used anchor to the front of the anchor list, only if the previous
                // anchor is still in the anchor list.
                if let Some(prev) = self.get_prev_placement()
                    && let Some(prev_offset) = prev.variable_offsets.get(&cross_tile_id)
                {
                    let prev_anchor = prev_offset.anchor;
                    let found = variable_text_anchors.iter().position(|anchor| *anchor == prev_anchor);
                    if let Some(found) = found
                        && found != 0
                    {
                        let mut filtered = vec![prev_anchor];
                        if !self.is_tilted_view() {
                            for anchor in &variable_text_anchors {
                                if *anchor != prev_anchor {
                                    filtered.push(*anchor);
                                }
                            }
                        }
                        variable_text_anchors = filtered;
                    }
                }

                let do_variable_icon_placement = ctx.has_icon_text_fit
                    && ctx.icon_overlap_mode == OverlapMode::Never
                    && symbol_instance.placed_icon_index.is_some();
                let variable_text_anchors_ref = &variable_text_anchors;
                let shift_ref = &shift;
                let place_feature_for_variable_anchors =
                    |this: &mut Placement,
                     text_collision_feature: &CollisionFeature,
                     orientation: TextWritingModeType,
                     icon_collision_feature: &CollisionFeature| {
                        this.place_feature_for_variable_anchors(
                            text_collision_feature,
                            orientation,
                            icon_collision_feature,
                            symbol_instance,
                            ctx,
                            projectors,
                            &state.symbol_instance_pools,
                            placed_symbol,
                            &state.text.pools,
                            font_size,
                            &pos_matrix,
                            variable_text_anchors_ref,
                            do_variable_icon_placement,
                            shift_ref,
                        )
                    };

                let mut place_horizontal = |this: &mut Placement| {
                    place_feature_for_variable_anchors(
                        this,
                        symbol_instance.get_text_collision_feature(),
                        TextWritingModeType::Horizontal,
                        symbol_instance.get_icon_collision_feature(),
                    )
                };

                let mut place_vertical = |this: &mut Placement, placed: PlacedFeatureResult| {
                    if bucket.allow_vertical_placement
                        && !placed.placed
                        && let Some(vertical) = symbol_instance.get_vertical_text_collision_feature()
                    {
                        // C++ passes the vertical text feature as the
                        // icon feature too (its `? :` always picks it).
                        return place_feature_for_variable_anchors(
                            this,
                            vertical,
                            TextWritingModeType::Vertical,
                            vertical,
                        );
                    }
                    PlacedFeatureResult::default()
                };

                place_text_for_placement_modes(
                    self,
                    bucket,
                    symbol_instance,
                    &mut placed,
                    &mut placed_vertical_text,
                    &mut place_horizontal,
                    &mut place_vertical,
                );

                place_text = placed.placed;
                offscreen &= placed.offscreen;

                update_previous_orientation_if_not_placed(self, placed.placed);

                // If we didn't get placed, we still need to copy our position from
                // the last placement for fade animations
                if !place_text
                    && let Some(prev_offset) =
                        self.get_prev_placement().and_then(|prev| prev.variable_offsets.get(&cross_tile_id)).copied()
                {
                    self.variable_offsets.insert(cross_tile_id, prev_offset);
                }
            }
        }

        if let Some(placed_icon_index) = symbol_instance.placed_icon_index {
            // An icon fitted to text that took a variable anchor follows the
            // text's shift; no other icon has one.
            let icon_shift = if ctx.has_icon_text_fit && place_text && !variable_text_anchors.is_empty() {
                Some(shift.get())
            } else {
                None
            };

            let icon_buffer = if symbol_instance.has_sdf_icon() { &state.sdf_icon } else { &state.icon };
            let placed_symbol = &icon_buffer.placed_symbols[placed_icon_index];
            let font_size = evaluate_size_for_feature(&ctx.partially_evaluated_icon_size, placed_symbol);
            let place_icon_feature = |this: &mut Placement, collision_feature: &CollisionFeature| {
                this.collision_index.place_feature(
                    collision_feature,
                    &state.symbol_instance_pools,
                    icon_shift,
                    ctx.icon_translation,
                    tile_projector,
                    &projectors.icon_label_plane,
                    ctx.pixel_ratio,
                    placed_symbol,
                    &icon_buffer.pools,
                    ctx.scale,
                    font_size,
                    ctx.icon_overlap_mode,
                    ctx.pitch_text_with_map,
                    ctx.rotate_text_with_map,
                    this.show_collision_boxes,
                    ctx.avoid_edges.as_ref(),
                    ctx.collision_group_predicate(),
                    &mut this.icon_boxes,
                )
            };

            let placed_icon = match symbol_instance.get_vertical_icon_collision_feature() {
                Some(vertical) if placed_vertical_text.placed => {
                    placed_vertical_icon = place_icon_feature(self, vertical);
                    placed_vertical_icon
                }
                _ => place_icon_feature(self, symbol_instance.get_icon_collision_feature()),
            };
            place_icon = placed_icon.placed;
            icon_occluded = placed_icon.occluded;
            offscreen &= placed_icon.offscreen;
        }

        let icon_without_text = !symbol_instance.has_text() || *ctx.get_layout().text_optional.uniform();
        let text_without_icon = !symbol_instance.has_icon() || *ctx.get_layout().icon_optional.uniform();

        // combine placements for icon and text
        if !icon_without_text && !text_without_icon {
            place_text = place_text && place_icon;
            place_icon = place_text;
        } else if !text_without_icon {
            place_text = place_text && place_icon;
        } else if !icon_without_text {
            place_icon = place_text && place_icon;
        }

        if place_text {
            let feature = match symbol_instance.get_vertical_text_collision_feature() {
                Some(vertical) if placed_vertical_text.placed => vertical,
                _ => symbol_instance.get_text_collision_feature(),
            };
            self.collision_index.insert_feature(
                feature,
                &self.text_boxes,
                ctx.text_overlap_mode,
                *ctx.get_layout().text_ignore_placement.uniform(),
                bucket.bucket_instance_id,
                ctx.collision_group.0,
            );
        }

        if place_icon {
            let feature = match symbol_instance.get_vertical_icon_collision_feature() {
                Some(vertical) if placed_vertical_icon.placed => vertical,
                _ => symbol_instance.get_icon_collision_feature(),
            };
            self.collision_index.insert_feature(
                feature,
                &self.icon_boxes,
                ctx.icon_overlap_mode,
                *ctx.get_layout().icon_ignore_placement.uniform(),
                bucket.bucket_instance_id,
                ctx.collision_group.0,
            );
        }

        let has_icon_collision_circle_data = has_icon_collision_circle_data(state);
        let has_text_collision_circle_data = has_text_collision_circle_data(state);

        if has_icon_collision_circle_data
            && symbol_instance.get_icon_collision_feature().along_line
            && !self.icon_boxes.is_empty()
        {
            self.collision_circles.insert((bucket.bucket_instance_id, symbol_index, false), self.icon_boxes.clone());
        }
        if has_text_collision_circle_data
            && symbol_instance.get_text_collision_feature().along_line
            && !self.text_boxes.is_empty()
        {
            self.collision_circles.insert((bucket.bucket_instance_id, symbol_index, true), self.text_boxes.clone());
        }

        // If there's a previous placement with this ID, it comes from a tile that's fading out
        // Erase it so that the placement result from the non-fading tile supersedes it
        self.placements.remove(&cross_tile_id);

        // Symbols behind the planet's horizon stay hidden even when overlap is
        // allowed.
        let result = JointPlacement::new(
            (place_text || ctx.always_show_text) && !placed.occluded,
            (place_icon || ctx.always_show_icon) && !icon_occluded,
            offscreen || state.just_reloaded,
        );
        self.placements.entry(cross_tile_id).or_insert(result);
        let text_boxes = std::mem::take(&mut self.text_boxes);
        let icon_boxes = std::mem::take(&mut self.icon_boxes);
        self.new_symbol_placed(
            symbol_instance,
            &state.symbol_instance_pools,
            ctx,
            result,
            ctx.placement_type,
            &text_boxes,
            &icon_boxes,
        );
        self.text_boxes = text_boxes;
        self.icon_boxes = icon_boxes;
        result
    }

    /// The `placeFeatureForVariableAnchors` lambda of `placeSymbol`.
    #[expect(
        clippy::too_many_arguments,
        reason = "the text and icon features, the symbol and the placement context, projectors and anchors that place them"
    )]
    fn place_feature_for_variable_anchors(
        &mut self,
        text_collision_feature: &CollisionFeature,
        orientation: TextWritingModeType,
        icon_collision_feature: &CollisionFeature,
        symbol_instance: &SymbolInstance,
        ctx: &PlacementContext,
        projectors: &PlacementProjectors<'_>,
        instance_pools: &SymbolInstancePools,
        placed_symbol: &PlacedSymbol,
        pools: &PlacedSymbolPools,
        font_size: f32,
        pos_matrix: &Mat4,
        variable_text_anchors: &[SymbolAnchorType],
        do_variable_icon_placement: bool,
        shift: &Cell<Vec2>,
    ) -> PlacedFeatureResult {
        let bucket = ctx.get_bucket();
        let text_box = &instance_pools.boxes(text_collision_feature)[0];
        let width = text_box.x2 - text_box.x1;
        let height = text_box.y2 - text_box.y1;
        let text_box_scale = symbol_instance.get_text_box_scale();
        let mut placed_feature = PlacedFeatureResult::default();
        let anchors_size = variable_text_anchors.len();
        // The first pass places the text where it overlaps nothing; a second
        // pass, unless the text never overlaps, lets it overlap as its mode
        // allows.
        let placement_attempts =
            if ctx.text_overlap_mode == OverlapMode::Never { anchors_size } else { anchors_size * 2 };
        for i in 0..placement_attempts {
            // when anchorsSize is 0, placementAttempts is also 0,
            // so this code would not be reached
            let anchor = variable_text_anchors[i % anchors_size];
            let variable_text_offset = symbol_instance
                .get_text_variable_anchor_offset()
                .as_ref()
                .expect("textVariableAnchorOffset")
                .get_offset_by_anchor(&anchor);
            let overlap_mode = if i >= anchors_size { ctx.text_overlap_mode } else { OverlapMode::Never };
            shift.set(calculate_variable_layout_offset_unrotated(
                anchor,
                width,
                height,
                variable_text_offset,
                text_box_scale,
            ));
            self.text_boxes.clear();
            let screen_shift = calculate_variable_layout_offset(
                anchor,
                width,
                height,
                variable_text_offset,
                text_box_scale,
                ctx.rotate_text_with_map,
                ctx.pitch_text_with_map,
                ctx.bearing as f32,
            );
            if !self.can_place_at_variable_anchor(
                text_box,
                anchor,
                screen_shift,
                variable_text_anchors,
                pos_matrix,
                ctx.pixel_ratio,
            ) {
                continue;
            }

            placed_feature = self.collision_index.place_feature(
                text_collision_feature,
                instance_pools,
                Some(shift.get()),
                ctx.text_translation,
                &projectors.tile_projector,
                &projectors.text_label_plane,
                ctx.pixel_ratio,
                placed_symbol,
                pools,
                ctx.scale,
                font_size,
                overlap_mode,
                ctx.pitch_text_with_map,
                ctx.rotate_text_with_map,
                self.show_collision_boxes,
                ctx.avoid_edges.as_ref(),
                ctx.collision_group_predicate(),
                &mut self.text_boxes,
            );

            if do_variable_icon_placement {
                let placed_icon_feature = self.collision_index.place_feature(
                    icon_collision_feature,
                    instance_pools,
                    Some(shift.get()),
                    ctx.icon_translation,
                    &projectors.tile_projector,
                    &projectors.icon_label_plane,
                    ctx.pixel_ratio,
                    placed_symbol,
                    pools,
                    ctx.scale,
                    font_size,
                    ctx.icon_overlap_mode,
                    ctx.pitch_text_with_map, // TODO: shall it be pitchIconWithMap?
                    ctx.rotate_text_with_map,
                    self.show_collision_boxes,
                    ctx.avoid_edges.as_ref(),
                    ctx.collision_group_predicate(),
                    &mut self.icon_boxes,
                );
                self.icon_boxes.clear();
                if !placed_icon_feature.placed {
                    continue;
                }
            }

            if placed_feature.placed
                && let Some(cross_tile_id) = symbol_instance.cross_tile_id()
            {
                let mut prev_anchor: Option<SymbolAnchorType> = None;

                // If this label was placed in the previous
                // placement, record the anchor position to allow us
                // to animate the transition
                if let Some(prev) = self.get_prev_placement() {
                    let prev_offset = prev.variable_offsets.get(&cross_tile_id);
                    let prev_placements = prev.placements.get(&cross_tile_id);
                    if let (Some(prev_offset), Some(prev_placements)) = (prev_offset, prev_placements)
                        && prev_placements.text
                    {
                        // TODO: The prevAnchor seems to be unused, needs to be fixed.
                        prev_anchor = Some(prev_offset.anchor);
                    }
                }

                self.variable_offsets.entry(cross_tile_id).or_insert(VariableOffset {
                    offset: variable_text_offset,
                    width,
                    height,
                    anchor,
                    text_box_scale,
                    prev_anchor,
                });

                if bucket.allow_vertical_placement {
                    self.placed_orientations.entry(cross_tile_id).or_insert(orientation);
                }
                break;
            }
        }

        placed_feature
    }

    /// Returns indices into the bucket's symbol instances, in the order to
    /// place them.
    fn get_sorted_symbols(
        &self,
        params: &BucketPlacementData,
        bucket: &SymbolBucket,
        state: &SymbolBucketState,
        _pixel_ratio: f32,
    ) -> Vec<usize> {
        let mut sorted_symbols = get_bucket_symbols(
            bucket,
            state,
            params.sort_key_range.as_ref(),
            self.collision_index.get_transform_state().get_bearing(),
        );
        if let Some(previous_placement) = self.get_prev_placement()
            && self.is_tilted_view()
        {
            let symbols = &state.symbol_instances;
            // The symbols shown before first, then the new ones, then the
            // ones hidden before.
            sorted_symbols.sort_by_key(|&i| match previous_placement.get_symbol_placement(&symbols[i]) {
                Some(placement) if placement.placed() => 0,
                None => 1,
                Some(_) => 2,
            });
        }
        sorted_symbols
    }

    /// The virtual `commit()`.
    fn commit(&mut self) {
        match self.kind {
            PlacementKind::Continuous => self.continuous_commit(),
            PlacementKind::Static | PlacementKind::Tile(_) => self.static_commit(),
        }
    }

    fn continuous_commit(&mut self) {
        let mut placement_changed = false;
        let Some(prev) = self.prev_placement.take() else {
            debug_assert!(false);
            return;
        };
        self.prev_zoom_adjustment = prev.zoom_adjustment(self.placement_zoom);
        let increment = prev.symbol_fade_change(self.commit_time);

        // add the opacities from the current placement, and copy their current
        // values from the previous placement
        for (id, joint_placement) in &self.placements {
            if let Some(prev_opacity) = prev.opacities.get(id) {
                self.opacities.entry(*id).or_insert(JointOpacityState::from_prev(
                    prev_opacity,
                    increment,
                    joint_placement.text,
                    joint_placement.icon,
                ));
                placement_changed = placement_changed
                    || joint_placement.icon != prev_opacity.icon.placed
                    || joint_placement.text != prev_opacity.text.placed;
            } else {
                self.opacities.entry(*id).or_insert(JointOpacityState::new(
                    joint_placement.text,
                    joint_placement.icon,
                    joint_placement.skip_fade,
                ));
                placement_changed = placement_changed || joint_placement.icon || joint_placement.text;
            }
        }

        // copy and update values from the previous placement that aren't in the
        // current placement but haven't finished fading
        for (id, prev_opacity) in &prev.opacities {
            if !self.opacities.contains_key(id) {
                let joint_opacity = JointOpacityState::from_prev(prev_opacity, increment, false, false);
                if !joint_opacity.is_hidden() {
                    self.opacities.insert(*id, joint_opacity);
                    placement_changed = placement_changed || prev_opacity.icon.placed || prev_opacity.text.placed;
                }
            }
        }

        for (cross_tile_id, prev_offset) in &prev.variable_offsets {
            let found_offset = self.variable_offsets.contains_key(cross_tile_id);
            let found_opacity = self.opacities.get(cross_tile_id);
            if !found_offset && found_opacity.is_some_and(|opacity| !opacity.is_hidden()) {
                self.variable_offsets.insert(*cross_tile_id, *prev_offset);
            }
        }

        for (cross_tile_id, prev_orientation) in &prev.placed_orientations {
            let found_orientation = self.placed_orientations.contains_key(cross_tile_id);
            let found_opacity = self.opacities.get(cross_tile_id);
            if !found_orientation && found_opacity.is_some_and(|opacity| !opacity.is_hidden()) {
                self.placed_orientations.insert(*cross_tile_id, *prev_orientation);
            }
        }

        self.fade_start_time = if placement_changed { self.commit_time } else { prev.fade_start_time };
        self.spare_collision_index = Some(prev.collision_index);
    }

    fn static_commit(&mut self) {
        self.fade_start_time = self.commit_time;
        for (id, joint_placement) in &self.placements {
            self.opacities.entry(*id).or_insert(JointOpacityState::new(
                joint_placement.text,
                joint_placement.icon,
                joint_placement.skip_fade,
            ));
        }
    }

    pub(crate) fn update_layer_buckets(&self, layer: &dyn RenderLayer, state: &TransformState, update_opacities: bool) {
        let mut seen_cross_tile_ids: BTreeSet<NonZeroU32> = BTreeSet::new();
        for item in layer.get_placement_data() {
            // `sortKeyRange->isFirstRange()`: `start == 0u`.
            if item.sort_key_range.is_none_or(|range| range.start == 0) {
                update_vertices(&*item.bucket, self, update_opacities, state, item, &mut seen_cross_tile_ids);
            }
        }
    }

    /// Lays out the positions of a layer drawn from buckets other layers
    /// place, after `update_layer_buckets`.
    pub(crate) fn update_follower_buckets(&self, layer: &dyn RenderLayer, state: &TransformState) {
        for item in layer.get_follower_data() {
            if let Some(bucket) = symbol_bucket(&item.bucket) {
                bucket.update_layer_vertices(self, state, item, layer.get_id());
            }
        }
    }

    /// Updates the bucket's dynamic attribute data; returns whether its
    /// vertices were updated. With a layer ID, updates that layer's own
    /// positions, which it needs only at another translation than the
    /// leader's.
    pub(crate) fn update_bucket_dynamic_attribute_data(
        &self,
        bucket: &SymbolBucket,
        state: &TransformState,
        data: &BucketPlacementData,
        layer_id: Option<&str>,
    ) -> bool {
        let tile = data.tile();
        let layout = &*bucket.layout;
        let along_line = *layout.symbol_placement.uniform() != SymbolPlacementType::Point;
        let has_variable_text_anchors = bucket.has_variable_text_anchors();
        let mut guard = bucket.state();
        let bs = &mut *guard;
        let has_text = has_text_data(bs);
        let has_icon = has_icon_data(bs);
        let has_sdf_icon = has_sdf_icon_data(bs);
        let has_variable_anchors = has_variable_text_anchors && has_text;
        let update_text_fit_icon = *layout.icon_text_fit.uniform() != IconTextFitType::None
            && (bucket.allow_vertical_placement || has_variable_anchors)
            && (has_icon || has_sdf_icon);
        let mut result = false;
        let tile_projector = TileProjector::with_data(state, &tile.id, tile.projection);
        let screen_space = &layout.symbol_screen_space;
        let text_translation = translation_point(&tile.id, &data.text_translate, state, *screen_space.uniform());
        let icon_translation = translation_point(&tile.id, &data.icon_translate, state, *screen_space.uniform());
        match layer_id {
            None => {
                bs.dynamic_text_translation = text_translation;
                bs.dynamic_icon_translation = icon_translation;
            }
            Some(layer_id) => {
                if (!along_line && !has_variable_anchors)
                    || (!bs.has_layer_dynamic_attribute_data(layer_id)
                        && text_translation == bs.dynamic_text_translation
                        && icon_translation == bs.dynamic_icon_translation)
                {
                    return false;
                }
            }
        }
        // GL JS rewrites a shared bucket's positions for each layer it draws;
        // one at another translation keeps its own (`positions_and_symbols`).

        if along_line {
            if *layout.icon_rotation_alignment.uniform() == AlignmentType::Map {
                let pitch_with_map = *layout.icon_pitch_alignment.uniform() == AlignmentType::Map;
                let keep_upright = &layout.icon_keep_upright;
                if has_sdf_icon {
                    let (positions, placed_symbols, pools) = bs.sdf_icon.positions_and_symbols(layer_id);
                    reproject_line_labels(
                        positions,
                        placed_symbols,
                        pools,
                        &tile_projector,
                        pitch_with_map,
                        true, /*rotateWithMap*/
                        *keep_upright.uniform(),
                        tile,
                        &bucket.icon_size_binder,
                        state,
                        icon_translation,
                    );
                    result = true;
                }
                if has_icon {
                    let (positions, placed_symbols, pools) = bs.icon.positions_and_symbols(layer_id);
                    reproject_line_labels(
                        positions,
                        placed_symbols,
                        pools,
                        &tile_projector,
                        pitch_with_map,
                        true, /*rotateWithMap*/
                        *keep_upright.uniform(),
                        tile,
                        &bucket.icon_size_binder,
                        state,
                        icon_translation,
                    );
                    result = true;
                }
            }

            let text_rotation_alignment = *layout.text_rotation_alignment.uniform();
            if has_text && text_rotation_alignment != TextRotationAlignmentType::Viewport {
                let pitch_with_map = *layout.text_pitch_alignment.uniform() == AlignmentType::Map;
                let keep_upright = &layout.text_keep_upright;
                let (positions, placed_symbols, pools) = bs.text.positions_and_symbols(layer_id);
                reproject_line_labels(
                    positions,
                    placed_symbols,
                    pools,
                    &tile_projector,
                    pitch_with_map,
                    text_rotation_alignment == TextRotationAlignmentType::Map,
                    *keep_upright.uniform(),
                    tile,
                    &bucket.text_size_binder,
                    state,
                    text_translation,
                );
                result = true;
            }
        } else if has_variable_anchors {
            let mut has_variable_placement = false;

            let partially_evaluated_size = bucket.text_size_binder.evaluate_for_zoom(state.get_zoom() as f32);
            let tile_scale = 2f64.powf(state.get_zoom() - f64::from(tile.get_overscaled_tile_id().overscaled_z)) as f32;
            let rotate_with_map = *layout.text_rotation_alignment.uniform() == TextRotationAlignmentType::Map;
            let pitch_with_map = *layout.text_pitch_alignment.uniform() == AlignmentType::Map;
            let pixels_to_tile_units = tile.id.pixels_to_tile_units(1.0, state.get_zoom() as f32);
            // The shader leaves the translation to us for variable anchors,
            // so the shifted anchor carries it.
            let translation = text_translation;
            let label_plane = LabelPlaneProjector::new(
                &tile_projector,
                pitch_with_map,
                rotate_with_map,
                pixels_to_tile_units,
                translation,
            );
            // The shift follows the axes the collision box was projected
            // along, so the label lands in its own box.
            let mut vec_east = Vec2::new(1.0f32, 0.0);
            let mut vec_south = Vec2::new(0.0f32, 1.0);
            if pitch_with_map && !rotate_with_map {
                let (east, south) = get_tile_skew_vectors(state);
                vec_east = Vec2::new(east[0] as f32, east[1] as f32);
                vec_south = Vec2::new(south[0] as f32, south[1] as f32);
            }
            let mut placed_text_shifts: HashMap<usize, (usize, Vec2)> = HashMap::default();

            let (text_positions, text_placed_symbols, _) = bs.text.positions_and_symbols(layer_id);
            text_positions.clear();
            for (i, symbol) in text_placed_symbols.iter().enumerate() {
                let mut variable_offset: Option<VariableOffset> = None;
                let skip_orientation = bucket.allow_vertical_placement && symbol.placed_orientation.is_none();
                if !symbol.hidden
                    && !skip_orientation
                    && let Some(cross_tile_id) = symbol.cross_tile_id
                    && let Some(offset) = self.variable_offsets.get(&cross_tile_id)
                {
                    has_variable_placement = true;
                    variable_offset = Some(*offset);
                }

                match variable_offset {
                    None => {
                        // These symbols are from a justification that is not being
                        // used, or a label that wasn't placed so we don't need to do
                        // the extra math to figure out what incremental shift to apply.
                        hide_glyphs(symbol.glyph_offsets.len(), text_positions);
                    }
                    Some(variable_offset) => {
                        let label_plane = label_plane.raised(symbol.height_offset);
                        let tile_anchor = symbol.anchor_point;
                        let projected = if pitch_with_map {
                            label_plane.to_clip_space_from_tile(tile_anchor)
                        } else {
                            label_plane.project(tile_anchor)
                        };
                        let projected_anchor = Vec2::new(projected.point.x as f32, projected.point.y as f32);
                        let perspective_ratio = 0.5
                            + 0.5
                                * (state.get_camera_to_center_distance()
                                    / projected.signed_distance_from_camera as f32);
                        let mut render_text_size =
                            evaluate_size_for_feature(&partially_evaluated_size, symbol) * perspective_ratio / ONE_EM;
                        if pitch_with_map {
                            // Go from size in pixels to equivalent size in tile units
                            render_text_size *= bucket.tile_pixel_ratio / tile_scale;
                        }

                        let shift = calculate_variable_render_shift(
                            variable_offset.anchor,
                            variable_offset.width,
                            variable_offset.height,
                            variable_offset.offset,
                            variable_offset.text_box_scale,
                            render_text_size,
                        );

                        // Usual case is that we take the projected anchor and add the
                        // pixel-based shift calculated above. In the (somewhat weird)
                        // case of pitch-aligned text, we add an equivalent tile-unit
                        // based shift to the anchor before projecting to the label
                        // plane.
                        let shifted_anchor = if pitch_with_map {
                            let correction = tile_projector.pitched_text_correction(&Point::new(
                                f64::from(tile_anchor.x + translation.x),
                                f64::from(tile_anchor.y + translation.y),
                            )) as f32;
                            let tile_shift = vec_east * (shift.x * correction) + vec_south * (shift.y * correction);
                            let shifted = label_plane.project(tile_anchor + tile_shift).point;
                            Vec2::new(shifted.x as f32, shifted.y as f32)
                        } else if rotate_with_map {
                            // Screen-space labels stay east-west aligned, so
                            // the shift follows the projected east vector.
                            let east = label_plane.project(Vec2::new(tile_anchor.x + 1.0, tile_anchor.y)).point;
                            let to_east_x = east.x as f32 - projected_anchor.x;
                            let to_east_y = east.y as f32 - projected_anchor.y;
                            let angle = (to_east_y / to_east_x).atan()
                                + if to_east_x < 0.0 { std::f32::consts::PI } else { 0.0 };
                            let rotated = Vec2::from_angle(angle).rotate(shift);
                            projected_anchor + rotated
                        } else {
                            projected_anchor + shift
                        };

                        if update_text_fit_icon && let Some(placed_icon_index) = symbol.placed_icon_index {
                            placed_text_shifts.entry(placed_icon_index).or_insert((i, shifted_anchor));
                        }

                        let angle = symbol.angle;
                        for _ in 0..symbol.glyph_offsets.len() {
                            add_dynamic_attributes(shifted_anchor, angle, text_positions);
                        }
                    }
                }
            }
            if layer_id.is_none() {
                bs.has_variable_placement = has_variable_placement;
            }

            if update_text_fit_icon && has_variable_placement {
                let update_icon = |icon_buffer: &mut Buffer| {
                    let (icon_positions, icon_placed_symbols, _) = icon_buffer.positions_and_symbols(layer_id);
                    icon_positions.clear();
                    for (i, placed_icon) in icon_placed_symbols.iter().enumerate() {
                        if placed_icon.hidden
                            || (placed_icon.placed_orientation.is_none() && bucket.allow_vertical_placement)
                        {
                            hide_glyphs(placed_icon.glyph_offsets.len(), icon_positions);
                        } else {
                            match placed_text_shifts.get(&i) {
                                None => {
                                    hide_glyphs(placed_icon.glyph_offsets.len(), icon_positions);
                                }
                                Some(pair) => {
                                    for _ in 0..placed_icon.glyph_offsets.len() {
                                        add_dynamic_attributes(pair.1, placed_icon.angle, icon_positions);
                                    }
                                }
                            }
                        }
                    }
                };
                update_icon(&mut bs.icon);
                update_icon(&mut bs.sdf_icon);
            }

            result = true;
        } else if bucket.allow_vertical_placement && has_text {
            let update_dynamic_attribute_data = |buffer: &mut Buffer| {
                buffer.dynamic_attribute_data.clear();
                for symbol in &buffer.placed_symbols {
                    if symbol.hidden || symbol.placed_orientation.is_none() {
                        hide_glyphs(symbol.glyph_offsets.len(), &mut buffer.dynamic_attribute_data);
                    } else {
                        for _ in 0..symbol.glyph_offsets.len() {
                            add_dynamic_attributes(
                                symbol.anchor_point,
                                symbol.angle,
                                &mut buffer.dynamic_attribute_data,
                            );
                        }
                    }
                }
            };

            update_dynamic_attribute_data(&mut bs.text);
            // When text box is rotated, icon-text-fit icon must be rotated as well.
            if update_text_fit_icon {
                update_dynamic_attribute_data(&mut bs.icon);
                update_dynamic_attribute_data(&mut bs.sdf_icon);
            }

            result = true;
        }

        result
    }

    pub(crate) fn update_bucket_opacities(
        &self,
        bucket: &SymbolBucket,
        state: &TransformState,
        seen_cross_tile_ids: &mut BTreeSet<NonZeroU32>,
    ) {
        let layout = &*bucket.layout;
        let variable_placement = bucket.has_variable_text_anchors();
        let mut guard = bucket.state();
        let bs = &mut *guard;

        let has_text = has_text_data(bs);
        let has_icon = has_icon_data(bs);
        let has_sdf_icon = has_sdf_icon_data(bs);
        let has_icon_collision_box = has_icon_collision_box_data(bs);
        let has_icon_collision_circle = has_icon_collision_circle_data(bs);
        let has_text_collision_box = has_text_collision_box_data(bs);
        let has_text_collision_circle = has_text_collision_circle_data(bs);

        if has_text {
            bs.text.opacity_attribute_data.clear();
        }
        if has_icon {
            bs.icon.opacity_attribute_data.clear();
        }
        if has_sdf_icon {
            bs.sdf_icon.opacity_attribute_data.clear();
        }
        if has_icon_collision_box && let Some(buffer) = bs.icon_collision_box.as_mut() {
            buffer.dynamic_vertices.clear();
        }
        if has_icon_collision_circle && let Some(buffer) = bs.icon_collision_circle.as_mut() {
            buffer.dynamic_vertices.clear();
        }
        if has_text_collision_box && let Some(buffer) = bs.text_collision_box.as_mut() {
            buffer.dynamic_vertices.clear();
        }
        if has_text_collision_circle && let Some(buffer) = bs.text_collision_circle.as_mut() {
            buffer.dynamic_vertices.clear();
        }

        let duplicate_opacity_state = JointOpacityState::new(false, false, true);

        let text_always_overlap = OverlapMode::text(layout) == OverlapMode::Always;
        let icon_always_overlap = OverlapMode::icon(layout) == OverlapMode::Always;
        let rotate_with_map = *layout.text_rotation_alignment.uniform() == TextRotationAlignmentType::Map;
        let pitch_with_map = *layout.text_pitch_alignment.uniform() == AlignmentType::Map;
        let has_icon_text_fit = *layout.icon_text_fit.uniform() != IconTextFitType::None;
        let screen_space = &layout.symbol_screen_space;

        // If overlap is always, we can show symbols before placement runs on them
        // But we have to wait for placement if we potentially depend on a paired icon/text
        // with another overlap mode.
        // See https://github.com/mapbox/mapbox-gl-native/issues/12483
        // Prevent a flickering issue when showing a symbol allowing overlap.
        let default_opacity_state = JointOpacityState::new(
            (*screen_space.uniform() || bs.just_reloaded)
                && text_always_overlap
                && (icon_always_overlap || !(has_icon || has_sdf_icon) || *layout.icon_optional.uniform()),
            (*screen_space.uniform() || bs.just_reloaded)
                && icon_always_overlap
                && (text_always_overlap || !has_text || *layout.text_optional.uniform()),
            true,
        );

        let SymbolBucketState {
            symbol_instances,
            text,
            icon,
            sdf_icon,
            icon_collision_box,
            text_collision_box,
            icon_collision_circle,
            text_collision_circle,
            ..
        } = bs;

        for (symbol_index, symbol_instance) in symbol_instances.iter().enumerate() {
            let cross_tile_id = symbol_instance.cross_tile_id();
            let is_duplicate = cross_tile_id.is_none_or(|id| !seen_cross_tile_ids.insert(id));

            let mut opacity_state = default_opacity_state;
            if is_duplicate {
                opacity_state = duplicate_opacity_state;
            } else if let Some(opacity) = cross_tile_id.and_then(|id| self.opacities.get(&id)) {
                opacity_state = *opacity;
            }

            let count = 4;
            if symbol_instance.has_text() {
                let mut text_opacity_vertices_size = 0usize;
                let opacity_attributes =
                    SymbolBucket::opacity_attributes(opacity_state.text.placed, opacity_state.text.opacity);
                if let Some(i) = symbol_instance.placed_right_text_index {
                    text_opacity_vertices_size += symbol_instance.get_right_justified_glyph_quads_size() * count;
                    text.placed_symbols[i].hidden = opacity_state.is_hidden();
                }
                if let Some(i) = symbol_instance.placed_center_text_index
                    && !symbol_instance.get_single_line()
                {
                    text_opacity_vertices_size += symbol_instance.get_center_justified_glyph_quads_size() * count;
                    text.placed_symbols[i].hidden = opacity_state.is_hidden();
                }
                if let Some(i) = symbol_instance.placed_left_text_index
                    && !symbol_instance.get_single_line()
                {
                    text_opacity_vertices_size += symbol_instance.get_left_justified_glyph_quads_size() * count;
                    text.placed_symbols[i].hidden = opacity_state.is_hidden();
                }
                if let Some(i) = symbol_instance.placed_vertical_text_index {
                    text_opacity_vertices_size += symbol_instance.get_vertical_glyph_quads_size() * count;
                    text.placed_symbols[i].hidden = opacity_state.is_hidden();
                }

                extend(&mut text.opacity_attribute_data, text_opacity_vertices_size, opacity_attributes);

                let mut previous_orientation = TextWritingModeType::Horizontal;
                if bucket.allow_vertical_placement
                    && let Some(prev_orientation) = cross_tile_id.and_then(|id| self.placed_orientations.get(&id))
                {
                    previous_orientation = *prev_orientation;
                    mark_used_orientation(text, icon, sdf_icon, *prev_orientation, symbol_instance);
                }

                if let Some(prev_offset) = cross_tile_id.and_then(|id| self.variable_offsets.get(&id)) {
                    mark_used_justification(text, prev_offset.anchor, symbol_instance, previous_orientation);
                }
            }
            if symbol_instance.has_icon() {
                let mut icon_opacity_vertices_size = 0usize;
                let opacity_attributes =
                    SymbolBucket::opacity_attributes(opacity_state.icon.placed, opacity_state.icon.opacity);
                let icon_buffer: &mut Buffer = if symbol_instance.has_sdf_icon() { &mut *sdf_icon } else { &mut *icon };

                if let Some(i) = symbol_instance.placed_icon_index {
                    icon_opacity_vertices_size += symbol_instance.get_icon_quads_size() * count;
                    icon_buffer.placed_symbols[i].hidden = opacity_state.is_hidden();
                }

                if let Some(i) = symbol_instance.placed_vertical_icon_index {
                    icon_opacity_vertices_size += symbol_instance.get_icon_quads_size() * count;
                    icon_buffer.placed_symbols[i].hidden = opacity_state.is_hidden();
                }

                extend(&mut icon_buffer.opacity_attribute_data, icon_opacity_vertices_size, opacity_attributes);
            }

            let update_icon_collision_box =
                |icon_collision_box: &mut Option<crate::buckets::symbol_bucket::CollisionBuffer>,
                 feature: &CollisionFeature,
                 placed: bool,
                 shift: Vec2| {
                    if feature.along_line {
                        return;
                    }
                    let Some(icon_collision_box) = icon_collision_box.as_mut() else { return };
                    let dynamic_vertex = SymbolBucket::collision_dynamic_vertex(placed, false, shift);
                    extend(&mut icon_collision_box.dynamic_vertices, feature.boxes.len() * 4, dynamic_vertex);
                };

            let update_text_collision_box =
                |text_collision_box: &mut Option<crate::buckets::symbol_bucket::CollisionBuffer>,
                 feature: &CollisionFeature,
                 placed: bool|
                 -> Vec2 {
                    let mut shift = Vec2::ZERO;
                    if feature.along_line {
                        return shift;
                    }
                    let mut used = true;
                    if variable_placement {
                        if let Some(variable_offset) = cross_tile_id.and_then(|id| self.variable_offsets.get(&id)) {
                            // This will show either the currently placed position or
                            // the last successfully placed position (so you can
                            // visualize what collision just made the symbol disappear,
                            // and the most likely place for the symbol to come back)
                            shift = calculate_variable_layout_offset(
                                variable_offset.anchor,
                                variable_offset.width,
                                variable_offset.height,
                                variable_offset.offset,
                                variable_offset.text_box_scale,
                                rotate_with_map,
                                pitch_with_map,
                                state.get_bearing() as f32,
                            );
                        } else {
                            // No offset -> this symbol hasn't been placed since coming
                            // on-screen No single box is particularly meaningful and
                            // all of them would be too noisy Use the center box just to
                            // show something's there, but mark it "not used"
                            used = false;
                        }
                    }
                    let dynamic_vertex = SymbolBucket::collision_dynamic_vertex(placed, !used, shift);
                    let Some(text_collision_box) = text_collision_box.as_mut() else { return shift };
                    extend(&mut text_collision_box.dynamic_vertices, feature.boxes.len() * 4, dynamic_vertex);
                    shift
                };

            let update_collision_circles = |circle_buffer: &mut Option<
                crate::buckets::symbol_bucket::CollisionBuffer,
            >,
                                            feature: &CollisionFeature,
                                            placed: bool,
                                            is_text: bool| {
                if !feature.along_line {
                    return;
                }
                let Some(circle_buffer) = circle_buffer.as_mut() else { return };
                let dynamic_vertices = &mut circle_buffer.dynamic_vertices;
                if let Some(circles) = self.collision_circles.get(&(bucket.bucket_instance_id, symbol_index, is_text)) {
                    for circle in circles {
                        let dynamic_vertex =
                            SymbolBucket::collision_dynamic_vertex(placed, !circle.is_circle(), Vec2::ZERO);
                        extend(dynamic_vertices, 4, dynamic_vertex);
                    }
                } else {
                    // This feature was not placed, because it was not loaded or
                    // from a fading tile. Apply default values.
                    // C++ quirk: a function-local `static`, so the
                    // vertex of the first call is used from then on.
                    static DYNAMIC_VERTEX: OnceLock<CollisionBoxDynamicVertex> = OnceLock::new();
                    let dynamic_vertex = *DYNAMIC_VERTEX.get_or_init(|| {
                        SymbolBucket::collision_dynamic_vertex(placed, false /*not used*/, Vec2::ZERO)
                    });
                    extend(dynamic_vertices, 4 * feature.boxes.len(), dynamic_vertex);
                }
            };

            let mut text_shift = Vec2::ZERO;
            let mut vertical_text_shift = Vec2::ZERO;
            if has_text_collision_box {
                text_shift = update_text_collision_box(
                    text_collision_box,
                    symbol_instance.get_text_collision_feature(),
                    opacity_state.text.placed,
                );
                if bucket.allow_vertical_placement
                    && let Some(vertical) = symbol_instance.get_vertical_text_collision_feature()
                {
                    vertical_text_shift =
                        update_text_collision_box(text_collision_box, vertical, opacity_state.text.placed);
                }
            }
            if has_icon_collision_box {
                update_icon_collision_box(
                    icon_collision_box,
                    symbol_instance.get_icon_collision_feature(),
                    opacity_state.icon.placed,
                    if has_icon_text_fit { text_shift } else { Vec2::ZERO },
                );
                if bucket.allow_vertical_placement
                    && let Some(vertical) = symbol_instance.get_vertical_icon_collision_feature()
                {
                    update_icon_collision_box(
                        icon_collision_box,
                        vertical,
                        opacity_state.text.placed,
                        if has_icon_text_fit { vertical_text_shift } else { Vec2::ZERO },
                    );
                }
            }

            if has_icon_collision_circle {
                update_collision_circles(
                    icon_collision_circle,
                    symbol_instance.get_icon_collision_feature(),
                    opacity_state.icon.placed,
                    false,
                );
            }
            if has_text_collision_circle {
                update_collision_circles(
                    text_collision_circle,
                    symbol_instance.get_text_collision_feature(),
                    opacity_state.text.placed,
                    true,
                );
            }
        }
        drop(guard);

        bucket.sort_features(state.get_bearing() as f32);

        if let Some(retained_data) = self.retained_query_data.get(&bucket.bucket_instance_id) {
            *retained_data.feature_sort_order.borrow_mut() = bucket.state().feature_sort_order.clone();
        }
    }

    /// The virtual `symbolFadeChange(now)`.
    pub(crate) fn symbol_fade_change(&self, now: Option<Instant>) -> f32 {
        match self.kind {
            PlacementKind::Continuous => {
                let duration = self.transition_options.duration.unwrap_or(DEFAULT_TRANSITION_DURATION);
                if self.transitions_enabled() && duration > Duration::ZERO {
                    return elapsed(now, self.commit_time).div_duration_f32(duration) + self.prev_zoom_adjustment;
                }
                1.0
            }
            PlacementKind::Static | PlacementKind::Tile(_) => 1.0,
        }
    }

    pub(crate) fn zoom_adjustment(&self, zoom: f32) -> f32 {
        // When zooming out labels can overlap each other quickly. This
        // adjustment is used to reduce the fade duration for symbols while zooming
        // out quickly. It is also used to reduce the interval between placement
        // calculations. Reducing the interval between placements means collisions
        // are discovered and eliminated sooner.
        let adjustment = (self.placement_zoom - zoom) / 1.5;
        // `std::max(0.0f, x)`: `(0 < x) ? x : 0`.
        if 0.0 < adjustment { adjustment } else { 0.0 }
    }

    pub(crate) fn get_symbol_placement(&self, symbol: &SymbolInstance) -> Option<&JointPlacement> {
        self.placements.get(&symbol.cross_tile_id()?)
    }

    pub(crate) fn get_update_period(&self, zoom: f32) -> Duration {
        // Even if transitionOptions.duration is set to a value < 300ms, we still wait
        // for this default transition duration before attempting another placement operation.
        let duration = self.transition_options.duration.unwrap_or(DEFAULT_TRANSITION_DURATION);
        // `std::max(a, b)`: `(a < b) ? b : a`.
        let fade_duration = if DEFAULT_TRANSITION_DURATION < duration { duration } else { DEFAULT_TRANSITION_DURATION };
        // `duration_cast<Duration>(fadeDuration * (1.0 - zoomAdjustment(zoom)))`;
        // zero once the adjustment passes 1.
        Duration::from_nanos((fade_duration.as_nanos() as f64 * (1.0 - f64::from(self.zoom_adjustment(zoom)))) as u64)
    }

    /// The virtual `transitionsEnabled()`.
    pub(crate) fn transitions_enabled(&self) -> bool {
        match self.kind {
            PlacementKind::Continuous => self.transition_options.enable_placement_transitions,
            PlacementKind::Static | PlacementKind::Tile(_) => false,
        }
    }

    /// The virtual `hasTransitions(now)`.
    pub(crate) fn has_transitions(&self, now: Option<Instant>) -> bool {
        match self.kind {
            PlacementKind::Continuous => {
                debug_assert!(self.transitions_enabled());
                let duration = self.transition_options.duration.unwrap_or(DEFAULT_TRANSITION_DURATION);
                elapsed(now, self.fade_start_time) < duration
            }
            PlacementKind::Static | PlacementKind::Tile(_) => false,
        }
    }

    /// The virtual `collectPlacedSymbolData(enable)`.
    pub(crate) fn collect_placed_symbol_data(&mut self, enable: bool) {
        if let PlacementKind::Tile(tile) = &mut self.kind {
            tile.collect_data = enable;
        }
    }

    /// The virtual `getPlacedSymbolsData()`.
    pub(crate) fn get_placed_symbols_data(&self) -> &[PlacedSymbolData] {
        match &self.kind {
            PlacementKind::Tile(tile) => &tile.placed_symbols_data,
            _ => &[],
        }
    }

    /// The query data of a bucket instance; `Err` if there is none.
    pub(crate) fn get_query_data(&self, bucket_instance_id: u32) -> Result<&RetainedQueryData, String> {
        self.retained_query_data
            .get(&bucket_instance_id)
            .ok_or_else(|| "Placement::getQueryData with unrecognized bucketInstanceId".to_owned())
    }

    /// The virtual `newSymbolPlaced(...)` (only `TilePlacement` overrides it).
    #[expect(
        clippy::too_many_arguments,
        reason = "the symbol and its pools, the placement and its context and type, and the projected boxes"
    )]
    fn new_symbol_placed(
        &mut self,
        symbol: &SymbolInstance,
        instance_pools: &SymbolInstancePools,
        ctx: &PlacementContext,
        placement: JointPlacement,
        placement_type: SymbolPlacementType,
        text_collision_boxes: &[ProjectedCollisionBox],
        icon_collision_boxes: &[ProjectedCollisionBox],
    ) {
        let viewport_padding = self.collision_index.get_viewport_padding();
        let should_retry = self.should_retry_placement(placement, ctx);
        let PlacementKind::Tile(tile) = &mut self.kind else {
            return;
        };
        if !tile.collect_data || placement_type != SymbolPlacementType::Point || should_retry {
            return;
        }

        let mut text_collision_box: Option<BBox> = None;
        if let Some(box_) = text_collision_boxes.first() {
            debug_assert_eq!(text_collision_boxes.len(), 1);
            debug_assert!(box_.is_box());
            text_collision_box = Some(*box_.box_());
        }
        let mut icon_collision_box: Option<BBox> = None;
        if let Some(box_) = icon_collision_boxes.first() {
            debug_assert_eq!(icon_collision_boxes.len(), 1);
            debug_assert!(box_.is_box());
            icon_collision_box = Some(*box_.box_());
        }
        let symbol_data = PlacedSymbolData {
            key: instance_pools.key(symbol).to_vec(),
            text_collision_box,
            icon_collision_box,
            text_placed: placement.text,
            icon_placed: placement.icon,
            intersects_tile_border: !placement.skip_fade && tile.populate_intersections,
            viewport_padding,
            layer: ctx.get_bucket().bucket_leader_id.clone(),
        };
        tile.placed_symbols_data.push(symbol_data);
    }

    /// The virtual `getAvoidEdges(bucket, posMatrix)`.
    fn get_avoid_edges(&mut self, bucket: &SymbolBucket, pos_matrix: &Mat4) -> Option<CollisionBoundaries> {
        let PlacementKind::Tile(tile) = &mut self.kind else {
            return None;
        };
        // `TilePlacement::getAvoidEdges`.
        tile.tile_borders = Some(self.collision_index.project_tile_boundaries(pos_matrix));
        let layout = &*bucket.layout;
        if *layout.symbol_avoid_edges.uniform() || *layout.symbol_placement.uniform() == SymbolPlacementType::Line {
            return tile.tile_borders;
        }
        None
    }

    /// The virtual `canPlaceAtVariableAnchor(box, anchor, shift, anchors,
    /// posMatrix, textPixelRatio)`.
    fn can_place_at_variable_anchor(
        &self,
        box_: &CollisionBox,
        anchor: SymbolAnchorType,
        shift: Vec2,
        anchors: &[SymbolAnchorType],
        pos_matrix: &Mat4,
        text_pixel_ratio: f32,
    ) -> bool {
        let PlacementKind::Tile(tile) = &self.kind else {
            return true;
        };
        // `TilePlacement::canPlaceAtVariableAnchor`.
        let tile_borders = tile.tile_borders.as_ref().expect("tileBorders");
        if tile.populate_intersections {
            // A variable label is only allowed to intersect tile border with the first anchor.
            if anchors.first() == Some(&anchor) {
                // Check, that the label would intersect the tile borders even
                // without shift, otherwise intersection is not allowed (preventing
                // cut-offs in case the shift is lager than the buffer size).
                let status = self.collision_index.intersects_tile_edges(
                    box_,
                    Vec2::ZERO,
                    pos_matrix,
                    text_pixel_ratio,
                    tile_borders,
                );
                if status.flags != IntersectStatus::NONE {
                    return true;
                }
            }
            // The most important labels shall be placed first anyway, so we continue trying
            // the following variable anchors for them; less priority labels
            // will wait for the second round (when `populateIntersections` is `false`).
            if tile.current_intersection_priority > 0 {
                return false;
            }
        }
        // Can be placed, if it does not intersect tile borders.
        let status =
            self.collision_index.intersects_tile_edges(box_, shift, pos_matrix, text_pixel_ratio, tile_borders);
        status.flags == IntersectStatus::NONE
    }

    fn should_retry_placement(&self, placement: JointPlacement, ctx: &PlacementContext) -> bool {
        let PlacementKind::Tile(tile) = &self.kind else {
            return false;
        };
        // We re-try the placement to try out remaining variable anchors.
        tile.populate_intersections && !placement.placed() && ctx.get_bucket().has_variable_text_anchors()
    }

    fn tile_state(&mut self) -> &mut TilePlacementState {
        match &mut self.kind {
            PlacementKind::Tile(tile) => tile,
            _ => unreachable!("TilePlacement"),
        }
    }

    fn tile_place_layers(&mut self, layers: &[RenderLayerPtr]) {
        {
            let tile = self.tile_state();
            tile.placed_symbols_data.clear();
            tile.seen_cross_tile_ids.clear();
            tile.intersections.clear();
            tile.current_intersection_priority = 0;
            // Populate intersections.
            tile.populate_intersections = true;
        }
        let mut seen_cross_tile_ids = std::mem::take(&mut self.tile_state().seen_cross_tile_ids);
        for layer in layers.iter().rev() {
            self.place_layer(&*layer.borrow(), &mut seen_cross_tile_ids);
        }

        let intersections = std::mem::take(&mut self.tile_state().intersections);
        let mut order: Vec<usize> = (0..intersections.len()).collect();
        // A stable sort: intersections that tie on every key keep the order
        // the layers and their buckets collected them in.
        order.sort_by(|&ia, &ib| {
            let a = &intersections[ia];
            let b = &intersections[ib];
            a.priority
                .cmp(&b.priority)
                // Items arranged as: VerticalBorders & HorizontalBorders (3) ->
                // VerticalBorders (2) -> HorizontalBorders (1)
                .then(b.status.flags.cmp(&a.status.flags))
                // If both intersects the same border(s), look for a more noticeable cut-off.
                .then(b.status.min_section_length.cmp(&a.status.min_section_length))
                // Look at the anchor coordinates
                .then(a.anchor_point.y.total_cmp(&b.anchor_point.y))
                .then(a.anchor_point.x.total_cmp(&b.anchor_point.x))
        });
        // Place intersections.
        let transform_state = self.collision_index.get_transform_state().clone();
        for &i in &order {
            let intersection = &intersections[i];
            let ctx = &intersection.ctx;
            self.tile_state().current_intersection_priority = intersection.priority;
            let bucket = ctx.get_bucket();
            let state = bucket.state();
            let symbol = &state.symbol_instances[intersection.symbol_index];
            if symbol.cross_tile_id().is_some_and(|id| seen_cross_tile_ids.contains(&id)) {
                continue;
            }
            let projectors = ctx.projectors(&transform_state);
            let placement = self.place_symbol(intersection.symbol_index, symbol, &state, ctx, &projectors);
            if self.should_retry_placement(placement, ctx) {
                continue;
            }
            if let Some(id) = symbol.cross_tile_id() {
                seen_cross_tile_ids.insert(id);
            }
        }
        self.tile_state().intersections = intersections;
        // Place the rest labels.
        self.tile_state().populate_intersections = false;
        for layer in layers.iter().rev() {
            self.place_layer(&*layer.borrow(), &mut seen_cross_tile_ids);
        }
        self.tile_state().seen_cross_tile_ids = seen_cross_tile_ids;
        self.commit();
    }

    fn tile_place_symbol_bucket(&mut self, params: &BucketPlacementData, seen: &mut BTreeSet<NonZeroU32>) {
        /// Keeps the data necessary to find a feature location according to a tile.
        struct NeighborTileData {
            shift: Vec2,
            matrix: Mat4,
            borders: CollisionBoundaries,
        }

        debug_assert!(self.update_parameters.is_some());
        let Some(bucket) = symbol_bucket(&params.bucket) else {
            return;
        };
        let layout = &*bucket.layout;
        if !self.tile_state().populate_intersections {
            self.base_place_symbol_bucket(params, seen);
            return;
        }
        if *layout.symbol_placement.uniform() != SymbolPlacementType::Point || *layout.symbol_avoid_edges.uniform() {
            // Collect intersection only for point placement.
            return;
        }
        let render_tile = params.tile();
        let collision_group = self.collision_groups.get(&params.source_id);
        let avoid_edges = self.get_avoid_edges(bucket, &render_tile.matrix);
        let ctx = Rc::new(PlacementContext::new(
            params,
            self.collision_index.get_transform_state(),
            self.placement_zoom,
            collision_group,
            avoid_edges,
        ));

        let state = bucket.state();
        // In this case we first try to place symbols, which intersects the tile
        // borders, so that those symbols will remain even if each tile is handled
        // independently.
        let symbol_instances = get_bucket_symbols(
            bucket,
            &state,
            params.sort_key_range.as_ref(),
            self.collision_index.get_transform_state().get_bearing(),
        );

        let neighbor_tile_data = |collision_index: &CollisionIndex, id: UnwrappedTileID, shift: Vec2| {
            let m = collision_index.get_transform_state().get_projection_data(&id).fallback_matrix;
            let borders = collision_index.project_tile_boundaries(&m);
            NeighborTileData { shift, matrix: m, borders }
        };

        let z = render_tile.id.canonical.z;
        let x = render_tile.id.canonical.x;
        let y = render_tile.id.canonical.y;
        let extent = EXTENT as f32;
        // `y - 1` etc. are uint32_t arithmetic, converted to int64_t.
        let neighbours = [
            neighbor_tile_data(
                &self.collision_index,
                UnwrappedTileID::new(z, i64::from(x), i64::from(y.wrapping_sub(1))),
                Vec2::new(0.0, extent),
            ), // top
            neighbor_tile_data(
                &self.collision_index,
                UnwrappedTileID::new(z, i64::from(x), i64::from(y.wrapping_add(1))),
                Vec2::new(0.0, -extent),
            ), // bottom
            neighbor_tile_data(
                &self.collision_index,
                UnwrappedTileID::new(z, i64::from(x.wrapping_sub(1)), i64::from(y)),
                Vec2::new(extent, 0.0),
            ), // left
            neighbor_tile_data(
                &self.collision_index,
                UnwrappedTileID::new(z, i64::from(x.wrapping_add(1)), i64::from(y)),
                Vec2::new(-extent, 0.0),
            ), // right
        ];

        let tile_borders = self.tile_state().tile_borders.expect("tileBorders");
        let collision_index = &self.collision_index;
        let collision_box_intersects_tile_edges = |collision_box: &CollisionBox, shift: Vec2| -> IntersectStatus {
            let mut intersects = collision_index.intersects_tile_edges(
                collision_box,
                shift,
                &render_tile.matrix,
                ctx.pixel_ratio,
                &tile_borders,
            );
            // Check if this symbol intersects the neighbor tile borders. If so, it
            // also shall be placed with priority.
            for neighbor in &neighbours {
                if intersects.flags != IntersectStatus::NONE {
                    break;
                }
                intersects = collision_index.intersects_tile_edges(
                    collision_box,
                    shift + neighbor.shift,
                    &neighbor.matrix,
                    ctx.pixel_ratio,
                    &neighbor.borders,
                );
            }
            intersects
        };

        let pitch_text_with_map = ctx.pitch_text_with_map;
        let rotate_text_with_map = ctx.rotate_text_with_map;
        let variable_icon_placement = ctx.has_icon_text_fit && ctx.icon_overlap_mode == OverlapMode::Never;
        let bearing = ctx.bearing as f32;
        let instance_pools = &state.symbol_instance_pools;
        let symbol_intersects_tile_edges = |symbol: &SymbolInstance| -> IntersectStatus {
            let mut result = IntersectStatus::default();
            let mut variable_anchor: Option<SymbolAnchorType> = None;
            let text_variable_anchor_offset = symbol.get_text_variable_anchor_offset();
            if let Some(offsets) = text_variable_anchor_offset
                && !offsets.is_empty()
            {
                variable_anchor = offsets.iter().next().map(|pair| pair.anchor_type);
            }

            if let Some(text_collision_box) = instance_pools.boxes(symbol.get_text_collision_feature()).first() {
                let mut offset = Vec2::ZERO;
                if let Some(variable_anchor) = variable_anchor
                    && let Some(text_variable_anchor_offset) = &text_variable_anchor_offset
                {
                    let width = text_collision_box.x2 - text_collision_box.x1;
                    let height = text_collision_box.y2 - text_collision_box.y1;

                    let variable_text_offset = text_variable_anchor_offset.get_offset_by_anchor(&variable_anchor);

                    offset = calculate_variable_layout_offset(
                        variable_anchor,
                        width,
                        height,
                        variable_text_offset,
                        symbol.get_text_box_scale(),
                        rotate_text_with_map,
                        pitch_text_with_map,
                        bearing,
                    );
                }
                result = collision_box_intersects_tile_edges(text_collision_box, offset);
            }

            if let Some(icon_collision_box) = instance_pools.boxes(symbol.get_icon_collision_feature()).first() {
                let mut offset = Vec2::ZERO;
                if let Some(variable_anchor) = variable_anchor
                    && variable_icon_placement
                    && let Some(text_variable_anchor_offset) = &text_variable_anchor_offset
                {
                    let width = icon_collision_box.x2 - icon_collision_box.x1;
                    let height = icon_collision_box.y2 - icon_collision_box.y1;

                    let variable_text_offset = text_variable_anchor_offset.get_offset_by_anchor(&variable_anchor);

                    offset = calculate_variable_layout_offset(
                        variable_anchor,
                        width,
                        height,
                        variable_text_offset,
                        symbol.get_text_box_scale(),
                        rotate_text_with_map,
                        pitch_text_with_map,
                        bearing,
                    );
                }
                let icon_intersects = collision_box_intersects_tile_edges(icon_collision_box, offset);
                result.flags |= icon_intersects.flags;
                result.min_section_length = result.min_section_length.max(icon_intersects.min_section_length);
            }

            result
        };

        let mut new_intersections: Vec<Intersection> = Vec::new();
        let priority = match &self.kind {
            PlacementKind::Tile(tile) => tile.current_intersection_priority,
            _ => unreachable!("TilePlacement"),
        };
        for symbol_index in symbol_instances {
            let symbol = &state.symbol_instances[symbol_index];
            let intersect_status = symbol_intersects_tile_edges(symbol);
            if intersect_status.flags == IntersectStatus::NONE {
                continue;
            }
            new_intersections.push(Intersection {
                ctx: Rc::clone(&ctx),
                symbol_index,
                anchor_point: symbol.get_anchor().point,
                status: intersect_status,
                priority,
            });
        }
        drop(state);

        let tile = self.tile_state();
        tile.intersections.extend(new_intersections);
        tile.current_intersection_priority += 1;
    }
}

/// Places the bucket's symbols. Only symbol buckets have any.
pub(crate) fn place_bucket(
    bucket: &dyn Bucket,
    placement: &mut Placement,
    data: &BucketPlacementData,
    seen_ids: &mut BTreeSet<NonZeroU32>,
) {
    if let Some(symbol_bucket) = bucket.downcast_ref::<SymbolBucket>() {
        symbol_bucket.place(placement, data, seen_ids);
    }
}

/// Updates the bucket's vertices for `placement`. Only symbol buckets have
/// any to update.
pub(crate) fn update_vertices(
    bucket: &dyn Bucket,
    placement: &Placement,
    update_opacities: bool,
    state: &TransformState,
    data: &BucketPlacementData,
    seen_ids: &mut BTreeSet<NonZeroU32>,
) {
    if let Some(symbol_bucket) = bucket.downcast_ref::<SymbolBucket>() {
        symbol_bucket.update_vertices(placement, update_opacities, state, data, seen_ids);
    }
}

impl SymbolBucket {
    #[expect(clippy::unused_self, reason = "a bucket places its symbols, as the C++ `Placement` asks each bucket")]
    pub(crate) fn place(
        &self,
        placement: &mut Placement,
        data: &BucketPlacementData,
        seen_ids: &mut BTreeSet<NonZeroU32>,
    ) {
        placement.place_symbol_bucket(data, seen_ids);
    }

    pub(crate) fn update_vertices(
        &self,
        placement: &Placement,
        update_opacities: bool,
        state: &TransformState,
        data: &BucketPlacementData,
        seen_ids: &mut BTreeSet<NonZeroU32>,
    ) {
        if update_opacities {
            placement.update_bucket_opacities(self, state, seen_ids);
        }

        let dynamic_updated = placement.update_bucket_dynamic_attribute_data(self, state, data, None);

        // `uploaded = false` for either change, then the buffers'
        // `updateModified()` when not uploaded.
        self.mark_vertices_updated(update_opacities, dynamic_updated);
    }

    /// Lays out the positions a layer sharing this bucket draws at its own
    /// translation, from the leader's placement.
    pub(crate) fn update_layer_vertices(
        &self,
        placement: &Placement,
        state: &TransformState,
        data: &BucketPlacementData,
        layer_id: &str,
    ) {
        if !placement.update_bucket_dynamic_attribute_data(self, state, data, Some(layer_id)) {
            return;
        }
        // The layer's positions' `updateModified()`: drawables refresh their
        // dynamic attributes.
        self.dynamic_version.fetch_add(1, std::sync::atomic::Ordering::SeqCst);
        self.bump_version();
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn opacity_state() {
        let state = OpacityState::new(true, true);
        assert_eq!(state.opacity, 1.0);
        let faded = OpacityState::from_prev(OpacityState::new(false, false), 0.5, false);
        assert_eq!(faded.opacity, 0.0);
        assert!(faded.is_hidden());
        let fading_in = OpacityState::from_prev(OpacityState::new(true, false), 0.25, true);
        assert_eq!(fading_in.opacity, 0.25);
    }
}