Skip to content

Commit

Permalink
Merge pull request #583 from thekemkid/feat/vec5
Browse files Browse the repository at this point in the history
Add base support for vec6
  • Loading branch information
justadudewhohacks authored Jun 25, 2019
2 parents 8857337 + 816fd6a commit 4fb9333
Show file tree
Hide file tree
Showing 13 changed files with 287 additions and 23 deletions.
75 changes: 54 additions & 21 deletions cc/core/Vec.cc
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
Nan::Persistent<v8::FunctionTemplate> Vec2::constructor;
Nan::Persistent<v8::FunctionTemplate> Vec3::constructor;
Nan::Persistent<v8::FunctionTemplate> Vec4::constructor;
Nan::Persistent<v8::FunctionTemplate> Vec6::constructor;

NAN_MODULE_INIT(Vec::Init) {
v8::Local<v8::FunctionTemplate> vec2Ctor = Nan::New<v8::FunctionTemplate>(Vec2::New);
Expand Down Expand Up @@ -39,42 +40,74 @@ NAN_MODULE_INIT(Vec::Init) {
Nan::SetPrototypeMethod(vec4Ctor, "norm", Vec4::Norm);
Vec4::Init(vec4Ctor);

v8::Local<v8::FunctionTemplate> vec6Ctor = Nan::New<v8::FunctionTemplate>(Vec6::New);
Vec6::constructor.Reset(vec6Ctor);
vec6Ctor->InstanceTemplate()->SetInternalFieldCount(1);
vec6Ctor->SetClassName(Nan::New("Vec6").ToLocalChecked());
Nan::SetAccessor(vec6Ctor->InstanceTemplate(), Nan::New("u").ToLocalChecked(), Vec6::u_getter);
Nan::SetAccessor(vec6Ctor->InstanceTemplate(), Nan::New("v").ToLocalChecked(), Vec6::v_getter);
Nan::SetAccessor(vec6Ctor->InstanceTemplate(), Nan::New("w").ToLocalChecked(), Vec6::w_getter);
Nan::SetAccessor(vec6Ctor->InstanceTemplate(), Nan::New("x").ToLocalChecked(), Vec6::x_getter);
Nan::SetAccessor(vec6Ctor->InstanceTemplate(), Nan::New("y").ToLocalChecked(), Vec6::y_getter);
Nan::SetAccessor(vec6Ctor->InstanceTemplate(), Nan::New("z").ToLocalChecked(), Vec6::z_getter);
Nan::SetPrototypeMethod(vec6Ctor, "at", Vec6::At);
Nan::SetPrototypeMethod(vec6Ctor, "norm", Vec6::Norm);
Vec6::Init(vec6Ctor);

v8::Local<v8::FunctionTemplate> ctor = Nan::New<v8::FunctionTemplate>(Vec::New);
ctor->InstanceTemplate()->SetInternalFieldCount(1);
ctor->SetClassName(Nan::New("Vec").ToLocalChecked());
Nan::Set(target,Nan::New("Vec").ToLocalChecked(), FF::getFunction(ctor));
Nan::Set(target,Nan::New("Vec2").ToLocalChecked(), FF::getFunction(ctor));
Nan::Set(target,Nan::New("Vec3").ToLocalChecked(), FF::getFunction(ctor));
Nan::Set(target,Nan::New("Vec4").ToLocalChecked(), FF::getFunction(ctor));
Nan::Set(target,Nan::New("Vec6").ToLocalChecked(), FF::getFunction(ctor));
};

NAN_METHOD(Vec::New) {
FF::TryCatch tryCatch("Vec::New");
FF_ASSERT_CONSTRUCT_CALL();
if (info.Length() < 2) {
return tryCatch.throwError("Vec::New - expected arguments (w), x, y, (z)");
if (info.Length() < 2 || info.Length() > 6 || info.Length() == 5) {
return tryCatch.throwError("Vec::New - expected arguments (u, v), (w), x, y, (z)");
}
v8::Local<v8::Object> jsVec;
if (info.Length() == 4) {
jsVec = FF::newInstance(Nan::New(Vec4::constructor));
Nan::ObjectWrap::Unwrap<Vec4>(jsVec)->self = cv::Vec4d(
info[0]->ToNumber(Nan::GetCurrentContext()).ToLocalChecked()->Value(),
info[1]->ToNumber(Nan::GetCurrentContext()).ToLocalChecked()->Value(),
info[2]->ToNumber(Nan::GetCurrentContext()).ToLocalChecked()->Value(),
info[3]->ToNumber(Nan::GetCurrentContext()).ToLocalChecked()->Value()
);
} else {
double x = info[0]->ToNumber(Nan::GetCurrentContext()).ToLocalChecked()->Value();
double y = info[1]->ToNumber(Nan::GetCurrentContext()).ToLocalChecked()->Value();
if (info.Length() == 3) {
double z = info[2]->ToNumber(Nan::GetCurrentContext()).ToLocalChecked()->Value();
jsVec = FF::newInstance(Nan::New(Vec3::constructor));
Nan::ObjectWrap::Unwrap<Vec3>(jsVec)->self = cv::Vec3d(x, y, z);
}
else {

switch(info.Length()) {
case 2:
jsVec = FF::newInstance(Nan::New(Vec2::constructor));
Nan::ObjectWrap::Unwrap<Vec2>(jsVec)->self = cv::Vec2d(x, y);
}
Nan::ObjectWrap::Unwrap<Vec2>(jsVec)->self = cv::Vec2d(
info[0]->ToNumber(Nan::GetCurrentContext()).ToLocalChecked()->Value(),
info[1]->ToNumber(Nan::GetCurrentContext()).ToLocalChecked()->Value()
);
break;
case 3:
jsVec = FF::newInstance(Nan::New(Vec3::constructor));
Nan::ObjectWrap::Unwrap<Vec3>(jsVec)->self = cv::Vec3d(
info[0]->ToNumber(Nan::GetCurrentContext()).ToLocalChecked()->Value(),
info[1]->ToNumber(Nan::GetCurrentContext()).ToLocalChecked()->Value(),
info[2]->ToNumber(Nan::GetCurrentContext()).ToLocalChecked()->Value()
);
break;
case 4:
jsVec = FF::newInstance(Nan::New(Vec4::constructor));
Nan::ObjectWrap::Unwrap<Vec4>(jsVec)->self = cv::Vec4d(
info[0]->ToNumber(Nan::GetCurrentContext()).ToLocalChecked()->Value(),
info[1]->ToNumber(Nan::GetCurrentContext()).ToLocalChecked()->Value(),
info[2]->ToNumber(Nan::GetCurrentContext()).ToLocalChecked()->Value(),
info[3]->ToNumber(Nan::GetCurrentContext()).ToLocalChecked()->Value()
);
break;
case 6:
jsVec = FF::newInstance(Nan::New(Vec6::constructor));
Nan::ObjectWrap::Unwrap<Vec6>(jsVec)->self = cv::Vec6d(
info[0]->ToNumber(Nan::GetCurrentContext()).ToLocalChecked()->Value(),
info[1]->ToNumber(Nan::GetCurrentContext()).ToLocalChecked()->Value(),
info[2]->ToNumber(Nan::GetCurrentContext()).ToLocalChecked()->Value(),
info[3]->ToNumber(Nan::GetCurrentContext()).ToLocalChecked()->Value(),
info[4]->ToNumber(Nan::GetCurrentContext()).ToLocalChecked()->Value(),
info[5]->ToNumber(Nan::GetCurrentContext()).ToLocalChecked()->Value()
);
break;
}
info.GetReturnValue().Set(jsVec);
}
2 changes: 2 additions & 0 deletions cc/core/Vec.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#include "Vec2.h"
#include "Vec3.h"
#include "Vec4.h"
#include "Vec6.h"

#ifndef __FF_VEC_H__
#define __FF_VEC_H__
Expand All @@ -14,6 +15,7 @@ class Vec : public Nan::ObjectWrap {
static NAN_METHOD(NewVec2);
static NAN_METHOD(NewVec3);
static NAN_METHOD(NewVec4);
static NAN_METHOD(NewVec6);

static Nan::Persistent<v8::FunctionTemplate> constructor;
};
Expand Down
50 changes: 50 additions & 0 deletions cc/core/Vec6.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
#include <opencv2/core.hpp>
#include "coreUtils.h"
#include "NativeNodeUtils.h"
#include "macros.h"

#ifndef __FF_VEC6_H__
#define __FF_VEC6_H__

class Vec6 : public FF::ObjectWrap<Vec6, cv::Vec6d> {
public:
static Nan::Persistent<v8::FunctionTemplate> constructor;

static const char* getClassName() {
return "Vec6";
}

static NAN_METHOD(New) {
Vec6* self = new Vec6();
self->Wrap(info.Holder());
info.GetReturnValue().Set(info.Holder());
}

static void Init(v8::Local<v8::FunctionTemplate> ctor) {
FF_PROTO_SET_MATRIX_OPERATIONS(ctor);
}

FF_GETTER_CUSTOM(u, FF::DoubleConverter, self[0]);
FF_GETTER_CUSTOM(v, FF::DoubleConverter, self[1]);
FF_GETTER_CUSTOM(w, FF::DoubleConverter, self[2]);
FF_GETTER_CUSTOM(x, FF::DoubleConverter, self[3]);
FF_GETTER_CUSTOM(y, FF::DoubleConverter, self[4]);
FF_GETTER_CUSTOM(z, FF::DoubleConverter, self[5]);

FF_INIT_VEC6_OPERATIONS();
static NAN_METHOD(Dot) {
FF_OPERATOR_RET_SCALAR(&cv::Vec6d::dot, FF_APPLY_CLASS_FUNC, Vec6, "Dot");
}
static NAN_METHOD(Norm) {
info.GetReturnValue().Set(Nan::New(cv::norm(Vec6::unwrapSelf(info))));
}

static NAN_METHOD(At) {
FF::TryCatch tryCatch("Vec6::At");
FF_ASSERT_INDEX_RANGE(info[0]->ToInt32(Nan::GetCurrentContext()).ToLocalChecked()->Value(), 5, "Vec6");
cv::Vec6d vecSelf = Vec6::unwrapSelf(info);
info.GetReturnValue().Set(vecSelf[info[0]->ToInt32(Nan::GetCurrentContext()).ToLocalChecked()->Value()]);
}
};

#endif
7 changes: 7 additions & 0 deletions cc/core/core.cc
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,13 @@ NAN_METHOD(Core::Partition) {
}
numLabels = cv::partition(pts, labels, predicateFactory<Vec4, cv::Vec4d>(cb));
}
else if (Vec6::hasInstance(data0)) {
std::vector<cv::Vec6d> pts;
if (Vec6::ArrayConverter::arg(0, &pts, info)) {
return tryCatch.reThrow();
}
numLabels = cv::partition(pts, labels, predicateFactory<Vec6, cv::Vec6d>(cb));
}
else if (Mat::hasInstance(data0)) {
std::vector<cv::Mat> mats;
if (Mat::ArrayConverter::arg(0, &mats, info)) {
Expand Down
1 change: 1 addition & 0 deletions cc/core/coreUtils.h
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,7 @@
#define FF_INIT_VEC2_OPERATIONS() FF_INIT_MATRIX_OPERATIONS(Vec2);
#define FF_INIT_VEC3_OPERATIONS() FF_INIT_MATRIX_OPERATIONS(Vec3);
#define FF_INIT_VEC4_OPERATIONS() FF_INIT_MATRIX_OPERATIONS(Vec4);
#define FF_INIT_VEC6_OPERATIONS() FF_INIT_MATRIX_OPERATIONS(Vec6);

namespace FF {
template<int cn>
Expand Down
1 change: 1 addition & 0 deletions lib/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ export * from './typings/Vec.d';
export * from './typings/Vec2.d';
export * from './typings/Vec3.d';
export * from './typings/Vec4.d';
export * from './typings/Vec6.d';
export * from './typings/Point.d';
export * from './typings/Point2.d';
export * from './typings/Point3.d';
Expand Down
4 changes: 2 additions & 2 deletions lib/typings/Vec4.d.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { Vec } from './Vec.d';

export class Vec4 extends Vec {
readonly w: number;
readonly x: number;
readonly y: number;
readonly z: number;
readonly w: number;
constructor(x: number, y: number, z: number, w: number);
constructor(w: number, x: number, y: number, z: number);
}
11 changes: 11 additions & 0 deletions lib/typings/Vec6.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { Vec } from './Vec.d';

export class Vec6 extends Vec {
readonly u: number;
readonly v: number;
readonly w: number;
readonly x: number;
readonly y: number;
readonly z: number;
constructor(u: number, v: number, w: number, x: number, y: number, z: number);
}
2 changes: 2 additions & 0 deletions lib/typings/cv.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { Size } from './Size.d';
import { Vec2 } from './Vec2.d';
import { Vec3 } from './Vec3.d';
import { Vec4 } from './Vec4.d';
import { Vec6 } from './Vec6.d';
import { Point2 } from './Point2.d';
import { Point3 } from './Point3.d';
import { KeyPoint } from './KeyPoint.d';
Expand Down Expand Up @@ -139,6 +140,7 @@ export function partition(data: Point3[], predicate: (pt1: Point3, pt2: Point3)
export function partition(data: Vec2[], predicate: (vec1: Vec2, vec2: Vec2) => boolean): { labels: number[], numLabels: number };
export function partition(data: Vec3[], predicate: (vec1: Vec3, vec2: Vec3) => boolean): { labels: number[], numLabels: number };
export function partition(data: Vec4[], predicate: (vec1: Vec4, vec2: Vec4) => boolean): { labels: number[], numLabels: number };
export function partition(data: Vec6[], predicate: (vec1: Vec6, vec2: Vec6) => boolean): { labels: number[], numLabels: number };
export function partition(data: Mat[], predicate: (mat1: Mat, mat2: Mat) => boolean): { labels: number[], numLabels: number };
export function perspectiveTransform(mat: Mat, m: Mat): Mat;
export function perspectiveTransformAsync(mat: Mat, m: Mat): Promise<Mat>;
Expand Down
17 changes: 17 additions & 0 deletions test/tests/core/Vec/VecTests.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,23 @@ module.exports = function ({ cv, utils }) {
expect(vec4.at(3)).to.equal(30);
});
});

describe('Vec6', () => {
const vec6 = new cv.Vec(5, 10, 20, 30, 40, 50);
it('should throw index out of bounds', () => {
assertError(() => vec6.at(-1), 'Index out of bounds: Vec6 at index -1');
assertError(() => vec6.at(6), 'Index out of bounds: Vec6 at index 6');
});

it('should return values from indices', () => {
expect(vec6.at(0)).to.equal(5);
expect(vec6.at(1)).to.equal(10);
expect(vec6.at(2)).to.equal(20);
expect(vec6.at(3)).to.equal(30);
expect(vec6.at(4)).to.equal(40);
expect(vec6.at(5)).to.equal(50);
});
});
});

};
48 changes: 48 additions & 0 deletions test/tests/core/Vec/constructorTests.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@ module.exports = function ({ cv, utils }) {
assertError(() => new cv.Vec(0), 'expected arguments');
});

