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

Expand builder API #1083

Merged
merged 7 commits into from
Sep 14, 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
30 changes: 23 additions & 7 deletions crates/fj-kernel/src/builder/curve.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use fj_math::{Line, Point, Vector};
use fj_math::{Line, Point, Scalar, Vector};

use crate::{
objects::{Curve, GlobalCurve, Surface},
Expand All @@ -18,23 +18,33 @@ impl CurveBuilder {
Self { surface }
}

/// Create a line that represents the u-axis on the surface
/// Build a line that represents the u-axis on the surface
pub fn u_axis(&self) -> Curve {
let a = Point::origin();
let b = a + Vector::unit_u();

self.line_from_points([a, b])
}

/// Create a line that represents the v-axis on the surface
/// Build a line that represents the v-axis on the surface
pub fn v_axis(&self) -> Curve {
let a = Point::origin();
let b = a + Vector::unit_v();

self.line_from_points([a, b])
}

/// Create a line from the given points
/// Build a circle from the given radius
pub fn circle_from_radius(&self, radius: impl Into<Scalar>) -> Curve {
let radius = radius.into();

let path = SurfacePath::circle_from_radius(radius);
let global_form = GlobalCurveBuilder.circle_from_radius(radius);

Curve::new(self.surface, path, global_form)
}

/// Build a line from the given points
pub fn line_from_points(&self, points: [impl Into<Point<2>>; 2]) -> Curve {
let points = points.map(Into::into);

Expand All @@ -55,21 +65,27 @@ impl CurveBuilder {
pub struct GlobalCurveBuilder;

impl GlobalCurveBuilder {
/// Create a line that represents the x-axis
/// Build a line that represents the x-axis
pub fn x_axis(&self) -> GlobalCurve {
GlobalCurve::from_path(GlobalPath::x_axis())
}

/// Create a line that represents the y-axis
/// Build a line that represents the y-axis
pub fn y_axis(&self) -> GlobalCurve {
GlobalCurve::from_path(GlobalPath::y_axis())
}

/// Create a line that represents the z-axis
/// Build a line that represents the z-axis
pub fn z_axis(&self) -> GlobalCurve {
GlobalCurve::from_path(GlobalPath::z_axis())
}

/// Build a circle from the given radius
pub fn circle_from_radius(&self, radius: impl Into<Scalar>) -> GlobalCurve {
let path = GlobalPath::circle_from_radius(radius);
GlobalCurve::from_path(path)
}

/// Create a line from the given points
pub fn line_from_points(
&self,
Expand Down
20 changes: 3 additions & 17 deletions crates/fj-kernel/src/builder/edge.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use fj_math::{Circle, Line, Point, Scalar, Vector};
use fj_math::{Line, Point, Scalar};

use crate::{
objects::{
Expand All @@ -22,22 +22,8 @@ impl HalfEdgeBuilder {
}

/// Build a circle from the given radius
pub fn circle_from_radius(&self, radius: Scalar) -> HalfEdge {
let curve = {
let path = SurfacePath::Circle(Circle::new(
Point::origin(),
Vector::from([radius, Scalar::ZERO]),
Vector::from([Scalar::ZERO, radius]),
));
let global =
GlobalCurve::from_path(GlobalPath::Circle(Circle::new(
Point::origin(),
Vector::from([radius, Scalar::ZERO, Scalar::ZERO]),
Vector::from([Scalar::ZERO, radius, Scalar::ZERO]),
)));

Curve::new(self.surface, path, global)
};
pub fn circle_from_radius(&self, radius: impl Into<Scalar>) -> HalfEdge {
let curve = Curve::build(self.surface).circle_from_radius(radius);

let vertices = {
let [a_curve, b_curve] =
Expand Down
34 changes: 28 additions & 6 deletions crates/fj-kernel/src/path.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
//! [`Surface`]: crate::objects::Surface
//! [#1021]: https://github.com/hannobraun/Fornjot/issues/1021

use fj_math::{Circle, Line, Point, Vector};
use fj_math::{Circle, Line, Point, Scalar, Vector};

/// A path through surface (2D) space
#[derive(Clone, Copy, Debug, Eq, PartialEq, Hash, Ord, PartialOrd)]
Expand All @@ -35,6 +35,17 @@ pub enum SurfacePath {
}

impl SurfacePath {
/// Build a circle from the given radius
pub fn circle_from_radius(radius: impl Into<Scalar>) -> Self {
let radius = radius.into();

SurfacePath::Circle(Circle::new(
Point::origin(),
Vector::from([radius, Scalar::ZERO]),
Vector::from([Scalar::ZERO, radius]),
))
}

/// Construct a line from two points
pub fn line_from_points(points: [impl Into<Point<2>>; 2]) -> Self {
Self::Line(Line::from_points(points))
Expand Down Expand Up @@ -63,11 +74,6 @@ pub enum GlobalPath {
}

impl GlobalPath {
/// Construct a line from two points
pub fn line_from_points(points: [impl Into<Point<3>>; 2]) -> Self {
Self::Line(Line::from_points(points))
}

/// Construct a `GlobalPath` that represents the x-axis
pub fn x_axis() -> Self {
Self::Line(Line::from_origin_and_direction(
Expand All @@ -92,6 +98,22 @@ impl GlobalPath {
))
}

/// Build a circle from the given radius
pub fn circle_from_radius(radius: impl Into<Scalar>) -> Self {
let radius = radius.into();

GlobalPath::Circle(Circle::new(
Point::origin(),
Vector::from([radius, Scalar::ZERO, Scalar::ZERO]),
Vector::from([Scalar::ZERO, radius, Scalar::ZERO]),
))
}

/// Construct a line from two points
pub fn line_from_points(points: [impl Into<Point<3>>; 2]) -> Self {
Self::Line(Line::from_points(points))
}

/// Access the origin of the path's coordinate system
pub fn origin(&self) -> Point<3> {
match self {
Expand Down