Skip to content

Commit

Permalink
add rng async
Browse files Browse the repository at this point in the history
  • Loading branch information
rainyl committed Jun 30, 2024
1 parent 6442830 commit dcea8c3
Show file tree
Hide file tree
Showing 7 changed files with 60 additions and 10 deletions.
1 change: 1 addition & 0 deletions lib/opencv_dart.dart
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ export 'src/core/moments.dart';
export 'src/core/point.dart';
export 'src/core/rect.dart';
export 'src/core/rng.dart';
export 'src/core/rng_async.dart';
export 'src/core/scalar.dart';
export 'src/core/size.dart';
export 'src/core/vec.dart';
Expand Down
2 changes: 1 addition & 1 deletion lib/src/core/core_async.dart
Original file line number Diff line number Diff line change
Expand Up @@ -899,7 +899,7 @@ Future<Mat> scaleAddAsync(InputArray src1, double alpha, InputArray src2) async
///
/// https://docs.opencv.org/master/d2/de8/group__core__array.html#ga388d7575224a4a277ceb98ccaa327c99
Future<Mat> setIdentityAsync(InputOutputArray mtx, {Scalar? s}) async => cvRunAsync0(
(callback) => CFFI.core_SetIdentity_Async(mtx.ref, s?.ref ?? Scalar.default_().ref, callback),
(callback) => CFFI.core_SetIdentity_Async(mtx.ref, s?.ref ?? Scalar.all(1).ref, callback),
(c) => c.complete(mtx),
);

Expand Down
20 changes: 15 additions & 5 deletions lib/src/core/rng_async.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,15 @@ import 'package:ffi/ffi.dart';
import '../opencv.g.dart' as cvg;
import 'base.dart';
import 'mat.dart';
import 'mat_async.dart';
import 'rng.dart';

extension RngAsync on Rng {
static Future<Rng> createAsync() async => cvRunAsync(
CFFI.Rng_New_Async,
(completer, p) => completer.complete(Rng.fromTheRng(p.cast<cvg.RNG>())),
);

static Future<Rng> fromSeedAsync(int seed) async => cvRunAsync(
(callback) => CFFI.Rng_NewWithState_Async(seed, callback),
(completer, p) => completer.complete(Rng.fromTheRng(p.cast<cvg.RNG>())),
Expand All @@ -24,12 +30,16 @@ extension RngAsync on Rng {
bool inplace = false,
}) async {
if (inplace) {
cvRun(() => CFFI.RNG_Fill(ref, mat.ref, distType, a, b, saturateRange));
return mat;
return cvRunAsync0<Mat>(
(callback) => CFFI.RNG_Fill_Async(ref, mat.ref, distType, a, b, saturateRange, callback),
(c) => c.complete(mat),
);
} else {
final m = mat.clone();
cvRun(() => CFFI.RNG_Fill(ref, m.ref, distType, a, b, saturateRange));
return m;
final m = await mat.cloneAsync();
return cvRunAsync0<Mat>(
(callback) => CFFI.RNG_Fill_Async(ref, m.ref, distType, a, b, saturateRange, callback),
(c) => c.complete(m),
);
}
}

Expand Down
7 changes: 5 additions & 2 deletions lib/src/opencv.g.dart
Original file line number Diff line number Diff line change
Expand Up @@ -21013,6 +21013,7 @@ class CvNative {
double a,
double b,
bool saturateRange,
CvCallback_0 callback,
) {
return _RNG_Fill_Async(
rng,
Expand All @@ -21021,15 +21022,17 @@ class CvNative {
a,
b,
saturateRange,
callback,
);
}

late final _RNG_Fill_AsyncPtr = _lookup<
ffi.NativeFunction<
ffi.Pointer<CvStatus> Function(RNG, Mat, ffi.Int, ffi.Double,
ffi.Double, ffi.Bool)>>('RNG_Fill_Async');
ffi.Double, ffi.Bool, CvCallback_0)>>('RNG_Fill_Async');
late final _RNG_Fill_Async = _RNG_Fill_AsyncPtr.asFunction<
ffi.Pointer<CvStatus> Function(RNG, Mat, int, double, double, bool)>();
ffi.Pointer<CvStatus> Function(
RNG, Mat, int, double, double, bool, CvCallback_0)>();

ffi.Pointer<CvStatus> RNG_Gaussian(
RNG rng,
Expand Down
5 changes: 4 additions & 1 deletion src/core/core_async.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1143,9 +1143,12 @@ CvStatus *Rng_NewWithState_Async(uint64_t state, CvCallback_1 callback) {
END_WRAP
}

CvStatus *RNG_Fill_Async(RNG rng, Mat mat, int distType, double a, double b, bool saturateRange) {
CvStatus *RNG_Fill_Async(
RNG rng, Mat mat, int distType, double a, double b, bool saturateRange, CvCallback_0 callback
) {
BEGIN_WRAP
rng.ptr->fill(*mat.ptr, distType, a, b, saturateRange);
callback();
END_WRAP
}

Expand Down
2 changes: 1 addition & 1 deletion src/core/core_async.h
Original file line number Diff line number Diff line change
Expand Up @@ -374,7 +374,7 @@ CvStatus *core_NormWithMats_Async(Mat src1, Mat src2, int normType, CvCallback_1

CvStatus *Rng_New_Async(CvCallback_1 callback);
CvStatus *Rng_NewWithState_Async(uint64_t state, CvCallback_1 callback);
CvStatus *RNG_Fill_Async(RNG rng, Mat mat, int distType, double a, double b, bool saturateRange);
CvStatus *RNG_Fill_Async(RNG rng, Mat mat, int distType, double a, double b, bool saturateRange, CvCallback_0 callback);
CvStatus *RNG_Gaussian_Async(RNG rng, double sigma, CvCallback_1 callback);
CvStatus *RNG_Uniform_Async(RNG rng, int a, int b, CvCallback_1 callback);
CvStatus *RNG_UniformDouble_Async(RNG rng, double a, double b, CvCallback_1 callback);
Expand Down
33 changes: 33 additions & 0 deletions test/core/rng_async_test.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import 'package:opencv_dart/opencv_dart.dart' as cv;
import 'package:test/test.dart';

void main() async {
test("cv.RNG", () async {
final rng = await cv.RngAsync.createAsync();
final v = List.generate(100000, (index) => rng.uniform(0, 241));
expect(v, everyElement(greaterThanOrEqualTo(0)));
final v1 = List.generate(100000, (index) => rng.uniform<double>(2.41, 241.0));
expect(v1, everyElement(greaterThanOrEqualTo(2.41)));

rng.dispose();
});

test("cv.RNG.fromSeed", () async {
final rng = await cv.RngAsync.fromSeedAsync(241);
final v = List.generate(100000, (index) => rng.gaussian(2.41));
expect(v.length, equals(100000));

final v1 = await rng.nextAsync();
expect(v1, isA<int>());

final rng1 = cv.Rng.fromSeed(241);
expect(rng == rng1, false);
});

test("cv.RNG.fill", () async {
final rng = cv.Rng.fromSeed(241);
final mat = cv.Mat.zeros(241, 241, cv.MatType.CV_32FC3);
await rng.fillAsync(mat, cv.RNG_DIST_NORMAL, 0, 10, inplace: true);
expect(mat.at<double>(0, 0) != 0, true);
});
}

0 comments on commit dcea8c3

Please sign in to comment.