use super::{BufferAttribute, BufferGeometry};
use crate::materials::Material;
use std::sync::Arc;
#[derive(Debug, Clone)]
pub struct LineSegments {
pub geometry: Arc<BufferGeometry>,
pub material: Arc<Material>,
pub strip: Option<LineStrip>,
}
#[derive(Debug, Clone)]
pub struct LineStrip {
pub geometry: Arc<BufferGeometry>,
pub closed: bool,
}
impl LineSegments {
pub fn new(geometry: BufferGeometry, material: Material) -> Self {
Self {
geometry: Arc::new(geometry),
material: Arc::new(material),
strip: None,
}
}
pub fn from_arc(geometry: Arc<BufferGeometry>, material: Arc<Material>) -> Self {
Self {
geometry,
material,
strip: None,
}
}
pub fn strip(strip: Arc<BufferGeometry>, material: Arc<Material>, closed: bool) -> Self {
Self {
geometry: Arc::new(strip_to_list(&strip, closed)),
material,
strip: Some(LineStrip {
geometry: strip,
closed,
}),
}
}
}
fn strip_to_list(strip: &BufferGeometry, closed: bool) -> BufferGeometry {
let vertices = strip
.get_attribute("position")
.map(|a| a.count())
.unwrap_or(0);
let count = match &strip.index {
Some(idx) => idx.len(),
None => vertices,
};
let start = (strip.draw_range.start as usize).min(count);
let end = (strip.draw_range.end() as usize).min(count);
let at = |i: usize| match &strip.index {
Some(idx) => idx[i] as usize,
None => i,
};
let mut pairs = Vec::new();
if end > start + 1 {
for i in start..end - 1 {
pairs.push((at(i), at(i + 1)));
}
}
if closed && end > start {
pairs.push((at(end - 1), at(start)));
}
let mut out = BufferGeometry::new();
for (name, attr) in &strip.attributes {
let n = attr.item_size.max(1);
if attr.count() != vertices {
continue;
}
let mut array = Vec::with_capacity(pairs.len() * 2 * n);
for &(a, b) in &pairs {
for v in [a, b] {
match attr.array.get(v * n..v * n + n) {
Some(s) => array.extend_from_slice(s),
None => array.extend(std::iter::repeat_n(0.0, n)),
}
}
}
out.set_attribute(name.clone(), BufferAttribute::new(array, n));
}
out
}
#[cfg(test)]
mod tests {
use super::*;
fn material() -> Arc<Material> {
Arc::new(Material::Basic(crate::materials::BasicMaterial::new(
crate::math::Color::WHITE,
)))
}
fn strip(n: usize) -> Arc<BufferGeometry> {
let mut g = BufferGeometry::new();
let p: Vec<f32> = (0..n).flat_map(|i| [i as f32, 0.0, 0.0]).collect();
g.set_attribute("position", BufferAttribute::new(p, 3));
Arc::new(g)
}
fn xs(ls: &LineSegments) -> Vec<f32> {
let a = ls.geometry.get_attribute("position").unwrap();
a.array.chunks(3).map(|c| c[0]).collect()
}
#[test]
fn a_strip_becomes_its_segments() {
let m = material();
assert_eq!(
xs(&LineSegments::strip(strip(4), m.clone(), false)),
[0., 1., 1., 2., 2., 3.]
);
assert_eq!(
xs(&LineSegments::strip(strip(3), m, true)),
[0., 1., 1., 2., 2., 0.],
"a loop closes"
);
}
#[test]
fn the_index_and_draw_range_order_the_strip() {
let mut g = (*strip(5)).clone();
g.set_index(vec![4, 2, 0, 1]);
g.set_draw_range(1, 3);
let ls = LineSegments::strip(Arc::new(g), material(), true);
assert_eq!(xs(&ls), [2., 0., 0., 1., 1., 2.]);
}
}