From 4bff4af7e3b17b1fe719ab66dc77cf48d8b83da2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Hans=20W=C3=BCrfel?= Date: Mon, 22 Jan 2024 13:22:32 +0100 Subject: [PATCH 1/2] add Aqua.jl tests and remove method ambiguity --- Project.toml | 10 +++++++++- src/beziercurves.jl | 4 ++-- test/Aqua.jl | 6 ++++++ test/runtests.jl | 2 ++ 4 files changed, 19 insertions(+), 3 deletions(-) create mode 100644 test/Aqua.jl diff --git a/Project.toml b/Project.toml index 21ee8eff..aef525eb 100644 --- a/Project.toml +++ b/Project.toml @@ -15,17 +15,25 @@ SimpleTraits = "699a6c99-e7fa-54fc-8d76-47d257e15c1d" StaticArrays = "90137ffa-7385-5640-81b9-e52037218182" [compat] +Aqua = "0.8" +CairoMakie = "0.11" DataStructures = "0.17, 0.18" +FileIO = "1" GeometryBasics = "0.4" Graphs = "1.4" +LinearAlgebra="1" +Literate = "2" Makie = "0.20" NetworkLayout = "0.4.3" PolynomialRoots = "1" +ReferenceTests = "0.10" SimpleTraits = "0.9" StaticArrays = "1.2" +Test = "1" julia = "1" [extras] +Aqua = "4c88cf16-eb10-579e-8560-4a9242c79595" CairoMakie = "13f3f980-e62b-5c42-98c6-ff1f3baf88f0" FileIO = "5789e2e9-d7fb-5bc7-8068-2c6fae9b9549" Literate = "98b081ad-f1c9-55d3-8b20-4c87d4299306" @@ -33,4 +41,4 @@ ReferenceTests = "324d217c-45ce-50fc-942e-d289b448e8cf" Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40" [targets] -test = ["CairoMakie", "FileIO", "Literate", "ReferenceTests", "Test"] +test = ["Aqua", "CairoMakie", "FileIO", "Literate", "ReferenceTests", "Test"] diff --git a/src/beziercurves.jl b/src/beziercurves.jl index 2f1a9f51..269a2ab1 100644 --- a/src/beziercurves.jl +++ b/src/beziercurves.jl @@ -89,7 +89,7 @@ Method: Calculates the square distance between `pt` and path `p` and minimizes ( let a = p0 - pt, b = p - p0 t = -(a[1]*b[1] + a[2]*b[2]) / (b[1]^2 + b[2]^2) """ -function inverse_interpolate(p::BezierPath{<:Point2}, pt) +function inverse_interpolate(p::BezierPath{<:Point2}, pt::Point2) p0 = p.commands[end-1].p c = p.commands[end] N = length(p.commands) - 1 @@ -103,7 +103,7 @@ function inverse_interpolate(p::BezierPath{<:Point2}, pt) return t end -function inverse_interpolate(l::Line{PT}, pt) where PT +function inverse_interpolate(l::Line{PT}, pt::PT) where PT a = l.p0 - pt b = l.p - l.p0 t = -(a[1]*b[1] + a[2]*b[2]) / (b[1]^2 + b[2]^2) diff --git a/test/Aqua.jl b/test/Aqua.jl new file mode 100644 index 00000000..171d6412 --- /dev/null +++ b/test/Aqua.jl @@ -0,0 +1,6 @@ +using Aqua + +Aqua.test_ambiguities(GraphMakie) +Aqua.test_all(GraphMakie; + ambiguities=false, + unbound_args=false) diff --git a/test/runtests.jl b/test/runtests.jl index 34f9611f..d9e148bd 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -6,6 +6,8 @@ using Makie.Colors using StaticArrays using Test +include("Aqua.jl") + include("beziercurves_test.jl") @testset "GraphMakie.jl" begin From 845643f63b6334af14e3a174dfd702758e5461f5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Hans=20W=C3=BCrfel?= Date: Mon, 22 Jan 2024 15:38:34 +0100 Subject: [PATCH 2/2] create 3d+:end warning without method ambiguity --- src/beziercurves.jl | 8 +------- src/recipes.jl | 44 ++++++++++++++++++++++++++++---------------- 2 files changed, 29 insertions(+), 23 deletions(-) diff --git a/src/beziercurves.jl b/src/beziercurves.jl index 269a2ab1..a3313afe 100644 --- a/src/beziercurves.jl +++ b/src/beziercurves.jl @@ -103,19 +103,13 @@ function inverse_interpolate(p::BezierPath{<:Point2}, pt::Point2) return t end -function inverse_interpolate(l::Line{PT}, pt::PT) where PT +function inverse_interpolate(l::Line{<:Point2}, pt::Point2) a = l.p0 - pt b = l.p - l.p0 t = -(a[1]*b[1] + a[2]*b[2]) / (b[1]^2 + b[2]^2) return t end -function inverse_interpolate(p, pt::Point3) - # TODO: is this the right place to throw an error when trying to shift arrows to destination nodes? - @warn "arrow_shift = :end will not display properly for 3D plots." - nothing -end - _inverse_interpolate(c::LineTo{<:Point2}, p0, pt) = inverse_interpolate(Line(p0, c.p), pt) function _inverse_interpolate(c::CurveTo{<:Point2}, p0, pt) p1, p2, p3 = c.c1, c.c2, c.p diff --git a/src/recipes.jl b/src/recipes.jl index c2eafbd7..26114937 100644 --- a/src/recipes.jl +++ b/src/recipes.jl @@ -753,30 +753,42 @@ lands on the surface of the destination node. function update_arrow_shift(g, gp, edge_paths::Vector{<:AbstractPath{PT}}, to_px, node_markers, node_sizes, shift) where {PT} arrow_shift = Vector{Float32}(undef, ne(g)) + warn_nan = false + warn_3d = false for (i,e) in enumerate(edges(g)) t = getattr(shift, i, 0.5) if t === :end - j = dst(e) - p0 = getattr(gp.node_pos, j) - node_marker = getattr(node_markers, j) - node_size = getattr(node_sizes, j) - arrow_marker = getattr(gp.arrow_marker, i) - arrow_size = getattr(gp.arrow_size, i) - d = distance_between_markers(node_marker, node_size, arrow_marker, arrow_size) - p1 = point_near_dst(edge_paths[i], p0, d, to_px) - t = inverse_interpolate(edge_paths[i], p1) - if isnan(t) - @warn """ - Shifting arrowheads to destination nodes failed. - This can happen when the markers are inadequately scaled (e.g., when zooming out too far). - Arrow shift has been reset to 0.5. - """ + if PT <: Point2 + j = dst(e) + p0 = getattr(gp.node_pos, j) + node_marker = getattr(node_markers, j) + node_size = getattr(node_sizes, j) + arrow_marker = getattr(gp.arrow_marker, i) + arrow_size = getattr(gp.arrow_size, i) + d = distance_between_markers(node_marker, node_size, arrow_marker, arrow_size) + p1 = point_near_dst(edge_paths[i], p0, d, to_px) + t = inverse_interpolate(edge_paths[i], p1) + if isnan(t) + warn_nan = true + t = 0.5 + end + else + warn_3d = true t = 0.5 end end arrow_shift[i] = t end - + if warn_nan + @warn """ + Shifting arrowheads to destination nodes failed. + This can happen when the markers are inadequately scaled (e.g., when zooming out too far). + Arrow shift has been reset to 0.5. + """ + end + if warn_3d + @warn "`arrow_shift=:end` not implemented for 3d plot!" + end return arrow_shift end