File: Point.cc

package info (click to toggle)
node-opencv 6.0.0+git20180416.cfc96ba0-3
  • links: PTS, VCS
  • area: main
  • in suites: buster, sid
  • size: 24,632 kB
  • sloc: xml: 476,707; cpp: 5,950; makefile: 114; sh: 59; ansic: 20
file content (72 lines) | stat: -rwxr-xr-x 2,031 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
#include "Point.h"
#include "OpenCV.h"

Nan::Persistent<FunctionTemplate> Point::constructor;

void Point::Init(Local<Object> target) {
  Nan::HandleScope scope;

  // Constructor
  Local<FunctionTemplate> ctor = Nan::New<FunctionTemplate>(Point::New);
  constructor.Reset(ctor);
  ctor->InstanceTemplate()->SetInternalFieldCount(1);
  ctor->SetClassName(Nan::New("Point").ToLocalChecked());

  // Prototype
  Local<ObjectTemplate> proto = ctor->PrototypeTemplate();
  Nan::SetAccessor(proto, Nan::New("x").ToLocalChecked(), GetX, RaiseImmutable);
  Nan::SetAccessor(proto, Nan::New("y").ToLocalChecked(), GetY, RaiseImmutable);

  Nan::SetPrototypeMethod(ctor, "dot", Dot);

  target->Set(Nan::New("Point").ToLocalChecked(), ctor->GetFunction());
};

NAN_METHOD(Point::New) {
  Nan::HandleScope scope;

  if (info.This()->InternalFieldCount() == 0) {
    return Nan::ThrowTypeError("Cannot Instantiate without new");
  }

  double x = 0, y = 0;
  if (info[0]->IsNumber()) {
    x = info[0]->NumberValue();
  }
  if (info[1]->IsNumber()) {
    y = info[1]->NumberValue();
  }
  Point *pt = new Point(x, y);
  pt->Wrap(info.This());
  info.GetReturnValue().Set(info.This());
}

NAN_GETTER(Point::GetX) {
  Nan::HandleScope scope;
  Point *pt = Nan::ObjectWrap::Unwrap<Point>(info.This());
  info.GetReturnValue().Set(Nan::New<Number>(pt->point.x));
}

NAN_GETTER(Point::GetY) {
  Nan::HandleScope scope;
  Point *pt = Nan::ObjectWrap::Unwrap<Point>(info.This());
  info.GetReturnValue().Set(Nan::New<Number>(pt->point.y));
}

NAN_SETTER(Point::RaiseImmutable) {
  Nan::ThrowTypeError("Point is immutable");
}

NAN_METHOD(Point::Dot) {
  Nan::HandleScope scope;
  Point *p1 = Nan::ObjectWrap::Unwrap<Point>(info.This());
  Point *p2 = Nan::ObjectWrap::Unwrap<Point>(info[0]->ToObject());

  // Since V 2.3 Native Dot no longer supported
  info.GetReturnValue().Set(Nan::New<Number>(p1->point.x * p2->point.x + p1->point.y * p2->point.y));
}

Point::Point(double x, double y) :
    Nan::ObjectWrap() {
  point = cvPoint2D32f(x, y);
}