com.shinolab.autd3-sdk.pattern 0.9.0 → 0.13.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
@@ -47,293 +47,236 @@ namespace AUTD3
47
47
  public static TransducerMask AllEnabled => new TransducerMask(null);
48
48
 
49
49
  public static TransducerMask Masked(bool[][] mask) => new TransducerMask(mask);
50
- }
51
-
52
- public sealed class TransducerGroups<TKey> where TKey : struct
53
- {
54
- private readonly int[] _numTransducers;
55
- private readonly List<TKey> _keys = new List<TKey>();
56
- private readonly Dictionary<TKey, int> _lookup = new Dictionary<TKey, int>();
57
-
58
- internal int[] Indices { get; }
59
-
60
- public IReadOnlyList<TKey> Keys => _keys;
61
50
 
62
- public int NumDevices => _numTransducers.Length;
63
-
64
- public TransducerGroups(Geometry geometry, Func<Device, int, TKey?> key)
51
+ public void Validate(Geometry geometry)
65
52
  {
66
- _numTransducers = new int[geometry.NumDevices];
67
- Indices = new int[geometry.NumTransducers];
68
- var k = 0;
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
+ }
69
63
  var dev = 0;
70
64
  foreach (var device in geometry)
71
65
  {
72
- var numTransducers = device.NumTransducers;
73
- _numTransducers[dev++] = numTransducers;
74
- for (var tr = 0; tr < numTransducers; tr++)
66
+ var got = Mask[dev]?.Length ?? 0;
67
+ if (got != device.NumTransducers)
75
68
  {
76
- var value = key(device, tr);
77
- if (value == null)
78
- {
79
- Indices[k++] = -1;
80
- continue;
81
- }
82
- if (!_lookup.TryGetValue(value.Value, out var index))
83
- {
84
- index = _keys.Count;
85
- _lookup.Add(value.Value, index);
86
- _keys.Add(value.Value);
87
- }
88
- Indices[k++] = index;
69
+ throw new Autd3Exception(
70
+ $"the mask slot for device {dev} has {got} transducers but the device has {device.NumTransducers}",
71
+ Autd3ErrorCode.InvalidArgument);
89
72
  }
73
+ dev++;
90
74
  }
91
75
  }
92
76
 
93
- public TKey? Key(int device, int transducer)
94
- {
95
- if (device < 0 || device >= _numTransducers.Length)
96
- {
97
- throw new ArgumentOutOfRangeException(nameof(device));
98
- }
99
- if (transducer < 0 || transducer >= _numTransducers[device])
100
- {
101
- throw new ArgumentOutOfRangeException(nameof(transducer));
102
- }
103
- var offset = 0;
104
- for (var dev = 0; dev < device; dev++)
105
- {
106
- offset += _numTransducers[dev];
107
- }
108
- var index = Indices[offset + transducer];
109
- return index < 0 ? (TKey?)null : _keys[index];
110
- }
77
+ public bool IsEnabled(int device, int transducer) => Mask is null || Mask[device][transducer];
111
78
 
112
- public TransducerMask Mask(TKey key)
79
+ public int NumEnabled(Geometry geometry)
113
80
  {
114
- if (!_lookup.TryGetValue(key, out var index))
81
+ if (Mask is null)
115
82
  {
116
- throw new ArgumentException($"no transducer is assigned the key {key}", nameof(key));
83
+ return geometry.NumTransducers;
117
84
  }
118
- var mask = new bool[_numTransducers.Length][];
119
- var k = 0;
120
- for (var dev = 0; dev < mask.Length; dev++)
85
+ var count = 0;
86
+ foreach (var device in Mask)
121
87
  {
122
- mask[dev] = new bool[_numTransducers[dev]];
123
- for (var tr = 0; tr < mask[dev].Length; tr++)
88
+ foreach (var enabled in device)
124
89
  {
125
- mask[dev][tr] = Indices[k++] == index;
90
+ if (enabled)
91
+ {
92
+ count++;
93
+ }
126
94
  }
127
95
  }
128
- return TransducerMask.Masked(mask);
96
+ return count;
129
97
  }
130
98
  }
131
99
 
132
- [StructLayout(LayoutKind.Sequential)]
133
- internal struct EmissionNative
134
- {
135
- public byte Phase;
136
- public byte Intensity;
137
- }
138
-
139
100
  internal static class NativePattern
