Finding near neighbors

前端 未结 3 543
梦如初夏
梦如初夏 2021-02-07 07:12

I need to find \"near\" neighbors among a set of points.

\"pointSet\"

There are 10 points in the above imag

3条回答
  •  一生所求
    2021-02-07 07:24

    You can implement your first idea of selecting edges whose mid-points fall on the intersection with the Voronoi lines by making use of the DelaunayTri class and its edges and nearestNeighbor methods. Here's an example with 10 random pairs of x and y values:

    x = rand(10,1);                     %# Random x data
    y = rand(10,1);                     %# Random y data
    dt = DelaunayTri(x,y);              %# Compute the Delaunay triangulation
    edgeIndex = edges(dt);              %# Triangulation edge indices
    midpts = [mean(x(edgeIndex),2) ...  %# Triangulation edge midpoints
              mean(y(edgeIndex),2)];
    nearIndex = nearestNeighbor(dt,midpts);  %# Find the vertex nearest the midpoints
    keepIndex = (nearIndex == edgeIndex(:,1)) | ...  %# Find the edges where the
                (nearIndex == edgeIndex(:,2));       %#   midpoint is not closer to
                                                     %#   another vertex than it is
                                                     %#   to one of its end vertices
    edgeIndex = edgeIndex(keepIndex,:);      %# The "good" edges
    

    And now edgeIndex is an N-by-2 matrix where each row contains the indices into x and y for one edge that defines a "near" connection. The following plot illustrates the Delaunay triangulation (red lines), Voronoi diagram (blue lines), midpoints of the triangulation edges (black asterisks), and the "good" edges that remain in edgeIndex (thick red lines):

    triplot(dt,'r');  %# Plot the Delaunay triangulation
    hold on;          %# Add to the plot
    plot(x(edgeIndex).',y(edgeIndex).','r-','LineWidth',3);  %# Plot the "good" edges
    voronoi(dt,'b');  %# Plot the Voronoi diagram
    plot(midpts(:,1),midpts(:,2),'k*');  %# Plot the triangulation edge midpoints
    

    enter image description here

    How it works...

    The Voronoi diagram is comprised of a series of Voronoi polygons, or cells. In the above image, each cell represents the region around a given triangulation vertex which encloses all the points in space that are closer to that vertex than any other vertex. As a result of this, when you have 2 vertices that aren't close to any other vertices (like vertices 6 and 8 in your image) then the midpoint of the line joining those vertices falls on the separating line between the Voronoi cells for the vertices.

    However, when there is a third vertex that is close to the line joining 2 given vertices then the Voronoi cell for the third vertex may extend between the 2 given vertices, crossing the line joining them and enclosing that lines midpoint. This third vertex can therefore be considered a "nearer" neighbor to the 2 given vertices than the 2 vertices are to each other. In your image, the Voronoi cell for vertex 7 extends into the region between vertices 1 and 2 (and 1 and 3), so vertex 7 is considered a nearer neighbor to vertex 1 than vertex 2 (or 3) is.

    In some cases, this algorithm may not consider two vertices as "near" neighbors even though their Voronoi cells touch. Vertices 3 and 5 in your image are an example of this, where vertex 2 is considered a nearer neighbor to vertices 3 or 5 than vertices 3 or 5 are to each other.

提交回复
热议问题