use std::fmt;
use std::str::FromStr;
use super::error::{Error, Result};
#[derive(Debug, Clone)]
pub struct SliceStack {
pub id: u32,
pub z_bottom: Option<f64>,
pub content: SliceContent,
}
impl SliceStack {
pub fn new(id: u32) -> Self {
Self {
id,
z_bottom: None,
content: SliceContent::Slices(Vec::new()),
}
}
}
#[derive(Debug, Clone)]
pub enum SliceContent {
Slices(Vec<Slice>),
SliceRefs(Vec<SliceRef>),
}
#[derive(Debug, Clone)]
pub struct Slice {
pub z_top: f64,
pub vertices: Vertices2D,
pub polygons: Vec<Polygon>,
}
impl Slice {
pub fn new(z_top: f64) -> Self {
Self {
z_top,
vertices: Vertices2D::default(),
polygons: Vec::new(),
}
}
}
#[derive(Debug, Clone, PartialEq, Default)]
pub struct Vertices2D {
pub vertices: Vec<Vertex2D>,
}
#[derive(Debug, Clone, Copy, PartialEq, Default)]
pub struct Vertex2D {
pub x: f64,
pub y: f64,
}
impl Vertex2D {
pub fn new(x: f64, y: f64) -> Self {
Self { x, y }
}
}
#[derive(Debug, Clone)]
pub struct Polygon {
pub start_v: u32,
pub segments: Vec<Segment>,
}
impl Polygon {
pub fn new(start_v: u32) -> Self {
Self {
start_v,
segments: Vec::new(),
}
}
}
#[derive(Debug, Clone)]
pub struct Segment {
pub v2: u32,
pub p1: Option<u32>,
pub p2: Option<u32>,
pub pid: Option<u32>,
}
impl Segment {
pub fn new(v2: u32) -> Self {
Self {
v2,
p1: None,
p2: None,
pid: None,
}
}
}
#[derive(Debug, Clone)]
pub struct SliceRef {
pub slice_stack_id: u32,
pub slice_path: String,
}
#[derive(Debug, Clone, Default)]
pub struct SliceResources {
pub slice_stacks: Vec<SliceStack>,
}
#[derive(Debug, Clone, Default)]
pub struct SliceObject {
pub slice_stack_id: Option<u32>,
pub mesh_resolution: Option<MeshResolution>,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub enum MeshResolution {
#[default]
FullRes,
LowRes,
}
impl fmt::Display for MeshResolution {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
MeshResolution::FullRes => write!(f, "fullres"),
MeshResolution::LowRes => write!(f, "lowres"),
}
}
}
impl FromStr for MeshResolution {
type Err = Error;
fn from_str(s: &str) -> Result<Self> {
match s.to_lowercase().as_str() {
"fullres" => Ok(MeshResolution::FullRes),
"lowres" => Ok(MeshResolution::LowRes),
_ => Err(Error::InvalidAttribute {
name: "meshresolution".to_string(),
message: format!("unknown mesh resolution: {}", s),
}),
}
}
}