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
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
//! This crate provides an interface to manipulate life
//! cellular automata grids.
//! Those grids can be toroidal or resizable.

extern crate rand;
#[macro_use]
extern crate vulkano_shader_derive;
extern crate vulkano;

pub mod analysis;
pub mod error;
pub mod file;
pub mod processing;
pub mod view;
mod vulkan;

use std::fmt;
use std::sync::Arc;

use vulkano::buffer::BufferUsage;
use vulkano::buffer::CpuAccessibleBuffer;
use vulkano::device::Device;
use vulkano::device::Queue;

use error::GridErrorKind;

/// This struct contains the grid of a life cellular automaton.
///
/// This grid is stored as a `Vec<bool>`.
/// When it is toroidal, its size is constant. When it is not,
/// it is resized when computing the next generation
/// according to the size of the contained pattern.
///
/// The origin of the pattern is also stored in `Grid`:
/// the coordinates of its north west corner is stored as a
/// `(usize, usize)`.
///
/// It also contains the cellular automaton's rules stored as two `Vec<u8>`s.
/// These are the survival and birth conditions into `survival` and `birth`
/// respectivly.
pub struct Grid {
    format: String,                          // Contains the file format used
    toroidal: Arc<CpuAccessibleBuffer<i32>>, // Resizable grid if set to false (note: false = 0 and true = 1)

    survival: Arc<CpuAccessibleBuffer<[u32]>>,
    birth: Arc<CpuAccessibleBuffer<[u32]>>,

    width: usize,
    height: usize,
    cells: Arc<CpuAccessibleBuffer<[u8]>>,

    device: Arc<Device>,
    queue: Arc<Queue>,
}

impl Grid {
    /// Returns a new `Grid`:
    /// * containing the file format `frmt`
    /// * toroidal if `trdl` is `true`, resizable otherwise
    /// * containing the rules given by `srvl` and `brth`
    /// * whose grid's size is determined by `width` and `height`
    pub fn new(
        frmt: &String,
        trdl: bool,
        srvl: &Vec<u32>,
        brth: &Vec<u32>,
        width: usize,
        height: usize,
    ) -> Grid {
        let (device, queue) = vulkan::vk_init();

        let new_cells_iter = (0..width * height).map(|_| 0u8);
        let new_cells =
            CpuAccessibleBuffer::from_iter(device.clone(), BufferUsage::all(), new_cells_iter)
                .expect("failed to create buffer");

        let toroidal_val = if trdl { 1 } else { 0 };
        let toroidal =
            CpuAccessibleBuffer::from_data(device.clone(), BufferUsage::all(), toroidal_val)
                .expect("failed to create buffer");

        let survival = CpuAccessibleBuffer::from_iter(
            device.clone(),
            BufferUsage::all(),
            srvl.iter().map(|&n| n),
        )
        .expect("failed to create buffer");

        let birth = CpuAccessibleBuffer::from_iter(
            device.clone(),
            BufferUsage::all(),
            brth.iter().map(|&n| n),
        )
        .expect("failed to create buffer");

        Grid {
            format: frmt.clone(),
            toroidal,
            survival,
            birth,
            width,
            height,
            cells: new_cells,
            device,
            queue,
        }
    }

    /// Returns a new `Grid` and initializes its cells randomly.
    pub fn new_random(
        frmt: &String,
        trdl: bool,
        srvl: &Vec<u32>,
        brth: &Vec<u32>,
        width: usize,
        height: usize,
    ) -> Grid {
        let mut new_grid = Grid::new(frmt, trdl, srvl, brth, width, height);
        new_grid.randomize();
        new_grid
    }

    /// Returns the file format used.
    pub fn get_format(&self) -> String {
        self.format.clone()
    }

    /// Sets a new file format for this `Grid`.
    pub fn set_format(&mut self, frmt: &String) {
        self.format = frmt.clone();
    }

    /// Returns `true` if the grid is toroidal. Otherwise the grid is resizable.
    pub fn is_toroidal(&self) -> bool {
        let toroidal = self.toroidal.read().unwrap();

        match *toroidal {
            0 => false,
            _ => true,
        }
    }

    /// Returns the survival conditions of the cellular automaton.
    pub fn get_survival(&self) -> Vec<u32> {
        self.survival.read().unwrap().to_vec()
    }

    /// Redefines the survival conditions of the cellular automaton.
    pub fn set_survival(&mut self, srvl: &Vec<u32>) {
        self.survival = CpuAccessibleBuffer::from_iter(
            self.device.clone(),
            BufferUsage::all(),
            srvl.iter().map(|&n| n),
        )
        .expect("failed to create buffer");
    }

    /// Returns the birth conditions of the cellular automaton.
    pub fn get_birth(&self) -> Vec<u32> {
        self.birth.read().unwrap().to_vec()
    }

