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
// mod iter;

// mod common;

use crate::common;
use crate::common::IsoType;
pub trait Voxel {
    fn value(&self) -> IsoType;
    fn interp(&self, data2: &Self, t: common::PrecisionType) -> Self;
}

pub enum GridValues<T>
where
    T: Voxel,
{
    Full(Option<Vec<T>>),
    Empty(Option<Vec<T>>),
    Mixed(Vec<T>),
}
pub enum GridState<T>
where
    T: Voxel 
{
    Initable,
    Generating,//(GridValues<T>)

    Generated(GridValues<T>)
}

pub struct Grid<T>
where
    T: Voxel,
{
    size: usize,
    voxels: GridState<T>,
}
impl<T> Grid<T>
where
    T: Voxel,
{
    pub fn new(size: usize) -> Grid<T> {
        Grid {
            size,
            voxels: GridState::Initable,
        }
    }
    pub fn size(&self)->usize {
        self.size
    }
    pub fn grid_state(&self) -> &GridState<T> {
        &self.voxels
    }
    fn panic_values(&self, new_data: &GridValues<T>) {
        match new_data {
            GridValues::Empty(values) => {
                if let Some(values_found)=values {
                    self.panic_length(values_found);
                }
            },
            GridValues::Full(values) => {
                if let Some(values_found)=values {
                    self.panic_length(values_found);
                }
            },
            GridValues::Mixed(values) => {
                self.panic_length(values);
            },
        }
    }
    fn panic_state(&self, new_data: &GridState<T>) {
        match new_data {
            GridState::Generated(grid_values) => self.panic_values(grid_values),
            GridState::Generating => panic!("GridState::Generating is internal state. Don't use it."),
            GridState::Initable => ()
        };
    }
    fn panic_length(&self, new_data: &Vec<T>) {
        if new_data.len() > self.size * self.size * self.size {
            panic!("Vector length not corresponding to the Grid parameters");
        }
    }
    pub fn take_to_generate(&mut self) -> Result<GridValues<T>,&'static str> {
        match &mut self.voxels {
            GridState::Initable => {
                self.voxels = GridState::Generating;
                Ok(GridValues::Empty(None))
            },
            GridState::Generating => Err("Already taken to generate."),
            GridState::Generated(grid_values) => {
                let mut result = GridValues::Empty(None);
                std::mem::swap(&mut result, grid_values);
                self.voxels = GridState::Generating;
                Ok(result)
            },
        }
    }
    pub fn get_grid_values(&self) -> Result<&GridValues<T>,&'static str> {
        match &self.voxels {
            GridState::Initable => Err("No grid to take."),
            GridState::Generating => Err("Already taken to generate."),
            GridState::Generated(grid_values) => {
                Ok(grid_values)
            },
        }
    }
    pub fn set_chunk_state(&mut self, cs: GridState<T>) {
        self.panic_state(&cs);
        self.voxels = cs;
    }
    pub fn set_generated(&mut self, new_data: GridValues<T>) {
        self.panic_values(&new_data);
        self.voxels = GridState::Generated(new_data);
    }
    pub fn set_voxels(&mut self, new_data: Vec<T>) -> Result<(), &'static str> {
        self.panic_length(&new_data);

        let mut empty_full = 0;
        for v in &new_data {
            empty_full |= if v.value() < 0. { 
                0x1 
            } else if v.value() > 1. { 
                0x8
            } else {
                if v.value() < common::ISO { 
                    0x2
                } else { 
                    0x4
                }
            };
            if empty_full&0b0011 != 0b0011 || empty_full&0b1100 != 0b1100  { break; }
        }
        self.voxels = GridState::Generated(match empty_full {
            0b0001 => GridValues::Empty(None),
            0b1000 => GridValues::Full(None),
            0b0011 => GridValues::Empty(Some(new_data)),
            0b1100 => GridValues::Full(Some(new_data)),
            _ => GridValues::Mixed(new_data),
        });
        Ok(())
    }
    pub fn voxels(&self) -> Option<&Vec<T>> {
        let state = match &self.voxels {
            GridState::Generated(v) => v,
            GridState::Generating => return None,
            _ => return None
        };
        match state {
            GridValues::Empty(v) => v.as_ref(),
            GridValues::Full(v) => v.as_ref(),
            GridValues::Mixed(v) => Some(&v)
        }
    }
}