dima634 / baby_shark

Geometry processing library in pure rust
MIT License
65 stars 7 forks source link

Decimation from fine mesh causes artifacts. #22

Open thelazyone opened 8 months ago

thelazyone commented 8 months ago

After the excellent addition to the Voxel Remeshing I've been trying to find a workflow to create clean 3D models for printing, which implies decimation too.

The current workflow uses a modified voxel_remeshing.rs example, which performs both the remeshing and the decimation at once.

use std::path::Path;

use baby_shark::{
    io::stl::{StlReader, StlWriter},
    mesh::polygon_soup::data_structure::PolygonSoup,
    mesh::traits::Mesh,
    algo::merge_points::merge_points,
    remeshing::voxel::VoxelRemesher,
    mesh::corner_table::prelude::CornerTableF,
    decimation::{edge_decimation::ConstantErrorDecimationCriteria, prelude::EdgeDecimator},
};

fn main() {
    type Mesh = PolygonSoup<f32>;

    let mut reader = StlReader::new();
    let mesh: Mesh = reader
        .read_stl_from_file(Path::new("./assets/head.stl"))
        .expect("Read mesh");

    let mut remesher = VoxelRemesher::default().with_voxel_size(0.02);
    let remeshed = remesher.remesh(&mesh).unwrap();

    let faces = remeshed.vertices().map(|v| *remeshed.vertex_position(&v)).collect::<Vec<_>>();
    let indexed_faces = merge_points(&faces);
    let mut mesh = CornerTableF::from_vertices_and_indices(&indexed_faces.points, &indexed_faces.indices);

    let decimation_criteria = ConstantErrorDecimationCriteria::new(0.1f32);
    let mut decimator = EdgeDecimator::new().decimation_criteria(decimation_criteria);
    decimator.decimate(&mut mesh);

    StlWriter::new()
        .write_stl_to_file(&mesh, Path::new("remeshed.stl"))
        .expect("Write mesh");
}

The results of the remeshing show that there might be some artifacts causing noise on the surface. I performed some tests using a generic head model (included here head.zip ) as follows:

image

Hopefully this could be a good benchmark and a goal for the remeshing process in baby_shark.

dima634 commented 8 months ago

Could you share model decimated in blender?

thelazyone commented 8 months ago

Sure thing!

decimation_comparison.zip