t-geometry.test.js 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. import { test } from 'node:test';
  2. import assert from 'node:assert/strict';
  3. import { roundProfile, revolveVolume, radiusAtHeight, checkG1 } from '../core/geometry/profile.js';
  4. import { partsVolumeCm3 } from '../core/geometry/rect.js';
  5. import { buildParts, explodeOffsets } from '../core/geometry/assembly.js';
  6. import { interference } from '../core/geometry/interference.js';
  7. 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 };
  8. test('圆件轮廓 G1 连续:无硬边台阶', () => {
  9. const pr = roundProfile(round);
  10. assert.equal(pr.g1, true, pr.warnings.join(';'));
  11. assert.ok(checkG1(pr.points).worst < 12);
  12. });
  13. test('圆件轮廓极值符合包络', () => {
  14. const pr = roundProfile(round);
  15. assert.ok(Math.abs(pr.meta.bbox.height - 14.6) < 1e-6);
  16. assert.ok(pr.meta.outerR <= 61.6 / 2 + 1e-6);
  17. assert.ok(pr.meta.outerR > 27, '外径应接近 Ø61.6');
  18. assert.ok(Math.abs(pr.meta.screenR - 45.68 / 2) < 1e-9);
  19. });
  20. test('极端参数仍不产生 >30° 外轮廓硬边', () => {
  21. const cases = [];
  22. 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])
  23. for (const screenD of [30, 45, 52]) for (const draft of [0, 1, 3])
  24. cases.push({ ...round, wall, domeR, platform, screenD, draft });
  25. for (const c of cases) {
  26. const pr = roundProfile(c);
  27. const bad = pr.points.filter((_, i, a) => {
  28. const p = a[(i - 1 + a.length) % a.length], q = a[i], r = a[(i + 1) % a.length];
  29. const v1 = [q[0] - p[0], q[1] - p[1]], v2 = [r[0] - q[0], r[1] - q[1]];
  30. const l1 = Math.hypot(...v1), l2 = Math.hypot(...v2);
  31. if (l1 < 1e-6 || l2 < 1e-6) return false;
  32. return Math.acos(Math.max(-1, Math.min(1, (v1[0] * v2[0] + v1[1] * v2[1]) / (l1 * l2)))) * 180 / Math.PI > 30;
  33. });
  34. assert.equal(bad.length, 0, JSON.stringify(c));
  35. }
  36. });
  37. test('拔模角增大 → 底半径减小', () => {
  38. const a = roundProfile({ ...round, draft: 0 }).meta.rBot;
  39. const b = roundProfile({ ...round, draft: 3 }).meta.rBot;
  40. assert.ok(b < a);
  41. assert.ok(Math.abs(a - b - 14.6 * Math.tan(Math.PI / 180 / 0.5773) ) > 0);
  42. });
  43. test('体积为正且随壁厚增大而增大', () => {
  44. const v1 = revolveVolume(roundProfile(round));
  45. const v2 = revolveVolume(roundProfile({ ...round, wall: 3 }));
  46. assert.ok(v1 > 0 && v2 > v1);
  47. });
  48. test('radiusAtHeight 外壁单调', () => {
  49. const pr = roundProfile(round);
  50. assert.ok(radiusAtHeight(pr, 10) >= radiusAtHeight(pr, 2));
  51. });
  52. test('方件体积', () => {
  53. const v = partsVolumeCm3({ l: 40, w: 30, t: 5, hollow: true, wall: 1.5 });
  54. assert.ok(v > 0 && v < 40 * 30 * 5 / 1000);
  55. });
  56. test('装配分层与爆炸位移', () => {
  57. const parts = buildParts({ type: 'round', defaults: round }, [{ id: 'back', role: 'back' }, { id: 'front', role: 'lid' }]);
  58. assert.ok(parts.length >= 9);
  59. const offs = explodeOffsets(parts, 100);
  60. assert.ok(offs.some((o) => o.dist > 0));
  61. });
  62. test('干涉检测', () => {
  63. const r = interference({ l: 10, w: 10, t: 5 }, { l: 10, w: 10, t: 5 }, { z: 0 }, 1, 1, 0.4);
  64. assert.equal(r.clash, true);
  65. assert.ok(r.gap < 0);
  66. const r2 = interference({ l: 10, w: 10, t: 5 }, { l: 10, w: 10, t: 5 }, { z: 8 }, 1, 1, 0.4);
  67. assert.ok(r2.gap > 0);
  68. });