 - /// <summary>
- /// 获取两个曲线的交点集合(支持大坐标偏移优化)
- /// </summary>
- /// <param name="curve1">第一个曲线(线段或圆弧可延伸,多段线不可延伸)</param>
- /// <param name="curve2">第二个曲线(线段或圆弧可延伸,多段线不可延伸)</param>
- /// <param name="intersectOption">相交处理方式:
- /// <para>OnBothOperands - 两者都不延伸</para>
- /// <para>ExtendBoth - 两者都延伸</para>
- /// <para>ExtendThis - 仅延伸当前曲线(curve1)</para>
- /// <para>ExtendArgument - 仅延伸参数曲线(curve2)</para></param>
- /// <returns>输出的交点集合</returns>
- public static Point3dCollection GetInterPoint(
- this Curve curve1,
- Curve curve2,
- Intersect intersectOption = Intersect.OnBothOperands)
- {
- var intersectionPoints = new Point3dCollection();
- // 第一阶段:快速包围盒检查
- var box1 = curve1.GeometricExtents;
- var box2 = curve2.GeometricExtents;
-
- if (!box1.IsIntersect(box2))
- return intersectionPoints;
- var plan = new Plane(Point3d.Origin, Vector3d.ZAxis);
- // 第二阶段:大坐标偏移处理
- const double coordinateThreshold = 1e9; // 坐标偏移阈值
- // 计算合并包围盒中心
- var combinedBox = box1.CombinedBox(box2);
- var centroid = combinedBox.GetCenterPoint();
- // 判断是否需要坐标偏移
- bool needTransform =
- Math.Abs(centroid.X) > coordinateThreshold ||
- Math.Abs(centroid.Y) > coordinateThreshold;
- if (needTransform)
- {
- // 构建平移矩阵
- Vector3d offset = -centroid.GetAsVector();
- Matrix3d transform = Matrix3d.Displacement(offset);
- Matrix3d inverseTransform = Matrix3d.Displacement(offset.Negate());
- // 克隆并平移曲线
- using Curve transCurve1 = (Curve)curve1.Clone();
- using Curve transCurve2 = (Curve)curve2.Clone();
- transCurve1.TransformBy(transform);
- transCurve2.TransformBy(transform);
- // 在平移后坐标系求交
- var tempPoints = new Point3dCollection();
- transCurve1.IntersectWith(
- transCurve2,
- intersectOption,
- plan,
- tempPoints,
- IntPtr.Zero,
- IntPtr.Zero);
- // 坐标逆向转换
- foreach (Point3d pt in tempPoints)
- {
- intersectionPoints.Add(pt.TransformBy(inverseTransform));
- }
- }
- else
- {
- // 直接求交
- curve1.IntersectWith(
- curve2,
- intersectOption,
- plan,
- intersectionPoints,
- IntPtr.Zero,
- IntPtr.Zero);
- }
- return intersectionPoints;
- }
|