use crate::error::{Error, Result};
use crate::mesh::Mesh;
use crate::profile::{Profile2D, Profile2DWithVoids, Triangulation, VoidInfo};
use nalgebra::{Matrix4, Point2, Point3, Vector3};
#[inline]
pub fn extrude_profile(
profile: &Profile2D,
depth: f64,
transform: Option<Matrix4<f64>>,
) -> Result<Mesh> {
if depth <= 0.0 {
return Err(Error::InvalidExtrusion(
"Depth must be positive".to_string(),
));
}
let (min_x, max_x, min_y, max_y) = profile.outer.iter().fold(
(f64::MAX, f64::MIN, f64::MAX, f64::MIN),
|(min_x, max_x, min_y, max_y), p| {
(min_x.min(p.x), max_x.max(p.x), min_y.min(p.y), max_y.max(p.y))
}
);
let profile_span = ((max_x - min_x).powi(2) + (max_y - min_y).powi(2)).sqrt();
if profile_span > 10.0 {
eprintln!("[DEBUG-H3H4] extrude_profile: span={:.2}m pts={} X=[{:.2},{:.2}] Y=[{:.2},{:.2}] depth={:.3}",
profile_span, profile.outer.len(), min_x, max_x, min_y, max_y, depth);
}
let should_skip_caps = profile_has_extreme_aspect_ratio(&profile.outer);
let triangulation = if should_skip_caps {
None
} else {
Some(profile.triangulate()?)
};
let cap_vertex_count = triangulation.as_ref().map(|t| t.points.len() * 2).unwrap_or(0);
let side_vertex_count = profile.outer.len() * 2;
let total_vertices = cap_vertex_count + side_vertex_count;
let cap_index_count = triangulation.as_ref().map(|t| t.indices.len() * 2).unwrap_or(0);
let mut mesh = Mesh::with_capacity(
total_vertices,
cap_index_count + profile.outer.len() * 6,
);
if let Some(ref tri) = triangulation {
create_cap_mesh(tri, 0.0, Vector3::new(0.0, 0.0, -1.0), &mut mesh);
create_cap_mesh(
tri,
depth,
Vector3::new(0.0, 0.0, 1.0),
&mut mesh,
);
}
create_side_walls(&profile.outer, depth, &mut mesh);
for hole in &profile.holes {
create_side_walls(hole, depth, &mut mesh);
}
if let Some(mat) = transform {
apply_transform(&mut mesh, &mat);
}
Ok(mesh)
}
#[inline]
fn profile_has_extreme_aspect_ratio(outer: &[Point2<f64>]) -> bool {
if outer.len() < 3 {
return false;
}
let mut min_x = f64::MAX;
let mut max_x = f64::MIN;
let mut min_y = f64::MAX;
let mut max_y = f64::MIN;
for p in outer {
min_x = min_x.min(p.x);
max_x = max_x.max(p.x);
min_y = min_y.min(p.y);
max_y = max_y.max(p.y);
}
let width = max_x - min_x;
let height = max_y - min_y;
if width < 0.001 || height < 0.001 {
return false;
}
let aspect_ratio = (width / height).max(height / width);
aspect_ratio > 100.0
}
#[inline]
pub fn extrude_profile_with_voids(
profile_with_voids: &Profile2DWithVoids,
depth: f64,
transform: Option<Matrix4<f64>>,
) -> Result<Mesh> {
if depth <= 0.0 {
return Err(Error::InvalidExtrusion(
"Depth must be positive".to_string(),
));
}
let profile_with_holes = profile_with_voids.profile_with_through_holes();
let triangulation = profile_with_holes.triangulate()?;
let partial_void_count = profile_with_voids.partial_voids().count();
let vertex_count = triangulation.points.len() * 2;
let side_vertex_count = profile_with_holes.outer.len() * 2
+ profile_with_holes.holes.iter().map(|h| h.len() * 2).sum::<usize>();
let partial_void_vertices = partial_void_count * 100; let total_vertices = vertex_count + side_vertex_count + partial_void_vertices;
let mut mesh = Mesh::with_capacity(
total_vertices,
triangulation.indices.len() * 2 + profile_with_holes.outer.len() * 6,
);
create_cap_mesh(&triangulation, 0.0, Vector3::new(0.0, 0.0, -1.0), &mut mesh);
create_cap_mesh(
&triangulation,
depth,
Vector3::new(0.0, 0.0, 1.0),
&mut mesh,
);
create_side_walls(&profile_with_holes.outer, depth, &mut mesh);
for hole in &profile_with_holes.holes {
create_side_walls(hole, depth, &mut mesh);
}
for void in profile_with_voids.partial_voids() {
create_partial_void_geometry(void, depth, &mut mesh)?;
}
if let Some(mat) = transform {
apply_transform(&mut mesh, &mat);
}
Ok(mesh)
}
fn create_partial_void_geometry(void: &VoidInfo, total_depth: f64, mesh: &mut Mesh) -> Result<()> {
if void.contour.len() < 3 {
return Ok(());
}
let epsilon = 0.001;
let void_profile = Profile2D::new(void.contour.clone());
let void_triangulation = match void_profile.triangulate() {
Ok(t) => t,
Err(_) => return Ok(()), };
if void.depth_start > epsilon {
create_cap_mesh(
&void_triangulation,
void.depth_start,
Vector3::new(0.0, 0.0, -1.0), mesh,
);
}
if void.depth_end < total_depth - epsilon {
create_cap_mesh(
&void_triangulation,
void.depth_end,
Vector3::new(0.0, 0.0, 1.0), mesh,
);
}
let void_depth = void.depth_end - void.depth_start;
if void_depth > epsilon {
create_void_side_walls(&void.contour, void.depth_start, void.depth_end, mesh);
}
Ok(())
}
fn create_void_side_walls(
contour: &[Point2<f64>],
z_start: f64,
z_end: f64,
mesh: &mut Mesh,
) {
let base_index = mesh.vertex_count() as u32;
let mut quad_count = 0u32;
for i in 0..contour.len() {
let j = (i + 1) % contour.len();
let p0 = &contour[i];
let p1 = &contour[j];
let edge = Vector3::new(p1.x - p0.x, p1.y - p0.y, 0.0);
let normal = match Vector3::new(edge.y, -edge.x, 0.0).try_normalize(1e-10) {
Some(n) => n,
None => continue, };
let v0_bottom = Point3::new(p0.x, p0.y, z_start);
let v1_bottom = Point3::new(p1.x, p1.y, z_start);
let v0_top = Point3::new(p0.x, p0.y, z_end);
let v1_top = Point3::new(p1.x, p1.y, z_end);
let idx = base_index + (quad_count * 4);
mesh.add_vertex(v0_bottom, normal);
mesh.add_vertex(v1_bottom, normal);
mesh.add_vertex(v1_top, normal);
mesh.add_vertex(v0_top, normal);
mesh.add_triangle(idx, idx + 2, idx + 1);
mesh.add_triangle(idx, idx + 3, idx + 2);
quad_count += 1;
}
}
#[inline]
fn create_cap_mesh(triangulation: &Triangulation, z: f64, normal: Vector3<f64>, mesh: &mut Mesh) {
let base_index = mesh.vertex_count() as u32;
for point in &triangulation.points {
mesh.add_vertex(Point3::new(point.x, point.y, z), normal);
}
for i in (0..triangulation.indices.len()).step_by(3) {
if i + 2 >= triangulation.indices.len() {
break;
}
let i0 = base_index + triangulation.indices[i] as u32;
let i1 = base_index + triangulation.indices[i + 1] as u32;
let i2 = base_index + triangulation.indices[i + 2] as u32;
if z == 0.0 {
mesh.add_triangle(i0, i2, i1);
} else {
mesh.add_triangle(i0, i1, i2);
}
}
}
#[inline]
fn create_side_walls(boundary: &[nalgebra::Point2<f64>], depth: f64, mesh: &mut Mesh) {
let n = boundary.len();
if n < 2 {
return;
}
let mut cx = 0.0;
let mut cy = 0.0;
for p in boundary.iter() {
cx += p.x;
cy += p.y;
}
cx /= n as f64;
cy /= n as f64;
let use_smooth_radial_normals = is_approximately_circular_profile(boundary, cx, cy);
let vertex_normals: Vec<Vector3<f64>> = if use_smooth_radial_normals {
boundary
.iter()
.map(|p| {
Vector3::new(p.x - cx, p.y - cy, 0.0)
.try_normalize(1e-10)
.unwrap_or(Vector3::new(0.0, 0.0, 1.0))
})
.collect()
} else {
Vec::new()
};
let base_index = mesh.vertex_count() as u32;
let mut quad_count = 0u32;
for i in 0..n {
let j = (i + 1) % n;
let p0 = &boundary[i];
let p1 = &boundary[j];
let edge = Vector3::new(p1.x - p0.x, p1.y - p0.y, 0.0);
if edge.magnitude_squared() < 1e-20 {
continue;
}
let flat_normal = Vector3::new(-edge.y, edge.x, 0.0)
.try_normalize(1e-10)
.unwrap_or(Vector3::new(0.0, 0.0, 1.0));
let n0 = if use_smooth_radial_normals {
vertex_normals[i]
} else {
flat_normal
};
let n1 = if use_smooth_radial_normals {
vertex_normals[j]
} else {
flat_normal
};
let v0_bottom = Point3::new(p0.x, p0.y, 0.0);
let v1_bottom = Point3::new(p1.x, p1.y, 0.0);
let v0_top = Point3::new(p0.x, p0.y, depth);
let v1_top = Point3::new(p1.x, p1.y, depth);
let idx = base_index + (quad_count * 4);
mesh.add_vertex(v0_bottom, n0);
mesh.add_vertex(v1_bottom, n1);
mesh.add_vertex(v1_top, n1);
mesh.add_vertex(v0_top, n0);
mesh.add_triangle(idx, idx + 1, idx + 2);
mesh.add_triangle(idx, idx + 2, idx + 3);
quad_count += 1;
}
}
#[inline]
fn is_approximately_circular_profile(boundary: &[Point2<f64>], cx: f64, cy: f64) -> bool {
if boundary.len() < 12 {
return false;
}
let mut radii: Vec<f64> = Vec::with_capacity(boundary.len());
for p in boundary {
let r = ((p.x - cx).powi(2) + (p.y - cy).powi(2)).sqrt();
if !r.is_finite() || r < 1e-9 {
return false;
}
radii.push(r);
}
let mean = radii.iter().sum::<f64>() / radii.len() as f64;
if mean < 1e-9 {
return false;
}
let variance = radii
.iter()
.map(|r| {
let d = r - mean;
d * d
})
.sum::<f64>()
/ radii.len() as f64;
let std_dev = variance.sqrt();
let coeff_var = std_dev / mean;
coeff_var < 0.15
}
#[inline]
pub fn apply_transform(mesh: &mut Mesh, transform: &Matrix4<f64>) {
mesh.positions.chunks_exact_mut(3).for_each(|chunk| {
let point = Point3::new(chunk[0] as f64, chunk[1] as f64, chunk[2] as f64);
let transformed = transform.transform_point(&point);
chunk[0] = transformed.x as f32;
chunk[1] = transformed.y as f32;
chunk[2] = transformed.z as f32;
});
let normal_matrix = transform.try_inverse().unwrap_or(*transform).transpose();
mesh.normals.chunks_exact_mut(3).for_each(|chunk| {
let normal = Vector3::new(chunk[0] as f64, chunk[1] as f64, chunk[2] as f64);
let transformed = (normal_matrix * normal.to_homogeneous()).xyz().normalize();
chunk[0] = transformed.x as f32;
chunk[1] = transformed.y as f32;
chunk[2] = transformed.z as f32;
});
}
#[inline]
pub fn apply_transform_with_rtc(
mesh: &mut Mesh,
transform: &Matrix4<f64>,
rtc_offset: (f64, f64, f64),
) {
mesh.positions.chunks_exact_mut(3).for_each(|chunk| {
let point = Point3::new(chunk[0] as f64, chunk[1] as f64, chunk[2] as f64);
let transformed = transform.transform_point(&point);
chunk[0] = (transformed.x - rtc_offset.0) as f32;
chunk[1] = (transformed.y - rtc_offset.1) as f32;
chunk[2] = (transformed.z - rtc_offset.2) as f32;
});
let normal_matrix = transform.try_inverse().unwrap_or(*transform).transpose();
mesh.normals.chunks_exact_mut(3).for_each(|chunk| {
let normal = Vector3::new(chunk[0] as f64, chunk[1] as f64, chunk[2] as f64);
let transformed = (normal_matrix * normal.to_homogeneous()).xyz().normalize();
chunk[0] = transformed.x as f32;
chunk[1] = transformed.y as f32;
chunk[2] = transformed.z as f32;
});
}
#[cfg(test)]
mod tests {
use super::*;
use crate::profile::create_rectangle;
#[test]
fn test_extrude_rectangle() {
let profile = create_rectangle(10.0, 5.0);
let mesh = extrude_profile(&profile, 20.0, None).unwrap();
assert!(mesh.vertex_count() > 0);
assert!(mesh.triangle_count() > 0);
let (min, max) = mesh.bounds();
assert!((min.x - -5.0).abs() < 0.01);
assert!((max.x - 5.0).abs() < 0.01);
assert!((min.y - -2.5).abs() < 0.01);
assert!((max.y - 2.5).abs() < 0.01);
assert!((min.z - 0.0).abs() < 0.01);
assert!((max.z - 20.0).abs() < 0.01);
}
#[test]
fn test_extrude_with_transform() {
let profile = create_rectangle(10.0, 5.0);
let transform = Matrix4::new_translation(&Vector3::new(100.0, 200.0, 300.0));
let mesh = extrude_profile(&profile, 20.0, Some(transform)).unwrap();
let (min, max) = mesh.bounds();
assert!((min.x - 95.0).abs() < 0.01); assert!((max.x - 105.0).abs() < 0.01); assert!((min.y - 197.5).abs() < 0.01); assert!((max.y - 202.5).abs() < 0.01); assert!((min.z - 300.0).abs() < 0.01); assert!((max.z - 320.0).abs() < 0.01); }
#[test]
fn test_extrude_circle() {
use crate::profile::create_circle;
let profile = create_circle(5.0, None);
let mesh = extrude_profile(&profile, 10.0, None).unwrap();
assert!(mesh.vertex_count() > 0);
assert!(mesh.triangle_count() > 0);
let (min, max) = mesh.bounds();
assert!((min.x - -5.0).abs() < 0.1);
assert!((max.x - 5.0).abs() < 0.1);
assert!((min.y - -5.0).abs() < 0.1);
assert!((max.y - 5.0).abs() < 0.1);
}
#[test]
fn test_extrude_hollow_circle() {
use crate::profile::create_circle;
let profile = create_circle(10.0, Some(5.0));
let mesh = extrude_profile(&profile, 15.0, None).unwrap();
assert!(mesh.triangle_count() > 20);
}
#[test]
fn test_invalid_depth() {
let profile = create_rectangle(10.0, 5.0);
let result = extrude_profile(&profile, -1.0, None);
assert!(result.is_err());
}
#[test]
fn test_circular_profile_detection() {
use crate::profile::create_circle;
let circle = create_circle(5.0, None);
let mut cx = 0.0;
let mut cy = 0.0;
for p in &circle.outer {
cx += p.x;
cy += p.y;
}
cx /= circle.outer.len() as f64;
cy /= circle.outer.len() as f64;
assert!(is_approximately_circular_profile(&circle.outer, cx, cy));
}
#[test]
fn test_rectangular_profile_not_detected_as_circular() {
let rect = create_rectangle(10.0, 5.0);
let mut cx = 0.0;
let mut cy = 0.0;
for p in &rect.outer {
cx += p.x;
cy += p.y;
}
cx /= rect.outer.len() as f64;
cy /= rect.outer.len() as f64;
assert!(!is_approximately_circular_profile(&rect.outer, cx, cy));
}
}