# Adventures in 3D

I recently had the idea to create a model of the rings of
Saturn. After a bit of reading, I learned that there are 14 major subdivisions
of the rings. But each ring has lots of small variations in color.
Unfortunately, I could not find any data that showed a more detailed breakdown
of each of the major subdivisions. So, using some satellite images, I measured and
recorded some of the more visible color differences, building out a data set as
I went.

Like the image in the header of this post (from the Jet Propulsion Laboratory/NASA), pictures of the planet tend to show a sideways view. This often leads to people believing that the rings orbit
the planet in an elliptical manner, similarly to the planets around the sun.
But this is not the case—the rings, which are made up of billions of particles
of various sizes, actually orbit Saturn in a circular manner.

So, to visualize the rings, I’d need to create circular
polygons for each ring in my data set. These polygons would need to first draw
an inner circle for the ring, then an outer circle, connecting them together,
like this:

I also figured that I could create Saturn using a similar
circular polygon. The only difference would be that the inner ring would have a
radius of 0 (i.e. there would be no inner ring at all, so it would just be a
solid circle).

To create each of the circles, I leveraged parametric
equations (for more on these, see Beyond Show Me Part 3: Parametric Equations). In the end, I had what I think is a pretty cool top-down view of
Saturn (click the image to interact with the full visualization).

**Going 3D**

But, I had bigger plans for this viz—I wanted to make it 3D
and rotatable, which would allow you to see any angle of the planet and its rings.
To do this, I’d have to add a 3

^{rd}dimension to the viz, then I’d leverage the work of Bora Beran to project the 3D coordinates (x, y, z) onto a 2D plane (x, y).
The first obstacle would be adding thickness to the rings. But
I got lucky here. The rings have a thickness ranging from as little as 10 m to
1 km. When this is translated to my dashboard size of 1200 x 1200 pixels, that
thickness would be less than a single pixel. So, I wouldn’t actually have to
worry about the thickness at all.

The second obstacle would be making the planet itself 3D. I
mulled over how to do this for a while. I could create a 3D sphere using
methods outlined by Anya A’Hearn and Phillip Riggs in The 3D Tableau Full Monty, but I really wanted to avoid using an entirely different methodology to
create the sphere than I was using to create my rings. Eventually, I realized
that I could create a sphere by layering multiple circles on top of each other.
I’d start with the circle I already had, then add layers above that (on the z
axis) which slowly decrease in radius until they form the top of the sphere.
I’d then do the same thing on the bottom of the main circle to form the bottom
of the sphere. By doing this, then I’d be able to create this 3D model using
nothing but circles.

The trickiest part of all this would be doing the math to
calculate the z position and radius of each circle. I can feel people’s eyes
rolling back in their heads already, so I’m not going to go into the math too
much, but suffice to say that I realized that I could just turn the model over
so that my z axis is in the position of a y axis and my x axis remains the
same. Using trig, I could then calculate the radius and z position of each
circle.

Once I had the math nailed, I attempted to create my sphere.
For my first attempt, I created a data set with 500 records (one for each
circle) and pre-calculated all my coordinates. I then visualized it in Tableau,
colored the circles using the Hue Circle color palette, and created this:

The sphere can be rotated a full 360° in any direction. Here’s a gif of it
being rotated:

After creating this, I wanted to see if I could create the
same sphere using bins only. I was able to make it work, but the table
calculations got a bit complex and that had a significant impact on
performance, so I decided to stick with the pre-calculated data set.

**Making Saturn 3D**

So, now armed with the knowledge of how to create a sphere
in Tableau with only circles, I returned to Saturn and applied the technique. I
created 500 new records in my data set, then updated my viz. I then added some
color variation to the circles in an attempt to show the cloud formations on
the planet. Finally, I performed the 3D to 2D projection and rotated it so that
I’d see a side view of the planet. And then…disappointment.

