euclid/
homogen.rs

1// Copyright 2018 The Servo Project Developers. See the COPYRIGHT
2// file at the top-level directory of this distribution.
3//
4// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
5// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
6// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
7// option. This file may not be copied, modified, or distributed
8// except according to those terms.
9
10use point::{TypedPoint2D, TypedPoint3D};
11use vector::{TypedVector2D, TypedVector3D};
12
13use num::{One, Zero};
14
15use core::fmt;
16use core::marker::PhantomData;
17use core::ops::Div;
18
19
20/// Homogeneous vector in 3D space.
21#[derive(EuclidMatrix)]
22#[repr(C)]
23pub struct HomogeneousVector<T, U> {
24    pub x: T,
25    pub y: T,
26    pub z: T,
27    pub w: T,
28    #[doc(hidden)]
29    pub _unit: PhantomData<U>,
30}
31
32
33impl<T, U> HomogeneousVector<T, U> {
34    /// Constructor taking scalar values directly.
35    #[inline]
36    pub fn new(x: T, y: T, z: T, w: T) -> Self {
37        HomogeneousVector { x, y, z, w, _unit: PhantomData }
38    }
39}
40
41
42impl<T: Copy + Div<T, Output=T> + Zero + PartialOrd, U> HomogeneousVector<T, U> {
43    /// Convert into Cartesian 2D point.
44    ///
45    /// Returns None if the point is on or behind the W=0 hemisphere.
46    #[inline]
47    pub fn to_point2d(&self) -> Option<TypedPoint2D<T, U>> {
48        if self.w > T::zero() {
49            Some(TypedPoint2D::new(self.x / self.w, self.y / self.w))
50        } else {
51            None
52        }
53    }
54
55    /// Convert into Cartesian 3D point.
56    ///
57    /// Returns None if the point is on or behind the W=0 hemisphere.
58    #[inline]
59    pub fn to_point3d(&self) -> Option<TypedPoint3D<T, U>> {
60        if self.w > T::zero() {
61            Some(TypedPoint3D::new(self.x / self.w, self.y / self.w, self.z / self.w))
62        } else {
63            None
64        }
65    }
66}
67
68impl<T: Zero, U> From<TypedVector2D<T, U>> for HomogeneousVector<T, U> {
69    #[inline]
70    fn from(v: TypedVector2D<T, U>) -> Self {
71        HomogeneousVector::new(v.x, v.y, T::zero(), T::zero())
72    }
73}
74
75impl<T: Zero, U> From<TypedVector3D<T, U>> for HomogeneousVector<T, U> {
76    #[inline]
77    fn from(v: TypedVector3D<T, U>) -> Self {
78        HomogeneousVector::new(v.x, v.y, v.z, T::zero())
79    }
80}
81
82impl<T: Zero + One, U> From<TypedPoint2D<T, U>> for HomogeneousVector<T, U> {
83    #[inline]
84    fn from(p: TypedPoint2D<T, U>) -> Self {
85        HomogeneousVector::new(p.x, p.y, T::zero(), T::one())
86    }
87}
88
89impl<T: One, U> From<TypedPoint3D<T, U>> for HomogeneousVector<T, U> {
90    #[inline]
91    fn from(p: TypedPoint3D<T, U>) -> Self {
92        HomogeneousVector::new(p.x, p.y, p.z, T::one())
93    }
94}
95
96impl<T: fmt::Debug, U> fmt::Debug for HomogeneousVector<T, U> {
97    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
98        write!(f, "({:?},{:?},{:?},{:?})", self.x, self.y, self.z, self.w)
99    }
100}
101
102impl<T: fmt::Display, U> fmt::Display for HomogeneousVector<T, U> {
103    fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
104        write!(formatter, "({},{},{},{})", self.x, self.y, self.z, self.w)
105    }
106}
107
108
109#[cfg(test)]
110mod homogeneous {
111    use super::HomogeneousVector;
112    use point::{Point2D, Point3D};
113
114    #[test]
115    fn roundtrip() {
116        assert_eq!(Some(Point2D::new(1.0, 2.0)), HomogeneousVector::from(Point2D::new(1.0, 2.0)).to_point2d());
117        assert_eq!(Some(Point3D::new(1.0, -2.0, 0.1)), HomogeneousVector::from(Point3D::new(1.0, -2.0, 0.1)).to_point3d());
118    }
119
120    #[test]
121    fn negative() {
122        assert_eq!(None, HomogeneousVector::<f32, ()>::new(1.0, 2.0, 3.0, 0.0).to_point2d());
123        assert_eq!(None, HomogeneousVector::<f32, ()>::new(1.0, -2.0, -3.0, -2.0).to_point3d());
124    }
125}