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
#[cfg(feature = "use-dyn-lib")]
use {crate::LIB, std::ffi::CStr};

use super::*;
use crate::{
    block::block_from_structure,
    column::ColInfo,
    site2::util::Dir,
    util::{RandomField, Sampler},
    CanvasInfo, ColumnSample,
};
use common::{
    generation::EntityInfo,
    store::{Id, Store},
    terrain::{
        structure::{Structure as PrefabStructure, StructureBlock},
        Block, BlockKind, SpriteCfg,
    },
    vol::ReadVol,
};
use num::cast::AsPrimitive;
use std::{
    cell::RefCell,
    f32::consts::{PI, TAU},
    ops::RangeBounds,
    sync::Arc,
};
use vek::*;

#[allow(dead_code)]
pub enum Primitive {
    Empty, // Placeholder

    // Shapes
    Aabb(Aabb<i32>),
    Pyramid {
        aabb: Aabb<i32>,
        inset: i32,
    },
    Ramp {
        aabb: Aabb<i32>,
        inset: i32,
        dir: Dir,
    },
    Gable {
        aabb: Aabb<i32>,
        inset: i32,
        // X axis parallel or Y axis parallel
        dir: Dir,
    },
    Cylinder(Aabb<i32>),
    Cone(Aabb<i32>),
    Sphere(Aabb<i32>),
    /// An Aabb with rounded corners. The degree relates to how rounded the
    /// corners are. A value less than 1.0 results in concave faces. A value
    /// of 2.0 results in an ellipsoid. Values greater than 2.0 result in a
    /// rounded aabb. Values less than 0.0 are clamped to 0.0 as negative values
    /// would theoretically yield shapes extending to infinity.
    Superquadric {
        aabb: Aabb<i32>,
        degree: f32,
    },
    Plane(Aabr<i32>, Vec3<i32>, Vec2<f32>),
    /// A line segment from start to finish point with a given radius for both
    /// points
    Segment {
        segment: LineSegment3<f32>,
        r0: f32,
        r1: f32,
    },
    /// A prism created by projecting a line segment with a given radius along
    /// the z axis up to a provided height
    SegmentPrism {
        segment: LineSegment3<f32>,
        radius: f32,
        height: f32,
    },
    /// A sampling function is always a subset of another primitive to avoid
    /// needing infinite bounds
    Sampling(Id<Primitive>, Box<dyn Fn(Vec3<i32>) -> bool>),
    ColSampling(Id<Primitive>, Box<dyn Fn(Vec3<i32>, &ColInfo) -> bool>),
    Prefab(Box<PrefabStructure>),

    // Combinators
    Intersect(Id<Primitive>, Id<Primitive>),
    Union(Id<Primitive>, Id<Primitive>),
    // Not commutative
    Without(Id<Primitive>, Id<Primitive>),
    // Operators
    Translate(Id<Primitive>, Vec3<i32>),
    Scale(Id<Primitive>, Vec3<f32>),
    RotateAbout(Id<Primitive>, Mat3<i32>, Vec3<f32>),
    /// Repeat a primitive a number of times in a given direction, overlapping
    /// between repeats are unspecified.
    Repeat(Id<Primitive>, Vec3<i32>, u32),
}

impl std::fmt::Debug for Primitive {
    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
        match self {
            Primitive::Empty => f.debug_tuple("Empty").finish(),
            Primitive::Aabb(aabb) => f.debug_tuple("Aabb").field(&aabb).finish(),
            Primitive::Pyramid { aabb, inset } => {
                f.debug_tuple("Pyramid").field(&aabb).field(&inset).finish()
            },
            Primitive::Ramp { aabb, inset, dir } => f
                .debug_tuple("Ramp")
                .field(&aabb)
                .field(&inset)
                .field(&dir)
                .finish(),
            Primitive::Gable { aabb, inset, dir } => f
                .debug_tuple("Gable")
                .field(&aabb)
                .field(&inset)
                .field(&dir)
                .finish(),
            Primitive::Cylinder(aabb) => f.debug_tuple("Cylinder").field(&aabb).finish(),
            Primitive::Cone(aabb) => f.debug_tuple("Cone").field(&aabb).finish(),
            Primitive::Sphere(aabb) => f.debug_tuple("Sphere").field(&aabb).finish(),
            Primitive::Superquadric { aabb, degree } => f
                .debug_tuple("Superquadric")
                .field(&aabb)
                .field(&degree)
                .finish(),
            Primitive::Plane(aabr, origin, gradient) => f
                .debug_tuple("Plane")
                .field(&aabr)
                .field(&origin)
                .field(&gradient)
                .finish(),
            Primitive::Segment { segment, r0, r1 } => f
                .debug_tuple("Segment")
                .field(&segment)
                .field(&r0)
                .field(&r1)
                .finish(),
            Primitive::SegmentPrism {
                segment,
                radius,
                height,
            } => f
                .debug_tuple("SegmentPrism")
                .field(&segment)
                .field(&radius)
                .field(&height)
                .finish(),
            Primitive::Sampling(prim, _) => f.debug_tuple("Sampling").field(&prim).finish(),
            Primitive::ColSampling(prim, _) => f.debug_tuple("ColSampling").field(&prim).finish(),
            Primitive::Prefab(prefab) => f.debug_tuple("Prefab").field(&prefab).finish(),
            Primitive::Intersect(a, b) => f.debug_tuple("Intersect").field(&a).field(&b).finish(),
            Primitive::Union(a, b) => f.debug_tuple("Union").field(&a).field(&b).finish(),
            Primitive::Without(a, b) => f.debug_tuple("Without").field(&a).field(&b).finish(),
            Primitive::Translate(a, vec) => {
                f.debug_tuple("Translate").field(&a).field(&vec).finish()
            },
            Primitive::Scale(a, vec) => f.debug_tuple("Scale").field(&a).field(&vec).finish(),
            Primitive::RotateAbout(a, mat, vec) => f
                .debug_tuple("RotateAbout")
                .field(&a)
                .field(&mat)
                .field(&vec)
                .finish(),
            Primitive::Repeat(a, offset, n) => f
                .debug_tuple("Repeat")
                .field(&a)
                .field(&offset)
                .field(&n)
                .finish(),
        }
    }
}

impl Primitive {
    pub fn intersect(a: impl Into<Id<Primitive>>, b: impl Into<Id<Primitive>>) -> Self {
        Self::Intersect(a.into(), b.into())
    }

    pub fn union(a: impl Into<Id<Primitive>>, b: impl Into<Id<Primitive>>) -> Self {
        Self::Union(a.into(), b.into())
    }

    pub fn without(a: impl Into<Id<Primitive>>, b: impl Into<Id<Primitive>>) -> Self {
        Self::Without(a.into(), b.into())
    }

    pub fn sampling(a: impl Into<Id<Primitive>>, f: Box<dyn Fn(Vec3<i32>) -> bool>) -> Self {
        Self::Sampling(a.into(), f)
    }

    pub fn column_sampling(
        a: impl Into<Id<Primitive>>,
        f: Box<dyn Fn(Vec3<i32>, &ColInfo) -> bool>,
    ) -> Self {
        Self::ColSampling(a.into(), f)
    }

    pub fn translate(a: impl Into<Id<Primitive>>, trans: Vec3<i32>) -> Self {
        Self::Translate(a.into(), trans)
    }

    pub fn scale(a: impl Into<Id<Primitive>>, scale: Vec3<f32>) -> Self {
        Self::Scale(a.into(), scale)
    }

    pub fn rotate_about(
        a: impl Into<Id<Primitive>>,
        rot: Mat3<i32>,
        point: Vec3<impl AsPrimitive<f32>>,
    ) -> Self {
        Self::RotateAbout(a.into(), rot, point.as_())
    }

    /// Rotates a primitive 90 degrees CCW about the Z axis `n` times
    pub fn rotate_z_90_about(
        a: impl Into<Id<Primitive>>,
        n: i32,
        point: Vec3<impl AsPrimitive<f32>>,
    ) -> Self {
        let rot = match n % 4 {
            1 => Mat3::new(0, -1, 0, 1, 0, 0, 0, 0, 1),
            2 => Mat3::new(-1, 0, 0, 0, -1, 0, 0, 0, 1),
            3 => Mat3::new(0, 1, 0, -1, 0, 0, 0, 0, 1),
            _ => Mat3::new(1, 0, 0, 0, 1, 0, 0, 0, 1),
        };
        Self::RotateAbout(a.into(), rot, point.as_())
    }

