logo
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
use crate::entity::borrow::Reborrow;
use crate::entity::Lifetime;

pub type Data<M> = <M as Parametric>::Data;

/// Graph data.
///
/// Specifies the types used to represent data in vertices, arcs, edges, and
/// faces in a [`MeshGraph`]. Arbitrary types can be used, including the unit
/// type `()` for no data at all.
///
/// Geometric operations depend on understanding the positional data in vertices
/// exposed by the [`AsPosition`] trait. If the `Vertex` type implements
/// [`AsPosition`], then geometric operations supported by the `Position` type
/// are exposed by graph APIs.
///
/// # Examples
///
/// ```rust
/// # extern crate decorum;
/// # extern crate nalgebra;
/// # extern crate num;
/// # extern crate plexus;
/// #
/// use decorum::R64;
/// use nalgebra::{Point3, Vector4};
/// use num::Zero;
/// use plexus::geometry::{AsPosition, IntoGeometry};
/// use plexus::graph::{GraphData, MeshGraph};
/// use plexus::prelude::*;
/// use plexus::primitive::generate::Position;
/// use plexus::primitive::sphere::UvSphere;
///
/// #[derive(Clone, Copy, Eq, Hash, PartialEq)]
/// pub struct Vertex {
///     pub position: Point3<R64>,
///     pub color: Vector4<R64>,
/// }
///
/// impl GraphData for Vertex {
///     type Vertex = Self;
///     type Arc = ();
///     type Edge = ();
///     type Face = ();
/// }
///
/// impl AsPosition for Vertex {
///     type Position = Point3<R64>;
///
///     fn as_position(&self) -> &Self::Position {
///         &self.position
///     }
/// }
///
/// // Create a mesh from a uv-sphere.
/// let mut graph: MeshGraph<Vertex> = UvSphere::new(8, 8)
///     .polygons::<Position<Point3<R64>>>()
///     .map_vertices(|position| Vertex {
///         position,
///         color: Zero::zero(),
///     })
///     .collect();
/// ```
///
/// [`AsPosition`]: crate::geometry::AsPosition
/// [`MeshGraph`]: crate::graph::MeshGraph
pub trait GraphData: Lifetime + Sized {
    type Vertex: Clone + Lifetime;
    type Arc: Clone + Default + Lifetime;
    type Edge: Clone + Default + Lifetime;
    type Face: Clone + Default + Lifetime;
}

impl GraphData for () {
    type Vertex = ();
    type Arc = ();
    type Edge = ();
    type Face = ();
}

impl<T> GraphData for (T, T)
where
    T: Clone + Lifetime,
{
    type Vertex = Self;
    type Arc = ();
    type Edge = ();
    type Face = ();
}

impl<T> GraphData for (T, T, T)
where
    T: Clone + Lifetime,
{
    type Vertex = Self;
    type Arc = ();
    type Edge = ();
    type Face = ();
}

impl<T> GraphData for [T; 2]
where
    T: Clone + Lifetime,
{
    type Vertex = Self;
    type Arc = ();
    type Edge = ();
    type Face = ();
}

impl<T> GraphData for [T; 3]
where
    T: Clone + Lifetime,
{
    type Vertex = Self;
    type Arc = ();
    type Edge = ();
    type Face = ();
}

pub trait Parametric {
    type Data: GraphData;
}

impl<B> Parametric for B
where
    B: Reborrow,
    B::Target: Parametric,
{
    type Data = <B::Target as Parametric>::Data;
}