Skip to content

Commit

Permalink
add dist 2
Browse files Browse the repository at this point in the history
  • Loading branch information
besok committed Jan 9, 2025
1 parent 9472b55 commit e47e380
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 6 deletions.
7 changes: 3 additions & 4 deletions examples/distance/main.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
use glam::{Mat4, Vec3};
use tessellate::gpu::options::{CameraOptions, GpuOptions, LightOptions};
use tessellate::mesh::distance::{distance_between_surfaces, distance_to_surface};
use tessellate::mesh::shape::pyramid::Pyramid;
use tessellate::mesh::shape::torus::Torus;
use tessellate::mesh::transform::Transform;
Expand All @@ -17,9 +16,9 @@ fn main() -> TessResult<()> {

println!("Distances");
println!(" Centroids = {}", torus_centroid.distance(&pyramid_centroid));
println!(" Torus centroid and pyramid = {}", distance_to_surface(&torus_centroid, &pyramid)?);
println!(" Pyramid centroid and torus = {}", distance_to_surface(&pyramid_centroid, &torus)?);
println!(" Surfaces = {}", distance_between_surfaces(&pyramid, &torus)?);
println!(" Torus centroid and pyramid = {}", &torus_centroid.distance_to_mesh(&pyramid)?);
println!(" Pyramid centroid and torus = {}", &pyramid_centroid.distance_to_mesh(&torus)?);
println!(" Surfaces = {}", pyramid.distance(&torus)?);

Ok(gpu::visualize(vec![torus.mesh().clone(), pyramid.mesh().clone()], GpuOptions::default())?)
}
21 changes: 19 additions & 2 deletions src/mesh.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
use crate::mesh::attributes::{Attributes, MeshType};
use crate::mesh::distance::distance_between_surfaces;
use crate::mesh::material::Color;
use crate::mesh::normals::MeshNormals;
use crate::mesh::parts::edge::Edge;
use crate::mesh::tables::MeshTables;
use crate::mesh::tables::MeshTables;
use parts::bbox::BoundingBox;
use parts::edge::MeshEdge;
use parts::face::Face;
Expand Down Expand Up @@ -360,7 +361,6 @@ impl Mesh {
Polygon::new(self.vertices().to_vec()).centroid()
}
}

}
impl Mesh {
pub fn try_tables(&self) -> MeshResult<MeshTables> {
Expand Down Expand Up @@ -453,6 +453,23 @@ impl Mesh {
pub fn face_idx_to_polygon(&self, idx: usize) -> MeshResult<Polygon> {
self.face_to_polygon(self.faces().get(idx).ok_or("Invalid face index")?)
}
/// Calculates the distance between the surfaces of two meshes.
///
/// This function computes the minimum distance between the surfaces of the current mesh
/// and another mesh. It takes into account the vertices, edges, and faces of both meshes
/// to determine the closest points and the distance between them.
///
/// # Parameters
///
/// * `other` - A reference to the other `Mesh` to calculate the distance to.
///
/// # Returns
///
/// A `MeshResult` containing the distance as an `f32` value if successful, or a `MeshError` if an error occurs.
pub fn distance(&self, other: &Mesh) -> MeshResult<f32> {
distance_between_surfaces(self, other)
}

fn face_to_polygon(&self, face: &Face) -> MeshResult<Polygon> {
face.flatten()
Expand Down
19 changes: 19 additions & 0 deletions src/mesh/parts/vertex.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use crate::mesh::distance::distance_to_surface;
use crate::mesh::{Mesh, MeshResult};
use glam::Vec3;
use std::cmp::Ordering;
use std::f32::consts::PI;
Expand Down Expand Up @@ -251,6 +253,23 @@ impl Vertex {
.sum::<f32>()
.sqrt()
}
/// Calculates the distance from the vertex to the nearest surface of the given mesh.
///
/// # Arguments
///
/// * `other` - A reference to the mesh to calculate the distance to.
///
/// # Returns
///
/// A `MeshResult` containing the distance as a `f32` value.
///
/// # Errors
///
/// Returns an error if the distance calculation fails.
pub fn distance_to_mesh(&self, other: &Mesh) -> MeshResult<f32> {
distance_to_surface(self, other)
}

pub fn distance_rounded(&self, other: &Vertex) -> usize {
self.distance(other).round() as usize
}
Expand Down

0 comments on commit e47e380

Please sign in to comment.