Struct bezier_rs::Bezier

source ·
pub struct Bezier {
    pub start: DVec2,
    pub end: DVec2,
    pub handles: BezierHandles,
}
Expand description

Representation of a bezier curve with 2D points.

Fields§

§start: DVec2

Start point of the bezier curve.

§end: DVec2

Start point of the bezier curve.

§handles: BezierHandles

Handles of the bezier curve.

Implementations§

source§

impl Bezier

Functionality relating to core Bezier operations, such as constructors and abs_diff_eq.

source

pub fn from_linear_coordinates(x1: f64, y1: f64, x2: f64, y2: f64) -> Self

Create a linear bezier using the provided coordinates as the start and end points.

source

pub fn from_linear_dvec2(p1: DVec2, p2: DVec2) -> Self

Create a linear bezier using the provided DVec2s as the start and end points.

source

pub fn from_quadratic_coordinates( x1: f64, y1: f64, x2: f64, y2: f64, x3: f64, y3: f64 ) -> Self

Create a quadratic bezier using the provided coordinates as the start, handle, and end points.

source

pub fn from_quadratic_dvec2(p1: DVec2, p2: DVec2, p3: DVec2) -> Self

Create a quadratic bezier using the provided DVec2s as the start, handle, and end points.

source

pub fn from_cubic_coordinates( x1: f64, y1: f64, x2: f64, y2: f64, x3: f64, y3: f64, x4: f64, y4: f64 ) -> Self

Create a cubic bezier using the provided coordinates as the start, handles, and end points.

source

pub fn from_cubic_dvec2(p1: DVec2, p2: DVec2, p3: DVec2, p4: DVec2) -> Self

Create a cubic bezier using the provided DVec2s as the start, handles, and end points.

source

pub fn quadratic_through_points( start: DVec2, point_on_curve: DVec2, end: DVec2, t: Option<f64> ) -> Self

Create a quadratic bezier curve that goes through 3 points, where the middle point will be at the corresponding position t on the curve.

  • t - A representation of how far along the curve the provided point should occur at. The default value is 0.5. Note that when t = 0 or t = 1, the expectation is that the point_on_curve should be equal to start and end respectively. In these cases, if the provided values are not equal, this function will use the point_on_curve as the start/end instead.
source

pub fn cubic_through_points( start: DVec2, point_on_curve: DVec2, end: DVec2, t: Option<f64>, midpoint_separation: Option<f64> ) -> Self

Create a cubic bezier curve that goes through 3 points, where the middle point will be at the corresponding position t on the curve.

  • t - A representation of how far along the curve the provided point should occur at. The default value is 0.5. Note that when t = 0 or t = 1, the expectation is that the point_on_curve should be equal to start and end respectively. In these cases, if the provided values are not equal, this function will use the point_on_curve as the start/end instead.
  • midpoint_separation - A representation of how wide the resulting curve will be around t on the curve. This parameter designates the distance between the e1 and e2 defined in the projection identity section of Pomax’s bezier curve primer. It is an optional parameter and the default value is the distance between the points B and C defined in the primer.
source

pub fn svg_curve_argument(&self) -> String

Return the string argument used to create a curve in an SVG path, excluding the start point.

source

pub fn write_curve_argument(&self, svg: &mut String) -> Result

Write the curve argument to the string

source

pub fn curve_to_svg(&self, svg: &mut String, attributes: String)

Appends to the svg mutable string with an SVG shape representation of the curve.

source

pub fn handle_lines_to_svg(&self, svg: &mut String, attributes: String)

Appends to the svg mutable string with an SVG shape representation of the handle lines.

source

pub fn anchors_to_svg(&self, svg: &mut String, attributes: String)

Appends to the svg mutable string with an SVG shape representation of the anchors.

source

pub fn handles_to_svg(&self, svg: &mut String, attributes: String)

Appends to the svg mutable string with an SVG shape representation of the handles.

source

pub fn to_svg( &self, svg: &mut String, curve_attributes: String, anchor_attributes: String, handle_attributes: String, handle_line_attributes: String )

