use l3d_rs::{Geometry, Luminaire};
#[derive(Debug, Clone, PartialEq, serde::Serialize, serde::Deserialize)]
pub struct LeoFootprint {
pub part_name: String,
pub x: f32,
pub y: f32,
pub z: f32,
pub rot_z_deg: f32,
pub shape: LeoShape,
}
#[derive(Debug, Clone, PartialEq, serde::Serialize, serde::Deserialize)]
pub enum LeoShape {
Rectangle { size_x: f64, size_y: f64 },
Circle { diameter: f64 },
Point,
}
impl LeoFootprint {
pub fn half_extent(&self) -> (f64, f64) {
match self.shape {
LeoShape::Rectangle { size_x, size_y } => (size_x / 2.0, size_y / 2.0),
LeoShape::Circle { diameter } => (diameter / 2.0, diameter / 2.0),
LeoShape::Point => (0.01, 0.01),
}
}
}
pub fn extract_leos(l3d_bytes: &[u8]) -> anyhow::Result<Vec<LeoFootprint>> {
let l3d = l3d_rs::from_buffer(l3d_bytes);
let xml = l3d.file.structure.trim();
if xml.is_empty() {
return Ok(Vec::new());
}
let luminaire = Luminaire::from_xml(xml)?;
let mut out = Vec::new();
walk(&luminaire.structure.geometry, &mut out);
Ok(out)
}
fn walk(geometry: &Geometry, out: &mut Vec<LeoFootprint>) {
if let Some(leos) = geometry.light_emitting_objects.as_ref() {
for leo in &leos.light_emitting_object {
let (px, py, pz) = leo.position();
let (_, _, rz) = leo.rotation();
let shape = if let Some(r) = leo.rectangle() {
LeoShape::Rectangle {
size_x: r.size_x,
size_y: r.size_y,
}
} else if let Some(c) = leo.circle() {
LeoShape::Circle {
diameter: c.diameter,
}
} else {
LeoShape::Point
};
out.push(LeoFootprint {
part_name: leo.part_name().to_string(),
x: px,
y: py,
z: pz,
rot_z_deg: rz,
shape,
});
}
}
if let Some(joints) = geometry.joints.as_ref() {
for joint in &joints.joint {
for child in &joint.geometries.geometry {
walk(child, out);
}
}
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn extracts_leo0_and_leo1_from_example_007() {
let path = concat!(
env!("CARGO_MANIFEST_DIR"),
"/../../tests/test-create/example_007.l3d"
);
let bytes = std::fs::read(path).expect("read example_007.l3d");
let leos = extract_leos(&bytes).expect("extract leos");
let names: Vec<&str> = leos.iter().map(|l| l.part_name.as_str()).collect();
assert!(names.contains(&"LEO0"), "LEO0 present, got {names:?}");
assert!(names.contains(&"LEO1"), "LEO1 present, got {names:?}");
let leo0 = leos.iter().find(|l| l.part_name == "LEO0").unwrap();
assert!(
matches!(leo0.shape, LeoShape::Circle { .. }),
"LEO0 is circular, got {:?}",
leo0.shape
);
}
#[test]
fn non_l3d_bytes_yield_empty_list() {
let leos = extract_leos(b"not a zip").expect("soft-fails to empty");
assert!(leos.is_empty());
}
}