pub struct Vol<C, O = ZMaj> { /* private fields */ }Expand description
Type for volume data stored in a slice, or for generating linear indexing.
Cis some slice container type, e.g.&[T]orBox<[T]>. It may also be()to describe a linearization without actually storing data.Ospecifies the choice of linearization. Currently, only one choice exists,ZMaj.
In addition to the data, each Vol stores the GridAab defining its size;
the container’s length must be equal to the volume of that AAB. Whether that can be
relied upon entirely depends on whether the specific container value produces
the same length of slice every time it is Dereferenced without mutating it directly.
For example, Vec<T> and Box<[T]> satisfy this criterion; the fact that Vec has
length-mutating operations is irrelevant because no &mut Vec<T> is exposed.
A Vol whose volume exceeds usize::MAX cannot exist.
Implementations§
Source§impl<O> Vol<(), O>
impl<O> Vol<(), O>
Sourcepub fn with_elements<C, V>(
self,
elements: C,
) -> Result<Vol<C, O>, VolLengthError>
pub fn with_elements<C, V>( self, elements: C, ) -> Result<Vol<C, O>, VolLengthError>
Attach some data to this dataless Vol.
Returns a VolLengthError if the number of elements does not match
bounds.volume().
Source§impl Vol<(), ZMaj>
impl Vol<(), ZMaj>
Sourcepub fn subdivide(self) -> Result<[Self; 2], Self>
pub fn subdivide(self) -> Result<[Self; 2], Self>
Divide self into two approximately equal-sized parts which, if they had elements, would
each be contiguous in the linear ordering.
Returns None if self does not have at least two cubes.
Note that this is one of several subdivide() methods for different container types;
it is also implemented for immutable and mutable references.
These are intended to be useful in executing parallel algorithms on volume data.
Source§impl<C, O: Default, V> Vol<C, O>
Constructors from linear containers.
impl<C, O: Default, V> Vol<C, O>
Constructors from linear containers.
Sourcepub fn from_elements(
bounds: GridAab,
elements: impl Into<C>,
) -> Result<Self, VolLengthError>
pub fn from_elements( bounds: GridAab, elements: impl Into<C>, ) -> Result<Self, VolLengthError>
Constructs a Vol<C> containing the provided elements, which must be in the
ordering specified by O.
Returns a VolLengthError if the number of elements does not match
bounds.volume().
Source§impl<C, V> Vol<C, ZMaj>
Constructors from elements.
impl<C, V> Vol<C, ZMaj>
Constructors from elements.
Sourcepub fn from_fn<F>(bounds: GridAab, f: F) -> Self
pub fn from_fn<F>(bounds: GridAab, f: F) -> Self
Constructs a Vol<C> by using the provided function to compute a value
for each point.
Panics if bounds has a volume exceeding usize::MAX.
(But there will likely be a memory allocation failure well below that point.)
Sourcepub fn repeat(bounds: GridAab, value: V) -> Selfwhere
V: Clone,
pub fn repeat(bounds: GridAab, value: V) -> Selfwhere
V: Clone,
Constructs a Vol<C> by cloning the provided value for each point.
Sourcepub fn from_element(value: V) -> Self
pub fn from_element(value: V) -> Self
Constructs a Vol<C> with a single value, in bounds ORIGIN_CUBE.
If the single element should be at a different location, you can call
.translate(offset), or use Vol::from_elements()
instead.
Source§impl<C, O> Vol<C, O>
impl<C, O> Vol<C, O>
Sourcepub fn into_elements(self) -> C
pub fn into_elements(self) -> C
Extracts the linear contents, discarding the bounds and ordering.
Sourcepub fn without_elements(&self) -> Vol<(), O>where
O: Clone,
pub fn without_elements(&self) -> Vol<(), O>where
O: Clone,
Returns a Vol with the same bounds and ordering but no data.
This is the inverse operation to Vol::with_elements().
Sourcepub fn translate(self, offset: impl Into<GridVector>) -> Self
pub fn translate(self, offset: impl Into<GridVector>) -> Self
Translates the volume without affecting its contents.
Panics if this would cause numeric overflow.
TODO: example
Source§impl<C> Vol<C, ZMaj>
impl<C> Vol<C, ZMaj>
Sourcepub fn iter_cubes(&self) -> GridIter ⓘ
pub fn iter_cubes(&self) -> GridIter ⓘ
Iterate over all cubes that this contains, in the order of the linearization, without including the stored data (if there is any).
Sourcepub fn index(&self, cube: Cube) -> Option<usize>
pub fn index(&self, cube: Cube) -> Option<usize>
Determines whether a unit cube lies within this volume and, if it does, returns the linearized slice index into it.
The linearized element order is defined by the O type.
use all_is_cubes::math::{Vol, GridAab};
let vol = GridAab::from_lower_size([0, 0, 0], [10, 10, 10]).to_vol().unwrap();
assert_eq!(vol.index([0, 0, 0].into()), Some(0));
assert_eq!(vol.index([1, 2, 3].into()), Some(123));
assert_eq!(vol.index([9, 9, 9].into()), Some(999));
assert_eq!(vol.index([0, 0, -1].into()), None);
assert_eq!(vol.index([0, 0, 10].into()), None);TODO: more example, less unit-test
Source§impl<C, O, V> Vol<C, O>
Linear data access.
impl<C, O, V> Vol<C, O>
Linear data access.
Source§impl<'a, V> Vol<&'a [V], ZMaj>
impl<'a, V> Vol<&'a [V], ZMaj>
Sourcepub fn get_ref(&self, position: impl Into<Cube>) -> Option<&'a V>
pub fn get_ref(&self, position: impl Into<Cube>) -> Option<&'a V>
Returns the element at position of this volume data, or None if position is out
of bounds.
This differs from Self::get() in that it inherits the lifetime of the container
reference, rather than reborrowing. It is therefore only available for Vol<&'a [V]>.
Sourcepub fn subdivide(self) -> Result<[Self; 2], Self>
pub fn subdivide(self) -> Result<[Self; 2], Self>
Divide self into two approximately equal-sized parts,
each of which refers to the appropriate sub-slice of elements.
Returns None if self does not have at least two cubes.
Note that this is one of several subdivide() methods for different container types;
it is also implemented for mutable references and ().
These are intended to be useful in executing parallel algorithms on volume data.
Source§impl<V> Vol<&mut [V], ZMaj>
impl<V> Vol<&mut [V], ZMaj>
Sourcepub fn subdivide(
self,
filter: impl FnOnce([Vol<()>; 2]) -> bool,
) -> Result<[Self; 2], Self>
pub fn subdivide( self, filter: impl FnOnce([Vol<()>; 2]) -> bool, ) -> Result<[Self; 2], Self>
Divide self into two approximately equal-sized parts.
each of which refers to the appropriate sub-slice of elements.
filter may be used to reject subdivisions that are too small or otherwise unsuitable;
it is passed the bounds of the two slices that would be returned.
Returns Err containing self if self does not have at least two cubes,
or if filter returns false.
Note that this is one of several subdivide() methods for different container types;
it is also implemented for immutable references and ().
These are intended to be useful in executing parallel algorithms on volume data.
Source§impl<C, V> Vol<C, ZMaj>
Element lookup operations by 3D coordinates.
impl<C, V> Vol<C, ZMaj>
Element lookup operations by 3D coordinates.
Sourcepub fn get(&self, position: impl Into<Cube>) -> Option<&V>
pub fn get(&self, position: impl Into<Cube>) -> Option<&V>
Returns the element at position of this volume data, or None if position is out
of bounds.
Sourcepub fn get_mut(&mut self, position: impl Into<Cube>) -> Option<&mut V>where
C: DerefMut,
pub fn get_mut(&mut self, position: impl Into<Cube>) -> Option<&mut V>where
C: DerefMut,
Returns a mutable reference to the element at position of this volume data,
or None if position is out of bounds.