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.
|
|
|
|
|
2014-02-14 13:03:39 -06:00
|
|
|
// Multi-language Perlin noise benchmark.
|
|
|
|
// See https://github.com/nsf/pnoise for timings and alternative implementations.
|
2014-07-21 17:57:14 -05:00
|
|
|
// ignore-lexer-test FIXME #15679
|
2013-01-16 14:51:46 -06:00
|
|
|
|
2014-02-14 13:03:39 -06:00
|
|
|
use std::f32::consts::PI;
|
2014-11-09 23:26:10 -06:00
|
|
|
use std::num::{Float, FloatMath};
|
std: Recreate a `rand` module
This commit shuffles around some of the `rand` code, along with some
reorganization. The new state of the world is as follows:
* The librand crate now only depends on libcore. This interface is experimental.
* The standard library has a new module, `std::rand`. This interface will
eventually become stable.
Unfortunately, this entailed more of a breaking change than just shuffling some
names around. The following breaking changes were made to the rand library:
* Rng::gen_vec() was removed. This has been replaced with Rng::gen_iter() which
will return an infinite stream of random values. Previous behavior can be
regained with `rng.gen_iter().take(n).collect()`
* Rng::gen_ascii_str() was removed. This has been replaced with
Rng::gen_ascii_chars() which will return an infinite stream of random ascii
characters. Similarly to gen_iter(), previous behavior can be emulated with
`rng.gen_ascii_chars().take(n).collect()`
* {IsaacRng, Isaac64Rng, XorShiftRng}::new() have all been removed. These all
relied on being able to use an OSRng for seeding, but this is no longer
available in librand (where these types are defined). To retain the same
functionality, these types now implement the `Rand` trait so they can be
generated with a random seed from another random number generator. This allows
the stdlib to use an OSRng to create seeded instances of these RNGs.
* Rand implementations for `Box<T>` and `@T` were removed. These seemed to be
pretty rare in the codebase, and it allows for librand to not depend on
liballoc. Additionally, other pointer types like Rc<T> and Arc<T> were not
supported. If this is undesirable, librand can depend on liballoc and regain
these implementations.
* The WeightedChoice structure is no longer built with a `Vec<Weighted<T>>`,
but rather a `&mut [Weighted<T>]`. This means that the WeightedChoice
structure now has a lifetime associated with it.
* The `sample` method on `Rng` has been moved to a top-level function in the
`rand` module due to its dependence on `Vec`.
cc #13851
[breaking-change]
2014-05-25 03:39:37 -05:00
|
|
|
use std::rand::{Rng, StdRng};
|
2013-03-14 13:22:14 -05:00
|
|
|
|
2013-01-16 14:51:46 -06:00
|
|
|
struct Vec2 {
|
|
|
|
x: f32,
|
|
|
|
y: f32,
|
|
|
|
}
|
|
|
|
|
librustc: Make `Copy` opt-in.
This change makes the compiler no longer infer whether types (structures
and enumerations) implement the `Copy` trait (and thus are implicitly
copyable). Rather, you must implement `Copy` yourself via `impl Copy for
MyType {}`.
A new warning has been added, `missing_copy_implementations`, to warn
you if a non-generic public type has been added that could have
implemented `Copy` but didn't.
For convenience, you may *temporarily* opt out of this behavior by using
`#![feature(opt_out_copy)]`. Note though that this feature gate will never be
accepted and will be removed by the time that 1.0 is released, so you should
transition your code away from using it.
This breaks code like:
#[deriving(Show)]
struct Point2D {
x: int,
y: int,
}
fn main() {
let mypoint = Point2D {
x: 1,
y: 1,
};
let otherpoint = mypoint;
println!("{}{}", mypoint, otherpoint);
}
Change this code to:
#[deriving(Show)]
struct Point2D {
x: int,
y: int,
}
impl Copy for Point2D {}
fn main() {
let mypoint = Point2D {
x: 1,
y: 1,
};
let otherpoint = mypoint;
println!("{}{}", mypoint, otherpoint);
}
This is the backwards-incompatible part of #13231.
Part of RFC #3.
[breaking-change]
2014-12-05 19:01:33 -06:00
|
|
|
impl Copy for Vec2 {}
|
|
|
|
|
2013-04-03 05:54:14 -05:00
|
|
|
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
|
|
|
fn smooth(v: f32) -> f32 { v * v * (3.0 - 2.0 * v) }
|
|
|
|
|
2014-02-14 13:03:39 -06:00
|
|
|
fn random_gradient<R: Rng>(r: &mut R) -> Vec2 {
|
|
|
|
let v = PI * 2.0 * r.gen();
|
|
|
|
Vec2 { x: v.cos(), y: v.sin() }
|
2013-01-16 14:51:46 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
fn gradient(orig: Vec2, grad: Vec2, p: Vec2) -> f32 {
|
2014-02-14 13:03:39 -06:00
|
|
|
(p.x - orig.x) * grad.x + (p.y - orig.y) * grad.y
|
2013-01-16 14:51:46 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
struct Noise2DContext {
|
2013-03-22 20:52:04 -05:00
|
|
|
rgradients: [Vec2, ..256],
|
2014-02-14 13:03:39 -06:00
|
|
|
permutations: [i32, ..256],
|
2013-01-16 14:51:46 -06:00
|
|
|
}
|
|
|
|
|
2013-05-31 17:17:22 -05:00
|
|
|
impl Noise2DContext {
|
2014-02-14 13:03:39 -06:00
|
|
|
fn new() -> Noise2DContext {
|
2014-03-25 00:13:11 -05:00
|
|
|
let mut rng = StdRng::new().unwrap();
|
2014-02-14 13:03:39 -06:00
|
|
|
|
|
|
|
let mut rgradients = [Vec2 { x: 0.0, y: 0.0 }, ..256];
|
2014-09-14 22:27:36 -05:00
|
|
|
for x in rgradients.iter_mut() {
|
2014-02-14 13:03:39 -06:00
|
|
|
*x = random_gradient(&mut rng);
|
2013-05-06 21:29:04 -05:00
|
|
|
}
|
2013-04-03 05:54:14 -05:00
|
|
|
|
2014-02-14 13:03:39 -06:00
|
|
|
let mut permutations = [0i32, ..256];
|
2014-09-14 22:27:36 -05:00
|
|
|
for (i, x) in permutations.iter_mut().enumerate() {
|
2014-02-14 13:03:39 -06:00
|
|
|
*x = i as i32;
|
2013-04-03 05:54:14 -05:00
|
|
|
}
|
2014-11-17 02:39:01 -06:00
|
|
|
rng.shuffle(&mut permutations);
|
2014-02-14 13:03:39 -06:00
|
|
|
|
|
|
|
Noise2DContext { rgradients: rgradients, permutations: permutations }
|
2013-01-16 14:51:46 -06:00
|
|
|
}
|
|
|
|
|
2014-02-14 13:03:39 -06:00
|
|
|
fn get_gradient(&self, x: i32, y: i32) -> Vec2 {
|
2014-04-01 22:39:26 -05:00
|
|
|
let idx = self.permutations[(x & 255) as uint] +
|
|
|
|
self.permutations[(y & 255) as uint];
|
|
|
|
self.rgradients[(idx & 255) as uint]
|
2013-01-16 14:51:46 -06:00
|
|
|
}
|
|
|
|
|
2014-02-14 13:03:39 -06:00
|
|
|
fn get_gradients(&self, x: f32, y: f32) -> ([Vec2, ..4], [Vec2, ..4]) {
|
2013-07-08 13:22:19 -05:00
|
|
|
let x0f = x.floor();
|
|
|
|
let y0f = y.floor();
|
2014-02-14 13:03:39 -06:00
|
|
|
let x1f = x0f + 1.0;
|
|
|
|
let y1f = y0f + 1.0;
|
|
|
|
|
|
|
|
let x0 = x0f as i32;
|
|
|
|
let y0 = y0f as i32;
|
2013-01-16 14:51:46 -06:00
|
|
|
let x1 = x0 + 1;
|
|
|
|
let y1 = y0 + 1;
|
|
|
|
|
2014-02-14 13:03:39 -06:00
|
|
|
([self.get_gradient(x0, y0), self.get_gradient(x1, y0),
|
|
|
|
self.get_gradient(x0, y1), self.get_gradient(x1, y1)],
|
|
|
|
[Vec2 { x: x0f, y: y0f }, Vec2 { x: x1f, y: y0f },
|
|
|
|
Vec2 { x: x0f, y: y1f }, Vec2 { x: x1f, y: y1f }])
|
2013-01-16 14:51:46 -06:00
|
|
|
}
|
|
|
|
|
2014-02-14 13:03:39 -06:00
|
|
|
fn get(&self, x: f32, y: f32) -> f32 {
|
2013-04-03 05:54:14 -05:00
|
|
|
let p = Vec2 {x: x, y: y};
|
2014-02-14 13:03:39 -06:00
|
|
|
let (gradients, origins) = self.get_gradients(x, y);
|
|
|
|
|
2013-01-16 14:51:46 -06:00
|
|
|
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);
|
2014-02-14 13:03:39 -06:00
|
|
|
|
2013-01-16 14:51:46 -06:00
|
|
|
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);
|
2014-02-14 13:03:39 -06:00
|
|
|
|
2013-01-16 14:51:46 -06:00
|
|
|
lerp(vx0, vx1, fy)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn main() {
|
2014-02-14 13:03:39 -06:00
|
|
|
let symbols = [' ', '░', '▒', '▓', '█', '█'];
|
2013-04-03 05:54:14 -05:00
|
|
|
let mut pixels = [0f32, ..256*256];
|
2014-02-14 13:03:39 -06:00
|
|
|
let n2d = Noise2DContext::new();
|
|
|
|
|
2014-04-21 16:58:52 -05:00
|
|
|
for _ in range(0u, 100) {
|
2014-04-01 22:39:26 -05:00
|
|
|
for y in range(0u, 256) {
|
|
|
|
for x in range(0u, 256) {
|
2014-02-14 13:03:39 -06:00
|
|
|
let v = n2d.get(x as f32 * 0.1, y as f32 * 0.1);
|
|
|
|
pixels[y*256+x] = v * 0.5 + 0.5;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2013-01-16 14:51:46 -06:00
|
|
|
|
2014-04-01 22:39:26 -05:00
|
|
|
for y in range(0u, 256) {
|
|
|
|
for x in range(0u, 256) {
|
2014-02-14 13:03:39 -06:00
|
|
|
let idx = (pixels[y*256+x] / 0.2) as uint;
|
2014-11-17 13:29:38 -06:00
|
|
|
print!("{}", symbols[idx]);
|
2013-01-16 14:51:46 -06:00
|
|
|
}
|
2014-02-14 13:03:39 -06:00
|
|
|
print!("\n");
|
2013-06-28 16:14:28 -05:00
|
|
|
}
|
2013-01-16 14:51:46 -06:00
|
|
|
}
|