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

Units absorbance #4

Merged
merged 2 commits into from
Jun 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name = "SwissVAMyKnife"
uuid = "fafdf525-54c7-48ec-ae29-528bd4cccfab"
authors = ["roflmaostc <[email protected]>"]
version = "0.1.1"
version = "0.2.0"

[deps]
CUDA = "052768ef-5323-5732-b1bb-66c8b64840ba"
Expand Down
94 changes: 63 additions & 31 deletions examples/1_glass_vial_pattern_optimization.jl
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
### A Pluto.jl notebook ###
# v0.19.38
# v0.19.42

using Markdown
using InteractiveUtils
Expand Down Expand Up @@ -67,6 +67,12 @@ However, we add a trailing 1 z dimension.
sz = (256, 256, 1)

# ╔═╡ b2dae3ea-5a40-498f-a134-5a9a98e58de0
# ╠═╡ disabled = true
#=╠═╡
target = box(Float32, sz, (150, 110, 1), offset=(90, 160, 1)) .- box(Float32, sz, (80, 50, 1), offset=(90, 160, 1));
╠═╡ =#

# ╔═╡ 827f6f0a-12d9-4a72-8f8b-c3869a037590
target = box(Float32, sz, (150, 110, 1)) .- box(Float32, sz, (80, 50, 1));

# ╔═╡ 047023bf-96dd-4427-aa13-3fb2340af90e
Expand All @@ -86,7 +92,7 @@ Let's use the `LossThreshold` to achieve this:
"

# ╔═╡ ae3fdae9-7b3c-4391-8fff-530f7758942c
loss = LossThreshold(thresholds=(0.85, 0.95))
loss = LossThreshold(thresholds=(0.9, 0.98))

# ╔═╡ b58c9e1f-7ca6-4171-8004-2b84d33cf335
md"## Specify Geometry
Expand All @@ -99,13 +105,13 @@ As absorption we set $μ=nothing$, so we ignore absorption first!
"

# ╔═╡ 0dd200a7-a0f0-416e-ad69-7343e210a36e
angles = range(0, 2π, 400)
angles = range(0, 2π, 401)[begin:end-1]

# ╔═╡ 303a363f-f941-45b9-9194-62a2c411964a
μ = nothing

# ╔═╡ 6d130809-8b78-485c-bba1-fc958bc9d873
geometry = ParallelRayOptics(angles, μ)
geometry = ParallelRayOptics(angles=angles, μ=μ, DMD_diameter=10e-3)

# ╔═╡ cb8b9ccc-4085-4abe-bd71-5e70cc8decfa
md" As optimizer we use a gradient descent based variant"
Expand All @@ -120,6 +126,7 @@ Let's try to run the optimization: `togoc` moves the target onto the CUDA device
# ╔═╡ 07e97af6-f30b-4100-999c-c61b52cec76e
@mytime patterns, printed_intensity, optim_res = optimize_patterns(togoc(target), geometry, optimizer, loss)


# ╔═╡ c13abe2d-ff27-42e2-9534-cb1936e68236
optim_res

Expand Down Expand Up @@ -148,31 +155,35 @@ Printed intensity ----------------------------- Printed intensity after threshol
# ╔═╡ 5801f61a-79fa-4326-82dd-3901dd4d493a
[simshow(Array(printed_intensity[:, :, 1])) simshow(ones((sz[1], 5))) simshow(thresh .< Array(printed_intensity[:, :, 1])) simshow(ones((sz[1], 5))) simshow(target[:, :, 1])]

# ╔═╡ 605e1421-23dd-465d-b94d-3a649154f6f8
simshow(360 .< Array(backproject(patterns, angles; μ)[:, :, 1]))

# ╔═╡ b08e897d-c260-4a42-8065-989abbbcab4e
simshow(Array(backproject(reverse(patterns, dims=Tuple(())), .-angles; μ)[:, :, 1]))

