com.shinolab.autd3-sdk.pattern 0.8.0 → 0.10.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/Pattern.cs CHANGED
@@ -7,113 +7,218 @@ using System.Runtime.InteropServices;
7
7
 
8
8
  namespace AUTD3
9
9
  {
10
- [StructLayout(LayoutKind.Sequential)]
11
- internal struct PatternOptionNative
10
+ public readonly struct LaguerreGaussianOption
12
11
  {
13
- public byte Intensity;
14
- public byte PhaseOffset;
12
+ public uint P { get; }
13
+ public int L { get; }
14
+ public Length Waist { get; }
15
+
16
+ public LaguerreGaussianOption(uint p, int l, Length waist)
17
+ {
18
+ P = p;
19
+ L = l;
20
+ Waist = waist;
21
+ }
15
22
  }
16
23
 
17
- public readonly struct FocusOption
24
+ public readonly struct HermiteGaussianOption
18
25
  {
19
- public Intensity Intensity { get; }
20
- public Phase PhaseOffset { get; }
26
+ public uint M { get; }
27
+ public uint N { get; }
28
+ public Length Waist { get; }
21
29
 
22
- public FocusOption() : this(intensity: null)
30
+ public HermiteGaussianOption(uint m, uint n, Length waist)
23
31
  {
32
+ M = m;
33
+ N = n;
34
+ Waist = waist;
24
35
  }
36
+ }
37
+
38
+ public readonly struct TransducerMask
39
+ {
40
+ internal bool[][]? Mask { get; }
25
41
 
26
- public FocusOption(Intensity? intensity = null, Phase? phaseOffset = null)
42
+ private TransducerMask(bool[][]? mask)
27
43
  {
28
- Intensity = intensity ?? Intensity.Max;
29
- PhaseOffset = phaseOffset ?? Phase.Zero;
44
+ Mask = mask;
30
45
  }
31
46
 
32
- internal PatternOptionNative ToNative() =>
33
- new PatternOptionNative { Intensity = Intensity.Value, PhaseOffset = PhaseOffset.Value };
34
- }
47
+ public static TransducerMask AllEnabled => new TransducerMask(null);
35
48
 
36
- public readonly struct PlaneOption
37
- {
38
- public Intensity Intensity { get; }
39
- public Phase PhaseOffset { get; }
49
+ public static TransducerMask Masked(bool[][] mask) => new TransducerMask(mask);
40
50
 
41
- public PlaneOption() : this(intensity: null)
51
+ public void Validate(Geometry geometry)
42
52
  {
53
+ if (Mask is null)
54
+ {
55
+ return;
56
+ }
57
+ if (Mask.Length != geometry.NumDevices)
58
+ {
59
+ throw new Autd3Exception(
60
+ $"the mask has {Mask.Length} device slots but the geometry has {geometry.NumDevices} devices",
61
+ Autd3ErrorCode.InvalidArgument);
62
+ }
63
+ var dev = 0;
64
+ foreach (var device in geometry)
65
+ {
66
+ var got = Mask[dev]?.Length ?? 0;
67
+ if (got != device.NumTransducers)
68
+ {
69
+ throw new Autd3Exception(
70
+ $"the mask slot for device {dev} has {got} transducers but the device has {device.NumTransducers}",
71
+ Autd3ErrorCode.InvalidArgument);
72
+ }
73
+ dev++;
74
+ }
43
75
  }
44
76
 
45
- public PlaneOption(Intensity? intensity = null, Phase? phaseOffset = null)
77
+ public bool IsEnabled(int device, int transducer) => Mask is null || Mask[device][transducer];
78
+
79
+ public int NumEnabled(Geometry geometry)
46
80
  {
47
- Intensity = intensity ?? Intensity.Max;
48
- PhaseOffset = phaseOffset ?? Phase.Zero;
81
+ if (Mask is null)
82
+ {
83
+ return geometry.NumTransducers;
84
+ }
85
+ var count = 0;
86
+ foreach (var device in Mask)
87
+ {
88
+ foreach (var enabled in device)
89
+ {
90
+ if (enabled)
91
+ {
92
+ count++;
93
+ }
94
+ }
95
+ }
96
+ return count;
49
97
  }
50
-
51
- internal PatternOptionNative ToNative() =>
52
- new PatternOptionNative { Intensity = Intensity.Value, PhaseOffset = PhaseOffset.Value };
53
98
  }
54
99
 
55
- public readonly struct BesselOption
100
+ public sealed class TransducerGroups<TKey> where TKey : notnull
56
101
  {
57
- public Intensity Intensity { get; }
58
- public Phase PhaseOffset { get; }
102
+ private readonly int[] _numTransducers;
103
+ private readonly List<TKey> _keys = new List<TKey>();
104
+ private readonly Dictionary<TKey, int> _lookup = new Dictionary<TKey, int>();
105
+
106
+ internal int[] FlatIndices { get; }
107
+
108
+ public IReadOnlyList<TKey> Keys => _keys;
109
+
110
+ public int NumDevices => _numTransducers.Length;
59
111
 
60
- public BesselOption() : this(intensity: null)
112
+ public TransducerGroups(Geometry geometry, Func<Device, int, TKey> key)
61
113
  {
114
+ _numTransducers = new int[geometry.NumDevices];
115
+ FlatIndices = new int[geometry.NumTransducers];
116
+ var k = 0;
117
+ var dev = 0;
118
+ foreach (var device in geometry)
119
+ {
120
+ var numTransducers = device.NumTransducers;
121
+ _numTransducers[dev++] = numTransducers;
122
+ for (var tr = 0; tr < numTransducers; tr++)
123
+ {
124
+ var value = key(device, tr);
125
+ if (!_lookup.TryGetValue(value, out var index))
126
+ {
127
+ index = _keys.Count;
128
+ _lookup.Add(value, index);
129
+ _keys.Add(value);
130
+ }
131
+ FlatIndices[k++] = index;
132
+ }
133
+ }
62
134
  }
63
135
 
64
- public BesselOption(Intensity? intensity = null, Phase? phaseOffset = null)
136
+ public TKey Key(int device, int transducer)
65
137
  {
66
- Intensity = intensity ?? Intensity.Max;
67
- PhaseOffset = phaseOffset ?? Phase.Zero;
138
+ if (device < 0 || device >= _numTransducers.Length)
139
+ {
140
+ throw new ArgumentOutOfRangeException(nameof(device));
141
+ }
142
+ if (transducer < 0 || transducer >= _numTransducers[device])
143
+ {
144
+ throw new ArgumentOutOfRangeException(nameof(transducer));
145
+ }
146
+ return _keys[FlatIndices[Offset(device) + transducer]];
68
147
  }
69
148
 
70
- internal PatternOptionNative ToNative() =>
71
- new PatternOptionNative { Intensity = Intensity.Value, PhaseOffset = PhaseOffset.Value };
72
- }
149
+ public int Index(int device, int transducer)
150
+ {
151
+ if (transducer < 0 || transducer >= NumTransducers(device))
152
+ {
153
+ throw new ArgumentOutOfRangeException(nameof(transducer));
154
+ }
155
+ return FlatIndices[Offset(device) + transducer];
156
+ }
73
157
 
74
- public readonly struct TwinTrapOption
75
- {
76
- public Intensity Intensity { get; }
77
- public Phase PhaseOffset { get; }
158
+ public IReadOnlyList<int> Indices(int device) =>
159
+ new ArraySegment<int>(FlatIndices, Offset(device), NumTransducers(device));
78
160
 
79
- public TwinTrapOption() : this(intensity: null)
161
+ public int NumTransducers(int device)
80
162
  {
163
+ if (device < 0 || device >= _numTransducers.Length)
164
+ {
165
+ throw new ArgumentOutOfRangeException(nameof(device));
166
+ }
167
+ return _numTransducers[device];
81
168
  }
82
169
 
83
- public TwinTrapOption(Intensity? intensity = null, Phase? phaseOffset = null)
170
+ public int NumTransducersIn(TKey key)
84
171
  {
85
- Intensity = intensity ?? Intensity.Max;
86
- PhaseOffset = phaseOffset ?? Phase.Zero;
172
+ if (!_lookup.TryGetValue(key, out var index))
173
+ {
174
+ return 0;
175
+ }
176
+ var count = 0;
177
+ foreach (var i in FlatIndices)
178
+ {
179
+ if (i == index)
180
+ {
181
+ count++;
182
+ }
183
+ }
184
+ return count;
87
185
  }
88
186
 
89
- internal PatternOptionNative ToNative() =>
90
- new PatternOptionNative { Intensity = Intensity.Value, PhaseOffset = PhaseOffset.Value };
91
- }
92
-
93
- public readonly struct VortexOption
94
- {
95
- public Intensity Intensity { get; }
96
- public Phase PhaseOffset { get; }
97
-
98
- public VortexOption() : this(intensity: null)
187
+ public IEnumerable<(TKey Key, TransducerMask Mask)> Masks()
99
188
  {
189
+ foreach (var key in _keys)
190
+ {
191
+ yield return (key, MaskOf(_lookup[key]));
192
+ }
100
193
  }
101
194
 
102
- public VortexOption(Intensity? intensity = null, Phase? phaseOffset = null)
195
+ private int Offset(int device)
103
196
  {
104
- Intensity = intensity ?? Intensity.Max;
105
- PhaseOffset = phaseOffset ?? Phase.Zero;
197
+ var offset = 0;
198
+ for (var dev = 0; dev < device; dev++)
199
+ {
200
+ offset += _numTransducers[dev];
201
+ }
202
+ return offset;
106
203
  }
107
204
 
108
- internal PatternOptionNative ToNative() =>
109
- new PatternOptionNative { Intensity = Intensity.Value, PhaseOffset = PhaseOffset.Value };
110
- }
205
+ public TransducerMask? Mask(TKey key) =>
206
+ _lookup.TryGetValue(key, out var index) ? MaskOf(index) : null;
111
207
 
112
- [StructLayout(LayoutKind.Sequential)]
113
- internal struct EmissionNative
114
- {
115
- public byte Phase;
116
- public byte Intensity;
208
+ internal TransducerMask MaskOf(int index)
209
+ {
210
+ var mask = new bool[_numTransducers.Length][];
211
+ var k = 0;
212
+ for (var dev = 0; dev < mask.Length; dev++)
213
+ {
214
+ mask[dev] = new bool[_numTransducers[dev]];
215
+ for (var tr = 0; tr < mask[dev].Length; tr++)
216
+ {
217
+ mask[dev][tr] = FlatIndices[k++] == index;
218
+ }
219
+ }
220
+ return TransducerMask.Masked(mask);
221
+ }
117
222
  }
118
223
 
119
224
  internal static class NativePattern
@@ -138,86 +243,140 @@ namespace AUTD3
138
243
  internal static extern float autd3_pattern_wavelength(float soundSpeedMmPerS);
139
244
 
140
245
  [DllImport(Lib, CallingConvention = CallingConvention.Cdecl)]
141
- internal static extern IntPtr autd3_core_geometry_pattern_buffer(GeometryHandle geometry);
246
+ internal static extern IntPtr autd3_core_geometry_phase_buffer(GeometryHandle geometry);
142
247
 
143
248
  [DllImport(Lib, CallingConvention = CallingConvention.Cdecl)]
144
- internal static extern IntPtr autd3_pattern_buffer_from_array(EmissionNative[] emissions, UIntPtr numDevices);
249
+ internal static extern IntPtr autd3_phase_buffer_from_array(byte[] src, UIntPtr numDevices);
145
250
 
146
251
  [DllImport(Lib, CallingConvention = CallingConvention.Cdecl)]
147
- internal static extern UIntPtr autd3_pattern_buffer_num_devices(PatternBufferHandle buffer);
252
+ internal static extern UIntPtr autd3_phase_buffer_num_devices(PhaseBufferHandle buffer);
148
253
 
149
254
  [DllImport(Lib, CallingConvention = CallingConvention.Cdecl)]
150
- internal static extern UIntPtr autd3_pattern_buffer_num_transducers(PatternBufferHandle buffer, UIntPtr dev);
255
+ internal static extern UIntPtr autd3_phase_buffer_num_transducers(PhaseBufferHandle buffer, UIntPtr dev);
151
256
 
152
257
  [DllImport(Lib, CallingConvention = CallingConvention.Cdecl)]
153
- internal static extern int autd3_pattern_buffer_get(PatternBufferHandle buffer, UIntPtr dev, UIntPtr tr, out EmissionNative @out);
258
+ internal static extern int autd3_phase_buffer_get(PhaseBufferHandle buffer, UIntPtr dev, UIntPtr tr, out byte @out);
154
259
 
155
260
  [DllImport(Lib, CallingConvention = CallingConvention.Cdecl)]
156
- internal static extern int autd3_pattern_buffer_set(PatternBufferHandle buffer, UIntPtr dev, UIntPtr tr, EmissionNative emission);
261
+ internal static extern int autd3_phase_buffer_set(PhaseBufferHandle buffer, UIntPtr dev, UIntPtr tr, byte value);
157
262
 
158
263
  [DllImport(Lib, CallingConvention = CallingConvention.Cdecl)]
159
- internal static extern void autd3_pattern_buffer_free(IntPtr buffer);
264
+ internal static extern int autd3_phase_buffer_copy_to(PhaseBufferHandle buffer, [Out] byte[] dst, UIntPtr len);
160
265
 
161
266
  [DllImport(Lib, CallingConvention = CallingConvention.Cdecl)]
162
- internal static extern int autd3_pattern_focus(GeometryHandle geometry, float[] target, float wavelengthMm, in PatternOptionNative option, PatternBufferHandle buffer);
267
+ internal static extern void autd3_phase_buffer_free(IntPtr buffer);
163
268
 
164
269
  [DllImport(Lib, CallingConvention = CallingConvention.Cdecl)]
165
- internal static extern int autd3_pattern_focus_device(GeometryHandle geometry, UIntPtr dev, float[] target, float wavelengthMm, in PatternOptionNative option, [Out] EmissionNative[] dst);
270
+ internal static extern IntPtr autd3_core_geometry_intensity_buffer(GeometryHandle geometry);
166
271
 
167
272
  [DllImport(Lib, CallingConvention = CallingConvention.Cdecl)]
168
- internal static extern int autd3_pattern_focus_transducer(float[] position, float[] target, float wavelengthMm, in PatternOptionNative option, out EmissionNative @out);
273
+ internal static extern IntPtr autd3_intensity_buffer_from_array(byte[] src, UIntPtr numDevices);
169
274
 
170
275
  [DllImport(Lib, CallingConvention = CallingConvention.Cdecl)]
171
- internal static extern int autd3_pattern_plane(GeometryHandle geometry, float[] dir, float wavelengthMm, in PatternOptionNative option, PatternBufferHandle buffer);
276
+ internal static extern UIntPtr autd3_intensity_buffer_num_devices(IntensityBufferHandle buffer);
172
277
 
173
278
  [DllImport(Lib, CallingConvention = CallingConvention.Cdecl)]
174
- internal static extern int autd3_pattern_plane_device(GeometryHandle geometry, UIntPtr dev, float[] dir, float wavelengthMm, in PatternOptionNative option, [Out] EmissionNative[] dst);
279
+ internal static extern UIntPtr autd3_intensity_buffer_num_transducers(IntensityBufferHandle buffer, UIntPtr dev);
175
280
 
176
281
  [DllImport(Lib, CallingConvention = CallingConvention.Cdecl)]
177
- internal static extern int autd3_pattern_plane_transducer(float[] position, float[] dir, float wavelengthMm, in PatternOptionNative option, out EmissionNative @out);
282
+ internal static extern int autd3_intensity_buffer_get(IntensityBufferHandle buffer, UIntPtr dev, UIntPtr tr, out byte @out);
178
283
 
179
284
  [DllImport(Lib, CallingConvention = CallingConvention.Cdecl)]
180
- internal static extern int autd3_pattern_bessel(GeometryHandle geometry, float[] apex, float[] dir, float thetaRad, float wavelengthMm, in PatternOptionNative option, PatternBufferHandle buffer);
285
+ internal static extern int autd3_intensity_buffer_set(IntensityBufferHandle buffer, UIntPtr dev, UIntPtr tr, byte value);
181
286
 
182
287
  [DllImport(Lib, CallingConvention = CallingConvention.Cdecl)]
183
- internal static extern int autd3_pattern_bessel_device(GeometryHandle geometry, UIntPtr dev, float[] apex, float[] dir, float thetaRad, float wavelengthMm, in PatternOptionNative option, [Out] EmissionNative[] dst);
288
+ internal static extern int autd3_intensity_buffer_copy_to(IntensityBufferHandle buffer, [Out] byte[] dst, UIntPtr len);
184
289
 
185
290
  [DllImport(Lib, CallingConvention = CallingConvention.Cdecl)]
186
- internal static extern int autd3_pattern_bessel_transducer(float[] position, float[] apex, float[] dir, float thetaRad, float wavelengthMm, in PatternOptionNative option, out EmissionNative @out);
291
+ internal static extern void autd3_intensity_buffer_free(IntPtr buffer);
187
292
 
188
293
  [DllImport(Lib, CallingConvention = CallingConvention.Cdecl)]
189
- internal static extern int autd3_pattern_twin_trap(GeometryHandle geometry, float[] target, float[] normal, float wavelengthMm, in PatternOptionNative option, PatternBufferHandle buffer);
294
+ internal static extern int autd3_pattern_focus(GeometryHandle geometry, float[] target, float wavelengthMm, PhaseBufferHandle buffer);
190
295
 
191
296
  [DllImport(Lib, CallingConvention = CallingConvention.Cdecl)]
192
- internal static extern int autd3_pattern_twin_trap_device(GeometryHandle geometry, UIntPtr dev, float[] target, float[] normal, float wavelengthMm, in PatternOptionNative option, [Out] EmissionNative[] dst);
297
+ internal static extern int autd3_pattern_focus_device(GeometryHandle geometry, UIntPtr dev, float[] target, float wavelengthMm, [In, Out] byte[] dst);
193
298
 
194
299
  [DllImport(Lib, CallingConvention = CallingConvention.Cdecl)]
195
- internal static extern int autd3_pattern_twin_trap_transducer(float[] position, float[] target, float[] normal, float wavelengthMm, in PatternOptionNative option, out EmissionNative @out);
300
+ internal static extern int autd3_pattern_focus_transducer(float[] position, float[] target, float wavelengthMm, out byte @out);
196
301
 
197
302
  [DllImport(Lib, CallingConvention = CallingConvention.Cdecl)]
198
- internal static extern int autd3_pattern_vortex(GeometryHandle geometry, float[] target, float[] axis, int order, float wavelengthMm, in PatternOptionNative option, PatternBufferHandle buffer);
303
+ internal static extern int autd3_pattern_plane(GeometryHandle geometry, float[] dir, float wavelengthMm, PhaseBufferHandle buffer);
199
304
 
200
305
  [DllImport(Lib, CallingConvention = CallingConvention.Cdecl)]
201
- internal static extern int autd3_pattern_vortex_device(GeometryHandle geometry, UIntPtr dev, float[] target, float[] axis, int order, float wavelengthMm, in PatternOptionNative option, [Out] EmissionNative[] dst);
306
+ internal static extern int autd3_pattern_plane_device(GeometryHandle geometry, UIntPtr dev, float[] dir, float wavelengthMm, [In, Out] byte[] dst);
202
307
 
203
308
  [DllImport(Lib, CallingConvention = CallingConvention.Cdecl)]
204
- internal static extern int autd3_pattern_vortex_transducer(float[] position, float[] target, float[] axis, int order, float wavelengthMm, in PatternOptionNative option, out EmissionNative @out);
309
+ internal static extern int autd3_pattern_plane_transducer(float[] position, float[] dir, float wavelengthMm, out byte @out);
205
310
 
206
311
  [DllImport(Lib, CallingConvention = CallingConvention.Cdecl)]
207
- internal static extern int autd3_pattern_uniform(byte phase, byte intensity, PatternBufferHandle buffer);
312
+ internal static extern int autd3_pattern_bessel(GeometryHandle geometry, float[] apex, float[] dir, float thetaRad, float wavelengthMm, PhaseBufferHandle buffer);
208
313
 
209
314
  [DllImport(Lib, CallingConvention = CallingConvention.Cdecl)]
210
- internal static extern void autd3_pattern_null(PatternBufferHandle buffer);
315
+ internal static extern int autd3_pattern_bessel_device(GeometryHandle geometry, UIntPtr dev, float[] apex, float[] dir, float thetaRad, float wavelengthMm, [In, Out] byte[] dst);
316
+
317
+ [DllImport(Lib, CallingConvention = CallingConvention.Cdecl)]
318
+ internal static extern int autd3_pattern_bessel_transducer(float[] position, float[] apex, float[] dir, float thetaRad, float wavelengthMm, out byte @out);
319
+
320
+ [DllImport(Lib, CallingConvention = CallingConvention.Cdecl)]
321
+ internal static extern int autd3_pattern_laguerre_gaussian_phase(GeometryHandle geometry, float[] target, float[] axis, uint p, int l, float waistMm, float wavelengthMm, PhaseBufferHandle buffer);
322
+
323
+ [DllImport(Lib, CallingConvention = CallingConvention.Cdecl)]
324
+ internal static extern int autd3_pattern_laguerre_gaussian_phase_device(GeometryHandle geometry, UIntPtr dev, float[] target, float[] axis, uint p, int l, float waistMm, float wavelengthMm, [In, Out] byte[] dst);
325
+
326
+ [DllImport(Lib, CallingConvention = CallingConvention.Cdecl)]
327
+ internal static extern int autd3_pattern_laguerre_gaussian_phase_transducer(float[] position, float[] target, float[] axis, uint p, int l, float waistMm, float wavelengthMm, out byte @out);
328
+
329
+ [DllImport(Lib, CallingConvention = CallingConvention.Cdecl)]
330
+ internal static extern int autd3_pattern_laguerre_gaussian_intensity(GeometryHandle geometry, float[] target, float[] axis, uint p, int l, float waistMm, float wavelengthMm, IntensityBufferHandle buffer);
331
+
332
+ [DllImport(Lib, CallingConvention = CallingConvention.Cdecl)]
333
+ internal static extern int autd3_pattern_laguerre_gaussian_intensity_device(GeometryHandle geometry, UIntPtr dev, float[] target, float[] axis, uint p, int l, float waistMm, float wavelengthMm, [In, Out] byte[] dst);
334
+
335
+ [DllImport(Lib, CallingConvention = CallingConvention.Cdecl)]
336
+ internal static extern int autd3_pattern_hermite_gaussian_phase(GeometryHandle geometry, float[] target, float[] axis, float[] xDir, uint m, uint n, float waistMm, float wavelengthMm, PhaseBufferHandle buffer);
337
+
338
+ [DllImport(Lib, CallingConvention = CallingConvention.Cdecl)]
339
+ internal static extern int autd3_pattern_hermite_gaussian_phase_device(GeometryHandle geometry, UIntPtr dev, float[] target, float[] axis, float[] xDir, uint m, uint n, float waistMm, float wavelengthMm, [In, Out] byte[] dst);
340
+
341
+ [DllImport(Lib, CallingConvention = CallingConvention.Cdecl)]
342
+ internal static extern int autd3_pattern_hermite_gaussian_phase_transducer(float[] position, float[] target, float[] axis, float[] xDir, uint m, uint n, float waistMm, float wavelengthMm, out byte @out);
343
+
344
+ [DllImport(Lib, CallingConvention = CallingConvention.Cdecl)]
345
+ internal static extern int autd3_pattern_hermite_gaussian_intensity(GeometryHandle geometry, float[] target, float[] axis, float[] xDir, uint m, uint n, float waistMm, float wavelengthMm, IntensityBufferHandle buffer);
346
+
347
+ [DllImport(Lib, CallingConvention = CallingConvention.Cdecl)]
348
+ internal static extern int autd3_pattern_hermite_gaussian_intensity_device(GeometryHandle geometry, UIntPtr dev, float[] target, float[] axis, float[] xDir, uint m, uint n, float waistMm, float wavelengthMm, [In, Out] byte[] dst);
349
+
350
+ [DllImport(Lib, CallingConvention = CallingConvention.Cdecl)]
351
+ internal static extern int autd3_pattern_set_intensity(byte intensity, IntensityBufferHandle buffer);
352
+
353
+ [DllImport(Lib, CallingConvention = CallingConvention.Cdecl)]
354
+ internal static extern int autd3_pattern_set_phase(byte phase, PhaseBufferHandle buffer);
355
+
356
+ [DllImport(Lib, CallingConvention = CallingConvention.Cdecl)]
357
+ internal static extern int autd3_pattern_add_phase(byte phase, PhaseBufferHandle buffer);
358
+
359
+ [DllImport(Lib, CallingConvention = CallingConvention.Cdecl)]
360
+ internal static extern int autd3_pattern_group_phase(GeometryHandle geometry, int[] keys, IntPtr[] sources, UIntPtr numSources, PhaseBufferHandle buffer);
361
+
362
+ [DllImport(Lib, CallingConvention = CallingConvention.Cdecl)]
363
+ internal static extern int autd3_pattern_group_copy_phase(GeometryHandle geometry, int[] indices, int index, PhaseBufferHandle source, PhaseBufferHandle buffer);
364
+
365
+ [DllImport(Lib, CallingConvention = CallingConvention.Cdecl)]
366
+ internal static extern int autd3_pattern_group_intensity(GeometryHandle geometry, int[] keys, IntPtr[] sources, UIntPtr numSources, IntensityBufferHandle buffer);
367
+
368
+ [DllImport(Lib, CallingConvention = CallingConvention.Cdecl)]
369
+ internal static extern int autd3_pattern_group_copy_intensity(GeometryHandle geometry, int[] indices, int index, IntensityBufferHandle source, IntensityBufferHandle buffer);
211
370
 
212
371
 
213
372
  [DllImport(ClientLib, CallingConvention = CallingConvention.Cdecl)]
214
- internal static extern IntPtr autd3_op_pattern(byte bank, PatternBufferHandle patternBuffer, byte transitionMode, ulong transitionValue, uint transitionMarginNs);
373
+ internal static extern IntPtr autd3_op_pattern(byte bank, PhaseBufferHandle phases, IntPtr intensities, byte uniformIntensity, byte transitionMode, ulong transitionValue);
215
374
 
216
375
  [DllImport(ClientLib, CallingConvention = CallingConvention.Cdecl)]
217
- internal static extern IntPtr autd3_op_write_pattern_buffer(byte bank, ushort index, PatternBufferHandle patternBuffer);
376
+ internal static extern IntPtr autd3_op_write_pattern_buffer(byte bank, ushort index, PhaseBufferHandle phases, IntPtr intensities, byte uniformIntensity);
218
377
 
219
378
  [DllImport(ClientLib, CallingConvention = CallingConvention.Cdecl)]
220
- internal static extern IntPtr autd3_op_write_pattern_compressed(byte bank, uint index, byte format, IntPtr[] patterns, UIntPtr numPatterns);
379
+ internal static extern IntPtr autd3_op_write_pattern_phase(byte bank, ushort index, byte depth, byte intensity, IntPtr[] patterns, UIntPtr numPatterns);
221
380
 
222
381
  [DllImport(ClientLib, CallingConvention = CallingConvention.Cdecl)]
223
382
  internal static extern IntPtr autd3_op_config_pattern(byte bank, IntPtr samplingConfig, uint size, ushort rep);
@@ -226,55 +385,67 @@ namespace AUTD3
226
385
  internal static extern IntPtr autd3_op_config_foci_stm(byte bank, IntPtr samplingConfig, uint size, byte numFoci, float soundSpeedMPerS, ushort rep);
227
386
 
228
387
  [DllImport(ClientLib, CallingConvention = CallingConvention.Cdecl)]
229
- internal static extern IntPtr autd3_op_change_pattern_bank(byte bank, byte transitionMode, ulong transitionValue, uint transitionMarginNs);
388
+ internal static extern IntPtr autd3_op_activate_pattern_bank(byte bank, byte transitionMode, ulong transitionValue);
230
389
 
231
390
  [DllImport(ClientLib, CallingConvention = CallingConvention.Cdecl)]
232
- internal static extern int autd3_pattern_compression_per_frame(byte format, out UIntPtr @out);
391
+ internal static extern int autd3_phase_depth_max_count(byte depth, out UIntPtr @out);
233
392
  }
