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(Debug, Default)]
/// Series of connecting 2D Bezier Curves
pub struct Spline {
    /// 1+3*(num_curves) points, where the first curve is represented by the first 4 points and each subsequent curve is represented by the last point in the previous curve plus 3 points
    /// Points are of PointF64 type.
    pub points: Vec<PointF64>,
}

impl Spline {

    /// Creates an empty spline defined by a starting point
    pub fn new(point: PointF64) -> Self {
        Self {
            points: vec![point],
        }
    }

    /// Adds a curve to the end of the spline. Takes 3 points that are the second to fourth control points of the bezier curve. Note that the first control point is taken from the last point of the previous curve.
    pub fn add(&mut self, point2: PointF64, point3: PointF64, point4: PointF64) {
        self.points.push(point2);
        self.points.push(point3);
        self.points.push(point4);
    }

    /// Returns an iterator on the vector of points on the spline
    pub fn iter(&self) -> std::slice::Iter<PointF64> {
        self.points.iter()
    }

    /// Returns the number of points on the spline
    pub fn len(&self) -> usize {
        self.points.len()
    }

    /// Returns the number of curves on the spline
    pub fn num_curves(&self) -> usize {
        if !self.points.is_empty() {(self.points.len()-1)/3} else {0}
    }

    /// Returns true if the spline contains no curve, false otherwise
    /// A curve is defined by 4 points, so a non-empty spline should contain at least 4 points.
    pub fn is_empty(&self) -> bool {
        self.points.len() <= 3
    }

    /// Applies an offset to all points on the spline
    pub fn offset(&mut self, offset: &PointF64) {
        for path in self.points.iter_mut() {
            path.x += offset.x;
            path.y += offset.y;
        }
    }

    /// Returns a spline created from image.
    /// The following steps are performed:
    /// 1. Convert pixels into path
    /// 2. Simplify the path into polygon
    /// 3. Smoothen the polygon and approximate it with a curve-fitter
    /// 
    /// Corner/Splice thresholds are specified in radians.
    /// Length threshold is specified in pixels (length unit in path coordinate system).
    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)
    }

    /// Returns a spline by curve-fitting a path.
    /// 
    /// Splice threshold is specified in radians.
    pub fn from_path_f64(path: &PathF64, splice_threshold: f64) -> Self {
        // First locate all the splice points
        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;
        }

        // This vector stores the indices
        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}); // Dummy initialization
        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);

            // Only the first curve need to add the first point
            if i==0 {
                result = Self::new(bezier_points[0]);
            }
            // Subsequent curves take their first point from previous curve's last point
            result.add(bezier_points[1], bezier_points[2], bezier_points[3]);
        }

        result
    }

    /// Converts spline to svg path. Panic if the length of spline is not valid (not 1+3n for some integer n)
    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
    }

}