# New Year, new tools – OpenScad Tetrahedron

I fired up OpenScad, and created a new tetrahedron model using it.  I’ve done tons with platonic solids in OpenScad in the past.  This time around though, I figured I’d create the simplest tetrahedron I know.  Here’s the OpenScad code for the same:

```hull(){
translate([0,0,0])
sphere(4);

translate([100,100,0])
sphere(4);

translate([0, 100, 100])
sphere(4);

translate([100, 0, 100])
sphere(4);
}
```

The trick here is in the fact of the special relationship between a tetrahedron and a cube. The cube has 8 vertices, and you can create a tetrahedron by using 4 of those vertices. You just have to pick the right ones. You can easily choose the right ones by picking a set at opposite corners of any face (cutting a diagonal across that face). Then, choose the face exactly opposite, and select the two corners that are different from the ones you picked on the first face. Now you have these 4 vertices.

OpenScad has had the ‘hull()’ module since forever, but it didn’t always work so well. In this modern OpenScad, and for this particular case, it works extremely well. Just take the 4 vertices, throw them into a hull(), and out comes a nice tetrahedron!

This is the tetrahedron for the masses. No math required, except to know the vertices of a unit cube. And of course you can scale it up to any size you please.

If you want to do anything tricky with it though, like subtract some holes and the like, you’ll have to know the math again. But, there you have it, tetrahedron solid in a few lines of OpenScad.

# Contemplating Publishing

I have been using OpenScad for roughly a year now.  It’s been a great experience, and I’ve learned a lot about the program, and the limitations of my own design skills.  I have written a lot about using OpenScad, and where its strengths and limitations are, at least relative to my own design skills.

I recently asked a question on the OpenScad mailing list: Does anyone know of an effort to write a book on OpenScad?

The response thus far has been ‘the wiki book is THE book’.

So, I am contemplating the following, as I have before: Is there room/desire for a printed book on how to use OpenScad?

I had asked this question some time back in a different forum, and the recommendation at that time was “go ahead and start writing chapters, and put them on your blog”.

This is what I will start doing.  I believe there is room for some structured walk throughs of some nuances of OpenScad.  I will put some form around my various OpenScad musings and put them up here.  My intention is to not just cover what can already be found in the user’s manual, but taking a modeler’s approach.  Basically, show a picture of a thing, then deconstruct how that thing can be created with OpenScad, showing interesting techniques, and explaining various OpenScad functions along the way.  Similar to some of the screen casts and other tutorials I’ve seen, but probably getting into deeper detail.

Now, if only I can figure out how to put ‘code’ into my WordPress posts.  I will be able to do the following:

``` difference() { cube(10); cylinder(r=3, h=25, center=true); } ```

And it will show up with a pretty background, and line numbers.  That’s quite an incentive to improve my WordPress skills.

# More Plastic Parts for Construction

After creating the first quad connector, I thought about how I could improve upon the design.

The design is pretty straight forward.  I want to support some slats of corrugated plastic panel that are arranged in a torsion box fashion.  That just means the pieces interlock with each other with notches cut into them to accomodate the overlap.  The support piece simply has slots within which the slats can slide and be supported.  For the base of the connector, I didn’t want to simply make it square, because I don’t think that is the optimal shape for saving on plastic.  So, in the first design, I reduced the amount of plastic by cutting out the corners with a cylinder.

This design works well enough.  As you can specify the length and depth of the supporting walls, it’s fairly straightforward to specify exactly how much support you want out of the piece.

There are two sides to a circle though.  I decided to try it the other way, by making the connector base circular.

This one has the advantage that it does not have the sharp edges on the base like the previous one does.  It still has sharp edges on the supports though.  I think this one is more aesthetically pleasing, and easier to “understand”.  It’s just a round cap that goes over some joined pieces of material.

As this is as much a study in how to use OpenScad as it is about creating an actually useable piece, the OpenScad code has been updated in the github library.

One of the nice things about OpenScad, since it is a text based modeling program, is that you can easily change some parameters and generate a different result.  In this particular case, the primary routine is called ‘connector()’.  The full list of parameters is as follows:

connector(length, thickness, height, gap, corners, hole, style)

This last parameter determines the style of the connector.  It can be either ‘CIRCULAR’ or ‘CUTOUT’.  The default style is ‘CIRCULAR’.

Just to pull it altogether, I created a more complete image of the torsion box.

