rm-graphical-computing-node 1.0.163 → 1.0.165

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.
Files changed (2) hide show
  1. package/dist/index.mjs +1442 -41
  2. package/package.json +1 -1
package/dist/index.mjs CHANGED
@@ -4,7 +4,13 @@ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
4
  var __getOwnPropNames = Object.getOwnPropertyNames;
5
5
  var __getProtoOf = Object.getPrototypeOf;
6
6
  var __hasOwnProp = Object.prototype.hasOwnProperty;
7
- var __commonJS = (cb, mod) => function __require() {
7
+ var __require = /* @__PURE__ */ ((x) => typeof require !== "undefined" ? require : typeof Proxy !== "undefined" ? new Proxy(x, {
8
+ get: (a, b) => (typeof require !== "undefined" ? require : a)[b]
9
+ }) : x)(function(x) {
10
+ if (typeof require !== "undefined") return require.apply(this, arguments);
11
+ throw Error('Dynamic require of "' + x + '" is not supported');
12
+ });
13
+ var __commonJS = (cb, mod) => function __require2() {
8
14
  return mod || (0, cb[__getOwnPropNames(cb)[0]])((mod = { exports: {} }).exports, mod), mod.exports;
9
15
  };
10
16
  var __export = (target, all) => {
@@ -30,9 +36,9 @@ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__ge
30
36
 
31
37
  // node_modules/splaytree/dist/splaytree.umd.cjs
32
38
  var require_splaytree_umd = __commonJS({
33
- "node_modules/splaytree/dist/splaytree.umd.cjs"(exports, module) {
39
+ "node_modules/splaytree/dist/splaytree.umd.cjs"(exports, module2) {
34
40
  (function(h, c) {
35
- typeof exports == "object" && typeof module < "u" ? module.exports = c() : typeof define == "function" && define.amd ? define(c) : (h = typeof globalThis < "u" ? globalThis : h || self, h.SplayTree = c());
41
+ typeof exports == "object" && typeof module2 < "u" ? module2.exports = c() : typeof define == "function" && define.amd ? define(c) : (h = typeof globalThis < "u" ? globalThis : h || self, h.SplayTree = c());
36
42
  })(exports, (function() {
37
43
  "use strict";
38
44
  class h {
@@ -331,7 +337,7 @@ var require_splaytree_umd = __commonJS({
331
337
 
332
338
  // node_modules/polygon-clipping/dist/polygon-clipping.cjs.js
333
339
  var require_polygon_clipping_cjs = __commonJS({
334
- "node_modules/polygon-clipping/dist/polygon-clipping.cjs.js"(exports, module) {
340
+ "node_modules/polygon-clipping/dist/polygon-clipping.cjs.js"(exports, module2) {
335
341
  "use strict";
336
342
  var SplayTree = require_splaytree_umd();
337
343
  function _interopDefaultLegacy(e) {
@@ -1744,15 +1750,15 @@ var require_polygon_clipping_cjs = __commonJS({
1744
1750
  xor,
1745
1751
  difference
1746
1752
  };
1747
- module.exports = index;
1753
+ module2.exports = index;
1748
1754
  }
1749
1755
  });
1750
1756
 
1751
1757
  // node_modules/rbush/rbush.js
1752
1758
  var require_rbush = __commonJS({
1753
- "node_modules/rbush/rbush.js"(exports, module) {
1759
+ "node_modules/rbush/rbush.js"(exports, module2) {
1754
1760
  (function(global, factory) {
1755
- typeof exports === "object" && typeof module !== "undefined" ? module.exports = factory() : typeof define === "function" && define.amd ? define(factory) : (global = global || self, global.RBush = factory());
1761
+ typeof exports === "object" && typeof module2 !== "undefined" ? module2.exports = factory() : typeof define === "function" && define.amd ? define(factory) : (global = global || self, global.RBush = factory());
1756
1762
  })(exports, function() {
1757
1763
  "use strict";
1758
1764
  function quickselect(arr, k, left, right, compare) {
@@ -2229,9 +2235,9 @@ var require_rbush = __commonJS({
2229
2235
 
2230
2236
  // node_modules/tinyqueue/tinyqueue.js
2231
2237
  var require_tinyqueue = __commonJS({
2232
- "node_modules/tinyqueue/tinyqueue.js"(exports, module) {
2238
+ "node_modules/tinyqueue/tinyqueue.js"(exports, module2) {
2233
2239
  (function(global, factory) {
2234
- typeof exports === "object" && typeof module !== "undefined" ? module.exports = factory() : typeof define === "function" && define.amd ? define(factory) : (global = global || self, global.TinyQueue = factory());
2240
+ typeof exports === "object" && typeof module2 !== "undefined" ? module2.exports = factory() : typeof define === "function" && define.amd ? define(factory) : (global = global || self, global.TinyQueue = factory());
2235
2241
  })(exports, function() {
2236
2242
  "use strict";
2237
2243
  var TinyQueue = function TinyQueue2(data, compare) {
@@ -2315,8 +2321,8 @@ var require_tinyqueue = __commonJS({
2315
2321
 
2316
2322
  // node_modules/point-in-polygon/flat.js
2317
2323
  var require_flat = __commonJS({
2318
- "node_modules/point-in-polygon/flat.js"(exports, module) {
2319
- module.exports = function pointInPolygonFlat(point2, vs, start, end) {
2324
+ "node_modules/point-in-polygon/flat.js"(exports, module2) {
2325
+ module2.exports = function pointInPolygonFlat(point2, vs, start, end) {
2320
2326
  var x = point2[0], y = point2[1];
2321
2327
  var inside = false;
2322
2328
  if (start === void 0) start = 0;
@@ -2335,8 +2341,8 @@ var require_flat = __commonJS({
2335
2341
 
2336
2342
  // node_modules/point-in-polygon/nested.js
2337
2343
  var require_nested = __commonJS({
2338
- "node_modules/point-in-polygon/nested.js"(exports, module) {
2339
- module.exports = function pointInPolygonNested(point2, vs, start, end) {
2344
+ "node_modules/point-in-polygon/nested.js"(exports, module2) {
2345
+ module2.exports = function pointInPolygonNested(point2, vs, start, end) {
2340
2346
  var x = point2[0], y = point2[1];
2341
2347
  var inside = false;
2342
2348
  if (start === void 0) start = 0;
@@ -2355,26 +2361,26 @@ var require_nested = __commonJS({
2355
2361
 
2356
2362
  // node_modules/point-in-polygon/index.js
2357
2363
  var require_point_in_polygon = __commonJS({
2358
- "node_modules/point-in-polygon/index.js"(exports, module) {
2364
+ "node_modules/point-in-polygon/index.js"(exports, module2) {
2359
2365
  var pointInPolygonFlat = require_flat();
2360
2366
  var pointInPolygonNested = require_nested();
2361
- module.exports = function pointInPolygon2(point2, vs, start, end) {
2367
+ module2.exports = function pointInPolygon2(point2, vs, start, end) {
2362
2368
  if (vs.length > 0 && Array.isArray(vs[0])) {
2363
2369
  return pointInPolygonNested(point2, vs, start, end);
2364
2370
  } else {
2365
2371
  return pointInPolygonFlat(point2, vs, start, end);
2366
2372
  }
2367
2373
  };
2368
- module.exports.nested = pointInPolygonNested;
2369
- module.exports.flat = pointInPolygonFlat;
2374
+ module2.exports.nested = pointInPolygonNested;
2375
+ module2.exports.flat = pointInPolygonFlat;
2370
2376
  }
2371
2377
  });
2372
2378
 
2373
2379
  // node_modules/robust-predicates/umd/orient2d.min.js
2374
2380
  var require_orient2d_min = __commonJS({
2375
- "node_modules/robust-predicates/umd/orient2d.min.js"(exports, module) {
2381
+ "node_modules/robust-predicates/umd/orient2d.min.js"(exports, module2) {
2376
2382
  !(function(t, e) {
2377
- "object" == typeof exports && "undefined" != typeof module ? e(exports) : "function" == typeof define && define.amd ? define(["exports"], e) : e((t = t || self).predicates = {});
2383
+ "object" == typeof exports && "undefined" != typeof module2 ? e(exports) : "function" == typeof define && define.amd ? define(["exports"], e) : e((t = t || self).predicates = {});
2378
2384
  })(exports, function(t) {
2379
2385
  "use strict";
2380
2386
  const e = 134217729, n = 33306690738754706e-32;
@@ -2424,7 +2430,7 @@ var require_orient2d_min = __commonJS({
2424
2430
 
2425
2431
  // node_modules/concaveman/index.js
2426
2432
  var require_concaveman = __commonJS({
2427
- "node_modules/concaveman/index.js"(exports, module) {
2433
+ "node_modules/concaveman/index.js"(exports, module2) {
2428
2434
  "use strict";
2429
2435
  var RBush = require_rbush();
2430
2436
  var Queue = require_tinyqueue();
@@ -2433,8 +2439,8 @@ var require_concaveman = __commonJS({
2433
2439
  if (Queue.default) {
2434
2440
  Queue = Queue.default;
2435
2441
  }
2436
- module.exports = concaveman2;
2437
- module.exports.default = concaveman2;
2442
+ module2.exports = concaveman2;
2443
+ module2.exports.default = concaveman2;
2438
2444
  function concaveman2(points, concavity, lengthThreshold) {
2439
2445
  concavity = Math.max(0, concavity === void 0 ? 2 : concavity);
2440
2446
  lengthThreshold = lengthThreshold || 0;
@@ -8831,6 +8837,8 @@ var getBeamLine = async (lines, runDataList, pcdData, lineReader, lineEditor, on
8831
8837
  if (doorAndDoorFrameDatas[n].category == "metal framing of interior glass partitions") {
8832
8838
  continue;
8833
8839
  }
8840
+ if (length2 < doorAndDoorFrameDatas[n].length && doorAndDoorFrameDatas[n].length - length2 > 0.5)
8841
+ continue;
8834
8842
  let tmppLine3 = new THREE5.Line3(beamSt, beamEd);
8835
8843
  if (dist11 < nearDist || dist33 < nearDist) {
8836
8844
  doorOpenEndPoint = beamSt.clone();
@@ -8874,6 +8882,8 @@ var getBeamLine = async (lines, runDataList, pcdData, lineReader, lineEditor, on
8874
8882
  obj_id = doorAndDoorFrameDatas[n].obj_id;
8875
8883
  doorAndDoorFrameDatas[n].isFind = true;
8876
8884
  findDoorDataIndex = n;
8885
+ if (length2 < 1.2)
8886
+ break;
8877
8887
  }
8878
8888
  }
8879
8889
  }
@@ -19247,6 +19257,1396 @@ var getDoorPcdData = (lines, pcdData, needFindOtherDoor = true) => {
19247
19257
  return lines;
19248
19258
  };
19249
19259
 
19260
+ // src/geometry/wallLine.mjs
19261
+ import * as THREE12 from "three";
19262
+
19263
+ // src/utils/arc-tools.js
19264
+ function fitPolyline(points, epsilon3 = 0.01, binCount = 32) {
19265
+ if (!Array.isArray(points)) {
19266
+ throw new Error("points \u5FC5\u987B\u4E3A\u6570\u7EC4");
19267
+ }
19268
+ if (points.some(
19269
+ (p) => !p || !Number.isFinite(p.x) || !Number.isFinite(p.y)
19270
+ )) {
19271
+ throw new Error("\u70B9\u5750\u6807\u5FC5\u987B\u4E3A\u6709\u9650\u6570\u503C");
19272
+ }
19273
+ if (!Number.isFinite(epsilon3) || epsilon3 < 0 || !Number.isInteger(binCount) || binCount < 2) {
19274
+ throw new Error("epsilon \u5FC5\u987B\u975E\u8D1F\uFF0CbinCount \u5FC5\u987B\u4E3A\u5927\u4E8E\u7B49\u4E8E 2 \u7684\u6574\u6570");
19275
+ }
19276
+ if (points.length < 2) {
19277
+ return points.map((p) => ({ x: p.x, y: p.y }));
19278
+ }
19279
+ const pts = points.map((p) => ({
19280
+ x: p.x,
19281
+ y: p.y
19282
+ }));
19283
+ const circle = fitCircle2D(pts);
19284
+ if (!circle || !Number.isFinite(circle.radius)) {
19285
+ throw new Error("\u5706\u5FC3\u62DF\u5408\u5931\u8D25\uFF0C\u65E0\u6CD5\u63D0\u53D6\u5F27\u5F62\u4E2D\u5FC3\u7EBF");
19286
+ }
19287
+ const cx = circle.center.x;
19288
+ const cy = circle.center.y;
19289
+ const polarPoints = pts.map((p) => {
19290
+ const dx = p.x - cx;
19291
+ const dy = p.y - cy;
19292
+ let angle = Math.atan2(dy, dx);
19293
+ if (angle < 0) angle += Math.PI * 2;
19294
+ return {
19295
+ angle,
19296
+ radius: Math.hypot(dx, dy)
19297
+ };
19298
+ });
19299
+ polarPoints.sort((a, b) => a.angle - b.angle);
19300
+ let maxGap = -1;
19301
+ let startIndex = 0;
19302
+ for (let i = 0; i < polarPoints.length; i++) {
19303
+ const next2 = (i + 1) % polarPoints.length;
19304
+ let gap = polarPoints[next2].angle - polarPoints[i].angle;
19305
+ if (next2 === 0) {
19306
+ gap += Math.PI * 2;
19307
+ }
19308
+ if (gap > maxGap) {
19309
+ maxGap = gap;
19310
+ startIndex = next2;
19311
+ }
19312
+ }
19313
+ const ordered = [];
19314
+ let previousAngle = polarPoints[startIndex].angle;
19315
+ for (let i = 0; i < polarPoints.length; i++) {
19316
+ const index = (startIndex + i) % polarPoints.length;
19317
+ const p = polarPoints[index];
19318
+ let angle = p.angle;
19319
+ if (i > 0 && angle < previousAngle) {
19320
+ angle += Math.PI * 2;
19321
+ }
19322
+ ordered.push({
19323
+ angle,
19324
+ radius: p.radius
19325
+ });
19326
+ previousAngle = angle;
19327
+ }
19328
+ const minAngle = ordered[0].angle;
19329
+ const maxAngle = ordered[ordered.length - 1].angle;
19330
+ const angleSpan = maxAngle - minAngle;
19331
+ if (angleSpan < 1e-8) {
19332
+ return [{ x: cx, y: cy }];
19333
+ }
19334
+ const bins = Array.from(
19335
+ { length: binCount },
19336
+ () => []
19337
+ );
19338
+ for (const p of ordered) {
19339
+ const t = (p.angle - minAngle) / angleSpan;
19340
+ const index = Math.min(
19341
+ binCount - 1,
19342
+ Math.floor(t * binCount)
19343
+ );
19344
+ bins[index].push(p);
19345
+ }
19346
+ const centerline = [];
19347
+ for (const bin of bins) {
19348
+ if (bin.length === 0) continue;
19349
+ const angles = bin.map((p) => p.angle);
19350
+ const radii = bin.map((p) => p.radius);
19351
+ const angle = medianValue(angles);
19352
+ const radius = medianValue(radii);
19353
+ centerline.push({
19354
+ x: cx + radius * Math.cos(angle),
19355
+ y: cy + radius * Math.sin(angle)
19356
+ });
19357
+ }
19358
+ const result = [];
19359
+ for (const p of centerline) {
19360
+ const last = result[result.length - 1];
19361
+ if (!last || Math.hypot(p.x - last.x, p.y - last.y) > 1e-10) {
19362
+ result.push(p);
19363
+ }
19364
+ }
19365
+ return rdp(result, epsilon3);
19366
+ }
19367
+ function medianValue(values) {
19368
+ if (values.length === 0) return 0;
19369
+ const sorted = values.slice().sort((a, b) => a - b);
19370
+ const mid = Math.floor(sorted.length / 2);
19371
+ if (sorted.length % 2 === 0) {
19372
+ return (sorted[mid - 1] + sorted[mid]) / 2;
19373
+ }
19374
+ return sorted[mid];
19375
+ }
19376
+ function segmentDistance2(p, a, b) {
19377
+ const dx = b.x - a.x, dy = b.y - a.y, length2 = dx * dx + dy * dy;
19378
+ const t = length2 ? Math.max(0, Math.min(1, ((p.x - a.x) * dx + (p.y - a.y) * dy) / length2)) : 0;
19379
+ return (p.x - a.x - t * dx) ** 2 + (p.y - a.y - t * dy) ** 2;
19380
+ }
19381
+ function rdp(points, epsilon3) {
19382
+ if (points.length < 3) return points.slice();
19383
+ const keep = /* @__PURE__ */ new Set([0, points.length - 1]), stack = [[0, points.length - 1]];
19384
+ while (stack.length) {
19385
+ const [a, b] = stack.pop();
19386
+ let max2 = epsilon3 * epsilon3, index = -1;
19387
+ for (let i = a + 1; i < b; i++) {
19388
+ const d = segmentDistance2(points[i], points[a], points[b]);
19389
+ if (d > max2) {
19390
+ max2 = d;
19391
+ index = i;
19392
+ }
19393
+ }
19394
+ if (index !== -1) {
19395
+ keep.add(index);
19396
+ stack.push([a, index], [index, b]);
19397
+ }
19398
+ }
19399
+ return [...keep].sort((a, b) => a - b).map((i) => points[i]);
19400
+ }
19401
+ if (typeof module !== "undefined" && module.exports) {
19402
+ module.exports = { fitPolyline, rdp };
19403
+ }
19404
+ function smoothPolyline(vertices, samplesPerSegment = 40) {
19405
+ if (!Number.isInteger(samplesPerSegment) || samplesPerSegment < 1) {
19406
+ throw new Error("samplesPerSegment \u5FC5\u987B\u4E3A\u6B63\u6574\u6570");
19407
+ }
19408
+ if (!Array.isArray(vertices) || vertices.some(
19409
+ (p) => !p || !Number.isFinite(p.x) || !Number.isFinite(p.y)
19410
+ )) {
19411
+ throw new Error("vertices \u5FC5\u987B\u4E3A\u6709\u6548 XY \u70B9\u6570\u7EC4");
19412
+ }
19413
+ const points = [];
19414
+ for (const p of vertices) {
19415
+ const last = points[points.length - 1];
19416
+ if (!last || Math.hypot(p.x - last.x, p.y - last.y) > 1e-12) {
19417
+ points.push({ x: p.x, y: p.y });
19418
+ }
19419
+ }
19420
+ if (points.length < 2) return points;
19421
+ const dt = (a, b) => Math.sqrt(Math.hypot(b.x - a.x, b.y - a.y));
19422
+ const extrapolate = (a, b) => ({ x: 2 * a.x - b.x, y: 2 * a.y - b.y });
19423
+ const blend = (a, b, ta, tb, t) => {
19424
+ const w = (t - ta) / (tb - ta);
19425
+ return { x: a.x * (1 - w) + b.x * w, y: a.y * (1 - w) + b.y * w };
19426
+ };
19427
+ const result = [{ ...points[0] }];
19428
+ for (let i = 0; i < points.length - 1; i++) {
19429
+ const p1 = points[i], p2 = points[i + 1];
19430
+ const p0 = i > 0 ? points[i - 1] : extrapolate(p1, p2);
19431
+ const p3 = i + 2 < points.length ? points[i + 2] : extrapolate(p2, p1);
19432
+ const t0 = 0, t1 = dt(p0, p1);
19433
+ const t2 = t1 + dt(p1, p2), t3 = t2 + dt(p2, p3);
19434
+ for (let j = 1; j <= samplesPerSegment; j++) {
19435
+ if (j === samplesPerSegment) {
19436
+ result.push({ ...p2 });
19437
+ continue;
19438
+ }
19439
+ const t = t1 + (t2 - t1) * j / samplesPerSegment;
19440
+ const a1 = blend(p0, p1, t0, t1, t);
19441
+ const a2 = blend(p1, p2, t1, t2, t);
19442
+ const a3 = blend(p2, p3, t2, t3, t);
19443
+ const b1 = blend(a1, a2, t0, t2, t);
19444
+ const b2 = blend(a2, a3, t1, t3, t);
19445
+ result.push(blend(b1, b2, t1, t2, t));
19446
+ }
19447
+ }
19448
+ return result;
19449
+ }
19450
+ if (typeof module !== "undefined" && module.exports) {
19451
+ module.exports.smoothPolyline = smoothPolyline;
19452
+ }
19453
+ function validateArc(vertices, options = {}) {
19454
+ const opts = {
19455
+ maxRelativeRms: 0.01,
19456
+ maxRelativeMax: 0.03,
19457
+ minTurnConsistency: 0.8,
19458
+ maxCurvatureCV: 0.8,
19459
+ minSweepDegrees: 10,
19460
+ maxSweepDegrees: 350,
19461
+ curvatureSamples: 9,
19462
+ ...options
19463
+ };
19464
+ if (!Array.isArray(vertices) || vertices.some((p2) => !p2 || !Number.isFinite(p2.x) || !Number.isFinite(p2.y))) {
19465
+ throw new Error("vertices \u5FC5\u987B\u4E3A\u6709\u6548 XY \u70B9\u6570\u7EC4");
19466
+ }
19467
+ if (!Number.isInteger(opts.curvatureSamples) || opts.curvatureSamples < 5)
19468
+ throw new Error("curvatureSamples \u5FC5\u987B\u4E3A\u81F3\u5C11 5 \u7684\u6574\u6570");
19469
+ for (const key of ["maxRelativeRms", "maxRelativeMax", "minTurnConsistency", "maxCurvatureCV", "minSweepDegrees", "maxSweepDegrees"]) {
19470
+ if (!Number.isFinite(opts[key]) || opts[key] < 0) throw new Error("\u9608\u503C\u5FC5\u987B\u4E3A\u975E\u8D1F\u6709\u9650\u6570\u503C");
19471
+ }
19472
+ if (opts.minTurnConsistency > 1 || opts.maxSweepDegrees < opts.minSweepDegrees)
19473
+ throw new Error("\u8F6C\u5411\u4E00\u81F4\u6027\u6216\u626B\u89D2\u9608\u503C\u65E0\u6548");
19474
+ const p = [];
19475
+ for (const q of vertices) {
19476
+ const last = p[p.length - 1];
19477
+ if (!last || Math.hypot(q.x - last.x, q.y - last.y) > 1e-12) p.push({ x: q.x, y: q.y });
19478
+ }
19479
+ if (p.length < 5) return { isArcLike: false, reason: "\u81F3\u5C11\u9700\u8981 5 \u4E2A\u4E0D\u540C\u7684\u6709\u5E8F\u9876\u70B9" };
19480
+ const circle = fitCircle2D(p);
19481
+ if (!circle) return { isArcLike: false, reason: "\u5706\u62DF\u5408\u9000\u5316\uFF1A\u70B9\u96C6\u53EF\u80FD\u63A5\u8FD1\u76F4\u7EBF" };
19482
+ const radialResiduals = p.map((q) => Math.hypot(q.x - circle.center.x, q.y - circle.center.y) - circle.radius);
19483
+ const rms = Math.sqrt(radialResiduals.reduce((s, v2) => s + v2 * v2, 0) / p.length);
19484
+ const max2 = Math.max(...radialResiduals.map(Math.abs));
19485
+ let sweep = 0;
19486
+ for (let i = 1; i < p.length; i++) {
19487
+ const a = Math.atan2(p[i - 1].y - circle.center.y, p[i - 1].x - circle.center.x);
19488
+ const b = Math.atan2(p[i].y - circle.center.y, p[i].x - circle.center.x);
19489
+ sweep += Math.atan2(Math.sin(b - a), Math.cos(b - a));
19490
+ }
19491
+ const sweepDegrees = Math.abs(sweep) * 180 / Math.PI;
19492
+ const coarsePoints = resampleByLength(p, opts.curvatureSamples);
19493
+ const coarse = curvatureMetrics(coarsePoints);
19494
+ const fine = curvatureMetrics(p);
19495
+ const checks = {
19496
+ radialRms: rms / circle.radius <= opts.maxRelativeRms,
19497
+ radialMax: max2 / circle.radius <= opts.maxRelativeMax,
19498
+ turning: coarse.turnConsistency >= opts.minTurnConsistency,
19499
+ curvature: Number.isFinite(coarse.curvatureCV) && coarse.curvatureCV <= opts.maxCurvatureCV,
19500
+ sweep: sweepDegrees >= opts.minSweepDegrees && sweepDegrees <= opts.maxSweepDegrees
19501
+ };
19502
+ const strictChecks = {
19503
+ radialRms: checks.radialRms,
19504
+ radialMax: checks.radialMax,
19505
+ turning: fine.turnConsistency >= 0.9,
19506
+ curvature: Number.isFinite(fine.curvatureCV) && fine.curvatureCV <= 0.25,
19507
+ sweep: checks.sweep
19508
+ };
19509
+ return {
19510
+ isArcLike: Object.values(checks).every(Boolean),
19511
+ passesStrictChecks: Object.values(strictChecks).every(Boolean),
19512
+ inputCount: p.length,
19513
+ center: circle.center,
19514
+ radius: circle.radius,
19515
+ radialRms: rms,
19516
+ relativeRms: rms / circle.radius,
19517
+ radialMax: max2,
19518
+ relativeMax: max2 / circle.radius,
19519
+ sweepDegrees,
19520
+ coarse,
19521
+ fine,
19522
+ checks,
19523
+ strictChecks,
19524
+ thresholds: opts,
19525
+ radialResiduals
19526
+ };
19527
+ }
19528
+ function fitCircle2D(points) {
19529
+ const n = points.length;
19530
+ const mx = points.reduce((s, p) => s + p.x, 0) / n;
19531
+ const my = points.reduce((s, p) => s + p.y, 0) / n;
19532
+ const scale3 = Math.max(...points.map((p) => Math.hypot(p.x - mx, p.y - my)));
19533
+ if (!(scale3 > 1e-12)) return null;
19534
+ const A = Array.from({ length: 3 }, () => [0, 0, 0]), b = [0, 0, 0];
19535
+ for (const p of points) {
19536
+ const x = (p.x - mx) / scale3, y = (p.y - my) / scale3;
19537
+ const row = [x, y, 1], target = -(x * x + y * y);
19538
+ for (let i = 0; i < 3; i++) {
19539
+ b[i] += row[i] * target;
19540
+ for (let j = 0; j < 3; j++) A[i][j] += row[i] * row[j];
19541
+ }
19542
+ }
19543
+ const sol = solveLinear3(A, b);
19544
+ if (!sol) return null;
19545
+ const [D2, E, F] = sol, r2 = (D2 * D2 + E * E) / 4 - F;
19546
+ if (!(r2 > 0)) return null;
19547
+ const result = { center: { x: mx - D2 * scale3 / 2, y: my - E * scale3 / 2 }, radius: Math.sqrt(r2) * scale3 };
19548
+ if (!Number.isFinite(result.radius) || !Number.isFinite(result.center.x) || !Number.isFinite(result.center.y)) return null;
19549
+ return result;
19550
+ }
19551
+ function solveLinear3(A, b) {
19552
+ const norm = Math.max(...A.flat().map(Math.abs));
19553
+ const m = A.map((r, i) => [...r, b[i]]);
19554
+ for (let j = 0; j < 3; j++) {
19555
+ let k = j;
19556
+ for (let i = j + 1; i < 3; i++) if (Math.abs(m[i][j]) > Math.abs(m[k][j])) k = i;
19557
+ if (Math.abs(m[k][j]) < norm * 1e-12) return null;
19558
+ [m[j], m[k]] = [m[k], m[j]];
19559
+ const pivot = m[j][j];
19560
+ for (let h = j; h < 4; h++) m[j][h] /= pivot;
19561
+ for (let i = 0; i < 3; i++) if (i !== j) {
19562
+ const f = m[i][j];
19563
+ for (let h = j; h < 4; h++) m[i][h] -= f * m[j][h];
19564
+ }
19565
+ }
19566
+ return m.map((r) => r[3]);
19567
+ }
19568
+ function resampleByLength(points, count = 161) {
19569
+ const lengths = [0];
19570
+ for (let i = 1; i < points.length; i++) lengths.push(lengths[i - 1] + Math.hypot(points[i].x - points[i - 1].x, points[i].y - points[i - 1].y));
19571
+ const total = lengths[lengths.length - 1];
19572
+ if (!(total > 0)) return [{ ...points[0] }];
19573
+ const result = [];
19574
+ let k = 1;
19575
+ for (let i = 0; i < count; i++) {
19576
+ const distance2 = total * i / (count - 1);
19577
+ while (k < points.length - 1 && lengths[k] < distance2) k++;
19578
+ const t = (distance2 - lengths[k - 1]) / (lengths[k] - lengths[k - 1]);
19579
+ result.push({ x: points[k - 1].x + (points[k].x - points[k - 1].x) * t, y: points[k - 1].y + (points[k].y - points[k - 1].y) * t });
19580
+ }
19581
+ return result;
19582
+ }
19583
+ function curvatureMetrics(points) {
19584
+ const curvature = [], turns = [];
19585
+ for (let i = 1; i < points.length - 1; i++) {
19586
+ const ax = points[i].x - points[i - 1].x, ay = points[i].y - points[i - 1].y;
19587
+ const bx = points[i + 1].x - points[i].x, by = points[i + 1].y - points[i].y;
19588
+ const a = Math.hypot(ax, ay), b = Math.hypot(bx, by);
19589
+ const c = Math.hypot(points[i + 1].x - points[i - 1].x, points[i + 1].y - points[i - 1].y);
19590
+ if (a * b * c < 1e-18) continue;
19591
+ curvature.push(2 * (ax * by - ay * bx) / (a * b * c));
19592
+ turns.push(Math.atan2(ax * by - ay * bx, ax * bx + ay * by));
19593
+ }
19594
+ const valid = turns.filter((t) => Math.abs(t) > 1e-7);
19595
+ const positive = valid.filter((t) => t > 0).length;
19596
+ const abs = curvature.map(Math.abs);
19597
+ const mean = abs.length ? abs.reduce((s, v2) => s + v2, 0) / abs.length : 0;
19598
+ const std = abs.length ? Math.sqrt(abs.reduce((s, v2) => s + (v2 - mean) ** 2, 0) / abs.length) : 0;
19599
+ return {
19600
+ sampleCount: points.length,
19601
+ turnConsistency: valid.length ? Math.max(positive, valid.length - positive) / valid.length : 0,
19602
+ curvatureMean: mean,
19603
+ curvatureCV: mean > 1e-12 ? std / mean : null
19604
+ };
19605
+ }
19606
+ if (typeof module !== "undefined" && module.exports) {
19607
+ Object.assign(module.exports, { validateArc, fitCircle2D, resampleByLength });
19608
+ }
19609
+ function fitLine2D(points) {
19610
+ const n = points.length;
19611
+ if (n < 2) return null;
19612
+ const mx = points.reduce((s, p) => s + p.x, 0) / n;
19613
+ const my = points.reduce((s, p) => s + p.y, 0) / n;
19614
+ let xx = 0, xy = 0, yy = 0;
19615
+ for (const p of points) {
19616
+ const x = p.x - mx, y = p.y - my;
19617
+ xx += x * x;
19618
+ xy += x * y;
19619
+ yy += y * y;
19620
+ }
19621
+ const angle = 0.5 * Math.atan2(2 * xy, xx - yy);
19622
+ const ux = Math.cos(angle), uy = Math.sin(angle);
19623
+ let lo = Infinity, hi = -Infinity, sum2 = 0, max2 = 0;
19624
+ for (const p of points) {
19625
+ const x = p.x - mx, y = p.y - my;
19626
+ const distance2 = Math.abs(-uy * x + ux * y), t = ux * x + uy * y;
19627
+ lo = Math.min(lo, t);
19628
+ hi = Math.max(hi, t);
19629
+ sum2 += distance2 * distance2;
19630
+ max2 = Math.max(max2, distance2);
19631
+ }
19632
+ const length2 = hi - lo;
19633
+ if (!(length2 > 1e-12)) return null;
19634
+ const rms = Math.sqrt(sum2 / n);
19635
+ return { center: { x: mx, y: my }, direction: { x: ux, y: uy }, length: length2, rms, max: max2, relativeRms: rms / length2, relativeMax: max2 / length2 };
19636
+ }
19637
+ function classifyPath(vertices, options = {}) {
19638
+ const {
19639
+ maxLineRelativeRms = 0.01,
19640
+ maxLineRelativeMax = 0.03,
19641
+ minArcRelativeBend = 0.02,
19642
+ ...arcOptions
19643
+ } = options;
19644
+ if (!Array.isArray(vertices) || vertices.some((p2) => !p2 || !Number.isFinite(p2.x) || !Number.isFinite(p2.y)))
19645
+ throw new Error("vertices \u5FC5\u987B\u662F\u6709\u6548 XY \u70B9\u6570\u7EC4");
19646
+ for (const value of [maxLineRelativeRms, maxLineRelativeMax, minArcRelativeBend])
19647
+ if (!Number.isFinite(value) || value < 0) throw new Error("\u76F4\u7EBF\u53CA\u5F2F\u66F2\u9608\u503C\u5FC5\u987B\u4E3A\u975E\u8D1F\u6709\u9650\u6570\u503C");
19648
+ const p = [];
19649
+ for (const q of vertices) {
19650
+ const last = p[p.length - 1];
19651
+ if (!last || Math.hypot(q.x - last.x, q.y - last.y) > 1e-12) p.push({ x: q.x, y: q.y });
19652
+ }
19653
+ if (p.length < 2) return { type: "degenerate", isArcLike: false, reason: "\u6709\u6548\u70B9\u4E0D\u8DB3" };
19654
+ const sampled = resampleByLength(p, 161);
19655
+ const line2 = fitLine2D(sampled);
19656
+ if (!line2) return { type: "degenerate", isArcLike: false, reason: "\u8DEF\u5F84\u957F\u5EA6\u8FC7\u5C0F" };
19657
+ for (const q of p) {
19658
+ const d = Math.abs(-line2.direction.y * (q.x - line2.center.x) + line2.direction.x * (q.y - line2.center.y));
19659
+ line2.max = Math.max(line2.max, d);
19660
+ }
19661
+ line2.relativeMax = line2.max / line2.length;
19662
+ const a = p[0], b = p[p.length - 1], dx = b.x - a.x, dy = b.y - a.y;
19663
+ const chordLength = Math.hypot(dx, dy);
19664
+ let maxChordDistance = 0;
19665
+ if (chordLength > 1e-12) {
19666
+ for (const q of p) maxChordDistance = Math.max(maxChordDistance, Math.abs(dx * (q.y - a.y) - dy * (q.x - a.x)) / chordLength);
19667
+ }
19668
+ const relativeBend = chordLength > 1e-12 ? maxChordDistance / chordLength : null;
19669
+ const nearLine = line2.relativeRms <= maxLineRelativeRms && line2.relativeMax <= maxLineRelativeMax;
19670
+ const lineThresholds = { maxLineRelativeRms, maxLineRelativeMax, minArcRelativeBend };
19671
+ const detail = { line: line2, chordLength, maxChordDistance: relativeBend === null ? null : maxChordDistance, relativeBend, lineThresholds };
19672
+ if (nearLine) return { type: "line", isArcLike: false, reason: "\u8DEF\u5F84\u5DF2\u6EE1\u8DB3\u8FD1\u4F3C\u76F4\u7EBF\u9608\u503C\uFF0C\u4F18\u5148\u5F52\u4E3A\u76F4\u7EBF", ...detail };
19673
+ if (relativeBend !== null && relativeBend < minArcRelativeBend)
19674
+ return { type: "shallow", isArcLike: false, reason: "\u5F2F\u66F2\u5E45\u5EA6\u4E0D\u8DB3\u4EE5\u8BA4\u5B9A\u4E3A\u5F27\u5F62", ...detail };
19675
+ const arc = validateArc(sampled, arcOptions);
19676
+ return { type: arc.isArcLike ? "arc" : "other", isArcLike: arc.isArcLike, reason: arc.isArcLike ? "\u901A\u8FC7\u8FD1\u4F3C\u5706\u5F27\u68C0\u67E5" : "\u4E0D\u6EE1\u8DB3\u5706\u5F27\u68C0\u67E5", ...detail, arc };
19677
+ }
19678
+ if (typeof module !== "undefined" && module.exports) {
19679
+ Object.assign(module.exports, { fitLine2D, classifyPath });
19680
+ }
19681
+ if (typeof module !== "undefined" && __require.main === module) {
19682
+ const fs = __require("fs");
19683
+ const opts = { maxRelativeRms: 0.02, maxRelativeMax: 0.05, minTurnConsistency: 0.7, maxCurvatureCV: 1, minSweepDegrees: 5, curvatureSamples: 9 };
19684
+ for (const filename of process.argv.slice(2)) {
19685
+ const raw = JSON.parse(fs.readFileSync(filename, "utf8"));
19686
+ const polyline = fitPolyline(raw, 5e-3, 32);
19687
+ const curve = resampleByLength(smoothPolyline(polyline, 200), 161);
19688
+ console.log(JSON.stringify({ filename, rawCount: raw.length, polyline: classifyPath(polyline, opts), curve: classifyPath(curve, opts) }, null, 2));
19689
+ }
19690
+ }
19691
+
19692
+ // src/utils/arc-segment-detector.js
19693
+ function isFinitePoint(point2) {
19694
+ return point2 && Number.isFinite(point2.x) && Number.isFinite(point2.y);
19695
+ }
19696
+ function getSegment(line2, index, options) {
19697
+ if (!isFinitePoint(line2?.start) || !isFinitePoint(line2?.end)) return null;
19698
+ const dx = line2.end.x - line2.start.x;
19699
+ const dy = line2.end.y - line2.start.y;
19700
+ const length2 = Math.hypot(dx, dy);
19701
+ if (length2 <= 1e-8) return null;
19702
+ const direction = { x: dx / length2, y: dy / length2 };
19703
+ const points = Array.isArray(line2.originalPoints) ? line2.originalPoints : [];
19704
+ let pointSupport = true;
19705
+ if (points.length) {
19706
+ const sampleCount = Math.min(points.length, options.maxPointSamples);
19707
+ let validCount = 0;
19708
+ let inlierCount = 0;
19709
+ for (let sample = 0; sample < sampleCount; sample++) {
19710
+ const pointIndex = sampleCount === 1 ? 0 : Math.floor(sample * (points.length - 1) / (sampleCount - 1));
19711
+ const point2 = points[pointIndex];
19712
+ if (!isFinitePoint(point2)) continue;
19713
+ validCount++;
19714
+ const distance2 = Math.abs(
19715
+ dx * (line2.start.y - point2.y) - (line2.start.x - point2.x) * dy
19716
+ ) / length2;
19717
+ if (distance2 <= options.maxPointLineDeviation) {
19718
+ inlierCount++;
19719
+ }
19720
+ }
19721
+ pointSupport = validCount >= 8 && inlierCount / validCount >= options.minPointLineInlierRatio;
19722
+ }
19723
+ return {
19724
+ index,
19725
+ start: line2.start,
19726
+ end: line2.end,
19727
+ midpoint: {
19728
+ x: (line2.start.x + line2.end.x) / 2,
19729
+ y: (line2.start.y + line2.end.y) / 2
19730
+ },
19731
+ direction,
19732
+ length: length2,
19733
+ pointSupport
19734
+ };
19735
+ }
19736
+ function endpointGap(a, b) {
19737
+ const endpointsA = [a.start, a.end];
19738
+ const endpointsB = [b.start, b.end];
19739
+ let gap = Infinity;
19740
+ for (const first of endpointsA) {
19741
+ for (const second of endpointsB) {
19742
+ const dz = Number.isFinite(first.z) && Number.isFinite(second.z) ? first.z - second.z : 0;
19743
+ gap = Math.min(
19744
+ gap,
19745
+ Math.hypot(
19746
+ first.x - second.x,
19747
+ first.y - second.y,
19748
+ dz
19749
+ )
19750
+ );
19751
+ }
19752
+ }
19753
+ return gap;
19754
+ }
19755
+ function directionAgreement(a, b) {
19756
+ return Math.abs(
19757
+ a.direction.x * b.direction.x + a.direction.y * b.direction.y
19758
+ );
19759
+ }
19760
+ function centerGap(a, b) {
19761
+ return Math.hypot(
19762
+ a.midpoint.x - b.midpoint.x,
19763
+ a.midpoint.y - b.midpoint.y
19764
+ );
19765
+ }
19766
+ function solve3(matrix, vector) {
19767
+ const rows = matrix.map((row, index) => [...row, vector[index]]);
19768
+ for (let column = 0; column < 3; column++) {
19769
+ let pivot = column;
19770
+ for (let row = column + 1; row < 3; row++) {
19771
+ if (Math.abs(rows[row][column]) > Math.abs(rows[pivot][column])) {
19772
+ pivot = row;
19773
+ }
19774
+ }
19775
+ if (Math.abs(rows[pivot][column]) < 1e-12) return null;
19776
+ [rows[column], rows[pivot]] = [rows[pivot], rows[column]];
19777
+ const divisor = rows[column][column];
19778
+ for (let j = column; j < 4; j++) {
19779
+ rows[column][j] /= divisor;
19780
+ }
19781
+ for (let row = 0; row < 3; row++) {
19782
+ if (row === column) continue;
19783
+ const factor = rows[row][column];
19784
+ for (let j = column; j < 4; j++) {
19785
+ rows[row][j] -= factor * rows[column][j];
19786
+ }
19787
+ }
19788
+ }
19789
+ return rows.map((row) => row[3]);
19790
+ }
19791
+ function fitCircle(points) {
19792
+ if (points.length < 3) return null;
19793
+ const centerX = points.reduce((sum2, point2) => sum2 + point2.x, 0) / points.length;
19794
+ const centerY = points.reduce((sum2, point2) => sum2 + point2.y, 0) / points.length;
19795
+ let xx = 0, xy = 0, x1 = 0;
19796
+ let yy = 0, y1 = 0, one = 0;
19797
+ let bx = 0, by = 0, b1 = 0;
19798
+ for (const point2 of points) {
19799
+ const x = point2.x - centerX;
19800
+ const y = point2.y - centerY;
19801
+ const x2y2 = x * x + y * y;
19802
+ xx += x * x;
19803
+ xy += x * y;
19804
+ x1 += x;
19805
+ yy += y * y;
19806
+ y1 += y;
19807
+ one++;
19808
+ bx -= x * x2y2;
19809
+ by -= y * x2y2;
19810
+ b1 -= x2y2;
19811
+ }
19812
+ const solution = solve3(
19813
+ [
19814
+ [xx, xy, x1],
19815
+ [xy, yy, y1],
19816
+ [x1, y1, one]
19817
+ ],
19818
+ [bx, by, b1]
19819
+ );
19820
+ if (!solution) return null;
19821
+ const [a, b, c] = solution;
19822
+ const radiusSquared = (a * a + b * b) / 4 - c;
19823
+ if (!(radiusSquared > 0)) return null;
19824
+ const circle = {
19825
+ center: {
19826
+ x: centerX - a / 2,
19827
+ y: centerY - b / 2
19828
+ },
19829
+ radius: Math.sqrt(radiusSquared)
19830
+ };
19831
+ return Number.isFinite(circle.radius) ? circle : null;
19832
+ }
19833
+ function evaluatePath(path3, segments, options) {
19834
+ if (path3.length < options.minSegments) return null;
19835
+ const pathSegments = path3.map((index) => segments[index]);
19836
+ if (pathSegments.some((segment) => !segment)) return null;
19837
+ const supportedSegments = pathSegments.filter(
19838
+ (segment) => segment.pointSupport
19839
+ ).length;
19840
+ if (supportedSegments / pathSegments.length < options.minPointSupportRatio) {
19841
+ return null;
19842
+ }
19843
+ for (let index = 1; index < pathSegments.length; index++) {
19844
+ const previous = pathSegments[index - 1];
19845
+ const current = pathSegments[index];
19846
+ const gap = endpointGap(previous, current);
19847
+ if (Number.isFinite(options.maxEndpointGapToMinSegmentLengthRatio)) {
19848
+ const minLength = Math.min(previous.length, current.length);
19849
+ if (gap > minLength * options.maxEndpointGapToMinSegmentLengthRatio) {
19850
+ return null;
19851
+ }
19852
+ }
19853
+ if (Number.isFinite(options.maxStepGapToSegmentLengthRatio)) {
19854
+ const meanLength = (previous.length + current.length) / 2;
19855
+ if (centerGap(previous, current) > meanLength * options.maxStepGapToSegmentLengthRatio) {
19856
+ return null;
19857
+ }
19858
+ }
19859
+ }
19860
+ const centers = pathSegments.map((segment) => segment.midpoint);
19861
+ const circle = fitCircle(centers);
19862
+ if (!circle || circle.radius < options.minRadius || circle.radius > options.maxRadius) {
19863
+ return null;
19864
+ }
19865
+ const residuals = centers.map(
19866
+ (point2) => Math.hypot(
19867
+ point2.x - circle.center.x,
19868
+ point2.y - circle.center.y
19869
+ ) - circle.radius
19870
+ );
19871
+ const rms = Math.sqrt(
19872
+ residuals.reduce((sum2, value) => sum2 + value * value, 0) / residuals.length
19873
+ );
19874
+ const maxResidual = Math.max(...residuals.map(Math.abs));
19875
+ if (rms > options.maxRadialRms || maxResidual > options.maxRadialMax || rms / circle.radius > options.maxRelativeRms) {
19876
+ return null;
19877
+ }
19878
+ const angles = centers.map(
19879
+ (point2) => Math.atan2(
19880
+ point2.y - circle.center.y,
19881
+ point2.x - circle.center.x
19882
+ )
19883
+ );
19884
+ const turns = [];
19885
+ for (let index = 1; index < angles.length; index++) {
19886
+ const delta = angles[index] - angles[index - 1];
19887
+ turns.push(
19888
+ Math.atan2(Math.sin(delta), Math.cos(delta))
19889
+ );
19890
+ }
19891
+ const significantTurns = turns.filter(
19892
+ (turn) => Math.abs(turn) >= options.minTurnRadians
19893
+ );
19894
+ if (!significantTurns.length) return null;
19895
+ const turnSign = Math.sign(significantTurns[0]);
19896
+ const consistentTurns = significantTurns.filter(
19897
+ (turn) => Math.sign(turn) === turnSign
19898
+ ).length;
19899
+ const turnConsistency = consistentTurns / significantTurns.length;
19900
+ if (turnConsistency < options.minTurnConsistency) return null;
19901
+ if (turns.some((turn) => Math.abs(turn) > options.maxStepTurnRadians)) {
19902
+ return null;
19903
+ }
19904
+ const sweepRadians = Math.abs(
19905
+ turns.reduce((sum2, turn) => sum2 + turn, 0)
19906
+ );
19907
+ const sweepDegrees = sweepRadians * 180 / Math.PI;
19908
+ if (sweepDegrees < options.minSweepDegrees || sweepDegrees > options.maxSweepDegrees) {
19909
+ return null;
19910
+ }
19911
+ let tangentAgreement = 0;
19912
+ for (const segment of pathSegments) {
19913
+ const dx = segment.midpoint.x - circle.center.x;
19914
+ const dy = segment.midpoint.y - circle.center.y;
19915
+ const radius = Math.hypot(dx, dy);
19916
+ if (radius <= 1e-12) return null;
19917
+ const tangent = {
19918
+ x: -dy / radius,
19919
+ y: dx / radius
19920
+ };
19921
+ tangentAgreement += Math.abs(
19922
+ segment.direction.x * tangent.x + segment.direction.y * tangent.y
19923
+ );
19924
+ }
19925
+ tangentAgreement /= pathSegments.length;
19926
+ if (tangentAgreement < options.minTangentAgreement) {
19927
+ return null;
19928
+ }
19929
+ const minX = Math.min(...centers.map((point2) => point2.x));
19930
+ const maxX = Math.max(...centers.map((point2) => point2.x));
19931
+ const minY = Math.min(...centers.map((point2) => point2.y));
19932
+ const maxY = Math.max(...centers.map((point2) => point2.y));
19933
+ const span = Math.hypot(maxX - minX, maxY - minY);
19934
+ if (!(span > 0) || circle.radius > span * options.maxRadiusToSpan) {
19935
+ return null;
19936
+ }
19937
+ const meanX = centers.reduce((sum2, point2) => sum2 + point2.x, 0) / centers.length;
19938
+ const meanY = centers.reduce((sum2, point2) => sum2 + point2.y, 0) / centers.length;
19939
+ let xx = 0, xy = 0, yy = 0;
19940
+ for (const point2 of centers) {
19941
+ const x = point2.x - meanX;
19942
+ const y = point2.y - meanY;
19943
+ xx += x * x;
19944
+ xy += x * y;
19945
+ yy += y * y;
19946
+ }
19947
+ const smallestEigenvalue = Math.max(
19948
+ 0,
19949
+ (xx + yy - Math.sqrt((xx - yy) ** 2 + 4 * xy * xy)) / 2
19950
+ );
19951
+ const lineRms = Math.sqrt(smallestEigenvalue / centers.length);
19952
+ if (lineRms < span * options.minLineDeviationRatio || lineRms < rms * options.minLineToCircleRmsRatio) {
19953
+ return null;
19954
+ }
19955
+ const shape = classifyPath(centers, {
19956
+ maxLineRelativeRms: 0.01,
19957
+ maxLineRelativeMax: 0.03,
19958
+ minArcRelativeBend: 0.02,
19959
+ maxRelativeRms: 0.03,
19960
+ maxRelativeMax: 0.08,
19961
+ minTurnConsistency: 0.8,
19962
+ maxCurvatureCV: 1.5,
19963
+ minSweepDegrees: 15,
19964
+ maxSweepDegrees: 190,
19965
+ curvatureSamples: 9
19966
+ });
19967
+ if (!shape.isArcLike) return null;
19968
+ return {
19969
+ segmentIndices: pathSegments.map((segment) => segment.index),
19970
+ center: circle.center,
19971
+ radius: circle.radius,
19972
+ sweepDegrees,
19973
+ radialRms: rms,
19974
+ relativeRms: rms / circle.radius,
19975
+ radialMax: maxResidual,
19976
+ turnConsistency,
19977
+ tangentAgreement
19978
+ };
19979
+ }
19980
+ function buildAdjacency(segments, lineCount, options) {
19981
+ const valid = segments.map((segment, index) => segment ? index : -1).filter((index) => index >= 0);
19982
+ const adjacency = Array.from(
19983
+ { length: lineCount },
19984
+ () => []
19985
+ );
19986
+ for (let position = 0; position < valid.length; position++) {
19987
+ const firstIndex = valid[position];
19988
+ const first = segments[firstIndex];
19989
+ for (let next2 = position + 1; next2 < valid.length; next2++) {
19990
+ const secondIndex = valid[next2];
19991
+ const second = segments[secondIndex];
19992
+ if (centerGap(first, second) > options.maxCenterGap) continue;
19993
+ if (endpointGap(first, second) > options.maxNeighborGap) continue;
19994
+ if (directionAgreement(first, second) < options.minDirectionAgreement) {
19995
+ continue;
19996
+ }
19997
+ adjacency[firstIndex].push(secondIndex);
19998
+ adjacency[secondIndex].push(firstIndex);
19999
+ }
20000
+ }
20001
+ for (const index of valid) {
20002
+ adjacency[index].sort(
20003
+ (a, b) => directionAgreement(segments[index], segments[b]) - directionAgreement(segments[index], segments[a]) || centerGap(segments[index], segments[a]) - centerGap(segments[index], segments[b])
20004
+ );
20005
+ }
20006
+ return adjacency;
20007
+ }
20008
+ function findArcCandidates(segments, adjacency, options, claimed = /* @__PURE__ */ new Set(), allowClaimedTraversal = false) {
20009
+ const candidates = /* @__PURE__ */ new Map();
20010
+ let evaluatedPaths = 0;
20011
+ for (let start = 0; start < segments.length && evaluatedPaths < options.maxPathEvaluations; start++) {
20012
+ if (claimed.has(start)) continue;
20013
+ if (!segments[start]) continue;
20014
+ const path3 = [start];
20015
+ const visited = new Set(path3);
20016
+ let pathsForStart = 0;
20017
+ const visit = () => {
20018
+ if (path3.length >= options.minSegments) {
20019
+ evaluatedPaths++;
20020
+ pathsForStart++;
20021
+ const hasNewSegment = path3.some((index) => !claimed.has(index));
20022
+ if (hasNewSegment) {
20023
+ const result = evaluatePath(path3, segments, options);
20024
+ if (result) {
20025
+ const key = result.segmentIndices.slice().sort((a, b) => a - b).join(",");
20026
+ const previous = candidates.get(key);
20027
+ if (!previous || result.relativeRms < previous.relativeRms || result.relativeRms === previous.relativeRms && result.sweepDegrees > previous.sweepDegrees) {
20028
+ candidates.set(key, result);
20029
+ }
20030
+ }
20031
+ }
20032
+ }
20033
+ if (path3.length >= options.maxSegments || pathsForStart >= options.maxPathsPerStart || evaluatedPaths >= options.maxPathEvaluations) {
20034
+ return;
20035
+ }
20036
+ const last = path3[path3.length - 1];
20037
+ for (const next2 of adjacency[last]) {
20038
+ if (visited.has(next2)) continue;
20039
+ if (!allowClaimedTraversal && claimed.has(next2)) continue;
20040
+ visited.add(next2);
20041
+ path3.push(next2);
20042
+ visit();
20043
+ path3.pop();
20044
+ visited.delete(next2);
20045
+ if (pathsForStart >= options.maxPathsPerStart || evaluatedPaths >= options.maxPathEvaluations) {
20046
+ return;
20047
+ }
20048
+ }
20049
+ };
20050
+ visit();
20051
+ }
20052
+ return [...candidates.values()];
20053
+ }
20054
+ function normalizeAngle(angle) {
20055
+ const full = Math.PI * 2;
20056
+ return (angle % full + full) % full;
20057
+ }
20058
+ function orderIndicesByArc(indices, group, segments) {
20059
+ const uniqueIndices = [...new Set(indices)];
20060
+ if (uniqueIndices.length <= 2) return uniqueIndices;
20061
+ const oldIndices = group.segmentIndices;
20062
+ const firstSegment = segments[oldIndices[0]];
20063
+ if (!firstSegment) return uniqueIndices;
20064
+ const firstAngle = Math.atan2(
20065
+ firstSegment.midpoint.y - group.center.y,
20066
+ firstSegment.midpoint.x - group.center.x
20067
+ );
20068
+ let direction = 1;
20069
+ if (oldIndices.length >= 2) {
20070
+ const secondSegment = segments[oldIndices[1]];
20071
+ if (secondSegment) {
20072
+ const secondAngle = Math.atan2(
20073
+ secondSegment.midpoint.y - group.center.y,
20074
+ secondSegment.midpoint.x - group.center.x
20075
+ );
20076
+ const delta = Math.atan2(
20077
+ Math.sin(secondAngle - firstAngle),
20078
+ Math.cos(secondAngle - firstAngle)
20079
+ );
20080
+ direction = delta >= 0 ? 1 : -1;
20081
+ }
20082
+ }
20083
+ return uniqueIndices.filter((index) => segments[index]).sort((a, b) => {
20084
+ const segmentA = segments[a];
20085
+ const segmentB = segments[b];
20086
+ const angleA = Math.atan2(
20087
+ segmentA.midpoint.y - group.center.y,
20088
+ segmentA.midpoint.x - group.center.x
20089
+ );
20090
+ const angleB = Math.atan2(
20091
+ segmentB.midpoint.y - group.center.y,
20092
+ segmentB.midpoint.x - group.center.x
20093
+ );
20094
+ const progressA = direction > 0 ? normalizeAngle(angleA - firstAngle) : normalizeAngle(firstAngle - angleA);
20095
+ const progressB = direction > 0 ? normalizeAngle(angleB - firstAngle) : normalizeAngle(firstAngle - angleB);
20096
+ return progressA - progressB;
20097
+ });
20098
+ }
20099
+ function matchExistingGroup(candidate, group, options) {
20100
+ const candidateSet = new Set(candidate.segmentIndices);
20101
+ const sharedIndices = group.segmentIndices.filter(
20102
+ (index) => candidateSet.has(index)
20103
+ );
20104
+ const overlapRatio = sharedIndices.length / Math.min(candidate.segmentIndices.length, group.segmentIndices.length);
20105
+ if (overlapRatio < options.minMergeOverlapRatio) {
20106
+ return null;
20107
+ }
20108
+ const centerDistance = Math.hypot(
20109
+ candidate.center.x - group.center.x,
20110
+ candidate.center.y - group.center.y
20111
+ );
20112
+ const radiusDifference = Math.abs(
20113
+ candidate.radius - group.radius
20114
+ );
20115
+ if (centerDistance > options.maxMergeCenterDistance) {
20116
+ return null;
20117
+ }
20118
+ if (radiusDifference > options.maxMergeRadiusDifference) {
20119
+ return null;
20120
+ }
20121
+ return {
20122
+ overlapRatio,
20123
+ centerDistance,
20124
+ radiusDifference,
20125
+ score: overlapRatio * 10 - centerDistance - radiusDifference
20126
+ };
20127
+ }
20128
+ function mergeCandidateIntoGroup(candidate, group, segments, options) {
20129
+ const mergedIndices = [
20130
+ .../* @__PURE__ */ new Set([
20131
+ ...group.segmentIndices,
20132
+ ...candidate.segmentIndices
20133
+ ])
20134
+ ];
20135
+ const orderedIndices = orderIndicesByArc(
20136
+ mergedIndices,
20137
+ group,
20138
+ segments
20139
+ );
20140
+ const mergedResult = evaluatePath(
20141
+ orderedIndices,
20142
+ segments,
20143
+ options
20144
+ );
20145
+ if (mergedResult) {
20146
+ group.segmentIndices = mergedResult.segmentIndices;
20147
+ group.center = mergedResult.center;
20148
+ group.radius = mergedResult.radius;
20149
+ group.sweepDegrees = mergedResult.sweepDegrees;
20150
+ group.radialRms = mergedResult.radialRms;
20151
+ group.relativeRms = mergedResult.relativeRms;
20152
+ group.radialMax = mergedResult.radialMax;
20153
+ group.turnConsistency = mergedResult.turnConsistency;
20154
+ group.tangentAgreement = mergedResult.tangentAgreement;
20155
+ return true;
20156
+ }
20157
+ return false;
20158
+ }
20159
+ function detectArcSegmentGroups(lines, options = {}) {
20160
+ const opts = {
20161
+ maxNeighborGap: 0.1,
20162
+ maxCenterGap: 2.75,
20163
+ minDirectionAgreement: 0.65,
20164
+ minSegments: 4,
20165
+ maxSegments: 6,
20166
+ minRadius: 0.3,
20167
+ maxRadius: 100,
20168
+ maxRadiusToSpan: 12,
20169
+ minLineDeviationRatio: 0.02,
20170
+ minLineToCircleRmsRatio: 4,
20171
+ maxRadialRms: 0.05,
20172
+ maxRadialMax: 0.09,
20173
+ maxRelativeRms: 0.015,
20174
+ minSweepDegrees: 12,
20175
+ maxSweepDegrees: 190,
20176
+ minTurnConsistency: 0.8,
20177
+ minTurnRadians: 0.5 * Math.PI / 180,
20178
+ maxStepTurnRadians: 65 * Math.PI / 180,
20179
+ minTangentAgreement: 0.9,
20180
+ maxPointLineDeviation: 0.1,
20181
+ minPointLineInlierRatio: 0.9,
20182
+ minPointSupportRatio: 0.85,
20183
+ maxPointSamples: 128,
20184
+ maxPathEvaluations: 4e5,
20185
+ maxPathsPerStart: 4e3,
20186
+ // 合并判断参数
20187
+ minMergeOverlapRatio: 0.5,
20188
+ maxMergeCenterDistance: 0.1,
20189
+ maxMergeRadiusDifference: 0.08,
20190
+ ...options
20191
+ };
20192
+ if (!Array.isArray(lines)) {
20193
+ throw new Error("lines \u5FC5\u987B\u662F\u6570\u7EC4");
20194
+ }
20195
+ if (!Number.isFinite(opts.maxNeighborGap) || opts.maxNeighborGap < 0 || !Number.isFinite(opts.maxCenterGap) || opts.maxCenterGap <= 0 || !Number.isFinite(opts.minDirectionAgreement) || opts.minDirectionAgreement < 0 || opts.minDirectionAgreement > 1 || !Number.isFinite(opts.minLineDeviationRatio) || opts.minLineDeviationRatio < 0 || !Number.isFinite(opts.minLineToCircleRmsRatio) || opts.minLineToCircleRmsRatio < 0 || !Number.isFinite(opts.maxPointLineDeviation) || opts.maxPointLineDeviation < 0 || !Number.isFinite(opts.minPointLineInlierRatio) || opts.minPointLineInlierRatio < 0 || opts.minPointLineInlierRatio > 1 || !Number.isFinite(opts.minPointSupportRatio) || opts.minPointSupportRatio < 0 || opts.minPointSupportRatio > 1 || !Number.isInteger(opts.maxPointSamples) || opts.maxPointSamples < 1 || !Number.isInteger(opts.minSegments) || opts.minSegments < 3 || !Number.isInteger(opts.maxSegments) || opts.maxSegments < opts.minSegments || !Number.isInteger(opts.maxPathEvaluations) || opts.maxPathEvaluations < 1 || !Number.isInteger(opts.maxPathsPerStart) || opts.maxPathsPerStart < 1) {
20196
+ throw new Error("\u5F27\u6BB5\u68C0\u6D4B\u53C2\u6570\u65E0\u6548");
20197
+ }
20198
+ const segments = lines.map(
20199
+ (line2, index) => getSegment(line2, index, opts)
20200
+ );
20201
+ const sortCandidates = (candidates) => candidates.sort(
20202
+ (a, b) => b.segmentIndices.length - a.segmentIndices.length || a.relativeRms - b.relativeRms || b.sweepDegrees - a.sweepDegrees
20203
+ );
20204
+ const groups = [];
20205
+ const claimed = /* @__PURE__ */ new Set();
20206
+ const addUnclaimedCandidates = (candidates) => {
20207
+ for (const candidate of sortCandidates(candidates)) {
20208
+ if (candidate.segmentIndices.some((index) => claimed.has(index))) {
20209
+ continue;
20210
+ }
20211
+ groups.push(candidate);
20212
+ for (const index of candidate.segmentIndices) {
20213
+ claimed.add(index);
20214
+ }
20215
+ }
20216
+ };
20217
+ const mergeOrAddCandidates = (candidates, mergeOptions) => {
20218
+ for (const candidate of sortCandidates(candidates)) {
20219
+ const newIndices = candidate.segmentIndices.filter(
20220
+ (index) => !claimed.has(index)
20221
+ );
20222
+ if (!newIndices.length) continue;
20223
+ let bestMatch = null;
20224
+ let bestGroup = null;
20225
+ for (const group of groups) {
20226
+ const match = matchExistingGroup(
20227
+ candidate,
20228
+ group,
20229
+ mergeOptions
20230
+ );
20231
+ if (!match) continue;
20232
+ if (!bestMatch || match.score > bestMatch.score) {
20233
+ bestMatch = match;
20234
+ bestGroup = group;
20235
+ }
20236
+ }
20237
+ if (bestGroup) {
20238
+ const merged = mergeCandidateIntoGroup(
20239
+ candidate,
20240
+ bestGroup,
20241
+ segments,
20242
+ mergeOptions
20243
+ );
20244
+ if (merged) {
20245
+ for (const index of candidate.segmentIndices) {
20246
+ claimed.add(index);
20247
+ }
20248
+ }
20249
+ continue;
20250
+ }
20251
+ const overlapsClaimed = candidate.segmentIndices.some(
20252
+ (index) => claimed.has(index)
20253
+ );
20254
+ if (overlapsClaimed) continue;
20255
+ groups.push(candidate);
20256
+ for (const index of candidate.segmentIndices) {
20257
+ claimed.add(index);
20258
+ }
20259
+ }
20260
+ };
20261
+ const primaryAdjacency = buildAdjacency(
20262
+ segments,
20263
+ lines.length,
20264
+ opts
20265
+ );
20266
+ const primaryCandidates = findArcCandidates(
20267
+ segments,
20268
+ primaryAdjacency,
20269
+ opts,
20270
+ claimed,
20271
+ false
20272
+ );
20273
+ addUnclaimedCandidates(primaryCandidates);
20274
+ const fallbackOptions = {
20275
+ ...opts,
20276
+ maxNeighborGap: 0.2,
20277
+ maxCenterGap: 1.5,
20278
+ minDirectionAgreement: 0.65,
20279
+ minSegments: Math.max(opts.minSegments, 5),
20280
+ maxRadialRms: 0.055,
20281
+ maxRadialMax: 0.095,
20282
+ maxRelativeRms: 0.02,
20283
+ minSweepDegrees: 25,
20284
+ maxStepGapToSegmentLengthRatio: 1.2,
20285
+ maxEndpointGapToMinSegmentLengthRatio: 0.5
20286
+ };
20287
+ if (fallbackOptions.minSegments <= fallbackOptions.maxSegments) {
20288
+ const fallbackAdjacency = buildAdjacency(
20289
+ segments,
20290
+ lines.length,
20291
+ fallbackOptions
20292
+ );
20293
+ const fallbackCandidates = findArcCandidates(
20294
+ segments,
20295
+ fallbackAdjacency,
20296
+ fallbackOptions,
20297
+ claimed,
20298
+ true
20299
+ );
20300
+ mergeOrAddCandidates(
20301
+ fallbackCandidates,
20302
+ fallbackOptions
20303
+ );
20304
+ }
20305
+ return groups;
20306
+ }
20307
+ if (typeof module !== "undefined" && module.exports) {
20308
+ module.exports = { detectArcSegmentGroups };
20309
+ }
20310
+
20311
+ // src/geometry/wallLine.mjs
20312
+ var getArcWallLine = (lines) => {
20313
+ const arcGroups = detectArcSegmentGroups(lines);
20314
+ console.log(arcGroups);
20315
+ const arc = [];
20316
+ const pathArc = [];
20317
+ let delIndex = [];
20318
+ for (let i = 0; i < arcGroups.length; i++) {
20319
+ if (delIndex.includes(i))
20320
+ continue;
20321
+ const indexs1 = arcGroups[i].segmentIndices;
20322
+ let lastIndex1 = indexs1.length - 1;
20323
+ let firstLineStPt1 = getPt3(lines[indexs1[0]].start);
20324
+ let firstLineEdPt1 = getPt3(lines[indexs1[0]].end);
20325
+ let lastLineStPt1 = getPt3(lines[indexs1[lastIndex1]].start);
20326
+ let lastLineEdPt1 = getPt3(lines[indexs1[lastIndex1]].end);
20327
+ for (let j = 0; j < arcGroups.length; j++) {
20328
+ if (i == j || delIndex.includes(j))
20329
+ continue;
20330
+ const indexs2 = arcGroups[j].segmentIndices;
20331
+ let lastIndex2 = indexs2.length - 1;
20332
+ let firstLineStPt2 = getPt3(lines[indexs2[0]].start);
20333
+ let firstLineEdPt2 = getPt3(lines[indexs2[0]].end);
20334
+ let lastLineStPt2 = getPt3(lines[indexs2[lastIndex2]].start);
20335
+ let lastLineEdPt2 = getPt3(lines[indexs2[lastIndex2]].end);
20336
+ if (lineAndLineIsNear(firstLineStPt1, firstLineEdPt1, firstLineStPt2, firstLineEdPt2)) {
20337
+ indexs2.reverse();
20338
+ let newIndexs = indexs2;
20339
+ newIndexs.push(...indexs1);
20340
+ delIndex.push(i);
20341
+ delIndex.push(j);
20342
+ arcGroups.push({
20343
+ segmentIndices: newIndexs
20344
+ });
20345
+ break;
20346
+ } else if (lineAndLineIsNear(firstLineStPt1, firstLineEdPt1, lastLineStPt2, lastLineEdPt2)) {
20347
+ indexs2.reverse();
20348
+ let newIndexs = indexs2;
20349
+ newIndexs.push(...indexs1);
20350
+ delIndex.push(i);
20351
+ delIndex.push(j);
20352
+ arcGroups.push({
20353
+ segmentIndices: newIndexs
20354
+ });
20355
+ break;
20356
+ } else if (lineAndLineIsNear(lastLineStPt1, lastLineEdPt1, firstLineStPt2, firstLineEdPt2)) {
20357
+ let newIndexs = indexs1;
20358
+ newIndexs.push(...indexs2);
20359
+ delIndex.push(i);
20360
+ delIndex.push(j);
20361
+ arcGroups.push({
20362
+ segmentIndices: newIndexs
20363
+ });
20364
+ break;
20365
+ } else if (lineAndLineIsNear(lastLineStPt1, lastLineEdPt1, lastLineStPt2, lastLineEdPt2)) {
20366
+ indexs2.reverse();
20367
+ let newIndexs = indexs1;
20368
+ newIndexs.push(...indexs2);
20369
+ delIndex.push(i);
20370
+ delIndex.push(j);
20371
+ arcGroups.push({
20372
+ segmentIndices: newIndexs
20373
+ });
20374
+ break;
20375
+ }
20376
+ }
20377
+ }
20378
+ let mergeIndexs = [];
20379
+ for (let i = 0; i < arcGroups.length; i++) {
20380
+ if (!delIndex.includes(i))
20381
+ mergeIndexs.push(arcGroups[i]);
20382
+ }
20383
+ console.log(mergeIndexs);
20384
+ for (let i = 0; i < mergeIndexs.length; i++) {
20385
+ const acceptedArcSegments = mergeIndexs[i].segmentIndices;
20386
+ let pts = [];
20387
+ let rooftopPz = -99999;
20388
+ let floorZ = 99999;
20389
+ for (const index of acceptedArcSegments) {
20390
+ const line2 = lines[index];
20391
+ line2.isArc = true;
20392
+ for (let j = 0; j < line2.originalPoints.length; j++)
20393
+ pts.push(line2.originalPoints[j]);
20394
+ if (line2.rooftopPz > rooftopPz)
20395
+ rooftopPz = line2.rooftopPz;
20396
+ if (line2.start.z < floorZ)
20397
+ floorZ = line2.start.z;
20398
+ }
20399
+ const polyline = fitPolyline(pts, 5e-3, 32);
20400
+ const curve = resampleByLength(
20401
+ smoothPolyline(polyline, 200),
20402
+ 161
20403
+ );
20404
+ const threeData = curve.map((p) => new THREE12.Vector3(p.x, p.y, 0));
20405
+ pathArc.push(threeData);
20406
+ const points2D = threeData.map(
20407
+ (p) => new THREE12.Vector2(p.x, p.y)
20408
+ );
20409
+ const result = fitMultiCubicBezier2D(points2D, {
20410
+ tolerance: 5e-3,
20411
+ minPoints: 6,
20412
+ maxDepth: 20,
20413
+ maxSegments: 100
20414
+ });
20415
+ if (result.success) {
20416
+ for (let j = 0; j < result.segmentCount; j++) {
20417
+ if (result.segments[j].controlPoints.length != 4)
20418
+ continue;
20419
+ const [P0, P1, P2, P3] = result.segments[j].controlPoints;
20420
+ arc.push({
20421
+ start: { x: P0.x, y: P0.y, z: floorZ },
20422
+ end: { x: P3.x, y: P3.y, z: floorZ },
20423
+ Shape: {
20424
+ type: "cubic-bezier",
20425
+ controlPointStart: { x: P1.x, y: P1.y, z: floorZ },
20426
+ controlPointEnd: { x: P2.x, y: P2.y, z: floorZ }
20427
+ },
20428
+ rooftopPz
20429
+ });
20430
+ }
20431
+ }
20432
+ }
20433
+ return arc;
20434
+ };
20435
+ function lineAndLineIsNear(pt1, pt2, pt3, pt4) {
20436
+ if (ptIsEqual(pt1, pt3) || ptIsEqual(pt1, pt4) || ptIsEqual(pt2, pt3) || ptIsEqual(pt2, pt4))
20437
+ return true;
20438
+ if (isPointOnLine2(pt1, pt2, pt3, 0.01, true) || isPointOnLine2(pt1, pt2, pt4, 0.01, true) || isPointOnLine2(pt3, pt4, pt1, 0.01, true) || isPointOnLine2(pt3, pt4, pt2, 0.01, true))
20439
+ return true;
20440
+ let pt = segmentsIntersect2D(pt1, pt2, pt3, pt4, 0.01);
20441
+ if (pt != null) {
20442
+ return true;
20443
+ }
20444
+ return false;
20445
+ }
20446
+ function evaluateCubicBezier(P0, P1, P2, P3, t) {
20447
+ const mt = 1 - t;
20448
+ const mt2 = mt * mt;
20449
+ const t2 = t * t;
20450
+ return new THREE12.Vector2(
20451
+ mt2 * mt * P0.x + 3 * mt2 * t * P1.x + 3 * mt * t2 * P2.x + t2 * t * P3.x,
20452
+ mt2 * mt * P0.y + 3 * mt2 * t * P1.y + 3 * mt * t2 * P2.y + t2 * t * P3.y
20453
+ );
20454
+ }
20455
+ function fitSingleBezier(points) {
20456
+ const n = points.length;
20457
+ if (n < 2) return null;
20458
+ const P0 = points[0].clone();
20459
+ const P3 = points[n - 1].clone();
20460
+ const ts = [0];
20461
+ let totalLength = 0;
20462
+ for (let i = 1; i < n; i++) {
20463
+ totalLength += points[i].distanceTo(points[i - 1]);
20464
+ ts.push(totalLength);
20465
+ }
20466
+ if (totalLength < 1e-12) return null;
20467
+ for (let i = 1; i < n; i++) {
20468
+ ts[i] /= totalLength;
20469
+ }
20470
+ let C00 = 0;
20471
+ let C01 = 0;
20472
+ let C11 = 0;
20473
+ let X0 = 0;
20474
+ let X1 = 0;
20475
+ let Y0 = 0;
20476
+ let Y1 = 0;
20477
+ for (let i = 0; i < n; i++) {
20478
+ const t = ts[i];
20479
+ const mt = 1 - t;
20480
+ const a = 3 * mt * mt * t;
20481
+ const b = 3 * mt * t * t;
20482
+ const c = mt * mt * mt;
20483
+ const d = t * t * t;
20484
+ const x = points[i].x - c * P0.x - d * P3.x;
20485
+ const y = points[i].y - c * P0.y - d * P3.y;
20486
+ C00 += a * a;
20487
+ C01 += a * b;
20488
+ C11 += b * b;
20489
+ X0 += a * x;
20490
+ X1 += b * x;
20491
+ Y0 += a * y;
20492
+ Y1 += b * y;
20493
+ }
20494
+ const det = C00 * C11 - C01 * C01;
20495
+ let P1;
20496
+ let P2;
20497
+ if (Math.abs(det) > 1e-12) {
20498
+ const invDet = 1 / det;
20499
+ P1 = new THREE12.Vector2(
20500
+ (X0 * C11 - X1 * C01) * invDet,
20501
+ (Y0 * C11 - Y1 * C01) * invDet
20502
+ );
20503
+ P2 = new THREE12.Vector2(
20504
+ (X1 * C00 - X0 * C01) * invDet,
20505
+ (Y1 * C00 - Y0 * C01) * invDet
20506
+ );
20507
+ } else {
20508
+ P1 = P0.clone().lerp(P3, 1 / 3);
20509
+ P2 = P0.clone().lerp(P3, 2 / 3);
20510
+ }
20511
+ let errorSum = 0;
20512
+ let maxError = 0;
20513
+ let maxErrorIndex = 0;
20514
+ const fittedPoints = [];
20515
+ for (let i = 0; i < n; i++) {
20516
+ const fitted = evaluateCubicBezier(
20517
+ P0,
20518
+ P1,
20519
+ P2,
20520
+ P3,
20521
+ ts[i]
20522
+ );
20523
+ const error = fitted.distanceTo(points[i]);
20524
+ errorSum += error * error;
20525
+ if (error > maxError) {
20526
+ maxError = error;
20527
+ maxErrorIndex = i;
20528
+ }
20529
+ fittedPoints.push(fitted);
20530
+ }
20531
+ return {
20532
+ controlPoints: [P0, P1, P2, P3],
20533
+ error: Math.sqrt(errorSum / n),
20534
+ maxError,
20535
+ maxErrorIndex,
20536
+ fittedPoints
20537
+ };
20538
+ }
20539
+ function fitMultiCubicBezier2D(inputPoints, options = {}) {
20540
+ const {
20541
+ tolerance = 5e-3,
20542
+ minPoints = 6,
20543
+ maxDepth = 20,
20544
+ maxSegments = 100
20545
+ } = options;
20546
+ if (!inputPoints || inputPoints.length < 4) {
20547
+ return null;
20548
+ }
20549
+ const points = inputPoints.map(
20550
+ (p) => p.clone ? p.clone() : new THREE12.Vector2(p.x, p.y)
20551
+ );
20552
+ if (points.some(
20553
+ (p) => !Number.isFinite(p.x) || !Number.isFinite(p.y)
20554
+ )) {
20555
+ return null;
20556
+ }
20557
+ const segments = [];
20558
+ let failed = false;
20559
+ function fitRecursive(startIndex, endIndex, depth) {
20560
+ if (failed) return;
20561
+ const segmentPoints = points.slice(
20562
+ startIndex,
20563
+ endIndex + 1
20564
+ );
20565
+ const result = fitSingleBezier(segmentPoints);
20566
+ if (!result) {
20567
+ failed = true;
20568
+ return;
20569
+ }
20570
+ if (result.maxError <= tolerance || segmentPoints.length <= minPoints) {
20571
+ if (result.maxError > tolerance && segmentPoints.length <= minPoints) {
20572
+ failed = true;
20573
+ return;
20574
+ }
20575
+ segments.push({
20576
+ startIndex,
20577
+ endIndex,
20578
+ controlPoints: result.controlPoints,
20579
+ error: result.error,
20580
+ maxError: result.maxError,
20581
+ fittedPoints: result.fittedPoints
20582
+ });
20583
+ if (segments.length > maxSegments) {
20584
+ failed = true;
20585
+ }
20586
+ return;
20587
+ }
20588
+ if (depth >= maxDepth) {
20589
+ failed = true;
20590
+ return;
20591
+ }
20592
+ let splitIndex = startIndex + result.maxErrorIndex;
20593
+ const minSplitIndex = startIndex + Math.ceil(minPoints / 2);
20594
+ const maxSplitIndex = endIndex - Math.ceil(minPoints / 2);
20595
+ if (minSplitIndex > maxSplitIndex) {
20596
+ failed = true;
20597
+ return;
20598
+ }
20599
+ splitIndex = Math.max(
20600
+ minSplitIndex,
20601
+ Math.min(maxSplitIndex, splitIndex)
20602
+ );
20603
+ fitRecursive(startIndex, splitIndex, depth + 1);
20604
+ fitRecursive(splitIndex, endIndex, depth + 1);
20605
+ }
20606
+ fitRecursive(0, points.length - 1, 0);
20607
+ if (failed || segments.length === 0) {
20608
+ return {
20609
+ success: false,
20610
+ reason: "\u65E0\u6CD5\u5728\u6307\u5B9A\u8BEF\u5DEE\u8303\u56F4\u5185\u5B8C\u6210\u62DF\u5408",
20611
+ segments: [],
20612
+ error: null,
20613
+ maxError: null
20614
+ };
20615
+ }
20616
+ segments.sort(
20617
+ (a, b) => a.startIndex - b.startIndex
20618
+ );
20619
+ let errorSum = 0;
20620
+ let totalCount = 0;
20621
+ let maxError = 0;
20622
+ for (const segment of segments) {
20623
+ for (const p of segment.fittedPoints) {
20624
+ const original = points[segment.startIndex + segment.fittedPoints.indexOf(p)];
20625
+ const error = p.distanceTo(original);
20626
+ errorSum += error * error;
20627
+ totalCount++;
20628
+ maxError = Math.max(maxError, error);
20629
+ }
20630
+ }
20631
+ return {
20632
+ success: true,
20633
+ // 多段贝塞尔曲线
20634
+ segments,
20635
+ // 所有控制点
20636
+ controlPoints: segments.map(
20637
+ (segment) => segment.controlPoints
20638
+ ),
20639
+ // 整体 RMS 误差
20640
+ error: Math.sqrt(errorSum / totalCount),
20641
+ // 整体最大误差
20642
+ maxError,
20643
+ // 曲线段数量
20644
+ segmentCount: segments.length,
20645
+ // 原始路径点
20646
+ points
20647
+ };
20648
+ }
20649
+
19250
20650
  // src/wall-window-matcher/utils.ts
19251
20651
  function dist2D(a, b) {
19252
20652
  const dx = a.x - b.x;
@@ -20358,7 +21758,7 @@ function processData(wallSegments, objects, options) {
20358
21758
  }
20359
21759
 
20360
21760
  // src/wall-door-matcher/matcher.ts
20361
- import * as THREE12 from "three";
21761
+ import * as THREE13 from "three";
20362
21762
 
20363
21763
  // src/wall-door-matcher/utils.ts
20364
21764
  function dist2D2(a, b) {
@@ -21203,13 +22603,13 @@ function collectCandidates2(candidateSegments, doorLong, doorDirX, doorDirY, che
21203
22603
  if (isLine2)
21204
22604
  continue;
21205
22605
  if (passageEntrance) {
21206
- const pt1 = new THREE12.Vector3(nline.start.x, nline.start.y, nline.start.z);
21207
- const pt2 = new THREE12.Vector3(nline.end.x, nline.end.y, nline.end.z);
22606
+ const pt1 = new THREE13.Vector3(nline.start.x, nline.start.y, nline.start.z);
22607
+ const pt2 = new THREE13.Vector3(nline.end.x, nline.end.y, nline.end.z);
21208
22608
  let isOverLap = false;
21209
22609
  for (let v2 = 0; v2 < passageEntrance.length; v2++) {
21210
22610
  const data = passageEntrance[v2];
21211
- const pt3 = new THREE12.Vector3(data.start.x, data.start.y, data.start.z);
21212
- const pt4 = new THREE12.Vector3(data.end.x, data.end.y, data.end.z);
22611
+ const pt3 = new THREE13.Vector3(data.start.x, data.start.y, data.start.z);
22612
+ const pt4 = new THREE13.Vector3(data.end.x, data.end.y, data.end.z);
21213
22613
  let resultTmp = classifySegments(pt1, pt2, pt3, pt4);
21214
22614
  if (resultTmp.type == "collinear_overlap" && resultTmp.maxPerpendicularDistance < 0.1) {
21215
22615
  isOverLap = true;
@@ -22861,7 +24261,7 @@ function angleOfSegment(dx, dz) {
22861
24261
  if (a >= Math.PI) a -= Math.PI;
22862
24262
  return a;
22863
24263
  }
22864
- function normalizeAngle(angle) {
24264
+ function normalizeAngle2(angle) {
22865
24265
  while (angle > Math.PI) angle -= 2 * Math.PI;
22866
24266
  while (angle < -Math.PI) angle += 2 * Math.PI;
22867
24267
  return angle;
@@ -22911,7 +24311,7 @@ function distToSegment(px, pz, ax, az, bx, bz) {
22911
24311
  t = Math.max(0, Math.min(1, t));
22912
24312
  return dist2D4(px, pz, ax + t * dx, az + t * dz);
22913
24313
  }
22914
- function smoothPolyline(pts) {
24314
+ function smoothPolyline2(pts) {
22915
24315
  if (pts.length < 4) return pts;
22916
24316
  let cur = pts.map((p) => ({ x: p.x, z: p.z }));
22917
24317
  for (let iter = 0; iter < 3; iter++) {
@@ -23159,7 +24559,7 @@ var DEFAULT_PC_CONFIG = {
23159
24559
  densityRatio: 0.12
23160
24560
  // 自适应密度比例:保留密度≥中位数×12%的格子
23161
24561
  };
23162
- function fitCircle(pts) {
24562
+ function fitCircle2(pts) {
23163
24563
  const n = pts.length;
23164
24564
  if (n < 3) return { cx: 0, cz: 0, r: 0, rmsResidual: 0 };
23165
24565
  let sx = 0, sz = 0, sx2 = 0, sz2 = 0, sxz = 0, sx3 = 0, sz3 = 0, sx2z = 0, sxz2 = 0;
@@ -23260,7 +24660,7 @@ function classifyWallByPointCloudPCA(wallPoints, _cloudPoints, _radius) {
23260
24660
  segLen += Math.sqrt(dx * dx + dz * dz);
23261
24661
  }
23262
24662
  const smoothPts = smoothPoints(wallPoints);
23263
- const circleFit = fitCircle(smoothPts);
24663
+ const circleFit = fitCircle2(smoothPts);
23264
24664
  const goodArc = circleFit.r > 0.1 && circleFit.r < Math.max(segLen * 5, 2) && circleFit.rmsResidual < 0.07;
23265
24665
  if (goodArc) {
23266
24666
  return {
@@ -23525,7 +24925,7 @@ function detectArcsByBruteForceFit(contour, n) {
23525
24925
  const pts = [];
23526
24926
  for (let j = i; j <= end; j++) pts.push(contour[j]);
23527
24927
  if (pts.length < W - 1) continue;
23528
- const { cx, cz, r, rmsResidual } = fitCircle(pts);
24928
+ const { cx, cz, r, rmsResidual } = fitCircle2(pts);
23529
24929
  if (r < 0.1 || rmsResidual > RMS_TH || r < MIN_RADIUS) continue;
23530
24930
  let arcAng = 0;
23531
24931
  for (let k = 1; k < pts.length; k++) {
@@ -23731,7 +25131,7 @@ function segmentContourCurves(contour, config) {
23731
25131
  }
23732
25132
  let sc = 0;
23733
25133
  for (let j = 1; j < pts.length; j++) if (angles[(cand.start + j - 1) % n] * angles[(cand.start + j) % n] < -1e-8) sc++;
23734
- const { cx, cz, r, rmsResidual } = fitCircle(pts);
25134
+ const { cx, cz, r, rmsResidual } = fitCircle2(pts);
23735
25135
  let arcAng = 0, netAng = 0;
23736
25136
  for (let i = 1; i < pts.length; i++) {
23737
25137
  const dx1 = pts[i - 1].x - cx, dz1 = pts[i - 1].z - cz, dx2 = pts[i].x - cx, dz2 = pts[i].z - cz;
@@ -23845,7 +25245,7 @@ function segmentContourCurves(contour, config) {
23845
25245
  allPts.push({ ...contour[j] });
23846
25246
  if (j === uEnd) break;
23847
25247
  }
23848
- const { cx, cz, r } = fitCircle(allPts);
25248
+ const { cx, cz, r } = fitCircle2(allPts);
23849
25249
  let arcAng = 0;
23850
25250
  for (let i2 = 1; i2 < allPts.length; i2++) {
23851
25251
  const dx1 = allPts[i2 - 1].x - cx, dz1 = allPts[i2 - 1].z - cz;
@@ -23890,7 +25290,7 @@ function segmentContourCurves(contour, config) {
23890
25290
  allPts.push({ ...contour[j] });
23891
25291
  if (j === merged[i].end) break;
23892
25292
  }
23893
- const { cx, cz, r } = fitCircle(allPts);
25293
+ const { cx, cz, r } = fitCircle2(allPts);
23894
25294
  let arcAng = 0;
23895
25295
  for (let i2 = 1; i2 < allPts.length; i2++) {
23896
25296
  const dx1 = allPts[i2 - 1].x - cx, dz1 = allPts[i2 - 1].z - cz;
@@ -23935,7 +25335,7 @@ function detectUWall(contour) {
23935
25335
  if (j === best.end) break;
23936
25336
  }
23937
25337
  if (pts.length < 10) return null;
23938
- const { cx, cz, r, rmsResidual } = fitCircle(pts);
25338
+ const { cx, cz, r, rmsResidual } = fitCircle2(pts);
23939
25339
  if (r > 0.1 && rmsResidual > 0.1) return null;
23940
25340
  const arcAng = best.actualAngle;
23941
25341
  const totalLen = pts.reduce((s, p, i) => {
@@ -24137,7 +25537,7 @@ function processFromPointCloud(points3D, rawLines, pcConfig) {
24137
25537
  pts = result;
24138
25538
  }
24139
25539
  }
24140
- pts = smoothPolyline(pts);
25540
+ pts = smoothPolyline2(pts);
24141
25541
  if (wallCorners.length > 0 && pts.length >= 5) {
24142
25542
  for (const corner of wallCorners) {
24143
25543
  let bestDist = Infinity, bestJ = -1;
@@ -24259,7 +25659,7 @@ function processFromSegments(segments, config) {
24259
25659
  }
24260
25660
  const t4 = Date.now();
24261
25661
  let pts = simplifyPolyline(boundary, epsilon3);
24262
- pts = smoothPolyline(pts);
25662
+ pts = smoothPolyline2(pts);
24263
25663
  if (pts.length > 2) {
24264
25664
  const first = pts[0], last = pts[pts.length - 1];
24265
25665
  if (Math.abs(first.x - last.x) > 1e-3 || Math.abs(first.z - last.z) > 1e-3) {
@@ -24818,7 +26218,7 @@ function growPolylines(lines, connectDist, angleTolRad) {
24818
26218
  const d = Math.min(d1, d2);
24819
26219
  if (d > bestDist) continue;
24820
26220
  const candAngle = useReversed ? angleOfSegment(cand.x1 - cand.x2, cand.z1 - cand.z2) : angleOfSegment(cand.x2 - cand.x1, cand.z2 - cand.z1);
24821
- const angleDiff = Math.abs(normalizeAngle(candAngle - tipAngle));
26221
+ const angleDiff = Math.abs(normalizeAngle2(candAngle - tipAngle));
24822
26222
  if (angleDiff > angleTolRad) continue;
24823
26223
  best = cand;
24824
26224
  bestDist = d;
@@ -24860,7 +26260,7 @@ function growPolylines(lines, connectDist, angleTolRad) {
24860
26260
  const d = Math.min(d1, d2);
24861
26261
  if (d > bestDist) continue;
24862
26262
  const candAngle = useReversed ? angleOfSegment(cand.x1 - cand.x2, cand.z1 - cand.z2) : angleOfSegment(cand.x2 - cand.x1, cand.z2 - cand.z1);
24863
- const angleDiff = Math.abs(normalizeAngle(candAngle - tipAngle));
26263
+ const angleDiff = Math.abs(normalizeAngle2(candAngle - tipAngle));
24864
26264
  if (angleDiff > angleTolRad) continue;
24865
26265
  best = cand;
24866
26266
  bestDist = d;
@@ -24931,7 +26331,7 @@ function computeTurnAngles(points) {
24931
26331
  const dz2 = points[i + 1].z - points[i].z;
24932
26332
  const angle1 = Math.atan2(dz1, dx1);
24933
26333
  const angle2 = Math.atan2(dz2, dx2);
24934
- let delta = normalizeAngle(angle2 - angle1);
26334
+ let delta = normalizeAngle2(angle2 - angle1);
24935
26335
  deltas.push(delta);
24936
26336
  }
24937
26337
  return deltas;
@@ -25449,6 +26849,7 @@ export {
25449
26849
  findDoorWalls,
25450
26850
  findMainByLoops,
25451
26851
  findWindowWalls,
26852
+ getArcWallLine,
25452
26853
  getBeamLine,
25453
26854
  getColLine,
25454
26855
  getDoorPcdData,