-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathbasic-trajectory-generator.ts
More file actions
75 lines (65 loc) · 1.59 KB
/
basic-trajectory-generator.ts
File metadata and controls
75 lines (65 loc) · 1.59 KB
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
import { transfer } from 'comlink'
import {
BufferAttributeLike,
clamp,
getCurvePositions,
getTrajectory,
limit,
packBufferGeometryLike,
PositionLog,
ReadonlyStore,
} from '../sdk'
export async function generateBasicTrajectory(
this: ReadonlyStore,
id: string,
segmentsPerMeter: number,
simplificationThreshold: number = 0,
fromMsl?: number,
includeLengths: boolean = false
) {
const poslogMsl = await limit(() =>
this.get<PositionLog>('position-logs', id)
)
const trajectory = getTrajectory(id, poslogMsl)
if (!trajectory) return null
const from =
fromMsl !== undefined
? clamp(
(fromMsl - trajectory.measuredTop) / trajectory.measuredLength,
0,
1
)
: 0
const curvePositions = getCurvePositions(
trajectory.curve,
from,
1,
segmentsPerMeter,
simplificationThreshold
)
const positions = new Float32Array(curvePositions.length * 3)
const lengths = includeLengths ? new Float32Array(curvePositions.length) : null
curvePositions.forEach((u, i) => {
const pos = trajectory.curve.getPointAt(u)
positions[i * 3] = pos[0]
positions[i * 3 + 1] = pos[1]
positions[i * 3 + 2] = pos[2]
if (lengths) {
lengths[i] = u
}
})
const attributes: Record<string, BufferAttributeLike> = {
position: {
array: positions,
itemSize: 3,
},
}
if (lengths) {
attributes.lengths = {
array: lengths,
itemSize: 1,
}
}
const [geometry, transferrables] = packBufferGeometryLike({ attributes })
return transfer(geometry, transferrables)
}