import k3d
import numpy as np
from sklearn.neighbors import NearestNeighbors
plot = k3d.plot()
N = 10000
vertices = np.random.normal(size=(N, 3)).astype(np.float32)
nbrs = NearestNeighbors(n_neighbors=3, algorithm='ball_tree').fit(vertices)
distances, indices = nbrs.kneighbors(vertices)
obj = k3d.mesh(vertices, indices.astype(np.uint32), side='double')
plot += obj
plot.display()
triangles_attribute = []
for ind in indices:
v = vertices[ind]
triangles_attribute.append(np.linalg.norm(np.cross(v[2] - v[1], v[0] - v[1])) * 0.5)
triangles_attribute = np.array(triangles_attribute, dtype=np.float32)
obj.triangles_attribute = triangles_attribute
obj.color_range = (np.max(triangles_attribute) / 8, 0)