flo_animation 0.1.0

Describes a FlowBetween animation
Documentation
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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
use super::super::traits::*;

use std::iter;
use std::ops::*;

use curves::*;
use curves::bezier;
use canvas::*;

// Minimum distance between points to use to fit to a curve
const MIN_DISTANCE: f64 = 2.0;

// Scaling used on the pressure for coordinate fitting
// (As we fit to a distance of 1.0, having a pressure of 0-1 means the curve fitter can use essentially any pressure)
const INK_PRESSURE_SCALE: f64 = 50.0;

///
/// The ink brush draws a solid line with width based on pressure
/// 
pub struct InkBrush {
    /// The blend mode that this brush will use
    blend_mode: BlendMode,

    /// Width at pressure 0%
    min_width: f32,

    /// Width at pressure 100%
    max_width: f32,

    // Distance to scale up at the start of the brush stroke
    scale_up_distance: f32
}

impl InkBrush {
    ///
    /// Creates a new ink brush with the default settings
    /// 
    pub fn new(definition: &InkDefinition, drawing_style: BrushDrawingStyle) -> InkBrush {
        use BrushDrawingStyle::*;

        let blend_mode = match drawing_style {
            Draw    => BlendMode::SourceOver,
            Erase   => BlendMode::DestinationOut
        };

        InkBrush {
            blend_mode:         blend_mode,
            min_width:          definition.min_width,
            max_width:          definition.max_width,
            scale_up_distance:  definition.scale_up_distance
        }
    }
}

///
/// Ink brush coordinate (used for curve fitting)
/// 
#[derive(Clone, Copy)]
struct InkCoord {
    x: f64,
    y: f64,
    pressure: f64
}

impl InkCoord {
    pub fn pressure(&self) -> f64 { self.pressure }
    pub fn set_pressure(&mut self, new_pressure: f64) {
        self.pressure = new_pressure;
    }

    pub fn to_coord2(&self) -> (Coord2, f64) {
        (Coord2(self.x, self.y), self.pressure)
    }
}

impl<'a> From<&'a RawPoint> for InkCoord {
    fn from(src: &'a RawPoint) -> InkCoord {
        InkCoord {
            x: src.position.0 as f64,
            y: src.position.1 as f64,
            pressure: (src.pressure as f64)*INK_PRESSURE_SCALE
        }
    }
}

impl<'a> From<&'a BrushPoint> for InkCoord {
    fn from(src: &'a BrushPoint) -> InkCoord {
        InkCoord {
            x: src.position.0 as f64,
            y: src.position.1 as f64,
            pressure: (src.width as f64)*INK_PRESSURE_SCALE
        }
    }
}

impl Add<InkCoord> for InkCoord {
    type Output=InkCoord;

    #[inline]
    fn add(self, rhs: InkCoord) -> InkCoord {
        InkCoord {
            x: self.x + rhs.x,
            y: self.y + rhs.y,
            pressure: self.pressure + rhs.pressure
        }
    }
}

impl Sub<InkCoord> for InkCoord {
    type Output=InkCoord;

    #[inline]
    fn sub(self, rhs: InkCoord) -> InkCoord {
        InkCoord {
            x: self.x - rhs.x,
            y: self.y - rhs.y,
            pressure: self.pressure - rhs.pressure
        }
    }
}

impl Mul<f64> for InkCoord {
    type Output=InkCoord;

    #[inline]
    fn mul(self, rhs: f64) -> InkCoord {
        InkCoord {
            x: self.x * rhs,
            y: self.y * rhs,
            pressure: self.pressure * rhs
        }
    }
}

impl Coordinate for InkCoord {
    #[inline]
    fn from_components(components: &[f64]) -> InkCoord {
        InkCoord { x: components[0], y: components[1], pressure: components[2] }
    }

    #[inline]
    fn origin() -> InkCoord {
        InkCoord { x: 0.0, y: 0.0, pressure: 0.0 }
    }

    #[inline]
    fn len() -> usize { 3 }

    #[inline]
    fn get(&self, index: usize) -> f64 { 
        match index {
            0 => self.x,
            1 => self.y,
            2 => self.pressure,
            _ => panic!("InkCoord only has three components")
        }
    }

    fn from_biggest_components(p1: InkCoord, p2: InkCoord) -> InkCoord {
        InkCoord {
            x: f64::from_biggest_components(p1.x, p2.x),
            y: f64::from_biggest_components(p1.y, p2.y),
            pressure: f64::from_biggest_components(p1.pressure, p2.pressure)
        }
    }

    fn from_smallest_components(p1: InkCoord, p2: InkCoord) -> InkCoord {
        InkCoord {
            x: f64::from_smallest_components(p1.x, p2.x),
            y: f64::from_smallest_components(p1.y, p2.y),
            pressure: f64::from_smallest_components(p1.pressure, p2.pressure)
        }
    }

