Skip to content

Instantly share code, notes, and snippets.

@zeffii
Last active February 12, 2020 14:59
Show Gist options
  • Save zeffii/361d629ea0134b9f453213665ec3ef25 to your computer and use it in GitHub Desktop.
Save zeffii/361d629ea0134b9f453213665ec3ef25 to your computer and use it in GitHub Desktop.
"""
in size s d=1.0 n=2
in divx s d=10 n=2
in divy s d=10 n=2
in divz s d=10 n=2
out verts v
out faces s
"""
import numpy
import itertools
def make_verts(size, divx, divy, divz):
size /= 2
sidex = numpy.linspace(-size, size, divx)
sidey = numpy.linspace(-size, size, divy)
sidez = numpy.linspace(-size, size, divz)
plane = lambda z: [(x, y, z) for x in sidex[1:-1] for y in sidey[1:-1]]
base = plane(-size)
top = plane(size)
mid = []
add_mid = mid.append
for z in sidez:
# side 1
for y in sidey:
add_mid(tuple((sidex[0],y,z)))
# side 2
for x in sidex[1:-1]:
add_mid(tuple((x,sidey[-1],z)))
# side 3
for y in sidey[::-1]:
add_mid(tuple((sidex[-1],y,z)))
# side 4
for x in sidex[1:-1][::-1]:
add_mid(tuple((x,sidey[0],z)))
return [(mid + base + top)][0]
def roll_around(direction, outside, inside, divx, divy, num_iplanar):
final_list = []
final_list.append(outside[0:divy])
stride = divy-2
for idx, i in enumerate(range(0, num_iplanar, stride)):
side1 = outside[-(idx+1)]
side2 = outside[divy+idx]
final_list.append([side1] + inside[i:i+stride] + [side2])
opposite = divy + (divx-2)
final_list.append(outside[opposite:opposite+divy][::-1])
face_list = []
for i in range(divx-1):
for j in range(divy-1):
top_row = final_list[i][j:j+2]
low_row = [final_list[i+1][j+1], final_list[i+1][j]]
storage = top_row + low_row
if direction == 'ccw':
storage = storage[::-1]
face_list.append(storage)
return face_list
def make_faces(size, divx, divy, divz):
z_spread = (divx+divx+divy+divy) - 4
faces = []
idx = 0
# per z ring, loop and closing extend
for z_ring in range(divz-1):
offset = z_spread * z_ring
for idx in range(z_spread-1):
i = idx + offset
faces.extend([[i, i+1, i+1+z_spread, i+z_spread]])
faces.extend([[
z_spread - 1 + offset,
offset,
z_spread + offset,
z_spread + z_spread -1 + offset
]])
top_max = divz * z_spread
top_outer_ring = list(range(top_max-(z_spread), top_max))
bottom_outer_ring = list(range(0, z_spread))
# num internal planar
num_iplanar = (divx-2) * (divy-2)
# book keeping
num_verts_bass = divx * divy
num_verts_ring = len(top_outer_ring)
num_verts_ring_total = (divz - 2) * num_verts_ring
last_vertex_idx = (num_verts_bass * 2) + num_verts_ring_total
top_internal = list(range(last_vertex_idx-num_iplanar, last_vertex_idx))
bottom_internal = [i-num_iplanar for i in top_internal]
f = roll_around('cw', top_outer_ring, top_internal, divx, divy, num_iplanar)
faces.extend(f)
f = roll_around('ccw', bottom_outer_ring, bottom_internal, divx, divy, num_iplanar)
faces.extend(f)
return faces
verts.append(make_verts(size, divx, divy, divz))
faces.append(make_faces(size, divx, divy, divz))
"""
in size s d=1.0 n=2
in divx s d=10 n=2
in divy s d=10 n=2
in divz s d=10 n=2
out verts v
out faces s
"""
import numpy as np
import itertools
def make_verts(size, divx, divy, divz):
size /= 2
sidex = np.linspace(-size, size, divx)
sidey = np.linspace(-size, size, divy)
sidez = np.linspace(-size, size, divz)
base = np.vstack(np.meshgrid(sidex, sidey, -size)).reshape(3, -1).T.tolist()
top = np.vstack(np.meshgrid(sidex, sidey, size)).reshape(3, -1).T.tolist()
mid = []
add_mid = mid.append
for z in sidez:
# side 1
for y in sidey:
add_mid(tuple((sidex[0],y,z)))
# side 2
for x in sidex[1:-1]:
add_mid(tuple((x,sidey[-1],z)))
# side 3
for y in sidey[::-1]:
add_mid(tuple((sidex[-1],y,z)))
# side 4
for x in sidex[1:-1][::-1]:
add_mid(tuple((x,sidey[0],z)))
return [(mid + base + top)][0]
vert_list = make_verts(size, divx, divy, divz)
verts.append(vert_list)
"""
in size s d=1.0 n=2
in divx s d=10 n=2
in divy s d=10 n=2
in divz s d=10 n=2
out verts v
out faces s
"""
import numpy as np
import itertools
def make_verts(size, divx, divy, divz):
size /= 2
sidex = np.linspace(-size, size, divx)
sidey = np.linspace(-size, size, divy)
sidez = np.linspace(-size, size, divz)
base = np.vstack(np.meshgrid(sidex, sidey, -size)).reshape(3, -1).T.tolist()
top = np.vstack(np.meshgrid(sidex, sidey, size)).reshape(3, -1).T.tolist()
mid = []
add_mid = mid.extend
if sidez.shape[0] >= 3:
horizontal_slice = []
add_slice_part = horizontal_slice.extend
# side 1
for y in sidey:
add_slice_part(tuple((sidex[0], y, 0)))
# side 2
for x in sidex[1:-1]:
add_slice_part(tuple((x, sidey[-1], 0)))
# side 3
for y in sidey[::-1]:
add_slice_part(tuple((sidex[-1], y, 0)))
# side 4
for x in sidex[1:-1][::-1]:
add_slice_part(tuple((x, sidey[0], 0)))
slice = np.array(horizontal_slice).reshape(-1, 3)
Z = sidez[1:-1]
for z in np.nditer(Z):
slice[:, 2] = z
add_mid(slice.copy().reshape(-1, 3).tolist())
return [(mid + base + top)][0]
vert_list = make_verts(size, divx, divy, divz)
verts.append(vert_list)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment