diff --git a/crates/engine/src/lib.rs b/crates/engine/src/lib.rs index 041aca62..336ba602 100644 --- a/crates/engine/src/lib.rs +++ b/crates/engine/src/lib.rs @@ -86,7 +86,6 @@ mod tile_transform_params; mod tool_availability; pub mod tool_cmds; pub mod tool_settings; -mod triangle_partition; mod uv_stroke_state; mod uv_symmetry; pub mod uv_tile_cmds; diff --git a/crates/engine/src/polygon_fill.rs b/crates/engine/src/polygon_fill.rs index 3292c61b..4f65c090 100644 --- a/crates/engine/src/polygon_fill.rs +++ b/crates/engine/src/polygon_fill.rs @@ -5,18 +5,15 @@ use std::collections::{BTreeSet, HashSet}; use texturecraft_compose::MaskGray; use texturecraft_geom::glam::Vec2; -use texturecraft_geom::{Mesh, NO_TRI}; +use texturecraft_geom::triangle_partition::CORNER_GRID_STEPS; +use texturecraft_geom::{Mesh, NO_TRI, TrianglePartition}; use texturecraft_paint::{EyeDepth, fill_triangles, screen_point}; use crate::fill_mode::FillMode; use crate::polygon_fill_drag::PolygonFillDrag; use crate::session::Session; -use crate::triangle_partition::TrianglePartition; use crate::{EngineError, Result}; -/// Corners closer than one step of this grid (per unit of position or UV) count as the same corner -const CORNER_GRID_STEPS: f32 = 1e5; - impl Session { /// Fill the triangle / polygon / UV island / connected mesh under viewport pixel `screen` pub fn polygon_fill(&mut self, screen: Vec2, viewport: Vec2) -> Result { @@ -178,7 +175,7 @@ impl Session { match self.tool.fill_mode { FillMode::Triangle => TrianglePartition::new(&self.mesh, material, |_, _| Vec::::new()), FillMode::Polygon => TrianglePartition::new(&self.mesh, material, polygon_edges), - FillMode::UvIsland => TrianglePartition::new(&self.mesh, material, uv_edges), + FillMode::UvIsland => TrianglePartition::uv_islands(&self.mesh, material), FillMode::Mesh => TrianglePartition::new(&self.mesh, material, position_edges), } } @@ -298,12 +295,6 @@ fn pixel_span(low: f32, high: f32, count: u32) -> std::ops::Range { first..end.max(first) } -/// Texture coordinates of `triangle`'s corners -fn uv_corners(mesh: &Mesh, triangle: usize) -> Option<[Vec2; 3]> { - let [first, second, third] = mesh.tri(triangle)?; - Some([*mesh.uvs.get(first)?, *mesh.uvs.get(second)?, *mesh.uvs.get(third)?]) -} - /// The polygon diagonals of `triangle`: edges the importer added to split a polygon fn polygon_edges(mesh: &Mesh, triangle: usize) -> Vec<(u32, u32)> { let hidden = mesh.diagonals.get(triangle).copied().unwrap_or(0); @@ -318,14 +309,6 @@ fn polygon_edges(mesh: &Mesh, triangle: usize) -> Vec<(u32, u32)> { .collect() } -/// `triangle`'s edges in UV space: imports often split vertices along hard edges where the UVs run on, so vertex indices alone break islands apart -fn uv_edges(mesh: &Mesh, triangle: usize) -> Vec<([i64; 2], [i64; 2])> { - let corner = |uv: Vec2| (uv * CORNER_GRID_STEPS).round().to_array().map(|step| step as i64); - let Some([first, second, third]) = uv_corners(mesh, triangle).map(|uvs| uvs.map(corner)) else { return Vec::new() }; - - [(first, second), (second, third), (third, first)].map(|(from, to)| (from.min(to), from.max(to))).to_vec() -} - /// `triangle`'s edges by position: UV seams and hard edges split vertices but leave the geometry connected fn position_edges(mesh: &Mesh, triangle: usize) -> Vec<([i64; 3], [i64; 3])> { let corner = |vertex: usize| mesh.positions.get(vertex).map(|position| (*position * CORNER_GRID_STEPS).round().to_array().map(|step| step as i64)); diff --git a/crates/engine/src/polygon_fill_drag.rs b/crates/engine/src/polygon_fill_drag.rs index 09c20905..39c64507 100644 --- a/crates/engine/src/polygon_fill_drag.rs +++ b/crates/engine/src/polygon_fill_drag.rs @@ -9,7 +9,7 @@ use texturecraft_paint::EyeDepth; use texturecraft_texel::TileImage; use crate::session::{Document, StackTarget}; -use crate::triangle_partition::TrianglePartition; +use texturecraft_geom::TrianglePartition; /// A polygon fill box being dragged: the fill shows live and becomes one undo step when the drag ends pub(crate) struct PolygonFillDrag { diff --git a/crates/engine/src/polygon_fill_mirror.rs b/crates/engine/src/polygon_fill_mirror.rs index 945d15bb..fd362ff2 100644 --- a/crates/engine/src/polygon_fill_mirror.rs +++ b/crates/engine/src/polygon_fill_mirror.rs @@ -5,7 +5,7 @@ use std::collections::{BTreeSet, HashSet}; use texturecraft_geom::glam::Mat3; use crate::session::Session; -use crate::triangle_partition::TrianglePartition; +use texturecraft_geom::TrianglePartition; /// How far a mirrored face centre may be from the surface and still count as on it, as a fraction of the mesh's bounding-box diagonal const MIRROR_TOLERANCE: f32 = 1e-3; diff --git a/crates/geom/src/lib.rs b/crates/geom/src/lib.rs index 3ca0f8ed..eb71f854 100644 --- a/crates/geom/src/lib.rs +++ b/crates/geom/src/lib.rs @@ -11,6 +11,7 @@ pub mod camera; pub mod gbuffer; pub mod mesh; pub mod primitives; +pub mod triangle_partition; pub mod unwrap; pub use bvh::{Bvh, Hit}; @@ -18,6 +19,7 @@ pub use camera::{OrbitCamera, Ray}; pub use gbuffer::{NO_TRI, Surface, UvGBuffer, surface_at}; pub use glam; pub use mesh::{BaseColor, MAX_TRIANGLES, Mesh, SubMesh}; +pub use triangle_partition::TrianglePartition; #[derive(Debug, thiserror::Error, Clone, PartialEq)] pub enum GeomError { diff --git a/crates/engine/src/triangle_partition.rs b/crates/geom/src/triangle_partition.rs similarity index 78% rename from crates/engine/src/triangle_partition.rs rename to crates/geom/src/triangle_partition.rs index d6ad31cb..ab578c2c 100644 --- a/crates/engine/src/triangle_partition.rs +++ b/crates/geom/src/triangle_partition.rs @@ -1,9 +1,14 @@ -//! Triangles of one material grouped into the pieces polygon fill selects and fills. +//! Triangles of one material grouped into pieces joined across shared edges: the UV islands, polygons or connected meshes polygon fill selects and the UV island generators colour use std::collections::HashMap; use std::hash::Hash; -use texturecraft_geom::Mesh; +use glam::Vec2; + +use crate::Mesh; + +/// Corners closer than one step of this grid (per unit of position or UV) count as the same corner +pub const CORNER_GRID_STEPS: f32 = 1e5; /// A material's triangles split into groups joined across shared edges pub struct TrianglePartition { @@ -58,15 +63,34 @@ impl TrianglePartition { self.group_of.get(&triangle).copied() } + /// `material`'s UV islands: triangles joined where they share an edge in UV space + pub fn uv_islands(mesh: &Mesh, material: usize) -> TrianglePartition { + TrianglePartition::new(mesh, material, uv_edges) + } + /// The group holding `triangle` (None for a triangle of another material) pub fn group(&self, triangle: u32) -> Option<&[u32]> { self.index_of(triangle).and_then(|group| self.groups.get(group)).map(Vec::as_slice) } } +/// Texture coordinates of `triangle`'s corners +fn uv_corners(mesh: &Mesh, triangle: usize) -> Option<[Vec2; 3]> { + let [first, second, third] = mesh.tri(triangle)?; + Some([*mesh.uvs.get(first)?, *mesh.uvs.get(second)?, *mesh.uvs.get(third)?]) +} + +/// `triangle`'s edges in UV space: imports often split vertices along hard edges where the UVs run on, so vertex indices alone break islands apart +pub fn uv_edges(mesh: &Mesh, triangle: usize) -> Vec<([i64; 2], [i64; 2])> { + let corner = |uv: Vec2| (uv * CORNER_GRID_STEPS).round().to_array().map(|step| step as i64); + let Some([first, second, third]) = uv_corners(mesh, triangle).map(|uvs| uvs.map(corner)) else { return Vec::new() }; + + [(first, second), (second, third), (third, first)].map(|(from, to)| (from.min(to), from.max(to))).to_vec() +} + #[cfg(test)] mod tests { - use texturecraft_geom::primitives; + use crate::primitives; use super::*;