Creating Custom Gain

In the Rust library, you can create your own Gain.

NOTE: This feature is not available in the C++, C#, and Python libraries. However, you can use Custom for the same purpose.

Here, let’s actually define a FocalPoint that generates a single focal point like Focus.

use autd3::core::derive::*;
use autd3::prelude::*;

#[derive(Gain, Debug)]
pub struct FocalPoint {
    pos: Point3,
}

#[derive(Clone, Copy)]
pub struct Impl {
    pos: Point3,
    wavenumber: f32,
}

impl GainCalculator for Impl {
    fn calc(&self, tr: &Transducer) -> Drive {
        Drive {
            phase: Phase::from(-(self.pos - tr.position()).norm() * self.wavenumber * rad),
            intensity: Intensity::MAX,
        }
    }
}

impl GainCalculatorGenerator for Impl {
    type Calculator = Impl;

    fn generate(&mut self, _: &Device) -> Self::Calculator {
        *self
    }
}

impl Gain for FocalPoint {
    type G = Impl;

    fn init(
        self,
        _geometry: &Geometry,
        env: &Environment,
        _filter: &TransducerFilter,
    ) -> Result<Self::G, GainError> {
        Ok(Impl {
            pos: self.pos,
            wavenumber: env.wavenumber(),
        })
    }
}
fn main() {}

Gain::init returns a (struct that implements) GainContextGenerator that generates a (struct that implements) GainCalculator for each device. The actual phase and intensity calculations are performed in the GainCalculator::calc function.