Skip to content

Commit

Permalink
feat(api): add typedArrayOfVec()
Browse files Browse the repository at this point in the history
  • Loading branch information
postspectacular committed Apr 12, 2023
1 parent 89c6b76 commit 39307bf
Showing 1 changed file with 49 additions and 0 deletions.
49 changes: 49 additions & 0 deletions packages/api/src/typedarray.ts
Original file line number Diff line number Diff line change
Expand Up @@ -301,6 +301,55 @@ export function typedArray<T extends Type | GLType | BigType>(
return new (ctor || TYPEDARRAY_CTORS[asNativeType(<any>type)])(...xs);
}

/**
* Constructs a typed array for given `type` and populates it with given vector
* values.
*
* @remarks
* The size of the array will be `data.length * stride`, where `stride` is the
* number of elements per item and defaulting to the size of the first data
* item/vector given.
*
* @example
* ```ts
* // inferred stride=2 (2d vectors)
* typedArrayOfVec("f32", [[1,2], [3,4], [-10,20]]);
* // Float32Array(6) [ 1, 2, 3, 4, -10, 20 ]
*
* // with custom stride=4
* typedArrayOfVec("f32", [[1,2], [3,4], [-10,20]], 4);
* // Float32Array(12) [ 1, 2, 0, 0, 3,4, 0, 0, -10, 20, 0, 0 ]
* ```
*
* @param type
* @param data
* @param stride
*/
export function typedArrayOfVec<T extends Type | GLType>(
type: T,
data: Iterable<ArrayLike<number>>,
stride?: number
): TypedArrayTypeMap[T];
export function typedArrayOfVec<T extends BigType>(
type: T,
data: Iterable<ArrayLike<bigint>>,
stride?: number
): BigTypedArrayTypeMap[T];
export function typedArrayOfVec<T extends Type | GLType | BigType>(
type: T,
data: Iterable<ArrayLike<number | bigint>>,
stride?: number
) {
const $data = Array.isArray(data) ? data : [...data];
if (stride === undefined) stride = $data[0].length;
const num = $data.length;
const res = typedArray(<Type>type, num * stride!);
for (let i = 0, j = 0; i < num; i++, j += stride!) {
res.set($data[i], j);
}
return res;
}

/**
* Takes an {@link NumericArray} and returns its corresponding {@link Type} ID.
* Standard JS arrays will default to {@link "f64"}.
Expand Down

0 comments on commit 39307bf

Please sign in to comment.