140
101
  {
141
102
  private const string Lib = "autd3_pattern";
142
103
 
143
- private const string ClientLib = "autd3capi";
144
-
145
- static NativePattern()
146
- {
147
- NativeAbi.Verify(Lib, autd3_abi_version());
148
- NativeAbi.Verify(ClientLib, autd3capi_abi_version());
149
- }
104
+ static NativePattern() => NativeAbi.Verify(Lib, autd3_abi_version());
150
105
 
151
106
  [DllImport(Lib, CallingConvention = CallingConvention.Cdecl)]
152
107
  private static extern uint autd3_abi_version();
153
108
 
154
- [DllImport(ClientLib, EntryPoint = "autd3_abi_version", CallingConvention = CallingConvention.Cdecl)]
155
- private static extern uint autd3capi_abi_version();
156
-
157
109
  [DllImport(Lib, CallingConvention = CallingConvention.Cdecl)]
158
110
  internal static extern float autd3_pattern_wavelength(float soundSpeedMmPerS);
159
111
 
160
112
  [DllImport(Lib, CallingConvention = CallingConvention.Cdecl)]
161
- internal static extern IntPtr autd3_core_geometry_pattern_buffer(GeometryHandle geometry);
113
+ internal static extern IntPtr autd3_core_geometry_phase_buffer(GeometryHandle geometry);
162
114
 
163
115
  [DllImport(Lib, CallingConvention = CallingConvention.Cdecl)]
164
- internal static extern IntPtr autd3_pattern_buffer_from_array(EmissionNative[] emissions, UIntPtr numDevices);
116
+ internal static extern IntPtr autd3_phase_buffer_from_array(byte[] src, UIntPtr numDevices);
165
117
 
166
118
  [DllImport(Lib, CallingConvention = CallingConvention.Cdecl)]
167
- internal static extern UIntPtr autd3_pattern_buffer_num_devices(PatternBufferHandle buffer);
119
+ internal static extern UIntPtr autd3_phase_buffer_num_devices(PhaseBufferHandle buffer);
168
120
 
169
121
  [DllImport(Lib, CallingConvention = CallingConvention.Cdecl)]
170
- internal static extern UIntPtr autd3_pattern_buffer_num_transducers(PatternBufferHandle buffer, UIntPtr dev);
122
+ internal static extern UIntPtr autd3_phase_buffer_num_transducers(PhaseBufferHandle buffer, UIntPtr dev);
171
123
 
172
124
  [DllImport(Lib, CallingConvention = CallingConvention.Cdecl)]
173
- internal static extern int autd3_pattern_buffer_get(PatternBufferHandle buffer, UIntPtr dev, UIntPtr tr, out EmissionNative @out);
125
+ internal static extern int autd3_phase_buffer_get(PhaseBufferHandle buffer, UIntPtr dev, UIntPtr tr, out byte @out);
174
126
 
175
127
  [DllImport(Lib, CallingConvention = CallingConvention.Cdecl)]
176
- internal static extern int autd3_pattern_buffer_set(PatternBufferHandle buffer, UIntPtr dev, UIntPtr tr, EmissionNative emission);
128
+ internal static extern int autd3_phase_buffer_set(PhaseBufferHandle buffer, UIntPtr dev, UIntPtr tr, byte value);
177
129
 
178
130
  [DllImport(Lib, CallingConvention = CallingConvention.Cdecl)]
179
- internal static extern void autd3_pattern_buffer_free(IntPtr buffer);
131
+ internal static extern int autd3_phase_buffer_copy_to(PhaseBufferHandle buffer, [Out] byte[] dst, UIntPtr len);
180
132
 
181
133
  [DllImport(Lib, CallingConvention = CallingConvention.Cdecl)]
182
- internal static extern int autd3_pattern_focus(GeometryHandle geometry, float[] target, float wavelengthMm, PatternBufferHandle buffer);
134
+ internal static extern void autd3_phase_buffer_free(IntPtr buffer);
183
135
 
184
136
  [DllImport(Lib, CallingConvention = CallingConvention.Cdecl)]
185
- internal static extern int autd3_pattern_focus_device(GeometryHandle geometry, UIntPtr dev, float[] target, float wavelengthMm, [In, Out] EmissionNative[] dst);
137
+ internal static extern IntPtr autd3_core_geometry_intensity_buffer(GeometryHandle geometry);
186
138
 
187
139
  [DllImport(Lib, CallingConvention = CallingConvention.Cdecl)]
188
- internal static extern int autd3_pattern_focus_transducer(float[] position, float[] target, float wavelengthMm, out byte @out);
140
+ internal static extern IntPtr autd3_intensity_buffer_from_array(byte[] src, UIntPtr numDevices);
189
141
 
190
142
  [DllImport(Lib, CallingConvention = CallingConvention.Cdecl)]
191
- internal static extern int autd3_pattern_plane(GeometryHandle geometry, float[] dir, float wavelengthMm, PatternBufferHandle buffer);
143
+ internal static extern UIntPtr autd3_intensity_buffer_num_devices(IntensityBufferHandle buffer);
192
144
 
193
145
  [DllImport(Lib, CallingConvention = CallingConvention.Cdecl)]
194
- internal static extern int autd3_pattern_plane_device(GeometryHandle geometry, UIntPtr dev, float[] dir, float wavelengthMm, [In, Out] EmissionNative[] dst);
146
+ internal static extern UIntPtr autd3_intensity_buffer_num_transducers(IntensityBufferHandle buffer, UIntPtr dev);
195
147
 
196
148
  [DllImport(Lib, CallingConvention = CallingConvention.Cdecl)]
197
- internal static extern int autd3_pattern_plane_transducer(float[] position, float[] dir, float wavelengthMm, out byte @out);
149
+ internal static extern int autd3_intensity_buffer_get(IntensityBufferHandle buffer, UIntPtr dev, UIntPtr tr, out byte @out);
198
150
 
199
151
  [DllImport(Lib, CallingConvention = CallingConvention.Cdecl)]
200
- internal static extern int autd3_pattern_bessel(GeometryHandle geometry, float[] apex, float[] dir, float thetaRad, float wavelengthMm, PatternBufferHandle buffer);
152
+ internal static extern int autd3_intensity_buffer_set(IntensityBufferHandle buffer, UIntPtr dev, UIntPtr tr, byte value);
201
153
 
202
154
  [DllImport(Lib, CallingConvention = CallingConvention.Cdecl)]
203
- internal static extern int autd3_pattern_bessel_device(GeometryHandle geometry, UIntPtr dev, float[] apex, float[] dir, float thetaRad, float wavelengthMm, [In, Out] EmissionNative[] dst);
155
+ internal static extern int autd3_intensity_buffer_copy_to(IntensityBufferHandle buffer, [Out] byte[] dst, UIntPtr len);
204
156
 
205
157
  [DllImport(Lib, CallingConvention = CallingConvention.Cdecl)]
206
- internal static extern int autd3_pattern_bessel_transducer(float[] position, float[] apex, float[] dir, float thetaRad, float wavelengthMm, out byte @out);
158
+ internal static extern void autd3_intensity_buffer_free(IntPtr buffer);
207
159
 
208
160
  [DllImport(Lib, CallingConvention = CallingConvention.Cdecl)]
209
- internal static extern int autd3_pattern_laguerre_gaussian_phase(GeometryHandle geometry, float[] target, float[] axis, uint p, int l, float waistMm, float wavelengthMm, PatternBufferHandle buffer);
161
+ internal static extern int autd3_pattern_focus(GeometryHandle geometry, float[] target, float wavelengthMm, PhaseBufferHandle buffer);
210
162
 
211
163
  [DllImport(Lib, CallingConvention = CallingConvention.Cdecl)]
212
- 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] EmissionNative[] dst);
164
+ internal static extern int autd3_pattern_focus_transducer(float[] position, float[] target, float wavelengthMm, out byte @out);
213
165
 
214
166
  [DllImport(Lib, CallingConvention = CallingConvention.Cdecl)]
215
- 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);
167
+ internal static extern int autd3_pattern_plane(GeometryHandle geometry, float[] dir, float wavelengthMm, PhaseBufferHandle buffer);
216
168
 
217
169
  [DllImport(Lib, CallingConvention = CallingConvention.Cdecl)]
218
- internal static extern int autd3_pattern_laguerre_gaussian_intensity(GeometryHandle geometry, float[] target, float[] axis, uint p, int l, float waistMm, float wavelengthMm, PatternBufferHandle buffer);
170
+ internal static extern int autd3_pattern_plane_transducer(float[] position, float[] dir, float wavelengthMm, out byte @out);
219
171
 
220
172
  [DllImport(Lib, CallingConvention = CallingConvention.Cdecl)]
221
- 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] EmissionNative[] dst);
173
+ internal static extern int autd3_pattern_bessel(GeometryHandle geometry, float[] apex, float[] dir, float thetaRad, float wavelengthMm, PhaseBufferHandle buffer);
222
174
 
223
175
  [DllImport(Lib, CallingConvention = CallingConvention.Cdecl)]
224
- 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, PatternBufferHandle buffer);
176
+ internal static extern int autd3_pattern_bessel_transducer(float[] position, float[] apex, float[] dir, float thetaRad, float wavelengthMm, out byte @out);
225
177
 
226
178
  [DllImport(Lib, CallingConvention = CallingConvention.Cdecl)]
227
- 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] EmissionNative[] dst);
179
+ 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);
228
180
 
229
181
  [DllImport(Lib, CallingConvention = CallingConvention.Cdecl)]
230
- 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);
182
+ 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);
231
183
 
232
184
  [DllImport(Lib, CallingConvention = CallingConvention.Cdecl)]
233
- 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, PatternBufferHandle buffer);
185
+ 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);
234
186
 
235
187
  [DllImport(Lib, CallingConvention = CallingConvention.Cdecl)]
236
- 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] EmissionNative[] dst);
188
+ 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);
237
189
 
238
190
  [DllImport(Lib, CallingConvention = CallingConvention.Cdecl)]
239
- internal static extern int autd3_pattern_set_intensity(byte intensity, PatternBufferHandle buffer);
191
+ 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);
240
192
 
241
193
  [DllImport(Lib, CallingConvention = CallingConvention.Cdecl)]
