修改直线交点返回值和共线时交点

pull/68/head
Zoe 6 years ago
parent 186876cec5
commit 42a6c6f6ad

@ -123,49 +123,66 @@ Array [
`; `;
exports[`三维空间直线相交测试 1`] = ` exports[`三维空间直线相交测试 1`] = `
Vector3 { Array [
"x": 5, Vector3 {
"y": 5, "x": 5,
"z": 5, "y": 5,
} "z": 5,
},
]
`; `;
exports[`三维空间直线相交测试 2`] = ` exports[`三维空间直线相交测试 2`] = `
Vector3 { Array [
"x": 4.5, Vector3 {
"y": 4.5, "x": 3,
"z": 4.5, "y": 3,
} "z": 3,
},
Vector3 {
"x": 6,
"y": 6,
"z": 6,
},
]
`; `;
exports[`三维空间直线相交测试 3`] = ` exports[`三维空间直线相交测试 3`] = `
Vector3 { Array [
"x": 5, Vector3 {
"y": 5, "x": 5,
"z": 5, "y": 5,
} "z": 5,
},
]
`; `;
exports[`相交测试 1`] = ` exports[`相交测试 1`] = `
Vector3 { Array [
"x": 0.5, Vector3 {
"y": 0, "x": 0.5,
"z": 0, "y": 0,
} "z": 0,
},
]
`; `;
exports[`相交测试 2`] = ` exports[`相交测试 2`] = `
Vector3 { Array [
"x": 2, Vector3 {
"y": 0, "x": 2,
"z": 0, "y": 0,
} "z": 0,
},
]
`; `;
exports[`相交测试 3`] = ` exports[`相交测试 3`] = `
Vector3 { Array [
"x": 0.5, Vector3 {
"y": 5, "x": 0.5,
"z": 0, "y": 5,
} "z": 0,
},
]
`; `;

@ -56,14 +56,14 @@ test('三维空间直线相交测试', () =>
new Vector3(2, 2, 2), new Vector3(2, 2, 2),
new Vector3(2, 2, 10) new Vector3(2, 2, 10)
) )
expect(res).toBeUndefined(); expect(res.length).toBe(0);
res = IntersectLAndLFor3D( res = IntersectLAndLFor3D(
new Vector3(1, 0, 1), new Vector3(1, 0, 1),
new Vector3(1, 0, 4), new Vector3(1, 0, 4),
new Vector3(0.5, 0, 3), new Vector3(0.5, 0, 3),
new Vector3(0.5, 0, 10) new Vector3(0.5, 0, 10)
) )
expect(res).toBeUndefined(); expect(res.length).toBe(0);
}) })
test('三维空间圆圆相交测试', () => test('三维空间圆圆相交测试', () =>
{ {

@ -21,7 +21,7 @@ export class TestIntersect implements Command
let exRefSsRes = await app.m_Editor.GetSelection({ Msg: "请选择对象<全部选择>:", UseSelect: true }); let exRefSsRes = await app.m_Editor.GetSelection({ Msg: "请选择对象<全部选择>:", UseSelect: true });
if (exRefSsRes.Status !== PromptStatus.OK) return; if (exRefSsRes.Status !== PromptStatus.OK) return;
let cus = exRefSsRes.SelectSet.SelectEntityList as Curve[]; let cus = exRefSsRes.SelectSet.SelectEntityList as Curve[];
let pt = IntersectLAndLFor3D(cus[0].StartPoint, cus[0].EndPoint, cus[1].StartPoint, cus[1].EndPoint); let pt = IntersectLAndLFor3D(cus[0].StartPoint, cus[0].EndPoint, cus[1].StartPoint, cus[1].EndPoint)[0];
console.log('pt: ', pt); console.log('pt: ', pt);
if (pt) if (pt)
{ {

@ -116,7 +116,7 @@ export class LineAngularDimension extends Dimension
private getCaclSPtAndEPt() private getCaclSPtAndEPt()
{ {
// 2线交点即为圆心 // 2线交点即为圆心
let center = IntersectLAndLFor3D(this.m_StartPoint1, this.m_EndPoint1, this.m_StartPoint2, this.m_EndPoint2); let center = IntersectLAndLFor3D(this.m_StartPoint1, this.m_EndPoint1, this.m_StartPoint2, this.m_EndPoint2)[0];
// 获取实际的首尾点,离圆心近的的为起始点 // 获取实际的首尾点,离圆心近的的为起始点
let [spt1, ept1] = this.StartPoint1.distanceTo(center) < this.EndPoint1.distanceTo(center) ? [this.StartPoint1, this.EndPoint1] : [this.EndPoint1, this.StartPoint1]; let [spt1, ept1] = this.StartPoint1.distanceTo(center) < this.EndPoint1.distanceTo(center) ? [this.StartPoint1, this.EndPoint1] : [this.EndPoint1, this.StartPoint1];
let [spt2, ept2] = this.StartPoint2.distanceTo(center) < this.EndPoint2.distanceTo(center) ? [this.StartPoint2, this.EndPoint2] : [this.EndPoint2, this.StartPoint2]; let [spt2, ept2] = this.StartPoint2.distanceTo(center) < this.EndPoint2.distanceTo(center) ? [this.StartPoint2, this.EndPoint2] : [this.EndPoint2, this.StartPoint2];
@ -126,7 +126,7 @@ export class LineAngularDimension extends Dimension
private getDimArcData() private getDimArcData()
{ {
// 2线交点即为圆心 // 2线交点即为圆心
let center = IntersectLAndLFor3D(this.m_StartPoint1, this.m_EndPoint1, this.m_StartPoint2, this.m_EndPoint2); let center = IntersectLAndLFor3D(this.m_StartPoint1, this.m_EndPoint1, this.m_StartPoint2, this.m_EndPoint2)[0];
let rad = this.m_ArcPoint.distanceTo(center); let rad = this.m_ArcPoint.distanceTo(center);
let { ept1, ept2 } = this.getCaclSPtAndEPt(); let { ept1, ept2 } = this.getCaclSPtAndEPt();
//标注线段的起始端点 //标注线段的起始端点

@ -104,7 +104,7 @@ export class SnapServices
for (let j = i + 1; j < snapAxisList.length; j++) for (let j = i + 1; j < snapAxisList.length; j++)
{ {
let axis2 = snapAxisList[j]; let axis2 = snapAxisList[j];
let insP = IntersectLAndLFor3D(axis1.BasePoint, axis1.SnapPoint, axis2.BasePoint, axis2.SnapPoint); let insP = IntersectLAndLFor3D(axis1.BasePoint, axis1.SnapPoint, axis2.BasePoint, axis2.SnapPoint)[0];
if (insP) if (insP)
axisIntersectList.push({ axisIntersectList.push({
IntersectPoint: insP, IntersectPoint: insP,

@ -224,7 +224,7 @@ export function IntersectLAndLFor2D(p1: Vector3, p2: Vector3, p3: Vector3, p4: V
return pt; return pt;
} }
export function IntersectLAndLFor3D(p1: Vector3, p2: Vector3, p3: Vector3, p4: Vector3) export function IntersectLAndLFor3D1(p1: Vector3, p2: Vector3, p3: Vector3, p4: Vector3)
{ {
let x12 = p1.x - p2.x; let x12 = p1.x - p2.x;
let x43 = p4.x - p3.x; let x43 = p4.x - p3.x;
@ -272,7 +272,7 @@ export function IntersectLAndLFor3D(p1: Vector3, p2: Vector3, p3: Vector3, p4: V
} }
return pt; return pt;
} }
export function IntersectLAndLFor3D1(p1: Vector3, p2: Vector3, p3: Vector3, p4: Vector3) export function IntersectLAndLFor3D(p1: Vector3, p2: Vector3, p3: Vector3, p4: Vector3)
{ {
let x12 = p1.x - p2.x; let x12 = p1.x - p2.x;
let x43 = p4.x - p3.x; let x43 = p4.x - p3.x;
@ -284,7 +284,6 @@ export function IntersectLAndLFor3D1(p1: Vector3, p2: Vector3, p3: Vector3, p4:
let z43 = p4.z - p3.z; let z43 = p4.z - p3.z;
let z42 = p4.z - p2.z; let z42 = p4.z - p2.z;
let pt: Vector3;
let pts: Vector3[] = []; let pts: Vector3[] = [];
let v1 = p2.clone().sub(p1).normalize(); let v1 = p2.clone().sub(p1).normalize();
let v2 = p4.clone().sub(p3).normalize(); let v2 = p4.clone().sub(p3).normalize();
@ -292,15 +291,15 @@ export function IntersectLAndLFor3D1(p1: Vector3, p2: Vector3, p3: Vector3, p4:
if (equaln(Math.abs(v1.dot(v2)), 1, 1e-6) && equaln(Math.abs(v1.dot(w)), 1, 1e-6)) //平行共线 if (equaln(Math.abs(v1.dot(v2)), 1, 1e-6) && equaln(Math.abs(v1.dot(w)), 1, 1e-6)) //平行共线
{ {
let pts = [p1, p2, p3, p4]; let tmpPts = [p1, p2, p3, p4];
pts.sort(comparePoint('xyz')); tmpPts.sort(comparePoint('xyz'));
if (equal(pts[1], p3) || equal(pts[1], p4)) if (equal(tmpPts[1], p3) || equal(tmpPts[1], p4))
{ {
pts = [pts[1], pts[2]]; pts = [tmpPts[1], tmpPts[2]];
} }
else else
{ {
pts = [midPoint(pts[1], pts[2])] pts = [midPoint(tmpPts[1], tmpPts[2])]
} }
} }
else if (equaln(Math.abs(v1.dot(v2)), 1, 1e-6)) //平行不共线 else if (equaln(Math.abs(v1.dot(v2)), 1, 1e-6)) //平行不共线
@ -331,7 +330,7 @@ export function IntersectLineAndLine(l1: Line, l2: Line, extType: IntersectOptio
{ {
let pt = IntersectLAndLFor3D(l1.StartPoint, l1.EndPoint, l2.StartPoint, l2.EndPoint); let pt = IntersectLAndLFor3D(l1.StartPoint, l1.EndPoint, l2.StartPoint, l2.EndPoint);
return pt ? CheckPointOnCurve([pt], l1, l2, extType) : []; return CheckPointOnCurve(pt, l1, l2, extType);
} }
export function IntersectPolylineAndCurve(pl: Polyline, cu: Curve, extType: IntersectOption): Vector3[] export function IntersectPolylineAndCurve(pl: Polyline, cu: Curve, extType: IntersectOption): Vector3[]

@ -2,23 +2,28 @@ import { Vector3 } from "three";
import { Curve } from "../DatabaseServices/Curve"; import { Curve } from "../DatabaseServices/Curve";
import { CurveIntersection } from "../Geometry/CurveIntersection"; import { CurveIntersection } from "../Geometry/CurveIntersection";
import { CurveMap } from "../Geometry/CurveMap"; import { CurveMap } from "../Geometry/CurveMap";
import { equal } from "../Geometry/GeUtils"; import { equal, equaln } from "../Geometry/GeUtils";
import { Stand } from "../Geometry/RegionParse"; import { Stand } from "../Geometry/RegionParse";
import { app } from "../ApplicationServices/Application"; import { app } from "../ApplicationServices/Application";
import { Circle } from "../DatabaseServices/Circle";
export class LinkSelf export class LinkSelf
{ {
private curveUseData: WeakMap<Curve, boolean> = new WeakMap(); private curveUseData: WeakMap<Curve, boolean> = new WeakMap();
private curveIndexData: WeakMap<Curve, number> = new WeakMap(); private curveIndexData: WeakMap<Curve, number> = new WeakMap();
sealCus: Set<Curve>[] = []; sealCus: Set<Curve>[] = [];
noSealCus: Curve[][] = []; noSealCus: Curve[][] = [];
private m_Count;
private cuMap: CurveMap; private cuMap: CurveMap;
constructor(cus: Curve[]) constructor(cus: Curve[])
{ {
this.m_Count = cus.length;
//打断曲线 //打断曲线
let breakCus: Curve[] = this.BreakCurve(cus); let breakCus: Curve[] = this.BreakCurve(cus);
// breakCus.forEach(c =>
// {
// c.ColorIndex = 3;
// app.m_Database.ModelSpace.Append(c);
// })
//曲线图 用来快速搜索求交 //曲线图 用来快速搜索求交
this.cuMap = this.GenerateCurveMap(breakCus); this.cuMap = this.GenerateCurveMap(breakCus);
@ -36,8 +41,8 @@ export class LinkSelf
let insMap = new CurveIntersection(cus.concat()); let insMap = new CurveIntersection(cus.concat());
//打断后存储的曲线列表 //打断后存储的曲线列表
let breakCus: Curve[] = []; let breakCus: Curve[] = [];
let count = cus.length; // let count = cus.length;
for (let i = 0; i < count; i++) for (let i = 0; i < this.m_Count; i++)
{ {
let cu = cus[i]; let cu = cus[i];
//交点数据 //交点数据
@ -59,8 +64,11 @@ export class LinkSelf
{ {
for (let c of spCus) for (let c of spCus)
{ {
this.SetCurveIndex(c, i); if (!equaln(c.Length, 0, 1e-6))
breakCus.push(c); {
this.SetCurveIndex(c, i);
breakCus.push(c);
}
} }
} }
} }
@ -72,8 +80,6 @@ export class LinkSelf
{ {
let breakCount = breakCus.length; let breakCount = breakCus.length;
let selfRoutes: Set<Curve>[] = [];
//搜索闭合自交 //搜索闭合自交
for (let i = 0; i < breakCount; i++) for (let i = 0; i < breakCount; i++)
{ {
@ -99,18 +105,18 @@ export class LinkSelf
if (routeIndex < cuIndex) if (routeIndex < cuIndex)
continue;//不和小于自己索引的计算 continue;//不和小于自己索引的计算
routeCus.add(cu);
routeCus.add(route.curve);
//如果存在闭合区域 //如果存在闭合区域
if (this.FindCloseCurve(route.to, cuIndex, ways, routeCus, cuMap)) if (this.FindCloseCurve(route.to, cuIndex, ways, routeCus, cuMap))
{ {
routeCus.add(route.curve);
routeCus.add(cu);
//提取闭合区域 //提取闭合区域
for (let c of routeCus) for (let c of routeCus)
{ {
this.SetCurveUse(c); this.SetCurveUse(c);
} }
this.sealCus.push(routeCus); this.sealCus.push(routeCus);
break;
} }
} }
} }
@ -145,6 +151,17 @@ export class LinkSelf
{ {
let oldCount = cs.length; let oldCount = cs.length;
let routes = cuMap.GetStand(isInv ? originCu.StartPoint : originCu.EndPoint).routes; let routes = cuMap.GetStand(isInv ? originCu.StartPoint : originCu.EndPoint).routes;
// 最后一段从大到小搜
for (let i = 0; i < routes.length; i++)
{
let index = this.GetCurveIndex(routes[i].curve);
if (index === cuIndex)
{
routes.unshift(routes.splice(i, 1)[0]);
}
}
for (let j = routes.length; j--;) //按照索引从小到大搜索 for (let j = routes.length; j--;) //按照索引从小到大搜索
{ {
let cu2 = routes[j].curve; let cu2 = routes[j].curve;
@ -152,7 +169,7 @@ export class LinkSelf
if (this.GetCurveUse(cu2)) continue; if (this.GetCurveUse(cu2)) continue;
if (cuIndex === cu2Index) if (cuIndex === cu2Index)
{ {
continue;//如果和自身的线连接,则需要判断这条路线是否合理 // continue;//如果和自身的线连接,则需要判断这条路线是否合理
} }
//能够连接 //能够连接
let isLink = isInv ? equal(cu2.EndPoint, originCu.StartPoint) : equal(cu2.StartPoint, originCu.EndPoint); let isLink = isInv ? equal(cu2.EndPoint, originCu.StartPoint) : equal(cu2.StartPoint, originCu.EndPoint);
@ -225,7 +242,6 @@ export class LinkSelf
this.curveIndexData.set(curve, index); this.curveIndexData.set(curve, index);
} }
//搜索闭合的区域 //搜索闭合的区域
private FindCloseCurve(nowStand: Stand, cuIndex: number, ways: Stand[], routes: Set<Curve>, cuMap: CurveMap) private FindCloseCurve(nowStand: Stand, cuIndex: number, ways: Stand[], routes: Set<Curve>, cuMap: CurveMap)
{ {
@ -242,7 +258,6 @@ export class LinkSelf
if (routeIndex < cuIndex) if (routeIndex < cuIndex)
continue;//不和小于自己索引的计算 continue;//不和小于自己索引的计算
//验证通过,可以连接. //验证通过,可以连接.
let toStand = route.to; let toStand = route.to;

@ -73,10 +73,11 @@ export class PolyOffestUtil
if (!this.IsKeepAllCurves) if (!this.IsKeepAllCurves)
{ {
// console.time('k'); // console.time('k');
// rets = this.optimizeCus(rets); rets = this.optimizeCus(rets);
// console.timeEnd('k'); // console.timeEnd('k');
} }
return rets; return rets;
// return [];
} }
/** /**
* 线 * 线
@ -416,50 +417,54 @@ export class PolyOffestUtil
let outline = c.Outline; let outline = c.Outline;
for (let l of needCutCus) for (let l of needCutCus)
{ {
let posSrcForTar = TargetCurPosForSourceCur(outline, l as Arc | Line); // let posSrcForTar = TargetCurPosForSourceCur(outline, l as Arc | Line);
if (posSrcForTar.onSrc || posSrcForTar.outSrc) // if (posSrcForTar.onSrc || posSrcForTar.outSrc)
// {
// tmpCus.push(l);
// }
// else if (posSrcForTar.throughSrc)
// {
// let par = posSrcForTar.pts.map(p => l.GetParamAtPoint(p));
// let cus = l.GetSplitCurves(par);
// if (cus.length === 0)
// {
// tmpCus.push(l);
// }
// else
// {
// tmpCus.push(...cus.filter(cu => !equaln(cu.Length, 0, 1e-6) && isTargetCurOutOrOnSourceCur(outline, cu)));
// }
// }
// else
// {
// l instanceof Arc && tmpCus.push(l);
// }
// 在上面或者在外面
if (isTargetCurOutOrOnSourceCur(outline, l))
{ {
tmpCus.push(l); tmpCus.push(l);
} }
else if (posSrcForTar.throughSrc) else if (!isTargetCurInOrOnSourceCur(outline, l))
{ {
let par = posSrcForTar.pts.map(p => l.GetParamAtPoint(p)); let pts = l.IntersectWith(outline, IntersectOption.OnBothOperands);
let cus = l.GetSplitCurves(par); if (pts.length > 0)
if (cus.length === 0)
{
tmpCus.push(l);
}
else
{ {
tmpCus.push(...cus.filter(cu => !equaln(cu.Length, 0, 1e-6) && isTargetCurOutOrOnSourceCur(outline, cu))); let par = pts.map(p => l.GetParamAtPoint(p));
let cus = l.GetSplitCurves(par);
if (cus.length > 0)
tmpCus.push(...cus.filter(cu => !equaln(cu.Length, 0, 1e-6) && !isTargetCurInOrOnSourceCur(outline, cu)));
else
{
tmpCus.push(l);
}
} }
} }
else else
{ {
l instanceof Arc && tmpCus.push(l); // l instanceof Arc && tmpCus.push(l);
} }
// 在上面或者在外面
// if (isTargetCurOutOrOnSourceCur(outline, l))
// {
// tmpCus.push(l);
// }
// else
// {
// let pts = l.IntersectWith(outline, IntersectOption.OnBothOperands);
// if (pts.length > 0)
// {
// let par = pts.map(p => l.GetParamAtPoint(p));
// let cus = l.GetSplitCurves(par);
// if (cus.length > 0)
// tmpCus.push(...cus.filter(cu => !equaln(cu.Length, 0, 1e-6) && !isTargetCurInOrOnSourceCur(outline, cu)));
// else
// {
// tmpCus.push(l);
// }
// }
// }
} }
needCutCus = tmpCus; needCutCus = tmpCus;
}) })
@ -609,7 +614,7 @@ export class PolyOffestUtil
{ {
let retPls: Polyline[] = []; let retPls: Polyline[] = [];
let retsCus = new LinkSelf(cus); let retsCus = new LinkSelf(cus);
let noSealCus: Polyline[] = retsCus.noSealCus.map(cs => let noSealCus = retsCus.noSealCus.map(cs =>
{ {
return this.linkCurves2(cs); return this.linkCurves2(cs);
}) })
@ -617,14 +622,14 @@ export class PolyOffestUtil
{ {
return this.linkCurves2(s); return this.linkCurves2(s);
}) })
let firstLine = noSealCus[0]; // let firstLine = noSealCus[0];
noSealCus.sort((a, b) => b.Length - a.Length); // noSealCus.sort((a, b) => b.Length - a.Length);
if (firstLine !== noSealCus[0]) // if (firstLine !== noSealCus[0])
{ // {
retPls.push(noSealCus[0]); // retPls.push(noSealCus[0]);
} // }
firstLine && retPls.push(firstLine); // firstLine && retPls.push(firstLine);
// retPls.push(...noSealCus); retPls.push(...noSealCus);
retPls.push(...sealCus); retPls.push(...sealCus);
return retPls; return retPls;
} }

Loading…
Cancel
Save