lk3d 0.2.0 → 0.2.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -169,6 +169,26 @@
169
169
  "fixed": {
170
170
  "type": "boolean",
171
171
  "description": "true: the reader cannot move the cut (by dragging its face or its slider); the page still can. By default, while the model is cut open, the reader moves the plane along its normal."
172
+ },
173
+ "second": {
174
+ "description": "A second plane, for a quarter cut: then only what lies beyond both planes is cut away (two planes at right angles take out a quarter). false: none (a step's, over the scene's).",
175
+ "anyOf": [
176
+ {
177
+ "const": false
178
+ },
179
+ {
180
+ "type": "object",
181
+ "properties": {
182
+ "normal": {
183
+ "$ref": "#/$defs/vec3"
184
+ },
185
+ "offset": {
186
+ "type": "number"
187
+ }
188
+ },
189
+ "additionalProperties": false
190
+ }
191
+ ]
172
192
  }
173
193
  },
174
194
  "additionalProperties": false
@@ -225,6 +245,14 @@
225
245
  "x-message": "scale is a bar ({bar: {length, label}}) and/or a note ({note: \"not to scale\"})",
226
246
  "additionalProperties": false
227
247
  },
248
+ "dimensions": {
249
+ "type": "array",
250
+ "minItems": 1,
251
+ "items": {
252
+ "$ref": "#/$defs/dimension"
253
+ },
254
+ "description": "Dimensions drawn on the figure: lengths between points and angles at vertices, with their values, as on a technical drawing; worked out from the points as shown, so a length to a moving part changes as it moves."
255
+ },
228
256
  "models": {
229
257
  "type": "object",
230
258
  "description": "glTF 2.0 models (.glb) by name, for {model: name} shapes.",
@@ -2059,6 +2087,26 @@
2059
2087
  },
2060
2088
  "offset": {
2061
2089
  "type": "number"
2090
+ },
2091
+ "second": {
2092
+ "description": "A second plane, for a quarter cut: then only what lies beyond both planes is cut away (two planes at right angles take out a quarter). false: none (a step's, over the scene's).",
2093
+ "anyOf": [
2094
+ {
2095
+ "const": false
2096
+ },
2097
+ {
2098
+ "type": "object",
2099
+ "properties": {
2100
+ "normal": {
2101
+ "$ref": "#/$defs/vec3"
2102
+ },
2103
+ "offset": {
2104
+ "type": "number"
2105
+ }
2106
+ },
2107
+ "additionalProperties": false
2108
+ }
2109
+ ]
2062
2110
  }
2063
2111
  },
2064
2112
  "additionalProperties": false,
@@ -2112,6 +2160,14 @@
2112
2160
  },
2113
2161
  "minItems": 1,
2114
2162
  "description": "Motions played as the step comes, from the value it gives them."
2163
+ },
2164
+ "dimensions": {
2165
+ "type": "array",
2166
+ "items": {
2167
+ "type": "string",
2168
+ "minLength": 1
2169
+ },
2170
+ "description": "The dimensions shown in this step, by id (none: [ ]); without it, the scene's own (those not hidden)."
2115
2171
  }
2116
2172
  },
2117
2173
  "anyOf": [
@@ -2124,6 +2180,114 @@
2124
2180
  ],
2125
2181
  "x-message": "a step needs its words: a title, text, or both",
2126
2182
  "additionalProperties": false
