Vendor dependencies for 0.3.0 release

This commit is contained in:
2025-09-27 10:29:08 -05:00
parent 0c8d39d483
commit 82ab7f317b
26803 changed files with 16134934 additions and 0 deletions

177
vendor/gltf/src/mesh/iter.rs vendored Normal file
View File

@@ -0,0 +1,177 @@
use std::{collections, iter, slice};
use super::{Attribute, Mesh, MorphTarget, Primitive};
use crate::Document;
/// An `Iterator` that visits the morph targets of a `Primitive`.
#[derive(Clone, Debug)]
pub struct MorphTargets<'a> {
/// The parent `Document` struct.
pub(crate) document: &'a Document,
/// The internal JSON iterator.
pub(crate) iter: slice::Iter<'a, json::mesh::MorphTarget>,
}
/// An `Iterator` that visits the attributes of a `Primitive`.
#[derive(Clone, Debug)]
pub struct Attributes<'a> {
/// The parent `Document` struct.
pub(crate) document: &'a Document,
/// The parent `Primitive` struct.
#[allow(dead_code)]
pub(crate) prim: Primitive<'a>,
/// The internal attribute iterator.
pub(crate) iter: collections::btree_map::Iter<
'a,
json::validation::Checked<json::mesh::Semantic>,
json::Index<json::accessor::Accessor>,
>,
}
/// An `Iterator` that visits the primitives of a `Mesh`.
#[derive(Clone, Debug)]
pub struct Primitives<'a> {
/// The parent `Mesh` struct.
pub(crate) mesh: Mesh<'a>,
/// The internal JSON primitive iterator.
pub(crate) iter: iter::Enumerate<slice::Iter<'a, json::mesh::Primitive>>,
}
impl<'a> ExactSizeIterator for Attributes<'a> {}
impl<'a> Iterator for Attributes<'a> {
type Item = Attribute<'a>;
fn next(&mut self) -> Option<Self::Item> {
self.iter.next().map(|(key, index)| {
let semantic = key.as_ref().unwrap().clone();
let accessor = self.document.accessors().nth(index.value()).unwrap();
(semantic, accessor)
})
}
fn size_hint(&self) -> (usize, Option<usize>) {
self.iter.size_hint()
}
}
impl<'a> ExactSizeIterator for Primitives<'a> {}
impl<'a> Iterator for Primitives<'a> {
type Item = Primitive<'a>;
fn next(&mut self) -> Option<Self::Item> {
self.iter
.next()
.map(|(index, json)| Primitive::new(self.mesh.clone(), index, json))
}
fn size_hint(&self) -> (usize, Option<usize>) {
self.iter.size_hint()
}
fn count(self) -> usize {
self.iter.count()
}
fn last(self) -> Option<Self::Item> {
let mesh = self.mesh;
self.iter
.last()
.map(|(index, json)| Primitive::new(mesh, index, json))
}
fn nth(&mut self, n: usize) -> Option<Self::Item> {
self.iter
.nth(n)
.map(|(index, json)| Primitive::new(self.mesh.clone(), index, json))
}
}
fn map_morph_target<'a>(
document: &'a crate::Document,
json: &json::mesh::MorphTarget,
) -> MorphTarget<'a> {
let positions = json
.positions
.as_ref()
.map(|index| document.accessors().nth(index.value()).unwrap());
let normals = json
.normals
.as_ref()
.map(|index| document.accessors().nth(index.value()).unwrap());
let tangents = json
.tangents
.as_ref()
.map(|index| document.accessors().nth(index.value()).unwrap());
MorphTarget {
positions,
normals,
tangents,
}
}
impl<'a> ExactSizeIterator for MorphTargets<'a> {}
impl<'a> Iterator for MorphTargets<'a> {
type Item = MorphTarget<'a>;
fn next(&mut self) -> Option<Self::Item> {
self.iter
.next()
.map(|json| map_morph_target(self.document, json))
}
fn size_hint(&self) -> (usize, Option<usize>) {
self.iter.size_hint()
}
fn count(self) -> usize {
self.iter.count()
}
fn last(self) -> Option<Self::Item> {
let document = self.document;
self.iter
.last()
.map(|json| map_morph_target(document, json))
}
fn nth(&mut self, n: usize) -> Option<Self::Item> {
self.iter
.nth(n)
.map(|json| map_morph_target(self.document, json))
}
}
/// An `Iterator` that visits the variant mappings of a `Mesh`.
#[cfg(feature = "KHR_materials_variants")]
#[derive(Clone, Debug)]
pub struct Mappings<'a> {
/// Internal mapping iterator.
pub(crate) iter: slice::Iter<'a, json::extensions::mesh::Mapping>,
/// The internal root glTF object.
pub(crate) document: &'a Document,
}
#[cfg(feature = "KHR_materials_variants")]
impl<'a> ExactSizeIterator for Mappings<'a> {}
#[cfg(feature = "KHR_materials_variants")]
impl<'a> Iterator for Mappings<'a> {
type Item = crate::khr_materials_variants::Mapping<'a>;
fn next(&mut self) -> Option<Self::Item> {
let document = self.document;
self.iter
.next()
.map(|json| crate::khr_materials_variants::Mapping::new(document, json))
}
fn size_hint(&self) -> (usize, Option<usize>) {
self.iter.size_hint()
}
fn count(self) -> usize {
self.iter.count()
}
fn last(self) -> Option<Self::Item> {
let document = self.document;
self.iter
.last()
.map(|json| crate::khr_materials_variants::Mapping::new(document, json))
}
fn nth(&mut self, n: usize) -> Option<Self::Item> {
let document = self.document;
self.iter
.nth(n)
.map(|json| crate::khr_materials_variants::Mapping::new(document, json))
}
}

476
vendor/gltf/src/mesh/mod.rs vendored Normal file
View File

