mirror of
https://github.com/GraphiteEditor/Graphite.git
synced 2026-10-02 20:48:12 +08:00
Upgrade to the Rust 2024 edition (#2367)
* Update to rust 2024 edition * Fixes * Clean up imports * Cargo fmt again --------- Co-authored-by: Keavon Chambers <keavon@keavon.com>
This commit is contained in:
co-authored by
Keavon Chambers
parent
927d7dd9b2
commit
beb1c6ae64
@@ -1,7 +1,6 @@
|
||||
use super::*;
|
||||
use utils::format_point;
|
||||
|
||||
use std::fmt::Write;
|
||||
use utils::format_point;
|
||||
|
||||
/// Functionality relating to core `Bezier` operations, such as constructors and `abs_diff_eq`.
|
||||
impl Bezier {
|
||||
|
||||
@@ -1,6 +1,5 @@
|
||||
use crate::utils::{TValue, TValueType};
|
||||
|
||||
use super::*;
|
||||
use crate::utils::{TValue, TValueType};
|
||||
|
||||
/// Functionality relating to looking up properties of the `Bezier` or points along the `Bezier`.
|
||||
impl Bezier {
|
||||
|
||||
@@ -69,7 +69,7 @@ impl Bezier {
|
||||
/// - 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`.
|
||||
pub fn get_points(&self) -> impl Iterator<Item = DVec2> {
|
||||
pub fn get_points(&self) -> impl Iterator<Item = DVec2> + use<> {
|
||||
match self.handles {
|
||||
BezierHandles::Linear => [self.start, self.end, DVec2::ZERO, DVec2::ZERO].into_iter().take(2),
|
||||
BezierHandles::Quadratic { handle } => [self.start, handle, self.end, DVec2::ZERO].into_iter().take(3),
|
||||
|
||||
@@ -7,11 +7,9 @@ mod transform;
|
||||
|
||||
use crate::consts::*;
|
||||
use crate::utils;
|
||||
|
||||
pub use structs::*;
|
||||
|
||||
use glam::DVec2;
|
||||
use std::fmt::{Debug, Formatter, Result};
|
||||
pub use structs::*;
|
||||
|
||||
/// Representation of the handle point(s) in a bezier segment.
|
||||
#[derive(Copy, Clone, PartialEq, Debug)]
|
||||
|
||||
@@ -1,8 +1,7 @@
|
||||
use super::*;
|
||||
use crate::polynomial::Polynomial;
|
||||
use crate::utils::{solve_cubic, solve_quadratic, TValue};
|
||||
use crate::{to_symmetrical_basis_pair, SymmetricalBasis};
|
||||
|
||||
use crate::utils::{TValue, solve_cubic, solve_quadratic};
|
||||
use crate::{SymmetricalBasis, to_symmetrical_basis_pair};
|
||||
use glam::DMat2;
|
||||
use std::ops::Range;
|
||||
|
||||
@@ -99,11 +98,7 @@ impl Bezier {
|
||||
pub fn tangent(&self, t: TValue) -> DVec2 {
|
||||
let t = self.t_value_to_parametric(t);
|
||||
let tangent = self.non_normalized_tangent(t);
|
||||
if tangent.length() > 0. {
|
||||
tangent.normalize()
|
||||
} else {
|
||||
tangent
|
||||
}
|
||||
if tangent.length() > 0. { tangent.normalize() } else { tangent }
|
||||
}
|
||||
|
||||
/// Find the `t`-value(s) such that the tangent(s) at `t` pass through the specified point.
|
||||
@@ -147,11 +142,7 @@ impl Bezier {
|
||||
|
||||
let numerator = d.x * dd.y - d.y * dd.x;
|
||||
let denominator = (d.x.powf(2.) + d.y.powf(2.)).powf(1.5);
|
||||
if denominator.abs() < MAX_ABSOLUTE_DIFFERENCE {
|
||||
0.
|
||||
} else {
|
||||
numerator / denominator
|
||||
}
|
||||
if denominator.abs() < MAX_ABSOLUTE_DIFFERENCE { 0. } else { numerator / denominator }
|
||||
}
|
||||
|
||||
/// Returns two lists of `t`-values representing the local extrema of the `x` and `y` parametric curves respectively.
|
||||
@@ -228,7 +219,7 @@ impl Bezier {
|
||||
}
|
||||
|
||||
/// Returns an `Iterator` containing all possible parametric `t`-values at the given `x`-coordinate.
|
||||
pub fn find_tvalues_for_x(&self, x: f64) -> impl Iterator<Item = f64> {
|
||||
pub fn find_tvalues_for_x(&self, x: f64) -> impl Iterator<Item = f64> + use<> {
|
||||
// Compute the roots of the resulting bezier curve
|
||||
match self.handles {
|
||||
BezierHandles::Linear => {
|
||||
|
||||
@@ -1,9 +1,7 @@
|
||||
use super::*;
|
||||
|
||||
use crate::compare::compare_points;
|
||||
use crate::utils::{f64_compare, Cap, TValue};
|
||||
use crate::utils::{Cap, TValue, f64_compare};
|
||||
use crate::{AppendType, ManipulatorGroup, Subpath};
|
||||
|
||||
use glam::DMat2;
|
||||
use std::f64::consts::PI;
|
||||
|
||||
@@ -476,11 +474,7 @@ impl Bezier {
|
||||
error,
|
||||
max_iterations,
|
||||
});
|
||||
if final_low_t != 1. {
|
||||
[auto_arcs, arc_approximations].concat()
|
||||
} else {
|
||||
auto_arcs
|
||||
}
|
||||
if final_low_t != 1. { [auto_arcs, arc_approximations].concat() } else { auto_arcs }
|
||||
}
|
||||
ArcStrategy::FavorLargerArcs => self.approximate_curve_with_arcs(0., 1., error, max_iterations, false).0,
|
||||
ArcStrategy::FavorCorrectness => self
|
||||
@@ -620,9 +614,9 @@ impl Bezier {
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
use crate::EmptyId;
|
||||
use crate::compare::{compare_arcs, compare_points};
|
||||
use crate::utils::{Cap, TValue};
|
||||
use crate::EmptyId;
|
||||
|
||||
#[test]
|
||||
fn test_split() {
|
||||
@@ -777,14 +771,18 @@ mod tests {
|
||||
|
||||
// Check that the reduce helper is correct
|
||||
let (helper_curves, helper_t_values) = bezier.reduced_curves_and_t_values(None);
|
||||
assert!(reduced_curves
|
||||
.iter()
|
||||
.zip(helper_curves.iter())
|
||||
.all(|(bezier1, bezier2)| bezier1.abs_diff_eq(bezier2, MAX_ABSOLUTE_DIFFERENCE)));
|
||||
assert!(reduced_curves
|
||||
.iter()
|
||||
.zip(helper_t_values.iter())
|
||||
.all(|(curve, t_pair)| curve.abs_diff_eq(&bezier.trim(TValue::Parametric(t_pair[0]), TValue::Parametric(t_pair[1])), MAX_ABSOLUTE_DIFFERENCE)))
|
||||
assert!(
|
||||
reduced_curves
|
||||
.iter()
|
||||
.zip(helper_curves.iter())
|
||||
.all(|(bezier1, bezier2)| bezier1.abs_diff_eq(bezier2, MAX_ABSOLUTE_DIFFERENCE))
|
||||
);
|
||||
assert!(
|
||||
reduced_curves
|
||||
.iter()
|
||||
.zip(helper_t_values.iter())
|
||||
.all(|(curve, t_pair)| curve.abs_diff_eq(&bezier.trim(TValue::Parametric(t_pair[0]), TValue::Parametric(t_pair[1])), MAX_ABSOLUTE_DIFFERENCE))
|
||||
)
|
||||
}
|
||||
|
||||
fn assert_valid_offset<PointId: crate::Identifier>(bezier: &Bezier, offset: &Subpath<PointId>, expected_distance: f64) {
|
||||
|
||||
@@ -1,11 +1,9 @@
|
||||
/// Comparison functions used for tests in the bezier module
|
||||
#[cfg(test)]
|
||||
use super::{CircleArc, Subpath};
|
||||
use crate::consts::MAX_ABSOLUTE_DIFFERENCE;
|
||||
#[cfg(test)]
|
||||
use crate::utils::f64_compare;
|
||||
|
||||
use crate::consts::MAX_ABSOLUTE_DIFFERENCE;
|
||||
|
||||
use glam::DVec2;
|
||||
|
||||
// Compare two f64s with some maximum absolute difference to account for floating point errors
|
||||
|
||||
@@ -102,7 +102,7 @@ impl<const N: usize> Default for Polynomial<N> {
|
||||
impl<const N: usize> Display for Polynomial<N> {
|
||||
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
|
||||
let mut first = true;
|
||||
for (index, coefficient) in self.coefficients.iter().enumerate().rev().filter(|(_, &coefficient)| coefficient != 0.) {
|
||||
for (index, coefficient) in self.coefficients.iter().enumerate().rev().filter(|&(_, &coefficient)| coefficient != 0.) {
|
||||
if first {
|
||||
first = false;
|
||||
} else {
|
||||
|
||||
@@ -1,7 +1,6 @@
|
||||
use super::*;
|
||||
use crate::consts::*;
|
||||
use crate::utils::format_point;
|
||||
|
||||
use glam::DVec2;
|
||||
use std::fmt::Write;
|
||||
|
||||
|
||||
@@ -124,7 +124,6 @@ impl<PointId: crate::Identifier> Subpath<PointId> {
|
||||
mod tests {
|
||||
use super::*;
|
||||
use crate::utils::SubpathTValue;
|
||||
|
||||
use glam::DVec2;
|
||||
|
||||
fn set_up_open_subpath() -> Subpath<EmptyId> {
|
||||
|
||||
@@ -4,13 +4,12 @@ mod manipulators;
|
||||
mod solvers;
|
||||
mod structs;
|
||||
mod transform;
|
||||
pub use core::*;
|
||||
pub use structs::*;
|
||||
|
||||
use crate::Bezier;
|
||||
|
||||
pub use core::*;
|
||||
use std::fmt::{Debug, Formatter, Result};
|
||||
use std::ops::{Index, IndexMut};
|
||||
pub use structs::*;
|
||||
|
||||
/// Structure used to represent a path composed of [Bezier] curves.
|
||||
#[derive(Clone, PartialEq, Hash)]
|
||||
|
||||
@@ -1,8 +1,7 @@
|
||||
use super::*;
|
||||
use crate::consts::MAX_ABSOLUTE_DIFFERENCE;
|
||||
use crate::utils::{compute_circular_subpath_details, is_rectangle_inside_other, line_intersection, SubpathTValue};
|
||||
use crate::TValue;
|
||||
|
||||
use crate::consts::MAX_ABSOLUTE_DIFFERENCE;
|
||||
use crate::utils::{SubpathTValue, compute_circular_subpath_details, is_rectangle_inside_other, line_intersection};
|
||||
use glam::{DAffine2, DMat2, DVec2};
|
||||
use std::f64::consts::PI;
|
||||
|
||||
@@ -539,10 +538,9 @@ impl<PointId: crate::Identifier> Subpath<PointId> {
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
use crate::Bezier;
|
||||
use crate::consts::MAX_ABSOLUTE_DIFFERENCE;
|
||||
use crate::utils;
|
||||
use crate::Bezier;
|
||||
|
||||
use glam::DVec2;
|
||||
|
||||
fn normalize_t(n: i64, t: f64) -> f64 {
|
||||
@@ -629,44 +627,54 @@ mod tests {
|
||||
let mut n = (subpath.len() as i64) - 1;
|
||||
|
||||
let t0 = 0.;
|
||||
assert!(utils::dvec2_compare(
|
||||
subpath.evaluate(SubpathTValue::GlobalParametric(t0)),
|
||||
linear_bezier.evaluate(TValue::Parametric(normalize_t(n, t0))),
|
||||
MAX_ABSOLUTE_DIFFERENCE
|
||||
)
|
||||
.all());
|
||||
assert!(
|
||||
utils::dvec2_compare(
|
||||
subpath.evaluate(SubpathTValue::GlobalParametric(t0)),
|
||||
linear_bezier.evaluate(TValue::Parametric(normalize_t(n, t0))),
|
||||
MAX_ABSOLUTE_DIFFERENCE
|
||||
)
|
||||
.all()
|
||||
);
|
||||
|
||||
let t1 = 0.25;
|
||||
assert!(utils::dvec2_compare(
|
||||
subpath.evaluate(SubpathTValue::GlobalParametric(t1)),
|
||||
linear_bezier.evaluate(TValue::Parametric(normalize_t(n, t1))),
|
||||
MAX_ABSOLUTE_DIFFERENCE
|
||||
)
|
||||
.all());
|
||||
assert!(
|
||||
utils::dvec2_compare(
|
||||
subpath.evaluate(SubpathTValue::GlobalParametric(t1)),
|
||||
linear_bezier.evaluate(TValue::Parametric(normalize_t(n, t1))),
|
||||
MAX_ABSOLUTE_DIFFERENCE
|
||||
)
|
||||
.all()
|
||||
);
|
||||
|
||||
let t2 = 0.50;
|
||||
assert!(utils::dvec2_compare(
|
||||
subpath.evaluate(SubpathTValue::GlobalParametric(t2)),
|
||||
quadratic_bezier.evaluate(TValue::Parametric(normalize_t(n, t2))),
|
||||
MAX_ABSOLUTE_DIFFERENCE
|
||||
)
|
||||
.all());
|
||||
assert!(
|
||||
utils::dvec2_compare(
|
||||
subpath.evaluate(SubpathTValue::GlobalParametric(t2)),
|
||||
quadratic_bezier.evaluate(TValue::Parametric(normalize_t(n, t2))),
|
||||
MAX_ABSOLUTE_DIFFERENCE
|
||||
)
|
||||
.all()
|
||||
);
|
||||
|
||||
let t3 = 0.75;
|
||||
assert!(utils::dvec2_compare(
|
||||
subpath.evaluate(SubpathTValue::GlobalParametric(t3)),
|
||||
quadratic_bezier.evaluate(TValue::Parametric(normalize_t(n, t3))),
|
||||
MAX_ABSOLUTE_DIFFERENCE
|
||||
)
|
||||
.all());
|
||||
assert!(
|
||||
utils::dvec2_compare(
|
||||
subpath.evaluate(SubpathTValue::GlobalParametric(t3)),
|
||||
quadratic_bezier.evaluate(TValue::Parametric(normalize_t(n, t3))),
|
||||
MAX_ABSOLUTE_DIFFERENCE
|
||||
)
|
||||
.all()
|
||||
);
|
||||
|
||||
let t4 = 1.;
|
||||
assert!(utils::dvec2_compare(
|
||||
subpath.evaluate(SubpathTValue::GlobalParametric(t4)),
|
||||
quadratic_bezier.evaluate(TValue::Parametric(1.)),
|
||||
MAX_ABSOLUTE_DIFFERENCE
|
||||
)
|
||||
.all());
|
||||
assert!(
|
||||
utils::dvec2_compare(
|
||||
subpath.evaluate(SubpathTValue::GlobalParametric(t4)),
|
||||
quadratic_bezier.evaluate(TValue::Parametric(1.)),
|
||||
MAX_ABSOLUTE_DIFFERENCE
|
||||
)
|
||||
.all()
|
||||
);
|
||||
|
||||
// Test closed subpath
|
||||
|
||||
@@ -674,20 +682,24 @@ mod tests {
|
||||
n = subpath.len() as i64;
|
||||
|
||||
let t5 = 2. / 3.;
|
||||
assert!(utils::dvec2_compare(
|
||||
subpath.evaluate(SubpathTValue::GlobalParametric(t5)),
|
||||
cubic_bezier.evaluate(TValue::Parametric(normalize_t(n, t5))),
|
||||
MAX_ABSOLUTE_DIFFERENCE
|
||||
)
|
||||
.all());
|
||||
assert!(
|
||||
utils::dvec2_compare(
|
||||
subpath.evaluate(SubpathTValue::GlobalParametric(t5)),
|
||||
cubic_bezier.evaluate(TValue::Parametric(normalize_t(n, t5))),
|
||||
MAX_ABSOLUTE_DIFFERENCE
|
||||
)
|
||||
.all()
|
||||
);
|
||||
|
||||
let t6 = 1.;
|
||||
assert!(utils::dvec2_compare(
|
||||
subpath.evaluate(SubpathTValue::GlobalParametric(t6)),
|
||||
cubic_bezier.evaluate(TValue::Parametric(1.)),
|
||||
MAX_ABSOLUTE_DIFFERENCE
|
||||
)
|
||||
.all());
|
||||
assert!(
|
||||
utils::dvec2_compare(
|
||||
subpath.evaluate(SubpathTValue::GlobalParametric(t6)),
|
||||
cubic_bezier.evaluate(TValue::Parametric(1.)),
|
||||
MAX_ABSOLUTE_DIFFERENCE
|
||||
)
|
||||
.all()
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
@@ -737,35 +749,41 @@ mod tests {
|
||||
let quadratic_1_intersections = quadratic_bezier_1.intersections(&line, None, None);
|
||||
let subpath_intersections = subpath.intersections(&line, None, None);
|
||||
|
||||
assert!(utils::dvec2_compare(
|
||||
cubic_bezier.evaluate(TValue::Parametric(cubic_intersections[0])),
|
||||
subpath.evaluate(SubpathTValue::Parametric {
|
||||
segment_index: subpath_intersections[0].0,
|
||||
t: subpath_intersections[0].1
|
||||
}),
|
||||
MAX_ABSOLUTE_DIFFERENCE
|
||||
)
|
||||
.all());
|
||||
assert!(
|
||||
utils::dvec2_compare(
|
||||
cubic_bezier.evaluate(TValue::Parametric(cubic_intersections[0])),
|
||||
subpath.evaluate(SubpathTValue::Parametric {
|
||||
segment_index: subpath_intersections[0].0,
|
||||
t: subpath_intersections[0].1
|
||||
}),
|
||||
MAX_ABSOLUTE_DIFFERENCE
|
||||
)
|
||||
.all()
|
||||
);
|
||||
|
||||
assert!(utils::dvec2_compare(
|
||||
quadratic_bezier_1.evaluate(TValue::Parametric(quadratic_1_intersections[0])),
|
||||
subpath.evaluate(SubpathTValue::Parametric {
|
||||
segment_index: subpath_intersections[1].0,
|
||||
t: subpath_intersections[1].1
|
||||
}),
|
||||
MAX_ABSOLUTE_DIFFERENCE
|
||||
)
|
||||
.all());
|
||||
assert!(
|
||||
utils::dvec2_compare(
|
||||
quadratic_bezier_1.evaluate(TValue::Parametric(quadratic_1_intersections[0])),
|
||||
subpath.evaluate(SubpathTValue::Parametric {
|
||||
segment_index: subpath_intersections[1].0,
|
||||
t: subpath_intersections[1].1
|
||||
}),
|
||||
MAX_ABSOLUTE_DIFFERENCE
|
||||
)
|
||||
.all()
|
||||
);
|
||||
|
||||
assert!(utils::dvec2_compare(
|
||||
quadratic_bezier_1.evaluate(TValue::Parametric(quadratic_1_intersections[1])),
|
||||
subpath.evaluate(SubpathTValue::Parametric {
|
||||
segment_index: subpath_intersections[2].0,
|
||||
t: subpath_intersections[2].1
|
||||
}),
|
||||
MAX_ABSOLUTE_DIFFERENCE
|
||||
)
|
||||
.all());
|
||||
assert!(
|
||||
utils::dvec2_compare(
|
||||
quadratic_bezier_1.evaluate(TValue::Parametric(quadratic_1_intersections[1])),
|
||||
subpath.evaluate(SubpathTValue::Parametric {
|
||||
segment_index: subpath_intersections[2].0,
|
||||
t: subpath_intersections[2].1
|
||||
}),
|
||||
MAX_ABSOLUTE_DIFFERENCE
|
||||
)
|
||||
.all()
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
@@ -816,25 +834,29 @@ mod tests {
|
||||
let quadratic_1_intersections = quadratic_bezier_1.intersections(&line, None, None);
|
||||
let subpath_intersections = subpath.intersections(&line, None, None);
|
||||
|
||||
assert!(utils::dvec2_compare(
|
||||
cubic_bezier.evaluate(TValue::Parametric(cubic_intersections[0])),
|
||||
subpath.evaluate(SubpathTValue::Parametric {
|
||||
segment_index: subpath_intersections[0].0,
|
||||
t: subpath_intersections[0].1
|
||||
}),
|
||||
MAX_ABSOLUTE_DIFFERENCE
|
||||
)
|
||||
.all());
|
||||
assert!(
|
||||
utils::dvec2_compare(
|
||||
cubic_bezier.evaluate(TValue::Parametric(cubic_intersections[0])),
|
||||
subpath.evaluate(SubpathTValue::Parametric {
|
||||
segment_index: subpath_intersections[0].0,
|
||||
t: subpath_intersections[0].1
|
||||
}),
|
||||
MAX_ABSOLUTE_DIFFERENCE
|
||||
)
|
||||
.all()
|
||||
);
|
||||
|
||||
assert!(utils::dvec2_compare(
|
||||
quadratic_bezier_1.evaluate(TValue::Parametric(quadratic_1_intersections[0])),
|
||||
subpath.evaluate(SubpathTValue::Parametric {
|
||||
segment_index: subpath_intersections[1].0,
|
||||
t: subpath_intersections[1].1
|
||||
}),
|
||||
MAX_ABSOLUTE_DIFFERENCE
|
||||
)
|
||||
.all());
|
||||
assert!(
|
||||
utils::dvec2_compare(
|
||||
quadratic_bezier_1.evaluate(TValue::Parametric(quadratic_1_intersections[0])),
|
||||
subpath.evaluate(SubpathTValue::Parametric {
|
||||
segment_index: subpath_intersections[1].0,
|
||||
t: subpath_intersections[1].1
|
||||
}),
|
||||
MAX_ABSOLUTE_DIFFERENCE
|
||||
)
|
||||
.all()
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
@@ -884,35 +906,41 @@ mod tests {
|
||||
let quadratic_1_intersections = quadratic_bezier_1.intersections(&line, None, None);
|
||||
let subpath_intersections = subpath.intersections(&line, None, None);
|
||||
|
||||
assert!(utils::dvec2_compare(
|
||||
cubic_bezier.evaluate(TValue::Parametric(cubic_intersections[0])),
|
||||
subpath.evaluate(SubpathTValue::Parametric {
|
||||
segment_index: subpath_intersections[0].0,
|
||||
t: subpath_intersections[0].1
|
||||
}),
|
||||
MAX_ABSOLUTE_DIFFERENCE
|
||||
)
|
||||
.all());
|
||||
assert!(
|
||||
utils::dvec2_compare(
|
||||
cubic_bezier.evaluate(TValue::Parametric(cubic_intersections[0])),
|
||||
subpath.evaluate(SubpathTValue::Parametric {
|
||||
segment_index: subpath_intersections[0].0,
|
||||
t: subpath_intersections[0].1
|
||||
}),
|
||||
MAX_ABSOLUTE_DIFFERENCE
|
||||
)
|
||||
.all()
|
||||
);
|
||||
|
||||
assert!(utils::dvec2_compare(
|
||||
quadratic_bezier_1.evaluate(TValue::Parametric(quadratic_1_intersections[0])),
|
||||
subpath.evaluate(SubpathTValue::Parametric {
|
||||
segment_index: subpath_intersections[1].0,
|
||||
t: subpath_intersections[1].1
|
||||
}),
|
||||
MAX_ABSOLUTE_DIFFERENCE
|
||||
)
|
||||
.all());
|
||||
assert!(
|
||||
utils::dvec2_compare(
|
||||
quadratic_bezier_1.evaluate(TValue::Parametric(quadratic_1_intersections[0])),
|
||||
subpath.evaluate(SubpathTValue::Parametric {
|
||||
segment_index: subpath_intersections[1].0,
|
||||
t: subpath_intersections[1].1
|
||||
}),
|
||||
MAX_ABSOLUTE_DIFFERENCE
|
||||
)
|
||||
.all()
|
||||
);
|
||||
|
||||
assert!(utils::dvec2_compare(
|
||||
quadratic_bezier_1.evaluate(TValue::Parametric(quadratic_1_intersections[1])),
|
||||
subpath.evaluate(SubpathTValue::Parametric {
|
||||
segment_index: subpath_intersections[2].0,
|
||||
t: subpath_intersections[2].1
|
||||
}),
|
||||
MAX_ABSOLUTE_DIFFERENCE
|
||||
)
|
||||
.all());
|
||||
assert!(
|
||||
utils::dvec2_compare(
|
||||
quadratic_bezier_1.evaluate(TValue::Parametric(quadratic_1_intersections[1])),
|
||||
subpath.evaluate(SubpathTValue::Parametric {
|
||||
segment_index: subpath_intersections[2].0,
|
||||
t: subpath_intersections[2].1
|
||||
}),
|
||||
MAX_ABSOLUTE_DIFFERENCE
|
||||
)
|
||||
.all()
|
||||
);
|
||||
}
|
||||
|
||||
// TODO: add more intersection tests
|
||||
@@ -924,19 +952,19 @@ mod tests {
|
||||
|
||||
let curve = Bezier::from_quadratic_dvec2(DVec2::new(189., 289.), DVec2::new(9., 286.), DVec2::new(45., 410.));
|
||||
let curve_intersecting = Subpath::<EmptyId>::from_bezier(&curve);
|
||||
assert_eq!(curve_intersecting.is_inside_subpath(&boundary_polygon, None, None), false);
|
||||
assert!(!curve_intersecting.is_inside_subpath(&boundary_polygon, None, None));
|
||||
|
||||
let curve = Bezier::from_quadratic_dvec2(DVec2::new(115., 37.), DVec2::new(51.4, 91.8), DVec2::new(76.5, 242.));
|
||||
let curve_outside = Subpath::<EmptyId>::from_bezier(&curve);
|
||||
assert_eq!(curve_outside.is_inside_subpath(&boundary_polygon, None, None), false);
|
||||
assert!(!curve_outside.is_inside_subpath(&boundary_polygon, None, None));
|
||||
|
||||
let curve = Bezier::from_cubic_dvec2(DVec2::new(210.1, 133.5), DVec2::new(150.2, 436.9), DVec2::new(436., 285.), DVec2::new(247.6, 240.7));
|
||||
let curve_inside = Subpath::<EmptyId>::from_bezier(&curve);
|
||||
assert_eq!(curve_inside.is_inside_subpath(&boundary_polygon, None, None), true);
|
||||
assert!(curve_inside.is_inside_subpath(&boundary_polygon, None, None));
|
||||
|
||||
let line = Bezier::from_linear_dvec2(DVec2::new(101., 101.5), DVec2::new(150.2, 499.));
|
||||
let line_inside = Subpath::<EmptyId>::from_bezier(&line);
|
||||
assert_eq!(line_inside.is_inside_subpath(&boundary_polygon, None, None), true);
|
||||
assert!(line_inside.is_inside_subpath(&boundary_polygon, None, None));
|
||||
}
|
||||
|
||||
#[test]
|
||||
|
||||
@@ -1,10 +1,7 @@
|
||||
use super::Bezier;
|
||||
|
||||
use glam::{DAffine2, DVec2};
|
||||
use std::{
|
||||
fmt::{Debug, Formatter, Result},
|
||||
hash::Hash,
|
||||
};
|
||||
use std::fmt::{Debug, Formatter, Result};
|
||||
use std::hash::Hash;
|
||||
|
||||
/// An id type used for each [ManipulatorGroup].
|
||||
pub trait Identifier: Sized + Clone + PartialEq + Hash + 'static {
|
||||
@@ -113,7 +110,7 @@ impl<PointId: crate::Identifier> ManipulatorGroup<PointId> {
|
||||
|
||||
/// Are all handles at finite positions
|
||||
pub fn is_finite(&self) -> bool {
|
||||
self.anchor.is_finite() && self.in_handle.map_or(true, |handle| handle.is_finite()) && self.out_handle.map_or(true, |handle| handle.is_finite())
|
||||
self.anchor.is_finite() && self.in_handle.is_none_or(|handle| handle.is_finite()) && self.out_handle.is_none_or(|handle| handle.is_finite())
|
||||
}
|
||||
|
||||
/// Reverse directions of handles
|
||||
|
||||
@@ -1,20 +1,14 @@
|
||||
use std::vec;
|
||||
|
||||
use super::*;
|
||||
use crate::consts::MAX_ABSOLUTE_DIFFERENCE;
|
||||
use crate::utils::{Cap, Join, SubpathTValue, TValue};
|
||||
|
||||
use glam::{DAffine2, DVec2};
|
||||
use std::vec;
|
||||
|
||||
/// Helper function to ensure the index and t value pair is mapped within a maximum index value.
|
||||
/// Allows for the point to be fetched without needing to handle an additional edge case.
|
||||
/// - Ex. Via `subpath.iter().nth(index).evaluate(t);`
|
||||
fn map_index_within_range(index: usize, t: f64, max_size: usize) -> (usize, f64) {
|
||||
if max_size > 0 && index == max_size && t == 0. {
|
||||
(index - 1, 1.)
|
||||
} else {
|
||||
(index, t)
|
||||
}
|
||||
if max_size > 0 && index == max_size && t == 0. { (index - 1, 1.) } else { (index, t) }
|
||||
}
|
||||
|
||||
/// Functionality that transforms Subpaths, such as split, reduce, offset, etc.
|
||||
@@ -549,10 +543,10 @@ impl<PointId: crate::Identifier> Subpath<PointId> {
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::{Cap, Join, ManipulatorGroup, Subpath};
|
||||
use crate::EmptyId;
|
||||
use crate::compare::{compare_points, compare_subpaths, compare_vec_of_points};
|
||||
use crate::consts::MAX_ABSOLUTE_DIFFERENCE;
|
||||
use crate::utils::{SubpathTValue, TValue};
|
||||
use crate::EmptyId;
|
||||
use glam::DVec2;
|
||||
|
||||
fn set_up_open_subpath() -> Subpath<EmptyId> {
|
||||
|
||||
@@ -38,7 +38,6 @@
|
||||
*/
|
||||
|
||||
use crate::{Bezier, BezierHandles};
|
||||
|
||||
use glam::DVec2;
|
||||
|
||||
impl std::ops::Index<usize> for Bezier {
|
||||
|
||||
@@ -1,6 +1,5 @@
|
||||
use crate::consts::{MAX_ABSOLUTE_DIFFERENCE, STRICT_MAX_ABSOLUTE_DIFFERENCE};
|
||||
use crate::{ManipulatorGroup, Subpath};
|
||||
|
||||
use glam::{BVec2, DMat2, DVec2};
|
||||
use std::fmt::Write;
|
||||
|
||||
@@ -94,11 +93,7 @@ pub fn compute_abc_for_cubic_through_points(start_point: DVec2, point_on_curve:
|
||||
/// Find the roots of the linear equation `ax + b`.
|
||||
pub fn solve_linear(a: f64, b: f64) -> [Option<f64>; 3] {
|
||||
// There exist roots when `a` is not 0
|
||||
if a.abs() > MAX_ABSOLUTE_DIFFERENCE {
|
||||
[Some(-b / a), None, None]
|
||||
} else {
|
||||
[None; 3]
|
||||
}
|
||||
if a.abs() > MAX_ABSOLUTE_DIFFERENCE { [Some(-b / a), None, None] } else { [None; 3] }
|
||||
}
|
||||
|
||||
/// Find the roots of the linear equation `ax^2 + bx + c`.
|
||||
@@ -310,7 +305,8 @@ pub fn format_point(svg: &mut String, prefix: &str, x: f64, y: f64) -> std::fmt:
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
use crate::{consts::MAX_ABSOLUTE_DIFFERENCE, Bezier, EmptyId};
|
||||
use crate::consts::MAX_ABSOLUTE_DIFFERENCE;
|
||||
use crate::{Bezier, EmptyId};
|
||||
|
||||
/// Compare vectors of `f64`s with a provided max absolute value difference.
|
||||
fn f64_compare_vector(a: Vec<f64>, b: Vec<f64>, max_abs_diff: f64) -> bool {
|
||||
|
||||
Reference in New Issue
Block a user