Skip to content

Commit

Permalink
Merge pull request #13 from DanielVandH/dt
Browse files Browse the repository at this point in the history
Fix package
  • Loading branch information
jonocarroll authored May 13, 2024
2 parents f6c4916 + 18947d5 commit 2aa2adf
Show file tree
Hide file tree
Showing 9 changed files with 52 additions and 42 deletions.
9 changes: 4 additions & 5 deletions Project.toml
Original file line number Diff line number Diff line change
@@ -1,31 +1,30 @@
name = "TriangulArt"
uuid = "4e825928-4f37-4da1-b333-216cc9c54310"
authors = ["Jonathan Carroll"]
version = "1.0.0-DEV"
version = "1.0.1"

[deps]
DelaunayTriangulation = "927a84f5-c5f4-47a5-9785-b46e178433df"
FileIO = "5789e2e9-d7fb-5bc7-8068-2c6fae9b9549"
Images = "916415d5-f1e6-5110-898d-aaa5f9f070e0"
Plots = "91a5bcdd-55d7-5caf-9e0b-520d859cae80"
PolygonOps = "647866c9-e3ac-4575-94e7-e3d426903924"
PyCall = "438e738f-606a-5dbb-bf0a-cddfbfd45ab0"
StableRNGs = "860ef19b-820b-49d6-a774-d7a799459cd3"
StatsBase = "2913bbd2-ae8a-5f71-8c99-4fb6c76f3a91"

[compat]
DelaunayTriangulation = "0.8, 1"
DelaunayTriangulation = "1"
FileIO = "1"
Images = "0.26"
Plots = "1"
PolygonOps = "0.1"
PyCall = "1"
StableRNGs = "1"
StatsBase = "0.34"
julia = "1"

[extras]
ReferenceTests = "324d217c-45ce-50fc-942e-d289b448e8cf"
Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"

[targets]
test = ["Test"]
test = ["Test", "ReferenceTests"]
57 changes: 23 additions & 34 deletions src/TriangulArt.jl
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ using StatsBase
using DelaunayTriangulation
using StableRNGs
using Plots
using PolygonOps

using PyCall

