7
7
8
8
Nan::Persistent<v8::FunctionTemplate> Contour::constructor;
9
9
10
- void Contour::Init ( ) {
11
- v8::Local<v8::FunctionTemplate> ctor = Nan::New<v8::FunctionTemplate>(Contour::New);
12
- constructor.Reset (ctor);
13
- ctor->InstanceTemplate ()->SetInternalFieldCount (1 );
14
- ctor->SetClassName (Nan::New (" Contour" ).ToLocalChecked ());
10
+ NAN_MODULE_INIT ( Contour::Init) {
11
+ v8::Local<v8::FunctionTemplate> ctor = Nan::New<v8::FunctionTemplate>(Contour::New);
12
+ constructor.Reset (ctor);
13
+ ctor->InstanceTemplate ()->SetInternalFieldCount (1 );
14
+ ctor->SetClassName (Nan::New (" Contour" ).ToLocalChecked ());
15
15
16
16
Nan::SetAccessor (ctor->InstanceTemplate (), FF_NEW_STRING (" isConvex" ), GetIsConvex);
17
17
Nan::SetAccessor (ctor->InstanceTemplate (), FF_NEW_STRING (" area" ), GetArea);
@@ -33,8 +33,48 @@ void Contour::Init() {
33
33
Nan::SetPrototypeMethod (ctor, " matchShapes" , MatchShapes);
34
34
Nan::SetPrototypeMethod (ctor, " fitEllipse" , FitEllipse);
35
35
Nan::SetPrototypeMethod (ctor, " moments" , _Moments);
36
+
37
+ target->Set (Nan::New (" Contour" ).ToLocalChecked (), ctor->GetFunction ());
36
38
};
37
39
40
+ NAN_METHOD (Contour::New) {
41
+ if (info.Length () > 1 ) {
42
+ return Nan::ThrowError (" Contour::New - expected one or zero argument" );
43
+ }
44
+ if (info.Length () == 1 && !info[0 ]->IsArray ()) {
45
+ return Nan::ThrowError (" Contour::New - expected arg0 to be an array" );
46
+ }
47
+
48
+ Contour* self = new Contour ();
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 ));
71
+ }
72
+ }
73
+ self->hierarchy = cv::Vec4i (-1 , -1 , -1 , -1 );
74
+ self->Wrap (info.Holder ());
75
+ info.GetReturnValue ().Set (info.Holder ());
76
+ }
77
+
38
78
NAN_METHOD (Contour::GetPoints) {
39
79
info.GetReturnValue ().Set (Point::packJSPoint2Array (FF_UNWRAP_CONTOUR_AND_GET (info.This ())));
40
80
}
0 commit comments