# ╔═╡ 44ea2ea2-1e4f-44d3-aa02-68e96d6c7b39
size(patterns)

# ╔═╡ 92f5b987-13c7-463c-b150-71c51ec4aa72
simshow(Array(patterns)[:, :, 1], cmap=:turbo)

# ╔═╡ 6654e278-3d71-48a3-9382-7b8e486f4d72
sum(patterns ./ maximum(patterns)) / length(patterns)

# ╔═╡ 3fb2afdb-2ad2-4ce5-9ca3-e1c5c1d45970
histogram(Array(patterns[:]), yscale=:log10)

# ╔═╡ 11af6da4-5fa2-4e48-a1c8-32bc686b8239
md"# 4. Include Absorption of the Photo initiator
Let's add some absorption!

256 is the size of the object in pixels.
A $μ=2/(256)$ would mean that after propagation through the vial, the ray has only $I(x) = I_0 \cdot \exp(-2) ≈ I_0 \cdot 0.135$ of the initial intensity left.

And the printing is still possible!
Empirically, a factor of 3 might be possible to print. A higher $\mu$ means actually shorter printing times.
$\mu$ expects units of inverse meters.
Empirically, a factor of 3/10mm is possible to print.
This is equivalent that only $\exp(-3)$ of the light is left after the propagation through the vial (if the vial has a diameter of 10mm).
A higher $\mu$ means actually shorter printing times.
"

# ╔═╡ 2f51ec68-09c6-4475-81b9-ec252fba5df7
μ2 = 2 / (256)
μ2 = 3 / (10e-3)

# ╔═╡ 2e76c088-e61f-4deb-ac69-b73d6e3f361c
geometry_μ = ParallelRayOptics(angles, μ2)
geometry_μ = ParallelRayOptics(angles=angles, DMD_diameter=10e-3, μ=μ2)

