Skip to content

Commit

Permalink
cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
t-bltg committed Dec 23, 2023
1 parent a7ce767 commit 316040d
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 38 deletions.
105 changes: 68 additions & 37 deletions CairoMakie/src/primitives.jl
Original file line number Diff line number Diff line change
Expand Up @@ -86,21 +86,24 @@ function draw_bezierpath_lines(ctx, bezierpath::BezierPath, scene, color, space,
Cairo.set_source_rgba(ctx, rgbatuple(color)...)
Cairo.set_line_width(ctx, linewidth)
Cairo.stroke(ctx)
nothing
return
end

project_command(m::MoveTo, scene, space, model) =
function project_command(m::MoveTo, scene, space, model)
MoveTo(project_position(scene, space, m.p, model))
end

project_command(l::LineTo, scene, space, model) =
function project_command(l::LineTo, scene, space, model)
LineTo(project_position(scene, space, l.p, model))
end

project_command(c::CurveTo, scene, space, model) =
function project_command(c::CurveTo, scene, space, model)
CurveTo(
project_position(scene, space, c.c1, model),
project_position(scene, space, c.c2, model),
project_position(scene, space, c.p, model),
)
end

project_command(c::ClosePath, scene, space, model) = c

Expand All @@ -127,7 +130,9 @@ function draw_single(primitive::Lines, ctx, positions)
end

function draw_single(primitive::LineSegments, ctx, positions)

@assert iseven(length(positions))

@inbounds for i in 1:2:length(positions)-1
p1 = positions[i]
p2 = positions[i+1]
Expand All @@ -145,12 +150,14 @@ function draw_single(primitive::LineSegments, ctx, positions)
end

# if linewidth is not an array
draw_multi(primitive, ctx, positions, colors::AbstractArray, linewidth, dash) =
function draw_multi(primitive, ctx, positions, colors::AbstractArray, linewidth, dash)
draw_multi(primitive, ctx, positions, colors, [linewidth for c in colors], dash)
end

# if color is not an array
draw_multi(primitive, ctx, positions, color, linewidths::AbstractArray, dash) =
function draw_multi(primitive, ctx, positions, color, linewidths::AbstractArray, dash)
draw_multi(primitive, ctx, positions, [color for l in linewidths], linewidths, dash)
end

function draw_multi(primitive::LineSegments, ctx, positions, colors::AbstractArray, linewidths::AbstractArray, dash)
@assert iseven(length(positions))
Expand All @@ -168,8 +175,9 @@ function draw_multi(primitive::LineSegments, ctx, positions, colors::AbstractArr
Cairo.line_to(ctx, positions[i+1]...)
Cairo.set_line_width(ctx, linewidths[i])

isnothing(dash) || Cairo.set_dash(ctx, dash .* linewidths[i])
c1, c2 = colors[i], colors[i+1]
!isnothing(dash) && Cairo.set_dash(ctx, dash .* linewidths[i])
c1 = colors[i]
c2 = colors[i+1]
# we can avoid the more expensive gradient if the colors are the same
# this happens if one color was given for each segment
if c1 == c2
Expand All @@ -196,8 +204,12 @@ function draw_multi(primitive::Lines, ctx, positions, colors::AbstractArray, lin
prev_nan = isnan(prev_position)
prev_continued = false

# first is not nan, move_to
prev_nan || Cairo.move_to(ctx, positions[begin]...)
if !prev_nan
# first is not nan, move_to
Cairo.move_to(ctx, positions[begin]...)
else
# first is nan, do nothing
end

for i in eachindex(positions)[begin+1:end]
this_position = positions[i]
Expand All @@ -209,7 +221,7 @@ function draw_multi(primitive::Lines, ctx, positions, colors::AbstractArray, lin
if prev_continued
# and this is prev_continued, so set source and stroke to finish previous line
Cairo.set_line_width(ctx, this_linewidth)
isnothing(dash) || Cairo.set_dash(ctx, dash .* this_linewidth)
!isnothing(dash) && Cairo.set_dash(ctx, dash .* this_linewidth)
Cairo.set_source_rgba(ctx, red(prev_color), green(prev_color), blue(prev_color), alpha(prev_color))
Cairo.stroke(ctx)
else
Expand All @@ -218,36 +230,41 @@ function draw_multi(primitive::Lines, ctx, positions, colors::AbstractArray, lin
end
if prev_nan
# previous was nan
if this_nan # this is also nan, do nothing
else # this is not nan, so move to this position
if !this_nan
# but this is not nan, so move to this position
Cairo.move_to(ctx, this_position...)
else
# and this is also nan, do nothing
end
else
if this_color == prev_color
# this color is like the previous
if !this_nan # this is not nan, so line_to and set prev_continued
if !this_nan
# and this is not nan, so line_to and set prev_continued
this_linewidth != prev_linewidth && error("Encountered two different linewidth values $prev_linewidth and $this_linewidth in `lines` at index $(i-1). Different linewidths in one line are only permitted in CairoMakie when separated by a NaN point.")
Cairo.line_to(ctx, this_position...)
prev_continued = true

if i == lastindex(positions)
# this is the last element so stroke this
Cairo.set_line_width(ctx, this_linewidth)
isnothing(dash) || Cairo.set_dash(ctx, dash .* this_linewidth)
!isnothing(dash) && Cairo.set_dash(ctx, dash .* this_linewidth)
Cairo.set_source_rgba(ctx, red(this_color), green(this_color), blue(this_color), alpha(this_color))
Cairo.stroke(ctx)
end
else
# but this is nan, so do nothing
end
else
prev_continued = false
if !this_nan # this is not nan, so line_to and set prev_continued
if !this_nan
this_linewidth != prev_linewidth && error("Encountered two different linewidth values $prev_linewidth and $this_linewidth in `lines` at index $(i-1). Different linewidths in one line are only permitted in CairoMakie when separated by a NaN point.")
# this is not nan
# and this color is different than the previous, so move_to prev and line_to this
# create gradient pattern and stroke
Cairo.move_to(ctx, prev_position...)
Cairo.line_to(ctx, this_position...)
isnothing(dash) || Cairo.set_dash(ctx, dash .* this_linewidth)
!isnothing(dash) && Cairo.set_dash(ctx, dash .* this_linewidth)
Cairo.set_line_width(ctx, this_linewidth)

pat = Cairo.pattern_create_linear(prev_position..., this_position...)
Expand All @@ -258,6 +275,8 @@ function draw_multi(primitive::Lines, ctx, positions, colors::AbstractArray, lin
Cairo.destroy(pat)

Cairo.move_to(ctx, this_position...)
else
# this is nan, do nothing
end
end
end
Expand Down Expand Up @@ -289,8 +308,9 @@ function draw_atomic(scene::Scene, screen::Screen, @nospecialize(primitive::Scat
space = primitive.space[]
transfunc = Makie.transform_func(primitive)

draw_atomic_scatter(scene, ctx, transfunc, colors, markersize, strokecolor, strokewidth, marker,
marker_offset, rotations, model, positions, size_model, font, markerspace, space)
return draw_atomic_scatter(scene, ctx, transfunc, colors, markersize, strokecolor, strokewidth, marker,
marker_offset, rotations, model, positions, size_model, font, markerspace,
space)
end

function draw_atomic_scatter(scene, ctx, transfunc, colors, markersize, strokecolor, strokewidth, marker, marker_offset, rotations, model, positions, size_model, font, markerspace, space)
Expand Down Expand Up @@ -319,7 +339,7 @@ function draw_atomic_scatter(scene, ctx, transfunc, colors, markersize, strokeco
end
Cairo.restore(ctx)
end
nothing
return
end

function draw_marker(ctx, marker::Char, font, pos, scale, strokecolor, strokewidth, marker_offset, rotation)
Expand Down Expand Up @@ -500,6 +520,7 @@ function draw_atomic(scene::Scene, screen::Screen, @nospecialize(primitive::Text
scene, ctx, position, glyph_collection, remove_billboard(rotation),
model, space, markerspace, offset, primitive.transformation, transform_marker
)

nothing
end

Expand Down Expand Up @@ -615,7 +636,7 @@ function draw_glyph_collection(
end

Cairo.restore(ctx)
nothing
return
end

################################################################################
Expand All @@ -630,15 +651,15 @@ If not, returns array unchanged.
function regularly_spaced_array_to_range(arr)
diffs = unique!(sort!(diff(arr)))
step = sum(diffs) ./ length(diffs)
return if all(x-> x step, diffs)
if all(x-> x step, diffs)
m, M = extrema(arr)
if step < zero(step)
m, M = M, m
end
# don't use stop=M, since that may not include M
range(m; step=step, length=length(arr))
return range(m; step=step, length=length(arr))
else
arr
return arr
end
end

Expand All @@ -656,15 +677,19 @@ function draw_atomic(scene::Scene, screen::Screen, @nospecialize(primitive::Unio
ctx = screen.context
image = primitive[3][]
xs, ys = primitive[1][], primitive[2][]
xs = if xs isa AbstractVector
regularly_spaced_array_to_range(xs)
if !(xs isa AbstractVector)
l, r = extrema(xs)
N = size(image, 1)
xs = range(l, r, length = N+1)
else
range(extrema(xs)..., length = size(image, 1) + 1)
xs = regularly_spaced_array_to_range(xs)
end
ys = if ys isa AbstractVector
regularly_spaced_array_to_range(ys)
if !(ys isa AbstractVector)
l, r = extrema(ys)
N = size(image, 2)
ys = range(l, r, length = N+1)
else
range(extrema(ys)..., length = size(image, 2) + 1)
ys = regularly_spaced_array_to_range(ys)
end
model = primitive.model[]::Mat4f
interpolate = to_value(primitive.interpolate)
Expand All @@ -691,8 +716,12 @@ function draw_atomic(scene::Scene, screen::Screen, @nospecialize(primitive::Unio
end

if interpolate
regular_grid || error("$(typeof(primitive).parameters[1]) with interpolate = true with a non-regular grid is not supported right now.")
identity_transform || error("$(typeof(primitive).parameters[1]) with interpolate = true with a non-identity transform is not supported right now.")
if !regular_grid
error("$(typeof(primitive).parameters[1]) with interpolate = true with a non-regular grid is not supported right now.")
end
if !identity_transform
error("$(typeof(primitive).parameters[1]) with interpolate = true with a non-identity transform is not supported right now.")
end
end

imsize = ((first(xs), last(xs)), (first(ys), last(ys)))
Expand Down Expand Up @@ -792,7 +821,7 @@ function draw_atomic(scene::Scene, screen::Screen, @nospecialize(primitive::Maki
end
draw_mesh3D(scene, screen, primitive, mesh)
end
nothing
return nothing
end

function draw_mesh2D(scene, screen, @nospecialize(plot), @nospecialize(mesh))
Expand All @@ -804,7 +833,7 @@ function draw_mesh2D(scene, screen, @nospecialize(plot), @nospecialize(mesh))
cols = per_face_colors(color, nothing, fs, nothing, uv)
space = to_value(get(plot, :space, :data))::Symbol
transform_func = Makie.transform_func(plot)
draw_mesh2D(scene, screen, cols, space, transform_func, vs, fs, model)
return draw_mesh2D(scene, screen, cols, space, transform_func, vs, fs, model)
end

function draw_mesh2D(scene, screen, per_face_cols, space::Symbol, transform_func,
Expand Down Expand Up @@ -841,6 +870,7 @@ function draw_mesh2D(scene, screen, per_face_cols, space::Symbol, transform_func
Cairo.paint(ctx)
Cairo.destroy(pattern)
end
return nothing
end

function average_z(positions, face)
Expand Down Expand Up @@ -956,7 +986,7 @@ function draw_mesh3D(
zorder = filter(i -> any(last.(ns[meshfaces[i]]) .> faceculling), zorder)

draw_pattern(ctx, zorder, shading, meshfaces, ts, per_face_col, ns, vs, lightdirection, light_color, shininess, diffuse, ambient, specular)
nothing
return
end

function _calculate_shaded_vertexcolors(N, v, c, lightdir, light_color, ambient, diffuse, specular, shininess)
Expand Down Expand Up @@ -1044,7 +1074,7 @@ function draw_atomic(scene::Scene, screen::Screen, @nospecialize(primitive::Maki
end
draw_mesh3D(scene, screen, primitive, mesh)
primitive[:color] = old
nothing
return nothing
end


Expand Down Expand Up @@ -1097,5 +1127,6 @@ function draw_atomic(scene::Scene, screen::Screen, @nospecialize(primitive::Maki
rotation = rotation
)
end
nothing

return nothing
end
2 changes: 1 addition & 1 deletion src/basic_recipes/contours.jl
Original file line number Diff line number Diff line change
Expand Up @@ -283,7 +283,7 @@ function plot!(plot::T) where T <: Union{Contour, Contour3d}

bboxes = lift(labels, texts.text; ignore_equal_values=true) do labels, _
labels || return
broadcast(texts.plots[1][1].val, texts.positions.val, texts.rotation.val) do gc, pt, rot
return broadcast(texts.plots[1][1].val, texts.positions.val, texts.rotation.val) do gc, pt, rot
# drop the depth component of the bounding box for 3D
px_pos = project(scene, apply_transform(transform_func(plot), pt, space))
bb = unchecked_boundingbox(gc, to_ndim(Point3f, px_pos, 0f0), to_rotation(rot))
Expand Down

0 comments on commit 316040d

Please sign in to comment.