Skip to content

Instantly share code, notes, and snippets.

@asahidari
Last active June 10, 2020 16:09
Show Gist options
  • Save asahidari/33a79e0314b227bc871cea2c43f8a0f3 to your computer and use it in GitHub Desktop.
Save asahidari/33a79e0314b227bc871cea2c43f8a0f3 to your computer and use it in GitHub Desktop.
"""
in n_dimension s d=2 n=2
in a_radian s d=0.4 n=2
in x_dim s d=20 n=2
in y_dim s d=20 n=2
out verts v
out edges s
out faces s
"""
import cmath
import numpy as np
from math import sin, cos, sinh, cosh, pi
from mathutils import Vector
def calcZ1(x, y, k, n):
return cmath.exp(1j*(2*cmath.pi*k/n)) * (cmath.cos(x+y*1j)**(2/n))
def calcZ2(x, y, k, n):
return cmath.exp(1j*(2*cmath.pi*k/n)) * (cmath.sin(x+y*1j)**(2/n))
def calcZ1Real(x, y, k, n):
return (calcZ1(x, y, k, n)).real
def calcZ2Real(x, y, k, n):
return (calcZ2(x, y, k, n)).real
def calcZ(x, y, k1_, k2_, n, a_):
z1 = calcZ1(x, y, k1, n)
z2 = calcZ2(x, y, k2, n)
return z1.imag * cos(a_) + z2.imag*sin(a_)
x = np.linspace(0, pi/2, x_dim)
y = np.linspace(-pi/2, pi/2, y_dim)
x, y = np.meshgrid(x, y)
verts = [[]]
edges = [[]]
edge_set = []
faces = [[]]
face_set = []
n = n_dimension
for i in range(n*n):
edge_set.append(set())
face_set.append(set())
count = 0
for k1 in range(n):
for k2 in range(n):
# calc X, Y, Z values
X = np.frompyfunc(calcZ1Real, 4, 1)(x, y, k1, n).astype('float32')
Y = np.frompyfunc(calcZ2Real, 4, 1)(x, y, k2, n).astype('float32')
Z = np.frompyfunc(calcZ, 6, 1)(x, y, k1, k2, n, a_radian).astype('float32')
X_ = X.flatten()
Y_ = Y.flatten()
Z_ = Z.flatten()
v = []
for x1, y1, z1 in zip(X_, Y_, Z_):
v.append((x1, y1, z1))
verts[0].extend(v)
for i in range(x_dim * y_dim):
y_index = i / y_dim
x_index = i % y_dim
j = i + count * x_dim * y_dim
if (y_index < y_dim - 1) and (x_index < x_dim - 1):
edge_set[count].add(tuple(sorted([j, j+y_dim])))
edge_set[count].add(tuple(sorted([j+y_dim, j+y_dim+1])))
edge_set[count].add(tuple(sorted([j+y_dim+1, j+1])))
edge_set[count].add(tuple(sorted([j+1, j])))
face_set[count].add(tuple(([j, j+y_dim, j+y_dim+1, j+1])))
count += 1
for i in range(n*n):
edges[0].extend(list(edge_set[i]))
faces[0].extend(list(face_set[i]))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment