//! Design model: named rigid parts and their mass properties. //! //! A part references its mesh by content hash, so one copy of a mesh serves every //! part that uses it (docs/STORAGE.md). Poses are plain arrays because the core //! math types intentionally do not depend on serde; conversion to [`Transform`] //! normalises the quaternion and rejects degenerate input. mod mass; mod mesh; mod persist; use core::fmt; use std::io; use fabrilis_core::vec::{Quat, Transform, Vec3}; use fabrilis_storage::blob::BlobHash; use fabrilis_storage::format::FormatError; use serde::{Deserialize, Serialize}; use crate::import::MeshError; pub use mesh::{add_part_from_stl, load_mesh, store_mesh}; pub use persist::DESIGN_FILE; /// A serialisable rigid pose: position in metres and a `wxyz` quaternion. #[derive(Debug, Clone, Copy, PartialEq, Serialize, Deserialize)] pub struct Pose { /// Position, m. pub position: [f64; 3], /// Rotation as a `wxyz` quaternion; not necessarily unit length. pub rotation: [f64; 4], } impl Default for Pose { fn default() -> Self { Self { position: [0.0; 3], rotation: [1.0, 0.0, 0.0, 0.0], } } } impl Pose { /// The corresponding [`Transform`], or `None` for a non-finite position or a /// zero/non-finite quaternion. #[must_use] pub fn to_transform(self) -> Option { let position = Vec3::new(self.position[0], self.position[1], self.position[2]); if !position.is_finite() { return None; } let rotation = Quat::new( self.rotation[0], self.rotation[1], self.rotation[2], self.rotation[3], ) .normalize()?; Some(Transform::new(position, rotation)) } /// The pose that places a transform, keeping its quaternion as given. #[must_use] pub fn from_transform(transform: Transform) -> Self { Self { position: [ transform.position.x, transform.position.y, transform.position.z, ], rotation: [ transform.rotation.w, transform.rotation.x, transform.rotation.y, transform.rotation.z, ], } } } /// One rigid part: a mesh, a material and a pose in the design's frame. #[derive(Debug, Clone, PartialEq, Serialize, Deserialize)] pub struct Part { /// Name, unique within the design by convention. pub name: String, /// Content hash of the stored mesh. pub mesh: BlobHash, /// Material name in a [`fabrilis_materials::db::MaterialDb`]. pub material: String, /// Placement of the part's mesh in the design frame. pub pose: Pose, } /// A named collection of parts. #[derive(Debug, Clone, PartialEq, Serialize, Deserialize)] pub struct Design { /// Design name. pub name: String, /// Parts in insertion order. pub parts: Vec, } impl Design { /// An empty design. #[must_use] pub fn new(name: impl Into) -> Self { Self { name: name.into(), parts: Vec::new(), } } /// Appends a part. pub fn push(&mut self, part: Part) { self.parts.push(part); } } /// Why a design, a mesh blob or a mass computation failed. #[derive(Debug)] pub enum SceneError { /// Reading or writing a project file failed. Io(io::Error), /// The versioned scene envelope is invalid. Format(FormatError), /// The mesh itself is invalid before it is stored. Mesh(MeshError), /// A stored mesh blob cannot be decoded. MeshBlob(String), /// The material name is not in the database. UnknownMaterial(String), /// The pose has a non-finite position or a zero/non-finite rotation. BadPose, /// The unit scale must be finite and positive. BadScale(f64), /// There is nothing to aggregate. EmptyDesign, /// Aggregated mass properties are not finite. BadMass, } impl fmt::Display for SceneError { fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result { match self { Self::Io(error) => write!(formatter, "project I/O failed: {error}"), Self::Format(error) => write!(formatter, "scene format error: {error}"), Self::Mesh(error) => write!(formatter, "mesh error: {error}"), Self::MeshBlob(reason) => write!(formatter, "stored mesh is invalid: {reason}"), Self::UnknownMaterial(name) => write!(formatter, "unknown material `{name}`"), Self::BadPose => write!( formatter, "pose has a non-finite position or a zero/non-finite rotation" ), Self::BadScale(scale) => { write!(formatter, "unit scale {scale} must be finite and positive") } Self::EmptyDesign => write!(formatter, "design has no parts"), Self::BadMass => write!(formatter, "aggregated mass properties are not finite"), } } } impl std::error::Error for SceneError { fn source(&self) -> Option<&(dyn std::error::Error + 'static)> { match self { Self::Io(error) => Some(error), Self::Format(error) => Some(error), Self::Mesh(error) => Some(error), _ => None, } } } impl From for SceneError { fn from(error: io::Error) -> Self { Self::Io(error) } } impl From for SceneError { fn from(error: FormatError) -> Self { Self::Format(error) } } impl From for SceneError { fn from(error: MeshError) -> Self { Self::Mesh(error) } } #[cfg(test)] mod tests;