geometry.ts 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305
  1. import { circleRing, extrude, insetPlan, onTop, pathOf, planOutline, slabOf, topMatrix, type Flat, type Plan, type Plane, type Point3 } from "../../kit/iso-kit";
  2. import {
  3. BAR,
  4. BAR_H,
  5. BASE,
  6. BASE_H,
  7. BUSH_D,
  8. BUSH_END,
  9. BUSH_W,
  10. CARRIAGE,
  11. CARRIAGE_H,
  12. CARRIAGE_TOP,
  13. CARRIAGE_Z,
  14. DIAL,
  15. DIAL_H,
  16. DIAL_R,
  17. DIAL_SPAN,
  18. DIAL_TOP,
  19. DIAL_Z,
  20. FOOT_H,
  21. FRAME_X,
  22. FRAME_Y,
  23. GLASS_TOP,
  24. HOUSING_H,
  25. HOUSING_R,
  26. HOUSING_Z,
  27. P,
  28. PAD_H,
  29. PAD_R,
  30. PLUNGER_Z,
  31. POST,
  32. POST_L,
  33. POST_R,
  34. PROBE_HOME,
  35. RAIL_D,
  36. RAIL_H,
  37. RAIL_Y,
  38. RAIL_Z,
  39. ROD_HALF,
  40. ROD_Y,
  41. ROD_Z,
  42. STEM,
  43. at,
  44. } from "./view";
  45. const solid = (plan: Plan, z: number, h: number, steps = 6, bevel = 0) => slabOf(plan, z, h, P, steps, bevel);
  46. const cylinder = (cx: number, cy: number, r: number, z: number, h: number, steps = 32) => extrude(circleRing(cx, cy, r, steps), z, h, P, { convex: true });
  47. const ring = (cx: number, cy: number, r: number, z: number, steps = 40) => onTop(circleRing(cx, cy, r, steps), z, P, true);
  48. const segment = (a: Point3, b: Point3) => pathOf([at(a), at(b)]);
  49. const join = (paths: string[]) => paths.join("");
  50. const RADIANS = Math.PI / 180;
  51. function knurl(cx: number, cy: number, r: number, z0: number, z1: number, count: number) {
  52. const lines: string[] = [];
  53. for (let index = 0; index < count; index++) {
  54. const angle = (index / count) * Math.PI * 2;
  55. const nx = Math.cos(angle);
  56. const ny = Math.sin(angle);
  57. if (nx * Math.cos(57 * RADIANS) + ny * Math.sin(57 * RADIANS) < 0.15) continue;
  58. lines.push(segment([cx + r * nx, cy + r * ny, z0 + 0.4], [cx + r * nx, cy + r * ny, z1 - 0.4]));
  59. }
  60. return join(lines);
  61. }
  62. function sideArc(cx: number, cy: number, r: number, z: number, steps = 24) {
  63. const points: Flat[] = [];
  64. for (let step = 0; step <= steps; step++) {
  65. const angle = (57 - 90 + (step / steps) * 180) * RADIANS;
  66. points.push(at([cx + r * Math.cos(angle), cy + r * Math.sin(angle), z]));
  67. }
  68. return pathOf(points);
  69. }
  70. function faceRing(x: number, cy: number, cz: number, r: number, steps = 20) {
  71. const points: Flat[] = [];
  72. for (let step = 0; step < steps; step++) {
  73. const angle = (step / steps) * Math.PI * 2;
  74. points.push(at([x, cy + r * Math.cos(angle), cz + r * Math.sin(angle)]));
  75. }
  76. return pathOf(points, true);
  77. }
  78. const FEET: Plane[] = [
  79. [BASE.x + 18, BASE.y + 16],
  80. [BASE.x + BASE.w - 18, BASE.y + 16],
  81. [BASE.x + 18, BASE.y + BASE.d - 16],
  82. [BASE.x + BASE.w - 18, BASE.y + BASE.d - 16],
  83. ];
  84. const BASE_SCREWS: Plane[] = [
  85. [BASE.x + 8, BASE.y + 8],
  86. [BASE.x + BASE.w - 8, BASE.y + 8],
  87. [BASE.x + 8, BASE.y + BASE.d - 8],
  88. [BASE.x + BASE.w - 8, BASE.y + BASE.d - 8],
  89. ];
  90. const PLATE: Plan = { x: BASE.x + 20, y: FRAME_Y + BAR + 6, w: 56, d: 11, r: 2 };
  91. export const BASE_ART = {
  92. halo: solid(BASE, -BASE_H - FOOT_H, BASE_H).fill,
  93. feet: FEET.map(([x, y]) => [cylinder(x, y, 7, -BASE_H - FOOT_H, 1.6), cylinder(x, y, 5.5, -BASE_H - FOOT_H + 1.6, FOOT_H - 1.6)]),
  94. slab: solid(BASE, -BASE_H, BASE_H, 8, 1.6),
  95. groove: planOutline(insetPlan(BASE, 4.5), 0, P),
  96. screws: BASE_SCREWS.map(([x, y]) => at([x, y, 0])),
  97. screwRings: join(BASE_SCREWS.map(([x, y]) => ring(x, y, 2.2, 0, 16))),
  98. plate: solid(PLATE, 0, 0.8, 4),
  99. plateLine: planOutline(insetPlan(PLATE, 1.6), 0.8, P),
  100. plateScrews: [at([PLATE.x + 3.5, PLATE.y + PLATE.d / 2, 0.8]), at([PLATE.x + PLATE.w - 3.5, PLATE.y + PLATE.d / 2, 0.8])],
  101. plateRule: join([0, 1, 2].map((row) => onTop([[PLATE.x + 8, PLATE.y + 3.4 + row * 2.1], [PLATE.x + (row === 1 ? 34 : 44), PLATE.y + 3.4 + row * 2.1]], 0.8, P))),
  102. };
  103. const POST_PLANS: Plan[] = [POST_L, POST_R].map((x) => ({ x, y: RAIL_Y - POST / 2, w: POST, d: POST, r: 2 }));
  104. const RAIL: Plan = { x: POST_L - 5, y: RAIL_Y - RAIL_D / 2, w: POST_R + POST - POST_L + 10, d: RAIL_D, r: 2 };
  105. const RAIL_FACE = RAIL_Y + RAIL_D / 2;
  106. const RAIL_TOP = RAIL_Z + RAIL_H;
  107. const RULE_FROM = POST_L + POST + 4;
  108. const RULE_TO = POST_R - 4;
  109. function ruler() {
  110. const minor: string[] = [];
  111. const major: string[] = [];
  112. for (let x = Math.ceil(RULE_FROM / 5) * 5; x <= RULE_TO; x += 5) {
  113. const big = x % 25 === 0;
  114. (big ? major : minor).push(segment([x, RAIL_FACE, RAIL_TOP - 1.2], [x, RAIL_FACE, RAIL_TOP - (big ? 5 : 3)]));
  115. }
  116. return { minor: join(minor), major: join(major) };
  117. }
  118. const RULE = ruler();
  119. export const RAIL_ART = {
  120. feet: POST_PLANS.map((plan) => solid(insetPlan(plan, -3), 0, 2, 4)),
  121. footScrews: POST_PLANS.flatMap((plan) => [at([plan.x - 1, plan.y + POST / 2, 2]), at([plan.x + POST + 1, plan.y + POST / 2, 2])]),
  122. posts: POST_PLANS.map((plan) => solid(plan, 2, RAIL_Z - 2, 4)),
  123. postLines: join(POST_PLANS.map((plan) => segment([plan.x + POST, plan.y + POST, 6], [plan.x + POST, plan.y + POST, RAIL_Z - 4]))),
  124. rail: solid(RAIL, RAIL_Z, RAIL_H, 4, 0.8),
  125. railScrews: POST_PLANS.map((plan) => at([plan.x + POST / 2, RAIL_Y, RAIL_TOP])),
  126. ruleMinor: RULE.minor,
  127. ruleMajor: RULE.major,
  128. ruleBase: segment([RULE_FROM, RAIL_FACE, RAIL_TOP - 1.2], [RULE_TO, RAIL_FACE, RAIL_TOP - 1.2]),
  129. stops: [RULE_FROM - 2, RULE_TO + 2].map((x) => solid({ x: x - 1.5, y: RAIL_Y - RAIL_D / 2 - 0.5, w: 3, d: RAIL_D + 1, r: 0.8 }, RAIL_TOP, 2.2, 3)),
  130. };
  131. export const RIDER = {
  132. body: solid({ x: -1.6, y: RAIL_Y - RAIL_D / 2 - 0.6, w: 3.2, d: RAIL_D + 1.2, r: 0.8 }, RAIL_TOP, 1.4, 3),
  133. tick: segment([0, RAIL_FACE + 0.6, RAIL_TOP + 1.4], [0, RAIL_FACE + 0.6, RAIL_TOP - 3]),
  134. };
  135. const H = PROBE_HOME;
  136. const BLOCK: Plan = { x: H - 11, y: RAIL_Y - 7, w: 22, d: 14, r: 2.5 };
  137. const BLOCK_Z = RAIL_TOP;
  138. const BLOCK_H = 9;
  139. const ARM_Z = BLOCK_Z + 3;
  140. const ARM_H = 5;
  141. const ARM: Plan = { x: H - 3.5, y: BLOCK.y + BLOCK.d - 1, w: 7, d: -(BLOCK.y + BLOCK.d - 1) + 2, r: 1.5 };
  142. export const PROBE_ART = {
  143. block: solid(BLOCK, BLOCK_Z, BLOCK_H, 5, 0.8),
  144. blockLine: planOutline(insetPlan(BLOCK, 2), BLOCK_Z + BLOCK_H, P),
  145. blockScrews: [at([BLOCK.x + 3, BLOCK.y + 3, BLOCK_Z + BLOCK_H]), at([BLOCK.x + BLOCK.w - 3, BLOCK.y + 3, BLOCK_Z + BLOCK_H])],
  146. thumb: cylinder(H - 5, RAIL_Y - 1, 2.6, BLOCK_Z + BLOCK_H, 3.2, 20),
  147. thumbKnurl: knurl(H - 5, RAIL_Y - 1, 2.6, BLOCK_Z + BLOCK_H, BLOCK_Z + BLOCK_H + 3.2, 16),
  148. pointer: solid({ x: H - 2, y: BLOCK.y + BLOCK.d, w: 4, d: 1, r: 0.4 }, RAIL_TOP - 4, 4 + 2, 2),
  149. index: segment([H, BLOCK.y + BLOCK.d + 1, RAIL_TOP + 1.5], [H, BLOCK.y + BLOCK.d + 1, RAIL_TOP - 3.6]),
  150. arm: solid(ARM, ARM_Z, ARM_H, 4, 0.6),
  151. armLine: onTop([[H, ARM.y + 3], [H, -9]], ARM_Z + ARM_H, P),
  152. armScrews: [at([H, ARM.y + 2, ARM_Z + ARM_H]), at([H, -2, ARM_Z + ARM_H])],
  153. housing: cylinder(H, 0, HOUSING_R, HOUSING_Z, HOUSING_H - 2, 32),
  154. cap: cylinder(H, 0, HOUSING_R + 0.8, HOUSING_Z + HOUSING_H - 2, 2, 32),
  155. collar: cylinder(H, 0, HOUSING_R + 0.8, HOUSING_Z, 2.4, 32),
  156. housingSeam: join([HOUSING_Z + 6, HOUSING_Z + 11].map((z) => sideArc(H, 0, HOUSING_R, z))),
  157. lock: solid({ x: H - 1.6, y: HOUSING_R - 0.5, w: 3.2, d: 3.5, r: 0.8 }, HOUSING_Z + 8, 3.2, 2),
  158. lockHead: cylinder(H, HOUSING_R + 4.4, 2.2, HOUSING_Z + 7.4, 4.4, 16),
  159. };
  160. const PAD_Z = GLASS_TOP;
  161. export const FINGER_ART = {
  162. stem: cylinder(H, 0, 1.5, PAD_Z + PAD_H + 2, STEM, 16),
  163. stemMarks: join([8, 11, 14].map((rise) => sideArc(H, 0, 1.5, PAD_Z + PAD_H + 2 + rise, 12))),
  164. collar: cylinder(H, 0, 2.6, PAD_Z + PAD_H, 2, 20),
  165. pad: cylinder(H, 0, PAD_R, PAD_Z, PAD_H, 32),
  166. padRing: ring(H, 0, PAD_R - 1.2, PAD_Z + PAD_H, 28),
  167. };
  168. const BARS: Plan[] = [
  169. { x: -FRAME_X - BAR, y: -FRAME_Y - BAR, w: (FRAME_X + BAR) * 2, d: BAR, r: 1.5 },
  170. { x: -FRAME_X - BAR, y: -FRAME_Y, w: BAR, d: FRAME_Y * 2, r: 0.6 },
  171. { x: FRAME_X, y: -FRAME_Y, w: BAR, d: FRAME_Y * 2, r: 0.6 },
  172. { x: -FRAME_X - BAR, y: FRAME_Y, w: (FRAME_X + BAR) * 2, d: BAR, r: 1.5 },
  173. ];
  174. const barScrews = (plan: Plan): Flat[] =>
  175. plan.w > plan.d
  176. ? [-1, 1].map((side) => at([side * (FRAME_X + BAR / 2), plan.y + BAR / 2, BAR_H]))
  177. : [-1, 1].map((side) => at([plan.x + BAR / 2, side * (FRAME_Y - 6), BAR_H]));
  178. const FLOOR: Plan = { x: -FRAME_X, y: -FRAME_Y, w: FRAME_X * 2, d: FRAME_Y * 2, r: 0 };
  179. function floorGrid() {
  180. const lines: string[] = [];
  181. for (let x = -FRAME_X + 10; x < FRAME_X; x += 10) lines.push(onTop([[x, -FRAME_Y], [x, FRAME_Y]], 0.8, P));
  182. for (let y = -FRAME_Y + 7.4; y < FRAME_Y; y += 7.4) lines.push(onTop([[-FRAME_X, y], [FRAME_X, y]], 0.8, P));
  183. return join(lines);
  184. }
  185. function barTicks() {
  186. const minor: string[] = [];
  187. const major: string[] = [];
  188. const y = FRAME_Y + 1;
  189. for (let x = -40; x <= 40; x += 2) {
  190. const big = x % 10 === 0;
  191. (big ? major : minor).push(onTop([[x, y], [x, y + (big ? 3.2 : 1.8)]], BAR_H, P));
  192. }
  193. return { minor: join(minor), major: join(major) };
  194. }
  195. const TICKS = barTicks();
  196. export const FRAME_ART = {
  197. back: solid(BARS[0]!, 0, BAR_H, 4, 0.8),
  198. left: solid(BARS[1]!, 0, BAR_H, 3, 0.8),
  199. right: solid(BARS[2]!, 0, BAR_H, 3, 0.8),
  200. front: solid(BARS[3]!, 0, BAR_H, 4, 0.8),
  201. backScrews: barScrews(BARS[0]!),
  202. leftScrews: barScrews(BARS[1]!),
  203. rightScrews: barScrews(BARS[2]!),
  204. frontScrews: barScrews(BARS[3]!),
  205. floor: solid(FLOOR, 0, 0.8, 2),
  206. grid: floorGrid(),
  207. ticksMinor: TICKS.minor,
  208. ticksMajor: TICKS.major,
  209. zero: onTop([[0, FRAME_Y + 0.6], [0, FRAME_Y + BAR - 0.6]], BAR_H, P),
  210. rodEnds: join([-ROD_Y, ROD_Y].map((y) => faceRing(FRAME_X + BAR, y, ROD_Z, 3, 16))),
  211. rodNuts: [-ROD_Y, ROD_Y].map((y) => at([FRAME_X + BAR, y, ROD_Z])),
  212. bore: faceRing(FRAME_X + BAR, 0, PLUNGER_Z, 2.2, 14),
  213. leftBores: join([-ROD_Y, ROD_Y].map((y) => faceRing(-FRAME_X, y, ROD_Z, 3, 16))),
  214. };
  215. export const RODS = [-ROD_Y, ROD_Y].map((y) => solid({ x: -FRAME_X, y: y - ROD_HALF, w: FRAME_X * 2, d: ROD_HALF * 2, r: ROD_HALF - 0.2 }, ROD_Z - ROD_HALF, ROD_HALF * 2, 3));
  216. const SEAT: Plan = { x: -63, y: -18, w: 126, d: 36, r: 18 };
  217. const BUSHES: Plan[] = [-1, 1].flatMap((side) =>
  218. [-ROD_Y, ROD_Y].map((y) => ({ x: side < 0 ? -BUSH_END : BUSH_END - BUSH_W, y: y - BUSH_D / 2, w: BUSH_W, d: BUSH_D, r: 2 })),
  219. );
  220. export const CARRIAGE_ART = {
  221. halo: solid(insetPlan(CARRIAGE, 3), 0.8, 0.1).fill,
  222. plate: solid(CARRIAGE, CARRIAGE_Z, CARRIAGE_H, 6, 0.8),
  223. seat: planOutline(SEAT, CARRIAGE_TOP, P, 10),
  224. seatInner: planOutline(insetPlan(SEAT, 2.4), CARRIAGE_TOP, P, 10),
  225. bushes: BUSHES.map((plan) => solid(plan, CARRIAGE_Z - 1, CARRIAGE_H + 4, 3, 0.6)),
  226. bushScrews: BUSHES.flatMap((plan) => [at([plan.x + 3, plan.y + plan.d / 2, CARRIAGE_TOP + 3]), at([plan.x + plan.w - 3, plan.y + plan.d / 2, CARRIAGE_TOP + 3])]),
  227. bushBores: join([-ROD_Y, ROD_Y].map((y) => faceRing(BUSH_END, y, ROD_Z, 2.8, 14))),
  228. contact: solid({ x: BUSH_END - 8, y: -4, w: 8, d: 8, r: 2 }, CARRIAGE_Z, CARRIAGE_H, 3, 0.4),
  229. rivets: [-56, -28, 0, 28, 56].map((x) => at([x, CARRIAGE.y + CARRIAGE.d - 2.4, CARRIAGE_TOP])),
  230. };
  231. export const PLUNGER_ART = {
  232. rod: solid({ x: BUSH_END, y: -1.1, w: 44, d: 2.2, r: 1 }, PLUNGER_Z - 1.1, 2.2, 3),
  233. collar: solid({ x: BUSH_END + 0.4, y: -2.2, w: 2.4, d: 4.4, r: 1 }, PLUNGER_Z - 2.2, 4.4, 3, 0.3),
  234. };
  235. const [DX, DY] = DIAL;
  236. const STAND: Plan = { x: DX - 15, y: DY - 14, w: 30, d: 28, r: 3 };
  237. function dialTicks() {
  238. const minor: string[] = [];
  239. const major: string[] = [];
  240. for (let step = 0; step < DIAL_SPAN; step++) {
  241. const angle = (step / DIAL_SPAN) * Math.PI * 2;
  242. const big = step % 5 === 0;
  243. const sx = Math.sin(angle);
  244. const sy = -Math.cos(angle);
  245. const outer = DIAL_R - 2.4;
  246. const inner = outer - (big ? 2.6 : 1.4);
  247. (big ? major : minor).push(onTop([[DX + sx * inner, DY + sy * inner], [DX + sx * outer, DY + sy * outer]], DIAL_TOP, P));
  248. }
  249. return { minor: join(minor), major: join(major) };
  250. }
  251. const DIAL_TICKS = dialTicks();
  252. const GIVE_ANGLE = (14 / DIAL_SPAN) * Math.PI * 2;
  253. const R_GIVE = DIAL_R - 1.4;
  254. export const DIAL_ART = {
  255. stand: solid(STAND, 0, DIAL_Z, 4, 0.6),
  256. standScrews: [at([STAND.x + 3.4, STAND.y + 3.4, DIAL_Z]), at([STAND.x + STAND.w - 3.4, STAND.y + STAND.d - 3.4, DIAL_Z]), at([STAND.x + 3.4, STAND.y + STAND.d - 3.4, DIAL_Z]), at([STAND.x + STAND.w - 3.4, STAND.y + 3.4, DIAL_Z])],
  257. sleeve: solid({ x: FRAME_X + BAR, y: -2.6, w: DX - DIAL_R - FRAME_X - BAR + 0.6, d: 5.2, r: 1 }, PLUNGER_Z - 2.6, 5.2, 3),
  258. clamp: cylinder(FRAME_X + BAR + 3.2, 0, 1.6, PLUNGER_Z + 2.6, 2, 12),
  259. puck: cylinder(DX, DY, DIAL_R, DIAL_Z, DIAL_H, 56),
  260. bezel: ring(DX, DY, DIAL_R - 1, DIAL_TOP, 56),
  261. face: ring(DX, DY, DIAL_R - 2.4, DIAL_TOP, 56),
  262. knurl: knurl(DX, DY, DIAL_R, DIAL_Z + DIAL_H - 3, DIAL_Z + DIAL_H, 56),
  263. band: sideArc(DX, DY, DIAL_R, DIAL_Z + DIAL_H - 3, 40),
  264. minor: DIAL_TICKS.minor,
  265. major: DIAL_TICKS.major,
  266. give: [-1, 1].map((side) => at([DX + Math.sin(side * GIVE_ANGLE) * R_GIVE, DY - Math.cos(side * GIVE_ANGLE) * R_GIVE, DIAL_TOP])),
  267. crown: solid({ x: DX + DIAL_R - 0.6, y: -2.4, w: 4.4, d: 4.8, r: 1.2 }, DIAL_Z + 2, 4, 3),
  268. crownCap: solid({ x: DX + DIAL_R + 3.6, y: -3.2, w: 2.6, d: 6.4, r: 1.2 }, DIAL_Z + 1.2, 5.6, 3),
  269. lug: solid({ x: DX - 3, y: DY - DIAL_R - 3.4, w: 6, d: 4, r: 1.2 }, DIAL_Z + 1.5, 5, 3),
  270. label: topMatrix([DX, DY + 5.2], DIAL_TOP, P, "x"),
  271. };
  272. const REST_GLASS = { x: -60, y: -16, w: 120, d: 32, r: 16 };
  273. export const REST_OUTLINE = planOutline(REST_GLASS, GLASS_TOP, P, 12);