-
Notifications
You must be signed in to change notification settings - Fork 3
/
origami.py
executable file
·152 lines (116 loc) · 5.43 KB
/
origami.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
#! /usr/bin/python
### Make Origami vertices using the Miura-Ori design
import numpy as np
from scipy.interpolate import griddata
import math
def make_ori_MO(m_in,n_in,scale_in):
# Origami parameters
ori_size = 100.0
m = m_in*2
n = n_in*2
Tx = 2.0*ori_size/(m)
Ty = 2.0*ori_size/(n)
hx = Tx*scale_in
hy = Ty*scale_in
Vx = np.zeros([m,n])
Vy = np.zeros([m,n])
Vz = np.zeros([m,n])
for i in range(1,m+1):
for j in range(1,n+1):
Vx[i-1,j-1] = (i-1)*Tx/2
Vy[i-1,j-1] = ((j-1)*Ty/2) + ((1 + (-1)**i)/2)*(hx/hy)*np.sqrt((Ty/2)**2 + hy**2)
Vz[i-1,j-1] = (1 + (-1)**j)*hy/2
# Make creases
Vtx = np.zeros([m,n])
Vty = np.zeros([m,n])
Vtx[0,0] = 0.0
Vty[0,0] = 0.0
for i in range(1,m):
vec_i_1 = np.array([ Vx[i-1,0], Vy[i-1,0], Vz[i-1,0] ])
vec_i_2 = np.array([ Vx[i-1,1], Vy[i-1,2], Vz[i-1,2] ])
vec_ip1_1 = np.array([ Vx[i,0], Vy[i,0], Vz[i,0] ])
vec_ip1_2 = np.array([ Vx[i,1], Vy[i,1], Vz[i,1] ])
cos_phi = ((np.linalg.norm(vec_i_1-vec_i_2)**2) + (np.linalg.norm(vec_i_1-vec_ip1_1)**2) - (np.linalg.norm(vec_i_2-vec_ip1_1)**2))/(2.0*np.linalg.norm(vec_i_1-vec_i_2)*np.linalg.norm(vec_i_1-vec_ip1_1))
cos_chi = ((np.linalg.norm(vec_ip1_1-vec_ip1_2)**2) + (np.linalg.norm(vec_i_1-vec_ip1_1)**2) - (np.linalg.norm(vec_ip1_2-vec_i_1)**2))/(2.0*np.linalg.norm(vec_ip1_1-vec_ip1_2)*np.linalg.norm(vec_i_1-vec_ip1_1))
if ((cos_phi <= 1.0) and (cos_phi >= 0.0)):
phi = np.arccos(cos_phi)
Vtx[i,0] = Vtx[i-1,0] + (np.sin(phi)*np.linalg.norm(vec_i_1-vec_ip1_1))
Vty[i,0] = Vty[i-1,0] + (np.cos(phi)*np.linalg.norm(vec_i_1-vec_ip1_1))
else:
chi = np.arccos(cos_chi)
Vtx[i,0] = Vtx[i-1,0] + (np.sin(chi)*np.linalg.norm(vec_i_1-vec_ip1_1))
Vty[i,0] = Vty[i-1,0] - (np.cos(chi)*np.linalg.norm(vec_i_1-vec_ip1_1))
for j in range(1,n):
for i in range(0,m):
vec_i_j = np.array([ Vx[i,j], Vy[i,j], Vz[i,j] ])
vec_i_jm1 = np.array([ Vx[i,j-1], Vy[i,j-1], Vz[i,j-1] ])
Vtx[i,j] = Vtx[i,j-1]
Vty[i,j] = Vty[i,j-1] + np.linalg.norm(vec_i_jm1-vec_i_j)
# Convert to tuples
x_extent = (max(Vtx[:,0]) - min(Vtx[:,0]))*(m/(m-1.0))
y_extent = (max(Vty[0,:]) - min(Vty[0,:]))*(n/(n-1.0))
crease_x = Vtx.reshape(m*n,1)
crease_y = Vty.reshape(m*n,1)
initpoints_i_j = np.array([])
initpoints_i_j = np.append(crease_x,crease_y,1) # i,j
initpoints_ip1_j = np.append(crease_x+x_extent,crease_y,1) # i+1,j
initpoints_im1_j = np.append(crease_x-x_extent,crease_y,1) # i-1,j
initpoints_i_jp1 = np.append(crease_x,crease_y+y_extent,1) # i,j+1
initpoints_i_jm1 = np.append(crease_x,crease_y-y_extent,1) # i,j-1
initpoints_ip1_jp1 = np.append(crease_x+x_extent,crease_y+y_extent,1) # i+1,j+1
initpoints_ip1_jm1 = np.append(crease_x+x_extent,crease_y-y_extent,1) # i+1,j-1
initpoints_im1_jp1 = np.append(crease_x-x_extent,crease_y+y_extent,1) # i-1,j+1
initpoints_im1_jm1 = np.append(crease_x-x_extent,crease_y-y_extent,1) # i-1,j-1
initpoints = initpoints_i_j
initpoints = np.append(initpoints,initpoints_ip1_j,0)
initpoints = np.append(initpoints,initpoints_im1_j,0)
initpoints = np.append(initpoints,initpoints_i_jp1,0)
initpoints = np.append(initpoints,initpoints_i_jm1,0)
initpoints = np.append(initpoints,initpoints_ip1_jp1,0)
initpoints = np.append(initpoints,initpoints_ip1_jm1,0)
initpoints = np.append(initpoints,initpoints_im1_jp1,0)
initpoints = np.append(initpoints,initpoints_im1_jm1,0)
## Normalize to [0,1]
initpoints[:,0] = initpoints[:,0]/x_extent
initpoints[:,1] = initpoints[:,1]/y_extent
# Convert Origami vertices to tuples
values_ij = Vx.reshape(m*n,1)
x_extent = (max(Vx[:,0]) - min(Vx[:,0]))*(m/(m-1.0))
values = values_ij
values = np.append(values,values_ij+x_extent,0)
values = np.append(values,values_ij-x_extent,0)
values = np.append(values,values_ij,0)
values = np.append(values,values_ij,0)
values = np.append(values,values_ij+x_extent,0)
values = np.append(values,values_ij+x_extent,0)
values = np.append(values,values_ij-x_extent,0)
values = np.append(values,values_ij-x_extent,0)
valuesx = values/x_extent
values_ij = Vy.reshape(m*n,1)
y_extent = (max(Vy[0,:]) - min(Vy[0,:]))*(n/(n-1.0))
values = values_ij
values = np.append(values,values_ij,0)
values = np.append(values,values_ij,0)
values = np.append(values,values_ij+y_extent,0)
values = np.append(values,values_ij-y_extent,0)
values = np.append(values,values_ij+y_extent,0)
values = np.append(values,values_ij-y_extent,0)
values = np.append(values,values_ij+y_extent,0)
values = np.append(values,values_ij-y_extent,0)
valuesy = values/y_extent
values_ij = Vz.reshape(m*n,1)
values = values_ij
values = np.append(values,values_ij,0)
values = np.append(values,values_ij,0)
values = np.append(values,values_ij,0)
values = np.append(values,values_ij,0)
values = np.append(values,values_ij,0)
values = np.append(values,values_ij,0)
values = np.append(values,values_ij,0)
values = np.append(values,values_ij,0)
valuesz = values/math.sqrt(x_extent*y_extent)
folded = valuesx
folded = np.append(folded, valuesy, 1)
folded = np.append(folded, valuesz, 1)
unfolded = initpoints
return unfolded, folded