-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathpenrose2.py
More file actions
307 lines (289 loc) · 9.4 KB
/
penrose2.py
File metadata and controls
307 lines (289 loc) · 9.4 KB
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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
import math
import cmath
##import cairo
import bpy
#------ Configuration --------
IMAGE_SIZE = (1000, 1000)
NUM_SUBDIVISIONS = 5
POLY = 6
MGOLDENRATIO = False
#-----------------------------
goldenRatio = (1 + math.sqrt(5)) / 2
def subdivide(triangles):
result = []
for color, A, B, C in triangles:
if color == 0:
# Subdivide red triangle
P = A + (B - A) / goldenRatio
result += [(0, C, P, B), (1, P, C, A)]
else:
# Subdivide blue triangle
Q = B + (A - B) / goldenRatio
R = B + (C - B) / goldenRatio
result += [(1, R, C, A), (1, Q, R, B), (0, R, Q, A)]
return result
def measurePhi():
B = cmath.rect(1,math.pi/2.0)
C = cmath.rect(1,math.pi/2.0-2.0*math.pi/(POLY/2.0))
BC = C-B
sidelen = abs(BC)
chord = BC.real
chord *= 2.0
return chord/sidelen
def clockwisewalktest(walk):
## works with nonconvex polygons should be safe
## I believe for the primitive polygon type (3 vertices)
## constructed in this algorithm.
prev = None
samt = 0.0
newwalk = walk[0:len(walk)]
newwalk.append(walk[0])
for vert in newwalk:
if prev == None:
prev = vert
continue
## samt += (vert[0]-prev[0])*(vert[1]+prev[1])
samt += (prev[0]*vert[1]-prev[1]*vert[0])
## if walk.index(vert) == len(walk)-1:
## samt += (walk[0][0] - vert[0])*(walk[0][1]+vert[1])
prev = vert
##print('samt: ', samt)
if samt < 0:
##print('original walk is clockwise')
return True
else:
##print('original walk is counter clockwise')
return False
def connectivity(triangle, vertsface):
## check for connectivity
color,A,B,C = triangle
if (A,B) in vertsface:
for V in vertsface[(A,B)]:
if V != C:
if color == vertsface[(A,B)][V][1]:
return (True, (A,B), C)
if (B,A) in vertsface:
for V in vertsface[(B,A)]:
if V != C:
if color == vertsface[(B,A)][V][1]:
return (True, (B,A), C)
if (A,C) in vertsface:
for V in vertsface[(A,C)]:
if V != B:
if color == vertsface[(A,C)][V][1]:
return (True, (A,C), B)
if (C,A) in vertsface:
for V in vertsface[(C,A)]:
if V != B:
if color == vertsface[(C,A)][V][1]:
return (True, (C,A), B)
if (B,C) in vertsface:
for V in vertsface[(B,C)]:
if V != A:
if color == vertsface[(B,C)][V][1]:
return (True, (B,C), A)
if (C,B) in vertsface:
for V in vertsface[(C,B)]:
if V != A:
if color == vertsface[(C,B)][V][1]:
return (True, (C,B), A)
return (False, (None,None), None)
def updatevertsface(triangle, vertsface, faceindex):
color, A,B,C = triangle
if not (A,B) in vertsface:
vertsface[(A,B)] = {C:(faceindex,color)}
else:
if not C in vertsface[(A,B)]:
vertsface[(A,B)][C] = (faceindex,color)
if not (A,C) in vertsface:
vertsface[(A,C)] = {B:(faceindex,color)}
else:
if not B in vertsface[(A,C)]:
vertsface[(A,C)][B] = (faceindex,color)
if not (B,C) in vertsface:
vertsface[(B,C)] = {A:(faceindex,color)}
else:
if not A in vertsface[(B,C)]:
vertsface[(B,C)][A] = (faceindex,color)
if MGOLDENRATIO:
goldenRatio = measurePhi()
# Create wheel of red triangles around the origin
##triangles = []
##for i in range(POLY):
#### if i % 2 == 0:
## B = cmath.rect(1, (2*i - 1) * math.pi / POLY)
## C = cmath.rect(1, (2*i + 1) * math.pi / POLY)
#### else:
#### B = cmath.rect(0.6180339887498948, (2*i - 1) * math.pi / 10)
#### C = cmath.rect(1, (2*i + 1) * math.pi / 10)
## if i % 2 == 0:
## B, C = C, B # Make sure to mirror every second triangle
## triangles.append((0, 0j, B, C))
def penrosesubdivde(triangles, subdivide = True):
# Perform subdivisions
if subdivide:
for i in range(NUM_SUBDIVISIONS):
triangles = subdivide(triangles)
vertices = []
faces = []
vertindex = {}
vertsface = {}
## vertsface keyed by triangle double complex pair
## values are tuple paired by face index and color assignment
for color, A, B, C in triangles:
Ax = A.real
Ay = A.imag
Av = (Ax,Ay,0.0)
Bx = B.real
By = B.imag
Bv = (Bx,By,0.0)
Cx = C.real
Cy = C.imag
Cv = (Cx,Cy,0.0)
face = []
wface = []
## if check:
## faceind, fc = vertsface[pair]
## face = list(faces[faceind])
## A,B = pair
## C = add
## Ax = A.real
## Ay = A.imag
## Av = (Ax,Ay,0.0)
## Bx = B.real
## By = B.imag
## Bv = (Bx,By,0.0)
## Cx = C.real
## Cy = C.imag
## Cv = (Cx,Cy,0.0)
## Cind = None
## if Cv in vertindex:
## Cind = vertindex[Cv]
## else:
## vertices.append(Cv)
## Cind = len(vertices)-1
## vertindex[Cv] = Cind
## if Bv in vertindex:
## Bind = vertindex[Bv]
## Binsertind = face.index(Bind)
## face.insert(Binsertind, Cind)
## faces[faceind] = face
## updatevertsface((color,A,B,C), vertsface, faceind)
## else:
if Av in vertindex:
face.append(vertindex[Av])
wface.append(Av)
else:
vertices.append(Av)
Aind = len(vertices)-1
face.append(Aind)
wface.append(Av)
vertindex[Av] = Aind
if Bv in vertindex:
face.append(vertindex[Bv])
wface.append(Bv)
else:
vertices.append(Bv)
Bind = len(vertices)-1
face.append(Bind)
vertindex[Bv] = Bind
wface.append(Bv)
if Cv in vertindex:
face.append(vertindex[Cv])
wface.append(Cv)
else:
vertices.append(Cv)
Cind = len(vertices)-1
face.append(Cind)
vertindex[Cv] = Cind
wface.append(Cv)
if not clockwisewalktest(wface):
face0 = [face[0]]
facec = face[1:len(face)]
facec = facec[::-1]
face0 += facec
face = face0
faces.append(tuple(face))
faceind = len(faces)-1
updatevertsface((color,A,B,C), vertsface, faceind)
##for color, A, B, C in triangles:
## check, pair, add = connectivity((color,A,B,C), vertsface)
## if check:
## faceind, fc = vertsface[pair]
## face = list(faces[faceind])
## A,B = pair
## C = add
## Ax = A.real
## Ay = A.imag
## Av = (Ax,Ay,0.0)
## Bx = B.real
## By = B.imag
## Bv = (Bx,By,0.0)
## Cx = C.real
## Cy = C.imag
## Cv = (Cx,Cy,0.0)
## Cind = None
## if Cv in vertindex:
## Cind = vertindex[Cv]
## else:
## vertices.append(Cv)
## Cind = len(vertices)-1
## vertindex[Cv] = Cind
## if Bv in vertindex:
## Bind = vertindex[Bv]
## Binsertind = face.index(Bind)
## face.insert(Binsertind, Cind)
## faces[faceind] = face
## meshName = "PenroseGraph"
## obName = "PenroseGraphObj"
## me = bpy.data.meshes.new(meshName)
## ob = bpy.data.objects.new(obName, me)
## ob.location = bpy.context.scene.cursor_location
## bpy.context.scene.objects.link(ob)
## me.from_pydata(vertices,[],faces)
## me.update(calc_edges=True)
##
##
## scn = bpy.context.scene
## scn.objects.active = ob
## ob.select = True
## obj = bpy.context.active_object
##
## bpy.ops.object.mode_set(mode = 'EDIT')
## bpy.ops.mesh.select_all(action = 'DESELECT')
## bpy.ops.object.mode_set(mode = 'OBJECT')
##
## cvertices = obj.data.vertices
## cfaces = obj.data.polygons
selectionverts = []
for vertpair in vertsface:
if len(vertsface[vertpair]) > 1:
colorbreak = False
for value in vertsface[vertpair]:
s,color = vertsface[vertpair][value]
if color == 0:
## colorbreak = True
## cfaces[s].select = True
selectionverts.append(s)
## if colorbreak:
## continue
## A,B = vertpair
## Ax = A.real
## Ay = A.imag
## Av = (Ax,Ay,0.0)
## Bx = B.real
## By = B.imag
## Bv = (Bx,By,0.0)
## Aind = vertindex[Av]
## Bind = vertindex[Bv]
## cvertices[Aind].select = True
## cvertices[Bind].select = True
## bpy.ops.object.mode_set(mode = 'EDIT')
## bpy.ops.mesh.dissolve_edges()
## bpy.ops.mesh.select_all(action = 'DESELECT')
## bpy.ops.object.mode_set(mode = 'OBJECT')
## bpy.ops.object.mode_set(mode = 'EDIT')
## bpy.ops.object.vertex_group_add()
## bpy.ops.object.vertex_group_assign()
## bpy.ops.mesh.dissolve_edges()
return (vertices,faces, selectionverts)