This shows a more completely, if fairly minimal, torsion box, with circular connectors installed on all of the vertices.  The torsion box can be of any size, and utilize any appropriate material.  My original intent was to use the corrugated plastic panel.  As I have not as yet tried, I don’t really know how well it will work.  I tend to think the connectors will actually face down to the ground, leaving a totally flat surface on the top to support flat flooring panels.

The code found in connector.scad is fairly straightforward, with an emphasis on modularity.  I could have coded this up in a single routine, but instead I broke it down into constituent parts so the design could be easily modified, altered, and improved.  If you’re into studying OpenScad code, this might be a good one to look at for a simple object.

Up until recently, I was living in a very large house, that was out in the Suburbs.  The house was too big, and the neighborhood too lonely, so we’ve decided to move to a high rise apartment in the city.  Before making the move, I wanted to do one last thing on Thingiverse before packing up the computers.  Recently, I was reading some posts on the OpenScad mail alias, and someone mentioned lighting.  That got me to thinking, ‘why can’t I deal with my own lighting in OpenScad?’  Well, for one reason or another, it’s just not possible.  OpenScad does not really expose any of the rendering pipeline to the script interface, so all you can do is your basic CSG modeling, as the renderer is just a part of the display system.

OK, so I can’t really get into the rendering pipeline, or can I?…

One of the common aspects of any rendering pipeline is doing things with “textures”, which is nothing more than a fancy term for a picture.  In classic 3D rendering, the texture object is used to give some life and, well, texture to objects displayed in a scene.  Textures are most typically like decals.  You just paste them onto the surface of what you’re viewing.

So, I created this thing: http://www.thingiverse.com/thing:11616

OpenScad has no native ability to do much with images within the CSG environment, so I had to start from the ground up.

First, I’ve got a generalized technique for representing “objects” in the environment.  OpenScad does have arrays, and you can put anything into an array, even a mix of numbers, and strings.  The first thing I wanted was a way to represent an image.  I need the basic following information:

width, height – in pixels

maxvalue – the scale of the numbers used to represent the pixels

values – an array that contains the color values representing the image.  There should be width*height*cpe numbers in the array.

cpe – components per element.  How many of the numbers are used per pixel

Now that I know how I want to represent an image, I want a convenience routine to make it easy for me to use:

image(width, height, maxvalue, values, cpe) = [width, height, maxvalue, values, cpe];

So, if I had image data that looked like this:

values = [0,0,0, 255,255,255,  255,255,255,  0,0,0];

checker = image(2,2,255,values,3)

I would get the representation of a 2×2 checkerboard pattern.  That’s a great thing, but does not quite represent an ability to do texture mapping.

The next thing I have to do is get individual pixel values out of my image.  First, is simply getting pixel values:

image_getpixel(checker, 0,0);

This will return an array with the pixel value located at [0,0] (starting from 0), which so happens to be the color value:

[0,0,0]

similarly, image_getpixel(checker, 1,0), would return the value [1,1,1]

Note that the color values have been normalized.  That is, take the raw picture data value, and divide it by ‘maxvalue’ and you get numbers between [0..1].  This is what OpenScad can deal with, so it makes sense to just do it from the beginning.

That’s great.  This in and of itself is a good step towards allowing texture mapping to occur.  How would you use it?  Well, first, you need to be in control of the generation of your objects, down to the individual facets (triangles).  If you are, as I am when generating Bezier surfaces, then you can simply apply the color to each facet.  The easiest example would be generating a replication of the image, and creating a cube with the color for each pixel:

for (x=[0:width-1])

for(y=[0:height-1])

color(image_getpixel(image, x,y))

translate([x,y,0])

cube();

That’s nice.  Now you have a little picture where the ‘pixels’ are each 1mm in size, and each pixel in the image matches one of the little cubes.

But wait, we don’t want there to be a direct connection between the number of pixels in the image and the size of our object.  Also, I don’t want to have to know the actual size of the image.  I want to be able to write routines in such a way that I can assume a single size for all images, and magic will just happen.

Therefore we need ‘normalized’ pixel locations, which will be the subject of the next post.

# Improved Tubing Connector

The first version of the tubing connector looked nice, and was functional in theory, but it was a bit challenging to actually print on my Up! printer.  There was an ‘overhang’ which required the use of support, and that’s kind of a waste of plastic on such a small piece.  Besides that, it was hard to print at very small sizes.

I made a couple of improvements to the design

So, now there is Flanged Connector – V2.  On this connector, there is a cylinder located at the hub.  This provides a nice strong flat spot from which the rest of the print can be supported.  Also, the tubing coming from that center out to the flanges is now the same diameter as the widest part of the flange.  This ensures there is no overhang for this portion of the object.  This design is much easier to print than the first one, even down at smaller sizes.

