@margoengine/road 0.0.0-stage → 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +201 -0
- package/NOTICE +4 -0
- package/README.md +206 -3
- package/dist/banking.d.ts +11 -0
- package/dist/banking.js +45 -0
- package/dist/design-mesh-DpZRXXz0.js +38 -0
- package/dist/design-mesh.d.ts +17 -0
- package/dist/drive-path.d.ts +49 -0
- package/dist/driving.d.ts +4 -0
- package/dist/driving.js +438 -0
- package/dist/errors-CsQwshHA.js +7 -0
- package/dist/frame-AjJIb-k3.js +9 -0
- package/dist/frame.d.ts +3 -0
- package/dist/geojson.d.ts +66 -0
- package/dist/geojson.js +375 -0
- package/dist/graph.d.ts +31 -0
- package/dist/graph.js +29 -0
- package/dist/index.d.ts +43 -0
- package/dist/index.js +114 -0
- package/dist/intersections.d.ts +38 -0
- package/dist/intersections.js +100 -0
- package/dist/junctions.d.ts +33 -0
- package/dist/junctions.js +68 -0
- package/dist/lanes.d.ts +73 -0
- package/dist/lanes.js +100 -0
- package/dist/markings.d.ts +27 -0
- package/dist/markings.js +53 -0
- package/dist/repair.d.ts +11 -0
- package/dist/repair.js +23 -0
- package/dist/road-driver.d.ts +88 -0
- package/dist/road-surface.d.ts +32 -0
- package/dist/road-vehicle.d.ts +25 -0
- package/dist/shared/errors.d.ts +16 -0
- package/dist/structures.d.ts +91 -0
- package/dist/structures.js +229 -0
- package/dist/terrain.d.ts +16 -0
- package/dist/terrain.js +71 -0
- package/package.json +106 -6
package/dist/driving.js
ADDED
|
@@ -0,0 +1,438 @@
|
|
|
1
|
+
import { t as e } from "./frame-AjJIb-k3.js";
|
|
2
|
+
import { MargoError as t } from "@margoengine/math/errors";
|
|
3
|
+
import { cos as n, hypot as r, sin as i } from "@margoengine/math/pinned";
|
|
4
|
+
//#region src/road-surface.ts
|
|
5
|
+
function a(e, n = 0) {
|
|
6
|
+
let { dry: r, wet: i } = e;
|
|
7
|
+
if (!(Number.isFinite(r) && Number.isFinite(i) && i >= 0 && r >= i)) throw new t("state", "Choose finite road grip coefficients with 0 ≤ wet ≤ dry.");
|
|
8
|
+
if (!(Number.isFinite(n) && n >= 0 && n <= 1)) throw new t("state", "Choose normalized road wetness between 0 and 1.");
|
|
9
|
+
return r + (i - r) * n;
|
|
10
|
+
}
|
|
11
|
+
var o = class {
|
|
12
|
+
constructor() {
|
|
13
|
+
this.materials = /* @__PURE__ */ new WeakMap(), this.sample = (e) => {
|
|
14
|
+
let t = this.materials.get(e.collider);
|
|
15
|
+
if (!t) return;
|
|
16
|
+
let n = typeof t.wetness == "function" ? t.wetness(e.point) : t.wetness;
|
|
17
|
+
return {
|
|
18
|
+
grip: a(t.traction, n),
|
|
19
|
+
soil: t.traction.soil
|
|
20
|
+
};
|
|
21
|
+
};
|
|
22
|
+
}
|
|
23
|
+
register(e, t) {
|
|
24
|
+
a(t.traction, typeof t.wetness == "number" ? t.wetness : 0);
|
|
25
|
+
let { traction: n, wetness: r } = t;
|
|
26
|
+
return this.materials.set(e, {
|
|
27
|
+
traction: {
|
|
28
|
+
...n,
|
|
29
|
+
soil: n.soil && { ...n.soil }
|
|
30
|
+
},
|
|
31
|
+
wetness: r
|
|
32
|
+
}), this;
|
|
33
|
+
}
|
|
34
|
+
add(e, t, n) {
|
|
35
|
+
let r = a(n.traction);
|
|
36
|
+
typeof n.wetness == "number" && a(n.traction, n.wetness);
|
|
37
|
+
let { geometry: i } = t, o = i.vertices, s = new Float32Array(o.length / 2);
|
|
38
|
+
for (let e = 0; e < o.length; e += 6) s.set(o.subarray(e, e + 3), e / 2);
|
|
39
|
+
let c = e.addBody(null, {
|
|
40
|
+
type: "fixed",
|
|
41
|
+
position: n.position,
|
|
42
|
+
colliders: {
|
|
43
|
+
shape: "trimesh",
|
|
44
|
+
vertices: s,
|
|
45
|
+
indices: i.indices,
|
|
46
|
+
fixInternalEdges: !0,
|
|
47
|
+
friction: r
|
|
48
|
+
}
|
|
49
|
+
});
|
|
50
|
+
return this.register(c.colliders[0], n), c;
|
|
51
|
+
}
|
|
52
|
+
};
|
|
53
|
+
//#endregion
|
|
54
|
+
//#region src/drive-path.ts
|
|
55
|
+
function s(e, n, r, i = Number.MAX_VALUE) {
|
|
56
|
+
if (!Number.isFinite(e) || e < r || e > i) throw new t("state", `Choose road driver ${n} between ${r} and ${i}.`);
|
|
57
|
+
return e;
|
|
58
|
+
}
|
|
59
|
+
var c = (e, t, n) => e + (t - e) * n;
|
|
60
|
+
function l(a, o = {}) {
|
|
61
|
+
let l = s(o.spacing ?? 1, "spacing", Number.MIN_VALUE), u = s(a.length, "spline length", Number.MIN_VALUE), d = s(o.offset ?? 0, "lane offset", -Number.MAX_VALUE), f = o.bank ?? 0, p = typeof f == "number" ? [f, f] : [...f];
|
|
62
|
+
if (p.length < 2 || a.closed && p[0] !== p.at(-1)) throw new t("state", "Provide at least two bank samples and repeat the initial bank on a closed spline.");
|
|
63
|
+
p.forEach((e) => s(e, "bank angle", -Math.PI / 4, Math.PI / 4));
|
|
64
|
+
let m = Math.max(a.closed ? 3 : 1, Math.ceil(u / l));
|
|
65
|
+
if (!Number.isSafeInteger(m) || m * 8 >= 4294967296) throw new t("state", "Increase road driver spacing so the route fits the addressable memory.");
|
|
66
|
+
let h = [], g = [], _ = /* @__PURE__ */ new Float64Array(3), v = /* @__PURE__ */ new Float64Array(3), y = /* @__PURE__ */ new Float64Array(3), b = u / m * .01, x = a.closed ? m : m + 1;
|
|
67
|
+
for (let o = 0; o < x; o++) {
|
|
68
|
+
let s = o / m;
|
|
69
|
+
e(a, u * s, b, _, v, y);
|
|
70
|
+
let l = y[0] - v[0], f = y[1] - v[1], x = y[2] - v[2], S = r(l, x), C = r(l, f, x);
|
|
71
|
+
if (!(S > 0)) throw new t("state", "Use a road spline whose tangent has a horizontal component.");
|
|
72
|
+
let w = s * (p.length - 1), T = Math.min(p.length - 2, Math.floor(w)), E = c(p[T], p[T + 1], w - T), D = x / S, O = -l / S, k = d * i(E) / C, A = d * n(E);
|
|
73
|
+
h.push([
|
|
74
|
+
_[0] + A * D + f * O * k,
|
|
75
|
+
_[1] + S * k,
|
|
76
|
+
_[2] + A * O - f * D * k
|
|
77
|
+
]), g.push(E);
|
|
78
|
+
}
|
|
79
|
+
return {
|
|
80
|
+
points: h,
|
|
81
|
+
banks: g,
|
|
82
|
+
closed: a.closed
|
|
83
|
+
};
|
|
84
|
+
}
|
|
85
|
+
function u(e, t, n) {
|
|
86
|
+
let i = t[0] - e[0], a = t[2] - e[2], o = n[0] - t[0], s = n[2] - t[2], c = r(i, a) * r(o, s) * r(n[0] - e[0], n[2] - e[2]);
|
|
87
|
+
return c > 0 ? 2 * (a * o - i * s) / c : 0;
|
|
88
|
+
}
|
|
89
|
+
function d(e) {
|
|
90
|
+
let { points: n, banks: i, closed: a = !1 } = e;
|
|
91
|
+
if (n.length < (a ? 3 : 2) || i && i.length !== n.length) throw new t("state", "Give the road driver at least two distinct points, or three for a loop, and one bank angle per point.");
|
|
92
|
+
let o = n.length, c = [], l = 0;
|
|
93
|
+
for (let e = 0; e < o; e++) {
|
|
94
|
+
let d = n[e];
|
|
95
|
+
if (d.length !== 3 || !d.every(Number.isFinite)) throw new t("state", "Choose finite XYZ coordinates for every lane station.");
|
|
96
|
+
if (e > 0) {
|
|
97
|
+
let t = n[e - 1];
|
|
98
|
+
l += s(r(d[0] - t[0], d[2] - t[2]), "horizontal station spacing", Number.MIN_VALUE);
|
|
99
|
+
}
|
|
100
|
+
let f = a ? e : Math.max(1, Math.min(o - 2, e)), p = o < 3 ? 0 : u(n[(f + o - 1) % o], n[f], n[(f + 1) % o]);
|
|
101
|
+
c.push({
|
|
102
|
+
x: d[0],
|
|
103
|
+
y: d[1],
|
|
104
|
+
z: d[2],
|
|
105
|
+
bank: s(i?.[e] ?? 0, "bank", -Math.PI / 4, Math.PI / 4),
|
|
106
|
+
curvature: p,
|
|
107
|
+
progress: l
|
|
108
|
+
});
|
|
109
|
+
}
|
|
110
|
+
if (a) {
|
|
111
|
+
let e = c[o - 1], t = c[0];
|
|
112
|
+
l += s(r(t.x - e.x, t.z - e.z), "closing station spacing", Number.MIN_VALUE), c.push({
|
|
113
|
+
...t,
|
|
114
|
+
progress: l
|
|
115
|
+
});
|
|
116
|
+
}
|
|
117
|
+
return {
|
|
118
|
+
stations: c,
|
|
119
|
+
loop: a,
|
|
120
|
+
length: l
|
|
121
|
+
};
|
|
122
|
+
}
|
|
123
|
+
function f(e, t, n, r) {
|
|
124
|
+
let i = e.stations[t], a = e.stations[t + 1], o = a.x - i.x, s = a.z - i.z, l = Math.max(0, Math.min(1, ((n - i.x) * o + (r - i.z) * s) / (o * o + s * s))), u = n - c(i.x, a.x, l), d = r - c(i.z, a.z, l);
|
|
125
|
+
return {
|
|
126
|
+
segment: t,
|
|
127
|
+
t: l,
|
|
128
|
+
distance2: u * u + d * d,
|
|
129
|
+
progress: c(i.progress, a.progress, l)
|
|
130
|
+
};
|
|
131
|
+
}
|
|
132
|
+
function p(e, t, n, r) {
|
|
133
|
+
let i = e.stations.length - 1, a = f(e, r < 0 ? 0 : r, t, n);
|
|
134
|
+
if (r < 0) for (let r = 1; r < i; r++) {
|
|
135
|
+
let i = f(e, r, t, n);
|
|
136
|
+
i.distance2 < a.distance2 && (a = i);
|
|
137
|
+
}
|
|
138
|
+
else for (let r = 0; r < i; r++) {
|
|
139
|
+
let r = a.segment > 0 ? a.segment - 1 : e.loop ? i - 1 : 0, o = a.segment + 1 < i ? a.segment + 1 : e.loop ? 0 : i - 1, s = f(e, r, t, n), c = f(e, o, t, n);
|
|
140
|
+
if (s.distance2 >= a.distance2 && c.distance2 >= a.distance2) break;
|
|
141
|
+
a = s.distance2 <= c.distance2 ? s : c;
|
|
142
|
+
}
|
|
143
|
+
return a;
|
|
144
|
+
}
|
|
145
|
+
function m(e, t, n, r, i) {
|
|
146
|
+
let a = e.stations.length - 1, o = i * i, s = t.distance2 >= o ? t.distance2 + o : o, l = [
|
|
147
|
+
n,
|
|
148
|
+
0,
|
|
149
|
+
r
|
|
150
|
+
];
|
|
151
|
+
for (let i = 0, o = t.segment; i < a; i++, o = (o + 1) % a) {
|
|
152
|
+
let u = e.stations[o], d = e.stations[o + 1], f = d.x - u.x, p = d.z - u.z, m = u.x - n, h = u.z - r, g = d.x - n, _ = d.z - r;
|
|
153
|
+
if (l = [
|
|
154
|
+
d.x,
|
|
155
|
+
d.y,
|
|
156
|
+
d.z
|
|
157
|
+
], g * g + _ * _ >= s) {
|
|
158
|
+
let e = f * f + p * p, n = m * f + h * p, r = m * m + h * h - s, a = (-n + Math.sqrt(Math.max(0, n * n - e * r))) / e, o = Math.max(i === 0 ? t.t : 0, Math.min(1, a));
|
|
159
|
+
return [
|
|
160
|
+
c(u.x, d.x, o),
|
|
161
|
+
c(u.y, d.y, o),
|
|
162
|
+
c(u.z, d.z, o)
|
|
163
|
+
];
|
|
164
|
+
}
|
|
165
|
+
if (!e.loop && o + 1 === a) break;
|
|
166
|
+
}
|
|
167
|
+
return l;
|
|
168
|
+
}
|
|
169
|
+
function h(e, t, r, a = 9.81) {
|
|
170
|
+
if (s(e, "curvature", -Number.MAX_VALUE), s(t, "bank angle", -Math.PI / 4, Math.PI / 4), s(r, "grip", 0), s(a, "gravity", 0), e === 0) return Infinity;
|
|
171
|
+
let o = -Math.sign(e) * t, c = i(o), l = n(o), u = c + r * l;
|
|
172
|
+
if (u <= 0) return 0;
|
|
173
|
+
let d = l - r * c;
|
|
174
|
+
return d <= 0 ? Infinity : Math.sqrt(u / d * a / Math.abs(e));
|
|
175
|
+
}
|
|
176
|
+
function g(e, t, n, r, i, a) {
|
|
177
|
+
let o = e.stations.length - 1, s = e.stations[t.segment], l = e.stations[t.segment + 1], u = h(c(s.curvature, l.curvature, t.t), c(s.bank, l.bank, t.t), i, a), d = Math.min(n, u);
|
|
178
|
+
d *= d;
|
|
179
|
+
let f = 0, p = t.segment + 1;
|
|
180
|
+
for (let n = 0; n < o; n++) {
|
|
181
|
+
let n = e.stations[p], s = 2 * r * (n.progress + f - t.progress);
|
|
182
|
+
if (s >= d) break;
|
|
183
|
+
let c = p === o && !e.loop, l = c ? 0 : h(n.curvature, n.bank, i, a);
|
|
184
|
+
if (d = Math.min(d, l * l + s), c) break;
|
|
185
|
+
p === o ? (f += e.length, p = 1) : p++;
|
|
186
|
+
}
|
|
187
|
+
return Math.sqrt(d);
|
|
188
|
+
}
|
|
189
|
+
//#endregion
|
|
190
|
+
//#region src/road-driver.ts
|
|
191
|
+
var _ = [
|
|
192
|
+
0,
|
|
193
|
+
0,
|
|
194
|
+
0
|
|
195
|
+
], v = 1, y = Float64Array.BYTES_PER_ELEMENT, b = 0, x = 1, S = 2, C = 3, w = 4, T = 5, E = 6, D = 7, O = 8, k = 1;
|
|
196
|
+
function A([e, t, n]) {
|
|
197
|
+
let i = s(r(e, t, n), "forward length", Number.MIN_VALUE);
|
|
198
|
+
return [
|
|
199
|
+
e / i,
|
|
200
|
+
t / i,
|
|
201
|
+
n / i
|
|
202
|
+
];
|
|
203
|
+
}
|
|
204
|
+
function j(e, n) {
|
|
205
|
+
let r = s(e.body, "body row", 0), i = s(e.wetnessWord ?? 0, "wetness word", 0);
|
|
206
|
+
if (!Number.isSafeInteger(i) || !Number.isSafeInteger(r) || e.attachment && e.attachment.chassis.row !== r) throw new t("state", "Choose integer body and rainfall rows and match the attachment chassis row.");
|
|
207
|
+
let o = e.wetness ?? 0;
|
|
208
|
+
return a(e.traction, o), {
|
|
209
|
+
body: r,
|
|
210
|
+
wetnessWord: i,
|
|
211
|
+
wetness: o,
|
|
212
|
+
motionOffset: n,
|
|
213
|
+
attachment: e.attachment,
|
|
214
|
+
route: d(e.path),
|
|
215
|
+
traction: { ...e.traction },
|
|
216
|
+
maxSpeed: s(e.maxSpeed ?? 13.9, "maximum speed", Number.MIN_VALUE),
|
|
217
|
+
wheelbase: s(e.wheelbase, "wheelbase", Number.MIN_VALUE),
|
|
218
|
+
rearOffset: s(e.rearOffset ?? 0, "rear offset", -Number.MAX_VALUE),
|
|
219
|
+
forward: A(e.forward ?? [
|
|
220
|
+
0,
|
|
221
|
+
0,
|
|
222
|
+
1
|
|
223
|
+
]),
|
|
224
|
+
maxSteer: s(e.maxSteer ?? e.attachment?.maxSteeringAngle ?? .5, "steering angle", Number.MIN_VALUE, Math.PI / 2),
|
|
225
|
+
lookAhead: s(e.lookAhead ?? 2, "look-ahead distance", Number.MIN_VALUE),
|
|
226
|
+
lookAheadTime: s(e.lookAheadTime ?? .35, "look-ahead time", 0),
|
|
227
|
+
acceleration: s(e.acceleration ?? 3, "acceleration", Number.MIN_VALUE),
|
|
228
|
+
deceleration: s(e.deceleration ?? 4, "braking acceleration", Number.MIN_VALUE),
|
|
229
|
+
speedResponse: s(e.speedResponse ?? .75, "speed response time", Number.MIN_VALUE)
|
|
230
|
+
};
|
|
231
|
+
}
|
|
232
|
+
function M(e, t) {
|
|
233
|
+
let n = t * 8;
|
|
234
|
+
return {
|
|
235
|
+
steer: e[n + b],
|
|
236
|
+
throttle: e[n + x],
|
|
237
|
+
brake: e[n + S],
|
|
238
|
+
targetSpeed: e[n + C],
|
|
239
|
+
speed: e[n + w],
|
|
240
|
+
progress: e[n + T],
|
|
241
|
+
wetness: e[n + E],
|
|
242
|
+
segment: e[n + D]
|
|
243
|
+
};
|
|
244
|
+
}
|
|
245
|
+
function N(e, t, r) {
|
|
246
|
+
if (e === 0) return 0;
|
|
247
|
+
let a = Math.abs(e) * t, o = 0, s = r;
|
|
248
|
+
if (i(r) < a * n(r)) return Math.sign(e);
|
|
249
|
+
for (let e = 0; e < 30; e++) {
|
|
250
|
+
let e = .5 * (o + s);
|
|
251
|
+
i(e) < a * n(e) ? o = e : s = e;
|
|
252
|
+
}
|
|
253
|
+
return Math.sign(e) * ((o + s) / (2 * r));
|
|
254
|
+
}
|
|
255
|
+
function P(e, n, [r, i, a]) {
|
|
256
|
+
let o = e[n + 3], s = e[n + 4], c = e[n + 5], l = e[n + 6], u = o * o + s * s + c * c + l * l;
|
|
257
|
+
if (!(u > 0)) throw new t("state", "Choose a live rigid body row for every road driver.");
|
|
258
|
+
let d = 2 / u, f = d * (s * a - c * i), p = d * (c * r - o * a), m = d * (o * i - s * r);
|
|
259
|
+
return [
|
|
260
|
+
r + l * f + s * m - c * p,
|
|
261
|
+
i + l * p + c * f - o * m,
|
|
262
|
+
a + l * m + o * p - s * f
|
|
263
|
+
];
|
|
264
|
+
}
|
|
265
|
+
function F(e, n, i, o, s, c, l, u) {
|
|
266
|
+
let d = e.body * u;
|
|
267
|
+
if (d + u > s.length) throw new t("state", "Choose a road chassis row within the published RigidSolver capacity.");
|
|
268
|
+
let f = i * 8, h = P(s, d, e.forward), _ = r(h[0], h[2]);
|
|
269
|
+
if (!(_ > 0)) {
|
|
270
|
+
o[f + x] = 0, o[f + S] = 1;
|
|
271
|
+
return;
|
|
272
|
+
}
|
|
273
|
+
let v = h[0] / _, y = h[2] / _, b = s[d] + l[0] + h[0] * e.rearOffset, C = s[d + 2] + l[2] + h[2] * e.rearOffset, w = s[d + 10] * v + s[d + 12] * y, T = p(e.route, b, C, o[f + D]), E = e.lookAhead + Math.max(0, w) * e.lookAheadTime, [O, , k] = m(e.route, T, b, C, E), A = O - b, j = k - C, M = A * A + j * j, F = M > 0 ? 2 * (y * A - v * j) / M : 0, I = c ? c[e.wetnessWord] : e.wetness;
|
|
274
|
+
if (I === void 0) throw new t("state", "Use a road wetness index within the published field.");
|
|
275
|
+
let L = a(e.traction, I), R = Math.min(e.deceleration, L * n.gravity), z = g(e.route, T, e.maxSpeed, R, L, n.gravity), B = (z - w) / e.speedResponse, V = Math.max(0, Math.min(1, B / e.acceleration)), H = Math.max(0, Math.min(1, -B / e.deceleration));
|
|
276
|
+
o.set([
|
|
277
|
+
N(F, e.wheelbase, e.maxSteer),
|
|
278
|
+
V,
|
|
279
|
+
H,
|
|
280
|
+
z,
|
|
281
|
+
w,
|
|
282
|
+
T.progress,
|
|
283
|
+
I,
|
|
284
|
+
T.segment
|
|
285
|
+
], f);
|
|
286
|
+
}
|
|
287
|
+
var I = class e {
|
|
288
|
+
static async create(t = {}, n) {
|
|
289
|
+
n?.throwIfAborted();
|
|
290
|
+
let r = await import("@margoengine/physics/world");
|
|
291
|
+
return n?.throwIfAborted(), new e(r, t);
|
|
292
|
+
}
|
|
293
|
+
constructor(e, { id: n = "road-driver", wetnessField: r }) {
|
|
294
|
+
if (this.port = e, this.bound = /* @__PURE__ */ new Set(), r && r.unit !== "1") throw new t("state", "Use a dimensionless relative wetness field for the road driver.");
|
|
295
|
+
this.id = n, this.rainfall = r && { ...r }, this.commandsField = {
|
|
296
|
+
name: `${n}:commands`,
|
|
297
|
+
kind: "bodySet",
|
|
298
|
+
quantity: "roadDriveCommand",
|
|
299
|
+
unit: "1, m/s, m",
|
|
300
|
+
centring: "particle"
|
|
301
|
+
};
|
|
302
|
+
}
|
|
303
|
+
describe() {
|
|
304
|
+
return {
|
|
305
|
+
id: this.id,
|
|
306
|
+
rate: 0,
|
|
307
|
+
stableRate: () => 0,
|
|
308
|
+
fields: [
|
|
309
|
+
{
|
|
310
|
+
field: this.port.BODIES_FIELD,
|
|
311
|
+
access: "read"
|
|
312
|
+
},
|
|
313
|
+
{
|
|
314
|
+
field: this.commandsField,
|
|
315
|
+
access: "write"
|
|
316
|
+
},
|
|
317
|
+
...this.rainfall ? [{
|
|
318
|
+
field: this.rainfall,
|
|
319
|
+
access: "read"
|
|
320
|
+
}] : []
|
|
321
|
+
]
|
|
322
|
+
};
|
|
323
|
+
}
|
|
324
|
+
init(e, n) {
|
|
325
|
+
if (!n.vehicles?.length) throw new t("state", "Provide at least one road driver vehicle.");
|
|
326
|
+
let i = 0, a = /* @__PURE__ */ new Set(), o = n.vehicles.map((e) => {
|
|
327
|
+
let n = j(e, i);
|
|
328
|
+
if (a.has(n.body)) throw new t("state", "Use only one road driver per rigid chassis.");
|
|
329
|
+
return a.add(n.body), i += n.attachment?.motionStateWords ?? 0, n;
|
|
330
|
+
}), c = {
|
|
331
|
+
host: e,
|
|
332
|
+
drivers: o,
|
|
333
|
+
gravity: r(...e.gravity),
|
|
334
|
+
cellSize: n.cellSize === void 0 ? void 0 : s(n.cellSize, "world cell size", Number.MIN_VALUE),
|
|
335
|
+
commands: e.allocate("road-controls", o.length * 8 * y, "f64", v),
|
|
336
|
+
motion: i ? e.allocate("vehicle-motion", i * y, "f64", v) : void 0
|
|
337
|
+
}, l = this.port.regionWords(e.memory, c.commands);
|
|
338
|
+
for (let e = 0; e < o.length; e++) {
|
|
339
|
+
l[e * 8 + D] = -1, l[e * 8 + E] = o[e].wetness;
|
|
340
|
+
let t = o[e];
|
|
341
|
+
t.attachment && t.attachment.writeMotionState(this.motion(c, t));
|
|
342
|
+
}
|
|
343
|
+
this.state = c;
|
|
344
|
+
}
|
|
345
|
+
step(e) {
|
|
346
|
+
let n = this.requireState(), r = e.inputs.get(this.port.BODIES_FIELD.name);
|
|
347
|
+
if (!r) return;
|
|
348
|
+
let { region: i } = r;
|
|
349
|
+
if (i.type !== "f32" || i.layout !== 2 || i.byteLength % (this.port.BODY_WORDS * Float32Array.BYTES_PER_ELEMENT)) throw new t("state", "Use the RigidSolver layout-2 bodies port for road driving.");
|
|
350
|
+
let a = r.cell.map((t, n) => t - e.cell[n]);
|
|
351
|
+
if (n.cellSize) for (let e = 0; e < 3; e++) a[e] = a[e] * n.cellSize;
|
|
352
|
+
else if (a.some((e) => e !== 0)) throw new t("state", "Set the road driver world cell size for cross-cell queries.");
|
|
353
|
+
let o = this.rainfall ? e.inputs.get(this.rainfall.name) : void 0;
|
|
354
|
+
if (o && (o.region.type !== "f64" || o.region.layout !== v)) throw new t("state", "Use a layout-1 f64 relative wetness port from the weather solver.");
|
|
355
|
+
let s = this.port.regionWords(n.host.memory, n.commands), c = this.port.regionWords(n.host.memory, i), l = o ? this.port.regionWords(n.host.memory, o.region) : void 0;
|
|
356
|
+
for (let e = 0; e < n.drivers.length; e++) F(n.drivers[e], n, e, s, c, l, a, this.port.BODY_WORDS);
|
|
357
|
+
}
|
|
358
|
+
exportPorts(e) {
|
|
359
|
+
return [{
|
|
360
|
+
field: this.commandsField.name,
|
|
361
|
+
region: this.requireState().commands,
|
|
362
|
+
tick: e,
|
|
363
|
+
cell: _
|
|
364
|
+
}];
|
|
365
|
+
}
|
|
366
|
+
importPorts(e) {}
|
|
367
|
+
stateRegions() {
|
|
368
|
+
return [this.requireState().commands];
|
|
369
|
+
}
|
|
370
|
+
extraState() {
|
|
371
|
+
let { motion: e } = this.requireState();
|
|
372
|
+
return e ? [e] : [];
|
|
373
|
+
}
|
|
374
|
+
read(e) {
|
|
375
|
+
let t = this.requireState();
|
|
376
|
+
return this.driver(t, e), M(this.port.regionWords(t.host.memory, t.commands), e);
|
|
377
|
+
}
|
|
378
|
+
bindVehicle(e, n, r) {
|
|
379
|
+
let i = this.requireState(), a = this.driver(i, r), o = a.attachment;
|
|
380
|
+
if (!o || !n.owns(o.chassis) || e.memory !== i.host.memory || this.bound.has(r)) throw new t("state", "Use each configured road attachment once on its original PhysicsWorld and Physics body.");
|
|
381
|
+
this.bound.add(r);
|
|
382
|
+
let s = n.on("beforestep", () => {
|
|
383
|
+
if (!o.valid) return;
|
|
384
|
+
let t = e.fields.port(this.commandsField.name);
|
|
385
|
+
o.readMotionState(this.motion(i, a)), t && o.setInput(M(this.port.regionWords(e.memory, t.region), r));
|
|
386
|
+
}), c = n.on("afterstep", () => {
|
|
387
|
+
o.valid && o.writeMotionState(this.motion(i, a));
|
|
388
|
+
}), l = !0;
|
|
389
|
+
return () => {
|
|
390
|
+
l && (l = !1, s(), c(), this.bound.delete(r));
|
|
391
|
+
};
|
|
392
|
+
}
|
|
393
|
+
motion(e, t) {
|
|
394
|
+
return this.port.regionWords(e.host.memory, e.motion).subarray(t.motionOffset, t.motionOffset + t.attachment.motionStateWords);
|
|
395
|
+
}
|
|
396
|
+
driver(e, n) {
|
|
397
|
+
if (!Number.isSafeInteger(n) || n < 0 || n >= e.drivers.length) throw new t("state", "Use an index in the configured road drivers.");
|
|
398
|
+
return e.drivers[n];
|
|
399
|
+
}
|
|
400
|
+
requireState() {
|
|
401
|
+
if (!this.state) throw new t("state", "Add RoadDriver to a PhysicsWorld before reading or binding it.");
|
|
402
|
+
return this.state;
|
|
403
|
+
}
|
|
404
|
+
};
|
|
405
|
+
//#endregion
|
|
406
|
+
//#region src/road-vehicle.ts
|
|
407
|
+
async function L(e, t, n, r) {
|
|
408
|
+
r?.throwIfAborted();
|
|
409
|
+
let { Vehicle: i, WindForce: a } = await import("@margoengine/physics");
|
|
410
|
+
r?.throwIfAborted();
|
|
411
|
+
let { size: o, mass: c, position: l, colliderOffset: u, wind: d, ...f } = n, p = [
|
|
412
|
+
.5 * s(o[0], "chassis size X", Number.MIN_VALUE),
|
|
413
|
+
.5 * s(o[1], "chassis size Y", Number.MIN_VALUE),
|
|
414
|
+
.5 * s(o[2], "chassis size Z", Number.MIN_VALUE)
|
|
415
|
+
], m = e.addBody(t, {
|
|
416
|
+
position: l,
|
|
417
|
+
colliders: {
|
|
418
|
+
shape: "box",
|
|
419
|
+
halfExtents: p,
|
|
420
|
+
offset: u,
|
|
421
|
+
mass: s(c, "chassis mass", Number.MIN_VALUE)
|
|
422
|
+
}
|
|
423
|
+
});
|
|
424
|
+
try {
|
|
425
|
+
return {
|
|
426
|
+
body: m,
|
|
427
|
+
vehicle: new i(e, m, f),
|
|
428
|
+
wind: d ? new a(e, {
|
|
429
|
+
velocity: d,
|
|
430
|
+
filter: { predicate: (e) => e === m }
|
|
431
|
+
}) : void 0
|
|
432
|
+
};
|
|
433
|
+
} catch (t) {
|
|
434
|
+
throw e.removeBody(m), t;
|
|
435
|
+
}
|
|
436
|
+
}
|
|
437
|
+
//#endregion
|
|
438
|
+
export { k as ROAD_DRIVER_LAYOUT, O as ROAD_DRIVER_WORDS, I as RoadDriver, o as RoadSurfaces, L as addRoadVehicle, h as roadCornerSpeed, l as roadDrivePath, a as roadGrip };
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
//#region src/frame.ts
|
|
2
|
+
function e(e, t, n, r, i, a) {
|
|
3
|
+
let o = e.length, s = e.closed && t === o ? 0 : t;
|
|
4
|
+
e.atDistance(s, r);
|
|
5
|
+
let c = s - n, l = s + n;
|
|
6
|
+
e.atDistance(e.closed && c < 0 ? c + o : Math.max(0, c), i), e.atDistance(e.closed && l > o ? l - o : Math.min(o, l), a);
|
|
7
|
+
}
|
|
8
|
+
//#endregion
|
|
9
|
+
export { e as t };
|
package/dist/frame.d.ts
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
1
|
+
import type { Spline } from '@margoengine/splines';
|
|
2
|
+
/** One periodic road frame sample; open roads retain their one-sided end tangents. */
|
|
3
|
+
export declare function sampleRoadFrame(curve: Spline, distance: number, epsilon: number, center: Float64Array, before: Float64Array, after: Float64Array): void;
|
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
import { Spline } from '@margoengine/splines';
|
|
2
|
+
import { type Point3 } from './design-mesh.js';
|
|
3
|
+
/** Per-highway fallback policy; these are rendering assumptions, not inferred survey measurements. */
|
|
4
|
+
export interface HighwayDefaults {
|
|
5
|
+
/** Default lane count. */
|
|
6
|
+
lanes: number;
|
|
7
|
+
/** Default width per lane, metres. */
|
|
8
|
+
laneWidth: number;
|
|
9
|
+
}
|
|
10
|
+
/** Import settings shared by GeoJSON and OSM XML/Overpass JSON. */
|
|
11
|
+
export interface RoadImportOptions {
|
|
12
|
+
/** WGS84 [longitude,latitude,height] local origin. Default first road coordinate. */
|
|
13
|
+
origin?: readonly [number, number, number];
|
|
14
|
+
/** Override WGS84 conversion, returning local Y-up [x,y,z] metres. Default WGS84 ECEF to east/up/south. */
|
|
15
|
+
project?: (coordinate: readonly number[]) => Point3;
|
|
16
|
+
/** Override fallback lanes/width by highway class. Default main roads 2*3.5 m; service/track 1*3 m; paths 1*1.5 m. */
|
|
17
|
+
defaults?: Readonly<Record<string, HighwayDefaults>>;
|
|
18
|
+
/** Allowed height difference when joining GeoJSON line crossings. Default 0.5 m. OSM trusts node identities instead. */
|
|
19
|
+
crossingHeight?: number;
|
|
20
|
+
/** Checked before and after synchronous import. Default none. */
|
|
21
|
+
signal?: AbortSignal;
|
|
22
|
+
}
|
|
23
|
+
/** A graph edge that can be passed directly to road(), lanes() or roadGraph(). */
|
|
24
|
+
export interface ImportedRoad {
|
|
25
|
+
/** Source feature/way identifier, shared by all its split edges. */
|
|
26
|
+
sourceId: string;
|
|
27
|
+
/** Network vertex IDs. */
|
|
28
|
+
from: number;
|
|
29
|
+
/** Network vertex ID at the authored end. */
|
|
30
|
+
to: number;
|
|
31
|
+
/** Two-point, exactly straight spline for this split source-polyline segment. */
|
|
32
|
+
curve: Spline;
|
|
33
|
+
/** Owned local Y-up endpoints. */
|
|
34
|
+
points: [Point3, Point3];
|
|
35
|
+
/** Total width in metres. */
|
|
36
|
+
width: number;
|
|
37
|
+
/** Lane count. */
|
|
38
|
+
lanes: number;
|
|
39
|
+
/** Per-lane metre widths, equal when no width:lanes tag exists. */
|
|
40
|
+
laneWidths: number[];
|
|
41
|
+
/** Relative to source point order. */
|
|
42
|
+
direction: 'both' | 'forward' | 'reverse';
|
|
43
|
+
/** OSM layer or inferred 1 for bridge, -1 for tunnel, 0 otherwise. */
|
|
44
|
+
layer: number;
|
|
45
|
+
/** True when explicitly tagged as a bridge. */
|
|
46
|
+
bridge: boolean;
|
|
47
|
+
/** True when explicitly tagged as a tunnel. */
|
|
48
|
+
tunnel: boolean;
|
|
49
|
+
/** Owned source string tags, including unrecognised tags. */
|
|
50
|
+
tags: Record<string, string>;
|
|
51
|
+
}
|
|
52
|
+
/** Local road network and reproducible geographic origin. */
|
|
53
|
+
export interface RoadNetwork {
|
|
54
|
+
/** Owned Y-up node coordinates. */
|
|
55
|
+
nodes: Point3[];
|
|
56
|
+
/** Split road edges, preserving source IDs. */
|
|
57
|
+
roads: ImportedRoad[];
|
|
58
|
+
/** Owned WGS84 origin; null for an empty network or custom projector. */
|
|
59
|
+
origin: [number, number, number] | null;
|
|
60
|
+
/** Explicit metadata fallback and zero-length-segment diagnostics. */
|
|
61
|
+
warnings: string[];
|
|
62
|
+
}
|
|
63
|
+
/** GeoJSON RFC 7946 line features; mixed non-line geometries are intentionally ignored. */
|
|
64
|
+
export declare function importGeoJSON(document: unknown, options?: RoadImportOptions): RoadNetwork;
|
|
65
|
+
/** Read OSM 0.6 XML or Overpass JSON elements, preserving explicit node topology and grade separation. */
|
|
66
|
+
export declare function importOSM(document: unknown, options?: RoadImportOptions): RoadNetwork;
|