File: perspbox.R

package info (click to toggle)
r-cran-plot3d 1.4.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 2,588 kB
  • sloc: makefile: 2
file content (491 lines) | stat: -rw-r--r-- 14,951 bytes parent folder | download | duplicates (2)
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
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
# useable ranges for constants

checklim <- function(lim) {
  if (diff(lim) == 0)
    lim <- lim * c(0.8, 1.2)
  if (diff(lim) == 0)
    lim <- lim + c(-0.1, 0.1)
  return(lim)
}

# ==============================================================================
# Box around a perspective plot, x, y matrix or vector; z = matrix
# ==============================================================================

perspbox <- function(x = seq(0, 1, length.out = nrow(z)),
                     y = seq(0, 1, length.out = ncol(z)), z,  
                     bty = c("b", "b2", "f", "g", "bl", "bl2", "u", "n"),    # predefined types
                     ..., col.axis = "black", 
                     col.panel = NULL, lwd.panel = 1,
                     col.grid = NULL, lwd.grid = 1,
                     phi = 40, theta = 40, col = NULL,
                     colkey = NULL, plot = TRUE){

  dot <- list(...)
  plist <- list(type = "3D", plt = NULL, persp = NULL, alpha = dot$alpha)
  dot$alpha <- NULL
  dot$clog <- NULL
 
 # which ranges are imposed...
  plist$setlim <- c(dot$setlim1, dot$setlim2, dot$setlim3)
  dot$setlim1 <- dot$setlim2 <- dot$setlim3 <- NULL 
  
  if (plot) 
    plist$plt$ori   <- par("plt")
  plist$persp$box <- FALSE
  
  plist$persp$expand <- ifelse (is.null(dot$expand), 1, dot$expand)
  
 # check inputs 
  if (! is.matrix(z)) 
    z <- diag(nrow = length(z), x = z)
    
  if (is.null (x))
    x <- seq(0, 1, length.out = nrow(z))

  if (is.null (y))
    y <- seq(0, 1, length.out = ncol(z))

  if (is.null(dot$xlim))
    plist$xlim <- range(x)
  else
    plist$xlim <- dot$xlim
  
  if (is.null(dot$ylim))
    plist$ylim <- range(y)
  else
    plist$ylim <- dot$ylim
  
  if (is.null(dot$zlim))
    plist$zlim <- range(z)
  else
    plist$zlim <- dot$zlim

  plist$xlim <- checklim(plist$xlim)
  plist$ylim <- checklim(plist$ylim)
  plist$zlim <- checklim(plist$zlim)

  lim <- setlim (plist$xlim, plist$ylim, plist$zlim, 
    dot[["scale"]], dot[["expand"]]) 
  plist$scalefac <- lim

  dot$xlim <- dot$ylim <- dot$zlim <- NULL
  dot$scalefac <- NULL

  plist$mat <- transmat (phi, theta, plist$scalefac, dot$r, dot$d)
  
  if (is.null(dot$xlab)) dot$xlab <- "x"
  if (is.null(dot$ylab)) dot$ylab <- "y"
  if (is.null(dot$zlab)) dot$zlab <- "z"

  bty <- match.arg(bty)#, c("b", "b2", "f", "g", "bl", "bl2", "u"))
  if (bty == "n")   
    dot$box <- FALSE
  plist$persp$bty <- bty
    
  plist$persp$drawbox <- TRUE
  if (! is.null(dot$box))
    if (!dot$box) 
      plist$persp$drawbox <- FALSE

  if (plist$persp$drawbox) {
    if (bty != "u") {
      col.axis <- "black"; col.panel <- NULL
      lwd.panel <- 1; col.grid <- NULL; lwd.grid <- 1
    }
    back <- bty %in% c("b", "b2", "u")
    if (bty == "b2")  
      col.grid <- "grey"
    else if (bty == "g") {
      col.panel <- grey(0.925) 
      col.axis <- "grey"
      lwd.grid <- 2
      col.grid <- "white"
    } else if (bty == "bl") {
      col.panel <- "black" 
    } else if (bty == "bl2") {
      col.panel <- "black" 
      col.axis <- "grey"
      lwd.grid <- 2
      col.grid <- "grey"
    }
  
    if (back & is.null(col.panel)) {
      col.panel <- "white" #par("bg")   toggled off as this opens a window...
#      if (col.panel == "transparent")
#        col.panel <- "white"
    }  
  } # drawbox
  plist$persp$panel <- list(col.axis = col.axis, 
                      col.panel = col.panel, lwd.panel = lwd.panel,
                      col.grid = col.grid, lwd.grid = lwd.grid)

  if (is.null(col))
    col <- jet.col(100)
       
  iscolkey <- is.colkey(colkey, col = col)
  if (plot)
    plist$plt$main <- par("plt")
  if (iscolkey) {
    colkey <- check.colkey(colkey)
    plist$plt$main <- colkey$parplt
  }

  dot$col <- dot$border <- NULL      
    
  plist$dot <- dot
  plist$persp$theta <- theta
  plist$persp$phi <- phi
  
  if (plot) {
    plist <- plotbox(plist)
    par(plt = plist$plt$ori)
  }  
  class(plist) <- c("plist", "list")
  setplist(plist)
  invisible(plist$mat)
}

