How to convert ellipsoid to mesh3d in R?

378 Views Asked by At

I use the ellipsoidhull method from cluster package to obtain the minimum volume enclosing ellipsoid (mvee) from a set of points. This method returns an object of class ellipsoid. I need to plot the generated ellipsoid. I tried to use the wire3d method from rgl package to plot ellipsoids but this method gets objects of class mesh3d as input parameter. How can I convert an ellipsoid object to a mesh3d object?

2

There are 2 best solutions below

3
Mike Wise On

If you don't actually care about a mesh, and are just looking to plot a transparent ellipsoid you can use this:

library(rgl)
library(cluster)
open3d()

ellipsoid3d <- function(cen, a = 1,b = 1,c = 1,n = 65, ...){
  f <- function(s,t){ 
    cbind(   a * cos(t)*cos(s) + cen[1],
             b *        sin(s) + cen[2],
             c * sin(t)*cos(s) + cen[3])
  }
  persp3d(f, slim = c(-pi/2,pi/2), tlim = c(0, 2*pi), n = n, add = T, ...)
}

set.seed(123)
n <- 6
for (i in 1:n){
   cen <- 3*runif(3)
   a <- runif(1)
   b <- runif(1)
   c <- runif(1)

   clr <- c("red","blue","green")[i %% 3 + 1 ]
   elpf <- ellipsoid3d(cen,a=a,b=b,c=c,col=clr,alpha=0.5)
}

Yielding:

enter image description here

I modified the interesting answer from cuttlefish44 to get this - see this link: enter link description here

There is also a qmesh3d answer from dww there that you could modify in a similar manner to get a mesh3d if that is what you really want, but I thought this more elegant.

0
Stéphane Laurent On
library(cluster)
xyz <- cbind(rnorm(10), rnorm(10), rnorm(10))
e <- ellipsoidhull(xyz)
A <- e$cov
center <- e$loc
r <- sqrt(e$d2)

library(Rvcg)
sphr <- vcgSphere()
library(rgl)
ell <- translate3d(
  scale3d(
    transform3d(sphr, chol(A)), 
    r, r, r),
  center[1], center[2], center[3])

shade3d(ell, color="red", alpha=0.3)
points3d(xyz)

enter image description here