From 521e753183465acc623d97715b02e7a4618b5899 Mon Sep 17 00:00:00 2001 From: Thomas Breloff Date: Fri, 1 Jul 2016 17:23:25 -0400 Subject: [PATCH] string fix; iter_segments change to use unused and skip initial NaNs; working on glvisualize shape type --- src/backends/glvisualize.jl | 43 +++++++++++++++++++++++++++---------- src/colors.jl | 8 ++++--- src/utils.jl | 23 ++++++++++++-------- 3 files changed, 51 insertions(+), 23 deletions(-) diff --git a/src/backends/glvisualize.jl b/src/backends/glvisualize.jl index d24d4e7b..94bb358b 100644 --- a/src/backends/glvisualize.jl +++ b/src/backends/glvisualize.jl @@ -29,7 +29,7 @@ supported_args(::GLVisualizeBackend) = merge_with_base_supported([ # :clims, # :inset_subplots, ]) -supported_types(::GLVisualizeBackend) = [:surface, :scatter, :scatter3d, :path, :path3d] +supported_types(::GLVisualizeBackend) = [:surface, :scatter, :scatter3d, :path, :path3d, :shape] supported_styles(::GLVisualizeBackend) = [:auto, :solid] supported_markers(::GLVisualizeBackend) = vcat([:none, :auto, :circle], collect(keys(_gl_marker_map))) supported_scales(::GLVisualizeBackend) = [:identity] @@ -101,8 +101,10 @@ gl_color(c::RGBA{Float32}) = c # convert to RGBA function gl_color(c, a=nothing) + @show c, a c = convertColor(c, a) - RGBA{Float32}(getColor(c)) + @show c + RGBA{Float32}(c) end function gl_viewport(bb, rect) @@ -125,11 +127,13 @@ function gl_draw_lines_2d(x, y, color, linewidth, sp_screen) for rng in iter_segments(x, y) n = length(rng) n < 2 && continue + pts = gl_make_points(x[rng], y[rng]) + @show pts, n viz = GLVisualize.visualize( - gl_make_points(x[rng], y[rng]), + pts, n==2 ? :linesegment : :lines, - color=color, - thickness = Float32(linewidth) + color = color, + thickness = thickness ) GLVisualize.view(viz, sp_screen, camera=:orthographic_pixel) end @@ -141,11 +145,12 @@ function gl_draw_lines_3d(x, y, z, color, linewidth, sp_screen) for rng in iter_segments(x, y, z) n = length(rng) n < 2 && continue + pts = gl_make_points(x[rng], y[rng], z[rng]) viz = GLVisualize.visualize( - gl_make_points(x[rng], y[rng], z[rng]), + pts, n==2 ? :linesegment : :lines, color=color, - thickness = Float32(linewidth) + thickness = thickness ) GLVisualize.view(viz, sp_screen, camera=:perspective) end @@ -206,7 +211,7 @@ function gl_display(plt::Plot{GLVisualizeBackend}) sp.o = sp_screen if !is3d(sp) - gl_draw_axes_2d(sp) + # gl_draw_axes_2d(sp) end # loop over the series and add them to the subplot @@ -216,16 +221,16 @@ function gl_display(plt::Plot{GLVisualizeBackend}) x, y = map(Float32, d[:x]), map(Float32, d[:y]) msize = gl_relative_size(plt, d[:markersize]) - viz = if st == :surface + if st == :surface # TODO: can pass just the ranges and surface ismatrix(x) || (x = repmat(x', length(y), 1)) ismatrix(y) || (y = repmat(y, 1, length(x))) z = transpose_z(d, map(Float32, d[:z].surf), false) viz = GLVisualize.visualize((x, y, z), :surface) - GLVisualize.view(viz, sp_screen, camera = :perspective) + GLVisualize.view(viz, sp_screen, camera = camera) else - # paths and scatters + # paths, scatters, and shape _3d && (z = map(Float32, d[:z])) @@ -274,6 +279,22 @@ function gl_display(plt::Plot{GLVisualizeBackend}) # billboard=true #)) end + + if st == :shape + for rng in iter_segments(x, y) + pts = Point2f0[Point2f0(x[i], y[i]) for i in rng] + @show pts + mesh = GeometryTypes.GLNormalMesh(pts) + @show mesh + if !isempty(GeometryTypes.faces(mesh)) + viz = GLVisualize.visualize( + mesh, + color = gl_color(d[:fillcolor], d[:fillalpha]) + ) + GLVisualize.view(viz, sp_screen, camera = camera) + end + end + end end end GLAbstraction.center!(sp_screen, camera) diff --git a/src/colors.jl b/src/colors.jl index a118e38a..cd3fb652 100644 --- a/src/colors.jl +++ b/src/colors.jl @@ -32,6 +32,7 @@ getColor(scheme::ColorScheme) = getColor(scheme, 1) getColorVector(scheme::ColorScheme) = [getColor(scheme)] colorscheme(scheme::ColorScheme) = scheme +colorscheme(s::AbstractString; kw...) = colorscheme(Symbol(s); kw...) colorscheme(s::Symbol; kw...) = haskey(_gradients, s) ? ColorGradient(s; kw...) : ColorWrapper(convertColor(s); kw...) colorscheme{T<:Real}(s::Symbol, vals::AVec{T}; kw...) = ColorGradient(s, vals; kw...) colorscheme(cs::AVec, vs::AVec; kw...) = ColorGradient(cs, vs; kw...) @@ -55,10 +56,10 @@ convertColor(b::Bool) = b ? RGBA(0,0,0,1) : RGBA(0,0,0,0) function convertColor(c, α::Real) c = convertColor(c) - RGBA(RGB(c), α) + RGBA(RGB(getColor(c)), α) end convertColor(cs::AVec, α::Real) = map(c -> convertColor(c, α), cs) -convertColor(c, α::@compat(Void)) = convertColor(c) +convertColor(c, α::Void) = convertColor(c) # backup... try to convert getColor(c) = convertColor(c) @@ -220,7 +221,7 @@ immutable ColorZFunction <: ColorScheme f::Function end -getColorZ(scheme::ColorFunction, z::Real) = scheme.f(z) +getColorZ(scheme::ColorZFunction, z::Real) = scheme.f(z) # -------------------------------------------------------------- @@ -246,6 +247,7 @@ ColorWrapper(s::Symbol; alpha = nothing) = ColorWrapper(convertColor(parse(Color getColor(scheme::ColorWrapper, idx::Int) = scheme.c getColorZ(scheme::ColorWrapper, z::Real) = scheme.c +convertColor(c::ColorWrapper, α::Void) = c.c # -------------------------------------------------------------- diff --git a/src/utils.jl b/src/utils.jl index b8add3d0..1ff21004 100644 --- a/src/utils.jl +++ b/src/utils.jl @@ -166,13 +166,13 @@ end type SegmentsIterator args::Tuple - nextidx::Int + # nextidx::Int n::Int end function iter_segments(args...) tup = Plots.wraptuple(args) n = maximum(map(length, tup)) - SegmentsIterator(tup, 0, n) + SegmentsIterator(tup, n) end # helpers to figure out if there are NaN values in a list of array types @@ -180,10 +180,16 @@ anynan(i::Int, args...) = any(a -> !isfinite(cycle(a,i)), args) anynan(istart::Int, iend::Int, args...) = any(i -> anynan(i, args...), istart:iend) allnan(istart::Int, iend::Int, args...) = all(i -> anynan(i, args...), istart:iend) -Base.start(itr::SegmentsIterator) = (itr.nextidx = 1) #resets -Base.done(itr::SegmentsIterator, unused::Int) = itr.nextidx > itr.n -function Base.next(itr::SegmentsIterator, unused::Int) - i = istart = iend = itr.nextidx +function Base.start(itr::SegmentsIterator) + nextidx = 1 + if anynan(1, itr.args...) + _, nextidx = next(itr, 1) + end + nextidx +end +Base.done(itr::SegmentsIterator, nextidx::Int) = nextidx > itr.n +function Base.next(itr::SegmentsIterator, nextidx::Int) + i = istart = iend = nextidx # find the next NaN, and iend is the one before while i <= itr.n + 1 @@ -195,7 +201,7 @@ function Base.next(itr::SegmentsIterator, unused::Int) i += 1 end - # find the next non-NaN, and set itr.nextidx + # find the next non-NaN, and set nextidx while i <= itr.n if !anynan(i, itr.args...) break @@ -203,8 +209,7 @@ function Base.next(itr::SegmentsIterator, unused::Int) i += 1 end - itr.nextidx = i - istart:iend, 0 + istart:iend, i end # ------------------------------------------------------------------------------------