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
use crate::*;

/// 2D point/vector/normal.
pub struct TreeVec2 {
    pub x: Tree,
    pub y: Tree,
}

impl TreeVec2 {
    pub fn new(x: f32, y: f32) -> Self {
        Self {
            x: Tree::from(x),
            y: Tree::from(y),
        }
    }
}

impl Default for TreeVec2 {
    fn default() -> Self {
        Self {
            x: Tree::from(0.0),
            y: Tree::from(0.0),
        }
    }
}

/// 3D point/vector/normal.
pub struct TreeVec3 {
    pub x: Tree,
    pub y: Tree,
    pub z: Tree,
}

impl TreeVec3 {
    pub fn new(x: f32, y: f32, z: f32) -> Self {
        Self {
            x: Tree::from(x),
            y: Tree::from(y),
            z: Tree::from(z),
        }
    }
}

impl Default for TreeVec3 {
    fn default() -> Self {
        Self {
            x: Tree::from(0.0),
            y: Tree::from(0.0),
            z: Tree::from(0.0),
        }
    }
}

include!("shapes.rs");
include!("generators.rs");
include!("csg.rs");

/// A collection of [`Tree`]s.
///
/// This is used for the [`*_multi()`](Tree#multi_csg) CSG operations.
pub type Trees = Vec<Tree>;

/// <a name="multi_csg"></a>
/// Operations taking multiple 2nd arguments.
impl Tree {
    pub fn union_multi(self, trees: Trees) -> Self {
        if trees.is_empty() {
            Tree::emptiness()
        } else {
            trees.into_iter().fold(self, |a, b| a.union(b))
        }
    }

    pub fn intersection_multi(self, trees: Trees) -> Self {
        if trees.is_empty() {
            Tree::emptiness()
        } else {
            trees.into_iter().fold(self, |a, b| a.intersection(b))
        }
    }

    pub fn difference_multi(self, trees: Trees) -> Self {
        if trees.is_empty() {
            self
        } else if 1 == trees.len() {
            self.intersection(trees[0].clone())
        } else {
            let first = trees[0].clone();
            self.intersection(
                trees
                    .into_iter()
                    .skip(1)
                    .fold(first, |a, b| a.union(b))
                    .inverse(),
            )
        }
    }
}

include!("transforms.rs");
include!("text.rs");