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
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
// Anima Engine. The quirky game engine
// Copyright (C) 2016  Dragoș Tiselice
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with this program.  If not, see <http://www.gnu.org/licenses/>.

use math::Vector;

/// A `macro` useful for defining Bézier curves.
///
/// # Examples
///
/// ```
/// # #[macro_use] extern crate anima_engine;
/// # use anima_engine::math::Bezier;
/// # use anima_engine::math::Vector;
/// fn main() {
///     let b = bez!(
///         (0.0, 0.0, 0.0),
///         (0.0, 0.0, 0.0),
///         (0.0, 0.0, 0.0),
///         (0.0, 0.0, 0.0)
///     );
/// }
/// ```
#[macro_export]
macro_rules! bez {
    (
        ( $x1:expr, $y1:expr, $z1:expr ),
        ( $x2:expr, $y2:expr, $z2:expr ),
        ( $x3:expr, $y3:expr, $z3:expr )
    ) => {
        Bezier::new_sqr(
            Vector::new($x1, $y1, $z1),
            Vector::new($x2, $y2, $z2),
            Vector::new($x3, $y3, $z3)
        )
    };

    (
        ( $x1:expr, $y1:expr, $z1:expr ),
        ( $x2:expr, $y2:expr, $z2:expr ),
        ( $x3:expr, $y3:expr, $z3:expr ),
        ( $x4:expr, $y4:expr, $z4:expr )
    ) => {
        Bezier::new_cub(
            Vector::new($x1, $y1, $z1),
            Vector::new($x2, $y2, $z2),
            Vector::new($x3, $y3, $z3),
            Vector::new($x4, $y4, $z4)
        )
    };
}

/// A `macro` useful for defining Bézier paths.
///
/// # Examples
///
/// ```
/// # #[macro_use] extern crate anima_engine;
/// # use anima_engine::math::BezierPath;
/// # use anima_engine::math::Bezier;
/// # use anima_engine::math::Vector;
/// fn main() {
///     let p = path!(
///         bez!(
///             (0.0, 0.0, 0.0),
///             (0.0, 0.0, 0.0),
///             (0.0, 0.0, 0.0),
///             (0.0, 0.0, 0.0)
///         ),
///         bez!(
///             (0.0, 0.0, 0.0),
///             (0.0, 0.0, 0.0),
///             (0.0, 0.0, 0.0)
///         )
///     );
/// }
/// ```
#[macro_export]
macro_rules! path {
    ( $( $curve:expr ),* ) => {
        BezierPath::new(
            vec![$( $curve ),*]
        )
    }
}

/// A `struct` useful for creating square and cubic Bézier curves.
#[derive(Clone, Copy, Debug, PartialEq)]
pub struct Bezier {
    v1: Vector,
    v2: Vector,
    v3: Vector,
    v4: Option<Vector>
}

impl Bezier {
    /// Creates a square Bézier from `v1` to `v3` curving towards `v2`.
    ///
    /// # Examples
    ///
    /// ```
    /// # use anima_engine::math::Bezier;
    /// # use anima_engine::math::Vector;
    /// let b = Bezier::new_sqr(
    ///     Vector::new(0.0, 0.0, 0.0),
    ///     Vector::new(1.0, 1.0, 0.0),
    ///     Vector::new(2.0, 0.0, 0.0)
    /// );
    /// ```
    pub fn new_sqr(v1: Vector, v2: Vector, v3: Vector) -> Bezier {
        Bezier {
            v1: v1,
            v2: v2,
            v3: v3,
            v4: None
        }
    }

    /// Creates a cubic Bézier from `v1` to `v4` curving towards `v2` and `v3`.
    ///
    /// # Examples
    ///
    /// ```
    /// # use anima_engine::math::Bezier;
    /// # use anima_engine::math::Vector;
    /// let b = Bezier::new_cub(
    ///     Vector::new(0.0, 0.0, 0.0),
    ///     Vector::new(0.0, 1.0, 0.0),
    ///     Vector::new(1.0, 1.0, 0.0),
    ///     Vector::new(1.0, 0.0, 0.0)
    /// );
    /// ```
    pub fn new_cub(v1: Vector, v2: Vector, v3: Vector, v4: Vector) -> Bezier {
        Bezier {
            v1: v1,
            v2: v2,
            v3: v3,
            v4: Some(v4)
        }
    }