242
- internal static extern int autd3_pattern_set_intensity_device(byte intensity, [In, Out] EmissionNative[] dst, UIntPtr len);
194
+ 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);
195
+ }
243
196
 
244
- [DllImport(Lib, CallingConvention = CallingConvention.Cdecl)]
245
- internal static extern int autd3_pattern_set_phase(byte phase, PatternBufferHandle buffer);
197
+ internal static class NativePatternOp
198
+ {
199
+ private const string Lib = "autd3capi";
246
200
 
247
- [DllImport(Lib, CallingConvention = CallingConvention.Cdecl)]
248
- internal static extern int autd3_pattern_set_phase_device(byte phase, [In, Out] EmissionNative[] dst, UIntPtr len);
201
+ static NativePatternOp() => NativeAbi.Verify(Lib, autd3_abi_version());
249
202
 
250
203
  [DllImport(Lib, CallingConvention = CallingConvention.Cdecl)]
251
- internal static extern int autd3_pattern_set_phase_and_intensity(byte phase, byte intensity, PatternBufferHandle buffer);
204
+ private static extern uint autd3_abi_version();
252
205
 
253
206
  [DllImport(Lib, CallingConvention = CallingConvention.Cdecl)]
254
- internal static extern int autd3_pattern_set_phase_and_intensity_device(byte phase, byte intensity, [In, Out] EmissionNative[] dst, UIntPtr len);
207
+ internal static extern IntPtr autd3_op_pattern(byte bank, PhaseBufferHandle phases, IntPtr intensities, byte uniformIntensity, byte transitionMode, ulong transitionValue);
255
208
 
256
209
  [DllImport(Lib, CallingConvention = CallingConvention.Cdecl)]
257
- internal static extern int autd3_pattern_add_phase(byte phase, PatternBufferHandle buffer);
210
+ internal static extern IntPtr autd3_op_write_pattern_phase(byte bank, ushort startIndex, byte depth, byte intensity, IntPtr[] patterns, UIntPtr numPatterns);
258
211
 
259
212
  [DllImport(Lib, CallingConvention = CallingConvention.Cdecl)]
260
- internal static extern int autd3_pattern_add_phase_device(byte phase, [In, Out] EmissionNative[] dst, UIntPtr len);
213
+ internal static extern IntPtr autd3_op_config_pattern(byte bank, ushort samplingDivide, uint size, ushort rep);
261
214
 
262
215
  [DllImport(Lib, CallingConvention = CallingConvention.Cdecl)]
263
- internal static extern int autd3_pattern_group(GeometryHandle geometry, int[] keys, IntPtr[] sources, UIntPtr numSources, PatternBufferHandle buffer);
216
+ internal static extern IntPtr autd3_op_config_foci_stm(byte bank, ushort samplingDivide, uint size, byte numFoci, float soundSpeedMPerS, ushort rep);
264
217
 
265
218
  [DllImport(Lib, CallingConvention = CallingConvention.Cdecl)]
266
- internal static extern int autd3_pattern_group_null(GeometryHandle geometry, int[] indices, PatternBufferHandle buffer);
219
+ internal static extern IntPtr autd3_op_activate_pattern_bank(byte bank, byte transitionMode, ulong transitionValue);
267
220
 
268
221
  [DllImport(Lib, CallingConvention = CallingConvention.Cdecl)]
269
- internal static extern int autd3_pattern_group_copy(GeometryHandle geometry, int[] indices, int index, PatternBufferHandle source, PatternBufferHandle buffer);
270
-
271
-
272
- [DllImport(ClientLib, CallingConvention = CallingConvention.Cdecl)]
273
- internal static extern IntPtr autd3_op_pattern(byte bank, PatternBufferHandle patternBuffer, byte transitionMode, ulong transitionValue, uint transitionMarginNs);
274
-
275
- [DllImport(ClientLib, CallingConvention = CallingConvention.Cdecl)]
276
- internal static extern IntPtr autd3_op_write_pattern_buffer(byte bank, ushort index, PatternBufferHandle patternBuffer);
277
-
278
- [DllImport(ClientLib, CallingConvention = CallingConvention.Cdecl)]
279
- internal static extern IntPtr autd3_op_write_pattern_compressed(byte bank, uint index, byte format, IntPtr[] patterns, UIntPtr numPatterns);
280
-
281
- [DllImport(ClientLib, CallingConvention = CallingConvention.Cdecl)]
282
- internal static extern IntPtr autd3_op_config_pattern(byte bank, IntPtr samplingConfig, uint size, ushort rep);
283
-
284
- [DllImport(ClientLib, CallingConvention = CallingConvention.Cdecl)]
285
- internal static extern IntPtr autd3_op_config_foci_stm(byte bank, IntPtr samplingConfig, uint size, byte numFoci, float soundSpeedMPerS, ushort rep);
222
+ internal static extern int autd3_phase_depth_max_count(byte depth, out UIntPtr @out);
223
+ }
286
224
 
287
- [DllImport(ClientLib, CallingConvention = CallingConvention.Cdecl)]
288
- internal static extern IntPtr autd3_op_change_pattern_bank(byte bank, byte transitionMode, ulong transitionValue, uint transitionMarginNs);
225
+ internal sealed class PhaseBufferHandle : Autd3SafeHandle
226
+ {
227
+ internal PhaseBufferHandle(IntPtr handle) : base(handle)
228
+ {
229
+ }
289
230
 
290
- [DllImport(ClientLib, CallingConvention = CallingConvention.Cdecl)]
291
- internal static extern int autd3_pattern_compression_per_frame(byte format, out UIntPtr @out);
231
+ protected override bool ReleaseHandle()
232
+ {
233
+ NativePattern.autd3_phase_buffer_free(handle);
234
+ return true;
235
+ }
292
236
  }
293
237
 
294
- internal sealed class PatternBufferHandle : Autd3SafeHandle
238
+ internal sealed class IntensityBufferHandle : Autd3SafeHandle
295
239
  {
296
- internal PatternBufferHandle(IntPtr handle) : base(handle)
240
+ internal IntensityBufferHandle(IntPtr handle) : base(handle)
297
241
  {
298
242
  }
299
243
 
300
244
  protected override bool ReleaseHandle()
301
245
  {
302
- NativePattern.autd3_pattern_buffer_free(handle);
246
+ NativePattern.autd3_intensity_buffer_free(handle);
303
247
  return true;
304
248
  }
305
249
  }
306
250
 