234
393
 
235
- internal sealed class PatternBufferHandle : Autd3SafeHandle
394
+ internal sealed class PhaseBufferHandle : Autd3SafeHandle
236
395
  {
237
- internal PatternBufferHandle(IntPtr handle) : base(handle)
396
+ internal PhaseBufferHandle(IntPtr handle) : base(handle)
238
397
  {
239
398
  }
240
399
 
241
400
  protected override bool ReleaseHandle()
242
401
  {
243
- NativePattern.autd3_pattern_buffer_free(handle);
402
+ NativePattern.autd3_phase_buffer_free(handle);
244
403
  return true;
245
404
  }
246
405
  }
247
406
 
248
- public readonly struct DevicePattern : IEnumerable<Emission>
407
+ internal sealed class IntensityBufferHandle : Autd3SafeHandle
249
408
  {
250
- private readonly PatternBufferHandle _buffer;
409
+ internal IntensityBufferHandle(IntPtr handle) : base(handle)
410
+ {
411
+ }
412
+
413
+ protected override bool ReleaseHandle()
414
+ {
415
+ NativePattern.autd3_intensity_buffer_free(handle);
416
+ return true;
417
+ }
418
+ }
419
+
420
+ public readonly struct DevicePhases : IEnumerable<Phase>
421
+ {
422
+ private readonly PhaseBufferHandle _buffer;
251
423
  private readonly UIntPtr _dev;
252
424
 
253
- internal DevicePattern(PatternBufferHandle buffer, UIntPtr dev)
425
+ internal DevicePhases(PhaseBufferHandle buffer, UIntPtr dev)
254
426
  {
255
427
  _buffer = buffer;
256
428
  _dev = dev;
257
429
  }
258
430
 
259
- public int NumTransducers => (int)NativePattern.autd3_pattern_buffer_num_transducers(_buffer, _dev);
431
+ public int NumTransducers => (int)NativePattern.autd3_phase_buffer_num_transducers(_buffer, _dev);
260
432
 
261
- public Emission this[int tr]
433
+ public Phase this[int tr]
262
434
  {
263
435
  get
264
436
  {
265
- if (NativePattern.autd3_pattern_buffer_get(_buffer, _dev, (UIntPtr)tr, out var e) != 0)
437
+ if (NativePattern.autd3_phase_buffer_get(_buffer, _dev, (UIntPtr)tr, out var v) != 0)
266
438
  throw new ArgumentOutOfRangeException(nameof(tr));
267
- return new Emission(new Phase(e.Phase), new Intensity(e.Intensity));
439
+ return new Phase(v);
268
440
  }
269
441
  set
270
442
  {
271
- var native = new EmissionNative { Phase = value.Phase.Value, Intensity = value.Intensity.Value };
272
- if (NativePattern.autd3_pattern_buffer_set(_buffer, _dev, (UIntPtr)tr, native) != 0)
443
+ if (NativePattern.autd3_phase_buffer_set(_buffer, _dev, (UIntPtr)tr, value.Value) != 0)
273
444
  throw new ArgumentOutOfRangeException(nameof(tr));
274
445
  }
275
446
  }
276
447
 
277
- public IEnumerator<Emission> GetEnumerator()
448
+ public IEnumerator<Phase> GetEnumerator()
278
449
  {
279
450
  var count = NumTransducers;
280
451
  for (var i = 0; i < count; i++)
@@ -286,58 +457,177 @@ namespace AUTD3
286
457
  IEnumerator IEnumerable.GetEnumerator() => GetEnumerator();
287
458
  }
288
459
 
289
- public sealed class PatternBuffer : IDisposable, IEnumerable<DevicePattern>
460
+ public readonly struct DeviceIntensities : IEnumerable<Intensity>
290
461
  {
291
- internal const int NumTransducers = 249;
462
+ private readonly IntensityBufferHandle _buffer;
463
+ private readonly UIntPtr _dev;
464
+
465
+ internal DeviceIntensities(IntensityBufferHandle buffer, UIntPtr dev)
466
+ {
467
+ _buffer = buffer;
468
+ _dev = dev;
469
+ }
470
+
471
+ public int NumTransducers => (int)NativePattern.autd3_intensity_buffer_num_transducers(_buffer, _dev);
472
+
473
+ public Intensity this[int tr]
474
+ {
475
+ get
476
+ {
477
+ if (NativePattern.autd3_intensity_buffer_get(_buffer, _dev, (UIntPtr)tr, out var v) != 0)
478
+ throw new ArgumentOutOfRangeException(nameof(tr));
479
+ return new Intensity(v);
480
+ }
481
+ set
482
+ {
483
+ if (NativePattern.autd3_intensity_buffer_set(_buffer, _dev, (UIntPtr)tr, value.Value) != 0)
484
+ throw new ArgumentOutOfRangeException(nameof(tr));
485
+ }
486
+ }
487
+
488
+ public IEnumerator<Intensity> GetEnumerator()
489
+ {
490
+ var count = NumTransducers;
491
+ for (var i = 0; i < count; i++)
492
+ {
493
+ yield return this[i];
494
+ }
495
+ }
292
496
 
293
- private readonly PatternBufferHandle _handle;
497
+ IEnumerator IEnumerable.GetEnumerator() => GetEnumerator();
498
+ }
499
+
500
+ public sealed class PhaseBuffer : IDisposable, IEnumerable<DevicePhases>
501
+ {
502
+ private readonly PhaseBufferHandle _handle;
294
503
 
295
- internal PatternBufferHandle Handle => _handle;
504
+ internal PhaseBufferHandle Handle => _handle;
296
505
 
297
- internal PatternBuffer(IntPtr handle)
506
+ internal PhaseBuffer(IntPtr handle)
298
507
  {
299
508
  if (handle == IntPtr.Zero)
300
509
  {
301
- throw new Autd3Exception("failed to create pattern buffer");
510
+ throw new Autd3Exception("failed to create phase buffer");
302
511
  }
303
- _handle = new PatternBufferHandle(handle);
512
+ _handle = new PhaseBufferHandle(handle);
304
513
  }
305
514
 
306
- public static PatternBuffer FromArray(Emission[][] emissions)
515
+ public static PhaseBuffer FromArray(Phase[][] phases)
516
+ {
517
+ var flat = BufferArray.Flatten(phases, p => p.Value, "phases");
518
+ return new PhaseBuffer(NativePattern.autd3_phase_buffer_from_array(flat, (UIntPtr)phases.Length));
519
+ }
520
+
521
+ public int NumDevices => (int)NativePattern.autd3_phase_buffer_num_devices(Handle);
522
+
523
+ public Phase[][] ToArray()
307
524
  {
308
- var numDevices = emissions.Length;
309
- var flat = new EmissionNative[numDevices * NumTransducers];
310
- for (var d = 0; d < numDevices; d++)
525
+ var result = new Phase[NumDevices][];
526
+ var total = 0;
527
+ for (var dev = 0; dev < result.Length; dev++)
311
528
  {
312
- if (emissions[d].Length != NumTransducers)
529
+ result[dev] = new Phase[(int)NativePattern.autd3_phase_buffer_num_transducers(Handle, (UIntPtr)dev)];
530
+ total += result[dev].Length;
531
+ }
532
+ var flat = new byte[total];
533
+ if (NativePattern.autd3_phase_buffer_copy_to(Handle, flat, (UIntPtr)flat.Length) != 0)
534
+ {
535
+ throw new Autd3Exception("failed to copy the buffer");
536
+ }
537
+ var k = 0;
538
+ foreach (var device in result)
539
+ {
540
+ for (var tr = 0; tr < device.Length; tr++)
313
541
  {
314
- throw new Autd3Exception($"each device requires {NumTransducers} emissions");
542
+ device[tr] = new Phase(flat[k++]);
315
543
  }
316
- for (var t = 0; t < NumTransducers; t++)
544
+ }
545
+ return result;
546
+ }
547
+
548
+ public DevicePhases this[int dev]
549
+ {
550
+ get
551
+ {
552
+ if (dev < 0 || dev >= NumDevices)
553
+ throw new ArgumentOutOfRangeException(nameof(dev));
554
+ return new DevicePhases(Handle, (UIntPtr)dev);
555
+ }
556
+ }
557
+
558
+ public IEnumerator<DevicePhases> GetEnumerator()
559
+ {
560
+ var count = NumDevices;
561
+ for (var i = 0; i < count; i++)
562
+ {
563
+ yield return this[i];
564
+ }
565
+ }
566
+
567
+ IEnumerator IEnumerable.GetEnumerator() => GetEnumerator();
568
+
569
+ public void Dispose() => _handle.Dispose();
570
+ }
571
+
572
+ public sealed class IntensityBuffer : IDisposable, IEnumerable<DeviceIntensities>
573
+ {
574
+ private readonly IntensityBufferHandle _handle;
575
+
576
+ internal IntensityBufferHandle Handle => _handle;
577
+
578
+ internal IntensityBuffer(IntPtr handle)
579
+ {
580
+ if (handle == IntPtr.Zero)
581
+ {
582
+ throw new Autd3Exception("failed to create intensity buffer");
583
+ }
584
+ _handle = new IntensityBufferHandle(handle);
585
+ }
586
+
587
+ public static IntensityBuffer FromArray(Intensity[][] intensities)
588
+ {
589
+ var flat = BufferArray.Flatten(intensities, i => i.Value, "intensities");
590
+ return new IntensityBuffer(NativePattern.autd3_intensity_buffer_from_array(flat, (UIntPtr)intensities.Length));
591
+ }
592
+
593
+ public int NumDevices => (int)NativePattern.autd3_intensity_buffer_num_devices(Handle);
594
+
595
+ public Intensity[][] ToArray()
596
+ {
597
+ var result = new Intensity[NumDevices][];
598
+ var total = 0;
599
+ for (var dev = 0; dev < result.Length; dev++)
600
+ {
601
+ result[dev] = new Intensity[(int)NativePattern.autd3_intensity_buffer_num_transducers(Handle, (UIntPtr)dev)];
602
+ total += result[dev].Length;
603
+ }
604
+ var flat = new byte[total];
605
+ if (NativePattern.autd3_intensity_buffer_copy_to(Handle, flat, (UIntPtr)flat.Length) != 0)
606
+ {
607
+ throw new Autd3Exception("failed to copy the buffer");
608
+ }
609
+ var k = 0;
610
+ foreach (var device in result)
611
+ {
612
+ for (var tr = 0; tr < device.Length; tr++)
317
613
  {
318
- flat[d * NumTransducers + t] = new EmissionNative
319
- {
320
- Phase = emissions[d][t].Phase.Value,
321
- Intensity = emissions[d][t].Intensity.Value,
322
- };
614
+ device[tr] = new Intensity(flat[k++]);
323
615
  }
324
616
  }
325
- return new PatternBuffer(NativePattern.autd3_pattern_buffer_from_array(flat, (UIntPtr)numDevices));
617
+ return result;
326
618
  }
327
619
 
328
- public int NumDevices => (int)NativePattern.autd3_pattern_buffer_num_devices(Handle);
329
-
330
- public DevicePattern this[int dev]
620
+ public DeviceIntensities this[int dev]
331
621
  {
332
622
  get
333
623
  {
334
624
  if (dev < 0 || dev >= NumDevices)
335
625
  throw new ArgumentOutOfRangeException(nameof(dev));
336
- return new DevicePattern(Handle, (UIntPtr)dev);
626
+ return new DeviceIntensities(Handle, (UIntPtr)dev);
337
627
  }
338
628
  }
339
629
 
340
- public IEnumerator<DevicePattern> GetEnumerator()
630
+ public IEnumerator<DeviceIntensities> GetEnumerator()
341
631
  {
342
632
  var count = NumDevices;
343
633
  for (var i = 0; i < count; i++)
@@ -351,260 +641,424 @@ namespace AUTD3
351
641
  public void Dispose() => _handle.Dispose();
352
642
  }
353
643
 
644
+ internal static class BufferArray
645
+ {
646
+ internal static byte[] Flatten<T>(T[][] src, Func<T, byte> value, string name)
647
+ {
648
+ if (src == null)
649
+ {
650
+ throw new ArgumentNullException(name);
651
+ }
652
+ var flat = new byte[src.Length * Autd3.NumTransducers];
653
+ for (var d = 0; d < src.Length; d++)
654
+ {
655
+ if (src[d] == null || src[d].Length != Autd3.NumTransducers)
656
+ {
657
+ throw new Autd3Exception($"each device requires {Autd3.NumTransducers} {name}");
658
+ }
659
+ for (var t = 0; t < Autd3.NumTransducers; t++)
660
+ {
661
+ flat[d * Autd3.NumTransducers + t] = value(src[d][t]);
662
+ }
663
+ }
664
+ return flat;
665
+ }
666
+ }
667
+
354
668
  public static class GeometryPatternBufferExtensions
355
669
  {
356
- public static PatternBuffer PatternBuffer(this Geometry geometry) =>
357
- new PatternBuffer(NativePattern.autd3_core_geometry_pattern_buffer(geometry.Handle));
670
+ public static PhaseBuffer PhaseBuffer(this Geometry geometry) =>
671
+ new PhaseBuffer(NativePattern.autd3_core_geometry_phase_buffer(geometry.Handle));
672
+
673
+ public static IntensityBuffer IntensityBuffer(this Geometry geometry) =>
674
+ new IntensityBuffer(NativePattern.autd3_core_geometry_intensity_buffer(geometry.Handle));
675
+ }
676
+
677
+ public readonly struct PatternIntensity
678
+ {
679
+ private readonly IntensityBuffer? _buffer;
680
+ private readonly Intensity? _uniform;
681
+
682
+ public PatternIntensity(Intensity uniform)
683
+ {
684
+ _buffer = null;
685
+ _uniform = uniform;
686
+ }
687
+
688
+ public PatternIntensity(IntensityBuffer buffer)
689
+ {
690
+ _buffer = buffer ?? throw new ArgumentNullException(nameof(buffer));
691
+ _uniform = null;
692
+ }
693
+
694
+ internal IntensityBuffer? Buffer => _buffer;
695
+
696
+ internal byte Uniform => (_uniform ?? Intensity.Max).Value;
697
+
698
+ public static implicit operator PatternIntensity(Intensity uniform) => new PatternIntensity(uniform);
699
+
700
+ public static implicit operator PatternIntensity(IntensityBuffer buffer) => new PatternIntensity(buffer);
358
701
  }
359
702
 
360
703
  public sealed class Pattern : ICommand
361
704
  {
362
705
  private readonly PatternBank _bank;
363
- private readonly PatternBuffer _buffer;
706
+ private readonly PhaseBuffer _phases;
707
+ private readonly PatternIntensity _intensities;
364
708
  private readonly TransitionMode _transitionMode;
365
709
 
366
- public Pattern(PatternBuffer emissions, TransitionMode? transitionMode = null)
367
- : this(PatternBank.B0, emissions, transitionMode)
710
+ public Pattern(PhaseBuffer phases, PatternIntensity intensities, TransitionMode? transitionMode = null)
711
+ : this(PatternBank.B0, phases, intensities, transitionMode)
368
712
  {
369
713
  }
370
714
 
371
- public Pattern(PatternBank bank, PatternBuffer emissions, TransitionMode? transitionMode = null)
715
+ public Pattern(PatternBank bank, PhaseBuffer phases, PatternIntensity intensities, TransitionMode? transitionMode = null)
372
716
  {
373
717
  _bank = bank;
374
- _buffer = emissions;
718
+ _phases = phases;
719
+ _intensities = intensities;
375
720
  _transitionMode = transitionMode ?? TransitionMode.Immediate;
376
721
  }
377
722
 
378
- IntPtr ICommand.CreateOp() =>
379
- NativePattern.autd3_op_pattern((byte)_bank, _buffer.Handle, _transitionMode.Mode, _transitionMode.Value, _transitionMode.MarginNs);
723
+ IntPtr ICommand.CreateOp(Geometry geometry)
724
+ {
725
+ using var intensityLease = new HandleLease(_intensities.Buffer?.Handle);
726
+ return NativePattern.autd3_op_pattern((byte)_bank, _phases.Handle, intensityLease.Pointer, _intensities.Uniform, _transitionMode.Mode, _transitionMode.Value);
727
+ }
380
728
 
381
729
 
382
730
  public static Length Wavelength(Velocity soundSpeed) =>
383
731
  new Length(NativePattern.autd3_pattern_wavelength(soundSpeed.MmS));
384
732
 
385
733
 
386
- public static void Focus(Geometry geometry, Vector3 target, Length wavelength, FocusOption option, PatternBuffer dst)
734
+ public static void Focus(Geometry geometry, Vector3 target, Length wavelength, PhaseBuffer dst)
387
735
  {
388
- var t = Coords.PointArray(target);
389
- var o = option.ToNative();
390
- if (NativePattern.autd3_pattern_focus(geometry.Handle, t, wavelength.Mm, in o, dst.Handle) != 0)
736
+ if (NativePattern.autd3_pattern_focus(geometry.Handle, Coords.PointArray(target), wavelength.Mm, dst.Handle) != 0)
391
737
  {
392
738
  throw new Autd3Exception("focus failed (buffer device count must match geometry)");
393
739
  }
394
740
  }
395
741
 
396
- public static void Focus(Geometry geometry, Vector3 target, Length wavelength, Intensity intensity, PatternBuffer dst) =>
397
- Focus(geometry, target, wavelength, new FocusOption(intensity), dst);
398
-
399
- public static void FocusDevice(Device device, Vector3 target, Length wavelength, FocusOption option, Emission[] dst)
742
+ public static void FocusDevice(Device device, Vector3 target, Length wavelength, Phase[] dst)
400
743
  {
401
- var native = ToNativeDst(dst);
402
- var o = option.ToNative();
744
+ var native = ToNative(dst);
403
745
  if (NativePattern.autd3_pattern_focus_device(device.GeometryHandle, device.DeviceIndex,
404
- Coords.PointArray(target), wavelength.Mm, in o, native) != 0)
746
+ Coords.PointArray(target), wavelength.Mm, native) != 0)
405
747
  {
406
748
  throw new Autd3Exception("focus_device failed");
407
749
  }
408
- FromNativeDst(native, dst);
750
+ FromNative(native, dst);
409
751
  }
410
752
 
411
- public static Emission FocusTransducer(Vector3 position, Vector3 target, Length wavelength, FocusOption option)
753
+ public static Phase FocusTransducer(Vector3 position, Vector3 target, Length wavelength)
412
754
  {
413
- var o = option.ToNative();
414
755
  if (NativePattern.autd3_pattern_focus_transducer(
415
756
  Coords.PointArray(position),
416
- Coords.PointArray(target), wavelength.Mm, in o, out var e) != 0)
757
+ Coords.PointArray(target), wavelength.Mm, out var p) != 0)
417
758
  {
418
759
  throw new Autd3Exception("focus_transducer failed");
419
760
  }
