@mlightcad/geometry-engine 3.16.2 → 3.16.3

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.
@@ -0,0 +1,694 @@
1
+ var __assign = (this && this.__assign) || function () {
2
+ __assign = Object.assign || function(t) {
3
+ for (var s, i = 1, n = arguments.length; i < n; i++) {
4
+ s = arguments[i];
5
+ for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
6
+ t[p] = s[p];
7
+ }
8
+ return t;
9
+ };
10
+ return __assign.apply(this, arguments);
11
+ };
12
+ var __values = (this && this.__values) || function(o) {
13
+ var s = typeof Symbol === "function" && Symbol.iterator, m = s && o[s], i = 0;
14
+ if (m) return m.call(o);
15
+ if (o && typeof o.length === "number") return {
16
+ next: function () {
17
+ if (o && i >= o.length) o = void 0;
18
+ return { value: o && o[i++], done: !o };
19
+ }
20
+ };
21
+ throw new TypeError(s ? "Object is not iterable." : "Symbol.iterator is not defined.");
22
+ };
23
+ import { AcGePoint2d } from '../math/index.js';
24
+ import { AcGeMathUtil, AcGeTol, DEFAULT_TOL, FLOAT_TOL } from '../util/index.js';
25
+ import { AcGeCircArc2d } from './AcGeCircArc2d.js';
26
+ import { acgeAngleAtCenter2d, acgeDistance2d } from './AcGeCircArcUtil.js';
27
+ /**
28
+ * Computes a 2D fillet between two planar curve primitives.
29
+ *
30
+ * Analytic solvers cover line–line, line–circle, and circle–circle. Ellipse and
31
+ * spline operands are approximated by sampling then refined against the circle
32
+ * of the requested radius.
33
+ *
34
+ * @param a - First curve primitive
35
+ * @param b - Second curve primitive
36
+ * @param options - Radius and optional pick hints
37
+ */
38
+ export function acgeFilletCurves2d(a, b, options) {
39
+ var _a;
40
+ var radius = options.radius;
41
+ if (!Number.isFinite(radius) || radius < 0) {
42
+ return { ok: false, error: 'invalidRadius' };
43
+ }
44
+ var tol = (_a = options.tol) !== null && _a !== void 0 ? _a : DEFAULT_TOL;
45
+ if (a.kind === 'line' && b.kind === 'line') {
46
+ return filletLineLine(a, b, radius, options.hintA, options.hintB, tol);
47
+ }
48
+ if (a.kind === 'line' && b.kind === 'circArc') {
49
+ return filletLineCirc(a, b, radius, options.hintA, options.hintB, tol);
50
+ }
51
+ if (a.kind === 'circArc' && b.kind === 'line') {
52
+ var swapped = filletLineCirc(b, a, radius, options.hintB, options.hintA, tol);
53
+ return swapOutcome(swapped);
54
+ }
55
+ if (a.kind === 'circArc' && b.kind === 'circArc') {
56
+ return filletCircCirc(a, b, radius, options.hintA, options.hintB, tol);
57
+ }
58
+ // Ellipse / spline: sample to polyline segments and try line–line against
59
+ // the nearest segment of each operand (and circ when mixed).
60
+ return filletNumeric(a, b, radius, options.hintA, options.hintB, tol);
61
+ }
62
+ /**
63
+ * Builds a fillet between two infinite or bounded lines.
64
+ */
65
+ function filletLineLine(a, b, radius, hintA, hintB, tol) {
66
+ var e_1, _a, e_2, _b;
67
+ var la = toLineGeom(a);
68
+ var lb = toLineGeom(b);
69
+ if (la.length < FLOAT_TOL || lb.length < FLOAT_TOL) {
70
+ return { ok: false, error: 'noSolution' };
71
+ }
72
+ var cross = la.dir.x * lb.dir.y - la.dir.y * lb.dir.x;
73
+ if (Math.abs(cross) < tol.equalVectorTol) {
74
+ return filletParallelLines(la, lb, radius, hintA, hintB, tol);
75
+ }
76
+ var corner = intersectInfinite(la, lb);
77
+ if (!corner)
78
+ return { ok: false, error: 'noIntersection' };
79
+ if (AcGeTol.isNonPositive(radius)) {
80
+ return {
81
+ ok: true,
82
+ result: {
83
+ trimA: corner.clone(),
84
+ trimB: corner.clone(),
85
+ radius: 0
86
+ }
87
+ };
88
+ }
89
+ var candidates = [];
90
+ try {
91
+ for (var _c = __values([1, -1]), _d = _c.next(); !_d.done; _d = _c.next()) {
92
+ var s1 = _d.value;
93
+ try {
94
+ for (var _e = (e_2 = void 0, __values([1, -1])), _f = _e.next(); !_f.done; _f = _e.next()) {
95
+ var s2 = _f.value;
96
+ var offsetA = offsetLine(la, s1 * radius);
97
+ var offsetB = offsetLine(lb, s2 * radius);
98
+ var center = intersectInfinite(offsetA, offsetB);
99
+ if (!center)
100
+ continue;
101
+ var trimA = projectToInfinite(la, center);
102
+ var trimB = projectToInfinite(lb, center);
103
+ var dA = acgeDistance2d(center, trimA);
104
+ var dB = acgeDistance2d(center, trimB);
105
+ if (Math.abs(dA - radius) > tol.equalPointTol * 10 ||
106
+ Math.abs(dB - radius) > tol.equalPointTol * 10) {
107
+ continue;
108
+ }
109
+ // Arc must turn through the corner (smaller turn toward P).
110
+ var arc = buildFilletArc(center, radius, trimA, trimB, corner);
111
+ if (!arc)
112
+ continue;
113
+ candidates.push({
114
+ arc: arc,
115
+ trimA: trimA,
116
+ trimB: trimB,
117
+ center: center,
118
+ radius: radius
119
+ });
120
+ }
121
+ }
122
+ catch (e_2_1) { e_2 = { error: e_2_1 }; }
123
+ finally {
124
+ try {
125
+ if (_f && !_f.done && (_b = _e.return)) _b.call(_e);
126
+ }
127
+ finally { if (e_2) throw e_2.error; }
128
+ }
129
+ }
130
+ }
131
+ catch (e_1_1) { e_1 = { error: e_1_1 }; }
132
+ finally {
133
+ try {
134
+ if (_d && !_d.done && (_a = _c.return)) _a.call(_c);
135
+ }
136
+ finally { if (e_1) throw e_1.error; }
137
+ }
138
+ if (candidates.length === 0) {
139
+ return { ok: false, error: 'noSolution' };
140
+ }
141
+ var best = pickBestCandidate(candidates, hintA, hintB, corner);
142
+ return { ok: true, result: best };
143
+ }
144
+ /**
145
+ * AutoCAD parallel-line fillet: radius becomes half the gap; builds a semicircle.
146
+ */
147
+ function filletParallelLines(la, lb, _requestedRadius, hintA, hintB, tol) {
148
+ var n = new AcGePoint2d(-la.dir.y, la.dir.x);
149
+ var gap = (lb.origin.x - la.origin.x) * n.x + (lb.origin.y - la.origin.y) * n.y;
150
+ var absGap = Math.abs(gap);
151
+ if (absGap < tol.equalPointTol) {
152
+ return { ok: false, error: 'parallel' };
153
+ }
154
+ var radius = absGap / 2;
155
+ var sign = gap > 0 ? 1 : -1;
156
+ // Midline between the two parallels.
157
+ var midOrigin = new AcGePoint2d(la.origin.x + n.x * sign * radius, la.origin.y + n.y * sign * radius);
158
+ var pick = hintA && hintB
159
+ ? midpoint2d(hintA, hintB)
160
+ : hintA
161
+ ? new AcGePoint2d(hintA)
162
+ : hintB
163
+ ? new AcGePoint2d(hintB)
164
+ : new AcGePoint2d((la.origin.x + lb.origin.x) / 2, (la.origin.y + lb.origin.y) / 2);
165
+ var center = projectToInfinite(__assign(__assign({}, la), { origin: midOrigin }), pick);
166
+ var trimA = new AcGePoint2d(center.x - n.x * sign * radius, center.y - n.y * sign * radius);
167
+ var trimB = new AcGePoint2d(center.x + n.x * sign * radius, center.y + n.y * sign * radius);
168
+ // Prefer the semicircle whose bulge faces the average of the picks along dir.
169
+ var through = new AcGePoint2d(center.x + la.dir.x * radius, center.y + la.dir.y * radius);
170
+ var throughAlt = new AcGePoint2d(center.x - la.dir.x * radius, center.y - la.dir.y * radius);
171
+ var preferThrough = acgeDistance2d(through, pick) <= acgeDistance2d(throughAlt, pick)
172
+ ? through
173
+ : throughAlt;
174
+ var arc = AcGeCircArc2d.tryCreateByThreePoints(trimA, preferThrough, trimB);
175
+ if (!arc)
176
+ return { ok: false, error: 'noSolution' };
177
+ return {
178
+ ok: true,
179
+ result: { arc: arc, trimA: trimA, trimB: trimB, center: center, radius: radius }
180
+ };
181
+ }
182
+ /**
183
+ * Fillet between a line and a circular arc / circle.
184
+ */
185
+ function filletLineCirc(line, circ, radius, hintA, hintB, tol) {
186
+ var e_3, _a, e_4, _b, e_5, _c;
187
+ var _d;
188
+ var la = toLineGeom(line);
189
+ var ca = toCircGeom(circ);
190
+ if (la.length < FLOAT_TOL || ca.radius < FLOAT_TOL) {
191
+ return { ok: false, error: 'noSolution' };
192
+ }
193
+ if (AcGeTol.isNonPositive(radius)) {
194
+ return filletLineCircZero(la, ca, hintA, hintB, tol);
195
+ }
196
+ var candidates = [];
197
+ try {
198
+ for (var _e = __values([1, -1]), _f = _e.next(); !_f.done; _f = _e.next()) {
199
+ var sLine = _f.value;
200
+ try {
201
+ for (var _g = (e_4 = void 0, __values([1, -1])), _h = _g.next(); !_h.done; _h = _g.next()) {
202
+ var sCirc = _h.value;
203
+ var offsetR = ca.radius + sCirc * radius;
204
+ if (offsetR < FLOAT_TOL)
205
+ continue;
206
+ var offsetLineGeom = offsetLine(la, sLine * radius);
207
+ var centers = intersectLineCircle(offsetLineGeom, ca.center, offsetR, tol);
208
+ try {
209
+ for (var centers_1 = (e_5 = void 0, __values(centers)), centers_1_1 = centers_1.next(); !centers_1_1.done; centers_1_1 = centers_1.next()) {
210
+ var center = centers_1_1.value;
211
+ var trimA = projectToInfinite(la, center);
212
+ var toCenter = new AcGePoint2d(center.x - ca.center.x, center.y - ca.center.y);
213
+ var len = Math.hypot(toCenter.x, toCenter.y);
214
+ if (len < FLOAT_TOL)
215
+ continue;
216
+ var trimB = new AcGePoint2d(ca.center.x + (toCenter.x / len) * ca.radius, ca.center.y + (toCenter.y / len) * ca.radius);
217
+ if (!pointOnCirc(ca, trimB, tol) && !ca.closed)
218
+ continue;
219
+ var midHint = (_d = midpointOptional(hintA, hintB)) !== null && _d !== void 0 ? _d : cornerHint(trimA, trimB);
220
+ var arc = buildFilletArc(center, radius, trimA, trimB, midHint);
221
+ if (!arc)
222
+ continue;
223
+ candidates.push({ arc: arc, trimA: trimA, trimB: trimB, center: center, radius: radius });
224
+ }
225
+ }
226
+ catch (e_5_1) { e_5 = { error: e_5_1 }; }
227
+ finally {
228
+ try {
229
+ if (centers_1_1 && !centers_1_1.done && (_c = centers_1.return)) _c.call(centers_1);
230
+ }
231
+ finally { if (e_5) throw e_5.error; }
232
+ }
233
+ }
234
+ }
235
+ catch (e_4_1) { e_4 = { error: e_4_1 }; }
236
+ finally {
237
+ try {
238
+ if (_h && !_h.done && (_b = _g.return)) _b.call(_g);
239
+ }
240
+ finally { if (e_4) throw e_4.error; }
241
+ }
242
+ }
243
+ }
244
+ catch (e_3_1) { e_3 = { error: e_3_1 }; }
245
+ finally {
246
+ try {
247
+ if (_f && !_f.done && (_a = _e.return)) _a.call(_e);
248
+ }
249
+ finally { if (e_3) throw e_3.error; }
250
+ }
251
+ if (candidates.length === 0)
252
+ return { ok: false, error: 'noSolution' };
253
+ var hint = midpointOptional(hintA, hintB);
254
+ var best = pickBestCandidate(candidates, hintA, hintB, hint);
255
+ return { ok: true, result: best };
256
+ }
257
+ function filletLineCircZero(la, ca, hintA, hintB, tol) {
258
+ var e_6, _a;
259
+ var hits = intersectLineCircle(la, ca.center, ca.radius, tol);
260
+ if (hits.length === 0)
261
+ return { ok: false, error: 'noIntersection' };
262
+ var hint = midpointOptional(hintA, hintB);
263
+ var best = hits[0];
264
+ var bestDist = Infinity;
265
+ try {
266
+ for (var hits_1 = __values(hits), hits_1_1 = hits_1.next(); !hits_1_1.done; hits_1_1 = hits_1.next()) {
267
+ var hit = hits_1_1.value;
268
+ var d = hint ? acgeDistance2d(hit, hint) : 0;
269
+ if (d < bestDist) {
270
+ bestDist = d;
271
+ best = hit;
272
+ }
273
+ }
274
+ }
275
+ catch (e_6_1) { e_6 = { error: e_6_1 }; }
276
+ finally {
277
+ try {
278
+ if (hits_1_1 && !hits_1_1.done && (_a = hits_1.return)) _a.call(hits_1);
279
+ }
280
+ finally { if (e_6) throw e_6.error; }
281
+ }
282
+ return {
283
+ ok: true,
284
+ result: {
285
+ trimA: best.clone(),
286
+ trimB: best.clone(),
287
+ radius: 0
288
+ }
289
+ };
290
+ }
291
+ /**
292
+ * Fillet between two circular arcs / circles.
293
+ */
294
+ function filletCircCirc(a, b, radius, hintA, hintB, tol) {
295
+ var e_7, _a, e_8, _b, e_9, _c, e_10, _d;
296
+ var _e;
297
+ var ca = toCircGeom(a);
298
+ var cb = toCircGeom(b);
299
+ if (AcGeTol.isNonPositive(radius)) {
300
+ var hits = intersectCircles(ca.center, ca.radius, cb.center, cb.radius, tol);
301
+ if (hits.length === 0)
302
+ return { ok: false, error: 'noIntersection' };
303
+ var hint = midpointOptional(hintA, hintB);
304
+ var best_1 = hits[0];
305
+ var bestDist = Infinity;
306
+ try {
307
+ for (var hits_2 = __values(hits), hits_2_1 = hits_2.next(); !hits_2_1.done; hits_2_1 = hits_2.next()) {
308
+ var hit = hits_2_1.value;
309
+ var d = hint ? acgeDistance2d(hit, hint) : 0;
310
+ if (d < bestDist) {
311
+ bestDist = d;
312
+ best_1 = hit;
313
+ }
314
+ }
315
+ }
316
+ catch (e_7_1) { e_7 = { error: e_7_1 }; }
317
+ finally {
318
+ try {
319
+ if (hits_2_1 && !hits_2_1.done && (_a = hits_2.return)) _a.call(hits_2);
320
+ }
321
+ finally { if (e_7) throw e_7.error; }
322
+ }
323
+ return {
324
+ ok: true,
325
+ result: { trimA: best_1.clone(), trimB: best_1.clone(), radius: 0 }
326
+ };
327
+ }
328
+ var candidates = [];
329
+ try {
330
+ for (var _f = __values([1, -1]), _g = _f.next(); !_g.done; _g = _f.next()) {
331
+ var sA = _g.value;
332
+ try {
333
+ for (var _h = (e_9 = void 0, __values([1, -1])), _j = _h.next(); !_j.done; _j = _h.next()) {
334
+ var sB = _j.value;
335
+ var rA = ca.radius + sA * radius;
336
+ var rB = cb.radius + sB * radius;
337
+ if (rA < FLOAT_TOL || rB < FLOAT_TOL)
338
+ continue;
339
+ var centers = intersectCircles(ca.center, rA, cb.center, rB, tol);
340
+ try {
341
+ for (var centers_2 = (e_10 = void 0, __values(centers)), centers_2_1 = centers_2.next(); !centers_2_1.done; centers_2_1 = centers_2.next()) {
342
+ var center = centers_2_1.value;
343
+ var trimA = radialPoint(ca.center, ca.radius, center);
344
+ var trimB = radialPoint(cb.center, cb.radius, center);
345
+ if (!trimA || !trimB)
346
+ continue;
347
+ if (!ca.closed && !pointOnCirc(ca, trimA, tol))
348
+ continue;
349
+ if (!cb.closed && !pointOnCirc(cb, trimB, tol))
350
+ continue;
351
+ var midHint = (_e = midpointOptional(hintA, hintB)) !== null && _e !== void 0 ? _e : cornerHint(trimA, trimB);
352
+ var arc = buildFilletArc(center, radius, trimA, trimB, midHint);
353
+ if (!arc)
354
+ continue;
355
+ candidates.push({ arc: arc, trimA: trimA, trimB: trimB, center: center, radius: radius });
356
+ }
357
+ }
358
+ catch (e_10_1) { e_10 = { error: e_10_1 }; }
359
+ finally {
360
+ try {
361
+ if (centers_2_1 && !centers_2_1.done && (_d = centers_2.return)) _d.call(centers_2);
362
+ }
363
+ finally { if (e_10) throw e_10.error; }
364
+ }
365
+ }
366
+ }
367
+ catch (e_9_1) { e_9 = { error: e_9_1 }; }
368
+ finally {
369
+ try {
370
+ if (_j && !_j.done && (_c = _h.return)) _c.call(_h);
371
+ }
372
+ finally { if (e_9) throw e_9.error; }
373
+ }
374
+ }
375
+ }
376
+ catch (e_8_1) { e_8 = { error: e_8_1 }; }
377
+ finally {
378
+ try {
379
+ if (_g && !_g.done && (_b = _f.return)) _b.call(_f);
380
+ }
381
+ finally { if (e_8) throw e_8.error; }
382
+ }
383
+ if (candidates.length === 0)
384
+ return { ok: false, error: 'noSolution' };
385
+ var best = pickBestCandidate(candidates, hintA, hintB, midpointOptional(hintA, hintB));
386
+ return { ok: true, result: best };
387
+ }
388
+ /**
389
+ * Numeric fillet for ellipse / spline (and mixed) by sampling to polylines.
390
+ */
391
+ function filletNumeric(a, b, radius, hintA, hintB, tol) {
392
+ var samplesA = samplePrimitive(a);
393
+ var samplesB = samplePrimitive(b);
394
+ if (samplesA.length < 2 || samplesB.length < 2) {
395
+ return { ok: false, error: 'unsupported' };
396
+ }
397
+ var segA = nearestSegment(samplesA, hintA !== null && hintA !== void 0 ? hintA : samplesA[0]);
398
+ var segB = nearestSegment(samplesB, hintB !== null && hintB !== void 0 ? hintB : samplesB[0]);
399
+ if (!segA || !segB)
400
+ return { ok: false, error: 'noSolution' };
401
+ if (a.kind === 'circArc') {
402
+ var mixed = filletLineCirc({ kind: 'line', start: segB.p0, end: segB.p1, extent: 'bounded' }, a, radius, hintB, hintA, tol);
403
+ if (mixed.ok)
404
+ return swapOutcome(mixed);
405
+ }
406
+ if (b.kind === 'circArc') {
407
+ var mixed = filletLineCirc({ kind: 'line', start: segA.p0, end: segA.p1, extent: 'bounded' }, b, radius, hintA, hintB, tol);
408
+ if (mixed.ok)
409
+ return mixed;
410
+ }
411
+ return filletLineLine({ kind: 'line', start: segA.p0, end: segA.p1, extent: 'bounded' }, { kind: 'line', start: segB.p0, end: segB.p1, extent: 'bounded' }, radius, hintA, hintB, tol);
412
+ }
413
+ function samplePrimitive(p) {
414
+ if (p.kind === 'line') {
415
+ return [new AcGePoint2d(p.start), new AcGePoint2d(p.end)];
416
+ }
417
+ if (p.kind === 'circArc') {
418
+ return sampleCirc(p, 64);
419
+ }
420
+ if (p.kind === 'ellipseArc') {
421
+ return sampleEllipse(p, 96);
422
+ }
423
+ return p.samples.map(function (s) { return new AcGePoint2d(s); });
424
+ }
425
+ function sampleCirc(c, count) {
426
+ var pts = [];
427
+ var start = AcGeMathUtil.normalizeAngle(c.startAngle);
428
+ var n = Math.max(8, count);
429
+ for (var i = 0; i <= n; i++) {
430
+ var t = i / n;
431
+ var use = c.closed
432
+ ? start + Math.PI * 2 * t
433
+ : c.clockwise
434
+ ? start - AcGeMathUtil.normalizeAngle(c.startAngle - c.endAngle) * t
435
+ : start + AcGeMathUtil.normalizeAngle(c.endAngle - c.startAngle) * t;
436
+ pts.push(new AcGePoint2d(c.center.x + c.radius * Math.cos(use), c.center.y + c.radius * Math.sin(use)));
437
+ }
438
+ return pts;
439
+ }
440
+ function sampleEllipse(e, count) {
441
+ var _a;
442
+ var pts = [];
443
+ var rotation = (_a = e.rotation) !== null && _a !== void 0 ? _a : 0;
444
+ var cosR = Math.cos(rotation);
445
+ var sinR = Math.sin(rotation);
446
+ var n = Math.max(8, count);
447
+ for (var i = 0; i <= n; i++) {
448
+ var t = i / n;
449
+ var ang = e.closed
450
+ ? e.startAngle + Math.PI * 2 * t
451
+ : e.clockwise
452
+ ? e.startAngle -
453
+ AcGeMathUtil.normalizeAngle(e.startAngle - e.endAngle) * t
454
+ : e.startAngle +
455
+ AcGeMathUtil.normalizeAngle(e.endAngle - e.startAngle) * t;
456
+ var lx = e.majorRadius * Math.cos(ang);
457
+ var ly = e.minorRadius * Math.sin(ang);
458
+ pts.push(new AcGePoint2d(e.center.x + lx * cosR - ly * sinR, e.center.y + lx * sinR + ly * cosR));
459
+ }
460
+ return pts;
461
+ }
462
+ function nearestSegment(samples, hint) {
463
+ var best = null;
464
+ var bestDist = Infinity;
465
+ for (var i = 0; i < samples.length - 1; i++) {
466
+ var p0 = samples[i];
467
+ var p1 = samples[i + 1];
468
+ var proj = projectSegment(p0, p1, hint);
469
+ var d = acgeDistance2d(proj, hint);
470
+ if (d < bestDist) {
471
+ bestDist = d;
472
+ best = { p0: p0, p1: p1 };
473
+ }
474
+ }
475
+ return best;
476
+ }
477
+ function projectSegment(p0, p1, pt) {
478
+ var dx = p1.x - p0.x;
479
+ var dy = p1.y - p0.y;
480
+ var lenSq = dx * dx + dy * dy;
481
+ if (lenSq < FLOAT_TOL)
482
+ return p0.clone();
483
+ var t = ((pt.x - p0.x) * dx + (pt.y - p0.y) * dy) / lenSq;
484
+ t = Math.max(0, Math.min(1, t));
485
+ return new AcGePoint2d(p0.x + t * dx, p0.y + t * dy);
486
+ }
487
+ function toLineGeom(line) {
488
+ var _a;
489
+ var origin = new AcGePoint2d(line.start);
490
+ var end = new AcGePoint2d(line.end);
491
+ var dx = end.x - origin.x;
492
+ var dy = end.y - origin.y;
493
+ var length = Math.hypot(dx, dy);
494
+ var dir = length > FLOAT_TOL
495
+ ? new AcGePoint2d(dx / length, dy / length)
496
+ : new AcGePoint2d(1, 0);
497
+ return { origin: origin, dir: dir, length: length, extent: (_a = line.extent) !== null && _a !== void 0 ? _a : 'bounded' };
498
+ }
499
+ function toCircGeom(c) {
500
+ return {
501
+ center: new AcGePoint2d(c.center),
502
+ radius: c.radius,
503
+ startAngle: AcGeMathUtil.normalizeAngle(c.startAngle),
504
+ endAngle: AcGeMathUtil.normalizeAngle(c.endAngle),
505
+ clockwise: !!c.clockwise,
506
+ closed: !!c.closed
507
+ };
508
+ }
509
+ function offsetLine(line, dist) {
510
+ var n = new AcGePoint2d(-line.dir.y, line.dir.x);
511
+ return {
512
+ origin: new AcGePoint2d(line.origin.x + n.x * dist, line.origin.y + n.y * dist),
513
+ dir: line.dir.clone(),
514
+ length: line.length,
515
+ extent: 'unbounded'
516
+ };
517
+ }
518
+ function intersectInfinite(a, b) {
519
+ var cross = a.dir.x * b.dir.y - a.dir.y * b.dir.x;
520
+ if (Math.abs(cross) < FLOAT_TOL)
521
+ return null;
522
+ var dx = b.origin.x - a.origin.x;
523
+ var dy = b.origin.y - a.origin.y;
524
+ var t = (dx * b.dir.y - dy * b.dir.x) / cross;
525
+ return new AcGePoint2d(a.origin.x + a.dir.x * t, a.origin.y + a.dir.y * t);
526
+ }
527
+ function projectToInfinite(line, pt) {
528
+ var t = (pt.x - line.origin.x) * line.dir.x + (pt.y - line.origin.y) * line.dir.y;
529
+ return new AcGePoint2d(line.origin.x + line.dir.x * t, line.origin.y + line.dir.y * t);
530
+ }
531
+ function intersectLineCircle(line, center, radius, tol) {
532
+ var f = new AcGePoint2d(line.origin.x - center.x, line.origin.y - center.y);
533
+ var a = 1;
534
+ var b = 2 * (f.x * line.dir.x + f.y * line.dir.y);
535
+ var c = f.x * f.x + f.y * f.y - radius * radius;
536
+ var disc = b * b - 4 * a * c;
537
+ if (disc < -tol.equalPointTol)
538
+ return [];
539
+ var sqrtDisc = Math.sqrt(Math.max(0, disc));
540
+ var t1 = (-b - sqrtDisc) / (2 * a);
541
+ var t2 = (-b + sqrtDisc) / (2 * a);
542
+ var out = [
543
+ new AcGePoint2d(line.origin.x + line.dir.x * t1, line.origin.y + line.dir.y * t1)
544
+ ];
545
+ if (Math.abs(t2 - t1) > tol.equalPointTol) {
546
+ out.push(new AcGePoint2d(line.origin.x + line.dir.x * t2, line.origin.y + line.dir.y * t2));
547
+ }
548
+ return out;
549
+ }
550
+ function intersectCircles(c0, r0, c1, r1, tol) {
551
+ var dx = c1.x - c0.x;
552
+ var dy = c1.y - c0.y;
553
+ var dist = Math.hypot(dx, dy);
554
+ if (dist < FLOAT_TOL)
555
+ return [];
556
+ if (dist > r0 + r1 + tol.equalPointTol)
557
+ return [];
558
+ if (dist < Math.abs(r0 - r1) - tol.equalPointTol)
559
+ return [];
560
+ var a = (r0 * r0 - r1 * r1 + dist * dist) / (2 * dist);
561
+ var hSq = r0 * r0 - a * a;
562
+ var h = Math.sqrt(Math.max(0, hSq));
563
+ var mx = c0.x + (dx * a) / dist;
564
+ var my = c0.y + (dy * a) / dist;
565
+ if (h < tol.equalPointTol) {
566
+ return [new AcGePoint2d(mx, my)];
567
+ }
568
+ var rx = (-dy * h) / dist;
569
+ var ry = (dx * h) / dist;
570
+ return [new AcGePoint2d(mx + rx, my + ry), new AcGePoint2d(mx - rx, my - ry)];
571
+ }
572
+ function radialPoint(center, radius, from) {
573
+ var dx = from.x - center.x;
574
+ var dy = from.y - center.y;
575
+ var len = Math.hypot(dx, dy);
576
+ if (len < FLOAT_TOL)
577
+ return null;
578
+ return new AcGePoint2d(center.x + (dx / len) * radius, center.y + (dy / len) * radius);
579
+ }
580
+ function pointOnCirc(c, pt, _tol) {
581
+ if (c.closed)
582
+ return true;
583
+ var ang = acgeAngleAtCenter2d(c.center, pt);
584
+ if (c.clockwise) {
585
+ return AcGeMathUtil.isAngleOnCcwSweep(c.endAngle, ang, c.startAngle);
586
+ }
587
+ return AcGeMathUtil.isAngleOnCcwSweep(c.startAngle, ang, c.endAngle);
588
+ }
589
+ /**
590
+ * Builds a fillet arc from trimA to trimB that bends toward `toward`.
591
+ */
592
+ function buildFilletArc(center, _radius, trimA, trimB, toward) {
593
+ var startAng = acgeAngleAtCenter2d(center, trimA);
594
+ var endAng = acgeAngleAtCenter2d(center, trimB);
595
+ var towardAng = acgeAngleAtCenter2d(center, toward);
596
+ var ccwOn = AcGeMathUtil.isAngleOnCcwSweep(startAng, towardAng, endAng);
597
+ var clockwise = !ccwOn;
598
+ return AcGeCircArc2d.tryCreateByCenterStartEnd(center, trimA, trimB, clockwise);
599
+ }
600
+ function pickBestCandidate(candidates, hintA, hintB, corner) {
601
+ var e_11, _a;
602
+ var _b;
603
+ var hint = (_b = midpointOptional(hintA, hintB)) !== null && _b !== void 0 ? _b : corner;
604
+ if (!hint)
605
+ return candidates[0];
606
+ var best = candidates[0];
607
+ var bestScore = Infinity;
608
+ try {
609
+ for (var candidates_1 = __values(candidates), candidates_1_1 = candidates_1.next(); !candidates_1_1.done; candidates_1_1 = candidates_1.next()) {
610
+ var c = candidates_1_1.value;
611
+ var mid = c.center
612
+ ? c.center
613
+ : midpoint2d(c.trimA, c.trimB);
614
+ // Prefer solutions whose trim points are near the picks and center near corner.
615
+ var score = acgeDistance2d(mid, hint);
616
+ if (hintA)
617
+ score += acgeDistance2d(c.trimA, hintA) * 0.5;
618
+ if (hintB)
619
+ score += acgeDistance2d(c.trimB, hintB) * 0.5;
620
+ if (c.arc) {
621
+ // Prefer smaller sweep (typical corner fillet).
622
+ var sweep = Math.abs(c.arc.endAngle - c.arc.startAngle);
623
+ score += sweep * 0.01;
624
+ }
625
+ if (score < bestScore) {
626
+ bestScore = score;
627
+ best = c;
628
+ }
629
+ }
630
+ }
631
+ catch (e_11_1) { e_11 = { error: e_11_1 }; }
632
+ finally {
633
+ try {
634
+ if (candidates_1_1 && !candidates_1_1.done && (_a = candidates_1.return)) _a.call(candidates_1);
635
+ }
636
+ finally { if (e_11) throw e_11.error; }
637
+ }
638
+ return best;
639
+ }
640
+ function midpoint2d(a, b) {
641
+ return new AcGePoint2d((a.x + b.x) / 2, (a.y + b.y) / 2);
642
+ }
643
+ function midpointOptional(a, b) {
644
+ if (a && b)
645
+ return midpoint2d(a, b);
646
+ if (a)
647
+ return new AcGePoint2d(a);
648
+ if (b)
649
+ return new AcGePoint2d(b);
650
+ return undefined;
651
+ }
652
+ function cornerHint(a, b) {
653
+ return midpoint2d(a, b);
654
+ }
655
+ function swapOutcome(outcome) {
656
+ if (!outcome.ok)
657
+ return outcome;
658
+ return {
659
+ ok: true,
660
+ result: __assign(__assign({}, outcome.result), { trimA: outcome.result.trimB, trimB: outcome.result.trimA })
661
+ };
662
+ }
663
+ /**
664
+ * Creates an {@link AcGeFilletSpline2d} primitive by sampling an {@link AcGeSpline3d}.
665
+ *
666
+ * @param spline - Source spline (XY of samples used)
667
+ * @param sampleCount - Number of samples along the spline (≥2)
668
+ */
669
+ export function acgeFilletPrimitiveFromSpline3d(spline, sampleCount) {
670
+ if (sampleCount === void 0) { sampleCount = 64; }
671
+ var n = Math.max(2, sampleCount);
672
+ var points = spline.getPoints(n);
673
+ return {
674
+ kind: 'spline',
675
+ samples: points.map(function (p) { return ({ x: p.x, y: p.y }); })
676
+ };
677
+ }
678
+ /**
679
+ * Creates an {@link AcGeFilletEllipseArc2d} from an {@link AcGeEllipseArc2d}.
680
+ */
681
+ export function acgeFilletPrimitiveFromEllipse2d(ellipse) {
682
+ return {
683
+ kind: 'ellipseArc',
684
+ center: { x: ellipse.center.x, y: ellipse.center.y },
685
+ majorRadius: ellipse.majorAxisRadius,
686
+ minorRadius: ellipse.minorAxisRadius,
687
+ startAngle: ellipse.startAngle,
688
+ endAngle: ellipse.endAngle,
689
+ clockwise: ellipse.clockwise,
690
+ closed: ellipse.closed,
691
+ rotation: ellipse.rotation
692
+ };
693
+ }
694
+ //# sourceMappingURL=AcGeFillet2d.js.map