def pairwise_distance2(s):
    old_settings = np.seterr(all='ignore')
N = N_segments
p0 = np.repeat(s[:, 0:3].reshape((N, 1, 3)), N, axis=1)
p1 = np.repeat(s[:, 3:6].reshape((N, 1, 3)), N, axis=1)
q0 = np.repeat(s[:, 0:3].reshape((1, N, 3)), N, axis=0)
q1 = np.repeat(s[:, 3:6].reshape((1, N, 3)), N, axis=0)
a = np.sum((p1 - p0) * (p1 - p0), axis=-1).reshape((N, N, 1))
b = np.sum((p1 - p0) * (q1 - q0), axis=-1).reshape((N, N, 1))
c = np.sum((q1 - q0) * (q1 - q0), axis=-1).reshape((N, N, 1))
d = np.sum((p1 - p0) * (p0 - q0), axis=-1).reshape((N, N, 1))
e = np.sum((q1 - q0) * (p0 - q0), axis=-1).reshape((N, N, 1))
s = (b * e - c * d) / (a * c - b * b)
t = (a * e - b * d) / (a * c - b * b)
pairwise = np.sqrt(np.sum((p0 + (p1 - p0) * s - (q0 + (q1 - q0) * t)) ** 2,
    axis=-1))
np.seterr(**old_settings)
pairwise[np.tril_indices(N)] = 0.0
return pairwise
Python, Pairwise 'distance', need a fast way to do it