420
- return new Emission(new Phase(e.Phase), new Intensity(e.Intensity));
761
+ return new Phase(p);
421
762
  }
422
763
 
423
- public static void Plane(Geometry geometry, Vector3 dir, Length wavelength, PlaneOption option, PatternBuffer dst)
764
+ public static void Plane(Geometry geometry, Vector3 dir, Length wavelength, PhaseBuffer dst)
424
765
  {
425
- var d = Coords.DirArray(dir);
426
- var o = option.ToNative();
427
- if (NativePattern.autd3_pattern_plane(geometry.Handle, d, wavelength.Mm, in o, dst.Handle) != 0)
766
+ if (NativePattern.autd3_pattern_plane(geometry.Handle, Coords.DirArray(dir), wavelength.Mm, dst.Handle) != 0)
428
767
  {
429
768
  throw new Autd3Exception("plane failed (buffer device count must match geometry)");
430
769
  }
431
770
  }
432
771
 
433
- public static void PlaneDevice(Device device, Vector3 dir, Length wavelength, PlaneOption option, Emission[] dst)
772
+ public static void PlaneDevice(Device device, Vector3 dir, Length wavelength, Phase[] dst)
434
773
  {
435
- var native = ToNativeDst(dst);
436
- var o = option.ToNative();
774
+ var native = ToNative(dst);
437
775
  if (NativePattern.autd3_pattern_plane_device(device.GeometryHandle, device.DeviceIndex,
438
- Coords.DirArray(dir), wavelength.Mm, in o, native) != 0)
776
+ Coords.DirArray(dir), wavelength.Mm, native) != 0)
439
777
  {
440
778
  throw new Autd3Exception("plane_device failed");
441
779
  }
