Skip to content

Instantly share code, notes, and snippets.

@iilei
Forked from asahidari/miura_fold_b3d_an.py
Created August 26, 2025 03:49
Show Gist options
  • Save iilei/ec9f0d6573eccb27efdfec37495787da to your computer and use it in GitHub Desktop.
Save iilei/ec9f0d6573eccb27efdfec37495787da to your computer and use it in GitHub Desktop.
"""
in x_dim s d=3 n=2
in y_dim s d=3 n=2
in theta s d=1.0471973 n=2
in alpha s d=0. n=2
out verts v
out edges s
out faces s
"""
"""
A---- B ---- C
/ / /
O---- D(I)----E
theta : angle between AOD
alpha : angle between DOI (I: point under D)
beta : angle between AOI
"""
from mathutils import Vector
from animation_nodes.data_structures import Vector3DList, EdgeIndicesList, PolygonIndicesList
# import numpy as np
import math
if x_dim < 1: x_dim = 1
if y_dim < 1: y_dim = 1
if math.cos(alpha) < 0.03:
beta = 0.0
else:
beta = math.acos(math.cos(theta)/math.cos(alpha))
# vertices of the unit cell
v_base = [
[math.cos(beta), math.sin(beta), 0],
[math.cos(alpha) + math.cos(beta), math.sin(beta), math.sin(alpha)],
[2*math.cos(alpha) + math.cos(beta), math.sin(beta), 0],
[0,0,0],
[math.cos(alpha), 0, math.sin(alpha)],
[2*math.cos(alpha), 0, 0],
[math.cos(beta), -math.sin(beta), 0],
[math.cos(alpha) + math.cos(beta), -math.sin(beta), math.sin(alpha)],
[2*math.cos(alpha) + math.cos(beta), -math.sin(beta), 0]
]
# arrange vertices
verts = Vector3DList()
v = []
for i in range(y_dim):
v1 = []
v2 = []
v3 = []
for j in range(x_dim):
x_offset = 2*math.cos(alpha)*j
y_offset = -2*math.sin(beta)*i
if j == 0:
if i == 0:
v1.append(Vector((v_base[0][0], v_base[0][1], v_base[0][2])))
v2.append(Vector((v_base[3][0], y_offset + v_base[3][1], v_base[3][2])))
v3.append(Vector((v_base[6][0], y_offset + v_base[6][1], v_base[6][2])))
if i == 0:
v1.append(Vector((x_offset + v_base[1][0], y_offset + v_base[1][1], v_base[1][2])))
v1.append(Vector((x_offset + v_base[2][0], y_offset + v_base[2][1], v_base[2][2])))
v2.append(Vector((x_offset + v_base[4][0], y_offset + v_base[4][1], v_base[4][2])))
v2.append(Vector((x_offset + v_base[5][0], y_offset + v_base[5][1], v_base[5][2])))
v3.append(Vector((x_offset + v_base[7][0], y_offset + v_base[7][1], v_base[7][2])))
v3.append(Vector((x_offset + v_base[8][0], y_offset + v_base[8][1], v_base[8][2])))
if i == 0:
v.extend(v1)
v.extend(v2)
v.extend(v3)
verts.extend(v)
# define edges
edges = EdgeIndicesList()
edge_set = set()
y = 3 + 2*(y_dim-1)
x = 3 + 2*(x_dim-1)
index = 0
for i in range(y-1):
for j in range(x-1):
index = i * x + j
edge_set.add(tuple(sorted([index, index+1])))
edge_set.add(tuple(sorted([index, index+x])))
# right side
edge_set.add(tuple(sorted([index + 1, index+x + 1])))
# bottom line
for k in range(x-1):
index = (y-1) * x + k
edge_set.add(tuple(sorted([index, index + 1])))
edges.extend(list(edge_set))
# define faces
faces = PolygonIndicesList()
face_set = set()
for i in range(y-1):
for j in range(x-1):
index = i * x + j
face_set.add(tuple((index, index+1, index+x+1, index+x)))
faces.extend(list(face_set))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment