build-dxf 0.0.9 → 0.0.11

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