libfive/
stdlib.rs

1use crate::*;
2
3/// 2D point/vector/normal.
4pub struct TreeVec2 {
5    pub x: Tree,
6    pub y: Tree,
7}
8
9impl TreeVec2 {
10    pub fn new(x: f32, y: f32) -> Self {
11        Self {
12            x: Tree::from(x),
13            y: Tree::from(y),
14        }
15    }
16}
17
18impl Default for TreeVec2 {
19    fn default() -> Self {
20        Self {
21            x: Tree::from(0.0),
22            y: Tree::from(0.0),
23        }
24    }
25}
26
27/// 3D point/vector/normal.
28pub struct TreeVec3 {
29    pub x: Tree,
30    pub y: Tree,
31    pub z: Tree,
32}
33
34impl TreeVec3 {
35    pub fn new(x: f32, y: f32, z: f32) -> Self {
36        Self {
37            x: Tree::from(x),
38            y: Tree::from(y),
39            z: Tree::from(z),
40        }
41    }
42}
43
44impl Default for TreeVec3 {
45    fn default() -> Self {
46        Self {
47            x: Tree::from(0.0),
48            y: Tree::from(0.0),
49            z: Tree::from(0.0),
50        }
51    }
52}
53
54include!("shapes.rs");
55include!("generators.rs");
56include!("csg.rs");
57
58/// A collection of [`Tree`]s.
59///
60/// This is used for the [`*_multi()`](Tree#multi_csg) CSG operations.
61pub type Trees = Vec<Tree>;
62
63/// <a name="multi_csg"></a>
64/// Operations taking multiple 2nd arguments.
65impl Tree {
66    pub fn union_multi(self, trees: Trees) -> Self {
67        if trees.is_empty() {
68            Tree::emptiness()
69        } else {
70            trees.into_iter().fold(self, |a, b| a.union(b))
71        }
72    }
73
74    pub fn intersection_multi(self, trees: Trees) -> Self {
75        if trees.is_empty() {
76            Tree::emptiness()
77        } else {
78            trees.into_iter().fold(self, |a, b| a.intersection(b))
79        }
80    }
81
82    pub fn difference_multi(self, trees: Trees) -> Self {
83        if trees.is_empty() {
84            self
85        } else {
86            self.intersection(
87                trees
88                    .into_iter()
89                    .reduce(|a, b| a.union(b))
90                    .expect("there is at least one tree")
91                    .inverse(),
92            )
93        }
94    }
95}
96
97include!("transforms.rs");
98include!("text.rs");