]> git.lizzy.rs Git - rust.git/blob - src/test/bench/noise.rs
Auto merge of #23678 - richo:check-flightcheck, r=alexcrichton
[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 #![feature(rand, core)]
16
17 use std::f32::consts::PI;
18 use std::num::Float;
19 use std::rand::{Rng, StdRng};
20
21 #[derive(Copy)]
22 struct Vec2 {
23     x: f32,
24     y: f32,
25 }
26
27 fn lerp(a: f32, b: f32, v: f32) -> f32 { a * (1.0 - v) + b * v }
28
29 fn smooth(v: f32) -> f32 { v * v * (3.0 - 2.0 * v) }
30
31 fn random_gradient<R: Rng>(r: &mut R) -> Vec2 {
32     let v = PI * 2.0 * r.gen::<f32>();
33     Vec2 { x: v.cos(), y: v.sin() }
34 }
35
36 fn gradient(orig: Vec2, grad: Vec2, p: Vec2) -> f32 {
37     (p.x - orig.x) * grad.x + (p.y - orig.y) * grad.y
38 }
39
40 struct Noise2DContext {
41     rgradients: [Vec2; 256],
42     permutations: [i32; 256],
43 }
44
45 impl Noise2DContext {
46     fn new() -> Noise2DContext {
47         let mut rng = StdRng::new().unwrap();
48
49         let mut rgradients = [Vec2 { x: 0.0, y: 0.0 }; 256];
50         for x in &mut rgradients[..] {
51             *x = random_gradient(&mut rng);
52         }
53
54         let mut permutations = [0; 256];
55         for (i, x) in permutations.iter_mut().enumerate() {
56             *x = i as i32;
57         }
58         rng.shuffle(&mut permutations);
59
60         Noise2DContext { rgradients: rgradients, permutations: permutations }
61     }
62
63     fn get_gradient(&self, x: i32, y: i32) -> Vec2 {
64         let idx = self.permutations[(x & 255) as usize] +
65                     self.permutations[(y & 255) as usize];
66         self.rgradients[(idx & 255) as usize]
67     }
68
69     fn get_gradients(&self, x: f32, y: f32) -> ([Vec2; 4], [Vec2; 4]) {
70         let x0f = x.floor();
71         let y0f = y.floor();
72         let x1f = x0f + 1.0;
73         let y1f = y0f + 1.0;
74
75         let x0 = x0f as i32;
76         let y0 = y0f as i32;
77         let x1 = x0 + 1;
78         let y1 = y0 + 1;
79
80         ([self.get_gradient(x0, y0), self.get_gradient(x1, y0),
81           self.get_gradient(x0, y1), self.get_gradient(x1, y1)],
82          [Vec2 { x: x0f, y: y0f }, Vec2 { x: x1f, y: y0f },
83           Vec2 { x: x0f, y: y1f }, Vec2 { x: x1f, y: y1f }])
84     }
85
86     fn get(&self, x: f32, y: f32) -> f32 {
87         let p = Vec2 {x: x, y: y};
88         let (gradients, origins) = self.get_gradients(x, y);
89
90         let v0 = gradient(origins[0], gradients[0], p);
91         let v1 = gradient(origins[1], gradients[1], p);
92         let v2 = gradient(origins[2], gradients[2], p);
93         let v3 = gradient(origins[3], gradients[3], p);
94
95         let fx = smooth(x - origins[0].x);
96         let vx0 = lerp(v0, v1, fx);
97         let vx1 = lerp(v2, v3, fx);
98         let fy = smooth(y - origins[0].y);
99
100         lerp(vx0, vx1, fy)
101     }
102 }
103
104 fn main() {
105     let symbols = [' ', '░', '▒', '▓', '█', '█'];
106     let mut pixels = [0f32; 256*256];
107     let n2d = Noise2DContext::new();
108
109     for _ in 0..100 {
110         for y in 0..256 {
111             for x in 0..256 {
112                 let v = n2d.get(x as f32 * 0.1, y as f32 * 0.1);
113                 pixels[y*256+x] = v * 0.5 + 0.5;
114             }
115         }
116     }
117
118     for y in 0..256 {
119         for x in 0..256 {
120             let idx = (pixels[y*256+x] / 0.2) as usize;
121             print!("{}", symbols[idx]);
122         }
123         print!("\n");
124     }
125 }