Skip to content

Commit

Permalink
chore(map): avoid typed array overhead for short array lengths
Browse files Browse the repository at this point in the history
  • Loading branch information
fallenoak committed Feb 8, 2024
1 parent e78d399 commit 97796b6
Show file tree
Hide file tree
Showing 6 changed files with 21 additions and 29 deletions.
8 changes: 4 additions & 4 deletions src/lib/map/Map.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,8 @@ class Map {
*
* @param defPosition - Vector in doodad / map obj def coordinate system
*/
static getNormalizedDefPosition(defPosition: Float32Array) {
const normalized = new Float32Array(3);
static getNormalizedDefPosition(defPosition: number[]) {
const normalized = new Array(3);

normalized[0] = MAP_CORNER_X - defPosition[2];
normalized[1] = MAP_CORNER_Y - defPosition[0];
Expand All @@ -79,13 +79,13 @@ class Map {
*
* @param defRotation - Euler angle in doodad / map obj def coordinate system
*/
static getNormalizedDefRotation(defRotation: Float32Array) {
static getNormalizedDefRotation(defRotation: number[]) {
const normalized = quat.create();

// glMatrix defaults to the correct euler order (zyx) to convert to the map coordinate system
quat.fromEuler(normalized, defRotation[2], defRotation[0], defRotation[1] + 180);

return normalized as Float32Array;
return Array.from(normalized);
}

load(source: IoSource) {
Expand Down
4 changes: 2 additions & 2 deletions src/lib/map/MapChunk.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,11 @@ class MapChunk {
#areaId: number;
#holes: number = 0;
#layers: MapLayer[] = [];
#position: Float32Array;
#position: number[];
#vertexHeights: Float32Array;
#vertexNormals: Int8Array;

constructor(position: Float32Array, areaId: number, holes: number = 0) {
constructor(position: number[], areaId: number, holes: number = 0) {
this.#position = position;
this.#areaId = areaId;
this.#holes = holes;
Expand Down
12 changes: 3 additions & 9 deletions src/lib/map/MapDoodadDef.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,11 @@
class MapDoodadDef {
#name: string;
#id: number;
#position: Float32Array;
#rotation: Float32Array;
#position: number[];
#rotation: number[];
#scale: number;

constructor(
name: string,
id: number,
position: Float32Array,
rotation: Float32Array,
scale: number,
) {
constructor(name: string, id: number, position: number[], rotation: number[], scale: number) {
this.#name = name;
this.#id = id;
this.#position = position;
Expand Down
6 changes: 3 additions & 3 deletions src/lib/map/MapObjDef.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
class MapObjDef {
#name: string;
#id: number;
#position: Float32Array;
#rotation: Float32Array;
#position: number[];
#rotation: number[];

constructor(name: string, id: number, position: Float32Array, rotation: Float32Array) {
constructor(name: string, id: number, position: number[], rotation: number[]) {
this.#name = name;
this.#id = id;
this.#position = position;
Expand Down
10 changes: 5 additions & 5 deletions src/lib/map/io/adt.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,18 +13,18 @@ const mapChunkInfo = io.struct({
const mapDoodadDef = io.struct({
nameId: io.uint32le,
uniqueId: io.uint32le,
position: io.typedArray(io.float32le, { size: 3 }),
rotation: io.typedArray(io.float32le, { size: 3 }),
position: io.array(io.float32le, { size: 3 }),
rotation: io.array(io.float32le, { size: 3 }),
scale: io.uint16le,
flags: io.uint16le,
});

const mapObjDef = io.struct({
nameId: io.uint32le,
uniqueId: io.uint32le,
position: io.typedArray(io.float32le, { size: 3 }),
rotation: io.typedArray(io.float32le, { size: 3 }),
extents: io.typedArray(io.float32le, { size: 6 }),
position: io.array(io.float32le, { size: 3 }),
rotation: io.array(io.float32le, { size: 3 }),
extents: io.array(io.float32le, { size: 6 }),
flags: io.uint16le,
doodadSet: io.uint16le,
nameSet: io.uint16le,
Expand Down
10 changes: 4 additions & 6 deletions src/spec/map/MapArea.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,13 @@ describe('MapArea', () => {

const doodadDef1 = mapArea.doodadDefs.find((def) => def.id === 206410);
expect(doodadDef1.name.split(/\\/).at(-1)).toBe('TIRISFALLGLADECANOPYTREE05.M2');
expect(doodadDef1.rotation).toEqual(new Float32Array([0, 0, 1, 6.123234262925839e-17]));
expect(doodadDef1.rotation).toEqual([0, 0, 1, 6.123234262925839e-17]);

const doodadDef2 = mapArea.doodadDefs.find((def) => def.id === 2542963);
expect(doodadDef2.name.split(/\\/).at(-1)).toBe('TIRISFALLGLADECANOPYTREE05.M2');
expect(doodadDef2.rotation).toEqual(
new Float32Array([
0.0701502189040184, -0.038644179701805115, 0.9901213645935059, 0.11508788913488388,
]),
);
expect(doodadDef2.rotation).toEqual([
0.0701502189040184, -0.038644179701805115, 0.9901213645935059, 0.11508788913488388,
]);
});
});

Expand Down

0 comments on commit 97796b6

Please sign in to comment.