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