-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathbinary.cpp
More file actions
468 lines (435 loc) · 14.9 KB
/
binary.cpp
File metadata and controls
468 lines (435 loc) · 14.9 KB
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
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
#include <string.h>
#include <assert.h>
#include <sys/types.h>
#include <string>
#include <fstream>
#include <algorithm>
#include <iterator>
#include <limits>
#include <mujs.h>
#include "mytypes.h"
#include "js-support.h"
#include "xerror.h"
#include "Vec3.h"
#include "Mat3.h"
const char *TAG_Binary = "Binary";
//
// helpers
//
template<typename T>
inline void Append(Binary &b, T val) {
std::copy((Binary::value_type*)&val, (Binary::value_type*)((&val)+1), std::back_inserter(b));
}
template<typename T>
inline void Put(js_State *J, Binary &b, unsigned off, T val) {
if (off+sizeof(T) > b.size())
JS_ERROR("Binary.put: offset=" << off << " is out-of-bounds: arraySize=" << b.size() << ", valueSize=" << sizeof(T))
*(T*)&b[off] = val;
}
template<typename T>
inline T Get(js_State *J, Binary &b, unsigned off) {
if (off+sizeof(T) > b.size())
JS_ERROR("Binary.get: offset=" << off << " is out-of-bounds: arraySize=" << b.size() << ", valueSize=" << sizeof(T))
return *(T*)&b[off];
}
template<typename Area>
struct ComparatorInc {
bool operator()(Area &a, Area &b) const {return a.value < b.value;}
};
template<typename Area>
struct ComparatorDecr {
bool operator()(Area &a, Area &b) const {return a.value > b.value;}
};
template<typename Area>
inline void SortOne(Binary &b, size_t sz, bool orderInc) {
if (orderInc)
std::sort((Area*)&b[0], (Area*)&b[sz], ComparatorInc<Area>());
else
std::sort((Area*)&b[0], (Area*)&b[sz], ComparatorDecr<Area>());
}
template<unsigned SzM1, typename T, unsigned SzM2>
struct Area {
#pragma pack(push, 1)
uint8_t m1[SzM1];
T value;
uint8_t m2[SzM2];
#pragma pack(pop)
}; // Area
template<typename T>
inline void Sort(js_State *J, Binary &b, unsigned areaSize, unsigned fldOffset, bool orderInc) {
auto sz = b.size();
if (sz % areaSize != 0)
JS_ERROR("Binary.sortAteasXxx: size=" << sz << " is not a multiple of areaSize=" << areaSize)
if (fldOffset + sizeof(T) > areaSize)
JS_ERROR("Binary.sortAteasXxx: fldOffset=" << fldOffset << " + sizeof(T)=" << sizeof(T) << " doesn't fit in the area, areaSize=" << areaSize)
switch (areaSize) {
case sizeof(T): SortOne<Area<0,T,0>>(b, sz, orderInc); return;
case sizeof(T)+1: {
switch (fldOffset) {
case 0: SortOne<Area<0,T,1>>(b, sz, orderInc); return;
case 1: SortOne<Area<1,T,0>>(b, sz, orderInc); return;
}
break;
} case sizeof(T)+2: {
switch (fldOffset) {
case 0: SortOne<Area<0,T,2>>(b, sz, orderInc); return;
case 1: SortOne<Area<1,T,1>>(b, sz, orderInc); return;
case 2: SortOne<Area<2,T,0>>(b, sz, orderInc); return;
}
break;
} case sizeof(T)+3: {
switch (fldOffset) {
case 0: SortOne<Area<0,T,3>>(b, sz, orderInc); return;
case 1: SortOne<Area<1,T,2>>(b, sz, orderInc); return;
case 2: SortOne<Area<2,T,1>>(b, sz, orderInc); return;
case 3: SortOne<Area<3,T,0>>(b, sz, orderInc); return;
}
break;
} case sizeof(T)+4: {
switch (fldOffset) {
case 0: SortOne<Area<0,T,4>>(b, sz, orderInc); return;
case 1: SortOne<Area<1,T,3>>(b, sz, orderInc); return;
case 2: SortOne<Area<2,T,2>>(b, sz, orderInc); return;
case 3: SortOne<Area<3,T,1>>(b, sz, orderInc); return;
case 4: SortOne<Area<4,T,0>>(b, sz, orderInc); return;
}
break;
} case sizeof(T)+8: {
switch (fldOffset) {
case 0: SortOne<Area<0,T,8>>(b, sz, orderInc); return;
case 1: SortOne<Area<1,T,7>>(b, sz, orderInc); return;
case 2: SortOne<Area<2,T,6>>(b, sz, orderInc); return;
case 3: SortOne<Area<3,T,5>>(b, sz, orderInc); return;
case 4: SortOne<Area<4,T,4>>(b, sz, orderInc); return;
case 5: SortOne<Area<5,T,3>>(b, sz, orderInc); return;
case 6: SortOne<Area<6,T,2>>(b, sz, orderInc); return;
case 7: SortOne<Area<7,T,1>>(b, sz, orderInc); return;
case 8: SortOne<Area<8,T,0>>(b, sz, orderInc); return;
}
break;
}}
ERROR("Binary::sort: unsupported sizes fldOffset=" << fldOffset << " and areaSize=" << areaSize)
}
template<typename T>
std::vector<std::pair<T,T>> BBox(js_State *J, const Binary &b, unsigned ndims, unsigned leading, unsigned trailing) {
auto sz = b.size();
if (sz % (leading + ndims*sizeof(T) + trailing) != 0)
JS_ERROR("Binary.bboxXx: size=" << sz << " is not a multiple of areaSize=" << (leading + ndims*sizeof(T) + trailing))
// initialize bb
std::vector<std::pair<T,T>> bb;
bb.reserve(ndims);
while (bb.size() < ndims)
bb.push_back(std::pair<T,T>(std::numeric_limits<T>::max(), std::numeric_limits<T>::min()));
for (auto it = b.begin()+leading, ite = b.end(); it < ite;) {
for (int d = 0; d < ndims; d++) {
T c = *(T*)&(*it);
if (c < bb[d].first)
bb[d].first = c;
if (c > bb[d].second)
bb[d].second = c;
it += sizeof(T);
}
it += trailing;
}
return bb;
}
template<typename Float>
Binary* CreateMulMat3PlusVec3(js_State *J, const Binary &b, unsigned leading, unsigned trailing, const TMat3<Float> &m, const TVec3<Float> &v) {
typedef TVec3<Float> V;
auto sz = b.size();
if (sz % (leading + sizeof(V) + trailing) != 0)
JS_ERROR("Binary.createMulMat3PlusVec3Xx: size=" << sz << " is not a multiple of areaSize=" << (leading + sizeof(V) + trailing))
std::unique_ptr<Binary> output(new Binary);
output->reserve(sz);
for (auto it = b.begin(), ite = b.end(); it < ite;) {
// leading area before coords (FIXME often zero-length, so this is a waste, have a template param for that?)
for (unsigned i = 0; i < leading; i++)
output->push_back(*it++);
// matrix operation
union {const V *vec; const Float *f;} src;
uint8_t res[sizeof(V)];
src.f = (Float*)&*it;
*(V*)&res[0] = m*(*src.vec) + v;
for (unsigned i = 0; i < sizeof(V); i++)
output->push_back(res[i]);
it += 3*sizeof(Float);
// trailing bytes
for (unsigned i = 0; i < trailing; i++)
output->push_back(*it++);
}
return output.release();
}
template<typename Float>
void MulScalarPlusVec3(js_State *J, Binary &b, unsigned leading, unsigned trailing, const TVec3<Float> &scalar, const TVec3<Float> &v) {
typedef TVec3<Float> V;
auto sz = b.size();
auto areaSize = leading + sizeof(V) + trailing;
if (sz % areaSize != 0)
JS_ERROR("Binary.bboxXx: size=" << sz << " is not a multiple of areaSize=" << areaSize)
for (auto it = b.begin() + leading, ite = b.end(); it < ite; it += areaSize) {
V *vec = (V*)&*it;
*vec = (*vec).scale(scalar) + v;
}
}
namespace JsBinding {
namespace JsBinary {
void xnewo(js_State *J, Binary *b) {
js_getglobal(J, TAG_Binary);
js_getproperty(J, -1, "prototype");
js_newuserdata(J, TAG_Binary, b, [](js_State *J, void *p) {
delete (Binary*)p;
});
}
void init(js_State *J) {
JsSupport::beginDefineClass(J, TAG_Binary, [](js_State *J) {
AssertNargsRange(0,1)
switch (GetNArgs()) {
case 0: { // create the empty binary
ReturnObj(new Binary);
break;
} case 1: { // create a binary with a string
auto b = new Binary;
auto str = GetArgStringCptr(1);
auto strLen = ::strlen(str);
b->resize(strLen);
::memcpy(&(*b)[0], str, strLen); // FIXME inefficient copying char* -> Binary, should do this in one step
ReturnObj(b);
break;
}}
});
{ // methods
ADD_METHOD_CPP(Binary, dupl, {
AssertNargs(0)
ReturnObj(new Binary(*GetArg(Binary, 0)));
}, 0)
ADD_METHOD_CPP(Binary, size, {
AssertNargs(0)
Return(J, (unsigned)GetArg(Binary, 0)->size());
}, 0)
ADD_METHOD_CPP(Binary, resize, {
AssertNargs(1)
GetArg(Binary, 0)->resize(GetArgUInt32(1));
ReturnVoid(J);
}, 1)
ADD_METHOD_CPP(Binary, clear, {
AssertNargs(0)
GetArg(Binary, 0)->clear();
ReturnVoid(J);
}, 0)
// appendXx methods
ADD_METHOD_CPP(Binary, append, {
AssertNargs(1)
auto b = GetArg(Binary, 0);
auto b1 = GetArg(Binary, 1);
b->insert(b->end(), b1->begin(), b1->end());
ReturnVoid(J);
}, 1)
ADD_METHOD_CPP(Binary, appendRange, { // CAVEAT doesn't check argument #2 (offBegin) and argument #3 (offEnd) for the range to be in the argument
AssertNargs(3)
auto b = GetArg(Binary, 0);
auto b1 = GetArg(Binary, 1);
b->insert(b->end(), b1->begin()+GetArgUInt32(2), b1->begin()+GetArgUInt32(3));
ReturnVoid(J);
}, 3)
ADD_METHOD_CPP(Binary, appendByte, { // appends the 'int' value as bytes
AssertNargs(1)
GetArg(Binary, 0)->push_back((uint8_t)GetArgUInt32(1));
ReturnVoid(J);
}, 1)
ADD_METHOD_CPP(Binary, appendString, {
AssertNargs(1)
auto b = GetArg(Binary, 0);
auto str = GetArgString(1);
b->insert(b->end(), str.begin(), str.end());
ReturnVoid(J);
}, 1)
ADD_METHOD_CPP(Binary, appendInt, { // appends the 'int' value as bytes
AssertNargs(1)
Append(*GetArg(Binary, 0), GetArgInt32(1));
ReturnVoid(J);
}, 1)
ADD_METHOD_CPP(Binary, appendUInt, { // appends the 'unsigned' value as bytes
AssertNargs(1)
Append(*GetArg(Binary, 0), GetArgUInt32(1));
ReturnVoid(J);
}, 1)
ADD_METHOD_CPP(Binary, appendFloat4, { // appends the 'float' value as bytes
AssertNargs(1)
Append(*GetArg(Binary, 0), (float)GetArgFloat(1));
ReturnVoid(J);
}, 1)
ADD_METHOD_CPP(Binary, appendFloat8, { // appends the 'double' value as bytes
AssertNargs(1)
Append(*GetArg(Binary, 0), (double)GetArgFloat(1));
ReturnVoid(J);
}, 1)
// putXx methods
ADD_METHOD_CPP(Binary, putByte, {
AssertNargs(2)
Put(J, *GetArg(Binary, 0), GetArgUInt32(1), (uint8_t)GetArgUInt32(2));
ReturnVoid(J);
}, 2)
ADD_METHOD_CPP(Binary, putInt, {
AssertNargs(2)
Put(J, *GetArg(Binary, 0), GetArgUInt32(1), GetArgInt32(2));
ReturnVoid(J);
}, 2)
ADD_METHOD_CPP(Binary, putUInt, {
AssertNargs(2)
Put(J, *GetArg(Binary, 0), GetArgUInt32(1), GetArgUInt32(2));
ReturnVoid(J);
}, 2)
ADD_METHOD_CPP(Binary, putFloat4, {
AssertNargs(2)
Put(J, *GetArg(Binary, 0), GetArgUInt32(1), (float)GetArgFloat(2));
ReturnVoid(J);
}, 2)
ADD_METHOD_CPP(Binary, putFloat8, {
AssertNargs(2)
Put(J, *GetArg(Binary, 0), GetArgUInt32(1), (double)GetArgFloat(2));
ReturnVoid(J);
}, 2)
// getXx methods
ADD_METHOD_CPP(Binary, getByte, {
AssertNargs(1)
Return(J, Get<uint8_t>(J, *GetArg(Binary, 0), GetArgUInt32(1)));
}, 1)
ADD_METHOD_CPP(Binary, getChar, {
AssertNargs(1)
Return(J, Get<char>(J, *GetArg(Binary, 0), GetArgUInt32(1)));
}, 1)
ADD_METHOD_CPP(Binary, getInt, {
AssertNargs(1)
Return(J, Get<int>(J, *GetArg(Binary, 0), GetArgUInt32(1)));
}, 1)
ADD_METHOD_CPP(Binary, getUInt, {
AssertNargs(1)
Return(J, Get<unsigned>(J, *GetArg(Binary, 0), GetArgUInt32(1)));
}, 1)
ADD_METHOD_CPP(Binary, getFloat4, {
AssertNargs(1)
Return(J, Get<float>(J, *GetArg(Binary, 0), GetArgUInt32(1)));
}, 1)
ADD_METHOD_CPP(Binary, getFloat8, {
AssertNargs(1)
Return(J, Get<double>(J, *GetArg(Binary, 0), GetArgUInt32(1)));
}, 1)
// find
ADD_METHOD_CPP(Binary, findChar, { // XXX does not check argument correctness
AssertNargs(2)
auto b = GetArg(Binary, 0);
auto off = GetArgUInt32(1);
auto p = memchr(&(*b)[off], GetArgChar(2), b->size() - off);
Return(J, p ? (int)((uint8_t*)p - &(*b)[0]) : -1);
}, 2)
// sort
ADD_METHOD_CPP(Binary, sortAreasByFloat4Field, {
AssertNargs(3)
Sort<float>(J,
*GetArg(Binary, 0),
GetArgUInt32(1), // areaSize
GetArgUInt32(2), // fldOffset
GetArgBoolean(3) // orderInc
);
ReturnVoid(J);
}, 3)
ADD_METHOD_CPP(Binary, sortAreasByFloat8Field, {
AssertNargs(3)
Sort<double>(J,
*GetArg(Binary, 0),
GetArgUInt32(1), // areaSize
GetArgUInt32(2), // fldOffset
GetArgBoolean(3) // orderInc
);
ReturnVoid(J);
}, 3)
//
ADD_METHOD_CPP(Binary, concatenate, {
AssertNargs(1)
auto b = new Binary(*GetArg(Binary, 0));
auto b1 = GetArg(Binary, 1);
b->insert(b->end(), b1->begin(), b1->end());
ReturnObj(b);
}, 1)
ADD_METHOD_CPP(Binary, toString, {
AssertNargs(0)
auto b = GetArg(Binary, 0);
Return(J, std::string(b->begin(), b->end()));
}, 0)
ADD_METHOD_CPP(Binary, getSubString, {
AssertNargs(2)
auto b = GetArg(Binary, 0);
Return(J, std::string(b->begin()+GetArgUInt32(1), b->begin()+GetArgUInt32(2)));
}, 2)
ADD_METHOD_CPP(Binary, toFile, {
AssertNargs(1)
std::ofstream file(GetArgString(1), std::ios::out | std::ios::binary);
auto b = GetArg(Binary, 0);
file.write((char*)&(*b)[0], b->size());
ReturnVoid(J);
}, 1)
ADD_METHOD_CPP(Binary, bboxFloat4, {
AssertNargs(3)
Return(J, BBox<float>(J,
*GetArg(Binary, 0),
GetArgUInt32(1), // ndims
GetArgUInt32(2), // leading
GetArgUInt32(3) // trailing
));
}, 3)
ADD_METHOD_CPP(Binary, bboxFloat8, {
AssertNargs(3)
Return(J, BBox<double>(J,
*GetArg(Binary, 0),
GetArgUInt32(1), // ndims
GetArgUInt32(2), // leading
GetArgUInt32(3) // trailing
));
}, 3)
ADD_METHOD_CPP(Binary, createMulMat3PlusVec3Float4, {
AssertNargs(4)
ReturnObj(CreateMulMat3PlusVec3<float>(J,
*GetArg(Binary, 0),
GetArgUInt32(1), // leading
GetArgUInt32(2), // trailing
Mat3ToType<double, float>::convert(GetArgMat3x3(3)), // M
Vec3ToType<double, float>::convert(GetArgVec3(4)) // V
));
}, 4)
ADD_METHOD_CPP(Binary, createMulMat3PlusVec3Float8, {
AssertNargs(4)
ReturnObj(CreateMulMat3PlusVec3<double>(J,
*GetArg(Binary, 0),
GetArgUInt32(1), // leading
GetArgUInt32(2), // trailing
GetArgMat3x3(3), // M
GetArgVec3(4) // V
));
}, 4)
ADD_METHOD_CPP(Binary, mulScalarPlusVec3Float4, {
AssertNargs(4)
MulScalarPlusVec3<float>(J,
*GetArg(Binary, 0),
GetArgUInt32(1), // leading
GetArgUInt32(2), // trailing
Vec3ToType<double,float>::convert(GetArgVec3(3)), // Scalar
Vec3ToType<double,float>::convert(GetArgVec3(4)) // Vec
);
ReturnVoid(J);
}, 4)
ADD_METHOD_CPP(Binary, mulScalarPlusVec3Float8, {
AssertNargs(4)
MulScalarPlusVec3<double>(J,
*GetArg(Binary, 0),
GetArgUInt32(1), // leading
GetArgUInt32(2), // trailing
GetArgVec3(3), // Scalar
GetArgVec3(4) // Vec
);
ReturnVoid(J);
}, 4)
}
JsSupport::endDefineClass(J);
}
} // JsBinary
} // JsBinding