algebraeon_geometry/simplexes/
boolean_opperations.rs

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
use std::collections::{HashMap, HashSet};

use super::*;

#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum VennLabel {
    Left,
    Middle,
    Right,
}

impl<FS: OrderedRingStructure + FieldStructure, SP: Borrow<AffineSpace<FS>> + Clone> Simplex<FS, SP>
where
    FS::Set: Hash,
{
    pub fn venn(&self, other: &Self) -> LabelledPartialSimplicialComplex<FS, SP, VennLabel> {
        let ambient_space = common_space(self.ambient_space(), other.ambient_space()).unwrap();

        let overlap = ConvexHull::intersect(
            &ConvexHull::from_simplex(self.clone()),
            &ConvexHull::from_simplex(other.clone()),
        );

        let mut self_ext = overlap.clone();
        for pt in self.points() {
            self_ext.extend_by_point(pt.clone());
        }
        let self_parts = self_ext
            .as_simplicial_complex()
            .subset_by_label(&InteriorBoundaryLabel::Interior)
            .into_simplexes();

        let mut other_ext = overlap.clone();
        for pt in other.points() {
            other_ext.extend_by_point(pt.clone());
        }
        let other_parts = other_ext
            .as_simplicial_complex()
            .subset_by_label(&InteriorBoundaryLabel::Interior)
            .into_simplexes();

        let all_parts = self_parts.union(&other_parts);
        LabelledPartialSimplicialComplex::<FS, SP, VennLabel>::new_labelled_unchecked(
            ambient_space.clone(),
            all_parts
                .into_iter()
                .map(|spx| {
                    let label = match (self_parts.contains(spx), other_parts.contains(spx)) {
                        (true, false) => VennLabel::Left,
                        (true, true) => VennLabel::Middle,
                        (false, true) => VennLabel::Right,
                        (false, false) => {
                            unreachable!()
                        }
                    };
                    (spx.clone(), label)
                })
                .collect(),
        )
    }
}

impl<
        FS: OrderedRingStructure + FieldStructure,
        SP: Borrow<AffineSpace<FS>> + Clone,
        T: Eq + Clone,
    > LabelledSimplicialDisjointUnion<FS, SP, T>
where
    FS::Set: Hash,
{
    pub fn subtract_raw<S: Eq + Clone>(
        &self,
        other: &LabelledSimplicialDisjointUnion<FS, SP, S>,
    ) -> LabelledSimplicialDisjointUnion<FS, SP, T> {
        let ambient_space = common_space(self.ambient_space(), other.ambient_space()).unwrap();

        Self::new_labelled_unchecked(ambient_space.clone(), {
            let mut simplexes = HashMap::new();
            for (self_spx, self_spx_label) in self.labelled_simplexes() {
                let mut self_leftover = HashSet::from([self_spx.clone()]);
                for other_spx in other.simplexes() {
                    self_leftover = self_leftover
                        .into_iter()
                        .map(|self_leftover_spx| {
                            Simplex::venn(&self_leftover_spx, other_spx)
                                .subset_by_label(&VennLabel::Left)
                                .into_simplexes()
                        })
                        .flatten()
                        .collect();
                }
                for spx in self_leftover {
                    simplexes.insert(spx, self_spx_label.clone());
                }
            }
            simplexes
        })
    }

    pub fn intersection_raw<S: Eq + Clone>(
        &self,
        other: &LabelledSimplicialDisjointUnion<FS, SP, S>,
    ) -> LabelledSimplicialDisjointUnion<FS, SP, (T, S)> {
        let ambient_space = common_space(self.ambient_space(), other.ambient_space()).unwrap();
        LabelledSimplicialDisjointUnion::new_labelled_unchecked(ambient_space.clone(), {
            let mut simplexes = HashMap::new();
            for (self_spx, self_spx_label) in self.labelled_simplexes() {
                for (other_spx, other_spx_label) in other.labelled_simplexes() {
                    for spx in Simplex::venn(self_spx, other_spx)
                        .subset_by_label(&VennLabel::Middle)
                        .into_simplexes()
                    {
                        simplexes.insert(spx, (self_spx_label.clone(), other_spx_label.clone()));
                    }
                }
            }
            simplexes
        })
    }
}

impl<FS: OrderedRingStructure + FieldStructure, SP: Borrow<AffineSpace<FS>> + Clone>
    SimplicialDisjointUnion<FS, SP>
where
    FS::Set: Hash,
{
    pub fn union_raw(&self, other: &Self) -> SimplicialDisjointUnion<FS, SP> {
        let ambient_space = common_space(self.ambient_space(), other.ambient_space()).unwrap();
        let mut simplexes = HashSet::new();
        for spx in Self::subtract_raw(other, self).into_simplexes() {
            simplexes.insert(spx);
        }
        for spx in self.simplexes() {
            simplexes.insert(spx.clone());
        }
        return Self::new_unchecked(ambient_space, simplexes);
    }
}

// impl<FS: OrderedRingStructure + FieldStructure, SP: Borrow<AffineSpace<FS>> + Clone>
//     PartialSimplicialComplex<FS, SP>
// where
//     FS::Set: Hash,
// {
//     pub fn union(&self, other: &Self) -> Self {
//         self.union_raw(other).simplify()
//     }

//     pub fn union_raw(&self, other: &Self) -> Self {
//         SimplicialComplex::union_raw(&self, other)
//     }

//     pub fn intersection_raw(&self, other: &Self) -> Self {
//         SimplicialDisjointUnion::intersection_raw(&self.into(), &other.into())
//             .refine_to_partial_simplicial_complex()
//             .closure()
//             .forget_labels()
//             .into()
//     }

//     pub fn intersection(&self, other: &Self) -> Self {
//         self.intersection_raw(other).simplify()
//     }
// }

impl<FS: OrderedRingStructure + FieldStructure, SP: Borrow<AffineSpace<FS>> + Clone>
    SimplicialComplex<FS, SP>
where
    FS::Set: Hash,
{
    pub fn union_raw(&self, other: &Self) -> Self {
        LabelledSimplicialDisjointUnion::union_raw(&self.into(), &other.into())
            .refine_to_partial_simplicial_complex()
            .try_as_simplicial_complex()
            .unwrap()
    }

    pub fn union(&self, other: &Self) -> Self {
        self.union_raw(other).simplify()
    }

    pub fn intersection_raw(&self, other: &Self) -> Self {
        LabelledSimplicialDisjointUnion::intersection_raw(&self.into(), &other.into())
            .refine_to_partial_simplicial_complex()
            .apply_label_function(|_| ())
            .try_as_simplicial_complex()
            .unwrap()
    }

    pub fn intersection(&self, other: &Self) -> Self {
        self.intersection_raw(other).simplify()
    }
}

/*
 - Venn dju <T1> and dju <T2> to produce dju <(Option<T1>, Option<T2>)>
 - Replace partial simplicial complex (psc) with labelled simplicial complex <bool>
 - Intersect psc, psc -> psc
 - Union psc, psc -> psc
 - Subtract psc, psc -> psc
 - Have a trait for a collection of labelled simplexes
   - Labelled subset
   - Filtered labelled subset
   - Union -> PartialSimplicialComplex
   - Intersection -> PartialSimplicialComplex
   - Difference -> PartialSimplicialComplex
 - Implement it for:
   - SimplexUnion
   - SimplexDisjointUnion
   - SemiSimplicialComplex
   - SimplicialComplex
*/