Boucle du Hainaut : application de réalité augmentée (v46)

PWA three.js + LocAR : pylônes et câbles de la ligne 380 kV sur le tracé
projeté, à l'échelle réelle, cachés par le relief (SPW 10 m), les obstacles
LiDAR (1/2/4 m) et les bâtiments OpenStreetMap. Photo avant/après, calage
de la vue, mini-carte, hors-ligne complet.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
mathieuandClaude Opus 5.5 committed 2026-11-15 22:19:19 +01:00
commit 3662fd4993
3571 files changed
+83668

No files matched your search

+143
View File
@@ -0,0 +1,143 @@
// Étape 7a : bâtiments OpenStreetMap utilisés comme « écrans » invisibles.
// Ils n'écrivent que dans le tampon de profondeur : les pylônes et câbles situés derrière
// un bâtiment ne sont pas dessinés, mais le bâtiment lui-même reste invisible (on voit la caméra).
//
// Données : extraites une fois d'OpenStreetMap (© contributeurs OpenStreetMap, ODbL) dans un couloir
// de ~3 km autour du tracé, puis découpées en dalles de 1 km × 1 km (data/bat/), hébergées avec l'app :
// aucune position n'est envoyée à un service extérieur pendant la balade.
// Format d'une dalle : { h: [hauteurs en m], p: [[x0,y0,x1,y1,…] en décimètres depuis le coin SO de la dalle] }
// dans la même projection locale que l'app (équirectangulaire, LAT0 = 50,6°).
import * as THREE from 'three';
import { withClearZone } from './terrain.js?v=46';
const TILE = 1000; // m
export class BuildingOccluders {
constructor({ scene, projection, terrain, baseUrl = 'data/bat/', radius = 800, onError = () => {} }) {
this.scene = scene;
this.terrain = terrain;
this.proj = projection;
this.base = baseUrl;
this.radius = radius;
this.onError = onError;
this.index = null; // Set des dalles existantes
this.tiles = new Map(); // clé → { mesh, count } | 'loading'
this.origin = null; // décalage projection → monde LocAR
this.visible = false; // afficher les bâtiments (contrôle)
this.count = 0;
// Petite zone dégagée autour de l'utilisateur (au cas où le GPS le place « dans » un bâtiment)
this.clear = { center: { value: new THREE.Vector2() }, radius: { value: 4 } };
this.hidden = withClearZone(new THREE.MeshBasicMaterial({ colorWrite: false }), this.clear);
this.shown = withClearZone(new THREE.MeshBasicMaterial({ color: 0x4cd137, transparent: true, opacity: 0.45 }), this.clear);
this.ready = fetch(this.base + 'index.json')
.then((r) => { if (!r.ok) throw new Error(`HTTP ${r.status}`); return r.json(); })
.then((j) => { this.index = new Set(j.tiles); this.meta = j; this.build = j.build || '0'; })
.catch((e) => this.onError(`bâtiments non chargés (${e.message})`));
}
// locar.lonLatToWorldCoords(lon, lat) = [X - ox, -(Y - oy)] → on retrouve (ox, oy)
setOrigin(locar, lon, lat) {
const [X, Y] = this.proj.project(lon, lat);
const [wx, wz] = locar.lonLatToWorldCoords(lon, lat);
this.origin = [X - wx, Y + wz];
}
setClearZone(x, z, radius) {
this.clear.center.value.set(x, z);
this.clear.radius.value = radius;
}
setVisible(v) {
this.visible = v;
for (const t of this.tiles.values()) if (t !== 'loading') t.mesh.material = v ? this.shown : this.hidden;
}
// Active / désactive le masquage (les bâtiments restent chargés)
setEnabled(v) {
this.enabled = v;
for (const t of this.tiles.values()) if (t !== 'loading') t.mesh.visible = v;
}
// Charge les dalles proches de (lon, lat), libère les lointaines
update(lon, lat) {
if (!this.index || !this.origin) return;
const [X, Y] = this.proj.project(lon, lat);
const need = new Set();
for (let kx = Math.floor((X - this.radius) / TILE); kx <= Math.floor((X + this.radius) / TILE); kx++) {
for (let ky = Math.floor((Y - this.radius) / TILE); ky <= Math.floor((Y + this.radius) / TILE); ky++) {
const key = `${kx}_${ky}`;
if (this.index.has(key)) need.add(key);
}
}
for (const key of need) if (!this.tiles.has(key)) this.load(key);
for (const [key, t] of this.tiles) {
if (need.has(key) || t === 'loading') continue;
const [kx, ky] = key.split('_').map(Number);
const cx = (kx + 0.5) * TILE, cy = (ky + 0.5) * TILE;
if (Math.hypot(cx - X, cy - Y) > this.radius + 2 * TILE) { // assez loin : on libère
this.scene.remove(t.mesh); t.mesh.geometry.dispose(); this.tiles.delete(key); this.count -= t.count;
}
}
}
dispose() {
this.disposed = true;
for (const t of this.tiles.values()) if (t !== 'loading') { this.scene.remove(t.mesh); t.mesh.geometry.dispose(); }
this.tiles.clear(); this.count = 0;
}
async load(key) {
this.tiles.set(key, 'loading');
try {
const r = await fetch(`${this.base}${key}.json?b=${this.build}`); // ?b= : nouvelle extraction → nouvelles URL
if (!r.ok) throw new Error(`HTTP ${r.status}`);
const d = await r.json();
await this.terrain.ensure(this.terrain.neighbours(key));
if (this.disposed) return;
const [kx, ky] = key.split('_').map(Number);
const ox = kx * TILE - this.origin[0], oy = ky * TILE - this.origin[1];
const geoms = [];
d.p.forEach((ring, i) => {
const pts = [];
let lo = Infinity, sum = 0;
for (let j = 0; j < ring.length; j += 2) {
pts.push(new THREE.Vector2(ox + ring[j] / 10, oy + ring[j + 1] / 10));
const a = this.terrain.alt(kx * TILE + ring[j] / 10, ky * TILE + ring[j + 1] / 10);
lo = Math.min(lo, a); sum += a;
}
if (pts.length < 3) return;
// Posé sur le sol : base un peu sous le point le plus bas (pente), toit à sol moyen + hauteur
const base = lo - 1, top = sum / pts.length + d.h[i];
const g = new THREE.ExtrudeGeometry(new THREE.Shape(pts), { depth: top - base, bevelEnabled: false });
g.translate(0, 0, base);
geoms.push(g);
});
const geom = mergeGeometries(geoms);
geom.rotateX(-Math.PI / 2); // forme dans (est, nord), extrusion vers le haut → monde (x, y, -nord)
const mesh = new THREE.Mesh(geom, this.visible ? this.shown : this.hidden);
mesh.renderOrder = -1; // dessinés avant les pylônes et câbles
mesh.frustumCulled = false;
mesh.visible = this.enabled !== false;
this.scene.add(mesh);
this.tiles.set(key, { mesh, count: d.h.length });
this.count += d.h.length;
} catch (e) {
this.tiles.delete(key);
this.onError(`dalle de bâtiments ${key} : ${e.message}`);
}
}
}
// Fusion simple (positions seulement : ces maillages ne sont pas éclairés)
function mergeGeometries(geoms) {
let n = 0;
const arrays = geoms.map((g) => { const a = (g.index ? g.toNonIndexed() : g).attributes.position.array; n += a.length; return a; });
const pos = new Float32Array(n);
let o = 0;
for (const a of arrays) { pos.set(a, o); o += a.length; }
geoms.forEach((g) => g.dispose());
const out = new THREE.BufferGeometry();
out.setAttribute('position', new THREE.BufferAttribute(pos, 3));
return out;
}