Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactoring / rewriting #11

Open
wants to merge 71 commits into
base: main
Choose a base branch
from
Open
Changes from 1 commit
Commits
Show all changes
71 commits
Select commit Hold shift + click to select a range
05a5844
Merge branch 'main' of https://github.com/Emmanuel-R8/causticsEnginee…
AlbaIntelligence Aug 31, 2021
4fc5fcc
Merge branch 'main' of https://github.com/Emmanuel-R8/causticsEnginee…
AlbaIntelligence Aug 31, 2021
3d9de25
Refactorin ongoing. Snapshot
AlbaIntelligence Sep 1, 2021
c56c6f1
Refactorin ongoing. Snapshot
AlbaIntelligence Sep 1, 2021
54716c3
Merge branch 'main' of https://github.com/Emmanuel-R8/causticsEnginee…
AlbaIntelligence Sep 1, 2021
d975aaa
Refactorig. Snapshot 2
Emmanuel-R8 Sep 1, 2021
624fbef
Refactorig. Snapshot 3
Emmanuel-R8 Sep 1, 2021
311cd7a
Refactorig. Snapshot 4
Emmanuel-R8 Sep 1, 2021
9e12ce9
Refactorig. Snapshot 5
Emmanuel-R8 Sep 2, 2021
8adb7c5
Refactoring. Snapshot 5
Emmanuel-R8 Sep 3, 2021
c3477a4
Refactoring. Snapshot 6
Emmanuel-R8 Sep 3, 2021
ce67002
Refactoring. Snapshot 7
Emmanuel-R8 Sep 3, 2021
24f47f5
Refactoring. Snapshot 7
Emmanuel-R8 Sep 5, 2021
c566148
Refactoring. Snapshot 8
Emmanuel-R8 Sep 5, 2021
f3d0e84
Refactoring. Snapshot 9
Emmanuel-R8 Sep 6, 2021
00e9612
Minor
AlbaIntelligence Sep 6, 2021
ffd9a2b
Merge branch 'main' of https://github.com/Emmanuel-R8/causticsEnginee…
AlbaIntelligence Sep 6, 2021
7884810
Refactoring ongoing. Snapshot 10
AlbaIntelligence Sep 6, 2021
c97a317
gitignore
Emmanuel-R8 Sep 7, 2021
ddb9137
Refactoring ongoing. Snapshot 11
AlbaIntelligence Sep 7, 2021
34a17e2
Refactoring ongoing. Snapshot 12
AlbaIntelligence Sep 8, 2021
5802b2a
Refactoring ongoing. Snapshot 12
AlbaIntelligence Sep 9, 2021
80409c4
Refactoring. Snapshot 13
Emmanuel-R8 Sep 9, 2021
a85c048
Refactoring. Snapshot 14
Emmanuel-R8 Sep 10, 2021
6392ad5
Refactoring. Snapshot 15
Emmanuel-R8 Sep 10, 2021
5c835c4
Refactoring - Looks like working
Emmanuel-R8 Sep 11, 2021
4332b80
Refactoring - Looks like working
Emmanuel-R8 Sep 11, 2021
8a6bdae
Merge branch 'main' into main
Emmanuel-R8 Sep 11, 2021
fd94975
Minor
Emmanuel-R8 Sep 12, 2021
7c5955e
Merge branch 'main' of https://github.com/Emmanuel-R8/causticsEnginee…
Emmanuel-R8 Sep 12, 2021
4c28798
Plotting bugs. Change of optimisation halting conditions.
Emmanuel-R8 Sep 12, 2021
4a1a064
Plotting bugs. Change of optimisation halting conditions. Corrected b…
Emmanuel-R8 Sep 13, 2021
e0ad5e7
Halting criteria changes. Snapshot
Emmanuel-R8 Sep 13, 2021
e76e0db
Merge branch 'main' of https://github.com/Emmanuel-R8/causticsEnginee…
AlbaIntelligence Sep 13, 2021
ac3c1fd
Tracking area conservation bug
AlbaIntelligence Sep 13, 2021
8abc24c
Tracking area convergence bug
AlbaIntelligence Sep 14, 2021
891c1f5
Move divergence function to maths utilities.
Emmanuel-R8 Sep 14, 2021
5e4dbbc
Minor.
Emmanuel-R8 Sep 15, 2021
8be5b20
Tracking converging bug.
Emmanuel-R8 Sep 16, 2021
ca33de3
Tracking area convergence bug
AlbaIntelligence Sep 16, 2021
cadba11
Tracking convergence bug
AlbaIntelligence Sep 16, 2021
76baa66
Tracking convergence bug
AlbaIntelligence Sep 17, 2021
12c93ce
Tracking convergence bug
AlbaIntelligence Sep 17, 2021
29e0c18
Tracking convergence bug
AlbaIntelligence Sep 19, 2021
59853dd
Tracking convergence bug
AlbaIntelligence Sep 19, 2021
6939ec0
Tracking convergence bug
AlbaIntelligence Sep 19, 2021
250e000
Tracking convergence bug
AlbaIntelligence Sep 19, 2021
883cda2
Tracking convergence bug
AlbaIntelligence Sep 19, 2021
034c4ec
Tracking convergence bug
AlbaIntelligence Sep 20, 2021
3d4dd72
Tracking area convergence bug
AlbaIntelligence Sep 20, 2021
4fa88d8
Tracking area convergence bug
AlbaIntelligence Sep 21, 2021
9473c94
Tracking area convergence bug
AlbaIntelligence Sep 25, 2021
4727036
Minor
Emmanuel-R8 Sep 25, 2021
14d3639
Tracking converging bug.
Emmanuel-R8 Sep 25, 2021
be9d1b5
Merge branch 'bisect' of https://github.com/Emmanuel-R8/causticsEngin…
AlbaIntelligence Sep 25, 2021
d7527b5
Tracking area convergence bug
AlbaIntelligence Sep 25, 2021
6a1b210
Tracking area convergence bug
AlbaIntelligence Sep 25, 2021
4f9de3c
Tracking area convergence bug
AlbaIntelligence Sep 25, 2021
fd50f9e
Tracking area convergence bug
AlbaIntelligence Sep 25, 2021
7bf9231
Tracking area convergence bug
AlbaIntelligence Sep 25, 2021
860904b
Minor.
Emmanuel-R8 Sep 26, 2021
f06cfea
Minor.
Emmanuel-R8 Sep 26, 2021
abf06db
Done
Emmanuel-R8 Sep 26, 2021
15179af
Clean up
Emmanuel-R8 Sep 26, 2021
3fbdc84
Merge pull request #2 from Emmanuel-R8/bisect
Emmanuel-R8 Sep 26, 2021
a77a436
Merge branch 'main' of https://github.com/Emmanuel-R8/causticsEnginee…
Emmanuel-R8 Sep 26, 2021
2523dcd
Merge branch 'triangular_targets' of https://github.com/Emmanuel-R8/c…
Emmanuel-R8 Sep 26, 2021
5aa5da2
Rebase
Emmanuel-R8 Sep 26, 2021
485873b
Minor
Emmanuel-R8 Sep 30, 2021
24ce62b
Documenter signatures
Emmanuel-R8 Oct 3, 2021
47e0a42
Change run.jl to use a temporary project
Emmanuel-R8 Sep 12, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Tracking area convergence bug
AlbaIntelligence committed Sep 25, 2021
commit 6a1b210b5b44935346355d78f533265e99326afe
90 changes: 65 additions & 25 deletions src/create_mesh.jl
Original file line number Diff line number Diff line change
@@ -21,50 +21,62 @@ function engineer_caustics(source_image)
# imageBW is normalised to have 1 unit per pixel on average.
imageBW /= average(imageBW)

