Files
homeclaw/tests/stl-circles.test.ts

76 lines
4.2 KiB
TypeScript

/**
* detectCircles — STL 단면에서 원형 구멍·원통 지름 검출
*
* 합성 부품은 OpenSCAD로 만든다(없으면 건너뜀). 검출값이 설계 지름에 근접해야 하고,
* 한쪽이 열린 C자 홈처럼 닫힌 원이 아닌 것은 검출하지 않아야 한다.
*/
import { test, describe } from 'node:test';
import assert from 'node:assert/strict';
import fs from 'fs';
import os from 'os';
import path from 'path';
import { OPENSCAD_BIN, runOpenscad, readStlTriangles } from '../src/tools/stl-cad-core';
import { detectCircles } from '../src/tools/stl-circles';
const HAS = fs.existsSync(OPENSCAD_BIN);
const dir = fs.mkdtempSync(path.join(os.tmpdir(), 'circ-'));
async function makeStl(name: string, scad: string): Promise<Float32Array> {
const s = path.join(dir, `${name}.scad`), o = path.join(dir, `${name}.stl`);
fs.writeFileSync(s, scad);
const r = await runOpenscad(['-o', o, '--export-format=binstl', s], 60_000);
assert.equal(r.ok, true, r.ok ? '' : (r as any).detail);
return readStlTriangles(o);
}
describe('detectCircles', { skip: !HAS }, () => {
test('판의 관통 원형 구멍: 위치와 지름을 잰다(z축)', async () => {
const tri = await makeStl('plate', 'difference(){ cube([60,40,6]); translate([15,25,-1]) cylinder(d=12,h=8,$fn=96); }');
const cs = detectCircles(tri, 'z');
assert.equal(cs.length, 1);
assert.ok(Math.abs(cs[0].dia - 12) < 0.15, `지름 ${cs[0].dia}`);
assert.ok(Math.abs(cs[0].u - 15) < 0.1 && Math.abs(cs[0].v - 25) < 0.1, `중심 (${cs[0].u}, ${cs[0].v})`);
assert.ok(cs[0].hi - cs[0].lo > 4, '관통 구멍은 넓은 높이 범위에서 보인다');
});
test('서로 다른 지름 두 개(동심 계단 구멍)를 각각 잰다', async () => {
const tri = await makeStl('step', 'difference(){ cube([50,50,10]); translate([25,25,-1]) cylinder(d=8,h=12,$fn=96); translate([25,25,5]) cylinder(d=20,h=6,$fn=96); }');
const cs = detectCircles(tri, 'z').map((c) => Math.round(c.dia)).sort((a, b) => a - b);
assert.deepEqual(cs, [8, 20]);
});
test('옆면 구멍(y축으로 뚫린 구멍)은 y축 단면에서 (x,z) 좌표로 나온다', async () => {
const tri = await makeStl('side', 'difference(){ cube([40,30,20]); translate([10,-1,14]) rotate([-90,0,0]) cylinder(d=6,h=32,$fn=96); }');
const cs = detectCircles(tri, 'y');
assert.equal(cs.length, 1);
assert.ok(Math.abs(cs[0].dia - 6) < 0.1);
assert.ok(Math.abs(cs[0].u - 10) < 0.1 && Math.abs(cs[0].v - 14) < 0.1, `(x,z)=(${cs[0].u}, ${cs[0].v})`);
});
test('원뿔(지름이 높이에 따라 변함)은 원 여러 개가 아니라 테이퍼 하나로 요약한다', async () => {
const tri = await makeStl('cone', 'difference(){ cube([50,50,30]); translate([25,25,-1]) cylinder(d1=10,d2=34,h=32,$fn=96); }');
const cs = detectCircles(tri, 'z');
assert.equal(cs.length, 1);
assert.equal(cs[0].taper, true);
assert.ok(cs[0].dia >= 9 && cs[0].dia < 16 && cs[0].diaMax! > 28 && cs[0].diaMax! <= 34.5, `Ø${cs[0].dia}~${cs[0].diaMax}`);
});
test('계단 원통(같은 높이에 안쪽·바깥 벽이 함께 있음)은 테이퍼로 묶지 않고 각각 잰다', async () => {
const tri = await makeStl('tube', 'difference(){ union(){ cylinder(d=32,h=50,$fn=96); translate([0,0,25]) cylinder(d=38,h=25,$fn=96); } translate([0,0,-1]) cylinder(d=29,h=52,$fn=96); }');
const cs = detectCircles(tri, 'z');
const dias = cs.map((c) => Math.round(c.dia)).sort((a, b) => a - b);
assert.ok(cs.every((c) => !c.taper), '테이퍼로 오인');
assert.ok(dias.includes(29) && dias.includes(32) && dias.includes(38), `검출: ${dias.join(',')}`);
});
test('한쪽이 열린 C자 홈은 원으로 검출하지 않는다', async () => {
const tri = await makeStl('cshape', 'difference(){ cube([50,50,8]); translate([25,25,-1]) cylinder(d=20,h=10,$fn=96); translate([25,-1,-1]) cube([12,30,10]); }');
assert.equal(detectCircles(tri, 'z').length, 0);
});
test('구멍이 없는 단순 블록은 아무것도 검출하지 않는다', async () => {
const tri = await makeStl('block', 'cube([30,20,10]);');
assert.equal(detectCircles(tri, 'z').length, 0);
assert.equal(detectCircles(new Float32Array(0), 'z').length, 0);
});
});