307
- public readonly struct DevicePattern : IEnumerable<Emission>
251
+ public readonly struct DevicePhases : IEnumerable<Phase>
308
252
  {
309
- private readonly PatternBufferHandle _buffer;
253
+ private readonly PhaseBufferHandle _buffer;
310
254
  private readonly UIntPtr _dev;
311
255
 
312
- internal DevicePattern(PatternBufferHandle buffer, UIntPtr dev)
256
+ internal DevicePhases(PhaseBufferHandle buffer, UIntPtr dev)
313
257
  {
314
258
  _buffer = buffer;
315
259
  _dev = dev;
316
260
  }
317
261
 
318
- public int NumTransducers => (int)NativePattern.autd3_pattern_buffer_num_transducers(_buffer, _dev);
262
+ public int NumTransducers => (int)NativePattern.autd3_phase_buffer_num_transducers(_buffer, _dev);
319
263
 
320
- public Emission this[int tr]
264
+ public Phase this[int tr]
321
265
  {
322
266
  get
323
267
  {
324
- if (NativePattern.autd3_pattern_buffer_get(_buffer, _dev, (UIntPtr)tr, out var e) != 0)
268
+ if (NativePattern.autd3_phase_buffer_get(_buffer, _dev, (UIntPtr)tr, out var v) != 0)
325
269
  throw new ArgumentOutOfRangeException(nameof(tr));
326
- return new Emission(new Phase(e.Phase), new Intensity(e.Intensity));
270
+ return new Phase(v);
327
271
  }
328
272
  set
329
273
  {
330
- var native = new EmissionNative { Phase = value.Phase.Value, Intensity = value.Intensity.Value };
331
- if (NativePattern.autd3_pattern_buffer_set(_buffer, _dev, (UIntPtr)tr, native) != 0)
274
+ if (NativePattern.autd3_phase_buffer_set(_buffer, _dev, (UIntPtr)tr, value.Value) != 0)
332
275
  throw new ArgumentOutOfRangeException(nameof(tr));
333
276
  }
334
277
  }
335
278
 
336
- public IEnumerator<Emission> GetEnumerator()
279
+ public IEnumerator<Phase> GetEnumerator()
337
280
  {
338
281
  var count = NumTransducers;
339
282
  for (var i = 0; i < count; i++)
@@ -345,58 +288,177 @@ namespace AUTD3
345
288
  IEnumerator IEnumerable.GetEnumerator() => GetEnumerator();
346
289
  }
347
290
 
348
- public sealed class PatternBuffer : IDisposable, IEnumerable<DevicePattern>
291
+ public readonly struct DeviceIntensities : IEnumerable<Intensity>
349
292
  {
350
- internal const int NumTransducers = 249;
293
+ private readonly IntensityBufferHandle _buffer;
294
+ private readonly UIntPtr _dev;
351
295
 
352
- private readonly PatternBufferHandle _handle;
296
+ internal DeviceIntensities(IntensityBufferHandle buffer, UIntPtr dev)
297
+ {
298
+ _buffer = buffer;
299
+ _dev = dev;
300
+ }
353
301
 
354
- internal PatternBufferHandle Handle => _handle;
302
+ public int NumTransducers => (int)NativePattern.autd3_intensity_buffer_num_transducers(_buffer, _dev);
355
303
 
356
- internal PatternBuffer(IntPtr handle)
304
+ public Intensity this[int tr]
305
+ {
306
+ get
307
+ {
308
+ if (NativePattern.autd3_intensity_buffer_get(_buffer, _dev, (UIntPtr)tr, out var v) != 0)
309
+ throw new ArgumentOutOfRangeException(nameof(tr));
310
+ return new Intensity(v);
311
+ }
312
+ set
313
+ {
314
+ if (NativePattern.autd3_intensity_buffer_set(_buffer, _dev, (UIntPtr)tr, value.Value) != 0)
315
+ throw new ArgumentOutOfRangeException(nameof(tr));
316
+ }
317
+ }
318
+
319
+ public IEnumerator<Intensity> GetEnumerator()
320
+ {
321
+ var count = NumTransducers;
322
+ for (var i = 0; i < count; i++)
323
+ {
324
+ yield return this[i];
325
+ }
326
+ }
327
+
328
+ IEnumerator IEnumerable.GetEnumerator() => GetEnumerator();
329
+ }
330
+
331
+ public sealed class PhaseBuffer : IDisposable, IEnumerable<DevicePhases>
332
+ {
333
+ private readonly PhaseBufferHandle _handle;
334
+
335
+ internal PhaseBufferHandle Handle => _handle;
336
+
337
+ internal PhaseBuffer(IntPtr handle)
357
338
  {
358
339
  if (handle == IntPtr.Zero)
359
340
  {
360
- throw new Autd3Exception("failed to create pattern buffer");
341
+ throw new Autd3Exception("failed to create phase buffer");
361
342
  }
362
- _handle = new PatternBufferHandle(handle);
343
+ _handle = new PhaseBufferHandle(handle);
363
344
  }
364
345
 
365
- public static PatternBuffer FromArray(Emission[][] emissions)
346
+ public static PhaseBuffer FromArray(Phase[][] phases)
366
347
  {
367
- var numDevices = emissions.Length;
368
- var flat = new EmissionNative[numDevices * NumTransducers];
369
- for (var d = 0; d < numDevices; d++)
348
+ var flat = BufferArray.Flatten(phases, p => p.Value, "phases");
349
+ return new PhaseBuffer(NativePattern.autd3_phase_buffer_from_array(flat, (UIntPtr)phases.Length));
350
+ }
351
+
352
+ public int NumDevices => (int)NativePattern.autd3_phase_buffer_num_devices(Handle);
353
+
354
+ public Phase[][] ToArray()
355
+ {
356
+ var result = new Phase[NumDevices][];
357
+ var total = 0;
358
+ for (var dev = 0; dev < result.Length; dev++)
370
359
  {
371
- if (emissions[d].Length != NumTransducers)
360
+ result[dev] = new Phase[(int)NativePattern.autd3_phase_buffer_num_transducers(Handle, (UIntPtr)dev)];
361
+ total += result[dev].Length;
362
+ }
363
+ var flat = new byte[total];
364
+ if (NativePattern.autd3_phase_buffer_copy_to(Handle, flat, (UIntPtr)flat.Length) != 0)
365
+ {
366
+ throw new Autd3Exception("failed to copy the buffer");
367
+ }
368
+ var k = 0;
369
+ foreach (var device in result)
370
+ {
371
+ for (var tr = 0; tr < device.Length; tr++)
372
372
  {
373
- throw new Autd3Exception($"each device requires {NumTransducers} emissions");
373
+ device[tr] = new Phase(flat[k++]);
374
374
  }
375
- for (var t = 0; t < NumTransducers; t++)
375
+ }
376
+ return result;
377
+ }
378
+
379
+ public DevicePhases this[int dev]
380
+ {
381
+ get
382
+ {
383
+ if (dev < 0 || dev >= NumDevices)
384
+ throw new ArgumentOutOfRangeException(nameof(dev));
385
+ return new DevicePhases(Handle, (UIntPtr)dev);
386
+ }
387
+ }
388
+
389
+ public IEnumerator<DevicePhases> GetEnumerator()
390
+ {
391
+ var count = NumDevices;
392
+ for (var i = 0; i < count; i++)
393
+ {
394
+ yield return this[i];
395
+ }
396
+ }
397
+
398
+ IEnumerator IEnumerable.GetEnumerator() => GetEnumerator();
399
+
400
+ public void Dispose() => _handle.Dispose();
401
+ }
402
+
403
+ public sealed class IntensityBuffer : IDisposable, IEnumerable<DeviceIntensities>
404
+ {
405
+ private readonly IntensityBufferHandle _handle;
406
+
407
+ internal IntensityBufferHandle Handle => _handle;
408
+
409
+ internal IntensityBuffer(IntPtr handle)
410
+ {
411
+ if (handle == IntPtr.Zero)
412
+ {
413
+ throw new Autd3Exception("failed to create intensity buffer");
414
+ }
415
+ _handle = new IntensityBufferHandle(handle);
416
+ }
417
+
418
+ public static IntensityBuffer FromArray(Intensity[][] intensities)
419
+ {
420
+ var flat = BufferArray.Flatten(intensities, i => i.Value, "intensities");
421
+ return new IntensityBuffer(NativePattern.autd3_intensity_buffer_from_array(flat, (UIntPtr)intensities.Length));
422
+ }
423
+
424
+ public int NumDevices => (int)NativePattern.autd3_intensity_buffer_num_devices(Handle);
425
+
426
+ public Intensity[][] ToArray()
427
+ {
428
+ var result = new Intensity[NumDevices][];
429
+ var total = 0;
430
+ for (var dev = 0; dev < result.Length; dev++)
431
+ {
432
+ result[dev] = new Intensity[(int)NativePattern.autd3_intensity_buffer_num_transducers(Handle, (UIntPtr)dev)];
433
+ total += result[dev].Length;
434
+ }
435
+ var flat = new byte[total];
436
+ if (NativePattern.autd3_intensity_buffer_copy_to(Handle, flat, (UIntPtr)flat.Length) != 0)
437
+ {
438
+ throw new Autd3Exception("failed to copy the buffer");
439
+ }
440
+ var k = 0;
441
+ foreach (var device in result)
442
+ {
443
+ for (var tr = 0; tr < device.Length; tr++)
376
444
  {
377
- flat[d * NumTransducers + t] = new EmissionNative
378
- {
379
- Phase = emissions[d][t].Phase.Value,
380
- Intensity = emissions[d][t].Intensity.Value,
381
- };
445
+ device[tr] = new Intensity(flat[k++]);
382
446
  }
383
447
  }
384
- return new PatternBuffer(NativePattern.autd3_pattern_buffer_from_array(flat, (UIntPtr)numDevices));
448
+ return result;
385
449
  }
386
450
 
387
- public int NumDevices => (int)NativePattern.autd3_pattern_buffer_num_devices(Handle);
388
-
389
- public DevicePattern this[int dev]
451
+ public DeviceIntensities this[int dev]
390
452
  {
391
453
  get
392
454
  {
393
455
  if (dev < 0 || dev >= NumDevices)
394
456
  throw new ArgumentOutOfRangeException(nameof(dev));
395
- return new DevicePattern(Handle, (UIntPtr)dev);
457
+ return new DeviceIntensities(Handle, (UIntPtr)dev);
396
458
  }
397
459
  }
398
460
 
399
- public IEnumerator<DevicePattern> GetEnumerator()
461
+ public IEnumerator<DeviceIntensities> GetEnumerator()
400
462
  {
401
463
  var count = NumDevices;
402
464
  for (var i = 0; i < count; i++)
@@ -410,55 +472,102 @@ namespace AUTD3
410
472
  public void Dispose() => _handle.Dispose();
411
473
  }
412
474
 
475
+ internal static class BufferArray
476
+ {
477
+ internal static byte[] Flatten<T>(T[][] src, Func<T, byte> value, string name)
478
+ {
479
+ if (src == null)
480
+ {
481
+ throw new ArgumentNullException(name);
482
+ }
483
+ var flat = new byte[src.Length * Autd3.NumTransducers];
484
+ for (var d = 0; d < src.Length; d++)
485
+ {
486
+ if (src[d] == null || src[d].Length != Autd3.NumTransducers)
487
+ {
488
+ throw new Autd3Exception($"each device requires {Autd3.NumTransducers} {name}", Autd3ErrorCode.InvalidArgument);
489
+ }
490
+ for (var t = 0; t < Autd3.NumTransducers; t++)
491
+ {
492
+ flat[d * Autd3.NumTransducers + t] = value(src[d][t]);
493
+ }
494
+ }
495
+ return flat;
496
+ }
497
+ }
498
+
413
499
  public static class GeometryPatternBufferExtensions
414
500
  {
415
- public static PatternBuffer PatternBuffer(this Geometry geometry) =>
416
- new PatternBuffer(NativePattern.autd3_core_geometry_pattern_buffer(geometry.Handle));
501
+ public static PhaseBuffer PhaseBuffer(this Geometry geometry) =>
502
+ new PhaseBuffer(NativePattern.autd3_core_geometry_phase_buffer(geometry.Handle));
503
+
504
+ public static IntensityBuffer IntensityBuffer(this Geometry geometry) =>
505
+ new IntensityBuffer(NativePattern.autd3_core_geometry_intensity_buffer(geometry.Handle));
506
+ }
507
+
508
+ public readonly struct PatternIntensity
509
+ {
510
+ private readonly IntensityBuffer? _buffer;
511
+ private readonly Intensity? _uniform;
512
+
513
+ public PatternIntensity(Intensity uniform)
514
+ {
515
+ _buffer = null;
516
+ _uniform = uniform;
517
+ }
518
+
519
+ public PatternIntensity(IntensityBuffer buffer)
520
+ {
521
+ _buffer = buffer ?? throw new ArgumentNullException(nameof(buffer));
522
+ _uniform = null;
523
+ }
524
+
525
+ internal IntensityBuffer? Buffer => _buffer;
526
+
527
+ internal byte Uniform => (_uniform ?? Intensity.Max).Value;
528
+
529
+ public static implicit operator PatternIntensity(Intensity uniform) => new PatternIntensity(uniform);
530
+
531
+ public static implicit operator PatternIntensity(IntensityBuffer buffer) => new PatternIntensity(buffer);
417
532
  }
418
533
 
419
534
  public sealed class Pattern : ICommand
420
535
  {
421
536
  private readonly PatternBank _bank;
422
- private readonly PatternBuffer _buffer;
537
+ private readonly PhaseBuffer _phases;
538
+ private readonly PatternIntensity _intensities;
423
539
  private readonly TransitionMode _transitionMode;
424
540
 
425
- public Pattern(PatternBuffer emissions, TransitionMode? transitionMode = null)
426
- : this(PatternBank.B0, emissions, transitionMode)
541
+ public Pattern(PhaseBuffer phases, PatternIntensity intensities, TransitionMode transitionMode = default)
542
+ : this(PatternBank.B0, phases, intensities, transitionMode)
427
543
  {
428
544
  }
429
545
 
430
- public Pattern(PatternBank bank, PatternBuffer emissions, TransitionMode? transitionMode = null)
546
+ public Pattern(PatternBank bank, PhaseBuffer phases, PatternIntensity intensities, TransitionMode transitionMode = default)
431
547
  {
432
548
  _bank = bank;
433
- _buffer = emissions;
434
- _transitionMode = transitionMode ?? TransitionMode.Immediate;
549
+ _phases = phases;
550
+ _intensities = intensities;
551
+ _transitionMode = transitionMode;
435
552
  }
436
553
 
437
- IntPtr ICommand.CreateOp() =>
438
- NativePattern.autd3_op_pattern((byte)_bank, _buffer.Handle, _transitionMode.Mode, _transitionMode.Value, _transitionMode.MarginNs);
554
+ IntPtr ICommand.CreateOp(Geometry geometry)
555
+ {
556
+ using var intensityLease = new HandleLease(_intensities.Buffer?.Handle);
557
+ return NativePatternOp.autd3_op_pattern((byte)_bank, _phases.Handle, intensityLease.Pointer, _intensities.Uniform, _transitionMode.Mode, _transitionMode.Value);
558
+ }
439
559
 
440
560
 
441
561
  public static Length Wavelength(Velocity soundSpeed) =>
442
562
  new Length(NativePattern.autd3_pattern_wavelength(soundSpeed.MmS));
443
563
 
444
564
 
445
- public static void Focus(Geometry geometry, Vector3 target, Length wavelength, PatternBuffer dst)
565
+ public static void Focus(Geometry geometry, Vector3 target, Length wavelength, PhaseBuffer dst)
446
566
  {
447
567
  if (NativePattern.autd3_pattern_focus(geometry.Handle, Coords.PointArray(target), wavelength.Mm, dst.Handle) != 0)
448
568
  {
449
- throw new Autd3Exception("focus failed (buffer device count must match geometry)");
450
- }
451
- }
452
-
453
- public static void FocusDevice(Device device, Vector3 target, Length wavelength, Emission[] dst)
454
- {
455
- var native = ToNativeDst(dst);
456
- if (NativePattern.autd3_pattern_focus_device(device.GeometryHandle, device.DeviceIndex,
457
- Coords.PointArray(target), wavelength.Mm, native) != 0)
458
- {
459
- throw new Autd3Exception("focus_device failed");
569
+ throw new Autd3Exception("focus failed (buffer device count must match geometry)", Autd3ErrorCode.InvalidArgument);
460
570
  }
461
- FromNativeDst(native, dst);
462
571
  }
463
572
 
464
573
  public static Phase FocusTransducer(Vector3 position, Vector3 target, Length wavelength)
@@ -467,28 +576,17 @@ namespace AUTD3
467
576
  Coords.PointArray(position),
468
577
  Coords.PointArray(target), wavelength.Mm, out var p) != 0)
469
578
  {
470
- throw new Autd3Exception("focus_transducer failed");
579
+ throw new Autd3Exception("focus_transducer failed", Autd3ErrorCode.InvalidArgument);
471
580
  }
472
581
  return new Phase(p);
473
582
  }
