2

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?

m.taheri
  • 309
  • 3
  • 21

2 Answers2

2

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.

Community
  • 1
  • 1
Mike Wise
  • 22,131
  • 8
  • 81
  • 104
  • Thank you. Of course I have just the equation of ellipsoid in matrix form: t(x - c) * A * (x - c ) = 1. Thus I don't have ellipsoid radius. How can I obtain radius? – m.taheri Mar 16 '17 at 20:51
  • That is work, and really a separate question - there are so few rgl questions - we need more activity. Please accept this (and upvote it too please). Then post your request as a separate question and I will tackle it on the weekend if someone else doesn't get to it first. – Mike Wise Mar 17 '17 at 11:14
  • I voted up your answer as it may help someone to plot an ellipsoid. I need to obtain the mesh3d object. – m.taheri Jul 20 '17 at 11:58
1
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

Stéphane Laurent
  • 75,186
  • 15
  • 119
  • 225