Simplify Mesh

This commit is contained in:
Hanno Braun 2025-03-19 20:48:44 +01:00
parent ae0a88d270
commit 2d23e9f65f
6 changed files with 18 additions and 43 deletions

View File

@ -4,7 +4,6 @@ mod delaunay;
mod polygon;
use fj_interop::Mesh;
use fj_math::Point;
use crate::{Core, geometry::Tolerance, operations::presentation::GetColor};
@ -15,7 +14,7 @@ use super::approx::{Approx, face::FaceApprox};
/// Triangulate a shape
pub trait Triangulate: Sized {
/// Triangulate the shape
fn triangulate(self, core: &mut Core) -> Mesh<Point<3>> {
fn triangulate(self, core: &mut Core) -> Mesh {
let mut mesh = Mesh::new();
self.triangulate_into_mesh(&mut mesh, core);
mesh
@ -25,7 +24,7 @@ pub trait Triangulate: Sized {
///
/// This is a low-level method, intended for implementation of
/// `Triangulate`. Most callers should prefer [`Triangulate::triangulate`].
fn triangulate_into_mesh(self, mesh: &mut Mesh<Point<3>>, core: &mut Core);
fn triangulate_into_mesh(self, mesh: &mut Mesh, core: &mut Core);
}
impl<T> Triangulate for (T, Tolerance)
@ -33,7 +32,7 @@ where
T: Approx,
T::Approximation: IntoIterator<Item = FaceApprox>,
{
fn triangulate_into_mesh(self, mesh: &mut Mesh<Point<3>>, core: &mut Core) {
fn triangulate_into_mesh(self, mesh: &mut Mesh, core: &mut Core) {
let (approx, tolerance) = self;
let approx = approx.approx(tolerance, &core.layers.geometry);
@ -45,7 +44,7 @@ where
}
impl Triangulate for FaceApprox {
fn triangulate_into_mesh(self, mesh: &mut Mesh<Point<3>>, core: &mut Core) {
fn triangulate_into_mesh(self, mesh: &mut Mesh, core: &mut Core) {
let face_as_polygon = Polygon::new()
.with_exterior(
self.exterior
@ -281,7 +280,7 @@ mod tests {
fn triangulate(
face: Handle<Face>,
core: &mut Core,
) -> anyhow::Result<Mesh<Point<3>>> {
) -> anyhow::Result<Mesh> {
let tolerance = Tolerance::from_scalar(Scalar::ONE)?;
Ok(approx_face(
face,

View File

@ -17,7 +17,7 @@ use std::{
use thiserror::Error;
use fj_interop::Mesh;
use fj_math::{Point, Triangle};
use fj_math::Triangle;
/// Export the provided mesh to the file at the given path.
///
@ -25,7 +25,7 @@ use fj_math::{Point, Triangle};
///
/// Currently 3MF & STL file types are supported. The case insensitive file extension of
/// the provided path is used to switch between supported types.
pub fn export(mesh: &Mesh<Point<3>>, path: &Path) -> Result<(), Error> {
pub fn export(mesh: &Mesh, path: &Path) -> Result<(), Error> {
match path.extension() {
Some(extension) if extension.eq_ignore_ascii_case("3MF") => {
let mut file = File::create(path)?;
@ -47,10 +47,7 @@ pub fn export(mesh: &Mesh<Point<3>>, path: &Path) -> Result<(), Error> {
}
/// Export the provided mesh to the provided writer in the 3MF format.
pub fn export_3mf(
mesh: &Mesh<Point<3>>,
write: impl Write + Seek,
) -> Result<(), Error> {
pub fn export_3mf(mesh: &Mesh, write: impl Write + Seek) -> Result<(), Error> {
let vertices = mesh
.vertices()
.map(|point| threemf::model::Vertex {
@ -83,10 +80,7 @@ pub fn export_3mf(
}
/// Export the provided mesh to the provided writer in the STL format.
pub fn export_stl(
mesh: &Mesh<Point<3>>,
mut write: impl Write,
) -> Result<(), Error> {
pub fn export_stl(mesh: &Mesh, mut write: impl Write) -> Result<(), Error> {
let points = mesh
.triangles()
.map(|triangle| triangle.inner.points)
@ -130,10 +124,7 @@ pub fn export_stl(
}
/// Export the provided mesh to the provided writer in the OBJ format.
pub fn export_obj(
mesh: &Mesh<Point<3>>,
mut write: impl Write,
) -> Result<(), Error> {
pub fn export_obj(mesh: &Mesh, mut write: impl Write) -> Result<(), Error> {
for (cnt, t) in mesh.triangles().enumerate() {
// write each point of the triangle
for v in t.inner.points {

View File

@ -5,16 +5,16 @@ use fj_math::{Aabb, Point};
use crate::Color;
/// A triangle mesh
#[derive(Clone, Debug)]
pub struct Mesh<V> {
vertices: Vec<V>,
#[derive(Clone, Debug, Default)]
pub struct Mesh {
vertices: Vec<Point<3>>,
indices: Vec<Index>,
indices_by_vertex: HashMap<V, Index>,
indices_by_vertex: HashMap<Point<3>, Index>,
triangles: Vec<Triangle>,
}
impl Mesh<Point<3>> {
impl Mesh {
/// Construct a new instance of `Mesh`
pub fn new() -> Self {
Self::default()
@ -91,19 +91,6 @@ impl Mesh<Point<3>> {
}
}
// This needs to be a manual implementation. Deriving `Default` would require
// `V` to be `Default` as well, even though that is not necessary.
impl<V> Default for Mesh<V> {
fn default() -> Self {
Self {
vertices: Vec::default(),
indices: Vec::default(),
indices_by_vertex: HashMap::default(),
triangles: Vec::default(),
}
}
}
/// An index that refers to a vertex in a mesh
pub type Index = u32;

View File

@ -1,10 +1,8 @@
use fj_math::Point;
use crate::mesh::Mesh;
/// An approximated model
#[derive(Clone, Debug)]
pub struct Model {
/// The triangle mesh that approximates the model
pub mesh: Mesh<Point<3>>,
pub mesh: Mesh,
}

View File

@ -92,7 +92,7 @@ impl Camera {
fn calculate_focus_point(
&self,
cursor: Option<NormalizedScreenPosition>,
mesh: &Mesh<Point<3>>,
mesh: &Mesh,
) -> Option<FocusPoint> {
// Transform camera and cursor positions to model space.
let origin = self.position();

View File

@ -17,7 +17,7 @@ impl Vertices {
}
}
pub fn from_mesh(mesh: &Mesh<fj_math::Point<3>>) -> Self {
pub fn from_mesh(mesh: &Mesh) -> Self {
let mut vertices = Vec::new();
let mut indices = Vec::new();
let mut indices_by_vertex = BTreeMap::new();