use std::{error::Error, fmt};
use super::SurfaceStyle3d;
use crate::{Color, LogicalPixels};
const MAX_LOGICAL_EDGE_METRIC: f32 = 1_048_576.0;
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct MeshStyle3d {
surface: Option<SurfaceStyle3d>,
wireframe: Option<WireframeStyle3d>,
}
impl MeshStyle3d {
pub const fn surface(surface: SurfaceStyle3d) -> Self {
Self {
surface: Some(surface),
wireframe: None,
}
}
pub const fn wireframe(wireframe: WireframeStyle3d) -> Self {
Self {
surface: None,
wireframe: Some(wireframe),
}
}
pub const fn with_wireframe(mut self, wireframe: WireframeStyle3d) -> Self {
self.wireframe = Some(wireframe);
self
}
pub const fn surface_style(self) -> Option<SurfaceStyle3d> {
self.surface
}
pub const fn wireframe_style(self) -> Option<WireframeStyle3d> {
self.wireframe
}
}
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct WireframeStyle3d {
visible_color: Color,
visible_width: LogicalPixels,
hidden: Option<HiddenEdgeStyle3d>,
}
#[derive(Debug, Clone, Copy, PartialEq)]
struct HiddenEdgeStyle3d {
color: Color,
width: LogicalPixels,
dash_length: LogicalPixels,
gap_length: LogicalPixels,
}
impl WireframeStyle3d {
pub const MAX_LOGICAL_PIXELS: f32 = MAX_LOGICAL_EDGE_METRIC;
pub fn visible(color: Color, width: LogicalPixels) -> Result<Self, Mesh3dStyleError> {
validate_edge_color_width(color, width)?;
Ok(Self {
visible_color: color,
visible_width: width,
hidden: None,
})
}
pub fn with_hidden(
mut self,
color: Color,
width: LogicalPixels,
dash_length: LogicalPixels,
gap_length: LogicalPixels,
) -> Result<Self, Mesh3dStyleError> {
validate_edge_color_width(color, width)?;
if !is_stable_edge_metric(dash_length.get())
|| !is_stable_edge_metric(gap_length.get())
|| !(dash_length.get() + gap_length.get()).is_finite()
{
return Err(Mesh3dStyleError::InvalidDashPattern);
}
self.hidden = Some(HiddenEdgeStyle3d {
color,
width,
dash_length,
gap_length,
});
Ok(self)
}
pub const fn visible_color(self) -> Color {
self.visible_color
}
pub const fn visible_width(self) -> LogicalPixels {
self.visible_width
}
pub const fn hidden_enabled(self) -> bool {
self.hidden.is_some()
}
pub const fn hidden_color(self) -> Option<Color> {
match self.hidden {
Some(hidden) => Some(hidden.color),
None => None,
}
}
pub const fn hidden_width(self) -> Option<LogicalPixels> {
match self.hidden {
Some(hidden) => Some(hidden.width),
None => None,
}
}
pub const fn hidden_pattern(self) -> Option<(LogicalPixels, LogicalPixels)> {
match self.hidden {
Some(hidden) => Some((hidden.dash_length, hidden.gap_length)),
None => None,
}
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Mesh3dStyleError {
InvalidMaterialColor,
InvalidMaskCutoff,
InvalidSurfaceColor,
InvalidColorOrWidth,
InvalidDashPattern,
}
impl fmt::Display for Mesh3dStyleError {
fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::InvalidMaterialColor => write!(
formatter,
"3D material color must be normalized straight-linear RGBA"
),
Self::InvalidMaskCutoff => write!(
formatter,
"3D mask cutoff must be zero or normal and finite in 0..=1"
),
Self::InvalidSurfaceColor => write!(formatter, "3D surface color must be opaque"),
Self::InvalidColorOrWidth => {
write!(
formatter,
"3D edge color must be opaque and width must be normal and in 0..={MAX_LOGICAL_EDGE_METRIC} logical pixels"
)
}
Self::InvalidDashPattern => {
write!(
formatter,
"3D hidden-edge dash and gap must each be normal and in 0..={MAX_LOGICAL_EDGE_METRIC} logical pixels with a finite sum"
)
}
}
}
}
impl Error for Mesh3dStyleError {}
fn validate_edge_color_width(color: Color, width: LogicalPixels) -> Result<(), Mesh3dStyleError> {
if color.is_normalized() && color.alpha() == 1.0 && is_stable_edge_metric(width.get()) {
Ok(())
} else {
Err(Mesh3dStyleError::InvalidColorOrWidth)
}
}
fn is_stable_edge_metric(value: f32) -> bool {
value.is_normal() && value <= MAX_LOGICAL_EDGE_METRIC
}