use glam::Vec3;
use indicatrix::geometry::plane::tier_is_crown_side;
use indicatrix_cut_core::ConstraintTier;
fn theta_phi(angle_deg: f64, index_on_gear: f64, gear_teeth: u32) -> (f32, f32) {
let gear = gear_teeth.max(1) as f32;
let theta = (angle_deg.abs() as f32).to_radians();
let phi = 2.0 * std::f32::consts::PI * (index_on_gear as f32) / gear;
(theta, phi)
}
fn sin_cos_phi(phi: f32, indexless: bool) -> (f32, f32) {
if indexless {
(1.0, 0.0)
} else {
(phi.sin(), phi.cos())
}
}
pub(super) fn facet_normal(angle_deg: f64, index_on_gear: f64, gear_teeth: u32) -> Vec3 {
facet_normal_with(angle_deg, index_on_gear, gear_teeth, false)
}
fn facet_normal_with(angle_deg: f64, index_on_gear: f64, gear_teeth: u32, indexless: bool) -> Vec3 {
let (theta, phi) = theta_phi(angle_deg, index_on_gear, gear_teeth);
let (sin_theta, cos_theta) = (theta.sin(), theta.cos());
let (sin_phi, cos_phi) = sin_cos_phi(phi, indexless);
let normal = if tier_is_crown_side(angle_deg) {
Vec3::new(sin_theta * cos_phi, cos_theta, sin_theta * sin_phi)
} else {
Vec3::new(sin_theta * cos_phi, -cos_theta, sin_theta * sin_phi)
};
normal.normalize()
}
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct FacetFrame {
pub centroid: Vec3,
pub normal: Vec3,
pub angle_deg: f64,
pub index_on_gear: f64,
pub gear_teeth: u32,
indexless: bool,
}
impl FacetFrame {
#[must_use]
pub fn from_tier(
tier: &ConstraintTier,
index_on_gear: f64,
gear_teeth: u32,
centroid: Vec3,
) -> Self {
let gear_teeth = gear_teeth.max(1);
let indexless = tier.indices.is_empty();
Self {
centroid,
normal: facet_normal_with(tier.angle_deg, index_on_gear, gear_teeth, indexless),
angle_deg: tier.angle_deg,
index_on_gear,
gear_teeth,
indexless,
}
}
#[must_use]
pub fn from_tier_with_reference(
tier: &ConstraintTier,
index_on_gear: f64,
gear_reference_angle: f32,
gear_teeth: u32,
centroid: Vec3,
) -> Self {
Self::from_tier(
tier,
index_on_gear + f64::from(gear_reference_angle),
gear_teeth,
centroid,
)
}
#[must_use]
pub const fn is_indexless(&self) -> bool {
self.indexless
}
#[must_use]
pub const fn is_crown(&self) -> bool {
tier_is_crown_side(self.angle_deg)
}
#[must_use]
pub fn tangent_theta(&self) -> Vec3 {
let (theta, phi) = theta_phi(self.angle_deg, self.index_on_gear, self.gear_teeth);
let (sin_theta, cos_theta) = (theta.sin(), theta.cos());
let (sin_phi, cos_phi) = sin_cos_phi(phi, self.indexless);
let y = if self.is_crown() {
-sin_theta
} else {
sin_theta
};
Vec3::new(cos_theta * cos_phi, y, cos_theta * sin_phi)
}
#[must_use]
pub fn tangent_phi(&self) -> Vec3 {
let (_, phi) = theta_phi(self.angle_deg, self.index_on_gear, self.gear_teeth);
let (sin_phi, cos_phi) = sin_cos_phi(phi, self.indexless);
Vec3::new(-sin_phi, 0.0, cos_phi)
}
}