use core::cell::{Ref, RefMut};
use core::cmp;
use core::convert::{TryFrom, TryInto};
use core::ops::{self, Deref, DerefMut};
use nannou_core::geom;
pub mod channel;
pub mod vertex;
pub use self::channel::{Channel, ChannelMut};
pub trait GetVertex<I> {
type Vertex;
fn get_vertex(&self, index: I) -> Option<Self::Vertex>;
}
pub trait Points {
type Point;
type Points: Channel<Element = Self::Point>;
fn points(&self) -> &Self::Points;
}
pub trait Indices {
type Index;
type Indices: Channel<Element = Self::Index>;
fn indices(&self) -> &Self::Indices;
}
pub trait Colors {
type Color;
type Colors: Channel<Element = Self::Color>;
fn colors(&self) -> &Self::Colors;
}
pub trait TexCoords {
type TexCoord;
type TexCoords: Channel<Element = Self::TexCoord>;
fn tex_coords(&self) -> &Self::TexCoords;
}
pub trait Normals {
type Normal;
type Normals: Channel<Element = Self::Normal>;
fn normals(&self) -> &Self::Normals;
}
pub trait PushVertex<V> {
fn push_vertex(&mut self, vertex: V);
}
pub trait PushIndex {
type Index;
fn push_index(&mut self, index: Self::Index);
fn extend_indices<I>(&mut self, indices: I)
where
I: IntoIterator<Item = Self::Index>,
{
for i in indices {
self.push_index(i);
}
}
}
pub trait ClearIndices {
fn clear_indices(&mut self);
}
pub trait ClearVertices {
fn clear_vertices(&mut self);
}
pub trait Clear: ClearIndices + ClearVertices {
fn clear(&mut self) {
self.clear_indices();
self.clear_vertices();
}
}
pub trait ExtendFromSlice<'a> {
type Slice: 'a;
fn extend_from_slice(&mut self, slice: Self::Slice);
}
#[derive(Copy, Clone, Debug, PartialEq)]
pub struct MeshPoints<P> {
points: P,
}
#[derive(Copy, Clone, Debug, PartialEq)]
pub struct WithIndices<M, I> {
mesh: M,
indices: I,
}
#[derive(Copy, Clone, Debug, PartialEq)]
pub struct WithColors<M, C> {
mesh: M,
colors: C,
}
#[derive(Copy, Clone, Debug, PartialEq)]
pub struct WithTexCoords<M, T> {
mesh: M,
tex_coords: T,
}
#[derive(Copy, Clone, Debug, PartialEq)]
pub struct WithNormals<M, N> {
mesh: M,
normals: N,
}
impl<'a, M, I> GetVertex<I> for &'a M
where
M: GetVertex<I>,
{
type Vertex = M::Vertex;
fn get_vertex(&self, index: I) -> Option<Self::Vertex> {
(**self).get_vertex(index)
}
}
impl<'a, M, I> GetVertex<I> for &'a mut M
where
M: GetVertex<I>,
{
type Vertex = M::Vertex;
fn get_vertex(&self, index: I) -> Option<Self::Vertex> {
(**self).get_vertex(index)
}
}
impl<'a, M, I> GetVertex<I> for Ref<'a, M>
where
M: GetVertex<I>,
{
type Vertex = M::Vertex;
fn get_vertex(&self, index: I) -> Option<Self::Vertex> {
(**self).get_vertex(index)
}
}
impl<'a, M, I> GetVertex<I> for RefMut<'a, M>
where
M: GetVertex<I>,
{
type Vertex = M::Vertex;
fn get_vertex(&self, index: I) -> Option<Self::Vertex> {
(**self).get_vertex(index)
}
}
impl<P, I> GetVertex<I> for MeshPoints<P>
where
P: Channel,
P::Element: Clone,
I: TryInto<usize>,
{
type Vertex = P::Element;
fn get_vertex(&self, index: I) -> Option<Self::Vertex> {
let index = index
.try_into()
.unwrap_or_else(|_err| panic!("index out of range of valid `usize` values"));
self.points.channel().get(index).map(|p| p.clone())
}
}
impl<M, I, Ix> GetVertex<Ix> for WithIndices<M, I>
where
M: GetVertex<Ix>,
{
type Vertex = M::Vertex;
fn get_vertex(&self, index: Ix) -> Option<Self::Vertex> {
self.mesh.get_vertex(index)
}
}
impl<M, C, I> GetVertex<I> for WithColors<M, C>
where
M: GetVertex<I>,
C: Channel,
C::Element: Clone,
I: Copy + TryInto<usize>,
{
type Vertex = vertex::WithColor<M::Vertex, C::Element>;
fn get_vertex(&self, index: I) -> Option<Self::Vertex> {
self.mesh.get_vertex(index).and_then(|vertex| {
let index: usize = index
.try_into()
.unwrap_or_else(|_err| panic!("index out of range of valid usize values"));
self.colors.channel().get(index).map(|color: &C::Element| {
let color = color.clone();
vertex::WithColor { vertex, color }
})
})
}
}
impl<M, T, I> GetVertex<I> for WithTexCoords<M, T>
where
M: GetVertex<I>,
T: Channel,
T::Element: Clone,
I: Copy + TryInto<usize>,
{
type Vertex = vertex::WithTexCoords<M::Vertex, T::Element>;
fn get_vertex(&self, index: I) -> Option<Self::Vertex> {
self.mesh.get_vertex(index).and_then(|vertex| {
let index: usize = index
.try_into()
.unwrap_or_else(|_err| panic!("index out of range of valid usize values"));
self.tex_coords.channel().get(index).map(|tex_coords| {
let tex_coords = tex_coords.clone();
vertex::WithTexCoords { vertex, tex_coords }
})
})
}
}
impl<M, N, I> GetVertex<I> for WithNormals<M, N>
where
M: GetVertex<I>,
N: Channel,
N::Element: Clone,
I: Copy + TryInto<usize>,
{
type Vertex = vertex::WithNormal<M::Vertex, N::Element>;
fn get_vertex(&self, index: I) -> Option<Self::Vertex> {
self.mesh.get_vertex(index).and_then(|vertex| {
let index: usize = index
.try_into()
.unwrap_or_else(|_err| panic!("index out of range of valid usize values"));
self.normals.channel().get(index).map(|normal| {
let normal = normal.clone();
vertex::WithNormal { vertex, normal }
})
})
}
}
impl<P> Points for MeshPoints<P>
where
P: Channel,
{
type Point = P::Element;
type Points = P;
fn points(&self) -> &Self::Points {
&self.points
}
}
impl<'a, M> Points for &'a M
where
M: Points,
{
type Point = M::Point;
type Points = M::Points;
fn points(&self) -> &Self::Points {
(**self).points()
}
}
impl<'a, M> Points for &'a mut M
where
M: Points,
{
type Point = M::Point;
type Points = M::Points;
fn points(&self) -> &Self::Points {
(**self).points()
}
}
impl<'a, M> Points for Ref<'a, M>
where
M: Points,
{
type Point = M::Point;
type Points = M::Points;
fn points(&self) -> &Self::Points {
(**self).points()
}
}
impl<'a, M> Points for RefMut<'a, M>
where
M: Points,
{
type Point = M::Point;
type Points = M::Points;
fn points(&self) -> &Self::Points {
(**self).points()
}
}
impl<M, I> Points for WithIndices<M, I>
where
M: Points,
{
type Point = M::Point;
type Points = M::Points;
fn points(&self) -> &Self::Points {
self.mesh.points()
}
}
impl<M, C> Points for WithColors<M, C>
where
M: Points,
{
type Point = M::Point;
type Points = M::Points;
fn points(&self) -> &Self::Points {
self.mesh.points()
}
}
impl<M, T> Points for WithTexCoords<M, T>
where
M: Points,
{
type Point = M::Point;
type Points = M::Points;
fn points(&self) -> &Self::Points {
self.mesh.points()
}
}
impl<M, N> Points for WithNormals<M, N>
where
M: Points,
{
type Point = M::Point;
type Points = M::Points;
fn points(&self) -> &Self::Points {
self.mesh.points()
}
}
impl<M, I> Indices for WithIndices<M, I>
where
I: Channel,
{
type Index = I::Element;
type Indices = I;
fn indices(&self) -> &Self::Indices {
&self.indices
}
}
impl<'a, M> Indices for &'a M
where
M: Indices,
{
type Index = M::Index;
type Indices = M::Indices;
fn indices(&self) -> &Self::Indices {
(**self).indices()
}
}
impl<'a, M> Indices for &'a mut M
where
M: Indices,
{
type Index = M::Index;
type Indices = M::Indices;
fn indices(&self) -> &Self::Indices {
(**self).indices()
}
}
impl<'a, M> Indices for Ref<'a, M>
where
M: Indices,
{
type Index = M::Index;
type Indices = M::Indices;
fn indices(&self) -> &Self::Indices {
(**self).indices()
}
}
impl<'a, M> Indices for RefMut<'a, M>
where
M: Indices,
{
type Index = M::Index;
type Indices = M::Indices;
fn indices(&self) -> &Self::Indices {
(**self).indices()
}
}
impl<M, C> Indices for WithColors<M, C>
where
M: Indices,
{
type Index = M::Index;
type Indices = M::Indices;
fn indices(&self) -> &Self::Indices {
self.mesh.indices()
}
}
impl<M, T> Indices for WithTexCoords<M, T>
where
M: Indices,
{
type Index = M::Index;
type Indices = M::Indices;
fn indices(&self) -> &Self::Indices {
self.mesh.indices()
}
}
impl<M, N> Indices for WithNormals<M, N>
where
M: Indices,
{
type Index = M::Index;
type Indices = M::Indices;
fn indices(&self) -> &Self::Indices {
self.mesh.indices()
}
}
impl<M, C> Colors for WithColors<M, C>
where
C: Channel,
{
type Color = C::Element;
type Colors = C;
fn colors(&self) -> &Self::Colors {
&self.colors
}
}
impl<'a, M> Colors for &'a M
where
M: Colors,
{
type Color = M::Color;
type Colors = M::Colors;
fn colors(&self) -> &Self::Colors {
(**self).colors()
}
}
impl<'a, M> Colors for &'a mut M
where
M: Colors,
{
type Color = M::Color;
type Colors = M::Colors;
fn colors(&self) -> &Self::Colors {
(**self).colors()
}
}
impl<'a, M> Colors for Ref<'a, M>
where
M: Colors,
{
type Color = M::Color;
type Colors = M::Colors;
fn colors(&self) -> &Self::Colors {
(**self).colors()
}
}
impl<'a, M> Colors for RefMut<'a, M>
where
M: Colors,
{
type Color = M::Color;
type Colors = M::Colors;
fn colors(&self) -> &Self::Colors {
(**self).colors()
}
}
impl<M, I> Colors for WithIndices<M, I>
where
M: Colors,
{
type Color = M::Color;
type Colors = M::Colors;
fn colors(&self) -> &Self::Colors {
self.mesh.colors()
}
}
impl<M, T> Colors for WithTexCoords<M, T>
where
M: Colors,
{
type Color = M::Color;
type Colors = M::Colors;
fn colors(&self) -> &Self::Colors {
self.mesh.colors()
}
}
impl<M, N> Colors for WithNormals<M, N>
where
M: Colors,
{
type Color = M::Color;
type Colors = M::Colors;
fn colors(&self) -> &Self::Colors {
self.mesh.colors()
}
}
impl<M, T> TexCoords for WithTexCoords<M, T>
where
T: Channel,
{
type TexCoord = T::Element;
type TexCoords = T;
fn tex_coords(&self) -> &Self::TexCoords {
&self.tex_coords
}
}
impl<'a, M> TexCoords for &'a M
where
M: TexCoords,
{
type TexCoord = M::TexCoord;
type TexCoords = M::TexCoords;
fn tex_coords(&self) -> &Self::TexCoords {
(**self).tex_coords()
}
}
impl<'a, M> TexCoords for &'a mut M
where
M: TexCoords,
{
type TexCoord = M::TexCoord;
type TexCoords = M::TexCoords;
fn tex_coords(&self) -> &Self::TexCoords {
(**self).tex_coords()
}
}
impl<'a, M> TexCoords for Ref<'a, M>
where
M: TexCoords,
{
type TexCoord = M::TexCoord;
type TexCoords = M::TexCoords;
fn tex_coords(&self) -> &Self::TexCoords {
(**self).tex_coords()
}
}
impl<'a, M> TexCoords for RefMut<'a, M>
where
M: TexCoords,
{
type TexCoord = M::TexCoord;
type TexCoords = M::TexCoords;
fn tex_coords(&self) -> &Self::TexCoords {
(**self).tex_coords()
}
}
impl<M, I> TexCoords for WithIndices<M, I>
where
M: TexCoords,
{
type TexCoord = M::TexCoord;
type TexCoords = M::TexCoords;
fn tex_coords(&self) -> &Self::TexCoords {
self.mesh.tex_coords()
}
}
impl<M, C> TexCoords for WithColors<M, C>
where
M: TexCoords,
{
type TexCoord = M::TexCoord;
type TexCoords = M::TexCoords;
fn tex_coords(&self) -> &Self::TexCoords {
self.mesh.tex_coords()
}
}
impl<M, N> TexCoords for WithNormals<M, N>
where
M: TexCoords,
{
type TexCoord = M::TexCoord;
type TexCoords = M::TexCoords;
fn tex_coords(&self) -> &Self::TexCoords {
self.mesh.tex_coords()
}
}
impl<M, N> Normals for WithNormals<M, N>
where
M: Points,
N: Channel,
{
type Normal = N::Element;
type Normals = N;
fn normals(&self) -> &Self::Normals {
&self.normals
}
}
impl<'a, M> Normals for &'a M
where
M: Normals,
{
type Normal = M::Normal;
type Normals = M::Normals;
fn normals(&self) -> &Self::Normals {
(**self).normals()
}
}
impl<'a, M> Normals for &'a mut M
where
M: Normals,
{
type Normal = M::Normal;
type Normals = M::Normals;
fn normals(&self) -> &Self::Normals {
(**self).normals()
}
}
impl<'a, M> Normals for Ref<'a, M>
where
M: Normals,
{
type Normal = M::Normal;
type Normals = M::Normals;
fn normals(&self) -> &Self::Normals {
(**self).normals()
}
}
impl<'a, M> Normals for RefMut<'a, M>
where
M: Normals,
{
type Normal = M::Normal;
type Normals = M::Normals;
fn normals(&self) -> &Self::Normals {
(**self).normals()
}
}
impl<M, I> Normals for WithIndices<M, I>
where
M: Normals,
{
type Normal = M::Normal;
type Normals = M::Normals;
fn normals(&self) -> &Self::Normals {
self.mesh.normals()
}
}
impl<M, C> Normals for WithColors<M, C>
where
M: Normals,
{
type Normal = M::Normal;
type Normals = M::Normals;
fn normals(&self) -> &Self::Normals {
self.mesh.normals()
}
}
impl<M, T> Normals for WithTexCoords<M, T>
where
M: Normals,
{
type Normal = M::Normal;
type Normals = M::Normals;
fn normals(&self) -> &Self::Normals {
self.mesh.normals()
}
}
impl<'a, M, V> PushVertex<V> for &'a mut M
where
M: PushVertex<V>,
{
fn push_vertex(&mut self, v: V) {
(**self).push_vertex(v)
}
}
impl<'a, M, V> PushVertex<V> for RefMut<'a, M>
where
M: PushVertex<V>,
{
fn push_vertex(&mut self, v: V) {
(**self).push_vertex(v)
}
}
impl<V> PushVertex<V> for MeshPoints<Vec<V>> {
fn push_vertex(&mut self, v: V) {
self.points.push(v);
}
}
impl<M, I, V> PushVertex<V> for WithIndices<M, Vec<I>>
where
M: PushVertex<V>,
{
fn push_vertex(&mut self, v: V) {
self.mesh.push_vertex(v);
}
}
impl<M, V, C> PushVertex<vertex::WithColor<V, C>> for WithColors<M, Vec<C>>
where
M: PushVertex<V>,
{
fn push_vertex(&mut self, v: vertex::WithColor<V, C>) {
let vertex::WithColor { vertex, color } = v;
self.colors.push(color);
self.mesh.push_vertex(vertex);
}
}
impl<M, V, T> PushVertex<vertex::WithTexCoords<V, T>> for WithTexCoords<M, Vec<T>>
where
M: PushVertex<V>,
{
fn push_vertex(&mut self, v: vertex::WithTexCoords<V, T>) {
let vertex::WithTexCoords { vertex, tex_coords } = v;
self.tex_coords.push(tex_coords);
self.mesh.push_vertex(vertex);
}
}
impl<M, V, N> PushVertex<vertex::WithNormal<V, N>> for WithNormals<M, Vec<N>>
where
M: PushVertex<V>,
{
fn push_vertex(&mut self, v: vertex::WithNormal<V, N>) {
let vertex::WithNormal { vertex, normal } = v;
self.normals.push(normal);
self.mesh.push_vertex(vertex);
}
}
impl<'a, M> PushIndex for &'a mut M
where
M: PushIndex,
{
type Index = M::Index;
fn push_index(&mut self, index: Self::Index) {
(**self).push_index(index);
}
fn extend_indices<I>(&mut self, indices: I)
where
I: IntoIterator<Item = Self::Index>,
{
(**self).extend_indices(indices);
}
}
impl<'a, M> PushIndex for RefMut<'a, M>
where
M: PushIndex,
{
type Index = M::Index;
fn push_index(&mut self, index: Self::Index) {
(**self).push_index(index);
}
fn extend_indices<I>(&mut self, indices: I)
where
I: IntoIterator<Item = Self::Index>,
{
(**self).extend_indices(indices);
}
}
impl<M, I> PushIndex for WithIndices<M, Vec<I>> {
type Index = I;
fn push_index(&mut self, index: Self::Index) {
self.indices.push(index);
}
fn extend_indices<It>(&mut self, indices: It)
where
It: IntoIterator<Item = Self::Index>,
{
self.indices.extend(indices);
}
}
impl<M, C> PushIndex for WithColors<M, C>
where
M: PushIndex,
{
type Index = M::Index;
fn push_index(&mut self, index: Self::Index) {
self.mesh.push_index(index);
}
fn extend_indices<I>(&mut self, indices: I)
where
I: IntoIterator<Item = Self::Index>,
{
self.mesh.extend_indices(indices);
}
}
impl<M, T> PushIndex for WithTexCoords<M, T>
where
M: PushIndex,
{
type Index = M::Index;
fn push_index(&mut self, index: Self::Index) {
self.mesh.push_index(index);
}
fn extend_indices<I>(&mut self, indices: I)
where
I: IntoIterator<Item = Self::Index>,
{
self.mesh.extend_indices(indices);
}
}
impl<M, N> PushIndex for WithNormals<M, N>
where
M: PushIndex,
{
type Index = M::Index;
fn push_index(&mut self, index: M::Index) {
self.mesh.push_index(index);
}
fn extend_indices<I>(&mut self, indices: I)
where
I: IntoIterator<Item = M::Index>,
{
self.mesh.extend_indices(indices);
}
}
impl<'a, M> ClearIndices for &'a mut M
where
M: ClearIndices,
{
fn clear_indices(&mut self) {
(**self).clear_indices();
}
}
impl<'a, M> ClearIndices for RefMut<'a, M>
where
M: ClearIndices,
{
fn clear_indices(&mut self) {
(**self).clear_indices();
}
}
impl<M, I> ClearIndices for WithIndices<M, Vec<I>> {
fn clear_indices(&mut self) {
self.indices.clear();
}
}
impl<M, C> ClearIndices for WithColors<M, C>
where
M: ClearIndices,
{
fn clear_indices(&mut self) {
self.mesh.clear_indices();
}
}
impl<M, T> ClearIndices for WithTexCoords<M, T>
where
M: ClearIndices,
{
fn clear_indices(&mut self) {
self.mesh.clear_indices();
}
}
impl<M, N> ClearIndices for WithNormals<M, N>
where
M: ClearIndices,
{
fn clear_indices(&mut self) {
self.mesh.clear_indices();
}
}
impl<'a, M> ClearVertices for &'a mut M
where
M: ClearVertices,
{
fn clear_vertices(&mut self) {
(**self).clear_vertices()
}
}
impl<'a, M> ClearVertices for RefMut<'a, M>
where
M: ClearVertices,
{
fn clear_vertices(&mut self) {
(**self).clear_vertices()
}
}
impl<V> ClearVertices for MeshPoints<Vec<V>> {
fn clear_vertices(&mut self) {
self.points.clear();
}
}
impl<M, I> ClearVertices for WithIndices<M, Vec<I>>
where
M: ClearVertices,
{
fn clear_vertices(&mut self) {
self.mesh.clear_vertices();
self.indices.clear();
}
}
impl<M, C> ClearVertices for WithColors<M, Vec<C>>
where
M: ClearVertices,
{
fn clear_vertices(&mut self) {
self.mesh.clear_vertices();
self.colors.clear();
}
}
impl<M, T> ClearVertices for WithTexCoords<M, Vec<T>>
where
M: ClearVertices,
{
fn clear_vertices(&mut self) {
self.mesh.clear_vertices();
self.tex_coords.clear();
}
}
impl<M, N> ClearVertices for WithNormals<M, Vec<N>>
where
M: ClearVertices,
{
fn clear_vertices(&mut self) {
self.mesh.clear_vertices();
self.normals.clear();
}
}
impl<'a, P> ExtendFromSlice<'a> for MeshPoints<Vec<P>>
where
P: 'a + Clone,
{
type Slice = &'a [P];
fn extend_from_slice(&mut self, slice: Self::Slice) {
self.points.extend_from_slice(slice);
}
}
impl<'a, M, I> ExtendFromSlice<'a> for WithIndices<M, Vec<I>>
where
M: ExtendFromSlice<'a>,
I: 'a + Clone,
{
type Slice = (&'a [I], M::Slice);
fn extend_from_slice(&mut self, slice: Self::Slice) {
let (slice, inner) = slice;
self.mesh.extend_from_slice(inner);
self.indices.extend_from_slice(slice);
}
}
impl<'a, M, C> ExtendFromSlice<'a> for WithColors<M, Vec<C>>
where
M: ExtendFromSlice<'a>,
C: 'a + Clone,
{
type Slice = (&'a [C], M::Slice);
fn extend_from_slice(&mut self, slice: Self::Slice) {
let (slice, inner) = slice;
self.mesh.extend_from_slice(inner);
self.colors.extend_from_slice(slice);
}
}
impl<'a, M, T> ExtendFromSlice<'a> for WithTexCoords<M, Vec<T>>
where
M: ExtendFromSlice<'a>,
T: 'a + Clone,
{
type Slice = (&'a [T], M::Slice);
fn extend_from_slice(&mut self, slice: Self::Slice) {
let (slice, inner) = slice;
self.mesh.extend_from_slice(inner);
self.tex_coords.extend_from_slice(slice);
}
}
impl<'a, M, N> ExtendFromSlice<'a> for WithNormals<M, Vec<N>>
where
M: ExtendFromSlice<'a>,
N: 'a + Clone,
{
type Slice = (&'a [N], M::Slice);
fn extend_from_slice(&mut self, slice: Self::Slice) {
let (slice, inner) = slice;
self.mesh.extend_from_slice(inner);
self.normals.extend_from_slice(slice);
}
}
impl<T> Clear for T where T: ClearIndices + ClearVertices {}
impl<P> Default for MeshPoints<P>
where
P: Default,
{
fn default() -> Self {
let points = Default::default();
MeshPoints { points }
}
}
impl<M, I> Default for WithIndices<M, I>
where
M: Default,
I: Default,
{
fn default() -> Self {
let mesh = Default::default();
let indices = Default::default();
WithIndices { mesh, indices }
}
}
impl<M, C> Default for WithColors<M, C>
where
M: Default,
C: Default,
{
fn default() -> Self {
let mesh = Default::default();
let colors = Default::default();
WithColors { mesh, colors }
}
}
impl<M, T> Default for WithTexCoords<M, T>
where
M: Default,
T: Default,
{
fn default() -> Self {
let mesh = Default::default();
let tex_coords = Default::default();
WithTexCoords { mesh, tex_coords }
}
}
impl<M, N> Default for WithNormals<M, N>
where
M: Default,
N: Default,
{
fn default() -> Self {
let mesh = Default::default();
let normals = Default::default();
WithNormals { mesh, normals }
}
}
impl<M, I> Deref for WithIndices<M, I> {
type Target = M;
fn deref(&self) -> &Self::Target {
&self.mesh
}
}
impl<M, I> DerefMut for WithIndices<M, I> {
fn deref_mut(&mut self) -> &mut Self::Target {
&mut self.mesh
}
}
impl<M, C> Deref for WithColors<M, C> {
type Target = M;
fn deref(&self) -> &Self::Target {
&self.mesh
}
}
impl<M, C> DerefMut for WithColors<M, C> {
fn deref_mut(&mut self) -> &mut Self::Target {
&mut self.mesh
}
}
impl<M, T> Deref for WithTexCoords<M, T> {
type Target = M;
fn deref(&self) -> &Self::Target {
&self.mesh
}
}
impl<M, T> DerefMut for WithTexCoords<M, T> {
fn deref_mut(&mut self) -> &mut Self::Target {
&mut self.mesh
}
}
impl<M, N> Deref for WithNormals<M, N> {
type Target = M;
fn deref(&self) -> &Self::Target {
&self.mesh
}
}
impl<M, N> DerefMut for WithNormals<M, N> {
fn deref_mut(&mut self) -> &mut Self::Target {
&mut self.mesh
}
}
pub fn raw_vertex_count<M>(mesh: M) -> usize
where
M: Points,
{
mesh.points().channel().len()
}
pub fn vertex_count<M>(mesh: M) -> usize
where
M: Indices,
{
mesh.indices().channel().len()
}
pub fn triangle_count<M>(mesh: M) -> usize
where
M: Indices,
{
vertex_count(mesh) / geom::tri::NUM_VERTICES as usize
}
pub fn from_points<P>(points: P) -> MeshPoints<P>
where
P: Channel,
{
MeshPoints { points }
}
pub fn with_indices<M, I, Ix>(mesh: M, indices: I) -> WithIndices<M, I>
where
M: GetVertex<Ix>,
I: Channel<Element = Ix>,
{
WithIndices { mesh, indices }
}
pub fn with_colors<M, C>(mesh: M, colors: C) -> WithColors<M, C>
where
M: Points,
C: Channel,
{
assert_eq!(raw_vertex_count(&mesh), colors.channel().len());
WithColors { mesh, colors }
}
pub fn with_tex_coords<M, T>(mesh: M, tex_coords: T) -> WithTexCoords<M, T>
where
M: Points,
T: Channel,
{
assert_eq!(raw_vertex_count(&mesh), tex_coords.channel().len());
WithTexCoords { mesh, tex_coords }
}
pub fn with_normals<M, N>(mesh: M, normals: N) -> WithNormals<M, N>
where
M: Points,
N: Channel,
{
assert_eq!(raw_vertex_count(&mesh), normals.channel().len());
WithNormals { mesh, normals }
}
pub fn push_vertex<M, V>(mut mesh: M, vertex: V)
where
M: PushVertex<V>,
{
mesh.push_vertex(vertex);
}
pub fn extend_vertices<M, I>(mut mesh: M, vertices: I)
where
M: PushVertex<I::Item>,
I: IntoIterator,
{
for v in vertices {
push_vertex(&mut mesh, v);
}
}
pub fn push_index<M>(mut mesh: M, index: M::Index)
where
M: PushIndex,
{
mesh.push_index(index);
}
pub fn extend_indices<M, I>(mut mesh: M, indices: I)
where
M: PushIndex,
I: IntoIterator<Item = M::Index>,
{
mesh.extend_indices(indices);
}
pub fn clear_vertices<M>(mut mesh: M)
where
M: ClearVertices,
{
mesh.clear_vertices();
}
pub fn clear_indices<M>(mut mesh: M)
where
M: ClearIndices,
{
mesh.clear_indices();
}
pub fn clear<M>(mut mesh: M)
where
M: Clear,
{
mesh.clear();
}
#[derive(Clone, Debug)]
pub struct RawVertices<M> {
range: ops::Range<usize>,
mesh: M,
}
#[derive(Clone, Debug)]
pub struct Vertices<M> {
index_range: ops::Range<usize>,
mesh: M,
}
pub type Triangles<M> = geom::tri::IterFromVertices<Vertices<M>>;
pub fn raw_vertices<M>(mesh: M) -> RawVertices<M>
where
M: Points,
{
let len = raw_vertex_count(&mesh);
let range = 0..len;
RawVertices { range, mesh }
}
pub fn vertices<M, I>(mesh: M) -> Vertices<M>
where
M: Indices<Index = I> + GetVertex<I>,
I: TryFrom<usize>,
{
let len = mesh.indices().channel().len();
let index_range = 0..len;
Vertices { index_range, mesh }
}
pub fn triangles<M, I>(mesh: M) -> Triangles<M>
where
M: Indices<Index = I> + GetVertex<I>,
I: Copy + TryFrom<usize>,
{
geom::tri::iter_from_vertices(vertices(mesh))
}
const NO_VERTEX_FOR_INDEX: &'static str =
"no vertex for the index produced by the mesh's indices channel";
impl<M> RawVertices<M> {
pub fn range(mut self, range: ops::Range<usize>) -> Self {
self.range = range;
self
}
}
impl<M> Vertices<M>
where
M: Indices,
{
pub fn index_range(mut self, range: ops::Range<usize>) -> Self {
self.index_range = range;
self
}
pub fn triangles<I>(self) -> Triangles<M>
where
M: Indices<Index = I> + GetVertex<I>,
I: Copy,
{
geom::tri::iter_from_vertices(self)
}
}
impl<M> Iterator for RawVertices<M>
where
M: GetVertex<usize>,
{
type Item = M::Vertex;
fn next(&mut self) -> Option<Self::Item> {
if let Some(vertex) = self.range.next().and_then(|i| self.mesh.get_vertex(i)) {
return Some(vertex);
}
None
}
}
impl<M, I> Iterator for Vertices<M>
where
M: Indices<Index = I> + GetVertex<I>,
I: Copy,
{
type Item = M::Vertex;
fn next(&mut self) -> Option<Self::Item> {
if let Some(i) = self.index_range.next() {
if let Some(&index) = self.mesh.indices().channel().get(i) {
let vertex = self.mesh.get_vertex(index).expect(NO_VERTEX_FOR_INDEX);
return Some(vertex);
}
}
None
}
fn size_hint(&self) -> (usize, Option<usize>) {
let len = self.len();
(len, Some(len))
}
}
impl<M> ExactSizeIterator for RawVertices<M>
where
M: GetVertex<usize>,
{
fn len(&self) -> usize {
self.range.len()
}
}
impl<M, I> DoubleEndedIterator for Vertices<M>
where
M: Indices<Index = I> + GetVertex<I>,
I: Copy,
{
fn next_back(&mut self) -> Option<Self::Item> {
if let Some(i) = self.index_range.next_back() {
if let Some(&index) = self.mesh.indices().channel().get(i) {
let vertex = self.mesh.get_vertex(index).expect(NO_VERTEX_FOR_INDEX);
return Some(vertex);
}
}
None
}
}
impl<M, I> ExactSizeIterator for Vertices<M>
where
M: Indices<Index = I> + GetVertex<I>,
I: Copy,
{
fn len(&self) -> usize {
let indices_len = self.mesh.indices().channel().len();
let remaining_indices = indices_len - self.index_range.start;
let range_len = self.index_range.len();
cmp::min(remaining_indices, range_len)
}
}