use super::helpers::{curve_from_inscribed_circles, find_tmax_circle, reverse_inscribed_circles};
use crate::airfoil::{CamberOrient, InscribedCircle};
use crate::{Curve2, Result, Vector2};
pub struct TMaxFwd {}
impl Default for TMaxFwd {
fn default() -> Self {
Self::new()
}
}
impl TMaxFwd {
pub fn new() -> Self {
TMaxFwd {}
}
pub fn make() -> Box<dyn CamberOrient> {
Box::new(TMaxFwd::new())
}
}
impl CamberOrient for TMaxFwd {
fn orient_camber_line(
&self,
_section: &Curve2,
stations: Vec<InscribedCircle>,
) -> Result<Vec<InscribedCircle>> {
let mut moved = stations;
let camber = curve_from_inscribed_circles(&moved, 1e-4)?;
let tmax = find_tmax_circle(&moved)
.ok_or("Empty inscribed circles container during tmax forward orientation.")?;
let l = camber.at_closest_to_point(&tmax.center()).length_along();
let fraction = l / camber.length();
if fraction > 0.5 {
reverse_inscribed_circles(&mut moved);
}
Ok(moved)
}
}
pub struct DirectionFwd {
pub direction: Vector2,
}
impl DirectionFwd {
pub fn new(direction: Vector2) -> Self {
DirectionFwd { direction }
}
pub fn make(direction: Vector2) -> Box<dyn CamberOrient> {
Box::new(DirectionFwd::new(direction))
}
}
impl CamberOrient for DirectionFwd {
fn orient_camber_line(
&self,
_section: &Curve2,
stations: Vec<InscribedCircle>,
) -> Result<Vec<InscribedCircle>> {
let mut moved = stations;
let c0 = moved
.first()
.ok_or("Empty inscribed circles container during camber orientation.")?
.circle
.center;
let c1 = moved
.last()
.ok_or("Empty inscribed circles container during camber orientation.")?
.circle
.center;
if self.direction.dot(&c0.coords) < self.direction.dot(&c1.coords) {
reverse_inscribed_circles(&mut moved);
}
Ok(moved)
}
}