Skip to content

Commit 4fb9333

Browse files
Merge pull request #583 from thekemkid/feat/vec5
Add base support for vec6
2 parents 8857337 + 816fd6a commit 4fb9333

File tree

13 files changed

+287
-23
lines changed

13 files changed

+287
-23
lines changed

cc/core/Vec.cc

Lines changed: 54 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
Nan::Persistent<v8::FunctionTemplate> Vec2::constructor;
44
Nan::Persistent<v8::FunctionTemplate> Vec3::constructor;
55
Nan::Persistent<v8::FunctionTemplate> Vec4::constructor;
6+
Nan::Persistent<v8::FunctionTemplate> Vec6::constructor;
67

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

43+
v8::Local<v8::FunctionTemplate> vec6Ctor = Nan::New<v8::FunctionTemplate>(Vec6::New);
44+
Vec6::constructor.Reset(vec6Ctor);
45+
vec6Ctor->InstanceTemplate()->SetInternalFieldCount(1);
46+
vec6Ctor->SetClassName(Nan::New("Vec6").ToLocalChecked());
47+
Nan::SetAccessor(vec6Ctor->InstanceTemplate(), Nan::New("u").ToLocalChecked(), Vec6::u_getter);
48+
Nan::SetAccessor(vec6Ctor->InstanceTemplate(), Nan::New("v").ToLocalChecked(), Vec6::v_getter);
49+
Nan::SetAccessor(vec6Ctor->InstanceTemplate(), Nan::New("w").ToLocalChecked(), Vec6::w_getter);
50+
Nan::SetAccessor(vec6Ctor->InstanceTemplate(), Nan::New("x").ToLocalChecked(), Vec6::x_getter);
51+
Nan::SetAccessor(vec6Ctor->InstanceTemplate(), Nan::New("y").ToLocalChecked(), Vec6::y_getter);
52+
Nan::SetAccessor(vec6Ctor->InstanceTemplate(), Nan::New("z").ToLocalChecked(), Vec6::z_getter);
53+
Nan::SetPrototypeMethod(vec6Ctor, "at", Vec6::At);
54+
Nan::SetPrototypeMethod(vec6Ctor, "norm", Vec6::Norm);
55+
Vec6::Init(vec6Ctor);
56+
4257
v8::Local<v8::FunctionTemplate> ctor = Nan::New<v8::FunctionTemplate>(Vec::New);
4358
ctor->InstanceTemplate()->SetInternalFieldCount(1);
4459
ctor->SetClassName(Nan::New("Vec").ToLocalChecked());
4560
Nan::Set(target,Nan::New("Vec").ToLocalChecked(), FF::getFunction(ctor));
4661
Nan::Set(target,Nan::New("Vec2").ToLocalChecked(), FF::getFunction(ctor));
4762
Nan::Set(target,Nan::New("Vec3").ToLocalChecked(), FF::getFunction(ctor));
4863
Nan::Set(target,Nan::New("Vec4").ToLocalChecked(), FF::getFunction(ctor));
64+
Nan::Set(target,Nan::New("Vec6").ToLocalChecked(), FF::getFunction(ctor));
4965
};
5066

