use super::raster::simplify_ring;
use indicatrix::geometry::meet_solver::Block;
mod fill;
mod labels;
mod layout;
mod legend;
mod render;
#[cfg(test)]
mod tests;
pub use layout::{PanelLayout, project_point};
pub use render::{render_diagram, render_diagram_end_view, render_diagram_single_panel};
pub(super) use labels::{TextCanvas, draw_text_into_buffer, text_size};
const NORMAL_EPS: f32 = 1e-3;
const HATCH_PERIOD: i32 = 6;
const MIN_LABEL_SPAN: f32 = 10.0;
const LEADER_LINE_MIN_SPAN: f32 = 2.5;
const LEADER_LINE_LENGTH: f32 = 22.0;
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum PanelKind {
Crown,
Pavilion,
Profile,
}
impl PanelKind {
const fn tag(self) -> u8 {
match self {
Self::Crown => 1,
Self::Pavilion => 2,
Self::Profile => 3,
}
}
const fn from_tag(tag: u8) -> Option<Self> {
match tag {
1 => Some(Self::Crown),
2 => Some(Self::Pavilion),
3 => Some(Self::Profile),
_ => None,
}
}
const fn has_index_wheel(self) -> bool {
matches!(self, Self::Crown | Self::Pavilion)
}
const fn caption(self) -> &'static str {
match self {
Self::Crown => "CROWN",
Self::Pavilion => "PAVILION",
Self::Profile => "PROFILE",
}
}
#[must_use]
pub fn shows(self, normal: glam::Vec3) -> bool {
match self {
Self::Crown => layout::crown_visible(normal),
Self::Pavilion => layout::pavilion_visible(normal),
Self::Profile => layout::profile_visible(normal),
}
}
}
#[derive(Debug, Clone, Default, PartialEq)]
pub struct DiagramLayout {
pub panels: Vec<PanelLayout>,
pub enlarged: bool,
pub gear_teeth: u32,
pub gear_reference_angle: f32,
}
impl DiagramLayout {
#[must_use]
pub fn panel(&self, kind: PanelKind) -> Option<&PanelLayout> {
self.panels.iter().find(|panel| panel.kind == kind)
}
#[must_use]
pub fn panel_at(&self, x: f32, y: f32) -> Option<&PanelLayout> {
self.panels.iter().find(|panel| {
let (left, top, right, bottom) = panel.clip;
x >= left as f32 && x < (right + 1) as f32 && y >= top as f32 && y < (bottom + 1) as f32
})
}
}
#[derive(Debug, Clone, Copy)]
pub struct DiagramConfig {
pub width: u32,
pub height: u32,
pub gear_teeth: u32,
pub gear_reference_angle: f32,
pub symmetry_order: u32,
pub mirror: bool,
}
#[derive(Debug, Clone)]
pub struct DiagramStyle {
pub base_color: [u8; 3],
pub edge_color: [u8; 3],
pub pending_color: [u8; 3],
pub hatch_color: [u8; 3],
pub selected_color: [u8; 3],
pub hover_color: [u8; 3],
pub selected_facet_color: [u8; 3],
pub multi_selected_color: [u8; 3],
pub moved_color: [u8; 3],
pub background: [u8; 4],
pub flagged: Vec<bool>,
pub pending: Vec<bool>,
pub selected: Vec<bool>,
pub facet_labels: Vec<String>,
pub hovered: Option<u32>,
pub selected_facet: Option<u32>,
pub multi_selected: Vec<u32>,
pub moved: Vec<u32>,
pub facet_index_on_gear: Vec<u32>,
pub meet_marker_pairs: Vec<(u32, u32)>,
pub meet_marker_color: [u8; 3],
pub symmetry_line_color: [u8; 3],
pub mirror_line_color: [u8; 3],
pub wheel_color: [u8; 3],
pub tool_facet_block: Vec<Option<Block>>,
}
impl Default for DiagramStyle {
fn default() -> Self {
Self {
base_color: [200, 205, 215],
edge_color: [30, 32, 38],
pending_color: [245, 158, 11],
hatch_color: [90, 40, 40],
selected_color: [59, 130, 246],
hover_color: [226, 232, 240],
selected_facet_color: [168, 85, 247],
multi_selected_color: [56, 189, 248],
moved_color: [251, 146, 60],
background: [12, 14, 20, 255],
flagged: Vec::new(),
pending: Vec::new(),
selected: Vec::new(),
facet_labels: Vec::new(),
hovered: None,
selected_facet: None,
multi_selected: Vec::new(),
moved: Vec::new(),
facet_index_on_gear: Vec::new(),
meet_marker_pairs: Vec::new(),
meet_marker_color: [250, 250, 250],
symmetry_line_color: [70, 74, 84],
mirror_line_color: [45, 212, 191],
wheel_color: [128, 136, 152],
tool_facet_block: Vec::new(),
}
}
}
#[derive(Debug, Clone)]
pub struct DiagramFrame {
pub width: u32,
pub height: u32,
pub color: Vec<u8>,
pub pick: Vec<u32>,
panel: Vec<u8>,
pub tooth: Vec<u32>,
pub layout: DiagramLayout,
depth: Vec<f32>,
}
impl DiagramFrame {
fn blank(width: u32, height: u32, background: [u8; 4]) -> Self {
let n = (width as usize) * (height as usize);
let mut color = vec![0u8; n * 4];
let (pixels, _remainder) = color.as_chunks_mut::<4>();
for px in pixels {
px.copy_from_slice(&background);
}
Self {
width,
height,
color,
pick: vec![0u32; n],
panel: vec![0u8; n],
tooth: vec![0u32; n],
layout: DiagramLayout::default(),
depth: vec![f32::INFINITY; n],
}
}
#[must_use]
pub fn pick_at(&self, x: u32, y: u32) -> Option<u32> {
if x >= self.width || y >= self.height {
return None;
}
let v = self.pick[(y * self.width + x) as usize];
(v != 0).then(|| v - 1)
}
#[must_use]
pub fn panel_at(&self, x: u32, y: u32) -> Option<PanelKind> {
if x >= self.width || y >= self.height {
return None;
}
PanelKind::from_tag(self.panel[(y * self.width + x) as usize])
}
#[must_use]
pub fn panel_tags(&self) -> Vec<u32> {
self.panel.iter().map(|&tag| u32::from(tag)).collect()
}
#[must_use]
pub fn tooth_at(&self, x: u32, y: u32) -> Option<u32> {
if x >= self.width || y >= self.height {
return None;
}
let v = self.tooth[(y * self.width + x) as usize];
(v != 0).then(|| v - 1)
}
const fn idx(&self, x: i32, y: i32) -> Option<usize> {
if x < 0 || y < 0 || x as u32 >= self.width || y as u32 >= self.height {
return None;
}
Some((y as u32 * self.width + x as u32) as usize)
}
}