Expand Down Expand Up @@ -76,12 +75,13 @@ function generate_weighted_points(image::Matrix{RGB{N0f8}}, n_points::Integer=10
end
end
end

unique(epts, dims=1)
end

function which_tri(trivec::Vector{Vector{Vector{Float64}}}, x::Integer, y::Integer)
findfirst(z -> 1 == inpolygon((x, y), trivec[z]), 1:length(trivec))
function which_tri(tri::Triangulation, x, y, k=0)
q = (float(x), float(y))
V = iszero(k) ? jump_and_march(tri, q) : jump_and_march(tri, q; k)
return DelaunayTriangulation.sort_triangle(V)
end

"""
Expand Down Expand Up @@ -126,12 +126,11 @@ function triangulArt(
end
boundary_points = edge_points(image)
all_points = vcat(points, boundary_points)

all_points = @views unique!(tuple.(all_points[:, 1], all_points[:, 2]))
rng = StableRNG(2)
all_points = unique(collect(reinterpret(reshape, Tuple{Float64,Float64}, all_points')), dims=1)
tri = triangulate(all_points; rng)
if (refine)
refine!(tri, max_area=prod(size(image)) / 200, min_area=prod(size(image)) / (2000))
if refine
refine!(tri, max_area=prod(size(image)) / 200, min_area=prod(size(image)) / (2000); rng)
end

# change from color, width, height to height, width, color
Expand All @@ -140,27 +139,20 @@ function triangulArt(
# correctly order height, width
img_HWC = permutedims(img_HWC[:, :, :, 1], (2, 1, 3))

trivec = Vector{Vector{Vector{Float64}}}()
for T in each_triangle(tri)
i, j, k = indices(T)
p, q, r = get_point(tri, i, j, k)
push!(trivec, [[first(p), last(p)],
[first(q), last(q)],
[first(r), last(r)],
[first(p), last(p)]])
end

locs = Vector{Tuple{Integer,Integer,Vector{N0f8},Union{Nothing,Integer}}}()
locs = Vector{Tuple{Int,Int,Vector{N0f8},NTuple{3,Int}}}()
sizey, sizex = size(img_HWC)
k = 0
for x in 1:10:sizex
for y in 1:10:sizey
push!(locs, (x, y, img_HWC[y, x, :], which_tri(trivec, x, y)))
V = which_tri(tri, x, y, k)
k = rand(rng, triangle_vertices(V)) # just take some random vertex to start from
push!(locs, (x, y, img_HWC[y, x, :], V))
end
end
# average colours
at = map(z -> z[4], locs)
at = getindex.(locs, 4)
ind = map(elem -> findall(isequal(elem), at), unique(at))
cols = Dict()
cols = Dict{NTuple{3,Int},NTuple{3,Float32}}()
for i in ind
tr = locs[i][1][4]
r = map(e -> e[3][1], locs[i])
Expand All @@ -170,28 +162,25 @@ function triangulArt(
end

if showimage
pl = plot(Gray.(img_HWC[:, :, 1, 1]), showaxis=false, xlim=(1, sizex), ylim=(1, sizey), aspect_ratio=:auto)
@views pl = plot(Gray.(img_HWC[:, :, 1, 1]), showaxis=false, xlim=(1, sizex), ylim=(1, sizey), aspect_ratio=:auto)
elseif showimagecol
pl = plot(orig_image, showaxis=false, xlim=(1, sizex), ylim=(1, sizey), aspect_ratio=:auto)
else
pl = plot(showaxis=false, yflip=true, xlim=(1, sizex), ylim=(1, sizey))
end
if debug
@views if debug
scatter!(pl, boundary_points[:, 1], boundary_points[:, 2], markercolor=:blue, legend=false)
scatter!(pl, points[:, 1], points[:, 2], markercolor=:red, legend=false)
end

for ii in eachindex(trivec)
pts = trivec[ii]
xs = first.(pts)
ys = last.(pts)
if (haskey(cols, ii))
cr, cg, cb = cols[ii]
else
cr, cg, cb = 0, 0, 0
end
for V in each_solid_triangle(tri)
V = DelaunayTriangulation.sort_triangle(V) # note that we sorted earlier. This ensures that (1) all triangulations are represented in the same order (e.g. (3, 1, 2) and (1, 2, 3) both go to (1, 2, 3)), and that (2) the keys in the Dict only store the triangle once
u, v, w = triangle_vertices(V)
p, q, r = get_point(tri, u, v, w)
xs = [getx(p), getx(q), getx(r), getx(p)]
ys = [gety(p), gety(q), gety(r), gety(p)]
cr, cg, cb = get(cols, V, (0.0f0, 0.0f0, 0.0f0))
tricol = RGBA(cr, cg, cb, 1.0)

if debug
plot!(pl, xs, ys, linecolor=:black, legend=false)
else
Expand Down
Binary file added test/readme_images/readme2.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added test/readme_images/readme3.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added test/readme_images/readme4.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added test/readme_images/readme5.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added test/readme_images/readme6.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added test/readme_images/readme7.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
28 changes: 25 additions & 3 deletions test/runtests.jl
Original file line number Diff line number Diff line change
@@ -1,6 +1,28 @@
using TriangulArt
using Test

@testset "TriangulArt.jl" begin
# Write your tests here.
end
using FileIO
using ReferenceTests

refimg_path = joinpath(dirname(dirname(pathof(TriangulArt))), "test", "readme_images")
readmeimg_path = joinpath(dirname(dirname(pathof(TriangulArt))), "examples")

@testset "TriangulArt" begin
bird = load(joinpath(readmeimg_path, "bird_small.jpg"))
readme1 = triangulArt(bird, npts=50, fast=true) # don't test this: rng is not controlled
# Another problem with the above is that, due to the random sampling,
# not all of the image will be covered by a color.
readme2 = triangulArt(bird)
@test_reference joinpath(refimg_path, "readme2.png") readme2
everest = load(joinpath(readmeimg_path, "everest.jpeg"))
readme3 = triangulArt(everest)
@test_reference joinpath(refimg_path, "readme3.png") readme3
readme4 = triangulArt(bird, npts=500)
@test_reference joinpath(refimg_path, "readme4.png") readme4
readme5 = triangulArt(bird, npts=100, refine=true)
@test_reference joinpath(refimg_path, "readme5.png") readme5
readme6 = triangulArt(bird, npts=100, debug=true)
@test_reference joinpath(refimg_path, "readme6.png") readme6
readme7 = triangulArt(bird, npts=100, showimagecol=false, debug=true)
@test_reference joinpath(refimg_path, "readme7.png") readme7
end

0 comments on commit 2aa2adf

Please sign in to comment.