    pub fn repeat(a: impl Into<Id<Primitive>>, offset: Vec3<i32>, count: u32) -> Self {
        Self::Repeat(a.into(), offset, count)
    }
}

#[derive(Clone)]
pub enum Fill {
    Sprite(Block),
    ResourceSprite(Block),
    CfgSprite(Block, SpriteCfg),

    Block(Block),
    Brick(BlockKind, Rgb<u8>, u8),
    Gradient(util::gradient::Gradient, BlockKind),
    // TODO: the offset field for Prefab is a hack that breaks the compositionality of Translate,
    // we probably need an evaluator for the primitive tree that gets which point is queried at
    // leaf nodes given an input point to make Translate/Rotate work generally
    Prefab(Box<PrefabStructure>, Vec3<i32>, u32),
    Sampling(Arc<dyn Fn(Vec3<i32>) -> Option<Block>>),
}

impl Fill {
    pub fn sprite(kind: SpriteKind) -> Self { Fill::Block(Block::empty().with_sprite(kind)) }

    pub fn sprite_ori(kind: SpriteKind, ori: u8) -> Self {
        let block = Block::empty().with_sprite(kind);

        let block = block.with_ori(ori).unwrap_or(block);
        Fill::Sprite(block)
    }

    pub fn resource_sprite(kind: SpriteKind) -> Self {
        Fill::ResourceSprite(Block::empty().with_sprite(kind))
    }

    pub fn resource_sprite_ori(kind: SpriteKind, ori: u8) -> Self {
        let block = Block::empty().with_sprite(kind);

        let block = block.with_ori(ori).unwrap_or(block);
        Fill::ResourceSprite(block)
    }

    pub fn owned_resource_sprite_ori(kind: SpriteKind, ori: u8) -> Self {
        let block = Block::empty().with_sprite(kind);

        let block = block.with_ori(ori).unwrap_or(block);
        let block = block
            .with_attr(common::terrain::sprite::Owned(true))
            .unwrap_or(block);

        Fill::ResourceSprite(block)
    }

    pub fn sprite_ori_cfg(kind: SpriteKind, ori: u8, cfg: SpriteCfg) -> Self {
        let block = Block::empty().with_sprite(kind);

        let block = block.with_ori(ori).unwrap_or(block);
        Fill::CfgSprite(block, cfg)
    }

    fn contains_at(
        tree: &Store<Primitive>,
        prim: Id<Primitive>,
        pos: Vec3<i32>,
        col: &ColInfo,
    ) -> bool {
        // Custom closure because vek's impl of `contains_point` is inclusive :(
        let aabb_contains = |aabb: Aabb<i32>, pos: Vec3<i32>| {
            (aabb.min.x..aabb.max.x).contains(&pos.x)
                && (aabb.min.y..aabb.max.y).contains(&pos.y)
                && (aabb.min.z..aabb.max.z).contains(&pos.z)
        };

        match &tree[prim] {
            Primitive::Empty => false,

            Primitive::Aabb(aabb) => aabb_contains(*aabb, pos),
            Primitive::Ramp { aabb, inset, dir } => {
                let inset = (*inset).max(aabb.size().reduce_min());
                let inner = match dir {
                    Dir::X => Aabr {
                        min: Vec2::new(aabb.min.x - 1 + inset, aabb.min.y),
                        max: Vec2::new(aabb.max.x, aabb.max.y),
                    },
                    Dir::NegX => Aabr {
                        min: Vec2::new(aabb.min.x, aabb.min.y),
                        max: Vec2::new(aabb.max.x - inset, aabb.max.y),
                    },
                    Dir::Y => Aabr {
                        min: Vec2::new(aabb.min.x, aabb.min.y - 1 + inset),
                        max: Vec2::new(aabb.max.x, aabb.max.y),
                    },
                    Dir::NegY => Aabr {
                        min: Vec2::new(aabb.min.x, aabb.min.y),
                        max: Vec2::new(aabb.max.x, aabb.max.y - inset),
                    },
                };
                aabb_contains(*aabb, pos)
                    && (inner.projected_point(pos.xy()) - pos.xy())
                        .map(|e| e.abs())
                        .reduce_max() as f32
                        / (inset as f32)
                        < 1.0
                            - ((pos.z - aabb.min.z) as f32 + 0.5) / (aabb.max.z - aabb.min.z) as f32
            },
            Primitive::Pyramid { aabb, inset } => {
                let inset = (*inset).max(aabb.size().reduce_min());
                let inner = Aabr {
                    min: aabb.min.xy() - 1 + inset,
                    max: aabb.max.xy() - inset,
                };
                aabb_contains(*aabb, pos)
                    && (inner.projected_point(pos.xy()) - pos.xy())
                        .map(|e| e.abs())
                        .reduce_max() as f32
                        / (inset as f32)
                        < 1.0
                            - ((pos.z - aabb.min.z) as f32 + 0.5) / (aabb.max.z - aabb.min.z) as f32
            },
            Primitive::Gable { aabb, inset, dir } => {
                let inset = (*inset).max(aabb.size().reduce_min());
                let inner = if dir.is_y() {
                    Aabr {
                        min: Vec2::new(aabb.min.x - 1 + inset, aabb.min.y),
                        max: Vec2::new(aabb.max.x - inset, aabb.max.y),
                    }
                } else {
                    Aabr {
                        min: Vec2::new(aabb.min.x, aabb.min.y - 1 + inset),
                        max: Vec2::new(aabb.max.x, aabb.max.y - inset),
                    }
                };
                aabb_contains(*aabb, pos)
                    && (inner.projected_point(pos.xy()) - pos.xy())
                        .map(|e| e.abs())
                        .reduce_max() as f32
                        / (inset as f32)
                        < 1.0
                            - ((pos.z - aabb.min.z) as f32 + 0.5) / (aabb.max.z - aabb.min.z) as f32
            },
            Primitive::Cylinder(aabb) => {
                // Add 0.5 since the aabb is exclusive.
                let fpos = pos.as_::<f32>().xy() - aabb.as_::<f32>().center().xy() + 0.5;
                let size = Vec3::from(aabb.size().as_::<f32>()).xy();
                (aabb.min.z..aabb.max.z).contains(&pos.z)
                    && (2.0 * fpos / size).magnitude_squared() <= 1.0
            },
            Primitive::Cone(aabb) => {
                (aabb.min.z..aabb.max.z).contains(&pos.z)
                    && pos
                        .xy()
                        .as_()
                        .distance_squared(aabb.as_().center().xy() - 0.5)
                        < (((aabb.max.z - pos.z) as f32 / aabb.size().d as f32)
                            * (aabb.size().w.min(aabb.size().h) as f32 / 2.0))
                            .powi(2)
            },
            Primitive::Sphere(aabb) => {
                aabb_contains(*aabb, pos)
                    && pos.as_().distance_squared(aabb.as_().center() - 0.5)
                        < (aabb.size().w.min(aabb.size().h) as f32 / 2.0).powi(2)
            },
            Primitive::Superquadric { aabb, degree } => {
                let degree = degree.max(0.0);
                let center = aabb.center().map(|e| e as f32);
                let a: f32 = aabb.max.x as f32 - center.x - 0.5;
                let b: f32 = aabb.max.y as f32 - center.y - 0.5;
                let c: f32 = aabb.max.z as f32 - center.z - 0.5;
                let rpos = pos.as_::<f32>() + 0.5 - center;
                aabb_contains(*aabb, pos)
                    && (rpos.x / a).abs().powf(degree)
                        + (rpos.y / b).abs().powf(degree)
                        + (rpos.z / c).abs().powf(degree)
                        < 1.0
            },
            Primitive::Plane(aabr, origin, gradient) => {
                // Maybe <= instead of ==
                (aabr.min.x..aabr.max.x).contains(&pos.x)
                    && (aabr.min.y..aabr.max.y).contains(&pos.y)
                    && pos.z
                        == origin.z
                            + ((pos.xy() - origin.xy())
                                .map(|x| x.abs())
                                .as_()
                                .dot(*gradient) as i32)
            },
            // TODO: Aabb calculation could be improved here by only considering the relevant radius
            Primitive::Segment { segment, r0, r1 } => {
                let distance = segment.end - segment.start;
                let length = pos - segment.start.as_();
                let t =
                    (length.as_().dot(distance) / distance.magnitude_squared()).clamped(0.0, 1.0);
                segment.distance_to_point(pos.map(|e| e as f32)) < Lerp::lerp(r0, r1, t) - 0.25
            },
            Primitive::SegmentPrism {
                segment,
                radius,
                height,
            } => {
                let segment_2d = LineSegment2 {
                    start: segment.start.xy(),
                    end: segment.end.xy(),
                };
                let projected_point_2d: Vec2<f32> =
                    segment_2d.as_().projected_point(pos.xy().as_());
                let xy_check = projected_point_2d.distance(pos.xy().as_()) < radius - 0.25;
                let projected_z = {
                    let len_sq: f32 = segment_2d
                        .start
                        .as_()
                        .distance_squared(segment_2d.end.as_());
                    if len_sq < 0.1 {
                        segment.start.z
                    } else {
                        let frac = ((projected_point_2d - segment_2d.start.as_())
                            .dot(segment_2d.end.as_() - segment_2d.start.as_())
                            / len_sq)
                            .clamp(0.0, 1.0);
                        (segment.end.z - segment.start.z) * frac + segment.start.z
                    }
                };
                let z_check = (projected_z..=(projected_z + height)).contains(&(pos.z as f32));
                xy_check && z_check
            },
            Primitive::Sampling(a, f) => Self::contains_at(tree, *a, pos, col) && f(pos),
            Primitive::ColSampling(a, f) => Self::contains_at(tree, *a, pos, col) && f(pos, col),
            Primitive::Prefab(p) => !matches!(p.get(pos), Err(_) | Ok(StructureBlock::None)),
            Primitive::Intersect(a, b) => {
                Self::contains_at(tree, *a, pos, col) && Self::contains_at(tree, *b, pos, col)
            },
            Primitive::Union(a, b) => {
                Self::contains_at(tree, *a, pos, col) || Self::contains_at(tree, *b, pos, col)
            },
            Primitive::Without(a, b) => {
                Self::contains_at(tree, *a, pos, col) && !Self::contains_at(tree, *b, pos, col)
            },
            Primitive::Translate(prim, vec) => {
                Self::contains_at(tree, *prim, pos.map2(*vec, i32::saturating_sub), col)
            },
            Primitive::Scale(prim, vec) => {
                let center = Self::get_bounds(tree, *prim).as_::<f32>().center();
                let fpos = pos.as_::<f32>();
                let spos = (center + ((fpos - center) / vec))
                    .map(|x| x.round())
                    .as_::<i32>();
                Self::contains_at(tree, *prim, spos, col)
            },
            Primitive::RotateAbout(prim, mat, vec) => {
                let mat = mat.as_::<f32>().transposed();
                let vec = vec - 0.5;
                Self::contains_at(
                    tree,
                    *prim,
                    (vec + mat * (pos.as_::<f32>() - vec)).as_(),
                    col,
                )
            },
            Primitive::Repeat(prim, offset, count) => {
                if count == &0 {
                    false
                } else {
                    let count = count - 1;
                    let aabb = Self::get_bounds(tree, *prim);
                    let aabb_corner = {
                        let min_red = aabb.min.map2(*offset, |a, b| if b < 0 { 0 } else { a });
                        let max_red = aabb.max.map2(*offset, |a, b| if b < 0 { a } else { 0 });
                        min_red + max_red
                    };
                    let diff = pos - aabb_corner;
                    let min = diff
                        .map2(*offset, |a, b| if b == 0 { i32::MAX } else { a / b })
                        .reduce_min()
                        .clamp(0, count as i32);
                    let pos = pos - offset * min;
                    Self::contains_at(tree, *prim, pos, col)
                }
            },
        }
    }