@@ -0,0 +1,476 @@
//! # Basic usage
//!
//! Listing the attributes of each mesh primitive in a glTF asset.
//!
//! ```
//! # fn run() -> Result<(), Box<dyn std::error::Error>> {
//! # let gltf = gltf::Gltf::open("examples/Box.gltf")?;
//! for mesh in gltf.meshes() {
//! println!("Mesh #{}", mesh.index());
//! for primitive in mesh.primitives() {
//! println!("- Primitive #{}", primitive.index());
//! for (semantic, _) in primitive.attributes() {
//! println!("-- {:?}", semantic);
//! }
//! }
//! }
//! # Ok(())
//! # }
//! # fn main() {
//! # let _ = run().expect("runtime error");
//! # }
//! ```
//!
//! # Reader utility
//!
//! Printing the vertex positions of each primitive of each mesh in
//! a glTF asset.
//!
//! ```
//! # fn run() -> Result<(), Box<dyn std::error::Error>> {
//! let (gltf, buffers, _) = gltf::import("examples/Box.gltf")?;
//! for mesh in gltf.meshes() {
//! println!("Mesh #{}", mesh.index());
//! for primitive in mesh.primitives() {
//! println!("- Primitive #{}", primitive.index());
//! let reader = primitive.reader(|buffer| Some(&buffers[buffer.index()]));
//! if let Some(iter) = reader.read_positions() {
//! for vertex_position in iter {
//! println!("{:?}", vertex_position);
//! }
//! }
//! }
//! }
//! # Ok(())
//! # }
//! # fn main() {
//! # let _ = run().expect("runtime error");
//! # }
//! ```
/// Iterators.
pub mod iter;
/// Utility functions.
#[cfg(feature = "utils")]
#[cfg_attr(docsrs, doc(cfg(feature = "utils")))]
pub mod util;
use crate::{Accessor, Buffer, Document, Material};
#[cfg(feature = "utils")]
use crate::accessor;
pub use json::mesh::{Mode, Semantic};
use json::validation::Checked;
#[cfg(feature = "extensions")]
use serde_json::{Map, Value};
/// Vertex attribute data.
pub type Attribute<'a> = (Semantic, Accessor<'a>);
/// Vertex position bounding box.
pub type BoundingBox = Bounds<[f32; 3]>;
/// The minimum and maximum values for a generic accessor.
#[derive(Clone, Debug, PartialEq)]
pub struct Bounds<T> {
/// Minimum value.
pub min: T,
/// Maximum value.
pub max: T,
}
/// A set of primitives to be rendered.
#[derive(Clone, Debug)]
pub struct Mesh<'a> {
/// The parent `Document` struct.
document: &'a Document,
/// The corresponding JSON index.
index: usize,
/// The corresponding JSON struct.
json: &'a json::mesh::Mesh,
}
/// A single morph target for a mesh primitive.
#[derive(Clone, Debug)]
pub struct MorphTarget<'a> {
/// XYZ vertex position displacements.
positions: Option<Accessor<'a>>,
/// XYZ vertex normal displacements.
normals: Option<Accessor<'a>>,
/// XYZ vertex tangent displacements.
tangents: Option<Accessor<'a>>,
}
/// Geometry to be rendered with the given material.
#[derive(Clone, Debug)]
pub struct Primitive<'a> {
/// The parent `Mesh` struct.
mesh: Mesh<'a>,
/// The corresponding JSON index.
index: usize,
/// The corresponding JSON struct.
json: &'a json::mesh::Primitive,
}
/// Mesh primitive reader.
#[derive(Clone, Debug)]
pub struct Reader<'a, 's, F>
where
F: Clone + Fn(Buffer<'a>) -> Option<&'s [u8]>,
{
#[allow(dead_code)]
pub(crate) primitive: &'a Primitive<'a>,
#[allow(dead_code)]
pub(crate) get_buffer_data: F,
}
impl<'a> Mesh<'a> {
/// Constructs a `Mesh`.
pub(crate) fn new(document: &'a Document, index: usize, json: &'a json::mesh::Mesh) -> Self {
Self {
document,
index,
json,
}
}
/// Returns the internal JSON index.
pub fn index(&self) -> usize {
self.index
}
/// Returns extension data unknown to this crate version.
#[cfg(feature = "extensions")]
#[cfg_attr(docsrs, doc(cfg(feature = "extensions")))]
pub fn extensions(&self) -> Option<&Map<String, Value>> {
let ext = self.json.extensions.as_ref()?;
Some(&ext.others)
}
/// Queries extension data unknown to this crate version.
#[cfg(feature = "extensions")]
#[cfg_attr(docsrs, doc(cfg(feature = "extensions")))]
pub fn extension_value(&self, ext_name: &str) -> Option<&Value> {
let ext = self.json.extensions.as_ref()?;
ext.others.get(ext_name)
}
/// Optional application specific data.
pub fn extras(&self) -> &'a json::Extras {
&self.json.extras
}
/// Optional user-defined name for this object.
#[cfg(feature = "names")]
#[cfg_attr(docsrs, doc(cfg(feature = "names")))]
pub fn name(&self) -> Option<&'a str> {
self.json.name.as_deref()
}
/// Defines the geometry to be renderered with a material.
pub fn primitives(&self) -> iter::Primitives<'a> {
iter::Primitives {
mesh: self.clone(),
iter: self.json.primitives.iter().enumerate(),
}
}
/// Defines the weights to be applied to the morph targets.
pub fn weights(&self) -> Option<&'a [f32]> {
self.json.weights.as_deref()
}
}
impl<'a> Primitive<'a> {
/// Constructs a `Primitive`.
pub(crate) fn new(mesh: Mesh<'a>, index: usize, json: &'a json::mesh::Primitive) -> Self {
Self { mesh, index, json }
}
/// Returns the bounds of the `POSITION` vertex attribute.
pub fn bounding_box(&self) -> BoundingBox {
// NOTE: cannot panic if validated "minimally"
let pos_accessor_index = self
.json
.attributes
.get(&Checked::Valid(Semantic::Positions))
.unwrap();
let pos_accessor = self
.mesh
.document
.accessors()
.nth(pos_accessor_index.value())
.unwrap();
let min: [f32; 3] = json::deserialize::from_value(pos_accessor.min().unwrap()).unwrap();
let max: [f32; 3] = json::deserialize::from_value(pos_accessor.max().unwrap()).unwrap();
Bounds { min, max }
}
/// Returns extension data unknown to this crate version.
#[cfg(feature = "extensions")]
#[cfg_attr(docsrs, doc(cfg(feature = "extensions")))]
pub fn extensions(&self) -> Option<&Map<String, Value>> {
let ext = self.json.extensions.as_ref()?;
Some(&ext.others)
}
/// Queries extension data unknown to this crate version.
#[cfg(feature = "extensions")]
#[cfg_attr(docsrs, doc(cfg(feature = "extensions")))]
pub fn extension_value(&self, ext_name: &str) -> Option<&Value> {
let ext = self.json.extensions.as_ref()?;
ext.others.get(ext_name)
}
/// Optional application specific data.
pub fn extras(&self) -> &'a json::Extras {
&self.json.extras
}
/// Return the accessor with the given semantic.
pub fn get(&self, semantic: &Semantic) -> Option<Accessor<'a>> {
self.json
.attributes
.get(&json::validation::Checked::Valid(semantic.clone()))
.map(|index| self.mesh.document.accessors().nth(index.value()).unwrap())
}
/// Returns the internal JSON index.
pub fn index(&self) -> usize {
self.index
}
/// Returns the accessor containing the primitive indices, if provided.
pub fn indices(&self) -> Option<Accessor<'a>> {
self.json
.indices
.as_ref()
.map(|index| self.mesh.document.accessors().nth(index.value()).unwrap())
}
/// Returns an `Iterator` that visits the vertex attributes.
pub fn attributes(&self) -> iter::Attributes<'a> {
iter::Attributes {
document: self.mesh.document,
prim: self.clone(),
iter: self.json.attributes.iter(),
}
}
/// Returns the material to apply to this primitive when rendering
pub fn material(&self) -> Material<'a> {
self.json
.material
.as_ref()
.map(|index| self.mesh.document.materials().nth(index.value()).unwrap())
.unwrap_or_else(|| Material::default(self.mesh.document))
}
/// The type of primitives to render.
pub fn mode(&self) -> Mode {
self.json.mode.unwrap()
}
/// Returns an `Iterator` that visits the morph targets of the primitive.
pub fn morph_targets(&self) -> iter::MorphTargets<'a> {
if let Some(slice) = self.json.targets.as_ref() {
iter::MorphTargets {
document: self.mesh.document,
iter: slice.iter(),
}
} else {
iter::MorphTargets {
document: self.mesh.document,
iter: ([]).iter(),
}
}
}
/// Get the material variants.
#[cfg(feature = "KHR_materials_variants")]
#[cfg_attr(docsrs, doc(cfg(feature = "KHR_materials_variants")))]
pub fn mappings(&self) -> iter::Mappings<'a> {
let iter = self
.json
.extensions
.as_ref()
.and_then(|extensions| extensions.khr_materials_variants.as_ref())
.map(|variants| variants.mappings.iter())
.unwrap_or_else(|| ([]).iter());
iter::Mappings {
document: self.mesh.document,
iter,
}
}
/// Constructs the primitive reader.
#[cfg(feature = "utils")]
#[cfg_attr(docsrs, doc(cfg(feature = "utils")))]
pub fn reader<'s, F>(&'a self, get_buffer_data: F) -> Reader<'a, 's, F>
where
F: Clone + Fn(Buffer<'a>) -> Option<&'s [u8]>,
{
Reader {
primitive: self,
get_buffer_data,
}
}
}
#[cfg(feature = "utils")]
impl<'a, 's, F> Reader<'a, 's, F>
where
F: Clone + Fn(Buffer<'a>) -> Option<&'s [u8]>,
{
/// Visits the vertex positions of a primitive.
pub fn read_positions(&self) -> Option<util::ReadPositions<'s>> {
self.primitive
.get(&Semantic::Positions)
.and_then(|accessor| accessor::Iter::new(accessor, self.get_buffer_data.clone()))
}
/// Visits the vertex normals of a primitive.
pub fn read_normals(&self) -> Option<util::ReadNormals<'s>> {
self.primitive
.get(&Semantic::Normals)
.and_then(|accessor| accessor::Iter::new(accessor, self.get_buffer_data.clone()))
}
/// Visits the vertex tangents of a primitive.
pub fn read_tangents(&self) -> Option<util::ReadTangents<'s>> {
self.primitive
.get(&Semantic::Tangents)
.and_then(|accessor| accessor::Iter::new(accessor, self.get_buffer_data.clone()))
}
/// Visits the vertex colors of a primitive.
pub fn read_colors(&self, set: u32) -> Option<util::ReadColors<'s>> {
use self::util::ReadColors;
use accessor::DataType::{F32, U16, U8};
use accessor::Dimensions::{Vec3, Vec4};
self.primitive
.get(&Semantic::Colors(set))
.and_then(
|accessor| match (accessor.data_type(), accessor.dimensions()) {
(U8, Vec3) => accessor::Iter::new(accessor, self.get_buffer_data.clone())
.map(ReadColors::RgbU8),
(U16, Vec3) => accessor::Iter::new(accessor, self.get_buffer_data.clone())
.map(ReadColors::RgbU16),
(F32, Vec3) => accessor::Iter::new(accessor, self.get_buffer_data.clone())
.map(ReadColors::RgbF32),
(U8, Vec4) => accessor::Iter::new(accessor, self.get_buffer_data.clone())
.map(ReadColors::RgbaU8),
(U16, Vec4) => accessor::Iter::new(accessor, self.get_buffer_data.clone())
.map(ReadColors::RgbaU16),
(F32, Vec4) => accessor::Iter::new(accessor, self.get_buffer_data.clone())
.map(ReadColors::RgbaF32),
_ => unreachable!(),
},
)
}
/// Visits the vertex draw sequence of a primitive.
pub fn read_indices(&self) -> Option<util::ReadIndices<'s>> {
use self::util::ReadIndices;
use accessor::DataType;
self.primitive
.indices()
.and_then(|accessor| match accessor.data_type() {
DataType::U8 => {
accessor::Iter::new(accessor, self.get_buffer_data.clone()).map(ReadIndices::U8)
}
DataType::U16 => accessor::Iter::new(accessor, self.get_buffer_data.clone())
.map(ReadIndices::U16),
DataType::U32 => accessor::Iter::new(accessor, self.get_buffer_data.clone())
.map(ReadIndices::U32),
_ => unreachable!(),
})
}
/// Visits the joint indices of the primitive.
pub fn read_joints(&self, set: u32) -> Option<util::ReadJoints<'s>> {
use self::util::ReadJoints;
use accessor::DataType;
self.primitive
.get(&Semantic::Joints(set))
.and_then(|accessor| match accessor.data_type() {
DataType::U8 => {
accessor::Iter::new(accessor, self.get_buffer_data.clone()).map(ReadJoints::U8)
}
DataType::U16 => {
accessor::Iter::new(accessor, self.get_buffer_data.clone()).map(ReadJoints::U16)
}
_ => unreachable!(),
})
}
/// Visits the vertex texture co-ordinates of a primitive.
pub fn read_tex_coords(&self, set: u32) -> Option<util::ReadTexCoords<'s>> {
use self::util::ReadTexCoords;
use accessor::DataType;
self.primitive
.get(&Semantic::TexCoords(set))
.and_then(|accessor| match accessor.data_type() {
DataType::U8 => accessor::Iter::new(accessor, self.get_buffer_data.clone())
.map(ReadTexCoords::U8),
DataType::U16 => accessor::Iter::new(accessor, self.get_buffer_data.clone())
.map(ReadTexCoords::U16),
DataType::F32 => accessor::Iter::new(accessor, self.get_buffer_data.clone())
.map(ReadTexCoords::F32),
_ => unreachable!(),
})
}
/// Visits the joint weights of the primitive.
pub fn read_weights(&self, set: u32) -> Option<util::ReadWeights<'s>> {
use self::accessor::DataType;
use self::util::ReadWeights;
self.primitive
.get(&Semantic::Weights(set))
.and_then(|accessor| match accessor.data_type() {
DataType::U8 => {
accessor::Iter::new(accessor, self.get_buffer_data.clone()).map(ReadWeights::U8)
}
DataType::U16 => accessor::Iter::new(accessor, self.get_buffer_data.clone())
.map(ReadWeights::U16),
DataType::F32 => accessor::Iter::new(accessor, self.get_buffer_data.clone())
.map(ReadWeights::F32),
_ => unreachable!(),
})
}
/// Visits the morph targets of the primitive.
pub fn read_morph_targets(&self) -> util::ReadMorphTargets<'a, 's, F> {
util::ReadMorphTargets {
index: 0,
reader: self.clone(),
}
}
}
impl<'a> MorphTarget<'a> {
/// Returns the XYZ vertex position displacements.
pub fn positions(&self) -> Option<Accessor<'a>> {
self.positions.clone()
}
/// Returns the XYZ vertex normal displacements.
pub fn normals(&self) -> Option<Accessor<'a>> {
self.normals.clone()
}
/// Returns the XYZ vertex tangent displacements.
pub fn tangents(&self) -> Option<Accessor<'a>> {
self.tangents.clone()
}
}

