Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement orient3d + insphere #23

Merged
merged 10 commits into from
Mar 27, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
71 changes: 24 additions & 47 deletions examples/predicate-map/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,19 +21,13 @@ fn simple_incircle(a: Coord<f64>, b: Coord<f64>, c: Coord<f64>, d: Coord<f64>) -
let m32 = c.y - d.y;
let m33 = m31.powi(2) + m32.powi(2);

m11 * (m22 * m33 - m23 * m32)
- m12 * (m21 * m33 - m23 * m31)
+ m13 * (m21 * m32 - m22 * m31)
m11 * (m22 * m33 - m23 * m32) - m12 * (m21 * m33 - m23 * m31) + m13 * (m21 * m32 - m22 * m31)
}


use std::cmp::Ordering;
fn orient2d_tests<F>(
predicate: F,
start: Coord<f64>,
width: usize, height: usize,
) -> Vec<Ordering>
where F: Fn(Coord<f64>) -> f64
fn orient2d_tests<F>(predicate: F, start: Coord<f64>, width: usize, height: usize) -> Vec<Ordering>
where
F: Fn(Coord<f64>) -> f64,
{
use float_extras::f64::nextafter;
let mut yd = start.y;
Expand All @@ -42,10 +36,7 @@ where F: Fn(Coord<f64>) -> f64
for _ in 0..height {
let mut xd = start.x;
for _ in 0..width {
let p = Coord{
x: xd,
y: yd,
};
let p = Coord { x: xd, y: yd };
data.push(predicate(p).partial_cmp(&0.).unwrap());
xd = nextafter(xd, std::f64::INFINITY);
}
Expand All @@ -56,11 +47,7 @@ where F: Fn(Coord<f64>) -> f64
}

use std::path::Path;
fn write_png(
data: &[Ordering],
path: &Path,
width: usize, height: usize,
) {
fn write_png(data: &[Ordering], path: &Path, width: usize, height: usize) {
assert_eq!(data.len(), width * height);

use std::fs::File;
Expand All @@ -74,18 +61,22 @@ fn write_png(
encoder.set_depth(png::BitDepth::Eight);

let mut writer = encoder.write_header().unwrap();
let data = data.iter().map(|w| {
match w {
let data = data
.iter()
.map(|w| match w {
Ordering::Less => 0u8,
Ordering::Equal => 127,
Ordering::Greater => 255,
}
}).collect::<Vec<_>>();
})
.collect::<Vec<_>>();
writer.write_image_data(&data).unwrap();
}

fn usage(name: &str) -> ! {
eprintln!("Usage: {} {{naive | robust}} {{incircle | orient2d}} <output.png>", name);
eprintln!(
"Usage: {} {{naive | robust}} {{incircle | orient2d}} <output.png>",
name
);
std::process::exit(1);
}

Expand All @@ -95,31 +86,17 @@ fn main() {
usage(&args[0])
}

let p1 = Coord{x: 12., y: 12.};
let p2 = Coord{x: 24., y: 24.};
let p3 = Coord{x: -12., y: -12.};
let p1 = Coord { x: 12., y: 12. };
let p2 = Coord { x: 24., y: 24. };
let p3 = Coord { x: -12., y: -12. };
let predicate: Box<dyn Fn(Coord<f64>) -> f64> = match (args[1].as_str(), args[2].as_str()) {
("naive", "incircle") => {
Box::new(|p| simple_incircle(p1, p3, p2, p))
},
("naive", "orient2d") => {
Box::new(|p| simple_orient2d(p1, p, p2))
},
("robust", "incircle") => {
Box::new(|p| robust::incircle(p1, p3, p2, p))
},
("robust", "orient2d") => {
Box::new(|p| robust::orient2d(p1, p, p2))
},
_ => {
usage(&args[0])
}
("naive", "incircle") => Box::new(|p| simple_incircle(p1, p3, p2, p)),
("naive", "orient2d") => Box::new(|p| simple_orient2d(p1, p, p2)),
("robust", "incircle") => Box::new(|p| robust::incircle(p1, p3, p2, p)),
("robust", "orient2d") => Box::new(|p| robust::orient2d(p1, p, p2)),
_ => usage(&args[0]),
};

let data = orient2d_tests(
predicate,
Coord{x: 0.5, y: 0.5},
256, 256,
);
let data = orient2d_tests(predicate, Coord { x: 0.5, y: 0.5 }, 256, 256);
write_png(&data, Path::new(&args[3]), 256, 256);
}
Loading