@@ -38,35 +38,37 @@ NAN_MODULE_INIT(Contour::Init) {
38
38
};
39
39
40
40
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" );
43
43
}
44
- if (!info[0 ]->IsArray ()) {
44
+ if (info. Length () == 1 && !info[0 ]->IsArray ()) {
45
45
return Nan::ThrowError (" Contour::New - expected arg0 to be an array" );
46
46
}
47
47
48
48
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 ));
61
71
}
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 ));
70
72
}
71
73
self->hierarchy = cv::Vec4i (-1 , -1 , -1 , -1 );
72
74
self->Wrap (info.Holder ());
0 commit comments