Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Re-use fj_math::Triangle in fj_interop::mesh #886

Merged
merged 4 commits into from
Jul 28, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions crates/fj-export/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ fn export_3mf(mesh: &Mesh<Point<3>>, path: &Path) -> Result<(), Error> {
fn export_stl(mesh: &Mesh<Point<3>>, path: &Path) -> Result<(), Error> {
let points = mesh
.triangles()
.map(|triangle| triangle.points)
.map(|triangle| triangle.inner.points())
.collect::<Vec<_>>();

let vertices = points.iter().map(|points| {
Expand Down Expand Up @@ -119,8 +119,8 @@ pub enum Error {
#[error("no extension specified")]
NoExtension,

/// Unrecognised extension found
#[error("unrecognised extension found `{0:?}`")]
/// Unrecognized extension found
#[error("unrecognized extension found `{0:?}`")]
InvalidExtension(String),

/// I/O error whilst exporting to file
Expand Down
26 changes: 19 additions & 7 deletions crates/fj-interop/src/mesh.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,14 @@ where
///
/// Returns true, if a triangle with any combination of the provided points
/// is part of the mesh.
pub fn contains_triangle(&self, points: [impl Into<Point<3>>; 3]) -> bool {
let triangle = fj_math::Triangle::from_points(points).normalize();
pub fn contains_triangle(
&self,
triangle: impl Into<fj_math::Triangle<3>>,
) -> bool {
let triangle = triangle.into().normalize();

for t in &self.triangles {
let t = fj_math::Triangle::from_points(t.points).normalize();
let t = t.inner.normalize();
if triangle == t {
return true;
}
Expand All @@ -69,12 +72,21 @@ where

impl Mesh<Point<3>> {
/// Add a triangle to the mesh
pub fn push_triangle(&mut self, points: [Point<3>; 3], color: Color) {
for point in points {
pub fn push_triangle(
&mut self,
triangle: impl Into<fj_math::Triangle<3>>,
color: Color,
) {
let triangle = triangle.into();

for point in triangle.points() {
self.push_vertex(point);
}

self.triangles.push(Triangle { points, color });
self.triangles.push(Triangle {
inner: triangle,
color,
});
}
}

Expand All @@ -100,7 +112,7 @@ pub type Index = u32;
#[derive(Clone, Copy, Debug, Eq, PartialEq, Ord, PartialOrd)]
pub struct Triangle {
/// The points of the triangle
pub points: [Point<3>; 3],
pub inner: fj_math::Triangle<3>,

/// The color of the triangle
pub color: Color,
Expand Down
2 changes: 1 addition & 1 deletion crates/fj-kernel/src/algorithms/triangulate/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ pub fn triangulate(
for face in faces {
if let Some(triangles) = face.triangles() {
for &(triangle, color) in triangles {
mesh.push_triangle(triangle.points(), color);
mesh.push_triangle(triangle, color);
}
continue;
}
Expand Down
12 changes: 5 additions & 7 deletions crates/fj-viewer/src/camera.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
use std::f64::consts::FRAC_PI_2;

use fj_interop::processed_shape::ProcessedShape;
use fj_math::{Aabb, Point, Scalar, Transform, Triangle, Vector};
use fj_math::{Aabb, Point, Scalar, Transform, Vector};

use crate::screen::NormalizedPosition;

Expand Down Expand Up @@ -140,12 +140,10 @@ impl Camera {
let mut min_t = None;

for triangle in shape.mesh.triangles() {
let t = Triangle::from_points(triangle.points).cast_local_ray(
origin,
dir,
f64::INFINITY,
true,
);
let t =
triangle
.inner
.cast_local_ray(origin, dir, f64::INFINITY, true);

if let Some(t) = t {
if t <= min_t.unwrap_or(t) {
Expand Down
2 changes: 1 addition & 1 deletion crates/fj-viewer/src/graphics/vertices.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ impl From<&Mesh<fj_math::Point<3>>> for Vertices {
let mut m = Mesh::new();

for triangle in mesh.triangles() {
let [a, b, c] = triangle.points;
let [a, b, c] = triangle.inner.points();

let normal = (b - a).cross(&(c - a)).normalize();
let color = triangle.color;
Expand Down