it('should throw for trying to insantiate invalid vec5', () => {
assertError(() => new cv.Vec(5, 10, 20, 30, 40), 'Vec::New - expected arguments (u, v), (w), x, y, (z)');
});

describe('Vec2', () => {
it('should have int positions', () => {
const x = 100;
Expand Down Expand Up @@ -107,5 +111,49 @@ module.exports = function ({ cv, utils }) {
assertPropsWithValue(new cv.Vec(w, x, y, z))({ w, x, y, z });
});
});

describe('Vec6', () => {
it('should have int positions', () => {
const u = 50;
const v = 100;
const w = 200;
const x = 300;
const y = 400;
const z = 500;
assertPropsWithValue(new cv.Vec(u, v, w, x, y, z))({ u, v, w, x, y, z });
});

it('should have double positions', () => {
const u = 50.99;
const v = 100.12345;
const w = 200.89764;
const x = 300.034;
const y = 400.254;
const z = 500.543;
assertPropsWithValue(new cv.Vec(u, v, w, x, y, z))({ u, v, w, x, y, z });
});

it('should have negative int positions', () => {
it('should have int positions', () => {
const u = -50;
const v = -100;
const w = -200;
const x = -300;
const y = -400;
const z = -500;
assertPropsWithValue(new cv.Vec(u, v, w, x, y, z))({ u, v, w, x, y, z });
});
});

it('should have negative double positions', () => {
const u = -50.99;
const v = -100.12345;
const w = -200.89764;
const x = -300.034;
const y = -400.254;
const z = -500.543;
assertPropsWithValue(new cv.Vec(u, v, w, x, y, z))({ u, v, w, x, y, z });
});
});
});
};
Loading

0 comments on commit 4fb9333

Please sign in to comment.