build-dxf 0.0.8 → 0.0.10

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/src/index.js CHANGED
@@ -1,1752 +1,6 @@
1
- import * as THREE from "three";
2
- import { EventDispatcher as EventDispatcher$1 } from "three";
3
- import ClipperLib from "clipper-lib";
4
- import Drawing from "dxf-writer";
5
- function uuid() {
6
- return "xxxx-xxxx-4xxx-yxxx-xxxx".replace(/[xy]/g, function(c) {
7
- var r = Math.random() * 16 | 0, v = c == "x" ? r : r & 3 | 8;
8
- return v.toString(16);
9
- });
10
- }
11
- class EventDispatcher extends EventDispatcher$1 {
12
- uuid = uuid();
13
- }
14
- class Component extends EventDispatcher {
15
- parent;
16
- constructor(...arg) {
17
- super();
18
- this.addEventListener("addFromParent", (e) => {
19
- this.parent = e.parent;
20
- this.onAddFromParent(e.parent);
21
- });
22
- this.addEventListener("removeFromParent", (e) => {
23
- this.parent = void 0;
24
- this.onRemoveFromParent(e.parent);
25
- });
26
- }
27
- onAddFromParent(parent) {
28
- }
29
- onRemoveFromParent(parent) {
30
- }
31
- destroy() {
32
- }
33
- }
34
- class ComponentManager extends EventDispatcher {
35
- static EventType = {
36
- ADD_COMPONENT: "addComponent"
37
- };
38
- components = [];
39
- /**
40
- * 添加组件
41
- * @param component
42
- */
43
- addComponent(component) {
44
- if (component instanceof Component) {
45
- this.components.push(component);
46
- this.dispatchEvent({
47
- type: "addComponent",
48
- component
49
- });
50
- component.dispatchEvent({
51
- type: "addFromParent",
52
- parent: this
53
- });
54
- }
55
- return this;
56
- }
57
- /**
58
- * 移除组件
59
- * @param component
60
- */
61
- removeComponent(component) {
62
- if (component instanceof Component) {
63
- const index = this.components.indexOf(component);
64
- if (index > -1) {
65
- this.components.splice(index, 1);
66
- this.dispatchEvent({
67
- type: "removeComponent",
68
- component
69
- });
70
- component.dispatchEvent({
71
- type: "removeFromParent",
72
- parent: this
73
- });
74
- }
75
- }
76
- }
77
- /**
78
- * 查找符合条件的第一个组件
79
- * @param callBack
80
- */
81
- findComponent(predicate) {
82
- for (let i = 0; i < this.components.length; i++) {
83
- const component = this.components[i];
84
- if (predicate(component, i)) return component;
85
- }
86
- }
87
- /**
88
- * 查找所有符合条件的组件
89
- * @param callBack
90
- */
91
- findComponents(predicate) {
92
- return this.components.find(predicate);
93
- }
94
- /**
95
- *
96
- * @param type
97
- */
98
- findComponentByType(type) {
99
- const component = this.findComponent((c) => c instanceof type);
100
- return component ? component : null;
101
- }
102
- }
103
- class Point {
104
- x;
105
- y;
106
- get X() {
107
- return this.x;
108
- }
109
- get Y() {
110
- return this.y;
111
- }
112
- /**
113
- *
114
- * @param x
115
- * @param y
116
- */
117
- constructor(x = 0, y = 0) {
118
- this.x = x;
119
- this.y = y;
120
- }
121
- set(x, y) {
122
- this.x = x;
123
- this.y = y;
124
- return this;
125
- }
126
- setX(x) {
127
- this.x = x;
128
- return this;
129
- }
130
- setY(y) {
131
- this.y = y;
132
- return this;
133
- }
134
- /**
135
- *
136
- * @param point
137
- * @returns
138
- */
139
- equal(point) {
140
- return point.x === this.x && point.y === this.y;
141
- }
142
- /**
143
- *
144
- * @param arr
145
- */
146
- setByArray(arr) {
147
- this.x = arr[0];
148
- this.y = arr[1];
149
- return this;
150
- }
151
- /**
152
- *
153
- */
154
- toArray() {
155
- return [this.x, this.y];
156
- }
157
- /**
158
- * multiplyScalar
159
- * @param scalar
160
- */
161
- mutiplyScalar(scalar) {
162
- this.x *= scalar;
163
- this.y *= scalar;
164
- return this;
165
- }
166
- multiplyScalar(scalar) {
167
- this.x *= scalar;
168
- this.y *= scalar;
169
- return this;
170
- }
171
- /**
172
- *
173
- * @param scalar
174
- * @returns
175
- */
176
- divisionScalar(scalar) {
177
- this.x /= scalar;
178
- this.y /= scalar;
179
- return this;
180
- }
181
- /**
182
- * 减法
183
- * @description 将当前点的坐标减去指定点的坐标
184
- * @param point
185
- * @returns
186
- */
187
- division(point) {
188
- this.x -= point.x;
189
- this.y -= point.y;
190
- return this;
191
- }
192
- /**
193
- * 加法
194
- * @description 将当前点的坐标加上指定点的坐标
195
- * @param point
196
- * @returns
197
- */
198
- add(point) {
199
- this.x += point.x;
200
- this.y += point.y;
201
- return this;
202
- }
203
- /**
204
- * 保留小数位数
205
- * @param count
206
- */
207
- fixed(count) {
208
- this.x = Number(this.x.toFixed(count));
209
- this.y = Number(this.y.toFixed(count));
210
- return this;
211
- }
212
- /**
213
- * 归一化
214
- * @description 将当前点的坐标归一化为单位向量
215
- * @returns
216
- */
217
- normalize() {
218
- const length = Math.sqrt(this.x * this.x + this.y * this.y);
219
- if (length === 0) return this;
220
- this.x /= length;
221
- this.y /= length;
222
- return this;
223
- }
224
- /**
225
- * 获取单位法向量
226
- * @description 计算当前点到指定点的方向向量,并返回逆时针旋转90度后的单位法向量
227
- * @param point
228
- * @returns
229
- */
230
- normal(point) {
231
- const dx = this.x - point.x;
232
- const dy = this.y - point.y;
233
- const length = Math.sqrt(dx * dx + dy * dy);
234
- const nx = -dy / length;
235
- const ny = dx / length;
236
- return new Point(nx, ny);
237
- }
238
- /**
239
- * 获取由传入的点到该点的单位方向向量
240
- * @description 计算当前点到指定点的方向向量,并返回单位方向
241
- * @param point
242
- * @returns
243
- */
244
- direction(point) {
245
- const dx = this.x - point.x;
246
- const dy = this.y - point.y;
247
- const length = Math.sqrt(dx * dx + dy * dy);
248
- if (length === 0) return new Point(0, 0);
249
- return new Point(dx / length, dy / length);
250
- }
251
- /**
252
- * 计算模长
253
- * @returns
254
- */
255
- magnitude() {
256
- return Math.sqrt(this.x * this.x + this.y * this.y);
257
- }
258
- /**
259
- * 计算点点积
260
- * @param point
261
- * @returns
262
- */
263
- dot(point) {
264
- return this.x * point.x + this.y * point.y;
265
- }
266
- /** 计算两个向量夹角
267
- * @description 公式:a · b = |a| × |b| × cosθ
268
- * @param point
269
- * @returns
270
- */
271
- angleBetween(point) {
272
- const dotProduct = this.dot(point);
273
- const magnitude1 = this.magnitude();
274
- const magnitude2 = point.magnitude();
275
- if (magnitude1 === 0 || magnitude2 === 0) return 0;
276
- const cosTheta = dotProduct / (magnitude1 * magnitude2);
277
- const clampedCosTheta = Math.max(-1, Math.min(1, cosTheta));
278
- return Math.acos(clampedCosTheta);
279
- }
280
- /** 获取向量长度
281
- */
282
- length() {
283
- const magnitude = Math.sqrt(this.x * this.x + this.y * this.y);
284
- if (Math.abs(magnitude - Math.round(magnitude)) < 1e-9) {
285
- return Math.round(magnitude);
286
- }
287
- return magnitude;
288
- }
289
- /**
290
- * 获取两个点长度
291
- * @param point
292
- */
293
- distance(point) {
294
- return Math.sqrt(
295
- (this.x - point.x) * (this.x - point.x) + (this.y - point.y) * (this.y - point.y)
296
- );
297
- }
298
- /**
299
- * 克隆
300
- * @returns
301
- */
302
- clone() {
303
- return new Point(this.x, this.y);
304
- }
305
- static from(arr) {
306
- if (Array.isArray(arr)) {
307
- return new Point(arr[0], arr[1]);
308
- } else if ("x" in arr && "y" in arr) {
309
- return new Point(arr.x, arr.y);
310
- } else if ("X" in arr && "Y" in arr) {
311
- return new Point(arr.X, arr.Y);
312
- }
313
- return this.zero();
314
- }
315
- static zero() {
316
- return new Point(0, 0);
317
- }
318
- }
319
- class Box2 {
320
- minX = 0;
321
- maxX = 0;
322
- minY = 0;
323
- maxY = 0;
324
- get points() {
325
- return [
326
- new Point(this.minX, this.minY),
327
- new Point(this.maxX, this.minY),
328
- new Point(this.maxX, this.maxY),
329
- new Point(this.minX, this.maxY)
330
- ];
331
- }
332
- get width() {
333
- return this.maxX - this.minX;
334
- }
335
- get height() {
336
- return this.maxY - this.minY;
337
- }
338
- get center() {
339
- return new Point(
340
- this.minX + (this.maxX - this.minX) * 0.5,
341
- this.minY + (this.maxY - this.minY) * 0.5
342
- );
343
- }
344
- constructor(minX = 0, maxX = 0, minY = 0, maxY = 0) {
345
- this.minX = minX;
346
- this.maxX = maxX;
347
- this.minY = minY;
348
- this.maxY = maxY;
349
- }
350
- /**
351
- *
352
- * @param z
353
- * @returns
354
- */
355
- getPaths3D(z = 0) {
356
- const points = this.points, list = [];
357
- points.forEach((p, i) => {
358
- const nextP = points[(i + 1) % points.length];
359
- list.push(p.x, p.y, z);
360
- list.push(nextP.x, nextP.y, z);
361
- });
362
- return list;
363
- }
364
- /**
365
- * 判断线段是与包围盒相交
366
- * @description Liang-Barsky算法的变种
367
- * @param line
368
- */
369
- intersectLineSegment(line) {
370
- const p1 = line.points[0];
371
- const p2 = line.points[1];
372
- const dx = p2.x - p1.x;
373
- const dy = p2.y - p1.y;
374
- if (dx === 0 && dy === 0) {
375
- return this.minX <= p1.x && p1.x <= this.maxX && this.minY <= p1.y && p1.y <= this.maxY;
376
- }
377
- let tNear = Number.NEGATIVE_INFINITY;
378
- let tFar = Number.POSITIVE_INFINITY;
379
- if (dx !== 0) {
380
- const tx1 = (this.minX - p1.x) / dx;
381
- const tx2 = (this.maxX - p1.x) / dx;
382
- tNear = Math.max(tNear, Math.min(tx1, tx2));
383
- tFar = Math.min(tFar, Math.max(tx1, tx2));
384
- } else if (p1.x < this.minX || p1.x > this.maxX) {
385
- return false;
386
- }
387
- if (dy !== 0) {
388
- const ty1 = (this.minY - p1.y) / dy;
389
- const ty2 = (this.maxY - p1.y) / dy;
390
- tNear = Math.max(tNear, Math.min(ty1, ty2));
391
- tFar = Math.min(tFar, Math.max(ty1, ty2));
392
- } else if (p1.y < this.minY || p1.y > this.maxY) {
393
- return false;
394
- }
395
- return tNear <= tFar && tNear <= 1 && tFar >= 0;
396
- }
397
- /**
398
- * 判断线段是在包围盒内
399
- * @param line
400
- */
401
- containsLineSegment(line) {
402
- const [p1, p2] = line.points;
403
- return this.minX <= p1.x && p1.x <= this.maxX && this.minY <= p1.y && p1.y <= this.maxY && this.minX <= p2.x && p2.x <= this.maxX && this.minY <= p2.y && p2.y <= this.maxY;
404
- }
405
- /**
406
- * 判断矩形与包围盒相交
407
- * @param rectangle
408
- */
409
- intersectRectangle(rectangle) {
410
- const isPointInBox = (p) => this.minX <= p.x && p.x <= this.maxX && this.minY <= p.y && p.y <= this.maxY;
411
- const isPointInRect = (point) => {
412
- let sign = 0;
413
- for (let i = 0; i < 4; i++) {
414
- const p1 = rectangle.points[i];
415
- const p2 = rectangle.points[(i + 1) % 4];
416
- const edge = { x: p2.x - p1.x, y: p2.y - p1.y };
417
- const toPoint = { x: point.x - p1.x, y: point.y - p1.y };
418
- const cross = edge.x * toPoint.y - edge.y * toPoint.x;
419
- if (cross === 0) {
420
- const t = edge.x !== 0 ? (point.x - p1.x) / edge.x : (point.y - p1.y) / edge.y;
421
- if (t >= 0 && t <= 1) return true;
422
- } else {
423
- const currentSign = cross > 0 ? 1 : -1;
424
- if (sign === 0) sign = currentSign;
425
- if (sign !== currentSign) return false;
426
- }
427
- }
428
- return true;
429
- };
430
- const doIntersect = (l1p1, l1p2, l2p1, l2p2) => {
431
- const orientation = (p, q, r) => {
432
- const val = (q.y - p.y) * (r.x - q.x) - (q.x - p.x) * (r.y - q.y);
433
- if (val === 0) return 0;
434
- return val > 0 ? 1 : 2;
435
- };
436
- const onSegment = (p, q, r) => {
437
- return Math.min(p.x, r.x) <= q.x && q.x <= Math.max(p.x, r.x) && Math.min(p.y, r.y) <= q.y && q.y <= Math.max(p.y, r.y);
438
- };
439
- const o1 = orientation(l1p1, l1p2, l2p1);
440
- const o2 = orientation(l1p1, l1p2, l2p2);
441
- const o3 = orientation(l2p1, l2p2, l1p1);
442
- const o4 = orientation(l2p1, l2p2, l1p2);
443
- if (o1 !== o2 && o3 !== o4) return true;
444
- if (o1 === 0 && onSegment(l1p1, l2p1, l1p2)) return true;
445
- if (o2 === 0 && onSegment(l1p1, l2p2, l1p2)) return true;
446
- if (o3 === 0 && onSegment(l2p1, l1p1, l2p2)) return true;
447
- if (o4 === 0 && onSegment(l2p1, l1p2, l2p2)) return true;
448
- return false;
449
- };
450
- const boxPoints = this.points;
451
- for (let i = 0; i < 4; i++) {
452
- const bp1 = boxPoints[i];
453
- const bp2 = boxPoints[(i + 1) % 4];
454
- for (let j = 0; j < 4; j++) {
455
- const rp1 = rectangle.points[j];
456
- const rp2 = rectangle.points[(j + 1) % 4];
457
- if (doIntersect(bp1, bp2, rp1, rp2)) return true;
458
- }
459
- }
460
- for (let p of rectangle.points) {
461
- if (isPointInBox(p)) return true;
462
- }
463
- for (let p of boxPoints) {
464
- if (isPointInRect(p)) return true;
465
- }
466
- return false;
467
- }
468
- /**
469
- * 判断矩形是在包围盒内
470
- * @param rectangle
471
- */
472
- containsRectangle(rectangle) {
473
- return rectangle.points.every(
474
- (p) => this.minX <= p.x && p.x <= this.maxX && this.minY <= p.y && p.y <= this.maxY
475
- );
476
- }
477
- /**
478
- * 判断包围盒与包围盒相交
479
- * @param box
480
- */
481
- intersectBox(box) {
482
- return !(this.maxX < box.minX || this.minX > box.maxX || this.maxY < box.minY || this.minY > box.maxY);
483
- }
484
- /**
485
- * 判断包围盒是在包围盒内
486
- * @param box
487
- */
488
- containsBox(box) {
489
- return this.minX <= box.minX && box.maxX <= this.maxX && this.minY <= box.minY && box.maxY <= this.maxY;
490
- }
491
- /** 判断点是在包围盒内
492
- * @param point
493
- */
494
- containsPoint(point) {
495
- return point.x >= this.minX && point.x <= this.maxX && point.y >= this.minY && point.y <= this.maxY;
496
- }
497
- /**
498
- *
499
- * @param minX
500
- * @param minY
501
- * @param maxX
502
- * @param maxY
503
- * @returns
504
- */
505
- set(minX, minY, maxX, maxY) {
506
- this.minX = minX;
507
- this.minY = minY;
508
- this.maxX = maxX;
509
- this.maxY = maxY;
510
- return this;
511
- }
512
- /**
513
- *
514
- * @param maxWidth
515
- * @param maxHeight
516
- * @param mode
517
- * @returns
518
- */
519
- scaleSize(maxWidth, maxHeight, mode = "min") {
520
- const scaleX = maxWidth / this.width;
521
- const scaleY = maxHeight / this.height;
522
- return Math[mode](scaleX, scaleY);
523
- }
524
- /**
525
- *
526
- * @param scalar
527
- * @returns
528
- */
529
- multiplyScalar(scalar) {
530
- this.minX *= scalar;
531
- this.minY *= scalar;
532
- this.maxX *= scalar;
533
- this.maxY *= scalar;
534
- return this;
535
- }
536
- /**
537
- *
538
- * @returns
539
- */
540
- clone() {
541
- return new Box2(this.minX, this.minY, this.maxX, this.maxY);
542
- }
543
- /**
544
- *
545
- * @param points
546
- * @returns
547
- */
548
- static fromByPoints(...points) {
549
- const xList = [], yList = [];
550
- points.forEach((p) => {
551
- xList.push(p.x);
552
- yList.push(p.y);
553
- });
554
- return new Box2(
555
- Math.min(...xList),
556
- Math.max(...xList),
557
- Math.min(...yList),
558
- Math.max(...yList)
559
- );
560
- }
561
- }
562
- class LineSegment {
563
- points = [new Point(), new Point()];
564
- userData;
565
- get center() {
566
- return new Point(
567
- this.points[0].x + (this.points[1].x - this.points[0].x) * 0.5,
568
- this.points[0].y + (this.points[1].y - this.points[0].y) * 0.5
569
- );
570
- }
571
- get start() {
572
- return this.points[0];
573
- }
574
- get end() {
575
- return this.points[1];
576
- }
577
- constructor(p1 = new Point(), p2 = new Point()) {
578
- this.points = [p1, p2];
579
- }
580
- /**
581
- * 计算线段的长度
582
- * @returns 线段的长度
583
- */
584
- getLength() {
585
- return this.points[0].distance(this.points[1]);
586
- }
587
- /**
588
- * 获取方向
589
- * @returns
590
- */
591
- direction() {
592
- return this.points[1].direction(this.points[0]);
593
- }
594
- /**
595
- * 计算一条线段在另一条直线上的投影,并裁剪超出目标线段的部分
596
- * @param line 要投影的线段
597
- * @returns 投影并裁剪后的线段
598
- */
599
- projectLineSegment(line) {
600
- if (line.points.length !== 2 || this.points.length !== 2) {
601
- throw new Error("每条线段必须由两个点定义");
602
- }
603
- const [p1, p2] = line.points;
604
- const [q1, q2] = this.points;
605
- const dir = new Point(q2.x - q1.x, q2.y - q1.y);
606
- if (dir.x === 0 && dir.y === 0) {
607
- throw new Error("投影目标线段的两个点不能重合");
608
- }
609
- const projectPoint = (point) => {
610
- const pq = new Point(point.x - q1.x, point.y - q1.y);
611
- const dirLengthSquared = dir.x * dir.x + dir.y * dir.y;
612
- const dotProduct = pq.x * dir.x + pq.y * dir.y;
613
- const t = dotProduct / dirLengthSquared;
614
- const projX = q1.x + t * dir.x;
615
- const projY = q1.y + t * dir.y;
616
- return new Point(projX, projY);
617
- };
618
- let projP1 = projectPoint(p1);
619
- let projP2 = projectPoint(p2);
620
- const getT = (point) => {
621
- const pq = new Point(point.x - q1.x, point.y - q1.y);
622
- const dirLengthSquared = dir.x * dir.x + dir.y * dir.y;
623
- return (pq.x * dir.x + pq.y * dir.y) / dirLengthSquared;
624
- };
625
- let t1 = getT(projP1);
626
- let t2 = getT(projP2);
627
- const clampPoint = (t) => {
628
- const clampedT = Math.max(0, Math.min(1, t));
629
- const x = q1.x + clampedT * dir.x;
630
- const y = q1.y + clampedT * dir.y;
631
- return new Point(x, y);
632
- };
633
- if (t1 < 0 || t1 > 1) {
634
- projP1 = clampPoint(t1);
635
- }
636
- if (t2 < 0 || t2 > 1) {
637
- projP2 = clampPoint(t2);
638
- }
639
- if (projP1.x === projP2.x && projP1.y === projP2.y) {
640
- return new LineSegment(projP1, projP1);
641
- }
642
- return new LineSegment(projP1, projP2);
643
- }
644
- clone() {
645
- return new LineSegment(
646
- this.points[0].clone(),
647
- this.points[1].clone()
648
- );
649
- }
650
- }
651
- const units = {
652
- Unitless: 1,
653
- // 无单位,1米 = 1(无单位)
654
- Inches: 39.37007874015748,
655
- // 英寸,1米 = 39.37007874015748英寸
656
- Feet: 3.280839895013123,
657
- // 英尺,1米 = 3.280839895013123英尺
658
- Miles: 6213711922373339e-19,
659
- // 英里,1米 = 0.0006213711922373339英里
660
- Millimeters: 1e3,
661
- // 毫米,1米 = 1000毫米
662
- Centimeters: 100,
663
- // 厘米,1米 = 100厘米
664
- Meters: 1,
665
- // 米,1米 = 1米
666
- Kilometers: 1e-3,
667
- // 千米,1米 = 0.001千米
668
- Microinches: 3937007874015748e-8,
669
- // 微英寸,1米 = 39370078.74015748微英寸
670
- Mils: 39370.07874015748,
671
- // 密耳,1米 = 39370.07874015748密耳
672
- Yards: 1.0936132983377078,
673
- // 码,1米 = 1.0936132983377078码
674
- Angstroms: 1e10,
675
- // 埃,1米 = 10^10埃
676
- Nanometers: 1e9,
677
- // 纳米,1米 = 10^9纳米
678
- Microns: 1e6,
679
- // 微米,1米 = 10^6微米
680
- Decimeters: 10,
681
- // 分米,1米 = 10分米
682
- Decameters: 0.1,
683
- // 十米,1米 = 0.1十米
684
- Hectometers: 0.01,
685
- // 百米,1米 = 0.01百米
686
- Gigameters: 1e-9,
687
- // 吉米,1米 = 10^-9吉米
688
- "Astronomical units": 6684587122268445e-27,
689
- // 天文单位,1米 = 0.000000000006684587122268445天文单位
690
- "Light years": 10570008340246154e-32,
691
- // 光年,1米 ≈ 0.00000000000000010570008340246154光年
692
- Parsecs: 3240779289666404e-32
693
- // 秒差距,1米 ≈ 0.00000000000000003240779289666404秒差距
694
- };
695
- class Dxf extends Component {
696
- width = 0.04;
697
- scale = 1;
698
- originalData = [];
699
- data = [];
700
- originalBox = new Box2(0, 0, 0, 0);
701
- box = new Box2(0, 0, 0, 0);
702
- pointsGroups = [];
703
- wallsGroup = [];
704
- doors = [];
705
- lineSegments = [];
706
- originalZAverage = 0;
707
- static EndType = {
708
- etOpenSquare: 0,
709
- etOpenRound: 1,
710
- etOpenButt: 2
711
- // etClosedLine: 3,
712
- // etClosedPolygon: 4
713
- };
714
- static JoinType = {
715
- jtSquare: 0,
716
- jtRound: 1,
717
- jtMiter: 2
718
- };
719
- /** 原始数据组
720
- */
721
- get lines() {
722
- return this.lineSegments;
723
- }
724
- /**初始化
725
- * @param data 点云数据
726
- * @param width 墙体宽度
727
- * @param scale 缩放比例
728
- */
729
- constructor(width = 0.04, scale = 1) {
730
- super();
731
- this.width = width;
732
- this.scale = scale;
733
- }
734
- /**
735
- * 设置
736
- * @param data
737
- * @param width
738
- * @param scale
739
- */
740
- async set(data, width = this.width, scale = this.scale) {
741
- if (typeof data === "string") {
742
- if (typeof global !== "undefined") {
743
- const packageName = "fs";
744
- const { default: fs } = await import(
745
- /* @vite-ignore */
746
- packageName
747
- );
748
- const buffer = fs.readFileSync(data);
749
- const json = JSON.parse(buffer.toString("utf-8"));
750
- return this.set(json, width, scale);
751
- } else {
752
- throw new Error("非node环境不允许使用路径");
753
- }
754
- }
755
- this.scale = scale;
756
- this.width = width;
757
- this.originalData = data;
758
- const zList = [];
759
- this.data = data.map(({ start, end, insetionArr, isDoor = false }, index) => {
760
- zList.push(start.z ?? 0, end.z ?? 0);
761
- const lineSegment = new LineSegment(
762
- Point.from(start).mutiplyScalar(scale),
763
- Point.from(end).mutiplyScalar(scale)
764
- );
765
- lineSegment.userData = { isDoor };
766
- this.lineSegments.push(lineSegment);
767
- return [
768
- lineSegment.points[0],
769
- lineSegment.points[1],
770
- (insetionArr ?? []).map((i) => i.index),
771
- isDoor,
772
- index
773
- ];
774
- });
775
- this.originalZAverage = zList.reduce((count, num) => count + num, 0) / zList.length;
776
- this.computedOriginalSize(data, this.originalBox);
777
- this.dispatchEvent({
778
- type: "setDta",
779
- originalData: this.originalData,
780
- data: this.data
781
- });
782
- this.createGroups();
783
- this.computedSize();
784
- this.dispatchEvent({
785
- type: "createGroup",
786
- groups: this.pointsGroups
787
- });
788
- }
789
- /** 创建分组
790
- * @description 根据相交数组insetionArr, 把相交的线段,划分到一组内,方便后续路径查找合并使用
791
- * @returns
792
- */
793
- createGroups() {
794
- const groups = [], visited = /* @__PURE__ */ new Set(), doorSet = /* @__PURE__ */ new Set(), doorVisitedRecord = /* @__PURE__ */ new Map();
795
- const dfs = (index, group, preIndex = -1) => {
796
- const [start, end, insetionArr, isDoor] = this.data[index];
797
- visited.add(index);
798
- if (isDoor) {
799
- if (!doorVisitedRecord.has(index)) doorVisitedRecord.set(index, []);
800
- doorVisitedRecord.get(index)?.push(preIndex);
801
- return doorSet.add(this.data[index]);
802
- }
803
- group.push([start, end]);
804
- insetionArr.forEach((i) => {
805
- if (!visited.has(i)) {
806
- dfs(i, group, index);
807
- }
808
- });
809
- };
810
- this.data.forEach((_, index) => {
811
- if (!visited.has(index)) {
812
- const group = [];
813
- dfs(index, group);
814
- groups.push(group);
815
- }
816
- });
817
- this.doors = [...doorSet];
818
- this.pointsGroups = groups;
819
- return groups;
820
- }
821
- /** 计算当前墙体数据的边界框
822
- * @description 根据分组数据pointsGroups,计算包围盒, pointsGroups数据为缩放后数据。
823
- * @description 可通过box属性查看计算结果。
824
- * @returns
825
- */
826
- computedSize() {
827
- const xArr = this.pointsGroups.flatMap((points) => points.flatMap((p) => [p[0].x, p[1].x]));
828
- const yArr = this.pointsGroups.flatMap((points) => points.flatMap((p) => [p[0].y, p[1].y]));
829
- const minX = Math.min(...xArr);
830
- const minY = Math.min(...yArr);
831
- const maxX = Math.max(...xArr);
832
- const maxY = Math.max(...yArr);
833
- this.box.set(minX, minY, maxX, maxY);
834
- return this.box;
835
- }
836
- /** 线路拓扑
837
- * @description 处理线路拓扑,使线路有序链接,形成长路径
838
- * @param lines
839
- */
840
- lineTopology(lines) {
841
- const visited = [];
842
- function dfs(index, linePath) {
843
- const [_0, a2] = lines[index];
844
- visited[index] = true;
845
- linePath.push(a2);
846
- for (let i = 0; i < lines.length; i++) {
847
- const [b1, _1] = lines[i];
848
- if (!visited[i]) {
849
- if (Math.abs(a2.x - b1.x) < 1e-6 && Math.abs(a2.y - b1.y) < 1e-6) {
850
- return dfs(i, linePath);
851
- }
852
- }
853
- }
854
- }
855
- const linePaths = [];
856
- for (let i = 0; i < lines.length; i++) {
857
- if (!visited[i]) {
858
- const linePath = [lines[i][0]];
859
- dfs(i, linePath);
860
- linePaths.push(linePath);
861
- }
862
- }
863
- return linePaths;
864
- }
865
- /** etOpenRound 去除毛刺
866
- * @description 检查连续的短线段数量,去除合并后产生的毛刺
867
- */
868
- squareRemoveBurr() {
869
- this.wallsGroup = this.wallsGroup.map((lines) => {
870
- if (lines.length < 3) return lines;
871
- const filterLines = [lines[0]];
872
- for (let i = 1; i < lines.length; i++) {
873
- const prev = lines[i - 1];
874
- const curr = lines[i];
875
- const len = prev.distance(curr);
876
- if (len < this.width * 0.5) {
877
- let count = 0;
878
- for (let j = i + 1; j < lines.length; j++) {
879
- const prev2 = lines[j - 1];
880
- const curr2 = lines[j];
881
- const len2 = prev2.distance(curr2);
882
- if (len2 < this.width * 0.8) count++;
883
- else break;
884
- }
885
- if (count === 0 && i + count === lines.length - 1) ;
886
- else if (i == 1 && count === 1) ;
887
- else if (count === 3) {
888
- filterLines.push(lines[i + 1]);
889
- i += count;
890
- } else if (count === 5) {
891
- filterLines.push(lines[i + 2]);
892
- i += count;
893
- } else {
894
- filterLines.push(curr);
895
- }
896
- } else {
897
- filterLines.push(curr);
898
- }
899
- }
900
- return filterLines;
901
- });
902
- }
903
- /** 线偏移
904
- * @description 使用 ClipperLib 对每个点组进行线偏移处理,生成具有指定宽度的墙体路径
905
- */
906
- lineOffset(endType = Dxf.EndType.etOpenSquare, joinType = Dxf.JoinType.jtMiter, scale = 1e4) {
907
- const solutions = new ClipperLib.Paths();
908
- const offset = new ClipperLib.ClipperOffset(20, 0.25);
909
- this.pointsGroups.forEach((points) => {
910
- const linePaths = this.lineTopology(points).map((linePath) => linePath.map((p) => p.clone().mutiplyScalar(scale)));
911
- offset.AddPaths(linePaths, joinType, endType);
912
- });
913
- offset.Execute(solutions, this.width / 2 * scale);
914
- offset.Execute(solutions, this.width / 2 * scale);
915
- this.wallsGroup = solutions.map((ps) => ps.map((p) => Point.from(p).divisionScalar(scale)));
916
- if (endType == Dxf.EndType.etOpenSquare) this.squareRemoveBurr();
917
- this.dispatchEvent({
918
- type: "lineOffset",
919
- wallsGroup: this.wallsGroup
920
- });
921
- return this.wallsGroup;
922
- }
923
- /**
924
- * 将点云结构转换为Float32Array
925
- */
926
- to3DArray(scale) {
927
- const array = [];
928
- this.wallsGroup.forEach((points) => {
929
- for (let i = 0; i < points.length; i++) {
930
- const point1 = points[i];
931
- const nextIndex = i === points.length - 1 ? 0 : i + 1;
932
- const point2 = points[nextIndex];
933
- array.push(point1.X * scale, point1.Y * scale, this.originalZAverage, point2.X * scale, point2.Y * scale, this.originalZAverage);
934
- }
935
- });
936
- return new Float32Array(array);
937
- }
938
- /**
939
- * 将点云结构转换为string
940
- */
941
- toDxfString(unit = "Millimeters") {
942
- const d = new Drawing();
943
- d.setUnits("Millimeters");
944
- const s = units[unit];
945
- this.wallsGroup.forEach((points) => {
946
- for (let i = 0; i < points.length; i++) {
947
- const point1 = points[i];
948
- const nextIndex = i === points.length - 1 ? 0 : i + 1;
949
- const point2 = points[nextIndex];
950
- d.drawLine(point1.X * s, point1.Y * s, point2.X * s, point2.Y * s);
951
- }
952
- });
953
- return d.toDxfString();
954
- }
955
- /**
956
- * 将点云结构转换为DXF格式
957
- * @returns
958
- */
959
- toDxfBlob(unit = "Millimeters") {
960
- const blob = new Blob([this.toDxfString(unit)]);
961
- return blob;
962
- }
963
- /**
964
- * 下载
965
- * @param filename
966
- */
967
- async download(filename, unit = "Millimeters") {
968
- if (typeof window !== "undefined") {
969
- const blob = this.toDxfBlob(unit);
970
- const a = document.createElement("a");
971
- a.href = URL.createObjectURL(blob);
972
- a.download = filename + ".dxf";
973
- a.click();
974
- } else if (typeof global !== "undefined") {
975
- const packageName = "fs";
976
- const { default: fs } = await import(
977
- /* @vite-ignore */
978
- packageName
979
- );
980
- fs.writeFileSync(filename, this.toDxfString(unit));
981
- }
982
- }
983
- /**
984
- * 计算原始数据的边界框
985
- * @description 计算所有线段的起点和终点的最小最大值,形成一个边界框
986
- * @returns
987
- */
988
- computedOriginalSize(data, originalBox = new Box2(0, 0, 0, 0)) {
989
- const xArr = data.flatMap((item) => [item.start.x, item.end.x]);
990
- const yArr = data.flatMap((item) => [item.start.y, item.end.y]);
991
- const minX = Math.min(...xArr);
992
- const minY = Math.min(...yArr);
993
- const maxX = Math.max(...xArr);
994
- const maxY = Math.max(...yArr);
995
- originalBox.set(minX, minY, maxX, maxY);
996
- return originalBox;
997
- }
998
- /**
999
- * 创建数据
1000
- * @param pointsGroups
1001
- * @returns
1002
- */
1003
- static createData(pointsGroups, sealed = true) {
1004
- let count = 0;
1005
- const data = pointsGroups.flatMap((points) => {
1006
- const lines = points.map((point, index) => {
1007
- const nextIndex = index === points.length - 1 ? 0 : index + 1;
1008
- const nextPoint = points[nextIndex];
1009
- return {
1010
- start: { x: point.x, y: point.y },
1011
- end: { x: nextPoint.x, y: nextPoint.y },
1012
- insetionArr: [
1013
- {
1014
- index: nextIndex + count
1015
- }
1016
- ]
1017
- };
1018
- });
1019
- count += points.length;
1020
- if (!sealed) {
1021
- lines.pop();
1022
- lines[lines.length - 1].insetionArr.length = 0;
1023
- count--;
1024
- }
1025
- return lines;
1026
- });
1027
- return data;
1028
- }
1029
- }
1030
- class Variable extends Component {
1031
- originalLineVisible = true;
1032
- dxfVisible = true;
1033
- whiteModelVisible = true;
1034
- isLook = false;
1035
- currentWheel = 0;
1036
- pointerMove = { x: 0, y: 0 };
1037
- currentKeyUp = "";
1038
- set(key, value) {
1039
- if (key in this) {
1040
- const oldValue = this[key];
1041
- this[key] = value;
1042
- this.dispatchEvent({
1043
- type: key,
1044
- value,
1045
- oldValue
1046
- });
1047
- }
1048
- }
1049
- get(key) {
1050
- if (key in this) return this[key];
1051
- }
1052
- }
1053
- class Rectangle {
1054
- points;
1055
- get path() {
1056
- return this.points.flatMap((p, i) => {
1057
- const np = this.points[(i + 1) % this.points.length];
1058
- return [p.x, p.y, 0, np.x, np.y, 0];
1059
- });
1060
- }
1061
- constructor(points) {
1062
- if (points.length !== 4) {
1063
- throw new Error("Rectangle must be defined by exactly 4 points");
1064
- }
1065
- this.points = points;
1066
- }
1067
- /**
1068
- * 判断线段是否与矩形相交
1069
- * @param line 线段
1070
- * @returns 是否与矩形相交
1071
- */
1072
- intersectLineSegment(line) {
1073
- if (line.points.length !== 2) {
1074
- throw new Error("LineSegment must have exactly 2 points");
1075
- }
1076
- const [p1, p2] = line.points;
1077
- const doIntersect = (l1p1, l1p2, l2p1, l2p2) => {
1078
- const orientation = (p, q, r) => {
1079
- const val = (q.y - p.y) * (r.x - q.x) - (q.x - p.x) * (r.y - q.y);
1080
- if (val === 0) return 0;
1081
- return val > 0 ? 1 : 2;
1082
- };
1083
- const onSegment = (p, q, r) => {
1084
- return Math.min(p.x, r.x) <= q.x && q.x <= Math.max(p.x, r.x) && Math.min(p.y, r.y) <= q.y && q.y <= Math.max(p.y, r.y);
1085
- };
1086
- const o1 = orientation(l1p1, l1p2, l2p1);
1087
- const o2 = orientation(l1p1, l1p2, l2p2);
1088
- const o3 = orientation(l2p1, l2p2, l1p1);
1089
- const o4 = orientation(l2p1, l2p2, l1p2);
1090
- if (o1 !== o2 && o3 !== o4) return true;
1091
- if (o1 === 0 && onSegment(l1p1, l2p1, l1p2)) return true;
1092
- if (o2 === 0 && onSegment(l1p1, l2p2, l1p2)) return true;
1093
- if (o3 === 0 && onSegment(l2p1, l1p1, l2p2)) return true;
1094
- if (o4 === 0 && onSegment(l2p1, l1p2, l2p2)) return true;
1095
- return false;
1096
- };
1097
- for (let i = 0; i < 4; i++) {
1098
- const rectP1 = this.points[i];
1099
- const rectP2 = this.points[(i + 1) % 4];
1100
- if (doIntersect(p1, p2, rectP1, rectP2)) {
1101
- return true;
1102
- }
1103
- }
1104
- if (this.containsLineSegment(line)) {
1105
- return true;
1106
- }
1107
- return false;
1108
- }
1109
- /**
1110
- * 判断线段是否完全位于矩形内部
1111
- * @param line 线段
1112
- * @returns 是否完全在矩形内部
1113
- */
1114
- containsLineSegment(line) {
1115
- if (line.points.length !== 2) {
1116
- throw new Error("LineSegment must have exactly 2 points");
1117
- }
1118
- const isPointInRectangle = (point) => {
1119
- let sign = 0;
1120
- for (let i = 0; i < 4; i++) {
1121
- const p1 = this.points[i];
1122
- const p2 = this.points[(i + 1) % 4];
1123
- const edge = { x: p2.x - p1.x, y: p2.y - p1.y };
1124
- const toPoint = { x: point.x - p1.x, y: point.y - p1.y };
1125
- const cross = edge.x * toPoint.y - edge.y * toPoint.x;
1126
- if (cross === 0) {
1127
- const t = edge.x !== 0 ? (point.x - p1.x) / edge.x : (point.y - p1.y) / edge.y;
1128
- if (t >= 0 && t <= 1) return true;
1129
- } else {
1130
- const currentSign = cross > 0 ? 1 : -1;
1131
- if (sign === 0) sign = currentSign;
1132
- if (sign !== currentSign) return false;
1133
- }
1134
- }
1135
- return true;
1136
- };
1137
- return isPointInRectangle(line.points[0]) && isPointInRectangle(line.points[1]);
1138
- }
1139
- /**
1140
- * 判断点是否完全位于矩形内部
1141
- * @param point
1142
- */
1143
- containsPoint(point) {
1144
- let positiveCount = 0;
1145
- let negativeCount = 0;
1146
- for (let i = 0; i < 4; i++) {
1147
- const p1 = this.points[i];
1148
- const p2 = this.points[(i + 1) % 4];
1149
- const cross = (p2.x - p1.x) * (point.y - p1.y) - (p2.y - p1.y) * (point.x - p1.x);
1150
- if (cross > 0) positiveCount++;
1151
- else if (cross < 0) negativeCount++;
1152
- else return false;
1153
- }
1154
- return positiveCount === 4 || negativeCount === 4;
1155
- }
1156
- /**
1157
- *
1158
- * @returns
1159
- */
1160
- toBox() {
1161
- let minX = Infinity, maxX = -Infinity, minY = Infinity, maxY = -Infinity;
1162
- this.points.forEach((p) => {
1163
- maxX = Math.max(p.x, maxX);
1164
- minX = Math.min(p.x, minX);
1165
- maxY = Math.max(p.x, maxY);
1166
- minY = Math.min(p.x, minY);
1167
- });
1168
- return new Box2(minX, maxX, minY, maxY);
1169
- }
1170
- /**
1171
- *
1172
- * @param line
1173
- * @param width
1174
- * @returns
1175
- */
1176
- static fromByLineSegment(line, width = 0.1, horizontal = false, hScale = 0.5) {
1177
- const p1 = line.points[0], p2 = line.points[1], normal = p2.normal(p1), pDirect = horizontal ? p2.direction(p1).mutiplyScalar(width * hScale) : Point.zero(), nDirect = horizontal ? p1.direction(p2).mutiplyScalar(width * hScale) : Point.zero();
1178
- const offsetX = normal.x * width * 0.5;
1179
- const offsetY = normal.y * width * 0.5;
1180
- return new Rectangle([
1181
- new Point(p1.x + offsetX, p1.y + offsetY).add(nDirect),
1182
- new Point(p2.x + offsetX, p2.y + offsetY).add(pDirect),
1183
- new Point(p2.x - offsetX, p2.y - offsetY).add(pDirect),
1184
- new Point(p1.x - offsetX, p1.y - offsetY).add(nDirect)
1185
- ]);
1186
- }
1187
- }
1188
- class Quadtree {
1189
- bounds;
1190
- // 包围盒
1191
- capacity;
1192
- // 节点容量
1193
- maxDepth;
1194
- // 最大深度
1195
- depth;
1196
- // 当前深度
1197
- isLeaf = true;
1198
- // 是否为叶子节点
1199
- children = null;
1200
- // 子节点数组
1201
- nodes = [];
1202
- // 存储的节点
1203
- color = [Math.random(), Math.random(), Math.random()];
1204
- // 颜色
1205
- constructor(bounds, capacity = 8, maxDepth = 10, depth = 1) {
1206
- this.bounds = bounds;
1207
- this.capacity = capacity;
1208
- this.depth = depth;
1209
- this.maxDepth = maxDepth;
1210
- }
1211
- /**
1212
- * 插入线段节点
1213
- * @param node 线段节点
1214
- */
1215
- insert(node) {
1216
- if (!this.isLeaf) {
1217
- const quadrant = this.getQuadrant(node.line);
1218
- if (quadrant !== -1) {
1219
- this.children[quadrant].insert(node);
1220
- return;
1221
- }
1222
- }
1223
- this.nodes.push(node);
1224
- if (this.isLeaf && this.nodes.length > this.capacity && this.depth < this.maxDepth) {
1225
- this.subdivide();
1226
- const nodes = this.nodes;
1227
- this.nodes = [];
1228
- for (const n of nodes) {
1229
- const quadrant = this.getQuadrant(n.line);
1230
- if (quadrant !== -1) {
1231
- this.children[quadrant].insert(n);
1232
- } else {
1233
- this.nodes.push(n);
1234
- }
1235
- }
1236
- }
1237
- }
1238
- /**
1239
- * 获取线段所属的象限
1240
- * @param line 线段
1241
- * @returns 象限索引(0:西北,1:东北,2:西南,3:东南)或-1(跨多个象限)
1242
- */
1243
- getQuadrant(line) {
1244
- const intersectsNW = this.children[0].bounds.intersectLineSegment(line);
1245
- const intersectsNE = this.children[1].bounds.intersectLineSegment(line);
1246
- const intersectsSW = this.children[2].bounds.intersectLineSegment(line);
1247
- const intersectsSE = this.children[3].bounds.intersectLineSegment(line);
1248
- let count = 0;
1249
- let quadrant = -1;
1250
- if (intersectsNW) {
1251
- count++;
1252
- quadrant = 0;
1253
- }
1254
- if (intersectsNE) {
1255
- count++;
1256
- quadrant = 1;
1257
- }
1258
- if (intersectsSW) {
1259
- count++;
1260
- quadrant = 2;
1261
- }
1262
- if (intersectsSE) {
1263
- count++;
1264
- quadrant = 3;
1265
- }
1266
- if (count === 1) return quadrant;
1267
- return -1;
1268
- }
1269
- /**
1270
- * 细分当前节点为四个子节点
1271
- */
1272
- subdivide() {
1273
- if (!this.isLeaf) return;
1274
- this.isLeaf = false;
1275
- this.children = [];
1276
- const midX = (this.bounds.minX + this.bounds.maxX) / 2;
1277
- const midY = (this.bounds.minY + this.bounds.maxY) / 2;
1278
- this.children[0] = new Quadtree(
1279
- new Box2(this.bounds.minX, midX, this.bounds.minY, midY),
1280
- this.capacity,
1281
- this.maxDepth,
1282
- this.depth + 1
1283
- );
1284
- this.children[1] = new Quadtree(
1285
- new Box2(midX, this.bounds.maxX, this.bounds.minY, midY),
1286
- this.capacity,
1287
- this.maxDepth,
1288
- this.depth + 1
1289
- );
1290
- this.children[2] = new Quadtree(
1291
- new Box2(this.bounds.minX, midX, midY, this.bounds.maxY),
1292
- this.capacity,
1293
- this.maxDepth,
1294
- this.depth + 1
1295
- );
1296
- this.children[3] = new Quadtree(
1297
- new Box2(midX, this.bounds.maxX, midY, this.bounds.maxY),
1298
- this.capacity,
1299
- this.maxDepth,
1300
- this.depth + 1
1301
- );
1302
- }
1303
- /**
1304
- * 查询与包围盒相交的线段节点
1305
- * @param box2 包围盒
1306
- * @returns 相交的节点数组
1307
- */
1308
- queryBox(box2) {
1309
- const result = [];
1310
- if (!this.bounds.intersectBox(box2)) {
1311
- return result;
1312
- }
1313
- for (const node of this.nodes) {
1314
- if (box2.intersectLineSegment(node.line)) {
1315
- result.push(node);
1316
- }
1317
- }
1318
- if (!this.isLeaf) {
1319
- for (const child of this.children) {
1320
- result.push(...child.queryBox(box2));
1321
- }
1322
- }
1323
- return result;
1324
- }
1325
- /**
1326
- * 查询与圆形区域相交的线段节点
1327
- * @param pos 圆心
1328
- * @param radius 半径
1329
- * @returns 相交的节点数组
1330
- */
1331
- queryCircle(pos, radius) {
1332
- const result = [];
1333
- const circleBox = new Box2(
1334
- pos.x - radius,
1335
- pos.x + radius,
1336
- pos.y - radius,
1337
- pos.y + radius
1338
- );
1339
- if (!this.bounds.intersectBox(circleBox)) {
1340
- return result;
1341
- }
1342
- for (const node of this.nodes) {
1343
- const [p1, p2] = node.line.points;
1344
- const dx = p2.x - p1.x;
1345
- const dy = p2.y - p1.y;
1346
- const l2 = dx * dx + dy * dy;
1347
- let t = ((pos.x - p1.x) * dx + (pos.y - p1.y) * dy) / l2;
1348
- t = Math.max(0, Math.min(1, t));
1349
- const closestX = p1.x + t * dx;
1350
- const closestY = p1.y + t * dy;
1351
- const distance = pos.distance(new Point(closestX, closestY));
1352
- if (distance <= radius) {
1353
- result.push(node);
1354
- }
1355
- }
1356
- if (!this.isLeaf) {
1357
- for (const child of this.children) {
1358
- result.push(...child.queryCircle(pos, radius));
1359
- }
1360
- }
1361
- return result;
1362
- }
1363
- /**
1364
- * 查询与矩形相交的线段节点
1365
- * @param rectangle 矩形
1366
- * @returns 相交的节点数组
1367
- */
1368
- queryRect(rectangle) {
1369
- const result = [];
1370
- if (!this.bounds.intersectRectangle(rectangle)) {
1371
- return result;
1372
- }
1373
- for (const node of this.nodes) {
1374
- if (rectangle.intersectLineSegment(node.line)) {
1375
- result.push(node);
1376
- }
1377
- }
1378
- if (!this.isLeaf) {
1379
- for (const child of this.children) {
1380
- result.push(...child.queryRect(rectangle));
1381
- }
1382
- }
1383
- return result;
1384
- }
1385
- /**
1386
- * 包围盒转换为数组
1387
- * @param array
1388
- * @param colors
1389
- * @returns
1390
- */
1391
- boundsToArray(array = [], colors, recursion = true) {
1392
- if (!this.isLeaf && recursion) {
1393
- this.children?.forEach((child) => child.boundsToArray(array, colors));
1394
- }
1395
- array.push(...this.bounds.points.flatMap((p, i, array2) => {
1396
- const np = array2[(i + 1) % array2.length];
1397
- colors?.push(...this.color);
1398
- colors?.push(...this.color);
1399
- return [p.x, p.y, 0, np.x, np.y, 0];
1400
- }));
1401
- return array;
1402
- }
1403
- }
1404
- class PointVirtualGrid {
1405
- map = /* @__PURE__ */ new Map();
1406
- gridSize;
1407
- constructor(gridSize = 2) {
1408
- this.gridSize = gridSize;
1409
- }
1410
- /**
1411
- * 插入
1412
- * @param point
1413
- * @param userData
1414
- */
1415
- insert(point, userData) {
1416
- if (!point || isNaN(point.x) || isNaN(point.y)) {
1417
- throw new Error("无效的点坐标");
1418
- }
1419
- const id = this.getGridId(point);
1420
- if (!this.map.has(id)) this.map.set(id, /* @__PURE__ */ new Set());
1421
- const set = this.map.get(id);
1422
- set?.add({ point, userData });
1423
- }
1424
- /**
1425
- * 批量加入
1426
- * @param points
1427
- */
1428
- insertBatch(points) {
1429
- for (const { point, userData } of points) {
1430
- this.insert(point, userData);
1431
- }
1432
- }
1433
- /**
1434
- * 获取通过坐标,获取唯一网格索引
1435
- * @param point
1436
- * @returns
1437
- */
1438
- getGridId(point) {
1439
- const i = Math.ceil(point.x / this.gridSize), j = Math.ceil(point.y / this.gridSize);
1440
- return `${i}.${j}`;
1441
- }
1442
- /**
1443
- *
1444
- * @param gridId
1445
- * @returns
1446
- */
1447
- decodeGridId(gridId) {
1448
- const [i, j] = gridId.split(".").map(Number);
1449
- return new Point(i, j);
1450
- }
1451
- /**
1452
- * 查询与矩形相交的点
1453
- * @param rectangle 矩形
1454
- * @returns 相交的节点数组
1455
- */
1456
- queryRect(rectangle) {
1457
- const box2 = rectangle.toBox();
1458
- const minI = Math.ceil(box2.minX / this.gridSize), maxI = Math.ceil(box2.maxX / this.gridSize), minJ = Math.ceil(box2.minY / this.gridSize), maxJ = Math.ceil(box2.maxY / this.gridSize);
1459
- for (let i = minI; i <= maxI; i++) {
1460
- for (let j = minJ; j <= maxJ; j++) {
1461
- const id = `${i}.${j}`;
1462
- if (!this.map.has(id)) continue;
1463
- const set = this.map.get(id);
1464
- set?.forEach((item) => {
1465
- if (rectangle.containsPoint(item.point)) ;
1466
- });
1467
- }
1468
- }
1469
- }
1470
- /**
1471
- * 查询与圆形区域相交的点
1472
- * @param pos 圆心
1473
- * @param radius 半径
1474
- * @returns 相交的节点数组
1475
- */
1476
- queryCircle(pos, radius) {
1477
- const box2 = new Box2(pos.x - radius, pos.x + radius, pos.y - radius, pos.y + radius);
1478
- const minI = Math.ceil(box2.minX / this.gridSize), maxI = Math.ceil(box2.maxX / this.gridSize), minJ = Math.ceil(box2.minY / this.gridSize), maxJ = Math.ceil(box2.maxY / this.gridSize), list = [];
1479
- for (let i = minI; i <= maxI; i++) {
1480
- for (let j = minJ; j <= maxJ; j++) {
1481
- const id = `${i}.${j}`;
1482
- if (!this.map.has(id)) continue;
1483
- const set = this.map.get(id);
1484
- set?.forEach((item) => {
1485
- if (pos.distance(item.point) <= radius) list.push(item);
1486
- });
1487
- }
1488
- }
1489
- return list;
1490
- }
1491
- /**
1492
- * 查询与包围盒相交的点
1493
- * @param box2 包围盒
1494
- * @returns 相交的节点数组
1495
- */
1496
- queryBox(box2) {
1497
- const minI = Math.ceil(box2.minX / this.gridSize), maxI = Math.ceil(box2.maxX / this.gridSize), minJ = Math.ceil(box2.minY / this.gridSize), maxJ = Math.ceil(box2.maxY / this.gridSize), list = [];
1498
- for (let i = minI; i <= maxI; i++) {
1499
- for (let j = minJ; j <= maxJ; j++) {
1500
- const id = `${i}.${j}`;
1501
- if (!this.map.has(id)) continue;
1502
- const set = this.map.get(id);
1503
- set?.forEach((item) => {
1504
- if (box2.containsPoint(item.point)) list.push(item);
1505
- });
1506
- }
1507
- }
1508
- return list;
1509
- }
1510
- /**
1511
- * 查找相同点
1512
- * @param point
1513
- */
1514
- queryPoint(point) {
1515
- const id = this.getGridId(point), list = [];
1516
- if (this.map.has(id)) {
1517
- const set = this.map.get(id);
1518
- set?.forEach((item) => {
1519
- if (point.equal(item.point)) list.push(item);
1520
- });
1521
- }
1522
- return list;
1523
- }
1524
- }
1525
- class LineAnalysis extends Component {
1526
- Dxf = null;
1527
- Variable = null;
1528
- lineSegmentList = [];
1529
- container = new THREE.Group();
1530
- // 误差角度
1531
- errorAngle = 4;
1532
- width = 0.4;
1533
- /**
1534
- *
1535
- * @param parent
1536
- */
1537
- onAddFromParent(parent) {
1538
- this.Dxf = parent.findComponentByType(Dxf);
1539
- this.Variable = this.parent?.findComponentByType(Variable);
1540
- this.Dxf.addEventListener("setDta", this.lineAnalysis.bind(this));
1541
- this.Dxf.addEventListener("lineOffset", this.duplicatePointFiltering.bind(this));
1542
- }
1543
- /**
1544
- * 去除路径上重复的点
1545
- * @description 判断方向向量,一个连续的方向上,只应该出现两个点
1546
- */
1547
- duplicatePointFiltering() {
1548
- const dxf = this.Dxf;
1549
- dxf.wallsGroup = dxf.wallsGroup.map((points) => {
1550
- const filterPoints = [];
1551
- points.forEach((point, index) => {
1552
- if (index < 1 || points.length - 1 === index) return filterPoints.push(point);
1553
- const preP = points[index - 1], nextP = points[index + 1], direct1 = point.direction(preP), direct2 = nextP.direction(point), angle = direct1.angleBetween(direct2) / (Math.PI / 180);
1554
- if (angle > 0.02 && angle < 180 - 0.02) filterPoints.push(point);
1555
- });
1556
- points = [filterPoints[0]];
1557
- for (let i = 1; i < filterPoints.length; i++) {
1558
- const point = filterPoints[i];
1559
- const nextP = filterPoints[i - 1];
1560
- if (nextP.distance(point) < dxf.width * 0.5) ;
1561
- points.push(point);
1562
- }
1563
- return points;
1564
- });
1565
- dxf.wallsGroup = dxf.wallsGroup.filter((i) => i.length >= 4);
1566
- }
1567
- /**
1568
- *
1569
- * @param p1
1570
- * @param p2
1571
- * @param width
1572
- * @returns
1573
- */
1574
- expandLineSegment(p1, p2, width = 0.1) {
1575
- const normal = p2.normal(p1);
1576
- const pDirect = p2.direction(p1).mutiplyScalar(width * 0.5);
1577
- const nDirect = p1.direction(p2).mutiplyScalar(width * 0.5);
1578
- const offsetX = normal.x * width * 0.5;
1579
- const offsetY = normal.y * width * 0.5;
1580
- return {
1581
- points: [
1582
- // 第一条线
1583
- new Point(p1.x + offsetX, p1.y + offsetY).add(nDirect),
1584
- new Point(p2.x + offsetX, p2.y + offsetY).add(pDirect),
1585
- // 第二条线
1586
- new Point(p1.x - offsetX, p1.y - offsetY).add(nDirect),
1587
- new Point(p2.x - offsetX, p2.y - offsetY).add(pDirect)
1588
- ],
1589
- indices: [0, 1, 1, 3, 3, 2, 2, 0],
1590
- rectIndices: [0, 1, 3, 2, 0]
1591
- };
1592
- }
1593
- appendLineSegmentList = [];
1594
- /**
1595
- * 追加数据
1596
- * @param p1
1597
- * @param p2
1598
- */
1599
- addData(p1, p2) {
1600
- const dxf = this.Dxf;
1601
- dxf.data.push([p1.clone(), p2.clone(), [], false, dxf.data.length]);
1602
- this.appendLineSegmentList.push(new LineSegment(p1.clone(), p2.clone()));
1603
- }
1604
- /** 结果分析创建矩形
1605
- * @param result
1606
- */
1607
- createRectangle(result) {
1608
- const dxf = this.Dxf;
1609
- const project0 = result.project, project1 = result.project2;
1610
- this.addData(project0.points[0], project1.points[0]);
1611
- this.addData(project0.points[1], project1.points[1]);
1612
- const leftHeight = project0.points[0].distance(project1.points[0]), rightHeight = project0.points[1].distance(project1.points[1]), count = Math.ceil(Math.max(leftHeight, rightHeight) / dxf.width), leftFragment = leftHeight / count, rightFragment = rightHeight / count, leftDirection = project1.points[0].direction(project0.points[0]), rightDirection = project1.points[1].direction(project0.points[1]), leftP = project0.points[0].clone(), rightP = project0.points[1].clone(), direction = rightP.direction(leftP);
1613
- direction.multiplyScalar(dxf.width * 0.5);
1614
- const _leftP = leftP.clone().add(direction), _rightP = rightP.clone().add(direction.multiplyScalar(-1)), d1 = leftP.direction(rightP), d2 = _leftP.direction(_rightP);
1615
- if (d1.x > 0 && d2.x < 0 || d1.x < 0 && d2.x > 0 || d1.y > 0 && d2.y < 0 || d1.y < 0 && d2.y > 0) return;
1616
- leftP.set(_leftP.x, _leftP.y);
1617
- rightP.set(_rightP.x, _rightP.y);
1618
- for (let i = 1; i < count; i++) {
1619
- const left = leftDirection.clone().multiplyScalar(leftFragment * i), right = rightDirection.clone().multiplyScalar(rightFragment * i), p1 = leftP.clone().add(left), p2 = rightP.clone().add(right);
1620
- this.addData(p1, p2);
1621
- }
1622
- }
1623
- pointVirtualGrid = new PointVirtualGrid();
1624
- /**
1625
- * 构建点的虚拟网格索引
1626
- */
1627
- buildVirtualGrid() {
1628
- const dxf = this.Dxf;
1629
- const pointVirtualGrid = new PointVirtualGrid();
1630
- dxf.originalData.forEach((d, index) => {
1631
- const [p1, p2] = [Point.from(d.start), Point.from(d.end)];
1632
- pointVirtualGrid.insert(p1, { index, type: "start" });
1633
- pointVirtualGrid.insert(p2, { index, type: "end" });
1634
- });
1635
- this.pointVirtualGrid = pointVirtualGrid;
1636
- }
1637
- quadtree;
1638
- /**
1639
- * 构建线段四叉树,快速查找,
1640
- */
1641
- buildQuadtree() {
1642
- const dxf = this.Dxf;
1643
- const lineSegmentList = [];
1644
- this.quadtree = new Quadtree(dxf.originalBox, 2);
1645
- dxf.lineSegments.forEach((lineSegment) => {
1646
- if (lineSegment.userData?.isDoor) return;
1647
- this.quadtree?.insert({
1648
- line: lineSegment,
1649
- userData: lineSegmentList.length
1650
- });
1651
- lineSegmentList.push(lineSegment);
1652
- });
1653
- this.lineSegmentList = lineSegmentList;
1654
- }
1655
- resultList = [];
1656
- /** 线段分析
1657
- * @description 判断两条线段距离是否较短且趋近平行,然后查找两条线段的重合部分的投影线,以此判断两根线是否需要合并
1658
- * @param data
1659
- */
1660
- lineAnalysis() {
1661
- this.buildQuadtree();
1662
- this.buildVirtualGrid();
1663
- const quadtree = this.quadtree;
1664
- const lineSegmentList = this.lineSegmentList;
1665
- const visited = /* @__PURE__ */ new Set(), resultList = [];
1666
- lineSegmentList.forEach((_0, i) => {
1667
- const sourceLineSegment = lineSegmentList[i], rectangle = Rectangle.fromByLineSegment(sourceLineSegment, this.width * 2, false, -0.01), ids = quadtree.queryRect(rectangle).map((i2) => i2.userData).filter((index) => index !== i);
1668
- ids.forEach((id) => {
1669
- try {
1670
- if (visited.has(`${i}-${id}`) || visited.has(`${id}-${i}`)) return;
1671
- const res = this.projectionAnalysis(id, i, sourceLineSegment, lineSegmentList);
1672
- if (res) resultList.push(res);
1673
- visited.add(`${i}-${id}`);
1674
- } catch (error) {
1675
- }
1676
- });
1677
- });
1678
- this.appendLineSegmentList.length = 0;
1679
- resultList.forEach(this.createRectangle.bind(this));
1680
- this.resultList = [];
1681
- }
1682
- /** 线段投影分析
1683
- * @param index
1684
- * @param sourceLineSegment
1685
- * @param lineSegmentList
1686
- * @returns
1687
- */
1688
- projectionAnalysis(index, sourceIndex, sourceLineSegment, lineSegmentList) {
1689
- const temLineSegment = lineSegmentList[index], direct = sourceLineSegment.direction(), temDirect = temLineSegment.direction(), angle = direct.angleBetween(temDirect) / (Math.PI / 180);
1690
- if (angle < this.errorAngle || angle > 180 - this.errorAngle) {
1691
- let data;
1692
- const p1 = temLineSegment.projectLineSegment(sourceLineSegment), p2 = sourceLineSegment.projectLineSegment(temLineSegment), d1 = p1.direction(), d2 = p2.direction();
1693
- if (d1.x > 0 && d2.x < 0 || d1.x < 0 && d2.x > 0 || d1.y > 0 && d2.y < 0 || d1.y < 0 && d2.y > 0) {
1694
- p1.points = [p1.points[1], p1.points[0]];
1695
- }
1696
- if (p1.getLength() > p2.getLength()) {
1697
- data = {
1698
- target: temLineSegment,
1699
- targetIndex: index,
1700
- source: sourceLineSegment,
1701
- sourceIndex,
1702
- project: p1,
1703
- project2: p2
1704
- };
1705
- } else {
1706
- data = {
1707
- target: sourceLineSegment,
1708
- targetIndex: sourceIndex,
1709
- source: temLineSegment,
1710
- sourceIndex: index,
1711
- project: p2,
1712
- project2: p1
1713
- };
1714
- }
1715
- if (!data || data.project.getLength() < 0.01) return;
1716
- return data;
1717
- }
1718
- }
1719
- }
1720
- class DxfSystem extends ComponentManager {
1721
- Dxf;
1722
- Variable;
1723
- wallWidth;
1724
- environment;
1725
- /** 构造函数
1726
- * @param wallWidth 输出墙壁厚度,该墙壁厚度不受缩放影响
1727
- * @param scale 原始数据缩放比例
1728
- */
1729
- constructor(wallWidth = 0.1, scale = 1) {
1730
- super();
1731
- this.environment = typeof window !== "undefined" ? "browser" : typeof global !== "undefined" ? "node" : "unknown";
1732
- this.wallWidth = wallWidth;
1733
- this.Dxf = new Dxf(this.wallWidth, scale);
1734
- this.Variable = new Variable();
1735
- this.addComponent(this.Variable);
1736
- this.addComponent(this.Dxf);
1737
- this.addComponent(new LineAnalysis());
1738
- }
1739
- usePlugin(plugin) {
1740
- if (typeof plugin === "function") plugin.call(this, this);
1741
- return this;
1742
- }
1743
- destroy() {
1744
- this.components.forEach((com) => {
1745
- this.removeComponent(com);
1746
- com.destroy();
1747
- });
1748
- }
1749
- }
1
+ import { D, i, l } from "./build.js";
1750
2
  export {
1751
- DxfSystem
3
+ D as DxfSystem,
4
+ i as ModelDataPlugin,
5
+ l as loadRenderPlugin
1752
6
  };