marginal_change = nothing
max_update = Inf
marginal_change_top = marginal_change_bot = nothing
max_update_top = marginal_change_top = Inf
counter = 0
while (abs(max_update) > 1e-6 && counter < 10_000)
while (abs(max_update_top+marginal_change_bot) > 1e-6 && counter < 10_000)
counter += 1

print(
"""
================================================================================================================
STARTING ITERATION $(counter):
starting ϕ field = $(field_summary(mesh.corners.ϕ))
starting ϕ TOP field = $(field_summary(mesh.corners.ϕ_top))
starting ϕ BOT field = $(field_summary(mesh.corners.ϕ_bot))

""",
)

ϕ = mesh.corners.ϕ
ε, max_update = solve_velocity_potential!(mesh, imageBW, "it$(counter)")
ϕ_top = mesh.corners.ϕ_top
ε_top, max_update_top = solve_velocity_potential!(mesh.corners.ϕ_top, imageBW/2., "it$(counter)_top")
marginal_change_top = mesh.corners.ϕ_top - ϕ_top

marginal_change = mesh.corners.ϕ - ϕ
ϕ_bot = mesh.corners.ϕ_bot
ε_bot, max_update_bot = solve_velocity_potential!(mesh.corners.ϕ_bot, imageBW/2., "it$(counter)_bot")
marginal_change_bot = mesh.corners.ϕ_bot - ϕ_bot

