|
4 | 4 | #' @aliases transform scale rotate translation flip flop mirror sequence |
5 | 5 | #' |
6 | 6 | #' @param x \code{SpatialData} element. |
| 7 | +#' @param i scalar integer or string; target coordinate space. |
7 | 8 | #' @param t transformation data; exceptions: for \code{mirror}, controls |
8 | 9 | #' whether to perform \bold{v}ertical or \bold{h}orizontal reflection; |
9 | 10 | #' no data is needed for \code{flip} (\bold{v}) and \code{flop} (\bold{h}). |
10 | 11 | #' @param k scalar index specifying which scale to use; |
11 | 12 | #' \code{Inf} to use lowest available resolution; |
12 | 13 | #' only applies to \code{SpatialDataArray}s (images, labels). |
13 | 14 | #' @param ... option arguments passed to and from other methods. |
| 15 | +#' @param rev flag; should transformation(s) be reversed? |
14 | 16 | #' |
15 | 17 | #' @returns \code{SpatialData} element with transformation(s) applied. |
16 | 18 | #' |
|
48 | 50 |
|
49 | 51 | #' @export |
50 | 52 | #' @rdname trans |
51 | | -setMethod("transform", c("SpatialDataElement", "missing"), \(x, i, ...) transform(x, 1, ...)) |
| 53 | +setMethod("transform", |
| 54 | + c("SpatialDataElement", "missing"), |
| 55 | + \(x, i, ...) transform(x, 1, ...)) |
52 | 56 |
|
53 | 57 | #' @export |
54 | 58 | #' @rdname trans |
55 | | -setMethod("transform", c("SpatialDataElement", "numeric"), \(x, i, ...) transform(x, CTname(x)[i], ...)) |
| 59 | +setMethod("transform", |
| 60 | + c("SpatialDataElement", "numeric"), |
| 61 | + \(x, i, ...) transform(x, CTname(x)[i], ...)) |
56 | 62 |
|
57 | 63 | #' @export |
58 | 64 | #' @rdname trans |
@@ -100,30 +106,26 @@ setMethod("flip", "SpatialDataArray", \(x, k=1, ...) .mirror(x, -90, k)) |
100 | 106 | #' @rdname trans |
101 | 107 | setMethod("flop", "SpatialDataArray", \(x, k=1, ...) .mirror(x, 90, k)) |
102 | 108 |
|
103 | | -#' @importFrom methods as |
104 | | -#' @importFrom S4Vectors metadata<- |
105 | | -.trans_a. <- \(x, f, k=1) { |
106 | | - a <- f(aperm(as.array(data(x, k)))) |
107 | | - metadata(x)$data_type <- data_type(x) |
108 | | - data(x) <- list(as(aperm(a), "SparseArray")) |
109 | | - return(x) |
110 | | -} |
111 | | - |
112 | 109 | # rotation matrix to rotate points counter-clockwise through an angle 't' |
113 | 110 | .R <- \(t) matrix(c(cos(t), -sin(t), sin(t), cos(t)), 2, 2) |
114 | 111 |
|
115 | 112 | #' @export |
116 | 113 | #' @rdname trans |
| 114 | +#' @importFrom methods as |
117 | 115 | #' @importFrom EBImage rotate |
| 116 | +#' @importFrom S4Vectors metadata<- |
118 | 117 | setMethod("rotate", c("SpatialDataArray", "numeric"), \(x, t, k=1, ..., rev=FALSE) { |
119 | 118 | # negate angle since 'EBImage' rotates clockwise |
120 | 119 | stopifnot(length(t) == 1, is.finite(t)) |
121 | 120 | if (t %% 360 == 0) return(x) |
122 | 121 | if (rev) t <- -t |
123 | | - f <- \(.) EBImage::rotate(., -t) |
124 | 122 | if (length(d <- dim(data(x, k))) == 3) d <- d[-1] |
125 | 123 | metadata(x)$wh <- lapply(rev(d), \(.) c(c(0, .) %*% .R(t*pi/180))) |
126 | | - .trans_a.(x, f, k) |
| 124 | + f <- \(.) EBImage::rotate(., -t) |
| 125 | + a <- f(aperm(as.array(data(x, k)))) |
| 126 | + metadata(x)$data_type <- data_type(x) |
| 127 | + data(x) <- list(as(aperm(a), "SparseArray")) |
| 128 | + return(x) |
127 | 129 | }) |
128 | 130 |
|
129 | 131 | .trans_a <- \(x, t, f=c("scale", "translation"), k=1, rev=FALSE) { |
@@ -153,25 +155,30 @@ setMethod("rotate", c("SpatialDataArray", "numeric"), \(x, t, k=1, ..., rev=FALS |
153 | 155 |
|
154 | 156 | #' @export |
155 | 157 | #' @rdname trans |
156 | | -setMethod("scale", c("SpatialDataArray", "numeric"), \(x, t, ...) .trans_a(x, t, "scale", ...)) |
| 158 | +setMethod("scale", |
| 159 | + c("SpatialDataArray", "numeric"), |
| 160 | + \(x, t, ...) .trans_a(x, t, "scale", ...)) |
157 | 161 |
|
158 | 162 | #' @export |
159 | 163 | #' @rdname trans |
160 | | -setMethod("translation", c("SpatialDataArray", "numeric"), \(x, t, ...) .trans_a(x, t, "translation", ...)) |
| 164 | +setMethod("translation", |
| 165 | + c("SpatialDataArray", "numeric"), |
| 166 | + \(x, t, ...) .trans_a(x, t, "translation", ...)) |
161 | 167 |
|
162 | 168 | # point/shape ---- |
163 | 169 |
|
164 | 170 | #' @importFrom dplyr mutate |
165 | 171 | #' @importFrom rlang call2 !! |
166 | 172 | .trans_f <- \(x, t, f=c("scale", "rotate", "translation"), rev=FALSE) { |
| 173 | + ST_Scale <- ST_Rotate <- ST_Translate <- radius <- NULL # R CMD check |
| 174 | + |
167 | 175 | f <- match.arg(f) |
168 | 176 | n <- length(axes(x)) |
169 | | - ST_Scale <- ST_Rotate <- ST_Translate <- NULL # R CMD check |
170 | 177 |
|
171 | 178 | # setup: length, identity, function |
172 | 179 | map <- list( |
173 | | - len=c(scale=n, translation=n, rotate=1), |
174 | | - ids=c(scale=1, translation=0, rotate=0), |
| 180 | + len=c(scale=n, rotate=1, translation=n), |
| 181 | + ids=c(scale=1, rotate=0, translation=0), |
175 | 182 | fns=c(scale="ST_Scale", rotate="ST_Rotate", translation="ST_Translate")) |
176 | 183 |
|
177 | 184 | # validation |
@@ -202,12 +209,17 @@ setMethod("translation", c("SpatialDataArray", "numeric"), \(x, t, ...) .trans_a |
202 | 209 | #' @export |
203 | 210 | #' @rdname trans |
204 | 211 | setMethod("rotate", |
205 | | - c("SpatialDataFrame", "numeric"), \(x, t, ...) .trans_f(x, t, "rotate", ...)) |
| 212 | + c("SpatialDataFrame", "numeric"), |
| 213 | + \(x, t, ...) .trans_f(x, t, "rotate", ...)) |
206 | 214 |
|
207 | 215 | #' @export |
208 | 216 | #' @rdname trans |
209 | | -setMethod("scale", c("SpatialDataFrame", "numeric"), \(x, t, ...) .trans_f(x, t, "scale", ...)) |
| 217 | +setMethod("scale", |
| 218 | + c("SpatialDataFrame", "numeric"), |
| 219 | + \(x, t, ...) .trans_f(x, t, "scale", ...)) |
210 | 220 |
|
211 | 221 | #' @export |
212 | 222 | #' @rdname trans |
213 | | -setMethod("translation", c("SpatialDataFrame", "numeric"), \(x, t, ...) .trans_f(x, t, "translation", ...)) |
| 223 | +setMethod("translation", |
| 224 | + c("SpatialDataFrame", "numeric"), |
| 225 | + \(x, t, ...) .trans_f(x, t, "translation", ...)) |
0 commit comments