pub struct CellMap<L, T>where
L: Layer,{ /* private fields */ }
Expand description
Provides a many-layer 2D map of cellular data.
Implementations§
Source§impl<L, T> CellMap<L, T>where
L: Layer,
impl<L, T> CellMap<L, T>where
L: Layer,
Sourcepub fn new_from_data(
params: CellMapParams,
data: Vec<Array2<T>>,
) -> Result<Self, Error>
pub fn new_from_data( params: CellMapParams, data: Vec<Array2<T>>, ) -> Result<Self, Error>
Creates a new map from the given data.
If data is the wrong shape or has the wrong number of layers this function will return an error.
Sourcepub fn num_cells(&self) -> Vector2<usize>
pub fn num_cells(&self) -> Vector2<usize>
Returns the number of cells in each direction of the map.
Sourcepub fn cell_bounds(&self) -> Bounds
pub fn cell_bounds(&self) -> Bounds
Returns the bounds of this map
Sourcepub fn params(&self) -> CellMapParams
pub fn params(&self) -> CellMapParams
Returns the parameters used to build this map.
Sourcepub fn to_parent(&self) -> Affine2<f64>
pub fn to_parent(&self) -> Affine2<f64>
Gets the nalgebra::Affine2<f64>
transformation between the map frame and the parent
frame.
Sourcepub fn move_map(
&mut self,
position_in_parent: Vector2<f64>,
rotation_in_parent_rad: f64,
)
pub fn move_map( &mut self, position_in_parent: Vector2<f64>, rotation_in_parent_rad: f64, )
Moves this map relative to a new position and rotation relative to the parent frame.
Note: This doesn’t move the data relative to the map origin, the indexes into the map remain the same, but the position of each cell in the map will change.
Sourcepub fn index_in_map(&self, index: Point2<usize>) -> bool
pub fn index_in_map(&self, index: Point2<usize>) -> bool
Returns whether or not the given index is inside the map.
Sourcepub fn position_in_map(&self, position: Point2<f64>) -> bool
pub fn position_in_map(&self, position: Point2<f64>) -> bool
Returns whether or not the given parent-relative position is inside the map.
Sourcepub fn get(&self, layer: L, index: Point2<usize>) -> Option<&T>
pub fn get(&self, layer: L, index: Point2<usize>) -> Option<&T>
Get a reference to the value at the given layer and index. Returns None
if the index is
outside the bounds of the map.
Sourcepub unsafe fn get_unchecked(&self, layer: L, index: Point2<usize>) -> &T
pub unsafe fn get_unchecked(&self, layer: L, index: Point2<usize>) -> &T
Get a reference to the value at the given layer and index, without checking the bounds of the map.
§Safety
This function will panic if index
is outside the map.
Sourcepub fn get_mut(&mut self, layer: L, index: Point2<usize>) -> Option<&mut T>
pub fn get_mut(&mut self, layer: L, index: Point2<usize>) -> Option<&mut T>
Get a mutable reference to the value at the given layer and index. Returns None
if the
index is outside the bounds of the map.
Sourcepub unsafe fn get_mut_unchecked(
&mut self,
layer: L,
index: Point2<usize>,
) -> &mut T
pub unsafe fn get_mut_unchecked( &mut self, layer: L, index: Point2<usize>, ) -> &mut T
Get a mutable reference to the value at the given layer and index, without checking the bounds of the map.
§Safety
This function will panic if index
is outside the map.
Sourcepub fn set(
&mut self,
layer: L,
index: Point2<usize>,
value: T,
) -> Result<(), Error>
pub fn set( &mut self, layer: L, index: Point2<usize>, value: T, ) -> Result<(), Error>
Set the given layer and index in the map to the given value. Returns an Error
if the
index was outside the map.
Sourcepub unsafe fn set_unchecked(&mut self, layer: L, index: Point2<usize>, value: T)
pub unsafe fn set_unchecked(&mut self, layer: L, index: Point2<usize>, value: T)
Set the given layer and index in the map to the given value, without checking if index is the map.
§Safety
This function will panic if index
is outside the map
Sourcepub fn position(&self, index: Point2<usize>) -> Option<Point2<f64>>
pub fn position(&self, index: Point2<usize>) -> Option<Point2<f64>>
Returns the position in the parent frame of the centre of the given cell index.
Returns None
if the given index
is not inside the map.
Sourcepub fn position_unchecked(&self, index: Point2<usize>) -> Point2<f64>
pub fn position_unchecked(&self, index: Point2<usize>) -> Point2<f64>
Returns the position in the parent frame of the centre of the given cell index, without
checking that the index
is inside the map.
§Safety
This method won’t panic if index
is outside the map, but it’s result can’t be guaranteed
to be a position in the map.
Sourcepub fn index(&self, position: Point2<f64>) -> Option<Point2<usize>>
pub fn index(&self, position: Point2<f64>) -> Option<Point2<usize>>
Get the cell index of the given poisition.
Returns None
if the given position
is not inside the map.
Sourcepub unsafe fn index_unchecked(&self, position: Point2<f64>) -> Point2<isize>
pub unsafe fn index_unchecked(&self, position: Point2<f64>) -> Point2<isize>
Get the cell index of the given poisition, without checking that the position is inside the map.
§Safety
This function will not panic if position
is outside the map, but use of the result to
index into the map is not guaranteed to be safe. It is possible for this function to return
a negative index value, which would indicate that the cell is outside the map.
Sourcepub fn iter(&self) -> CellMapIter<'_, L, T, Many<L>, Cells> ⓘ
pub fn iter(&self) -> CellMapIter<'_, L, T, Many<L>, Cells> ⓘ
Returns an iterator over each cell in all layers of the map.
Sourcepub fn iter_mut(&mut self) -> CellMapIterMut<'_, L, T, Many<L>, Cells> ⓘ
pub fn iter_mut(&mut self) -> CellMapIterMut<'_, L, T, Many<L>, Cells> ⓘ
Returns a mutable iterator over each cell in all layers of the map.
Sourcepub fn window_iter(
&self,
semi_width: Vector2<usize>,
) -> Result<CellMapIter<'_, L, T, Many<L>, Windows>, Error>
pub fn window_iter( &self, semi_width: Vector2<usize>, ) -> Result<CellMapIter<'_, L, T, Many<L>, Windows>, Error>
Returns an iterator over windows of cells in the map.
The semi_width
is half the size of the window in the x and y axes, not including
the central cell. E.g. to have a window which is in total 5x5, the semi_window_size
needs
to be Vector2::new(2, 2)
.
Sourcepub fn window_iter_mut(
&mut self,
semi_width: Vector2<usize>,
) -> Result<CellMapIterMut<'_, L, T, Many<L>, Windows>, Error>
pub fn window_iter_mut( &mut self, semi_width: Vector2<usize>, ) -> Result<CellMapIterMut<'_, L, T, Many<L>, Windows>, Error>
Returns a mutable iterator over windows of cells in the map.
The semi_width
is half the size of the window in the x and y axes, not including
the central cell. E.g. to have a window which is in total 5x5, the semi_window_size
needs
to be Vector2::new(2, 2)
.
Sourcepub fn line_iter(
&self,
start_position: Point2<f64>,
end_position: Point2<f64>,
) -> Result<CellMapIter<'_, L, T, Many<L>, Line>, Error>
pub fn line_iter( &self, start_position: Point2<f64>, end_position: Point2<f64>, ) -> Result<CellMapIter<'_, L, T, Many<L>, Line>, Error>
Returns an iterator over cells along the line joining start_position
and
end_position
, which are expressed as positions in the map’s parent frame.
Sourcepub fn line_iter_mut(
&mut self,
start_position: Point2<f64>,
end_position: Point2<f64>,
) -> Result<CellMapIterMut<'_, L, T, Many<L>, Line>, Error>
pub fn line_iter_mut( &mut self, start_position: Point2<f64>, end_position: Point2<f64>, ) -> Result<CellMapIterMut<'_, L, T, Many<L>, Line>, Error>
Returns a mutable iterator over cells along the line joining start_position
and
end_position
, which are expressed as positions in the map’s parent frame.
Source§impl<L, T> CellMap<L, T>
impl<L, T> CellMap<L, T>
Sourcepub fn to_cell_map_file(&self) -> CellMapFile<L, T>
pub fn to_cell_map_file(&self) -> CellMapFile<L, T>
Builds a new CellMapFile
from the given map, which can be serialised or deserialised
using serde.
Source§impl<L, T> CellMap<L, T>
impl<L, T> CellMap<L, T>
Sourcepub fn new_from_elem(params: CellMapParams, elem: T) -> Self
pub fn new_from_elem(params: CellMapParams, elem: T) -> Self
Creates a new CellMap
from the given params, filling each cell with elem
.
Source§impl<L, T> CellMap<L, T>
impl<L, T> CellMap<L, T>
Sourcepub fn new(params: CellMapParams) -> Self
pub fn new(params: CellMapParams) -> Self
Creates a new CellMap
from the given params, filling each cell with T::default()
.
Examples found in repository?
15fn main() {
16 let translated = CellMap::<Layers, f64>::new(CellMapParams {
17 cell_size: Vector2::new(1.0, 1.0),
18 cell_bounds: Bounds::new((0, 10), (0, 10)).unwrap(),
19 position_in_parent: Vector2::new(5.0, 5.0),
20 ..Default::default()
21 });
22
23 let rotated = CellMap::<Layers, f64>::new(CellMapParams {
24 cell_bounds: Bounds::new((0, 10), (0, 10)).unwrap(),
25 cell_size: Vector2::new(1.0, 1.0),
26 position_in_parent: Vector2::new(5.0, 5.0),
27 rotation_in_parent_rad: std::f64::consts::FRAC_PI_4,
28 ..Default::default()
29 });
30
31 let scaled = CellMap::<Layers, f64>::new(CellMapParams {
32 cell_bounds: Bounds::new((0, 10), (0, 10)).unwrap(),
33 cell_size: Vector2::new(0.5, 0.5),
34 position_in_parent: Vector2::new(5.0, 5.0),
35 rotation_in_parent_rad: std::f64::consts::FRAC_PI_4,
36 ..Default::default()
37 });
38
39 #[cfg(all(feature = "debug_maps"))]
40 {
41 use cell_map::write_debug_map;
42
43 write_debug_map(&translated, "translated");
44 write_debug_map(&rotated, "rotated");
45 write_debug_map(&scaled, "scaled");
46 }
47}
Sourcepub fn resize(&mut self, new_bounds: Bounds)
pub fn resize(&mut self, new_bounds: Bounds)
Resizes the map into the new bounds, filling any newly added cells with T::default()
.
Any cells that are in the map currently, which would be outside the new map, are removed.
Sourcepub fn merge<F: Fn(&T, &[T]) -> T>(&mut self, other: &CellMap<L, T>, func: F)
pub fn merge<F: Fn(&T, &[T]) -> T>(&mut self, other: &CellMap<L, T>, func: F)
Merge other
into self, resizing self
so that other
will be fully included in the map.
Both maps should belong to the same parent frame, and other.cell_size <= self.cell_size
.
For other
s that have larger cells than self
you should implement your own merge functon
based on this one that could for example use 2D linear interpolation.
func
is responsible for actually merging data in both self
and other
into a single
new value in self
. The first argument is the value of the cell in self
, while the
second argument will be the values from cells in other
whose centres lie within the cell
in self
.
Trait Implementations§
Source§impl<'de, L, T> Deserialize<'de> for CellMap<L, T>
impl<'de, L, T> Deserialize<'de> for CellMap<L, T>
Source§fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>where
__D: Deserializer<'de>,
fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>where
__D: Deserializer<'de>,
Source§impl<L, T> From<CellMap<L, T>> for CellMapFile<L, T>
impl<L, T> From<CellMap<L, T>> for CellMapFile<L, T>
Auto Trait Implementations§
impl<L, T> Freeze for CellMap<L, T>
impl<L, T> RefUnwindSafe for CellMap<L, T>where
L: RefUnwindSafe,
T: RefUnwindSafe,
impl<L, T> Send for CellMap<L, T>
impl<L, T> Sync for CellMap<L, T>
impl<L, T> Unpin for CellMap<L, T>where
L: Unpin,
impl<L, T> UnwindSafe for CellMap<L, T>where
L: UnwindSafe,
T: RefUnwindSafe,
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<SS, SP> SupersetOf<SS> for SPwhere
SS: SubsetOf<SP>,
impl<SS, SP> SupersetOf<SS> for SPwhere
SS: SubsetOf<SP>,
Source§fn to_subset(&self) -> Option<SS>
fn to_subset(&self) -> Option<SS>
self
from the equivalent element of its
superset. Read moreSource§fn is_in_subset(&self) -> bool
fn is_in_subset(&self) -> bool
self
is actually part of its subset T
(and can be converted to it).Source§fn to_subset_unchecked(&self) -> SS
fn to_subset_unchecked(&self) -> SS
self.to_subset
but without any property checks. Always succeeds.Source§fn from_subset(element: &SS) -> SP
fn from_subset(element: &SS) -> SP
self
to the equivalent element of its superset.