Skip to content

Commit c52957a

Browse files
committed
Refactor few methods
1 parent 153c3a8 commit c52957a

File tree

1 file changed

+27
-14
lines changed
  • node-graph/gcore/src/subpath

1 file changed

+27
-14
lines changed

node-graph/gcore/src/subpath/core.rs

Lines changed: 27 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,24 @@ use super::*;
55
use glam::DVec2;
66
use kurbo::PathSeg;
77

8-
pub fn pathseg_points(segment: PathSeg) -> (DVec2, Option<DVec2>, Option<DVec2>, DVec2) {
8+
pub struct PathSegPoints {
9+
p0: DVec2,
10+
p1: Option<DVec2>,
11+
p2: Option<DVec2>,
12+
p3: DVec2,
13+
}
14+
15+
impl PathSegPoints {
16+
pub fn new(p0: DVec2, p1: Option<DVec2>, p2: Option<DVec2>, p3: DVec2) -> Self {
17+
Self { p0, p1, p2, p3 }
18+
}
19+
}
20+
21+
pub fn pathseg_points(segment: PathSeg) -> PathSegPoints {
922
match segment {
10-
PathSeg::Line(line) => (point_to_dvec2(line.p0), None, None, point_to_dvec2(line.p1)),
11-
PathSeg::Quad(quad) => (point_to_dvec2(quad.p0), None, Some(point_to_dvec2(quad.p1)), point_to_dvec2(quad.p1)),
12-
PathSeg::Cubic(cube) => (point_to_dvec2(cube.p0), Some(point_to_dvec2(cube.p1)), Some(point_to_dvec2(cube.p2)), point_to_dvec2(cube.p1)),
23+
PathSeg::Line(line) => PathSegPoints::new(point_to_dvec2(line.p0), None, None, point_to_dvec2(line.p1)),
24+
PathSeg::Quad(quad) => PathSegPoints::new(point_to_dvec2(quad.p0), None, Some(point_to_dvec2(quad.p1)), point_to_dvec2(quad.p1)),
25+
PathSeg::Cubic(cube) => PathSegPoints::new(point_to_dvec2(cube.p0), Some(point_to_dvec2(cube.p1)), Some(point_to_dvec2(cube.p2)), point_to_dvec2(cube.p1)),
1326
}
1427
}
1528

@@ -25,8 +38,8 @@ impl<PointId: Identifier> Subpath<PointId> {
2538

2639
/// Create a `Subpath` consisting of 2 manipulator groups from a `Bezier`.
2740
pub fn from_bezier(segment: PathSeg) -> Self {
28-
let (p1, h1, h2, p2) = pathseg_points(segment);
29-
Subpath::new(vec![ManipulatorGroup::new(p1, None, h1), ManipulatorGroup::new(p2, h2, None)], false)
41+
let PathSegPoints { p0, p1, p2, p3 } = pathseg_points(segment);
42+
Subpath::new(vec![ManipulatorGroup::new(p0, None, p1), ManipulatorGroup::new(p3, p2, None)], false)
3043
}
3144

3245
/// Creates a subpath from a slice of [Bezier]. When two consecutive Beziers do not share an end and start point, this function
@@ -41,17 +54,17 @@ impl<PointId: Identifier> Subpath<PointId> {
4154

4255
let first = beziers.first().unwrap();
4356
let mut manipulator_groups = vec![ManipulatorGroup {
44-
anchor: first.0,
57+
anchor: first.p0,
4558
in_handle: None,
46-
out_handle: first.1,
59+
out_handle: first.p1,
4760
id: PointId::new(),
4861
}];
4962
let mut inner_groups: Vec<ManipulatorGroup<PointId>> = beziers
5063
.windows(2)
5164
.map(|bezier_pair| ManipulatorGroup {
52-
anchor: bezier_pair[1].0,
53-
in_handle: bezier_pair[0].2,
54-
out_handle: bezier_pair[1].1,
65+
anchor: bezier_pair[1].p0,
66+
in_handle: bezier_pair[0].p2,
67+
out_handle: bezier_pair[1].p1,
5568
id: PointId::new(),
5669
})
5770
.collect::<Vec<ManipulatorGroup<PointId>>>();
@@ -60,15 +73,15 @@ impl<PointId: Identifier> Subpath<PointId> {
6073
let last = beziers.last().unwrap();
6174
if !closed {
6275
manipulator_groups.push(ManipulatorGroup {
63-
anchor: last.3,
64-
in_handle: last.2,
76+
anchor: last.p3,
77+
in_handle: last.p2,
6578
out_handle: None,
6679
id: PointId::new(),
6780
});
6881
return Subpath::new(manipulator_groups, false);
6982
}
7083

71-
manipulator_groups[0].in_handle = last.2;
84+
manipulator_groups[0].in_handle = last.p2;
7285
Subpath::new(manipulator_groups, true)
7386
}
7487

0 commit comments

Comments
 (0)