-
Notifications
You must be signed in to change notification settings - Fork 0
/
splineTree.py
380 lines (322 loc) · 14.6 KB
/
splineTree.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
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
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
#!/usr/bin/python3
# -*- coding: utf-8 -*-
##
import sys
import numpy as np
from PyQt5.QtWidgets import QWidget, QDesktopWidget, QTextEdit, QApplication, QDoubleSpinBox, QSpinBox, QGridLayout, QHBoxLayout, QVBoxLayout, QLabel, QSpacerItem, QSizePolicy, QPushButton
from PyQt5.QtCore import Qt, QObject, QThread, pyqtSignal, pyqtSlot
from PyQt5.QtGui import QCloseEvent, QPixmap, QImage
from matplotlib.backends.backend_qt5agg import FigureCanvasQTAgg as FigureCanvas
from matplotlib.figure import Figure
import matplotlib.pyplot as plt
import matplotlib.patches as patches
import bezier
import time
current_milli_time = lambda: int(round(time.time() * 1000))
class Noise3D(QObject):
def __init__(self, noise_depth=192, noise_height=192, noise_width=192):
super().__init__()
self.depth = noise_depth
self.height = noise_height
self.width = noise_width
self.noise = np.abs(np.random.randn(self.depth,self.height,self.width))
def smooth(self, x, y, z):
# get fractional part
fractX = x - int(x)
fractY = y - int(y)
fractZ = z - int(z)
# wrap around
x1 = (int(x) + self.width) % self.width
y1 = (int(y) + self.height) % self.height
z1 = (int(z) + self.depth) % self.depth
# neighbor values
x2 = (x1 + self.width - 1) % self.width
y2 = (y1 + self.height - 1) % self.height
z2 = (z1 + self.depth - 1) % self.depth
# smooth the noise with bilinear interpolation
value = 0.0
value += fractX * fractY * fractZ * self.noise[z1,y1,x1]
value += fractX * (1 - fractY) * fractZ * self.noise[z1,y2,x1]
value += (1 - fractX) * fractY * fractZ * self.noise[z1,y1,x2]
value += (1 - fractX) * (1 - fractY) * fractZ * self.noise[z1,y2,x2]
value += fractX * fractY * (1 - fractZ) * self.noise[z2,y1,x1]
value += fractX * (1 - fractY) * (1 - fractZ) * self.noise[z2,y2,x1]
value += (1 - fractX) * fractY * (1 - fractZ) * self.noise[z2,y1,x2]
value += (1 - fractX) * (1 - fractY) * (1 - fractZ) * self.noise[z2,y2,x2]
return value
def turbulence(self, x, y, z, size):
value = 0.0
initialSize = size
while size >= 1.0:
value += self.smooth(x / size, y / size, z / size) * size
size /= 2.0
return(128.0 * value / initialSize);
class Tree:
def __init__(self, data=np.array([0,0]), nrOfBranches=2):
self.data = data
self.brancheCounter = 0
self.nrOfBranches = nrOfBranches
self.branches = []
def add(self, data=[0,0], nrOfBranches=2):
if (self.brancheCounter<self.nrOfBranches): # there is room
self.branches.append(Tree(data, nrOfBranches))
self.brancheCounter += 1
else:
print("Error: no more branches left")
def getCurrBranche(self):
if (self.brancheCounter>0):
return self.branches[self.brancheCounter-1]
else:
print("Error: no branches")
# def printPath(self, path): # this doesn't work. path keeps appending, although in different recursion level...
# path.append(self.data)
# print(self.data)
# if not self.branches: # this is a leaf
# print(np.asarray(path))
# else:
# for i in range(self.brancheCounter):
# self.branches[i].plotPath(path)
def printPaths(self, root):
if not root.branches: # this is a leaf
return [str(root.data)] # [root.data] #
else:
full_subtree = []
for i in range(root.brancheCounter):
full_subtree += self.printPaths(root.branches[i])
list1 = []
for leaf in full_subtree: # middle part of the comprehension
## list1.append([root.data, leaf])
list1.append(str(root.data) + ',' + leaf) # the left part
return list1
def BezierPlot(self, axis):
brancheStr = self.printPaths(self)
for i in range(len(brancheStr)): # parse the list of tree path strings
nodeStr = brancheStr[i].split(",")
brancheLength = len(nodeStr)
points = np.empty((brancheLength,2))
for j in range(brancheLength):
points[j,] = np.fromstring(nodeStr[j].translate({ord(ch): None for ch in '[]'}), dtype=float, sep=' ')
nodes = np.asfortranarray(points.transpose())
degree = nodes.shape[-1] - 1
curve = bezier.Curve(nodes, degree=degree)
greyVal = np.random.rand()
curve.plot(num_pts=25, ax=axis, color= (greyVal,greyVal,greyVal))
def traversePlot(self, axis):
for i in range(self.brancheCounter):
axis.plot((self.data[0],self.branches[i].data[0]), (self.data[1],self.branches[i].data[1]), color=(0,0,0), linewidth=1)
self.branches[i].traversePlot(axis)
def getDepth(self):
depth=0
node=self
while node.brancheCounter>0:
depth += 1
node = node.branches[0]
return depth
class WorkerThread(QThread):
sigMsg = pyqtSignal(str) # message to be shown to user
treeReady = pyqtSignal(Tree)
thread_name = "worker"
def __init__(self, maxRecursions=1, angle=.25, shrink=1.3, nrOfBranches=2, noiseToroidRadius=20, noiseToroidPhaseInc=0.05):
super().__init__()
self.maxRecursions = maxRecursions
self.angle = angle
self.shrink = shrink
self.stemLength = 2
self.nrOfBranches = nrOfBranches
self.noiseToroidRadius = noiseToroidRadius
self.noiseToroidPhase = 0
self.noiseToroidPhaseInc = noiseToroidPhaseInc
self.noise = Noise3D(2*self.noiseToroidRadius,2*self.noiseToroidRadius,2*self.noiseToroidRadius)
def run(self):
try:
# toroid through noise space
self.x = 0.25*self.noiseToroidRadius*(1+np.cos(2*np.pi*self.noiseToroidPhase/self.noiseToroidRadius))*(1+np.cos(2*np.pi*self.noiseToroidPhase/self.noiseToroidRadius))
self.y = 0.25*self.noiseToroidRadius*(1+np.sin(2*np.pi*self.noiseToroidPhase/self.noiseToroidRadius))*(1+np.sin(2*np.pi*self.noiseToroidPhase/self.noiseToroidRadius))
self.z = 0.5*self.noiseToroidRadius*(1+np.sin(2*np.pi*self.noiseToroidPhase/self.noiseToroidRadius))
tree = Tree(nrOfBranches=self.nrOfBranches)
self.recursion(tree,[0,1],self.stemLength,0)
self.treeReady.emit(tree)
#### self.sigMsg.emit("Image generated by " + self.thread_name)
self.noiseToroidPhase += self.noiseToroidPhaseInc
except Exception as err:
self.sigMsg.emit(self.thread_name + ": Error, " + str(err))
def recursion(self, tree, currGrad, size, n=0):
## noiseValue = self.noise.turbulence(self.x+n, self.y+n, self.z, 2)
## size *= (noiseValue/200)
## newNode = np.add(tree.data, np.multiply(-size,currGrad))
## tree.add(newNode, tree.nrOfBranches)
newNode = np.add(tree.data, np.multiply(size,currGrad))
tree.add(newNode, tree.nrOfBranches)
if (n < self.maxRecursions):
for i in range(tree.nrOfBranches):
noiseValue = self.noise.turbulence(self.x+n+i, self.y+n+i, self.z, 2)
angle = self.angle + noiseValue/1000
angle *= (i+1)*(-1)**i
c, s = np.cos(angle), np.sin(angle)
R = np.array(((c,-s), (s, c))) # rotation matrix
newGrad = np.dot(R, currGrad)
self.recursion(tree.getCurrBranche(), newGrad, size/self.shrink, n+1)
@pyqtSlot(float)
def setAngle(self, n):
self.angle = n
self.start()
@pyqtSlot(float)
def setShrink(self, n):
self.shrink = n
self.start()
@pyqtSlot(int)
def setNrOfBranches(self, n):
self.nrOfBranches = n
self.start()
@pyqtSlot(int)
def setMaxRecursions(self, n):
self.maxRecursions = n
self.start()
@pyqtSlot(float)
def setNoiseToroidPhaseInc(self, n):
self.noiseToroidPhaseInc = n
self.start()
def getAngle(self):
return self.angle
def getShrink(self):
return self.shrink
def getMaxRecursions(self):
return self.maxRecursions
def getNoiseToroidPhaseInc(self):
return self.noiseToroidPhaseInc
class LogWindow(QWidget):
def __init__(self):
super().__init__()
self.setWindowTitle("Log")
self.move(100,100)
layout = QVBoxLayout()
self.setLayout(layout)
self.resize(400, 800)
self.log = QTextEdit()
layout.addWidget(self.log)
@pyqtSlot(str)
def append(self, s):
self.log.append(s)
class MainWindow(QWidget):
closing = pyqtSignal()
def __init__(self, maxRecursions=1, nrOfBranches=2, angle=.5, shrink=1.2, noiseToroidPhaseInc=0.1):
super().__init__()
self.image = None
self.beta = 0.0
self.maxRecursions = maxRecursions
self.nrOfBranches = nrOfBranches
self.angle = angle
self.shrink = shrink
self.noiseToroidPhaseInc = noiseToroidPhaseInc
self.initUI()
def initUI(self):
self.setWindowTitle('Recursion tree')
self.move(400,100)
# Figure
self.figure = plt.figure(figsize=(20,20)) # a figure instance to plot on
self.figure.patch.set_alpha(0.5) # transparent background
# this is the Canvas Widget that displays the `figure`
# it takes the `figure` instance as a parameter to __init__
self.canvas = FigureCanvas(self.figure)
# Button
self.saveButton = QPushButton("Save")
self.saveButton.clicked.connect(self.save)
# Spinboxes
self.maxRecursionsSpinBox = QSpinBox(self)
self.maxRecursionsSpinBoxTitle = QLabel("Max recursions")
self.maxRecursionsSpinBox.setMinimum(1)
self.maxRecursionsSpinBox.setMaximum(999)
self.maxRecursionsSpinBox.setValue(self.maxRecursions)
self.nrOfBranchesSpinBox = QSpinBox(self)
self.nrOfBranchesSpinBoxTitle = QLabel("Nr of branches")
self.nrOfBranchesSpinBox.setMinimum(1)
self.nrOfBranchesSpinBox.setMaximum(9)
self.nrOfBranchesSpinBox.setValue(self.nrOfBranches)
self.shrinkSpinBox = QDoubleSpinBox(self)
self.shrinkSpinBoxTitle = QLabel("Shrink")
self.shrinkSpinBox.setSingleStep(0.01)
self.shrinkSpinBox.setMinimum(0)
self.shrinkSpinBox.setMaximum(5)
self.shrinkSpinBox.setValue(self.shrink)
self.angleSpinBox = QDoubleSpinBox(self)
self.angleSpinBoxTitle = QLabel("Angle")
self.angleSpinBox.setSingleStep(0.01)
self.angleSpinBox.setMinimum(0)
self.angleSpinBox.setMaximum(5)
self.angleSpinBox.setValue(self.angle)
self.betaSpinBox = QDoubleSpinBox(self)
self.betaSpinBoxTitle = QLabel("Beta")
self.betaSpinBox.setSingleStep(0.01)
self.betaSpinBox.setMinimum(0.0)
self.betaSpinBox.setMaximum(1.0)
self.betaSpinBox.setValue(self.beta)
self.betaSpinBox.valueChanged.connect(self.setBeta)
self.noiseToroidPhaseIncSpinBox = QDoubleSpinBox(self)
self.noiseToroidPhaseIncSpinBoxTitle = QLabel("noiseToroidPhaseInc")
self.noiseToroidPhaseIncSpinBox.setSingleStep(0.01)
self.noiseToroidPhaseIncSpinBox.setMinimum(0.0)
self.noiseToroidPhaseIncSpinBox.setMaximum(1.0)
self.noiseToroidPhaseIncSpinBox.setValue(self.noiseToroidPhaseInc)
# Compose layout grid
keyWidgets = [self.nrOfBranchesSpinBoxTitle, self.maxRecursionsSpinBoxTitle, self.angleSpinBoxTitle, self.shrinkSpinBoxTitle,
self.betaSpinBoxTitle, self.noiseToroidPhaseIncSpinBoxTitle, self.saveButton]
valueWidgets = [self.nrOfBranchesSpinBox, self.maxRecursionsSpinBox, self.angleSpinBox, self.shrinkSpinBox,
self.betaSpinBox, self.noiseToroidPhaseIncSpinBox, None]
widgetLayout = QGridLayout()
for index, widget in enumerate(keyWidgets):
if widget is not None:
widgetLayout.addWidget(widget, index, 0, Qt.AlignLeft)
for index, widget in enumerate(valueWidgets):
if widget is not None:
widgetLayout.addWidget(widget, index, 1, Qt.AlignLeft)
widgetLayout.setSpacing(10)
widgetLayout.addItem(QSpacerItem(0, 0, QSizePolicy.Minimum,QSizePolicy.Expanding)) # variable space
layout = QHBoxLayout()
layout.addLayout(widgetLayout, Qt.AlignTop|Qt.AlignCenter)
layout.addWidget(self.canvas, Qt.AlignTop|Qt.AlignCenter)
layout.setSpacing(10)
self.setLayout(layout)
def save(self):
plt.savefig('RecursionTreeBezier_'+ str(current_milli_time()) + '.svg')
@pyqtSlot(Tree)
def onTreeReady(self, tree=None):
self.figure.clear()
ax = self.figure.add_subplot(111)
# tree.traversePlot(ax)
tree.BezierPlot(ax)
plt.axis('off')
## plt.xlim(-6, 6)
## plt.ylim(-2, 8)
ax.grid(False)
self.canvas.draw() # refresh canvas
@pyqtSlot(float)
def setBeta(self, n):
self.beta = n
def closeEvent(self, event: QCloseEvent):
self.closing.emit()
event.accept()
if __name__ == '__main__':
app = QApplication(sys.argv)
# Instantiate objects
worker = WorkerThread(noiseToroidRadius=10)
mainWindow = MainWindow(maxRecursions=worker.getMaxRecursions(),
angle=worker.getAngle(),
shrink=worker.getShrink())
logWindow = LogWindow()
# Connect signals and slots
worker.treeReady.connect(mainWindow.onTreeReady) # Post worker's result to main Window
worker.sigMsg.connect(logWindow.append) # Log worker messages
mainWindow.closing.connect(worker.quit) # Quit worker thread
mainWindow.closing.connect(logWindow.close) # Close log window
## mainWindow.closing.connect(app.exit) # Close app
mainWindow.nrOfBranchesSpinBox.valueChanged.connect(worker.setNrOfBranches)
mainWindow.maxRecursionsSpinBox.valueChanged.connect(worker.setMaxRecursions)
mainWindow.shrinkSpinBox.valueChanged.connect(worker.setShrink)
mainWindow.angleSpinBox.valueChanged.connect(worker.setAngle)
## mainWindow.noiseToroidPhaseIncSpinBox.valueChanged.connect(worker.setNoiseToroidPhaseInc)
# Start the show
worker.start()
logWindow.show()
mainWindow.show()
# sys.exit(app.exec_())
app.exec_()