use std::fs::File;
use std::io::{BufRead, BufReader};
use std::path::PathBuf;
use glam::Vec2;
#[derive(Copy, Clone, Debug)]
pub enum NumPoints {
OneHundred,
OneThousand,
TenThousand,
OneHundredThousand,
}
impl TryFrom<u32> for NumPoints {
type Error = &'static str;
fn try_from(value: u32) -> Result<Self, Self::Error> {
match value {
100 => Ok(Self::OneHundred),
1000 => Ok(Self::OneThousand),
10_000 => Ok(Self::TenThousand),
100_000 => Ok(Self::OneHundredThousand),
_ => Err("Invalid number of points"),
}
}
}
impl From<&NumPoints> for usize {
fn from(value: &NumPoints) -> Self {
match value {
NumPoints::OneHundred => 100,
NumPoints::OneThousand => 1000,
NumPoints::TenThousand => 10_000,
NumPoints::OneHundredThousand => 100_000,
}
}
}
impl From<NumPoints> for usize {
fn from(value: NumPoints) -> Self {
Self::from(&value)
}
}
pub fn load_points(number: &NumPoints, max_value: f32) -> Vec<Vec2> {
let number: usize = number.into();
let mut points_path: PathBuf = env!("CARGO_MANIFEST_DIR").into();
points_path.push("test_data");
points_path.push(format!("{number}-points.txt"));
let reader = BufReader::new(File::open(points_path).unwrap());
let mut points = Vec::with_capacity(number);
for line in reader.lines() {
let line = line.unwrap();
let mut numbers = line.split_whitespace();
let x: f32 = numbers.next().unwrap().parse().unwrap();
assert!(x.is_finite());
assert!(x >= 0.);
assert!(x <= 1.);
let y: f32 = numbers.next().unwrap().parse().unwrap();
assert!(y.is_finite());
assert!(y >= 0.);
assert!(y <= 1.);
points.push(max_value * 2. * (Vec2::new(x, y) - 0.5));
}
points
}