Skip to content

Commit

Permalink
Make use of CurveEdgeIntersection
Browse files Browse the repository at this point in the history
  • Loading branch information
hannobraun committed Jul 28, 2022
1 parent 80bba26 commit 470cba1
Showing 1 changed file with 14 additions and 62 deletions.
76 changes: 14 additions & 62 deletions crates/fj-kernel/src/algorithms/intersection/curve_face.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
use std::vec;

use fj_math::{Point, Segment};
use parry2d_f64::query::{Ray, RayCast};
use fj_math::Point;

use crate::objects::{Curve, Face};
use crate::{
algorithms::intersection::CurveEdgeIntersection,
objects::{Curve, Face},
};

/// The intersections between a [`Curve`] and a [`Face`], in curve coordinates
#[derive(Clone, Debug, Eq, PartialEq, Hash, Ord, PartialOrd)]
Expand All @@ -27,68 +29,18 @@ impl CurveFaceIntersectionList {

/// Compute the intersections between a [`Curve`] and a [`Face`]
pub fn compute(curve: &Curve<2>, face: &Face) -> Self {
let line = match curve {
Curve::Line(line) => line,
_ => todo!("Curve-face intersection only supports lines"),
};

let edges = face
.all_cycles()
.flat_map(|cycle| {
let edges: Vec<_> = cycle.edges().cloned().collect();
edges
})
.map(|edge| {
let line = match edge.curve().local_form() {
Curve::Line(line) => line,
_ => {
todo!("Curve-face intersection only supports polygons")
}
};

let vertices = match edge.vertices().get() {
Some(vertices) => vertices.map(|&vertex| vertex),
None => todo!(
"Curve-face intersection does not support faces with \
continuous edges"
),
};

(*line, vertices)
});
let edges = face.all_cycles().flat_map(|cycle| {
let edges: Vec<_> = cycle.edges().cloned().collect();
edges
});

let mut intersections = Vec::new();

for (edge_line, vertices) in edges {
let vertices = vertices.map(|vertex| {
edge_line.point_from_line_coords(vertex.position())
});
let segment = Segment::from_points(vertices);

let ray = Ray {
origin: line.origin.to_na(),
dir: line.direction.to_na(),
};
let ray_inv = Ray {
origin: line.origin.to_na(),
dir: -line.direction.to_na(),
};

let result =
segment
.to_parry()
.cast_local_ray(&ray, f64::INFINITY, false);
let result_inv = segment.to_parry().cast_local_ray(
&ray_inv,
f64::INFINITY,
false,
);

if let Some(result) = result {
intersections.push(Point::from([result]));
}
if let Some(result_inv) = result_inv {
intersections.push(Point::from([-result_inv]));
for edge in edges {
let intersection = CurveEdgeIntersection::compute(curve, &edge);

if let Some(intersection) = intersection {
intersections.push(intersection.point_on_curve());
}
}

Expand Down

0 comments on commit 470cba1

Please sign in to comment.