pose.ts 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. import { insetPlan, onTop, pathOf, planOutline, slabOf, type Flat, type SolidPaths } from "../../kit/iso-kit";
  2. import { carried, type Pose } from "./rig";
  3. import { AZIMUTH, BUSH_END, COIL_R, COIL_TURNS, DIAL, DIAL_SPAN, DIAL_TOP, DIAL_R, FRAME_X, GLASS_H, GLASS_TOP, GLASS_Z, P, ROD_Y, ROD_Z, at, shift } from "./view";
  4. export interface GlassPaths extends SolidPaths {
  5. rim: string;
  6. shine: string;
  7. }
  8. export interface Frame {
  9. glass: GlassPaths;
  10. coilsBack: string;
  11. coilsLeft: string;
  12. coilsRightBack: string;
  13. coilsRightFront: string;
  14. carriage: string;
  15. needle: string;
  16. grab: Flat;
  17. finger: Flat;
  18. pull: string;
  19. rider: string;
  20. probe: string;
  21. tip: string;
  22. }
  23. const RADIANS = Math.PI / 180;
  24. const SIN_A = Math.sin(AZIMUTH * RADIANS);
  25. const COS_E = Math.cos(30 * RADIANS);
  26. const SIN_E = Math.sin(30 * RADIANS);
  27. const PER_TURN = 16;
  28. const tenth = (value: number) => Math.round(value * 10) / 10;
  29. export function glassOf(pose: Pose): GlassPaths {
  30. const [hx, hy] = pose.half;
  31. const plan = { x: pose.mid - hx, y: -hy, w: hx * 2, d: hy * 2, r: hy };
  32. const paths = slabOf(plan, GLASS_Z, GLASS_H, P, 10, 1.2);
  33. const inner = insetPlan(plan, 3.2);
  34. const shine = [
  35. onTop([[pose.mid - hx * 0.62, -hy + 5.4], [pose.mid + hx * 0.18, -hy + 5.4]], GLASS_TOP, P),
  36. onTop([[pose.mid + hx * 0.3, -hy + 5.4], [pose.mid + hx * 0.42, -hy + 5.4]], GLASS_TOP, P),
  37. ].join("");
  38. return { ...paths, rim: planOutline(inner, GLASS_TOP, P, 10), shine };
  39. }
  40. function coil(from: number, to: number, y: number) {
  41. const count = COIL_TURNS * PER_TURN;
  42. const back: Flat[][] = [];
  43. const front: Flat[][] = [];
  44. let run: Flat[] = [];
  45. let facing: boolean | null = null;
  46. for (let index = 0; index <= count; index++) {
  47. const share = index / count;
  48. const angle = share * COIL_TURNS * Math.PI * 2;
  49. const cy = Math.cos(angle);
  50. const cz = Math.sin(angle);
  51. const point = at([from + (to - from) * share, y + COIL_R * cy, ROD_Z + COIL_R * cz]);
  52. const near = cy * SIN_A * COS_E + cz * SIN_E > 0;
  53. if (facing === null) facing = near;
  54. if (near !== facing) {
  55. run.push(point);
  56. (facing ? front : back).push(run);
  57. run = [point];
  58. facing = near;
  59. continue;
  60. }
  61. run.push(point);
  62. }
  63. if (run.length > 1) (facing ? front : back).push(run);
  64. return { back: back.map((points) => pathOf(points)).join(""), front: front.map((points) => pathOf(points)).join("") };
  65. }
  66. function needleOf(lean: number) {
  67. const angle = (-lean / DIAL_SPAN) * Math.PI * 2;
  68. const ux = Math.sin(angle);
  69. const uy = -Math.cos(angle);
  70. const [cx, cy] = DIAL;
  71. const point = (along: number, across: number) => at([cx + ux * along - uy * across, cy + uy * along + ux * across, DIAL_TOP + 0.1]);
  72. return pathOf([point(DIAL_R - 3.4, 0), point(0, 0.75), point(-3, 0.35), point(-3, -0.35), point(0, -0.75)], true);
  73. }
  74. export function frameOf(pose: Pose, lean: number, grab: number, finger: number, lift: number, home: number, held = false): Frame {
  75. const left = ROD_Y;
  76. const coils = [
  77. coil(-FRAME_X, -BUSH_END + lean, -left),
  78. coil(-FRAME_X, -BUSH_END + lean, left),
  79. coil(BUSH_END + lean, FRAME_X, -left),
  80. coil(BUSH_END + lean, FRAME_X, left),
  81. ];
  82. const grabX = carried(pose, grab);
  83. const grabAt = at([grabX, 0, GLASS_TOP]);
  84. const fingerAt = at([finger, 0, GLASS_TOP + lift]);
  85. return {
  86. glass: glassOf(pose),
  87. coilsBack: coils.map((one) => one.back).join(""),
  88. coilsLeft: coils[0]!.front + coils[1]!.front,
  89. coilsRightBack: coils[2]!.front,
  90. coilsRightFront: coils[3]!.front,
  91. carriage: shift(lean),
  92. needle: needleOf(lean),
  93. grab: [tenth(grabAt[0]), tenth(grabAt[1])],
  94. finger: [tenth(fingerAt[0]), tenth(fingerAt[1])],
  95. pull: !held || Math.abs(grabX - finger) < 0.6 ? "" : pathOf([grabAt, fingerAt]),
  96. rider: shift(grab),
  97. probe: shift(finger - home),
  98. tip: shift(finger - home, lift),
  99. };
  100. }