442
- FromNativeDst(native, dst);
780
+ FromNative(native, dst);
443
781
  }
444
782
 
445
- public static Emission PlaneTransducer(Vector3 position, Vector3 dir, Length wavelength, PlaneOption option)
783
+ public static Phase PlaneTransducer(Vector3 position, Vector3 dir, Length wavelength)
446
784
  {
447
- var o = option.ToNative();
448
785
  if (NativePattern.autd3_pattern_plane_transducer(
449
786
  Coords.PointArray(position),
450
- Coords.DirArray(dir), wavelength.Mm, in o, out var e) != 0)
787
+ Coords.DirArray(dir), wavelength.Mm, out var p) != 0)
451
788
  {
452
789
  throw new Autd3Exception("plane_transducer failed");
453
790
  }
454
- return new Emission(new Phase(e.Phase), new Intensity(e.Intensity));
791
+ return new Phase(p);
455
792
  }
456
793
 
457
- public static void Bessel(Geometry geometry, Vector3 apex, Vector3 dir, Angle theta, Length wavelength, BesselOption option, PatternBuffer dst)
794
+ public static void Bessel(Geometry geometry, Vector3 apex, Vector3 dir, Angle theta, Length wavelength, PhaseBuffer dst)
458
795
  {
459
- var a = Coords.PointArray(apex);
460
- var d = Coords.DirArray(dir);
461
- var o = option.ToNative();
462
- if (NativePattern.autd3_pattern_bessel(geometry.Handle, a, d, theta.Rad, wavelength.Mm, in o, dst.Handle) != 0)
796
+ if (NativePattern.autd3_pattern_bessel(geometry.Handle, Coords.PointArray(apex), Coords.DirArray(dir), theta.Rad, wavelength.Mm, dst.Handle) != 0)
463
797
  {
464
798
  throw new Autd3Exception("bessel failed (buffer device count must match geometry)");
465
799
  }
466
800
  }