Appends to the svg mutable string with an SVG shape representation that includes the curve, the handle lines, the anchors, and the handles.

source

pub fn abs_diff_eq(&self, other: &Bezier, max_abs_diff: f64) -> bool

Returns true if the corresponding points of the two Beziers are within the provided absolute value difference from each other. The points considered includes the start, end, and any relevant handles.

source

pub fn is_point(&self) -> bool

Returns true if the start, end and handles of the Bezier are all at the same location

source§

impl Bezier

Functionality relating to looking up properties of the Bezier or points along the Bezier.

source

pub fn euclidean_to_parametric(&self, ratio: f64, error: f64) -> f64

Convert a euclidean distance ratio along the Bezier curve to a parametric t-value.

source

pub fn euclidean_to_parametric_with_total_length( &self, euclidean_t: f64, error: f64, total_length: f64 ) -> f64

Convert a euclidean distance ratio along the Bezier curve to a parametric t-value. For performance reasons, this version of the [euclidean_to_parametric] function allows the caller to provide the total length of the curve so it doesn’t have to be calculated every time the function is called.

source

pub fn evaluate(&self, t: TValue) -> DVec2

Calculate the coordinates of the point t along the curve. Expects t to be within the inclusive range [0, 1].

source

pub fn compute_lookup_table( &self, steps: Option<usize>, tvalue_type: Option<TValueType> ) -> Vec<DVec2>

Return a selection of equidistant points on the bezier curve. If no value is provided for steps, then the function will default steps to be 10.

source

pub fn length(&self, num_subdivisions: Option<usize>) -> f64

Return an approximation of the length of the bezier curve.

  • num_subdivisions - Number of subdivisions used to approximate the curve. The default value is 1000.
source

pub fn project(&self, point: DVec2, options: Option<ProjectionOptions>) -> f64

Returns the parametric t-value that corresponds to the closest point on the curve to the provided point. Uses a searching algorithm akin to binary search that can be customized using the optional ProjectionOptions struct.

source§

impl Bezier

Functionality for the getters and setters of the various points in a Bezier

source

pub fn set_start(&mut self, s: DVec2)

Set the coordinates of the start point.

source

pub fn set_end(&mut self, e: DVec2)

Set the coordinates of the end point.

source

pub fn set_handle_start(&mut self, h1: DVec2)

Set the coordinates of the first handle point. This represents the only handle in a quadratic segment. If used on a linear segment, it will be changed to a quadratic.

source

pub fn set_handle_end(&mut self, h2: DVec2)

Set the coordinates of the second handle point. This will convert both linear and quadratic segments into cubic ones. For a linear segment, the first handle will be set to the start point.

source

pub fn start(&self) -> DVec2

Get the coordinates of the bezier segment’s start point.

source

pub fn end(&self) -> DVec2

Get the coordinates of the bezier segment’s end point.

source

pub fn handle_start(&self) -> Option<DVec2>

Get the coordinates of the bezier segment’s first handle point. This represents the only handle in a quadratic segment.

source

pub fn handle_end(&self) -> Option<DVec2>

Get the coordinates of the second handle point. This will return None for a quadratic segment.

source

pub fn get_points(&self) -> impl Iterator<Item = DVec2>

Get an iterator over the coordinates of all points in a vector.

  • For a linear segment, the order of the points will be: start, end.
  • For a quadratic segment, the order of the points will be: start, handle, end.
  • For a cubic segment, the order of the points will be: start, handle_start, handle_end, end.
source§

impl Bezier

Functionality that solve for various curve information such as derivative, tangent, intersect, etc.

source

pub fn roots(self) -> [Vec<f64>; 2]

Get roots as [[x], [y]]

source

pub fn de_casteljau_points(&self, t: TValue) -> Vec<Vec<DVec2>>

Returns a list of lists of points representing the De Casteljau points for all iterations at the point t along the curve using De Casteljau’s algorithm. The ith element of the list represents the set of points in the ith iteration. More information on the algorithm can be found in the De Casteljau section in Pomax’s primer.