    /// Redefines the birth conditions of the cellular automaton.
    pub fn set_birth(&mut self, brth: &Vec<u32>) {
        self.birth = CpuAccessibleBuffer::from_iter(
            self.device.clone(),
            BufferUsage::all(),
            brth.iter().map(|&n| n),
        )
        .expect("failed to create buffer");
    }

    /// Returns the width of the grid.
    pub fn get_width(&self) -> usize {
        self.width
    }

    /// Returns the height of the grid.
    pub fn get_height(&self) -> usize {
        self.height
    }

    /// Returns the state of the cell at the coordinates (`x`, `y`).
    ///
    /// If the coordinates are out of bounds and the grid is toroidal,
    /// then it returns the state of the cell at the coordinates
    /// modulo the size of the grid.
    /// Otherwise, if the coordinates are out of bounds but
    /// the grid is not toroidal, it returns `0u8`.
    pub fn get_cell_state(&self, x: i64, y: i64) -> u8 {
        let cells = self.cells.write().unwrap();

        if cells.is_empty() {
            return 0;
        }

        // If the `x` and `y` parameters are out of bound of the grid
        if x < 0 || y < 0 || x as usize >= self.width || y as usize >= self.height {
            if self.is_toroidal() {
                let (x, y) = (
                    if x < 0 {
                        (self.width as i64 + x) as usize
                    } else if x as usize >= self.width {
                        x as usize % self.width
                    } else {
                        x as usize
                    },
                    if y < 0 {
                        (self.height as i64 + y) as usize
                    } else if y as usize >= self.height {
                        y as usize % self.height
                    } else {
                        y as usize
                    },
                );
                cells[y * self.width + x]
            } else {
                0
            }
        } else {
            cells[y as usize * self.width + x as usize]
        }
    }

    /// Modifies the state of the cell at the coordinates (`x`, `y`)
    /// with `state`.
    /// Returns `Err(GridErrorKind::OutOfBoundCoords)` if the
    /// coordinates are out of bounds.
    pub fn set_cell_state(&mut self, x: usize, y: usize, state: u8) -> Result<(), GridErrorKind> {
        let mut cells = self.cells.write().unwrap();

        if x >= self.width || y >= self.height {
            Err(GridErrorKind::OutOfBoundCoords)
        } else {
            cells[y * self.width + x] = state;
            Ok(())
        }
    }
}

impl Clone for Grid {
    fn clone(&self) -> Grid {
        let new_format = self.get_format();
        let new_toroidal = self.is_toroidal();
        let new_survival = self.get_survival();
        let new_birth = self.get_birth();
        let new_width = self.get_width();
        let new_height = self.get_height();

        let mut new_grid = Grid::new(
            &new_format,
            new_toroidal,
            &new_survival,
            &new_birth,
            new_width,
            new_height,
        );

        new_grid.cells = CpuAccessibleBuffer::from_iter(
            new_grid.device.clone(),
            BufferUsage::all(),
            self.cells.read().unwrap().to_vec().into_iter(),
        )
        .expect("failed to create buffer");

        new_grid
    }
}

impl fmt::Debug for Grid {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        let Grid {
            ref format,
            ref toroidal,
            ref survival,
            ref birth,
            ref width,
            ref height,
            ..
        } = *self;

        write!(f, "Format:\n{:?}\nToroidal:\n{:?}\nSurvival:\n{:?}\nBirth:\n{:?}\nWidth:\n{:?}\nHeight:\n{:?}\nCells:\n{}", *format, *toroidal,  *survival, *birth, *width, height, self)
    }
}

impl fmt::Display for Grid {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        let Grid { width, height, .. } = *self;

        for y in 0..height {
            for x in 0..width {
                if self.get_cell_state(x as i64, y as i64) == 255 {
                    write!(f, "*")?;
                } else {
                    write!(f, ".")?;
                }
            }
            write!(f, "\n")?;
        }

        write!(f, "")
    }
}

#[cfg(test)]
mod tests {
    use super::vulkano::buffer::BufferUsage;
    use super::vulkano::buffer::CpuAccessibleBuffer;

    use super::vulkan;

    use Grid;

