-
Notifications
You must be signed in to change notification settings - Fork 5
/
operators.pyx
466 lines (420 loc) · 14.2 KB
/
operators.pyx
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
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
# Copyright (c) 2018, Yaroslav Zotov, https://github.com/qiray/
# All rights reserved.
# This file is part of MathArtist.
# MathArtist is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
# MathArtist is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
# You should have received a copy of the GNU General Public License
# along with MathArtist. If not, see <https://www.gnu.org/licenses/>.
################################################################################
# This file uses code from Andrej Bauer's randomart project under
# following conditions:
# Copyright (c) 2010, Andrej Bauer, http://andrej.com/
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are met:
#
# * Redistributions of source code must retain the above copyright notice,
# this list of conditions and the following disclaimer.
#
# * Redistributions in binary form must reproduce the above copyright
# notice, this list of conditions and the following disclaimer in the
# documentation and/or other materials provided with the distribution.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
# DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
# SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
# CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
# OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
# cython: language_level=3
import random
import math
cimport libc.math as cmath
from common import (average, well, tent, parse_color, sin_curve, abs_sin,
color_binary, wave)
from palettes import palettes
# We next define classes that represent expression trees.
# Each object that reprents and expression should have an eval(self, x, y) method
# which computes the value of the expression at (x, y). The __init__ should
# accept the objects representing its subexpressions. The class definition
# should contain the arity attribute which tells how many subexpressions should
# be passed to the __init__ constructor. Classes with arity == 0 are called
# terminals, the others are called nonterminals.The __repr__ method is used to
# print each object as a string. The mindepth attribute shows depth of
# expression tree where it is allowed to use this object.
# Some operators are adopted from https://github.com/vshymanskyy/randomart
# Terminals:
class VariableX():
arity = 0
mindepth = 4
def __init__(self):
pass
def __repr__(self):
return "x"
def eval(self, x, y):
return (x, x, x)
class VariableY():
arity = 0
mindepth = 4
def __init__(self):
pass
def __repr__(self):
return "y"
def eval(self, x, y):
return (y, y, y)
class Random():
arity = 0
mindepth = 4
def __init__(self, r = None, g = None, b = None):
if r and g and b: #for parsing
self.c = (r, g, b)
return
self.c = (random.uniform(0, 1), random.uniform(0, 1), random.uniform(0, 1))
def __repr__(self):
return 'Random(%g,%g,%g)' % self.c
def eval(self, x, y):
return self.c
class Palette():
arity = 0
mindepth = 3
palette = palettes[0]
paletteIndex = 0
def __init__(self, r = None, g = None, b = None):
if r and g and b: #for parsing
self.c = (r, g, b)
return
self.hex = Palette.palette[Palette.paletteIndex]
Palette.paletteIndex += 1
if Palette.paletteIndex >= len(Palette.palette):
Palette.paletteIndex = 0
self.c = tuple([x/128.0 - 1.0 for x in parse_color(self.hex)])
def __repr__(self):
return "Palette(%g, %g, %g)" % self.c
def eval(self, x, y):
return self.c
@staticmethod
def randomPalette(): #set random palette
Palette.palette = random.choice(palettes)
Palette.paletteIndex = 0
class White():
arity = 0
mindepth = 4
def __init__(self, r = None, g = None, b = None): #unused arguments for parsing
self.c = (1, 1, 1)
def __repr__(self):
return 'White(%g, %g, %g)' % self.c
def eval(self, x, y):
return self.c
class Chess():
arity = 0
mindepth = 5
def __init__(self, wX=None, wY=None):
if wX and wY: #for parsing
self.wX = wX
self.wY = wY
return
self.wX = random.uniform(0.1, 1.0)
self.wY = random.uniform(0.1, 1.0)
def __repr__(self):
return "Chess(%g, %g)" % (self.wX, self.wY)
def eval(self, x, y):
isOdd = False
isOdd ^= int(cmath.floor(x/self.wX)) & 1
isOdd ^= int(cmath.floor(y/self.wY)) & 1
return (-1, -1, -1) if isOdd else (1, 1, 1)
class Fibonacci():
arity = 0
mindepth = 3
fib_array = [0, 1]
def __init__(self):
pass
def __repr__(self):
return "Fibonacci()"
def eval(self, x, y):
result = (self.fibonacci(len(Fibonacci.fib_array) + 1)%255)/255 - 128
return (result, result, result)
def fibonacci(self, n):
if n < 0:
return Fibonacci.fib_array[0]
elif n < len(Fibonacci.fib_array):
return Fibonacci.fib_array[n]
else:
max_limit = 200 if n > 200 else n + 1
for i in range(len(Fibonacci.fib_array), max_limit):
Fibonacci.fib_array.append(Fibonacci.fib_array[i - 1] + Fibonacci.fib_array[i - 2])
return Fibonacci.fib_array[max_limit - 1]
# Nonterminals:
class Well():
arity = 1
mindepth = 3
def __init__(self, e):
self.e = e
self.e_func = self.e.eval
def __repr__(self):
return 'Well(%s)' % self.e
def eval(self, x, y):
(r, g, b) = self.e_func(x, y)
return (well(r), well(g), well(b))
class Tent():
arity = 1
mindepth = 3
def __init__(self, e):
self.e = e
self.e_func = self.e.eval
def __repr__(self):
return 'Tent(%s)' % self.e
def eval(self, x, y):
(r, g, b) = self.e_func(x, y)
return (tent(r), tent(g), tent(b))
class Sin():
arity = 1
mindepth = 0
def __init__(self, e, phase = None, freq = None):
self.e = e
self.e_func = self.e.eval
if phase and freq: #for parsing
self.phase = phase
self.freq = freq
return
self.phase = random.uniform(0, math.pi)
self.freq = random.uniform(1.0, 6.0)
def __repr__(self):
return 'Sin(%s, %g, %g)' % (self.e, self.phase, self.freq)
def eval(self, x, y):
(r1, g1, b1) = self.e_func(x, y)
r2 = cmath.sin(self.phase + self.freq * r1)
g2 = cmath.sin(self.phase + self.freq * g1)
b2 = cmath.sin(self.phase + self.freq * b1)
return (r2, g2, b2)
class Not():
arity = 1
mindepth = 3
def __init__(self, e):
self.e = e
self.e_func = self.e.eval
def __repr__(self):
return "Not(%s)" % self.e
def eval(self, x, y):
(r, g, b) = self.e_func(x, y)
return (-r, -g, -b)
class SinCurve():
arity = 1
mindepth = 0
def __init__(self, e):
self.e = e
self.e_func = self.e.eval
def __repr__(self):
return 'SinCurve(%s)' % self.e
def eval(self, x, y):
(r, g, b) = self.e_func(x, y)
return (sin_curve(r), sin_curve(g), sin_curve(b))
class AbsSin():
arity = 1
mindepth = 3
def __init__(self, e):
self.e = e
self.e_func = self.e.eval
def __repr__(self):
return 'AbsSin(%s)' % self.e
def eval(self, x, y):
(r, g, b) = self.e_func(x, y)
return (abs_sin(r), abs_sin(g), abs_sin(b))
class Atan():
arity = 1
mindepth = 0
def __init__(self, e):
self.e = e
self.e_func = self.e.eval
def __repr__(self):
return 'Atan(%s)' % (self.e)
def eval(self, x, y):
(r, g, b) = self.e_func(x, y)
return (cmath.atan(r)*2/cmath.pi, cmath.atan(g)*2/cmath.pi, cmath.atan(b)*2/cmath.pi)
class Sum():
arity = 2
mindepth = 2
def __init__(self, e1, e2):
self.e1 = e1
self.e2 = e2
def __repr__(self):
return 'Sum(%s, %s)' % (self.e1, self.e2)
def eval(self, x, y):
return average(self.e1.eval(x, y), self.e2.eval(x, y))
class Product():
arity = 2
mindepth = 2
def __init__(self, e1, e2):
self.e1 = e1
self.e2 = e2
def __repr__(self):
return 'Product(%s, %s)' % (self.e1, self.e2)
def eval(self, x, y):
(r1, g1, b1) = self.e1.eval(x, y)
(r2, g2, b2) = self.e2.eval(x, y)
r3 = r1 * r2
g3 = g1 * g2
b3 = b1 * b2
return (r3, g3, b3)
class Mod():
arity = 2
mindepth = 3
def __init__(self, e1, e2):
self.e1 = e1
self.e2 = e2
def __repr__(self):
return 'Mod(%s, %s)' % (self.e1, self.e2)
def eval(self, x, y):
(r1, g1, b1) = self.e1.eval(x, y)
(r2, g2, b2) = self.e2.eval(x, y)
try:
r3 = r1 % r2
g3 = g1 % g2
b3 = b1 % b2
return (r3, g3, b3)
except:
return (0, 0, 0)
class And():
arity = 2
mindepth = 0
def __init__(self, e1, e2):
self.e1 = e1
self.e2 = e2
def __repr__(self):
return 'And(%s, %s)' % (self.e1, self.e2)
def eval(self, x, y):
return color_binary(self.e1.eval(x, y), self.e2.eval(x, y), lambda x1,x2 : x1 & x2)
class Or():
arity = 2
mindepth = 0
def __init__(self, e1, e2):
self.e1 = e1
self.e2 = e2
def __repr__(self):
return 'Or(%s, %s)' % (self.e1, self.e2)
def eval(self, x, y):
return color_binary(self.e1.eval(x, y), self.e2.eval(x, y), lambda x1,x2 : x1 | x2)
class Xor():
arity = 2
mindepth = 0
def __init__(self, e1, e2):
self.e1 = e1
self.e2 = e2
def __repr__(self):
return 'Xor(%s, %s)' % (self.e1, self.e2)
def eval(self, x, y):
return color_binary(self.e1.eval(x, y), self.e2.eval(x, y), lambda x1,x2 : x1 ^ x2)
class Wave():
arity = 2
mindepth = 0
def __init__(self, e1, e2):
self.e1 = e1
self.e2 = e2
def __repr__(self):
return 'Wave(%s, %s)' % (self.e1, self.e2)
def eval(self, x, y):
(r1, g1, b1) = self.e1.eval(x, y)
(r2, g2, b2) = self.e2.eval(x, y)
return (wave(r1, r2), wave(g1, g2), wave(b1, b2))
class Level():
arity = 3
mindepth = 0
def __init__(self, level, e1, e2, treshold = None):
self.treshold = treshold if treshold else random.uniform(-1.0, 1.0) #for parsing
self.level = level
self.e1 = e1
self.e2 = e2
def __repr__(self):
return 'Level(%s, %s, %s, %g)' % (self.level, self.e1, self.e2, self.treshold)
def eval(self, x, y):
(r1, g1, b1) = self.level.eval(x, y)
(r2, g2, b2) = self.e1.eval(x, y)
(r3, g3, b3) = self.e2.eval(x, y)
r4 = r2 if r1 < self.treshold else r3
g4 = g2 if g1 < self.treshold else g3
b4 = b2 if b1 < self.treshold else b3
return (r4, g4, b4)
class Mix():
arity = 3
mindepth = 0
def __init__(self, w, e1, e2):
self.w = w
self.e1 = e1
self.e2 = e2
self.w_func = self.w.eval
self.e1_func = self.e1.eval
self.e2_func = self.e2.eval
def __repr__(self):
return 'Mix(%s, %s, %s)' % (self.w, self.e1, self.e2)
def eval(self, x, y):
w = 0.5 * (self.w_func(x, y)[0] + 1.0)
c1 = self.e1_func(x, y)
c2 = self.e2_func(x, y)
return average(c1, c2, w)
class RGB():
arity = 3
mindepth = 4
def __init__(self, e1, e2, e3):
self.e1 = e1
self.e2 = e2
self.e3 = e3
self.e1_func = self.e1.eval
self.e2_func = self.e2.eval
self.e3_func = self.e3.eval
def __repr__(self):
return 'RGB(%s, %s, %s)' % (self.e1, self.e2, self.e3)
def eval(self, x, y):
(r, _, _) = self.e1_func(x, y)
(_, g, _) = self.e2_func(x, y)
(_, _, b) = self.e3_func(x, y)
return (r, g, b)
class Closest():
arity = 3
mindepth = 3
def __init__(self, target, e1, e2):
self.target = target
self.e1 = e1
self.e2 = e2
self.target_func = self.target.eval
self.e1_func = self.e1.eval
self.e2_func = self.e2.eval
def __repr__(self):
return 'Closest(%s, %s, %s)' % (self.target, self.e1, self.e2)
def eval(self, x, y):
(r1, g1, b1) = self.target_func(x, y)
(r2, g2, b2) = self.e1_func(x, y)
(r3, g3, b3) = self.e2_func(x, y)
#distances between colors:
d1 = math.sqrt((r2-r1)**2+(g2-g1)**2+(b2-b1)**2)
d2 = math.sqrt((r3-r1)**2+(g3-g1)**2+(b3-b1)**2)
return (r2, g2, b2) if d1 < d2 else (r3, g3, b3)
class Far():
arity = 3
mindepth = 3
def __init__(self, target, e1, e2):
self.target = target
self.e1 = e1
self.e2 = e2
self.target_func = self.target.eval
self.e1_func = self.e1.eval
self.e2_func = self.e2.eval
def __repr__(self):
return 'Far(%s, %s, %s)' % (self.target, self.e1, self.e2)
def eval(self, x, y):
(r1, g1, b1) = self.target_func(x, y)
(r2, g2, b2) = self.e1_func(x, y)
(r3, g3, b3) = self.e2_func(x, y)
#distances between colors:
d1 = math.sqrt((r2-r1)**2+(g2-g1)**2+(b2-b1)**2)
d2 = math.sqrt((r3-r1)**2+(g3-g1)**2+(b3-b1)**2)
return (r2, g2, b2) if d1 > d2 else (r3, g3, b3)