source

pub fn derivative(&self) -> Option<Bezier>

Returns a Bezier representing the derivative of the original curve.

  • This function returns None for a linear segment.
source

pub fn tangent(&self, t: TValue) -> DVec2

Returns a normalized unit vector representing the tangent at the point t along the curve.

source

pub fn tangents_to_point(self, point: DVec2) -> Vec<f64>

Find the t-value(s) such that the tangent(s) at t pass through the specified point.

source

pub fn normal(&self, t: TValue) -> DVec2

Returns a normalized unit vector representing the direction of the normal at the point t along the curve.

source

pub fn normals_to_point(self, point: DVec2) -> Vec<f64>

Find the t-value(s) such that the normal(s) at t pass through the specified point.

source

pub fn curvature(&self, t: TValue) -> f64

Returns the curvature, a scalar value for the derivative at the point t along the curve. Curvature is 1 over the radius of a circle with an equivalent derivative.

source

pub fn local_extrema(&self) -> [impl Iterator<Item = f64>; 2]

Returns two lists of t-values representing the local extrema of the x and y parametric curves respectively. The list of t-values returned are filtered such that they fall within the range [0, 1].

source

pub fn bounding_box(&self) -> [DVec2; 2]

Return the min and max corners that represent the bounding box of the curve.

source

pub fn bounding_box_of_anchors_and_handles(&self) -> [DVec2; 2]

Return the min and max corners that represent the bounding box enclosing this Bezier’s two anchor points and any handles.

source

pub fn is_contained_within(&self, min_corner: DVec2, max_corner: DVec2) -> bool

Returns true if the bounding box of the bezier is contained entirely within a rectangle defined by its minimum and maximum corners.

source

pub fn find_tvalues_for_x(&self, x: f64) -> impl Iterator<Item = f64>

Returns an Iterator containing all possible parametric t-values at the given x-coordinate.

source

pub fn unrestricted_inflections(&self) -> impl Iterator<Item = f64>

Returns list of t-values representing the inflection points of the curve. The inflection points are defined to be points at which the second derivative of the curve is equal to zero.

source

pub fn inflections(&self) -> Vec<f64>

Returns list of parametric t-values representing the inflection points of the curve. The list of t-values returned are filtered such that they fall within the range [0, 1].

source

pub fn intersections( &self, other: &Bezier, error: Option<f64>, minimum_separation: Option<f64> ) -> Vec<f64>

Returns a list of filtered parametric t values that correspond to intersection points between the current bezier curve and the provided one such that the difference between adjacent t values in sorted order is greater than some minimum separation value. If the difference between 2 adjacent t values is less than the minimum difference, the filtering takes the larger t value and discards the smaller t value. The returned t values are with respect to the current bezier, not the provided parameter. If the provided curve is linear, then zero intersection points will be returned along colinear segments.

  • error - For intersections where the provided bezier is non-linear, error defines the threshold for bounding boxes to be considered an intersection point.
  • minimum_separation - The minimum difference between adjacent t values in sorted order
source

pub fn line_test_crossings( &self, point_on_line: DVec2, direction_vector: DVec2 ) -> impl Iterator<Item = f64> + '_

Returns a list of t values that correspond to points on this Bezier segment where they intersect with the given line. (direction_vector does not need to be normalized.) If this needs to be called frequently with a line of the same rotation angle, consider instead using [line_test_crossings_prerotated] and moving this function’s setup code into your own logic before the repeated call.

source

pub fn line_test_crossings_prerotated( &self, point_on_line: DVec2, rotation_matrix: DMat2, rotated_bezier: Self ) -> impl Iterator<Item = f64> + '_

Returns a list of t values that correspond to points on this Bezier segment where they intersect with the given infinite line. This version of the function is for better performance when calling it frequently without needing to change the rotation between each call. If that isn’t important, use [line_test_crossings] which wraps this and provides an easier interface by taking a line rotation vector. Instead, this version requires a rotation matrix for the line’s rotation and a version of this Bezier segment that has had its rotation already applied.

