I created some Graphs with networkx and now I want to smooth the connections of them with a B-Spline function. I found an very helpful post here for that: B-Spline But if I want to do it like that the order of the points is very important. For some Graphs it is easy to order them, but if they conclude T-Junctions it gets more difficult. For example it worked for this Graph pretty good and the results of the other graphs should look similar to that: Example Graph Example Result
But I had some issues for this graphs: Issue: Graph 1 Issue: Graph 2
The function for the working example is:
def bSplineCurves(G):
connectedComp = nx.connected_components(G)
for comp in connectedComp:
sortedComp = sortNodes(G, list(comp))
points = tuplesToPoints(sortedComp)
degree = 3
points = points + points[0:degree + 1]
points = np.array(points)
x = points[:,0]
y = points[:,1]
t = range(len(x))
ipl_t = np.linspace(1.0, len(points) - degree, 1000)
x_tup = si.splrep(t, x, k=degree, per=1)
y_tup = si.splrep(t, y, k=degree, per=1)
x_list = list(x_tup)
xl = x.tolist()
x_list[1] = [0.0] + xl + [0.0, 0.0, 0.0, 0.0]
y_list = list(y_tup)
yl = y.tolist()
y_list[1] = [0.0] + yl + [0.0, 0.0, 0.0, 0.0]
x_i = si.splev(ipl_t, x_list)
y_i = si.splev(ipl_t, y_list)
plt.plot(x_i, y_i, 'r')
plt.show()
return G
Has anybody an alternative way to use the B-Spline function on graphs? Or does anybody knows a good way to sort every kind of component that is returned by the connected_components function?
Edit:
With the help of the comment from @Paul Broderson I've manged to reach the following results: Result 1 Result 2
The chain_decomposition function works good on the first result, but not for the second. It didn't recognize the edges at the outside of the graph, that are not part of a cycle. Is there another function that would detect every path? Or do I have to implement something by myself?
My current code:
def chainToPoints(chain):
points = list()
for edge in chain:
points.append(edge[0])
points.append(chain[-1][1])
return points
def bSplineCurvesAlternative(G):
chain_decomposition = nx.chain_decomposition(G)
for chain in chain_decomposition:
points = chainToPoints(chain, G)
if points[0] == points[-1]:
bSplineForCycle(points)
else:
bSplineForPath(points)
plt.show()