File size: 2,915 Bytes
75a77d0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a987df5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4536b63
72b1843
ab47e29
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
//! Native analytic geometry policy; original-editor numerical parity is unverified.
pub mod math;
pub use math::{Aabb, DVec3, Equation, Policy};
use std::fmt;
#[derive(Clone, Debug, PartialEq, Eq)]
pub enum Error {
    NonFinite,
    Degenerate,
    InvalidPolicy,
    InvalidDimensions,
    InvalidBrush,
    Limit,
    Allocation,
    Overflow,
    FloatRange,
    InconsistentData,
}
impl fmt::Display for Error {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "geometry: {self:?}")
    }
}
impl std::error::Error for Error {}
pub type Result<T> = std::result::Result<T, Error>;
pub(crate) fn alloc<T>(n: usize) -> Result<Vec<T>> {
    let mut out = Vec::new();
    out.try_reserve_exact(n).map_err(|_| Error::Allocation)?;
    Ok(out)
}

pub mod brush;
#[derive(Clone, Copy, Debug)]
pub struct ComputeLimits {
    pub max_planes: usize,
    pub max_vertices: usize,
    pub max_work: usize,
    pub max_owned_bytes: usize,
}
impl Default for ComputeLimits {
    fn default() -> Self {
        Self {
            max_planes: 64,
            max_vertices: 100_000,
            max_work: 10_000_000,
            max_owned_bytes: 128 * 1024 * 1024,
        }
    }
}
#[derive(Clone, Copy, Debug, Default)]
pub struct Stats {
    pub work: usize,
    pub owned: usize,
    pub vertices: usize,
}
pub(crate) struct Budget {
    pub limits: ComputeLimits,
    pub stats: Stats,
}
impl Budget {
    fn work(&mut self, n: usize) -> Result<()> {
        self.stats.work = self.stats.work.checked_add(n).ok_or(Error::Overflow)?;
        if self.stats.work > self.limits.max_work {
            Err(Error::Limit)
        } else {
            Ok(())
        }
    }
    fn bytes(&mut self, n: usize) -> Result<()> {
        self.stats.owned = self.stats.owned.checked_add(n).ok_or(Error::Overflow)?;
        if self.stats.owned > self.limits.max_owned_bytes {
            Err(Error::Limit)
        } else {
            Ok(())
        }
    }
    fn reserve<T>(&mut self, n: usize) -> Result<Vec<T>> {
        self.bytes(
            n.checked_mul(std::mem::size_of::<T>())
                .ok_or(Error::Overflow)?,
        )?;
        alloc(n)
    }
    fn push<T>(&mut self, out: &mut Vec<T>, v: T) -> Result<()> {
        if out.len() == out.capacity() {
            let cap = out
                .capacity()
                .saturating_mul(2)
                .max(1)
                .min(self.limits.max_vertices);
            if cap <= out.len() {
                return Err(Error::Limit);
            }
            self.bytes(
                (cap - out.capacity())
                    .checked_mul(std::mem::size_of::<T>())
                    .ok_or(Error::Overflow)?,
            )?;
            out.try_reserve_exact(cap - out.len())
                .map_err(|_| Error::Allocation)?;
        }
        out.push(v);
        Ok(())
    }
}
pub mod patch;
pub mod ray;
pub mod world;