One of the challenges is that with small sizes, depending on your printer, there might be support material inserted inside the tubing.  That’s very hard to get out.  But, if you’re just using them as connectors, and not flowing liquid through them, you can just leave the support material inside.

I have used the connectors to form my favorite tetrahedron, albeit in spherical form.  This is vinyl tubing with a 1/4″ interior diameter, and the connectors were done with a 5/16″ outer diameter.  The fitting is snug for hand pressing.  The tubing is removable, but won’t come off easily.  I imagine that if I had used a spot of glue, it would be permanently affixed.

I am very pleased with the outcome of this design.  It is a useful item, that required only 2-3 hours of work between the first and second, and third iterations.  Now I have something added to my little toolbox of bits of plastic that can be used to do interesting things.

# Useful Household Items – Drip irrigation connectors

One of the first things I did when I purchased my Up! printer was to think about how it could be used to produce little things around the house.  To wit, I designed things like the electrical outlet cover plates, and other things with the ‘athome‘ tag on Thingiverse.

although it is very interesting to design many things that can be used in the home, sometimes it is not practical to actually print them out.  For example, the light switch cover plate, in order to print it requires quite a lot of plastic.  Given that these can be purchased for relatively cheap, since they’re so common, it’s not particularly cost effective to print at home.

But, other items make more sense, either because they are scarce, or so unique that you could not get them otherwise.  I recently pursued creating such an item.  It is the Flanged Connector.

There are several uses for this connector.  The most basic is to use it as part of a drip irrigation system.  You would insert this into the end of a piece of drip irrigation tubing.  Since it is hollow, it will allow water to flow through with ease.

When I designed it, I wasn’t actually trying to create something for drip irrigation.  I was after being able to easily connect drinking straws together.  I had purchased this book: Messing Around With Drinking Straw Construction.  I wanted to construct some structures with my daughter.  The method of connecting the drinking straws was to insert paper clips into the ends of the straws.  Where multiple straws came together, there were multiple paper clips interlocked.  It was that or pin, or tape.

As a general mechanism, it has the advantage of ease, and low cost.  Paper clips are readily available by the thousands, at relative low cost.

But, we found the technique to be a bit cumbersome.  The straws were not stiff enough, and the paper clips could not hold shape at the vertex, so it required multiple hands, or support to keep the structure from simply collapsing until the thing was entirely finished.

I wanted a better way to connect the straws, and at the time (around 2004), I was dreaming of a simple connector that I could stick into the ends of the straws.  I contacted an injection molding outfit, but they wanted a few thousand dollars to develop the design, do the mold, before anything was actually created.  I pretty much left that idea behind and bought quite a few Zome Tools instead.  That construction set worked quite well, and was more than sufficient to meet our needs.

Recently, I have been cleaning out the house, in preparation for a move, and came across the bundle of about 1,000 straws I had purchased so many years ago.  That bundle reminded me of the little connector that I wanted to create at that time.  Now, 7 years later, I actually know how to use 3D design tools, at least OpenScad.  Now I realize that what I want to create is a simple matter of a few odd shaped cones and cylinders, attached at various angles.

After going back and forth on a few design iterations with Sjoerd de Jong, there now exists a ready made library of routines to construct just about any type of vertex you want.  The Vertex Generator allows you to create vertices for a few well known solids, as well as create vertices of your own.  It utilizes some vectors that you pass to a routine, and it does the rest.

One of the things that I wanted from that library was the ability to separate the printing of the arms from the vertex math itself.  The library goes far down this path, and you could imagine replacing the ‘rod connectors’ with these tube inserts instead.

But, not needing the full generality of that library, I decided to start from scratch and create this imple connectors.  My requirements were that I could stick them into straws, as well as vinyl tubing.  I also wanted there to be some ready made connectors for common tasks.

I start with a straight connector, which can be used to simply join two pieces together in a straight line

This comes from the print_connectors(num) module.  Basically, the ‘num’ parameter is the number of connectors you want to be connected.  In this case, it was a simple matter of calling it: print_connectors(2);

You can call it similarly with any number of connectors.  There is a global parameter ‘standoff’.  This is the amount the based of the flanged connector is offset from the origin.  In the case where you have a high connector count, like 8, you might want to increase this size to accomodate the multiple connectors.

In this case, I used print_connectors(8); and ‘standoff=10;’.  This leaves enough space for the tubing to slide over the connectors, as well as get pretty close to the hub.

