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
#![cfg(feature = "geometry-cgmath")]

use cgmath::{self, Point2, Point3, Vector2, Vector3};
use decorum::{Finite, NotNan, Ordered, Primitive};
use num::{Float, NumCast, ToPrimitive};

use crate::graph::GraphGeometry;
use crate::FromGeometry;

impl<T, U> FromGeometry<(U, U)> for Vector2<T>
where
    T: NumCast,
    U: ToPrimitive,
{
    fn from_geometry(other: (U, U)) -> Self {
        Vector2::new(T::from(other.0).unwrap(), T::from(other.1).unwrap())
    }
}

impl<T, U> FromGeometry<Vector2<T>> for (U, U)
where
    T: ToPrimitive,
    U: NumCast,
{
    fn from_geometry(other: Vector2<T>) -> Self {
        (U::from(other.x).unwrap(), U::from(other.y).unwrap())
    }
}

impl<T, U> FromGeometry<(U, U, U)> for Vector3<T>
where
    T: NumCast,
    U: ToPrimitive,
{
    fn from_geometry(other: (U, U, U)) -> Self {
        Vector3::new(
            T::from(other.0).unwrap(),
            T::from(other.1).unwrap(),
            T::from(other.2).unwrap(),
        )
    }
}

impl<T, U> FromGeometry<Vector3<T>> for (U, U, U)
where
    T: ToPrimitive,
    U: NumCast,
{
    fn from_geometry(other: Vector3<T>) -> Self {
        (
            U::from(other.x).unwrap(),
            U::from(other.y).unwrap(),
            U::from(other.z).unwrap(),
        )
    }
}

impl<T, U> FromGeometry<(U, U)> for Point2<T>
where
    T: NumCast,
    U: ToPrimitive,
{
    fn from_geometry(other: (U, U)) -> Self {
        Point2::new(T::from(other.0).unwrap(), T::from(other.1).unwrap())
    }
}

impl<T, U> FromGeometry<Point2<T>> for (U, U)
where
    T: ToPrimitive,
    U: NumCast,
{
    fn from_geometry(other: Point2<T>) -> Self {
        (U::from(other.x).unwrap(), U::from(other.y).unwrap())
    }
}

impl<T, U> FromGeometry<(U, U, U)> for Point3<T>
where
    T: NumCast,
    U: ToPrimitive,
{
    fn from_geometry(other: (U, U, U)) -> Self {
        Point3::new(
            T::from(other.0).unwrap(),
            T::from(other.1).unwrap(),
            T::from(other.2).unwrap(),
        )
    }
}

impl<T, U> FromGeometry<Point3<T>> for (U, U, U)
where
    T: ToPrimitive,
    U: NumCast,
{
    fn from_geometry(other: Point3<T>) -> Self {
        (
            U::from(other.x).unwrap(),
            U::from(other.y).unwrap(),
            U::from(other.z).unwrap(),
        )
    }
}

impl<T> GraphGeometry for Point2<T>
where
    Self: Copy,
{
    type Vertex = Self;
    type Arc = ();
    type Edge = ();
    type Face = ();
}

impl<T> GraphGeometry for Point3<T>
where
    Self: Copy,
{
    type Vertex = Self;
    type Arc = ();
    type Edge = ();
    type Face = ();
}

macro_rules! impl_from_geometry_ordered {
    (geometry => $g:ident,proxy => $p:ident) => {
        impl<T> FromGeometry<$g<$p<T>>> for $g<T>
        where
            T: Float + Primitive,
        {
            fn from_geometry(other: $g<$p<T>>) -> Self {
                other.map(|value| value.into_inner())
            }
        }

        impl<T> FromGeometry<$g<T>> for $g<$p<T>>
        where
            T: Float + Primitive,
        {
            fn from_geometry(other: $g<T>) -> Self {
                other.map($p::<T>::from_inner)
            }
        }
    };
}
impl_from_geometry_ordered!(geometry => Vector2, proxy => Finite);
impl_from_geometry_ordered!(geometry => Vector2, proxy => NotNan);
impl_from_geometry_ordered!(geometry => Vector2, proxy => Ordered);
impl_from_geometry_ordered!(geometry => Vector3, proxy => Finite);
impl_from_geometry_ordered!(geometry => Vector3, proxy => NotNan);
impl_from_geometry_ordered!(geometry => Vector3, proxy => Ordered);
impl_from_geometry_ordered!(geometry => Point2, proxy => Finite);
impl_from_geometry_ordered!(geometry => Point2, proxy => NotNan);
impl_from_geometry_ordered!(geometry => Point2, proxy => Ordered);
impl_from_geometry_ordered!(geometry => Point3, proxy => Finite);
impl_from_geometry_ordered!(geometry => Point3, proxy => NotNan);
impl_from_geometry_ordered!(geometry => Point3, proxy => Ordered);