Question

I have 100 points representing the boundary of a sphere of radius .1 and center (.5,.5,.5). I would like to represent this sphere in an array of points on a 3-D gird. The values of the array would be binary. 1 to represent inside the sphere and 0 to represent outside the sphere. The representation would be upon the unit cube.

I forsee the array looking something like this. For an array 100 by 100 by 100. The (1,1,1) value would be 0. The (50,50,50) value would be 1, since this grid point is inside the sphere.

Here is the code that creates and plots the 100 (or more) points. These points all lay on the boundary of the sphere.

library(scatterplot3d)
n <- 100
r <- rep(.1,n)
theta <- runif(n,0,pi)
phi <- runif(n,0,2*pi)
x <- r*sin(theta)*cos(phi)+.5
y <- r*sin(theta)*sin(phi)+.5
z <- r*cos(theta)+.5
graphic <- scatterplot3d(x,y,z,xlim=c(0,1),ylim=c(0,1),zlim=c(0,1))
Was it helpful?

Solution

Maybe this helps you on your way. I'm guessing that visualization is no the main objective here, but I've included a series of image plots to show the sections of the 3d array.

Example:

n=25
cen <- c(0.5,0.5,0.5)
rad <- 1
xs <- seq(cen[1]-rad,cen[1]+rad,,n)
ys <- seq(cen[2]-rad,cen[2]+rad,,n)
zs <- seq(cen[3]-rad,cen[3]+rad,,n)
grd <- expand.grid(x=xs, y=ys, z=zs)
a <- array(0, dim=c(n,n,n))
for(i in seq(a)){
    a[i] <- as.numeric(dist(rbind(grd[i,], cen)) <= rad)
}

png("sections.png", units="in", width=10, height=4, res=400)
op <- par(mfrow=c(1,n), mar=rep(0.1,4))
for(i in seq(n)){
    image(x=xs, y=ys, z=a[,,i], col=c("white", "black"), axes=FALSE, xlab="", ylab="")
    abline(h=xs, col=8, lwd=0.2)
    abline(v=ys, col=8, lwd=0.2)
    #box()
}
par(op)
dev.off()

enter image description here

OTHER TIPS

n <- 3
x <- 1:n
y <- 1:n
z <- 1:n
grid <- expand.grid(x,y,z)
vec <- ((grid[,1]-rep(n/2+.5,n^3))^2 +
 (grid[,2]-rep(n/2+.5,n^3))^2 +
 (grid[,3]-rep(n/2+.5,n^3))^2)^.5
a <- array(round(vec,3),dim=c(n,n,n))     #in array (pixel) scale                       
a.metric <- a*(1/n)                 #in "metric" scale
a
a.metric
STACK <- array(as.numeric(a.metric <=.1),dim=c(n,n,n))
STACK
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top