Merge pull request #462 from hannobraun/mesh

Make some simplifications in `Mesh`
This commit is contained in:
Hanno Braun 2022-04-12 15:21:41 +02:00 committed by GitHub
commit b15a5f8766
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 17 additions and 35 deletions

View File

@ -1,7 +1,7 @@
use std::f64::consts::FRAC_PI_2; use std::f64::consts::FRAC_PI_2;
use fj_interop::mesh::Mesh; use fj_interop::mesh::Mesh;
use fj_math::{Aabb, Scalar}; use fj_math::{Aabb, Scalar, Triangle};
use nalgebra::{Point, TAffine, Transform, Translation, Vector}; use nalgebra::{Point, TAffine, Transform, Translation, Vector};
use parry3d_f64::query::{Ray, RayCast as _}; use parry3d_f64::query::{Ray, RayCast as _};
use winit::dpi::PhysicalPosition; use winit::dpi::PhysicalPosition;
@ -148,11 +148,9 @@ impl Camera {
let mut min_t = None; let mut min_t = None;
for triangle in mesh.triangles() { for triangle in mesh.triangles() {
let t = triangle.inner.to_parry().cast_local_ray( let t = Triangle::from_points(triangle.points)
&ray, .to_parry()
f64::INFINITY, .cast_local_ray(&ray, f64::INFINITY, true);
true,
);
if let Some(t) = t { if let Some(t) = t {
if t <= min_t.unwrap_or(t) { if t <= min_t.unwrap_or(t) {

View File

@ -71,7 +71,7 @@ impl From<&Mesh<fj_math::Point<3>>> for Vertices {
let mut m = Mesh::new(); let mut m = Mesh::new();
for triangle in mesh.triangles() { for triangle in mesh.triangles() {
let [a, b, c] = triangle.inner.points(); let [a, b, c] = triangle.points;
let normal = (b - a).cross(&(c - a)).normalize(); let normal = (b - a).cross(&(c - a)).normalize();
let color = triangle.color; let color = triangle.color;

View File

@ -36,16 +36,14 @@ where
/// Determine whether the mesh contains the provided triangle /// Determine whether the mesh contains the provided triangle
/// ///
/// Returns true, if a triangle with any combination of the points of the /// Returns true, if a triangle with any combination of the provided points
/// provided triangle is part of the mesh. /// is part of the mesh.
pub fn contains_triangle( pub fn contains_triangle(&self, points: [impl Into<Point<3>>; 3]) -> bool {
&self, let triangle = fj_math::Triangle::from_points(points).normalize();
triangle: impl Into<fj_math::Triangle<3>>,
) -> bool {
let triangle = triangle.into().normalize();
for t in &self.triangles { for t in &self.triangles {
if triangle == t.inner.normalize() { let t = fj_math::Triangle::from_points(t.points).normalize();
if triangle == t {
return true; return true;
} }
} }
@ -71,18 +69,12 @@ where
impl Mesh<Point<3>> { impl Mesh<Point<3>> {
/// Add a triangle to the mesh /// Add a triangle to the mesh
pub fn push_triangle( pub fn push_triangle(&mut self, points: [Point<3>; 3], color: Color) {
&mut self, for point in points {
triangle: impl Into<fj_math::Triangle<3>>,
color: Color,
) {
let triangle = triangle.into();
for point in triangle.points() {
self.push_vertex(point); self.push_vertex(point);
} }
self.triangles.push(Triangle::new(triangle, color)); self.triangles.push(Triangle { points, color });
} }
} }
@ -107,20 +99,12 @@ pub type Index = u32;
/// Extension of [`fj_math::Triangle`] that also includes a color. /// Extension of [`fj_math::Triangle`] that also includes a color.
#[derive(Clone, Copy, Debug, Eq, PartialEq, Ord, PartialOrd)] #[derive(Clone, Copy, Debug, Eq, PartialEq, Ord, PartialOrd)]
pub struct Triangle { pub struct Triangle {
/// The non-color part of the triangle /// The points of the triangle
pub inner: fj_math::Triangle<3>, pub points: [Point<3>; 3],
/// The color of the triangle /// The color of the triangle
pub color: Color, pub color: Color,
} }
impl Triangle {
/// Construct a new instance of `Triangle`
pub fn new(inner: impl Into<fj_math::Triangle<3>>, color: Color) -> Self {
let inner = inner.into();
Self { inner, color }
}
}
/// RGBA color /// RGBA color
pub type Color = [u8; 4]; pub type Color = [u8; 4];

View File

@ -69,7 +69,7 @@ pub fn triangulate(
} }
Face::Triangles(triangles) => { Face::Triangles(triangles) => {
for &(triangle, color) in triangles { for &(triangle, color) in triangles {
mesh.push_triangle(triangle, color); mesh.push_triangle(triangle.points(), color);
} }
} }
} }