Skip to content

Instantly share code, notes, and snippets.

@sofroniewn
Created September 15, 2019 17:16
Show Gist options
  • Save sofroniewn/f2302f4ad089af287e82537f3bb95805 to your computer and use it in GitHub Desktop.
Save sofroniewn/f2302f4ad089af287e82537f3bb95805 to your computer and use it in GitHub Desktop.
def generate_3D_path_meshes_2D(path, p=(0, 0, 1)):
"""Generates list of mesh vertices and triangles from a path
Parameters
----------
path : (N, D) array
Vertices specifying the path where D is 2 or 3.
p : 3-tuple, optional
orthogonal vector for segment calculation in 3D.
Returns
----------
centers : (4N-4, D) array
Vertices of all triangles for the lines
offsets : (4N-4, D) array
offsets of all triangles for the lines
triangles : (2N-2, 3) array
Vertex indices that form the mesh triangles
"""
ndim = path.shape[1]
centers = np.repeat(path, 4, axis=0)[2:-2]
offsets = segment_normal(path[:-1, :], path[1:, :], p=p)
offsets = np.repeat(offsets, 4, axis=0)
signs = np.ones((len(offsets), ndim))
signs[::2] = -1
offsets = offsets * signs
triangles = np.array(
[
[2 * i, 2 * i + 1, 2 * i + 2]
if i % 2 == 0
else [2 * i - 1, 2 * i, 2 * i + 1]
for i in range(2 * len(path) - 2)
]
).astype(np.uint32)
return centers, offsets, triangles
def generate_3D_path_meshes(path):
"""Generates list of mesh vertices and triangles from a path
Parameters
----------
path : (N, D) array
Vertices specifying the path where D is 2 or 3.
Returns
----------
centers : (4N-4, D) array
Vertices of all triangles for the lines
offsets : (4N-4, D) array
offsets of all triangles for the lines
triangles : (2N-2, 3) array
Vertex indices that form the mesh triangles
"""
ndim = path.shape[1]
if ndim == 2:
centers, offsets, triangles = generate_3D_path_meshes_2D(path)
else:
c_a, o_a, t_a = generate_3D_path_meshes_2D(path, p=(0, 0, 1))
c_b, o_b, t_b = generate_3D_path_meshes_2D(path, p=(1, 0, 0))
centers = np.concatenate([c_a, c_b], axis=0)
triangles = np.concatenate([t_a, len(c_a) + t_b], axis=0)
offsets = np.concatenate([o_a, o_b], axis=0)
return centers, offsets, triangles
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment