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, } 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: EmitIntensity::MAX, } } } impl GainCalculatorGenerator for FocalPoint { type Calculator = Impl; fn generate(&mut self, device: &Device) -> Self::Calculator { Impl { pos: self.pos, wavenumber: device.wavenumber(), } } } impl Gain for FocalPoint { type G = FocalPoint; fn init(self) -> Result<Self::G, GainError> { Ok(self) } } 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.