-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathjs_keypoint.hpp
74 lines (61 loc) · 1.87 KB
/
js_keypoint.hpp
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
73
74
#ifndef JS_KEYPOINT_HPP
#define JS_KEYPOINT_HPP
#include "jsbindings.hpp"
#include <quickjs.h>
#include <stddef.h>
#include <cstdint>
#include <opencv2/core/types.hpp>
#include <vector>
typedef cv::KeyPoint JSKeyPointData;
extern "C" {
extern thread_local JSValue keypoint_proto, keypoint_class;
extern thread_local JSClassID js_keypoint_class_id;
JSKeyPointData* js_keypoint_data2(JSContext*, JSValueConst val);
JSKeyPointData* js_keypoint_data(JSValueConst val);
}
extern "C" int js_keypoint_init(JSContext*, JSModuleDef*);
JSValue js_keypoint_new(JSContext* ctx, const JSKeyPointData& keypoint);
extern "C" int js_keypoint_init(JSContext*, JSModuleDef*);
template<> class js_array<JSKeyPointData> {
public:
static int64_t
to_vector(JSContext* ctx, JSValueConst arr, std::vector<JSKeyPointData>& out) {
int64_t i, n;
JSValue len;
if(!js_is_array(ctx, arr))
return -1;
len = JS_GetPropertyStr(ctx, arr, "length");
JS_ToInt64(ctx, &n, len);
out.reserve(out.size() + n);
for(i = 0; i < n; i++) {
JSKeyPointData* kp;
JSValue item = JS_GetPropertyUint32(ctx, arr, (uint32_t)i);
if(!(kp = js_keypoint_data2(ctx, item))) {
JS_FreeValue(ctx, item);
out.clear();
return -1;
}
out.push_back(*kp);
JS_FreeValue(ctx, item);
}
return n;
}
template<class Iterator>
static size_t
copy_sequence(JSContext* ctx, JSValueConst arr, const Iterator& start, const Iterator& end) {
size_t i = 0;
for(Iterator it = start; it != end; ++it) {
JS_SetPropertyUint32(ctx, arr, i, js_keypoint_new(ctx, *it));
++i;
}
return i;
}
template<class Iterator>
static JSValue
from_sequence(JSContext* ctx, const Iterator& start, const Iterator& end) {
JSValue arr = JS_NewArray(ctx);
copy_sequence(ctx, arr, start, end);
return arr;
}
};
#endif /* defined(JS_KEYPOINT_HPP) */