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