| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778 |
- import { test } from 'node:test';
- import assert from 'node:assert/strict';
- import { roundProfile, revolveVolume, radiusAtHeight, checkG1 } from '../core/geometry/profile.js';
- import { partsVolumeCm3 } from '../core/geometry/rect.js';
- import { buildParts, explodeOffsets } from '../core/geometry/assembly.js';
- import { interference } from '../core/geometry/interference.js';
- const round = { dTop: 61.6, dBottom: 58.0, height: 14.6, wall: 1.8, domeR: 2.0, platform: 1.0, recessR: 1.2, recessDepth: 1.0, screenD: 45.68, chamferC: 0.5, draft: 1.0 };
- test('圆件轮廓 G1 连续:无硬边台阶', () => {
- const pr = roundProfile(round);
- assert.equal(pr.g1, true, pr.warnings.join(';'));
- assert.ok(checkG1(pr.points).worst < 12);
- });
- test('圆件轮廓极值符合包络', () => {
- const pr = roundProfile(round);
- assert.ok(Math.abs(pr.meta.bbox.height - 14.6) < 1e-6);
- assert.ok(pr.meta.outerR <= 61.6 / 2 + 1e-6);
- assert.ok(pr.meta.outerR > 27, '外径应接近 Ø61.6');
- assert.ok(Math.abs(pr.meta.screenR - 45.68 / 2) < 1e-9);
- });
- test('极端参数仍不产生 >30° 外轮廓硬边', () => {
- const cases = [];
- for (const wall of [0.8, 3.0, 4.0]) for (const domeR of [0.5, 2, 5]) for (const platform of [0, 1, 4])
- for (const screenD of [30, 45, 52]) for (const draft of [0, 1, 3])
- cases.push({ ...round, wall, domeR, platform, screenD, draft });
- for (const c of cases) {
- const pr = roundProfile(c);
- const bad = pr.points.filter((_, i, a) => {
- const p = a[(i - 1 + a.length) % a.length], q = a[i], r = a[(i + 1) % a.length];
- const v1 = [q[0] - p[0], q[1] - p[1]], v2 = [r[0] - q[0], r[1] - q[1]];
- const l1 = Math.hypot(...v1), l2 = Math.hypot(...v2);
- if (l1 < 1e-6 || l2 < 1e-6) return false;
- return Math.acos(Math.max(-1, Math.min(1, (v1[0] * v2[0] + v1[1] * v2[1]) / (l1 * l2)))) * 180 / Math.PI > 30;
- });
- assert.equal(bad.length, 0, JSON.stringify(c));
- }
- });
- test('拔模角增大 → 底半径减小', () => {
- const a = roundProfile({ ...round, draft: 0 }).meta.rBot;
- const b = roundProfile({ ...round, draft: 3 }).meta.rBot;
- assert.ok(b < a);
- assert.ok(Math.abs(a - b - 14.6 * Math.tan(Math.PI / 180 / 0.5773) ) > 0);
- });
- test('体积为正且随壁厚增大而增大', () => {
- const v1 = revolveVolume(roundProfile(round));
- const v2 = revolveVolume(roundProfile({ ...round, wall: 3 }));
- assert.ok(v1 > 0 && v2 > v1);
- });
- test('radiusAtHeight 外壁单调', () => {
- const pr = roundProfile(round);
- assert.ok(radiusAtHeight(pr, 10) >= radiusAtHeight(pr, 2));
- });
- test('方件体积', () => {
- const v = partsVolumeCm3({ l: 40, w: 30, t: 5, hollow: true, wall: 1.5 });
- assert.ok(v > 0 && v < 40 * 30 * 5 / 1000);
- });
- test('装配分层与爆炸位移', () => {
- const parts = buildParts({ type: 'round', defaults: round }, [{ id: 'back', role: 'back' }, { id: 'front', role: 'lid' }]);
- assert.ok(parts.length >= 9);
- const offs = explodeOffsets(parts, 100);
- assert.ok(offs.some((o) => o.dist > 0));
- });
- test('干涉检测', () => {
- const r = interference({ l: 10, w: 10, t: 5 }, { l: 10, w: 10, t: 5 }, { z: 0 }, 1, 1, 0.4);
- assert.equal(r.clash, true);
- assert.ok(r.gap < 0);
- const r2 = interference({ l: 10, w: 10, t: 5 }, { l: 10, w: 10, t: 5 }, { z: 8 }, 1, 1, 0.4);
- assert.ok(r2.gap > 0);
- });
|