    /// Computes the vector on a Bézier curve correspoding to a `ratio` (between `0.0` and `1.0`).
    ///
    /// # Examples
    ///
    /// ```
    /// # use anima_engine::math::Bezier;
    /// # use anima_engine::math::Vector;
    /// let b = Bezier::new_sqr(
    ///     Vector::new(1.0, 0.0, 0.0),
    ///     Vector::new(1.0, 1.0, 0.0),
    ///     Vector::new(0.0, 1.0, 0.0)
    /// );
    ///
    /// assert_eq!(b.interpolate(0.5), Vector::new(0.75, 0.75, 0.0));
    /// ```
    ///
    /// ```
    /// # use anima_engine::math::Bezier;
    /// # use anima_engine::math::Vector;
    /// let b = Bezier::new_cub(
    ///     Vector::new(0.0, 0.0, 0.0),
    ///     Vector::new(0.0, 1.0, 0.0),
    ///     Vector::new(1.0, 1.0, 0.0),
    ///     Vector::new(1.0, 0.0, 0.0)
    /// );
    ///
    /// assert_eq!(b.interpolate(0.5), Vector::new(0.5, 0.75, 0.0));
    /// ```
    pub fn interpolate(&self, ratio: f32) -> Vector {
        match self.v4 {
            Some(v4) => {
                self.v1 * (1.0 - ratio).powi(3) +
                self.v2 * 3.0 * (1.0 - ratio).powi(2) * ratio +
                self.v3 * 3.0 * (1.0 - ratio) * ratio.powi(2) +
                v4 * ratio.powi(3)
            },
            None => {
                self.v1 * (1.0 - ratio).powi(2) +
                self.v2 * 2.0 * (1.0 - ratio) * ratio +
                self.v3 * ratio.powi(2)
            }
        }
    }

    /// Computes the approximated length of a Bézier curve by summing the distances between `steps`
    /// uniformly distrubuted, consecutive points.
    ///
    /// # Examples
    ///
    /// ```
    /// # use anima_engine::math::Bezier;
    /// # use anima_engine::math::Vector;
    /// # use std::f32::consts;
    /// // approximation of radius 1.0 circle arc
    /// let b = Bezier::new_cub(
    ///     Vector::new(0.0, 0.0, 0.0),
    ///     Vector::new(0.0, 0.55228, 0.0),
    ///     Vector::new(0.44772, 1.0, 0.0),
    ///     Vector::new(1.0, 1.0, 0.0)
    /// );
    ///
    /// const EPSILON: f32 = 0.001;
    ///
    /// assert!((b.len(20) - consts::PI / 2.0).abs() < EPSILON);
    /// ```
    pub fn len(&self, steps: i32) -> f32 {
        let (length, _) = (1..steps + 1).fold((0.0, self.v1), |(l, v), i| {
            let n = self.interpolate((i as f32) / (steps as f32));

            (l + v.dist(n), n)
        });

        length
    }
}

use mrusty::*;

mrclass!(Bezier, {
    def!("initialize", |mruby; args| {
        match args.len() {
            3 => {
                Bezier::new_sqr(
                    (*args[0].to_obj::<Vector>().unwrap()).clone(),
                    (*args[1].to_obj::<Vector>().unwrap()).clone(),
                    (*args[2].to_obj::<Vector>().unwrap()).clone()
                )
            }
            4 => {
                Bezier::new_cub(
                    (*args[0].to_obj::<Vector>().unwrap()).clone(),
                    (*args[1].to_obj::<Vector>().unwrap()).clone(),
                    (*args[2].to_obj::<Vector>().unwrap()).clone(),
                    (*args[3].to_obj::<Vector>().unwrap()).clone()
                )
            }
            _ => {
                return mruby.raise("ArgumentError", "wrong number of arguments")
            }
        }
    });

    def!("interpolate", |mruby, slf: Bezier, ratio: f64| {
        mruby.obj(slf.interpolate(ratio as f32))
    });

    def!("length", |mruby, slf: Bezier; args| {
        match args.len() {
            0 => mruby.float(slf.len(20) as f64),
            1 => mruby.float(slf.len(args[0].to_i32().unwrap()) as f64),
            _ => mruby.raise("ArgumentError", "wrong number of arguments")
        }
    });
});

/// A `struct` useful for creating a path of Bézier curves.
#[derive(Clone, Debug, PartialEq)]
pub struct BezierPath {
    /// `Vec<Bezier>` of curves forming the path
    pub curves: Vec<Bezier>,
    /// `Vec<f32>` containing the lengths of the `Bezier` curves with the same indices;
    /// (normalized so that they add up to `1.0`)
    pub lengths: Vec<f32>
}