337
vendor/gltf/src/mesh/util/colors.rs vendored Normal file
View File

@@ -0,0 +1,337 @@
use std::marker::PhantomData;
use crate::Normalize;
use super::ReadColors;
/// Casting iterator for `Colors`.
#[derive(Clone, Debug)]
pub struct CastingIter<'a, T>(ReadColors<'a>, PhantomData<T>);
/// Type which describes how to cast any color into RGB u8.
#[derive(Clone, Debug)]
pub struct RgbU8;
/// Type which describes how to cast any color into RGB u16.
#[derive(Clone, Debug)]
pub struct RgbU16;
/// Type which describes how to cast any color into RGB f32.
#[derive(Clone, Debug)]
pub struct RgbF32;
/// Type which describes how to cast any color into RGBA u8.
#[derive(Clone, Debug)]
pub struct RgbaU8;
/// Type which describes how to cast any color into RGBA u16.
#[derive(Clone, Debug)]
pub struct RgbaU16;
/// Type which describes how to cast any color into RGBA f32.
#[derive(Clone, Debug)]
pub struct RgbaF32;
trait ColorChannel {
fn max_color() -> Self;
}
impl ColorChannel for u8 {
fn max_color() -> Self {
u8::max_value()
}
}
impl ColorChannel for u16 {
fn max_color() -> Self {
u16::max_value()
}
}
impl ColorChannel for f32 {
fn max_color() -> Self {
1.0
}
}
trait ColorArray<T> {
fn into_rgb(self) -> [T; 3];
fn into_rgba(self) -> [T; 4];
}
impl<T: Copy + ColorChannel> ColorArray<T> for [T; 3] {
fn into_rgb(self) -> [T; 3] {
self
}
fn into_rgba(self) -> [T; 4] {
[self[0], self[1], self[2], T::max_color()]
}
}
impl<T: Copy + ColorChannel> ColorArray<T> for [T; 4] {
fn into_rgb(self) -> [T; 3] {
[self[0], self[1], self[2]]
}
fn into_rgba(self) -> [T; 4] {
self
}
}
/// Trait for types which describe casting behaviour.
pub trait Cast {
/// Output type.
type Output;
/// Cast from RGB u8.
fn cast_rgb_u8(x: [u8; 3]) -> Self::Output;
/// Cast from RGB u16.
fn cast_rgb_u16(x: [u16; 3]) -> Self::Output;
/// Cast from RGB f32.
fn cast_rgb_f32(x: [f32; 3]) -> Self::Output;
/// Cast from RGBA u8.
fn cast_rgba_u8(x: [u8; 4]) -> Self::Output;
/// Cast from RGBA u16.
fn cast_rgba_u16(x: [u16; 4]) -> Self::Output;
/// Cast from RGBA f32.
fn cast_rgba_f32(x: [f32; 4]) -> Self::Output;
}
impl<'a, A> CastingIter<'a, A> {
pub(crate) fn new(iter: ReadColors<'a>) -> Self {
CastingIter(iter, PhantomData)
}
/// Unwrap underlying `ReadColors` object.
pub fn unwrap(self) -> ReadColors<'a> {
self.0
}
}
impl<'a, A: Cast> ExactSizeIterator for CastingIter<'a, A> {}
impl<'a, A: Cast> Iterator for CastingIter<'a, A> {
type Item = A::Output;
#[inline]
fn next(&mut self) -> Option<Self::Item> {
match self.0 {
ReadColors::RgbU8(ref mut i) => i.next().map(A::cast_rgb_u8),
ReadColors::RgbU16(ref mut i) => i.next().map(A::cast_rgb_u16),
ReadColors::RgbF32(ref mut i) => i.next().map(A::cast_rgb_f32),
ReadColors::RgbaU8(ref mut i) => i.next().map(A::cast_rgba_u8),
ReadColors::RgbaU16(ref mut i) => i.next().map(A::cast_rgba_u16),
ReadColors::RgbaF32(ref mut i) => i.next().map(A::cast_rgba_f32),
}
}
#[inline]
fn nth(&mut self, x: usize) -> Option<Self::Item> {
match self.0 {
ReadColors::RgbU8(ref mut i) => i.nth(x).map(A::cast_rgb_u8),
ReadColors::RgbU16(ref mut i) => i.nth(x).map(A::cast_rgb_u16),
ReadColors::RgbF32(ref mut i) => i.nth(x).map(A::cast_rgb_f32),
ReadColors::RgbaU8(ref mut i) => i.nth(x).map(A::cast_rgba_u8),
ReadColors::RgbaU16(ref mut i) => i.nth(x).map(A::cast_rgba_u16),
ReadColors::RgbaF32(ref mut i) => i.nth(x).map(A::cast_rgba_f32),
}
}
fn last(self) -> Option<Self::Item> {
match self.0 {
ReadColors::RgbU8(i) => i.last().map(A::cast_rgb_u8),
ReadColors::RgbU16(i) => i.last().map(A::cast_rgb_u16),
ReadColors::RgbF32(i) => i.last().map(A::cast_rgb_f32),
ReadColors::RgbaU8(i) => i.last().map(A::cast_rgba_u8),
ReadColors::RgbaU16(i) => i.last().map(A::cast_rgba_u16),
ReadColors::RgbaF32(i) => i.last().map(A::cast_rgba_f32),
}
}
fn count(self) -> usize {
self.size_hint().0
}
#[inline]
fn size_hint(&self) -> (usize, Option<usize>) {
match self.0 {
ReadColors::RgbU8(ref i) => i.size_hint(),
ReadColors::RgbU16(ref i) => i.size_hint(),
ReadColors::RgbF32(ref i) => i.size_hint(),
ReadColors::RgbaU8(ref i) => i.size_hint(),
ReadColors::RgbaU16(ref i) => i.size_hint(),
ReadColors::RgbaF32(ref i) => i.size_hint(),
}
}
}
impl Cast for RgbU8 {
type Output = [u8; 3];
fn cast_rgb_u8(x: [u8; 3]) -> Self::Output {
x.into_rgb().normalize()
}
fn cast_rgb_u16(x: [u16; 3]) -> Self::Output {
x.into_rgb().normalize()
}
fn cast_rgb_f32(x: [f32; 3]) -> Self::Output {
x.into_rgb().normalize()
}
fn cast_rgba_u8(x: [u8; 4]) -> Self::Output {
x.into_rgb().normalize()
}
fn cast_rgba_u16(x: [u16; 4]) -> Self::Output {
x.into_rgb().normalize()
}
fn cast_rgba_f32(x: [f32; 4]) -> Self::Output {
x.into_rgb().normalize()
}
}
impl Cast for RgbU16 {
type Output = [u16; 3];
fn cast_rgb_u8(x: [u8; 3]) -> Self::Output {
x.into_rgb().normalize()
}
fn cast_rgb_u16(x: [u16; 3]) -> Self::Output {
x.into_rgb().normalize()
}
fn cast_rgb_f32(x: [f32; 3]) -> Self::Output {
x.into_rgb().normalize()
}
fn cast_rgba_u8(x: [u8; 4]) -> Self::Output {
x.into_rgb().normalize()
}
fn cast_rgba_u16(x: [u16; 4]) -> Self::Output {
x.into_rgb().normalize()
}
fn cast_rgba_f32(x: [f32; 4]) -> Self::Output {
x.into_rgb().normalize()
}
}
impl Cast for RgbF32 {
type Output = [f32; 3];
fn cast_rgb_u8(x: [u8; 3]) -> Self::Output {
x.into_rgb().normalize()
}
fn cast_rgb_u16(x: [u16; 3]) -> Self::Output {
x.into_rgb().normalize()
}
fn cast_rgb_f32(x: [f32; 3]) -> Self::Output {
x.into_rgb().normalize()
}
fn cast_rgba_u8(x: [u8; 4]) -> Self::Output {
x.into_rgb().normalize()
}
fn cast_rgba_u16(x: [u16; 4]) -> Self::Output {
x.into_rgb().normalize()
}
fn cast_rgba_f32(x: [f32; 4]) -> Self::Output {
x.into_rgb().normalize()
}
}
impl Cast for RgbaU8 {
type Output = [u8; 4];
fn cast_rgb_u8(x: [u8; 3]) -> Self::Output {
x.normalize().into_rgba()
}
fn cast_rgb_u16(x: [u16; 3]) -> Self::Output {
x.normalize().into_rgba()
}
fn cast_rgb_f32(x: [f32; 3]) -> Self::Output {
x.normalize().into_rgba()
}
fn cast_rgba_u8(x: [u8; 4]) -> Self::Output {
x.normalize().into_rgba()
}
fn cast_rgba_u16(x: [u16; 4]) -> Self::Output {
x.normalize().into_rgba()
}
fn cast_rgba_f32(x: [f32; 4]) -> Self::Output {
x.normalize().into_rgba()
}
}
impl Cast for RgbaU16 {
type Output = [u16; 4];
fn cast_rgb_u8(x: [u8; 3]) -> Self::Output {
x.normalize().into_rgba()
}
fn cast_rgb_u16(x: [u16; 3]) -> Self::Output {
x.normalize().into_rgba()
}
fn cast_rgb_f32(x: [f32; 3]) -> Self::Output {
x.normalize().into_rgba()
}
fn cast_rgba_u8(x: [u8; 4]) -> Self::Output {
x.normalize().into_rgba()
}
fn cast_rgba_u16(x: [u16; 4]) -> Self::Output {
x.normalize().into_rgba()
}
fn cast_rgba_f32(x: [f32; 4]) -> Self::Output {
x.normalize().into_rgba()
}
}
impl Cast for RgbaF32 {
type Output = [f32; 4];
fn cast_rgb_u8(x: [u8; 3]) -> Self::Output {
x.normalize().into_rgba()
}
fn cast_rgb_u16(x: [u16; 3]) -> Self::Output {
x.normalize().into_rgba()
}
fn cast_rgb_f32(x: [f32; 3]) -> Self::Output {
x.normalize().into_rgba()
}
fn cast_rgba_u8(x: [u8; 4]) -> Self::Output {
x.normalize().into_rgba()
}
fn cast_rgba_u16(x: [u16; 4]) -> Self::Output {
x.normalize().into_rgba()
}
fn cast_rgba_f32(x: [f32; 4]) -> Self::Output {
x.normalize().into_rgba()
}
}

