2019-04-11 22:36:55 -04:00
|
|
|
// pathfinder/renderer/src/tile_map.rs
|
|
|
|
//
|
|
|
|
// Copyright © 2019 The Pathfinder Project Developers.
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
|
|
|
|
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
|
|
|
|
// option. This file may not be copied, modified, or distributed
|
|
|
|
// except according to those terms.
|
|
|
|
|
2019-06-21 13:06:19 -04:00
|
|
|
use pathfinder_geometry::vector::Vector2I;
|
|
|
|
use pathfinder_geometry::rect::RectI;
|
2019-04-11 22:36:55 -04:00
|
|
|
|
|
|
|
#[derive(Debug)]
|
|
|
|
pub struct DenseTileMap<T> {
|
|
|
|
pub data: Vec<T>,
|
2019-05-29 22:13:42 -04:00
|
|
|
pub rect: RectI,
|
2019-04-11 22:36:55 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
impl<T> DenseTileMap<T> {
|
|
|
|
#[inline]
|
2019-05-29 22:13:42 -04:00
|
|
|
pub fn new(rect: RectI) -> DenseTileMap<T>
|
2019-04-29 19:45:29 -04:00
|
|
|
where
|
|
|
|
T: Copy + Clone + Default,
|
|
|
|
{
|
2019-04-11 22:36:55 -04:00
|
|
|
let length = rect.size().x() as usize * rect.size().y() as usize;
|
2019-04-29 19:45:29 -04:00
|
|
|
DenseTileMap {
|
|
|
|
data: vec![T::default(); length],
|
|
|
|
rect,
|
|
|
|
}
|
2019-04-11 22:36:55 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
2019-05-29 22:13:42 -04:00
|
|
|
pub fn from_builder<F>(build: F, rect: RectI) -> DenseTileMap<T>
|
2019-04-29 19:45:29 -04:00
|
|
|
where
|
|
|
|
F: FnMut(usize) -> T,
|
|
|
|
{
|
2019-04-11 22:36:55 -04:00
|
|
|
let length = rect.size().x() as usize * rect.size().y() as usize;
|
2019-04-29 19:45:29 -04:00
|
|
|
DenseTileMap {
|
|
|
|
data: (0..length).map(build).collect(),
|
|
|
|
rect,
|
|
|
|
}
|
2019-04-11 22:36:55 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
2019-06-03 15:39:29 -04:00
|
|
|
pub fn coords_to_index(&self, coords: Vector2I) -> Option<usize> {
|
2019-06-25 17:43:13 -04:00
|
|
|
if self.rect.contains_point(coords) {
|
|
|
|
Some(self.coords_to_index_unchecked(coords))
|
|
|
|
} else {
|
|
|
|
None
|
2019-04-11 22:36:55 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
2019-06-03 15:39:29 -04:00
|
|
|
pub fn coords_to_index_unchecked(&self, coords: Vector2I) -> usize {
|
2019-04-29 19:45:29 -04:00
|
|
|
(coords.y() - self.rect.min_y()) as usize * self.rect.size().x() as usize
|
|
|
|
+ (coords.x() - self.rect.min_x()) as usize
|
2019-04-11 22:36:55 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
2019-06-03 15:39:29 -04:00
|
|
|
pub fn index_to_coords(&self, index: usize) -> Vector2I {
|
2019-04-11 22:36:55 -04:00
|
|
|
let (width, index) = (self.rect.size().x(), index as i32);
|
2019-06-03 15:39:29 -04:00
|
|
|
self.rect.origin() + Vector2I::new(index % width, index / width)
|
2019-04-11 22:36:55 -04:00
|
|
|
}
|
|
|
|
}
|