Skip to content

Commit

Permalink
Merge branch 'feature/npop3d'
Browse files Browse the repository at this point in the history
* feature/npop3d:
  Add test for npop3d
  Add npop3d and teach to plot3d.neuronlist to prepare for it
  Make .plotted3d environment on startup
* part of fix for natverse/flycircuit#11
  • Loading branch information
jefferis committed Jul 7, 2014
2 parents a3983a1 + f3d7f9a commit e2594ca
Show file tree
Hide file tree
Showing 7 changed files with 72 additions and 2 deletions.
2 changes: 2 additions & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,7 @@ export(ngraph)
export(nlapply)
export(nmapply)
export(nopen3d)
export(npop3d)
export(nrrd.voxdims)
export(origin)
export(pointsinside)
Expand Down Expand Up @@ -239,6 +240,7 @@ importFrom(nat.utils,makelock)
importFrom(nat.utils,removelock)
importFrom(rgl,par3d)
importFrom(rgl,plot3d)
importFrom(rgl,pop3d)
importFrom(rgl,tmesh3d)
importFrom(rgl,triangles3d)
importFrom(tools,md5sum)
Expand Down
8 changes: 7 additions & 1 deletion R/neuronlist.R
Original file line number Diff line number Diff line change
Expand Up @@ -184,8 +184,13 @@ nmapply<-function(FUN, ..., MoreArgs = NULL, SIMPLIFY = FALSE, USE.NAMES = TRUE)
#' appropriate), the values are used to plot cell bodies. For neurons the
#' values are passed to \code{plot3d.neuron} for neurons. In contrast
#' \code{dotprops} objects still need special handling. There must be columns
#' called \code{X,Y,Z} in the data.frame attached to \code{x}, that are then
#' called \code{X,Y,Z} in the data.frame attached to \code{x}, that are then
#' used directly by code in \code{plot3d.neuronlist}.
#'
#' Whenever plot3d.neuronlist is called, it will add an entry to an
#' environment \code{.plotted3d} in \code{nat} that stores the ids of all the
#' plotted shapes (neurons, cell bodies) so that they can then be removed by a
#' call to \code{npop3d}.
#' @param x a neuron list or, for \code{plot3d.character}, a character vector of
#' neuron names. The default neuronlist used by plot3d.character can be set by
#' using \code{options(nat.default.neuronlist='mylist')}. See
Expand Down Expand Up @@ -261,6 +266,7 @@ plot3d.neuronlist<-function(x, subset, col=NULL, colpal=rainbow, skipRedraw=200,
if(is.logical(soma)) soma=2
rval <- c(rval, spheres3d(df[, c("X", "Y", "Z")], radius = soma, col = cols))
}
assign(".last.plot3d", rval, envir=.plotted3d)
df$col=cols
attr(rval,'df')=df
invisible(rval)
Expand Down
19 changes: 19 additions & 0 deletions R/pop3d.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#' Remove plotted neurons or other 3d objects
#'
#' The normal usage will not specify \code{x} in which case the last neurons
#' plotted by \code{plot3d.neuronlist} or any of its friends will be removed.
#' @param x rgl ids of objects to remove
#' @param slow Whether to remove neurons one by one (slowly) default: FALSE
#' @param type Type of objects to remove see \code{pop3d}.
#' @export
#' @seealso \code{\link[rgl]{pop3d}}, \code{\link{plot3d.neuronlist}}
#' @importFrom rgl pop3d
npop3d <- function(x, slow=FALSE, type='shapes') {
if(missing(x)){
if(exists(".last.plot3d", envir=.plotted3d))
x <- get(".last.plot3d", envir=.plotted3d)
else x <- NULL
}
if(slow) invisible(sapply(x, function(x) try(pop3d(x, type=type))))
else try(pop3d(unlist(x), type=type))
}
5 changes: 4 additions & 1 deletion R/zzz.R
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,7 @@
}

# will store information about formats that we can read
.fileformats <- new.env()
.fileformats <- new.env()

# Will store stack of plotted rgl objects, ready for popping
.plotted3d <- new.env()
24 changes: 24 additions & 0 deletions man/npop3d.Rd
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
% Generated by roxygen2 (4.0.1): do not edit by hand
\name{npop3d}
\alias{npop3d}
\title{Remove plotted neurons or other 3d objects}
\usage{
npop3d(x, slow = FALSE, type = "shapes")
}
\arguments{
\item{x}{rgl ids of objects to remove}

\item{slow}{Whether to remove neurons one by one (slowly)
default: FALSE}

\item{type}{Type of objects to remove see \code{pop3d}.}
}
\description{
The normal usage will not specify \code{x} in which case
the last neurons plotted by \code{plot3d.neuronlist} or any
of its friends will be removed.
}
\seealso{
\code{\link[rgl]{pop3d}}, \code{\link{plot3d.neuronlist}}
}

6 changes: 6 additions & 0 deletions man/plot3d.neuronlist.Rd
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,12 @@ must be columns called \code{X,Y,Z} in the data.frame
attached to \code{x}, that are then used directly by code
in \code{plot3d.neuronlist}.
Whenever plot3d.neuronlist is called, it will add an entry
to an environment \code{.plotted3d} in \code{nat} that
stores the ids of all the plotted shapes (neurons, cell
bodies) so that they can then be removed by a call to
\code{npop3d}.
plot3d.character will check if
options('nat.default.neuronlist') has been set and then use
x as an identifier to find a neuron in that neuronlist.
Expand Down
10 changes: 10 additions & 0 deletions tests/testthat/test-pop3d.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
context("npop3d")

test_that("pop last plotted neurons",{
open3d()
original.rgl.ids=rgl.ids()
plot3d(c("EBH11R", "EBH20L"), db=Cell07PNs)
npop3d()
expect_equal(rgl.ids(), original.rgl.ids)
rgl.close()
})

0 comments on commit e2594ca

Please sign in to comment.