474
583
 
475
- public static void Plane(Geometry geometry, Vector3 dir, Length wavelength, PatternBuffer dst)
584
+ public static void Plane(Geometry geometry, Vector3 dir, Length wavelength, PhaseBuffer dst)
476
585
  {
477
586
  if (NativePattern.autd3_pattern_plane(geometry.Handle, Coords.DirArray(dir), wavelength.Mm, dst.Handle) != 0)
478
587
  {
479
- throw new Autd3Exception("plane failed (buffer device count must match geometry)");
480
- }
481
- }
482
-
483
- public static void PlaneDevice(Device device, Vector3 dir, Length wavelength, Emission[] dst)
484
- {
485
- var native = ToNativeDst(dst);
486
- if (NativePattern.autd3_pattern_plane_device(device.GeometryHandle, device.DeviceIndex,
487
- Coords.DirArray(dir), wavelength.Mm, native) != 0)
488
- {
489
- throw new Autd3Exception("plane_device failed");
588
+ throw new Autd3Exception("plane failed (buffer device count must match geometry)", Autd3ErrorCode.InvalidArgument);
490
589
  }
491
- FromNativeDst(native, dst);
492
590
  }
493
591
 
494
592
  public static Phase PlaneTransducer(Vector3 position, Vector3 dir, Length wavelength)