    #[test]
    fn test_toroidal_getters() {
        let (device, queue) = vulkan::vk_init();

        let cells_content: Vec<u8> = vec![0, 0, 0, 255, 255, 255, 0, 0, 0];
        let cells = CpuAccessibleBuffer::from_iter(
            device.clone(),
            BufferUsage::all(),
            cells_content.into_iter(),
        )
        .expect("failed to create buffer");

        let toroidal = CpuAccessibleBuffer::from_data(device.clone(), BufferUsage::all(), 1)
            .expect("failed to create buffer");

        let srvl_content: Vec<u32> = vec![2, 3];
        let survival = CpuAccessibleBuffer::from_iter(
            device.clone(),
            BufferUsage::all(),
            srvl_content.into_iter(),
        )
        .expect("failed to create buffer");

        let brth_content: Vec<u32> = vec![3];
        let birth = CpuAccessibleBuffer::from_iter(
            device.clone(),
            BufferUsage::all(),
            brth_content.into_iter(),
        )
        .expect("failed to create buffer");

        let control_grid = Grid {
            format: String::from("#Toroidal Life"),
            toroidal,
            survival,
            birth,
            width: 3,
            height: 3,
            cells,
            device,
            queue,
        };

        // Test meta-data getters.
        assert_eq!("#Toroidal Life", control_grid.get_format());
        assert_eq!(true, control_grid.is_toroidal());
        assert_eq!(vec![2, 3], control_grid.get_survival());
        assert_eq!(vec![3], control_grid.get_birth());
        assert_eq!(3, control_grid.get_width());
        assert_eq!(3, control_grid.get_height());

        // Test cells getter.
        assert_eq!(0, control_grid.get_cell_state(0, 0));
        assert_eq!(255, control_grid.get_cell_state(0, 1));
        assert_eq!(0, control_grid.get_cell_state(0, 2));
        assert_eq!(0, control_grid.get_cell_state(1, 0));
        assert_eq!(255, control_grid.get_cell_state(1, 1));
        assert_eq!(0, control_grid.get_cell_state(1, 2));
        assert_eq!(0, control_grid.get_cell_state(2, 0));
        assert_eq!(255, control_grid.get_cell_state(2, 1));
        assert_eq!(0, control_grid.get_cell_state(2, 2));

        // Test cells getter for out of bound values.
        assert_eq!(0, control_grid.get_cell_state(-1, -1));
        assert_eq!(0, control_grid.get_cell_state(3, 3));
        assert_eq!(255, control_grid.get_cell_state(-1, 1));
        assert_eq!(255, control_grid.get_cell_state(3, 1));
    }

    #[test]
    fn test_toroidal_setters() {
        let (device, queue) = vulkan::vk_init();

        let cells_content: Vec<u8> = vec![0, 0, 0, 255, 255, 255, 0, 0, 0];
        let cells = CpuAccessibleBuffer::from_iter(
            device.clone(),
            BufferUsage::all(),
            cells_content.into_iter(),
        )
        .expect("failed to create buffer");

        let toroidal = CpuAccessibleBuffer::from_data(device.clone(), BufferUsage::all(), 1)
            .expect("failed to create buffer");

        let srvl_content: Vec<u32> = vec![2, 3];
        let survival = CpuAccessibleBuffer::from_iter(
            device.clone(),
            BufferUsage::all(),
            srvl_content.into_iter(),
        )
        .expect("failed to create buffer");

        let brth_content: Vec<u32> = vec![3];
        let birth = CpuAccessibleBuffer::from_iter(
            device.clone(),
            BufferUsage::all(),
            brth_content.into_iter(),
        )
        .expect("failed to create buffer");

        let mut control_grid = Grid {
            format: String::from("#Toroidal Life"),
            toroidal,
            survival,
            birth,
            width: 3,
            height: 3,
            cells,
            device,
            queue,
        };

        control_grid.set_format(&String::from("#Resizable Life"));
        control_grid.set_survival(&vec![1, 7]);
        control_grid.set_birth(&vec![5]);
        control_grid.set_cell_state(0, 0, 255).unwrap();
        control_grid.set_cell_state(1, 1, 0).unwrap();

        // Check new meta-data.
        assert_eq!("#Resizable Life", control_grid.get_format());
        assert_eq!(vec![1, 7], control_grid.get_survival());
        assert_eq!(vec![5], control_grid.get_birth());

        // Check new cells.
        assert_eq!(255, control_grid.get_cell_state(0, 0));
        assert_eq!(255, control_grid.get_cell_state(0, 1));
        assert_eq!(0, control_grid.get_cell_state(0, 2));
        assert_eq!(0, control_grid.get_cell_state(1, 0));
        assert_eq!(0, control_grid.get_cell_state(1, 1));
        assert_eq!(0, control_grid.get_cell_state(1, 2));
        assert_eq!(0, control_grid.get_cell_state(2, 0));
        assert_eq!(255, control_grid.get_cell_state(2, 1));
        assert_eq!(0, control_grid.get_cell_state(2, 2));
    }

