boxddd-sys 0.2.0

Low-level FFI bindings for Box3D built from vendored upstream sources
Documentation
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
#include "box3d/box3d.h"

#include <emscripten/emscripten.h>
#include <stdbool.h>
#include <stdint.h>

EM_JS(int, boxddd_js_debug_take_error, (uint32_t token), {
  const errors = Module.boxdddDebugDrawErrors;
  if (!errors) return 0;
  const key = token >>> 0;
  const error = errors.get(key) | 0;
  errors.delete(key);
  return error;
});

EM_JS(uint32_t, boxddd_js_debug_shape_create, (uint32_t token, const b3DebugShape* shape), {
  const exports = Module.boxdddAppExports;
  const setError = (code) => {
    const key = token >>> 0;
    const errors = Module.boxdddDebugDrawErrors || (Module.boxdddDebugDrawErrors = new Map());
    if (!errors.has(key)) errors.set(key, code | 0);
  };
  if (!exports || typeof exports.boxddd_debug_shape_create !== 'function') {
    setError(1);
    return 0;
  }
  try {
    return exports.boxddd_debug_shape_create(token >>> 0, shape >>> 0) >>> 0;
  } catch (error) {
    setError(2);
    if (Module.printErr) Module.printErr(`boxddd debug shape create failed: ${error}`);
    return 0;
  }
});

EM_JS(void, boxddd_js_debug_shape_destroy, (uint32_t token, uint32_t handle), {
  const exports = Module.boxdddAppExports;
  const setError = (code) => {
    const key = token >>> 0;
    const errors = Module.boxdddDebugDrawErrors || (Module.boxdddDebugDrawErrors = new Map());
    if (!errors.has(key)) errors.set(key, code | 0);
  };
  if (!exports || typeof exports.boxddd_debug_shape_destroy !== 'function') {
    setError(1);
    return;
  }
  try {
    exports.boxddd_debug_shape_destroy(token >>> 0, handle >>> 0);
  } catch (error) {
    setError(2);
    if (Module.printErr) Module.printErr(`boxddd debug shape destroy failed: ${error}`);
  }
});

EM_JS(int, boxddd_js_debug_draw_shape,
      (uint32_t token, uint32_t handle, const b3WorldTransform* transform, b3HexColor color), {
  const exports = Module.boxdddAppExports;
  const setError = (code) => {
    const key = token >>> 0;
    const errors = Module.boxdddDebugDrawErrors || (Module.boxdddDebugDrawErrors = new Map());
    if (!errors.has(key)) errors.set(key, code | 0);
  };
  if (!exports || typeof exports.boxddd_debug_draw_shape !== 'function') {
    setError(1);
    return 0;
  }
  try {
    return exports.boxddd_debug_draw_shape(token >>> 0, handle >>> 0, transform >>> 0, color | 0) ? 1 : 0;
  } catch (error) {
    setError(2);
    if (Module.printErr) Module.printErr(`boxddd debug draw shape failed: ${error}`);
    return 0;
  }
});

EM_JS(void, boxddd_js_debug_draw_segment,
      (uint32_t token, const b3Pos* p1, const b3Pos* p2, b3HexColor color), {
  const exports = Module.boxdddAppExports;
  const setError = (code) => {
    const key = token >>> 0;
    const errors = Module.boxdddDebugDrawErrors || (Module.boxdddDebugDrawErrors = new Map());
    if (!errors.has(key)) errors.set(key, code | 0);
  };
  if (!exports || typeof exports.boxddd_debug_draw_segment !== 'function') {
    setError(1);
    return;
  }
  try {
    exports.boxddd_debug_draw_segment(token >>> 0, p1 >>> 0, p2 >>> 0, color | 0);
  } catch (error) {
    setError(2);
    if (Module.printErr) Module.printErr(`boxddd debug draw segment failed: ${error}`);
  }
});

EM_JS(void, boxddd_js_debug_draw_transform, (uint32_t token, const b3WorldTransform* transform), {
  const exports = Module.boxdddAppExports;
  const setError = (code) => {
    const key = token >>> 0;
    const errors = Module.boxdddDebugDrawErrors || (Module.boxdddDebugDrawErrors = new Map());
    if (!errors.has(key)) errors.set(key, code | 0);
  };
  if (!exports || typeof exports.boxddd_debug_draw_transform !== 'function') {
    setError(1);
    return;
  }
  try {
    exports.boxddd_debug_draw_transform(token >>> 0, transform >>> 0);
  } catch (error) {
    setError(2);
    if (Module.printErr) Module.printErr(`boxddd debug draw transform failed: ${error}`);
  }
});

