Skip to content

Commit

Permalink
Merge pull request #1083 from hannobraun/builder
Browse files Browse the repository at this point in the history
Expand builder API
  • Loading branch information
hannobraun authored Sep 14, 2022
2 parents f88e399 + 691e7d6 commit e9e2fe6
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 30 deletions.
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

0 comments on commit e9e2fe6

Please sign in to comment.