## =============================================================================
## plot box based on plist
## =============================================================================

plotbox <- function (plist) {

  par(plt = plist$plt$main)
  plist$persp$box <- TRUE
   plist$mat <- 
      do.call("persp", c(alist(plist$xlim, plist$ylim,
            z = matrix(nrow = 2, ncol = 2, data = plist$zlim),
            phi = plist$persp$phi, theta = plist$persp$theta, border = NA, col = NA),
            plist$dot))

  if (plist$persp$drawbox) {
    P <- !visibility(plist$xlim, plist$ylim, plist$zlim, plist$mat)
 # e.g for theta <90  P <- c(FALSE, TRUE, TRUE, FALSE, TRUE, FALSE)
    with (plist$persp$panel, {
      panels <- (!is.null(col.panel) | lwd.panel != 1) 

      if (panels) 
        color.panels(P, plist$xlim, plist$ylim, plist$zlim, 
           plist$mat, col.panel, lwd.panel, col.axis)
    
      if (!is.null(col.grid))
        grid.panels(P, plist$xlim, plist$ylim, plist$zlim, 
           plist$mat, col.grid, plist$dot$nticks, lwd.grid)
    })
  }        
  plist  
}
  
## =============================================================================
## Set background color to backward panels
## =============================================================================

color.panels <- function(P, xlim, ylim, zlim, pmat, col, lwd, border) {
  
  if (is.null(border))
    border <- NA
  if (is.null(col))
    col <- "white"
      
  panelcol <- function(linex, liney, linez, col, border = NA) {
    XX <- trans3D(x = linex, y = liney, z = linez, pmat = pmat)
    polygon(XX$x, y = XX$y, col = col, lwd = lwd, border = NA, lty = 1)
    lines(XX$x, y = XX$y, col = border, lwd = lwd, lty = 1)
  }

  for (ii in 1:6) {
  
    if (P[ii]) {  # panel is visible
	    p <- Face[ii, ]
      
      pts <- Vertex[p, ]
      pts <- rbind(pts, pts[1,])  # to make sure that box is closed
      
      panelcol(linex = xlim[pts[,1]], 
               liney = ylim[pts[,2]], 
               linez = zlim[pts[,3]],
               col = col, border = border)
    }
  }
}

## =============================================================================
## gridlines on panels - maybe this can be shorter??
## =============================================================================