Part of the problem here is that my rings are not completely
opaque, so the colors sort of bleed through, but there is a bigger problems
that I had not accounted for—the z-order, which defines the ordering of overlapping
objects. By default, Tableau wants to show one object on top of another object. When I first posted this blog, I had noted that this is a limitation of Tableau, that Tableau simply wasn't designed to do 3D. But, that's not exactly correct--rather, as Allan Walker pointed out to me, we just need the math to be able to calculate camera and its positioning. And he also noted that we can even change the position of the light source. So, even though I've personally hit a wall with this visualization, it's important to note that this is all possible with the right math. And the solution to this problem

*has*already been cracked in Tableau. I'm personally going to be checking out Allan's work to see how it's all done. Maybe, if I'm able to figure it out, I'll revisit the 3D Saturn!**Why Do This?**

Before closing out this blog, I want to address the question
some of you may be asking…why do this at all? Well, first of all, I just
thought it would be cool to visualize Saturn. This may not necessarily be
analytically insightful, but I thought it would be interesting and fun to
create. Like a lot of my work, this is a bit more art than science. But, I am a
big believer in the value of exploring the boundaries of Tableau. It forces you
to think about problems in different and creative ways. And you’ll always learn
something new about Tableau and how it thinks, as I have here. So while you may
never have the need to create a sphere in Tableau, the lessons learned along
the way and the skills you’ve gained will definitely be of value.

…and, if nothing else, I was able to create this really cool
rotatable 3D eyeball ðŸ˜Š

Ken Flerlage, May 12, 2018

Hi Ken,

ReplyDeleteI'd like to answer this: "It doesn’t understand that parts of objects can overlap parts of other objects. It doesn’t understand viewing angles and perspective." with a link http://allanwalkerit.tumblr.com/post/146190117942/there-is-no-use-case. In that blog post, there are further links to Tableau workbooks where you'll find some calculated fields that do exactly what you believe can't be done. For example, "Viewing angles and perspective". To calculate a "viewing angle", you have to simulate a camera (and position the camera). You'll see in the TIE fighter example I have calculations "L" which simulate light coming from a source at 0,0,0 - and a calculation using the inner product. This is basically the most simple shader used in 3D graphics. Ironically, with parameters, you could actually change the position of the light source. In the X-Wing example, I used Slope to color the Z values. In the Stanford Bunny example, I use the smoothsteps calculation. My point is, please don't draw the conclusions that "it can't" - It's not Tableau's fault - it's yours. You either do the research (if you don't know how) or apply your own learning to achieve the result you want.

If you want the math for the sphere with a Z order, it's all here: http://www.datablick.com/blog/2017/2/25/3d-printing-in-mapbox-alteryx-and-tableau-ive-got-big-balls

Another alternative is to build a sphere object in Blender, and save out to an ASCII STL. As STL's are triangulated meshes, you'll find the polygon order is 1,2,3 (and repeat), which means you don't need to do much else from an ETL perspective.

Anyway at the end of the day it just comes down to math (it always does with Tableau, it's just a scatterplot).

Thanks, Allan. Somehow I missed this when I looked at how to do this. Considering the incredible stuff you've done with 3D, I should've thought to take a look at your work. Will do that now.

DeleteAnother way to think about angles and perspective, you could learn from Noah's Tesla. His 3D work was available before Bora's, but the math is on a different plane (pun fully intended) https://public.tableau.com/profile/nsalvate#!/vizhome/TableauTesla/TableauTesla Make sure you credit his work if you implement.

DeleteI've seen this Tesla. It's brilliant. I'll certainly take a look. Thanks!

DeleteThanks for the name drop and the compliments... I don’t see anything in this post that would suggest any attribution be given to me. I haven’t looked at these workbooks, I expect they are original, but even if they had used something of mine as a basis I’m flattered when I see my work used to create something new.

ReplyDeleteI've typically controlled the z order by sorting a discrete dimension at the top of the marks card. Tableau uses such a dimension to render marks in order, so sorting based on the position along the z-axis will result in the desired overlaps. The math shouldn’t be that tricky here. Feel free to ping me offline if you’d like to do a screen-share.