467
801
 
468
- public static void BesselDevice(Device device, Vector3 apex, Vector3 dir, Angle theta, Length wavelength, BesselOption option, Emission[] dst)
802
+ public static void BesselDevice(Device device, Vector3 apex, Vector3 dir, Angle theta, Length wavelength, Phase[] dst)
469
803
  {
470
- var native = ToNativeDst(dst);
471
- var o = option.ToNative();
804
+ var native = ToNative(dst);
472
805
  if (NativePattern.autd3_pattern_bessel_device(device.GeometryHandle, device.DeviceIndex,
473
806
  Coords.PointArray(apex),
474
- Coords.DirArray(dir), theta.Rad, wavelength.Mm, in o, native) != 0)
807
+ Coords.DirArray(dir), theta.Rad, wavelength.Mm, native) != 0)
475
808
  {
476
809
  throw new Autd3Exception("bessel_device failed");
477
810
  }
478
- FromNativeDst(native, dst);
811
+ FromNative(native, dst);
479
812
  }
480
813
 
481
- public static Emission BesselTransducer(Vector3 position, Vector3 apex, Vector3 dir, Angle theta, Length wavelength, BesselOption option)
814
+ public static Phase BesselTransducer(Vector3 position, Vector3 apex, Vector3 dir, Angle theta, Length wavelength)
482
815
  {
483
- var o = option.ToNative();
484
816
  if (NativePattern.autd3_pattern_bessel_transducer(
485
817
  Coords.PointArray(position),
486
818
  Coords.PointArray(apex),
487
- Coords.DirArray(dir), theta.Rad, wavelength.Mm, in o, out var e) != 0)
819
+ Coords.DirArray(dir), theta.Rad, wavelength.Mm, out var p) != 0)
488
820
  {
489
821
  throw new Autd3Exception("bessel_transducer failed");
490
822
  }