95
vendor/gltf/src/mesh/util/indices.rs vendored Normal file
View File

@@ -0,0 +1,95 @@
use std::marker::PhantomData;
use super::ReadIndices;
/// Casting iterator for `Indices`.
#[derive(Clone, Debug)]
pub struct CastingIter<'a, T>(ReadIndices<'a>, PhantomData<T>);
/// Type which describes how to cast any index into u32.
#[derive(Clone, Debug)]
pub struct U32;
/// Trait for types which describe casting behaviour.
pub trait Cast {
/// Output type.
type Output;
/// Cast from u8.
fn cast_u8(x: u8) -> Self::Output;
/// Cast from u16.
fn cast_u16(x: u16) -> Self::Output;
/// Cast from u32.
fn cast_u32(x: u32) -> Self::Output;
}
impl<'a, A> CastingIter<'a, A> {
pub(crate) fn new(iter: ReadIndices<'a>) -> Self {
CastingIter(iter, PhantomData)
}
/// Unwrap underlying `Indices` object.
pub fn unwrap(self) -> ReadIndices<'a> {
self.0
}
}
impl<'a, A: Cast> ExactSizeIterator for CastingIter<'a, A> {}
impl<'a, A: Cast> Iterator for CastingIter<'a, A> {
type Item = A::Output;
#[inline]
fn next(&mut self) -> Option<Self::Item> {
match self.0 {
ReadIndices::U8(ref mut i) => i.next().map(A::cast_u8),
ReadIndices::U16(ref mut i) => i.next().map(A::cast_u16),
ReadIndices::U32(ref mut i) => i.next().map(A::cast_u32),
}
}
#[inline]
fn nth(&mut self, x: usize) -> Option<Self::Item> {
match self.0 {
ReadIndices::U8(ref mut i) => i.nth(x).map(A::cast_u8),
ReadIndices::U16(ref mut i) => i.nth(x).map(A::cast_u16),
ReadIndices::U32(ref mut i) => i.nth(x).map(A::cast_u32),
}
}
fn last(self) -> Option<Self::Item> {
match self.0 {
ReadIndices::U8(i) => i.last().map(A::cast_u8),
ReadIndices::U16(i) => i.last().map(A::cast_u16),
ReadIndices::U32(i) => i.last().map(A::cast_u32),
}
}
fn count(self) -> usize {
self.size_hint().0
}
#[inline]
fn size_hint(&self) -> (usize, Option<usize>) {
match self.0 {
ReadIndices::U8(ref i) => i.size_hint(),
ReadIndices::U16(ref i) => i.size_hint(),
ReadIndices::U32(ref i) => i.size_hint(),
}
}
}
impl Cast for U32 {
type Output = u32;
fn cast_u8(x: u8) -> Self::Output {
x as Self::Output
}
fn cast_u16(x: u16) -> Self::Output {
x as Self::Output
}
fn cast_u32(x: u32) -> Self::Output {
x
}
}

