use crate::Resample::ByCount;
use crate::airfoil2::inscribed::{Inscribed, InscribedVec};
use crate::{Line2, Result, Vector2};
#[derive(Debug, Clone, Copy, PartialEq)]
pub enum OrientFwdAft {
TmaxFwd,
Airflow(Vector2),
Fwd(Vector2),
}
impl OrientFwdAft {
pub fn apply(&self, circles: Vec<Inscribed>) -> Result<Vec<Inscribed>> {
let mut working = InscribedVec::new(circles);
working.throw_if_less_than(2)?;
if match self {
OrientFwdAft::TmaxFwd => check_tmax_fwd(&working)?,
OrientFwdAft::Airflow(dir) => check_dir_fwd(&working, -dir)?,
OrientFwdAft::Fwd(dir) => check_dir_fwd(&working, *dir)?,
} {
working.reverse_order_only();
}
Ok(working.take_vec())
}
}
#[derive(Debug, Clone, Copy, PartialEq)]
pub enum OrientUpperLower {
Curvature,
Upper(Vector2),
Lower(Vector2),
}
impl OrientUpperLower {
pub fn apply(&self, circles: Vec<Inscribed>) -> Result<Vec<Inscribed>> {
let mut working = InscribedVec::new(circles);
working.throw_if_less_than(2)?;
if match self {
OrientUpperLower::Curvature => check_curvature_upper(&working)?,
OrientUpperLower::Upper(dir) => check_dir_upper(&working, *dir)?,
OrientUpperLower::Lower(dir) => check_dir_upper(&working, -dir)?,
} {
working.reverse_points();
}
Ok(working.take_vec())
}
}
fn check_dir_upper(working: &InscribedVec, dir: Vector2) -> Result<bool> {
let mut vote_flip = 0;
for c in working.iter() {
if !dir.dot(&c.contact_dir()).is_sign_positive() {
vote_flip += 1;
}
}
Ok(vote_flip > working.len() - vote_flip)
}
fn check_curvature_upper(working: &InscribedVec) -> Result<bool> {
let tol = working.average_spacing()? * 5e-2;
let camber = working.camber_curve(tol)?;
let points = camber.resample(ByCount(50))?.points().to_vec();
let line = Line2::from_points(&points[0], &points[points.len() - 1]);
let mut to_right = 0;
for p in points.iter() {
if line.signed_distance_to(p).is_sign_positive() {
to_right += 1;
}
}
let upper_to_right = to_right > points.len() - to_right;
let mut vote_flip = 0;
for c in working.iter() {
let sp = camber.at_closest_to_point(&c.center()).surface_point();
if upper_to_right != sp.scalar_projection(&c.p1).is_sign_positive() {
vote_flip += 1;
}
}
Ok(vote_flip > working.len() - vote_flip)
}
fn check_dir_fwd(working: &InscribedVec, dir: Vector2) -> Result<bool> {
let (front, back) = working.front_and_back()?;
Ok(dir.dot(&front.center().coords) < dir.dot(&back.center().coords))
}
fn check_tmax_fwd(working: &InscribedVec) -> Result<bool> {
let tol = working.average_spacing()? * 5e-2;
let camber = working.camber_curve(tol)?;
let i_max = working.index_of_tmax();
let c = camber.at_closest_to_point(&working[i_max].c.center);
if c.length_along() < camber.length() * 0.5 {
Ok(false)
} else {
Ok(true)
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::{Circle2, Point2};
fn ic(x: f64, y: f64, r: f64) -> Inscribed {
Inscribed::new(
Circle2::new(x, y, r),
Point2::new(x, y - r),
Point2::new(x, y + r),
)
}
fn quick() -> InscribedVec {
InscribedVec::new(vec![
ic(-1.0, 0.0, 1.0),
ic(0.0, 0.0, 2.0),
ic(2.0, 0.0, 1.0),
])
}
#[test]
fn tmax_fwd() -> Result<()> {
let mut data = quick();
assert!(!check_tmax_fwd(&data)?);
data.reverse_order();
assert!(check_tmax_fwd(&data)?);
Ok(())
}
#[test]
fn dir_fwd() -> Result<()> {
let mut data = quick();
let v = Vector2::x();
assert!(check_dir_fwd(&data, v)?);
data.reverse_order();
assert!(!check_dir_fwd(&data, v)?);
Ok(())
}
#[test]
fn dir_upper() -> Result<()> {
let mut data = quick();
let v = Vector2::y();
assert!(!check_dir_upper(&data, v)?);
data.reverse_order();
assert!(check_dir_upper(&data, v)?);
Ok(())
}
}