cellular_raza_concepts/
interaction.rs

1use crate::errors::CalcError;
2
3/// Exposes information which is used to calculate interactions between cells and the domain.
4pub trait InteractionInformation<Inf> {
5    /// Get additional information of cellular properties (ie. for cell-specific interactions).
6    fn get_interaction_information(&self) -> Inf;
7}
8
9/// Trait describing force-interactions between cellular agents.
10pub trait Interaction<Pos, Vel, Force, Inf = ()>: InteractionInformation<Inf> {
11    /// Calculates the forces (velocity-derivative) on the corresponding external position given
12    /// external velocity.
13    /// By providing velocities, we can calculate terms that are related to friction.
14    /// The function returns two forces, one acting on the current agent and the other on the
15    /// external agent.
16    fn calculate_force_between(
17        &self,
18        own_pos: &Pos,
19        own_vel: &Vel,
20        ext_pos: &Pos,
21        ext_vel: &Vel,
22        ext_info: &Inf,
23    ) -> Result<(Force, Force), CalcError>;
24}
25
26/// Allows reacting to multiple neighbors
27///
28/// Information of neighbors is first accumulated via the [NeighborSensing::accumulate_information]
29/// function and finally, the cell reacts to the gathered data via the
30/// [NeighborSensing::react_to_neighbors] function.
31/// Finally, the accumulator is cleared with [NeighborSensing::clear_accumulator].
32///
33/// ## Neighbor Counting
34/// ```
35/// use cellular_raza_concepts::*;
36///
37/// struct Cell {/* .. */};
38///
39/// impl<Pos, Inf> NeighborSensing<Pos, usize, Inf> for Cell
40/// where
41///     Cell: InteractionInformation<Inf>,
42/// {
43///     fn accumulate_information(
44///         &self,
45///         _: &Pos,
46///         _: &Pos,
47///         _: &Inf,
48///         neighbors: &mut usize
49///     ) -> Result<(), CalcError> {
50///         Ok(*neighbors += 1)
51///     }
52///
53///     fn react_to_neighbors(&mut self, neighbors: &usize) -> Result<(), CalcError> {
54///         /* .. */
55///         Ok(())
56///     }
57///
58///     fn clear_accumulator(neighbors: &mut usize) {
59///         *neighbors = 0;
60///     }
61/// }
62///
63/// ```
64pub trait NeighborSensing<Pos, Acc, Inf = ()>: InteractionInformation<Inf> {
65    /// Checks if the other cell represented by position and information is a neighbor to the current one or not.
66    fn accumulate_information(
67        &self,
68        own_pos: &Pos,
69        ext_pos: &Pos,
70        ext_inf: &Inf,
71        accumulator: &mut Acc,
72    ) -> Result<(), CalcError>;
73
74    /// Reacts to the results gathered by the [Interaction::is_neighbor]
75    /// method and changes the state of the cell.
76    fn react_to_neighbors(&mut self, accumulator: &Acc) -> Result<(), CalcError>;
77
78    /// Clears the accumulator
79    fn clear_accumulator(accumulator: &mut Acc);
80}