impl BezierPath {
    /// Creates a Bézier path using `Bezier` curves. Curves must be connected.
    ///
    /// # Examples
    ///
    /// ```
    /// # use anima_engine::math::BezierPath;
    /// # use anima_engine::math::Bezier;
    /// # use anima_engine::math::Vector;
    /// let p = BezierPath::new(vec!(Bezier::new_sqr(
    ///     Vector::new(0.0, 0.0, 0.0),
    ///     Vector::new(1.0, 0.0, 0.0),
    ///     Vector::new(2.0, 0.0, 0.0)
    /// )));
    ///
    /// assert_eq!(p, BezierPath {
    ///     curves: vec!(Bezier::new_sqr(
    ///         Vector::new(0.0, 0.0, 0.0),
    ///         Vector::new(1.0, 0.0, 0.0),
    ///         Vector::new(2.0, 0.0, 0.0)
    ///     )),
    ///     lengths: vec!(1.0)
    /// });
    /// ```
    pub fn new(curves: Vec<Bezier>) -> BezierPath {
        const STEPS: i32 = 20;

        let lengths: Vec<f32> = curves.iter().map(|c| c.len(STEPS)).collect();
        let sum = lengths.iter().fold(0.0, |s, l| s + l);

        BezierPath {
            curves: curves,
            lengths: lengths.iter().map(|l| l / sum).collect()
        }
    }

    /// Computes the vector on a Bézier path correspoding to a `ratio` (between `0.0` and `1.0`).
    ///
    /// # Examples
    ///
    /// ```
    /// # use anima_engine::math::BezierPath;
    /// # use anima_engine::math::Bezier;
    /// # use anima_engine::math::Vector;
    /// let b1 = Bezier::new_sqr(
    ///     Vector::new(0.0, 0.0, 0.0),
    ///     Vector::new(1.0, 1.0, 0.0),
    ///     Vector::new(2.0, 2.0, 0.0)
    /// );
    /// let b2 = Bezier::new_sqr(
    ///     Vector::new(2.0, 2.0, 0.0),
    ///     Vector::new(6.0, 6.0, 0.0),
    ///     Vector::new(10.0, 10.0, 0.0)
    /// );
    /// let p = BezierPath::new(vec![b1, b2]);
    ///
    /// assert_eq!(p.interpolate(0.5), Vector::new(5.0, 5.0, 0.0));
    /// assert_eq!(p.interpolate(1.2), Vector::new(12.0, 12.0, 0.0));
    /// ```
    pub fn interpolate(&self, ratio: f32) -> Vector {
        let mut sum = 0.0;

        let curve_length = self.curves.iter().zip(self.lengths.iter()).find(|&(_, l)| {
            if ratio <= sum + l {
                true
            } else {
                sum = sum + l;

                false
            }
        });

        let (curve, ratio) = match curve_length {
            Some((curve, length)) => (curve, (ratio - sum) / length),
            None                  => {
                let curve = self.curves.last();
                let length = self.lengths.last();

                match (curve, length) {
                    (Some(curve), Some(length)) => {
                        (curve, (ratio - sum + length) / length)
                    },
                    _ => panic!("Cannot interpolate an empty path.")
                }
            }
        };

        curve.interpolate(ratio)
    }

    /// Computes the approximated length of a Bézier path by summing the distances between `steps`
    /// uniformly distrubuted, consecutive points per curve.
    ///
    /// # Examples
    ///
    /// ```
    /// # use anima_engine::math::BezierPath;
    /// # use anima_engine::math::Bezier;
    /// # use anima_engine::math::Vector;
    /// let b1 = Bezier::new_sqr(
    ///     Vector::new(0.0, 0.0, 0.0),
    ///     Vector::new(1.0, 1.0, 0.0),
    ///     Vector::new(2.0, 2.0, 0.0)
    /// );
    /// let b2 = Bezier::new_sqr(
    ///     Vector::new(2.0, 2.0, 0.0),
    ///     Vector::new(6.0, 6.0, 0.0),
    ///     Vector::new(10.0, 10.0, 0.0)
    /// );
    /// let p = BezierPath::new(vec![b1, b2]);
    ///
    /// const EPSILON: f32 = 0.001;
    ///
    /// assert!((p.len(20) - 14.142137).abs() < EPSILON);
    /// ```
    pub fn len(&self, steps: i32) -> f32 {
        self.curves.iter().map(|curve| curve.len(steps)).fold(0.0, |s, l| s + l)
    }
}

