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
//! Mathematical point on the 2D (x, y) plane.

use crate::point2f::Point2f;
use crate::point2i::Point2i;

#[cfg(all(windows, feature = "d2d"))]
use winapi::um::dcommon::D2D_POINT_2U;

/// Mathematical point on the 2D (x, y) plane.
#[derive(Copy, Clone, Debug, Default, PartialEq, Eq)]
#[cfg_attr(feature = "serde_derive", derive(Serialize, Deserialize))]
#[repr(C)]
pub struct Point2u {
    /// Horizontal component
    pub x: u32,
    /// Vertical component
    pub y: u32,
}

impl Point2u {
    /// Mathematical origin point
    pub const ORIGIN: Point2u = Point2u { x: 0, y: 0 };

    /// Construct a point from its components
    #[inline]
    pub fn new(x: u32, y: u32) -> Point2u {
        Point2u { x, y }
    }

    /// Convert this value to a floating point
    #[inline]
    pub fn to_f32(self) -> Point2f {
        Point2f {
            x: self.x as f32,
            y: self.y as f32,
        }
    }

    /// Convert this value to an unsigned point. It is the caller's duty
    /// to ensure the values are not negative to avoid casting underflow.
    #[inline]
    pub fn to_i32(self) -> Point2i {
        Point2i {
            x: self.x as i32,
            y: self.y as i32,
        }
    }
}

impl From<(u32, u32)> for Point2u {
    #[inline]
    fn from((x, y): (u32, u32)) -> Point2u {
        Point2u { x, y }
    }
}

impl From<[u32; 2]> for Point2u {
    #[inline]
    fn from(p: [u32; 2]) -> Point2u {
        Point2u { x: p[0], y: p[0] }
    }
}

impl From<Point2u> for [u32; 2] {
    #[inline]
    fn from(p: Point2u) -> [u32; 2] {
        [p.x, p.y]
    }
}

#[cfg(all(windows, feature = "d2d"))]
impl From<Point2u> for D2D_POINT_2U {
    #[inline]
    fn from(point: Point2u) -> D2D_POINT_2U {
        D2D_POINT_2U {
            x: point.x,
            y: point.y,
        }
    }
}

#[cfg(all(windows, feature = "d2d"))]
impl From<D2D_POINT_2U> for Point2u {
    #[inline]
    fn from(point: D2D_POINT_2U) -> Point2u {
        Point2u {
            x: point.x,
            y: point.y,
        }
    }
}

#[cfg(feature = "mint")]
impl From<Point2u> for mint::Point2<u32> {
    #[inline]
    fn from(p: Point2u) -> mint::Point2<u32> {
        mint::Point2 { x: p.x, y: p.y }
    }
}

#[cfg(feature = "mint")]
impl From<mint::Point2<u32>> for Point2u {
    #[inline]
    fn from(p: mint::Point2<u32>) -> Point2u {
        Point2u { x: p.x, y: p.y }
    }
}

#[cfg(all(test, windows, feature = "d2d"))]
#[test]
fn pt2u_d2d_bin_compat() {
    use std::mem::size_of_val;

    fn ptr_eq<T>(a: &T, b: &T) -> bool {
        (a as *const T) == (b as *const T)
    }

    let pt = Point2u::ORIGIN;
    let d2d = unsafe { &*((&pt) as *const _ as *const D2D_POINT_2U) };

    assert!(ptr_eq(&pt.x, &d2d.x));
    assert!(ptr_eq(&pt.y, &d2d.y));
    assert_eq!(size_of_val(&pt), size_of_val(d2d));
}