use super::{
drag::snap_to_step,
frame::facet_normal,
projection::{ScreenPoint, ScreenSize, unproject},
};
use crate::loading::next_free_block_name;
use glam::Vec3;
use indicatrix::{geometry::meet_solver::MeetConstraint, optics::raytracer::Camera};
use indicatrix_cut_core::{ConstraintTier, ScheduleMeta, expected_orbit};
const MIN_SLICE_DRAG_PX: f32 = 4.0;
const SIDE_PROBE_PX: f32 = 10.0;
const MIN_NORMAL_LENGTH: f32 = 1e-6;
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub enum SliceSide {
#[default]
Right,
Left,
}
impl SliceSide {
#[must_use]
pub const fn flipped(self) -> Self {
match self {
Self::Right => Self::Left,
Self::Left => Self::Right,
}
}
}
#[must_use]
pub fn slice_normal(
camera: &Camera,
a: ScreenPoint,
b: ScreenPoint,
size: ScreenSize,
side: SliceSide,
) -> Option<Vec3> {
let (dx, dy) = a.delta_to(b);
let length = dx.hypot(dy);
if !length.is_finite() || length < MIN_SLICE_DRAG_PX {
return None;
}
let dir_a = unproject(camera, a, size).dir;
let dir_b = unproject(camera, b, size).dir;
let cross = dir_a.cross(dir_b);
if !cross.is_finite() || cross.length() < MIN_NORMAL_LENGTH {
return None;
}
let normal = cross.normalize();
let (rx, ry) = (-dy / length, dx / length);
let mid = a.midpoint(b);
let probe = ScreenPoint::new(
SIDE_PROBE_PX.mul_add(rx, mid.x),
SIDE_PROBE_PX.mul_add(ry, mid.y),
);
let probe_dir = unproject(camera, probe, size).dir;
let right_oriented = if probe_dir.dot(normal) > 0.0 {
normal
} else {
-normal
};
Some(match side {
SliceSide::Right => right_oriented,
SliceSide::Left => -right_oriented,
})
}
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct SnappedFacet {
pub angle_deg: f64,
pub index: f64,
pub normal: Vec3,
}
#[must_use]
pub fn snap_to_gear(normal: Vec3, gear_teeth: u32, angle_step_deg: f64) -> SnappedFacet {
let gear = gear_teeth.max(1);
let gear_f = f64::from(gear);
let theta = f64::from(normal.y.abs().clamp(0.0, 1.0))
.acos()
.to_degrees();
let theta = snap_to_step(theta, angle_step_deg);
let angle_deg = if normal.y >= 0.0 { theta } else { -theta };
let azimuth = f64::from(normal.z).atan2(f64::from(normal.x));
let wrapped = (azimuth * gear_f / std::f64::consts::TAU)
.round()
.rem_euclid(gear_f);
let index = if wrapped == 0.0 { 0.0 } else { wrapped };
SnappedFacet {
angle_deg,
index,
normal: facet_normal(angle_deg, index, gear),
}
}
#[must_use]
pub fn tangency_mast(normal: Vec3, corner_points: &[Vec3]) -> f64 {
let max = corner_points
.iter()
.map(|&p| normal.dot(p))
.max_by(f32::total_cmp);
max.filter(|m| m.is_finite()).map_or(1.0, f64::from)
}
#[must_use]
pub fn slice_tier(
snapped: &SnappedFacet,
mast: f64,
meta: &ScheduleMeta,
symmetric: bool,
existing_names: &[String],
) -> ConstraintTier {
let indices = if symmetric {
expected_orbit(
snapped.index,
meta.symmetry_order,
meta.mirror,
meta.gear_teeth_abs(),
)
} else {
vec![snapped.index]
};
ConstraintTier {
angle_deg: snapped.angle_deg,
name: next_free_block_name(snapped.angle_deg, existing_names),
indices,
constraint: MeetConstraint::ScaleReference(mast),
imported_meet: None,
original_notes: None,
detached: Vec::new(),
}
}