mrclass!(BezierPath, {
    def!("initialize", |mruby, curves: Vec| {
        let mut beziers = Vec::with_capacity(curves.len());

        for curve in curves {
            let curve = match curve.type_name() {
                "Bezier" => (*curve.to_obj::<Bezier>().unwrap()).clone(),
                "Array"  => {
                    fn to_vector(mruby: MrubyType, value: &Value) -> Result<Vector, Value> {
                        match value.type_name() {
                            "Vector" => Ok((*value.to_obj::<Vector>().unwrap()).clone()),
                            "Array"  => {
                                let array = value.to_vec().unwrap();

                                let x = array[0].to_f64().unwrap();
                                let y = array[1].to_f64().unwrap();
                                let z = array[2].to_f64().unwrap();

                                Ok(Vector::new(x as f32, y as f32, z as f32))
                            }
                            _ => Err(mruby.raise("ArgumentError",
                                                 "Array should contain Vector or Array"))
                        }
                    }

                    let array = curve.to_vec().unwrap();

                    match array.len() {
                        3 => {
                            Bezier::new_sqr(
                                match to_vector(mruby.clone(), &array[0]) {
                                    Ok(v)  => v,
                                    Err(v) => {
                                        return v;
                                    }
                                },
                                match to_vector(mruby.clone(), &array[1]) {
                                    Ok(v)  => v,
                                    Err(v) => {
                                        return v;
                                    }
                                },
                                match to_vector(mruby.clone(), &array[2]) {
                                    Ok(v)  => v,
                                    Err(v) => {
                                        return v;
                                    }
                                }
                            )
                        }
                        4 => {
                            Bezier::new_cub(
                                match to_vector(mruby.clone(), &array[0]) {
                                    Ok(v)  => v,
                                    Err(v) => {
                                        return v;
                                    }
                                },
                                match to_vector(mruby.clone(), &array[1]) {
                                    Ok(v)  => v,
                                    Err(v) => {
                                        return v;
                                    }
                                },
                                match to_vector(mruby.clone(), &array[2]) {
                                    Ok(v)  => v,
                                    Err(v) => {
                                        return v;
                                    }
                                },
                                match to_vector(mruby.clone(), &array[3]) {
                                    Ok(v)  => v,
                                    Err(v) => {
                                        return v;
                                    }
                                }
                            )
                        }
                        _ => {
                            return mruby.raise("ArgumentError", "Array should contain 3-4 items")
                        }
                    }
                },
                _ => {
                    return mruby.raise("ArgumentError", "pass Array of Bezier or Array")
                }
            };

            beziers.push(curve);
        }

        BezierPath::new(beziers)
    });

    def!("interpolate", |mruby, slf: BezierPath, ratio: f64| {
        mruby.obj(slf.interpolate(ratio as f32))
    });

    def!("length", |mruby, slf: BezierPath; args| {
        match args.len() {
            0 => mruby.float(slf.len(20) as f64),
            1 => mruby.float(slf.len(args[0].to_i32().unwrap()) as f64),
            _ => mruby.raise("ArgumentError", "wrong number of arguments")
        }
    });
});

#[cfg(test)]
mod test_bezier {
    use mrusty::*;

    use super::Bezier;
    use super::super::Vector;

    describe!(Bezier, (Vector), "
      context 'when square arc' do
        subject { Bezier.new Vector.forward, Vector.uniform(1.0), Vector.left }

        it 'interpolates Vectors on #interpolate' do
          expect(subject.interpolate 0.5).to eql Vector.new(0.75, 0.5, 0.75)
        end

        it 'returns approximated length on #length' do
          expect(subject.length).to be_within(0.000001).of 1.950975
        end

        it 'returns approximated length on #length with custom number of steps' do
          expect(subject.length 10).to be_within(0.01).of 1.950975
        end
      end
    ");
}

#[cfg(test)]
mod test_bezier_path {
    use mrusty::*;

    use super::Bezier;
    use super::BezierPath;
    use super::super::Vector;

    describe!(BezierPath, (Bezier, Vector), "
      context 'when initialized in all possible ways' do
        subject do
          BezierPath.new [
            [
              [1.0, 1.0, 1.0],
              [1.0, 1.0, 1.0],
              Vector.uniform(1.0)
            ],
            Bezier.new(
                Vector.uniform(1.0),
                Vector.uniform(1.0),
                Vector.uniform(1.0)
            )
          ]
        end

        it 'returns approximated length on #length' do
          expect(subject.length).to be_within(0.00001).of 0.0
        end
      end

      context 'when formed of two curves' do
        subject do
          BezierPath.new [
            [
              [0.0, 0.0, 0.0],
              [1.0, 1.0, 0.0],
              [2.0, 2.0, 0.0]
            ],
            [
              [2.0, 2.0, 0.0],
              [6.0, 6.0, 0.0],
              [10.0, 10.0, 0.0]
            ]
          ]
        end

        it 'interpolates Vectors on #interpolate' do
          interpolated = subject.interpolate 0.5

          expect(interpolated.x).to be_within(0.000001).of 5.0
          expect(interpolated.y).to be_within(0.000001).of 5.0
          expect(interpolated.z).to be_within(0.000001).of 0.0
        end
      end
    ");
}