491
- return new Emission(new Phase(e.Phase), new Intensity(e.Intensity));
823
+ return new Phase(p);
492
824
  }
493
825
 
494
- public static void TwinTrap(Geometry geometry, Vector3 target, Vector3 normal, Length wavelength, TwinTrapOption option, PatternBuffer dst)
826
+ public static void LaguerreGaussianPhase(Geometry geometry, Vector3 target, Vector3 axis, LaguerreGaussianOption option, Length wavelength, PhaseBuffer dst)
495
827
  {
496
- var t = Coords.PointArray(target);
497
- var n = Coords.DirArray(normal);
498
- var o = option.ToNative();
499
- if (NativePattern.autd3_pattern_twin_trap(geometry.Handle, t, n, wavelength.Mm, in o, dst.Handle) != 0)
828
+ if (NativePattern.autd3_pattern_laguerre_gaussian_phase(geometry.Handle, Coords.PointArray(target), Coords.DirArray(axis),
829
+ option.P, option.L, option.Waist.Mm, wavelength.Mm, dst.Handle) != 0)
500
830
  {
501
- throw new Autd3Exception("twin_trap failed (buffer device count must match geometry)");
831
+ throw new Autd3Exception("laguerre_gaussian_phase failed (waist must be positive and buffer device count must match geometry)");
502
832
  }
503
833
  }