print(
"""

RESULT AT ITERATION $(counter):
Luminosity error = $(field_summary(ε))
Vertical move max update = $(max_update)
Marginal change = $(field_summary(marginal_change))
RESULT AT ITERATION $(counter):
TOP
Luminosity error = $(field_summary(ε_top))
Vertical move max update = $(max_update_top)
Marginal change = $(field_summary(marginal_change_top))
end ϕ field = $(field_summary(mesh.corners.ϕ_top))

end ϕ field = $(field_summary(mesh.corners.ϕ))
================================================================================================================
BOT
end ϕ field = $(field_summary(mesh.corners.ϕ_bot))
Luminosity error = $(field_summary(ε_bot))
Vertical move max update = $(max_update_bot)
Marginal change = $(field_summary(marginal_change_bot))
end ϕ field = $(field_summary(mesh.corners.ϕ_bot))

================================================================================================================

""",
)
plot_as_quiver(mesh, n_steps = 50, scale = height / 10, max_length = height / 20)
average_absolute(marginal_change) < 1e-3 && break
plot_as_quiver(mesh.corners.ϕ_top, n_steps = 50, scale = height / 10, max_length = height / 20)
average_absolute(marginal_change_top + marginal_change_bot) < 1e-4 && break
end

println("\nSTARTING HORIZONTAL ITERATION ---")

max_update = solve_height_potential(mesh, imageBW; f = Focal_Length)
println("\t Horizontal max update = $(max_update)")
println("\t Horizontal max update TOP = $(max_update_top) / BOT = $(max_update_bot)")

# Move the around a nil average.
mesh.corners.ϕ .-= average(mesh.corners.ϕ)
mesh.corners.ϕ_top .-= average(mesh.corners.ϕ_top)
mesh.corners.ϕ_bot .-= average(mesh.corners.ϕ_bot)

return mesh, imageBW
end
@@ -84,29 +96,57 @@ function solve_velocity_potential!(mesh, image, prefix)
# Get the area of each individual pixel as stretch/shrunk on the lens. Area = energy.
# _FENCES_SIZED_
# Illumination only depends on the position of the corners, not their heights. ϕ is not relevant.
lens_pixels_area = get_lens_pixels_area(mesh)
lens_pixels_area_top, lens_pixels_area_bot = get_lens_pixels_area(mesh)

# Positive error => the triangle needs to shrink (less light). Enforce nil average error.
ε = zeros(Float64, height + 1, width + 1)
ε[1:end-1, 1:end-1] = Float64.(lens_pixels_area - image)
ε .-= average(ε)
ε_top = zeros(Float64, height, width)
ε_bot = zeros(Float64, height, width)

# Each triangle should have the luminosity of half a pixel
ε_top[1:end-1, 1:end-1] = Float64.(lens_pixels_area_top - image/2.)
ε_top .-= average(ε_top)

ε_bot[1:end-1, 1:end-1] = Float64.(lens_pixels_area_bot - image/2.)
ε_bot .-= average(ε_bot)

println("""
solve_velocity_potential! before loop:
$(field_summary(lens_pixels_area, "Pixel area"))
$(field_summary(ε, "luminosity error"))
$(field_summary(lens_pixels_area_top, "Pixel area"))
$(field_summary(ε_top, "luminosity error"))
$(field_summary(lens_pixels_area_bot, "Pixel area"))
$(field_summary(ε_bot, "luminosity error"))
""")

# Save the loss image as a png.
save_plot_scalar_field!(ε, "error_$(prefix)")
save_plot_scalar_field!(ε, "error_ε_$(prefix)")
save_plot_scalar_field!(ε_top, "error_εtop_$(prefix)")
save_plot_scalar_field!(ε_bot, "error_εbot_$(prefix)")

# Save the generated image as a png.
lens_pixels_area = lens_pixels_area_top + lens_pixels_area_bot
save(
"./examples/img_noluminosityratio_$(prefix).png",
Gray.(clamp.(lens_pixels_area, 0.0, 1.0)),
)

