diff --git a/__test__/Line/__snapshots__/line.test.ts.snap b/__test__/Line/__snapshots__/line.test.ts.snap new file mode 100644 index 000000000..b1c9b38aa --- /dev/null +++ b/__test__/Line/__snapshots__/line.test.ts.snap @@ -0,0 +1,17 @@ +// Jest Snapshot v1, https://goo.gl/fbAQLP + +exports[`直线偏移 1`] = ` +Vector3 { + "x": -6.123233995736766e-16, + "y": 10, + "z": 0, +} +`; + +exports[`直线偏移 2`] = ` +Vector3 { + "x": 4.999999999999999, + "y": 10, + "z": 0, +} +`; diff --git a/__test__/Line/line.test.ts b/__test__/Line/line.test.ts index d6f87c287..f7ecd3949 100644 --- a/__test__/Line/line.test.ts +++ b/__test__/Line/line.test.ts @@ -57,3 +57,38 @@ test("直线延伸-正向", () => l.Extend(2); expect(l.EndPoint).toMatchObject({ 'x': 10, 'y': 0, 'z': 0 }); }); +test("由距离得到直线参数", () => +{ + let l = new Line(new Vector3(0, 0, 0), new Vector3(5, 0, 0)); + expect(l.GetParamAtDist(10)).toBe(2); +} +); +test("由距离得到对应点", () => +{ + let l = new Line(new Vector3(0, 0, 0), new Vector3(5, 0, 0)); + expect(l.GetPointAtDistance(-10)).toMatchObject({ "x": -10, 'y': 0, 'z': 0 }); +} +); +test("由参数得到距离", () => +{ + let l = new Line(new Vector3(0, 0, 0), new Vector3(5, 0, 0)); + expect(l.GetDistAtParam(-2)).toBe(-10); +} +); +test("由点得到距离", () => +{ + let l = new Line(new Vector3(0, 0, 0), new Vector3(5, 0, 0)); + expect(l.GetDistAtPoint(new Vector3(-10, 0, 0))).toBe(-10); + expect(l.GetDistAtPoint(new Vector3(10, 0, 0))).toBe(10); +} +); +test('直线偏移', () => +{ + let l = new Line(new Vector3(0, 0, 0), new Vector3(5, 0, 0)); + let lines = l.GetOffsetCurves(-10); + expect(lines.length).toBe(1); + + let newLine = lines[0]; + expect(newLine.StartPoint).toMatchSnapshot(); + expect(newLine.EndPoint).toMatchSnapshot(); +}); diff --git a/src/DatabaseServices/Line.ts b/src/DatabaseServices/Line.ts index 45b2a5807..89b484d5c 100644 --- a/src/DatabaseServices/Line.ts +++ b/src/DatabaseServices/Line.ts @@ -3,7 +3,7 @@ import { Geometry, Matrix4, Object3D, Vector3 } from 'three'; import * as THREE from 'three'; import { ColorMaterial } from '../Common/ColorPalette'; -import { equal } from '../Geometry/GeUtils'; +import { equal, polar } from '../Geometry/GeUtils'; import { RenderType } from '../GraphicsSystem/Enum'; import { Intersect, IntersectLineAndLine } from '../GraphicsSystem/IntersectWith'; import { Factory } from './CADFactory'; @@ -101,9 +101,6 @@ export class Line extends Curve { return this.StartPoint.add(this.GetFistDeriv(0).multiplyScalar(param)); } - GetPointAtDistance(distance: number): Vector3 { return; } - GetDistAtParam(param: number): number { return; } - GetDistAtPoint(pt: Vector3): number { return; } GetParamAtPoint(pt: Vector3): number { let len = this.Length; @@ -128,7 +125,22 @@ export class Line extends Curve } return NaN; } - GetParamAtDist(d: number): number { return; } + GetParamAtDist(d: number): number + { + return d / this.Length; + } + GetPointAtDistance(distance: number): Vector3 + { + return this.GetPointAtParam(this.GetParamAtDist(distance)); + } + GetDistAtParam(param: number): number + { + return this.Length * param; + } + GetDistAtPoint(pt: Vector3): number + { + return this.GetDistAtParam(this.GetParamAtPoint(pt)); + } PtOnCurve(pt: Vector3): boolean { let param = this.GetParamAtPoint(pt); @@ -183,6 +195,16 @@ export class Line extends Curve this.EndPoint = this.GetPointAtParam(newParam); } } + + GetOffsetCurves(offsetDist: number): Array + { + let an = this.GetFistDeriv(0).angleTo(new Vector3(1, 0, 0)) - Math.PI * 0.5; + let newLine = this.Clone() as Line; + newLine.StartPoint = polar(this.StartPoint, an, offsetDist); + newLine.EndPoint = polar(this.EndPoint, an, offsetDist); + return [newLine]; + } + get StartParam() { return 0; diff --git a/src/Editor/CommandRegister.ts b/src/Editor/CommandRegister.ts index 0bfc6a6fb..97187fbf5 100644 --- a/src/Editor/CommandRegister.ts +++ b/src/Editor/CommandRegister.ts @@ -1,38 +1,38 @@ // import { Command_DrawBoard } from '../Add-on/DrawBoard'; // import { DrawFloor } from '../Add-on/DrawFloor'; import { DrawAxis } from '../Add-on/AddAxis'; +import { Command_Copy } from '../Add-on/Copy'; +import { CustomUcs } from '../Add-on/CostumUCS'; import { Union } from '../Add-on/CSGUnion'; +import { DrawArc } from '../Add-on/DrawArc'; import { DrawEllipse } from '../Add-on/DrawEllipse'; import { DrawFloor } from '../Add-on/DrawFloor'; import { DrawGripStretch } from '../Add-on/DrawGripStretch'; import { DrawCircle, DrawLine, DrawRect, DrawSphere, DrawTest, ZoomE } from '../Add-on/DrawLine'; -import { DrawArc } from '../Add-on/DrawArc'; +import { DrawPolyline } from '../Add-on/DrawPolyline'; +import { DrawRegion } from '../Add-on/DrawRegion'; import { DrawSky } from '../Add-on/DrawSky'; import { DrawSpline } from '../Add-on/DrawSpline'; +import { DrawRegTest } from '../Add-on/DrawTestReg'; import { DrawCircle0 } from '../Add-on/DrawZeroCircle'; import { Entsel } from '../Add-on/Entsel'; import { Command_Erase } from '../Add-on/Erase'; +import { Command_Extend } from '../Add-on/Extends'; import { Fbx } from '../Add-on/loadfbx'; import { LoadImg } from '../Add-on/LoadImg'; import { Command_Move } from '../Add-on/Move'; import { Open } from '../Add-on/Open'; +import { Command_RevPl } from '../Add-on/RevPl'; import { Command_Rotate } from '../Add-on/Rotate'; import { Save } from '../Add-on/Save'; import { Stretch } from '../Add-on/Stretch'; import { Command_SwitchCamera } from '../Add-on/SwitchCamera'; import { Command_SwitchPass } from '../Add-on/SwitchPass'; import { Test } from '../Add-on/test'; +import { Command_Trim } from '../Add-on/Trim'; import { Redo, Undo } from '../Add-on/Undo'; import { ViewToFront, ViewToTop } from '../Add-on/ViewChange'; import { commandMachine } from './CommandMachine'; -import { DrawRegion } from '../Add-on/DrawRegion'; -import { DrawRegTest } from '../Add-on/DrawTestReg'; -import { DrawPolyline } from '../Add-on/DrawPolyline'; -import { CustomUcs } from '../Add-on/CostumUCS'; -import { Command_Copy } from '../Add-on/Copy'; -import { Command_RevPl } from '../Add-on/RevPl'; -import { Command_Extend } from '../Add-on/Extends'; -import { Command_Trim } from '../Add-on/Trim'; // import { RevTarget, SaveTarget } from '../Add-on/RenderTarget'; export function registerCommand()