1
handle_xy <- function(xy) {
2 1
  if (is.list(xy) && length(xy) == 2L) xy <- stats::setNames(xy, c("x", "y"))
3 1
  if (is.list(xy)) xy <- do.call(cbind, xy)
4 1
  if (!dim(xy)[2L] == 2L) warning("expected two columns, x/y")
5 1
  if (dim(xy)[2L] < 2L) stop("expected two columns, x/y")
6 1
  if (dim(xy)[1L] < 3L) warning("fewer than 3 coordinates supplied")
7 1
  xy
8
}
9

10
#' Constrained polygon triangulation
11
#'
12
#' Produce a triangulation index into x,y coordinates of a polygon
13
#' that may include holes. Holes are specified by input argument `holes`
14
#' which marks the starting index of each hole, if any.
15
#'
16
#' Ear cutting (or ear clipping) applies constrained triangulation by
17
#' successively 'cutting' triangles from a polygon defined by path/s. Holes are supported, the earcut library works with
18
#' single-island-with-holes polygons, analogous to the POLYGON type in simple features.
19
#'
20
#' To understand the specification of holes, see the examples with comment
21
#' starting "1) Notice how the hole begins ..." in relation to the example code.
22
#' @param xy xy-coordinates, either a list, matrix, or data frame
23
#' @param holes index of starting position of each hole in x,y, leave set to `0` if no holes
24
#' @param ... unused
25
#' @return integer vector of triangle index, in sets of three
26
#' @export
27
#' @seealso plot_ears
28
#' @examples
29
#' ## single ring polygon
30
#' x <- c(0, 0, 0.75, 1, 0.5, 0.8, 0.69)
31
#' y <- c(0, 1, 1, 0.8, 0.7, 0.6, 0)
32
#' (ind <- earcut(cbind(x, y)))
33
#' plot_ears(cbind(x, y), ind)
34
#'
35
#' ## polygon with a hole
36
#' x <- c(0, 0, 0.75, 1, 0.5, 0.8, 0.69,
37
#'      0.2, 0.5, 0.5, 0.3, 0.2)
38
#' y <- c(0, 1, 1, 0.8, 0.7, 0.6, 0,
39
#'      0.2, 0.2, 0.4, 0.6, 0.4)
40
#' ind <- earcut(cbind(x, y), holes = 8)
41
#' plot_ears(cbind(x, y), ind)
42
#'
43
#' ## 1) Notice how the hole begins at index 8,
44
#' ## hence holes = 8 above, and holes = c(8, 13) below
45
#' plot_ears(cbind(x, y), ind, col = "grey", border = NA)
46
#' text(x, y, labels = seq_along(x), pos = 2)
47
#'
48
#' ## add another hole
49
#' x <- c(0, 0, 0.75, 1, 0.5, 0.8, 0.69,
50
#'      0.2, 0.5, 0.5, 0.3, 0.2,
51
#'       0.15, 0.23, 0.2)
52
#' y <- c(0, 1, 1, 0.8, 0.7, 0.6, 0,
53
#'      0.2, 0.2, 0.4, 0.6, 0.4,
54
#'       0.65, 0.65, 0.81)
55
#' ind <- earcut(cbind(x, y), holes = c(8, 13))
56
#' plot_ears(cbind(x, y), ind, col = "grey")
57

