This commit is contained in:
trung09122002
2026-09-23 13:36:48 +07:00
parent dbcbf84dc3
commit 0a1b1b13f1
38 changed files with 2331 additions and 138 deletions

View File

@@ -12,7 +12,7 @@ const MAGIC = 0x314C5356; // 'VSL1' little-endian
// Phien ban ma nguon giao dien. TANG khi doi app.js / app3d.js / style.css.
// Hien ngay tren thanh tren cung de phan biet "chua sua" voi "trinh duyet con
// cache ban cu".
const UI_VERSION = '2026-09-19g';
const UI_VERSION = '2026-09-23a';
// ⚠️ HAI bang mau, mot cho moi khung. Chung KHONG duoc giong nhau.
//
@@ -62,9 +62,12 @@ const PALETTE = {
},
};
const T_SCAN = 1, T_CLOUD = 2, T_MAP_FULL = 3, T_MAP_PATCH = 4, T_PATH = 5;
const T_SCAN = 1, T_CLOUD = 2, T_MAP_FULL = 3, T_PATH = 5;
// 4 = kMapPatch, DA BO o phia server. Giu lo trong, khong tai dung.
// Ban do THE GIOI cua bo mo phong -- khung nhin "Gazebo". Tinh: gui mot lan.
const T_WORLD_MAP = 6;
// Ban do voxel 3D: tap day du (chia manh) va phan them/bot.
const T_VOXEL_FULL = 7, T_VOXEL_DELTA = 8;
// Ba ngu nghia cua mot tia. Lan lon chung la lam sai ca man hinh.
const R_NAN = 0xFFFF; // khong do duoc (duoi range_min)
@@ -77,6 +80,14 @@ const state = {
// anchorPose(). `scanDelta` den tu khung state, dung ngay khung scan ke tiep.
cloudPose: null, scanPose: null, scanDelta: null,
map: null, mapImage: null, mapVersion: 0,
// Ban do voxel 3D. `set` la Set CAC SO (khong phai chuoi): o 100k phan tu
// thi Set<number> re hon han Set<string> ca ve bo nho lan thoi gian.
// `dirty` = co gi doi ke tu lan dung lai hinh gan nhat.
voxels: {
epoch: 0, res: 0.1, ox: 0, oy: 0, oz: 0,
set: new Set(), total: 0, seq: 0, syncing: false, dirty: false,
},
voxelResyncAt: 0,
bytes: 0, lastBytes: 0, wireRate: 0,
};
@@ -127,6 +138,32 @@ function setConn(ok) {
// --- Khung nhi phan --------------------------------------------------------
// Khoa voxel dang SO. Ba chi so i16 -> mot so nguyen <= 2^48, van chinh xac
// tuyet doi trong double.
function voxelKey(x, y, z) {
return ((x + 32768) * 65536 + (y + 32768)) * 65536 + (z + 32768);
}
function voxelUnpack(k) {
const z = (k % 65536) - 32768;
k = (k - (z + 32768)) / 65536;
const y = (k % 65536) - 32768;
k = (k - (y + 32768)) / 65536;
return [k - 32768, y, z];
}
// Xin server gui lai ca tap. Ham han MOT lan moi giay: lech epoch thuong keo
// theo mot chum khung delta, va moi khung lai goi vao day.
function requestVoxelResync() {
const now = Date.now();
if (now - state.voxelResyncAt < 1000) return;
state.voxelResyncAt = now;
state.voxels.seq = 0;
if (ws && ws.readyState === 1) {
ws.send(JSON.stringify({ t: 'voxel_resync' }));
}
}
function onBinary(dv) {
if (dv.byteLength < 16 || dv.getUint32(0, true) !== MAGIC) return;
const type = dv.getUint8(4);
@@ -212,6 +249,84 @@ function onBinary(dv) {
return;
}
// --- Ban do voxel 3D ---
//
// Khoa la mot SO, khong phai chuoi: `((x+32768)*65536 + (y+32768))*65536 +
// (z+32768)` toi da 2^48, double bieu dien chinh xac tuyet doi. Set<string>
// o 100k phan tu la mot con duong chac chan toi giat hinh.
if (type === T_VOXEL_FULL) {
const res = dv.getFloat32(off, true);
const ox = dv.getFloat32(off + 4, true);
const oy = dv.getFloat32(off + 8, true);
const oz = dv.getFloat32(off + 12, true);
const epoch = dv.getUint32(off + 16, true);
const total = dv.getUint32(off + 20, true);
const chunkIndex = dv.getUint16(off + 24, true);
const chunkCount = dv.getUint16(off + 26, true);
const n = dv.getUint32(off + 28, true);
const v = state.voxels;
// Manh dau, hoac epoch la giua chung -> bo het lam lai. Tron hai epoch se
// ra mot ban do khong ton tai o dau ca.
if (chunkIndex === 0 || epoch !== v.epoch) {
v.set = new Set();
v.epoch = epoch;
v.res = res;
v.ox = ox; v.oy = oy; v.oz = oz;
v.total = total;
v.seq = 0;
v.syncing = true;
}
for (let i = 0; i < n; i++) {
const b = off + 32 + i * 6;
v.set.add(voxelKey(dv.getInt16(b, true), dv.getInt16(b + 2, true),
dv.getInt16(b + 4, true)));
}
v.dirty = true;
if (chunkIndex + 1 >= chunkCount) {
v.syncing = false;
// Dem lai truoc khi bao "xong": manh full-sync la khung THUONG, server
// drop duoc chung khi hang doi day. Thieu mot manh ma van ack thi ban do
// co mot lo hong khong bao gio duoc lap, va khong mot dau hieu nao.
if (v.set.size !== v.total) {
requestVoxelResync();
} else if (ws && ws.readyState === 1) {
ws.send(JSON.stringify({ t: 'ack_voxel', epoch: v.epoch }));
}
}
return;
}
if (type === T_VOXEL_DELTA) {
const epoch = dv.getUint32(off, true);
const seq = dv.getUint32(off + 4, true);
const nAdd = dv.getUint32(off + 8, true);
const nDel = dv.getUint32(off + 12, true);
const v = state.voxels;
// Lech epoch = server da dung lai ban do (vong dong). Hut so thu tu = mat
// khung. Ca hai deu khong va duoc, phai xin lai ca tap.
if (epoch !== v.epoch || (v.seq !== 0 && seq !== v.seq + 1)) {
requestVoxelResync();
return;
}
v.seq = seq;
let b = off + 16;
for (let i = 0; i < nAdd; i++, b += 6) {
v.set.add(voxelKey(dv.getInt16(b, true), dv.getInt16(b + 2, true),
dv.getInt16(b + 4, true)));
}
for (let i = 0; i < nDel; i++, b += 6) {
v.set.delete(voxelKey(dv.getInt16(b, true), dv.getInt16(b + 2, true),
dv.getInt16(b + 4, true)));
}
v.total = v.set.size;
v.dirty = true;
return;
}
if (type === T_PATH) {
const n = dv.getUint32(off, true);
const pts = new Float32Array(n * 2);
@@ -1069,6 +1184,11 @@ function draw3d() {
// SLAM: lop phu tren san, hien DAN khi quet -- day la phan "RViz". O chua
// quet trong suot nen tuong 3D van nhin xuyen qua.
// VOXEL: "cai gi dung tren san", bu cho tam phang cua v3dSetMap ("da quet
// toi dau"). Hai lop khong tranh nhau: tam phang o y = -0.002, voxel bat
// dau tu z_min = 0.05.
v3dSetVoxels(state.voxels, on('lyVoxel'));
if (state.map && on('lyMap')) {
v3dSetMap({ ...state.map, data: state.map.cells, version: state.mapVersion });
}