Skip to content

Commit 15d3c31

Browse files
committed
cleanup
1 parent 2c34744 commit 15d3c31

3 files changed

Lines changed: 17 additions & 30 deletions

File tree

crates/monoxide-curves/src/convert/cube_to_quad.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,21 +4,21 @@
44
55
use kurbo::PathSeg;
66

7-
use crate::{CubicBezier, CubicSegment, QuadBezier, point::Point2D, quad::QuadBezierBuilder};
7+
use crate::{CubicBezier, CubicSegment, QuadBezier, point::Point2D};
88

99
/// Convert a cubic bezier curve into a quadratic bezier curve, with `prec` as
1010
/// the maximum allowed distance between the original curve and its
1111
/// approximation.
1212
pub fn cube_to_quad(cube: CubicBezier, prec: f64) -> QuadBezier<Point2D> {
13-
let mut quad = QuadBezierBuilder::new(cube.start);
13+
let mut quad = QuadBezier::builder(cube.start);
1414

1515
for seg in cube.segment_iter() {
1616
match seg.rest {
1717
CubicSegment::Line(end) => {
1818
quad.line_to(end);
1919
}
2020
CubicSegment::Curve(..) => {
21-
let PathSeg::Cubic(cubic) = seg.to_path_seg() else {
21+
let PathSeg::Cubic(cubic) = PathSeg::from(seg) else {
2222
unreachable!("CubicSegment::Curve always converts to PathSeg::Cubic");
2323
};
2424
for (_, _, q) in cubic.to_quads(prec) {

crates/monoxide-curves/src/cube.rs

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -55,15 +55,12 @@ pub struct CubicSegmentFull {
5555
pub rest: CubicSegment,
5656
}
5757

58-
impl CubicSegmentFull {
59-
/// Converts this segment into the equivalent [`kurbo::PathSeg`], so it
60-
/// can be used with the various curve algorithms `kurbo` provides (e.g.
61-
/// evaluation, arc length).
62-
pub fn to_path_seg(&self) -> PathSeg {
63-
match self.rest {
64-
CubicSegment::Line(end) => PathSeg::Line(Line::new(self.start, end)),
58+
impl From<CubicSegmentFull> for PathSeg {
59+
fn from(seg: CubicSegmentFull) -> Self {
60+
match seg.rest {
61+
CubicSegment::Line(end) => PathSeg::Line(Line::new(seg.start, end)),
6562
CubicSegment::Curve(c1, c2, end) => {
66-
PathSeg::Cubic(CubicBez::new(self.start, c1, c2, end))
63+
PathSeg::Cubic(CubicBez::new(seg.start, c1, c2, end))
6764
}
6865
}
6966
}
@@ -116,10 +113,7 @@ impl CubicBezier {
116113
/// Evaluates the point at parameter `t` (in `[0, 1]`) on the given
117114
/// segment.
118115
pub fn point_at(&self, segment: usize, t: f64) -> Point2D {
119-
self.segment(segment)
120-
.expect("segment index out of bounds")
121-
.to_path_seg()
122-
.eval(t)
116+
PathSeg::from(self.segment(segment).expect("segment index out of bounds")).eval(t)
123117
}
124118

125119
pub fn reversed(&self) -> Self {

crates/monoxide-curves/src/stroke/widths.rs

Lines changed: 8 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use std::{cell::LazyCell, collections::BTreeMap};
22

33
use itertools::Itertools;
4-
use kurbo::ParamCurveArclen;
4+
use kurbo::{ParamCurveArclen, PathSeg};
55

66
use crate::{
77
CubicBezier,
@@ -70,23 +70,16 @@ fn calc_curve_lengths(
7070
) -> Result<Vec<f64>> {
7171
let max_error = 0.001;
7272
let mut lengths = Vec::with_capacity(curve.len());
73+
let arclen = |seg| PathSeg::from(cubic.segment(seg).unwrap()).arclen(max_error);
7374
for &[from, to] in indices.array_windows() {
74-
let mut acc = 0.0;
75-
for seg in from..to {
76-
let seg_length = cubic.segment(seg).unwrap().to_path_seg().arclen(max_error);
77-
acc += seg_length;
78-
}
79-
lengths.push(acc);
75+
lengths.push((from..to).map(arclen).sum());
8076
}
8177
// last segment
82-
{
83-
let mut acc = 0.0;
84-
for seg in indices.last().copied().unwrap_or(0)..curve.len() {
85-
let seg_length = cubic.segment(seg).unwrap().to_path_seg().arclen(max_error);
86-
acc += seg_length;
87-
}
88-
lengths.push(acc);
89-
}
78+
lengths.push(
79+
(indices.last().copied().unwrap_or(0)..curve.len())
80+
.map(arclen)
81+
.sum(),
82+
);
9083
if lengths.len() != curve.len() {
9184
return Err(Error::internal("length calculation mismatch"));
9285
}

0 commit comments

Comments
 (0)