Generating 3d Terrain

3d model of the Hawaiian island chain.
3d model of the Hawaiian island chain, rendered in OpenSCAD.

After a lot of hours of experimentation I’ve finally settled on a workable method for generating large-scale 3d terrain.

Data from the NGDC’s Grid Extraction tool. The ETOPO1 (bedrock) option gives topography and bathymetry. You can select a rectangle from a map, but it can’t be too big and, which is quite annoying, you can’t cross the antimeridian.

The ETOPO1 data is downloaded as a GeoTIFF, which can be easily converted to a png (I use ImageMagick convert).

The Hawaiian data with the downloaded grayscale.
The Hawaiian data with the downloaded grayscale.

Adjusting the color scale. One interesting property of the data is that it uses a grayscale to represent the elevations that tops out at white at sea-level, then switches to black and starts from there for land (see the above image). While this makes it easy to see the land in the image, it needs to be adjusted to get a good heightmap for the 3d model. So I wrote a python script that uses matplotlib to read in the png image as an array and then I modify the values. I use it to output two images: one of the topography and one of just land and water that I’ll use as a mask later on.

Hawaiian Islands with adjusted topography and ocean-land.
Hawaiian Islands with adjusted topography and ocean-land.

The images I export using matplotlib as grayscale png’s, which can be opened in OpenSCAD using the surface command, and then saved as an stl file. Bigger image files are take longer. A 1000×1000 image will take a few minutes on my computer to save, however the stl file can be imported into 3d software to do with as you will.

Note: H.G. Deitz has a good summary of free tools for Converting Images Into OpenSCAD Models

OpenSCAD: Creating 3d Objects for Printing

To create a rectangular prism and then take a circle out of the center requires just a few lines of code.
To create a rectangular prism and then take a circle out of the center requires just a few lines of code.

OpenSCAD bills itself as “The Programmers Solid 3D CAD Modeller”. It does this job pretty well, which is probably why I like it so much. Like POV-RAY, which I’ve used before, you create primitive objects–spheres, boxes, cylinders, etc.–and add or subtract them from one another to create the three-dimensional shapes you want.

Unlike the more graphical 3d modeling programs, like SketchUp (which I’ve played with in the past), in OpenSCAD you have to specify in the script the exact dimensions of your primitives, and how to rotate them and translate them to get them where you want them to be in space. This makes it a great language to use in geometry class, or anywhere else you want students to learn about co-ordinate systems.

The script to create the box with a circle cut out of it (see figure above) is:

difference() {
	cube([40,40,50], center=true);
	sphere(25);
}

Vpython requires students of make similar geometric movements of their objects and renders them nicely in 3d, but given the incentive that they can print up a tangible result of their work, I’d be willing to bet that students, especially younger ones, would be quite motivated to work with OpenSCAD. Vpython does retain the advantage that it is able to do animations, while you can only print static objects. In addition, OpenSCAD is more of a scripting language than a programming language like Python (see some of my Vpython programs here).

Student models a "fez" he modelled in OpenSCAD. This was the first object, other than the test cube, that was printed on our 3d printer.
8th grader models a “fez” he modelled in OpenSCAD. This was the first object, other than the test cube, that was printed on our 3d printer.

The OpenSCAD documentation is quite good. I also found it easy to find instructions on how to create a 3d object from a black and white image, simply by extruding it in the third dimension (by iamwil on the Cubehero Blog and by I Heart Robotics).

One note: I’m using OSX 10.6.8 at the moment, and the current version of OpenSCAD does not work on it. Since I’m loathe to upgrade, I had to use the prior release of OpenSCAD-2013.06.dmg.

3d Molecule of the Month

Cyclohexane, from the interactive model on 3Dchem.com.

Molecular models tend to fascinate. As a introduction to the chemistry of elements, students seem to like putting them together, and they tend to enjoy finding out what their molecules are called.

You can’t beat fitting together molecules by hand as a learning experience, but 3Dchem has a nice collection of interactive, three-dimensional molecules, including molecules of the month.

Periodic spiral of the elements (from 3Dchem.com).

They also have three-dimensional periodic tables showing the sizes of the atoms in the traditional tabular form as well as a spiral.

Periodic Table showing the elements by size.