use crate::core::{BufferAttribute, BufferGeometry};
use crate::math::Vector3;
pub fn lathe_default_points() -> Vec<(f64, f64)> {
vec![(0.0, -0.5), (0.5, 0.0), (0.0, 0.5)]
}
pub fn lathe_geometry(points: &[(f64, f64)], segments: usize) -> BufferGeometry {
lathe_geometry_full(points, segments, 0.0, std::f64::consts::PI * 2.0)
}
pub fn lathe_geometry_full(
points: &[(f64, f64)],
segments: usize,
phi_start: f64,
phi_length: f64,
) -> BufferGeometry {
let phi_length = phi_length.clamp(0.0, std::f64::consts::PI * 2.0);
let mut indices: Vec<u32> = Vec::new();
let mut vertices: Vec<f32> = Vec::new();
let mut uvs: Vec<f32> = Vec::new();
let mut init_normals: Vec<f64> = Vec::new();
let mut normals: Vec<f32> = Vec::new();
let inverse_segments = 1.0 / segments as f64;
let mut vertex = Vector3::ZERO;
let mut normal = Vector3::ZERO;
let mut cur_normal = Vector3::ZERO;
let mut prev_normal = Vector3::ZERO;
let mut dx;
let mut dy;
if points.is_empty() {
let mut geometry = BufferGeometry::new();
geometry.set_index(&indices);
geometry.set_attribute("position", BufferAttribute::new(vertices, 3));
geometry.set_attribute("uv", BufferAttribute::new(uvs, 2));
geometry.set_attribute("normal", BufferAttribute::new(normals, 3));
return geometry;
}
let last = points.len() - 1;
for j in 0..=last {
if j == 0 {
dx = points[j + 1].0 - points[j].0;
dy = points[j + 1].1 - points[j].1;
normal.x = dy * 1.0;
normal.y = -dx;
normal.z = dy * 0.0;
prev_normal = normal;
normal.normalize();
init_normals.push(normal.x);
init_normals.push(normal.y);
init_normals.push(normal.z);
} else if j == last {
init_normals.push(prev_normal.x);
init_normals.push(prev_normal.y);
init_normals.push(prev_normal.z);
} else {
dx = points[j + 1].0 - points[j].0;
dy = points[j + 1].1 - points[j].1;
normal.x = dy * 1.0;
normal.y = -dx;
normal.z = dy * 0.0;
cur_normal = normal;
normal.x += prev_normal.x;
normal.y += prev_normal.y;
normal.z += prev_normal.z;
normal.normalize();
init_normals.push(normal.x);
init_normals.push(normal.y);
init_normals.push(normal.z);
prev_normal = cur_normal;
}
}
let _ = cur_normal;
for i in 0..=segments {
let phi = phi_start + i as f64 * inverse_segments * phi_length;
let sin = phi.sin();
let cos = phi.cos();
for j in 0..=last {
vertex.x = points[j].0 * sin;
vertex.y = points[j].1;
vertex.z = points[j].0 * cos;
vertices.push(vertex.x as f32);
vertices.push(vertex.y as f32);
vertices.push(vertex.z as f32);
uvs.push((i as f64 / segments as f64) as f32);
uvs.push((j as f64 / last as f64) as f32);
let x = init_normals[3 * j] * sin;
let y = init_normals[3 * j + 1];
let z = init_normals[3 * j] * cos;
normals.push(x as f32);
normals.push(y as f32);
normals.push(z as f32);
}
}
for i in 0..segments {
for j in 0..last {
let base = j + i * points.len();
let a = base as u32;
let b = (base + points.len()) as u32;
let c = (base + points.len() + 1) as u32;
let d = (base + 1) as u32;
indices.push(a);
indices.push(b);
indices.push(d);
indices.push(c);
indices.push(d);
indices.push(b);
}
}
let mut geometry = BufferGeometry::new();
geometry.set_index(&indices);
geometry.set_attribute("position", BufferAttribute::new(vertices, 3));
geometry.set_attribute("uv", BufferAttribute::new(uvs, 2));
geometry.set_attribute("normal", BufferAttribute::new(normals, 3));
geometry
}