2183
+ },
2184
+ "point": {
2185
+ "description": "A point of the scene, [x, y, z], or one carried by a part, {part, at}: it moves as the part does (taken apart, moved by a motion or a handle), so what is measured to it is measured as shown.",
2186
+ "anyOf": [
2187
+ {
2188
+ "$ref": "#/$defs/vec3"
2189
+ },
2190
+ {
2191
+ "type": "object",
2192
+ "required": ["part", "at"],
2193
+ "properties": {
2194
+ "part": {
2195
+ "type": "string",
2196
+ "minLength": 1,
2197
+ "description": "The part that carries it."
2198
+ },
2199
+ "at": {
2200
+ "$ref": "#/$defs/vec3",
2201
+ "description": "The point, in the part's own place at rest."
2202
+ }
2203
+ },
2204
+ "additionalProperties": false
2205
+ }
2206
+ ],
2207
+ "x-message": "a point is [x, y, z], or {part, at: [x, y, z]}"
2208
+ },
2209
+ "dimension": {
2210
+ "type": "object",
2211
+ "description": "A dimension drawn on the figure, as on a technical drawing: a length between two points (extension lines, a dimension line with arrows, its value), or an angle at a vertex (an arc with arrows, its value in degrees). Its value is worked out from the points as they are shown (in the scene's unit, when it has one), unless text says otherwise.",
2212
+ "properties": {
2213
+ "id": {
2214
+ "type": "string",
2215
+ "minLength": 1,
2216
+ "description": "Unique among the dimensions: what steps and the page name it by."
2217
+ },
2218
+ "name": {
2219
+ "type": "string",
2220
+ "minLength": 1,
2221
+ "description": "What is measured, for screen readers (\"the piston's stroke\")."
2222
+ },
2223
+ "from": {
2224
+ "$ref": "#/$defs/point",
2225
+ "description": "A length's first end."
2226
+ },
2227
+ "to": {
2228
+ "$ref": "#/$defs/point",
2229
+ "description": "A length's other end."
2230
+ },
2231
+ "offset": {
2232
+ "$ref": "#/$defs/vec3",
2233
+ "description": "Where the dimension line is drawn: the two ends moved by this much (default: on the points themselves), with extension lines out to it."
2234
+ },
2235
+ "angle": {
2236
+ "type": "array",
2237
+ "prefixItems": [
2238
+ {
2239
+ "$ref": "#/$defs/point"
2240
+ },
2241
+ {
2242
+ "$ref": "#/$defs/point"
2243
+ },
2244
+ {
2245
+ "$ref": "#/$defs/point"
2246
+ }
2247
+ ],
2248
+ "minItems": 3,
2249
+ "maxItems": 3,
2250
+ "description": "An angle: a point on one arm, the vertex, a point on the other arm."
2251
+ },
2252
+ "radius": {
2253
+ "type": "number",
2254
+ "exclusiveMinimum": 0,
2255
+ "description": "An angle's arc radius (default: a third of the shorter arm)."
2256
+ },
2257
+ "arms": {
2258
+ "type": "boolean",
2259
+ "description": "Draw an angle's arms from the vertex (false by default: they are usually edges of the model)."
2260
+ },
2261
+ "text": {
2262
+ "type": "string",
2263
+ "minLength": 1,
2264
+ "description": "The value shown, in place of the one worked out (\"Ø 40\", \"stroke\")."
2265
+ },
2266
+ "digits": {
2267
+ "type": "integer",
2268
+ "minimum": 0,
2269
+ "maximum": 6,
2270
+ "description": "Decimals in the value worked out (default: three significant figures)."
2271
+ },
2272
+ "hidden": {
2273
+ "type": "boolean",
2274
+ "description": "Not shown unless a step or the page names it."
2275
+ },
2276
+ "unit": {
2277
+ "enum": ["mm", "cm", "dm", "m", "km"],
2278
+ "description": "The unit a length's value is given in (the scene's unit by default); converted from the scene's unit, which it needs."
2279
+ }
2280
+ },
2281
+ "anyOf": [
2282
+ {
2283
+ "required": ["from", "to"]
2284
+ },
2285
+ {
2286
+ "required": ["angle"]
2287
+ }
2288
+ ],
2289
+ "x-message": "a dimension is a length (from, to) or an angle ([a, vertex, b])",
2290
+ "additionalProperties": false
2127
2291
  }
2128
2292
  },
2129
2293
  "additionalProperties": false
@@ -95,6 +95,9 @@ every number in scene units with one scale for the whole object, and say what is
95
95
  - **Living and grown forms** (cells, neurons, organs): pen or studio, perspective. Soft shapes get no edge lines.
96
96
  - `studio` when materials (metal, glass, tissue, a textured scan) are the point; `ink` for a drawn, warmer page.
97
97
  - A `theme` (`light`, `dark`, `blueprint`, `sepia`) only if the page around it asks for one.
98
+ - **Dimensions** where a size or an angle is the point (a stroke, a focal length, a slope): `dimensions` with the
99
+ points as the model has them (on a part, `{part, at}`, if it moves), an `offset` that keeps the line clear of the
100
+ model, and the scene's `unit` so the value is right. A few, never a dimension for every part.
98
101
  - The object fills about 55–65% of the stage, with air around it.
99
102
 
100
103
  ## 6. Steps, when the figure explains something in order
package/src/figure/api.js CHANGED
@@ -59,12 +59,17 @@ export function makeApi(c) {
59
59
  },
60
60
  // the cut's plane now: where the scene, a step, the page or the reader put it
