autd3_core/gain/
mod.rs

1mod drive;
2mod emit_intensity;
3mod error;
4mod phase;
5
6use std::collections::HashMap;
7
8/// A bit vector type.
9pub type BitVec = bit_vec::BitVec<u32>;
10
11pub use drive::Drive;
12pub use emit_intensity::EmitIntensity;
13pub use error::GainError;
14pub use phase::Phase;
15
16use crate::{
17    datagram::{Segment, TransitionMode},
18    geometry::{Device, Geometry, Transducer},
19};
20
21/// A trait to calculate the phase and intensity for [`Gain`].
22///
23/// [`Gain`]: crate::gain::Gain
24pub trait GainCalculator: Send + Sync {
25    /// Calculates the phase and intensity for the transducer.
26    #[must_use]
27    fn calc(&self, tr: &Transducer) -> Drive;
28}
29
30impl GainCalculator for Box<dyn GainCalculator> {
31    fn calc(&self, tr: &Transducer) -> Drive {
32        self.as_ref().calc(tr)
33    }
34}
35
36/// A trait for generating a calculator for the gain operation.
37pub trait GainCalculatorGenerator {
38    /// The type of the calculator that actually performs the calculation.
39    type Calculator: GainCalculator;
40
41    /// Generate a calculator for the given device.
42    #[must_use]
43    fn generate(&mut self, device: &Device) -> Self::Calculator;
44}
45
46/// Trait for calculating the phase/amplitude of each transducer.
47///
48/// See also [`Gain`] derive macro.
49///
50/// [`Gain`]: autd3_derive::Gain
51pub trait Gain: std::fmt::Debug + Sized {
52    /// The type of the calculator generator.
53    type G: GainCalculatorGenerator;
54
55    /// Initialize the gain and generate the calculator generator.
56    ///
57    /// `filter` is a hash map that holds a bit vector representing the indices of the enabled transducers for each device index.
58    /// If `filter` is `None`, all transducers are enabled.
59    fn init(
60        self,
61        geometry: &Geometry,
62        filter: Option<&HashMap<usize, BitVec>>,
63    ) -> Result<Self::G, GainError>;
64}
65
66#[doc(hidden)]
67pub struct GainOperationGenerator<G: GainCalculatorGenerator> {
68    pub generator: G,
69    pub segment: Segment,
70    pub transition: Option<TransitionMode>,
71}
72
73impl<G: GainCalculatorGenerator> GainOperationGenerator<G> {
74    pub fn new<T: Gain<G = G>>(
75        gain: T,
76        geometry: &Geometry,
77        segment: Segment,
78        transition: Option<TransitionMode>,
79    ) -> Result<Self, GainError> {
80        Ok(Self {
81            generator: gain.init(geometry, None)?,
82            segment,
83            transition,
84        })
85    }
86}
87
88#[derive(Debug, Clone, PartialEq)]
89/// The result of the [`Gain`] inspection.
90pub struct GainInspectionResult {
91    /// The type name of the gain.
92    pub name: String,
93    /// The data of the gain.
94    pub data: Vec<Drive>,
95    /// The segment of the gain.
96    pub segment: Segment,
97    /// The transition mode of the gain.
98    pub transition_mode: Option<TransitionMode>,
99}