Dear all,
I hope this is a good place for this question, i usually post on R-help.
I'm trying to visualize spherical harmonics on a sphere using the rgl
package. I created a function that generates a data.frame of the format,
x y z col
1 0.00000 0 1.00000 0.50000
2 0.10629 0 0.99433 0.65709
3 0.21138 0 0.97740 0.79829
4 0.31408 0 0.94940 0.90930
5 0.41321 0 0.91063 0.97888
6 0.50767 0 0.86155 1.00000
...
I've tried to plot the surface of the sphere defined by the x y z grid
with colors defined by col. So far, my best shot is quite ugly (using
spheres3d and one color only). Here's my code,
require(orthopolynom) # needed for the legendre polynomials
library(rgl)
sphericalHarmonics <- function(l, m, theta=seq(0, 2*pi, length=10),
phi=seq(0, pi, length=10)){
Pl <- lapply(legendre.polynomials(l), as.function)
Pl.phi <- lapply(Pl, do.call, list(x=cos(phi)))
a1 <- (2*l+1) / (4*pi)
a2 <- factorial(l-m) / factorial(l+m)
Ymn <- as.data.frame(sapply(Pl.phi, "*", e2=sqrt(a1*a2) *
exp(1i*m*theta)))
names(Ymn) <- paste("l", seq(1, l+1), sep="")
return(Ymn)
}
sphericalHarmonics(1, 1) # OK this works
theta.phi <- expand.grid(theta=seq(0, 2*pi, length=60), phi=seq(0, pi,
length=30)) # generates a uniform polar grid
ylm <- sphericalHarmonics(3, 3, theta.phi$theta, theta.phi$phi)
spherical2cartesian <- function(tp, r=1){
with(tp, data.frame(
x = r * sin(theta) * cos(phi),
y = r * sin(theta) * sin(phi),
z = r * cos(theta) ) )
}
xyz <- spherical2cartesian(theta.phi) # polar to cartesian
mdf <- data.frame(xyz, ylm)
# draw shperes in an rgl window
mdf <- within(mdf, col <- Im(l3))
mdf$col <- (mdf$col + abs(min(mdf$col)) )
mdf$col <- mdf$col/max(mdf$col)
with(mdf, spheres3d(x, y, z, radius=3, color=rgb(col, 0, 0)))
Any advice would be gratefully appreciated!
Baptiste
_____________________________
Baptiste AuguiƩ
School of Physics
University of Exeter
Stocker Road,
Exeter, Devon,
EX4 4QL, UK
Phone: +44 1392 264187
http://newton.ex.ac.uk/research/emag
_______________________________________________
R-sig-Geo mailing list
R-sig-Geo@stat.math.ethz.ch
https://stat.ethz.ch/mailman/listinfo/r-sig-geo