Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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::w_getter);
Nan::SetAccessor(vec6Ctor->InstanceTemplate(), Nan::New("v").ToLocalChecked(), Vec6::w_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()
);

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wanted to rewrite this using macros to minimise the duplication but I'm too unfamiliar in cpp land to do it correctly, so this is the alternative I came up with.

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No worries, no need to introduce macros here :)

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
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
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(vec4.at(0)).to.equal(5);
expect(vec4.at(1)).to.equal(10);
expect(vec4.at(2)).to.equal(20);
expect(vec4.at(3)).to.equal(30);
expect(vec4.at(4)).to.equal(40);
expect(vec4.at(5)).to.equal(50);
});
});
});

};
5 changes: 5 additions & 0 deletions test/tests/core/Vec/constructorTests.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,11 @@ 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