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

Implement intersection::curve_face #802

Merged
merged 2 commits into from
Jul 11, 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
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions crates/fj-kernel/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ categories = ["encoding", "mathematics", "rendering"]
anymap = "1.0.0-beta.2"
map-macro = "0.2.2"
parking_lot = "0.12.0"
parry2d-f64 = "0.9.0"
robust = "0.2.3"
slotmap = "1.0.6"
spade = "2.0.0"
Expand Down
126 changes: 126 additions & 0 deletions crates/fj-kernel/src/algorithms/intersection/curve_face.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
use fj_math::{Scalar, Segment};
use parry2d_f64::query::{Ray, RayCast};

use crate::objects::{Curve, Face};

/// Determine the intersection between a [`Curve`] and a [`Face`]
///
/// Returns a list of intersections in curve coordinates.
pub fn curve_face(curve: &Curve<2>, face: &Face) -> Vec<[Scalar; 2]> {
let line = match curve {
Curve::Line(line) => line,
_ => todo!("Curve-face intersection only supports lines"),
};

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

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

(line, vertices)
});

let mut intersections = Vec::new();

for (edge_line, vertices) in face_as_polygon {
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(Scalar::from(result));
}
if let Some(result_inv) = result_inv {
intersections.push(-Scalar::from(result_inv));
}
}

assert!(intersections.len() % 2 == 0);

intersections.sort();

// Can be cleaned up, once `array_chunks` is stable:
// https://doc.rust-lang.org/std/primitive.slice.html#method.array_chunks
intersections
.chunks(2)
.map(|chunk| {
// Can't panic, as we passed `2` to `windows`.
[chunk[0], chunk[1]]
})
.collect()
}

#[cfg(test)]
mod tests {
use fj_math::{Line, Point, Scalar, Vector};

use crate::objects::{Curve, Face, Surface};

#[test]
fn curve_face() {
let curve = Curve::Line(Line {
origin: Point::from([-3., 0.]),
direction: Vector::from([1., 0.]),
});

#[rustfmt::skip]
let exterior = [
[-2., -2.],
[ 2., -2.],
[ 2., 2.],
[-2., 2.],
];
#[rustfmt::skip]
let interior = [
[-1., -1.],
[ 1., -1.],
[ 1., 1.],
[-1., 1.],
];

let face = Face::builder(Surface::xy_plane())
.with_exterior_polygon(exterior)
.with_interior_polygon(interior)
.build();

let expected: Vec<_> = [[1., 2.], [4., 5.]]
.into_iter()
.map(|interval: [f64; 2]| interval.map(Scalar::from))
.collect();
assert_eq!(super::curve_face(&curve, &face), expected);
}
}
2 changes: 2 additions & 0 deletions crates/fj-kernel/src/algorithms/intersection/mod.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
//! Intersection algorithms

mod curve_face;
mod line_segment;
mod surface_surface;

pub use self::{
curve_face::curve_face,
line_segment::{line_segment, LineSegmentIntersection},
surface_surface::surface_surface,
};