    #[test]
    fn test_resizable_getters() {
        let (device, queue) = vulkan::vk_init();

        let cells_content: Vec<u8> = vec![0, 0, 0, 255, 255, 255, 0, 0, 0];
        let cells = CpuAccessibleBuffer::from_iter(
            device.clone(),
            BufferUsage::all(),
            cells_content.into_iter(),
        )
        .expect("failed to create buffer");

        let toroidal = CpuAccessibleBuffer::from_data(device.clone(), BufferUsage::all(), 0)
            .expect("failed to create buffer");

        let srvl_content: Vec<u32> = vec![2, 3];
        let survival = CpuAccessibleBuffer::from_iter(
            device.clone(),
            BufferUsage::all(),
            srvl_content.into_iter(),
        )
        .expect("failed to create buffer");

        let brth_content: Vec<u32> = vec![3];
        let birth = CpuAccessibleBuffer::from_iter(
            device.clone(),
            BufferUsage::all(),
            brth_content.into_iter(),
        )
        .expect("failed to create buffer");

        let control_grid = Grid {
            format: String::from("#Resizable Life"),
            toroidal,
            survival,
            birth,
            width: 3,
            height: 3,
            cells,
            device,
            queue,
        };

        // Test meta-data getters.
        assert_eq!("#Resizable Life", control_grid.get_format());
        assert_eq!(false, control_grid.is_toroidal());
        assert_eq!(vec![2, 3], control_grid.get_survival());
        assert_eq!(vec![3], control_grid.get_birth());
        assert_eq!(3, control_grid.get_width());
        assert_eq!(3, control_grid.get_height());

        // Test cells getter.
        assert_eq!(0, control_grid.get_cell_state(0, 0));
        assert_eq!(255, control_grid.get_cell_state(0, 1));
        assert_eq!(0, control_grid.get_cell_state(0, 2));
        assert_eq!(0, control_grid.get_cell_state(1, 0));
        assert_eq!(255, control_grid.get_cell_state(1, 1));
        assert_eq!(0, control_grid.get_cell_state(1, 2));
        assert_eq!(0, control_grid.get_cell_state(2, 0));
        assert_eq!(255, control_grid.get_cell_state(2, 1));
        assert_eq!(0, control_grid.get_cell_state(2, 2));

        // Test cells getter for out of bound values.
        assert_eq!(0, control_grid.get_cell_state(-1, -1));
        assert_eq!(0, control_grid.get_cell_state(3, 3));
        assert_eq!(0, control_grid.get_cell_state(1, -1));
        assert_eq!(0, control_grid.get_cell_state(1, 3));
    }

    #[test]
    fn test_resizable_setters() {
        let (device, queue) = vulkan::vk_init();

        let cells_content: Vec<u8> = vec![0, 0, 0, 255, 255, 255, 0, 0, 0];
        let cells = CpuAccessibleBuffer::from_iter(
            device.clone(),
            BufferUsage::all(),
            cells_content.into_iter(),
        )
        .expect("failed to create buffer");

        let toroidal = CpuAccessibleBuffer::from_data(device.clone(), BufferUsage::all(), 0)
            .expect("failed to create buffer");

        let srvl_content: Vec<u32> = vec![2, 3];
        let survival = CpuAccessibleBuffer::from_iter(
            device.clone(),
            BufferUsage::all(),
            srvl_content.into_iter(),
        )
        .expect("failed to create buffer");

        let brth_content: Vec<u32> = vec![3];
        let birth = CpuAccessibleBuffer::from_iter(
            device.clone(),
            BufferUsage::all(),
            brth_content.into_iter(),
        )
        .expect("failed to create buffer");

        let mut control_grid = Grid {
            format: String::from("#Resizable Life"),
            toroidal,
            survival,
            birth,
            width: 3,
            height: 3,
            cells,
            device,
            queue,
        };

        control_grid.set_format(&String::from("#Toroidal Life"));
        control_grid.set_survival(&vec![1, 7]);
        control_grid.set_birth(&vec![5]);
        control_grid.set_cell_state(0, 0, 255).unwrap();
        control_grid.set_cell_state(1, 1, 0).unwrap();

        // Check new meta-data.
        assert_eq!("#Toroidal Life", control_grid.get_format());
        assert_eq!(vec![1, 7], control_grid.get_survival());
        assert_eq!(vec![5], control_grid.get_birth());

        // Check new cells.
        assert_eq!(255, control_grid.get_cell_state(0, 0));
        assert_eq!(255, control_grid.get_cell_state(0, 1));
        assert_eq!(0, control_grid.get_cell_state(0, 2));
        assert_eq!(0, control_grid.get_cell_state(1, 0));
        assert_eq!(0, control_grid.get_cell_state(1, 1));
        assert_eq!(0, control_grid.get_cell_state(1, 2));
        assert_eq!(0, control_grid.get_cell_state(2, 0));
        assert_eq!(255, control_grid.get_cell_state(2, 1));
        assert_eq!(0, control_grid.get_cell_state(2, 2));
    }
}