61
61
  get section() {
62
- return { normal: c.state_.section.normal.slice(), offset: c.state_.section.offset };
62
+ const { normal, offset, second } = c.state_.section;
63
+ return {
64
+ normal: normal.slice(),
65
+ offset,
66
+ ...(second && { second: { normal: (second.normal || [1, 0, 0]).slice(), offset: second.offset || 0 } }),
67
+ };
63
68
  },
64
69
  _state: c.state_, // the engine's own state: not an API, it may change
65
70
  set(changes) {
66
71
  if ('explode' in changes) c.explodeNow_(changes.explode); // (the page's own value wins over a running tween)
67
- for (const key of ['spin', 'cut', 'labels', 'xray', 'style', 'theme', 'projection'])
72
+ for (const key of ['spin', 'cut', 'labels', 'xray', 'style', 'theme', 'projection', 'dimensions'])
68
73
  if (key in changes) c.state_[key] = changes[key];
69
74
  if ('transparent' in changes) c.state_.transparent = !!changes.transparent;
70
75
  if (changes.section) c.state_.section = { ...c.state_.section, ...changes.section };
@@ -1,7 +1,7 @@
1
1
  // @ts-check
2
2
  // The camera: where it stands and looks (fitted to the stage), the views and their glides, zooming towards a point,
3
3
  // the cut's plane, boxes round parts, and the ray through a point of the stage. Owns viewProj_, eye_, frustum_,
4
- // baseDist_, explodeShift_, camera_, project_, cutPlane_, cutRange_, boxOf_, frame_, shownBox_, rayAt_, zoom_, view_,
4
+ // baseDist_, explodeShift_, camera_, project_, cutPlane_, cutPlane2_, cutRange_, boxOf_, frame_, shownBox_, rayAt_, zoom_, view_,
5
5
  // viewAngles_ and cancelView_;
6
6
  // reads state_, spec_, built_, width_, height_ and animations_, and calls offsetOf_, pickHit_, measure_, markDirty_ and kick_.
7
7
  import { D2R, M, sub, add, mulv, len, norm, cross, dot } from '../math.js';
@@ -146,6 +146,14 @@ export function makeCamera(c) {
146
146
  }
147
147
  return [normal[0], normal[1], normal[2], -(swept < 1 ? from + (to - from) * swept : to)];
148
148
  };
149
+ // the second plane of a quarter cut (section.second): what lies beyond both planes is cut away; without one, a
150
+ // plane everything is beyond, so one plane cuts alone
151
+ c.cutPlane2_ = () => {
152
+ const second = c.state_.section.second;
153
+ if (!second) return [0, 0, 0, 1];
154
+ const normal = norm(second.normal || [1, 0, 0]);
155
+ return [normal[0], normal[1], normal[2], -(second.offset || 0)];
156
+ };
149
157
  // what is on screen now (for the shadow's reach) and the floor under the assembled model
150
158
  c.shownBox_ = () => c.boxOf_(c.built_.parts.filter((part) => part.drawAlpha >= 0.01));
151
159
  // the box round some parts where they are now (taken apart, moved)
@@ -162,8 +170,8 @@ export function makeCamera(c) {
162
170
  return { center: mulv(add(min, max), 0.5), radius: len(sub(max, min)) / 2 || 1, min, max };
163
171
  };
164
172
  // how far along the cut's normal the parts it cuts reach, as they are now: where the plane can go [from, to]
