From 128348828db63505f33eef21a599aa3a473180ac Mon Sep 17 00:00:00 2001 From: ChenX Date: Sun, 27 Jan 2019 11:32:56 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E6=AD=A3Polyline.Join,=E8=A1=A5?= =?UTF-8?q?=E5=85=A8=E6=B5=8B=E8=AF=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- __test__/Polyline/join.test.ts | 42 +++++++++++++++++++++ src/DatabaseServices/Polyline.ts | 64 ++++++++++++++++++++++---------- 2 files changed, 86 insertions(+), 20 deletions(-) create mode 100644 __test__/Polyline/join.test.ts diff --git a/__test__/Polyline/join.test.ts b/__test__/Polyline/join.test.ts new file mode 100644 index 000000000..2f2023088 --- /dev/null +++ b/__test__/Polyline/join.test.ts @@ -0,0 +1,42 @@ +import { LoadEntityFromFileData } from "../Utils/LoadEntity.util"; +import { Polyline } from "../../src/DatabaseServices/Polyline"; +import { equaln } from "../../src/Geometry/GeUtils"; + +test('多段线与多段线Join', () => +{ + let data = + [2, "Polyline", 2, 1, 102, false, 7, 0, [1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1], 1, 2, 4, [69.1964285714286, -106.02678571428574], 0, [-60.26785714285711, 58.03571428571427], 0.4847381535042383, [-241.07142857142856, 58.03571428571427], -1.124793513518748, [-340.9598214285714, 157.9241071428571], 0, false, "Polyline", 2, 1, 103, false, 7, 0, [1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1], 1, 2, 4, [69.1964285714286, -106.02678571428574], 0, [181.36160714285717, 6.138392857142875], -0.7890083669838478, [389.50892857142867, -121.65178571428574], 1.286428411698261, [556.9196428571429, -132.81250000000003], 0, false] + + let pls = LoadEntityFromFileData(data) as Polyline[]; + + let pl1 = pls[0].Clone(); + let pl2 = pls[1].Clone(); + + let length = pl1.Length + pl2.Length; + + //sp sp + expect(pl1.Join(pl2)).toBe(1); + expect(equaln(pl1.Length, length)).toBeTruthy(); + + pl1 = pls[0].Clone(); + pl2 = pls[1].Clone(); + pl2.Reverse(); + //sp ep + expect(pl1.Join(pl2)).toBe(1); + expect(equaln(pl1.Length, length)).toBeTruthy(); + + pl1 = pls[0].Clone(); + pl2 = pls[1].Clone(); + pl1.Reverse(); + //ep sp + expect(pl1.Join(pl2)).toBe(1); + expect(equaln(pl1.Length, length)).toBeTruthy(); + + pl1 = pls[0].Clone(); + pl2 = pls[1].Clone(); + pl1.Reverse(); + pl2.Reverse(); + //ep ep + expect(pl1.Join(pl2)).toBe(1); + expect(equaln(pl1.Length, length)).toBeTruthy(); +}); diff --git a/src/DatabaseServices/Polyline.ts b/src/DatabaseServices/Polyline.ts index 41d1c0fdd..cc6adcb05 100644 --- a/src/DatabaseServices/Polyline.ts +++ b/src/DatabaseServices/Polyline.ts @@ -4,7 +4,7 @@ import { CreateBoardUtil } from '../ApplicationServices/mesh/createBoard'; import { arrayLast, arrayRemoveDuplicateBySort, changeArrayStartIndex } from '../Common/ArrayExt'; import { ColorMaterial } from '../Common/ColorPalette'; import { getDeterminantFor2V, Vec2DTo3D, Vec3DTo2D } from '../Common/CurveUtils'; -import { matrixAlignCoordSys, reviseMirrorMatrix } from '../Common/Matrix4Utils'; +import { matrixAlignCoordSys, reviseMirrorMatrix, matrixIsCoplane } from '../Common/Matrix4Utils'; import { Status } from '../Common/Status'; import { FixIndex } from '../Common/Utils'; import { ObjectSnapMode } from '../Editor/ObjectSnapMode'; @@ -655,7 +655,38 @@ export class Polyline extends Curve this.Update(); } - Join(cu: Curve, fuzz = 1e-5) + MatrixAlignTo(toMatrix: Matrix4) + { + if (!matrixIsCoplane(this.m_Matrix, toMatrix)) + return; + + let x1 = new Vector3().setFromMatrixColumn(this.m_Matrix, 0); + let x2 = new Vector3().setFromMatrixColumn(toMatrix, 0); + let y1 = new Vector3().setFromMatrixColumn(this.m_Matrix, 1); + let y2 = new Vector3().setFromMatrixColumn(toMatrix, 1); + + if (equalv3(x1, x2) && equalv3(y1, y2)) + return; + + this.WriteAllObjectRecord(); + let m = matrixAlignCoordSys(this.m_Matrix, toMatrix); + + let z1 = this.Normal; + let z2 = new Vector3().setFromMatrixColumn(toMatrix, 2); + let isMirror = equalv3(z1, z2.negate()); + + for (let d of this.m_LineData) + { + let p = Vec2DTo3D(d.pt).applyMatrix4(m); + d.pt.x = p.x; + d.pt.y = p.y; + + if (isMirror) + d.bul = -d.bul; + } + } + + Join(cu: Curve, fuzz = 1e-4) { this.WriteAllObjectRecord(); if (this.m_ClosedMark) @@ -691,7 +722,6 @@ export class Polyline extends Curve } else { - let dir = equalv3(this.Normal, cu.Normal.negate()) ? -1 : 1; if (cu instanceof Line) { @@ -716,6 +746,7 @@ export class Polyline extends Curve } else if (cu instanceof Arc) { + let dir = equalv3(this.Normal, cu.Normal.negate()) ? -1 : 1; let bul = cu.Bul * dir; if (equalv3(cuSp, sp, fuzz)) { @@ -742,50 +773,43 @@ export class Polyline extends Curve { if (cu.CloseMark) return Status.False; - let { pts, buls } = this.PtsBuls; - - let alMat = matrixAlignCoordSys(cu.OCS, this.OCS); + cu.MatrixAlignTo(this.OCS); - let cuPtsBul = cu.PtsBuls; - //坐标系对齐 - for (let i = 0; i < cuPtsBul.pts.length; i++) - { - let p = cuPtsBul.pts[i]; - cuPtsBul.buls[i] *= dir; - p.copy(Vec3DTo2D(Vec2DTo3D(p).applyMatrix4(alMat))); - } + let { pts, buls } = this.PtsBuls; - if (equalv3(cuSp, sp)) + if (equalv3(cuSp, sp, fuzz)) { cu.Reverse(); + let cuPtsBul = cu.PtsBuls; cuPtsBul.pts.pop(); cuPtsBul.buls.pop(); pts = cuPtsBul.pts.concat(pts); buls = cuPtsBul.buls.concat(buls); } - else if (equalv3(cuSp, ep)) + else if (equalv3(cuSp, ep, fuzz)) { pts.pop(); buls.pop(); + let cuPtsBul = cu.PtsBuls; pts = pts.concat(cuPtsBul.pts); buls = buls.concat(cuPtsBul.buls); } - else if (equalv3(cuEp, sp)) + else if (equalv3(cuEp, sp, fuzz)) { + let cuPtsBul = cu.PtsBuls; cuPtsBul.pts.pop(); cuPtsBul.buls.pop(); pts = cuPtsBul.pts.concat(pts); buls = cuPtsBul.buls.concat(buls); } - else if (equalv3(cuEp, ep)) + else if (equalv3(cuEp, ep, fuzz)) { pts.pop(); buls.pop(); cu.Reverse(); - - cuPtsBul = cu.PtsBuls; + let cuPtsBul = cu.PtsBuls; pts = pts.concat(cuPtsBul.pts); buls = buls.concat(cuPtsBul.buls); }