    #[inline]
    fn distance_to(&self, target: &InkCoord) -> f64 {
        let dist_x = target.x-self.x;
        let dist_y = target.y-self.y;
        let dist_p = target.pressure-self.pressure;

        f64::sqrt(dist_x*dist_x + dist_y*dist_y + dist_p*dist_p)
    }

    #[inline]
    fn dot(&self, target: &Self) -> f64 {
        self.x*target.x + self.y*target.y + self.pressure*target.pressure
    }
}

///
/// Bezier curve using InkCoords
/// 
#[derive(Clone, Copy)]
struct InkCurve {
    pub start_point:    InkCoord,
    pub end_point:      InkCoord,
    pub control_points: (InkCoord, InkCoord)
}

impl InkCurve {
    ///
    /// Creates an ink curve from brush points
    /// 
    pub fn from_brush_points(last_point: &BrushPoint, next_point: &BrushPoint) -> InkCurve {
        InkCurve {
            start_point:    InkCoord { x: last_point.position.0 as f64, y: last_point.position.1 as f64, pressure: last_point.width as f64 },
            end_point:      InkCoord { x: next_point.position.0 as f64, y: next_point.position.1 as f64, pressure: next_point.width as f64 },
            control_points: (
                InkCoord { x: next_point.cp1.0 as f64, y: next_point.cp1.1 as f64, pressure: next_point.width as f64 },
                InkCoord { x: next_point.cp2.0 as f64, y: next_point.cp2.1 as f64, pressure: next_point.width as f64 }
            )
        }
    }

    ///
    /// Converts to a pair of offset curves
    /// 
    pub fn to_offset_curves(&self, min_width: f64, max_width: f64) -> (Vec<bezier::Curve>, Vec<bezier::Curve>) {
        // Fetch the coordinates for the offset curve
        let (start, start_pressure) = self.start_point().to_coord2();
        let (end, end_pressure)     = self.end_point().to_coord2();
        let cp1                     = self.control_points.0.to_coord2().0;
        let cp2                     = self.control_points.1.to_coord2().0;

        // Create the top and bottom offsets
        let start_offset    = start_pressure*(max_width-min_width) + min_width;
        let end_offset      = end_pressure*(max_width-min_width) + min_width;
        let base_curve      = bezier::Curve::from_points(start, end, cp1, cp2);

        let offset_up       = bezier::offset(&base_curve, start_offset, end_offset);
        let offset_down     = bezier::offset(&base_curve, -start_offset, -end_offset);

        (offset_up, offset_down)
    }
}

impl BezierCurve for InkCurve {
    type Point = InkCoord;

    fn from_points(start: InkCoord, end: InkCoord, control_point1: InkCoord, control_point2: InkCoord) -> InkCurve {
        InkCurve {
            start_point:    start,
            end_point:      end,
            control_points: (control_point1, control_point2)
        }
    }

    #[inline]
    fn start_point(&self) -> InkCoord {
        self.start_point
    }

    #[inline]
    fn end_point(&self) -> InkCoord {
        self.end_point
    }

    #[inline]
    fn control_points(&self) -> (InkCoord, InkCoord) {
        self.control_points
    }
}