EM_JS(void, boxddd_js_debug_draw_point,
      (uint32_t token, const b3Pos* position, float size, b3HexColor color), {
  const exports = Module.boxdddAppExports;
  const setError = (code) => {
    const key = token >>> 0;
    const errors = Module.boxdddDebugDrawErrors || (Module.boxdddDebugDrawErrors = new Map());
    if (!errors.has(key)) errors.set(key, code | 0);
  };
  if (!exports || typeof exports.boxddd_debug_draw_point !== 'function') {
    setError(1);
    return;
  }
  try {
    exports.boxddd_debug_draw_point(token >>> 0, position >>> 0, size, color | 0);
  } catch (error) {
    setError(2);
    if (Module.printErr) Module.printErr(`boxddd debug draw point failed: ${error}`);
  }
});

EM_JS(void, boxddd_js_debug_draw_sphere,
      (uint32_t token, const b3Pos* center, float radius, b3HexColor color, float alpha), {
  const exports = Module.boxdddAppExports;
  const setError = (code) => {
    const key = token >>> 0;
    const errors = Module.boxdddDebugDrawErrors || (Module.boxdddDebugDrawErrors = new Map());
    if (!errors.has(key)) errors.set(key, code | 0);
  };
  if (!exports || typeof exports.boxddd_debug_draw_sphere !== 'function') {
    setError(1);
    return;
  }
  try {
    exports.boxddd_debug_draw_sphere(token >>> 0, center >>> 0, radius, color | 0, alpha);
  } catch (error) {
    setError(2);
    if (Module.printErr) Module.printErr(`boxddd debug draw sphere failed: ${error}`);
  }
});

EM_JS(void, boxddd_js_debug_draw_capsule,
      (uint32_t token, const b3Pos* p1, const b3Pos* p2, float radius, b3HexColor color, float alpha), {
  const exports = Module.boxdddAppExports;
  const setError = (code) => {
    const key = token >>> 0;
    const errors = Module.boxdddDebugDrawErrors || (Module.boxdddDebugDrawErrors = new Map());
    if (!errors.has(key)) errors.set(key, code | 0);
  };
  if (!exports || typeof exports.boxddd_debug_draw_capsule !== 'function') {
    setError(1);
    return;
  }
  try {
    exports.boxddd_debug_draw_capsule(token >>> 0, p1 >>> 0, p2 >>> 0, radius, color | 0, alpha);
  } catch (error) {
    setError(2);
    if (Module.printErr) Module.printErr(`boxddd debug draw capsule failed: ${error}`);
  }
});

EM_JS(void, boxddd_js_debug_draw_bounds,
      (uint32_t token, const b3AABB* aabb, b3HexColor color), {
  const exports = Module.boxdddAppExports;
  const setError = (code) => {
    const key = token >>> 0;
    const errors = Module.boxdddDebugDrawErrors || (Module.boxdddDebugDrawErrors = new Map());
    if (!errors.has(key)) errors.set(key, code | 0);
  };
  if (!exports || typeof exports.boxddd_debug_draw_bounds !== 'function') {
    setError(1);
    return;
  }
  try {
    exports.boxddd_debug_draw_bounds(token >>> 0, aabb >>> 0, color | 0);
  } catch (error) {
    setError(2);
    if (Module.printErr) Module.printErr(`boxddd debug draw bounds failed: ${error}`);
  }
});

EM_JS(void, boxddd_js_debug_draw_box,
      (uint32_t token, const b3Vec3* extents, const b3WorldTransform* transform, b3HexColor color), {
  const exports = Module.boxdddAppExports;
  const setError = (code) => {
    const key = token >>> 0;
    const errors = Module.boxdddDebugDrawErrors || (Module.boxdddDebugDrawErrors = new Map());
    if (!errors.has(key)) errors.set(key, code | 0);
  };
  if (!exports || typeof exports.boxddd_debug_draw_box !== 'function') {
    setError(1);
    return;
  }
  try {
    exports.boxddd_debug_draw_box(token >>> 0, extents >>> 0, transform >>> 0, color | 0);
  } catch (error) {
    setError(2);
    if (Module.printErr) Module.printErr(`boxddd debug draw box failed: ${error}`);
  }
});

EM_JS(void, boxddd_js_debug_draw_string,
      (uint32_t token, const b3Pos* position, const char* text, b3HexColor color), {
  const exports = Module.boxdddAppExports;
  const setError = (code) => {
    const key = token >>> 0;
    const errors = Module.boxdddDebugDrawErrors || (Module.boxdddDebugDrawErrors = new Map());
    if (!errors.has(key)) errors.set(key, code | 0);
  };
  if (!exports || typeof exports.boxddd_debug_draw_string !== 'function') {
    setError(1);
    return;
  }
  try {
    exports.boxddd_debug_draw_string(token >>> 0, position >>> 0, text >>> 0, color | 0);
  } catch (error) {
    setError(2);
    if (Module.printErr) Module.printErr(`boxddd debug draw string failed: ${error}`);
  }
});

static void* boxddd_create_debug_shape(const b3DebugShape* shape, void* context)
{
    uint32_t token = (uint32_t)(uintptr_t)context;
    uint32_t handle = boxddd_js_debug_shape_create(token, shape);
    return (void*)(uintptr_t)handle;
}

static void boxddd_destroy_debug_shape(void* userShape, void* context)
{
    uint32_t token = (uint32_t)(uintptr_t)context;
    uint32_t handle = (uint32_t)(uintptr_t)userShape;
    if (handle != 0) {
        boxddd_js_debug_shape_destroy(token, handle);
    }
}

static bool boxddd_draw_shape(void* userShape, b3WorldTransform transform, b3HexColor color, void* context)
{
    uint32_t token = (uint32_t)(uintptr_t)context;
    uint32_t handle = (uint32_t)(uintptr_t)userShape;
    return boxddd_js_debug_draw_shape(token, handle, &transform, color) != 0;
}

static void boxddd_draw_segment(b3Pos p1, b3Pos p2, b3HexColor color, void* context)
{
    boxddd_js_debug_draw_segment((uint32_t)(uintptr_t)context, &p1, &p2, color);
}

static void boxddd_draw_transform(b3WorldTransform transform, void* context)
{
    boxddd_js_debug_draw_transform((uint32_t)(uintptr_t)context, &transform);
}

static void boxddd_draw_point(b3Pos position, float size, b3HexColor color, void* context)
{
    boxddd_js_debug_draw_point((uint32_t)(uintptr_t)context, &position, size, color);
}

static void boxddd_draw_sphere(b3Pos center, float radius, b3HexColor color, float alpha, void* context)
{
    boxddd_js_debug_draw_sphere((uint32_t)(uintptr_t)context, &center, radius, color, alpha);
}

static void boxddd_draw_capsule(b3Pos p1, b3Pos p2, float radius, b3HexColor color, float alpha, void* context)
{
    boxddd_js_debug_draw_capsule((uint32_t)(uintptr_t)context, &p1, &p2, radius, color, alpha);
}

static void boxddd_draw_bounds(b3AABB aabb, b3HexColor color, void* context)
{
    boxddd_js_debug_draw_bounds((uint32_t)(uintptr_t)context, &aabb, color);
}

static void boxddd_draw_box(b3Vec3 extents, b3WorldTransform transform, b3HexColor color, void* context)
{
    boxddd_js_debug_draw_box((uint32_t)(uintptr_t)context, &extents, &transform, color);
}

static void boxddd_draw_string(b3Pos position, const char* text, b3HexColor color, void* context)
{
    boxddd_js_debug_draw_string((uint32_t)(uintptr_t)context, &position, text, color);
}

void boxddd_provider_debug_install_world_def(b3WorldDef* def, uint32_t registryToken)
{
    def->createDebugShape = boxddd_create_debug_shape;
    def->destroyDebugShape = boxddd_destroy_debug_shape;
    def->userDebugShapeContext = (void*)(uintptr_t)registryToken;
}

void boxddd_provider_debug_init_draw(b3DebugDraw* draw, uint32_t drawToken)
{
    draw->DrawShapeFcn = boxddd_draw_shape;
    draw->DrawSegmentFcn = boxddd_draw_segment;
    draw->DrawTransformFcn = boxddd_draw_transform;
    draw->DrawPointFcn = boxddd_draw_point;
    draw->DrawSphereFcn = boxddd_draw_sphere;
    draw->DrawCapsuleFcn = boxddd_draw_capsule;
    draw->DrawBoundsFcn = boxddd_draw_bounds;
    draw->DrawBoxFcn = boxddd_draw_box;
    draw->DrawStringFcn = boxddd_draw_string;
    draw->context = (void*)(uintptr_t)drawToken;
}

int boxddd_provider_debug_take_error(uint32_t token)
{
    return boxddd_js_debug_take_error(token);
}

EM_JS(int, boxddd_js_query_take_error, (uint32_t token), {
  const errors = Module.boxdddQueryErrors;
  if (!errors) return 0;
  const key = token >>> 0;
  const error = errors.get(key) | 0;
  errors.delete(key);
  return error;
});

EM_JS(int, boxddd_js_query_overlap, (uint32_t token, const b3ShapeId* shape), {
  const exports = Module.boxdddAppExports;
  const setError = (code) => {
    const key = token >>> 0;
    const errors = Module.boxdddQueryErrors || (Module.boxdddQueryErrors = new Map());
    if (!errors.has(key)) errors.set(key, code | 0);
  };
  if (!exports || typeof exports.boxddd_query_overlap !== 'function') {
    setError(1);
    return 0;
  }
  try {
    return exports.boxddd_query_overlap(token >>> 0, shape >>> 0) ? 1 : 0;
  } catch (error) {
    setError(2);
    if (Module.printErr) Module.printErr(`boxddd query overlap failed: ${error}`);
    return 0;
  }
});

EM_JS(float, boxddd_js_query_cast,
      (uint32_t token, const b3ShapeId* shape, const b3Pos* point, const b3Vec3* normal, float fraction,
       const uint64_t* userMaterialId, int triangleIndex, int childIndex), {
  const exports = Module.boxdddAppExports;
  const setError = (code) => {
    const key = token >>> 0;
    const errors = Module.boxdddQueryErrors || (Module.boxdddQueryErrors = new Map());
    if (!errors.has(key)) errors.set(key, code | 0);
  };
  if (!exports || typeof exports.boxddd_query_cast !== 'function') {
    setError(1);
    return 0.0;
  }
  try {
    return exports.boxddd_query_cast(
      token >>> 0,
      shape >>> 0,
      point >>> 0,
      normal >>> 0,
      fraction,
      userMaterialId >>> 0,
      triangleIndex | 0,
      childIndex | 0
    );
  } catch (error) {
    setError(2);
    if (Module.printErr) Module.printErr(`boxddd query cast failed: ${error}`);
    return 0.0;
  }
});

static bool boxddd_query_overlap(b3ShapeId shapeId, void* context)
{
    uint32_t token = (uint32_t)(uintptr_t)context;
    return boxddd_js_query_overlap(token, &shapeId) != 0;
}

static float boxddd_query_cast(b3ShapeId shapeId, b3Pos point, b3Vec3 normal, float fraction, uint64_t userMaterialId,
                               int triangleIndex, int childIndex, void* context)
{
    uint32_t token = (uint32_t)(uintptr_t)context;
    return boxddd_js_query_cast(token, &shapeId, &point, &normal, fraction, &userMaterialId, triangleIndex, childIndex);
}

int boxddd_provider_query_take_error(uint32_t token)
{
    return boxddd_js_query_take_error(token);
}

b3TreeStats boxddd_provider_world_overlap_aabb(b3WorldId worldId, b3AABB aabb, b3QueryFilter filter, uint32_t token)
{
    return b3World_OverlapAABB(worldId, aabb, filter, boxddd_query_overlap, (void*)(uintptr_t)token);
}

b3TreeStats boxddd_provider_world_cast_ray(b3WorldId worldId, b3Pos origin, b3Vec3 translation, b3QueryFilter filter, uint32_t token)
{
    return b3World_CastRay(worldId, origin, translation, filter, boxddd_query_cast, (void*)(uintptr_t)token);
}