# ╔═╡ 0e4da85b-99bd-47e0-a8fe-3c69c3a05708
@mytime patterns_μ, printed_intensity_μ, optim_res_μ = optimize_patterns(togoc(target), geometry_μ,
Expand Down Expand Up @@ -218,10 +229,10 @@ Luckily, you just have to specify the following parameters for the geometry:
# ╔═╡ f9a82207-6841-44c9-9aec-e8da3de8e0b0
geometry_vial = VialRayOptics(
angles=angles,
μ=2/256,
μ=0/256,
R_outer=(16.60e-3) / 2,
R_inner=(15.2e-3) / 2,
DMD_diameter=14.6e-3,
DMD_diameter=16.60e-3,
n_vial=1.47,
n_resin=1.4849
)
Expand Down Expand Up @@ -266,7 +277,7 @@ sum(patterns_vial) / (maximum(patterns_vial) * length(patterns_vial))
md"# 6. Reduce Sparsity"

# ╔═╡ 699a0dbd-df85-41b9-b751-cc936d6c03c5
loss_sparse = LossThresholdSparsity(thresholds=(0.75f0, 0.85f0), λ=3f-6)
loss_sparse = LossThresholdSparsity(thresholds=(0.9f0, 0.97f0), λ=500f-6)

# ╔═╡ 006aa5f6-8008-455a-a2a6-54eeb0c097bf
@mytime patterns_vial_s, printed_intensity_vial_s, optim_res_vial_s = optimize_patterns(togoc(target), geometry_vial,
Expand All @@ -276,6 +287,9 @@ loss_sparse = LossThresholdSparsity(thresholds=(0.75f0, 0.85f0), λ=3f-6)
# ╔═╡ 9f7d62be-378f-4b3c-bf38-239c9daa8705
plot_intensity_histogram(target, printed_intensity_vial_s, loss_sparse.thresholds)

# ╔═╡ 93f5bffa-f916-4d4b-90f5-751613b48efb
calculate_IoU(target, Array(printed_intensity_vial_s .> 0.93))

# ╔═╡ b4c7ed0d-e31e-417b-abaf-2193980c9378
@bind thresh5 PlutoUI.Slider(0:0.01:1, show_value=true, default=0.5)

Expand All @@ -288,11 +302,15 @@ simshow(Array(patterns_vial_s[:,:,1]), cmap=:turbo)
# ╔═╡ dc0c21f2-980a-4c97-9a9d-b3d824121bbe
md"
The efficiency can be defined as the mean of the pixel values divided by the maximum.
Because in experiment the real absolute maximum is fixed by the light source intensity. Hence we would like to maximize the real efficiency of the DMD
Because in experiment the real absolute maximum is fixed by the light source intensity. Hence we would like to maximize the real efficiency of the DMD.
In this case the efficiency is $\eta$ = $(sum(patterns_vial_s) / (maximum(patterns_vial_s) * length(patterns_vial)))
"

# ╔═╡ 23c9a442-d892-464d-9174-960eca8de2e7
sum(patterns_vial_s) / (maximum(patterns_vial_s) * length(patterns_vial))
# ╔═╡ a0f85250-7c40-4026-b2b2-d267ec58de6f
histogram(xlabel="pixel intensity", ylabel="Occurence", Array(patterns_vial)[:], yscale=:log10, title="Without sparsity term")

# ╔═╡ 0a805204-d2ad-4672-88ab-4acdff243083
histogram(xlabel="pixel intensity", ylabel="Occurence", Array(patterns_vial_s)[:], yscale=:log10, title="With sparsity term")

# ╔═╡ a7cafba6-849a-4eb8-9709-a76cb98e9879
md"# 7. Let's do a bigger 3D object!
Expand All @@ -305,17 +323,18 @@ We deactive the cell by default. However of the (...) next to the cell and click

# ╔═╡ 8824fb56-0fbf-4cba-9aea-6449627923f2
geometry_vial2 = VialRayOptics(
angles=range(0, 2π, 300),
μ=2/256,
angles=range(0, 2π, 1000)[begin:end-1],
μ=nothing,
R_outer=(16.60e-3) / 2,
R_inner=(15.2e-3) / 2,
n_vial=1.47,
n_resin=1.4849
n_resin=1.4849,
DMD_diameter=10e-3
)

# ╔═╡ 0a655d51-e3b6-413b-83de-9781974242a2
begin
KK = 128
KK = 400
target_3D = box(Float32, (KK, KK, KK), (80, 80, 100)) .-
box(Float32, (KK, KK, KK), (60, 50, 80));
target_3D = Float32.(Bool.(target_3D) .|| (rr2(size(target_3D)) .< 30^2))
Expand All @@ -327,9 +346,12 @@ md"z slide value $(@bind slice PlutoUI.Slider(axes(target_3D, 3), show_value=tru
# ╔═╡ a05d8dd4-40c3-41da-9dbd-f58ab161b2d4
simshow(target_3D[:, :, slice])

# ╔═╡ 1bdd07f2-35df-4d5e-b296-7159ae774aa3
geometry3 = ParallelRayOptics(angles=angles[1:1], μ=1/16.6e-3, DMD_diameter=16.6e-3)

# ╔═╡ d6a59254-bffe-4116-9335-8884eb44556f
@mytime patterns_3D, printed_intensity_3D, optim_res_3D = optimize_patterns(togoc(target_3D), geometry_vial2,
GradientBased(optimizer=Optim.LBFGS(), options=Optim.Options(iterations=20, store_trace=true))
GradientBased(optimizer=Optim.LBFGS(m=3), options=Optim.Options(iterations=20, store_trace=true))
, loss)

# ╔═╡ 6009b601-6988-4be8-a519-7c59660c73ab
Expand All @@ -341,11 +363,14 @@ md"threshold value=$(@bind thresh4 PlutoUI.Slider(0:0.01:1, show_value=true, def
# ╔═╡ 10cc0e79-5d5b-43cf-afb8-ad21875d6d97
md"z slider value $(@bind slice2 PlutoUI.Slider(axes(target_3D, 3), show_value=true, default=0.5))"

# ╔═╡ 2a879d1f-c430-48dd-9264-4c1d6b5bbf18
exp(-0.99)

# ╔═╡ 0835d1b0-a562-4edf-939e-3044f94e53c7
[simshow(Array(printed_intensity_3D[:, :, slice2]), set_one=false) simshow(ones((size(target_3D, 1), 5))) simshow(thresh4 .< Array(printed_intensity_3D[:, :, slice2])) simshow(ones((size(target_3D, 1), 5))) simshow(target_3D[:, :, slice2])]

# ╔═╡ 83793ffe-38de-4a76-b80f-8dee1d18c5a5
plot_intensity_histogram(target_3D, printed_intensity_3D, loss.thresholds)
plot_intensity_histogram(target_3D, printed_intensity_3D, loss.thresholds, xlim=(0.7, 1.1))

# ╔═╡ 262b96e8-a78c-441c-9b46-2d87636286d7
md"angle $(@bind angle PlutoUI.Slider(axes(patterns_3D, 2), show_value=true, default=0.5))"
Expand All @@ -367,6 +392,7 @@ simshow(Array(patterns_3D[:,angle,:]), cmap=:turbo, set_one=true)
# ╟─465e830f-ca33-427a-ad7b-e18f3bbaa3c4
# ╠═4475e0c5-4ae6-403f-bb2b-c5bc8a25d18a
# ╠═b2dae3ea-5a40-498f-a134-5a9a98e58de0
# ╠═827f6f0a-12d9-4a72-8f8b-c3869a037590
# ╠═047023bf-96dd-4427-aa13-3fb2340af90e
# ╟─1d2bdd44-95d2-45ef-8e24-c8ca3df7485a
# ╠═ae3fdae9-7b3c-4391-8fff-530f7758942c
Expand All @@ -385,9 +411,11 @@ simshow(Array(patterns_3D[:,angle,:]), cmap=:turbo, set_one=true)
# ╟─dc538ea6-54e3-4a0f-a5ef-427802239cf2
# ╟─71333d50-fa28-4bdf-bad4-d787682e043a
# ╟─5801f61a-79fa-4326-82dd-3901dd4d493a
# ╠═605e1421-23dd-465d-b94d-3a649154f6f8
# ╠═b08e897d-c260-4a42-8065-989abbbcab4e
# ╠═44ea2ea2-1e4f-44d3-aa02-68e96d6c7b39
# ╠═92f5b987-13c7-463c-b150-71c51ec4aa72
# ╠═6654e278-3d71-48a3-9382-7b8e486f4d72
# ╠═3fb2afdb-2ad2-4ce5-9ca3-e1c5c1d45970
# ╟─11af6da4-5fa2-4e48-a1c8-32bc686b8239
# ╠═2f51ec68-09c6-4475-81b9-ec252fba5df7
# ╠═2e76c088-e61f-4deb-ac69-b73d6e3f361c
Expand All @@ -398,7 +426,7 @@ simshow(Array(patterns_3D[:,angle,:]), cmap=:turbo, set_one=true)
# ╠═17621d91-1f93-4fcf-abf1-5e97463a0bdf
# ╠═05c6a38e-a87d-46da-acb6-33b393369f4c
# ╟─f940c2a6-ebc2-4dca-986c-fe25bfa9e4f0
# ╟─f9a82207-6841-44c9-9aec-e8da3de8e0b0
# ╠═f9a82207-6841-44c9-9aec-e8da3de8e0b0
# ╠═71b479a0-29fc-4411-a02d-096427f2a631
# ╠═675764cb-721a-4b89-92c4-8a1ab7f5867f
# ╠═9ddd098d-2d78-4de8-a322-40a2463adcda
Expand All @@ -412,21 +440,25 @@ simshow(Array(patterns_3D[:,angle,:]), cmap=:turbo, set_one=true)
# ╠═699a0dbd-df85-41b9-b751-cc936d6c03c5
# ╠═006aa5f6-8008-455a-a2a6-54eeb0c097bf
# ╠═9f7d62be-378f-4b3c-bf38-239c9daa8705
# ╠═93f5bffa-f916-4d4b-90f5-751613b48efb
# ╟─b4c7ed0d-e31e-417b-abaf-2193980c9378
# ╟─afa94e1a-d426-4a29-b0fd-73bcdc3e914f
# ╠═0fc8abe6-2a22-477e-b2a4-6ccba1e16991
# ╟─dc0c21f2-980a-4c97-9a9d-b3d824121bbe
# ╟─23c9a442-d892-464d-9174-960eca8de2e7
# ╟─a0f85250-7c40-4026-b2b2-d267ec58de6f
# ╟─0a805204-d2ad-4672-88ab-4acdff243083
# ╟─a7cafba6-849a-4eb8-9709-a76cb98e9879
# ╠═8824fb56-0fbf-4cba-9aea-6449627923f2
# ╠═0a655d51-e3b6-413b-83de-9781974242a2
# ╟─877b1484-969c-45d7-a3e7-4f0301a81a4b
# ╠═a05d8dd4-40c3-41da-9dbd-f58ab161b2d4
# ╠═1bdd07f2-35df-4d5e-b296-7159ae774aa3
# ╠═d6a59254-bffe-4116-9335-8884eb44556f
# ╠═6009b601-6988-4be8-a519-7c59660c73ab
# ╟─fa45b520-18a8-4465-b642-a1c08de48e20
# ╟─10cc0e79-5d5b-43cf-afb8-ad21875d6d97
# ╠═2a879d1f-c430-48dd-9264-4c1d6b5bbf18
# ╟─0835d1b0-a562-4edf-939e-3044f94e53c7
# ╟─83793ffe-38de-4a76-b80f-8dee1d18c5a5
# ╠═83793ffe-38de-4a76-b80f-8dee1d18c5a5
# ╟─262b96e8-a78c-441c-9b46-2d87636286d7
# ╠═c8774a2b-47e0-4be6-b213-bdef6d7b0726
4 changes: 2 additions & 2 deletions examples/2_benchy_boat_optimization_ray_optics.jl
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
### A Pluto.jl notebook ###
# v0.19.40
# v0.19.42

using Markdown
using InteractiveUtils
Expand Down Expand Up @@ -112,7 +112,7 @@ loss = LossThreshold(thresholds=(0.7, 0.8))
angles = range(0, 2π, 201)[begin:end-1]

# ╔═╡ 93cebf3f-389b-4fde-8d13-af3e1165ad9c
geometry = ParallelRayOptics(angles, nothing)
geometry = ParallelRayOptics(angles=angles, μ=nothing, DMD_diameter=Inf)

# ╔═╡ 2ffbef8c-066d-49b6-8a29-1ef710099029
optimizer = GradientBased(optimizer=Optim.LBFGS(), options=Optim.Options(iterations=40, store_trace=true))
Expand Down
25 changes: 17 additions & 8 deletions examples/4_realistic_pattern_optimization.jl
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
### A Pluto.jl notebook ###
# v0.19.38
# v0.19.42

using Markdown
using InteractiveUtils
Expand Down Expand Up @@ -83,22 +83,30 @@ md"# 2. Specify Optimization Parameters"
loss = LossThreshold(thresholds=(0.65, 0.75))

# ╔═╡ e5bec833-47c5-4f65-8667-f28d4ccb1fbc
angles = range(0, 2π, 301)[begin:end-1]
angles = range(0, 2π, 1001)[begin:end-1]

# ╔═╡ e45eeeed-a86e-40e1-b4a2-e4c25cc6a368
optimizer = GradientBased(optimizer=Optim.LBFGS(), options=Optim.Options(iterations=20, store_trace=true))

# ╔═╡ f618cc32-6851-42de-b2c2-0ddc70b4668d
geometry_vial = VialRayOptics(
angles=angles,
μ=2/550,
μ=2 / 14.6e-3,
R_outer=(16.60e-3) / 2,
R_inner=(15.2e-3) / 2,
DMD_diameter=14.6e-3,
n_vial=1.47,
n_resin=1.4849
)

# ╔═╡ 29bbf20e-da89-42af-b7a0-a921c1c5fe3f
md"
In this case the DMD is smaller than the glass vial.
That means, our DMD area is smaller than the simulated target volume.
The resulting pattern size is: $(floor(Int, (14.6e-3 / 16.6e-3) * size(target, 1))) pixels

"

# ╔═╡ 3dfccb9a-8f21-4695-965c-c1d97077ba52
md"# 3. Optimize"

Expand Down Expand Up @@ -138,12 +146,12 @@ md"Different projection patterns: $(@bind angle PlutoUI.Slider(axes(patterns_via
simshow(Array(patterns_vial[:,angle,:])[end:-1:begin, :]', cmap=:turbo, set_one=true)

# ╔═╡ Cell order:
# ╠═c973ccfd-11b7-4e0a-9d1a-32b0d449bfd1
# ╟─c973ccfd-11b7-4e0a-9d1a-32b0d449bfd1
# ╠═32d9b7b4-bacd-48c5-8ac5-a7d16bc26f3f
# ╠═fb00f77e-3a43-43c6-83f5-ab9ffa690585
# ╠═2a2c5c57-3b51-463b-90b3-bf4733df1c71
# ╠═9122b9a9-43f4-48be-88f6-6d02a110f900
# ╠═5c3dda08-010e-4d76-bf35-1c3385a2a0c0
# ╟─5c3dda08-010e-4d76-bf35-1c3385a2a0c0
# ╠═efc9b013-0452-482f-9112-d444d2197ba9
# ╠═54e9b00d-2589-430e-9ac9-ffdb48a54c0e
# ╠═93591474-679b-4944-a673-616cb5f354e0
Expand All @@ -152,14 +160,15 @@ simshow(Array(patterns_vial[:,angle,:])[end:-1:begin, :]', cmap=:turbo, set_one=
# ╠═5e2ffa51-c23b-4223-89f8-dfa138cfaca5
# ╠═9aa5e2c5-f0d0-43dc-9c3a-b4cebc5bb100
# ╠═6869714a-040b-4694-b099-8962445daa33
# ╠═e7715af7-de78-4668-9733-487f482ac3ef
# ╟─e7715af7-de78-4668-9733-487f482ac3ef
# ╠═018120f1-8859-4606-ac62-507fbcaf4097
# ╠═f1cbc5fd-8454-430c-9267-73c67eca623b
# ╟─f1cbc5fd-8454-430c-9267-73c67eca623b
# ╠═3f00e0e5-2d87-406c-9e4a-42b76c3bf2ec
# ╠═e5bec833-47c5-4f65-8667-f28d4ccb1fbc
# ╠═e45eeeed-a86e-40e1-b4a2-e4c25cc6a368
# ╠═f618cc32-6851-42de-b2c2-0ddc70b4668d
# ╠═3dfccb9a-8f21-4695-965c-c1d97077ba52
# ╟─29bbf20e-da89-42af-b7a0-a921c1c5fe3f
# ╟─3dfccb9a-8f21-4695-965c-c1d97077ba52
# ╠═2350dd2f-11dd-4b81-93b0-a05e88dd0fa7
# ╠═6eb9c0bc-2aa7-470d-96ca-3716c3c75e4e
# ╟─819bb337-ef70-4088-82da-bda208264b30
Expand Down
Loading
Loading