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 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284
|
dynIdentify <- function(x,y,labels=seq_along(x),
corners = cbind( c(-1,0,1,-1,1,-1,0,1),
c(1,1,1,0,0,-1,-1,-1) ),
...) {
lx <- x # label positions
ly <- y
lx[ is.na(labels) ] <- NA
ly[ is.na(labels) ] <- NA
llx <- lx # line end positions
lly <- ly
replot <- function() {
plot(x,y,...)
segments(x,y, llx,lly)
text(lx,ly,labels)
}
replot()
# tmp <- cnvrt.coords(x,y, input='usr')$dev
dx <- grconvertX(x, to='ndc')
dy <- grconvertY(y, to='ndc') # device coordinates of points
widths <- strwidth(labels)/2
heights <- strheight(labels)/2
ci <- 0 # current label
mouse.down <- function(buttons, x, y){
if( any(buttons==2) ){
out <- list( labels=list(x=lx, y=ly),
lineends=list(x=llx, y=lly) )
return(out)
}
# tmp <- cnvrt.coords(lx,ly, input='usr')$dev
i <- which.min( (grconvertX(lx,to='ndc')-x)^2 + (grconvertY(ly,to='ndc')-y)^2 )
ci <<- i
NULL
}
mouse.up <- function(buttons, x, y){
# tmp <- cnvrt.coords(x,y, input='dev')$usr
cx <- grconvertX(x, from='ndc')
cy <- grconvertY(y, from='ndc')
tmpx <- cx + corners[,1]*widths[ci]
tmpy <- cy + corners[,2]*heights[ci]
# tmp <- cnvrt.coords(tmpx,tmpy, input='usr')$dev
i <- which.min( (dx[ci] - grconvertX(tmpx,to='ndc'))^2 +
(dy[ci] - grconvertY(tmpy, to='ndc'))^2 )
# tmp <- lx; tmp[ci] <- cx; lx <<- tmp
# tmp <- ly; tmp[ci] <- cy; ly <<- tmp
# tmp <- llx; tmp[ci] <- tmpx[i]; llx <<- tmp
# tmp <- lly; tmp[ci] <- tmpy[i]; lly <<- tmp
lx[ci] <<- cx
ly[ci] <<- cy
llx[ci] <<- tmpx[i]
lly[ci] <<- tmpy[i]
replot()
NULL
}
out <- getGraphicsEvent( prompt= "Click on points and drag label to position. \nRight click to exit\n",
onMouseDown=mouse.down,
onMouseUp=mouse.up)
invisible(out)
}
TkIdentify <- function(x,y,labels=seq_along(x), hscale=1.75, vscale=1.75,
corners = cbind( c(-1,0,1,-1,1,-1,0,1),
c(1,1,1,0,0,-1,-1,-1) ),
...) {
if( !requireNamespace('tkrplot', quietly = TRUE) ) stop('This function depends on the tkrplot package being available')
md <- FALSE
lx <- x # label positions
ly <- y
lx[ is.na(labels) ] <- NA
ly[ is.na(labels) ] <- NA
llx <- lx # line end positions
lly <- ly
first <- TRUE
dx <- dy <- dlx <- dly <- widths <- heights <- numeric(0)
ul <- ur <- ut <- ub <- 0
replot <- function() {
plot(x,y,...)
segments(x,y, llx,lly)
text(lx,ly,labels)
if(first) {
first <<- FALSE
# tmp <- cnvrt.coords(x,y, input='usr')$dev
dx <<- grconvertX(x, to='ndc')
dy <<- grconvertY(y, to='ndc')
widths <<- strwidth(labels)/2
heights <<- strheight(labels)/2
# tmp <- cnvrt.coords(c(0,1),c(0,1), input='dev')$usr
ul <<- grconvertX(0, from='ndc')
ur <<- grconvertX(1, from='ndc')
ub <<- grconvertY(0, from='ndc')
ut <<- grconvertY(1, from='ndc')
}
# tmp <- cnvrt.coords(lx,ly, input='usr')$dev
dlx <<- grconvertX(lx, to='ndc')
dly <<- grconvertY(ly, to='ndc')
}
tt <- tcltk::tktoplevel()
tcltk::tkwm.title(tt, "TkIdentify")
img <- tkrplot::tkrplot(tt, replot, vscale=vscale, hscale=hscale)
tcltk::tkpack(img, side='top')
tcltk::tkpack( tcltk::tkbutton(tt, text='Quit', command=function() tcltk::tkdestroy(tt)),
side='right')
corners <- cbind( c(-1,0,1,-1,1,-1,0,1), c(1,1,1,0,0,-1,-1,-1) )
ci <- 0 # current label
cx <- cy <- 0
iw <- as.numeric(tcltk::tcl('image','width',tcltk::tkcget(img,'-image')))
ih <- as.numeric(tcltk::tcl('image','height',tcltk::tkcget(img,'-image')))
mouse.move <- function(x,y) {
if(md){
tx <- (as.numeric(x)-1)/iw
ty <- 1-(as.numeric(y)-1)/ih
cx <<- tx*ur + (1-tx)*ul
cy <<- ty*ut + (1-ty)*ub
# tmp <- lx; tmp[ci] <- cx; lx <<- tmp
# tmp <- ly; tmp[ci] <- cy; ly <<- tmp
lx[ci] <<- cx
ly[ci] <<- cy
tmpx <- cx + corners[,1]*widths[ci]
tmpy <- cy + corners[,2]*heights[ci]
tmpxx <- (tmpx - ul)/(ur-ul)
tmpyy <- (tmpy - ub)/(ut-ub)
i <- which.min( (dx[ci] - tmpxx)^2 +
(dy[ci] - tmpyy)^2 )
# tmp <- lx; tmp[ci] <- cx; lx <<- tmp
# tmp <- ly; tmp[ci] <- cy; ly <<- tmp
# tmp <- llx; tmp[ci] <- tmpx[i]; llx <<- tmp
# tmp <- lly; tmp[ci] <- tmpy[i]; lly <<- tmp
lx[ci] <<- cx
ly[ci] <<- cy
llx[ci] <<- tmpx[i]
lly[ci] <<- tmpy[i]
tkrplot::tkrreplot(img)
}
}
mouse.down <- function(x,y){
tx <- (as.numeric(x)-1)/iw
ty <- 1-(as.numeric(y)-1)/ih
ci <<- which.min( (tx - dlx)^2 + (ty - dly)^2 )
md <<- TRUE
mouse.move(x,y)
}
mouse.up <- function(x,y){
md <<- FALSE
}
tcltk::tkbind(img, '<Motion>', mouse.move)
tcltk::tkbind(img, '<ButtonPress-1>', mouse.down)
tcltk::tkbind(img, '<ButtonRelease-1>', mouse.up)
tcltk::tkwait.window(tt)
out <- list( labels=list(x=lx, y=ly),
lineends=list(x=llx, y=lly) )
invisible(out)
}
### old version, possibilities for the Tk version
## dynIdentify <- function(x,y,labels=seq_along(x), ...) {
## plot(x,y,...)
##
## tmp <- cnvrt.coords(x,y, input='usr')$dev
## dx <- tmp$x
## dy <- tmp$y # device coordinates of points
##
## print(dx)
## print(dy)
##
## lx <- rep(NA, length(x) ) # label positions
## ly <- rep(NA, length(y) )
## llx <- lx # line end positions
## lly <- ly
##
## widths <- strwidth(labels)/2
## heights <- strheight(labels)/2
##
## corners <- cbind( c(-1,0,1,-1,1,-1,0,1), c(1,1,1,0,0,-1,-1,-1) )
##
## md <- FALSE # mouse button down
##
## cx <- 0 # current
## cy <- 0
## ci <- 0
##
## replot <- function() {
## if(!md){return()}
## plot(x,y,...)
## segments(x,y, llx,lly)
## text(lx,ly,labels)
## }
##
## mouse.move <- function(buttons, x, y){
##
## tmp <- cnvrt.coords(x,y, input='dev')$usr
## cx <<- tmp$x
## cy <<- tmp$y
## if(md){
##
## tmpx <- cx + corners[,1]*widths[ci]
## tmpy <- cy + corners[,2]*heights[ci]
## tmp <- cnvrt.coords(tmpx,tmpy, input='usr')$dev
## i <- which.min( (dx[ci] - tmp$x)^2 +
## (dy[ci] - tmp$y)^2 )
## tmp <- lx; tmp[ci] <- cx; lx <<- tmp
## tmp <- ly; tmp[ci] <- cy; ly <<- tmp
## tmp <- llx; tmp[ci] <- tmpx[i]; llx <<- tmp
## tmp <- lly; tmp[ci] <- tmpy[i]; lly <<- tmp
## replot()
## }
## NULL
## }
##
## mouse.down <- function(buttons, x, y){
##
## if( any(buttons==2) ){
## out <- list( labels=list(x=lx, y=ly),
## lineends=list(x=llx, y=lly) )
## return(out)
## }
## i <- which.min( (dx-x)^2 + (dy-y)^2 )
## ci <<- i
## md <<- TRUE
## mouse.move(buttons, x, y)
## NULL
## }
##
## mouse.up <- function(buttons, x, y){
##
## tmp <- dx; tmp[ci] <- NA; dx <<- tmp
## tmp <- dy; tmp[ci] <- NA; dy <<- tmp
##
## if(all(is.na(dx))) {
## out <- list( labels=list(x=lx, y=ly),
## lineends=list(x=llx, y=lly) )
## return(out)
## }
##
## md <<- FALSE
## ci <<- 0
## NULL
## }
##
## out <- getGraphicsEvent( prompt= "Click on points and drag label to position.\nRight click to exit\n",
## onMouseDown=mouse.down,
## onMouseMove=mouse.move,
## onMouseUp=mouse.up)
##
## invisible(out)
## }
|