Skip to main content

gammalooprs/graph/
cuts.rs

1use bincode_trait_derive::{Decode, Encode};
2use gammaloop_tracing_filter::LogMessage;
3use itertools::Itertools;
4use linnet::half_edge::{
5    involution::EdgeIndex,
6    subgraph::{SuBitGraph, SubSetLike},
7};
8
9use crate::cff::CutCFFIndex;
10use crate::cff::RaisedEsurfaceGroup;
11
12#[derive(Debug, Clone, Encode, Decode, PartialEq, Hash, Eq, PartialOrd, Ord)]
13pub struct CutSet {
14    pub residue_selector: ResidueSelector,
15    pub union: SuBitGraph,
16    pub canonicalize_external_shifts: bool,
17}
18
19impl LogMessage for CutSet {
20    fn log_display(&self) -> String {
21        format!("CutSet: union={}", self.union.string_label())
22    }
23}
24
25#[derive(Debug, Clone, Encode, Decode, PartialEq, Hash, Eq, PartialOrd, Ord)]
26pub struct LuCutSelection {
27    pub raised_group: RaisedEsurfaceGroup,
28    /// Normalized physical edge support for each actual Cutkosky cut in this
29    /// raised group. Alternative order remains the physical-cut order so a
30    /// future LTD policy can attach representation-specific data without
31    /// reconstructing alternatives from the lossy union subgraph.
32    pub cut_edge_alternatives: Vec<Vec<EdgeIndex>>,
33}
34
35#[derive(Debug, Clone, Encode, Decode, PartialEq, Hash, Eq, PartialOrd, Ord)]
36pub struct ResidueSelector {
37    pub lu: Option<LuCutSelection>,
38    pub left_th_cut: Option<RaisedEsurfaceGroup>,
39    pub right_th_cut: Option<RaisedEsurfaceGroup>,
40}
41
42impl ResidueSelector {
43    pub fn generate_allowed_keys(&self) -> Vec<CutCFFIndex> {
44        let allowed_keys = vec![CutCFFIndex::new_all_none()];
45        allowed_keys
46            .into_iter()
47            .flat_map(|index| {
48                if let Some(lu) = &self.lu {
49                    (1..=lu.raised_group.max_occurence)
50                        .map(|lu_cut_index| {
51                            let mut new_index = index;
52                            new_index.lu_cut_order = Some(lu_cut_index);
53                            new_index
54                        })
55                        .collect_vec()
56                } else {
57                    vec![index]
58                }
59            })
60            .flat_map(|index| {
61                if let Some(left_th_cut) = &self.left_th_cut {
62                    (1..=left_th_cut.max_occurence)
63                        .map(|left_th_cut_index| {
64                            let mut new_index = index;
65                            new_index.left_threshold_order = Some(left_th_cut_index);
66                            new_index
67                        })
68                        .collect_vec()
69                } else {
70                    vec![index]
71                }
72            })
73            .flat_map(|index| {
74                if let Some(right_th_cut) = &self.right_th_cut {
75                    (1..=right_th_cut.max_occurence)
76                        .map(|right_th_cut_index| {
77                            let mut new_index = index;
78                            new_index.right_threshold_order = Some(right_th_cut_index);
79                            new_index
80                        })
81                        .collect_vec()
82                } else {
83                    vec![index]
84                }
85            })
86            .collect()
87    }
88}
89
90impl CutSet {
91    pub fn empty(size: usize) -> Self {
92        CutSet {
93            residue_selector: ResidueSelector {
94                lu: None,
95                left_th_cut: None,
96                right_th_cut: None,
97            },
98            union: SuBitGraph::empty(size),
99            canonicalize_external_shifts: false,
100        }
101    }
102}