102 lines
4.6 KiB
TypeScript
102 lines
4.6 KiB
TypeScript
/**
|
|
* convertAsciiStlToBinary — ASCII STL을 바이너리로 제자리 변환
|
|
*
|
|
* 표본은 2026-09-30 3018 집진 슈 모델 10개(Printables 압축 파일): 전부 ASCII라 업로드해도
|
|
* 썸네일이 안 뜨고 바닥(z=0) 정렬도 안 됐다. 도구들이 바이너리만 읽기 때문이다.
|
|
* 고정하는 것: ASCII는 변환된다 / 바이너리는 (헤더가 "solid"여도) 손대지 않는다 / 쓰레기는 실패한다.
|
|
*/
|
|
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 { convertAsciiStlToBinary, readStlBBox, dropStlToFloor } from '../src/tools/stl-cad-core';
|
|
|
|
const tmp = fs.mkdtempSync(path.join(os.tmpdir(), 'stl-ascii-'));
|
|
const f = (name: string) => path.join(tmp, name);
|
|
|
|
function asciiTri(n: number[], v: number[][], eol = '\n'): string {
|
|
const fmt = (a: number[]) => a.join(' ');
|
|
return [`facet normal ${fmt(n)}`, ' outer loop', ...v.map((p) => ` vertex ${fmt(p)}`), ' endloop', 'endfacet'].join(eol) + eol;
|
|
}
|
|
|
|
// 밑면이 z=5인 사각뿔 대용: 삼각형 2개짜리 사각형(z=5~7) — 바닥 정렬 검증용
|
|
function asciiSquare(eol = '\n', z0 = 5): string {
|
|
return `solid test${eol}`
|
|
+ asciiTri([0, 0, 1], [[0, 0, z0], [10, 0, z0], [10, 10, z0 + 2]], eol)
|
|
+ asciiTri([0, 0, 1], [[0, 0, z0], [10, 10, z0 + 2], [0, 10, z0]], eol)
|
|
+ `endsolid test${eol}`;
|
|
}
|
|
|
|
function binaryStl(tris: number[][][], header = 'binary header'): Buffer {
|
|
const b = Buffer.alloc(84 + tris.length * 50);
|
|
b.write(header, 0, 'latin1');
|
|
b.writeUInt32LE(tris.length, 80);
|
|
let off = 84;
|
|
for (const t of tris) {
|
|
off += 12;
|
|
for (const p of t) { for (const c of p) { b.writeFloatLE(c, off); off += 4; } }
|
|
off += 2;
|
|
}
|
|
return b;
|
|
}
|
|
|
|
describe('convertAsciiStlToBinary', () => {
|
|
test('ASCII STL을 바이너리로 바꾸고, 이후 bbox·바닥 정렬이 동작한다', () => {
|
|
fs.writeFileSync(f('a.stl'), asciiSquare());
|
|
assert.deepEqual(readStlBBox(f('a.stl')).min, [0, 0, 5]); // bbox는 10-05부터 ASCII도 읽는다(71bfa19). 바닥 정렬은 여전히 바이너리만
|
|
const r = convertAsciiStlToBinary(f('a.stl'));
|
|
assert.deepEqual(r, { ok: true, converted: true, triangles: 2 });
|
|
const bb = readStlBBox(f('a.stl'));
|
|
assert.deepEqual(bb.min, [0, 0, 5]);
|
|
assert.deepEqual(bb.max, [10, 10, 7]);
|
|
const drop = dropStlToFloor(f('a.stl'));
|
|
assert.equal(drop.ok, true);
|
|
assert.equal(readStlBBox(f('a.stl')).min[2], 0);
|
|
});
|
|
|
|
test('CRLF 줄바꿈·탭·지수 표기(1e-3)도 읽는다', () => {
|
|
const txt = `solid x\r\nfacet normal 0 0 1\r\n\touter loop\r\n\t\tvertex 1.5e1 -2E0 +3.\r\n\t\tvertex 0 0 0\r\n\t\tvertex .5 1e-3 2\r\n\tendloop\r\nendfacet\r\nendsolid x\r\n`;
|
|
fs.writeFileSync(f('crlf.stl'), txt);
|
|
const r = convertAsciiStlToBinary(f('crlf.stl'));
|
|
assert.equal(r.ok && r.converted, true);
|
|
const buf = fs.readFileSync(f('crlf.stl'));
|
|
assert.equal(buf.length, 84 + 50);
|
|
assert.equal(buf.readFloatLE(84 + 12), 15); // 첫 정점 x
|
|
assert.equal(buf.readFloatLE(84 + 16), -2);
|
|
});
|
|
|
|
test('solid 여러 개인 파일은 삼각형을 모두 모은다', () => {
|
|
fs.writeFileSync(f('multi.stl'), asciiSquare() + asciiSquare('\n', 20));
|
|
const r = convertAsciiStlToBinary(f('multi.stl'));
|
|
assert.deepEqual(r, { ok: true, converted: true, triangles: 4 });
|
|
});
|
|
|
|
test('바이너리는 손대지 않는다 — 헤더가 "solid"로 시작해도(SolidWorks 등)', () => {
|
|
const b = binaryStl([[[0, 0, 0], [1, 0, 0], [0, 1, 0]]], 'solid exported by CAD');
|
|
fs.writeFileSync(f('bin.stl'), b);
|
|
const r = convertAsciiStlToBinary(f('bin.stl'));
|
|
assert.deepEqual(r, { ok: true, converted: false, triangles: 1 });
|
|
assert.deepEqual(fs.readFileSync(f('bin.stl')), b);
|
|
});
|
|
|
|
test('ASCII도 바이너리도 아니면 실패하고 파일을 그대로 둔다', () => {
|
|
fs.writeFileSync(f('junk.stl'), 'this is not an stl file at all, just some text.');
|
|
const before = fs.readFileSync(f('junk.stl'));
|
|
const r = convertAsciiStlToBinary(f('junk.stl'));
|
|
assert.equal(r.ok, false);
|
|
assert.deepEqual(fs.readFileSync(f('junk.stl')), before);
|
|
});
|
|
|
|
test('solid로 시작하지만 삼각형이 없으면 실패하고 파일을 그대로 둔다', () => {
|
|
fs.writeFileSync(f('empty.stl'), 'solid empty\nendsolid empty\n');
|
|
const r = convertAsciiStlToBinary(f('empty.stl'));
|
|
assert.equal(r.ok, false);
|
|
assert.equal(fs.readFileSync(f('empty.stl'), 'utf8'), 'solid empty\nendsolid empty\n');
|
|
});
|
|
|
|
test('없는 파일은 실패로 돌려준다(예외 아님)', () => {
|
|
assert.equal(convertAsciiStlToBinary(f('nope.stl')).ok, false);
|
|
});
|
|
});
|