com.shinolab.autd3-sdk.pattern 0.9.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/Ops.cs +36 -33
- package/Pattern.cs +481 -220
- package/Plugins/linux-x64/libautd3_pattern.so +0 -0
- package/Plugins/osx-arm64/libautd3_pattern.dylib +0 -0
- package/Plugins/win-x64/autd3_pattern.dll +0 -0
- package/THIRD-PARTY-LICENSES.md +32 -181
- package/package.json +2 -2
package/Pattern.cs
CHANGED
|
@@ -47,24 +47,72 @@ 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
|
+
public void Validate(Geometry geometry)
|
|
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
|
+
}
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
public bool IsEnabled(int device, int transducer) => Mask is null || Mask[device][transducer];
|
|
78
|
+
|
|
79
|
+
public int NumEnabled(Geometry geometry)
|
|
80
|
+
{
|
|
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;
|
|
97
|
+
}
|
|
50
98
|
}
|
|
51
99
|
|
|
52
|
-
public sealed class TransducerGroups<TKey> where TKey :
|
|
100
|
+
public sealed class TransducerGroups<TKey> where TKey : notnull
|
|
53
101
|
{
|
|
54
102
|
private readonly int[] _numTransducers;
|
|
55
103
|
private readonly List<TKey> _keys = new List<TKey>();
|
|
56
104
|
private readonly Dictionary<TKey, int> _lookup = new Dictionary<TKey, int>();
|
|
57
105
|
|
|
58
|
-
internal int[]
|
|
106
|
+
internal int[] FlatIndices { get; }
|
|
59
107
|
|
|
60
108
|
public IReadOnlyList<TKey> Keys => _keys;
|
|
61
109
|
|
|
62
110
|
public int NumDevices => _numTransducers.Length;
|
|
63
111
|
|
|
64
|
-
public TransducerGroups(Geometry geometry, Func<Device, int, TKey
|
|
112
|
+
public TransducerGroups(Geometry geometry, Func<Device, int, TKey> key)
|
|
65
113
|
{
|
|
66
114
|
_numTransducers = new int[geometry.NumDevices];
|
|
67
|
-
|
|
115
|
+
FlatIndices = new int[geometry.NumTransducers];
|
|
68
116
|
var k = 0;
|
|
69
117
|
var dev = 0;
|
|
70
118
|
foreach (var device in geometry)
|
|
@@ -74,23 +122,18 @@ namespace AUTD3
|
|
|
74
122
|
for (var tr = 0; tr < numTransducers; tr++)
|
|
75
123
|
{
|
|
76
124
|
var value = key(device, tr);
|
|
77
|
-
if (value
|
|
78
|
-
{
|
|
79
|
-
Indices[k++] = -1;
|
|
80
|
-
continue;
|
|
81
|
-
}
|
|
82
|
-
if (!_lookup.TryGetValue(value.Value, out var index))
|
|
125
|
+
if (!_lookup.TryGetValue(value, out var index))
|
|
83
126
|
{
|
|
84
127
|
index = _keys.Count;
|
|
85
|
-
_lookup.Add(value
|
|
86
|
-
_keys.Add(value
|
|
128
|
+
_lookup.Add(value, index);
|
|
129
|
+
_keys.Add(value);
|
|
87
130
|
}
|
|
88
|
-
|
|
131
|
+
FlatIndices[k++] = index;
|
|
89
132
|
}
|
|
90
133
|
}
|
|
91
134
|
}
|
|
92
135
|
|
|
93
|
-
public TKey
|
|
136
|
+
public TKey Key(int device, int transducer)
|
|
94
137
|
{
|
|
95
138
|
if (device < 0 || device >= _numTransducers.Length)
|
|
96
139
|
{
|
|
@@ -100,21 +143,70 @@ namespace AUTD3
|
|
|
100
143
|
{
|
|
101
144
|
throw new ArgumentOutOfRangeException(nameof(transducer));
|
|
102
145
|
}
|
|
103
|
-
|
|
104
|
-
|
|
146
|
+
return _keys[FlatIndices[Offset(device) + transducer]];
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
public int Index(int device, int transducer)
|
|
150
|
+
{
|
|
151
|
+
if (transducer < 0 || transducer >= NumTransducers(device))
|
|
105
152
|
{
|
|
106
|
-
|
|
153
|
+
throw new ArgumentOutOfRangeException(nameof(transducer));
|
|
154
|
+
}
|
|
155
|
+
return FlatIndices[Offset(device) + transducer];
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
public IReadOnlyList<int> Indices(int device) =>
|
|
159
|
+
new ArraySegment<int>(FlatIndices, Offset(device), NumTransducers(device));
|
|
160
|
+
|
|
161
|
+
public int NumTransducers(int device)
|
|
162
|
+
{
|
|
163
|
+
if (device < 0 || device >= _numTransducers.Length)
|
|
164
|
+
{
|
|
165
|
+
throw new ArgumentOutOfRangeException(nameof(device));
|
|
107
166
|
}
|
|
108
|
-
|
|
109
|
-
return index < 0 ? (TKey?)null : _keys[index];
|
|
167
|
+
return _numTransducers[device];
|
|
110
168
|
}
|
|
111
169
|
|
|
112
|
-
public
|
|
170
|
+
public int NumTransducersIn(TKey key)
|
|
113
171
|
{
|
|
114
172
|
if (!_lookup.TryGetValue(key, out var index))
|
|
115
173
|
{
|
|
116
|
-
|
|
174
|
+
return 0;
|
|
175
|
+
}
|
|
176
|
+
var count = 0;
|
|
177
|
+
foreach (var i in FlatIndices)
|
|
178
|
+
{
|
|
179
|
+
if (i == index)
|
|
180
|
+
{
|
|
181
|
+
count++;
|
|
182
|
+
}
|
|
117
183
|
}
|
|
184
|
+
return count;
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
public IEnumerable<(TKey Key, TransducerMask Mask)> Masks()
|
|
188
|
+
{
|
|
189
|
+
foreach (var key in _keys)
|
|
190
|
+
{
|
|
191
|
+
yield return (key, MaskOf(_lookup[key]));
|
|
192
|
+
}
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
private int Offset(int device)
|
|
196
|
+
{
|
|
197
|
+
var offset = 0;
|
|
198
|
+
for (var dev = 0; dev < device; dev++)
|
|
199
|
+
{
|
|
200
|
+
offset += _numTransducers[dev];
|
|
201
|
+
}
|
|
202
|
+
return offset;
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
public TransducerMask? Mask(TKey key) =>
|
|
206
|
+
_lookup.TryGetValue(key, out var index) ? MaskOf(index) : null;
|
|
207
|
+
|
|
208
|
+
internal TransducerMask MaskOf(int index)
|
|
209
|
+
{
|
|
118
210
|
var mask = new bool[_numTransducers.Length][];
|
|
119
211
|
var k = 0;
|
|
120
212
|
for (var dev = 0; dev < mask.Length; dev++)
|
|
@@ -122,20 +214,13 @@ namespace AUTD3
|
|
|
122
214
|
mask[dev] = new bool[_numTransducers[dev]];
|
|
123
215
|
for (var tr = 0; tr < mask[dev].Length; tr++)
|
|
124
216
|
{
|
|
125
|
-
mask[dev][tr] =
|
|
217
|
+
mask[dev][tr] = FlatIndices[k++] == index;
|
|
126
218
|
}
|
|
127
219
|
}
|
|
128
220
|
return TransducerMask.Masked(mask);
|
|
129
221
|
}
|
|
130
222
|
}
|
|
131
223
|
|
|
132
|
-
[StructLayout(LayoutKind.Sequential)]
|
|
133
|
-
internal struct EmissionNative
|
|
134
|
-
{
|
|
135
|
-
public byte Phase;
|
|
136
|
-
public byte Intensity;
|
|
137
|
-
}
|
|
138
|
-
|
|
139
224
|
internal static class NativePattern
|
|
140
225
|
{
|
|
141
226
|
private const string Lib = "autd3_pattern";
|
|
@@ -158,125 +243,140 @@ namespace AUTD3
|
|
|
158
243
|
internal static extern float autd3_pattern_wavelength(float soundSpeedMmPerS);
|
|
159
244
|
|
|
160
245
|
[DllImport(Lib, CallingConvention = CallingConvention.Cdecl)]
|
|
161
|
-
internal static extern IntPtr
|
|
246
|
+
internal static extern IntPtr autd3_core_geometry_phase_buffer(GeometryHandle geometry);
|
|
162
247
|
|
|
163
248
|
[DllImport(Lib, CallingConvention = CallingConvention.Cdecl)]
|
|
164
|
-
internal static extern IntPtr
|
|
249
|
+
internal static extern IntPtr autd3_phase_buffer_from_array(byte[] src, UIntPtr numDevices);
|
|
165
250
|
|
|
166
251
|
[DllImport(Lib, CallingConvention = CallingConvention.Cdecl)]
|
|
167
|
-
internal static extern UIntPtr
|
|
252
|
+
internal static extern UIntPtr autd3_phase_buffer_num_devices(PhaseBufferHandle buffer);
|
|
168
253
|
|
|
169
254
|
[DllImport(Lib, CallingConvention = CallingConvention.Cdecl)]
|
|
170
|
-
internal static extern UIntPtr
|
|
255
|
+
internal static extern UIntPtr autd3_phase_buffer_num_transducers(PhaseBufferHandle buffer, UIntPtr dev);
|
|
171
256
|
|
|
172
257
|
[DllImport(Lib, CallingConvention = CallingConvention.Cdecl)]
|
|
173
|
-
internal static extern int
|
|
258
|
+
internal static extern int autd3_phase_buffer_get(PhaseBufferHandle buffer, UIntPtr dev, UIntPtr tr, out byte @out);
|
|
174
259
|
|
|
175
260
|
[DllImport(Lib, CallingConvention = CallingConvention.Cdecl)]
|
|
176
|
-
internal static extern int
|
|
261
|
+
internal static extern int autd3_phase_buffer_set(PhaseBufferHandle buffer, UIntPtr dev, UIntPtr tr, byte value);
|
|
177
262
|
|
|
178
263
|
[DllImport(Lib, CallingConvention = CallingConvention.Cdecl)]
|
|
179
|
-
internal static extern
|
|
264
|
+
internal static extern int autd3_phase_buffer_copy_to(PhaseBufferHandle buffer, [Out] byte[] dst, UIntPtr len);
|
|
180
265
|
|
|
181
266
|
[DllImport(Lib, CallingConvention = CallingConvention.Cdecl)]
|
|
182
|
-
internal static extern
|
|
267
|
+
internal static extern void autd3_phase_buffer_free(IntPtr buffer);
|
|
183
268
|
|
|
184
269
|
[DllImport(Lib, CallingConvention = CallingConvention.Cdecl)]
|
|
185
|
-
internal static extern
|
|
270
|
+
internal static extern IntPtr autd3_core_geometry_intensity_buffer(GeometryHandle geometry);
|
|
186
271
|
|
|
187
272
|
[DllImport(Lib, CallingConvention = CallingConvention.Cdecl)]
|
|
188
|
-
internal static extern
|
|
273
|
+
internal static extern IntPtr autd3_intensity_buffer_from_array(byte[] src, UIntPtr numDevices);
|
|
189
274
|
|
|
190
275
|
[DllImport(Lib, CallingConvention = CallingConvention.Cdecl)]
|
|
191
|
-
internal static extern
|
|
276
|
+
internal static extern UIntPtr autd3_intensity_buffer_num_devices(IntensityBufferHandle buffer);
|
|
192
277
|
|
|
193
278
|
[DllImport(Lib, CallingConvention = CallingConvention.Cdecl)]
|
|
194
|
-
internal static extern
|
|
279
|
+
internal static extern UIntPtr autd3_intensity_buffer_num_transducers(IntensityBufferHandle buffer, UIntPtr dev);
|
|
195
280
|
|
|
196
281
|
[DllImport(Lib, CallingConvention = CallingConvention.Cdecl)]
|
|
197
|
-
internal static extern int
|
|
282
|
+
internal static extern int autd3_intensity_buffer_get(IntensityBufferHandle buffer, UIntPtr dev, UIntPtr tr, out byte @out);
|
|
198
283
|
|
|
199
284
|
[DllImport(Lib, CallingConvention = CallingConvention.Cdecl)]
|
|
200
|
-
internal static extern int
|
|
285
|
+
internal static extern int autd3_intensity_buffer_set(IntensityBufferHandle buffer, UIntPtr dev, UIntPtr tr, byte value);
|
|
201
286
|
|
|
202
287
|
[DllImport(Lib, CallingConvention = CallingConvention.Cdecl)]
|
|
203
|
-
internal static extern int
|
|
288
|
+
internal static extern int autd3_intensity_buffer_copy_to(IntensityBufferHandle buffer, [Out] byte[] dst, UIntPtr len);
|
|
204
289
|
|
|
205
290
|
[DllImport(Lib, CallingConvention = CallingConvention.Cdecl)]
|
|
206
|
-
internal static extern
|
|
291
|
+
internal static extern void autd3_intensity_buffer_free(IntPtr buffer);
|
|
207
292
|
|
|
208
293
|
[DllImport(Lib, CallingConvention = CallingConvention.Cdecl)]
|
|
209
|
-
internal static extern int
|
|
294
|
+
internal static extern int autd3_pattern_focus(GeometryHandle geometry, float[] target, float wavelengthMm, PhaseBufferHandle buffer);
|
|
210
295
|
|
|
211
296
|
[DllImport(Lib, CallingConvention = CallingConvention.Cdecl)]
|
|
212
|
-
internal static extern int
|
|
297
|
+
internal static extern int autd3_pattern_focus_device(GeometryHandle geometry, UIntPtr dev, float[] target, float wavelengthMm, [In, Out] byte[] dst);
|
|
213
298
|
|
|
214
299
|
[DllImport(Lib, CallingConvention = CallingConvention.Cdecl)]
|
|
215
|
-
internal static extern int
|
|
300
|
+
internal static extern int autd3_pattern_focus_transducer(float[] position, float[] target, float wavelengthMm, out byte @out);
|
|
216
301
|
|
|
217
302
|
[DllImport(Lib, CallingConvention = CallingConvention.Cdecl)]
|
|
218
|
-
internal static extern int
|
|
303
|
+
internal static extern int autd3_pattern_plane(GeometryHandle geometry, float[] dir, float wavelengthMm, PhaseBufferHandle buffer);
|
|
219
304
|
|
|
220
305
|
[DllImport(Lib, CallingConvention = CallingConvention.Cdecl)]
|
|
221
|
-
internal static extern int
|
|
306
|
+
internal static extern int autd3_pattern_plane_device(GeometryHandle geometry, UIntPtr dev, float[] dir, float wavelengthMm, [In, Out] byte[] dst);
|
|
222
307
|
|
|
223
308
|
[DllImport(Lib, CallingConvention = CallingConvention.Cdecl)]
|
|
224
|
-
internal static extern int
|
|
309
|
+
internal static extern int autd3_pattern_plane_transducer(float[] position, float[] dir, float wavelengthMm, out byte @out);
|
|
225
310
|
|
|
226
311
|
[DllImport(Lib, CallingConvention = CallingConvention.Cdecl)]
|
|
227
|
-
internal static extern int
|
|
312
|
+
internal static extern int autd3_pattern_bessel(GeometryHandle geometry, float[] apex, float[] dir, float thetaRad, float wavelengthMm, PhaseBufferHandle buffer);
|
|
228
313
|
|
|
229
314
|
[DllImport(Lib, CallingConvention = CallingConvention.Cdecl)]
|
|
230
|
-
internal static extern int
|
|
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);
|
|
231
316
|
|
|
232
317
|
[DllImport(Lib, CallingConvention = CallingConvention.Cdecl)]
|
|
233
|
-
internal static extern int
|
|
318
|
+
internal static extern int autd3_pattern_bessel_transducer(float[] position, float[] apex, float[] dir, float thetaRad, float wavelengthMm, out byte @out);
|
|
234
319
|
|
|
235
320
|
[DllImport(Lib, CallingConvention = CallingConvention.Cdecl)]
|
|
236
|
-
internal static extern int
|
|
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);
|
|
237
322
|
|
|
238
323
|
[DllImport(Lib, CallingConvention = CallingConvention.Cdecl)]
|
|
239
|
-
internal static extern int
|
|
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);
|
|
240
325
|
|
|
241
326
|
[DllImport(Lib, CallingConvention = CallingConvention.Cdecl)]
|
|
242
|
-
internal static extern int
|
|
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);
|
|
243
328
|
|
|
244
329
|
[DllImport(Lib, CallingConvention = CallingConvention.Cdecl)]
|
|
245
|
-
internal static extern int
|
|
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);
|
|
246
331
|
|
|
247
332
|
[DllImport(Lib, CallingConvention = CallingConvention.Cdecl)]
|
|
248
|
-
internal static extern int
|
|
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);
|
|
249
334
|
|
|
250
335
|
[DllImport(Lib, CallingConvention = CallingConvention.Cdecl)]
|
|
251
|
-
internal static extern int
|
|
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);
|
|
252
337
|
|
|
253
338
|
[DllImport(Lib, CallingConvention = CallingConvention.Cdecl)]
|
|
254
|
-
internal static extern int
|
|
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);
|
|
255
340
|
|
|
256
341
|
[DllImport(Lib, CallingConvention = CallingConvention.Cdecl)]
|
|
257
|
-
internal static extern int
|
|
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);
|
|
258
343
|
|
|
259
344
|
[DllImport(Lib, CallingConvention = CallingConvention.Cdecl)]
|
|
260
|
-
internal static extern int
|
|
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);
|
|
261
346
|
|
|
262
347
|
[DllImport(Lib, CallingConvention = CallingConvention.Cdecl)]
|
|
263
|
-
internal static extern int
|
|
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);
|
|
264
349
|
|
|
265
350
|
[DllImport(Lib, CallingConvention = CallingConvention.Cdecl)]
|
|
266
|
-
internal static extern int
|
|
351
|
+
internal static extern int autd3_pattern_set_intensity(byte intensity, IntensityBufferHandle buffer);
|
|
267
352
|
|
|
268
353
|
[DllImport(Lib, CallingConvention = CallingConvention.Cdecl)]
|
|
269
|
-
internal static extern int
|
|
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);
|
|
270
370
|
|
|
271
371
|
|
|
272
372
|
[DllImport(ClientLib, CallingConvention = CallingConvention.Cdecl)]
|
|
273
|
-
internal static extern IntPtr autd3_op_pattern(byte bank,
|
|
373
|
+
internal static extern IntPtr autd3_op_pattern(byte bank, PhaseBufferHandle phases, IntPtr intensities, byte uniformIntensity, byte transitionMode, ulong transitionValue);
|
|
274
374
|
|
|
275
375
|
[DllImport(ClientLib, CallingConvention = CallingConvention.Cdecl)]
|
|
276
|
-
internal static extern IntPtr autd3_op_write_pattern_buffer(byte bank, ushort index,
|
|
376
|
+
internal static extern IntPtr autd3_op_write_pattern_buffer(byte bank, ushort index, PhaseBufferHandle phases, IntPtr intensities, byte uniformIntensity);
|
|
277
377
|
|
|
278
378
|
[DllImport(ClientLib, CallingConvention = CallingConvention.Cdecl)]
|
|
279
|
-
internal static extern IntPtr
|
|
379
|
+
internal static extern IntPtr autd3_op_write_pattern_phase(byte bank, ushort index, byte depth, byte intensity, IntPtr[] patterns, UIntPtr numPatterns);
|
|
280
380
|
|
|
281
381
|
[DllImport(ClientLib, CallingConvention = CallingConvention.Cdecl)]
|
|
282
382
|
internal static extern IntPtr autd3_op_config_pattern(byte bank, IntPtr samplingConfig, uint size, ushort rep);
|
|
@@ -285,55 +385,67 @@ namespace AUTD3
|
|
|
285
385
|
internal static extern IntPtr autd3_op_config_foci_stm(byte bank, IntPtr samplingConfig, uint size, byte numFoci, float soundSpeedMPerS, ushort rep);
|
|
286
386
|
|
|
287
387
|
[DllImport(ClientLib, CallingConvention = CallingConvention.Cdecl)]
|
|
288
|
-
internal static extern IntPtr
|
|
388
|
+
internal static extern IntPtr autd3_op_activate_pattern_bank(byte bank, byte transitionMode, ulong transitionValue);
|
|
289
389
|
|
|
290
390
|
[DllImport(ClientLib, CallingConvention = CallingConvention.Cdecl)]
|
|
291
|
-
internal static extern int
|
|
391
|
+
internal static extern int autd3_phase_depth_max_count(byte depth, out UIntPtr @out);
|
|
292
392
|
}
|
|
293
393
|
|
|
294
|
-
internal sealed class
|
|
394
|
+
internal sealed class PhaseBufferHandle : Autd3SafeHandle
|
|
295
395
|
{
|
|
296
|
-
internal
|
|
396
|
+
internal PhaseBufferHandle(IntPtr handle) : base(handle)
|
|
297
397
|
{
|
|
298
398
|
}
|
|
299
399
|
|
|
300
400
|
protected override bool ReleaseHandle()
|
|
301
401
|
{
|
|
302
|
-
NativePattern.
|
|
402
|
+
NativePattern.autd3_phase_buffer_free(handle);
|
|
303
403
|
return true;
|
|
304
404
|
}
|
|
305
405
|
}
|
|
306
406
|
|
|
307
|
-
|
|
407
|
+
internal sealed class IntensityBufferHandle : Autd3SafeHandle
|
|
308
408
|
{
|
|
309
|
-
|
|
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;
|
|
310
423
|
private readonly UIntPtr _dev;
|
|
311
424
|
|
|
312
|
-
internal
|
|
425
|
+
internal DevicePhases(PhaseBufferHandle buffer, UIntPtr dev)
|
|
313
426
|
{
|
|
314
427
|
_buffer = buffer;
|
|
315
428
|
_dev = dev;
|
|
316
429
|
}
|
|
317
430
|
|
|
318
|
-
public int NumTransducers => (int)NativePattern.
|
|
431
|
+
public int NumTransducers => (int)NativePattern.autd3_phase_buffer_num_transducers(_buffer, _dev);
|
|
319
432
|
|
|
320
|
-
public
|
|
433
|
+
public Phase this[int tr]
|
|
321
434
|
{
|
|
322
435
|
get
|
|
323
436
|
{
|
|
324
|
-
if (NativePattern.
|
|
437
|
+
if (NativePattern.autd3_phase_buffer_get(_buffer, _dev, (UIntPtr)tr, out var v) != 0)
|
|
325
438
|
throw new ArgumentOutOfRangeException(nameof(tr));
|
|
326
|
-
return new
|
|
439
|
+
return new Phase(v);
|
|
327
440
|
}
|
|
328
441
|
set
|
|
329
442
|
{
|
|
330
|
-
|
|
331
|
-
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)
|
|
332
444
|
throw new ArgumentOutOfRangeException(nameof(tr));
|
|
333
445
|
}
|
|
334
446
|
}
|
|
335
447
|
|
|
336
|
-
public IEnumerator<
|
|
448
|
+
public IEnumerator<Phase> GetEnumerator()
|
|
337
449
|
{
|
|
338
450
|
var count = NumTransducers;
|
|
339
451
|
for (var i = 0; i < count; i++)
|
|
@@ -345,58 +457,177 @@ namespace AUTD3
|
|
|
345
457
|
IEnumerator IEnumerable.GetEnumerator() => GetEnumerator();
|
|
346
458
|
}
|
|
347
459
|
|
|
348
|
-
public
|
|
460
|
+
public readonly struct DeviceIntensities : IEnumerable<Intensity>
|
|
349
461
|
{
|
|
350
|
-
|
|
462
|
+
private readonly IntensityBufferHandle _buffer;
|
|
463
|
+
private readonly UIntPtr _dev;
|
|
351
464
|
|
|
352
|
-
|
|
465
|
+
internal DeviceIntensities(IntensityBufferHandle buffer, UIntPtr dev)
|
|
466
|
+
{
|
|
467
|
+
_buffer = buffer;
|
|
468
|
+
_dev = dev;
|
|
469
|
+
}
|
|
353
470
|
|
|
354
|
-
|
|
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
|
+
}
|
|
355
487
|
|
|
356
|
-
|
|
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
|
+
}
|
|
496
|
+
|
|
497
|
+
IEnumerator IEnumerable.GetEnumerator() => GetEnumerator();
|
|
498
|
+
}
|
|
499
|
+
|
|
500
|
+
public sealed class PhaseBuffer : IDisposable, IEnumerable<DevicePhases>
|
|
501
|
+
{
|
|
502
|
+
private readonly PhaseBufferHandle _handle;
|
|
503
|
+
|
|
504
|
+
internal PhaseBufferHandle Handle => _handle;
|
|
505
|
+
|
|
506
|
+
internal PhaseBuffer(IntPtr handle)
|
|
357
507
|
{
|
|
358
508
|
if (handle == IntPtr.Zero)
|
|
359
509
|
{
|
|
360
|
-
throw new Autd3Exception("failed to create
|
|
510
|
+
throw new Autd3Exception("failed to create phase buffer");
|
|
361
511
|
}
|
|
362
|
-
_handle = new
|
|
512
|
+
_handle = new PhaseBufferHandle(handle);
|
|
513
|
+
}
|
|
514
|
+
|
|
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));
|
|
363
519
|
}
|
|
364
520
|
|
|
365
|
-
public
|
|
521
|
+
public int NumDevices => (int)NativePattern.autd3_phase_buffer_num_devices(Handle);
|
|
522
|
+
|
|
523
|
+
public Phase[][] ToArray()
|
|
366
524
|
{
|
|
367
|
-
var
|
|
368
|
-
var
|
|
369
|
-
for (var
|
|
525
|
+
var result = new Phase[NumDevices][];
|
|
526
|
+
var total = 0;
|
|
527
|
+
for (var dev = 0; dev < result.Length; dev++)
|
|
528
|
+
{
|
|
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)
|
|
370
539
|
{
|
|
371
|
-
|
|
540
|
+
for (var tr = 0; tr < device.Length; tr++)
|
|
372
541
|
{
|
|
373
|
-
|
|
542
|
+
device[tr] = new Phase(flat[k++]);
|
|
374
543
|
}
|
|
375
|
-
|
|
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++)
|
|
376
613
|
{
|
|
377
|
-
|
|
378
|
-
{
|
|
379
|
-
Phase = emissions[d][t].Phase.Value,
|
|
380
|
-
Intensity = emissions[d][t].Intensity.Value,
|
|
381
|
-
};
|
|
614
|
+
device[tr] = new Intensity(flat[k++]);
|
|
382
615
|
}
|
|
383
616
|
}
|
|
384
|
-
return
|
|
617
|
+
return result;
|
|
385
618
|
}
|
|
386
619
|
|
|
387
|
-
public int
|
|
388
|
-
|
|
389
|
-
public DevicePattern this[int dev]
|
|
620
|
+
public DeviceIntensities this[int dev]
|
|
390
621
|
{
|
|
391
622
|
get
|
|
392
623
|
{
|
|
393
624
|
if (dev < 0 || dev >= NumDevices)
|
|
394
625
|
throw new ArgumentOutOfRangeException(nameof(dev));
|
|
395
|
-
return new
|
|
626
|
+
return new DeviceIntensities(Handle, (UIntPtr)dev);
|
|
396
627
|
}
|
|
397
628
|
}
|
|
398
629
|
|
|
399
|
-
public IEnumerator<
|
|
630
|
+
public IEnumerator<DeviceIntensities> GetEnumerator()
|
|
400
631
|
{
|
|
401
632
|
var count = NumDevices;
|
|
402
633
|
for (var i = 0; i < count; i++)
|
|
@@ -410,39 +641,97 @@ namespace AUTD3
|
|
|
410
641
|
public void Dispose() => _handle.Dispose();
|
|
411
642
|
}
|
|
412
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
|
+
|
|
413
668
|
public static class GeometryPatternBufferExtensions
|
|
414
669
|
{
|
|
415
|
-
public static
|
|
416
|
-
new
|
|
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);
|
|
417
701
|
}
|
|
418
702
|
|
|
419
703
|
public sealed class Pattern : ICommand
|
|
420
704
|
{
|
|
421
705
|
private readonly PatternBank _bank;
|
|
422
|
-
private readonly
|
|
706
|
+
private readonly PhaseBuffer _phases;
|
|
707
|
+
private readonly PatternIntensity _intensities;
|
|
423
708
|
private readonly TransitionMode _transitionMode;
|
|
424
709
|
|
|
425
|
-
public Pattern(
|
|
426
|
-
: this(PatternBank.B0,
|
|
710
|
+
public Pattern(PhaseBuffer phases, PatternIntensity intensities, TransitionMode? transitionMode = null)
|
|
711
|
+
: this(PatternBank.B0, phases, intensities, transitionMode)
|
|
427
712
|
{
|
|
428
713
|
}
|
|
429
714
|
|
|
430
|
-
public Pattern(PatternBank bank,
|
|
715
|
+
public Pattern(PatternBank bank, PhaseBuffer phases, PatternIntensity intensities, TransitionMode? transitionMode = null)
|
|
431
716
|
{
|
|
432
717
|
_bank = bank;
|
|
433
|
-
|
|
718
|
+
_phases = phases;
|
|
719
|
+
_intensities = intensities;
|
|
434
720
|
_transitionMode = transitionMode ?? TransitionMode.Immediate;
|
|
435
721
|
}
|
|
436
722
|
|
|
437
|
-
IntPtr ICommand.CreateOp()
|
|
438
|
-
|
|
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
|
+
}
|
|
439
728
|
|
|
440
729
|
|
|
441
730
|
public static Length Wavelength(Velocity soundSpeed) =>
|
|
442
731
|
new Length(NativePattern.autd3_pattern_wavelength(soundSpeed.MmS));
|
|
443
732
|
|
|
444
733
|
|
|
445
|
-
public static void Focus(Geometry geometry, Vector3 target, Length wavelength,
|
|
734
|
+
public static void Focus(Geometry geometry, Vector3 target, Length wavelength, PhaseBuffer dst)
|
|
446
735
|
{
|
|
447
736
|
if (NativePattern.autd3_pattern_focus(geometry.Handle, Coords.PointArray(target), wavelength.Mm, dst.Handle) != 0)
|
|
448
737
|
{
|
|
@@ -450,15 +739,15 @@ namespace AUTD3
|
|
|
450
739
|
}
|
|
451
740
|
}
|
|
452
741
|
|
|
453
|
-
public static void FocusDevice(Device device, Vector3 target, Length wavelength,
|
|
742
|
+
public static void FocusDevice(Device device, Vector3 target, Length wavelength, Phase[] dst)
|
|
454
743
|
{
|
|
455
|
-
var native =
|
|
744
|
+
var native = ToNative(dst);
|
|
456
745
|
if (NativePattern.autd3_pattern_focus_device(device.GeometryHandle, device.DeviceIndex,
|
|
457
746
|
Coords.PointArray(target), wavelength.Mm, native) != 0)
|
|
458
747
|
{
|
|
459
748
|
throw new Autd3Exception("focus_device failed");
|
|
460
749
|
}
|
|
461
|
-
|
|
750
|
+
FromNative(native, dst);
|
|
462
751
|
}
|
|
463
752
|
|
|
464
753
|
public static Phase FocusTransducer(Vector3 position, Vector3 target, Length wavelength)
|
|
@@ -472,7 +761,7 @@ namespace AUTD3
|
|
|
472
761
|
return new Phase(p);
|
|
473
762
|
}
|
|
474
763
|
|
|
475
|
-
public static void Plane(Geometry geometry, Vector3 dir, Length wavelength,
|
|
764
|
+
public static void Plane(Geometry geometry, Vector3 dir, Length wavelength, PhaseBuffer dst)
|
|
476
765
|
{
|
|
477
766
|
if (NativePattern.autd3_pattern_plane(geometry.Handle, Coords.DirArray(dir), wavelength.Mm, dst.Handle) != 0)
|
|
478
767
|
{
|
|
@@ -480,15 +769,15 @@ namespace AUTD3
|
|
|
480
769
|
}
|
|
481
770
|
}
|
|
482
771
|
|
|
483
|
-
public static void PlaneDevice(Device device, Vector3 dir, Length wavelength,
|
|
772
|
+
public static void PlaneDevice(Device device, Vector3 dir, Length wavelength, Phase[] dst)
|
|
484
773
|
{
|
|
485
|
-
var native =
|
|
774
|
+
var native = ToNative(dst);
|
|
486
775
|
if (NativePattern.autd3_pattern_plane_device(device.GeometryHandle, device.DeviceIndex,
|
|
487
776
|
Coords.DirArray(dir), wavelength.Mm, native) != 0)
|
|
488
777
|
{
|
|
489
778
|
throw new Autd3Exception("plane_device failed");
|
|
490
779
|
}
|
|
491
|
-
|
|
780
|
+
FromNative(native, dst);
|
|
492
781
|
}
|
|
493
782
|
|
|
494
783
|
public static Phase PlaneTransducer(Vector3 position, Vector3 dir, Length wavelength)
|
|
@@ -502,7 +791,7 @@ namespace AUTD3
|
|
|
502
791
|
return new Phase(p);
|
|
503
792
|
}
|
|
504
793
|
|
|
505
|
-
public static void Bessel(Geometry geometry, Vector3 apex, Vector3 dir, Angle theta, Length wavelength,
|
|
794
|
+
public static void Bessel(Geometry geometry, Vector3 apex, Vector3 dir, Angle theta, Length wavelength, PhaseBuffer dst)
|
|
506
795
|
{
|
|
507
796
|
if (NativePattern.autd3_pattern_bessel(geometry.Handle, Coords.PointArray(apex), Coords.DirArray(dir), theta.Rad, wavelength.Mm, dst.Handle) != 0)
|
|
508
797
|
{
|
|
@@ -510,16 +799,16 @@ namespace AUTD3
|
|
|
510
799
|
}
|
|
511
800
|
}
|
|
512
801
|
|
|
513
|
-
public static void BesselDevice(Device device, Vector3 apex, Vector3 dir, Angle theta, Length wavelength,
|
|
802
|
+
public static void BesselDevice(Device device, Vector3 apex, Vector3 dir, Angle theta, Length wavelength, Phase[] dst)
|
|
514
803
|
{
|
|
515
|
-
var native =
|
|
804
|
+
var native = ToNative(dst);
|
|
516
805
|
if (NativePattern.autd3_pattern_bessel_device(device.GeometryHandle, device.DeviceIndex,
|
|
517
806
|
Coords.PointArray(apex),
|
|
518
807
|
Coords.DirArray(dir), theta.Rad, wavelength.Mm, native) != 0)
|
|
519
808
|
{
|
|
520
809
|
throw new Autd3Exception("bessel_device failed");
|
|
521
810
|
}
|
|
522
|
-
|
|
811
|
+
FromNative(native, dst);
|
|
523
812
|
}
|
|
524
813
|
|
|
525
814
|
public static Phase BesselTransducer(Vector3 position, Vector3 apex, Vector3 dir, Angle theta, Length wavelength)
|
|
@@ -534,7 +823,7 @@ namespace AUTD3
|
|
|
534
823
|
return new Phase(p);
|
|
535
824
|
}
|
|
536
825
|
|
|
537
|
-
public static void LaguerreGaussianPhase(Geometry geometry, Vector3 target, Vector3 axis, LaguerreGaussianOption option, Length wavelength,
|
|
826
|
+
public static void LaguerreGaussianPhase(Geometry geometry, Vector3 target, Vector3 axis, LaguerreGaussianOption option, Length wavelength, PhaseBuffer dst)
|
|
538
827
|
{
|
|
539
828
|
if (NativePattern.autd3_pattern_laguerre_gaussian_phase(geometry.Handle, Coords.PointArray(target), Coords.DirArray(axis),
|
|
540
829
|
option.P, option.L, option.Waist.Mm, wavelength.Mm, dst.Handle) != 0)
|
|
@@ -543,15 +832,15 @@ namespace AUTD3
|
|
|
543
832
|
}
|
|
544
833
|
}
|
|
545
834
|
|
|
546
|
-
public static void LaguerreGaussianPhaseDevice(Device device, Vector3 target, Vector3 axis, LaguerreGaussianOption option, Length wavelength,
|
|
835
|
+
public static void LaguerreGaussianPhaseDevice(Device device, Vector3 target, Vector3 axis, LaguerreGaussianOption option, Length wavelength, Phase[] dst)
|
|
547
836
|
{
|
|
548
|
-
var native =
|
|
837
|
+
var native = ToNative(dst);
|
|
549
838
|
if (NativePattern.autd3_pattern_laguerre_gaussian_phase_device(device.GeometryHandle, device.DeviceIndex,
|
|
550
839
|
Coords.PointArray(target), Coords.DirArray(axis), option.P, option.L, option.Waist.Mm, wavelength.Mm, native) != 0)
|
|
551
840
|
{
|
|
552
841
|
throw new Autd3Exception("laguerre_gaussian_phase_device failed (waist must be positive)");
|
|
553
842
|
}
|
|
554
|
-
|
|
843
|
+
FromNative(native, dst);
|
|
555
844
|
}
|
|
556
845
|
|
|
557
846
|
public static Phase LaguerreGaussianPhaseTransducer(Vector3 position, Vector3 target, Vector3 axis, LaguerreGaussianOption option, Length wavelength)
|
|
@@ -566,7 +855,7 @@ namespace AUTD3
|
|
|
566
855
|
return new Phase(p);
|
|
567
856
|
}
|
|
568
857
|
|
|
569
|
-
public static void LaguerreGaussianIntensity(Geometry geometry, Vector3 target, Vector3 axis, LaguerreGaussianOption option, Length wavelength,
|
|
858
|
+
public static void LaguerreGaussianIntensity(Geometry geometry, Vector3 target, Vector3 axis, LaguerreGaussianOption option, Length wavelength, IntensityBuffer dst)
|
|
570
859
|
{
|
|
571
860
|
if (NativePattern.autd3_pattern_laguerre_gaussian_intensity(geometry.Handle, Coords.PointArray(target), Coords.DirArray(axis),
|
|
572
861
|
option.P, option.L, option.Waist.Mm, wavelength.Mm, dst.Handle) != 0)
|
|
@@ -575,18 +864,18 @@ namespace AUTD3
|
|
|
575
864
|
}
|
|
576
865
|
}
|
|
577
866
|
|
|
578
|
-
public static void LaguerreGaussianIntensityDevice(Device device, Vector3 target, Vector3 axis, LaguerreGaussianOption option, Length wavelength,
|
|
867
|
+
public static void LaguerreGaussianIntensityDevice(Device device, Vector3 target, Vector3 axis, LaguerreGaussianOption option, Length wavelength, Intensity[] dst)
|
|
579
868
|
{
|
|
580
|
-
var native =
|
|
869
|
+
var native = ToNative(dst);
|
|
581
870
|
if (NativePattern.autd3_pattern_laguerre_gaussian_intensity_device(device.GeometryHandle, device.DeviceIndex,
|
|
582
871
|
Coords.PointArray(target), Coords.DirArray(axis), option.P, option.L, option.Waist.Mm, wavelength.Mm, native) != 0)
|
|
583
872
|
{
|
|
584
873
|
throw new Autd3Exception("laguerre_gaussian_intensity_device failed (waist must be positive)");
|
|
585
874
|
}
|
|
586
|
-
|
|
875
|
+
FromNative(native, dst);
|
|
587
876
|
}
|
|
588
877
|
|
|
589
|
-
public static void HermiteGaussianPhase(Geometry geometry, Vector3 target, Vector3 axis, Vector3 xDir, HermiteGaussianOption option, Length wavelength,
|
|
878
|
+
public static void HermiteGaussianPhase(Geometry geometry, Vector3 target, Vector3 axis, Vector3 xDir, HermiteGaussianOption option, Length wavelength, PhaseBuffer dst)
|
|
590
879
|
{
|
|
591
880
|
if (NativePattern.autd3_pattern_hermite_gaussian_phase(geometry.Handle, Coords.PointArray(target), Coords.DirArray(axis), Coords.DirArray(xDir),
|
|
592
881
|
option.M, option.N, option.Waist.Mm, wavelength.Mm, dst.Handle) != 0)
|
|
@@ -595,15 +884,15 @@ namespace AUTD3
|
|
|
595
884
|
}
|
|
596
885
|
}
|
|
597
886
|
|
|
598
|
-
public static void HermiteGaussianPhaseDevice(Device device, Vector3 target, Vector3 axis, Vector3 xDir, HermiteGaussianOption option, Length wavelength,
|
|
887
|
+
public static void HermiteGaussianPhaseDevice(Device device, Vector3 target, Vector3 axis, Vector3 xDir, HermiteGaussianOption option, Length wavelength, Phase[] dst)
|
|
599
888
|
{
|
|
600
|
-
var native =
|
|
889
|
+
var native = ToNative(dst);
|
|
601
890
|
if (NativePattern.autd3_pattern_hermite_gaussian_phase_device(device.GeometryHandle, device.DeviceIndex,
|
|
602
891
|
Coords.PointArray(target), Coords.DirArray(axis), Coords.DirArray(xDir), option.M, option.N, option.Waist.Mm, wavelength.Mm, native) != 0)
|
|
603
892
|
{
|
|
604
893
|
throw new Autd3Exception("hermite_gaussian_phase_device failed (waist must be positive)");
|
|
605
894
|
}
|
|
606
|
-
|
|
895
|
+
FromNative(native, dst);
|
|
607
896
|
}
|
|
608
897
|
|
|
609
898
|
public static Phase HermiteGaussianPhaseTransducer(Vector3 position, Vector3 target, Vector3 axis, Vector3 xDir, HermiteGaussianOption option, Length wavelength)
|
|
@@ -619,7 +908,7 @@ namespace AUTD3
|
|
|
619
908
|
return new Phase(p);
|
|
620
909
|
}
|
|
621
910
|
|
|
622
|
-
public static void HermiteGaussianIntensity(Geometry geometry, Vector3 target, Vector3 axis, Vector3 xDir, HermiteGaussianOption option, Length wavelength,
|
|
911
|
+
public static void HermiteGaussianIntensity(Geometry geometry, Vector3 target, Vector3 axis, Vector3 xDir, HermiteGaussianOption option, Length wavelength, IntensityBuffer dst)
|
|
623
912
|
{
|
|
624
913
|
if (NativePattern.autd3_pattern_hermite_gaussian_intensity(geometry.Handle, Coords.PointArray(target), Coords.DirArray(axis), Coords.DirArray(xDir),
|
|
625
914
|
option.M, option.N, option.Waist.Mm, wavelength.Mm, dst.Handle) != 0)
|
|
@@ -628,18 +917,18 @@ namespace AUTD3
|
|
|
628
917
|
}
|
|
629
918
|
}
|
|
630
919
|
|
|
631
|
-
public static void HermiteGaussianIntensityDevice(Device device, Vector3 target, Vector3 axis, Vector3 xDir, HermiteGaussianOption option, Length wavelength,
|
|
920
|
+
public static void HermiteGaussianIntensityDevice(Device device, Vector3 target, Vector3 axis, Vector3 xDir, HermiteGaussianOption option, Length wavelength, Intensity[] dst)
|
|
632
921
|
{
|
|
633
|
-
var native =
|
|
922
|
+
var native = ToNative(dst);
|
|
634
923
|
if (NativePattern.autd3_pattern_hermite_gaussian_intensity_device(device.GeometryHandle, device.DeviceIndex,
|
|
635
924
|
Coords.PointArray(target), Coords.DirArray(axis), Coords.DirArray(xDir), option.M, option.N, option.Waist.Mm, wavelength.Mm, native) != 0)
|
|
636
925
|
{
|
|
637
926
|
throw new Autd3Exception("hermite_gaussian_intensity_device failed (waist must be positive)");
|
|
638
927
|
}
|
|
639
|
-
|
|
928
|
+
FromNative(native, dst);
|
|
640
929
|
}
|
|
641
930
|
|
|
642
|
-
public static void SetIntensity(Intensity intensity,
|
|
931
|
+
public static void SetIntensity(Intensity intensity, IntensityBuffer dst)
|
|
643
932
|
{
|
|
644
933
|
if (NativePattern.autd3_pattern_set_intensity(intensity.Value, dst.Handle) != 0)
|
|
645
934
|
{
|
|
@@ -647,17 +936,7 @@ namespace AUTD3
|
|
|
647
936
|
}
|
|
648
937
|
}
|
|
649
938
|
|
|
650
|
-
public static void
|
|
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)
|
|
939
|
+
public static void SetPhase(Phase phase, PhaseBuffer dst)
|
|
661
940
|
{
|
|
662
941
|
if (NativePattern.autd3_pattern_set_phase(phase.Value, dst.Handle) != 0)
|
|
663
942
|
{
|
|
@@ -665,55 +944,35 @@ namespace AUTD3
|
|
|
665
944
|
}
|
|
666
945
|
}
|
|
667
946
|
|
|
668
|
-
public static void
|
|
947
|
+
public static void AddPhase(Phase phase, PhaseBuffer dst)
|
|
669
948
|
{
|
|
670
|
-
|
|
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)
|
|
949
|
+
if (NativePattern.autd3_pattern_add_phase(phase.Value, dst.Handle) != 0)
|
|
690
950
|
{
|
|
691
|
-
throw new Autd3Exception("
|
|
951
|
+
throw new Autd3Exception("add_phase failed");
|
|
692
952
|
}
|
|
693
|
-
FromNativeDst(native, dst);
|
|
694
953
|
}
|
|
695
954
|
|
|
696
|
-
public static void
|
|
955
|
+
public static void Group<TKey>(Geometry geometry, TransducerGroups<TKey> groups, Func<TKey, PhaseBuffer> source, PhaseBuffer dst) where TKey : notnull
|
|
697
956
|
{
|
|
698
|
-
|
|
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)
|
|
699
959
|
{
|
|
700
|
-
throw new Autd3Exception("
|
|
960
|
+
throw new Autd3Exception("group failed (every buffer must match the geometry)");
|
|
701
961
|
}
|
|
702
962
|
}
|
|
703
963
|
|
|
704
|
-
public static void
|
|
964
|
+
public static void Group<TKey>(Geometry geometry, TransducerGroups<TKey> groups, Func<TKey, IntensityBuffer> source, IntensityBuffer dst) where TKey : notnull
|
|
705
965
|
{
|
|
706
|
-
var
|
|
707
|
-
if (NativePattern.
|
|
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)
|
|
708
968
|
{
|
|
709
|
-
throw new Autd3Exception("
|
|
969
|
+
throw new Autd3Exception("group failed (every buffer must match the geometry)");
|
|
710
970
|
}
|
|
711
|
-
FromNativeDst(native, dst);
|
|
712
971
|
}
|
|
713
972
|
|
|
714
|
-
|
|
973
|
+
private static HandleArray GroupSources<TKey>(Geometry geometry, TransducerGroups<TKey> groups, Func<TKey, SafeHandle?> source, SafeHandle dst) where TKey : notnull
|
|
715
974
|
{
|
|
716
|
-
if (groups.
|
|
975
|
+
if (groups.FlatIndices.Length != geometry.NumTransducers)
|
|
717
976
|
{
|
|
718
977
|
throw new Autd3Exception("groups must be built from the same geometry");
|
|
719
978
|
}
|
|
@@ -721,52 +980,55 @@ namespace AUTD3
|
|
|
721
980
|
var handles = new SafeHandle[keys.Count];
|
|
722
981
|
for (var i = 0; i < keys.Count; i++)
|
|
723
982
|
{
|
|
724
|
-
var
|
|
725
|
-
if (
|
|
983
|
+
var handle = source(keys[i]);
|
|
984
|
+
if (handle == null)
|
|
726
985
|
{
|
|
727
986
|
throw new Autd3Exception($"no source was given for the key {keys[i]}");
|
|
728
987
|
}
|
|
729
|
-
if (ReferenceEquals(
|
|
988
|
+
if (ReferenceEquals(handle, dst))
|
|
730
989
|
{
|
|
731
990
|
throw new Autd3Exception("dst must not be one of the sources");
|
|
732
991
|
}
|
|
733
|
-
handles[i] =
|
|
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)");
|
|
992
|
+
handles[i] = handle;
|
|
739
993
|
}
|
|
994
|
+
return new HandleArray(handles);
|
|
740
995
|
}
|
|
741
996
|
|
|
742
|
-
public static void GroupCompute<TKey>(Geometry geometry, TransducerGroups<TKey> groups, Action<TKey, TransducerMask,
|
|
997
|
+
public static void GroupCompute<TKey>(Geometry geometry, TransducerGroups<TKey> groups, Action<TKey, TransducerMask, PhaseBuffer, IntensityBuffer> compute, PhaseBuffer phases, IntensityBuffer intensities) where TKey : notnull
|
|
743
998
|
{
|
|
744
999
|
if (compute == null)
|
|
745
1000
|
{
|
|
746
1001
|
throw new ArgumentNullException(nameof(compute));
|
|
747
1002
|
}
|
|
748
|
-
if (groups.
|
|
1003
|
+
if (groups.FlatIndices.Length != geometry.NumTransducers)
|
|
749
1004
|
{
|
|
750
1005
|
throw new Autd3Exception("groups must be built from the same geometry");
|
|
751
1006
|
}
|
|
752
|
-
if (
|
|
1007
|
+
if (phases.NumDevices != geometry.NumDevices || intensities.NumDevices != geometry.NumDevices)
|
|
753
1008
|
{
|
|
754
1009
|
throw new Autd3Exception("group_compute failed (dst must match the geometry)");
|
|
755
1010
|
}
|
|
756
|
-
using var
|
|
1011
|
+
using var scratchPhases = geometry.PhaseBuffer();
|
|
1012
|
+
using var scratchIntensities = geometry.IntensityBuffer();
|
|
757
1013
|
var keys = groups.Keys;
|
|
758
1014
|
for (var i = 0; i < keys.Count; i++)
|
|
759
1015
|
{
|
|
760
|
-
|
|
761
|
-
|
|
762
|
-
|
|
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)
|
|
763
1021
|
{
|
|
764
1022
|
throw new Autd3Exception("group_compute failed (dst must match the geometry)");
|
|
765
1023
|
}
|
|
766
1024
|
}
|
|
767
1025
|
}
|
|
768
1026
|
|
|
769
|
-
private static
|
|
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)
|
|
770
1032
|
{
|
|
771
1033
|
if (dst == null)
|
|
772
1034
|
{
|
|
@@ -774,30 +1036,29 @@ namespace AUTD3
|
|
|
774
1036
|
}
|
|
775
1037
|
if (dst.Length != Autd3.NumTransducers)
|
|
776
1038
|
{
|
|
777
|
-
throw new Autd3Exception($"dst requires {Autd3.NumTransducers}
|
|
1039
|
+
throw new Autd3Exception($"dst requires {Autd3.NumTransducers} elements");
|
|
778
1040
|
}
|
|
779
|
-
|
|
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;
|
|
780
1047
|
}
|
|
781
1048
|
|
|
782
|
-
private static
|
|
1049
|
+
private static void FromNative(byte[] native, Phase[] dst)
|
|
783
1050
|
{
|
|
784
|
-
if (dst == null)
|
|
785
|
-
{
|
|
786
|
-
throw new ArgumentNullException(nameof(dst));
|
|
787
|
-
}
|
|
788
|
-
var native = new EmissionNative[dst.Length];
|
|
789
1051
|
for (var i = 0; i < dst.Length; i++)
|
|
790
1052
|
{
|
|
791
|
-
|
|
1053
|
+
dst[i] = new Phase(native[i]);
|
|
792
1054
|
}
|
|
793
|
-
return native;
|
|
794
1055
|
}
|
|
795
1056
|
|
|
796
|
-
private static void
|
|
1057
|
+
private static void FromNative(byte[] native, Intensity[] dst)
|
|
797
1058
|
{
|
|
798
1059
|
for (var i = 0; i < dst.Length; i++)
|
|
799
1060
|
{
|
|
800
|
-
dst[i] = new
|
|
1061
|
+
dst[i] = new Intensity(native[i]);
|
|
801
1062
|
}
|
|
802
1063
|
}
|
|
803
1064
|
}
|