    pub fn sample_at(
        &self,
        tree: &Store<Primitive>,
        prim: Id<Primitive>,
        pos: Vec3<i32>,
        canvas_info: &CanvasInfo,
        old_block: Block,
        sprite_cfg: &mut Option<SpriteCfg>,
        col: &ColInfo,
    ) -> Option<Block> {
        if Self::contains_at(tree, prim, pos, col) {
            match self {
                Fill::Sprite(sprite) | Fill::ResourceSprite(sprite) => {
                    Some(if old_block.is_filled() {
                        *sprite
                    } else {
                        old_block.with_data_of(*sprite)
                    })
                },
                Fill::CfgSprite(sprite, cfg) => {
                    *sprite_cfg = Some(cfg.clone());
                    Some(if old_block.is_filled() {
                        *sprite
                    } else {
                        old_block.with_data_of(*sprite)
                    })
                },
                Fill::Block(block) => Some(*block),
                Fill::Brick(bk, col, range) => Some(Block::new(
                    *bk,
                    *col + (RandomField::new(13)
                        .get((pos + Vec3::new(pos.z, pos.z, 0)) / Vec3::new(2, 2, 1))
                        % *range as u32) as u8,
                )),
                Fill::Gradient(gradient, bk) => Some(Block::new(*bk, gradient.sample(pos.as_()))),
                Fill::Prefab(p, tr, seed) => p.get(pos - tr).ok().and_then(|sb| {
                    let col_sample = canvas_info.col(canvas_info.wpos)?;
                    block_from_structure(
                        canvas_info.index,
                        sb,
                        pos - tr,
                        p.get_bounds().center().xy(),
                        *seed,
                        col_sample,
                        Block::air,
                        canvas_info.calendar(),
                        &Vec2::new(Vec2::new(1, 0), Vec2::new(0, 1)),
                    )
                    .map(|(block, cfg)| {
                        *sprite_cfg = cfg;
                        block
                    })
                }),
                Fill::Sampling(f) => f(pos),
            }
        } else {
            None
        }
    }

