Add a PVec3 type, for Padded Vector 3d.
This is the same as Vec3 but with an extra field for padding. This is useful for applications needing a power-of-two number of arguments (typically mixed CPU/GPU applications). The corresponding matrix is not yet implemented.
This commit is contained in:
parent
346a803b7f
commit
eb4e4a6aeb
54
src/vec.rs
54
src/vec.rs
|
@ -145,6 +145,60 @@ translate_impl!(Vec3)
|
|||
rotate_impl!(Vec3)
|
||||
transform_impl!(Vec3)
|
||||
|
||||
|
||||
/// Vector of dimension 3 with an extra component for padding.
|
||||
#[deriving(Eq, Encodable, Decodable, Clone, DeepClone, IterBytes, Rand, Zero, ToStr)]
|
||||
pub struct PVec3<N> {
|
||||
/// First component of the vector.
|
||||
x: N,
|
||||
/// Second component of the vector.
|
||||
y: N,
|
||||
/// Third component of the vector.
|
||||
z: N,
|
||||
// Unused component, for padding
|
||||
priv _unused: N
|
||||
}
|
||||
|
||||
impl<N: Clone> PVec3<N> {
|
||||
/// Creates a new 3d vector.
|
||||
pub fn new(x: N, y: N, z: N) -> PVec3<N> {
|
||||
PVec3 { x: x.clone(), y: y, z: z, _unused: x }
|
||||
}
|
||||
}
|
||||
|
||||
ord_impl!(PVec3, x, y, z)
|
||||
orderable_impl!(PVec3, x, y, z)
|
||||
vec_axis_impl!(PVec3, x, y, z)
|
||||
vec_cast_impl!(PVec3, x, y, z)
|
||||
indexable_impl!(PVec3, 3)
|
||||
new_repeat_impl!(PVec3, val, x, y, z, _unused)
|
||||
dim_impl!(PVec3, 3)
|
||||
// (specialized) basis_impl!(PVec3, 1)
|
||||
add_impl!(PVec3, x, y, z)
|
||||
sub_impl!(PVec3, x, y, z)
|
||||
neg_impl!(PVec3, x, y, z)
|
||||
dot_impl!(PVec3, x, y, z)
|
||||
scalar_mul_impl!(PVec3, x, y, z)
|
||||
scalar_div_impl!(PVec3, x, y, z)
|
||||
scalar_add_impl!(PVec3, x, y, z)
|
||||
scalar_sub_impl!(PVec3, x, y, z)
|
||||
translation_impl!(PVec3)
|
||||
norm_impl!(PVec3)
|
||||
approx_eq_impl!(PVec3, x, y, z)
|
||||
round_impl!(PVec3, x, y, z)
|
||||
one_impl!(PVec3)
|
||||
from_iterator_impl!(PVec3, iterator, iterator, iterator)
|
||||
bounded_impl!(PVec3)
|
||||
iterable_impl!(PVec3, 3)
|
||||
iterable_mut_impl!(PVec3, 3)
|
||||
to_homogeneous_impl!(PVec3, Vec4, w, x, y, z)
|
||||
from_homogeneous_impl!(PVec3, Vec4, w, x, y, z)
|
||||
translate_impl!(PVec3)
|
||||
rotate_impl!(PVec3)
|
||||
transform_impl!(PVec3)
|
||||
|
||||
|
||||
|
||||
/// Vector of dimension 4.
|
||||
#[deriving(Eq, Encodable, Decodable, Clone, DeepClone, IterBytes, Rand, Zero, ToStr)]
|
||||
pub struct Vec4<N> {
|
||||
|
|
|
@ -43,7 +43,7 @@ macro_rules! ord_impl(
|
|||
|
||||
macro_rules! orderable_impl(
|
||||
($t: ident, $comp0: ident $(,$compN: ident)*) => (
|
||||
impl<N: Orderable> Orderable for $t<N> {
|
||||
impl<N: Clone + Orderable> Orderable for $t<N> {
|
||||
#[inline]
|
||||
fn max(&self, other: &$t<N>) -> $t<N> {
|
||||
$t::new(self.$comp0.max(&other.$comp0) $(, self.$compN.max(&other.$compN))*)
|
||||
|
@ -93,7 +93,7 @@ macro_rules! vec_axis_impl(
|
|||
|
||||
macro_rules! vec_cast_impl(
|
||||
($t: ident, $comp0: ident $(,$compN: ident)*) => (
|
||||
impl<Nin: NumCast + Clone, Nout: NumCast> VecCast<$t<Nout>> for $t<Nin> {
|
||||
impl<Nin: NumCast + Clone, Nout: Clone + NumCast> VecCast<$t<Nout>> for $t<Nin> {
|
||||
#[inline]
|
||||
fn from(v: $t<Nin>) -> $t<Nout> {
|
||||
$t::new(NumCast::from(v.$comp0.clone()) $(, NumCast::from(v.$compN.clone()))*)
|
||||
|
@ -233,7 +233,7 @@ macro_rules! basis_impl(
|
|||
|
||||
macro_rules! add_impl(
|
||||
($t: ident, $comp0: ident $(,$compN: ident)*) => (
|
||||
impl<N: Add<N, N>> Add<$t<N>, $t<N>> for $t<N> {
|
||||
impl<N: Clone + Add<N, N>> Add<$t<N>, $t<N>> for $t<N> {
|
||||
#[inline]
|
||||
fn add(&self, other: &$t<N>) -> $t<N> {
|
||||
$t::new(self.$comp0 + other.$comp0 $(, self.$compN + other.$compN)*)
|
||||
|
@ -244,7 +244,7 @@ macro_rules! add_impl(
|
|||
|
||||
macro_rules! sub_impl(
|
||||
($t: ident, $comp0: ident $(,$compN: ident)*) => (
|
||||
impl<N: Sub<N, N>> Sub<$t<N>, $t<N>> for $t<N> {
|
||||
impl<N: Clone + Sub<N, N>> Sub<$t<N>, $t<N>> for $t<N> {
|
||||
#[inline]
|
||||
fn sub(&self, other: &$t<N>) -> $t<N> {
|
||||
$t::new(self.$comp0 - other.$comp0 $(, self.$compN - other.$compN)*)
|
||||
|
@ -255,7 +255,7 @@ macro_rules! sub_impl(
|
|||
|
||||
macro_rules! neg_impl(
|
||||
($t: ident, $comp0: ident $(,$compN: ident)*) => (
|
||||
impl<N: Neg<N>> Neg<$t<N>> for $t<N> {
|
||||
impl<N: Clone + Neg<N>> Neg<$t<N>> for $t<N> {
|
||||
#[inline]
|
||||
fn neg(&self) -> $t<N> {
|
||||
$t::new(-self.$comp0 $(, -self.$compN )*)
|
||||
|
@ -282,7 +282,7 @@ macro_rules! dot_impl(
|
|||
|
||||
macro_rules! scalar_mul_impl(
|
||||
($t: ident, $comp0: ident $(,$compN: ident)*) => (
|
||||
impl<N: Mul<N, N>> Mul<N, $t<N>> for $t<N> {
|
||||
impl<N: Clone + Mul<N, N>> Mul<N, $t<N>> for $t<N> {
|
||||
#[inline]
|
||||
fn mul(&self, s: &N) -> $t<N> {
|
||||
$t::new(self.$comp0 * *s $(, self.$compN * *s)*)
|
||||
|
@ -293,7 +293,7 @@ macro_rules! scalar_mul_impl(
|
|||
|
||||
macro_rules! scalar_div_impl(
|
||||
($t: ident, $comp0: ident $(,$compN: ident)*) => (
|
||||
impl<N: Div<N, N>> Div<N, $t<N>> for $t<N> {
|
||||
impl<N: Clone + Div<N, N>> Div<N, $t<N>> for $t<N> {
|
||||
#[inline]
|
||||
fn div(&self, s: &N) -> $t<N> {
|
||||
$t::new(self.$comp0 / *s $(, self.$compN / *s)*)
|
||||
|
@ -304,7 +304,7 @@ macro_rules! scalar_div_impl(
|
|||
|
||||
macro_rules! scalar_add_impl(
|
||||
($t: ident, $comp0: ident $(,$compN: ident)*) => (
|
||||
impl<N: Add<N, N>> ScalarAdd<N> for $t<N> {
|
||||
impl<N: Clone + Add<N, N>> ScalarAdd<N> for $t<N> {
|
||||
#[inline]
|
||||
fn scalar_add(&self, s: &N) -> $t<N> {
|
||||
$t::new(self.$comp0 + *s $(, self.$compN + *s)*)
|
||||
|
@ -321,7 +321,7 @@ macro_rules! scalar_add_impl(
|
|||
|
||||
macro_rules! scalar_sub_impl(
|
||||
($t: ident, $comp0: ident $(,$compN: ident)*) => (
|
||||
impl<N: Sub<N, N>> ScalarSub<N> for $t<N> {
|
||||
impl<N: Clone + Sub<N, N>> ScalarSub<N> for $t<N> {
|
||||
#[inline]
|
||||
fn scalar_sub(&self, s: &N) -> $t<N> {
|
||||
$t::new(self.$comp0 - *s $(, self.$compN - *s)*)
|
||||
|
@ -398,7 +398,7 @@ macro_rules! norm_impl(
|
|||
|
||||
macro_rules! round_impl(
|
||||
($t: ident, $comp0: ident $(,$compN: ident)*) => (
|
||||
impl<N: Round> Round for $t<N> {
|
||||
impl<N: Clone + Round> Round for $t<N> {
|
||||
fn floor(&self) -> $t<N> {
|
||||
$t::new(self.$comp0.floor() $(, self.$compN.floor())*)
|
||||
}
|
||||
|
@ -457,7 +457,7 @@ macro_rules! one_impl(
|
|||
|
||||
macro_rules! from_iterator_impl(
|
||||
($t: ident, $param0: ident $(, $paramN: ident)*) => (
|
||||
impl<N> FromIterator<N> for $t<N> {
|
||||
impl<N: Clone> FromIterator<N> for $t<N> {
|
||||
#[inline]
|
||||
fn from_iterator<I: Iterator<N>>($param0: &mut I) -> $t<N> {
|
||||
$t::new($param0.next().unwrap() $(, $paramN.next().unwrap())*)
|
||||
|
@ -516,7 +516,7 @@ macro_rules! from_homogeneous_impl(
|
|||
|
||||
macro_rules! translate_impl(
|
||||
($t: ident) => (
|
||||
impl<N: Add<N, N> + Sub<N, N>> Translate<$t<N>> for $t<N> {
|
||||
impl<N: Clone + Add<N, N> + Sub<N, N>> Translate<$t<N>> for $t<N> {
|
||||
fn translate(&self, other: &$t<N>) -> $t<N> {
|
||||
*other + *self
|
||||
}
|
||||
|
@ -544,7 +544,7 @@ macro_rules! rotate_impl(
|
|||
|
||||
macro_rules! transform_impl(
|
||||
($t: ident) => (
|
||||
impl<N: Add<N, N> + Sub<N, N>> Transform<$t<N>> for $t<N> {
|
||||
impl<N: Clone + Add<N, N> + Sub<N, N>> Transform<$t<N>> for $t<N> {
|
||||
fn transform(&self, other: &$t<N>) -> $t<N> {
|
||||
self.translate(other)
|
||||
}
|
||||
|
|
|
@ -185,7 +185,7 @@ static SAMPLES_3_F64: [Vec3<f64>, ..42] = [
|
|||
Vec3 { x: 0.162456 , y: 0.499995 , z: 0.850654 }
|
||||
];
|
||||
|
||||
impl<N: NumCast> UniformSphereSample for Vec2<N> {
|
||||
impl<N: NumCast + Clone> UniformSphereSample for Vec2<N> {
|
||||
#[inline(always)]
|
||||
fn sample(f: &fn(Vec2<N>)) {
|
||||
for sample in SAMPLES_2_F64.iter() {
|
||||
|
@ -194,7 +194,7 @@ impl<N: NumCast> UniformSphereSample for Vec2<N> {
|
|||
}
|
||||
}
|
||||
|
||||
impl<N: NumCast> UniformSphereSample for Vec3<N> {
|
||||
impl<N: NumCast + Clone> UniformSphereSample for Vec3<N> {
|
||||
#[inline(always)]
|
||||
fn sample(f: &fn(Vec3<N>)) {
|
||||
for sample in SAMPLES_3_F64.iter() {
|
||||
|
|
Loading…
Reference in New Issue