use crate::math::Vector3;
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub enum Infill {
Rectilinear,
AlignedRectilinear,
Grid,
Triangles,
TriHexagon,
Honeycomb,
Cubic,
QuarterCubic,
Concentric,
}
impl Infill {
pub const ALL: [Infill; 9] = [
Infill::Rectilinear,
Infill::AlignedRectilinear,
Infill::Grid,
Infill::Triangles,
Infill::TriHexagon,
Infill::Honeycomb,
Infill::Cubic,
Infill::QuarterCubic,
Infill::Concentric,
];
pub fn name(self) -> &'static str {
match self {
Infill::Rectilinear => "rectilinear",
Infill::AlignedRectilinear => "aligned-rectilinear",
Infill::Grid => "grid",
Infill::Triangles => "triangles",
Infill::TriHexagon => "tri-hexagon",
Infill::Honeycomb => "honeycomb",
Infill::Cubic => "cubic",
Infill::QuarterCubic => "quarter-cubic",
Infill::Concentric => "concentric",
}
}
pub fn from_name(name: &str) -> Option<Infill> {
Infill::ALL.into_iter().find(|i| i.name() == name)
}
pub fn is_layered(self) -> bool {
matches!(self, Infill::Rectilinear | Infill::QuarterCubic)
}
}
const CUBE_FACES: [[f32; 3]; 3] = [
[0.816_496_6, 0.0, 0.577_350_3],
[-0.408_248_3, std::f32::consts::FRAC_1_SQRT_2, 0.577_350_3],
[-0.408_248_3, -std::f32::consts::FRAC_1_SQRT_2, 0.577_350_3],
];
const CUBE_RISE: f32 = 1.732_050_8;
const ROW: f32 = 0.866_025_4;
pub(crate) fn distance(kind: Infill, p: Vector3, phase: Vector3, cell: Vector3, depth: f32) -> f32 {
let v = p - phase;
const A: [f32; 3] = [0.5, ROW, 0.0];
const B: [f32; 3] = [-0.5, ROW, 0.0];
let pitch = cell.x;
match kind {
Infill::AlignedRectilinear => axis(v.x, cell.x),
Infill::Rectilinear => {
if (v.z / cell.z).floor() as i64 % 2 == 0 {
axis(v.x, cell.x)
} else {
axis(v.y, cell.y)
}
}
Infill::Grid => axis(v.x, cell.x).min(axis(v.y, cell.y)),
Infill::Triangles => axis(v.x, pitch)
.min(family(v, A, 0.0, pitch))
.min(family(v, B, 0.0, pitch)),
Infill::TriHexagon => axis(v.x, pitch)
.min(family(v, A, 1.0 / 3.0, pitch))
.min(family(v, B, 2.0 / 3.0, pitch)),
Infill::Honeycomb => honeycomb(v, pitch),
Infill::Cubic => cube_faces(v, 0.0, pitch),
Infill::QuarterCubic => {
let layer = (v.z / (CUBE_RISE * pitch)).floor() as i64;
let shift = if layer.rem_euclid(2) == 0 { 0.0 } else { 0.5 };
cube_faces(v, shift, pitch)
}
Infill::Concentric => {
let spacing = cell.x.min(cell.y);
axis(depth - spacing * 0.5, spacing)
}
}
}
#[inline]
fn axis(offset: f32, spacing: f32) -> f32 {
let t = offset / spacing.max(1e-12);
(t - t.round()).abs() * spacing
}
#[inline]
fn family(v: Vector3, n: [f32; 3], phase: f32, spacing: f32) -> f32 {
let t = (n[0] * v.x + n[1] * v.y + n[2] * v.z) / spacing.max(1e-12) - phase;
(t - t.round()).abs() * spacing
}
#[inline]
fn cube_faces(v: Vector3, phase: f32, spacing: f32) -> f32 {
family(v, CUBE_FACES[0], phase, spacing)
.min(family(v, CUBE_FACES[1], phase, spacing))
.min(family(v, CUBE_FACES[2], phase, spacing))
}
fn honeycomb(v: Vector3, pitch: f32) -> f32 {
let (x, y) = (v.x / pitch, v.y / (pitch * ROW));
let j0 = y.round() as i64;
let i0 = (x - j0 as f32 * 0.5).round() as i64;
let mut sites = [(0.0f32, Vector3::ZERO); 25];
let mut count = 0;
for dj in -2..=2i64 {
for di in -2..=2i64 {
let (i, j) = (i0 + di, j0 + dj);
let d = Vector3::new(
(x - (i as f32 + j as f32 * 0.5)) * pitch,
(y - j as f32) * pitch * ROW,
0.0,
);
sites[count] = (d.length_sq(), d);
count += 1;
}
}
let mut nearest = 0;
for k in 1..count {
if sites[k].0 < sites[nearest].0 {
nearest = k;
}
}
let (near_sq, near) = sites[nearest];
let mut wall = f32::MAX;
for (k, &(far_sq, far)) in sites.iter().enumerate().take(count) {
if k == nearest {
continue;
}
let span = (near - far).length();
if span > 1e-9 {
wall = wall.min((far_sq - near_sq) / (2.0 * span));
}
}
wall.max(0.0)
}
#[cfg(test)]
mod tests {
use super::*;
const CELL: Vector3 = Vector3::ONE;
fn dist(kind: Infill, p: Vector3) -> f32 {
distance(kind, p, Vector3::ZERO, CELL, 1.0)
}
fn periodic() -> impl Iterator<Item = Infill> {
Infill::ALL.into_iter().filter(|k| *k != Infill::Concentric)
}
fn periods(kind: Infill) -> Vec<Vector3> {
let z = Vector3::new(0.0, 0.0, 1.0);
match kind {
Infill::AlignedRectilinear => vec![Vector3::new(1.0, 0.0, 0.0), z * 5.0],
Infill::Rectilinear => vec![
Vector3::new(1.0, 0.0, 0.0),
Vector3::new(0.0, 1.0, 0.0),
z * 2.0,
],
Infill::Grid => vec![
Vector3::new(1.0, 0.0, 0.0),
Vector3::new(0.0, 1.0, 0.0),
z * 7.0,
],
Infill::Triangles | Infill::TriHexagon => vec![
Vector3::new(2.0, 0.0, 0.0),
Vector3::new(0.0, 1.0 / ROW, 0.0),
z * 3.0,
],
Infill::Honeycomb => vec![
Vector3::new(1.0, 0.0, 0.0),
Vector3::new(0.0, 2.0 * ROW, 0.0),
z * 4.0,
],
Infill::Cubic => vec![
Vector3::new(
CUBE_FACES[0][0] - CUBE_FACES[1][0],
CUBE_FACES[0][1] - CUBE_FACES[1][1],
CUBE_FACES[0][2] - CUBE_FACES[1][2],
),
z * CUBE_RISE,
],
Infill::QuarterCubic => vec![
Vector3::new(
CUBE_FACES[0][0] - CUBE_FACES[1][0],
CUBE_FACES[0][1] - CUBE_FACES[1][1],
CUBE_FACES[0][2] - CUBE_FACES[1][2],
),
z * (2.0 * CUBE_RISE),
],
Infill::Concentric => vec![],
}
}
#[test]
fn every_pattern_repeats_on_its_own_lattice() {
for kind in periodic() {
for p in [
Vector3::new(0.21, 0.37, 0.63),
Vector3::new(0.5, 0.5, 0.1),
Vector3::new(0.83, 0.04, 0.29),
] {
let base = dist(kind, p);
for shift in periods(kind) {
for repeat in [1.0, -1.0, 3.0] {
let moved = dist(kind, p + shift * repeat);
assert!(
(base - moved).abs() < 1e-4,
"{} is not periodic over {shift:?}: {base} vs {moved}",
kind.name()
);
}
}
}
}
}
#[test]
fn walls_pass_through_the_cell_origin() {
for kind in periodic().filter(|k| *k != Infill::Honeycomb) {
assert!(
dist(kind, Vector3::ZERO) < 1e-5,
"{} has no wall at the origin: {}",
kind.name(),
dist(kind, Vector3::ZERO)
);
}
}
#[test]
fn cells_have_room_between_the_walls() {
for kind in periodic() {
let mut best = 0.0f32;
let n = 32;
for k in 0..n {
for j in 0..n {
for i in 0..n {
let s = |a: usize| (a as f32 + 0.5) / n as f32 * 2.0;
best = best.max(dist(kind, Vector3::new(s(i), s(j), s(k))));
}
}
}
assert!(best > 0.1, "{} leaves no void: {best}", kind.name());
}
}
#[test]
fn distance_is_a_length() {
for kind in [Infill::Grid, Infill::AlignedRectilinear] {
for i in 1..12 {
let step = i as f32 * 0.02;
let d = dist(kind, Vector3::new(step, 0.5, 0.5));
assert!(
(d - step).abs() < 1e-4,
"{} reported {d} at {step}",
kind.name()
);
}
}
}
#[test]
fn anisotropic_cells_stretch_rather_than_clip() {
let cell = Vector3::new(2.0, 1.0, 1.0);
let mid = distance(
Infill::AlignedRectilinear,
Vector3::new(1.0, 0.0, 0.0),
Vector3::ZERO,
cell,
1.0,
);
assert!((mid - 1.0).abs() < 1e-4, "{mid}");
}
#[test]
fn rectilinear_turns_ninety_degrees_each_layer() {
let on_x = Vector3::new(0.0, 0.4, 0.5);
let on_y = Vector3::new(0.4, 0.0, 1.5);
assert!(dist(Infill::Rectilinear, on_x) < 1e-5);
assert!(dist(Infill::Rectilinear, on_y) < 1e-5);
assert!(dist(Infill::Rectilinear, Vector3::new(0.4, 0.0, 0.5)) > 0.3);
assert!(dist(Infill::Rectilinear, Vector3::new(0.0, 0.4, 1.5)) > 0.3);
}
#[test]
fn quarter_cubic_shifts_every_other_layer() {
let p = Vector3::new(0.23, 0.41, 0.15);
let one = Vector3::new(p.x, p.y, p.z + CUBE_RISE);
let two = Vector3::new(p.x, p.y, p.z + 2.0 * CUBE_RISE);
let base = dist(Infill::QuarterCubic, p);
assert!((base - dist(Infill::QuarterCubic, two)).abs() < 1e-4);
assert!((base - dist(Infill::QuarterCubic, one)).abs() > 1e-3);
assert!((dist(Infill::Cubic, p) - dist(Infill::Cubic, one)).abs() < 1e-4);
}
#[test]
fn honeycomb_rings_each_cell_with_wall() {
let middle = honeycomb(Vector3::ZERO, 1.0);
assert!(
(middle - 0.5).abs() < 1e-4,
"hexagon is not a pitch across: {middle}"
);
assert!(honeycomb(Vector3::new(0.5, 0.0, 0.0), 1.0) < 1e-5);
let corner = Vector3::new(0.5, ROW / 3.0, 0.0);
assert!(honeycomb(corner, 1.0) < 1e-4, "not a hexagon corner");
assert!(
(honeycomb(Vector3::new(0.2, 0.1, 9.0), 1.0)
- honeycomb(Vector3::new(0.2, 0.1, 0.0), 1.0))
.abs()
< 1e-6
);
}
#[test]
fn concentric_shells_follow_the_boundary() {
let cell = Vector3::ONE;
for (depth, want) in [
(0.0, 0.5),
(0.5, 0.0),
(0.75, 0.25),
(1.5, 0.0),
(1.9, 0.4),
(2.5, 0.0),
] {
let d = distance(
Infill::Concentric,
Vector3::ZERO,
Vector3::ZERO,
cell,
depth,
);
assert!((d - want).abs() < 1e-5, "at depth {depth}: {d} not {want}");
}
}
}