    fn get_bounds_inner(tree: &Store<Primitive>, prim: Id<Primitive>) -> Vec<Aabb<i32>> {
        fn or_zip_with<T, F: FnOnce(T, T) -> T>(a: Option<T>, b: Option<T>, f: F) -> Option<T> {
            match (a, b) {
                (Some(a), Some(b)) => Some(f(a, b)),
                (Some(a), _) => Some(a),
                (_, b) => b,
            }
        }

        match &tree[prim] {
            Primitive::Empty => vec![],
            Primitive::Aabb(aabb) => vec![*aabb],
            Primitive::Pyramid { aabb, .. } => vec![*aabb],
            Primitive::Gable { aabb, .. } => vec![*aabb],
            Primitive::Ramp { aabb, .. } => vec![*aabb],
            Primitive::Cylinder(aabb) => vec![*aabb],
            Primitive::Cone(aabb) => vec![*aabb],
            Primitive::Sphere(aabb) => vec![*aabb],
            Primitive::Superquadric { aabb, .. } => vec![*aabb],
            Primitive::Plane(aabr, origin, gradient) => {
                let half_size = aabr.half_size().reduce_max();
                let longest_dist = ((aabr.center() - origin.xy()).map(|x| x.abs())
                    + half_size
                    + aabr.size().reduce_max() % 2)
                    .map(|x| x as f32);
                let z = if gradient.x.signum() == gradient.y.signum() {
                    Vec2::new(0, longest_dist.dot(*gradient) as i32)
                } else {
                    (longest_dist * gradient).as_()
                };
                let aabb = Aabb {
                    min: aabr.min.with_z(origin.z + z.reduce_min().min(0)),
                    max: aabr.max.with_z(origin.z + z.reduce_max().max(0)),
                };
                vec![aabb.made_valid()]
            },
            Primitive::Segment { segment, r0, r1 } => {
                let aabb = Aabb {
                    min: segment.start,
                    max: segment.end,
                }
                .made_valid();
                vec![Aabb {
                    min: (aabb.min - r0.max(*r1)).floor().as_(),
                    max: (aabb.max + r0.max(*r1)).ceil().as_(),
                }]
            },
            Primitive::SegmentPrism {
                segment,
                radius,
                height,
            } => {
                let aabb = Aabb {
                    min: segment.start,
                    max: segment.end,
                }
                .made_valid();
                let min = {
                    let xy = (aabb.min.xy() - *radius).floor();
                    xy.with_z(aabb.min.z).as_()
                };
                let max = {
                    let xy = (aabb.max.xy() + *radius).ceil();
                    xy.with_z((aabb.max.z + *height).ceil()).as_()
                };
                vec![Aabb { min, max }]
            },
            Primitive::Sampling(a, _) | Primitive::ColSampling(a, _) => {
                Self::get_bounds_inner(tree, *a)
            },
            Primitive::Prefab(p) => vec![p.get_bounds()],
            Primitive::Intersect(a, b) => or_zip_with(
                Self::get_bounds_opt(tree, *a),
                Self::get_bounds_opt(tree, *b),
                |a, b| a.intersection(b),
            )
            .into_iter()
            .collect(),

            Primitive::Union(a, b) => {
                fn jaccard(x: Aabb<i32>, y: Aabb<i32>) -> f32 {
                    let s_intersection = x.intersection(y).size().as_::<f32>().magnitude();
                    let s_union = x.union(y).size().as_::<f32>().magnitude();
                    s_intersection / s_union
                }
                let mut inputs = Vec::new();
                inputs.extend(Self::get_bounds_inner(tree, *a));
                inputs.extend(Self::get_bounds_inner(tree, *b));
                let mut results = Vec::new();
                if let Some(aabb) = inputs.pop() {
                    results.push(aabb);
                    for a in &inputs {
                        let best = results
                            .iter()
                            .enumerate()
                            .max_by_key(|(_, b)| (jaccard(*a, **b) * 1000.0) as usize);
                        match best {
                            Some((i, b)) if jaccard(*a, *b) > 0.3 => {
                                let mut aabb = results.swap_remove(i);
                                aabb = aabb.union(*a);
                                results.push(aabb);
                            },
                            _ => results.push(*a),
                        }
                    }
                    results
                } else {
                    results
                }
            },
            Primitive::Without(a, _) => Self::get_bounds_inner(tree, *a),
            Primitive::Translate(prim, vec) => Self::get_bounds_inner(tree, *prim)
                .into_iter()
                .map(|aabb| Aabb {
                    min: aabb.min.map2(*vec, i32::saturating_add),
                    max: aabb.max.map2(*vec, i32::saturating_add),
                })
                .collect(),
            Primitive::Scale(prim, vec) => Self::get_bounds_inner(tree, *prim)
                .into_iter()
                .map(|aabb| {
                    let center = aabb.center();
                    Aabb {
                        min: center + ((aabb.min - center).as_::<f32>() * vec).as_::<i32>(),
                        max: center + ((aabb.max - center).as_::<f32>() * vec).as_::<i32>(),
                    }
                })
                .collect(),
            Primitive::RotateAbout(prim, mat, vec) => Self::get_bounds_inner(tree, *prim)
                .into_iter()
                .map(|aabb| {
                    let mat = mat.as_::<f32>();
                    // - 0.5 because we want the point to be at the minimum of the voxel
                    let vec = vec - 0.5;
                    let new_aabb = Aabb::<f32> {
                        min: vec + mat * (aabb.min.as_() - vec),
                        // - 1 becuase we want the AABB to be inclusive when we rotate it, we then
                        //   add 1 back to make it exclusive again
                        max: vec + mat * ((aabb.max - 1).as_() - vec),
                    }
                    .made_valid();
                    Aabb::<i32> {
                        min: new_aabb.min.as_(),
                        max: new_aabb.max.as_() + 1,
                    }
                })
                .collect(),
            Primitive::Repeat(prim, offset, count) => {
                if count == &0 {
                    vec![]
                } else {
                    let count = count - 1;
                    Self::get_bounds_inner(tree, *prim)
                        .into_iter()
                        .map(|aabb| Aabb {
                            min: aabb
                                .min
                                .map2(aabb.min + offset * count as i32, |a, b| a.min(b)),
                            max: aabb
                                .max
                                .map2(aabb.max + offset * count as i32, |a, b| a.max(b)),
                        })
                        .collect()
                }
            },
        }
    }

    pub fn get_bounds_disjoint(tree: &Store<Primitive>, prim: Id<Primitive>) -> Vec<Aabb<i32>> {
        Self::get_bounds_inner(tree, prim)
    }

    pub fn get_bounds_opt(tree: &Store<Primitive>, prim: Id<Primitive>) -> Option<Aabb<i32>> {
        Self::get_bounds_inner(tree, prim)
            .into_iter()
            .reduce(|a, b| a.union(b))
    }

    pub fn get_bounds(tree: &Store<Primitive>, prim: Id<Primitive>) -> Aabb<i32> {
        Self::get_bounds_opt(tree, prim).unwrap_or_else(|| Aabb::new_empty(Vec3::zero()))
    }
}

pub struct Painter {
    prims: RefCell<Store<Primitive>>,
    fills: RefCell<Vec<(Id<Primitive>, Fill)>>,
    entities: RefCell<Vec<EntityInfo>>,
    render_area: Aabr<i32>,
}

impl Painter {
    /// Computes the depth of the tree rooted at `prim`
    pub fn depth(&self, prim: Id<Primitive>) -> usize {
        fn aux(prims: &Store<Primitive>, prim: Id<Primitive>, prev_depth: usize) -> usize {
            match prims[prim] {
                Primitive::Empty
                | Primitive::Aabb(_)
                | Primitive::Pyramid { .. }
                | Primitive::Ramp { .. }
                | Primitive::Gable { .. }
                | Primitive::Cylinder(_)
                | Primitive::Cone(_)
                | Primitive::Sphere(_)
                | Primitive::Superquadric { .. }
                | Primitive::Plane(_, _, _)
                | Primitive::Segment { .. }
                | Primitive::SegmentPrism { .. }
                | Primitive::Prefab(_) => prev_depth,
                Primitive::Sampling(a, _)
                | Primitive::ColSampling(a, _)
                | Primitive::Translate(a, _)
                | Primitive::Scale(a, _)
                | Primitive::RotateAbout(a, _, _)
                | Primitive::Repeat(a, _, _) => aux(prims, a, 1 + prev_depth),

                Primitive::Intersect(a, b) | Primitive::Union(a, b) | Primitive::Without(a, b) => {
                    aux(prims, a, 1 + prev_depth).max(aux(prims, b, 1 + prev_depth))
                },
            }
        }
        let prims = self.prims.borrow();
        aux(&prims, prim, 0)
    }

    /// Orders two primitives by depth, since (A && (B && C)) is cheaper to
    /// evaluate than ((A && B) && C) due to short-circuiting.
    pub fn order_by_depth(
        &self,
        a: impl Into<Id<Primitive>>,
        b: impl Into<Id<Primitive>>,
    ) -> (Id<Primitive>, Id<Primitive>) {
        let (a, b) = (a.into(), b.into());
        if self.depth(a) < self.depth(b) {
            (a, b)
        } else {
            (b, a)
        }
    }

    /// Returns a `PrimitiveRef` of an axis aligned bounding box. The geometric
    /// name of this shape is a "right rectangular prism."
    pub fn aabb(&self, aabb: Aabb<i32>) -> PrimitiveRef {
        self.prim(Primitive::Aabb(aabb.made_valid()))
    }

    /// Returns a `PrimitiveRef` of a sphere using a radius check.
    pub fn sphere(&self, aabb: Aabb<i32>) -> PrimitiveRef {
        self.prim(Primitive::Sphere(aabb.made_valid()))
    }

    /// Returns a `PrimitiveRef` of a sphere using a radius check where a radius
    /// and origin are parameters instead of a bounding box.
    pub fn sphere_with_radius(&self, origin: Vec3<i32>, radius: f32) -> PrimitiveRef {
        let min = origin - Vec3::broadcast(radius.round() as i32);
        let max = origin + Vec3::broadcast(radius.round() as i32);
        self.prim(Primitive::Sphere(Aabb { min, max }))
    }

    /// Returns a `PrimitiveRef` of a sphere by returning an ellipsoid with
    /// congruent legs. The voxel artifacts are slightly different from the
    /// radius check `sphere()` method.
    pub fn sphere2(&self, aabb: Aabb<i32>) -> PrimitiveRef {
        let aabb = aabb.made_valid();
        let radius = aabb.size().w.min(aabb.size().h) / 2;
        let aabb = Aabb {
            min: aabb.center() - radius,
            max: aabb.center() + radius,
        };
        let degree = 2.0;
        self.prim(Primitive::Superquadric { aabb, degree })
    }

    /// Returns a `PrimitiveRef` of an ellipsoid by constructing a superquadric
    /// with a degree value of 2.0.
    pub fn ellipsoid(&self, aabb: Aabb<i32>) -> PrimitiveRef {
        let aabb = aabb.made_valid();
        let degree = 2.0;
        self.prim(Primitive::Superquadric { aabb, degree })
    }

