use crate::curves::Curve3;
use crate::math::Vector3;
pub struct Catenary3 {
p0: Vector3,
p1: Vector3,
down: Vector3,
a: f32,
span: f32,
}
impl Catenary3 {
pub fn new(p0: Vector3, p1: Vector3, down: Vector3, sag: f32) -> Self {
let chord = p1 - p0;
let down = if down.length() > 1e-9 {
down.normalize()
} else {
Vector3::new(0.0, -1.0, 0.0)
};
let along = chord - down * chord.dot(down);
let span = along.length().max(1e-6);
let (mut lo, mut hi) = (1e-3f32, span * 1e3);
for _ in 0..60 {
let mid = 0.5 * (lo + hi);
let s = mid * ((span / (2.0 * mid)).cosh() - 1.0);
if s > sag {
lo = mid
} else {
hi = mid
}
}
Self {
p0,
p1,
down,
a: 0.5 * (lo + hi),
span,
}
}
}
impl Curve3 for Catenary3 {
fn get_point(&self, t: f32) -> Vector3 {
let t = t.clamp(0.0, 1.0);
let chord = self.p1 - self.p0;
let straight = self.p0 + chord * t;
let x = (t - 0.5) * self.span;
let d = self.a * ((x / self.a).cosh() - (self.span / (2.0 * self.a)).cosh());
straight + self.down * (-d)
}
}
pub struct Elastica3 {
pts: Vec<Vector3>,
}
impl Elastica3 {
pub fn clamped(
p0: Vector3,
t0: Vector3,
p1: Vector3,
t1: Vector3,
slack: f32,
samples: usize,
) -> Self {
let n = samples.max(8);
let chord = (p1 - p0).length();
let total = chord + slack.max(0.0);
let seg = total / (n - 1) as f32;
let t0 = if t0.length() > 1e-9 {
t0.normalize()
} else {
(p1 - p0).normalize()
};
let t1 = if t1.length() > 1e-9 {
t1.normalize()
} else {
(p1 - p0).normalize()
};
let chord_dir = if chord > 1e-9 {
(p1 - p0) * (1.0 / chord)
} else {
t0
};
let lean = (t0 - chord_dir * t0.dot(chord_dir)) + (t1 - chord_dir * t1.dot(chord_dir));
let bulge = if lean.length() > 1e-6 {
lean.normalize()
} else {
let seed = if chord_dir.x.abs() < 0.9 {
Vector3::new(1.0, 0.0, 0.0)
} else {
Vector3::new(0.0, 1.0, 0.0)
};
(seed - chord_dir * seed.dot(chord_dir)).normalize()
};
let amp = (4.0 * slack.max(0.0) * chord.max(1e-6)
/ (std::f32::consts::PI * std::f32::consts::PI))
.sqrt();
let scale = total;
let mut pts: Vec<Vector3> = (0..n)
.map(|i| {
let u = i as f32 / (n - 1) as f32;
let (u2, u3) = (u * u, u * u * u);
let h00 = 2.0 * u3 - 3.0 * u2 + 1.0;
let h10 = u3 - 2.0 * u2 + u;
let h01 = -2.0 * u3 + 3.0 * u2;
let h11 = u3 - u2;
let base = p0 * h00 + t0 * (h10 * scale) + p1 * h01 + t1 * (h11 * scale);
base + bulge * (amp * (u * std::f32::consts::PI).sin())
})
.collect();
let a0 = p0;
let a1 = p0 + t0 * seg;
let b1 = p1 - t1 * seg;
let b0 = p1;
let iters = (n * n / 2).clamp(2000, 200_000);
for _ in 0..iters {
let prev = pts.clone();
for i in 1..n - 1 {
let mid = (prev[i - 1] + prev[i + 1]) * 0.5;
pts[i] = prev[i] + (mid - prev[i]) * 0.08;
}
pts[0] = a0;
pts[1] = a1;
pts[n - 2] = b1;
pts[n - 1] = b0;
for _ in 0..12 {
for i in 1..n {
let d = pts[i] - pts[i - 1];
let l = d.length().max(1e-9);
let fix = d * ((l - seg) / l);
if i > 1 && i < n - 1 {
pts[i] = pts[i] - fix * 0.5;
pts[i - 1] = pts[i - 1] + fix * 0.5;
} else if i == 1 {
pts[i] = pts[i] - fix;
} else {
pts[i - 1] = pts[i - 1] + fix;
}
}
pts[0] = a0;
pts[1] = a1;
pts[n - 2] = b1;
pts[n - 1] = b0;
}
}
Self { pts }
}
pub fn points(&self) -> &[Vector3] {
&self.pts
}
pub fn min_bend_radius_over(&self, baseline: f32) -> f32 {
let n = self.pts.len();
if n < 3 {
return f32::INFINITY;
}
let seg = (self.pts[1] - self.pts[0]).length().max(1e-9);
let k = ((baseline / seg).round() as usize).clamp(1, (n - 1) / 2);
let mut worst = f32::INFINITY;
for i in k..n - k {
let (a, b, c) = (self.pts[i - k], self.pts[i], self.pts[i + k]);
let (u, v) = (b - a, c - b);
let (lu, lv) = (u.length(), v.length());
if lu < 1e-9 || lv < 1e-9 {
continue;
}
let cross = u.cross(v).length();
if cross < 1e-12 {
continue;
}
let r = (lu * lv * (c - a).length()) / (2.0 * cross);
if r < worst {
worst = r;
}
}
worst
}
pub fn min_bend_radius(&self) -> f32 {
let mut worst = f32::INFINITY;
for w in self.pts.windows(3) {
let (a, b, c) = (w[0], w[1], w[2]);
let (u, v) = (b - a, c - b);
let (lu, lv) = (u.length(), v.length());
if lu < 1e-9 || lv < 1e-9 {
continue;
}
let cross = u.cross(v).length();
if cross < 1e-12 {
continue;
}
let r = (lu * lv * (c - a).length()) / (2.0 * cross);
if r < worst {
worst = r;
}
}
worst
}
}
impl Curve3 for Elastica3 {
fn get_point(&self, t: f32) -> Vector3 {
let n = self.pts.len();
let s = t.clamp(0.0, 1.0) * (n - 1) as f32;
let i = (s.floor() as usize).min(n - 1);
let j = (i + 1).min(n - 1);
let f = s - i as f32;
self.pts[i] + (self.pts[j] - self.pts[i]) * f
}
}
pub struct ServiceHelix3 {
origin: Vector3,
axis: Vector3,
radial: Vector3,
radius: f32,
length: f32,
turns: f32,
}
impl ServiceHelix3 {
pub fn new(origin: Vector3, axis: Vector3, radius: f32, length: f32, turns: f32) -> Self {
let axis = if axis.length() > 1e-9 {
axis.normalize()
} else {
Vector3::new(0.0, 0.0, 1.0)
};
let seed = if axis.x.abs() < 0.9 {
Vector3::new(1.0, 0.0, 0.0)
} else {
Vector3::new(0.0, 1.0, 0.0)
};
let radial = (seed - axis * seed.dot(axis)).normalize();
Self {
origin,
axis,
radial,
radius,
length,
turns,
}
}
pub fn twist_rate(&self) -> f32 {
if self.length.abs() < 1e-9 {
f32::INFINITY
} else {
self.turns * 360.0 / self.length
}
}
}
impl Curve3 for ServiceHelix3 {
fn get_point(&self, t: f32) -> Vector3 {
let t = t.clamp(0.0, 1.0);
let ang = t * self.turns * std::f32::consts::TAU;
let bi = self.axis.cross(self.radial);
self.origin
+ self.axis * (self.length * t)
+ self.radial * (self.radius * ang.cos())
+ bi * (self.radius * ang.sin())
}
}
#[derive(Debug, Clone, Copy)]
pub struct WireSpec {
pub gauge: f32,
pub insulation: f32,
pub code: WireCode,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum WireCode {
Positive,
Return,
Bond,
SignalA,
SignalB,
Screen,
}
impl WireCode {
pub fn rgb(self) -> [f32; 3] {
match self {
WireCode::Positive => [0.55, 0.05, 0.05],
WireCode::Return => [0.04, 0.04, 0.045],
WireCode::Bond => [0.06, 0.30, 0.10],
WireCode::SignalA => [0.80, 0.80, 0.82],
WireCode::SignalB => [0.08, 0.16, 0.50],
WireCode::Screen => [0.35, 0.36, 0.38],
}
}
pub fn name(self) -> &'static str {
match self {
WireCode::Positive => "red / +28V",
WireCode::Return => "black / return",
WireCode::Bond => "green / bond",
WireCode::SignalA => "white / signal A",
WireCode::SignalB => "blue / signal B",
WireCode::Screen => "grey / screen",
}
}
}
impl WireSpec {
pub fn new(gauge: f32, insulation: f32, code: WireCode) -> Self {
Self {
gauge,
insulation,
code,
}
}
pub fn conductor_diameter(&self) -> f32 {
0.127 * 92f32.powf((36.0 - self.gauge) / 39.0)
}
pub fn conductor_radius(&self) -> f32 {
self.conductor_diameter() * 0.5
}
pub fn outer_diameter(&self) -> f32 {
self.conductor_diameter() + 2.0 * self.insulation
}
pub fn mass_per_mm(&self) -> f32 {
let rc = self.conductor_radius();
let ro = self.outer_diameter() * 0.5;
let a_cu = std::f32::consts::PI * rc * rc * 0.95;
let a_ins = std::f32::consts::PI * (ro * ro - rc * rc);
(a_cu * 8960.0 + a_ins * 2200.0) * 1e-9
}
pub fn ohms_per_mm(&self) -> f32 {
let rc = self.conductor_radius();
let a = std::f32::consts::PI * rc * rc * 0.95;
1.68e-8 / (a * 1e-6) * 1e-3
}
pub fn conductor(&self, path: &dyn Curve3, segments: usize) -> crate::BufferGeometry {
crate::geometries::TubeGeometry::new(path, segments, self.conductor_radius(), 8, false)
}
pub fn jacket(&self, path: &dyn Curve3, segments: usize) -> crate::BufferGeometry {
crate::geometries::TubeGeometry::new(path, segments, self.outer_diameter() * 0.5, 10, false)
}
}
pub fn power_pair(gauge: f32, insulation: f32) -> [WireSpec; 2] {
[
WireSpec::new(gauge, insulation, WireCode::Positive),
WireSpec::new(gauge, insulation, WireCode::Return),
]
}