修正Polyline.Join,补全测试

pull/240/MERGE
ChenX 6 years ago
parent 7112f85c86
commit 128348828d

@ -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();
});

@ -4,7 +4,7 @@ import { CreateBoardUtil } from '../ApplicationServices/mesh/createBoard';
import { arrayLast, arrayRemoveDuplicateBySort, changeArrayStartIndex } from '../Common/ArrayExt'; import { arrayLast, arrayRemoveDuplicateBySort, changeArrayStartIndex } from '../Common/ArrayExt';
import { ColorMaterial } from '../Common/ColorPalette'; import { ColorMaterial } from '../Common/ColorPalette';
import { getDeterminantFor2V, Vec2DTo3D, Vec3DTo2D } from '../Common/CurveUtils'; 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 { Status } from '../Common/Status';
import { FixIndex } from '../Common/Utils'; import { FixIndex } from '../Common/Utils';
import { ObjectSnapMode } from '../Editor/ObjectSnapMode'; import { ObjectSnapMode } from '../Editor/ObjectSnapMode';
@ -655,7 +655,38 @@ export class Polyline extends Curve
this.Update(); 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(); this.WriteAllObjectRecord();
if (this.m_ClosedMark) if (this.m_ClosedMark)
@ -691,7 +722,6 @@ export class Polyline extends Curve
} }
else else
{ {
let dir = equalv3(this.Normal, cu.Normal.negate()) ? -1 : 1;
if (cu instanceof Line) if (cu instanceof Line)
{ {
@ -716,6 +746,7 @@ export class Polyline extends Curve
} }
else if (cu instanceof Arc) else if (cu instanceof Arc)
{ {
let dir = equalv3(this.Normal, cu.Normal.negate()) ? -1 : 1;
let bul = cu.Bul * dir; let bul = cu.Bul * dir;
if (equalv3(cuSp, sp, fuzz)) if (equalv3(cuSp, sp, fuzz))
{ {
@ -742,50 +773,43 @@ export class Polyline extends Curve
{ {
if (cu.CloseMark) return Status.False; if (cu.CloseMark) return Status.False;
let { pts, buls } = this.PtsBuls; cu.MatrixAlignTo(this.OCS);
let alMat = matrixAlignCoordSys(cu.OCS, this.OCS);
let cuPtsBul = cu.PtsBuls; let { pts, buls } = this.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)));
}
if (equalv3(cuSp, sp)) if (equalv3(cuSp, sp, fuzz))
{ {
cu.Reverse(); cu.Reverse();
let cuPtsBul = cu.PtsBuls;
cuPtsBul.pts.pop(); cuPtsBul.pts.pop();
cuPtsBul.buls.pop(); cuPtsBul.buls.pop();
pts = cuPtsBul.pts.concat(pts); pts = cuPtsBul.pts.concat(pts);
buls = cuPtsBul.buls.concat(buls); buls = cuPtsBul.buls.concat(buls);
} }
else if (equalv3(cuSp, ep)) else if (equalv3(cuSp, ep, fuzz))
{ {
pts.pop(); pts.pop();
buls.pop(); buls.pop();
let cuPtsBul = cu.PtsBuls;
pts = pts.concat(cuPtsBul.pts); pts = pts.concat(cuPtsBul.pts);
buls = buls.concat(cuPtsBul.buls); buls = buls.concat(cuPtsBul.buls);
} }
else if (equalv3(cuEp, sp)) else if (equalv3(cuEp, sp, fuzz))
{ {
let cuPtsBul = cu.PtsBuls;
cuPtsBul.pts.pop(); cuPtsBul.pts.pop();
cuPtsBul.buls.pop(); cuPtsBul.buls.pop();
pts = cuPtsBul.pts.concat(pts); pts = cuPtsBul.pts.concat(pts);
buls = cuPtsBul.buls.concat(buls); buls = cuPtsBul.buls.concat(buls);
} }
else if (equalv3(cuEp, ep)) else if (equalv3(cuEp, ep, fuzz))
{ {
pts.pop(); pts.pop();
buls.pop(); buls.pop();
cu.Reverse(); cu.Reverse();
let cuPtsBul = cu.PtsBuls;
cuPtsBul = cu.PtsBuls;
pts = pts.concat(cuPtsBul.pts); pts = pts.concat(cuPtsBul.pts);
buls = buls.concat(cuPtsBul.buls); buls = buls.concat(cuPtsBul.buls);
} }

Loading…
Cancel
Save