    /// Returns a `PrimitiveRef` of a superquadric. A superquadric can be
    /// thought of as a rounded Aabb where the degree determines how rounded
    /// the corners are. Values from 0.0 to 1.0 produce concave faces or
    /// "inverse rounded corners." A value of 1.0 produces a stretched
    /// octahedron (or a non-stretched octahedron if the provided Aabb is a
    /// cube). Values from 1.0 to 2.0 produce an octahedron with convex
    /// faces. A degree of 2.0 produces an ellipsoid. Values larger than 2.0
    /// produce a rounded Aabb. The degree cannot be less than 0.0 without
    /// the shape extending to infinity.
    pub fn superquadric(&self, aabb: Aabb<i32>, degree: f32) -> PrimitiveRef {
        let aabb = aabb.made_valid();
        self.prim(Primitive::Superquadric { aabb, degree })
    }

    /// Returns a `PrimitiveRef` of a rounded Aabb by producing a superquadric
    /// with a degree value of 3.0.
    pub fn rounded_aabb(&self, aabb: Aabb<i32>) -> PrimitiveRef {
        let aabb = aabb.made_valid();
        self.prim(Primitive::Superquadric { aabb, degree: 3.0 })
    }

    /// Returns a `PrimitiveRef` of the largest cylinder that fits in the
    /// provided Aabb.
    pub fn cylinder(&self, aabb: Aabb<i32>) -> PrimitiveRef {
        self.prim(Primitive::Cylinder(aabb.made_valid()))
    }

    /// Returns a `PrimitiveRef` of the largest horizontal cylinder that fits in
    /// the provided Aabb.
    pub fn horizontal_cylinder(&self, aabb: Aabb<i32>, dir: Dir) -> PrimitiveRef {
        let aabr = Aabr::from(aabb);
        let length = dir.select(aabr.size());
        let height = aabb.max.z - aabb.min.z;
        let aabb = Aabb {
            min: (aabr.min - dir.abs().to_vec2() * height).with_z(aabb.min.z),
            max: (dir.abs().select_with(aabr.min, aabr.max)).with_z(aabb.min.z + length),
        };
        self.cylinder(aabb)
            .rotate_about((-dir.abs()).from_z_mat3(), aabr.min.with_z(aabb.min.z))
    }

    /// Returns a `PrimitiveRef` of a cylinder using a radius check where a
    /// radius and origin are parameters instead of a bounding box.
    pub fn cylinder_with_radius(
        &self,
        origin: Vec3<i32>,
        radius: f32,
        height: f32,
    ) -> PrimitiveRef {
        let min = origin - Vec2::broadcast(radius.round() as i32);
        let max = origin + Vec2::broadcast(radius.round() as i32).with_z(height.round() as i32);
        self.prim(Primitive::Cylinder(Aabb { min, max }))
    }

    /// Returns a `PrimitiveRef` of the largest cone that fits in the
    /// provided Aabb.
    pub fn cone(&self, aabb: Aabb<i32>) -> PrimitiveRef {
        self.prim(Primitive::Cone(aabb.made_valid()))
    }

    /// Returns a `PrimitiveRef` of a cone using a radius check where a radius
    /// and origin are parameters instead of a bounding box.
    pub fn cone_with_radius(&self, origin: Vec3<i32>, radius: f32, height: f32) -> PrimitiveRef {
        let min = origin - Vec2::broadcast(radius.round() as i32);
        let max = origin + Vec2::broadcast(radius.round() as i32).with_z(height.round() as i32);
        self.prim(Primitive::Cone(Aabb { min, max }))
    }

    /// Returns a `PrimitiveRef` of a 3-dimensional line segment with a provided
    /// radius.
    pub fn line(
        &self,
        a: Vec3<impl AsPrimitive<f32>>,
        b: Vec3<impl AsPrimitive<f32>>,
        radius: f32,
    ) -> PrimitiveRef {
        self.prim(Primitive::Segment {
            segment: LineSegment3 {
                start: a.as_(),
                end: b.as_(),
            },
            r0: radius,
            r1: radius,
        })
    }

    /// Returns a `PrimitiveRef` of a 3-dimensional line segment with two
    /// radius.
    pub fn line_two_radius(
        &self,
        a: Vec3<impl AsPrimitive<f32>>,
        b: Vec3<impl AsPrimitive<f32>>,
        r0: f32,
        r1: f32,
    ) -> PrimitiveRef {
        self.prim(Primitive::Segment {
            segment: LineSegment3 {
                start: a.as_(),
                end: b.as_(),
            },
            r0,
            r1,
        })
    }

    /// Returns a `PrimitiveRef` of a 3-dimensional line segment where the
    /// provided radius only affects the width of the shape. The height of
    /// the shape is determined by the `height` parameter. The height of the
    /// shape is extended upwards along the z axis from the line. The top and
    /// bottom of the shape are planar and parallel to each other and the line.
    pub fn segment_prism(
        &self,
        a: Vec3<impl AsPrimitive<f32>>,
        b: Vec3<impl AsPrimitive<f32>>,
        radius: f32,
        height: f32,
    ) -> PrimitiveRef {
        let segment = LineSegment3 {
            start: a.as_(),
            end: b.as_(),
        };
        self.prim(Primitive::SegmentPrism {
            segment,
            radius,
            height,
        })
    }

    /// Returns a `PrimitiveRef` of a 3-dimensional cubic bezier curve by
    /// dividing the curve into line segments with one segment approximately
    /// every length of 5 blocks.
    pub fn cubic_bezier(
        &self,
        start: Vec3<impl AsPrimitive<f32>>,
        ctrl0: Vec3<impl AsPrimitive<f32>>,
        ctrl1: Vec3<impl AsPrimitive<f32>>,
        end: Vec3<impl AsPrimitive<f32>>,
        radius: f32,
    ) -> PrimitiveRef {
        let bezier = CubicBezier3 {
            start: start.as_(),
            ctrl0: ctrl0.as_(),
            ctrl1: ctrl1.as_(),
            end: end.as_(),
        };
        let length = bezier.length_by_discretization(10);
        let num_segments = (0.2 * length).ceil() as u16;
        self.cubic_bezier_with_num_segments(bezier, radius, num_segments)
    }

    /// Returns a `PrimitiveRef` of a 3-dimensional cubic bezier curve by
    /// dividing the curve into `num_segments` line segments.
    pub fn cubic_bezier_with_num_segments(
        &self,
        bezier: CubicBezier3<f32>,
        radius: f32,
        num_segments: u16,
    ) -> PrimitiveRef {
        let mut bezier_prim = self.empty();
        let range: Vec<_> = (0..=num_segments).collect();
        range.windows(2).for_each(|w| {
            let segment_start = bezier.evaluate(w[0] as f32 / num_segments as f32);
            let segment_end = bezier.evaluate(w[1] as f32 / num_segments as f32);
            bezier_prim = bezier_prim.union(self.line(segment_start, segment_end, radius));
        });
        bezier_prim
    }

    /// Returns a `PrimitiveRef` of a 3-dimensional cubic bezier curve where the
    /// radius only governs the width of the curve. The height is governed
    /// by the `height` parameter where the shape extends upwards from the
    /// bezier curve by the value of `height`. The shape is constructed by
    /// dividing the curve into line segment prisms with one segment prism
    /// approximately every length of 5 blocks.
    pub fn cubic_bezier_prism(
        &self,
        start: Vec3<impl AsPrimitive<f32>>,
        ctrl0: Vec3<impl AsPrimitive<f32>>,
        ctrl1: Vec3<impl AsPrimitive<f32>>,
        end: Vec3<impl AsPrimitive<f32>>,
        radius: f32,
        height: f32,
    ) -> PrimitiveRef {
        let bezier = CubicBezier3 {
            start: start.as_(),
            ctrl0: ctrl0.as_(),
            ctrl1: ctrl1.as_(),
            end: end.as_(),
        };
        let length = bezier.length_by_discretization(10);
        let num_segments = (0.2 * length).ceil() as u16;
        self.cubic_bezier_prism_with_num_segments(bezier, radius, height, num_segments)
    }

    /// Returns a `PrimitiveRef` of a 3-dimensional cubic bezier curve where the
    /// radius only governs the width of the curve. The height is governed
    /// by the `height` parameter where the shape extends upwards from the
    /// bezier curve by the value of `height`. The shape is constructed by
    /// dividing the curve into `num_segments` line segment prisms.
    pub fn cubic_bezier_prism_with_num_segments(
        &self,
        bezier: CubicBezier3<f32>,
        radius: f32,
        height: f32,
        num_segments: u16,
    ) -> PrimitiveRef {
        let mut bezier_prim = self.empty();
        let range: Vec<_> = (0..=num_segments).collect();
        range.windows(2).for_each(|w| {
            let segment_start = bezier.evaluate(w[0] as f32 / num_segments as f32);
            let segment_end = bezier.evaluate(w[1] as f32 / num_segments as f32);
            bezier_prim =
                bezier_prim.union(self.segment_prism(segment_start, segment_end, radius, height));
        });
        bezier_prim
    }

