Skip to content

Commit

Permalink
feat(vector-pools): add AttribPool.attribArray(), add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
postspectacular committed May 25, 2019
1 parent 8fcf05c commit 285022a
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 3 deletions.
26 changes: 23 additions & 3 deletions packages/vector-pools/src/attrib-pool.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import {
TypedArray
} from "@thi.ng/api";
import { align, Pow2 } from "@thi.ng/binary";
import { MemPool, wrap } from "@thi.ng/malloc";
import { MemPool, TYPEDARRAY_CTORS, wrap } from "@thi.ng/malloc";
import { range } from "@thi.ng/transducers";
import { ReadonlyVec, Vec, zeroes } from "@thi.ng/vectors";
import { AttribPoolOpts, AttribSpec } from "./api";
Expand Down Expand Up @@ -99,9 +99,9 @@ export class AttribPool implements IRelease {
}

*attribValues(id: string) {
const buf = this.attribs[id];
assert(!!buf, `invalid attrib: ${id}`);
const spec = this.specs[id];
assert(!!spec, `invalid attrib: ${id}`);
const buf = this.attribs[id];
const stride = spec.stride;
const size = spec.size;
if (size > 1) {
Expand All @@ -115,6 +115,26 @@ export class AttribPool implements IRelease {
}
}

attribArray(id: string) {
const spec = this.specs[id];
assert(!!spec, `invalid attrib: ${id}`);
const n = this.capacity;
const size = spec.size;
const stride = spec.stride;
const src = this.attribs[id];
const dest = new TYPEDARRAY_CTORS[(asNativeType(spec.type))](n * size);
if (size > 1) {
for (let i = 0, j = 0; i < n; i++, j += stride) {
dest.set(src.subarray(j, j + size), i * size);
}
} else {
for (let i = 0; i < n; i++) {
dest[i] = src[i * stride];
}
}
return dest;
}

setAttribValue(id: string, index: number, v: number | ReadonlyVec) {
const spec = this.specs[id];
assert(!!spec, `invalid attrib: ${id}`);
Expand Down
10 changes: 10 additions & 0 deletions packages/vector-pools/test/attribs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,5 +61,15 @@ describe("vector-pools", () => {
[255, 254, 253, 252]
]
);
// prettier-ignore
assert.deepEqual(
pool.attribArray("pos"),
[1, 2, 3, 4, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
);
// prettier-ignore
assert.deepEqual(
pool.attribArray("index"),
[10, 20, 0, 0, 0, 0, 0, 0]
);
});
});

0 comments on commit 285022a

Please sign in to comment.