@@ -497,31 +595,19 @@ namespace AUTD3
497
595
  Coords.PointArray(position),
498
596
  Coords.DirArray(dir), wavelength.Mm, out var p) != 0)
499
597
  {
500
- throw new Autd3Exception("plane_transducer failed");
598
+ throw new Autd3Exception("plane_transducer failed", Autd3ErrorCode.InvalidArgument);
501
599
  }
502
600
  return new Phase(p);
503
601
  }
504
602
 
505
- public static void Bessel(Geometry geometry, Vector3 apex, Vector3 dir, Angle theta, Length wavelength, PatternBuffer dst)
603
+ public static void Bessel(Geometry geometry, Vector3 apex, Vector3 dir, Angle theta, Length wavelength, PhaseBuffer dst)
506
604
  {
507
605
  if (NativePattern.autd3_pattern_bessel(geometry.Handle, Coords.PointArray(apex), Coords.DirArray(dir), theta.Rad, wavelength.Mm, dst.Handle) != 0)
508
606
  {
509
- throw new Autd3Exception("bessel failed (buffer device count must match geometry)");
607
+ throw new Autd3Exception("bessel failed (buffer device count must match geometry)", Autd3ErrorCode.InvalidArgument);
510
608
  }
511
609
  }
512
610
 
513
- public static void BesselDevice(Device device, Vector3 apex, Vector3 dir, Angle theta, Length wavelength, Emission[] dst)
514
- {
515
- var native = ToNativeDst(dst);
516
- if (NativePattern.autd3_pattern_bessel_device(device.GeometryHandle, device.DeviceIndex,
517
- Coords.PointArray(apex),
518
- Coords.DirArray(dir), theta.Rad, wavelength.Mm, native) != 0)
519
- {
520
- throw new Autd3Exception("bessel_device failed");
521
- }
522
- FromNativeDst(native, dst);
523
- }
524
-
525
611
  public static Phase BesselTransducer(Vector3 position, Vector3 apex, Vector3 dir, Angle theta, Length wavelength)
526
612
  {
527
613
  if (NativePattern.autd3_pattern_bessel_transducer(
@@ -529,31 +615,20 @@ namespace AUTD3
529
615
  Coords.PointArray(apex),
530
616
  Coords.DirArray(dir), theta.Rad, wavelength.Mm, out var p) != 0)
531
617
  {
532
- throw new Autd3Exception("bessel_transducer failed");
618
+ throw new Autd3Exception("bessel_transducer failed", Autd3ErrorCode.InvalidArgument);
533
619
  }
534
620
  return new Phase(p);
535
621
  }
536
622
 
537
- public static void LaguerreGaussianPhase(Geometry geometry, Vector3 target, Vector3 axis, LaguerreGaussianOption option, Length wavelength, PatternBuffer dst)
623
+ public static void LaguerreGaussianPhase(Geometry geometry, Vector3 target, Vector3 axis, LaguerreGaussianOption option, Length wavelength, PhaseBuffer dst)
538
624
  {
539
625
  if (NativePattern.autd3_pattern_laguerre_gaussian_phase(geometry.Handle, Coords.PointArray(target), Coords.DirArray(axis),
540
626
  option.P, option.L, option.Waist.Mm, wavelength.Mm, dst.Handle) != 0)
541
627
  {
542
- throw new Autd3Exception("laguerre_gaussian_phase failed (waist must be positive and buffer device count must match geometry)");
628
+ throw new Autd3Exception("laguerre_gaussian_phase failed (waist must be a positive finite length and buffer device count must match geometry)", Autd3ErrorCode.InvalidArgument);
543
629
  }
544
630
  }
545
631
 
