Skip to content

Commit

Permalink
Merge pull request #7 from platawiec/isbits-edge
Browse files Browse the repository at this point in the history
Edge isbits, 20% faster
  • Loading branch information
platawiec authored Mar 20, 2018
2 parents fccfcee + f7cf2fe commit 17ce3e6
Showing 1 changed file with 21 additions and 18 deletions.
39 changes: 21 additions & 18 deletions src/unwrap_common.jl
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,17 @@ end
Pixel(v) = Pixel{typeof(v)}(0, v, rand(), 1)
@inline Base.length(p::Pixel) = p.head.groupsize

struct Edge{T}
struct Edge{N}
reliability::Float64
pixel_1::Pixel{T}
pixel_2::Pixel{T}
pixel_1::CartesianIndex{N}
pixel_2::CartesianIndex{N}
periods::Int
end
function Edge{N}(pixel_image, ind1, ind2) where N
@inbounds rel = pixel_image[ind1].reliability + pixel_image[ind2].reliability
@inbounds periods = find_period(pixel_image[ind1].val, pixel_image[ind2].val)
return Edge{N}(rel, ind1, ind2, periods)
end
Edge(g1, g2) = Edge(g1.reliability + g2.reliability,
g1, g2,
find_period(g1.val, g2.val))
Expand All @@ -38,15 +43,15 @@ function unwrap!(wrapped_image::AbstractArray{T, N},

pixel_image = init_pixels(wrapped_image)
calculate_reliability(pixel_image, wrap_around)
edges = Edge{eltype(wrapped_image)}[]
edges = Edge{N}[]
num_edges = _predict_num_edges(size(wrapped_image), wrap_around)
sizehint!(edges, num_edges)
for idx_dim=1:N
populate_edges!(edges, pixel_image, idx_dim, wrap_around[idx_dim])
end

sort!(edges, alg=MergeSort)
gather_pixels!(edges)
gather_pixels!(pixel_image, edges)
unwrap_image!(wrapped_image, pixel_image)

return wrapped_image
Expand All @@ -69,9 +74,11 @@ function init_pixels(wrapped_image)
return pixel_image
end

function gather_pixels!(edges)
function gather_pixels!(pixel_image, edges)
for edge in edges
merge_groups!(edge)
@inbounds p1 = pixel_image[edge.pixel_1]
@inbounds p2 = pixel_image[edge.pixel_2]
merge_groups!(edge, p1, p2)
end
end

Expand All @@ -98,9 +105,7 @@ function find_period(val_left, val_right)
return period
end

function merge_groups!(edge)
pixel_1 = edge.pixel_1
pixel_2 = edge.pixel_2
function merge_groups!(edge, pixel_1, pixel_2)
if is_differentgroup(pixel_1, pixel_2)
# pixel 2 is alone in group
if is_pixelalone(pixel_2)
Expand Down Expand Up @@ -155,22 +160,20 @@ function populate_edges!(edges, pixel_image::Array{T, N}, dim, connected) where
size_img[dim] -= 1
idx_step = fill(0, N)
idx_step[dim] += 1
idx_step = CartesianIndex{N}(idx_step...)
idx_step_cart = CartesianIndex{N}(idx_step...)
idx_size = CartesianIndex{N}(size_img...)
for i in CartesianRange(idx_size)
push!(edges, Edge(pixel_image[i],
pixel_image[i+idx_step]))
push!(edges, Edge{N}(pixel_image, i, i+idx_step_cart))
end
if connected
idx_step = fill(0, N)
idx_step[dim] = -size_img[dim]
idx_step = CartesianIndex{N}(idx_step...)
idx_step_cart = CartesianIndex{N}(idx_step...)
edge_begin = fill(1, N)
edge_begin[dim] = size(pixel_image)[dim]
edge_begin = CartesianIndex{N}(edge_begin...)
for i in CartesianRange(edge_begin, CartesianIndex(size(pixel_image)))
push!(edges, Edge(pixel_image[i],
pixel_image[i+idx_step]))
edge_begin_cart = CartesianIndex{N}(edge_begin...)
for i in CartesianRange(edge_begin_cart, CartesianIndex(size(pixel_image)))
push!(edges, Edge{N}(pixel_image, i, i+idx_step_cart))
end
end
end
Expand Down

0 comments on commit 17ce3e6

Please sign in to comment.