diff --git a/client/aux.js b/client/aux.js index ceb2897..c3e8ae3 100644 --- a/client/aux.js +++ b/client/aux.js @@ -156,6 +156,11 @@ function tv_add(tv, item) { tv.data[tv.size++] = item; } +function tv_add2(tv, item) { + tv_ensure_by(tv, 1); + tv_add(tv, item); +} + function tv_pop(tv) { const result = tv.data[tv.size - 1]; tv.size--; diff --git a/client/bvh.js b/client/bvh.js index 94486ff..45496c4 100644 --- a/client/bvh.js +++ b/client/bvh.js @@ -98,7 +98,7 @@ function bvh_add_stroke(bvh, index, stroke) { } if (bvh.pqueue.capacity < Math.ceil(bvh.nodes.length * 1.2)) { - bvh.pqueue = new MinQueue(bvh.pqueue.capacity * 2); + bvh.pqueue = new MinQueue(bvh.nodes.length * 2); } // It's as easy as 1-2-3 @@ -141,7 +141,7 @@ function bvh_add_stroke(bvh, index, stroke) { bvh.nodes[new_parent].bbox = new_bbox; bvh.nodes[new_parent].area = (new_bbox.x2 - new_bbox.x1) * (new_bbox.y2 - new_bbox.y1); - // 3. Refit + // 3. Refit and insert in fullnode let refit_index = bvh.nodes[leaf_index].parent_index; while (refit_index !== null) { const child1 = bvh.nodes[refit_index].child1; @@ -149,6 +149,10 @@ function bvh_add_stroke(bvh, index, stroke) { bvh.nodes[refit_index].bbox = quad_union(bvh.nodes[child1].bbox, bvh.nodes[child2].bbox); + if (bvh.nodes[refit_index].is_fullnode) { + tv_add2(bvh.nodes[refit_index].stroke_indices, index); + } + refit_index = bvh.nodes[refit_index].parent_index; } } diff --git a/client/speed.js b/client/speed.js index c03d4d8..37dcf75 100644 --- a/client/speed.js +++ b/client/speed.js @@ -119,9 +119,9 @@ function wasm_ensure_by(state, nstrokes, ncoords) { } if (realloc) { - const current_pages = state.wasm.memory.buffer.byteLength / (4096 * 16); - const need_pages = (state.wasm.coords_bytes * 3 + state.wasm.stroke_bytes * 2) / (4096 * 16); // TODO: figure out actual memory requirements - const grow_by = need_pages - current_pages; + const current_pages = Math.ceil(state.wasm.memory.buffer.byteLength / (4096 * 16)); + const need_pages = Math.ceil((state.wasm.coords_bytes * 3 + state.wasm.stroke_bytes * 2) / (4096 * 16)); // TODO: figure out actual memory requirements + const grow_by = Math.max(1, need_pages - current_pages); state.wasm.memory.grow(grow_by); state.wasm.exports.free_static();