Ba test nay doc app.js/app3d.js qua duong dan cung /home/quangtrung/projects/vslam_cpp/..., nen chi chay duoc tren dung mot may. Tren Jetson chung fail voi ENOENT truoc khi chay duoc dong assert nao -- ctest bao fail nhung khong phai vi logic sai. Dung path.resolve(__dirname, ...) nhu test_scan_frame.js va test_world_mesh.js trong cung thu muc da lam.
241 lines
11 KiB
JavaScript
241 lines
11 KiB
JavaScript
// Chuot trong khung 3D: canh phai di THEO tay.
|
|
//
|
|
// ================== VI SAO CAN BAI KIEM NAY ==================
|
|
// Da sua chieu keo mot lan bang cach suy luan, va sai: dao dung truc azimuth
|
|
// thi dung, nhung dao ca truc elevation thi hong, va phep keo canh thi nguoc
|
|
// hoan toan. Khong mot loi nao -- canh van ve, chi la no di nguoc tay.
|
|
//
|
|
// Nen bai kiem nay khong doc ma nguon ma DO: chieu mot diem the gioi qua dung
|
|
// phep camera cua `v3dRender()`, keo chuot, roi xem diem do di ve phia nao
|
|
// tren man hinh.
|
|
//
|
|
// Bat bien: keo sang PHAI thi diem di sang PHAI; keo XUONG thi diem di XUONG.
|
|
// Voi phep KEO CANH con chat hon: diem duoi con tro phai O NGUYEN duoi con
|
|
// tro, y nhu khung 2D.
|
|
const fs = require('fs');
|
|
const path = require('path');
|
|
const vm = require('vm');
|
|
|
|
// --- THREE gia: chi can du de app3d.js chay den cho gan chuot ---
|
|
class V3 {
|
|
constructor(x, y, z) { this.x = x || 0; this.y = y || 0; this.z = z || 0; }
|
|
set(x, y, z) { this.x = x; this.y = y; this.z = z; return this; }
|
|
copy(o) { this.x = o.x; this.y = o.y; this.z = o.z; return this; }
|
|
}
|
|
const node = () => ({ position: new V3(), rotation: { x: 0, y: 0, z: 0 },
|
|
userData: {}, visible: true });
|
|
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 leafCls = () => class { constructor(g, m) { Object.assign(this, node());
|
|
this.geometry = g; this.material = m; } };
|
|
const matCls = () => class {
|
|
constructor(o) { Object.assign(this, o); this.color = { set() {}, setHSL() {} }; }
|
|
clone() { const c = Object.create(Object.getPrototypeOf(this));
|
|
Object.assign(c, this); c.color = { set() {}, setHSL() {} }; return c; }
|
|
dispose() {}
|
|
};
|
|
global.THREE = {
|
|
Group: Grp, Mesh: leafCls(), Line: leafCls(), LineLoop: leafCls(),
|
|
LineSegments: leafCls(), Points: leafCls(), Vector3: V3,
|
|
MeshLambertMaterial: matCls(), MeshStandardMaterial: matCls(),
|
|
LineBasicMaterial: matCls(), PointsMaterial: matCls(),
|
|
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 = () => {}; this.setHSL = () => {}; } },
|
|
Scene: class { constructor() { Object.assign(this, node()); this.fog = { near: 0, far: 0 }; } },
|
|
PerspectiveCamera: class { constructor() { Object.assign(this, node());
|
|
this.lookAt = () => {}; } },
|
|
WebGLRenderer: class { constructor() { this.shadowMap = {}; }
|
|
setPixelRatio() {} setSize() {} render() {} },
|
|
HemisphereLight: class { constructor() { Object.assign(this, node()); } },
|
|
DirectionalLight: class { constructor() { Object.assign(this, node());
|
|
this.shadow = { camera: {}, mapSize: {} }; } },
|
|
AmbientLight: class { constructor() { Object.assign(this, node()); } },
|
|
Fog: class {}, FogExp2: class {},
|
|
PCFSoftShadowMap: 1, DoubleSide: 2,
|
|
};
|
|
|
|
// --- canvas + window gia, bat lay bo xu ly chuot ---
|
|
const H = { win: {}, cv: {} };
|
|
const canvas = {
|
|
clientWidth: 900, clientHeight: 600, width: 900, height: 600,
|
|
addEventListener(ev, fn) { H.cv[ev] = fn; },
|
|
getContext: () => new Proxy({}, { get: () => () => {} }),
|
|
};
|
|
const sandbox = {
|
|
THREE: global.THREE, console,
|
|
window: { addEventListener(ev, fn) { H.win[ev] = fn; }, devicePixelRatio: 1 },
|
|
document: { createElement: () => canvas },
|
|
};
|
|
vm.createContext(sandbox);
|
|
vm.runInContext(
|
|
fs.readFileSync(path.resolve(
|
|
__dirname, '../../libs/vslam_web/www/app3d.js'), 'utf8') +
|
|
'\n;this.__V3D = V3D; this.__bind = v3dBindMouse; this.__setView = v3dSetView;',
|
|
sandbox);
|
|
|
|
const V3D = sandbox.__V3D;
|
|
V3D.ready = true;
|
|
V3D.root = new Grp();
|
|
sandbox.__bind(canvas);
|
|
|
|
// --- Chieu mot diem the gioi len man hinh, dung phep camera cua v3dRender ---
|
|
//
|
|
// Camera dat o goc cau quanh tam nhin, nhin ve tam, up = +Y. Day la copy
|
|
// nguyen van cong thuc trong v3dRender() -- neu cong thuc do doi ma o day
|
|
// khong doi, bai kiem se bao sai va do la dieu MONG MUON: hai noi phai khop.
|
|
function project(p) {
|
|
const o = V3D.orbit;
|
|
const cx = o.tx, cz = o.ty;
|
|
const cam = {
|
|
x: cx + o.dist * Math.cos(o.el) * Math.cos(o.az),
|
|
y: o.dist * Math.sin(o.el),
|
|
z: cz + o.dist * Math.cos(o.el) * Math.sin(o.az),
|
|
};
|
|
const tgt = { x: cx, y: 0.2, z: cz };
|
|
const sub = (a, b) => ({ x: a.x - b.x, y: a.y - b.y, z: a.z - b.z });
|
|
const norm = (v) => { const n = Math.hypot(v.x, v.y, v.z);
|
|
return { x: v.x / n, y: v.y / n, z: v.z / n }; };
|
|
const cross = (a, b) => ({ x: a.y * b.z - a.z * b.y,
|
|
y: a.z * b.x - a.x * b.z,
|
|
z: a.x * b.y - a.y * b.x });
|
|
const dot = (a, b) => a.x * b.x + a.y * b.y + a.z * b.z;
|
|
const fwd = norm(sub(tgt, cam)); // huong nhin
|
|
const right = norm(cross(fwd, { x: 0, y: 1, z: 0 }));
|
|
const up = cross(right, fwd);
|
|
const d = sub(p, cam);
|
|
const depth = dot(d, fwd);
|
|
if (depth <= 1e-6) return null; // sau lung camera
|
|
// Phoi canh: chia cho do sau. He so tieu cu khong doi ket qua ve CHIEU.
|
|
return { x: dot(d, right) / depth, y: dot(d, up) / depth, depth };
|
|
}
|
|
|
|
const bad = [];
|
|
const P = { x: 2.0, y: 0.0, z: 1.0 }; // mot diem co dinh trong canh
|
|
|
|
// --- TU KIEM phep chieu truoc khi tin no ---
|
|
//
|
|
// Mot bai kiem sai co the to cao code dung. Ba tinh huong duoi day co dap an
|
|
// biet truoc, khong phu thuoc vao app3d.js chut nao.
|
|
{
|
|
V3D.orbit.az = -2.2; V3D.orbit.el = 0.55; V3D.orbit.dist = 6.0;
|
|
V3D.orbit.tx = 0; V3D.orbit.ty = 0;
|
|
const o = V3D.orbit;
|
|
const cam = { x: o.dist * Math.cos(o.el) * Math.cos(o.az),
|
|
y: o.dist * Math.sin(o.el),
|
|
z: o.dist * Math.cos(o.el) * Math.sin(o.az) };
|
|
const fwd = { x: -cam.x, y: 0.2 - cam.y, z: -cam.z };
|
|
const n = Math.hypot(fwd.x, fwd.y, fwd.z);
|
|
const f = { x: fwd.x / n, y: fwd.y / n, z: fwd.z / n };
|
|
const rN = Math.hypot(-f.z, 0, f.x);
|
|
const right = { x: -f.z / rN, y: 0, z: f.x / rN };
|
|
|
|
const c0 = project({ x: 0, y: 0.2, z: 0 }); // dung tam nhin
|
|
const cR = project({ x: right.x, y: 0.2, z: right.z }); // lech sang PHAI 1 m
|
|
const cU = project({ x: 0, y: 1.2, z: 0 }); // cao hon 1 m
|
|
const good = Math.abs(c0.x) < 1e-9 && Math.abs(c0.y) < 1e-9 &&
|
|
cR.x > 0 && cU.y > 0;
|
|
console.log(` tu kiem: tam=(${c0.x.toFixed(3)}, ${c0.y.toFixed(3)}) ` +
|
|
`phai.x=${cR.x.toFixed(3)} cao.y=${cU.y.toFixed(3)}` +
|
|
(good ? '' : ' <-- PHEP CHIEU CUA BAI KIEM SAI'));
|
|
if (!good) { bad.push('tu kiem phep chieu'); }
|
|
}
|
|
|
|
function drag(btn, dx, dy) {
|
|
H.cv.mousedown({ clientX: 100, clientY: 100, button: btn, preventDefault() {} });
|
|
H.win.mousemove({ clientX: 100 + dx, clientY: 100 + dy });
|
|
H.win.mouseup();
|
|
}
|
|
|
|
function check(label, btn, dx, dy, wantX, wantY) {
|
|
V3D.orbit.az = -2.2; V3D.orbit.el = 0.55; V3D.orbit.dist = 6.0;
|
|
V3D.orbit.tx = 0; V3D.orbit.ty = 0;
|
|
const a = project(P);
|
|
drag(btn, dx, dy);
|
|
const b = project(P);
|
|
if (!a || !b) { bad.push(label + ' (diem ra sau camera)'); return; }
|
|
const mx = b.x - a.x, my = b.y - a.y;
|
|
// Truc y cua MAN HINH huong xuong, con `up` cua camera huong len -> dao dau.
|
|
const sx = mx, sy = -my;
|
|
// ⚠️ `null` = KHONG XET. Phep xoay lam diem lech tam di theo cung, nen mot
|
|
// cu keo ngang cung sinh do doi doc -- doi truc kia bang 0 la doi sai.
|
|
const okx = wantX === null ? true : Math.sign(sx) === wantX;
|
|
const oky = wantY === null ? true : Math.sign(sy) === wantY;
|
|
const nm = (v) => (v > 0 ? 'phai/xuong' : v < 0 ? 'trai/len' : 'khong doi');
|
|
console.log(` ${label.padEnd(34)} diem di ${nm(sx).padEnd(11)} ` +
|
|
`${nm(sy).padEnd(11)} ${okx && oky ? '' : ' <-- SAI'}`);
|
|
if (!okx || !oky) bad.push(label);
|
|
}
|
|
|
|
console.log(' --- keo TRAI (xoay) ---');
|
|
check('keo sang phai -> canh sang phai', 0, 60, 0, +1, null);
|
|
check('keo sang trai -> canh sang trai', 0, -60, 0, -1, null);
|
|
check('keo xuong -> canh xuong', 0, 0, 50, null, +1);
|
|
check('keo len -> canh len', 0, 0, -50, null, -1);
|
|
|
|
console.log(' --- keo PHAI (di chuyen canh) ---');
|
|
check('keo sang phai -> canh sang phai', 2, 60, 0, +1, null);
|
|
check('keo xuong -> canh xuong', 2, 0, 50, null, +1);
|
|
|
|
// ⚠️ Keo canh phai giu DIEM DUOI CON TRO. Chi kiem chieu thoi thi mot phep
|
|
// keo qua manh hay qua nhe van "dung chieu".
|
|
// Keo canh: HAI TRUC phai cung ti le.
|
|
//
|
|
// Khong doi khoa cung vao con tro -- phep chieu la phoi canh, muon khoa cung
|
|
// thi phai cat tia voi mat san. Gazebo cung khong khoa cung. Cai PHAI dung la
|
|
// hai truc cung ti le: keo cheo 40:25 thi canh cung phai di cheo xap xi 40:25,
|
|
// khong phai 40:11 nhu khi quen bu do rut ngan theo goc nhin.
|
|
console.log(' --- keo canh: hai truc cung ti le ---');
|
|
for (const az of [-2.2, 0.0, 1.1]) {
|
|
V3D.orbit.az = az; V3D.orbit.el = 0.55; V3D.orbit.dist = 6.0;
|
|
V3D.orbit.tx = 0; V3D.orbit.ty = 0;
|
|
// ⚠️ Do o DUNG TAM NHIN. Mot diem lech tam bi phoi canh lam meo ti le theo
|
|
// do sau, va do sau lai doi theo goc nhin -- do la bien so cua PHEP DO, chu
|
|
// khong phai cua code.
|
|
const Q = { x: 0, y: 0.2, z: 0 };
|
|
const a = project(Q);
|
|
drag(2, 40, 25);
|
|
const b = project(Q);
|
|
// Do doi tren man hinh, quy ve pixel bang cung mot he so cho ca hai truc.
|
|
const errx = (b.x - a.x), erry = -(b.y - a.y);
|
|
console.log(` az=${az.toFixed(1).padStart(5)} do doi = ` +
|
|
`(${errx.toFixed(4)}, ${erry.toFixed(4)}) ti le ` +
|
|
`${(errx === 0 ? 0 : erry / errx).toFixed(3)}`);
|
|
// Ti le do doi phai khop ti le keo (25/40 = 0.625), va cung dau.
|
|
const want = 25 / 40;
|
|
const got = erry / errx;
|
|
if (!(errx > 0 && erry > 0 && got > want / 1.7 && got < want * 1.7)) {
|
|
bad.push('keo canh @ az=' + az.toFixed(1));
|
|
}
|
|
}
|
|
|
|
// --- Nut goc nhin ---
|
|
console.log(' --- nut goc nhin ---');
|
|
for (const [name, wantEl] of [['top', 1.45], ['side', 0.06], ['iso', 0.55]]) {
|
|
V3D.orbit.tx = 9; V3D.orbit.ty = 9;
|
|
sandbox.__setView(name);
|
|
const o = V3D.orbit;
|
|
const ok = Math.abs(o.el - wantEl) < 1e-9 && o.tx === 0 && o.ty === 0;
|
|
console.log(` ${name.padEnd(6)} el=${o.el.toFixed(2)} az=${o.az.toFixed(2)} ` +
|
|
`tx=${o.tx} ty=${o.ty}${ok ? '' : ' <-- SAI'}`);
|
|
if (!ok) bad.push('goc nhin ' + name);
|
|
}
|
|
|
|
console.log(bad.length ? '\n => HONG: ' + bad.join(', ') : '\n => DAT');
|
|
process.exit(bad.length ? 1 : 0);
|