impl Brush for InkBrush {
    fn brush_points_for_raw_points(&self, points: &[RawPoint]) -> Vec<BrushPoint> {
        // Nothing to draw if there are no points in the brush stroke (or only one point)
        if points.len() <= 2 {
            return vec![];
        }

        // Convert points to ink points
        let ink_points: Vec<_> = points.iter().map(|point| InkCoord::from(point)).collect();

        // Average points that are very close together so we don't overdo 
        // the curve fitting
        let mut averaged_points = vec![];
        let mut last_point      = ink_points[0];
        averaged_points.push(last_point);

        for point in ink_points.iter().skip(1) {
            // If the distance between this point and the last one is below a 
            // threshold, average them together
            let distance = last_point.distance_to(point);

            if distance < MIN_DISTANCE {
                // Average this point with the previous average
                // TODO: (We should really total up the number of points we're 
                // averaging over)
                let num_averaged    = averaged_points.len();
                let current_average = averaged_points[num_averaged-1];
                let averaged_point  = (current_average + last_point) * 0.5;

                // Update the earlier point (and don't update last_point: we'll 
                // keep averaging until we find a new point far enough away)
                averaged_points[num_averaged-1] = averaged_point;
            } else {
                // Keep this point
                averaged_points.push(*point);

                // Update the last point
                last_point = *point;
            }
        }

        // Smooth out the points to remove any jitteryness
        let mut ink_points = InkCoord::smooth(&averaged_points, &[0.1, 0.25, 0.3, 0.25, 0.1]);

        // Scale up the pressure at the start of the brush stroke
        let mut distance    = 0.0;
        let mut last_point  = ink_points[0];
        let scale_up_distance = self.scale_up_distance as f64;
        for point in ink_points.iter_mut() {
            // Add to the distance
            distance += last_point.distance_to(point);
            last_point = *point;

            // Scale the pressure by the distance
            if distance > scale_up_distance { break; }

            let pressure = point.pressure();
            point.set_pressure(pressure * (distance/scale_up_distance));
        }

        /*
         * -- TODO: needs its own config option
         *
        // Scale down the pressure at the end of the brush stroke
        last_point  = *ink_points.last().unwrap();
        distance    = 0.0;
        for index in (0..ink_points.len()).rev() {
            let point = &mut ink_points[index];

            distance += last_point.distance_to(point);
            last_point = *point;

            // Scale the pressure by the distance
            if distance > scale_up_distance { break; }

            let pressure = point.pressure();
            point.set_pressure(pressure * (distance/scale_up_distance));
        }
        */

        // Fit these points to a curve
        let curve = InkCurve::fit_from_points(&ink_points, 1.0);

        // Turn into brush points
        let mut brush_points = vec![];

        if let Some(curve) = curve {
            // First point is the start point, the control points don't matter for this
            let start = curve[0].start_point();;
            brush_points.push(BrushPoint {
                position:   (start.x as f32, start.y as f32),
                cp1:        (0.0, 0.0),
                cp2:        (0.0, 0.0),
                width:      (start.pressure/INK_PRESSURE_SCALE) as f32
            });

            // Convert the remaining curve segments
            for segment in curve {
                let end             = segment.end_point();
                let (cp1, cp2)      = segment.control_points();

                brush_points.push(BrushPoint {
                    position:   (end.x as f32, end.y as f32),
                    cp1:        (cp1.x as f32, cp1.y as f32),
                    cp2:        (cp2.x as f32, cp2.y as f32),
                    width:      (end.pressure/INK_PRESSURE_SCALE) as f32
                });
            }
        }

        brush_points
    }

    fn prepare_to_render<'a>(&'a self, properties: &BrushProperties) -> Box<'a+Iterator<Item=Draw>> {
        Box::new(vec![
            Draw::BlendMode(self.blend_mode),
            Draw::FillColor(properties.color.with_alpha(properties.opacity))
        ].into_iter())
    }

    fn render_brush<'a>(&'a self, properties: &'a BrushProperties, points: &'a Vec<BrushPoint>) -> Box<'a+Iterator<Item=Draw>> {
        let size_ratio = properties.size / self.max_width;
        
        // Nothing to do if there are too few points
        if points.len() < 2 {
            return Box::new(iter::empty());
        }

        // Create an ink curve from the brush points
        let mut curve       = vec![];
        let mut last_point  = &points[0];

        for brush_point in points.iter().skip(1) {
            curve.push(InkCurve::from_brush_points(last_point, brush_point));
            last_point = brush_point;
        }
        
        // Draw a variable width line for this curve
        let (upper_curves, lower_curves): (Vec<_>, Vec<_>) = curve.into_iter()
            .map(|ink_curve| ink_curve.to_offset_curves((self.min_width*size_ratio) as f64, (self.max_width*size_ratio) as f64))
            .unzip();

        // Upper portion
        let Coord2(x, y) = upper_curves[0][0].start_point();
        let preamble = vec![
            Draw::NewPath,
            Draw::Move(x as f32, y as f32)
        ];

        let upper_curves = upper_curves.into_iter()
            .flat_map(|curve_list|  curve_list.into_iter())
            .map(|curve_section|    Draw::from(&curve_section));

        // Lower portion (reverse everything)
        let Coord2(x, y)    = {
            let last_section    = &lower_curves[lower_curves.len()-1];
            let last_curve      = &last_section[last_section.len()-1];
            last_curve.end_point()
        };

        let end_cap = Draw::Line(x as f32, y as f32);

        let lower_curves = lower_curves.into_iter()
            .rev()
            .flat_map(|curve_list|  curve_list.into_iter().rev())
            .map(|curve_section|    Draw::from(&curve_section.reverse()));

        // Finish up
        let finish = Draw::Fill;

        // Assemble the final set of instructions
        let draw_brush = preamble.into_iter()
            .chain(upper_curves)
            .chain(iter::once(end_cap))
            .chain(lower_curves)
            .chain(iter::once(finish));
        
        Box::new(draw_brush)
    }

    ///
    /// Retrieves the definition for this brush
    /// 
    fn to_definition(&self) -> (BrushDefinition, BrushDrawingStyle) {
        let definition = BrushDefinition::Ink(InkDefinition {
            min_width:          self.min_width,
            max_width:          self.max_width,
            scale_up_distance:  self.scale_up_distance
        });
        
        let drawing_style = match self.blend_mode {
            BlendMode::DestinationOut   => BrushDrawingStyle::Erase,
            _                           => BrushDrawingStyle::Draw
        };

        (definition, drawing_style)
    }
}