euv-core 0.56.0

Core cross-platform runtime for euv featuring arena-based fine-grained reactivity, deterministic layout, flat scene graph, multi-mode rendering, and direct OS platform adapters.
Documentation
use super::*;

/// Quadric plane construction, addition, evaluation, and optimal contraction solver.
impl QuadricErrorMatrix {
    /// Builds a weighted quadric matrix from plane equation `ax + by + cz + d = 0`.
    ///
    /// # Arguments
    ///
    /// - `f32` - Plane normal X component.
    /// - `f32` - Plane normal Y component.
    /// - `f32` - Plane normal Z component.
    /// - `f32` - Plane signed offset `d`.
    /// - `f32` - Positive weighting factor.
    ///
    /// # Returns
    ///
    /// - `Self` - Constructed symmetric quadric matrix.
    #[must_use]
    pub(crate) fn from_plane(a: f32, b: f32, c: f32, d: f32, weight: f32) -> Self {
        let w: f32 = if weight.is_finite() && weight > 0.0 {
            weight
        } else {
            1.0
        };
        Self {
            a00: w * a * a,
            a01: w * a * b,
            a02: w * a * c,
            a03: w * a * d,
            a11: w * b * b,
            a12: w * b * c,
            a13: w * b * d,
            a22: w * c * c,
            a23: w * c * d,
            a33: w * d * d,
        }
    }

    /// Adds two symmetric quadric matrices component-wise.
    ///
    /// # Arguments
    ///
    /// - `Self` - Other quadric matrix to accumulate.
    ///
    /// # Returns
    ///
    /// - `Self` - Combined quadric matrix.
    #[must_use]
    pub(crate) fn combine(self, rhs: Self) -> Self {
        Self {
            a00: self.get_a00() + rhs.get_a00(),
            a01: self.get_a01() + rhs.get_a01(),
            a02: self.get_a02() + rhs.get_a02(),
            a03: self.get_a03() + rhs.get_a03(),
            a11: self.get_a11() + rhs.get_a11(),
            a12: self.get_a12() + rhs.get_a12(),
            a13: self.get_a13() + rhs.get_a13(),
            a22: self.get_a22() + rhs.get_a22(),
            a23: self.get_a23() + rhs.get_a23(),
            a33: self.get_a33() + rhs.get_a33(),
        }
    }

    /// Evaluates the non-negative quadratic error `v^T Q v` at 3D point `point`.
    ///
    /// # Arguments
    ///
    /// - `Vec3` - Query point in mesh local space.
    ///
    /// # Returns
    ///
    /// - `f32` - Clamped non-negative finite quadric error.
    #[must_use]
    pub(crate) fn evaluate(self, point: Vec3) -> f32 {
        let x: f32 = point.get_x();
        let y: f32 = point.get_y();
        let z: f32 = point.get_z();
        let err: f32 = self.get_a00() * x * x
            + 2.0 * self.get_a01() * x * y
            + 2.0 * self.get_a02() * x * z
            + 2.0 * self.get_a03() * x
            + self.get_a11() * y * y
            + 2.0 * self.get_a12() * y * z
            + 2.0 * self.get_a13() * y
            + self.get_a22() * z * z
            + 2.0 * self.get_a23() * z
            + self.get_a33();
        if err.is_finite() {
            err.max(0.0)
        } else {
            f32::MAX
        }
    }

    /// Computes the optimal contraction position and QEM cost for collapsing `p0` and `p1`.
    ///
    /// # Arguments
    ///
    /// - `Vec3` - First endpoint position.
    /// - `Vec3` - Second endpoint position.
    ///
    /// # Returns
    ///
    /// - `(Vec3, f32)` - Optimal vertex position and corresponding quadric error.
    #[must_use]
    pub(crate) fn solve_optimal_point(self, p0: Vec3, p1: Vec3) -> (Vec3, f32) {
        let m00: f32 = self.get_a00();
        let m01: f32 = self.get_a01();
        let m02: f32 = self.get_a02();
        let m11: f32 = self.get_a11();
        let m12: f32 = self.get_a12();
        let m22: f32 = self.get_a22();
        let c00: f32 = m11 * m22 - m12 * m12;
        let c01: f32 = m02 * m12 - m01 * m22;
        let c02: f32 = m01 * m12 - m02 * m11;
        let det: f32 = m00 * c00 + m01 * c01 + m02 * c02;
        let mid: Vec3 = p0.lerp(p1, 0.5);
        let edge_len: f32 = p1.sub(p0).length();
        let mut best_pos: Vec3 = mid;
        let mut best_err: f32 = self.evaluate(mid);
        let err0: f32 = self.evaluate(p0);
        if err0 < best_err {
            best_pos = p0;
            best_err = err0;
        }
        let err1: f32 = self.evaluate(p1);
        if err1 < best_err {
            best_pos = p1;
            best_err = err1;
        }
        if det.is_finite() && det.abs() > MESH_OPS_DET_EPSILON {
            let inv_det: f32 = 1.0 / det;
            let c11: f32 = m00 * m22 - m02 * m02;
            let c12: f32 = m01 * m02 - m00 * m12;
            let c22: f32 = m00 * m11 - m01 * m01;
            let rx: f32 = -self.get_a03();
            let ry: f32 = -self.get_a13();
            let rz: f32 = -self.get_a23();
            let sx: f32 = (c00 * rx + c01 * ry + c02 * rz) * inv_det;
            let sy: f32 = (c01 * rx + c11 * ry + c12 * rz) * inv_det;
            let sz: f32 = (c02 * rx + c12 * ry + c22 * rz) * inv_det;
            if sx.is_finite() && sy.is_finite() && sz.is_finite() {
                let candidate: Vec3 = Vec3::new(sx, sy, sz);
                let dist_from_mid: f32 = candidate.sub(mid).length();
                if dist_from_mid <= edge_len * 2.0 + 1e-3 {
                    let cand_err: f32 = self.evaluate(candidate);
                    if cand_err < best_err {
                        best_pos = candidate;
                        best_err = cand_err;
                    }
                }
            }
        }
        (best_pos, best_err)
    }
}