use crate::geom;
use crate::mesh::{self, MeshPoints, WithColors, WithIndices, WithTexCoords};
use std::ops::{Deref, DerefMut};
pub mod builder;
pub mod vertex;
pub use self::builder::MeshBuilder;
pub use self::vertex::Vertex;
pub type Points = Vec<vertex::Point>;
pub type Indices = Vec<u32>;
pub type Colors = Vec<vertex::Color>;
pub type TexCoords = Vec<vertex::TexCoords>;
pub type MeshType =
WithTexCoords<WithColors<WithIndices<MeshPoints<Points>, Indices>, Colors>, TexCoords>;
#[derive(Clone, Debug)]
pub struct Mesh {
mesh: MeshType,
}
impl Mesh {
pub fn raw_vertex_count(&self) -> usize {
mesh::raw_vertex_count(self)
}
pub fn vertex_count(&self) -> usize {
mesh::vertex_count(self)
}
pub fn triangle_count(&self) -> usize {
mesh::triangle_count(self)
}
pub fn points(&self) -> &[vertex::Point] {
mesh::Points::points(self)
}
pub fn indices(&self) -> &[u32] {
mesh::Indices::indices(self)
}
pub fn colors(&self) -> &[vertex::Color] {
mesh::Colors::colors(self)
}
pub fn tex_coords(&self) -> &[vertex::TexCoords] {
mesh::TexCoords::tex_coords(self)
}
pub fn push_vertex(&mut self, v: Vertex) {
mesh::push_vertex(self, v);
}
pub fn push_index(&mut self, i: u32) {
mesh::push_index(self, i);
}
pub fn extend_vertices<I>(&mut self, vs: I)
where
I: IntoIterator<Item = Vertex>,
{
mesh::extend_vertices(self, vs);
}
pub fn extend_indices<I>(&mut self, is: I)
where
I: IntoIterator<Item = u32>,
{
mesh::extend_indices(self, is);
}
pub fn extend<V, I>(&mut self, vs: V, is: I)
where
V: IntoIterator<Item = Vertex>,
I: IntoIterator<Item = u32>,
{
self.extend_vertices(vs);
self.extend_indices(is);
}
pub fn clear_vertices(&mut self) {
mesh::clear_vertices(self);
}
pub fn clear_indices(&mut self) {
mesh::clear_indices(self);
}
pub fn clear(&mut self) {
mesh::clear(self);
}
pub fn raw_vertices(&self) -> mesh::RawVertices<&Self> {
mesh::raw_vertices(self)
}
pub fn into_raw_vertices(self) -> mesh::RawVertices<Self> {
mesh::raw_vertices(self)
}
pub fn extend_from_slices(
&mut self,
points: &[vertex::Point],
indices: &[u32],
colors: &[vertex::Color],
tex_coords: &[vertex::TexCoords],
) {
assert_eq!(points.len(), colors.len());
assert_eq!(points.len(), tex_coords.len());
let slices = (tex_coords, (colors, (indices, points)));
mesh::ExtendFromSlice::extend_from_slice(&mut self.mesh, slices);
}
pub fn extend_vertices_from_slices(
&mut self,
points: &[vertex::Point],
colors: &[vertex::Color],
tex_coords: &[vertex::TexCoords],
) {
self.extend_from_slices(points, &[], colors, tex_coords);
}
pub fn extend_indices_from_slice(&mut self, indices: &[u32]) {
self.extend_from_slices(&[], indices, &[], &[]);
}
pub fn vertices(&self) -> mesh::Vertices<&Self> {
mesh::vertices(self)
}
pub fn triangles(&self) -> mesh::Triangles<&Self> {
mesh::triangles(self)
}
pub fn into_vertices(self) -> mesh::Vertices<Self> {
mesh::vertices(self)
}
pub fn into_triangles(self) -> mesh::Triangles<Self> {
mesh::triangles(self)
}
}
impl Default for Mesh {
fn default() -> Self {
let mesh = Default::default();
Mesh { mesh }
}
}
impl Deref for Mesh {
type Target = MeshType;
fn deref(&self) -> &Self::Target {
&self.mesh
}
}
impl DerefMut for Mesh {
fn deref_mut(&mut self) -> &mut Self::Target {
&mut self.mesh
}
}
impl mesh::GetVertex<u32> for Mesh {
type Vertex = Vertex;
fn get_vertex(&self, index: u32) -> Option<Self::Vertex> {
mesh::WithTexCoords::get_vertex(&self.mesh, index)
}
}
impl mesh::Points for Mesh {
type Point = vertex::Point;
type Points = Points;
fn points(&self) -> &Self::Points {
self.mesh.points()
}
}
impl mesh::Indices for Mesh {
type Index = u32;
type Indices = Indices;
fn indices(&self) -> &Self::Indices {
self.mesh.indices()
}
}
impl mesh::Colors for Mesh {
type Color = vertex::Color;
type Colors = Colors;
fn colors(&self) -> &Self::Colors {
self.mesh.colors()
}
}
impl mesh::TexCoords for Mesh {
type TexCoord = geom::Point2;
type TexCoords = TexCoords;
fn tex_coords(&self) -> &Self::TexCoords {
self.mesh.tex_coords()
}
}
impl mesh::PushVertex<Vertex> for Mesh {
fn push_vertex(&mut self, v: Vertex) {
self.mesh.push_vertex(v);
}
}
impl mesh::PushIndex for Mesh {
type Index = u32;
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 mesh::ClearIndices for Mesh {
fn clear_indices(&mut self) {
self.mesh.clear_indices();
}
}
impl mesh::ClearVertices for Mesh {
fn clear_vertices(&mut self) {
self.mesh.clear_vertices();
}
}
#[test]
fn test_method_access() {
let mesh: Mesh = Default::default();
assert_eq!(None, mesh::GetVertex::get_vertex(&mesh, 0));
mesh::Points::points(&mesh);
mesh::Indices::indices(&mesh);
mesh::Colors::colors(&mesh);
mesh::TexCoords::tex_coords(&mesh);
}