From 5fecb8fc818df712c512c8570c040d2bc5201d77 Mon Sep 17 00:00:00 2001 From: Brendan Zabarauskas Date: Thu, 25 Jul 2013 12:57:14 +1000 Subject: [PATCH] Generalise noise methods to take generic `Dimensioned` types --- examples/perlin.rs | 8 +++----- src/noise/perlin.rs | 24 ++++++++++++------------ src/noise/simplex.rs | 9 ++++----- 3 files changed, 19 insertions(+), 22 deletions(-) diff --git a/examples/perlin.rs b/examples/perlin.rs index a8fd2be..7783f90 100644 --- a/examples/perlin.rs +++ b/examples/perlin.rs @@ -21,7 +21,6 @@ extern mod lmath; use std::uint::range; use lmath::noise::perlin::Perlin; -use lmath::math::Point2; static WIDTH: uint = 100; static HEIGHT: uint = 100; @@ -34,10 +33,9 @@ fn main() { for range(0, HEIGHT) |y| { for range(0, WIDTH) |x| { - pixels[y][x] = perlin.noise2( - Point2::new(x as f32 * 0.1f32, - y as f32 * 0.1f32) - ) * 0.5f32 + 0.5f32; + pixels[y][x] = perlin.noise2([ + x as f32 * 0.1f32, y as f32 * 0.1f32 + ]) * 0.5f32 + 0.5f32; }; }; diff --git a/src/noise/perlin.rs b/src/noise/perlin.rs index 48a0d3b..920f87d 100644 --- a/src/noise/perlin.rs +++ b/src/noise/perlin.rs @@ -18,7 +18,7 @@ use std::num::cast; -use math::{Point2, Point3}; +use math::Dimensioned; pub struct Perlin { // permutation table @@ -31,24 +31,24 @@ impl Perlin { Perlin { ptable: P } } - pub fn noise1(&self, t: T) -> T { - self.noise3(Point3::new(t, zero!(T), zero!(T))) + pub fn noise1>(&self, v: V) -> T { + self.noise3([v.i(0).clone(), zero!(T), zero!(T)]) } - pub fn noise2(&self, pos: Point2) -> T { - self.noise3(Point3::new(pos.x.clone(), pos.y.clone(), zero!(T))) + pub fn noise2>(&self, v: V) -> T { + self.noise3([v.i(0).clone(), v.i(1).clone(), zero!(T)]) } - pub fn noise3(&self, pos: Point3) -> T { + pub fn noise3>(&self, v: V) -> T { // Find the unit cube that contains the point - let X = pos.x.floor().to_uint() & 255; - let Y = pos.y.floor().to_uint() & 255; - let Z = pos.z.floor().to_uint() & 255; + let X = v.i(0).floor().to_uint() & 255; + let Y = v.i(1).floor().to_uint() & 255; + let Z = v.i(2).floor().to_uint() & 255; // Find the relative X, Y, Z of point in the cube - let x = pos.x - pos.x.floor(); - let y = pos.y - pos.y.floor(); - let z = pos.z - pos.z.floor(); + let x = *v.i(0) - v.i(0).floor(); + let y = *v.i(1) - v.i(1).floor(); + let z = *v.i(2) - v.i(2).floor(); // Compute the fade curves for X, Y, Z let u = fade(x.clone()); diff --git a/src/noise/simplex.rs b/src/noise/simplex.rs index 7678d9f..c117991 100644 --- a/src/noise/simplex.rs +++ b/src/noise/simplex.rs @@ -13,8 +13,7 @@ // See the License for the specific language governing permissions and // limitations under the License. -use math::Vec4; -use math::{Point2, Point3}; +use math::Dimensioned; pub struct Simplex; @@ -23,15 +22,15 @@ impl Simplex { fail!("Not yet implemented!") } - pub fn noise2(&self, _pos: Point2) -> T { + pub fn noise2>(&self, _v: V) -> T { fail!("Not yet implemented!") } - pub fn noise3(&self, _pos: Point3) -> T { + pub fn noise3>(&self, _v: V) -> T { fail!("Not yet implemented!") } - pub fn noise4(&self, _vec: Vec4) -> T { + pub fn noise4>(&self, _v: V) -> T { fail!("Not yet implemented!") } }