58
#' # simpler shape with more than one hole
59
#' # the two inside holes are open to each other
60
#' # (so we can use the same data for one hole or two)
61
#' x <- c(0, 0, 1, 1,
62
#'        0.4, 0.2, 0.2, 0.4,
63
#'        0.6, 0.8, 0.8, 0.6
64
#' )
65
#' y <- c(0, 1, 1, 0,
66
#'        0.2, 0.2, 0.4, 0.4,
67
#'        0.6, 0.6, 0.4, 0.4
68
#' )
69
#' ind <- decido::earcut(cbind(x, y), holes = c(5, 9))
70
#' plot_ears(cbind(x, y), ind, col = "grey")
71
#' plot_holes(cbind(x, y), holes = c(5, 9), col = "grey")
72
#' ind <- decido::earcut(cbind(x, y), holes = 5)
73
#' plot_ears(cbind(x, y), ind, col = "grey")
74
#' plot_holes(cbind(x, y), holes = 5, col = "grey")
75
earcut <- function(xy, holes = 0, ...) {
76 1
 UseMethod("earcut")
77
}
78
#' @name earcut
79
#' @export
80
earcut.default <- function(xy, holes = 0L, ...) {
81 1
  xy <- handle_xy(xy)
82 1
  x <- xy[ ,1L]
83 1
  y <- xy[ ,2L]
84
  ## convert holes to C++ 0-based
85 1
  if (any(holes < 0)) stop("'holes' must be zero, or a vector of positive vaues")
86 1
  if (any(holes < 1) && length(holes) > 1) stop("cannot mix index 0 with non-zero for 'holes'")
87 1
  if (any(holes < 4) && length(holes) > 1) stop("no hole can begin before element 4")
88 1
  if (any(holes > (length(x) - 2))) stop("no hole can begin later than 3 elements from the end")
89 1
  nholes <- length(holes)
90 1
  if (holes[1] == 0) {
91
    ## a nonsense situation, so we reset to be sure
92 1
    nholes <- 0L
93 1
    holes <- 0L
94
  }
95 1
  earcut_cpp(x, y, holes = as.integer(holes - 1), numholes = as.integer(nholes)) + 1L
96
}
97
#' Plot ears or polygons
98
#'
99
#' Plot the triangles produced by [earcut], or plot the polygon paths
100
#' using the same interface as earcut uses. This allows for easy
101
#' comparison and checking of what the results should be.
102
#'
103
#' For both functions the first input is
104
#' a matrix of x,y coordinates.
105
#'
106
#' For [plot_ears] the second input is
107
#' the index output of earcut. The index is treated in sets of 3 values, with
108
#' individual calls to [polypath] to draw a polygon for each triangle.
109
#'
110
#' For [plot_holes] the second input is the `holes` argument that would
111
#' be used for earcut. This is used to split the coordinates at these positions,
112
#' inserting `NA` values as per the mechanism used by [graphics::polypath] to
113
#' break coordinates into separate polygon rings. (There's no winding rule here
114
#' plot_rules is hard-coded to always use the evenodd rule, so that winding
115
#' order may be ignored).
116
#' @param xy xy-coordinates, either a list, matrix, or data frame
117
#' @param idx index of triangles
118
#' @param holes index of starting position of holes (see [earcut])
119
#' @param add add to current plot, or create a new
120
#' @param ... arguments to polypath
121
#' @export
122
#' @importFrom graphics plot polypath
123
#' @seealso earcut
124
#' @examples
125
#' ## after ?polypath
126
#' x <- cbind(c(.1, .1, .9, .9,   .2, .2, .8, .8),
127
#'            c(.1, .9, .9, .1,   .2, .8, .8, .2))
128
#' plot_holes(x, holes = 5, col = "grey")
129
plot_ears <- function(xy, idx, add = FALSE, ...) {
130 1
  xy <- handle_xy(xy)
131 1
  if (!add) plot(xy, asp = 1)
132 1
  apply(matrix(idx, 3), 2, function(i) polypath(xy[i, ], ...))
133 1
  invisible(NULL)
134
}
135

136
#' @name plot_ears
137
#' @export
138
plot_holes <- function(xy, holes = 0, add = FALSE, ...) {
139 1
  xy <- handle_xy(xy)
140 1
  if (holes[1] > 0) {
141 1
    g <- c(0, cumsum(abs(diff(seq_len(nrow(xy)) %in% holes))))
142 1
    g[holes] <- g[holes] + 1
143 1
    g <- as.integer(factor(g))
144 1
    xy <- utils::head(do.call(rbind, lapply(split(as.data.frame(xy), g), function(itab) rbind(itab, NA))), -1L)
145

146
  }
147

148 1
  if (!add) plot(xy, type = "n", asp = 1)
149 1
  polypath(xy, rule = "evenodd", ...)
150 1
  invisible(NULL)
151
}

Read our documentation on viewing source code .

Loading