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
// Copyright (c) 2017, Marty Mills <daggerbot@gmail.com>
// This software is available under the terms of the zlib license.
// See COPYING.md for more information.

use std::marker::PhantomData;

use dnum::Zero;
use dvec::{Vec2};

/// Image boundaries trait.
pub trait Bounds {
    type Index;
    fn in_bounds (&self, index: Self::Index) -> bool;
}

/// Limitless boundaries.
pub struct Boundless<T> {
    _phantom: PhantomData<T>,
}

impl<T> Boundless<T> {
    pub fn new () -> Boundless<T> {
        Boundless { _phantom: PhantomData }
    }
}

impl<T> Bounds for Boundless<T> {
    type Index = T;

    #[inline(always)]
    fn in_bounds (&self, _: T) -> bool { true }
}

/// 2-dimensional sized boundaries.
#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)]
pub struct Size2<T> {
    pub width: T,
    pub height: T,
}

impl<T> Size2<T> {
    pub fn as_vec (self) -> Vec2<T>
        where T: Copy
    {
        Vec2 { x: self.width, y: self.height }
    }

    pub fn into_vec (self) -> Vec2<T> {
        Vec2 { x: self.width, y: self.height }
    }

    pub fn new (width: T, height: T) -> Size2<T> {
        Size2 { width, height }
    }
}

impl<T> Bounds for Size2<T>
    where T: Ord + Zero
{
    type Index = Vec2<T>;

    fn in_bounds (&self, pt: Vec2<T>) -> bool {
        let zero = T::zero();
        pt.x >= zero && pt.x < self.width && pt.y >= zero && pt.y < self.height
    }
}

impl<T> From<(T, T)> for Size2<T> {
    fn from (t: (T, T)) -> Size2<T> {
        Size2 { width: t.0, height: t.1 }
    }
}

impl<T> From<Vec2<T>> for Size2<T> {
    fn from (v: Vec2<T>) -> Size2<T> {
        Size2 { width: v.x, height: v.y }
    }
}