-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathHashmap.ts
51 lines (43 loc) · 1.46 KB
/
Hashmap.ts
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
import * as THREE from "three";
import { getArrayFromVector } from "./utils_three";
export interface VerticesHashMap {
[key: string]: number[];
}
export class VerticesHashMapHandler {
hashmap: VerticesHashMap;
constructor() {
this.hashmap = {};
}
add(key: string, value: number[]) {
if (!this.hashmap[key]) {
this.hashmap[key] = [...value];
} else {
this.hashmap[key].push(...value);
}
}
get(key: string) {
return this.hashmap[key];
}
iterateCoordinates(callback: (key: string, coordinateAsArray: number[]) => void) {
Object.entries(this.hashmap).forEach(([key, vertices]) => {
for (let i = 0; i < vertices.length; i += 3) {
const coordinateAsArray = vertices.slice(i, i + 3);
callback(key, coordinateAsArray);
}
});
}
}
export function createObjectVerticesHashMap(
getVerticeKeyPerObject = (object: THREE.Object3D) => object.name,
objectsToUse: THREE.Object3D[] = [],
) {
const positionsHashMap = new VerticesHashMapHandler();
const getVerticeKeyPerObjectFn = getVerticeKeyPerObject;
if (objectsToUse)
objectsToUse.forEach((object: THREE.Object3D) => {
const [x, y, z] = getArrayFromVector(object.position);
const mapKey = getVerticeKeyPerObjectFn(object);
positionsHashMap.add(mapKey, [x, y, z]);
});
return positionsHashMap;
}