Struct CellMap

Source
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,

Source

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.

Source

pub fn cell_size(&self) -> Vector2<f64>

Returns the size of the cells in the map.

Source

pub fn num_cells(&self) -> Vector2<usize>

Returns the number of cells in each direction of the map.

Source

pub fn cell_bounds(&self) -> Bounds

Returns the bounds of this map

Source

pub fn params(&self) -> CellMapParams

Returns the parameters used to build this map.

Source

pub fn to_parent(&self) -> Affine2<f64>

Gets the nalgebra::Affine2<f64> transformation between the map frame and the parent frame.

Source

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.

Source

pub fn index_in_map(&self, index: Point2<usize>) -> bool

Returns whether or not the given index is inside the map.

Source

pub fn position_in_map(&self, position: Point2<f64>) -> bool

Returns whether or not the given parent-relative position is inside the map.

Source

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.

Source

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.

Source

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.

Source

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.

Source

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.

Source

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

Source

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.

Source

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.

Source

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.

Source

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.

Source

pub fn iter(&self) -> CellMapIter<'_, L, T, Many<L>, Cells>

Returns an iterator over each cell in all layers of the map.

Source

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.

Source

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).

Source

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).

Source

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.

Source

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>
where L: Layer + Serialize, T: Clone + Serialize,

Source

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

pub fn write_json<P: AsRef<Path>>(&self, path: P) -> Result<(), Error>

Writes the map to the given path as a JSON file.

Source§

impl<L, T> CellMap<L, T>

Source

pub fn from_json<P: AsRef<Path>>(path: P) -> Result<Self, Error>

Loads a map stored in JSON format at the given path.

Source§

impl<L, T> CellMap<L, T>
where L: Layer, T: Clone,

Source

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>
where L: Layer, T: Default + Clone,

Source

pub fn new(params: CellMapParams) -> Self

Creates a new CellMap from the given params, filling each cell with T::default().

Examples found in repository?
examples/map_rotations_check.rs (lines 16-21)
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}
Source

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.

Source

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 others 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<L, T: Clone> Clone for CellMap<L, T>
where L: Layer + Clone,

Source§

fn clone(&self) -> CellMap<L, T>

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl<L, T: Debug> Debug for CellMap<L, T>
where L: Layer + Debug,

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<'de, L, T> Deserialize<'de> for CellMap<L, T>

Source§

fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>
where __D: Deserializer<'de>,

Deserialize this value from the given Serde deserializer. Read more
Source§

impl<L, T> From<CellMap<L, T>> for CellMapFile<L, T>
where L: Layer + Serialize, T: Clone + Serialize,

Source§

fn from(map: CellMap<L, T>) -> Self

Converts to this type from the input type.
Source§

impl<L, T> Index<(L, Point<usize, U2>)> for CellMap<L, T>
where L: Layer,

Source§

type Output = T

The returned type after indexing.
Source§

fn index(&self, index: (L, Point2<usize>)) -> &Self::Output

Performs the indexing (container[index]) operation. Read more
Source§

impl<L, T> Index<L> for CellMap<L, T>
where L: Layer,

Source§

type Output = ArrayBase<OwnedRepr<T>, Dim<[usize; 2]>>

The returned type after indexing.
Source§

fn index(&self, index: L) -> &Self::Output

Performs the indexing (container[index]) operation. Read more
Source§

impl<L, T> IndexMut<(L, Point<usize, U2>)> for CellMap<L, T>
where L: Layer,

Source§

fn index_mut(&mut self, index: (L, Point2<usize>)) -> &mut Self::Output

Performs the mutable indexing (container[index]) operation. Read more
Source§

impl<L, T> IndexMut<L> for CellMap<L, T>
where L: Layer,

Source§

fn index_mut(&mut self, index: L) -> &mut Self::Output

Performs the mutable indexing (container[index]) operation. Read more
Source§

impl<L, T> Serialize for CellMap<L, T>

Source§

fn serialize<__S>(&self, __serializer: __S) -> Result<__S::Ok, __S::Error>
where __S: Serializer,

Serialize this value into the given Serde serializer. Read more
Source§

impl<L, T> TryFrom<CellMapFile<L, T>> for CellMap<L, T>
where L: Layer,

Source§

type Error = Error

The type returned in the event of a conversion error.
Source§

fn try_from(value: CellMapFile<L, T>) -> Result<Self, Self::Error>

Performs the conversion.

Auto Trait Implementations§

§

impl<L, T> Freeze for CellMap<L, T>

§

impl<L, T> RefUnwindSafe for CellMap<L, T>

§

impl<L, T> Send for CellMap<L, T>
where L: Send, T: Send,

§

impl<L, T> Sync for CellMap<L, T>
where L: Sync, T: Sync,

§

impl<L, T> Unpin for CellMap<L, T>
where L: Unpin,

§

impl<L, T> UnwindSafe for CellMap<L, T>

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<SS, SP> SupersetOf<SS> for SP
where SS: SubsetOf<SP>,

Source§

fn to_subset(&self) -> Option<SS>

The inverse inclusion map: attempts to construct self from the equivalent element of its superset. Read more
Source§

fn is_in_subset(&self) -> bool

Checks if self is actually part of its subset T (and can be converted to it).
Source§

fn to_subset_unchecked(&self) -> SS

Use with care! Same as self.to_subset but without any property checks. Always succeeds.
Source§

fn from_subset(element: &SS) -> SP

The inclusion map: converts self to the equivalent element of its superset.
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<T> DeserializeOwned for T
where T: for<'de> Deserialize<'de>,