Skip to content

Commit

Permalink
General refactor: Important performance improvements (#2)
Browse files Browse the repository at this point in the history
* Update Color.jl to work with Base functions

* Add comment in PerlinBroadcasting.jl

* Rename CustomExpr to GeneticExpr, refactor the internal

* Refactor custom math functions to work with new internals

* Refactor ExprEvaluation.jl code, now much more performant

* Refactor render functions to work with new internals

* Update Genetic.jl, change CustomExpr to GeneticExpr

* Refactor DynamicalSystems.jl with new internals, enhance code

* Update with new exports

* Add meshgrid function to function utils

* Add WIP code in ExprGenerators.jl

* Update tests with new internals

* Replace trunc with round to fix small precision problems

* Add adjust_brightness function

* Minor fixes in Renderer.jl

* Minor fixes in DynamicalSystems.jl

* Minor fixes in Colors.jl

* Minor fix in Colors.jl

* Add fixes in ExprEvaluation.jl

* Fix generate_image_vectorized

* Add f function

* Extend Base simple functions for proper Expr evaluation

* Optimize core math functions by using @inbounds and removing merge

* Update color functions

* Update DynamicalSystems.jl

* update Expr functions

* Update boolean functions

* Update UI.jl

* Update Project.toml

* Add some garbage examples -- to be cleaned

* Update README.md
  • Loading branch information
jofrevalles authored Aug 25, 2024
1 parent c361266 commit c2fbf77
Show file tree
Hide file tree
Showing 65 changed files with 8,006 additions and 690 deletions.
10 changes: 9 additions & 1 deletion Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,19 @@ authors = ["jofrevalles <jofrevalles99@gmail.com>"]
version = "0.1.0"

[deps]
BenchmarkTools = "6e4b80f9-dd63-53aa-95a3-0cdb28fa8baf"
CSV = "336ed68f-0bac-5ca0-87d4-7b16caf5d00b"
CoherentNoise = "3547b4d8-3bdb-4c10-a099-d01a31e852e0"
ColorSchemes = "35d6a980-a343-548e-a6ea-1d62b119f2f4"
Colors = "5ae59095-9a9b-59fe-a467-6f913c188581"
DataFrames = "a93c6f00-e57d-5684-b7b6-d8193f3e46c0"
ExprTools = "e2ba6199-217a-4e67-a87a-7c52f15ade04"
FileIO = "5789e2e9-d7fb-5bc7-8068-2c6fae9b9549"
ForwardDiff = "f6369f11-7733-5829-9624-2563aa707210"
ImageMagick = "6218d12a-5da1-5696-b52f-db25d2ecc6d1"
Images = "916415d5-f1e6-5110-898d-aaa5f9f070e0"
Luxor = "ae8d54c2-7ccd-5906-9d76-62fc9837b5bc"
Plots = "91a5bcdd-55d7-5caf-9e0b-520d859cae80"
ProgressMeter = "92933f4c-e287-5a05-a399-4b506db050ca"
Random = "9a3f8284-a2c9-5f02-9a11-845980a1fd5c"
StaticArrays = "90137ffa-7385-5640-81b9-e52037218182"
Statistics = "10745b16-79ce-11e8-11f9-7d13ad32a3b2"
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,12 @@ Here are some examples of textures generated with GeneticTextures.jl:
<img src="./images/examples2.png" alt="Examples" style="display: inline-block; margin: 0 5px;" />
</div>

## Benchmarks
We also benchmarked the performance of the different function backends available in GeneticTextures.jl.
<div style="text-align: center;">
<img src="combined_performance_analysis_compared_minimum.png" alt="Benchmarks" style="display: inline-block; margin: 0 5px;" />
</div>

## Acknowledgments

GeneticTextures.jl makes use of the following third-party packages:
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
15 changes: 15 additions & 0 deletions examples/animation10.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
using GeneticTextures
F_A0 = :(Complex(x, y))
F_dA = :(x_grad(A) + y_grad(A) + 0.1 * laplacian(A))


color_expr = :((a) -> RGB(abs(a.r), abs(a.g), abs(a.b)))
complex_expr = :((c) -> abs(c))

A = VariableDynamics(:A, F_A0, F_dA)
# B = VariableDynamics(:B, F_B0, F_dB)
# ds = DynamicalSystem([A,B])

ds = DynamicalSystem([A])
w = h = 64
animate_system_2(ds, w, h, 100.0, 0.2; color_expr, complex_expr)
15 changes: 15 additions & 0 deletions examples/animation11.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
using GeneticTextures
F_A0 = :(Complex(x, y))
F_dA = :((x_grad(A) + y_grad(A)) * A + 0.1 * laplacian(A))


color_expr = :((a) -> RGB(abs(a.r), abs(a.g), abs(a.b)))
complex_expr = :((c) -> abs(c))

A = VariableDynamics(:A, F_A0, F_dA)
# B = VariableDynamics(:B, F_B0, F_dB)
# ds = DynamicalSystem([A,B])

ds = DynamicalSystem([A])
w = h = 64
animate_system_2(ds, w, h, 100.0, 0.5; color_expr, complex_expr)
16 changes: 16 additions & 0 deletions examples/animation12.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
using GeneticTextures
F_A0 = :(Complex(x, y))
# F_dA = :(A^2 + Complex(0.355, 0.355))
F_dA = :(A^2 + Complex(0.14, 0.13))


color_expr = :((a) -> RGB(abs(a.r), abs(a.g), abs(a.b)))
complex_expr = :((c) -> real(c))

A = VariableDynamics(:A, F_A0, F_dA)
# B = VariableDynamics(:B, F_B0, F_dB)
# ds = DynamicalSystem([A,B])

ds = DynamicalSystem([A])
w = h = 512
animate_system_2(ds, w, h, 200.0, 0.04; color_expr, complex_expr)
21 changes: 21 additions & 0 deletions examples/animation13.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
using GeneticTextures
F_A0 = :(y)
F_dA = :(C)

F_B0 = :(1.0)
F_dB = :(x_grad(C))

F_C = :(1 - ifs(rand_scalar() > 0.97, 0.0, 1.0) * y)
F_dC = :(neighbor_ave(grad_direction(B * 0.25)))


color_expr = :((a, b, c) -> RGB(abs(a.r), abs(b.g), abs(c.b)))
complex_expr = :((c) -> real(c))

A = VariableDynamics(:A, F_A0, F_dA)
B = VariableDynamics(:B, F_B0, F_dB)
C = VariableDynamics(:C, F_C, F_dC)

ds = DynamicalSystem([A, B, C])
w = h = 32
animate_system_2(ds, w, h, 10.0, 0.01; color_expr, complex_expr)
19 changes: 19 additions & 0 deletions examples/animation14.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
using GeneticTextures

F_A0 = :(Complex(4*x, 4*y))
F_dA = :(A^2 + Complex(0.355, 0.355))
# F_dA = :(A^2 + Complex(0.14, 0.13))

F_B0 = :(0)
F_dB = :(ifs(abs(A) > 2, B + 0.1, B))


color_expr = :((a, b) -> RGB(abs(b.r), abs(b.g), abs(b.b)))
complex_expr = :((c) -> real(c))

A = VariableDynamics(:A, F_A0, F_dA)
B = VariableDynamics(:B, F_B0, F_dB)
ds = DynamicalSystem([A,B])

w = h = 512
animate_system_2(ds, w, h, 200.0, 0.02; color_expr, complex_expr)
40 changes: 40 additions & 0 deletions examples/animation15.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
using GeneticTextures
using Colors

# Define a colormap
colormap_ = colormap("RdBu", 100) # Choose any available colormap

# Color mapping function
function map_to_color(b, colormap)
idx = min(floor(Int, abs(b) * length(colormap)) + 1, length(colormap))
return colormap[idx]
end

F_A0 = :(Complex(4*x, 4*y))
# F_dA = :(A^2 + Complex(0.355, 0.355))
F_da = :(A^2 + Complex(0.74543, 0.11301))

F_B0 = :(0)
F_dB = :(ifs(abs(A) > 2, B + 0.1, B))

color_expr = :(begin
using ColorSchemes

# Access the viridis color scheme
color_scheme = ColorSchemes.viridis

# Function to map a value between 0 and 1 to a color in the viridis scheme
function map_to_color(value)
return get(color_scheme, value)
end

(a, b) -> map_to_color(b.r)
end)
complex_expr = :((c) -> real(c))

A = VariableDynamics(:A, F_A0, F_dA)
B = VariableDynamics(:B, F_B0, F_dB)
ds = DynamicalSystem([A,B])

w = h = 128
animate_system_2(ds, w, h, 200.0, 0.02; color_expr, complex_expr)
34 changes: 34 additions & 0 deletions examples/animation16.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
using GeneticTextures

F_A0 = :(Complex(4*x, 4*y))
# F_dA = :(A^2 + Complex(0.355, 0.355))
F_da = :(A^2 + Complex(0.74543, 0.11301))

F_B0 = :(0)
F_dB = :(ifs(abs(A) > 2, B + 1, B))

F_C0 = :(0) # C will be used as a counter
F_dC = :(C + 1)

color_expr = :(begin
using ColorSchemes

# Access the viridis color scheme
color_scheme = ColorSchemes.viridis

# Function to map a value between 0 and 1 to a color in the viridis scheme
function map_to_color(value)
return get(color_scheme, value)
end

(a, b, c) -> map_to_color(b.r/c.r)
end)
complex_expr = :((c) -> real(c))

A = VariableDynamics(:A, F_A0, F_dA)
B = VariableDynamics(:B, F_B0, F_dB)
C = VariableDynamics(:C, F_C0, F_dC)
ds = DynamicalSystem([A,B,C])

w = h = 128
animate_system_2(ds, w, h, 200.0, 0.3; color_expr, complex_expr)
51 changes: 51 additions & 0 deletions examples/animation17.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
using GeneticTextures

F_A0 = :(Complex(4*x, 4*y))
F_dA = :(A^2 + Complex(0.355, 0.355))
# F_dA = :(A^2 + Complex(0.74543, 0.11301))

F_B0 = :(0)
F_dB = :(ifs(abs(A) > 2, B + 0.1, B))

F_C0 = :(0) # C will be used as a counter
F_dC = :(C + 0.1)


# Define the color expression
color_expr = :(begin
using ColorSchemes
smooth_modifier(k, z) = abs(z) > 2 ? k - log2(log2(abs(z))) : k

angle_modifier(k, z) = abs(z) > 2 ? angle(z)/2 * pi : k
# Access the viridis color scheme
color_scheme = ColorSchemes.viridis

# Function to map a value between 0 and 1 to a color in the viridis scheme
function map_to_color(value)
return get(color_scheme, value)
end

(a, b, c) -> begin
# Calculate the modified iteration count
modified_k = smooth_modifier(c.r, a.r)

# Normalize using the counter C
normalized_k = modified_k / c.r

# Map to color
map_to_color(normalized_k)
end
end)

complex_expr = :((c) -> real(c))

A = VariableDynamics(:A, F_A0, F_dA)
B = VariableDynamics(:B, F_B0, F_dB)
C = VariableDynamics(:C, F_C0, F_dC)
ds = DynamicalSystem([A,B,C])

w = h = 1024
animate_system_2(ds, w, h, 2.0, 0.01;
color_expr,
complex_expr)

37 changes: 37 additions & 0 deletions examples/animation18.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
using GeneticTextures
F_A0 = :(Complex(4*x, y))
# F_dA = :(A^2 + Complex(0.355, 0.355))
F_dA = :(A^2 + Complex(0.74543, 0.11301))

F_B0 = :(0)
F_dB = :(ifs(abs(A) > 2, B + 1, B))

F_C0 = :(0) # C will be used as a counter
F_dC = :(C + 1)


# Define the value expression
value_expr = :(begin
smooth_modifier(k, z) = abs(z) > 2 ? k - log2(log2(abs(z))) : k

angle_modifier(k, z) = abs(z) > 2 ? angle(z)/2 * pi : k

(a, b, c) -> begin
# Calculate the modified iteration count
modified_k = angle_modifier(c.r, a.r)
end
end)

complex_expr = :((c) -> real(c))

A = VariableDynamics(:A, F_A0, F_dA)
B = VariableDynamics(:B, F_B0, F_dB)
C = VariableDynamics(:C, F_C0, F_dC)
ds = DynamicalSystem([A,B,C])

w = h = 32
GeneticTextures.animate_system_3(ds, w, h, 200.0, 0.01;
cmap = :viridis,
value_expr,
complex_expr)

39 changes: 39 additions & 0 deletions examples/animation19.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
using GeneticTextures

F_A0 = :(Complex(4*x, 4*y))
# F_dA = :(A^2 + Complex(0.355, 0.355))
# F_dA = :(A^2 + Complex(0.74543, 0.11301))
F_dA = :(min(A, 1.0) / A + laplacian(A) * max(A, Complex(0.74543,0.11301))^2)

F_B0 = :(00.0+0.0)
F_dB = :(ifs(abs(A) > 2, B + 1, B))

F_C0 = :(0.0+0.0) # C will be used as a counter
F_dC = :(C + 1)


# Define the value expression
value_expr = :(begin
smooth_modifier(k, z) = abs(z) > 2 ? k - log2(log2(abs(z))) : k

angle_modifier(k, z) = abs(z) > 2 ? angle(z)/2 * pi : k

(a, b, c) -> begin
# Calculate the modified iteration count
modified_k = smooth_modifier(c.r, a.r)
end
end)

complex_expr = :((c) -> abs(c))

A = VariableDynamics(:A, F_A0, F_dA)
B = VariableDynamics(:B, F_B0, F_dB)
C = VariableDynamics(:C, F_C0, F_dC)
ds = DynamicalSystem([A,B,C])

w = h = 1024
GeneticTextures.animate_system_3(ds, w, h, 200.0, 0.01;
cmap = :viridis,
value_expr,
complex_expr)

Loading

0 comments on commit c2fbf77

Please sign in to comment.