mirror of https://gitee.com/cf-fz/WebCAD.git
parent
ec159ee25e
commit
45c8ec11e8
@ -0,0 +1,72 @@
|
||||
import { Box3, Vector3 } from "three";
|
||||
import { Curve } from "../DatabaseServices/Curve";
|
||||
import { IntersectOption } from "../GraphicsSystem/IntersectWith";
|
||||
|
||||
/**
|
||||
*
|
||||
* 简化优化版本的曲线求交, 优化版本可以参考(算法导论33.2 确定任意一对线段是否相交 p599)
|
||||
*
|
||||
* @class CurveIntersection
|
||||
*/
|
||||
class CurveIntersection
|
||||
{
|
||||
//用来缓存的曲线包围盒
|
||||
private boxMap: Map<Curve, Box3> = new Map();
|
||||
|
||||
/**
|
||||
* 交点数据集,key 为曲线 value 为和它相交的(曲线和交点的Map)
|
||||
*
|
||||
* @type {Map<Curve, Map<Curve, Vector3[]>>}
|
||||
* @memberof CurveIntersection
|
||||
*/
|
||||
intersect: Map<Curve, Map<Curve, Vector3[]>> = new Map();
|
||||
constructor(cus: Curve[])
|
||||
{
|
||||
cus.forEach(c =>
|
||||
{
|
||||
this.boxMap.set(c, c.BoundingBox);
|
||||
});
|
||||
|
||||
//按x排序
|
||||
cus.sort((c1, c2) =>
|
||||
{
|
||||
return this.boxMap.get(c1).min.x - this.boxMap.get(c2).min.x;
|
||||
});
|
||||
|
||||
let count = cus.length;
|
||||
for (let i = 0; i < count; i++)
|
||||
{
|
||||
let c1 = cus[i];
|
||||
let c1d = this.GetIntersect(c1);
|
||||
|
||||
let c1b = this.boxMap.get(c1);
|
||||
for (let j = i + 1; j < count; j++)
|
||||
{
|
||||
let c2 = cus[j];
|
||||
|
||||
//过滤掉不需要计算的曲线
|
||||
let c2b = this.boxMap.get(c2);
|
||||
if (c2b.min.x > c1b.max.x)
|
||||
break;
|
||||
if (c2b.min.y > c1b.max.y)
|
||||
continue;
|
||||
|
||||
let pts = c1.IntersectWith(c2, IntersectOption.OnBothOperands);
|
||||
if (pts.length > 0)
|
||||
{
|
||||
c1d.set(c2, pts);
|
||||
this.GetIntersect(c2).set(c1, pts);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
GetIntersect(cu: Curve): Map<Curve, Vector3[]>
|
||||
{
|
||||
if (this.intersect.has(cu))
|
||||
return this.intersect.get(cu);
|
||||
|
||||
let m = new Map();
|
||||
this.intersect.set(cu, m);
|
||||
return m;
|
||||
}
|
||||
}
|
Loading…
Reference in new issue