Class to get neighbouring polygons from edges and points.


This class assumes that the geometry is clean such that an edge is only shared by two polygons, if this is not the case then the information may not be correct.


The object passed to Neighbor.Init() must not be modified during the use of this class. For example, if you resize a polygon object or modify the polygons this class may crash if you then try and use the classes functions.


Return type: Neighbor
Returns: The new neighbor object.
Neighbor.Init(op[, bs=None])

Initialise the internal polygon information, this function must be called before the class can be used to get the neighbouring polygons.

  • op (PolygonObject) – The class object.
  • bs (BaseSelect) –

    BaseSelect that contains a polygon selection that will be used to build the neighbor information.


    Set to None to use all polygons.


Flushes the neighbor information.

Neighbor.GetNeighbor(a, b, poly)

Gets the polygon opposite to poly with respect to the edge from point a to b.

  • a (int) – The point index that defines the first edge point.
  • b (int) – The point index that defines the second edge point.
  • poly (int) – The index of the polygon to get the polygon opposite to.
  • poly – The index of the polygon to get the polygon opposite to.
Raises IndexError:

If a, b or poly is out of range.


The opposite polygon index, or NOTOK if none exists or if poly isn’t one of the edge polygons.


Get the polygons that are attached to the given point index.

For example:

To get the polygons around say point 137 you would use:

polys = neigbor.GetPointPolys(137)
for poly in polys:
    #do something with poly
Parameters: pnt (int) – The point index to use to find the associated polygons.
Raises IndexError:
  If pnt is out of range.
Return type: list
Returns: A list of returned polygons.
Neighbor.GetEdgePolys(a, b)

Get the polygons that neighbor the given edge:

first, second = neighbor.GetEdgePolys(1, 2)
  • a (int) – The point index that defines the first edge point.
  • b (int) – The point index that defines the second edge point.
Raises IndexError:

If a or b is out of range.

Return type:

tuple(int, int)


The first and second polygon associated with the edge.


Get the total number of edges found.

Return type: int
Returns: The number of edges.

Get a dict that contains neighbor information about the given polygon. One can use this to browse through all available edges using the following code:

nbr = utils.Neighbor()

vadr = op.GetAllPolygons()

for i in xrange(op.GetPolygonCount()):
    pli = nbr.GetPolyInfo(i)

    for side in xrange(4): # # test all 4 sides of a polygon
        # Only proceed if edge has not already been processed
        # and edge really exists (for triangles side 2 from c..d does not exist as c==d)

        if pli["mark"][side] or side==2 and vadr[i].c==vadr[i].d: continue

        # One can also skip the side==2 && vadr[i].c==vadr[i].d test as pli["mark"][2] is always True for triangles

        if side==0:
            a=vadr[i].a; b=vadr[i].b
        elif side==1:
            a=vadr[i].b; b=vadr[i].c
        elif side==2:
            a=vadr[i].c; b=vadr[i].d
        elif side==3:
            a=vadr[i].d; b=vadr[i].a

        # Do something with the edge a..b

0-1-2-3 are the indices for a-b/b-c/c-d/d-a. For triangles the face/edge index 2 is set to NOTOK (as c == d). e.g. a value of 5-8-2-1 for face means: a-b neighbor face is 5, b-c neighbor face is 8 etc.

Parameters: poly (int) – The polygon index to get the neighbor information for.
Return type: dict{mark: bool*4, face: int*4, edge: int*4}
Returns: The neighbor information about the given polygon.
Key mark: False if that polygon “generated” an edge, for example think of two polygons that share an edge, one has set mark = False for this edge because it was the first and “built” this edge and the other(s) will set mark = True as no new edge had to be generated.
Key face: The neighbouring polygons. Note: If a value is NOTOK this means there is no neighbor.
Key edge: The edges of the polygon.

Table Of Contents