-
Notifications
You must be signed in to change notification settings - Fork 0
/
plot-rgl-grid.R
executable file
·477 lines (406 loc) · 13 KB
/
plot-rgl-grid.R
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
#' 3d plotting of an icosahedral grid, its subset or a data layer
#'
#'
#' @usage plot3d(x,...)
#' @rdname plot3d
#' @export plot3d
"plot3d"
if(requireNamespace("rgl", quietly = TRUE)){
# package installed, and not working...
if(rgl::rgl.useNULL()){
# manually create S3 generic
plot3d <-function(x,...){
UseMethod("plot3d")
}
# rgl is available to be called
}else{
# manually copy S3 generic
plot3d <- rgl::plot3d
}
}else{
# manually create S3 generic
plot3d <-function(x,...){
UseMethod("plot3d")
}
}
#' @param x (\code{\link{trigrid}}, \code{\link{hexagrid}} or \code{\link{facelayer}}) Object to be plotted.
#'
#' @param type (\code{character}) Value specifying the part of the grid to be plotted by the call of the function.
#' \code{"v"} plots the grid vertex points.
#' \code{"e"} draws the grid edges.
#' \code{"f"} draws the grid faces.
#' \code{"c"} draws the face centers of the grid.
#'
#' @param sphere (\code{numeric}) Defaults to \code{NULL}, adding a central white sphere to the plot. Assigning a \code{numeric} value will draw a new sphere with the given radius,
#' \code{FALSE} does not plot the sphere.
#' @param guides (\code{logical}) Value indicating whether the guidelines of the polar coordinate system shall be plotted.
#' @param add (\code{logical}) Value indicating whether a new plot shall be drawn, or the currently plotted information should be added to the active \code{rgl} device.
#'
#' @param ... Further graphical parameters passed to (see \code{\link[rgl]{plot3d}}).
#'
#' @return The function does not return any value.
#'
#'
#' @examples
#' # create a hexagonal grid
#' g <- hexagrid(c(2,2))
#' # plot the grid in 3d space
#' # plot3d(g, col="blue")
#' # make a subset to select faces
#' subG <- subset(g, c("F5", "F2"))
#' # plot the subset defined above
#' # plot3d(subG, type="f", col=c("orange"), add=TRUE, lwd=1)
#' @rdname plot3d
#' @exportS3Method rgl::plot3d trigrid
#' @exportS3Method plot3d trigrid
#' @export plot3d.trigrid
plot3d.trigrid <- function(x, type=c("l"),sphere=NULL, add=FALSE, guides=TRUE, ...){
# package not installed
if(!requireNamespace("rgl", quietly = TRUE)){
stop("Install the 'rgl' package and reload 'icosa' to use this function.")
}
# package installed and it is actually working
if(!rgl::rgl.useNULL()){
#create new plot?
if(add==FALSE)
{
#checking plotting
rgl::plot3d(x@vertices, type="n", box=FALSE, axes=FALSE, xlab="", ylab="", zlab="")
#default sphere plotting
if(is.null(sphere)){
#get the radius
fc<-apply(x@vertices[x@faces[1,],],2,mean)-x@center
rad<-sqrt(fc[1]^2+fc[2]^2+fc[3]^2)-15
blankSphere(x@center[1],x@center[2], x@center[3], radius = rad, color ="white", ng=200, box=FALSE, axes=FALSE)
}else{
if(sphere){
blankSphere(x@center[1],x@center[2], x@center[3], radius = sphere, color ="white", ng=200, box=FALSE, axes=FALSE)
}
}
}
if(type=="p")
{
#single point
if(length(x@vertices)==3)
{
rgl::points3d(x=x@vertices[1],y=x@vertices[2],z=x@vertices[3],...)
}else{
rgl::points3d(x@vertices, ...)
}
}
if(type=="l")
{
lines3d(x, ...)
}
if(type=="f")
{
faces3d(x,...)
}
if(type=="n"){
}
# guides
if(guides){
guides3d(col="green", origin=x@center, radius=x@r, lwd=2)
}
}else{
message("Your rgl installation is not working.")
}
}
#' 3d plotting of an icosahedral grid or its subset
#' @rdname plot3d
#' @param color (\code{character}) Only for the hexagrid plotting: value/values passed to the \code{\link{faces3d}} function instead of \code{col}.
#' @exportS3Method rgl::plot3d hexagrid
#' @exportS3Method plot3d hexagrid
#' @export plot3d.hexagrid
plot3d.hexagrid <- function(x, type=c("l"),sphere=NULL, color="gray70", add=FALSE, guides=TRUE, ...){
if(!requireNamespace("rgl", quietly = TRUE)){
stop("Install the 'rgl' package and reload 'icosa' to use this function.")
}
# prevent access to rgl if it is not working.
if(!rgl::rgl.useNULL()){
#create new plot?
if(add==FALSE)
{
#empty plotting plotting
rgl::plot3d(x@vertices, type="n", box=FALSE, axes=FALSE, xlab="", ylab="", zlab="")
#default sphere plotting
if(is.null(sphere)){
fVect<-x@faces[2,!is.na(x@faces[2,])]
#get the radius
fc<-apply(x@vertices[fVect,],2,mean)-x@center
rad<-sqrt(fc[1]^2+fc[2]^2+fc[3]^2)-10
blankSphere(x@center[1],x@center[2], x@center[3], radius = rad, color ="white", ng=200, box=FALSE, axes=FALSE)
}else{
if(sphere){
blankSphere(x@center[1],x@center[2], x@center[3], radius = sphere, color ="white", ng=200, box=FALSE, axes=FALSE)
}
}
}
if(type=="p")
{
#single point
if(length(x@vertices)==3)
{
rgl::points3d(x=x@vertices[1],y=x@vertices[2],z=x@vertices[3], col=color,...)
}else{
rgl::points3d(x@vertices, col=color, ...)
}
}
if(type=="l")
{
lines3d(x, ...)
}
if(type=="f")
{
faces3d(x,...)
}
if(type=="c")
{
#single point
if(length(x@faceCenters)==3)
{
rgl::points3d(x=x@faceCenters[1],y=x@faceCenters[2],z=x@faceCenters[3], col=color, ...)
}else{
rgl::points3d(x@faceCenters, col=color, ...)
}
}
if(type=="t")
{
rgl::text3d(x@faceCenters, texts=rownames(x@faceCenters),col=color, ...)
}
if(type=="n"){
}
if(guides){
guides3d(col="green", origin=x@center, radius=x@r, lwd=2)
}
}else{
message("Your rgl installation is not working.")
}
}
#' Methods of 3d line plotting
#'
#' This is a generic function used to plot the edge lines of either a \code{trigrid} or a \code{hexagrid} object, a \code{facelayer}, or \code{Spatial} objects in 3d space. The method is also implemented for
#' the object classes defined by the package 'sp'.
#'
#' The function is built on the openGL renderer of the R package \code{rgl}, which needs to be installed for the function to run. Although the function is works without attaching rgl, note that if you want to attach both \code{icosa} and \code{rgl},the \code{rgl} package has to be loaded ifrst otherwise the function will not be usable.
#'
#' @param x (\code{\link{trigrid}}, \code{\link{hexagrid}}, \code{\link{facelayer}} or \code{sp}) Object to be plotted.
#' @param arcs \code{logical} Value setting whether great circle arcs or segments shall be drawn betwenn the points of the grid.
#'
#' @param ... Further graphical parameters passed to (see \code{\link[rgl]{plot3d}}).
#'
#' @return The function does not return any value.
#'
#' @examples
#' # create a hexagonal grid
#' g <- hexagrid(c(2,2))
#' # plot the grid in 3d space
#' # lines3d(g, col="blue")
#' @rdname lines3d
"lines3d"
if(requireNamespace("rgl", quietly = TRUE)){
# package installed, and it is working
if(!rgl::rgl.useNULL()){
setGeneric(
name="lines3d",
def=function(x,y=NULL,z=NULL, ...){
standardGeneric("lines3d")
}
)
# package is working, use its generic
}else{
setGeneric("lines3d", def=rgl::lines3d, package="rgl")
}
}else{
setGeneric(
name="lines3d",
def=function(x,y=NULL,z=NULL, ...){
standardGeneric("lines3d")
}
)
}
#' lines3d method for the trigrid class
#' @rdname lines3d
#' @exportMethod lines3d
setMethod(
"lines3d",
signature="trigrid",
definition=function(x, arcs=FALSE, ...){
# rgl not installed, quit immediately
if(!requireNamespace("rgl", quietly = TRUE)) stop("Install the 'rgl' package and reload 'icosa' to use this function.")
# prevent access to rgl if it is not working.
if(!rgl::rgl.useNULL()){
v<-x@skeleton$v
e<-x@skeleton$e[x@skeleton$aE,]
#create edgeMat with a simple Rccp function
edgeMat<-.Call(Cpp_icosa_edgeMatTri_, v=v, e=e)
# get the list of additional arguments
newArgs<-list(...)
if(prod(x@tessellation)<16 & arcs){
res<-10
edgeMat<-.Call(Cpp_icosa_expandEdges_, edgeMat, x@center, res)
}
edgeMatExp<-edgeMat
rgl::segments3d(x=edgeMatExp[,1],y=edgeMatExp[,2],z=edgeMatExp[,3], ...)
}else{
message("Your rgl installation is not working.")
}
}
)
#' Methods of 3D face plotting.
#'
#' This function is used to plot the faces of either a \code{\link{trigrid}}, \code{\link{hexagrid}} or \code{\link{facelayer}} object in 3D space.
#'
#' The function is built on the openGL renderer of the R package \code{rgl}.
#'
#' @name faces3d
#' @param x The \code{\link{trigrid}}, \code{\link{hexagrid}} or \code{\link{facelayer}} object to be plotted.
#'
#' @param ... Further graphical parameters passed to (see \code{\link[rgl]{plot3d}}) and the \code{\link{heatMapLegend}} function.
#'
#' @return The function does not return any value.
#'
#' @examples
#' # create a hexagonal grid
#' g <- hexagrid(c(2,2))
#' # plot the grid in 3d space
#' # faces3d(g)
#' @exportMethod faces3d
#' @rdname faces3d
setGeneric(
name="faces3d",
package="icosa",
def=function(x,...){
standardGeneric("faces3d")
}
)
#' Methods of 3d face plotting.
#'
#'
#' @rdname faces3d
setMethod(
"faces3d",
signature="trigrid",
definition=function(x, ...){
# not to user if rgl is not installed
if(!requireNamespace("rgl", quietly = TRUE)) stop("Install the 'rgl' package and reload 'icosa' to use this function.")
# rgl is installed but not workign
if(!rgl::rgl.useNULL()){
v<-x@skeleton$v
f<-x@skeleton$f[as.logical(x@skeleton$aF),1:3]
#create edgeMat with a simple Rccp function
triMat<- .Call(Cpp_icosa_triMatTri_, v, f)
rgl::triangles3d(x=triMat[,1],y=triMat[,2],z=triMat[,3],...)
}else {
message("Your rgl installation is not working.")
}
}
)
#' Methods of 3d face plotting.
#'
#' @rdname faces3d
setMethod(
"faces3d",
signature="hexagrid",
definition=function(x,...){
# not to user if rgl is not installed
if(!requireNamespace("rgl", quietly = TRUE)) stop("Install the 'rgl' package and reload 'icosa' to use this function.")
# rgl is installed but not workign
if(!rgl::rgl.useNULL()){
v<-x@skeleton$plotV
f<-x@skeleton$f[as.logical(x@skeleton$aSF),1:3]
# f2<-f[order(f[,1]),]
#
# f2<-unique(f2)
#create edgeMat with a simple Rccp function
triMat<- .Call(Cpp_icosa_triMatTri_, v, f)
rgl::triangles3d(x=triMat[,1],y=triMat[,2],z=triMat[,3],...)
}else {
message("Your rgl installation is not working.")
}
}
)
#' Display the names of the grid elements in 3d plots.
#'
#' This function will display the names of vertices, faces and edges on 3d plots.
#'
#' @name gridlabs3d
#'
#' @param gridObj (\code{\link{trigrid}}, \code{\link{hexagrid}}) An icosahedral grid.
#'
#' @param type (\code{character}) Vector containing either \code{"f"}, \code{"e"} or \code{"v"}, rendering the names
#' of either the faces, edges or vertives respectively.
#'
#' @param ... Additional arguments passed to \code{\link[rgl:texts]{text3d}} function of the \code{rgl} package.
#'
#' @return The function does not return any value.
#'
#' @examples
#' # create a hexagonal grid
#' g <- hexagrid(c(2,2))
#' # plot the grid in 3d space
#' # lines3d(g, guides=FALSE)
#' # labels
#' # gridlabs3d(g)
#' @exportMethod gridlabs3d
#' @rdname gridlabs3d
setGeneric(
name="gridlabs3d",
package="icosagrid",
def=function(gridObj,...){
if(!requireNamespace("rgl", quietly = TRUE)) stop("Install the 'rgl' package and reload 'icosa' to use this function.")
standardGeneric("gridlabs3d")
}
)
#' @rdname gridlabs3d
setMethod(
"gridlabs3d",
signature="trigrid",
definition=function(gridObj,type="f",...){
# rgl is installed but not workign
if(!rgl::rgl.useNULL()){
# vertex names
if("v"%in%type){
rgl::text3d(texts=rownames(gridObj@vertices), gridObj@vertices*1.005,...)
}
if("f"%in%type){
rgl::text3d(texts=rownames(gridObj@faceCenters), gridObj@faceCenters*1.005,...)
}
if("e"%in%type){
#the coordinates
coords<-apply(gridObj@edges,1,function(x){
apply(gridObj@vertices[x,], 2, mean)
})
rgl::text3d(t(coords)*1.005, texts=rownames(gridObj@edges),...)
}
}else {
message("Your rgl installation is not working.")
}
}
)
#' @rdname gridlabs3d
setMethod(
"gridlabs3d",
signature="hexagrid",
definition=function(gridObj,type="f",...){
# rgl is installed and working
if(!rgl::rgl.useNULL()){
# vertex names
if("v"%in%type){
rgl::text3d(texts=rownames(gridObj@vertices), gridObj@vertices*1.005,...)
}
if("f"%in%type){
rgl::text3d(texts=rownames(gridObj@faceCenters), gridObj@faceCenters*1.005,...)
}
if("e"%in%type){
#the coordinates
coords<-apply(gridObj@edges,1,function(x){
apply(gridObj@vertices[x,], 2, mean)
})
rgl::text3d(t(coords)*1.005, texts=rownames(gridObj@edges),...)
}
}else {
message("Your rgl installation is not working.")
}
}
)