lib.rs
7.15 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
extern crate lazy_static;
pub type Error = &'static str;
pub mod easel;
pub mod transform;
pub mod trigonometry;
pub mod vector;
pub mod geometry;
mod utils;
use vector::Vector;
use easel::{Canvas, Coordinate, Drawable, Fillable};
use geometry::{Camera, DirectLight, Polyeder, Primitives};
use transform::{TMatrix};
use std::fmt::{Display, Formatter, Result};
use std::sync::mpsc;
use std::time::Instant;
use wasm_bindgen::prelude::*;
// When the `wee_alloc` feature is enabled, use `wee_alloc` as the global
// allocator.
#[cfg(feature = "wee_alloc")]
#[global_allocator]
static ALLOC: wee_alloc::WeeAlloc = wee_alloc::WeeAlloc::INIT;
#[wasm_bindgen]
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub struct Color(u8, u8, u8, u8);
#[wasm_bindgen]
pub struct View3d { width :u16
, height :u16
, size :usize
, degree :i32
//, start :Instant
, tetrahedron :Polyeder<f64>
, cube :Polyeder<f64>
, camera :Option<Camera<f64>>
, light :DirectLight<f64>
, zbuf :Vec<f64>
, image :Vec<Color>
}
#[wasm_bindgen]
impl View3d {
pub fn new(width :u16, height :u16) -> Self {
let size = width as usize * height as usize;
let light_vector = Vector(0.0, 0.0, 1.0);
let mut view3d = Self { width: width
, height: height
, size: size
, degree: 0
//, start: Instant::now()
, tetrahedron: Polyeder::tetrahedron(100.0)
, cube: Polyeder::cube(56.25)
, camera: None
, light: DirectLight::new(light_vector)
, zbuf: vec!(0.0; size)
, image: vec!(Color(0, 0, 0, 0xFF); size)
};
view3d.camera = Some(Camera::<f64>::new(&view3d, 45));
view3d
}
pub fn width(&self) -> u16 {
self.width
}
pub fn height(&self) -> u16 {
self.height
}
pub fn update(&mut self) {
//let deg = ((self.start.elapsed() / 25).as_millis() % 360) as i32;
let t = TMatrix::translate(Vector(0.0, 0.0, 150.0));
let rz = TMatrix::rotate_z(self.degree);
let rx = TMatrix::rotate_x(-self.degree*2);
let ry = TMatrix::rotate_y(-self.degree*2);
let rot1 = TMatrix::combine(vec!(rz, rx, t));
let rot2 = TMatrix::combine(vec!(rz, ry, t));
let objects = vec!( (self.tetrahedron.transform(&rot1), 0xFFFF00)
, ( self.cube.transform(&rot2), 0x0000FF) );
self.degree = (self.degree + 1) % 360;
self.clear();
match self.camera {
None => {},
Some(camera) => {
for (o, color) in objects {
for (pg, c) in o.project(&camera, &self.light, color) {
(&pg).fill(self, c);
}
}
},
}
}
pub fn image(&self) -> *const Color {
self.image.as_ptr()
}
}
impl Canvas<f64> for View3d {
fn width(&self) -> u16 {
self.width
}
fn height(&self) -> u16 {
self.height
}
fn clear(&mut self) {
self.zbuf = vec!(0.0; self.size);
self.image = vec!(Color(0, 0, 0, 0xFF); self.size);
}
fn set_pixel(&mut self, c :Coordinate<f64>, color :u32) {
let Coordinate(x, y, zr) = c;
let idx :usize = (y * (self.width as i32) + x) as usize;
let r = ((color >> 16) & 0xFF) as u8;
let g = ((color >> 8) & 0xFF) as u8;
let b = ( color & 0xFF) as u8;
if self.zbuf[idx] < zr {
self.zbuf[idx] = zr;
self.image[idx] = Color(r, g, b, 0xFF);
}
}
// Empty implementations for now… mostly not needed because it is
// done from JavaScript…
fn init_events(&self) {}
fn start_events(&self, _ :mpsc::Sender<i32>) {}
fn draw( &mut self, _ :&dyn Drawable<f64>, _ :Coordinate<f64>, _ :u32 ) {}
fn put_text(&self, _ :Coordinate<f64>, _ :&str) {}
fn show(&self) {}
}
#[wasm_bindgen]
#[repr(u8)]
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum Cell {
Dead = 0,
Alive = 1,
}
#[wasm_bindgen]
pub struct Universe {
width :u32,
height :u32,
cells :Vec<Cell>,
}
#[wasm_bindgen]
impl Universe {
pub fn new() -> Universe {
let width = 64;
let height = 64;
let init_cells = |i :u32| {
if i % 2 == 0 || i % 7 == 0 { Cell::Alive } else { Cell::Dead }
};
let cells = (0..width * height).map(init_cells).collect();
Universe {
width: width,
height: height,
cells: cells,
}
}
pub fn width(&self) -> u32 {
self.width
}
pub fn height(&self) -> u32 {
self.height
}
pub fn cells(&self) -> *const Cell {
self.cells.as_ptr()
}
pub fn render(&self) -> String {
self.to_string()
}
pub fn tick(&mut self) {
let mut next = self.cells.clone();
for row in 0..self.height {
for col in 0..self.width {
let idx = self.get_index(row, col);
let cell = self.cells[idx];
let live_neighbors = self.live_neighbor_count(row, col);
// Game of life rules....
let next_cell = match (cell, live_neighbors) {
(Cell::Alive, 2) |
(Cell::Alive, 3) => Cell::Alive,
(Cell::Alive, _) => Cell::Dead,
( Cell::Dead, 3) => Cell::Alive,
( otherwise, _) => otherwise,
};
next[idx] = next_cell;
}
}
self.cells = next;
}
fn get_index(&self, row :u32, col :u32) -> usize {
(row * self.width + col) as usize
}
fn live_neighbor_count(&self, row :u32, col :u32) -> u8 {
let mut count = 0;
for delta_row in [self.height - 1, 0, 1].iter().cloned() {
for delta_col in [self.width - 1, 0, 1].iter().cloned() {
if delta_row == 0 && delta_col == 0 {
continue;
}
let neighbor_row = (row + delta_row) % self.height;
let neighbor_col = (col + delta_col) % self.width;
let idx = self.get_index(neighbor_row, neighbor_col);
count += self.cells[idx] as u8;
}
}
count
}
}
impl Display for Universe {
fn fmt(&self, f :&mut Formatter) -> Result {
for line in self.cells.as_slice().chunks(self.width as usize) {
for &cell in line {
let symbol = match cell {
Cell::Dead => ' ',
Cell::Alive => '*',
};
write!(f, "{}", symbol)?;
}
write!(f, "\n")?;
}
Ok(())
}
}