grid.panels <- function(P, xlim, ylim, zlim, pmat, gcol, nticks, lwd) {  
  addsegments <- function(x0, x1, y0, y1, z0, z1, col) {
    # trans3D projects and converts XX$x- and XX$y to a matrix
    XY0 <- trans3D(x = x0, y = y0, z = z0, pmat = pmat)
    XY1 <- trans3D(x = x1, y = y1, z = z1, pmat = pmat)
    segments(XY1$x, XY1$y, XY0$x, XY0$y, col = col, lwd = lwd)
  }

  if (is.null(nticks))
    nticks <- 5
    
  xseq <- pretty(xlim, nticks)
  yseq <- pretty(ylim, nticks)
  zseq <- pretty(zlim, nticks)
    
  xseq <- xseq [ -c(1, length(xseq))]
  yseq <- yseq [ -c(1, length(yseq))]
  zseq <- zseq [ -c(1, length(zseq))]
    
  npx <- length(xseq)
  npy <- length(yseq)
  npz <- length(zseq)
    
  if (P[1])
    addsegments(xseq, xseq,
                rep(ylim[1], npx), rep(ylim[1], npx),
                rep(zlim[1], npx), rep(zlim[2], npx), 
                col = gcol)
  if (P[1])
    addsegments(rep(xlim[1], npz), rep(xlim[2], npz),
                rep(ylim[1], npz), rep(ylim[1], npz),
                zseq, zseq, 
                col = gcol)

  if (P[3])
    addsegments(rep(xlim[1], npz), rep(xlim[1], npz),
                rep(ylim[1], npz), rep(ylim[2], npz),
                zseq, zseq, 
                col = gcol)
  if (P[3])
    addsegments(rep(xlim[1], npy), rep(xlim[1], npy),
                yseq, yseq,
                rep(zlim[1], npy), rep(zlim[2], npy), 
                col = gcol)
 
  if (P[2])
    addsegments(xseq, xseq, 
                rep(ylim[2], npx), rep(ylim[2], npx),
                rep(zlim[1], npx), rep(zlim[2], npx), 
                col = gcol)
  if (P[2])
    addsegments(rep(xlim[1], npz), rep(xlim[2], npz),
                rep(ylim[2], npz), rep(ylim[2], npz),
                zseq, zseq,
                col = gcol)
  if (P[4])
    addsegments(rep(xlim[2], npy), rep(xlim[2], npy),
                yseq, yseq,
                rep(zlim[1], npy), rep(zlim[2], npy), 
                col = gcol)
  if (P[4])
    addsegments(rep(xlim[2], npz), rep(xlim[2], npz),
                rep(ylim[1], npz), rep(ylim[2], npz),
                zseq, zseq,
                col = gcol)
  if (P[5])
    addsegments(xseq, xseq,
                rep(ylim[1], npx), rep(ylim[2], npx),
                rep(zlim[1], npx), rep(zlim[1], npx), 
                col = gcol)
  if (P[5])
    addsegments(rep(xlim[1], npy), rep(xlim[2], npy),
                yseq, yseq,
                rep(zlim[1], npy), rep(zlim[1], npy), 
                col = gcol)
  if (P[6])
    addsegments(rep(xlim[1], npy), rep(xlim[2], npy),
                yseq, yseq,
                rep(zlim[2], npy), rep(zlim[2], npy), 
                col = gcol)
  if (P[6])
    addsegments(xseq, xseq,
                rep(ylim[1], npx), rep(ylim[2], npx),
                rep(zlim[2], npx), rep(zlim[2], npx), 
                col = gcol)
}

# code based on plot3d.c
# points on edges (x,y,z)
  Vertex <- matrix(ncol = 3, byrow = TRUE, data = c(
       1, 1, 1,  #xlim[1], ylim[1], zlim[1]
       1, 1, 2,  #xlim[1], ylim[1], zlim[2]
       1, 2, 1,
       1, 2, 2,
       2, 1, 1,
       2, 1, 2,
       2, 2, 1,
       2, 2, 2))

# the points of Vertex belonging to a face
  Face  <- matrix (ncol = 4, byrow = TRUE, data = c(
      1, 2, 6, 5,
      3, 7, 8, 4,
      1, 3, 4, 2,
      5, 6, 8, 7,
      1, 5, 7, 3,
      2, 4, 8, 6  )) 

visibility <- function(xlim, ylim, zlim, pmat) {
  Near <- vector(length = 6)

  for (ii in 1:6) {
	  p <- Face[ii, ]

    pt <- Vertex[p[1], ]
    u0 <- c(xlim[pt[1]] , ylim[pt[2]], zlim[pt[3]], 1)

    pt <- Vertex[p[2], ]
  	u1 <- c(xlim[pt[1]] , ylim[pt[2]], zlim[pt[3]], 1)

    pt <- Vertex[p[3], ]
  	u2 <- c(xlim[pt[1]] , ylim[pt[2]], zlim[pt[3]], 1)

    pt <- Vertex[p[4], ]
  	u3 <- c(xlim[pt[1]] , ylim[pt[2]], zlim[pt[3]], 1)

    v0 <- u0 %*% pmat
    v1 <- u1 %*% pmat
    v2 <- u2 %*% pmat
    v3 <- u3 %*% pmat

	# Visibility test. */
	# Determine whether the surface normal is toward the eye. */
    d <- v1/v1[4] - v0/v0[4]
    e <- v2/v2[4] - v1/v1[4]

	  Near[ii] <- (d[1]*e[2] - d[2]*e[1]) < 0
  }
  return(Near) 
}

## =============================================================================
## Transformation matrix -  see plot.c (R-core)
## =============================================================================