The ‘print_connectors()’ module is fairly handy when it comes to creating a set of connectors that are spread evenly around the hub.  There are some cases where you might like to specialize though.  Elbows are acommon connection type, as are ‘T’s and risers.  The library currently includes a module to create elbows.

Here, there are two elbows.  The first is specified with ‘print_elbow(angle=90, \$fn=24);’, the second is printed with ‘print_elbow(angle=120, \$fn=24);’.  In this way, I have specialized for the case of printing two connectors with an angle between them.  Really, this could be generalized to specify any number of vectors in a list, and let the print_connectors() module do the right thing.  That may be an exercise for the next round of these routines.

The really nice thing about this design is the hollow tubing.  Meaning, these are like pipe connectors, in that they will allow liquid to flow through the connector itself.  That ‘liquid’ can be anything.  One example might be colored water that is placed inside the straws, just to make things look interesting.  But, it’s just an open space.  These connectors could easily be used to route cables, with attached conduit.  You could use vinyl tubing to route wiring in a device, and put a little bit of elwire in there as well to light up the path.

One other Idea I had, for forming rigid framing, is to use vinyl tubing to connect things, then fill the tubing with some hardening foam, or epoxy, or something else that can first flow as a liquid, then harden into something much more strong and rigid.  That might be a great way to ship some simple structure, and assemble it without having to worry about long strong members.

Lastly, printing these things, although relatively inexpensive, is quite challenging.  The amount of overhang, and the tiny proportions, will challenge most DIY 3D printers of today.  Ideally, this would be injection molded, which was my original intentions.  But who knows.  Over time, being able to render such a design, inexpensively, and quickly, at home may become a reality fairly soon.  When it is more feasible, I can imagine the joy children and adults will have when they want to create a new toy for themselves, they go to their insta-printer, and out comes the results they expect.

# Geodesic Math in OpenScad – Part 2 (of some)

Last time around, I introduced the very simplest concepts related to geodesic math, namely the spherical coordinates.  Now, let’s go one step further and utilize these spherical coordinates to do some calculations.  Here, I will reference the book: Geodesic Math and How TO Use IT by Hugh Kenner.  Although this book was originally written in the 70s, I found it to be the most descriptive when it comes to exploring the details of creating geodesic structures.

First of all, in order to form a geodesic structure, you can start by choosing a platonic solid.  For the purposes of the geodesics I’ll be dealing with, I use the Octahedron, and the Icosahedron.

One of the curious aspects of a platonic solid is that all of its vertices touch the circumscribed sphere of that solid.  That is the sphere the exactly fits the outside of all the points on the solid.

Given this funny nature, it is relatively easy to imagine extending various other points of the solid to meet the sphere.  Why bother?  Because each of the points extended in such a way represents another vertex, and when you’re actually building a geodesic dome, each vertex is a connection point for a stick or ‘strut’.

In the picture, we have an octahedron with its center at [0,0,0] of the cartesian coordinate system.

There are a couple of ways of specifying the coordinates of the vertices in the basic solid.  One is to simply use cartesian coordinates all around, thinking about the edge length, rather than the radius.  To do that, the mid-section might be easy enough:

In clockwise order, starting in the positive x/y quadrant: [1,1,0], [1,-1,0], [-1,-1,0], [-1,1,0]

Then you have to calculate the vertices that are up at the top on the z and at the bottom.  If you’re used to such things, you just pull out your handy dandy calculator and come up with the result.  If not, perhaps you’d prefer the spherical coordinates.  So, to do the same exercise in sperical coordinates, and instead of using an edge length, you use a radius of 1, and rotate around z for easier angles, you’d have: sph(0,90,1), sph(90,90,1), sph(180,90,1), sph(270,90,1)  And then, for the top and bottom: sph(0,0,1), sph(0,180,1)

Remeber from the last article, in spherical coordinates, sph(longitude, latitude, radius)

Using this technique, and using the sph_to_cart(), and sph_from_cart() functions, you can easily go between the two systems.  This is good because OpenScad knows about how to draw things based on cartesian coordinates, not on spherical coordinates.

The next step involves picking those intermediate points on the face of a platonic solid, and projecting them out to the circumscribed sphere.  As it involves a picture to demonstrate the technique, I’ll save it for the next post.

In the meantime, here’s the link to the current version of the code: Platonic Solids for OpenScad -v0.7

With this code, you can play around with the various solids, display in wireframe, take a look at duals of the forms and the like.  It’s all good preparation for the next round.