Skip to content

Commit

Permalink
Clean up proj.fill code
Browse files Browse the repository at this point in the history
  • Loading branch information
hughjonesd committed Feb 15, 2024
1 parent e283b84 commit acb4af9
Show file tree
Hide file tree
Showing 13 changed files with 35 additions and 26 deletions.
27 changes: 1 addition & 26 deletions R/geom-magnify.R
Original file line number Diff line number Diff line change
Expand Up @@ -383,32 +383,7 @@ GeomMagnify <- ggproto("GeomMagnify", Geom,
proj_fill_grob <- if (is.null(proj.fill) || identical(proj, "single")) {
NULL
} else {
line_pair_idx <- seq(1L, nrow(proj_df), by = 2L)
# each 2 rows of proj_df define a pair of matched projection lines
proj_fill_grobs <- lapply(line_pair_idx,
function (idx) {
# the two lines go in opposite directions (would be nice to clarify
# or remove this brittle long-range dependency)
# this lets us easily define a polygon
x <- c(proj_df$x[idx], proj_df$xend[idx],
proj_df$xend[idx + 1], proj_df$x[idx + 1])
y <- c(proj_df$y[idx], proj_df$yend[idx],
proj_df$yend[idx + 1], proj_df$y[idx + 1])

proj_fill_grob <- polygonGrob(x = x, y = y,
default.units = "native")
proj_fill_grob <- gridGeometry::polyclipGrob(proj_fill_grob, target_grob,
op = "minus",
gp = gpar(
col = "transparent",
fill = proj.fill,
lty = proj.linetype,
lwd = linewidth * .pt))
proj_fill_grob
})
grob_glist <- do.call(gList, proj_fill_grobs)
gTree(children = grob_glist)
# TODO check with grobs, maps etc
make_proj_fill_grob(proj_df, target_grob, proj.fill)
}

if (shadow) {
Expand Down
34 changes: 34 additions & 0 deletions R/projection.R
Original file line number Diff line number Diff line change
Expand Up @@ -274,3 +274,37 @@ calculate_proj_df_rect <- function(proj, data, corners, coord, panel_params) {
df <- data.frame(x = proj_x, y = proj_y, xend = proj_xend, yend = proj_yend)
coord$transform(df, panel_params)
}


#' Creates a grob to fill between the projection lines
#'
#' @param proj_df Data frame returned from calculate_proj_df_*.
#' This should have n pairs of matched projection lines,
#' both going in the same direction (i.e. from target to inset or vice versa)
#' @param target_grob From [draw_panel()].
#' @param fill The fill colour
#'
#' @return The grob, excluding the target grob.
#' @noRd
make_proj_fill_grob <- function (proj_df, target_grob, fill) {
line_pair_idx <- seq(1L, nrow(proj_df), by = 2L)

proj_fill_grobs <- lapply(line_pair_idx,
function (idx) {
x <- c(proj_df$x[idx],
proj_df$xend[idx],
proj_df$xend[idx + 1],
proj_df$x[idx + 1])
y <- c(proj_df$y[idx],
proj_df$yend[idx],
proj_df$yend[idx + 1],
proj_df$y[idx + 1])

unclipped_grob <- polygonGrob(x = x, y = y, default.units = "native")
gridGeometry::polyclipGrob(unclipped_grob, target_grob, op = "minus",
gp = gpar(col = "transparent", fill = fill))
})

proj_fill_grobs <- do.call(gList, proj_fill_grobs)
gTree(children = proj_fill_grobs)
}
Binary file modified tests/testthat/_snaps/basics/test-basics-clipping-ellipse.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified tests/testthat/_snaps/sf/test-sf-ellipse-with-from-aes.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified tests/testthat/_snaps/sf/test-sf-ellipse-with-from-list.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified tests/testthat/_snaps/sf/test-sf-multiple-outline-ellipse.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified tests/testthat/_snaps/sf/test-sf-multiple-outline.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified tests/testthat/test-sf-ellipse-with-from-aes.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified tests/testthat/test-sf-ellipse-with-from-list.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified tests/testthat/test-sf-multiple-outline-ellipse.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified tests/testthat/test-sf-multiple-outline-multiple-proj.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified tests/testthat/test-sf-multiple-outline.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit acb4af9

Please sign in to comment.