mirror of https://gitee.com/cf-fz/WebCAD.git
parent
5aa0362c5d
commit
bd6c4ecf2c
@ -1,122 +0,0 @@
|
||||
import { Polyline } from '../DatabaseServices/Polyline';
|
||||
import { Curve } from '../DatabaseServices/Curve';
|
||||
import { Vector3 } from 'three';
|
||||
import { IntersectOption } from './IntersectWith';
|
||||
import { Arc } from '../DatabaseServices/Arc';
|
||||
import { angle } from '../Geometry/GeUtils';
|
||||
import { Circle } from '../DatabaseServices/Circle';
|
||||
|
||||
|
||||
export function OffsetPolyline(pl: Polyline, dis: number): Curve[]
|
||||
{
|
||||
let oldCus = pl.Explode();
|
||||
let cuMap = new Map<Curve, Curve>();
|
||||
|
||||
//第一次偏移
|
||||
let offCus = oldCus.map(c =>
|
||||
{
|
||||
let newc = c.GetOffsetCurves(dis)[0];
|
||||
if (newc)
|
||||
cuMap.set(newc, c);
|
||||
else
|
||||
{
|
||||
//加圆 注意重复
|
||||
}
|
||||
return newc;
|
||||
}
|
||||
).filter(c => c);
|
||||
|
||||
let linkCus = linkCurve(offCus, dis, pl.IsClose, cuMap);
|
||||
|
||||
//3.斜率相反的去掉.
|
||||
|
||||
return linkCus;
|
||||
}
|
||||
|
||||
//将曲线连接起来
|
||||
function linkCurve(cus: Curve[], dis, isClose: boolean, cuMap: Map<Curve, Curve>)
|
||||
{
|
||||
//sp表示该线段和上一个线段的交点,ep表示和下一个线段的交点.
|
||||
let ep: Vector3;
|
||||
|
||||
if (isClose)
|
||||
cus.push(cus[0]);
|
||||
|
||||
let newCus: Curve[] = [];
|
||||
|
||||
let cuCount = cus.length;
|
||||
for (let i = 0; i < cuCount - 1; i++)
|
||||
{
|
||||
let c1 = cus[i];
|
||||
let c2 = cus[i + 1];
|
||||
|
||||
let pts = c1.IntersectWith(c2, IntersectOption.ExtendBoth);
|
||||
|
||||
newCus.push(c1);
|
||||
|
||||
switch (pts.length)
|
||||
{
|
||||
case 0://添加圆弧
|
||||
{
|
||||
for (let center of [
|
||||
cuMap.get(c1).EndPoint,
|
||||
cuMap.get(c2).StartPoint,
|
||||
])
|
||||
{
|
||||
let cir = new Circle(center, dis);
|
||||
|
||||
let pts1 = c1.IntersectWith(cir, IntersectOption.ExtendBoth);
|
||||
let pts2 = c2.IntersectWith(cir, IntersectOption.ExtendBoth);
|
||||
if (pts1.length > 0 && pts2.length > 0)
|
||||
{
|
||||
c1.EndPoint = selectPoint(pts1, cuMap.get(c1).EndPoint);
|
||||
c2.StartPoint = selectPoint(pts2, cuMap.get(c2).StartPoint);
|
||||
|
||||
let sa = angle(c1.EndPoint.sub(center));
|
||||
let ea = angle(c2.StartPoint.sub(center));
|
||||
|
||||
let c1Deriv = c1.GetFistDeriv(1);
|
||||
let chord = c2.StartPoint.sub(c1.EndPoint);
|
||||
|
||||
let dir = c1Deriv.cross(chord);
|
||||
let arc = new Arc(center, dis, sa, ea);
|
||||
arc.IsClockWise = dir.z < 0;
|
||||
|
||||
newCus.push(arc);
|
||||
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
continue;//
|
||||
}
|
||||
case 1:
|
||||
{
|
||||
ep = pts[0];
|
||||
break;
|
||||
}
|
||||
case 2:
|
||||
{
|
||||
//计算ep
|
||||
let oldp = cuMap.get(c1).EndPoint;
|
||||
ep = pts[0].distanceToSquared(oldp) < pts[1].distanceToSquared(oldp) ?
|
||||
pts[0] : pts[1];
|
||||
break;
|
||||
}
|
||||
default:
|
||||
break;
|
||||
}
|
||||
|
||||
c1.EndPoint = ep;
|
||||
c2.StartPoint = ep;
|
||||
}
|
||||
|
||||
return newCus;
|
||||
}
|
||||
|
||||
function selectPoint(pts: Vector3[], oldp: Vector3)
|
||||
{
|
||||
if (pts.length === 1) return pts[0];
|
||||
return pts[0].distanceToSquared(oldp) < pts[1].distanceToSquared(oldp) ?
|
||||
pts[0] : pts[1];
|
||||
}
|
Loading…
Reference in new issue