Skip to content
This repository was archived by the owner on Oct 18, 2023. It is now read-only.

Commit ba2ef9e

Browse files
Contour constructor with no arguments
1 parent 8d29f80 commit ba2ef9e

File tree

2 files changed

+26
-23
lines changed

2 files changed

+26
-23
lines changed

cc/modules/imgproc/Contour.cc

Lines changed: 25 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -38,35 +38,37 @@ NAN_MODULE_INIT(Contour::Init) {
3838
};
3939

4040
NAN_METHOD(Contour::New) {
41-
if (info.Length() != 1) {
42-
return Nan::ThrowError("Contour::New - expected one argument");
41+
if (info.Length() > 1) {
42+
return Nan::ThrowError("Contour::New - expected one or zero argument");
4343
}
44-
if (!info[0]->IsArray()) {
44+
if (info.Length() == 1 && !info[0]->IsArray()) {
4545
return Nan::ThrowError("Contour::New - expected arg0 to be an array");
4646
}
4747

4848
Contour* self = new Contour();
49-
FF_ARR jsPts = FF_ARR::Cast(info[0]);
50-
self->contour.reserve(jsPts->Length());
51-
for (int i = 0; i < jsPts->Length(); i++) {
52-
cv::Point2d cv_pt;
53-
auto jsPt = jsPts->Get(i);
54-
if (jsPt->IsArray()) {
55-
FF_ARR jsObj = FF_ARR::Cast(jsPt);
56-
if (jsObj->Length() != 2)
57-
return Nan::ThrowError("Contour::New - expected arg0 to have only Point2 or array of length 2");
58-
double x = jsObj->Get(0)->NumberValue();
59-
double y = jsObj->Get(1)->NumberValue();
60-
cv_pt = cv::Point2d(x, y);
49+
if (info.Length() == 1) {
50+
FF_ARR jsPts = FF_ARR::Cast(info[0]);
51+
self->contour.reserve(jsPts->Length());
52+
for (int i = 0; i < jsPts->Length(); i++) {
53+
cv::Point2d cv_pt;
54+
auto jsPt = jsPts->Get(i);
55+
if (jsPt->IsArray()) {
56+
FF_ARR jsObj = FF_ARR::Cast(jsPt);
57+
if (jsObj->Length() != 2)
58+
return Nan::ThrowError("Contour::New - expected arg0 to consist of only Point2 or array of length 2");
59+
double x = jsObj->Get(0)->NumberValue();
60+
double y = jsObj->Get(1)->NumberValue();
61+
cv_pt = cv::Point2d(x, y);
62+
}
63+
else if (FF_IS_INSTANCE(Point2::constructor, jsPt)) {
64+
FF_OBJ jsObj = FF_CAST_OBJ(jsPt);
65+
cv_pt = FF_UNWRAP_PT2_AND_GET(jsObj);
66+
}
67+
else {
68+
return Nan::ThrowError("Contour::New - expected arg0 to consist of only Point2 or array of length 2");
69+
}
70+
self->contour.emplace_back(cv::Point2i(cv_pt.x, cv_pt.y));
6171
}
62-
else if (FF_IS_INSTANCE(Point2::constructor, jsPt)) {
63-
FF_OBJ jsObj = FF_CAST_OBJ(jsPt);
64-
cv_pt = FF_UNWRAP_PT2_AND_GET(jsObj);
65-
}
66-
else {
67-
return Nan::ThrowError("Contour::New - expected arg0 to have only Point2 or array length 2");
68-
}
69-
self->contour.emplace_back(cv::Point2i(cv_pt.x, cv_pt.y));
7072
}
7173
self->hierarchy = cv::Vec4i(-1, -1, -1, -1);
7274
self->Wrap(info.Holder());

lib/typings/Contour.d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ export class Contour {
99
readonly area: number;
1010
readonly isConvex: boolean;
1111
readonly hierarchy: Vec4;
12+
constructor();
1213
constructor(pts: Point2[]);
1314
constructor(pts: number[][]);
1415
approxPolyDP(epsilon: number, closed: boolean): Point2[];

0 commit comments

Comments
 (0)