@margoengine/loaders 0.1.0 → 1.0.0
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.
- package/LICENSE +201 -201
- package/NOTICE +4 -4
- package/README.md +1666 -1198
- package/dist/crease.d.ts +7 -0
- package/dist/crease.js +67 -0
- package/dist/dae-common.js +1 -1
- package/dist/dae-kinematics.d.ts +65 -0
- package/dist/dae-kinematics.js +461 -0
- package/dist/dae.d.ts +9 -4
- package/dist/dae.js +16 -5
- package/dist/dxf-hatch.d.ts +8 -0
- package/dist/dxf-hatch.js +346 -0
- package/dist/dxf-leaders.d.ts +59 -0
- package/dist/dxf-leaders.js +258 -0
- package/dist/dxf.d.ts +42 -1
- package/dist/dxf.js +226 -15
- package/dist/engine.d.ts +5 -0
- package/dist/engine.js +3 -0
- package/dist/gcode.d.ts +37 -0
- package/dist/gcode.js +91 -0
- package/dist/geometry.d.ts +1 -1
- package/dist/index.d.ts +1 -1
- package/dist/mtl.d.ts +8 -3
- package/dist/mtl.js +91 -10
- package/dist/nrrd.d.ts +12 -5
- package/dist/nrrd.js +68 -26
- package/dist/obj.d.ts +3 -1
- package/dist/obj.js +7 -1
- package/dist/render.d.ts +9 -4
- package/dist/render.js +31 -3
- package/dist/shared/asset-url.js +82 -82
- package/dist/shared/crc32.d.ts +2 -2
- package/dist/shared/crc32.js +47 -47
- package/dist/shared/errors.js +36 -36
- package/dist/shared/inflate.js +284 -284
- package/dist/shared/normals.js +26 -26
- package/dist/shared/scene-scope.js +54 -54
- package/dist/shared/trailing-data.js +45 -42
- package/dist/threemf-beam-reader.d.ts +19 -0
- package/dist/threemf-beam-reader.js +165 -0
- package/dist/threemf-beams.d.ts +52 -0
- package/dist/threemf-beams.js +268 -0
- package/dist/threemf-boolean-read.d.ts +10 -0
- package/dist/threemf-boolean-read.js +113 -0
- package/dist/threemf-boolean.d.ts +59 -0
- package/dist/threemf-boolean.js +20 -0
- package/dist/threemf-csg.d.ts +14 -0
- package/dist/threemf-csg.js +193 -0
- package/dist/threemf-displacement-read.d.ts +8 -0
- package/dist/threemf-displacement-read.js +213 -0
- package/dist/threemf-displacement.d.ts +71 -0
- package/dist/threemf-displacement.js +18 -0
- package/dist/threemf-evaluate.d.ts +36 -0
- package/dist/threemf-evaluate.js +86 -0
- package/dist/threemf-functions.d.ts +33 -0
- package/dist/threemf-functions.js +359 -0
- package/dist/threemf-lattice-mesh.d.ts +9 -0
- package/dist/threemf-lattice-mesh.js +115 -0
- package/dist/threemf-lattice.js +10 -145
- package/dist/threemf-levelset.d.ts +23 -0
- package/dist/threemf-levelset.js +105 -0
- package/dist/threemf-materials.d.ts +89 -2
- package/dist/threemf-materials.js +194 -11
- package/dist/threemf-mesh.d.ts +21 -6
- package/dist/threemf-mesh.js +266 -45
- package/dist/threemf-secure.d.ts +50 -0
- package/dist/threemf-secure.js +153 -0
- package/dist/threemf-tessellate.d.ts +37 -0
- package/dist/threemf-tessellate.js +277 -0
- package/dist/threemf-volume-colour.d.ts +25 -0
- package/dist/threemf-volume-colour.js +233 -0
- package/dist/threemf-volume-sample.d.ts +8 -0
- package/dist/threemf-volume-sample.js +65 -0
- package/dist/threemf-volumetric-read.d.ts +15 -0
- package/dist/threemf-volumetric-read.js +539 -0
- package/dist/threemf-volumetric.d.ts +177 -0
- package/dist/threemf-volumetric.js +28 -0
- package/dist/threemf.d.ts +73 -4
- package/dist/threemf.js +235 -41
- package/dist/types.d.ts +18 -0
- package/dist/workers/usd.js +1 -1
- package/package.json +291 -246
package/dist/dae.js
CHANGED
|
@@ -368,17 +368,28 @@ function findNode(roots, match) {
|
|
|
368
368
|
* node transform elements and morph weights) and animation clips are read too. `up_axis` and `unit` are converted to
|
|
369
369
|
* Y-up meters, alike for geometry, node, bind shape and inverse bind matrices. `lookat` and `skew` join the transform
|
|
370
370
|
* stack; `<instance_camera>` and `<instance_light>` give `cameras` and `lights` (technique_common optics and lights)
|
|
371
|
-
* with their frames.
|
|
372
|
-
* imported; each is reported in `warnings`. Image paths are
|
|
373
|
-
* relative to the .dae folder, for the caller's resolver.
|
|
371
|
+
* with their frames. `<lines>` and `<linestrips>` come back as each geometry's `lines`. Polygon holes, and cameras,
|
|
372
|
+
* lights, channels or controllers that do not fit, are not imported; each is reported in `warnings`. Image paths are
|
|
373
|
+
* relative to the .dae folder, for the caller's resolver. Joints are read by `parseDAEKinematics` of
|
|
374
|
+
* `@margoengine/loaders/dae-kinematics`.
|
|
374
375
|
*/
|
|
375
376
|
export function parseDAE(input, options = {}) {
|
|
376
|
-
|
|
377
|
+
return readDAE(daeRoot(input, options), options);
|
|
378
|
+
}
|
|
379
|
+
/** The document's `<COLLADA>` element. */
|
|
380
|
+
export function daeRoot(input, options) {
|
|
381
|
+
// The limits are checked before the XML is read.
|
|
382
|
+
limits(options, F);
|
|
383
|
+
const root = readXMLTree(typeof input === 'string' ? input : decodeXML(toBytes(input, F)), F, {
|
|
377
384
|
external: options.external,
|
|
378
385
|
}).children[0];
|
|
379
386
|
if (root?.name !== 'COLLADA')
|
|
380
387
|
throw new LoaderError(F, 'header', `root element is <${root?.name ?? ''}>, not <COLLADA>`);
|
|
381
|
-
|
|
388
|
+
return root;
|
|
389
|
+
}
|
|
390
|
+
/** {@link parseDAE} of a document already read, for entries that also read other libraries of it. */
|
|
391
|
+
export function readDAE(root, options) {
|
|
392
|
+
const limit = limits(options, F), warnings = new Set(), warn = (message) => void warnings.add(message);
|
|
382
393
|
const ids = new Map(), images = new Map();
|
|
383
394
|
const index = (e) => {
|
|
384
395
|
if (e.attributes.id)
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import type { DXFHatchReader } from './dxf.js';
|
|
2
|
+
/**
|
|
3
|
+
* Reads one HATCH entity for {@link parseDXF}: pass it as `{ hatch: readDXFHatch }`. A solid hatch becomes triangles
|
|
4
|
+
* (a gradient hatch takes its first gradient colour), a pattern hatch its dashed line family, a dot being a segment of
|
|
5
|
+
* no length (round caps draw it). Returns `undefined` for a hatch without a readable boundary or pattern, which
|
|
6
|
+
* parseDXF counts in `skipped`.
|
|
7
|
+
*/
|
|
8
|
+
export declare const readDXFHatch: DXFHatchReader;
|
|
@@ -0,0 +1,346 @@
|
|
|
1
|
+
// DXF HATCH entities for parseDXF, written from Autodesk's published DXF reference (HATCH group codes: boundary
|
|
2
|
+
// paths, pattern definition lines, hatch style, gradient colours): solid hatches become triangles, pattern hatches
|
|
3
|
+
// become dashed line segments. Optional: `parseDXF(bytes, { hatch: readDXFHatch })`.
|
|
4
|
+
//
|
|
5
|
+
// Boundary angles: an edge flagged clockwise (code 73 = 0) stores its angles mirrored, so it runs from -start to -end
|
|
6
|
+
// (Autodesk's table names the codes only). Elliptic edge angles are angles from the major axis,
|
|
7
|
+
// not curve parameters. Pattern lines are stored with the hatch's angle and scale already applied.
|
|
8
|
+
//
|
|
9
|
+
// Fill: rings are nested by containment, and hatch style 0 fills by the even-odd rule, 1 the outermost ring down to
|
|
10
|
+
// its islands, 2 the outermost ring whole. Pattern lines are clipped by parity (J. Foley, A. van Dam, S. Feiner and
|
|
11
|
+
// J. Hughes, "Computer Graphics: Principles and Practice", 2nd ed., 1990, section 3.6, scan conversion of polygons),
|
|
12
|
+
// with the half-open vertex rule, so a line through a vertex is counted once.
|
|
13
|
+
import { triangulateShape } from '@margoengine/geometry/triangulate';
|
|
14
|
+
import { LoaderError } from './types.js';
|
|
15
|
+
const F = 'DXF';
|
|
16
|
+
/** Most boundary rings of one hatch: their nesting is found pair by pair. */
|
|
17
|
+
const MAX_RINGS = 10_000;
|
|
18
|
+
const over = (what, limit) => new LoaderError(F, 'limit', `HATCH ${what} exceeds the limit of ${limit}`);
|
|
19
|
+
/** The sweep in degrees from `start` to `end`, counterclockwise, in (0, 360]. */
|
|
20
|
+
function turn(start, end) {
|
|
21
|
+
const d = end - start;
|
|
22
|
+
if (Math.abs(d) >= 360 - 1e-6)
|
|
23
|
+
return 360;
|
|
24
|
+
const s = ((d % 360) + 360) % 360;
|
|
25
|
+
return s || 360;
|
|
26
|
+
}
|
|
27
|
+
function ring(points) {
|
|
28
|
+
const out = [];
|
|
29
|
+
let reach = 0;
|
|
30
|
+
for (const p of points)
|
|
31
|
+
reach = Math.max(reach, Math.abs(p[0]), Math.abs(p[1]));
|
|
32
|
+
const eps = reach * 1e-12;
|
|
33
|
+
const same = (a, b) => Math.abs(a[0] - b[0]) <= eps && Math.abs(a[1] - b[1]) <= eps;
|
|
34
|
+
for (const p of points)
|
|
35
|
+
if (!out.length || !same(out[out.length - 1], p))
|
|
36
|
+
out.push(p);
|
|
37
|
+
while (out.length > 1 && same(out[0], out[out.length - 1]))
|
|
38
|
+
out.pop();
|
|
39
|
+
if (out.length < 3)
|
|
40
|
+
return undefined;
|
|
41
|
+
const min = [...out[0]], max = [...out[0]];
|
|
42
|
+
let area = 0;
|
|
43
|
+
for (let i = 0; i < out.length; i++) {
|
|
44
|
+
const a = out[i], b = out[(i + 1) % out.length];
|
|
45
|
+
// About the first point, so rings far from the origin keep their precision.
|
|
46
|
+
area += (a[0] - out[0][0]) * (b[1] - out[0][1]) - (b[0] - out[0][0]) * (a[1] - out[0][1]);
|
|
47
|
+
for (const k of [0, 1]) {
|
|
48
|
+
min[k] = Math.min(min[k], a[k]);
|
|
49
|
+
max[k] = Math.max(max[k], a[k]);
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
return { points: out, area: Math.abs(area) / 2, min, max, parent: -1, depth: 0 };
|
|
53
|
+
}
|
|
54
|
+
/** Whether `p` is inside the ring (even-odd), or `undefined` when it lies on the ring. */
|
|
55
|
+
function inside(p, r) {
|
|
56
|
+
const eps = Math.max(r.max[0] - r.min[0], r.max[1] - r.min[1]) * 1e-9;
|
|
57
|
+
let odd = false;
|
|
58
|
+
for (let i = 0; i < r.points.length; i++) {
|
|
59
|
+
const a = r.points[i], b = r.points[(i + 1) % r.points.length], dx = b[0] - a[0], dy = b[1] - a[1], l = dx * dx + dy * dy, t = l ? Math.max(0, Math.min(1, ((p[0] - a[0]) * dx + (p[1] - a[1]) * dy) / l)) : 0;
|
|
60
|
+
if (Math.hypot(p[0] - a[0] - t * dx, p[1] - a[1] - t * dy) <= eps)
|
|
61
|
+
return undefined;
|
|
62
|
+
if (a[1] > p[1] !== b[1] > p[1] && p[0] < a[0] + (dx * (p[1] - a[1])) / dy)
|
|
63
|
+
odd = !odd;
|
|
64
|
+
}
|
|
65
|
+
return odd;
|
|
66
|
+
}
|
|
67
|
+
/** Whether ring `b` lies in ring `a`: judged at the first of b's edge midpoints that is not on a. */
|
|
68
|
+
function contains(a, b) {
|
|
69
|
+
if (b.min[0] < a.min[0] || b.min[1] < a.min[1] || b.max[0] > a.max[0] || b.max[1] > a.max[1])
|
|
70
|
+
return false;
|
|
71
|
+
for (let i = 0; i < b.points.length; i++) {
|
|
72
|
+
const p = b.points[i], q = b.points[(i + 1) % b.points.length], at = inside([(p[0] + q[0]) / 2, (p[1] + q[1]) / 2], a);
|
|
73
|
+
if (at !== undefined)
|
|
74
|
+
return at;
|
|
75
|
+
}
|
|
76
|
+
return true;
|
|
77
|
+
}
|
|
78
|
+
/** Rings from the largest, each knowing the smallest ring around it. */
|
|
79
|
+
function nest(rings) {
|
|
80
|
+
const sorted = [...rings].sort((a, b) => b.area - a.area);
|
|
81
|
+
for (let i = 1; i < sorted.length; i++)
|
|
82
|
+
for (let j = i - 1; j >= 0; j--)
|
|
83
|
+
if (contains(sorted[j], sorted[i])) {
|
|
84
|
+
sorted[i].parent = j;
|
|
85
|
+
sorted[i].depth = sorted[j].depth + 1;
|
|
86
|
+
break;
|
|
87
|
+
}
|
|
88
|
+
return sorted;
|
|
89
|
+
}
|
|
90
|
+
/**
|
|
91
|
+
* Reads one HATCH entity for {@link parseDXF}: pass it as `{ hatch: readDXFHatch }`. A solid hatch becomes triangles
|
|
92
|
+
* (a gradient hatch takes its first gradient colour), a pattern hatch its dashed line family, a dot being a segment of
|
|
93
|
+
* no length (round caps draw it). Returns `undefined` for a hatch without a readable boundary or pattern, which
|
|
94
|
+
* parseDXF counts in `skipped`.
|
|
95
|
+
*/
|
|
96
|
+
export const readDXFHatch = (input) => {
|
|
97
|
+
const { codes, values } = input;
|
|
98
|
+
let at = codes.indexOf(91), bad = at < 0;
|
|
99
|
+
if (bad)
|
|
100
|
+
return undefined;
|
|
101
|
+
const value = (i) => {
|
|
102
|
+
const n = Number(values[i]);
|
|
103
|
+
return Number.isFinite(n) ? n : 0;
|
|
104
|
+
};
|
|
105
|
+
/** The value of the next pair, which must have this code. */
|
|
106
|
+
const need = (code) => {
|
|
107
|
+
if (codes[at] !== code) {
|
|
108
|
+
bad = true;
|
|
109
|
+
return 0;
|
|
110
|
+
}
|
|
111
|
+
return value(at++);
|
|
112
|
+
};
|
|
113
|
+
const optional = (code, fallback = 0) => (codes[at] === code ? value(at++) : fallback);
|
|
114
|
+
const pair = (x) => [need(x), need(x + 10)];
|
|
115
|
+
const head = (code) => {
|
|
116
|
+
const i = codes.indexOf(code);
|
|
117
|
+
return i < 0 || i > codes.indexOf(91) ? 0 : value(i);
|
|
118
|
+
};
|
|
119
|
+
const elevation = head(30), solid = head(70) === 1, paths = need(91);
|
|
120
|
+
if (paths > MAX_RINGS)
|
|
121
|
+
throw over('boundary path count', MAX_RINGS);
|
|
122
|
+
// --- boundary paths ---------------------------------------------------------------------------------------------
|
|
123
|
+
const rings = [];
|
|
124
|
+
let total = 0;
|
|
125
|
+
const room = (points) => {
|
|
126
|
+
total += points.length;
|
|
127
|
+
if (total > input.maxVertices)
|
|
128
|
+
throw over('boundary point count', input.maxVertices);
|
|
129
|
+
};
|
|
130
|
+
const flat = (points) => points.map((p) => [p[0], p[1]]);
|
|
131
|
+
/** One edge of an edge path as points. */
|
|
132
|
+
const edge = () => {
|
|
133
|
+
const type = need(72);
|
|
134
|
+
if (type === 1)
|
|
135
|
+
return [pair(10), pair(11)];
|
|
136
|
+
if (type === 2) {
|
|
137
|
+
const c = pair(10), r = need(40), a0 = need(50), a1 = need(51), ccw = need(73) !== 0, sign = ccw ? 1 : -1, rad = Math.PI / 180;
|
|
138
|
+
return flat(input.arc(c[0], c[1], 0, r, sign * a0 * rad, sign * turn(a0, a1) * rad));
|
|
139
|
+
}
|
|
140
|
+
if (type === 3) {
|
|
141
|
+
const c = pair(10), major = pair(11), ratio = need(40), a0 = need(50), a1 = need(51), sign = need(73) !== 0 ? 1 : -1, sweep = turn(a0, a1);
|
|
142
|
+
if (!(ratio > 0)) {
|
|
143
|
+
bad = true;
|
|
144
|
+
return [];
|
|
145
|
+
}
|
|
146
|
+
const parameter = (degrees) => {
|
|
147
|
+
const a = (degrees * Math.PI) / 180;
|
|
148
|
+
return Math.atan2(Math.sin(a) / ratio, Math.cos(a));
|
|
149
|
+
};
|
|
150
|
+
const t0 = parameter(a0);
|
|
151
|
+
let span = parameter(a0 + sweep) - t0;
|
|
152
|
+
if (sweep === 360)
|
|
153
|
+
span = 2 * Math.PI;
|
|
154
|
+
else
|
|
155
|
+
while (span <= 0)
|
|
156
|
+
span += 2 * Math.PI;
|
|
157
|
+
const count = Math.max(1, Math.ceil((input.arcSegments * span) / (2 * Math.PI))), out = [];
|
|
158
|
+
for (let k = 0; k <= count; k++) {
|
|
159
|
+
const t = sign * (t0 + (span * k) / count), u = Math.cos(t), v = Math.sin(t) * ratio;
|
|
160
|
+
out.push([c[0] + major[0] * u - major[1] * v, c[1] + major[1] * u + major[0] * v]);
|
|
161
|
+
}
|
|
162
|
+
return out;
|
|
163
|
+
}
|
|
164
|
+
if (type === 4) {
|
|
165
|
+
const degree = need(94);
|
|
166
|
+
need(73);
|
|
167
|
+
need(74);
|
|
168
|
+
const knotCount = need(95), controlCount = need(96), knots = [], control = [], weights = [], fit = [];
|
|
169
|
+
for (let k = 0; k < knotCount && !bad; k++)
|
|
170
|
+
knots.push(need(40));
|
|
171
|
+
for (let k = 0; k < controlCount && !bad; k++) {
|
|
172
|
+
const p = pair(10);
|
|
173
|
+
control.push([p[0], p[1], 0]);
|
|
174
|
+
if (codes[at] === 42)
|
|
175
|
+
weights[k] = value(at++);
|
|
176
|
+
}
|
|
177
|
+
// Fit data (R2010 on) starts with code 97, as does the count of source objects after a path's last edge: only
|
|
178
|
+
// the former is followed by a fit point, another 97 or the next edge.
|
|
179
|
+
if (codes[at] === 97 && [11, 97, 72].includes(codes[at + 1])) {
|
|
180
|
+
const count = need(97);
|
|
181
|
+
for (let k = 0; k < count && !bad; k++)
|
|
182
|
+
fit.push(pair(11));
|
|
183
|
+
for (const tangent of [12, 13])
|
|
184
|
+
if (codes[at] === tangent)
|
|
185
|
+
pair(tangent);
|
|
186
|
+
}
|
|
187
|
+
return control.length ? flat(input.spline(degree, knots, control, weights)) : fit;
|
|
188
|
+
}
|
|
189
|
+
bad = true;
|
|
190
|
+
return [];
|
|
191
|
+
};
|
|
192
|
+
for (let p = 0; p < paths && !bad; p++) {
|
|
193
|
+
const flags = need(92);
|
|
194
|
+
let points = [];
|
|
195
|
+
if (flags & 2) {
|
|
196
|
+
const bulges = need(72);
|
|
197
|
+
need(73);
|
|
198
|
+
const count = need(93), vertices = [];
|
|
199
|
+
if (count > input.maxVertices)
|
|
200
|
+
throw over('boundary point count', input.maxVertices);
|
|
201
|
+
for (let k = 0; k < count && !bad; k++) {
|
|
202
|
+
const [x, y] = pair(10);
|
|
203
|
+
vertices.push({ x, y, bulge: bulges ? optional(42) : 0 });
|
|
204
|
+
}
|
|
205
|
+
// A boundary is closed whatever its flag says: a fill has no open side.
|
|
206
|
+
points = flat(input.bulged(vertices, true, 0));
|
|
207
|
+
room(points);
|
|
208
|
+
}
|
|
209
|
+
else {
|
|
210
|
+
const count = need(93);
|
|
211
|
+
for (let k = 0; k < count && !bad; k++) {
|
|
212
|
+
const next = edge();
|
|
213
|
+
room(next);
|
|
214
|
+
for (const q of next)
|
|
215
|
+
points.push(q);
|
|
216
|
+
}
|
|
217
|
+
}
|
|
218
|
+
const sources = optional(97);
|
|
219
|
+
for (let k = 0; k < sources && codes[at] === 330; k++)
|
|
220
|
+
at++;
|
|
221
|
+
const r = ring(points);
|
|
222
|
+
if (r)
|
|
223
|
+
rings.push(r);
|
|
224
|
+
}
|
|
225
|
+
if (bad || !rings.length)
|
|
226
|
+
return undefined;
|
|
227
|
+
// --- style, pattern lines and gradient --------------------------------------------------------------------------
|
|
228
|
+
let style = 0, color, gradient = false;
|
|
229
|
+
const pattern = [];
|
|
230
|
+
while (at < codes.length && !bad) {
|
|
231
|
+
const code = codes[at], v = value(at++);
|
|
232
|
+
if (code === 75)
|
|
233
|
+
style = v;
|
|
234
|
+
else if (code === 450)
|
|
235
|
+
gradient = v === 1;
|
|
236
|
+
else if (code === 421 && gradient && !color)
|
|
237
|
+
color = [((v >> 16) & 255) / 255, ((v >> 8) & 255) / 255, (v & 255) / 255];
|
|
238
|
+
else if (code === 78)
|
|
239
|
+
for (let k = 0; k < v && !bad; k++) {
|
|
240
|
+
const angle = (need(53) * Math.PI) / 180, base = [need(43), need(44)], offset = [need(45), need(46)], count = need(79), dashes = [];
|
|
241
|
+
for (let d = 0; d < count && !bad; d++)
|
|
242
|
+
dashes.push(need(49));
|
|
243
|
+
pattern.push({ angle, base, offset, dashes });
|
|
244
|
+
}
|
|
245
|
+
}
|
|
246
|
+
if (bad || (!solid && !pattern.length))
|
|
247
|
+
return undefined;
|
|
248
|
+
const all = nest(rings), kept = all.map((r) => (style === 2 ? r.depth === 0 : style === 1 ? r.depth <= 1 : true));
|
|
249
|
+
const triangles = [], segments = [];
|
|
250
|
+
if (solid) {
|
|
251
|
+
const holes = all.map(() => []);
|
|
252
|
+
all.forEach((r, i) => {
|
|
253
|
+
if (kept[i] && r.depth % 2 === 1)
|
|
254
|
+
holes[r.parent].push(r.points);
|
|
255
|
+
});
|
|
256
|
+
all.forEach((r, i) => {
|
|
257
|
+
if (!kept[i] || r.depth % 2)
|
|
258
|
+
return;
|
|
259
|
+
const fill = triangulateShape(r.points, holes[i]), corners = fill.contour.concat(...fill.holes);
|
|
260
|
+
if ((triangles.length / 2 + fill.indices.length) / 3 > input.maxTriangles)
|
|
261
|
+
throw over('triangle count', input.maxTriangles);
|
|
262
|
+
for (const index of fill.indices)
|
|
263
|
+
triangles.push(corners[index][0], corners[index][1]);
|
|
264
|
+
});
|
|
265
|
+
return { triangles, segments, elevation, color };
|
|
266
|
+
}
|
|
267
|
+
// --- pattern ----------------------------------------------------------------------------------------------------
|
|
268
|
+
const used = all.filter((_, i) => kept[i]);
|
|
269
|
+
let crossings = 0;
|
|
270
|
+
const most = input.maxSegments;
|
|
271
|
+
for (const line of pattern) {
|
|
272
|
+
const c = Math.cos(line.angle), s = Math.sin(line.angle), [bx, by] = line.base;
|
|
273
|
+
// Line k runs along (c, s) at k * gap across it, its dashes starting k * shift along it.
|
|
274
|
+
let gap = -s * line.offset[0] + c * line.offset[1], shift = c * line.offset[0] + s * line.offset[1];
|
|
275
|
+
if (gap < 0) {
|
|
276
|
+
gap = -gap;
|
|
277
|
+
shift = -shift;
|
|
278
|
+
}
|
|
279
|
+
let period = 0;
|
|
280
|
+
for (const d of line.dashes)
|
|
281
|
+
period += Math.abs(d);
|
|
282
|
+
// A family with no spacing, or with nothing but gaps, draws nothing.
|
|
283
|
+
if (!(gap > 0) || (line.dashes.length && (!(period > 0) || line.dashes.every((d) => d < 0))))
|
|
284
|
+
continue;
|
|
285
|
+
const cut = new Map();
|
|
286
|
+
for (const r of used) {
|
|
287
|
+
const along = [], across = [], first = [];
|
|
288
|
+
for (const [x, y] of r.points) {
|
|
289
|
+
along.push(c * (x - bx) + s * (y - by));
|
|
290
|
+
const v = -s * (x - bx) + c * (y - by);
|
|
291
|
+
across.push(v);
|
|
292
|
+
first.push(Math.ceil(v / gap));
|
|
293
|
+
}
|
|
294
|
+
for (let i = 0; i < r.points.length; i++) {
|
|
295
|
+
const j = (i + 1) % r.points.length, low = Math.min(first[i], first[j]), high = Math.max(first[i], first[j]);
|
|
296
|
+
crossings += high - low;
|
|
297
|
+
// Two crossings bound one run of line, so more than this cannot fit the segment limit.
|
|
298
|
+
if (!(crossings <= 2 * most))
|
|
299
|
+
throw over('line segment count', most);
|
|
300
|
+
for (let k = low; k < high; k++) {
|
|
301
|
+
const t = along[i] + ((along[j] - along[i]) * (k * gap - across[i])) / (across[j] - across[i]);
|
|
302
|
+
const list = cut.get(k);
|
|
303
|
+
if (list)
|
|
304
|
+
list.push(t);
|
|
305
|
+
else
|
|
306
|
+
cut.set(k, [t]);
|
|
307
|
+
}
|
|
308
|
+
}
|
|
309
|
+
}
|
|
310
|
+
for (const k of [...cut.keys()].sort((a, b) => a - b)) {
|
|
311
|
+
const ts = cut.get(k).sort((a, b) => a - b), v = k * gap, ox = bx - s * v, oy = by + c * v;
|
|
312
|
+
const emit = (from, to) => {
|
|
313
|
+
if (segments.length / 4 >= most)
|
|
314
|
+
throw over('line segment count', most);
|
|
315
|
+
segments.push(ox + c * from, oy + s * from, ox + c * to, oy + s * to);
|
|
316
|
+
};
|
|
317
|
+
for (let i = 0; i + 1 < ts.length; i += 2) {
|
|
318
|
+
const from = ts[i], to = ts[i + 1];
|
|
319
|
+
if (!(to > from))
|
|
320
|
+
continue;
|
|
321
|
+
if (!line.dashes.length) {
|
|
322
|
+
emit(from, to);
|
|
323
|
+
continue;
|
|
324
|
+
}
|
|
325
|
+
const origin = k * shift, start = Math.floor((from - origin) / period), end = Math.ceil((to - origin) / period);
|
|
326
|
+
if (!(end - start <= most + 2))
|
|
327
|
+
throw over('line segment count', most);
|
|
328
|
+
for (let n = start; n < end; n++) {
|
|
329
|
+
let t = origin + n * period;
|
|
330
|
+
for (const d of line.dashes) {
|
|
331
|
+
const length = Math.abs(d);
|
|
332
|
+
if (d > 0) {
|
|
333
|
+
const a = Math.max(t, from), b = Math.min(t + length, to);
|
|
334
|
+
if (b > a)
|
|
335
|
+
emit(a, b);
|
|
336
|
+
}
|
|
337
|
+
else if (d === 0 && t >= from && t <= to)
|
|
338
|
+
emit(t, t);
|
|
339
|
+
t += length;
|
|
340
|
+
}
|
|
341
|
+
}
|
|
342
|
+
}
|
|
343
|
+
}
|
|
344
|
+
}
|
|
345
|
+
return { triangles, segments, elevation, color };
|
|
346
|
+
};
|
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
type Value = string | number;
|
|
2
|
+
/** A point or vector, xyz. */
|
|
3
|
+
export type DXFPoint = [number, number, number];
|
|
4
|
+
/** An arrowhead at `tip`, pointing away from `from`, flat in the plane of `normal`. */
|
|
5
|
+
export interface DXFArrow {
|
|
6
|
+
tip: DXFPoint;
|
|
7
|
+
from: DXFPoint;
|
|
8
|
+
normal: DXFPoint;
|
|
9
|
+
size: number;
|
|
10
|
+
/** Handle of the BLOCK_RECORD that replaces the closed filled arrow. */
|
|
11
|
+
handle?: string;
|
|
12
|
+
}
|
|
13
|
+
export interface DXFLeaderPlan {
|
|
14
|
+
/** Open polylines. */
|
|
15
|
+
paths: DXFPoint[][];
|
|
16
|
+
arrows: DXFArrow[];
|
|
17
|
+
}
|
|
18
|
+
export interface DXFMultiLeaderPlan extends DXFLeaderPlan {
|
|
19
|
+
/** MTEXT content, formatting codes still in it. */
|
|
20
|
+
text?: {
|
|
21
|
+
raw: string;
|
|
22
|
+
position: DXFPoint;
|
|
23
|
+
height: number;
|
|
24
|
+
direction: DXFPoint;
|
|
25
|
+
normal: DXFPoint;
|
|
26
|
+
};
|
|
27
|
+
/** Block content, and the attribute values by the handle of their ATTDEF. */
|
|
28
|
+
block?: {
|
|
29
|
+
handle: string;
|
|
30
|
+
position: DXFPoint;
|
|
31
|
+
normal: DXFPoint;
|
|
32
|
+
rotation: number;
|
|
33
|
+
scale: DXFPoint;
|
|
34
|
+
attributes: [string, string][];
|
|
35
|
+
};
|
|
36
|
+
/** The content is a tolerance frame, which is not drawn. */
|
|
37
|
+
tolerance: boolean;
|
|
38
|
+
}
|
|
39
|
+
/** A dimension variable by its DIMSTYLE group code. */
|
|
40
|
+
export type DXFDimension = (code: number) => Value | undefined;
|
|
41
|
+
/** Samples of a spline between each pair of fit points, and the most samples that may still be made. */
|
|
42
|
+
export interface DXFCurveLimits {
|
|
43
|
+
segments: number;
|
|
44
|
+
most: number;
|
|
45
|
+
tooMany(): never;
|
|
46
|
+
}
|
|
47
|
+
/**
|
|
48
|
+
* The dimension variables an entity's xdata overrides: under application `ACAD`, `DSTYLE`, then between the braces
|
|
49
|
+
* pairs of a variable's code (1070) and its value.
|
|
50
|
+
*/
|
|
51
|
+
export declare function dimensionOverrides(codes: number[], values: Value[]): Map<number, Value>;
|
|
52
|
+
/** A LEADER: its vertices, as segments or a spline, the arrowhead at the first and the hook line under its text. */
|
|
53
|
+
export declare function readLeader(codes: number[], values: Value[], dimension: DXFDimension, limits: DXFCurveLimits): DXFLeaderPlan;
|
|
54
|
+
/**
|
|
55
|
+
* A MULTILEADER, or `undefined` without its context data. The context block holds the content and one block per
|
|
56
|
+
* leader (landing point, dogleg), which holds one block per leader line (vertices and breaks).
|
|
57
|
+
*/
|
|
58
|
+
export declare function readMultiLeader(codes: number[], values: Value[], dimension: DXFDimension, limits: DXFCurveLimits): DXFMultiLeaderPlan | undefined;
|
|
59
|
+
export {};
|