transmat <- function (phi, theta, scalefac, r, d) {
  
  if (is.null(r)) 
    r <- sqrt(3)
  
  if (is.null(d)) 
    d <- 1

  ph <- phi / 180 * pi
  th <- theta / 180 * pi
 
 # center at origin
  M <- diag(nrow = 4)
  M[4, 1:3] <- c(-scalefac$xc, -scalefac$yc, -scalefac$zc)
  VT <- diag(nrow = 4) %*% M
 
 # scale to -1,1
  M <- diag(nrow = 4, x = c(scalefac$x, scalefac$y, scalefac$z, 1))
  VT <- VT %*% M
 
 # rotation in x-direction, x-y plane to horizontal (-90)
  cosp <- cos(-0.5*pi)
  sinp <- sin(-0.5*pi)
  rotX <- matrix(nrow = 4, data = c(1, 0,   0,      0, 
                                    0, cosp, -sinp, 0, 
                                    0, sinp,  cosp, 0,
                                    0, 0,   0,      1))
  VT <- VT  %*% rotX
 
 # azimuthal rotation in y-direction
  cosp <- cos(-th)
  sinp <- sin(-th)
  rotY <- matrix(nrow = 4, data = c(cosp, 0,  sinp,      0, 
                                    0,    1,     0,      0, 
                                   -sinp, 0,  cosp,      0,
                                    0,    0,     0,      1))
  VT <- VT  %*% rotY
 
 # elevation rotation in x-direction
  cosp <- cos(ph)
  sinp <- sin(ph)
  rotX <- matrix(nrow = 4, data = c(1, 0,   0,      0, 
                                    0, cosp, -sinp, 0, 
                                    0, sinp,  cosp, 0,
                                    0, 0,   0,      1))
  VT <- VT  %*% rotX
 
 # translate to origin
  M <- diag(nrow = 4)
  M[4, 3] <- -r - d
  VT <- VT  %*% M
 
 # perspective
  M <- diag(nrow = 4)
  M[3, 4] <- -1/d
  VT <- VT  %*% M
  
  return(VT) 
}

## =============================================================================
## Draw visible edges of box
## =============================================================================

drawfullbox <- function(plist) {
  P <- visibility(plist$xlim, plist$ylim, plist$zlim, plist$mat)

  addsegments <- function(x0, x1, y0, y1, z0, z1) {

    XY0 <- trans3D(x = x0, y = y0, z = z0, pmat = plist$mat)
    XY1 <- trans3D(x = x1, y = y1, z = z1, pmat = plist$mat)
    segments(XY1$x, XY1$y, XY0$x, XY0$y, col = "black")
  }
  xlim <- plist$xlim
  ylim <- plist$ylim
  zlim <- plist$zlim
  
  if (P[1])
    addsegments(xlim, xlim,
                rep(ylim[1], 2), rep(ylim[1], 2),
                rep(zlim[1], 2), rep(zlim[2], 2))
  if (P[1])
    addsegments(rep(xlim[1], 2), rep(xlim[2], 2),
                rep(ylim[1], 2), rep(ylim[1], 2),
                zlim, zlim)

  if (P[3])
    addsegments(rep(xlim[1], 2), rep(xlim[1], 2),
                rep(ylim[1], 2), rep(ylim[2], 2),
                zlim, zlim)
  if (P[3])
    addsegments(rep(xlim[1], 2), rep(xlim[1], 2),
                ylim, ylim,
                rep(zlim[1], 2), rep(zlim[2], 2))
 
  if (P[2])
    addsegments(xlim, xlim, 
                rep(ylim[2], 2), rep(ylim[2], 2),
                rep(zlim[1], 2), rep(zlim[2], 2))
  if (P[2])
    addsegments(rep(xlim[1], 2), rep(xlim[2], 2),
                rep(ylim[2], 2), rep(ylim[2], 2),
                zlim, zlim)
  if (P[4])
    addsegments(rep(xlim[2], 2), rep(xlim[2], 2),
                ylim, ylim,
                rep(zlim[1], 2), rep(zlim[2], 2))
  if (P[4])
    addsegments(rep(xlim[2], 2), rep(xlim[2], 2),
                rep(ylim[1], 2), rep(ylim[2], 2),
                zlim, zlim)
  if (P[5])
    addsegments(xlim, xlim,
                rep(ylim[1], 2), rep(ylim[2], 2),
                rep(zlim[1], 2), rep(zlim[1], 2))
  if (P[5])
    addsegments(rep(xlim[1], 2), rep(xlim[2], 2),
                ylim, ylim,
                rep(zlim[1], 2), rep(zlim[1], 2))
  if (P[6])
    addsegments(rep(xlim[1], 2), rep(xlim[2], 2),
                ylim, ylim,
                rep(zlim[2], 2), rep(zlim[2], 2))
  if (P[6])
    addsegments(xlim, xlim,
                rep(ylim[1], 2), rep(ylim[2], 2),
                rep(zlim[2], 2), rep(zlim[2], 2))

}