    /// Returns a `PrimitiveRef` of a plane. The Aabr provides the bounds for
    /// the plane in the xy plane and the gradient determines its slope through
    /// the dot product. A gradient of <1.0, 0.0> creates a plane with a
    /// slope of 1.0 in the xz plane.
    pub fn plane(&self, aabr: Aabr<i32>, origin: Vec3<i32>, gradient: Vec2<f32>) -> PrimitiveRef {
        let aabr = aabr.made_valid();
        self.prim(Primitive::Plane(aabr, origin, gradient))
    }

    /// Returns a `PrimitiveRef` of an Aabb with a slope cut into it. The
    /// `inset` governs the slope. The `dir` determines which direction the
    /// ramp points.
    pub fn ramp_inset(&self, aabb: Aabb<i32>, inset: i32, dir: Dir) -> PrimitiveRef {
        let aabb = aabb.made_valid();
        self.prim(Primitive::Ramp { aabb, inset, dir })
    }

    pub fn ramp(&self, aabb: Aabb<i32>, dir: Dir) -> PrimitiveRef {
        let aabb = aabb.made_valid();
        self.prim(Primitive::Ramp {
            aabb,
            inset: dir.select((aabb.size().w, aabb.size().h)),
            dir,
        })
    }

    /// Returns a `PrimitiveRef` of a triangular prism with the base being
    /// vertical. A gable is a tent shape. The `inset` governs the slope of
    /// the gable. The `dir` determines which way the gable points.
    pub fn gable(&self, aabb: Aabb<i32>, inset: i32, dir: Dir) -> PrimitiveRef {
        let aabb = aabb.made_valid();
        self.prim(Primitive::Gable { aabb, inset, dir })
    }

    /// Places a sprite at the provided location with the default rotation.
    pub fn sprite(&self, pos: Vec3<i32>, sprite: SpriteKind) {
        self.aabb(Aabb {
            min: pos,
            max: pos + 1,
        })
        .fill(Fill::sprite(sprite))
    }

    /// Places a sprite at the provided location with the provided orientation.
    pub fn rotated_sprite(&self, pos: Vec3<i32>, sprite: SpriteKind, ori: u8) {
        self.aabb(Aabb {
            min: pos,
            max: pos + 1,
        })
        .fill(Fill::sprite_ori(sprite, ori))
    }

    /// Places a sprite at the provided location with the provided orientation
    /// and the provided [`SpriteCfg`].
    pub fn rotated_sprite_with_cfg(
        &self,
        pos: Vec3<i32>,
        sprite: SpriteKind,
        ori: u8,
        cfg: SpriteCfg,
    ) {
        self.aabb(Aabb {
            min: pos,
            max: pos + 1,
        })
        .fill(Fill::sprite_ori_cfg(sprite, ori, cfg))
    }

    /// Places a sprite at the provided location with the provided orientation
    /// which will be tracked by rtsim nature if the sprite has an associated
    /// [`ChunkResource`].
    pub fn resource_sprite(&self, pos: Vec3<i32>, sprite: SpriteKind, ori: u8) {
        self.aabb(Aabb {
            min: pos,
            max: pos + 1,
        })
        .fill(Fill::resource_sprite_ori(sprite, ori))
    }

    /// Places a sprite at the provided location with the provided orientation
    /// which will be tracked by rtsim nature if the sprite has an associated
    /// [`ChunkResource`].
    pub fn owned_resource_sprite(&self, pos: Vec3<i32>, sprite: SpriteKind, ori: u8) {
        self.aabb(Aabb {
            min: pos,
            max: pos + 1,
        })
        .fill(Fill::owned_resource_sprite_ori(sprite, ori))
    }

    /// Returns a `PrimitiveRef` of the largest pyramid with a slope of 1 that
    /// fits in the provided Aabb.
    pub fn pyramid(&self, aabb: Aabb<i32>) -> PrimitiveRef {
        let inset = 0;
        let aabb = aabb.made_valid();
        self.prim(Primitive::Ramp {
            aabb,
            inset,
            dir: Dir::X,
        })
        .intersect(self.prim(Primitive::Ramp {
            aabb,
            inset,
            dir: Dir::NegX,
        }))
        .intersect(self.prim(Primitive::Ramp {
            aabb,
            inset,
            dir: Dir::Y,
        }))
        .intersect(self.prim(Primitive::Ramp {
            aabb,
            inset,
            dir: Dir::NegY,
        }))
    }

    /// Used to create a new `PrimitiveRef`. Requires the desired `Primitive` to
    /// be supplied.
    pub fn prim(&self, prim: Primitive) -> PrimitiveRef {
        PrimitiveRef {
            id: self.prims.borrow_mut().insert(prim),
            painter: self,
        }
    }

    /// Returns a `PrimitiveRef` of an empty primitive. Useful when additional
    /// primitives are unioned within a loop.
    pub fn empty(&self) -> PrimitiveRef { self.prim(Primitive::Empty) }

    /// Fills the supplied primitive with the provided `Fill`.
    pub fn fill(&self, prim: impl Into<Id<Primitive>>, fill: Fill) {
        let prim = prim.into();
        if let Primitive::Union(a, b) = self.prims.borrow()[prim] {
            self.fill(a, fill.clone());
            self.fill(b, fill);
        } else {
            self.fills.borrow_mut().push((prim, fill));
        }
    }

    /// ```text
    ///     ___
    ///    /  /\
    ///   /__/  |
    ///  /   \  |
    /// |     | /
    /// |_____|/
    /// ```
    /// A horizontal half cylinder on top of an `Aabb`.
    pub fn vault(&self, aabb: Aabb<i32>, dir: Dir) -> PrimitiveRef {
        let h = dir.orthogonal().select(Vec3::from(aabb.size()).xy());

        let mut prim = self.horizontal_cylinder(
            Aabb {
                min: aabb.min.with_z(aabb.max.z - h),
                max: aabb.max,
            },
            dir,
        );

        if aabb.size().d < h {
            prim = prim.intersect(self.aabb(aabb));
        }

        self.aabb(Aabb {
            min: aabb.min,
            max: aabb.max.with_z(aabb.max.z - h / 2),
        })
        .union(prim)
    }

    /// Place aabbs around another aabb in a symmetric and distributed manner.
    pub fn aabbs_around_aabb(&self, aabb: Aabb<i32>, size: i32, offset: i32) -> PrimitiveRef {
        let pillar = self.aabb(Aabb {
            min: (aabb.min.xy() - 1).with_z(aabb.min.z),
            max: (aabb.min.xy() + size - 1).with_z(aabb.max.z),
        });

        let true_offset = offset + size;

        let size_x = aabb.max.x - aabb.min.x;
        let size_y = aabb.max.y - aabb.min.y;

        let num_aabbs = ((size_x + 1) / 2) / true_offset;
        let x = pillar.repeat(Vec3::new(true_offset, 0, 0), num_aabbs as u32 + 1);

        let num_aabbs = ((size_y + 1) / 2) / true_offset;
        let y = pillar.repeat(Vec3::new(0, true_offset, 0), num_aabbs as u32 + 1);
        let center = aabb.as_::<f32>().center();
        let shape = x.union(y);
        let shape =
            shape.union(shape.rotate_about(Mat3::new(-1, 0, 0, 0, -1, 0, 0, 0, -1), center));
        shape.union(shape.rotate_about(Mat3::new(0, 1, 0, -1, 0, 0, 0, 0, 1), center))
    }

    pub fn staircase_in_aabb(
        &self,
        aabb: Aabb<i32>,
        thickness: i32,
        start_dir: Dir,
    ) -> PrimitiveRef {
        let mut forward = start_dir;
        let mut z = aabb.max.z - 1;
        let aabr = Aabr::from(aabb);

        let mut prim = self.empty();

        while z > aabb.min.z {
            let right = forward.rotated_cw();
            let fc = forward.select_aabr(aabr);
            let corner =
                fc * forward.abs().to_vec2() + right.select_aabr(aabr) * right.abs().to_vec2();
            let aabb = Aabb {
                min: corner.with_z(z),
                max: (corner - (forward.to_vec2() + right.to_vec2()) * thickness).with_z(z + 1),
            }
            .made_valid();

            let stair_len = ((fc - (-forward).select_aabr(aabr)).abs() - thickness * 2).max(1) + 1;

            let stairs = self.ramp(
                Aabb {
                    min: (corner - right.to_vec2() * (stair_len + thickness)).with_z(z - stair_len),
                    max: (corner
                        - right.to_vec2() * (thickness - 1)
                        - forward.to_vec2() * thickness)
                        .with_z(z + 1),
                }
                .made_valid(),
                right,
            );

            prim = prim
                .union(self.aabb(aabb))
                .union(stairs.without(stairs.translate(Vec3::new(0, 0, -2))));

            z -= stair_len;
            forward = forward.rotated_ccw();
        }
        prim
    }

