Spaces:
Running on Zero

bpHigh commited on
Commit
528174b
·
verified ·
1 Parent(s): 33fc83e

Clear cab view for standing trains; see-through shed at Groningen

Browse files
openenv/rail_openenv/web/twin/SOURCES.md CHANGED
@@ -36,6 +36,9 @@ north of 53.22 N, 6.19 E (equirectangular), stored as integer decimetres.
36
  **Drawn (illustrative):**
37
  - Building heights: OSM `height` or `building:levels` where tagged, else by building type (a house 7 m, a barn 8 m,
38
  a warehouse 9 m).
 
 
 
39
  - Heights of landmarks without a tag: windmills 16 m, wind turbines a 60 m hub, towers 25 m.
40
  - Trees: the mapped trees and tree rows, plus woods filled at about one tree per 120 m² (capped at 60,000).
41
  - Train models: every train is drawn as a two-car unit (about 55 m), white, with a blue stripe for stopping trains and
 
36
  **Drawn (illustrative):**
37
  - Building heights: OSM `height` or `building:levels` where tagged, else by building type (a house 7 m, a barn 8 m,
38
  a warehouse 9 m).
39
+ - Buildings the track runs through (Groningen's train shed and two buildings that bridge the line) are drawn from a
40
+ 5.5 m clearance up and see-through, so trains pass under them and stay visible.
41
+ - The cab view of a standing train is from the end it will leave from (the driver changes ends).
42
  - Heights of landmarks without a tag: windmills 16 m, wind turbines a 60 m hub, towers 25 m.
43
  - Trees: the mapped trees and tree rows, plus woods filled at about one tree per 120 m² (capped at 60,000).
44
  - Train models: every train is drawn as a two-car unit (about 55 m), white, with a blue stripe for stopping trains and
openenv/rail_openenv/web/twin/geo.js CHANGED
@@ -146,3 +146,58 @@ export class Line {
146
  return offsets.map((d) => this.place(dir, lw, gn, r.kmAtS(s0 + d), lanes));
147
  }
148
  }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
146
  return offsets.map((d) => this.place(dir, lw, gn, r.kmAtS(s0 + d), lanes));
147
  }
148
  }
