lib.rs 6.95 KB
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::ptr;
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
                  , 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
                              , 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, 0); size) };

        view3d.camera = Some(Camera::<f64>::new(&view3d, 45));
        view3d
    }

    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(deg);
        let rx = TMatrix::rotate_x(-deg*2);
        let ry = TMatrix::rotate_y(-deg*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.clear();

        match self.camera {
            None         => {},
            Some(camera) => {
                for (o, color) in objects {
                    for (pg, c) in o.project(&camera, &self.light, color) {
                        (&pg).fill(&mut 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);
        unsafe {
            let ptr  = self.image.as_mut_ptr();
            ptr::write_bytes(ptr, 0, 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(())
    }
}