【问题标题】:Is there a more efficient way to detect polygon overlap/intersection than PathGeometry.FillContainsWithDetail()?是否有比 PathGeometry.FillContainsWithDetail() 更有效的方法来检测多边形重叠/相交?
【发布时间】:2012-06-25 17:22:43
【问题描述】:

我有一个方法会占用我 25% 的 CPU 时间。我每秒调用这种方法大约 27,000 次。 (是的,很多电话,因为它经常更新)。我想知道是否有人知道一种更快的方法来检测 2 个多边形是否重叠。基本上,我必须检查屏幕上的移动对象和屏幕上的静止对象。我正在使用 PathGeometry,下面的两个调用占用了我的程序使用的 CPU 时间的 25%。我传递的 PointCollection 对象只包含代表多边形 4 个角的 4 个点。它们可能不会创建一个矩形区域,但所有点都是连接的。我猜应该是梯形。

这些方法很短并且很容易实现,但我想我可能希望选择一个更复杂的解决方案,如果我可以让它比下面的代码运行得更快。有什么想法吗?

public static bool PointCollectionsOverlap(PointCollection area1, PointCollection area2)
{
    PathGeometry pathGeometry1 = GetPathGeometry(area1);
    PathGeometry pathGeometry2 = GetPathGeometry(area2);
    return pathGeometry1.FillContainsWithDetail(pathGeometry2) != IntersectionDetail.Empty;
}

public static PathGeometry GetPathGeometry(PointCollection polygonCorners)
{
    List<PathSegment> pathSegments = new List<PathSegment> 
                                         { new PolyLineSegment(polygonCorners, true) };
    PathGeometry pathGeometry = new PathGeometry();
    pathGeometry.Figures.Add(new PathFigure(polygonCorners[0], pathSegments, true));
    return pathGeometry;
}

【问题讨论】:

  • 如果您有这么多对象,您是否总是针对每个形状测试每个形状?因为首先,您不需要针对所有测试全部, set{a, b} 1)a->a, 2)a->b 3)b->a, 4)b->a; 4 个测试可以减少到 1 个(只是为了解释我的意思)。否则,为简单起见,您可以将形状分类为四叉树或简单网格,并且仅在 2 个形状共享同一个单元格时才使用昂贵的重叠测试。您还可以在第二种方法中缓存 PathGeometry 对象,至少对于您的固定形状。
  • A 组最多有 100 个形状,B 组最多有 16 个形状。我循环遍历 A 组,对于 A 组中的每个项目,我检查 B 组中的所有形状,所以我只检查我需要做的事情,而不是重复任何检查。我可以缓存我的 SetB 形状的 PathGeometries,因为它们不会经常更改。那会有所帮助。我不确定将形状排序成四叉树是什么意思..
  • 考虑这张图片codeproject.com/KB/recipes/QuadTree/QuadTree4.png。您将静止形状放在四叉树中,为动态形状生成边界框,而不是针对每个静止形状测试每个形状。您首先让您的动态形状检索它当前重叠的四叉树中的单元格,并且这些单元格附有固定形状,因此您知道您只需要测试这些。谷歌四叉树,对于这种情况,它是一种非常常见且广泛使用的数据结构。

标签: wpf polygon polygons pathgeometry point-in-polygon


【解决方案1】:

好的,经过大量研究并找到许多部分答案,但没有一个完全回答问题,我找到了一种更快的方法,它实际上比旧方法快了大约 4.6 倍。

我创建了一个特殊的测试应用来测试这个速度。您可以找到测试应用程序here。如果您下载它,您可以在应用程序顶部看到一个复选框。选中和取消选中它以在旧方式和新方式之间来回切换。该应用程序生成一堆随机多边形,当多边形与另一个多边形相交时,多边形的边界变为白色。 “重绘”按钮左侧的数字允许您输入多边形数量、边的最大长度和与正方形的最大偏移量(以使它们不那么正方形而形状更奇数)。按“刷新”以使用您输入的设置清除并重新生成新多边形。

