0

I have been working on this for quite some time. I have a list of Delaunay triangles with knowing all the vertex, now I need to calculate the neighbors of each triangle.

I know that python has the module Delaunay in scipy.spatial, which can be used to calculate the simplices and the neighbors knowing a list of points. But how can i calculate the neighbors given all the simplices.

The list of triangles look like this:

[[[634706.612442, 3086432.2967], [635268.645733, 3086636.61233],[634830.249107, 3087157.20293]]
[[634706.612442, 3086432.2967], [634830.249107, 3087157.20293], [634401.962216, 3086874.97886]]
[[656237.10083, 3061518.637755], [656776.863279, 3061883.38021], [656330.134218, 3062431.49804]]
[[656237.10083, 3061518.637755], [656330.134218, 3062431.49804], [655787.935768, 3061995.043438]]
[[656541.118122, 3060981.747767], [657223.592341, 3061335.26239], [656776.863279, 3061883.38021]]
[[656541.118122, 3060981.747767], [656776.863279, 3061883.38021], [656237.10083, 3061518.637755]]] 

The x,y coordinate of each vertex are given.

Paul Floyd
  • 5,530
  • 5
  • 29
  • 43
fengdongyuxi
  • 101
  • 1
  • 7

3 Answers3

0

You can loop through all edges of the triangles and test if it shared the same edges.

Micromega
  • 12,486
  • 7
  • 35
  • 72
  • Thank you for the reply! That's a good option. But I have too many of these triangles, more than 40,000. These triangles are the result of grid generator. It will be quite time consuming in my opinion. – fengdongyuxi Aug 21 '15 at 21:59
0

I have solved this problem. Actually, there is such module in Python that the neighbors of every triangle can be obtained with knowing the vertices of each triangle.

Such function is matplotlib.tri.Triangulation(x, y, triangles=None, mask=None) This class has two attributes: edges and neighbors that can be used to calculate the neighbors of each triangle.

fengdongyuxi
  • 101
  • 1
  • 7
0

My solution: Look at all the combinations of the simplices and then build a graph in a dictionary list.

from collections import defaultdict
from itertools import permutations
import matplotlib.pyplot as plt

points = np.array([[0, 0], [0, 1.1], [1, 0], [1, 1]])
tri = Delaunay(points)

# Visualize Delaunay Graph
plt.triplot(points[:, 0], points[:, 1], tri.simplices)
plt.plot(points[:, 0], points[:, 1], 'o')
for j, p in enumerate(points):
    plt.text(p[0] - 0.03, p[1] + 0.03, j, ha='right')  # label the points

graph = defaultdict(list)
for simplex in tri.simplices:
    sc = permutations(simplex, 2)
    for cc in sc:
        if cc[1] not in graph[cc[0]]:
            graph[cc[0]].append(cc[1])

print(graph)
Brian Wij
  • 21
  • 1
  • 5