source

pub fn ray_test_crossings( &self, ray_start: DVec2, ray_direction: DVec2 ) -> impl Iterator<Item = f64> + '_

Returns a list of t values that correspond to points on this Bezier segment where they intersect with the given ray. (ray_direction does not need to be normalized.) If this needs to be called frequently with a ray of the same rotation angle, consider instead using [ray_test_crossings_prerotated] and moving this function’s setup code into your own logic before the repeated call.

source

pub fn ray_test_crossings_prerotated( &self, ray_start: DVec2, rotation_matrix: DMat2, rotated_bezier: Self ) -> impl Iterator<Item = f64> + '_

Returns a list of t values that correspond to points on this Bezier segment where they intersect with the given infinite ray. This version of the function is for better performance when calling it frequently without needing to change the rotation between each call. If that isn’t important, use [ray_test_crossings] which wraps this and provides an easier interface by taking a ray direction vector. Instead, this version requires a rotation matrix for the ray’s rotation and a version of this Bezier segment that has had its rotation already applied.

source

pub fn self_intersections(&self, error: Option<f64>) -> Vec<[f64; 2]>

Returns a list of parametric t values that correspond to the self intersection points of the current bezier curve. For each intersection point, the returned t value is the smaller of the two that correspond to the point.

  • error - For intersections with non-linear beziers, error defines the threshold for bounding boxes to be considered an intersection point.
source

pub fn rectangle_intersections( &self, corner1: DVec2, corner2: DVec2 ) -> Vec<f64>

Returns a list of parametric t values that correspond to the intersection points between the curve and a rectangle defined by opposite corners.

source

pub fn join(&self, other: &Bezier) -> Bezier

Returns a cubic bezier which joins this with the provided bezier curve. The resulting path formed by the Bezier curves is continuous up to the first derivative.

source

pub fn winding(&self, target_point: DVec2) -> i32

Compute the winding number contribution of a single segment.

Cast a ray to the left and count intersections.

source§

impl Bezier

Functionality that transform Beziers, such as split, reduce, offset, etc.

source

pub fn to_linear(&self) -> Bezier

Returns a linear approximation of the given Bezier. For higher order Bezier, this means simply dropping the handles.

source

pub fn to_quadratic(&self) -> Bezier

Returns a quadratic approximation of the given Bezier. For cubic Bezier, which typically cannot be represented by a single quadratic segment, this function simply takes the average of the cubic handles to be the new quadratic handle.

source

pub fn to_cubic(&self) -> Bezier

Returns a cubic approximation of the given Bezier.

source

pub fn split(&self, t: TValue) -> [Bezier; 2]

Returns the pair of Bezier curves that result from splitting the original curve at the point t along the curve.

source

pub fn reverse(&self) -> Bezier

Returns a reversed version of the Bezier curve.

source

pub fn trim(&self, t1: TValue, t2: TValue) -> Bezier

Returns the Bezier curve representing the sub-curve between the two provided points. It will start at the point corresponding to the smaller of t1 and t2, and end at the point corresponding to the larger of t1 and t2.

source

pub fn apply_transformation( &self, transformation_function: impl Fn(DVec2) -> DVec2 ) -> Bezier

Returns a Bezier curve that results from applying the transformation function to each point in the Bezier.

source

pub fn rotate(&self, angle: f64) -> Bezier

Returns a Bezier curve that results from rotating the curve around the origin by the given angle (in radians).

source

pub fn rotate_about_point(&self, angle: f64, pivot: DVec2) -> Bezier

Returns a Bezier curve that results from rotating the curve around the provided point by the given angle (in radians).

source

pub fn translate(&self, translation: DVec2) -> Bezier

Returns a Bezier curve that results from translating the curve by the given DVec2.

source

pub fn reduce(&self, step_size: Option<f64>) -> Vec<Bezier>

