Skip to content
Snippets Groups Projects
Commit e565d096 authored by François  LAURENT's avatar François LAURENT
Browse files

kdtrees to pick nearest neighbors and accelerate the drag (#70)

parent cda5b477
No related branches found
No related tags found
No related merge requests found
Pipeline #92159 passed
...@@ -2,7 +2,7 @@ ...@@ -2,7 +2,7 @@
julia_version = "1.8.2" julia_version = "1.8.2"
manifest_format = "2.0" manifest_format = "2.0"
project_hash = "d37bc0692e4cb75dce4365cae9dc31c02a907c73" project_hash = "2a3d26785ece5a51ad6d73d93147b49dbedd4551"
[[deps.AbstractFFTs]] [[deps.AbstractFFTs]]
deps = ["ChainRulesCore", "LinearAlgebra"] deps = ["ChainRulesCore", "LinearAlgebra"]
......
...@@ -13,6 +13,7 @@ LinearAlgebra = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e" ...@@ -13,6 +13,7 @@ LinearAlgebra = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e"
Logging = "56ddb016-857b-54e1-b83d-db4d58db5568" Logging = "56ddb016-857b-54e1-b83d-db4d58db5568"
Makie = "ee78f7c6-11fb-53f2-987a-cfe4a2b5a57a" Makie = "ee78f7c6-11fb-53f2-987a-cfe4a2b5a57a"
Meshes = "eacbb407-ea5a-433e-ab97-5258b1ca43fa" Meshes = "eacbb407-ea5a-433e-ab97-5258b1ca43fa"
NearestNeighbors = "b8a86587-4115-5ab1-83bc-aa920d37bbce"
Observables = "510215fc-4207-5dde-b226-833fc4488ee2" Observables = "510215fc-4207-5dde-b226-833fc4488ee2"
ObservationPolicies = "6317928a-6b1a-42e8-b853-b8e2fc3e9ca3" ObservationPolicies = "6317928a-6b1a-42e8-b853-b8e2fc3e9ca3"
OrderedCollections = "bac558e1-5e72-5ebc-8fee-abe8a469f55d" OrderedCollections = "bac558e1-5e72-5ebc-8fee-abe8a469f55d"
......
...@@ -18,6 +18,7 @@ import Dates ...@@ -18,6 +18,7 @@ import Dates
using OrderedCollections using OrderedCollections
using Random using Random
using LinearAlgebra using LinearAlgebra
using NearestNeighbors
export larvaviewer, larvaeditor export larvaviewer, larvaeditor
......
...@@ -118,6 +118,7 @@ struct SingleLarvaView ...@@ -118,6 +118,7 @@ struct SingleLarvaView
usertags::Observable usertags::Observable
visible::Observable{Bool} visible::Observable{Bool}
path::Observable{PathOrOutline} path::Observable{PathOrOutline}
pathtree::Observable{<:NNTree}
outline::Observable{PathOrOutline} outline::Observable{PathOrOutline}
outline_color::Observable{String} outline_color::Observable{String}
# #
...@@ -139,6 +140,7 @@ function SingleLarvaView(larvae::Vector{LarvaModel}, controller; editabletags::B ...@@ -139,6 +140,7 @@ function SingleLarvaView(larvae::Vector{LarvaModel}, controller; editabletags::B
usertags = Observable(larva.usertags) usertags = Observable(larva.usertags)
visible = Observable(false) visible = Observable(false)
path = Observable(larva.path) path = Observable(larva.path)
pathtree = map(KDTree, path)
shape_outline = Observable(outline(Makie.Point2f, state)) shape_outline = Observable(outline(Makie.Point2f, state))
shape_color = Observable(html_color(color)) shape_color = Observable(html_color(color))
...@@ -207,7 +209,7 @@ function SingleLarvaView(larvae::Vector{LarvaModel}, controller; editabletags::B ...@@ -207,7 +209,7 @@ function SingleLarvaView(larvae::Vector{LarvaModel}, controller; editabletags::B
segment_visible = Observable(false) segment_visible = Observable(false)
SingleLarvaView(controller, model, usertags, visible, SingleLarvaView(controller, model, usertags, visible,
path, shape_outline, shape_color, path, pathtree, shape_outline, shape_color,
segment, segment_color, segment_visible) segment, segment_color, segment_visible)
end end
...@@ -218,10 +220,9 @@ norm2(p) = p[1]^2 + p[2]^2 ...@@ -218,10 +220,9 @@ norm2(p) = p[1]^2 + p[2]^2
function pick_timestep(scene, larva) function pick_timestep(scene, larva)
pos = mouseposition(scene) pos = mouseposition(scene)
dist2 = norm2(larva.path[], pos) best, dist = nn(larva.pathtree[], pos)
best = argmin(dist2)
timestep = larva.model[].alignedsteps[best] timestep = larva.model[].alignedsteps[best]
return timestep, best, dist2[best] return timestep, best, dist
end end
function setinitialstep!(initialstep, larva) function setinitialstep!(initialstep, larva)
...@@ -241,13 +242,13 @@ function setinitialstep!(initialstep, larva) ...@@ -241,13 +242,13 @@ function setinitialstep!(initialstep, larva)
end end
function pick_timesegment(scene, larva, initialstep) function pick_timesegment(scene, larva, initialstep)
timestep, best, dist2 = pick_timestep(scene, larva) timestep, best, dist = pick_timestep(scene, larva)
path = larva.path[] path = larva.path[]
if initialstep isa Ref if initialstep isa Ref
initialstep = initialstep[] initialstep = initialstep[]
end end
segment = initialstep <= best ? path[initialstep:best] : path[best:initialstep] segment = initialstep <= best ? path[initialstep:best] : path[best:initialstep]
return timestep, segment, dist2 return timestep, segment, dist
end end
function assign_tag_to_segment!(larvaview, firststep) function assign_tag_to_segment!(larvaview, firststep)
...@@ -359,7 +360,7 @@ function setmouseevents!(scene, larva::SingleLarvaView; ...@@ -359,7 +360,7 @@ function setmouseevents!(scene, larva::SingleLarvaView;
return Consume(false) return Consume(false)
end end
dodrag = newobservable(Cooldown(0.2), true) dodrag = newobservable(Cooldown(0.1), true)
Makie.onmouseleftdrag(mouseevents) do _ Makie.onmouseleftdrag(mouseevents) do _
if dragging[] if dragging[]
...@@ -380,9 +381,9 @@ function setmouseevents!(scene, larva::SingleLarvaView; ...@@ -380,9 +381,9 @@ function setmouseevents!(scene, larva::SingleLarvaView;
if b if b
@info "Dragging" @info "Dragging"
if iseditable(larva) if iseditable(larva)
timestep, segment, dist2 = pick_timesegment(scene, larva, initialstep) timestep, segment, dist = pick_timesegment(scene, larva, initialstep)
settimestep!(larva.controller, timestep) settimestep!(larva.controller, timestep)
if dist2 > (2reference_larva_size)^2 if dist > 2reference_larva_size
assignmentfailed!(larva.controller, "mouse pointer away") assignmentfailed!(larva.controller, "mouse pointer away")
stop() stop()
else else
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment