# [R] plotting an isosurface on a 3d plot

Duncan Murdoch murdoch@dunc@n @end|ng |rom gm@||@com
Fri Jun 14 21:37:07 CEST 2019

```On 14/06/2019 9:14 a.m., ravi wrote:
> Hi Duncan,
> Thanks a lot for your help. You have really opened up a road for me to
> to pursue further.
>
> Your later solution with cylinder3d is interesting. However, in my real
> application, I have several parallel planes and very irregular contours
> in each of them. So the quad3d command is more useful.  Are there more
> efficient ways of using this without the for loops?

Yes, but you need to work out a longer vector of indices into the p1 and
p2 vectors.  Depending on how you store your data that could be just
tedious or really hard:  do you know how the vertices in p1 and p2
correspond?  If they are general curves with no connection, that can be
a tricky question.

>
> Other points where I would like to have help are :
>      1. I would like to smoothen the surface. But I have not been able
> to figure out how I can use the addNormals command along with quad3d.
> Are there other ways of improving the smoothness?

triangles.  If you've done the calculations to plot quads, you can
create a qmesh3d object instead.  Create one matrix holding the
coordinates of all vertices (ideally with no repetitions), and another
matrix containing the indices of vertices which make up the quads.

For example, starting with the code I posted before, this comes quite close:

vertices <- cbind(t(p1), t(p2))
n <- nrow(p1)
indices <- rbind(2:n, 1:(n-1), n + 1:(n-1), n + 2:n)
cyl <- qmesh3d(vertices, indices, homogeneous = FALSE)

It's not quite right because vertices 1 and n have the same coordinates,
as do vertices n+1 and 2*n; you really want to set it up so vertices are
uniquely defined.

>      2. As I said before, I can have 5 to 6 parallel planes. Sometimes,
> the jump between two of them can be too high. So, I am thinking of using
> an interpolation method at an intermediate plane. Do you have any
> recommendations? I was thing of using a gam method where I make an
> additive model for y ~ s(x) + s(z). Then predict y at height z given x.
> I may have to figure out how I do this for closed curves (two or more
> values of y for a given x). I just wonder if you can indicate generally
> the route hat I should follow.

Nope, no idea on this one.  You might want to use the misc3d::contour3d
function rather than working this all out for yourself, but it takes
different inputs than you've described.

>      3. How do I make the surface transparent? How do I add an alpha
> value to the quad3d command?

That's a material property, so you can just put it in as part of the
dots.  For the meshes, it would go into the shade3d() call in the same
way, or the material argument to qmesh3d.

Duncan Murdoch

>
> Once again, I would like to thank you for your fantastic help.
> Thanking you,
> Ravi
>
> On Thursday, 13 June 2019, 22:52:07 CEST, Duncan Murdoch
> <murdoch.duncan using gmail.com> wrote:
>
>
> On 13/06/2019 4:32 p.m., Duncan Murdoch wrote:
>  > On 13/06/2019 12:47 p.m., ravi via R-help wrote:
>  >> Hi,I want to plot a surface joining a circle on a plane with another
> circle (a little offset w.r.t. the first one) on a parallel plane. This
> is a very simplified version of my problem.
>  >> I will explain with the following code :
>  >> # First, I plot the two circlesorig1 <- 0.4;radius1=0.3;theta <-
>  >> orig2 <- 0.7;radius2=0.2;theta <- seq(0,2*pi,pi/8)x2 <-
>  >>
> plot(x1,y1,type='b',col="red",xlim=c(0,1),ylim=c(0,1),asp=1)lines(x2,y2,type="b",col="blue")
>  >> #### the z coordinates on two parallel plnesz1 <-
> rep(0,length(x1))z2 <- rep(1,length(x2))
>  >>
>  >> I have tried to plot my 3d figure using the packages plot3D, misc3d,
> rgl etc. All these packages require z as a matrix. In my case, all of
> x,y and z are vectors. How can I plot this surface? In addition, I would
> like to add similiar surfaces to the same plot.
>  >
>  > It's not true that rgl requires z as a matrix:  that's one possibility,
>  > but there are others.  Here's one way to do what you want.
>  >
>  > # First, compute the two circles
>  > orig1 <- 0.4;radius1=0.3;theta <- seq(0,2*pi,pi/8)
>  >
>  > #### the z coordinates on two parallel plnes
>  > z1 <- rep(0,length(x1))
>  > z2 <- rep(1,length(x2))
>  >
>  > library(rgl)
>  > p1 <- cbind(x1,y1,z1)
>  > plot3d(p1, col="red")
>  > p2 <- cbind(x2, y2, z2)
>  > points3d(p2, col="blue")
>  >
>  > quads <- matrix(numeric(), 0, 3)
>  > for (i in seq_along(x1)[-1]) {
>  > }
>  >
>  > There are more efficient ways to do this (the for loop would be slow if
>  > you had bigger vectors), but this should give you the idea.
>
> Here's one of the more efficient ways:
>
> cylinder3d(rbind(c(0.4, 0.4, 0), c(0.7, 0.7, 1)),