Files
vslam_cpp/test/www/test_robot_marker.js
trung09122002 f8a04a2a0b update
2026-09-19 17:10:18 +07:00

188 lines
8.3 KiB
JavaScript

// Hai hinh robot (uoc luong + ground truth) va cao do cua chung.
//
// ================== VI SAO CAN BAI KIEM NAY ==================
// Nguoi van hanh bao "robot co mot nhan ban o canh" trong khi sai so uoc luong
// do duoc chi 9 mm. Nguyen nhan la CAU TRUC:
//
// robotGroup = new Group() <- y = 0
// └ v3dBuildRobot() <- y = wheel_radius (phep nang than)
// gtGroup = v3dBuildRobot() <- y = wheel_radius, KHONG co lop boc
//
// `v3dSetPose()` viet `position.set(p.x, 0, -p.y)`, xoa y cua nhom NGOAI. Voi
// robotGroup phep nang nam o tang trong nen song sot; voi gtGroup no nam dung
// tang bi xoa. Hinh ground truth tut xuong 0.075 m, va o goc camera nghieng
// chenh lech do chieu ra thanh khoang cach NGANG nhin thay duoc.
//
// Khong mot chi so nao doi. Ca hai hinh van duoc ve, van cap nhat, van dung
// mau. Chi la chung o hai cao do.
const fs = require('fs');
const path = require('path');
const vm = require('vm');
class V3 {
constructor(x, y, z) { this.x = x; this.y = y; this.z = z; }
copy(o) { this.x = o.x; this.y = o.y; this.z = o.z; return this; }
set(x, y, z) { this.x = x; this.y = y; this.z = z; return this; }
}
const node = () => ({ position: new V3(0, 0, 0), rotation: { x: 0, y: 0, z: 0 },
userData: {} });
class Grp {
constructor() { Object.assign(this, node()); this.children = []; }
add(c) { this.children.push(c); }
remove(c) { this.children = this.children.filter((x) => x !== c); }
traverse(f) { f(this); this.children.forEach((c) => (c.traverse ? c.traverse(f) : f(c))); }
}
const leaf = (extra) => class {
constructor(g, m) { Object.assign(this, node()); this.geometry = g; this.material = m;
Object.assign(this, extra || {}); }
};
const mat = () => class {
constructor(o) { Object.assign(this, o); this.color = { set() {} }; }
clone() { const c = Object.create(Object.getPrototypeOf(this));
Object.assign(c, this); c.color = { set() {} }; return c; }
dispose() {}
};
global.THREE = {
Group: Grp, Mesh: leaf(), Line: leaf(), LineLoop: leaf(), LineSegments: leaf(),
Points: leaf(), Vector3: V3,
MeshLambertMaterial: mat(), MeshStandardMaterial: mat(), LineBasicMaterial: mat(),
PointsMaterial: mat(),
CylinderGeometry: class {}, ConeGeometry: class {}, BoxGeometry: class { dispose() {} },
PlaneGeometry: class { dispose() {} }, GridHelper: class { constructor() { Object.assign(this, node()); } },
BufferGeometry: class {
constructor() { this.attributes = {}; }
setAttribute(n, a) { this.attributes[n] = a; }
setFromPoints() { return this; }
computeVertexNormals() {} dispose() {}
},
BufferAttribute: class { constructor(a, n) { this.array = a; this.itemSize = n; } },
InstancedMesh: class { constructor() { Object.assign(this, node()); this.instanceMatrix = {}; }
setMatrixAt() {} dispose() {} },
Matrix4: class { compose() {} }, Quaternion: class { setFromAxisAngle() {} },
Color: class { constructor() { this.set = () => {}; } },
};
const www = '/home/quangtrung/projects/vslam_cpp/libs/vslam_web/www';
const sb = { THREE: global.THREE, console, window: {}, document: {} };
vm.createContext(sb);
vm.runInContext(fs.readFileSync(path.join(www, 'app3d.js'), 'utf8') +
'\n;this.__V3D = V3D; this.__shape = v3dSetShape; this.__pose = v3dSetPose;' +
'\n;this.__scan = v3dSetScan;', sb);
const V3D = sb.__V3D;
V3D.ready = true;
V3D.root = new Grp();
// ⚠️ Hinh hoc THAT tu robot.yaml, khong phai so rut gon.
//
// Ban truoc bai kiem nay dung mot `r` thieu `body_l/w/h`, `body_off_x` va
// `clearance`, nen duong ve KHOI HOP -- duong that su duoc dung -- chua bao
// gio duoc chay. Mot bai kiem di duong khac voi ban chay that thi khong bao
// dam gi ca.
const r = { radius: 0.24, wheel_radius: 0.075, wheel_sep: 0.36,
laser_x: 0.24, laser_y: 0.0, laser_z: 0.125,
cam_x: 0.19, cam_y: 0.0, cam_z: 0.725, cam_pitch: 0.2618,
cam_hfov: 1.204, cam_vfov: 0.733,
body_l: 0.40, body_w: 0.34, body_h: 0.85,
body_off_x: -0.06, clearance: 0.04,
cliff_min: 1.10, cliff_max: 2.50 };
sb.__shape(r);
// Sai so thuc do duoc giua uoc luong va ground truth: 9 mm.
const pose = { x: 5.000, y: 2.000, yaw: 0.0 };
const gt = { x: 5.009, y: 2.000, yaw: 0.0 };
sb.__pose(V3D.robotGroup, pose);
sb.__pose(V3D.gtGroup, gt);
// Cao do TUYET DOI cua mot nut: cong y qua tung tang.
function absY(root, pred) {
let found = null;
(function walk(n, acc) {
const y = acc + (n.position ? n.position.y : 0);
if (found === null && pred(n)) found = y;
(n.children || []).forEach((c) => walk(c, y));
})(root, 0);
return found;
}
const isLidar = (n) => n.material && n.material.color !== undefined &&
n.geometry instanceof THREE.CylinderGeometry &&
n.position && Math.abs(n.position.x - r.laser_x) < 1e-9;
const bad = [];
const say = (label, got, want, tol) => {
// Diem quet luu trong Float32Array -- 0.125 + 0.075 khong tron duoc.
const ok = Math.abs(got - want) <= (tol === undefined ? 1e-6 : tol);
console.log(` ${label.padEnd(36)} ${got.toFixed(4)}${ok ? '' : ' != ' + want.toFixed(4)}`);
if (!ok) bad.push(label);
};
// 1. Khoang cach ve phai DUNG BANG khoang cach du lieu -- khong them gi.
const A = V3D.robotGroup.position, B = V3D.gtGroup.position;
say('khoang cach ve (m)', Math.hypot(A.x - B.x, A.y - B.y, A.z - B.z), 0.009, 1e-6);
// 2. Hai hinh phai o CUNG cao do. Day la thu bi hong.
const yRobot = absY(V3D.robotGroup, (n) => n !== V3D.robotGroup && n.children &&
n.children.length > 0);
const yGt = absY(V3D.gtGroup, (n) => n !== V3D.gtGroup && n.children &&
n.children.length > 0);
say('cao do than robot (m)', yRobot, r.wheel_radius);
say('cao do than ground truth (m)', yGt, r.wheel_radius);
// 3. ⚠️ Phep kiem MANH nhat: tru LiDAR tren robot va diem quet phai CUNG cao
// do. Hai duong code doc lap -- v3dBuildRobot() va v3dSetScan() -- nen
// chung chi khop khi ca hai deu dung.
sb.__scan(new Float32Array([2.0, 0.0]), pose, r);
const scanY = V3D.scanPts.geometry.attributes.position.array[1];
const lidarY = absY(V3D.robotGroup, isLidar);
say('cao do diem quet (m)', scanY, r.laser_z + r.wheel_radius);
if (lidarY === null) {
console.log(' khong tim thay tru LiDAR tren hinh robot');
bad.push('tru LiDAR');
} else {
say('cao do tru LiDAR tren robot (m)', lidarY, scanY);
}
// 4. Hop dong cua v3dSetPose(): no dat VI TRI TREN MAT PHANG, khong dung
// toi cao do. Mot nhom mang san phep nang phai giu duoc phep nang do.
//
// Khong co rang buoc nay thi `position.set(p.x, 0, -p.y)` quay lai luc nao
// cung duoc, va lan sau ai them mot lop boc co nang se lai mat no am tham.
const lifted = new Grp();
lifted.position.y = 0.5;
sb.__pose(lifted, { x: 1.0, y: 2.0, yaw: 0.3 });
say('v3dSetPose giu cao do san co (m)', lifted.position.y, 0.5);
say('v3dSetPose dat x', lifted.position.x, 1.0);
say('v3dSetPose dat z (= -y ROS)', lifted.position.z, -2.0);
// 5. ⚠️ Moi NUT tuong ung o hai hinh phai o CUNG cao do.
//
// So `group.position` khong du: hai nhom co the o cung cho ma THAN ben trong
// lai nam o hai tang khac nhau -- do dung la loi da xay ra mot lan (gtGroup
// thieu lop boc). Phai duyet ca cay va so tung nut mot.
function nodes(root) {
const out = [];
(function walk(n, acc) {
const y = acc + (n.position ? n.position.y : 0);
const x = (n.position ? n.position.x : 0);
if (n !== root) out.push({ y, x, kind: n.geometry ? n.geometry.constructor.name : 'group' });
(n.children || []).forEach((c) => walk(c, y));
})(root, 0);
return out;
}
{
const A = nodes(V3D.robotGroup), B = nodes(V3D.gtGroup);
// robotGroup co them vanh cliff; chi so cac nut co trong CA HAI.
const n = Math.min(A.length, B.length);
let worst = 0, worstK = '';
for (let i = 0; i < n; ++i) {
const dy = Math.abs(A[i].y - B[i].y), dx = Math.abs(A[i].x - B[i].x);
if (Math.max(dy, dx) > worst) { worst = Math.max(dy, dx); worstK = A[i].kind; }
}
console.log(` so ${n} nut tuong ung lech lon nhat ${worst.toFixed(4)} m` +
(worst > 1e-9 ? ' <-- ' + worstK : ''));
if (worst > 1e-9) bad.push('nut lech giua hai hinh');
}
console.log(bad.length ? '\n => HONG: ' + bad.join(', ') : '\n => DAT');
process.exit(bad.length ? 1 : 0);