    /// A simple numeral 0-9
    pub fn numeral(&self, origin: Vec3<i32>, numeral: usize) -> PrimitiveRef {
        let bottom_bar = self.aabb(Aabb {
            min: Vec2::new(origin.x, origin.y).with_z(origin.z),
            max: Vec2::new(origin.x + 1, origin.y + 4).with_z(origin.z + 1),
        });
        let mid_bar = self.aabb(Aabb {
            min: Vec2::new(origin.x, origin.y).with_z(origin.z + 2),
            max: Vec2::new(origin.x + 1, origin.y + 4).with_z(origin.z + 3),
        });
        let top_bar = self.aabb(Aabb {
            min: Vec2::new(origin.x, origin.y).with_z(origin.z + 4),
            max: Vec2::new(origin.x + 1, origin.y + 4).with_z(origin.z + 5),
        });
        let left_top = self.aabb(Aabb {
            min: Vec2::new(origin.x, origin.y).with_z(origin.z + 2),
            max: Vec2::new(origin.x + 1, origin.y + 1).with_z(origin.z + 5),
        });
        let left_bottom = self.aabb(Aabb {
            min: Vec2::new(origin.x, origin.y).with_z(origin.z),
            max: Vec2::new(origin.x + 1, origin.y + 1).with_z(origin.z + 3),
        });
        let right_top = self.aabb(Aabb {
            min: Vec2::new(origin.x, origin.y + 3).with_z(origin.z + 2),
            max: Vec2::new(origin.x + 1, origin.y + 4).with_z(origin.z + 5),
        });
        let right_bottom = self.aabb(Aabb {
            min: Vec2::new(origin.x, origin.y + 3).with_z(origin.z),
            max: Vec2::new(origin.x + 1, origin.y + 4).with_z(origin.z + 3),
        });
        let number_strokes = match numeral {
            0 => &[
                top_bar,
                bottom_bar,
                right_top,
                right_bottom,
                left_top,
                left_bottom,
            ] as &[_],
            1 => &[right_top, right_bottom],
            2 => &[top_bar, right_top, mid_bar, left_bottom, bottom_bar],
            3 => &[top_bar, bottom_bar, mid_bar, right_top, right_bottom],
            4 => &[left_top, mid_bar, right_top, right_bottom],
            5 => &[top_bar, left_top, mid_bar, right_bottom, bottom_bar],
            6 => &[
                top_bar,
                left_top,
                left_bottom,
                bottom_bar,
                right_bottom,
                mid_bar,
            ],
            7 => &[top_bar, right_top, right_bottom],
            8 => &[
                top_bar,
                left_top,
                left_bottom,
                mid_bar,
                right_top,
                right_bottom,
                bottom_bar,
            ],
            _ => &[top_bar, left_top, mid_bar, right_top, right_bottom],
        };

        let mut prim = self.empty();
        for stroke in number_strokes {
            prim = prim.union(*stroke)
        }

        prim
    }

    pub fn column(&self, point: Vec2<i32>, range: impl RangeBounds<i32>) -> PrimitiveRef {
        self.aabb(Aabb {
            min: point.with_z(match range.start_bound() {
                std::ops::Bound::Included(n) => *n,
                std::ops::Bound::Excluded(n) => n + 1,
                std::ops::Bound::Unbounded => i32::MIN,
            }),
            max: (point + 1).with_z(match range.end_bound() {
                std::ops::Bound::Included(n) => n + 1,
                std::ops::Bound::Excluded(n) => *n,
                std::ops::Bound::Unbounded => i32::MAX,
            }),
        })
    }

    /// The area that the canvas is currently rendering.
    pub fn render_aabr(&self) -> Aabr<i32> { self.render_area }

    /// Spawns an entity if it is in the render_aabr, otherwise does nothing.
    pub fn spawn(&self, entity: EntityInfo) {
        if self.render_area.contains_point(entity.pos.xy().as_()) {
            self.entities.borrow_mut().push(entity)
        }
    }
}

#[derive(Copy, Clone)]
pub struct PrimitiveRef<'a> {
    id: Id<Primitive>,
    painter: &'a Painter,
}

impl<'a> From<PrimitiveRef<'a>> for Id<Primitive> {
    fn from(r: PrimitiveRef<'a>) -> Self { r.id }
}

impl<'a> PrimitiveRef<'a> {
    /// Joins two primitives together by returning the total of the blocks of
    /// both primitives. In boolean logic this is an `OR` operation.
    #[must_use]
    pub fn union(self, other: impl Into<Id<Primitive>>) -> PrimitiveRef<'a> {
        let (a, b) = self.painter.order_by_depth(self, other);
        self.painter.prim(Primitive::union(a, b))
    }

    /// Joins two primitives together by returning only overlapping blocks. In
    /// boolean logic this is an `AND` operation.
    #[must_use]
    pub fn intersect(self, other: impl Into<Id<Primitive>>) -> PrimitiveRef<'a> {
        let (a, b) = self.painter.order_by_depth(self, other);
        self.painter.prim(Primitive::intersect(a, b))
    }

    /// Subtracts the blocks of the `other` primitive from `self`. In boolean
    /// logic this is a `NOT` operation.
    #[must_use]
    pub fn without(self, other: impl Into<Id<Primitive>>) -> PrimitiveRef<'a> {
        self.painter.prim(Primitive::without(self, other))
    }

    /// Fills the primitive with `fill` and paints it into the world.
    pub fn fill(self, fill: Fill) { self.painter.fill(self, fill); }

    /// Fills the primitive with empty blocks. This will subtract any
    /// blocks in the world that inhabit the same positions as the blocks in
    /// this primitive.
    pub fn clear(self) { self.painter.fill(self, Fill::Block(Block::empty())); }

    /// Returns a `PrimitiveRef` that conforms to the provided sampling
    /// function.
    #[must_use]
    pub fn sample(self, sampling: impl Fn(Vec3<i32>) -> bool + 'static) -> PrimitiveRef<'a> {
        self.painter
            .prim(Primitive::sampling(self, Box::new(sampling)))
    }

    /// Returns a `PrimitiveRef` that conforms to the provided sampling
    /// function.
    #[must_use]
    pub fn sample_with_column(
        self,
        sampling: impl Fn(Vec3<i32>, &ColInfo) -> bool + 'static,
    ) -> PrimitiveRef<'a> {
        self.painter
            .prim(Primitive::column_sampling(self, Box::new(sampling)))
    }

    /// Rotates a primitive about it's own's bounds minimum point,
    #[must_use]
    pub fn rotate_about_min(self, mat: Mat3<i32>) -> PrimitiveRef<'a> {
        let point = Fill::get_bounds(&self.painter.prims.borrow(), self.into()).min;
        self.rotate_about(mat, point)
    }
}

/// A trait to more easily manipulate groups of primitives.
pub trait PrimitiveTransform {
    /// Translates the primitive along the vector `trans`.
    #[must_use]
    fn translate(self, trans: Vec3<i32>) -> Self;
    /// Rotates the primitive about the given point by multiplying each block
    /// position by the provided rotation matrix.
    #[must_use]
    fn rotate_about(self, rot: Mat3<i32>, point: Vec3<impl AsPrimitive<f32>>) -> Self;
    /// Rotates the primitive 90 degrees counterclockwise about the given point
    /// along the Z axis n times.
    #[must_use]
    fn rotate_z_90_about(self, n: i32, point: Vec3<impl AsPrimitive<f32>>) -> Self;
    /// Scales the primitive along each axis by the x, y, and z components of
    /// the `scale` vector respectively.
    #[must_use]
    fn scale(self, scale: Vec3<impl AsPrimitive<f32>>) -> Self;
    /// Returns a `PrimitiveRef` of the primitive in addition to the same
    /// primitive translated by `offset` and repeated `count` times, each time
    /// translated by an additional offset.
    #[must_use]
    fn repeat(self, offset: Vec3<i32>, count: u32) -> Self;
}