Split the curve into a number of scalable subcurves. This function may introduce gaps if subsections of the curve are not reducible. The function takes the following parameter:

  • step_size - Dictates the granularity at which the function searches for reducible subcurves. The default value is 0.01. A small granularity may increase the chance the function does not introduce gaps, but will increase computation time.
source

pub fn graduated_scale(&self, start_distance: f64, end_distance: f64) -> Bezier

Version of the scale function which scales the curve such that the start of the scaled curve is start_distance from the original curve, while the end of of the scaled curve is end_distance from the original curve. The curve transitions from start_distance to end_distance gradually, proportional to the distance along the equation (t-value) of the curve.

source

pub fn offset<ManipulatorGroupId: Identifier>( &self, distance: f64 ) -> Subpath<ManipulatorGroupId>

Offset will break down the Bezier into reducible subcurves, and scale each subcurve a set distance from the original curve. Note that not all bezier curves are possible to offset, so this function first reduces the curve to scalable segments and then offsets those segments. A proof for why this is true can be found in the Curve offsetting section of Pomax’s bezier curve primer. Offset takes the following parameter:

  • distance - The offset’s distance from the curve. Positive values will offset the curve in the same direction as the endpoint normals, while negative values will offset in the opposite direction.
source

pub fn graduated_offset<ManipulatorGroupId: Identifier>( &self, start_distance: f64, end_distance: f64 ) -> Subpath<ManipulatorGroupId>

Version of the offset function which scales the offset such that the start of the offset is start_distance from the original curve, while the end of of the offset is end_distance from the original curve. The curve transitions from start_distance to end_distance gradually, proportional to the distance along the equation (t-value) of the curve. Similarly to the offset function, the returned result is an approximation.

source

pub fn outline<ManipulatorGroupId: Identifier>( &self, distance: f64, cap: Cap ) -> Subpath<ManipulatorGroupId>

Outline will return a vector of Beziers that creates an outline around the curve at the designated distance away from the curve. It makes use of the offset function, thus restrictions applicable to offset are relevant to this function as well. The ‘caps’, the linear segments at opposite ends of the outline, intersect the original curve at the midpoint of the cap. Outline takes the following parameter:

  • distance - The outline’s distance from the curve.
source

pub fn graduated_outline<ManipulatorGroupId: Identifier>( &self, start_distance: f64, end_distance: f64, cap: Cap ) -> Subpath<ManipulatorGroupId>

Version of the outline function which draws the outline at the specified distances away from the curve. The outline begins start_distance away, and gradually move to being end_distance away.

source

pub fn skewed_outline<ManipulatorGroupId: Identifier>( &self, distance1: f64, distance2: f64, distance3: f64, distance4: f64, cap: Cap ) -> Subpath<ManipulatorGroupId>

Version of the graduated_outline function that allows for the 4 corners of the outline to be different distances away from the curve.

source

pub fn arcs(&self, arcs_options: ArcsOptions) -> Vec<CircleArc>

Approximate a bezier curve with circular arcs. The algorithm can be customized using the ArcsOptions structure.

Trait Implementations§

source§

impl Clone for Bezier

source§

fn clone(&self) -> Bezier

Returns a copy of the value. Read more
1.0.0 · source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
source§

impl Debug for Bezier

source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
source§

impl Index<usize> for Bezier

§

type Output = DVec2

The returned type after indexing.
source§

fn index(&self, index: usize) -> &Self::Output

Performs the indexing (container[index]) operation. Read more
source§

impl PartialEq for Bezier

source§

fn eq(&self, other: &Bezier) -> bool

This method tests for self and other values to be equal, and is used by ==.
1.0.0 · source§

fn ne(&self, other: &Rhs) -> bool

This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
source§

impl Copy for Bezier

source§

impl StructuralPartialEq for Bezier

Auto Trait Implementations§

Blanket Implementations§

source§

impl<T> Any for T
where T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for T
where T: ?Sized,

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T, U> Into<U> for T
where U: From<T>,

source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

source§

impl<T> ToOwned for T
where T: Clone,

§

type Owned = T

The resulting type after obtaining ownership.
source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.