dimension-change.test.mjs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. // Changing a dimension moves as little of the sketch as it can (solveChange
  2. // in js/sketch/solver.js): an edge shared with other shapes stays, the free
  3. // end of the dimension moves, and nothing turns over.
  4. import * as M from '../js/sketch/model.js';
  5. import { createSolver } from '../js/sketch/solver.js';
  6. let failed = 0;
  7. const a = (c, m, extra) => { if (!c) { failed++; console.log('FAIL', m, extra === undefined ? '' : JSON.stringify(extra)); } };
  8. const near = (x, y, tol) => Math.abs(x - y) < (tol || 1e-6);
  9. const P = (sk, id) => M.point(sk, id);
  10. const solver = await createSolver();
  11. // A rectangle as the Rectangle tool draws it: first corner, the corner level
  12. // with it, the opposite corner, the corner above / below the first
  13. function rect(sk, from, to, start) {
  14. const c = [from, [to[0], from[1]], to, [from[0], to[1]]];
  15. const p = [start || M.addPoint(sk, c[0][0], c[0][1]), M.addPoint(sk, c[1][0], c[1][1]),
  16. M.addPoint(sk, c[2][0], c[2][1]), M.addPoint(sk, c[3][0], c[3][1])];
  17. const l = p.map((id, i) => M.addLine(sk, id, p[(i + 1) % 4]));
  18. ['horizontal', 'vertical', 'horizontal', 'vertical'].forEach((t, i) => M.addConstraint(sk, { type: t, line: l[i] }));
  19. return { p, l };
  20. }
  21. const places = sk => JSON.stringify(sk.geometry.filter(g => g.type === 'point').map(g => [g.id, +g.x.toFixed(6), +g.y.toFixed(6)]));
  22. const setValue = (sk, id, value) => { sk.constraints.find(c => c.id === id).value = value; };
  23. // Three rectangles side by side on one bottom line, the corners where they
  24. // meet shared (snapped), as in the owner's report
  25. function threeInARow() {
  26. const sk = { geometry: [], constraints: [] };
  27. const L = rect(sk, [-15, 2.9], [-5, -4.1]);
  28. const Mi = rect(sk, [-5, -4.1], [6.5, -2.05], L.p[2]);
  29. const R = rect(sk, [6.5, -4.1], [16.5, 30.6], Mi.p[1]);
  30. const d = {
  31. lw: M.addConstraint(sk, { type: 'distance', a: L.p[0], b: L.p[1], value: 10 }),
  32. lh: M.addConstraint(sk, { type: 'distance', a: L.p[3], b: L.p[0], value: 7 }),
  33. mh: M.addConstraint(sk, { type: 'distance', a: Mi.p[3], b: Mi.p[0], value: 2.05 }),
  34. rw: M.addConstraint(sk, { type: 'distance', a: R.p[3], b: R.p[2], value: 10 }),
  35. rh: M.addConstraint(sk, { type: 'distance', a: R.p[1], b: R.p[2], value: 34.7 }),
  36. all: M.addConstraint(sk, { type: 'hdistance', a: L.p[3], b: R.p[1], value: 31.5 })
  37. };
  38. a(solver.solve(sk).ok, 'three rectangles solve');
  39. return { sk, L, Mi, R, d };
  40. }
  41. // 1. the report: shortening the tall rectangle brings its top down; the
  42. // shared bottom line and the other two rectangles stay where they are
  43. {
  44. const { sk, L, Mi, R, d } = threeInARow();
  45. const others = JSON.stringify([L.p, Mi.p].flat().map(id => P(sk, id)));
  46. setValue(sk, d.rh, 7);
  47. const r = solver.solveChange(sk, [d.rh]);
  48. a(r.ok && r.dof === 2, 'shorter tall rectangle solves', r);
  49. a(near(P(sk, R.p[1])[1], -4.1) && near(P(sk, R.p[0])[1], -4.1), 'its bottom edge stays on the shared line', [P(sk, R.p[0]), P(sk, R.p[1])]);
  50. a(near(P(sk, R.p[2])[1], 2.9) && near(P(sk, R.p[3])[1], 2.9), 'its top edge comes down', [P(sk, R.p[2]), P(sk, R.p[3])]);
  51. a(JSON.stringify([L.p, Mi.p].flat().map(id => P(sk, id))) === others, 'the other two rectangles do not move');
  52. // and back up, past where it started
  53. setValue(sk, d.rh, 50);
  54. a(solver.solveChange(sk, [d.rh]).ok && near(P(sk, R.p[1])[1], -4.1) && near(P(sk, R.p[2])[1], 45.9), 'taller: the top goes up', P(sk, R.p[2]));
  55. }
  56. // the plain solve is what moved the shared line before
  57. {
  58. const { sk, R, d } = threeInARow();
  59. setValue(sk, d.rh, 7);
  60. solver.solve(sk);
  61. a(!near(P(sk, R.p[1])[1], -4.1), 'a plain solve still shares the change out (the old behaviour)', P(sk, R.p[1]));
  62. }
  63. // 2. the same for the other snapped edges: the free edge moves
  64. {
  65. const { sk, L, Mi, R, d } = threeInARow();
  66. setValue(sk, d.lh, 12);
  67. a(solver.solveChange(sk, [d.lh]).ok, 'left height solves');
  68. a(near(P(sk, L.p[3])[1], -4.1) && near(P(sk, L.p[0])[1], 7.9), 'left rectangle grows upwards from the shared line', [P(sk, L.p[3]), P(sk, L.p[0])]);
  69. a(near(P(sk, R.p[2])[1], 30.6) && near(P(sk, Mi.p[2])[1], -2.05), 'the others stay');
  70. setValue(sk, d.mh, 5);
  71. a(solver.solveChange(sk, [d.mh]).ok, 'middle height solves');
  72. a(near(P(sk, Mi.p[0])[1], -4.1) && near(P(sk, Mi.p[3])[1], 0.9) && near(P(sk, Mi.p[2])[1], 0.9), 'middle rectangle grows upwards', [P(sk, Mi.p[3]), P(sk, Mi.p[2])]);
  73. // Width of the left rectangle with the overall 31.5 holding both outer
  74. // sides: it grows into the middle one (3 points move) rather than
  75. // pushing the right rectangle along (7 points)
  76. setValue(sk, d.lw, 14);
  77. a(solver.solveChange(sk, [d.lw]).ok, 'left width solves');
  78. a(near(P(sk, L.p[3])[0], -15) && near(P(sk, L.p[2])[0], -1) && near(P(sk, R.p[1])[0], 16.5), 'left rectangle grows into the middle one', [P(sk, L.p[3]), P(sk, L.p[2])]);
  79. }
  80. // Without the overall width the left rectangle's free side moves out
  81. {
  82. const { sk, L, d } = threeInARow();
  83. M.remove(sk, [d.all]);
  84. solver.solve(sk);
  85. setValue(sk, d.lw, 14);
  86. a(solver.solveChange(sk, [d.lw]).ok, 'left width solves without the overall width');
  87. a(near(P(sk, L.p[2])[0], -5) && near(P(sk, L.p[3])[0], -19) && near(P(sk, L.p[0])[0], -19), 'left rectangle widens to the left', [P(sk, L.p[0]), P(sk, L.p[2])]);
  88. }
  89. // 3. a rectangle alone: on a tie the corner drawn first stays
  90. {
  91. const sk = { geometry: [], constraints: [] };
  92. const { p } = rect(sk, [0, 0], [20, -10]);
  93. const w = M.addConstraint(sk, { type: 'distance', a: p[0], b: p[1], value: 20 });
  94. const h = M.addConstraint(sk, { type: 'distance', a: p[1], b: p[2], value: 10 });
  95. a(solver.solve(sk).ok, 'lone rectangle');
  96. setValue(sk, w, 30);
  97. setValue(sk, h, 4);
  98. a(solver.solveChange(sk, [w]).ok && solver.solveChange(sk, [h]).ok, 'lone rectangle resizes');
  99. a(near(P(sk, p[0])[0], 0) && near(P(sk, p[0])[1], 0), 'first corner stays', P(sk, p[0]));
  100. a(near(P(sk, p[2])[0], 30) && near(P(sk, p[2])[1], -4), 'opposite corner moves', P(sk, p[2]));
  101. // A size the other way round keeps the same corner
  102. const sk2 = { geometry: [], constraints: [] };
  103. const q = rect(sk2, [0, 0], [20, -10]).p;
  104. const h2 = M.addConstraint(sk2, { type: 'distance', a: q[2], b: q[1], value: 10 });
  105. solver.solve(sk2);
  106. setValue(sk2, h2, 4);
  107. a(solver.solveChange(sk2, [h2]).ok && near(P(sk2, q[1])[1], 0) && near(P(sk2, q[2])[1], -4), 'the earlier end stays whichever way the dimension was picked', [P(sk2, q[1]), P(sk2, q[2])]);
  108. }
  109. // 4. anchored to the origin and fully constrained: the change still lands
  110. {
  111. const sk = { geometry: [], constraints: [] };
  112. const { p } = rect(sk, [0, 0], [20, 10]);
  113. M.addConstraint(sk, { type: 'coincident', a: p[0], b: M.ORIGIN });
  114. M.addConstraint(sk, { type: 'distance', a: p[0], b: p[1], value: 20 });
  115. const h = M.addConstraint(sk, { type: 'distance', a: p[2], b: p[1], value: 10 });
  116. a(solver.solve(sk).dof === 0, 'anchored rectangle is fully constrained');
  117. setValue(sk, h, 25);
  118. const r = solver.solveChange(sk, [h]);
  119. a(r.ok && r.dof === 0 && near(P(sk, p[2])[1], 25) && near(P(sk, p[0])[0], 0), 'fully constrained change', P(sk, p[2]));
  120. a(r.redundant.length === 0 && r.conflicting.length === 0, 'no holds left in the report', r);
  121. }
  122. // 5. a diameter keeps the centre; an angle turns the later line
  123. {
  124. const sk = { geometry: [], constraints: [] };
  125. const c = M.addPoint(sk, 5, 5);
  126. const k = M.addCircle(sk, c, 3);
  127. const dia = M.addConstraint(sk, { type: 'diameter', curve: k, value: 6 });
  128. const o = M.addPoint(sk, 30, 0);
  129. const e1 = M.addPoint(sk, 50, 0);
  130. const e2 = M.addPoint(sk, 40, 10);
  131. const l1 = M.addLine(sk, o, e1);
  132. const l2 = M.addLine(sk, o, e2);
  133. M.addConstraint(sk, { type: 'distance', a: o, b: e2, value: Math.hypot(10, 10) });
  134. const ang = M.addConstraint(sk, { type: 'angle', l1: l1, l2: l2, value: Math.PI / 4 });
  135. a(solver.solve(sk).ok, 'circle and angle');
  136. setValue(sk, dia, 16);
  137. a(solver.solveChange(sk, [dia]).ok && near(M.find(sk, k).r, 8) && near(P(sk, c)[0], 5) && near(P(sk, c)[1], 5), 'circle grows about its centre', [M.find(sk, k).r, P(sk, c)]);
  138. setValue(sk, ang, Math.PI / 2);
  139. a(solver.solveChange(sk, [ang]).ok, 'angle solves');
  140. a(near(P(sk, e1)[0], 50) && near(P(sk, e1)[1], 0) && near(P(sk, o)[0], 30), 'the first line stays', [P(sk, o), P(sk, e1)]);
  141. a(near(P(sk, e2)[0], 30, 1e-5) && near(P(sk, e2)[1], Math.hypot(10, 10), 1e-5), 'the second line turns about the shared end', P(sk, e2));
  142. }
  143. // 6. several at once (Manage > Parameters), and a value that cannot be met
  144. {
  145. const { sk, L, Mi, R, d } = threeInARow();
  146. setValue(sk, d.lh, 3);
  147. setValue(sk, d.rh, 9);
  148. const r = solver.solveChange(sk, [d.lh, d.rh]);
  149. a(r.ok && near(P(sk, L.p[0])[1], -1.1) && near(P(sk, R.p[2])[1], 4.9), 'two heights at once, both from the shared line', [P(sk, L.p[0]), P(sk, R.p[2])]);
  150. a(near(P(sk, Mi.p[0])[1], -4.1) && near(P(sk, R.p[1])[1], -4.1), 'shared line stays');
  151. a(sk.constraints.find(c => c.id === d.lh).value === 3 && sk.constraints.find(c => c.id === d.rh).value === 9, 'values are kept as typed');
  152. }
  153. // a value that cannot be met: a triangle side longer than the other two
  154. {
  155. const sk = { geometry: [], constraints: [] };
  156. const t = [[0, 0], [10, 0], [5, 8.66]].map(c => M.addPoint(sk, c[0], c[1]));
  157. t.forEach((id, i) => M.addLine(sk, id, t[(i + 1) % 3]));
  158. const s = t.map((id, i) => M.addConstraint(sk, { type: 'distance', a: id, b: t[(i + 1) % 3], value: 10 }));
  159. a(solver.solve(sk).ok, 'triangle solves');
  160. const before = places(sk);
  161. setValue(sk, s[0], 25);
  162. const bad = solver.solveChange(sk, [s[0]]);
  163. a(!bad.ok && places(sk) === before, 'an impossible value fails and leaves the sketch alone', bad);
  164. setValue(sk, s[0], 15);
  165. a(solver.solveChange(sk, [s[0]]).ok && near(M.measure(sk, sk.constraints.find(c => c.id === s[0])), 15), 'a possible one lands');
  166. }
  167. // 7. a long row: a change at one end leaves the rest alone, and stays quick
  168. {
  169. const sk = { geometry: [], constraints: [] };
  170. let start = null;
  171. let last = null;
  172. const heights = [];
  173. for (let i = 0; i < 25; i++) {
  174. last = rect(sk, [i * 10, 0], [i * 10 + 10, 5 + i % 3], start);
  175. start = last.p[1];
  176. heights.push(M.addConstraint(sk, { type: 'distance', a: last.p[1], b: last.p[2], value: 5 + i % 3 }));
  177. M.addConstraint(sk, { type: 'distance', a: last.p[0], b: last.p[1], value: 10 });
  178. }
  179. a(solver.solve(sk).ok, 'row of 25 solves');
  180. const first = JSON.parse(JSON.stringify(sk.geometry.slice(0, 8)));
  181. setValue(sk, heights[24], 20);
  182. const t0 = performance.now();
  183. const r = solver.solveChange(sk, [heights[24]]);
  184. const ms = performance.now() - t0;
  185. a(r.ok && near(P(sk, last.p[2])[1], 20) && near(P(sk, last.p[1])[1], 0), 'last rectangle grows upwards', P(sk, last.p[2]));
  186. const drift = Math.max(...first.map((g, k) => g.type === 'point' ? Math.hypot(sk.geometry[k].x - g.x, sk.geometry[k].y - g.y) : 0));
  187. a(drift < 1e-6, 'the first rectangles do not move', drift);
  188. a(ms < 1500, 'quick enough to run on Enter', ms);
  189. console.log('row of 25: ' + ms.toFixed(0) + ' ms');
  190. }
  191. solver.destroy();
  192. console.log(failed ? failed + ' FAILED' : 'all dimension change tests passed');
  193. process.exitCode = failed ? 1 : 0;