impl<'a> PrimitiveTransform for PrimitiveRef<'a> {
    fn translate(self, trans: Vec3<i32>) -> Self {
        self.painter.prim(Primitive::translate(self, trans))
    }

    fn rotate_about(self, rot: Mat3<i32>, point: Vec3<impl AsPrimitive<f32>>) -> Self {
        self.painter.prim(Primitive::rotate_about(self, rot, point))
    }

    fn rotate_z_90_about(self, n: i32, point: Vec3<impl AsPrimitive<f32>>) -> Self {
        self.painter
            .prim(Primitive::rotate_z_90_about(self, n, point))
    }

    fn scale(self, scale: Vec3<impl AsPrimitive<f32>>) -> Self {
        self.painter.prim(Primitive::scale(self, scale.as_()))
    }

    fn repeat(self, offset: Vec3<i32>, count: u32) -> Self {
        self.painter.prim(Primitive::repeat(self, offset, count))
    }
}

impl<'a, const N: usize> PrimitiveTransform for [PrimitiveRef<'a>; N] {
    fn translate(mut self, trans: Vec3<i32>) -> Self {
        for prim in &mut self {
            *prim = prim.translate(trans);
        }
        self
    }

    fn rotate_about(mut self, rot: Mat3<i32>, point: Vec3<impl AsPrimitive<f32>>) -> Self {
        for prim in &mut self {
            *prim = prim.rotate_about(rot, point);
        }
        self
    }

    fn rotate_z_90_about(mut self, n: i32, point: Vec3<impl AsPrimitive<f32>>) -> Self {
        for prim in &mut self {
            *prim = prim.rotate_z_90_about(n, point);
        }
        self
    }

    fn scale(mut self, scale: Vec3<impl AsPrimitive<f32>>) -> Self {
        for prim in &mut self {
            *prim = prim.scale(scale);
        }
        self
    }

    fn repeat(mut self, offset: Vec3<i32>, count: u32) -> Self {
        for prim in &mut self {
            *prim = prim.repeat(offset, count);
        }
        self
    }
}

pub trait Structure {
    #[cfg(feature = "use-dyn-lib")]
    const UPDATE_FN: &'static [u8];

    fn render_inner(&self, _site: &Site, _land: &Land, _painter: &Painter) {}

    fn render(&self, site: &Site, land: &Land, painter: &Painter) {
        #[cfg(not(feature = "use-dyn-lib"))]
        {
            self.render_inner(site, land, painter);
        }
        #[cfg(feature = "use-dyn-lib")]
        {
            let lock = LIB.lock().unwrap();
            let lib = &lock.as_ref().unwrap().lib;

            let update_fn: common_dynlib::Symbol<fn(&Self, &Site, &Land, &Painter)> = unsafe {
                //let start = std::time::Instant::now();
                // Overhead of 0.5-5 us (could use hashmap to mitigate if this is an issue)
                lib.get(Self::UPDATE_FN)
                //println!("{}", start.elapsed().as_nanos());
            }
            .unwrap_or_else(|e| {
                panic!(
                    "Trying to use: {} but had error: {:?}",
                    CStr::from_bytes_with_nul(Self::UPDATE_FN)
                        .map(CStr::to_str)
                        .unwrap()
                        .unwrap(),
                    e
                )
            });

            update_fn(self, site, land, painter);
        }
    }

    // Generate a primitive tree and fills for this structure
    fn render_collect(
        &self,
        site: &Site,
        canvas: &CanvasInfo,
    ) -> (
        Store<Primitive>,
        Vec<(Id<Primitive>, Fill)>,
        Vec<EntityInfo>,
    ) {
        let painter = Painter {
            prims: RefCell::new(Store::default()),
            fills: RefCell::new(Vec::new()),
            entities: RefCell::new(Vec::new()),
            render_area: Aabr {
                min: canvas.wpos,
                max: canvas.wpos + TerrainChunkSize::RECT_SIZE.map(|e| e as i32),
            },
        };

        self.render(site, &canvas.land(), &painter);
        (
            painter.prims.into_inner(),
            painter.fills.into_inner(),
            painter.entities.into_inner(),
        )
    }

    fn terrain_surface_at<R: Rng>(
        &self,
        _wpos: Vec2<i32>,
        _old: Block,
        _rng: &mut R,
        _col: &ColumnSample,
        _z_off: i32,
    ) -> Option<Block> {
        None
    }
}

/// Extend a 2d AABR to a 3d AABB
pub fn aabr_with_z<T>(aabr: Aabr<T>, z: Range<T>) -> Aabb<T> {
    Aabb {
        min: aabr.min.with_z(z.start),
        max: aabr.max.with_z(z.end),
    }
}

#[allow(dead_code)]
/// Just the corners of an AABB, good for outlining stuff when debugging
pub fn aabb_corners<F: FnMut(Primitive) -> Id<Primitive>>(
    prim: &mut F,
    aabb: Aabb<i32>,
) -> Id<Primitive> {
    let f = |prim: &mut F, ret, vec| {
        let sub = prim(Primitive::Aabb(Aabb {
            min: aabb.min + vec,
            max: aabb.max - vec,
        }));
        prim(Primitive::Without(ret, sub))
    };
    let mut ret = prim(Primitive::Aabb(aabb));
    ret = f(prim, ret, Vec3::new(1, 0, 0));
    ret = f(prim, ret, Vec3::new(0, 1, 0));
    ret = f(prim, ret, Vec3::new(0, 0, 1));
    ret
}

pub fn place_circular(
    center: Vec2<i32>,
    radius: f32,
    amount: i32,
) -> impl Iterator<Item = Vec2<i32>> {
    let phi = TAU / amount as f32;
    (1..=amount).map(move |n| {
        Vec2::new(
            center.x + (radius * ((n as f32 * phi).cos())) as i32,
            center.y + (radius * ((n as f32 * phi).sin())) as i32,
        )
    })
}

pub fn place_circular_as_vec(center: Vec2<i32>, radius: f32, amount: i32) -> Vec<Vec2<i32>> {
    let phi = TAU / amount as f32;
    let mut positions = vec![];
    for n in 1..=amount {
        let pos = Vec2::new(
            center.x + (radius * ((n as f32 * phi).cos())) as i32,
            center.y + (radius * ((n as f32 * phi).sin())) as i32,
        );
        positions.push(pos);
    }
    positions
}

pub fn spiral_staircase(
    origin: Vec3<i32>,
    radius: f32,
    inner_radius: f32,
    stretch: f32,
) -> Box<dyn Fn(Vec3<i32>) -> bool> {
    Box::new(move |pos: Vec3<i32>| {
        let pos = pos - origin;
        if (pos.xy().magnitude_squared() as f32) < inner_radius.powi(2) {
            true
        } else if (pos.xy().magnitude_squared() as f32) < radius.powi(2) {
            ((pos.x as f32).atan2(pos.y as f32) / (PI * 2.0) * stretch + pos.z as f32)
                .rem_euclid(stretch)
                < 1.5
        } else {
            false
        }
    })
}

pub fn wall_staircase(
    origin: Vec3<i32>,
    radius: f32,
    stretch: f32,
) -> Box<dyn Fn(Vec3<i32>) -> bool> {
    Box::new(move |pos: Vec3<i32>| {
        let pos = pos - origin;
        if (pos.x.abs().max(pos.y.abs())) as f32 > 0.6 * radius {
            ((pos.x as f32).atan2(pos.y as f32) / (PI * 2.0) * stretch + pos.z as f32)
                .rem_euclid(stretch)
                < 1.0
        } else {
            false
        }
    })
}

pub fn inscribed_polystar(
    origin: Vec2<i32>,
    radius: f32,
    sides: usize,
) -> Box<dyn Fn(Vec3<i32>) -> bool> {
    Box::new(move |pos| {
        use std::f32::consts::TAU;
        let rpos: Vec2<f32> = pos.xy().as_() - origin.as_();
        let is_border = rpos.magnitude_squared() > (radius - 2.0).powi(2);
        let is_line = (0..sides).any(|i| {
            let f = |j: f32| {
                let t = j * TAU / sides as f32;
                radius * Vec2::new(t.cos(), t.sin())
            };
            let line = LineSegment2 {
                start: f(i as f32),
                end: f((i + 2) as f32),
            };
            line.distance_to_point(rpos) <= 1.0
        });
        is_border || is_line
    })
}