1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
use std::cmp::Ordering;
use crate::{BinaryImage, PathF64, PointF64, PathSimplifyMode};
use super::{PathI32, smooth::SubdivideSmooth};
#[derive(Default)]
pub struct Spline {
pub points: Vec<PointF64>,
}
impl Spline {
pub fn new(point: PointF64) -> Self {
Self {
points: vec![point],
}
}
pub fn add(&mut self, point2: PointF64, point3: PointF64, point4: PointF64) {
self.points.push(point2);
self.points.push(point3);
self.points.push(point4);
}
pub fn iter(&self) -> std::slice::Iter<PointF64> {
self.points.iter()
}
pub fn len(&self) -> usize {
self.points.len()
}
pub fn num_curves(&self) -> usize {
if !self.points.is_empty() {(self.points.len()-1)/3} else {0}
}
pub fn is_empty(&self) -> bool {
self.points.len() <= 3
}
pub fn offset(&mut self, offset: &PointF64) {
for path in self.points.iter_mut() {
path.x += offset.x;
path.y += offset.y;
}
}
pub fn from_image(
image: &BinaryImage, clockwise: bool, corner_threshold: f64, outset_ratio: f64,
segment_length: f64, max_iterations: usize, splice_threshold: f64
) -> Self {
let path = PathI32::image_to_path(image, clockwise, PathSimplifyMode::Polygon);
let path = path.smooth(corner_threshold, outset_ratio, segment_length, max_iterations);
Self::from_path_f64(&path, splice_threshold)
}
pub fn from_path_f64(path: &PathF64, splice_threshold: f64) -> Self {
let splice_points = SubdivideSmooth::find_splice_points(&path, splice_threshold);
let path = &path.path[0..path.len()-1];
let len = path.len();
if len<=1 {
return Self::new(PointF64 {x:0.0,y:0.0});
}
if len==2 {
let mut result = Self::new(path[0]);
result.add(path[1], path[1], path[1]);
return result;
}
let mut cut_points: Vec<usize> = splice_points.iter()
.enumerate()
.filter(|(_, &cut)| {cut})
.map(|(i, _)| {i})
.collect();
if cut_points.is_empty() {
cut_points.push(0);
}
if cut_points.len() == 1 {
cut_points.push((cut_points[0]+len/2)%len);
}
let num_cut_points = cut_points.len();
let mut result = Self::new(PointF64 {x:0.0,y:0.0});
for i in 0..num_cut_points {
let j = (i+1)%num_cut_points;
let current = cut_points[i];
let next = cut_points[j];
let subpath = Self::get_circular_subpath(path, current, next);
let bezier_points = SubdivideSmooth::fit_points_with_bezier(&subpath);
if i==0 {
result = Self::new(bezier_points[0]);
}
result.add(bezier_points[1], bezier_points[2], bezier_points[3]);
}
result
}
pub fn to_svg_string(&self, close: bool, offset: &PointF64) -> String {
let o = offset;
if self.is_empty() {
return String::from("");
}
if (self.len() - 1) % 3 != 0 {
panic!("Invalid spline! Length must be 1+3n.");
}
let points = &self.points;
let len = points.len();
let mut result: Vec<String> = vec![format!("M{} {} ", points[0].x + o.x, points[0].y + o.y)];
let mut i = 1;
while i < len {
result.push(
format!("C{} {} {} {} {} {} ",
points[i].x + o.x, points[i].y + o.y,
points[i+1].x + o.x, points[i+1].y + o.y,
points[i+2].x + o.x, points[i+2].y + o.y)
);
i += 3;
}
if close {
result.push(String::from("Z "));
}
result.concat()
}
fn get_circular_subpath(path: &[PointF64], from: usize, to: usize) -> Vec<PointF64> {
let len = path.len();
let mut subpath: Vec<PointF64> = vec![];
match from.cmp(&to) {
Ordering::Less => {
subpath.extend_from_slice(&path[from..=to]);
},
Ordering::Greater => {
subpath.extend_from_slice(&path[from..len]);
subpath.extend_from_slice(&path[0..=to]);
},
Ordering:: Equal => {}
}
subpath
}
}