504
834
 
505
- public static void TwinTrapDevice(Device device, Vector3 target, Vector3 normal, Length wavelength, TwinTrapOption option, Emission[] dst)
835
+ public static void LaguerreGaussianPhaseDevice(Device device, Vector3 target, Vector3 axis, LaguerreGaussianOption option, Length wavelength, Phase[] dst)
506
836
  {
507
- var native = ToNativeDst(dst);
508
- var o = option.ToNative();
509
- if (NativePattern.autd3_pattern_twin_trap_device(device.GeometryHandle, device.DeviceIndex,
510
- Coords.PointArray(target), Coords.DirArray(normal), wavelength.Mm, in o, native) != 0)
837
+ var native = ToNative(dst);
838
+ if (NativePattern.autd3_pattern_laguerre_gaussian_phase_device(device.GeometryHandle, device.DeviceIndex,
839
+ Coords.PointArray(target), Coords.DirArray(axis), option.P, option.L, option.Waist.Mm, wavelength.Mm, native) != 0)
511
840
  {
512
- throw new Autd3Exception("twin_trap_device failed");
841
+ throw new Autd3Exception("laguerre_gaussian_phase_device failed (waist must be positive)");
513
842
  }
514
- FromNativeDst(native, dst);
843
+ FromNative(native, dst);
515
844
  }
516
845
 
517
- public static Emission TwinTrapTransducer(Vector3 position, Vector3 target, Vector3 normal, Length wavelength, TwinTrapOption option)
846
+ public static Phase LaguerreGaussianPhaseTransducer(Vector3 position, Vector3 target, Vector3 axis, LaguerreGaussianOption option, Length wavelength)
518
847
  {
519
- var o = option.ToNative();
520
- if (NativePattern.autd3_pattern_twin_trap_transducer(
848
+ if (NativePattern.autd3_pattern_laguerre_gaussian_phase_transducer(
521
849
  Coords.PointArray(position),
522
850
  Coords.PointArray(target),
523
- Coords.DirArray(normal), wavelength.Mm, in o, out var e) != 0)
851
+ Coords.DirArray(axis), option.P, option.L, option.Waist.Mm, wavelength.Mm, out var p) != 0)
524
852
  {
525
- throw new Autd3Exception("twin_trap_transducer failed");
853
+ throw new Autd3Exception("laguerre_gaussian_phase_transducer failed (waist must be positive)");
526
854
  }
527
- return new Emission(new Phase(e.Phase), new Intensity(e.Intensity));
855
+ return new Phase(p);
528
856
  }
529
857
 
530
- public static void Vortex(Geometry geometry, Vector3 target, Vector3 axis, int order, Length wavelength, VortexOption option, PatternBuffer dst)
858
+ public static void LaguerreGaussianIntensity(Geometry geometry, Vector3 target, Vector3 axis, LaguerreGaussianOption option, Length wavelength, IntensityBuffer dst)
531
859
  {
532
- var t = Coords.PointArray(target);
533
- var a = Coords.DirArray(axis);
534
- var o = option.ToNative();
535
- if (NativePattern.autd3_pattern_vortex(geometry.Handle, t, a, order, wavelength.Mm, in o, dst.Handle) != 0)
860
+ if (NativePattern.autd3_pattern_laguerre_gaussian_intensity(geometry.Handle, Coords.PointArray(target), Coords.DirArray(axis),
861
+ option.P, option.L, option.Waist.Mm, wavelength.Mm, dst.Handle) != 0)
536
862
  {
537
- throw new Autd3Exception("vortex failed (buffer device count must match geometry)");
863
+ throw new Autd3Exception("laguerre_gaussian_intensity failed (waist must be positive and buffer device count must match geometry)");
538
864
  }
539
865
  }
540
866
 
541
- public static void VortexDevice(Device device, Vector3 target, Vector3 axis, int order, Length wavelength, VortexOption option, Emission[] dst)
867
+ public static void LaguerreGaussianIntensityDevice(Device device, Vector3 target, Vector3 axis, LaguerreGaussianOption option, Length wavelength, Intensity[] dst)
542
868
  {
543
- var native = ToNativeDst(dst);
544
- var o = option.ToNative();
545
- if (NativePattern.autd3_pattern_vortex_device(device.GeometryHandle, device.DeviceIndex,
546
- Coords.PointArray(target), Coords.DirArray(axis), order, wavelength.Mm, in o, native) != 0)
869
+ var native = ToNative(dst);
870
+ if (NativePattern.autd3_pattern_laguerre_gaussian_intensity_device(device.GeometryHandle, device.DeviceIndex,
871
+ Coords.PointArray(target), Coords.DirArray(axis), option.P, option.L, option.Waist.Mm, wavelength.Mm, native) != 0)
547
872
  {
548
- throw new Autd3Exception("vortex_device failed");
873
+ throw new Autd3Exception("laguerre_gaussian_intensity_device failed (waist must be positive)");
549
874
  }
550
- FromNativeDst(native, dst);
875
+ FromNative(native, dst);
551
876
  }
552
877
 
553
- public static Emission VortexTransducer(Vector3 position, Vector3 target, Vector3 axis, int order, Length wavelength, VortexOption option)
878
+ public static void HermiteGaussianPhase(Geometry geometry, Vector3 target, Vector3 axis, Vector3 xDir, HermiteGaussianOption option, Length wavelength, PhaseBuffer dst)
554
879
  {
555
- var o = option.ToNative();
556
- if (NativePattern.autd3_pattern_vortex_transducer(
880
+ if (NativePattern.autd3_pattern_hermite_gaussian_phase(geometry.Handle, Coords.PointArray(target), Coords.DirArray(axis), Coords.DirArray(xDir),
881
+ option.M, option.N, option.Waist.Mm, wavelength.Mm, dst.Handle) != 0)
882
+ {
883
+ throw new Autd3Exception("hermite_gaussian_phase failed (waist must be positive and buffer device count must match geometry)");
884
+ }
885
+ }
886
+
887
+ public static void HermiteGaussianPhaseDevice(Device device, Vector3 target, Vector3 axis, Vector3 xDir, HermiteGaussianOption option, Length wavelength, Phase[] dst)
888
+ {
889
+ var native = ToNative(dst);
890
+ if (NativePattern.autd3_pattern_hermite_gaussian_phase_device(device.GeometryHandle, device.DeviceIndex,
891
+ Coords.PointArray(target), Coords.DirArray(axis), Coords.DirArray(xDir), option.M, option.N, option.Waist.Mm, wavelength.Mm, native) != 0)
892
+ {
893
+ throw new Autd3Exception("hermite_gaussian_phase_device failed (waist must be positive)");
894
+ }
895
+ FromNative(native, dst);
896
+ }
897
+
898
+ public static Phase HermiteGaussianPhaseTransducer(Vector3 position, Vector3 target, Vector3 axis, Vector3 xDir, HermiteGaussianOption option, Length wavelength)
899
+ {
900
+ if (NativePattern.autd3_pattern_hermite_gaussian_phase_transducer(
557
901
  Coords.PointArray(position),
558
902
  Coords.PointArray(target),
559
- Coords.DirArray(axis), order, wavelength.Mm, in o, out var e) != 0)
903
+ Coords.DirArray(axis),
904
+ Coords.DirArray(xDir), option.M, option.N, option.Waist.Mm, wavelength.Mm, out var p) != 0)
560
905
  {
561
- throw new Autd3Exception("vortex_transducer failed");
906
+ throw new Autd3Exception("hermite_gaussian_phase_transducer failed (waist must be positive)");
562
907
  }
563
- return new Emission(new Phase(e.Phase), new Intensity(e.Intensity));
908
+ return new Phase(p);
564
909
  }
565
910
 
