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

Clean up surface-related code; prepare for deeper integration into partial object API #1340

Merged
merged 17 commits into from
Nov 12, 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
54 changes: 32 additions & 22 deletions crates/fj-kernel/src/algorithms/approx/curve.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@
use std::collections::BTreeMap;

use crate::{
geometry::path::{GlobalPath, SurfacePath},
objects::{Curve, GlobalCurve},
path::{GlobalPath, SurfacePath},
storage::{Handle, ObjectId},
};

Expand Down Expand Up @@ -62,7 +62,7 @@ fn approx_global_curve(
// This will probably all be unified eventually, as `SurfacePath` and
// `GlobalPath` grow APIs that are better suited to implementing this code
// in a more abstract way.
let points = match (curve.path(), curve.surface().u()) {
let points = match (curve.path(), curve.surface().geometry().u) {
(SurfacePath::Circle(_), GlobalPath::Circle(_)) => {
todo!(
"Approximating a circle on a curved surface not supported yet."
Expand Down Expand Up @@ -90,6 +90,7 @@ fn approx_global_curve(

let point_global = curve
.surface()
.geometry()
.point_from_surface_coords(point_surface);
(point_curve, point_global)
})
Expand All @@ -101,15 +102,17 @@ fn approx_global_curve(
[curve.path().point_from_path_coords(point_curve).u]
}));

let approx_u = (curve.surface().u(), range_u)
let approx_u = (curve.surface().geometry().u, range_u)
.approx_with_cache(tolerance, &mut ());

let mut points = Vec::new();
for (u, _) in approx_u {
let t = (u.t - line.origin().u) / line.direction().u;
let point_surface = curve.path().point_from_path_coords([t]);
let point_global =
curve.surface().point_from_surface_coords(point_surface);
let point_global = curve
.surface()
.geometry()
.point_from_surface_coords(point_surface);
points.push((u, point_global));
}

Expand Down Expand Up @@ -197,11 +200,11 @@ mod tests {

use crate::{
algorithms::approx::{path::RangeOnPath, Approx, ApproxPoint},
builder::CurveBuilder,
builder::{CurveBuilder, SurfaceBuilder},
geometry::path::GlobalPath,
insert::Insert,
objects::{Objects, Surface},
partial::PartialCurve,
path::GlobalPath,
objects::Objects,
partial::{PartialCurve, PartialSurface},
};

use super::CurveApprox;
Expand All @@ -210,9 +213,10 @@ mod tests {
fn approx_line_on_flat_surface() -> anyhow::Result<()> {
let objects = Objects::new();

let surface = objects
.surfaces
.insert(Surface::new(GlobalPath::x_axis(), [0., 0., 1.]))?;
let surface =
PartialSurface::from_axes(GlobalPath::x_axis(), [0., 0., 1.])
.build(&objects)?
.insert(&objects)?;
let mut curve = PartialCurve {
surface: Some(surface),
..Default::default()
Expand All @@ -232,10 +236,12 @@ mod tests {
{
let objects = Objects::new();

let surface = objects.surfaces.insert(Surface::new(
let surface = PartialSurface::from_axes(
GlobalPath::circle_from_radius(1.),
[0., 0., 1.],
))?;
)
.build(&objects)?
.insert(&objects)?;
let mut curve = PartialCurve {
surface: Some(surface),
..Default::default()
Expand All @@ -255,8 +261,9 @@ mod tests {
let objects = Objects::new();

let path = GlobalPath::circle_from_radius(1.);
let surface =
objects.surfaces.insert(Surface::new(path, [0., 0., 1.]))?;
let surface = PartialSurface::from_axes(path, [0., 0., 1.])
.build(&objects)?
.insert(&objects)?;
let mut curve = PartialCurve {
surface: Some(surface.clone()),
..Default::default()
Expand All @@ -276,7 +283,7 @@ mod tests {
let point_surface =
curve.path().point_from_path_coords(point_local);
let point_global =
surface.point_from_surface_coords(point_surface);
surface.geometry().point_from_surface_coords(point_surface);
ApproxPoint::new(point_surface, point_global)
})
.collect::<Vec<_>>();
Expand All @@ -288,9 +295,10 @@ mod tests {
fn approx_circle_on_flat_surface() -> anyhow::Result<()> {
let objects = Objects::new();

let surface = objects
.surfaces
.insert(Surface::new(GlobalPath::x_axis(), [0., 0., 1.]))?;
let surface =
PartialSurface::from_axes(GlobalPath::x_axis(), [0., 0., 1.])
.build(&objects)?
.insert(&objects)?;
let mut curve = PartialCurve {
surface: Some(surface),
..Default::default()
Expand All @@ -306,8 +314,10 @@ mod tests {
.approx(tolerance)
.into_iter()
.map(|(_, point_surface)| {
let point_global =
curve.surface().point_from_surface_coords(point_surface);
let point_global = curve
.surface()
.geometry()
.point_from_surface_coords(point_surface);
ApproxPoint::new(point_surface, point_global)
})
.collect::<Vec<_>>();
Expand Down
2 changes: 1 addition & 1 deletion crates/fj-kernel/src/algorithms/approx/path.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ use std::iter;

use fj_math::{Circle, Point, Scalar, Sign};

use crate::path::{GlobalPath, SurfacePath};
use crate::geometry::path::{GlobalPath, SurfacePath};

use super::{Approx, Tolerance};

Expand Down
2 changes: 1 addition & 1 deletion crates/fj-kernel/src/algorithms/intersect/curve_edge.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
use fj_math::{Point, Segment};

use crate::{
geometry::path::SurfacePath,
objects::{Curve, HalfEdge},
path::SurfacePath,
};

use super::LineSegmentIntersection;
Expand Down
2 changes: 1 addition & 1 deletion crates/fj-kernel/src/algorithms/intersect/ray_edge.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ use fj_math::Segment;

use crate::{
algorithms::intersect::{HorizontalRayToTheRight, Intersect},
geometry::path::SurfacePath,
objects::HalfEdge,
path::SurfacePath,
storage::Handle,
};

Expand Down
6 changes: 3 additions & 3 deletions crates/fj-kernel/src/algorithms/intersect/ray_face.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ use fj_math::{Plane, Point, Scalar};

use crate::{
algorithms::intersect::face_point::FacePointIntersection,
geometry::path::GlobalPath,
objects::{Face, HalfEdge, Vertex},
path::GlobalPath,
storage::Handle,
};

Expand All @@ -17,14 +17,14 @@ impl Intersect for (&HorizontalRayToTheRight<3>, &Handle<Face>) {
fn intersect(self) -> Option<Self::Intersection> {
let (ray, face) = self;

let plane = match face.surface().u() {
let plane = match face.surface().geometry().u {
GlobalPath::Circle(_) => todo!(
"Casting a ray against a swept circle is not supported yet"
),
GlobalPath::Line(line) => Plane::from_parametric(
line.origin(),
line.direction(),
face.surface().v(),
face.surface().geometry().v,
),
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ use fj_interop::ext::ArrayExt;
use fj_math::{Line, Plane, Point, Scalar};

use crate::{
geometry::path::{GlobalPath, SurfacePath},
objects::{Curve, GlobalCurve, Objects, Surface},
path::{GlobalPath, SurfacePath},
storage::Handle,
validate::ValidationError,
};
Expand Down Expand Up @@ -74,12 +74,12 @@ impl SurfaceSurfaceIntersection {

fn plane_from_surface(surface: &Surface) -> Plane {
let (line, path) = {
let line = match surface.u() {
let line = match surface.geometry().u {
GlobalPath::Line(line) => line,
_ => todo!("Only plane-plane intersection is currently supported."),
};

(line, surface.v())
(line, surface.geometry().v)
};

Plane::from_parametric(line.origin(), line.direction(), path)
Expand Down
40 changes: 29 additions & 11 deletions crates/fj-kernel/src/algorithms/sweep/curve.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
use fj_math::{Circle, Line, Vector};

use crate::{
builder::SurfaceBuilder,
geometry::path::{GlobalPath, SurfacePath},
insert::Insert,
objects::{Curve, Objects, Surface},
path::{GlobalPath, SurfacePath},
partial::PartialSurface,
storage::Handle,
validate::ValidationError,
};
Expand All @@ -18,7 +21,7 @@ impl Sweep for Handle<Curve> {
_: &mut SweepCache,
objects: &Objects,
) -> Result<Self::Swept, ValidationError> {
match self.surface().u() {
match self.surface().geometry().u {
GlobalPath::Circle(_) => {
// Sweeping a `Curve` creates a `Surface`. The u-axis of that
// `Surface` is a `GlobalPath`, which we are computing below.
Expand All @@ -44,27 +47,42 @@ impl Sweep for Handle<Curve> {

let u = match self.path() {
SurfacePath::Circle(circle) => {
let center =
self.surface().point_from_surface_coords(circle.center());
let a = self.surface().vector_from_surface_coords(circle.a());
let b = self.surface().vector_from_surface_coords(circle.b());
let center = self
.surface()
.geometry()
.point_from_surface_coords(circle.center());
let a = self
.surface()
.geometry()
.vector_from_surface_coords(circle.a());
let b = self
.surface()
.geometry()
.vector_from_surface_coords(circle.b());

let circle = Circle::new(center, a, b);

GlobalPath::Circle(circle)
}
SurfacePath::Line(line) => {
let origin =
self.surface().point_from_surface_coords(line.origin());
let direction =
self.surface().vector_from_surface_coords(line.direction());
let origin = self
.surface()
.geometry()
.point_from_surface_coords(line.origin());
let direction = self
.surface()
.geometry()
.vector_from_surface_coords(line.direction());

let line = Line::from_origin_and_direction(origin, direction);

GlobalPath::Line(line)
}
};

Ok(objects.surfaces.insert(Surface::new(u, path))?)
let surface = PartialSurface::from_axes(u, path)
.build(objects)?
.insert(objects)?;
Ok(surface)
}
}
2 changes: 1 addition & 1 deletion crates/fj-kernel/src/algorithms/sweep/edge.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@ use iter_fixed::IntoIteratorFixed;

use crate::{
algorithms::{reverse::Reverse, transform::TransformObject},
geometry::path::SurfacePath,
insert::Insert,
objects::{
Curve, Cycle, Face, GlobalEdge, HalfEdge, Objects, SurfaceVertex,
Vertex,
},
partial::HasPartial,
path::SurfacePath,
storage::Handle,
validate::ValidationError,
};
Expand Down
6 changes: 3 additions & 3 deletions crates/fj-kernel/src/algorithms/sweep/face.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ use fj_math::{Scalar, Vector};

use crate::{
algorithms::{reverse::Reverse, transform::TransformObject},
geometry::path::GlobalPath,
objects::{Face, Objects, Shell},
path::GlobalPath,
storage::Handle,
validate::ValidationError,
};
Expand All @@ -24,14 +24,14 @@ impl Sweep for Handle<Face> {
let mut faces = Vec::new();

let is_negative_sweep = {
let u = match self.surface().u() {
let u = match self.surface().geometry().u {
GlobalPath::Circle(_) => todo!(
"Sweeping from faces defined in round surfaces is not \
supported"
),
GlobalPath::Line(line) => line.direction(),
};
let v = self.surface().v();
let v = self.surface().geometry().v;

let normal = u.cross(&v);

Expand Down
4 changes: 2 additions & 2 deletions crates/fj-kernel/src/algorithms/sweep/vertex.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@ use fj_math::{Line, Point, Scalar, Vector};
use try_insert_ext::EntryInsertExt;

use crate::{
geometry::path::SurfacePath,
objects::{
Curve, GlobalCurve, GlobalEdge, GlobalVertex, HalfEdge, Objects,
Surface, SurfaceVertex, Vertex,
},
path::SurfacePath,
storage::Handle,
validate::ValidationError,
};
Expand Down Expand Up @@ -56,7 +56,7 @@ impl Sweep for (Handle<Vertex>, Handle<Surface>) {
// not, we have no way of knowing the surface coordinates of the input
// `Vertex` on the `Surface`, and we're going to need to do that further
// down. There's no way to check for that, unfortunately.
assert_eq!(path, surface.v());
assert_eq!(path, surface.geometry().v);

// With that out of the way, let's start by creating the `GlobalEdge`,
// as that is the most straight-forward part of this operations, and
Expand Down
21 changes: 12 additions & 9 deletions crates/fj-kernel/src/algorithms/transform/surface.rs
Original file line number Diff line number Diff line change
@@ -1,22 +1,25 @@
use fj_math::Transform;

use crate::{
objects::{Objects, Surface},
storage::Handle,
validate::ValidationError,
geometry::surface::SurfaceGeometry, objects::Objects,
partial::PartialSurface, validate::ValidationError,
};

use super::TransformObject;

impl TransformObject for Handle<Surface> {
impl TransformObject for PartialSurface {
fn transform(
self,
transform: &Transform,
objects: &Objects,
_: &Objects,
) -> Result<Self, ValidationError> {
Ok(objects.surfaces.insert(Surface::new(
self.u().transform(transform),
transform.transform_vector(&self.v()),
))?)
let geometry = self.geometry.map(|geometry| {
let u = geometry.u.transform(transform);
let v = transform.transform_vector(&geometry.v);

SurfaceGeometry { u, v }
});

Ok(Self { geometry })
}
}
Loading