luminosity_ratio = sum(image) / sum(lens_pixels_area)
save(
"./examples/img_$(prefix).png",
Gray.(clamp.(lens_pixels_area * luminosity_ratio, 0.0, 1.0)),
)

luminosity_ratio = sum(image/2.) / sum(lens_pixels_area_top)
save(
"./examples/img_$(prefix).png",
Gray.(clamp.(lens_pixels_area_top * luminosity_ratio, 0.0, 1.0)),
)

luminosity_ratio = sum(image/2.) / sum(lens_pixels_area_bot)
save(
"./examples/img_$(prefix).png",
Gray.(clamp.(lens_pixels_area * lens_pixels_area_bot, 0.0, 1.0)),
)

# Start with a clean, flat potential field.
mesh.corners.ϕ = zeros(Float64, height + 1, width + 1)
ϕ_b4 = copy(mesh.corners.ϕ)
35 changes: 24 additions & 11 deletions src/utilities.jl
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
"""
$(TYPEDEF)

Coordinates as row (vertical), columns (horizontal). Represent the data of each topleft corner of a pixel.
Coordinates as row (vertical), columns (horizontal). Represent the data of each top corner of a pixel.
Topleft for top triangle, bottomleft for bottom triangle
vr, vc: velocity vector of that corner.
Warning: posts/fences: One more corner than number of pixels.
"""
@@ -95,7 +96,8 @@ mutable struct FieldVertex3D
vc::AbstractMatrix{Float64}

# Velocity potential at each corner
ϕ::AbstractMatrix{Float64}
ϕ_top::AbstractMatrix{Float64}
ϕ_bot::AbstractMatrix{Float64}

rows_numbers::AbstractMatrix{Float64}
cols_numbers::AbstractMatrix{Float64}
@@ -112,9 +114,10 @@ function FieldVertex3D(height, width)
mvr = zeros(Float64, height + 1, width + 1)
mvc = zeros(Float64, height + 1, width + 1)

mϕ = zeros(Float64, height + 1, width + 1)
mϕ_top = zeros(Float64, height , width )
mϕ_bot = zeros(Float64, height , width )

fv = FieldVertex3D((height, width), mr, mc, mvr, mvc, , rows_numbers, cols_numbers)
fv = FieldVertex3D((height, width), mr, mc, mvr, mvc, mϕ_top, mϕ_bot, rows_numbers, cols_numbers)
reset_border_values!(fv)
return fv
end
@@ -152,13 +155,14 @@ $(SIGNATURES)
function Vertex3D(fv::FieldVertex3D, row, col)
height, width = size(fv)

if (1 <= row <= height + 1) && (1 <= col <= width + 1)
if (1 <= row <= height ) && (1 <= col <= width)
Vertex3D(
fv.r[row, col],
fv.c[row, col],
fv.vr[row, col],
fv.vc[row, col],
fv.ϕ[row, col],
fv.ϕ_top[min(height, row), col(width, col)],
fv.ϕ_bot[min(height, row), col(width, col)],
)
else
missing
@@ -336,10 +340,8 @@ has been shifted and flexed around.
function get_lens_pixels_area(mesh::FaceMesh)
height, width = size(mesh)

top_tri_area =
area.([triangle3D(mesh, row, col, :bottom) for row ∈ 1:height, col ∈ 1:width])
bot_tri_area =
area.([triangle3D(mesh, row, col, :bottom) for row ∈ 1:height, col ∈ 1:width])
top_tri_area = get_lens_pixels_area(mesh, :top)
bot_tri_area =get_lens_pixels_area(mesh, :bottom)

total_area = top_tri_area + bot_tri_area

@@ -348,10 +350,21 @@ function get_lens_pixels_area(mesh::FaceMesh)

@assert abs(sum(luminosity_pixels) - height * width) < 1.0 "Total lens luminosity is incorrect"

return luminosity_pixels
# return luminosity_pixels
return top_tri_area, bot_tri_area
end


function get_lens_pixels_area(mesh::FaceMesh, side::Union{:top, :bottom})
height, width = size(mesh)

# return luminosity_pixels
return area.([triangle3D(mesh, row, col, side) for row ∈ 1:height, col ∈ 1:width])
end




"""
$(SIGNATURES)