-
-
Notifications
You must be signed in to change notification settings - Fork 290
/
overrides.jl
216 lines (181 loc) · 8.3 KB
/
overrides.jl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
################################################################################
# Poly - the not so primitive, primitive #
################################################################################
"""
Special method for polys so we don't fall back to atomic meshes, which are much more
complex and slower to draw than standard paths with single color.
"""
function draw_plot(scene::Scene, screen::Screen, poly::Poly)
# dispatch on input arguments to poly to use smarter drawing methods than
# meshes if possible
draw_poly(scene, screen, poly, to_value.(poly.input_args)...)
end
"""
Fallback method for args without special treatment.
"""
function draw_poly(scene::Scene, screen::Screen, poly, args...)
draw_poly_as_mesh(scene, screen, poly)
end
function draw_poly_as_mesh(scene, screen, poly)
draw_plot(scene, screen, poly.plots[1])
draw_plot(scene, screen, poly.plots[2])
end
# in the rare case of per-vertex colors redirect to mesh drawing
function draw_poly(scene::Scene, screen::Screen, poly, points::Vector{<:Point2}, color::AbstractArray, model, strokecolor, strokewidth)
draw_poly_as_mesh(scene, screen, poly)
end
function draw_poly(scene::Scene, screen::Screen, poly, points::Vector{<:Point2})
color = to_cairo_color(poly.color[], poly)
strokecolor = to_cairo_color(poly.strokecolor[], poly)
draw_poly(scene, screen, poly, points, color, poly.model[], strokecolor, poly.strokewidth[])
end
# when color is a Makie.AbstractPattern, we don't need to go to Mesh
function draw_poly(scene::Scene, screen::Screen, poly, points::Vector{<:Point2}, color::Union{Colorant, Cairo.CairoPattern},
model, strokecolor, strokewidth)
space = to_value(get(poly, :space, :data))
points = project_position.(Ref(scene), space, points, Ref(model))
Cairo.move_to(screen.context, points[1]...)
for p in points[2:end]
Cairo.line_to(screen.context, p...)
end
Cairo.close_path(screen.context)
set_source(screen.context, color)
Cairo.fill_preserve(screen.context)
Cairo.set_source_rgba(screen.context, rgbatuple(to_color(strokecolor))...)
Cairo.set_line_width(screen.context, strokewidth)
Cairo.stroke(screen.context)
end
function draw_poly(scene::Scene, screen::Screen, poly, points_list::Vector{<:Vector{<:Point2}})
color = to_cairo_color(poly.color[], poly)
strokecolor = to_cairo_color(poly.strokecolor[], poly)
broadcast_foreach(points_list, color,
strokecolor, poly.strokewidth[], Ref(poly.model[])) do points, color, strokecolor, strokewidth, model
draw_poly(scene, screen, poly, points, color, model, strokecolor, strokewidth)
end
end
draw_poly(scene::Scene, screen::Screen, poly, rect::Rect2) = draw_poly(scene, screen, poly, [rect])
function draw_poly(scene::Scene, screen::Screen, poly, rects::Vector{<:Rect2})
model = poly.model[]
space = to_value(get(poly, :space, :data))
projected_rects = project_rect.(Ref(scene), space, rects, Ref(model))
color = to_cairo_color(poly.color[], poly)
strokecolor = to_cairo_color(poly.strokecolor[], poly)
broadcast_foreach(projected_rects, color, strokecolor, poly.strokewidth[]) do r, c, sc, sw
Cairo.rectangle(screen.context, origin(r)..., widths(r)...)
set_source(screen.context, c)
Cairo.fill_preserve(screen.context)
set_source(screen.context, sc)
Cairo.set_line_width(screen.context, sw)
Cairo.stroke(screen.context)
end
end
function polypath(ctx, polygon)
ext = decompose(Point2f, polygon.exterior)
Cairo.set_fill_type(ctx, Cairo.CAIRO_FILL_RULE_EVEN_ODD)
Cairo.move_to(ctx, ext[1]...)
for point in ext[2:end]
Cairo.line_to(ctx, point...)
end
Cairo.close_path(ctx)
interiors = decompose.(Point2f, polygon.interiors)
for interior in interiors
# Cairo needs to have interiors counter clockwise
n = length(interior)
Cairo.move_to(ctx, interior[1]...)
for idx in 2:n
point = interior[idx]
Cairo.line_to(ctx, point...)
end
Cairo.close_path(ctx)
end
end
draw_poly(scene::Scene, screen::Screen, poly, polygon::Polygon) = draw_poly(scene, screen, poly, [polygon])
draw_poly(scene::Scene, screen::Screen, poly, circle::Circle) = draw_poly(scene, screen, poly, decompose(Point2f, circle))
function draw_poly(scene::Scene, screen::Screen, poly, polygons::AbstractArray{<:Polygon})
model = poly.model[]
space = to_value(get(poly, :space, :data))
projected_polys = project_polygon.(Ref(scene), space, polygons, Ref(model))
color = to_cairo_color(poly.color[], poly)
strokecolor = to_cairo_color(poly.strokecolor[], poly)
broadcast_foreach(projected_polys, color, strokecolor, poly.strokewidth[]) do po, c, sc, sw
polypath(screen.context, po)
set_source(screen.context, c)
Cairo.fill_preserve(screen.context)
set_source(screen.context, sc)
Cairo.set_line_width(screen.context, sw)
Cairo.stroke(screen.context)
end
end
function draw_poly(scene::Scene, screen::Screen, poly, polygons::AbstractArray{<: MultiPolygon})
model = poly.model[]
space = to_value(get(poly, :space, :data))
projected_polys = project_multipolygon.(Ref(scene), space, polygons, Ref(model))
color = to_cairo_color(poly.color[], poly)
strokecolor = to_cairo_color(poly.strokecolor[], poly)
broadcast_foreach(projected_polys, color, strokecolor, poly.strokewidth[]) do mpo, c, sc, sw
for po in mpo.polygons
polypath(screen.context, po)
set_source(screen.context, c)
Cairo.fill_preserve(screen.context)
set_source(screen.context, sc)
Cairo.set_line_width(screen.context, sw)
Cairo.stroke(screen.context)
end
end
end
################################################################################
# Band #
# Override because band is usually a polygon, but because it supports #
# gradients as well via `mesh` we have to intercept the poly use #
################################################################################
function draw_plot(scene::Scene, screen::Screen,
band::Band{<:Tuple{<:AbstractVector{<:Point2},<:AbstractVector{<:Point2}}})
if !(band.color[] isa AbstractArray)
color = to_cairo_color(band.color[], band)
upperpoints = band[1][]
lowerpoints = band[2][]
points = vcat(lowerpoints, reverse(upperpoints))
model = band.model[]
space = to_value(get(band, :space, :data))
points = project_position.(Ref(scene), space, points, Ref(model))
Cairo.move_to(screen.context, points[1]...)
for p in points[2:end]
Cairo.line_to(screen.context, p...)
end
Cairo.close_path(screen.context)
set_source(screen.context, color)
Cairo.fill(screen.context)
else
for p in band.plots
draw_plot(scene, screen, p)
end
end
nothing
end
#################################################################################
# Tricontourf #
# Tricontourf creates many disjoint polygons that are adjacent and form contour #
# bands, however, at the gaps we see white antialiasing artifacts. Therefore #
# we override behavior and draw each band in one go #
#################################################################################
function draw_plot(scene::Scene, screen::Screen, tric::Tricontourf)
pol = only(tric.plots)::Poly
colornumbers = pol.color[]
colors = to_cairo_color(colornumbers, pol)
polygons = pol[1][]
model = pol.model[]
space = to_value(get(pol, :space, :data))
projected_polys = project_polygon.(Ref(scene), space, polygons, Ref(model))
function draw_tripolys(polys, colornumbers, colors)
for (i, (pol, colnum, col)) in enumerate(zip(polys, colornumbers, colors))
polypath(screen.context, pol)
if i == length(colornumbers) || colnum != colornumbers[i+1]
set_source(screen.context, col)
Cairo.fill(screen.context)
end
end
return
end
draw_tripolys(projected_polys, colornumbers, colors)
return
end