86
vendor/gltf/src/mesh/util/joints.rs vendored Normal file
View File

@@ -0,0 +1,86 @@
use std::marker::PhantomData;
use super::ReadJoints;
/// Casting iterator for `Joints`.
#[derive(Clone, Debug)]
pub struct CastingIter<'a, T>(ReadJoints<'a>, PhantomData<T>);
/// Type which describes how to cast any joint into u16.
#[derive(Clone, Debug)]
pub struct U16;
/// Trait for types which describe casting behaviour.
pub trait Cast {
/// Output type.
type Output;
/// Cast from u8.
fn cast_u8(x: [u8; 4]) -> Self::Output;
/// Cast from u16.
fn cast_u16(x: [u16; 4]) -> Self::Output;
}
impl<'a, A> CastingIter<'a, A> {
pub(crate) fn new(iter: ReadJoints<'a>) -> Self {
CastingIter(iter, PhantomData)
}
/// Unwrap underlying `Joints` object.
pub fn unwrap(self) -> ReadJoints<'a> {
self.0
}
}
impl<'a, A: Cast> ExactSizeIterator for CastingIter<'a, A> {}
impl<'a, A: Cast> Iterator for CastingIter<'a, A> {
type Item = A::Output;
#[inline]
fn next(&mut self) -> Option<Self::Item> {
match self.0 {
ReadJoints::U8(ref mut i) => i.next().map(A::cast_u8),
ReadJoints::U16(ref mut i) => i.next().map(A::cast_u16),
}
}
#[inline]
fn nth(&mut self, x: usize) -> Option<Self::Item> {
match self.0 {
ReadJoints::U8(ref mut i) => i.nth(x).map(A::cast_u8),
ReadJoints::U16(ref mut i) => i.nth(x).map(A::cast_u16),
}
}
fn last(self) -> Option<Self::Item> {
match self.0 {
ReadJoints::U8(i) => i.last().map(A::cast_u8),
ReadJoints::U16(i) => i.last().map(A::cast_u16),
}
}
fn count(self) -> usize {
self.size_hint().0
}
#[inline]
fn size_hint(&self) -> (usize, Option<usize>) {
match self.0 {
ReadJoints::U8(ref i) => i.size_hint(),
ReadJoints::U16(ref i) => i.size_hint(),
}
}
}
impl Cast for U16 {
type Output = [u16; 4];
fn cast_u8(x: [u8; 4]) -> Self::Output {
[x[0] as u16, x[1] as u16, x[2] as u16, x[3] as u16]
}
fn cast_u16(x: [u16; 4]) -> Self::Output {
x
}
}

