Files
vslam_cpp/test/www/test_world_mesh.js
trung09122002 48bd5377dc upload
2026-09-18 18:14:42 +07:00

164 lines
6.5 KiB
JavaScript

// Bo giai ma `world.bin` cua trinh duyet, chay bang CHINH khoi do C++ sinh ra.
//
// ================== VI SAO CAN BAI KIEM NAY ==================
// Bo ma hoa o C++ va bo giai ma o JavaScript la hai ban cai dat cua MOT dinh
// dang. Chung lech nhau mot byte thi khung 3D ra rac hoac rong -- va mot khung
// 3D rong co dung moi chi so van hanh voi mot khung dung: HTTP 200, dung so
// byte, khong ngoai le nao.
//
// Nen bai kiem nay DEM NOI DUNG: bao nhieu hinh hoc, bao nhieu the hien, bao
// nhieu tam giac, va do dac nam o dau trong khong gian.
//
// Khong can bo model AWS 3 GB: khoi duoc sinh tu test/data/world/mini.world.
//
// node test_world_mesh.js <vslam_world_export> <thu_muc_data> <file_tam>
const fs = require('fs');
const path = require('path');
const { execFileSync } = require('child_process');
const [exporter, dataDir, tmpBin] = process.argv.slice(2);
const wwwDir = path.resolve(__dirname, '../../libs/vslam_web/www');
// --- THREE gia: chi du de dung hinh, khong ve gi ---
class Vector3 { constructor(x, y, z) { this.x = x; this.y = y; this.z = z; } }
class Color { constructor(r, g, b) { this.r = r; this.g = g; this.b = b; } }
class Quaternion { setFromAxisAngle(a, ang) { this.ang = ang; } }
class Matrix4 { compose(p, q, s) { this.p = p; this.q = q; } }
class BufferAttribute { constructor(a, n) { this.array = a; this.itemSize = n; } }
class BufferGeometry {
constructor() { this.attributes = {}; }
setAttribute(n, a) { this.attributes[n] = a; }
computeVertexNormals() { this.normals = true; }
dispose() { this.disposed = true; }
}
class Group {
constructor() { this.children = []; }
add(c) { this.children.push(c); }
traverse(f) { this.children.forEach(f); }
remove(c) { this.children = this.children.filter((x) => x !== c); }
}
class MeshStandardMaterial { constructor(o) { Object.assign(this, o); } dispose() {} }
class InstancedMesh {
constructor(g, m, n) {
this.geometry = g; this.material = m; this.count = n;
this.mats = []; this.userData = {}; this.instanceMatrix = {};
}
setMatrixAt(i, m) { this.mats[i] = { p: m.p, q: m.q }; }
dispose() {}
}
class Mesh {
constructor(g, m) {
this.geometry = g; this.material = m;
this.position = { set: (x, y, z) => { this.pos = [x, y, z]; } };
this.rotation = { x: 0, y: 0, z: 0 };
}
}
global.THREE = {
Vector3, Color, Quaternion, Matrix4, BufferAttribute, BufferGeometry, Group,
MeshStandardMaterial, InstancedMesh, Mesh,
PlaneGeometry: class { constructor(w, h) { this.w = w; this.h = h; } dispose() {} },
GridHelper: class { constructor(s) { this.size = s; } },
BoxGeometry: class { dispose() {} },
};
// --- nap app3d.js trong pham vi rieng ---
const vm = require('vm');
const sandbox = { THREE: global.THREE, console, window: {}, document: {} };
vm.createContext(sandbox);
vm.runInContext(
fs.readFileSync(path.join(wwwDir, 'app3d.js'), 'utf8') +
'\n;this.__V3D = V3D; this.__setMeshes = v3dSetWorldMeshes;' +
'\n;this.__setWorld = v3dSetWorld;',
sandbox);
const V3D = sandbox.__V3D;
V3D.ready = true;
V3D.root = new Group();
V3D.scene = { fog: { near: 0, far: 0 } };
// --- sinh khoi tu mini.world bang chinh bo ma hoa C++ ---
execFileSync(exporter, [
'--world', path.join(dataDir, 'world/mini.world'),
'--models', path.join(dataDir, 'world/models'),
'--meshes', tmpBin, '--meshes-visual',
], { stdio: ['ignore', 'ignore', 'inherit'] });
const raw = fs.readFileSync(tmpBin);
const ab = raw.buffer.slice(raw.byteOffset, raw.byteOffset + raw.byteLength);
const bad = [];
const check = (name, got, want) => {
const ok = JSON.stringify(got) === JSON.stringify(want);
console.log(` ${name.padEnd(28)} ${JSON.stringify(got)}${ok ? '' : ' != ' + JSON.stringify(want)}`);
if (!ok) bad.push(name);
};
if (!sandbox.__setMeshes(ab)) {
console.log(' => HONG: khong giai ma duoc khoi');
process.exit(1);
}
const g = V3D.meshWorld;
let nInst = 0, nMat = 0, nTri = 0;
const centers = [];
for (const im of g.children) {
nInst += im.count;
nMat += im.mats.length;
nTri += im.geometry.attributes.position.array.length / 9;
for (const m of im.mats) centers.push(m.p);
}
// mini.world: 4 model khac nhau, 5 the hien (the thu sau co pose hong, bi bo).
check('hinh hoc', g.children.length, 4);
check('the hien', nInst, 5);
check('ma tran da dat', nMat, 5);
// Moi hop 8 tam giac; box_m dung cho 2 the hien nen van chi 1 ban hinh hoc.
check('tam giac (duy nhat)', nTri, 32);
// Tam cac the hien, trong he three (x, y, z) = ROS (x, z, -y).
const xs = centers.map((p) => +p.x.toFixed(2)).sort((a, b) => a - b);
const zs = centers.map((p) => +p.z.toFixed(2)).sort((a, b) => a - b);
check('tam theo x', xs, [0, 0, 5, 10, 15]);
check('tam theo z (= -y ROS)', zs, [-6, 0, 0, 0, 0]);
// Hop dau tien khong quay: dinh phai o +-1.0 theo x, +-0.5 theo z.
const first = g.children[0].geometry.attributes.position.array;
let xmin = 1e9, xmax = -1e9, ymin = 1e9, ymax = -1e9, zmin = 1e9, zmax = -1e9;
for (let i = 0; i < first.length; i += 3) {
xmin = Math.min(xmin, first[i]); xmax = Math.max(xmax, first[i]);
ymin = Math.min(ymin, first[i + 1]); ymax = Math.max(ymax, first[i + 1]);
zmin = Math.min(zmin, first[i + 2]); zmax = Math.max(zmax, first[i + 2]);
}
check('hop 1: rong x', +(xmax - xmin).toFixed(2), 2);
check('hop 1: cao y', +(ymax - ymin).toFixed(2), 0.5);
check('hop 1: sau z', +(zmax - zmin).toFixed(2), 1);
// ⚠️ MOI hop phai cao 0.5 m, khong rieng hop dau.
//
// `..._walls_visual` khai Y_UP nhung du lieu da o Z-up: ap `<up_axis>` len no
// lam buc tuong 3 m thanh mot thanh DUNG DUNG cao 56 m. Mat bang van dung, nen
// chi do chieu cao moi thay.
let tallest = 0;
for (const im of g.children) {
const a = im.geometry.attributes.position.array;
let lo = 1e9, hi = -1e9;
for (let i = 1; i < a.length; i += 3) { lo = Math.min(lo, a[i]); hi = Math.max(hi, a[i]); }
tallest = Math.max(tallest, hi - lo);
}
check('hop cao nhat', +tallest.toFixed(2), 0.5);
// ⚠️ HOI QUY: san nam trong v3dSetWorld(), ma ham do THOAT SOM khi da co mesh
// that. Mot lan da lam ca toa nha lo lung tren nen den ma khong loi nao.
const grid = { w: 400, h: 400, res: 0.05, ox: -10, oy: -10,
cells: new Int8Array(400 * 400) };
sandbox.__setWorld(grid, 2.0);
check('san sau khi co mesh', !!V3D.floorMesh, true);
check('luoi san', !!V3D.gridHelper, true);
check('khoi hop da bo', V3D.worldMesh, null);
// Suong mu phai phu het bao, khong thi toa nha tan vao nen.
check('suong mu >= bao', V3D.scene.fog.far >= 400 * 0.05, true);
console.log(bad.length ? '\n => HONG: ' + bad.join(', ') : '\n => DAT');
process.exit(bad.length ? 1 : 0);