无论如何,这是两种不同实现的代码。您传入组成每个多边形的点的集合。旧方法使用的代码更少,但比新方法慢 4.6 倍

哦,一个简短的说明。新方法对“PointIsInsidePolygon”有几个调用。这些是必要的,因为没有它,当一个多边形完全包含在另一个多边形中时,该方法返回 false。但是 PointIsInsidePolygon 方法解决了这个问题。

希望这一切都能帮助其他人解决多边形截距和重叠问题。

Old Way(慢 4.6 倍。是的,真的慢 4.6 倍):

public static bool PointCollectionsOverlap_Slow(PointCollection area1, PointCollection area2)
{
    PathGeometry pathGeometry1 = GetPathGeometry(area1);
    PathGeometry pathGeometry2 = GetPathGeometry(area2);
    bool result = pathGeometry1.FillContainsWithDetail(pathGeometry2) != IntersectionDetail.Empty;
    return result;
}

public static PathGeometry GetPathGeometry(PointCollection polygonCorners)
{
    List<PathSegment> pathSegments = new List<PathSegment> { new PolyLineSegment(polygonCorners, true) };
    PathGeometry pathGeometry = new PathGeometry();
    pathGeometry.Figures.Add(new PathFigure(polygonCorners[0], pathSegments, true));
    return pathGeometry;
}

新方式(快 4.6 倍。是的,真的快 4.6 倍):

public static bool PointCollectionsOverlap_Fast(PointCollection area1, PointCollection area2)
{
    for (int i = 0; i < area1.Count; i++)
    {
        for (int j = 0; j < area2.Count; j++)
        {
            if (lineSegmentsIntersect(area1[i], area1[(i + 1) % area1.Count], area2[j], area2[(j + 1) % area2.Count]))
            {
                return true;
            }
        }
    }

    if (PointCollectionContainsPoint(area1, area2[0]) ||
        PointCollectionContainsPoint(area2, area1[0]))
    {
        return true;
    }

    return false;
}

public static bool PointCollectionContainsPoint(PointCollection area, Point point)
{
    Point start = new Point(-100, -100);
    int intersections = 0;

    for (int i = 0; i < area.Count; i++)
    {
        if (lineSegmentsIntersect(area[i], area[(i + 1) % area.Count], start, point))
        {
            intersections++;
        }
    }

    return (intersections % 2) == 1;
}

private static double determinant(Vector vector1, Vector vector2)
{
    return vector1.X * vector2.Y - vector1.Y * vector2.X;
}

private static bool lineSegmentsIntersect(Point _segment1_Start, Point _segment1_End, Point _segment2_Start, Point _segment2_End)
{
    double det = determinant(_segment1_End - _segment1_Start, _segment2_Start - _segment2_End);
    double t = determinant(_segment2_Start - _segment1_Start, _segment2_Start - _segment2_End) / det;
    double u = determinant(_segment1_End - _segment1_Start, _segment2_Start - _segment1_Start) / det;
    return (t >= 0) && (u >= 0) && (t <= 1) && (u <= 1);
}

【讨论】:

  • 非常感谢您。我能够将其移植到 javascript。为此,我借用了这个 Point 类 upshots.org/javascript/javascript-point-class。 Javascript Polyoverlap 在此处创建为要点:gist.github.com/3753426
  • 链接的网站 (gpwiki.org) 现在正在投放恶意广告。仍然可以在 Internet 档案中找到。
  • 我已经从我的帖子中删除了 gpwiki.org 链接。
  • 谢谢你,我只是不明白为什么 Point start = new Point(-100, -100); ?
  • 嗯,距离我第一次写这篇文章已经 8 年了。很抱歉,我不知道为什么要设置 Point start=new Point(-100, -100)...我只是不记得...我一定老了..
猜你喜欢
  • 2014-03-15
  • 1970-01-01
  • 2019-11-23
  • 2011-06-20
  • 2015-07-31
  • 2011-10-31
  • 2014-03-21
  • 1970-01-01
  • 2012-02-06
相关资源
最近更新 更多