546
- public static void LaguerreGaussianPhaseDevice(Device device, Vector3 target, Vector3 axis, LaguerreGaussianOption option, Length wavelength, Emission[] dst)
547
- {
548
- var native = ToNativeDst(dst);
549
- if (NativePattern.autd3_pattern_laguerre_gaussian_phase_device(device.GeometryHandle, device.DeviceIndex,
550
- Coords.PointArray(target), Coords.DirArray(axis), option.P, option.L, option.Waist.Mm, wavelength.Mm, native) != 0)
551
- {
552
- throw new Autd3Exception("laguerre_gaussian_phase_device failed (waist must be positive)");
553
- }
554
- FromNativeDst(native, dst);
555
- }
556
-
557
632
  public static Phase LaguerreGaussianPhaseTransducer(Vector3 position, Vector3 target, Vector3 axis, LaguerreGaussianOption option, Length wavelength)
558
633
  {
559
634
  if (NativePattern.autd3_pattern_laguerre_gaussian_phase_transducer(
@@ -561,51 +636,29 @@ namespace AUTD3
561
636
  Coords.PointArray(target),
562
637
  Coords.DirArray(axis), option.P, option.L, option.Waist.Mm, wavelength.Mm, out var p) != 0)
563
638
  {
564
- throw new Autd3Exception("laguerre_gaussian_phase_transducer failed (waist must be positive)");
639
+ throw new Autd3Exception("laguerre_gaussian_phase_transducer failed (waist must be a positive finite length)", Autd3ErrorCode.InvalidArgument);
565
640
  }
566
641
  return new Phase(p);
567
642
  }
568
643
 
569
- public static void LaguerreGaussianIntensity(Geometry geometry, Vector3 target, Vector3 axis, LaguerreGaussianOption option, Length wavelength, PatternBuffer dst)
644
+ public static void LaguerreGaussianIntensity(Geometry geometry, Vector3 target, Vector3 axis, LaguerreGaussianOption option, Length wavelength, IntensityBuffer dst)
570
645
  {
571
646
  if (NativePattern.autd3_pattern_laguerre_gaussian_intensity(geometry.Handle, Coords.PointArray(target), Coords.DirArray(axis),
572
647
  option.P, option.L, option.Waist.Mm, wavelength.Mm, dst.Handle) != 0)
573
648
  {
574
- throw new Autd3Exception("laguerre_gaussian_intensity failed (waist must be positive and buffer device count must match geometry)");
649
+ throw new Autd3Exception("laguerre_gaussian_intensity failed (waist must be a positive finite length and buffer device count must match geometry)", Autd3ErrorCode.InvalidArgument);
575
650
  }
576
651
  }
577
652
 
578
- public static void LaguerreGaussianIntensityDevice(Device device, Vector3 target, Vector3 axis, LaguerreGaussianOption option, Length wavelength, Emission[] dst)
579
- {
580
- var native = ToNativeDst(dst);
581
- if (NativePattern.autd3_pattern_laguerre_gaussian_intensity_device(device.GeometryHandle, device.DeviceIndex,
582
- Coords.PointArray(target), Coords.DirArray(axis), option.P, option.L, option.Waist.Mm, wavelength.Mm, native) != 0)
583
- {
584
- throw new Autd3Exception("laguerre_gaussian_intensity_device failed (waist must be positive)");
585
- }
586
- FromNativeDst(native, dst);
587
- }
588
-
589
- public static void HermiteGaussianPhase(Geometry geometry, Vector3 target, Vector3 axis, Vector3 xDir, HermiteGaussianOption option, Length wavelength, PatternBuffer dst)
653
+ public static void HermiteGaussianPhase(Geometry geometry, Vector3 target, Vector3 axis, Vector3 xDir, HermiteGaussianOption option, Length wavelength, PhaseBuffer dst)
590
654
  {
591
655
  if (NativePattern.autd3_pattern_hermite_gaussian_phase(geometry.Handle, Coords.PointArray(target), Coords.DirArray(axis), Coords.DirArray(xDir),
592
656
  option.M, option.N, option.Waist.Mm, wavelength.Mm, dst.Handle) != 0)
593
657
  {
594
- throw new Autd3Exception("hermite_gaussian_phase failed (waist must be positive and buffer device count must match geometry)");
658
+ throw new Autd3Exception("hermite_gaussian_phase failed (waist must be a positive finite length and buffer device count must match geometry)", Autd3ErrorCode.InvalidArgument);
595
659
  }
596
660
  }
597
661
 
598
- public static void HermiteGaussianPhaseDevice(Device device, Vector3 target, Vector3 axis, Vector3 xDir, HermiteGaussianOption option, Length wavelength, Emission[] dst)
599
- {
600
- var native = ToNativeDst(dst);
601
- if (NativePattern.autd3_pattern_hermite_gaussian_phase_device(device.GeometryHandle, device.DeviceIndex,
602
- Coords.PointArray(target), Coords.DirArray(axis), Coords.DirArray(xDir), option.M, option.N, option.Waist.Mm, wavelength.Mm, native) != 0)
603
- {
604
- throw new Autd3Exception("hermite_gaussian_phase_device failed (waist must be positive)");
605
- }
606
- FromNativeDst(native, dst);
607
- }
608
-
609
662
  public static Phase HermiteGaussianPhaseTransducer(Vector3 position, Vector3 target, Vector3 axis, Vector3 xDir, HermiteGaussianOption option, Length wavelength)
610
663
  {
611
664
  if (NativePattern.autd3_pattern_hermite_gaussian_phase_transducer(
@@ -614,190 +667,17 @@ namespace AUTD3
614
667
  Coords.DirArray(axis),
615
668
  Coords.DirArray(xDir), option.M, option.N, option.Waist.Mm, wavelength.Mm, out var p) != 0)
616
669
  {
617
- throw new Autd3Exception("hermite_gaussian_phase_transducer failed (waist must be positive)");
670
+ throw new Autd3Exception("hermite_gaussian_phase_transducer failed (waist must be a positive finite length)", Autd3ErrorCode.InvalidArgument);
618
671
  }
619
672
  return new Phase(p);
620
673
  }
621
674
 