242
vendor/gltf/src/mesh/util/mod.rs vendored Normal file
View File

@@ -0,0 +1,242 @@
/// Casting iterator adapters for colors.
pub mod colors;
/// Casting iterator adapters for vertex indices.
pub mod indices;
/// Casting iterator adapters for joint indices.
pub mod joints;
/// Casting iterator adapters for texture co-ordinates.
pub mod tex_coords;
/// Casting iterator adapters for node weights.
pub mod weights;
use crate::mesh;
use crate::accessor::Iter;
use crate::Buffer;
/// XYZ vertex positions of type `[f32; 3]`.
pub type ReadPositions<'a> = Iter<'a, [f32; 3]>;
/// XYZ vertex normals of type `[f32; 3]`.
pub type ReadNormals<'a> = Iter<'a, [f32; 3]>;
/// XYZW vertex tangents of type `[f32; 4]` where the `w` component is a
/// sign value (-1 or +1) indicating the handedness of the tangent basis.
pub type ReadTangents<'a> = Iter<'a, [f32; 4]>;
/// XYZ vertex position displacements of type `[f32; 3]`.
pub type ReadPositionDisplacements<'a> = Iter<'a, [f32; 3]>;
/// XYZ vertex normal displacements of type `[f32; 3]`.
pub type ReadNormalDisplacements<'a> = Iter<'a, [f32; 3]>;
/// XYZ vertex tangent displacements.
pub type ReadTangentDisplacements<'a> = Iter<'a, [f32; 3]>;
/// Vertex colors.
#[derive(Clone, Debug)]
pub enum ReadColors<'a> {
/// RGB vertex color of type `[u8; 3]>`.
RgbU8(Iter<'a, [u8; 3]>),
/// RGB vertex color of type `[u16; 3]>`.
RgbU16(Iter<'a, [u16; 3]>),
/// RGB vertex color of type `[f32; 3]`.
RgbF32(Iter<'a, [f32; 3]>),
/// RGBA vertex color of type `[u8; 4]>`.
RgbaU8(Iter<'a, [u8; 4]>),
/// RGBA vertex color of type `[u16; 4]>`.
RgbaU16(Iter<'a, [u16; 4]>),
/// RGBA vertex color of type `[f32; 4]`.
RgbaF32(Iter<'a, [f32; 4]>),
}
/// Index data.
#[derive(Clone, Debug)]
pub enum ReadIndices<'a> {
/// Index data of type U8
U8(Iter<'a, u8>),
/// Index data of type U16
U16(Iter<'a, u16>),
/// Index data of type U32
U32(Iter<'a, u32>),
}
/// Vertex joints.
#[derive(Clone, Debug)]
pub enum ReadJoints<'a> {
/// Joints of type `[u8; 4]`.
/// Refer to the documentation on morph targets and skins for more
/// information.
U8(Iter<'a, [u8; 4]>),
/// Joints of type `[u16; 4]`.
/// Refer to the documentation on morph targets and skins for more
/// information.
U16(Iter<'a, [u16; 4]>),
}
/// UV texture co-ordinates.
#[derive(Clone, Debug)]
pub enum ReadTexCoords<'a> {
/// UV texture co-ordinates of type `[u8; 2]>`.
U8(Iter<'a, [u8; 2]>),
/// UV texture co-ordinates of type `[u16; 2]>`.
U16(Iter<'a, [u16; 2]>),
/// UV texture co-ordinates of type `[f32; 2]`.
F32(Iter<'a, [f32; 2]>),
}
/// Weights.
#[derive(Clone, Debug)]
pub enum ReadWeights<'a> {
/// Weights of type `[u8; 4]`.
U8(Iter<'a, [u8; 4]>),
/// Weights of type `[u16; 4]`.
U16(Iter<'a, [u16; 4]>),
/// Weights of type `[f32; 4]`.
F32(Iter<'a, [f32; 4]>),
}
/// Morph targets.
#[derive(Clone, Debug)]
pub struct ReadMorphTargets<'a, 's, F>
where
F: Clone + Fn(Buffer<'a>) -> Option<&'s [u8]>,
{
pub(crate) index: usize,
pub(crate) reader: mesh::Reader<'a, 's, F>,
}
impl<'a, 's, F> ExactSizeIterator for ReadMorphTargets<'a, 's, F> where
F: Clone + Fn(Buffer<'a>) -> Option<&'s [u8]>
{
}
impl<'a, 's, F> Iterator for ReadMorphTargets<'a, 's, F>
where
F: Clone + Fn(Buffer<'a>) -> Option<&'s [u8]>,
{
type Item = (
Option<ReadPositionDisplacements<'s>>,
Option<ReadNormalDisplacements<'s>>,
Option<ReadTangentDisplacements<'s>>,
);
fn next(&mut self) -> Option<Self::Item> {
self.index += 1;
self.reader
.primitive
.morph_targets()
.nth(self.index - 1)
.map(|morph_target| {
let positions = morph_target
.positions()
.and_then(|accessor| Iter::new(accessor, self.reader.get_buffer_data.clone()));
let normals = morph_target
.normals()
.and_then(|accessor| Iter::new(accessor, self.reader.get_buffer_data.clone()));
let tangents = morph_target
.tangents()
.and_then(|accessor| Iter::new(accessor, self.reader.get_buffer_data.clone()));
(positions, normals, tangents)
})
}
fn size_hint(&self) -> (usize, Option<usize>) {
self.reader.primitive.morph_targets().size_hint()
}
}
impl<'a> ReadColors<'a> {
/// Reinterpret colors as RGB u8, discarding alpha, if present. Lossy if
/// the underlying iterator yields u16, f32 or any RGBA.
pub fn into_rgb_u8(self) -> self::colors::CastingIter<'a, self::colors::RgbU8> {
self::colors::CastingIter::new(self)
}
/// Reinterpret colors as RGB u16, discarding alpha, if present. Lossy if
/// the underlying iterator yields f32 or any RGBA.
pub fn into_rgb_u16(self) -> self::colors::CastingIter<'a, self::colors::RgbU16> {
self::colors::CastingIter::new(self)
}
/// Reinterpret colors as RGB f32, discarding alpha, if present. Lossy if
/// the underlying iterator yields u16 or any RGBA.
pub fn into_rgb_f32(self) -> self::colors::CastingIter<'a, self::colors::RgbF32> {
self::colors::CastingIter::new(self)
}
/// Reinterpret colors as RGBA u8, with default alpha 255. Lossy if the
/// underlying iterator yields u16 or f32.
pub fn into_rgba_u8(self) -> self::colors::CastingIter<'a, self::colors::RgbaU8> {
self::colors::CastingIter::new(self)
}
/// Reinterpret colors as RGBA u16, with default alpha 65535. Lossy if the
/// underlying iterator yields f32.
pub fn into_rgba_u16(self) -> self::colors::CastingIter<'a, self::colors::RgbaU16> {
self::colors::CastingIter::new(self)
}
/// Reinterpret colors as RGBA f32, with default alpha 1.0. Lossy if the
/// underlying iterator yields u16.
pub fn into_rgba_f32(self) -> self::colors::CastingIter<'a, self::colors::RgbaF32> {
self::colors::CastingIter::new(self)
}
}
impl<'a> ReadIndices<'a> {
/// Reinterpret indices as u32, which can fit any possible index.
pub fn into_u32(self) -> self::indices::CastingIter<'a, self::indices::U32> {
self::indices::CastingIter::new(self)
}
}
impl<'a> ReadJoints<'a> {
/// Reinterpret joints as u16, which can fit any possible joint.
pub fn into_u16(self) -> self::joints::CastingIter<'a, self::joints::U16> {
self::joints::CastingIter::new(self)
}
}
impl<'a> ReadTexCoords<'a> {
/// Reinterpret texture coordinates as u8. Lossy if the underlying iterator
/// yields u16 or f32.
pub fn into_u8(self) -> self::tex_coords::CastingIter<'a, self::tex_coords::U8> {
self::tex_coords::CastingIter::new(self)
}
/// Reinterpret texture coordinates as u16. Lossy if the underlying
/// iterator yields f32.
pub fn into_u16(self) -> self::tex_coords::CastingIter<'a, self::tex_coords::U16> {
self::tex_coords::CastingIter::new(self)
}
/// Reinterpret texture coordinates as f32. Lossy if the underlying
/// iterator yields u16.
pub fn into_f32(self) -> self::tex_coords::CastingIter<'a, self::tex_coords::F32> {
self::tex_coords::CastingIter::new(self)
}
}
impl<'a> ReadWeights<'a> {
/// Reinterpret weights as u8. Lossy if the underlying iterator yields u16
/// or f32.
pub fn into_u8(self) -> self::weights::CastingIter<'a, self::weights::U8> {
self::weights::CastingIter::new(self)
}
/// Reinterpret weights as u16. Lossy if the underlying iterator yields
/// f32.
pub fn into_u16(self) -> self::weights::CastingIter<'a, self::weights::U16> {
self::weights::CastingIter::new(self)
}
/// Reinterpret weights as f32. Lossy if the underlying iterator yields
/// u16.
pub fn into_f32(self) -> self::weights::CastingIter<'a, self::weights::F32> {
self::weights::CastingIter::new(self)
}
}

139
vendor/gltf/src/mesh/util/tex_coords.rs vendored Normal file
View File

@@ -0,0 +1,139 @@
use std::marker::PhantomData;
use crate::Normalize;
use super::ReadTexCoords;
/// Casting iterator for `TexCoords`.
#[derive(Clone, Debug)]
pub struct CastingIter<'a, T>(ReadTexCoords<'a>, PhantomData<T>);
/// Type which describes how to cast any texture coordinate into pair of u8.
#[derive(Clone, Debug)]
pub struct U8;
/// Type which describes how to cast any texture coordinate into pair of u16.
#[derive(Clone, Debug)]
pub struct U16;
/// Type which describes how to cast any texture coordinate into pair of f32.
#[derive(Clone, Debug)]
pub struct F32;
/// Trait for types which describe casting behaviour.
pub trait Cast {
/// Output type.
type Output;
/// Cast from u8 pair.
fn cast_u8(x: [u8; 2]) -> Self::Output;
/// Cast from u16 pair.
fn cast_u16(x: [u16; 2]) -> Self::Output;
/// Cast from f32 pair.
fn cast_f32(x: [f32; 2]) -> Self::Output;
}
impl<'a, A> CastingIter<'a, A> {
pub(crate) fn new(iter: ReadTexCoords<'a>) -> Self {
CastingIter(iter, PhantomData)
}
/// Unwrap underlying `TexCoords` object.
pub fn unwrap(self) -> ReadTexCoords<'a> {
self.0
}
}
impl<'a, A: Cast> ExactSizeIterator for CastingIter<'a, A> {}
impl<'a, A: Cast> Iterator for CastingIter<'a, A> {
type Item = A::Output;
#[inline]
fn next(&mut self) -> Option<Self::Item> {
match self.0 {
ReadTexCoords::U8(ref mut i) => i.next().map(A::cast_u8),
ReadTexCoords::U16(ref mut i) => i.next().map(A::cast_u16),
ReadTexCoords::F32(ref mut i) => i.next().map(A::cast_f32),
}
}
#[inline]
fn nth(&mut self, x: usize) -> Option<Self::Item> {
match self.0 {
ReadTexCoords::U8(ref mut i) => i.nth(x).map(A::cast_u8),
ReadTexCoords::U16(ref mut i) => i.nth(x).map(A::cast_u16),
ReadTexCoords::F32(ref mut i) => i.nth(x).map(A::cast_f32),
}
}
fn last(self) -> Option<Self::Item> {
match self.0 {
ReadTexCoords::U8(i) => i.last().map(A::cast_u8),
ReadTexCoords::U16(i) => i.last().map(A::cast_u16),
ReadTexCoords::F32(i) => i.last().map(A::cast_f32),
}
}
fn count(self) -> usize {
self.size_hint().0
}
#[inline]
fn size_hint(&self) -> (usize, Option<usize>) {
match self.0 {
ReadTexCoords::U8(ref i) => i.size_hint(),
ReadTexCoords::U16(ref i) => i.size_hint(),
ReadTexCoords::F32(ref i) => i.size_hint(),
}
}
}
impl Cast for U8 {
type Output = [u8; 2];
fn cast_u8(x: [u8; 2]) -> Self::Output {
x.normalize()
}
fn cast_u16(x: [u16; 2]) -> Self::Output {
x.normalize()
}
fn cast_f32(x: [f32; 2]) -> Self::Output {
x.normalize()
}
}
impl Cast for U16 {
type Output = [u16; 2];
fn cast_u8(x: [u8; 2]) -> Self::Output {
x.normalize()
}
fn cast_u16(x: [u16; 2]) -> Self::Output {
x.normalize()
}
fn cast_f32(x: [f32; 2]) -> Self::Output {
x.normalize()
}
}
impl Cast for F32 {
type Output = [f32; 2];
fn cast_u8(x: [u8; 2]) -> Self::Output {
x.normalize()
}
fn cast_u16(x: [u16; 2]) -> Self::Output {
x.normalize()
}
fn cast_f32(x: [f32; 2]) -> Self::Output {
x.normalize()
}
}

