]> git.lizzy.rs Git - rust.git/blob - src/test/bench/noise.rs
rollup merge of #17355 : gamazeps/issue17210
[rust.git] / src / test / bench / noise.rs
1 // Copyright 2014 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
4 //
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
10
11 // Multi-language Perlin noise benchmark.
12 // See https://github.com/nsf/pnoise for timings and alternative implementations.
13 // ignore-lexer-test FIXME #15679
14
15 use std::f32::consts::PI;
16 use std::rand::{Rng, StdRng};
17
18 struct Vec2 {
19     x: f32,
20     y: f32,
21 }
22
23 fn lerp(a: f32, b: f32, v: f32) -> f32 { a * (1.0 - v) + b * v }
24
25 fn smooth(v: f32) -> f32 { v * v * (3.0 - 2.0 * v) }
26
27 fn random_gradient<R: Rng>(r: &mut R) -> Vec2 {
28     let v = PI * 2.0 * r.gen();
29     Vec2 { x: v.cos(), y: v.sin() }
30 }
31
32 fn gradient(orig: Vec2, grad: Vec2, p: Vec2) -> f32 {
33     (p.x - orig.x) * grad.x + (p.y - orig.y) * grad.y
34 }
35
36 struct Noise2DContext {
37     rgradients: [Vec2, ..256],
38     permutations: [i32, ..256],
39 }
40
41 impl Noise2DContext {
42     fn new() -> Noise2DContext {
43         let mut rng = StdRng::new().unwrap();
44
45         let mut rgradients = [Vec2 { x: 0.0, y: 0.0 }, ..256];
46         for x in rgradients.iter_mut() {
47             *x = random_gradient(&mut rng);
48         }
49
50         let mut permutations = [0i32, ..256];
51         for (i, x) in permutations.iter_mut().enumerate() {
52             *x = i as i32;
53         }
54         rng.shuffle(permutations);
55
56         Noise2DContext { rgradients: rgradients, permutations: permutations }
57     }
58
59     fn get_gradient(&self, x: i32, y: i32) -> Vec2 {
60         let idx = self.permutations[(x & 255) as uint] +
61                     self.permutations[(y & 255) as uint];
62         self.rgradients[(idx & 255) as uint]
63     }
64
65     fn get_gradients(&self, x: f32, y: f32) -> ([Vec2, ..4], [Vec2, ..4]) {
66         let x0f = x.floor();
67         let y0f = y.floor();
68         let x1f = x0f + 1.0;
69         let y1f = y0f + 1.0;
70
71         let x0 = x0f as i32;
72         let y0 = y0f as i32;
73         let x1 = x0 + 1;
74         let y1 = y0 + 1;
75
76         ([self.get_gradient(x0, y0), self.get_gradient(x1, y0),
77           self.get_gradient(x0, y1), self.get_gradient(x1, y1)],
78          [Vec2 { x: x0f, y: y0f }, Vec2 { x: x1f, y: y0f },
79           Vec2 { x: x0f, y: y1f }, Vec2 { x: x1f, y: y1f }])
80     }
81
82     fn get(&self, x: f32, y: f32) -> f32 {
83         let p = Vec2 {x: x, y: y};
84         let (gradients, origins) = self.get_gradients(x, y);
85
86         let v0 = gradient(origins[0], gradients[0], p);
87         let v1 = gradient(origins[1], gradients[1], p);
88         let v2 = gradient(origins[2], gradients[2], p);
89         let v3 = gradient(origins[3], gradients[3], p);
90
91         let fx = smooth(x - origins[0].x);
92         let vx0 = lerp(v0, v1, fx);
93         let vx1 = lerp(v2, v3, fx);
94         let fy = smooth(y - origins[0].y);
95
96         lerp(vx0, vx1, fy)
97     }
98 }
99
100 fn main() {
101     let symbols = [' ', '░', '▒', '▓', '█', '█'];
102     let mut pixels = [0f32, ..256*256];
103     let n2d = Noise2DContext::new();
104
105     for _ in range(0u, 100) {
106         for y in range(0u, 256) {
107             for x in range(0u, 256) {
108                 let v = n2d.get(x as f32 * 0.1, y as f32 * 0.1);
109                 pixels[y*256+x] = v * 0.5 + 0.5;
110             }
111         }
112     }
113
114     for y in range(0u, 256) {
115         for x in range(0u, 256) {
116             let idx = (pixels[y*256+x] / 0.2) as uint;
117             print!("{:c}", symbols[idx]);
118         }
119         print!("\n");
120     }
121 }