622
- public static void HermiteGaussianIntensity(Geometry geometry, Vector3 target, Vector3 axis, Vector3 xDir, HermiteGaussianOption option, Length wavelength, PatternBuffer dst)
675
+ public static void HermiteGaussianIntensity(Geometry geometry, Vector3 target, Vector3 axis, Vector3 xDir, HermiteGaussianOption option, Length wavelength, IntensityBuffer dst)
623
676
  {
624
677
  if (NativePattern.autd3_pattern_hermite_gaussian_intensity(geometry.Handle, Coords.PointArray(target), Coords.DirArray(axis), Coords.DirArray(xDir),
625
678
  option.M, option.N, option.Waist.Mm, wavelength.Mm, dst.Handle) != 0)
626
679
  {
627
- throw new Autd3Exception("hermite_gaussian_intensity failed (waist must be positive and buffer device count must match geometry)");
628
- }
629
- }
630
-
631
- public static void HermiteGaussianIntensityDevice(Device device, Vector3 target, Vector3 axis, Vector3 xDir, HermiteGaussianOption option, Length wavelength, Emission[] dst)
632
- {
633
- var native = ToNativeDst(dst);
634
- if (NativePattern.autd3_pattern_hermite_gaussian_intensity_device(device.GeometryHandle, device.DeviceIndex,
635
- Coords.PointArray(target), Coords.DirArray(axis), Coords.DirArray(xDir), option.M, option.N, option.Waist.Mm, wavelength.Mm, native) != 0)
636
- {
637
- throw new Autd3Exception("hermite_gaussian_intensity_device failed (waist must be positive)");
638
- }
639
- FromNativeDst(native, dst);
640
- }
641
-
642
- public static void SetIntensity(Intensity intensity, PatternBuffer dst)
643
- {
644
- if (NativePattern.autd3_pattern_set_intensity(intensity.Value, dst.Handle) != 0)
645
- {
646
- throw new Autd3Exception("set_intensity failed");
647
- }
648
- }
649
-
650
- public static void SetIntensityDevice(Intensity intensity, Emission[] dst)
651
- {
652
- var native = ToNativeDstAnyLength(dst);
653
- if (NativePattern.autd3_pattern_set_intensity_device(intensity.Value, native, (UIntPtr)native.Length) != 0)
654
- {
655
- throw new Autd3Exception("set_intensity_device failed");
656
- }
657
- FromNativeDst(native, dst);
658
- }
659
-
660
- public static void SetPhase(Phase phase, PatternBuffer dst)
661
- {
662
- if (NativePattern.autd3_pattern_set_phase(phase.Value, dst.Handle) != 0)
663
- {
664
- throw new Autd3Exception("set_phase failed");
665
- }
666
- }
667
-
668
- public static void SetPhaseDevice(Phase phase, Emission[] dst)
669
- {
670
- var native = ToNativeDstAnyLength(dst);
671
- if (NativePattern.autd3_pattern_set_phase_device(phase.Value, native, (UIntPtr)native.Length) != 0)
672
- {
673
- throw new Autd3Exception("set_phase_device failed");
674
- }
675
- FromNativeDst(native, dst);
676
- }
677
-
678
- public static void SetPhaseAndIntensity(Phase phase, Intensity intensity, PatternBuffer dst)
679
- {
680
- if (NativePattern.autd3_pattern_set_phase_and_intensity(phase.Value, intensity.Value, dst.Handle) != 0)
681
- {
682
- throw new Autd3Exception("set_phase_and_intensity failed");
683
- }
684
- }
685
-
686
- public static void SetPhaseAndIntensityDevice(Phase phase, Intensity intensity, Emission[] dst)
687
- {
688
- var native = ToNativeDstAnyLength(dst);
689
- if (NativePattern.autd3_pattern_set_phase_and_intensity_device(phase.Value, intensity.Value, native, (UIntPtr)native.Length) != 0)
690
- {
691
- throw new Autd3Exception("set_phase_and_intensity_device failed");
692
- }
693
- FromNativeDst(native, dst);
694
- }
695
-
696
- public static void AddPhase(Phase phase, PatternBuffer dst)
697
- {
698
- if (NativePattern.autd3_pattern_add_phase(phase.Value, dst.Handle) != 0)
699
- {
700
- throw new Autd3Exception("add_phase failed");
701
- }
702
- }
703
-
704
- public static void AddPhaseDevice(Phase phase, Emission[] dst)
705
- {
706
- var native = ToNativeDstAnyLength(dst);
707
- if (NativePattern.autd3_pattern_add_phase_device(phase.Value, native, (UIntPtr)native.Length) != 0)
708
- {
709
- throw new Autd3Exception("add_phase_device failed");
710
- }
711
- FromNativeDst(native, dst);
712
- }
713
-
714
- public static void Group<TKey>(Geometry geometry, TransducerGroups<TKey> groups, Func<TKey, PatternBuffer> source, PatternBuffer dst) where TKey : struct
715
- {
716
- if (groups.Indices.Length != geometry.NumTransducers)
717
- {
718
- throw new Autd3Exception("groups must be built from the same geometry");
719
- }
720
- var keys = groups.Keys;
721
- var handles = new SafeHandle[keys.Count];
722
- for (var i = 0; i < keys.Count; i++)
723
- {
724
- var buffer = source(keys[i]);
725
- if (buffer == null)
726
- {
727
- throw new Autd3Exception($"no source was given for the key {keys[i]}");
728
- }
729
- if (ReferenceEquals(buffer, dst))
730
- {
731
- throw new Autd3Exception("dst must not be one of the sources");
732
- }
733
- handles[i] = buffer.Handle;
734
- }
735
- using var lease = new HandleArray(handles);
736
- if (NativePattern.autd3_pattern_group(geometry.Handle, groups.Indices, lease.Pointers, (UIntPtr)keys.Count, dst.Handle) != 0)
737
- {
738
- throw new Autd3Exception("group failed (every buffer must match the geometry)");
739
- }
740
- }
741
-
742
- public static void GroupCompute<TKey>(Geometry geometry, TransducerGroups<TKey> groups, Action<TKey, TransducerMask, PatternBuffer> compute, PatternBuffer dst) where TKey : struct
743
- {
744
- if (compute == null)
745
- {
746
- throw new ArgumentNullException(nameof(compute));
747
- }
748
- if (groups.Indices.Length != geometry.NumTransducers)
749
- {
750
- throw new Autd3Exception("groups must be built from the same geometry");
751
- }
752
- if (NativePattern.autd3_pattern_group_null(geometry.Handle, groups.Indices, dst.Handle) != 0)
753
- {
754
- throw new Autd3Exception("group_compute failed (dst must match the geometry)");
755
- }
756
- using var scratch = geometry.PatternBuffer();
757
- var keys = groups.Keys;
758
- for (var i = 0; i < keys.Count; i++)
759
- {
760
- SetPhaseAndIntensity(Phase.Zero, Intensity.Max, scratch);
761
- compute(keys[i], groups.Mask(keys[i]), scratch);
762
- if (NativePattern.autd3_pattern_group_copy(geometry.Handle, groups.Indices, i, scratch.Handle, dst.Handle) != 0)
763
- {
764
- throw new Autd3Exception("group_compute failed (dst must match the geometry)");
765
- }
766
- }
767
- }
768
-
769
- private static EmissionNative[] ToNativeDst(Emission[] dst)
770
- {
771
- if (dst == null)
772
- {
773
- throw new ArgumentNullException(nameof(dst));
774
- }
775
- if (dst.Length != Autd3.NumTransducers)
776
- {
777
- throw new Autd3Exception($"dst requires {Autd3.NumTransducers} emissions");
778
- }
779
- return ToNativeDstAnyLength(dst);
780
- }
781
-
782
- private static EmissionNative[] ToNativeDstAnyLength(Emission[] dst)
783
- {
784
- if (dst == null)
785
- {
786
- throw new ArgumentNullException(nameof(dst));
787
- }
788
- var native = new EmissionNative[dst.Length];
789
- for (var i = 0; i < dst.Length; i++)
790
- {
791
- native[i] = new EmissionNative { Phase = dst[i].Phase.Value, Intensity = dst[i].Intensity.Value };
792
- }
793
- return native;
794
- }
795
-
796
- private static void FromNativeDst(EmissionNative[] native, Emission[] dst)
797
- {
798
- for (var i = 0; i < dst.Length; i++)
799
- {
800
- dst[i] = new Emission(new Phase(native[i].Phase), new Intensity(native[i].Intensity));
680
+ throw new Autd3Exception("hermite_gaussian_intensity failed (waist must be a positive finite length and buffer device count must match geometry)", Autd3ErrorCode.InvalidArgument);
801
681
  }
802
682
  }
803
683
  }