Skip to content

pygeo.find_duplicate_coordinates

Daniel Flassig edited this page Jul 31, 2026 · 1 revision

Detects coordinates that lie within a tolerance of one another and reports which of them can be merged.

The function performs a purely geometric computation and does not create or modify any elements in the project. It is a useful processing step for point lists that are to be welded together for building a mesh or a polyline.

pygeo.find_duplicate_coordinates(coordinates [,tolerance])
Parameter Type Description
coordinates {{x,y[,z]}, ...} Array of coordinates. 2D or 3D coordinates are supported.
tolerance number Optional: maximum distance at which two coordinates are still considered duplicates. Must not be negative. Defaults to the general PYTHA tolerance.

Return value

Type Description
duplicates {[i] = index, ...}

Notes:

  • Two coordinates are duplicates if they differ by no more than tolerance in each coordinate direction. The test is therefore axis-aligned, not spherical: it accepts everything inside the cube of half-edge tolerance around a coordinate.
  • A surviving coordinate always carries a smaller index than the coordinates merged into it, and the reported index always refers to a surviving coordinate. Mappings never chain, and one forward pass over the result is enough to resolve them.
  • Guaranteed: every merged coordinate is within tolerance of the surviving coordinate it is mapped to, and no two surviving coordinates are within tolerance of one another. Clusters larger than the tolerance are therefore split, and which coordinate survives a cluster is implementation defined and depends on input order.
  • The result array contains nil elements, so use an explicit for loop from one to #coordinates or the pairs() function. ipairs will not work.
  • The coordinates themselves are never modified or averaged - the surviving coordinate keeps its exact input value.
  • Coordinates that contain a non-finite value take no part in the search: they are never merged into another coordinate, no coordinate is ever merged into them, and their entry stays nil.
  • An empty input array yields an empty result.

Example:

local coordinates = {
    {0.0,    0.0},
    {10.0,   0.0},
    {0.0005, 0.0},
    {10.0,   10.0},
    {9.9998, 10.0001},
}

local duplicates = pygeo.find_duplicate_coordinates(coordinates, 0.001)
-- duplicates[3] -> 1  (merged into coordinate 1)
-- duplicates[5] -> 4  (merged into coordinate 4)
-- all other entries are nil

-- build the welded coordinate list and an index translation table
local kept = {}
local new_index = {}

for i = 1, #coordinates do
    if duplicates[i] then
        new_index[i] = new_index[duplicates[i]]  -- the target is always an earlier, surviving coordinate
    else
        kept[#kept + 1] = coordinates[i]
        new_index[i] = #kept
    end
end
-- kept      -> {{0,0}, {10,0}, {10,10}}
-- new_index -> {1, 2, 1, 3, 3}

Version Support:

Minimum PYTHA Version: V27

See also:

pygeo, pygeo.create_triangulation, pygeo.clean_polygon_2d

Clone this wiki locally