Skip to content

Commit

Permalink
add test for subdiv2d
Browse files Browse the repository at this point in the history
  • Loading branch information
rainyl committed May 18, 2024
1 parent 8df83f9 commit d469061
Show file tree
Hide file tree
Showing 3 changed files with 143 additions and 13 deletions.
40 changes: 27 additions & 13 deletions lib/src/imgproc/subdiv2d.dart
Original file line number Diff line number Diff line change
Expand Up @@ -102,8 +102,8 @@ class Subdiv2D extends CvStruct<cvg.Subdiv2D> {
final psize = arena<ffi.Int>();
cvRun(() => CFFI.Subdiv2D_GetEdgeList(ref, pv, psize));
return List.generate(psize.value, (i) {
final v = pv[i];
return Vec4f(v.ref.val1, v.ref.val2, v.ref.val3, v.ref.val4);
final v = pv.value[i];
return Vec4f(v.val1, v.val2, v.val3, v.val4);
});
});
}
Expand Down Expand Up @@ -131,8 +131,8 @@ class Subdiv2D extends CvStruct<cvg.Subdiv2D> {
final psize = arena<ffi.Int>();
cvRun(() => CFFI.Subdiv2D_GetTriangleList(ref, pv, psize));
return List.generate(psize.value, (i) {
final v = pv[i];
return Vec6f(v.ref.val1, v.ref.val2, v.ref.val3, v.ref.val4, v.ref.val5, v.ref.val6);
final v = pv.value[i];
return Vec6f(v.val1, v.val2, v.val3, v.val4, v.val5, v.val6);
});
});
}
Expand Down Expand Up @@ -194,6 +194,14 @@ class Subdiv2D extends CvStruct<cvg.Subdiv2D> {

/// Returns the location of a point within a Delaunay triangulation.
///
/// [rval] an integer which specify one of the following five cases for point location:
///
/// - The point falls into some facet. The function returns [PTLOC_INSIDE] and edge will contain one of edges of the facet.
/// - The point falls onto the edge. The function returns [PTLOC_ON_EDGE] and edge will contain this edge.
/// - The point coincides with one of the subdivision vertices. The function returns [PTLOC_VERTEX] and vertex will contain a pointer to the vertex.
/// - The point is outside the subdivision reference rectangle. The function returns [PTLOC_OUTSIDE_RECT] and no pointers are filled.
/// - One of input arguments is invalid. A runtime error is raised or, if silent or "parent" error processing mode is selected, [PTLOC_ERROR] is returned.
///
/// https://docs.opencv.org/4.x/df/dbf/classcv_1_1Subdiv2D.html#aec8f1fd5a802f62faa97520b465897d7
(int rval, int edge, int vertex) locate(Point2f pt) {
return using<(int, int, int)>((arena) {
Expand Down Expand Up @@ -241,13 +249,19 @@ class Subdiv2D extends CvStruct<cvg.Subdiv2D> {

@override
cvg.Subdiv2D get ref => ptr.ref;
}

const int NEXT_AROUND_ORG = 0x00;
const int NEXT_AROUND_DST = 0x22;
const int PREV_AROUND_ORG = 0x11;
const int PREV_AROUND_DST = 0x33;
const int NEXT_AROUND_LEFT = 0x13;
const int NEXT_AROUND_RIGHT = 0x31;
const int PREV_AROUND_LEFT = 0x20;
const int PREV_AROUND_RIGHT = 0x02;
static const int NEXT_AROUND_ORG = 0x00;
static const int NEXT_AROUND_DST = 0x22;
static const int PREV_AROUND_ORG = 0x11;
static const int PREV_AROUND_DST = 0x33;
static const int NEXT_AROUND_LEFT = 0x13;
static const int NEXT_AROUND_RIGHT = 0x31;
static const int PREV_AROUND_LEFT = 0x20;
static const int PREV_AROUND_RIGHT = 0x02;

static const int PTLOC_ERROR = -2;
static const int PTLOC_OUTSIDE_RECT = -1;
static const int PTLOC_INSIDE = 0;
static const int PTLOC_VERTEX = 1;
static const int PTLOC_ON_EDGE = 2;
}
5 changes: 5 additions & 0 deletions test/imgproc/clahe_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,10 @@ void main() {
final clahe = cv.CLAHE();
final dst = clahe.apply(mat);
expect(dst.isEmpty, false);

clahe.clipLimit = 50;
clahe.tilesGridSize = (10, 10);
expect(clahe.tilesGridSize, (10, 10));
expect(clahe.clipLimit, closeTo(50, 1e-6));
});
}
111 changes: 111 additions & 0 deletions test/imgproc/subdiv2d_test.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
import 'package:test/test.dart';

import 'package:opencv_dart/opencv_dart.dart' as cv;

void main() {
final src = cv.Mat.fromScalar(400, 400, cv.MatType.CV_8UC3, cv.Scalar.white);
final points = [
cv.Point2f(23, 45),
cv.Point2f(243, 145),
cv.Point2f(308, 25),
cv.Point2f(180, 230),
cv.Point2f(343, 145),
cv.Point2f(108, 25),
];
for (var pt in points) {
cv.circle(src, cv.Point(pt.x.toInt(), pt.y.toInt()), 1, cv.Scalar.black, thickness: 2);
}

test("cv.Subdiv2D", () {
final subdiv = cv.Subdiv2D.fromRect(cv.Rect(0, 0, src.width, src.height));
subdiv.insertVec(points.cvd);

final triangleList = subdiv.getTriangleList();
expect(triangleList.length, greaterThan(0));
for (var tri in triangleList) {
final p1 = cv.Point(tri.val1.toInt(), tri.val2.toInt());
final p2 = cv.Point(tri.val3.toInt(), tri.val4.toInt());
final p3 = cv.Point(tri.val5.toInt(), tri.val6.toInt());
cv.line(src, p1, p2, cv.Scalar.red, thickness: 1);
cv.line(src, p1, p3, cv.Scalar.red, thickness: 1);
cv.line(src, p2, p3, cv.Scalar.red, thickness: 1);
}
// cv.imwrite("subdiv2d.png", src);
final win = cv.Window("Subdiv2D");
win.imshow(src);
// win.waitKey(0);
cv.destroyAllWindows();
});

test('cv.Subdiv2D.empty', () {
final sub1 = cv.Subdiv2D.empty();
sub1.initDelaunay(cv.Rect(0, 0, src.width, src.height));
sub1.insert(cv.Point2f(241, 241));
});

test('cv.Subdiv2D others', () {
final subdiv = cv.Subdiv2D.fromRect(cv.Rect(0, 0, src.width, src.height));
subdiv.insertVec(points.cvd);

{
final (rval, pt) = subdiv.edgeDst(1);
expect(rval, 0);
expect(pt, cv.Point2f(0, 0));
}

{
final (rval, pt) = subdiv.edgeOrg(1);
expect(rval, 0);
expect(pt, cv.Point2f(0, 0));
}

{
final (rval, pt) = subdiv.findNearest(cv.Point2f(241, 241));
expect(rval, 7);
expect(pt, cv.Point2f(180, 230));
}

{
final edge = subdiv.getEdge(1, cv.Subdiv2D.NEXT_AROUND_LEFT);
expect(edge, 1);
}

{
final edges = subdiv.getEdgeList();
expect(edges.length, greaterThan(0));
}

{
final r = subdiv.getLeadingEdgeList();
expect(r.length, greaterThan(0));
}

{
final (pt, v) = subdiv.getVertex(0);
expect(pt, cv.Point2f(0, 0));
expect(v, 0);
}

{
final (fl, fc) = subdiv.getVoronoiFacetList([0, 1].i32);
expect(fl.length, greaterThan(0));
expect(fc.length, greaterThan(0));
}

{
final (rval, edge, vertex) = subdiv.locate(cv.Point2f(241, 241));
expect(rval, cv.Subdiv2D.PTLOC_INSIDE);
expect(edge, 72);
expect(vertex, 0);
}

{
final nextEdge = subdiv.nextEdge(0);
expect(nextEdge, 0);
final rEdge = subdiv.rotateEdge(0, 90);
expect(rEdge, 2);
final sEdge = subdiv.symEdge(0);
expect(sEdge, 2);
}
});
}

0 comments on commit d469061

Please sign in to comment.