5167
NAN_METHOD(Vec::New) {
5268
FF::TryCatch tryCatch("Vec::New");
5369
FF_ASSERT_CONSTRUCT_CALL();
54-
if (info.Length() < 2) {
55-
return tryCatch.throwError("Vec::New - expected arguments (w), x, y, (z)");
70+
if (info.Length() < 2 || info.Length() > 6 || info.Length() == 5) {
71+
return tryCatch.throwError("Vec::New - expected arguments (u, v), (w), x, y, (z)");
5672
}
5773
v8::Local<v8::Object> jsVec;
58-
if (info.Length() == 4) {
59-
jsVec = FF::newInstance(Nan::New(Vec4::constructor));
60-
Nan::ObjectWrap::Unwrap<Vec4>(jsVec)->self = cv::Vec4d(
61-
info[0]->ToNumber(Nan::GetCurrentContext()).ToLocalChecked()->Value(),
62-
info[1]->ToNumber(Nan::GetCurrentContext()).ToLocalChecked()->Value(),
63-
info[2]->ToNumber(Nan::GetCurrentContext()).ToLocalChecked()->Value(),
64-
info[3]->ToNumber(Nan::GetCurrentContext()).ToLocalChecked()->Value()
65-
);
66-
} else {
67-
double x = info[0]->ToNumber(Nan::GetCurrentContext()).ToLocalChecked()->Value();
68-
double y = info[1]->ToNumber(Nan::GetCurrentContext()).ToLocalChecked()->Value();
69-
if (info.Length() == 3) {
70-
double z = info[2]->ToNumber(Nan::GetCurrentContext()).ToLocalChecked()->Value();
71-
jsVec = FF::newInstance(Nan::New(Vec3::constructor));
72-
Nan::ObjectWrap::Unwrap<Vec3>(jsVec)->self = cv::Vec3d(x, y, z);
73-
}
74-
else {
74+
75+
switch(info.Length()) {
76+
case 2:
7577
jsVec = FF::newInstance(Nan::New(Vec2::constructor));
76-
Nan::ObjectWrap::Unwrap<Vec2>(jsVec)->self = cv::Vec2d(x, y);
77-
}
78+
Nan::ObjectWrap::Unwrap<Vec2>(jsVec)->self = cv::Vec2d(
79+
info[0]->ToNumber(Nan::GetCurrentContext()).ToLocalChecked()->Value(),
80+
info[1]->ToNumber(Nan::GetCurrentContext()).ToLocalChecked()->Value()
81+
);
82+
break;
83+
case 3:
84+
jsVec = FF::newInstance(Nan::New(Vec3::constructor));
85+
Nan::ObjectWrap::Unwrap<Vec3>(jsVec)->self = cv::Vec3d(
86+
info[0]->ToNumber(Nan::GetCurrentContext()).ToLocalChecked()->Value(),
87+
info[1]->ToNumber(Nan::GetCurrentContext()).ToLocalChecked()->Value(),
88+
info[2]->ToNumber(Nan::GetCurrentContext()).ToLocalChecked()->Value()
89+
);
90+
break;
91+
case 4:
92+
jsVec = FF::newInstance(Nan::New(Vec4::constructor));
93+
Nan::ObjectWrap::Unwrap<Vec4>(jsVec)->self = cv::Vec4d(
94+
info[0]->ToNumber(Nan::GetCurrentContext()).ToLocalChecked()->Value(),
95+
info[1]->ToNumber(Nan::GetCurrentContext()).ToLocalChecked()->Value(),
96+
info[2]->ToNumber(Nan::GetCurrentContext()).ToLocalChecked()->Value(),
97+
info[3]->ToNumber(Nan::GetCurrentContext()).ToLocalChecked()->Value()
98+
);
99+
break;
100+
case 6:
101+
jsVec = FF::newInstance(Nan::New(Vec6::constructor));
102+
Nan::ObjectWrap::Unwrap<Vec6>(jsVec)->self = cv::Vec6d(
103+
info[0]->ToNumber(Nan::GetCurrentContext()).ToLocalChecked()->Value(),
104+
info[1]->ToNumber(Nan::GetCurrentContext()).ToLocalChecked()->Value(),
105+
info[2]->ToNumber(Nan::GetCurrentContext()).ToLocalChecked()->Value(),
106+
info[3]->ToNumber(Nan::GetCurrentContext()).ToLocalChecked()->Value(),
107+
info[4]->ToNumber(Nan::GetCurrentContext()).ToLocalChecked()->Value(),
108+
info[5]->ToNumber(Nan::GetCurrentContext()).ToLocalChecked()->Value()
109+
);
110+
break;
78111
}
79112
info.GetReturnValue().Set(jsVec);
80113
}

cc/core/Vec.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
#include "Vec2.h"
44
#include "Vec3.h"
55
#include "Vec4.h"
6+
#include "Vec6.h"
67

78
#ifndef __FF_VEC_H__
89
#define __FF_VEC_H__
@@ -14,6 +15,7 @@ class Vec : public Nan::ObjectWrap {
1415
static NAN_METHOD(NewVec2);
1516
static NAN_METHOD(NewVec3);
1617
static NAN_METHOD(NewVec4);
18+
static NAN_METHOD(NewVec6);
1719

1820
static Nan::Persistent<v8::FunctionTemplate> constructor;
1921
};

cc/core/Vec6.h

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
#include <opencv2/core.hpp>
2+
#include "coreUtils.h"
3+
#include "NativeNodeUtils.h"
4+
#include "macros.h"
5+
6+
#ifndef __FF_VEC6_H__
7+
#define __FF_VEC6_H__
8+
9+
class Vec6 : public FF::ObjectWrap<Vec6, cv::Vec6d> {
10+
public:
11+
static Nan::Persistent<v8::FunctionTemplate> constructor;
12+
13+
static const char* getClassName() {
14+
return "Vec6";
15+
}
16+
17+
static NAN_METHOD(New) {
18+
Vec6* self = new Vec6();
19+
self->Wrap(info.Holder());
20+
info.GetReturnValue().Set(info.Holder());
21+
}
22+
23+
static void Init(v8::Local<v8::FunctionTemplate> ctor) {
24+
FF_PROTO_SET_MATRIX_OPERATIONS(ctor);
25+
}
26+
27+
FF_GETTER_CUSTOM(u, FF::DoubleConverter, self[0]);
28+
FF_GETTER_CUSTOM(v, FF::DoubleConverter, self[1]);
29+
FF_GETTER_CUSTOM(w, FF::DoubleConverter, self[2]);
30+
FF_GETTER_CUSTOM(x, FF::DoubleConverter, self[3]);
31+
FF_GETTER_CUSTOM(y, FF::DoubleConverter, self[4]);
32+
FF_GETTER_CUSTOM(z, FF::DoubleConverter, self[5]);
33+
34+
FF_INIT_VEC6_OPERATIONS();
35+
static NAN_METHOD(Dot) {
36+
FF_OPERATOR_RET_SCALAR(&cv::Vec6d::dot, FF_APPLY_CLASS_FUNC, Vec6, "Dot");
37+
}
38+
static NAN_METHOD(Norm) {
39+
info.GetReturnValue().Set(Nan::New(cv::norm(Vec6::unwrapSelf(info))));
40+
}
41+
42+
static NAN_METHOD(At) {
43+
FF::TryCatch tryCatch("Vec6::At");
44+
FF_ASSERT_INDEX_RANGE(info[0]->ToInt32(Nan::GetCurrentContext()).ToLocalChecked()->Value(), 5, "Vec6");
45+
cv::Vec6d vecSelf = Vec6::unwrapSelf(info);
46+
info.GetReturnValue().Set(vecSelf[info[0]->ToInt32(Nan::GetCurrentContext()).ToLocalChecked()->Value()]);
47+
}
48+
};
49+
50+
#endif

cc/core/core.cc

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,13 @@ NAN_METHOD(Core::Partition) {
127127
}
128128
numLabels = cv::partition(pts, labels, predicateFactory<Vec4, cv::Vec4d>(cb));
129129
}
130+
else if (Vec6::hasInstance(data0)) {
131+
std::vector<cv::Vec6d> pts;
132+
if (Vec6::ArrayConverter::arg(0, &pts, info)) {
133+
return tryCatch.reThrow();
134+
}
135+
numLabels = cv::partition(pts, labels, predicateFactory<Vec6, cv::Vec6d>(cb));
136+
}
130137
else if (Mat::hasInstance(data0)) {
131138
std::vector<cv::Mat> mats;
132139
if (Mat::ArrayConverter::arg(0, &mats, info)) {

cc/core/coreUtils.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,7 @@
164164
#define FF_INIT_VEC2_OPERATIONS() FF_INIT_MATRIX_OPERATIONS(Vec2);
165165
#define FF_INIT_VEC3_OPERATIONS() FF_INIT_MATRIX_OPERATIONS(Vec3);
166166
#define FF_INIT_VEC4_OPERATIONS() FF_INIT_MATRIX_OPERATIONS(Vec4);
167+
#define FF_INIT_VEC6_OPERATIONS() FF_INIT_MATRIX_OPERATIONS(Vec6);
167168

168169
namespace FF {
169170
template<int cn>

lib/index.d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ export * from './typings/Vec.d';
66
export * from './typings/Vec2.d';
77
export * from './typings/Vec3.d';
88
export * from './typings/Vec4.d';
9+
export * from './typings/Vec6.d';
910
export * from './typings/Point.d';
1011
export * from './typings/Point2.d';
1112
export * from './typings/Point3.d';

lib/typings/Vec4.d.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
import { Vec } from './Vec.d';
22

33
export class Vec4 extends Vec {
4+
readonly w: number;
45
readonly x: number;
56
readonly y: number;
67
readonly z: number;
7-
readonly w: number;
8-
constructor(x: number, y: number, z: number, w: number);
8+
constructor(w: number, x: number, y: number, z: number);
99
}

lib/typings/Vec6.d.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import { Vec } from './Vec.d';
2+
3+
export class Vec6 extends Vec {
4+
readonly u: number;
5+
readonly v: number;
6+
readonly w: number;
7+
readonly x: number;
8+
readonly y: number;
9+
readonly z: number;
10+
constructor(u: number, v: number, w: number, x: number, y: number, z: number);
11+
}

lib/typings/cv.d.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { Size } from './Size.d';
33
import { Vec2 } from './Vec2.d';
44
import { Vec3 } from './Vec3.d';
55
import { Vec4 } from './Vec4.d';
6+
import { Vec6 } from './Vec6.d';
67
import { Point2 } from './Point2.d';
78
import { Point3 } from './Point3.d';
89
import { KeyPoint } from './KeyPoint.d';
@@ -139,6 +140,7 @@ export function partition(data: Point3[], predicate: (pt1: Point3, pt2: Point3)
139140
export function partition(data: Vec2[], predicate: (vec1: Vec2, vec2: Vec2) => boolean): { labels: number[], numLabels: number };
140141
export function partition(data: Vec3[], predicate: (vec1: Vec3, vec2: Vec3) => boolean): { labels: number[], numLabels: number };
141142
export function partition(data: Vec4[], predicate: (vec1: Vec4, vec2: Vec4) => boolean): { labels: number[], numLabels: number };
143+
export function partition(data: Vec6[], predicate: (vec1: Vec6, vec2: Vec6) => boolean): { labels: number[], numLabels: number };
142144
export function partition(data: Mat[], predicate: (mat1: Mat, mat2: Mat) => boolean): { labels: number[], numLabels: number };
143145
export function perspectiveTransform(mat: Mat, m: Mat): Mat;
144146
export function perspectiveTransformAsync(mat: Mat, m: Mat): Promise<Mat>;

test/tests/core/Vec/VecTests.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,23 @@ module.exports = function ({ cv, utils }) {
4545
expect(vec4.at(3)).to.equal(30);
4646
});
4747
});
48+
49+
describe('Vec6', () => {
50+
const vec6 = new cv.Vec(5, 10, 20, 30, 40, 50);
51+
it('should throw index out of bounds', () => {
52+
assertError(() => vec6.at(-1), 'Index out of bounds: Vec6 at index -1');
53+
assertError(() => vec6.at(6), 'Index out of bounds: Vec6 at index 6');
54+
});
55+
56+
it('should return values from indices', () => {
57+
expect(vec6.at(0)).to.equal(5);
58+
expect(vec6.at(1)).to.equal(10);
59+
expect(vec6.at(2)).to.equal(20);
60+
expect(vec6.at(3)).to.equal(30);
61+
expect(vec6.at(4)).to.equal(40);
62+
expect(vec6.at(5)).to.equal(50);
63+
});
64+
});
4865
});
4966

5067
};

0 commit comments

Comments
 (0)