165
- c.cutRange_ = () => {
166
- const normal = norm(c.state_.section.normal);
173
+ c.cutRange_ = (second = false) => {
174
+ const normal = norm(second ? (c.state_.section.second || {}).normal || [1, 0, 0] : c.state_.section.normal);
167
175
  let from = Infinity,
168
176
  to = -Infinity;
169
177
  for (const part of c.built_.parts)
@@ -44,8 +44,15 @@ export interface State {
44
44
  explode: number;
45
45
  spin: number;
46
46
  cut: boolean | number;
47
- section: { normal: Vec3 | number[]; offset: number };
47
+ /** the cut's plane, and a quarter cut's second (section.second) */
48
+ section: {
49
+ normal: Vec3 | number[];
50
+ offset: number;
51
+ second?: { normal?: Vec3 | number[]; offset?: number } | null | false;
52
+ };
48
53
  labels: any;
54
+ /** the dimensions shown, by id; null: the scene's own */
55
+ dimensions: string[] | null;
49
56
  xray: boolean;
50
57
  yaw: number;
51
58
  pitch: number;
@@ -192,8 +199,10 @@ export interface Ctx {
192
199
  project_(q: number[]): number[] | null;
193
200
  /** the cut's plane [nx, ny, nz, -offset] */
194
201
  cutPlane_(): number[];
202
+ /** a quarter cut's second plane, or [0, 0, 0, 1] (everything beyond it: the first plane cuts alone) */
203
+ cutPlane2_(): number[];
195
204
  /** how far along the cut's normal the parts it cuts reach now: where the plane can go [from, to] */
196
- cutRange_(): number[];
205
+ cutRange_(second?: boolean): number[];
197
206
  /** the box round the parts drawn now */
198
207
  shownBox_(): { center: number[]; radius: number; min: number[]; max: number[] };
199
208
  boxOf_(ps: Part[]): { center: number[]; radius: number; min: number[]; max: number[] };
@@ -272,7 +281,7 @@ export interface Ctx {
272
281
  /** whether the reader may move the cut now (it is on, and the scene does not fix it) */
273
282
  cutMovable_(): boolean;
274
283
  /** the cut moved by the reader (held within the parts it cuts; onSection called) */
275
- moveCut_(offset: number): void;
284
+ moveCut_(offset: number, second?: boolean): void;
276
285
 
277
286
  // ---- additions.js
278
287
  addParticles_(o: any): any;
@@ -283,6 +292,10 @@ export interface Ctx {
283
292
  // ---- api.js
284
293
  api_: any;
285
294
 
295
+ // ---- dimensions.js
296
+ /** the dimensions placed on the stage, after each picture */
297
+ placeDimensions_(): void;
298
+
286
299
  // ---- steps.js
287
300
  /** the step shown (from 0); -1 for a scene without steps */
288
301
  stepAt_: number;
@@ -0,0 +1,140 @@
1
+ // @ts-check
2
+ // Dimensions on the figure, as on a technical drawing (the scene's `dimensions`): a length between two points, with
3
+ // extension lines out to a dimension line, arrows and its value; an angle at a vertex, an arc with arrows and its value
4
+ // in degrees. A point may ride on a part ({part, at}), so a length to a moving part changes as it moves; the value is
5
+ // worked out from the points as shown (in the scene's unit), unless the scene gives its text. Drawn after each picture
6
+ // over the canvas, an SVG path and an HTML value each, changed only where they moved; listed for screen readers with
7
+ // their values. Which show: the scene's own (not hidden), or the ones a step or the page names (state_.dimensions).
8
+ // Owns placeDimensions_; reads spec_, built_, state_, words_, host_, leaderSvg_ and tagLayer_, and calls part_ and project_.
9
+ import { M, sub, add, mulv, len, norm, dot } from '../math.js';
10
+
11
+ /** @param {import('./context').Ctx} c */
12
+ export function makeDimensions(c) {
13
+ const dimensions = (c.spec_.dimensions || []).filter((dimension) => dimension && typeof dimension === 'object');
14
+ c.placeDimensions_ = () => {};
15
+ if (!dimensions.length) return;
16
+ const forReaders = document.createElement('ul');
17
+ forReaders.className = 'lk3d-sr';
18
+ forReaders.setAttribute('aria-label', c.words_.dimensions);
19
+ c.host_.append(forReaders);
20
+ const drawn = dimensions.map((dimension) => {
21
+ const path = /** @type {SVGPathElement} */ (document.createElementNS('http://www.w3.org/2000/svg', 'path')),
22
+ value = document.createElement('div'),
23
+ item = document.createElement('li');
24
+ path.setAttribute('class', 'lk3d-dim');
25
+ value.className = 'lk3d-dimtext';
26
+ value.setAttribute('aria-hidden', 'true');
27
+ c.leaderSvg_.append(path);
28
+ c.tagLayer_.append(value);
29
+ forReaders.append(item);
30
+ return { dimension, path, value, item, d: '', at: '', text: '', shown: true };
31
+ });
32
+
33
+ // a point as shown: [x, y, z], or one a part carries, through the part's matrix; null where that part is out of sight
34
+ /** @param {any} point */
35
+ const placed = (point) => {
36
+ if (Array.isArray(point)) return point;
37
+ const part = point && c.part_(point.part);
38
+ return part && !part.hidden ? M.pt(part.matrix, point.at) : null;
39
+ };
40
+ // a value in words: three significant figures (or the scene's digits), the locale's decimal mark, the unit
41
+ /** @param {number} number @param {number | undefined} digits */
42
+ const METRES = { mm: 0.001, cm: 0.01, dm: 0.1, m: 1, km: 1000 };
43
+ const say = (number, digits) =>
44
+ (digits == null ? String(+number.toPrecision(3)) : number.toFixed(digits)).replace('.', c.words_.decimal);
45
+ // an arrowhead at the stage point tip, pointing along direction (from the inside of the line to the tip)
46
+ /** @param {number[]} tip @param {number[]} along */
47
+ const arrow = (tip, along) => {
48
+ const length = Math.hypot(along[0], along[1]) || 1,
49
+ [x, y] = [along[0] / length, along[1] / length],
50
+ side = (sign) => [tip[0] - 7 * (x * 0.91 - sign * y * 0.42), tip[1] - 7 * (y * 0.91 + sign * x * 0.42)];
51
+ const [a, b] = [side(1), side(-1)];
52
+ return `M${a[0].toFixed(1)} ${a[1].toFixed(1)}L${tip[0].toFixed(1)} ${tip[1].toFixed(1)}L${b[0].toFixed(1)} ${b[1].toFixed(1)}`;
53
+ };
54
+ /** @param {number[][]} points */
55
+ const line = (points) => 'M' + points.map((point) => `${point[0].toFixed(1)} ${point[1].toFixed(1)}`).join('L');
56
+
57
+ c.placeDimensions_ = () => {
58
+ const wanted = c.state_.dimensions;
59
+ for (const each of drawn) {
60
+ const dimension = each.dimension,
61
+ named = wanted ? dimension.id != null && wanted.includes(dimension.id) : !dimension.hidden;
62
+ let d = '',
63
+ /** @type {number[] | null} */
64
+ at = null,
65
+ text = '';
66
+ if (named && dimension.angle) {
67
+ const [a, vertex, b] = dimension.angle.map(placed);
68
+ if (a && vertex && b) {
69
+ const u = sub(a, vertex),
70
+ w = sub(b, vertex),
71
+ theta = Math.acos(Math.max(-1, Math.min(1, dot(norm(u), norm(w))))),
72
+ radius = dimension.radius ?? Math.min(len(u), len(w)) / 3,
73
+ // (points along the arc, from one arm to the other: a slerp, or a half turn round a side when straight)
74
+ arc = Array.from({ length: 17 }, (_, k) => {
75
+ const t = k / 16,
76
+ s = Math.sin(theta) || 1e-6;
77
+ return add(
78
+ vertex,
79
+ mulv(add(mulv(norm(u), Math.sin((1 - t) * theta) / s), mulv(norm(w), Math.sin(t * theta) / s)), radius),
80
+ );
81
+ }).map((point) => c.project_(point));
82
+ if (theta > 1e-3 && theta < Math.PI - 1e-3 && arc.every(Boolean)) {
83
+ const onStage = /** @type {number[][]} */ (arc);
84
+ d =
85
+ line(onStage) +
86
+ arrow(onStage[0], sub(onStage[0], onStage[1])) +
87
+ arrow(onStage[16], sub(onStage[16], onStage[15]));
88
+ const ends = [c.project_(vertex), c.project_(a), c.project_(b)];
89
+ if (dimension.arms && ends.every(Boolean)) {
90
+ const [v, p, q] = /** @type {number[][]} */ (ends);
91
+ d += line([p, v, q]);
92
+ }
93
+ const middle = add(vertex, mulv(norm(add(norm(u), norm(w))), radius * 1.35));
94
+ at = c.project_(middle);
95
+ text = dimension.text ?? say((theta * 180) / Math.PI, dimension.digits ?? (theta < 0.17 ? 1 : 0)) + '°';
96
+ }
97
+ }
98
+ } else if (named) {
99
+ const from = placed(dimension.from),
100
+ to = placed(dimension.to);
101
+ if (from && to) {
102
+ const offset = dimension.offset || [0, 0, 0],
103
+ [a, b, a2, b2] = [from, to, add(from, offset), add(to, offset)].map((point) => c.project_(point));
104
+ if (a && b && a2 && b2) {
105
+ if (len(offset) > 0)
106
+ for (const [object, onLine] of [
107
+ [a, a2],
108
+ [b, b2],
109
+ ]) {
110
+ // (an extension line: a small gap at the object, past the dimension line a little)
111
+ const along = sub(onLine, object),
112
+ pixels = Math.hypot(along[0], along[1]) || 1,
113
+ step = mulv(along, 1 / pixels);
114
+ d += line([add(object, mulv(step, 3)), add(onLine, mulv(step, 5))]);
115
+ }
116
+ d += line([a2, b2]) + arrow(a2, sub(a2, b2)) + arrow(b2, sub(b2, a2));
117
+ at = mulv(add(a2, b2), 0.5);
118
+ const unit = dimension.unit || c.spec_.unit,
119
+ // (in the scene's unit, or converted to the dimension's own)
120
+ factor = dimension.unit && c.spec_.unit ? METRES[c.spec_.unit] / METRES[dimension.unit] : 1;
121
+ text = dimension.text ?? say(len(sub(to, from)) * factor, dimension.digits) + (unit ? ' ' + unit : '');
122
+ }
123
+ }
124
+ }
125
+ const show = !!(d && at);
126
+ if (show !== each.shown) {
127
+ each.path.style.display = each.value.style.display = show ? '' : 'none';
128
+ each.shown = show;
129
+ }
130
+ if (!show || !at) continue;
131
+ if (d !== each.d) each.path.setAttribute('d', (each.d = d));
132
+ const place = `translate(${at[0].toFixed(1)}px, ${at[1].toFixed(1)}px) translate(-50%, -50%)`;
133
+ if (place !== each.at) each.value.style.transform = each.at = place;
134
+ if (text !== each.text) {
135
+ each.value.textContent = each.text = text;
136
+ each.item.textContent = (dimension.name ? dimension.name + ': ' : '') + text;
137
+ }
138
+ }
139
+ };
140
+ }
@@ -123,30 +123,42 @@ export function makeInput(c) {
123
123
  // cut face is seen nearly face on (the normal pointing at the eye has no direction on the stage)
124
124
  c.cutMovable_ = () =>
125
125
  !!c.state_.cut && c.spec_.section !== false && !c.spec_.section?.fixed && c.renderer_.kind !== '2d';
126
- c.moveCut_ = (offset) => {
127
- const [from, to] = c.cutRange_(),
126
+ // (second: the quarter cut's other plane, section.second, moved instead)
127
+ c.moveCut_ = (offset, second = false) => {
128
+ const section = c.state_.section,
129
+ [from, to] = c.cutRange_(second),
128
130
  held = Math.max(from, Math.min(to, offset));
129
- if (held === c.state_.section.offset) return;
130
- c.state_.section = { ...c.state_.section, offset: held };
131
+ if (held === (second ? (section.second || {}).offset : section.offset)) return;
132
+ c.state_.section = second
133
+ ? { ...section, second: { ...section.second, offset: held } }
134
+ : { ...section, offset: held };
131
135
  if (c.controls_.style) c.controls_.style(); // (the slider follows)
132
136
  c.kick_();
133
- if (c.opts_.onSection) {
134
- const [x, y, z] = c.state_.section.normal;
135
- c.opts_.onSection({ normal: [x, y, z], offset: held });
136
- }
137
+ if (c.opts_.onSection) c.opts_.onSection(c.api_.section);
137
138
  };
138
- /** @type {{ id: number, x: number, y: number, from: number, along: number[], moved: number, part: string } | null} */
139
+ /** @type {{ id: number, x: number, y: number, from: number, along: number[], moved: number, part: string, second: boolean } | null} */
139
140
  let cutting = null;
140
141
  /** @param {number} x @param {number} y */
141
142
  function cutAt(x, y) {
142
143
  if (!c.cutMovable_()) return null;
143
144
  const hit = c.pickHit_(x, y);
144
145
  if (!hit.onCut || !hit.id) return null;
145
- const plane = c.cutPlane_(),
146
- normal = plane.slice(0, 3),
147
- facing = dot(normal, hit.direction);
148
- if (Math.abs(facing) < 1e-9) return null;
149
- const point = add(hit.origin, mulv(hit.direction, -(dot(normal, hit.origin) + plane[3]) / facing)),
146
+ // (the face taken hold of: the first plane's, or in a quarter cut the second's, where the ray's crossing of the
147
+ // first lies outside the cut-away wedge)
148
+ const crossing = (/** @type {number[]} */ plane) => {
149
+ const facing = dot(plane.slice(0, 3), hit.direction);
150
+ return Math.abs(facing) < 1e-9
151
+ ? null
152
+ : add(hit.origin, mulv(hit.direction, -(dot(plane.slice(0, 3), hit.origin) + plane[3]) / facing));
153
+ };
154
+ const first = c.cutPlane_(),
155
+ other = c.cutPlane2_();
156
+ let plane = first,
157
+ point = crossing(first);
158
+ const second = !point || dot(other.slice(0, 3), point) + other[3] < 0;
159
+ if (second) point = crossing((plane = other));
160
+ if (!point) return null;
161
+ const normal = plane.slice(0, 3),
150
162
  across = norm(cross(normal, Math.abs(normal[1]) < 0.9 ? [0, 1, 0] : [1, 0, 0])),
151
163
  here = c.project_(point),
152
164
  deeper = c.project_(add(point, normal)),
@@ -155,7 +167,16 @@ export function makeInput(c) {
155
167
  const perUnit = Math.hypot(aside[0] - here[0], aside[1] - here[1]); // (pixels a scene unit, across the cut)
156
168
  let along = [deeper[0] - here[0], deeper[1] - here[1]];
157
169
  if (Math.hypot(along[0], along[1]) < 0.3 * perUnit) along = [0, -perUnit];
158
- return { x, y, from: c.state_.section.offset, along, moved: 0, part: hit.id };
170
+ const section = c.state_.section;
171
+ return {
172
+ x,
173
+ y,
174
+ from: second ? (section.second || {}).offset || 0 : section.offset,
175
+ along,
176
+ moved: 0,
177
+ part: hit.id,
178
+ second,
179
+ };
159
180
  }
160
181
  const capture = (pointerId) => {
161
182
  try {
@@ -218,7 +239,10 @@ export function makeInput(c) {
218
239
  dy = y - cutting.y,
219
240
  along = cutting.along;
220
241
  cutting.moved = Math.max(cutting.moved, Math.abs(dx) + Math.abs(dy));
221
- c.moveCut_(cutting.from + (dx * along[0] + dy * along[1]) / (along[0] * along[0] + along[1] * along[1]));
242
+ c.moveCut_(
243
+ cutting.from + (dx * along[0] + dy * along[1]) / (along[0] * along[0] + along[1] * along[1]),
244
+ cutting.second,
245
+ );
222
246
  return;
223
247
  }
224
248
  if (grab && event.pointerId === grab.id) {
@@ -1,7 +1,7 @@
1
1
  // @ts-check
2
2
  // Labels and their leader lines (HTML and SVG over the canvas, measured once, moved with transforms), and the scale
3
3
  // bar. Owns labelWidths_ and placeLabels_ (the frame loop calls it after each picture); reads built_, state_, spec_,
4
- // tagLayer_, leaderSvg_, scaleEl_, lookKey_, width_ and height_, and calls project_, cutPlane_, explodeShift_, baseMatrix_ and
4
+ // tagLayer_, leaderSvg_, scaleEl_, lookKey_, width_ and height_, and calls project_, cutPlane_, cutPlane2_, explodeShift_, baseMatrix_ and
5
5
  // kick_.
6
6
  import { M, sub, add, mulv, len } from '../math.js';
7
7
  import textWidth from '../text-width.js';
@@ -125,16 +125,31 @@ export function makeLabels(c) {
125
125
  (which === 'auto' && c.state_.explode > 0.45));
126
126
  const middle = c.project_(add(c.built_.center, c.explodeShift_())) || [width / 2, height / 2];
127
127
  const items = [],
128
- plane = c.state_.cut ? c.cutPlane_() : null;
128
+ plane = c.state_.cut ? c.cutPlane_() : null,
129
+ second = c.cutPlane2_();
129
130
  for (const tag of tags) {
130
- // the part's anchor on the stage, if its label is wanted (a spinning part's label stays put), and not cut away
131
- // (a label pointing at nothing once the cut has passed its place)
132
- const anchor = M.pt(
133
- tag.part.spin || tag.part.handleOf?.round ? c.baseMatrix_(tag.part) : tag.part.matrix,
134
- tag.part.anchor,
135
- ),
136
- cutAway =
137
- plane && tag.part.cut && plane[0] * anchor[0] + plane[1] * anchor[1] + plane[2] * anchor[2] + plane[3] > 0;
131
+ // the part's anchor on the stage, if its label is wanted (a spinning part's label stays put). Where the cut has
132
+ // taken the anchor's place (beyond its plane, and beyond the second too in a quarter cut), the label points at the
133
+ // part's cut face instead (the anchor moved onto the nearer plane), and shows not at all once the cut has taken
134
+ // the whole part (it would point at nothing)
135
+ let anchor = M.pt(
136
+ tag.part.spin || tag.part.handleOf?.round ? c.baseMatrix_(tag.part) : tag.part.matrix,
137
+ tag.part.anchor,
138
+ );
139
+ const cut = /** @type {number[]} */ (plane),
140
+ along = (p, point) => p[0] * point[0] + p[1] * point[1] + p[2] * point[2] + p[3],
141
+ beyond = (point) => Math.min(along(cut, point), along(second, point)), // (> 0: cut away)
142
+ past = plane && tag.part.cut ? beyond(anchor) : 0;
143
+ let cutAway = false;
144
+ if (past > 0) {
145
+ const offset = c.offsetOf_(tag.part),
146
+ { min, max } = tag.part,
147
+ nearer = along(cut, anchor) <= along(second, anchor) ? cut : second;
148
+ cutAway = [0, 1, 2, 3, 4, 5, 6, 7].every(
149
+ (corner) => beyond([0, 1, 2].map((axis) => offset[axis] + ((corner >> axis) & 1 ? max : min)[axis])) > 0,
150
+ );
151
+ anchor = anchor.map((value, axis) => value - nearer[axis] * past);
152
+ }
138
153
  const at = which !== 'none' && wanted(tag.part) && !cutAway && c.project_(anchor);
139
154
  if (!at) {
140
155
  show(tag, false);
@@ -263,6 +263,7 @@ export function makeLoop(c) {
263
263
  edges: c.showEdges_(),
264
264
  cut: !!c.state_.cut,
265
265
  plane: c.cutPlane_(),
266
+ plane2: c.cutPlane2_(),
266
267
  hatch: c.built_.radius / 80, // (a cap's hatching: lines this far apart on the cut, a few px when the model fills the view)
267
268
  hover: c.state_.hover,
268
269
  selected: c.state_.selected,
@@ -295,6 +296,7 @@ export function makeLoop(c) {
295
296
  else {
296
297
  if (c.keptPicture_) c.keptPicture_.remove(); // (drawn again: the picture kept while the context was given up goes)
297
298
  c.placeLabels_();
299
+ c.placeDimensions_();
298
300
  dirty = false;
299
301
  if (c.stats_.firstFrameMs == null) {
300
302
  c.stats_.firstFrameMs = performance.now() - c.mountedAt_;
@@ -2,7 +2,7 @@
2
2
  // Picking: a ray from the eye through a point of the stage, against each part's bounding-volume trees (bvh.js, each
3
3
  // built when first needed, or in idle time: idle.js); the nearest hit on a part that is drawn and not cut away, and
4
4
  // whether what is seen there is the part's cut face. Owns bvhOf_, pickHit_ and pickAt_; reads built_ and state_, and
5
- // calls rayAt_ and cutPlane_.
5
+ // calls rayAt_, cutPlane_ and cutPlane2_.
6
6
  import { M, add, sub, mulv, cross, dot } from '../math.js';
7
7
  import { buildBVH, intersect } from '../bvh.js';
8
8
 
@@ -58,7 +58,8 @@ export function makePick(c) {
58
58
  c.pickHit_ = (x, y, minAlpha = 0.3) => {
59
59
  // parts fainter than minAlpha let the ray through; the part hit, and where along the ray (A + D t)
60
60
  const { origin, direction } = c.rayAt_(x, y),
61
- plane = c.cutPlane_();
61
+ plane = c.cutPlane_(),
62
+ second = c.cutPlane2_();
62
63
  let best = Infinity,
63
64
  /** @type {string | null} */
64
65
  id = null,
@@ -75,8 +76,9 @@ export function makePick(c) {
75
76
  const accept =
76
77
  c.state_.cut && part.cut
77
78
  ? (t) => {
78
- const point = add(origin, mulv(direction, t));
79
- return plane[0] * point[0] + plane[1] * point[1] + plane[2] * point[2] + plane[3] <= 0;
79
+ const point = add(origin, mulv(direction, t)),
80
+ beyond = (p) => p[0] * point[0] + p[1] * point[1] + p[2] * point[2] + p[3] > 0;
81
+ return !(beyond(plane) && beyond(second)); // (cut away only beyond both, in a quarter cut)
80
82
  }
81
83
  : null;
82
84
  const hit = intersect(batch.mesh, c.bvhOf_(batch), shapeOrigin, shapeDirection, best, false, accept);
@@ -99,6 +99,7 @@ export function makeSteps(c, buttonClass) {
99
99
  c.syncHandleInputs_();
100
100
  c.state_.xray = !!step.xray;
101
101
  c.state_.labels = step.labels ?? c.opts_.labels ?? 'auto';
102
+ c.state_.dimensions = step.dimensions ?? null;
102
103
  c.state_.section = { normal: [0, 0, 1], offset: 0, ...c.spec_.section, ...step.section };
103
104
  const part = step.select != null && c.part_(step.select);
104
105
  c.state_.selected = part && !part.hidden ? part.id : null;