2014-02-05 16:33:10 -06:00
|
|
|
// Copyright 2014 The Rust Project Developers. See the COPYRIGHT
|
|
|
|
// file at the top-level directory of this distribution and at
|
|
|
|
// http://rust-lang.org/COPYRIGHT.
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
|
|
|
|
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
|
|
|
|
// option. This file may not be copied, modified, or distributed
|
|
|
|
// except according to those terms.
|
|
|
|
|
2013-01-16 14:51:46 -06:00
|
|
|
// Perlin noise benchmark from https://gist.github.com/1170424
|
|
|
|
|
2013-09-26 01:26:09 -05:00
|
|
|
use std::f64;
|
2013-09-20 06:47:05 -05:00
|
|
|
use std::rand::Rng;
|
2013-05-24 21:35:29 -05:00
|
|
|
use std::rand;
|
2013-03-14 13:22:14 -05:00
|
|
|
|
2013-01-16 14:51:46 -06:00
|
|
|
struct Vec2 {
|
|
|
|
x: f32,
|
|
|
|
y: f32,
|
|
|
|
}
|
|
|
|
|
2013-04-03 05:54:14 -05:00
|
|
|
#[inline(always)]
|
|
|
|
fn lerp(a: f32, b: f32, v: f32) -> f32 { a * (1.0 - v) + b * v }
|
2013-01-16 14:51:46 -06:00
|
|
|
|
2013-04-03 05:54:14 -05:00
|
|
|
#[inline(always)]
|
|
|
|
fn smooth(v: f32) -> f32 { v * v * (3.0 - 2.0 * v) }
|
|
|
|
|
2013-05-06 21:29:04 -05:00
|
|
|
fn random_gradient<R:Rng>(r: &mut R) -> Vec2 {
|
2013-10-28 19:34:33 -05:00
|
|
|
let v = 2.0 * f64::consts::PI * r.gen();
|
2013-04-03 05:54:14 -05:00
|
|
|
Vec2 {
|
2013-07-08 13:22:19 -05:00
|
|
|
x: v.cos() as f32,
|
|
|
|
y: v.sin() as f32,
|
2013-01-16 14:51:46 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn gradient(orig: Vec2, grad: Vec2, p: Vec2) -> f32 {
|
2013-04-03 05:54:14 -05:00
|
|
|
let sp = Vec2 {x: p.x - orig.x, y: p.y - orig.y};
|
2013-08-17 08:16:37 -05:00
|
|
|
grad.x * sp.x + grad.y * sp.y
|
2013-01-16 14:51:46 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
struct Noise2DContext {
|
2013-03-22 20:52:04 -05:00
|
|
|
rgradients: [Vec2, ..256],
|
|
|
|
permutations: [int, ..256],
|
2013-01-16 14:51:46 -06:00
|
|
|
}
|
|
|
|
|
2013-05-31 17:17:22 -05:00
|
|
|
impl Noise2DContext {
|
|
|
|
pub fn new() -> Noise2DContext {
|
2013-05-06 21:29:04 -05:00
|
|
|
let mut r = rand::rng();
|
2013-04-03 05:54:14 -05:00
|
|
|
let mut rgradients = [ Vec2 { x: 0.0, y: 0.0 }, ..256 ];
|
2013-08-03 11:45:23 -05:00
|
|
|
for i in range(0, 256) {
|
2013-05-06 21:29:04 -05:00
|
|
|
rgradients[i] = random_gradient(&mut r);
|
|
|
|
}
|
2013-04-03 05:54:14 -05:00
|
|
|
let mut permutations = [ 0, ..256 ];
|
2013-08-03 11:45:23 -05:00
|
|
|
for i in range(0, 256) {
|
2013-05-06 21:29:04 -05:00
|
|
|
permutations[i] = i;
|
|
|
|
}
|
2013-04-03 05:54:14 -05:00
|
|
|
r.shuffle_mut(permutations);
|
|
|
|
|
|
|
|
Noise2DContext {
|
|
|
|
rgradients: rgradients,
|
|
|
|
permutations: permutations,
|
|
|
|
}
|
2013-01-16 14:51:46 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
#[inline(always)]
|
2013-05-31 17:17:22 -05:00
|
|
|
pub fn get_gradient(&self, x: int, y: int) -> Vec2 {
|
2013-01-16 14:51:46 -06:00
|
|
|
let idx = self.permutations[x & 255] + self.permutations[y & 255];
|
|
|
|
self.rgradients[idx & 255]
|
|
|
|
}
|
|
|
|
|
2013-04-03 05:54:14 -05:00
|
|
|
#[inline]
|
2013-05-31 17:17:22 -05:00
|
|
|
pub fn get_gradients(&self,
|
|
|
|
gradients: &mut [Vec2, ..4],
|
|
|
|
origins: &mut [Vec2, ..4],
|
|
|
|
x: f32,
|
|
|
|
y: f32) {
|
2013-07-08 13:22:19 -05:00
|
|
|
let x0f = x.floor();
|
|
|
|
let y0f = y.floor();
|
2013-01-16 14:51:46 -06:00
|
|
|
let x0 = x0f as int;
|
|
|
|
let y0 = y0f as int;
|
|
|
|
let x1 = x0 + 1;
|
|
|
|
let y1 = y0 + 1;
|
|
|
|
|
|
|
|
gradients[0] = self.get_gradient(x0, y0);
|
|
|
|
gradients[1] = self.get_gradient(x1, y0);
|
|
|
|
gradients[2] = self.get_gradient(x0, y1);
|
|
|
|
gradients[3] = self.get_gradient(x1, y1);
|
|
|
|
|
2013-04-03 05:54:14 -05:00
|
|
|
origins[0] = Vec2 {x: x0f + 0.0, y: y0f + 0.0};
|
|
|
|
origins[1] = Vec2 {x: x0f + 1.0, y: y0f + 0.0};
|
|
|
|
origins[2] = Vec2 {x: x0f + 0.0, y: y0f + 1.0};
|
|
|
|
origins[3] = Vec2 {x: x0f + 1.0, y: y0f + 1.0};
|
2013-01-16 14:51:46 -06:00
|
|
|
}
|
|
|
|
|
2013-04-03 05:54:14 -05:00
|
|
|
#[inline]
|
2013-05-31 17:17:22 -05:00
|
|
|
pub fn get(&self, x: f32, y: f32) -> f32 {
|
2013-04-03 05:54:14 -05:00
|
|
|
let p = Vec2 {x: x, y: y};
|
2013-01-16 14:51:46 -06:00
|
|
|
let mut gradients = [ Vec2 { x: 0.0, y: 0.0 }, ..4 ];
|
|
|
|
let mut origins = [ Vec2 { x: 0.0, y: 0.0 }, ..4 ];
|
|
|
|
self.get_gradients(&mut gradients, &mut origins, x, y);
|
|
|
|
let v0 = gradient(origins[0], gradients[0], p);
|
|
|
|
let v1 = gradient(origins[1], gradients[1], p);
|
|
|
|
let v2 = gradient(origins[2], gradients[2], p);
|
|
|
|
let v3 = gradient(origins[3], gradients[3], p);
|
|
|
|
let fx = smooth(x - origins[0].x);
|
|
|
|
let vx0 = lerp(v0, v1, fx);
|
|
|
|
let vx1 = lerp(v2, v3, fx);
|
|
|
|
let fy = smooth(y - origins[0].y);
|
|
|
|
lerp(vx0, vx1, fy)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn main() {
|
|
|
|
let symbols = [" ", "░", "▒", "▓", "█", "█"];
|
2013-04-03 05:54:14 -05:00
|
|
|
let mut pixels = [0f32, ..256*256];
|
|
|
|
let n2d = ~Noise2DContext::new();
|
2013-08-05 22:43:06 -05:00
|
|
|
for _ in range(0, 100u) {
|
2013-08-03 11:45:23 -05:00
|
|
|
for y in range(0, 256) {
|
|
|
|
for x in range(0, 256) {
|
2013-01-16 14:51:46 -06:00
|
|
|
let v = n2d.get(
|
|
|
|
x as f32 * 0.1f32,
|
|
|
|
y as f32 * 0.1f32
|
|
|
|
) * 0.5f32 + 0.5f32;
|
|
|
|
pixels[y*256+x] = v;
|
|
|
|
};
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
2013-08-03 11:45:23 -05:00
|
|
|
for y in range(0, 256) {
|
|
|
|
for x in range(0, 256) {
|
2014-01-09 04:06:55 -06:00
|
|
|
print!("{}", symbols[(pixels[y*256+x] / 0.2f32) as int]);
|
2013-01-16 14:51:46 -06:00
|
|
|
}
|
2014-01-09 04:06:55 -06:00
|
|
|
println!("");
|
2013-06-28 16:14:28 -05:00
|
|
|
}
|
2013-01-16 14:51:46 -06:00
|
|
|
}
|