use math_vector::Vector;
pub struct Rectangle {
pub w: f64,
pub h: f64,
}
impl super::SignedDistanceField for Rectangle {
fn call(&self, p: Vector<f64>) -> f64 {
let half_size = Vector {
x: 0.5 * self.w,
y: 0.5 * self.h,
z: 0.0,
};
let component_wise_edge_distance = Vector {
x: p.x.abs() - half_size.x,
y: p.y.abs() - half_size.y,
z: 0.0,
};
let outside_distance = Vector {
x: component_wise_edge_distance.x.max(0.0),
y: component_wise_edge_distance.y.max(0.0),
z: 0.0,
}
.length();
let inside_distance = component_wise_edge_distance
.x
.max(component_wise_edge_distance.y);
outside_distance + inside_distance
}
}
pub struct Circle {
pub r: f64,
}
impl super::SignedDistanceField for Circle {
fn call(&self, p: Vector<f64>) -> f64 {
p.length() - self.r
}
}
pub struct Straight {}
impl super::SignedDistanceField for Straight {
fn call(&self, p: Vector<f64>) -> f64 {
p.y.abs()
}
}
pub struct Line {
pub l: f64,
}
impl super::SignedDistanceField for Line {
fn call(&self, p: Vector<f64>) -> f64 {
if -0.5 * self.l > p.x {
(Vector {
x: -0.5 * self.l,
y: 0.0,
z: 0.0,
} - p)
.length()
} else if p.x > 0.5 * self.l {
(Vector {
x: 0.5 * self.l,
y: 0.0,
z: 0.0,
} - p)
.length()
} else {
p.y.abs()
}
}
}
pub struct Plane {}
impl super::SignedDistanceField for Plane {
fn call(&self, _p: Vector<f64>) -> f64 {
0.0
}
}