diff --git a/ext/GridVisualizeUnicodePlotsExt.jl b/ext/GridVisualizeUnicodePlotsExt.jl index 133538a..82d8925 100644 --- a/ext/GridVisualizeUnicodePlotsExt.jl +++ b/ext/GridVisualizeUnicodePlotsExt.jl @@ -61,8 +61,18 @@ function gridplot!(ctx, TP::Type{UnicodePlotsType}, ::Type{Val{2}}, grid) # find bounding box coords = grid[Coordinates] - ex = extrema(view(coords, 1, :)) - ey = extrema(view(coords, 2, :)) + xlimits = ctx[:xlimits] + ylimits = ctx[:ylimits] + if xlimits[1] < xlimits[2] + ex = xlimits + else + ex = extrema(view(coords, 1, :)) + end + if ylimits[1] < ylimits[2] + ey = ylimits + else + ey = extrema(view(coords, 2, :)) + end # line color for interior edges if typeof(ctx[:color]) <: RGB @@ -78,8 +88,7 @@ function gridplot!(ctx, TP::Type{UnicodePlotsType}, ::Type{Val{2}}, grid) # determine resolution (divided by 10, to reduce pixel count in the terminal) layout = ctx[:layout] resolution = ctx[:size] ./ 12 ./ (layout[2], layout[1]) - legend_space = 0 - aspect = ctx[:aspect] * resolution[1] / (resolution[1] + legend_space) + aspect = ctx[:aspect] if (true) # auto scale feature, do we want this? wx = ex[2] - ex[1] @@ -95,15 +104,17 @@ function gridplot!(ctx, TP::Type{UnicodePlotsType}, ::Type{Val{2}}, grid) # we need an integer resolution resolution = @. Int(round(resolution)) + # ensure that legend fits + ncellregions = num_cellregions(grid) + nbregions = num_bfaceregions(grid) + resolution = (resolution[1], max(resolution[2], 5 + ncellregions + nbregions)) + # create UnicodePlots.Canvas - padding = 0 #0.1 * max(ex[2] - ex[1], ey[2] - ey[1]) - ex = (ex[1] - 2 * padding, ex[2] + 0.5 * padding) - ey = (ey[1] - padding, ey[2] + padding) CanvasType = UnicodePlots.BrailleCanvas # should this be a changeable parameter ? canvas = CanvasType( - resolution[2], resolution[1] + legend_space, # number of rows and columns (characters) + resolution[2], resolution[1], # number of rows and columns (characters) origin_y = ey[1], origin_x = ex[1], # position in virtual space - height = (ey[2] - ey[1]) / (resolution[1] / (resolution[1] + legend_space)), width = ex[2] - ex[1]; blend = false + height = (ey[2] - ey[1]), width = ex[2] - ex[1]; blend = false ) ## plot all edges in the grid @@ -140,7 +151,6 @@ function gridplot!(ctx, TP::Type{UnicodePlotsType}, ::Type{Val{2}}, grid) # color cell midpoints with cell regions color cellregions = grid[CellRegions] - ncellregions = num_cellregions(grid) cmap = region_cmap(max(2, ncellregions)) ctx[:cmap] = cmap colors = [ @@ -170,7 +180,6 @@ function gridplot!(ctx, TP::Type{UnicodePlotsType}, ::Type{Val{2}}, grid) end # plot boundary faces with bregion_cmap colors - nbregions = num_bfaceregions(grid) bcmap = bregion_cmap(nbregions) ctx[:bcmap] = bcmap bcolors = [ @@ -201,8 +210,6 @@ function gridplot!(ctx, TP::Type{UnicodePlotsType}, ::Type{Val{2}}, grid) region_legend!(plt, "regions", y0 + 3, bcolors) # corner coordinates - ex = extrema(view(coords, 1, :)) - ey = extrema(view(coords, 2, :)) UnicodePlots.label!(plt, :bl, string(Float16(ex[1])), UnicodePlots.ansi_color(UnicodePlots.BORDER_COLOR[])) UnicodePlots.label!(plt, :b, "x") UnicodePlots.label!(plt, :br, string(Float16(ex[2])), UnicodePlots.ansi_color(UnicodePlots.BORDER_COLOR[])) @@ -218,11 +225,15 @@ end function gridplot!(ctx, TP::Type{UnicodePlotsType}, ::Type{Val{1}}, grid) UnicodePlots = ctx[:Plotter] - text_color = UnicodePlots.ansi_color(:normal) # find bounding box + xlimits = ctx[:xlimits] coords = grid[Coordinates] - ex = extrema(view(coords, 1, :)) + if xlimits[1] < xlimits[2] + ex = xlimits + else + ex = extrema(view(coords, 1, :)) + end # line color for interior edges if typeof(ctx[:color]) <: RGB @@ -242,12 +253,9 @@ function gridplot!(ctx, TP::Type{UnicodePlotsType}, ::Type{Val{1}}, grid) resolution = (Int(round(ctx[:size][1] / 6 / layout[2])), max(7, 5 + ncellregions + nbregions)) # create UnicodePlots.Canvas - legend_space = 0 #5 - padding = 0 #0.05 * (ex[2] - ex[1]) - ex = (ex[1] - padding, ex[2] + padding) CanvasType = UnicodePlots.BrailleCanvas # should this be a changeable parameter ? canvas = CanvasType( - resolution[2], resolution[1] + legend_space, # number of rows and columns (characters) + resolution[2], resolution[1], # number of rows and columns (characters) origin_y = 0, origin_x = ex[1], # position in virtual space height = 1, width = ex[2] - ex[1]; blend = false ) @@ -267,6 +275,12 @@ function gridplot!(ctx, TP::Type{UnicodePlotsType}, ::Type{Val{1}}, grid) cellgeoms = grid[CellGeometries] ncells = num_cells(grid) nnodes = num_nodes(grid) + text_color = UnicodePlots.ansi_color(:normal) + if nnodes < resolution[1] / 2 + for j in 1:nnodes + UnicodePlots.annotate!(canvas, coords[1, j], 0.5, "•", text_color, false) + end + end for j in 1:ncells cen = local_celledgenodes(cellgeoms[j]) r = cellregions[j] @@ -279,9 +293,6 @@ function gridplot!(ctx, TP::Type{UnicodePlotsType}, ::Type{Val{1}}, grid) ) end end - for j in 1:nnodes - UnicodePlots.annotate!(canvas, coords[1, j], 0.5, "•", text_color, false) - end # plot boundary nodes with bregion_cmap colors bcmap = bregion_cmap(nbregions) @@ -299,7 +310,7 @@ function gridplot!(ctx, TP::Type{UnicodePlotsType}, ::Type{Val{1}}, grid) for j in 1:nbfaces red, green, blue = UInt32.(bcolors[bfaceregions[j]]) uint_color = (red << 16) | (green << 8) | blue - UnicodePlots.annotate!(canvas, coords[1, bfacenodes[1, j]], 0.5, "•", UInt32(uint_color), false) + UnicodePlots.annotate!(canvas, coords[1, bfacenodes[1, j]], 0.5, "•", uint_color, false) end plt = UnicodePlots.Plot(canvas; title = ctx[:title], border = ctx[:border]) @@ -311,7 +322,6 @@ function gridplot!(ctx, TP::Type{UnicodePlotsType}, ::Type{Val{1}}, grid) # corner coordinates - ex = extrema(view(coords, 1, :)) UnicodePlots.label!(plt, :bl, string(Float16(ex[1])), UnicodePlots.ansi_color(UnicodePlots.BORDER_COLOR[])) UnicodePlots.label!(plt, :b, "x") UnicodePlots.label!(plt, :br, string(Float16(ex[2])), UnicodePlots.ansi_color(UnicodePlots.BORDER_COLOR[])) @@ -364,7 +374,7 @@ function scalarplot!( yscale == :log && (yscale = :log10) yscale == :symlog && (yscale = x -> sign(x) * (log10(1 + abs(x)))) - color = UnicodePlots.ansi_color(Symbol(ctx[:color])) + color = UnicodePlots.ansi_color(ctx[:color]) for ifunc in 1:nfuncs func = funcs[ifunc] @@ -479,10 +489,19 @@ function vectorplot!(ctx, TP::Type{UnicodePlotsType}, ::Type{Val{2}}, grid, func # find bounding box coords = grid[Coordinates] - ex = extrema(view(coords, 1, :)) - ey = extrema(view(coords, 2, :)) - - aspect = ctx[:aspect] * resolution[1] / resolution[1] + xlimits = ctx[:xlimits] + ylimits = ctx[:ylimits] + if xlimits[1] < xlimits[2] + ex = xlimits + else + ex = extrema(view(coords, 1, :)) + end + if ylimits[1] < ylimits[2] + ey = ylimits + else + ey = extrema(view(coords, 2, :)) + end + aspect = ctx[:aspect] if (true) # auto scale feature, do we want this? wx = ex[2] - ex[1] @@ -497,9 +516,10 @@ function vectorplot!(ctx, TP::Type{UnicodePlotsType}, ::Type{Val{2}}, grid, func # we need an integer resolution resolution = @. Int(round(resolution)) + @info resolution # query vector field raster points - rc, rv = vectorsample(grid, func; gridscale = ctx[:gridscale], rasterpoints = ((resolution[1] - 1) / 2, 2 * (resolution[2] - 1)), offset = ctx[:offset]) + rc, rv = vectorsample(grid, func; gridscale = ctx[:gridscale], rasterpoints = ((resolution[1] - 1) / 2, resolution[2] - 1), offset = ctx[:offset], xlimits = ex, ylimits = ey) qc, qv = quiverdata(rc, rv; vscale = ctx[:vscale], vnormalize = ctx[:vnormalize], vconstant = ctx[:vconstant]) # construct canvas @@ -511,7 +531,6 @@ function vectorplot!(ctx, TP::Type{UnicodePlotsType}, ::Type{Val{2}}, grid, func ) # plot arrows - scale = minimum(resolution) / maximum(ctx[:rasterpoints]) / 300 narrows = size(qv, 2) vscale = ctx[:vscale] # vscale steers arrow thickness if vscale <= 0.25 diff --git a/src/common.jl b/src/common.jl index 4bfa21c..0d0a949 100644 --- a/src/common.jl +++ b/src/common.jl @@ -257,7 +257,7 @@ function vectorsample( tol = reltol * extent # point spacing - spacing = [extent / rasterpoints[i] for i in 1:dim] + spacing = [(cminmax[i][2] - cminmax[i][1]) / rasterpoints[i] for i in 1:dim] # index range ijkmax = ones(Int, 3)