149
+
150
+ // Buildings the trains' track runs through: OSM maps Groningen's train shed (building=train_station) and buildings
151
+ // that bridge the line as footprints over the rails. They are drawn from this clearance up, so trains pass under them
152
+ // (drawn solid from the ground, they swallowed standing trains and filled the cab view with a wall).
153
+ export const CLEARANCE = 5.5;
154
+
155
+ export function overTrack(twin, step = 4, cell = 50) {
156
+ const grid = new Map();
157
+ for (const r of Object.values(twin.routes)) {
158
+ const P = [];
159
+ for (let i = 0; i < r.p.length; i += 2) P.push([r.p[i] / 10, r.p[i + 1] / 10]);
160
+ for (let i = 0; i + 1 < P.length; i++) {
161
+ const [ax, ay] = P[i], [bx, by] = P[i + 1];
162
+ const n = Math.max(1, Math.floor(Math.hypot(bx - ax, by - ay) / step));
163
+ for (let j = 0; j < n; j++) {
164
+ const x = ax + (bx - ax) * j / n, y = ay + (by - ay) * j / n;
165
+ const key = `${Math.floor(x / cell)},${Math.floor(y / cell)}`;
166
+ let list = grid.get(key);
167
+ if (!list) grid.set(key, (list = []));
168
+ list.push(x, y);
169
+ }
170
+ }
171
+ }
172
+ const inside = (x, y, ring) => {
173
+ let c = false;
174
+ for (let i = 0, j = ring.length - 1; i < ring.length; j = i++) {
175
+ const [xi, yi] = ring[i], [xj, yj] = ring[j];
176
+ if ((yi > y) !== (yj > y) && x < (xj - xi) * (y - yi) / (yj - yi) + xi) c = !c;
177
+ }
178
+ return c;
179
+ };
180
+ const out = new Set();
181
+ const B = twin.buildings;
182
+ for (let k = 0; k < B.p.length; k++) {
183
+ const ring = [];
184
+ for (let i = 0; i < B.p[k].length; i += 2) ring.push([B.p[k][i] / 10, B.p[k][i + 1] / 10]);
185
+ if (ring.length < 3) continue;
186
+ let x0 = Infinity, y0 = Infinity, x1 = -Infinity, y1 = -Infinity;
187
+ for (const [x, y] of ring) {
188
+ x0 = Math.min(x0, x); y0 = Math.min(y0, y); x1 = Math.max(x1, x); y1 = Math.max(y1, y);
189
+ }
190
+ search:
191
+ for (let cx = Math.floor(x0 / cell); cx <= Math.floor(x1 / cell); cx++) {
192
+ for (let cy = Math.floor(y0 / cell); cy <= Math.floor(y1 / cell); cy++) {
193
+ const list = grid.get(`${cx},${cy}`);
194
+ if (!list) continue;
195
+ for (let i = 0; i < list.length; i += 2) {
196
+ const x = list[i], y = list[i + 1];
197
+ if (x >= x0 && x <= x1 && y >= y0 && y <= y1 && inside(x, y, ring)) { out.add(k); break search; }
198
+ }
199
+ }
200
+ }
201
+ }
202
+ return out;
203
+ }
openenv/rail_openenv/web/twin/motion.js CHANGED
@@ -278,7 +278,10 @@ export class Sim {
278
  const facing = sg.prev ? sg.prev.dir : sg.next ? sg.next.dir : "E";
279
  const ref = sg.prev || sg.next;
280
  return {
281
- unit: un.id, km: sg.km, dir: facing, moving: false, speed: 0, code: sg.code,
 
 
 
282
  lw: sg.code === this.codes[0] && sg.track ? sg.track : ref ? lwTrack(ref, facing === "W") : "8",
283
  gn: sg.code === this.codes[this.codes.length - 1] && sg.track ? sg.track : ref ? gnTrack(ref) : "5a",
284
  lanes: sg.lane ? { [sg.code]: sg.lane } : null, train: sg.next ? sg.next.train : sg.prev ? sg.prev.train : "",
 
278
  const facing = sg.prev ? sg.prev.dir : sg.next ? sg.next.dir : "E";
279
  const ref = sg.prev || sg.next;
280
  return {
281
+ // cab: a set that will run again is driven from the end it leaves from (the driver changes ends), so the cab
282
+ // looks down the line it will take, not past the buffers it came in against
283
+ unit: un.id, km: sg.km, dir: facing, cab: sg.next ? sg.next.dir : facing, moving: false, speed: 0,
284
+ code: sg.code,
285
  lw: sg.code === this.codes[0] && sg.track ? sg.track : ref ? lwTrack(ref, facing === "W") : "8",
286
  gn: sg.code === this.codes[this.codes.length - 1] && sg.track ? sg.track : ref ? gnTrack(ref) : "5a",
287
  lanes: sg.lane ? { [sg.code]: sg.lane } : null, train: sg.next ? sg.next.train : sg.prev ? sg.prev.train : "",
openenv/rail_openenv/web/twin/scene.js CHANGED
@@ -680,7 +680,7 @@ export class TwinScene {
680
  if (tm && tm.group.visible && tm.state) {
681
  const s = tm.state;
682
  if (this.mode === "cab") {
683
- const pts = this.line.body(s.dir, s.lw, s.gn, s.km, [CAR + 0.5, CAR + 160], s.lanes);
684
  this.camera.position.set(pts[0][0], 3.4, -pts[0][1]);
685
  this.camera.lookAt(pts[1][0], 2.2, -pts[1][1]);
686
  } else {
 
680
  if (tm && tm.group.visible && tm.state) {
681
  const s = tm.state;
682
  if (this.mode === "cab") {
683
+ const pts = this.line.body(s.cab || s.dir, s.lw, s.gn, s.km, [CAR + 0.5, CAR + 160], s.lanes);
684
  this.camera.position.set(pts[0][0], 3.4, -pts[0][1]);
685
  this.camera.lookAt(pts[1][0], 2.2, -pts[1][1]);
686
  } else {
openenv/rail_openenv/web/twin/world.js CHANGED
@@ -6,6 +6,7 @@
6
 
7
  import * as THREE from "three";
8
  import { mergeGeometries } from "three/addons/utils/BufferGeometryUtils.js";
 
9
 
10
  export const TILE = 2000; // metres: tile size for culling
11
  const Y = { landcover: 0.02, farmland: 0.01, water: 0.05, waterway: 0.06, road: 0.09, platform: 0, track: 0.12 };
@@ -53,6 +54,13 @@ class Buf {
53
  const ny = (bz - az) * (cx - ax) - (bx - ax) * (cz - az);
54
  if (ny >= 0) this.idx.push(a, b, c); else this.idx.push(a, c, b);
55
  }
 
 
 
 
 
 
 
56
  geometry() {
57
  const g = new THREE.BufferGeometry();
58
  g.setAttribute("position", new THREE.Float32BufferAttribute(this.pos, 3));
@@ -210,20 +218,22 @@ export function buildWorld(twin, scene, opts = {}) {
210
  }
211
  root.add(meshes(rd, flat(vc(), -7), { receive: true }));
212
 
213
- // buildings: walls and flat roofs, coloured by kind
214
- const bl = new Map();
215
  const B = twin.buildings;
 
216
  const wall = new THREE.Color(), roof = new THREE.Color();
217
  for (let k = 0; k < B.p.length; k++) {
218
  const ring = decode(B.p[k]);
219
  if (ring.length < 3) continue;
220
  const h = B.h[k];
 
221
  const [wc, rc] = COLORS.building[B.k[k]] ?? COLORS.building[0];
222
  wall.setHex(wc).offsetHSL(0, 0, ((k * 29) % 7 - 3) * 0.012);
223
  roof.setHex(rc);
224
  let cx = 0, cy = 0;
225
  for (const [x, y] of ring) { cx += x; cy += y; }
226
- const b = tiled(bl, tileKey(cx / ring.length, cy / ring.length));
227
  // orientation: walls face outwards whichever way the ring runs
228
  let area = 0;
229
  for (let j = 0; j < ring.length; j++) {
@@ -234,7 +244,7 @@ export function buildWorld(twin, scene, opts = {}) {
234
  for (let j = 0; j < ring.length; j++) {
235
  const [ax, ay] = ring[j], [bx, by] = ring[(j + 1) % ring.length];
236
  const v = b.n;
237
- b.vert(ax, 0, -ay, wall); b.vert(bx, 0, -by, wall); b.vert(bx, h, -by, wall); b.vert(ax, h, -ay, wall);
238
  if (ccw) b.idx.push(v, v + 1, v + 2, v, v + 2, v + 3);
239
  else b.idx.push(v, v + 2, v + 1, v, v + 3, v + 2);
240
  }
@@ -244,10 +254,18 @@ export function buildWorld(twin, scene, opts = {}) {
244
  const v0 = b.n;
245
  for (const [x, y] of ring) b.vert(x, h, -y, roof);
246
  for (const [a, bb, c] of tris) b.up(v0 + a, v0 + bb, v0 + c);
 
 
 
 
 
247
  }
248
  const buildings = meshes(bl, vc({ flatShading: true }), { cast: true, receive: true });
249
  buildings.name = "buildings";
250
  root.add(buildings);
 
 
 
251
 
252
  // track: ballast with sleepers, then the two rails (1435 mm gauge)
253
  const tex = sleeperTexture();
 
6
 
7
  import * as THREE from "three";
8
  import { mergeGeometries } from "three/addons/utils/BufferGeometryUtils.js";
9
+ import { CLEARANCE, overTrack } from "./geo.js";
10
 
11
  export const TILE = 2000; // metres: tile size for culling
12
  const Y = { landcover: 0.02, farmland: 0.01, water: 0.05, waterway: 0.06, road: 0.09, platform: 0, track: 0.12 };
 
54
  const ny = (bz - az) * (cx - ax) - (bx - ax) * (cz - az);
55
  if (ny >= 0) this.idx.push(a, b, c); else this.idx.push(a, c, b);
56
  }
57
+ // a triangle that faces down (-y): the underside of a roof over the track
58
+ down(a, b, c) {
59
+ const P = this.pos;
60
+ const ax = P[3 * a], az = P[3 * a + 2], bx = P[3 * b], bz = P[3 * b + 2], cx = P[3 * c], cz = P[3 * c + 2];
61
+ const ny = (bz - az) * (cx - ax) - (bx - ax) * (cz - az);
62
+ if (ny < 0) this.idx.push(a, b, c); else this.idx.push(a, c, b);
63
+ }
64
  geometry() {
65
  const g = new THREE.BufferGeometry();
66
  g.setAttribute("position", new THREE.Float32BufferAttribute(this.pos, 3));
 
218
  }
219
  root.add(meshes(rd, flat(vc(), -7), { receive: true }));
220
 
221
+ // buildings: walls and flat roofs, coloured by kind; those over the track raised above it
222
+ const bl = new Map(), over = new Map();
223
  const B = twin.buildings;
224
+ const raised = overTrack(twin);
225
  const wall = new THREE.Color(), roof = new THREE.Color();
226
  for (let k = 0; k < B.p.length; k++) {
227
  const ring = decode(B.p[k]);
228
  if (ring.length < 3) continue;
229
  const h = B.h[k];
230
+ const base = raised.has(k) ? Math.min(CLEARANCE, h - 1) : 0;
231
  const [wc, rc] = COLORS.building[B.k[k]] ?? COLORS.building[0];
232
  wall.setHex(wc).offsetHSL(0, 0, ((k * 29) % 7 - 3) * 0.012);
233
  roof.setHex(rc);
234
  let cx = 0, cy = 0;
235
  for (const [x, y] of ring) { cx += x; cy += y; }
236
+ const b = tiled(base > 0 ? over : bl, tileKey(cx / ring.length, cy / ring.length));
237
  // orientation: walls face outwards whichever way the ring runs
238
  let area = 0;
239
  for (let j = 0; j < ring.length; j++) {
 
244
  for (let j = 0; j < ring.length; j++) {
245
  const [ax, ay] = ring[j], [bx, by] = ring[(j + 1) % ring.length];
246
  const v = b.n;
247
+ b.vert(ax, base, -ay, wall); b.vert(bx, base, -by, wall); b.vert(bx, h, -by, wall); b.vert(ax, h, -ay, wall);
248
  if (ccw) b.idx.push(v, v + 1, v + 2, v, v + 2, v + 3);
249
  else b.idx.push(v, v + 2, v + 1, v, v + 3, v + 2);
250
  }
 
254
  const v0 = b.n;
255
  for (const [x, y] of ring) b.vert(x, h, -y, roof);
256
  for (const [a, bb, c] of tris) b.up(v0 + a, v0 + bb, v0 + c);
257
+ if (base > 0) { // the underside, seen from the trains
258
+ const u0 = b.n;
259
+ for (const [x, y] of ring) b.vert(x, base, -y, wall);
260
+ for (const [a, bb, c] of tris) b.down(u0 + a, u0 + bb, u0 + c);
261
+ }
262
  }
263
  const buildings = meshes(bl, vc({ flatShading: true }), { cast: true, receive: true });
264
  buildings.name = "buildings";
265
  root.add(buildings);
266
+ // the raised ones are see-through, so the trains standing under Groningen's shed stay visible from above
267
+ root.add(meshes(over, vc({ flatShading: true, transparent: true, opacity: 0.4, depthWrite: false,
268
+ side: THREE.DoubleSide })));
269
 
270
  // track: ballast with sleepers, then the two rails (1435 mm gauge)
271
  const tex = sleeperTexture();