-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathjs_contour.hpp
278 lines (216 loc) · 6.93 KB
/
js_contour.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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
#ifndef JS_CONTOUR_HPP
#define JS_CONTOUR_HPP
#include "geometry.hpp"
#include "js_alloc.hpp"
#include "js_point.hpp"
#include "js_array.hpp"
#include "jsbindings.hpp"
#include <quickjs.h>
#include <cstdint>
#include <new>
#include <vector>
#include <cassert>
extern "C" {
extern thread_local JSValue contour_class, contour_proto;
extern thread_local JSClassID js_contour_class_id;
JSValue js_contour_create(JSContext* ctx, JSValueConst proto);
void js_contour_finalizer(JSRuntime* rt, JSValue val);
JSValue js_contour_to_string(JSContext*, JSValueConst this_val, int argc, JSValueConst argv[]);
int js_contour_init(JSContext*, JSModuleDef*);
JSModuleDef* js_init_module_contour(JSContext*, const char*);
void js_contour_constructor(JSContext* ctx, JSValue parent, const char* name);
JSContourData<double>* js_contour_data2(JSContext* ctx, JSValueConst val);
JSContourData<double>* js_contour_data(JSValueConst val);
};
JSValue js_contour_move(JSContext* ctx, JSContourData<double>&& points);
extern "C" int js_contour_init(JSContext*, JSModuleDef*);
template<typename T, typename U>
static inline size_t
contour_copy(const JSContourData<T>& src, JSContourData<U>& dst) {
dst.resize(src.size());
std::copy(src.begin(), src.end(), dst.begin());
return src.size();
}
/*template<typename T, typename U = double>
static inline JSContourData<T>
contour_convert(const JSContourData<U>& src) {
JSContourData<T> dst;
dst.resize(src.size());
std::copy(src.begin(), src.end(), dst.begin());
return dst;
}*/
template<typename T>
static inline cv::Mat
contour_getmat(JSContourData<T>& contour) {
JSMatDimensions size;
int t = point_traits<T>::type;
size.rows = 1;
size.cols = contour.size();
return cv::Mat(cv::Size(size), t, static_cast<void*>(contour.data()));
}
template<typename T>
static inline bool
contour_adjacent(const JSContourData<T>& contour, const JSPointData<T>& point) {
for(const JSPointData<T>& pt : contour)
if(point_adjacent<int>(pt, point))
return true;
return false;
}
template<typename T>
static inline bool
contour_adjacent(const JSContourData<T>& contour, const JSContourData<T>& other) {
for(const JSPointData<T>& pt : contour)
if(contour_adjacent(other, pt))
return true;
return false;
}
template<typename T>
static inline bool
contour_intersect(const JSContourData<T>& a, const JSContourData<T>& b, std::array<ssize_t, 2>* indexes, JSPointData<T>* intersection) {
const auto *ita = a.data(), *itb = b.data();
const auto *aend = ita + a.size() - 1, *bend = itb + b.size() - 1;
/*while(ita != aend) {
while(itb != bend) {
if(reinterpret_cast<const Line<T>*>(ita)->intersect(*reinterpret_cast<const Line<T>*>(itb), intersection)) {
if(indexes)
(*indexes) = std::array<ssize_t, 2>{ita - a.data(), itb - b.data()};
return true;
}
++itb;
}
++ita;
}*/
const auto it = std::find_first_of(ita, aend, itb, bend, [intersection](const JSPointData<T>& a, const JSPointData<T>& b) -> bool {
const auto& la = *reinterpret_cast<const Line<T>*>(&a);
const auto& lb = *reinterpret_cast<const Line<T>*>(&b);
return la.intersect(lb, intersection);
});
if(it != aend) {
if(indexes)
(*indexes)[0] = it - a.data();
return true;
}
return false;
}
template<class T>
static inline JSValue
js_contour_new(JSContext* ctx, JSValueConst proto, const JSContourData<T>& points) {
JSValue ret = js_contour_create(ctx, proto);
JSContourData<double>* contour = js_contour_data(ret);
contour_copy(points, *contour);
return ret;
}
template<class T>
static inline JSValue
js_contour_new(JSContext* ctx, const JSContourData<T>& points) {
return js_contour_new(ctx, contour_proto, points);
}
template<typename T = double>
static inline int
js_contour_read(JSContext* ctx, JSValueConst contour, JSContourData<T>* out) {
int ret = 0;
JSContourData<double>* c;
if((c = js_contour_data(contour))) {
ret = contour_copy(*c, *out);
} else if(js_is_iterable(ctx, contour)) {
JSValue iter = js_iterator_new(ctx, contour);
IteratorValue result;
JSPointData<double> pt;
for(uint32_t i = 0;; ++i) {
result = js_iterator_next(ctx, iter);
if(result.done)
break;
if(js_point_read(ctx, result.value, &pt)) {
out->push_back(pt);
} else {
JS_FreeValue(ctx, result.value);
break;
}
JS_FreeValue(ctx, result.value);
}
JS_FreeValue(ctx, iter);
ret = out->size();
}
return ret;
}
static inline JSContourData<double>
js_contour_get(JSContext* ctx, JSValueConst contour) {
JSContourData<double> r = {};
js_contour_read(ctx, contour, &r);
return r;
}
template<class T>
void
js_contours_copy(JSContext* ctx, JSValueConst arr, const JSContoursData<T>& contours) {
uint32_t i, size = contours.size();
js_array_clear(ctx, arr);
for(i = 0; i < size; i++) {
JSValue contour = js_contour_new(ctx, contour_proto, contours[i]);
JS_SetPropertyUint32(ctx, arr, i, contour);
}
}
template<class T>
JSValue
js_contours_new(JSContext* ctx, const JSContoursData<T>& contours) {
uint32_t i, size = contours.size();
JSValue arr = JS_NewArray(ctx);
for(i = 0; i < size; i++) {
JSValue contour = js_contour_new(ctx, contour_proto, contours[i]);
JS_SetPropertyUint32(ctx, arr, i, contour);
}
return arr;
}
template<class T> class js_array<JSContourData<T>> {
public:
typedef JSContoursData<T> contours_type;
typedef JSContourData<T> contour_type;
typedef JSPointData<T> point_type;
static int64_t
to_vector(JSContext* ctx, JSValueConst arr, contours_type& 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++) {
JSContourData<double>* ptr;
contour_type contour;
JSValue item = JS_GetPropertyUint32(ctx, arr, (uint32_t)i);
if((ptr = js_contour_data(item))) {
for(const auto& point : *ptr)
contour.emplace_back(point.x, point.y);
} else {
js_array_to(ctx, item, contour);
}
out.push_back(contour);
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) {
JSValue item = js_contour_new(ctx, contour_proto, *it);
JS_SetPropertyUint32(ctx, arr, i, item);
++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;
}
template<class Container>
static JSValue
from(JSContext* ctx, const Container& in) {
return from_sequence<typename Container::const_iterator>(ctx, in.begin(), in.end());
}
};
#endif /* defined(JS_CONTOUR_HPP) */