Skip to content

Commit

Permalink
Add as.mesh3d.boundingbox
Browse files Browse the repository at this point in the history
* permits fancier bounding box plotting with rgl
  • Loading branch information
jefferis committed Aug 21, 2018
1 parent 0fe8256 commit 1a9a80a
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 1 deletion.
1 change: 1 addition & 0 deletions NAMESPACE
Expand Up @@ -34,6 +34,7 @@ S3method(as.im3d,im3d)
S3method(as.im3d,matrix)
S3method(as.list,neuronlistfh)
S3method(as.mesh3d,ashape3d)
S3method(as.mesh3d,boundingbox)
S3method(as.mesh3d,hxsurf)
S3method(as.neuron,data.frame)
S3method(as.neuron,default)
Expand Down
21 changes: 21 additions & 0 deletions R/hxsurf.R
Expand Up @@ -291,6 +291,27 @@ as.mesh3d.hxsurf<-function(x, Regions=NULL, material=NULL, drop=TRUE, ...){
tmesh3d(vertices=verts, indices=inds, homogeneous = FALSE, material = material, ...)
}

#' @description \code{as.mesh3d.boundingbox} converts a nat
#' \code{\link{boundingbox}} object into an rgl compatible \code{mesh3d}
#' object.
#' @rdname as.mesh3d
#' @export
#' @examples
#' bb=boundingbox(kcs20)
#' mbb=as.mesh3d(bb)
#' \donttest{
#' plot3d(kcs20)
#' # simple plot
#' plot3d(bb)
#' shade3d(mbb, col='red', alpha=0.3)
#'
#' }
as.mesh3d.boundingbox <- function(x, ...) {
centroid=colMeans(x)
size=diff(x)/2
mat=scaleMatrix(size[1], size[2], size[3])%*%translationMatrix(centroid[1], centroid[2], centroid[3])
cube3d(mat)
}

#' Convert an object to a nat hxsurf object
#'
Expand Down
7 changes: 7 additions & 0 deletions man/as.mesh3d.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 7 additions & 1 deletion tests/testthat/test-hxsurf.R
Expand Up @@ -104,7 +104,13 @@ test_that("we can convert hxsurf to rgl::mesh3d",{
expect_equal(as.hxsurf(tet.mesh3d, region = 'Inside'), tet.hxsurf)

expect_equal(as.mesh3d(surf, Regions=c("LH_L","LH_R")),
as.mesh3d(subset(surf, c("LH_L","LH_R"), drop=TRUE)))
as.mesh3d(subset(surf, c("LH_L","LH_R"), drop=TRUE)))
})

test_that("we can convert boundingbox to rgl::mesh3d",{
corners=rbind(rep(1,3), rep(-1,3))
bb=boundingbox(corners)
expect_equal(as.mesh3d(bb), rgl::cube3d())
})

test_that("we can convert ashape3d to rgl::mesh3d",{
Expand Down

0 comments on commit 1a9a80a

Please sign in to comment.