139
vendor/gltf/src/mesh/util/weights.rs vendored Normal file
View File

@@ -0,0 +1,139 @@
use std::marker::PhantomData;
use crate::Normalize;
use super::ReadWeights;
/// Casting iterator for `Weights`.
#[derive(Clone, Debug)]
pub struct CastingIter<'a, T>(ReadWeights<'a>, PhantomData<T>);
/// Type which describes how to cast any weight into u8.
#[derive(Clone, Debug)]
pub struct U8;
/// Type which describes how to cast any weight into u16.
#[derive(Clone, Debug)]
pub struct U16;
/// Type which describes how to cast any weight into f32.
#[derive(Clone, Debug)]
pub struct F32;
/// Trait for types which describe casting behaviour.
pub trait Cast {
/// Output type.
type Output;
/// Cast from u8.
fn cast_u8(x: [u8; 4]) -> Self::Output;
/// Cast from u16.
fn cast_u16(x: [u16; 4]) -> Self::Output;
/// Cast from f32.
fn cast_f32(x: [f32; 4]) -> Self::Output;
}
impl<'a, A> CastingIter<'a, A> {
pub(crate) fn new(iter: ReadWeights<'a>) -> Self {
CastingIter(iter, PhantomData)
}
/// Unwrap underlying `Weights` object.
pub fn unwrap(self) -> ReadWeights<'a> {
self.0
}
}
impl<'a, A: Cast> ExactSizeIterator for CastingIter<'a, A> {}
impl<'a, A: Cast> Iterator for CastingIter<'a, A> {
type Item = A::Output;
#[inline]
fn next(&mut self) -> Option<Self::Item> {
match self.0 {
ReadWeights::U8(ref mut i) => i.next().map(A::cast_u8),
ReadWeights::U16(ref mut i) => i.next().map(A::cast_u16),
ReadWeights::F32(ref mut i) => i.next().map(A::cast_f32),
}
}
#[inline]
fn nth(&mut self, x: usize) -> Option<Self::Item> {
match self.0 {
ReadWeights::U8(ref mut i) => i.nth(x).map(A::cast_u8),
ReadWeights::U16(ref mut i) => i.nth(x).map(A::cast_u16),
ReadWeights::F32(ref mut i) => i.nth(x).map(A::cast_f32),
}
}
fn last(self) -> Option<Self::Item> {
match self.0 {
ReadWeights::U8(i) => i.last().map(A::cast_u8),
ReadWeights::U16(i) => i.last().map(A::cast_u16),
ReadWeights::F32(i) => i.last().map(A::cast_f32),
}
}
fn count(self) -> usize {
self.size_hint().0
}
#[inline]
fn size_hint(&self) -> (usize, Option<usize>) {
match self.0 {
ReadWeights::U8(ref i) => i.size_hint(),
ReadWeights::U16(ref i) => i.size_hint(),
ReadWeights::F32(ref i) => i.size_hint(),
}
}
}
impl Cast for U8 {
type Output = [u8; 4];
fn cast_u8(x: [u8; 4]) -> Self::Output {
x.normalize()
}
fn cast_u16(x: [u16; 4]) -> Self::Output {
x.normalize()
}
fn cast_f32(x: [f32; 4]) -> Self::Output {
x.normalize()
}
}
impl Cast for U16 {
type Output = [u16; 4];
fn cast_u8(x: [u8; 4]) -> Self::Output {
x.normalize()
}
fn cast_u16(x: [u16; 4]) -> Self::Output {
x.normalize()
}
fn cast_f32(x: [f32; 4]) -> Self::Output {
x.normalize()
}
}
impl Cast for F32 {
type Output = [f32; 4];
fn cast_u8(x: [u8; 4]) -> Self::Output {
x.normalize()
}
fn cast_u16(x: [u16; 4]) -> Self::Output {
x.normalize()
}
fn cast_f32(x: [f32; 4]) -> Self::Output {
x.normalize()
}
}