566
- public static void Uniform(Emission emission, PatternBuffer dst)
911
+ public static void HermiteGaussianIntensity(Geometry geometry, Vector3 target, Vector3 axis, Vector3 xDir, HermiteGaussianOption option, Length wavelength, IntensityBuffer dst)
567
912
  {
568
- if (NativePattern.autd3_pattern_uniform(emission.Phase.Value, emission.Intensity.Value, dst.Handle) != 0)
913
+ if (NativePattern.autd3_pattern_hermite_gaussian_intensity(geometry.Handle, Coords.PointArray(target), Coords.DirArray(axis), Coords.DirArray(xDir),
914
+ option.M, option.N, option.Waist.Mm, wavelength.Mm, dst.Handle) != 0)
569
915
  {
570
- throw new Autd3Exception("uniform failed");
916
+ throw new Autd3Exception("hermite_gaussian_intensity failed (waist must be positive and buffer device count must match geometry)");
571
917
  }
572
918
  }
573
919
 
574
- public static void UniformDevice(Emission emission, Emission[] dst)
920
+ public static void HermiteGaussianIntensityDevice(Device device, Vector3 target, Vector3 axis, Vector3 xDir, HermiteGaussianOption option, Length wavelength, Intensity[] dst)
575
921
  {
576
- for (var i = 0; i < dst.Length; i++)
922
+ var native = ToNative(dst);
923
+ if (NativePattern.autd3_pattern_hermite_gaussian_intensity_device(device.GeometryHandle, device.DeviceIndex,
924
+ Coords.PointArray(target), Coords.DirArray(axis), Coords.DirArray(xDir), option.M, option.N, option.Waist.Mm, wavelength.Mm, native) != 0)
577
925
  {
578
- dst[i] = emission;
926
+ throw new Autd3Exception("hermite_gaussian_intensity_device failed (waist must be positive)");
579
927
  }
928
+ FromNative(native, dst);
580
929
  }
581
930
 
582
- public static void Null(PatternBuffer dst) => NativePattern.autd3_pattern_null(dst.Handle);
931
+ public static void SetIntensity(Intensity intensity, IntensityBuffer dst)
932
+ {
933
+ if (NativePattern.autd3_pattern_set_intensity(intensity.Value, dst.Handle) != 0)
934
+ {
935
+ throw new Autd3Exception("set_intensity failed");
936
+ }
937
+ }
583
938
 
584
- public static void NullDevice(Emission[] dst)
939
+ public static void SetPhase(Phase phase, PhaseBuffer dst)
585
940
  {
586
- for (var i = 0; i < dst.Length; i++)
941
+ if (NativePattern.autd3_pattern_set_phase(phase.Value, dst.Handle) != 0)
942
+ {
943
+ throw new Autd3Exception("set_phase failed");
944
+ }
945
+ }
946
+
947
+ public static void AddPhase(Phase phase, PhaseBuffer dst)
948
+ {
949
+ if (NativePattern.autd3_pattern_add_phase(phase.Value, dst.Handle) != 0)
950
+ {
951
+ throw new Autd3Exception("add_phase failed");
952
+ }
953
+ }
954
+
955
+ public static void Group<TKey>(Geometry geometry, TransducerGroups<TKey> groups, Func<TKey, PhaseBuffer> source, PhaseBuffer dst) where TKey : notnull
956
+ {
957
+ using var lease = GroupSources(geometry, groups, key => source(key)?.Handle, dst.Handle);
958
+ if (NativePattern.autd3_pattern_group_phase(geometry.Handle, groups.FlatIndices, lease.Pointers, (UIntPtr)groups.Keys.Count, dst.Handle) != 0)
587
959
  {
588
- dst[i] = Emission.Null;
960
+ throw new Autd3Exception("group failed (every buffer must match the geometry)");
589
961
  }
590
962
  }
591
963
 
592
- public static void NullTransducer(ref Emission dst) => dst = Emission.Null;
964
+ public static void Group<TKey>(Geometry geometry, TransducerGroups<TKey> groups, Func<TKey, IntensityBuffer> source, IntensityBuffer dst) where TKey : notnull
965
+ {
966
+ using var lease = GroupSources(geometry, groups, key => source(key)?.Handle, dst.Handle);
967
+ if (NativePattern.autd3_pattern_group_intensity(geometry.Handle, groups.FlatIndices, lease.Pointers, (UIntPtr)groups.Keys.Count, dst.Handle) != 0)
968
+ {
969
+ throw new Autd3Exception("group failed (every buffer must match the geometry)");
970
+ }
971
+ }
593
972
 
594
- private static EmissionNative[] ToNativeDst(Emission[] dst)
973
+ private static HandleArray GroupSources<TKey>(Geometry geometry, TransducerGroups<TKey> groups, Func<TKey, SafeHandle?> source, SafeHandle dst) where TKey : notnull
595
974
  {
975
+ if (groups.FlatIndices.Length != geometry.NumTransducers)
976
+ {
977
+ throw new Autd3Exception("groups must be built from the same geometry");
978
+ }
979
+ var keys = groups.Keys;
980
+ var handles = new SafeHandle[keys.Count];
981
+ for (var i = 0; i < keys.Count; i++)
982
+ {
983
+ var handle = source(keys[i]);
984
+ if (handle == null)
985
+ {
986
+ throw new Autd3Exception($"no source was given for the key {keys[i]}");
987
+ }
988
+ if (ReferenceEquals(handle, dst))
989
+ {
990
+ throw new Autd3Exception("dst must not be one of the sources");
991
+ }
992
+ handles[i] = handle;
993
+ }
994
+ return new HandleArray(handles);
995
+ }
996
+
997
+ public static void GroupCompute<TKey>(Geometry geometry, TransducerGroups<TKey> groups, Action<TKey, TransducerMask, PhaseBuffer, IntensityBuffer> compute, PhaseBuffer phases, IntensityBuffer intensities) where TKey : notnull
998
+ {
999
+ if (compute == null)
1000
+ {
1001
+ throw new ArgumentNullException(nameof(compute));
1002
+ }
1003
+ if (groups.FlatIndices.Length != geometry.NumTransducers)
1004
+ {
1005
+ throw new Autd3Exception("groups must be built from the same geometry");
1006
+ }
1007
+ if (phases.NumDevices != geometry.NumDevices || intensities.NumDevices != geometry.NumDevices)
1008
+ {
1009
+ throw new Autd3Exception("group_compute failed (dst must match the geometry)");
1010
+ }
1011
+ using var scratchPhases = geometry.PhaseBuffer();
1012
+ using var scratchIntensities = geometry.IntensityBuffer();
1013
+ var keys = groups.Keys;
1014
+ for (var i = 0; i < keys.Count; i++)
1015
+ {
1016
+ SetPhase(Phase.Zero, scratchPhases);
1017
+ SetIntensity(Intensity.Max, scratchIntensities);
1018
+ compute(keys[i], groups.MaskOf(i), scratchPhases, scratchIntensities);
1019
+ if (NativePattern.autd3_pattern_group_copy_phase(geometry.Handle, groups.FlatIndices, i, scratchPhases.Handle, phases.Handle) != 0
1020
+ || NativePattern.autd3_pattern_group_copy_intensity(geometry.Handle, groups.FlatIndices, i, scratchIntensities.Handle, intensities.Handle) != 0)
1021
+ {
1022
+ throw new Autd3Exception("group_compute failed (dst must match the geometry)");
1023
+ }
1024
+ }
1025
+ }
1026
+
1027
+ private static byte[] ToNative(Phase[] dst) => ToNative(dst, p => p.Value);
1028
+
1029
+ private static byte[] ToNative(Intensity[] dst) => ToNative(dst, i => i.Value);
1030
+
1031
+ private static byte[] ToNative<T>(T[] dst, Func<T, byte> value)
1032
+ {
1033
+ if (dst == null)
1034
+ {
1035
+ throw new ArgumentNullException(nameof(dst));
1036
+ }
596
1037
  if (dst.Length != Autd3.NumTransducers)
597
1038
  {
598
- throw new Autd3Exception($"dst requires {Autd3.NumTransducers} emissions");
1039
+ throw new Autd3Exception($"dst requires {Autd3.NumTransducers} elements");
1040
+ }
1041
+ var native = new byte[dst.Length];
1042
+ for (var i = 0; i < dst.Length; i++)
1043
+ {
1044
+ native[i] = value(dst[i]);
1045
+ }
1046
+ return native;
1047
+ }
1048
+
1049
+ private static void FromNative(byte[] native, Phase[] dst)
1050
+ {
1051
+ for (var i = 0; i < dst.Length; i++)
1052
+ {
1053
+ dst[i] = new Phase(native[i]);
599
1054
  }
600
- return new EmissionNative[dst.Length];
601
1055
  }
602
1056
 
603
- private static void FromNativeDst(EmissionNative[] native, Emission[] dst)
1057
+ private static void FromNative(byte[] native, Intensity[] dst)
604
1058
  {
605
1059
  for (var i = 0; i < dst.Length; i++)
606
1060
  {
607
- dst[i] = new Emission(new Phase(native[i].Phase), new Intensity(native[i].Intensity));
1061
+ dst[i] = new Intensity(native[i]);
608
1062
  }
609
1063
  }
610
1064
  }