## Sunday, November 11, 2018

### Kangaroo Physics in Rhino 6

This topic is an introduction to the Grasshopper feature Kangaroo which is a set of components for form-finding, physics simulation, and geometric constraint solving.

This post uses the version of Kangaroo built into Rhino 6.

### First Example - Project to Plane

As a first example let's look at simply moving a set of vertices onto a plane. You don't actually need Kangaroo for this - because you can do it in Rhino  ProjectToCPlane. But it's a good, simple example to get going and explain the key Kangaroo components and their use.

The first two components on the left, Box 2Pt and Populate 3D define a box and populate it with random points.

There are two Kangaroo components in this definition. One is a Goal and the other is the Solver.

The goal in this case is OnPlane. This moves a point to a given plane (and keeps it there). If you supply many points they are all pulled down to the plane. There are many goals supplied by Kangaroo. In general they take the current position of some points and output some target positions it "wants" the input points to move to. You can supply as many goals as you like and Kangaroo will adjust the points in order to meet all the goals.

The other component here is the Kangaroo Solver. This is the one which collects all the goals and solves the system. It outputs the "solved" vertex locations. In this particular example it outputs the points as moved to the plane.

There is also a Button component wired into the Reset socket. This resets the system and begins solving it again. The solver automatically runs over and over until it converges at a solution. Then it stops automatically. In some cases, such as this simple example, it converges so fast it stops immediately. That's because it can be mathematically calculated to instantly move points onto a plane. Other kinds of goals take iterations. For example making connected polygons all planar. That's an example we'll look at later - it often takes a few seconds to converge to a solution. Because the solver loops over and over converging towards a solution you can see it gradually solve the system.

In the simple definition above the Vertices output from the solver are wired into a Delaunay Mesh component which generates a mesh between all the points. If you press and hold the Reset button you'll see the mesh drawn through the original points. When you release the button the system is solved and the mesh is drawn through the planar points.
Meshed points before solution

Meshed points after being moved onto the ground plane

### Catenary Mesh

This definition is slightly more complex but is much more useful. It's a form finding technique used to generate structures in pure tension or compression. The term catenary refers to a curve formed by a string, or chain hanging freely from two points and forming a U shape.

This can be expanded to process the vertices of a mesh into a three-dimensional form:

The definition operates on a mesh. In this example the start mesh is a rectangular grid as seen on the ground plane. Shown above it is the resulting catenary form as generated by Kangaroo:

It has three goals:
• Anchor: This will keep a point in its original location. This is used to lock the corners of the mesh in place.
• Length (Line): This goal tries to keep two points (line endpoints) at a given distance form each other. With a higher Strength specified the points move less.
• Load: This goal applies a force to the points. The force is specified as a vector and the length of the vector is the magnitude of the applied load.
You can add a slider to the Strength component to control the stretch of the form. You can also affect that using the magnitude of the vector wired into the Load component. For example wire a slider into the Factor socket of the Unit Z vector.

#### Generate a Mesh of the Results

You can bake the O output of the Solver. This generates the set of lines of the form. You can use the Weaverbird Mesh From Lines component to create a Rhino mesh. Then use other tools such as Rhino's OffsetMesh command to turn the result into a mesh solid suitable for 3D printing or rendering.

#### Using Your Own Mesh as the Form

This basic definition can be modified to use your own mesh. Here's an example of using a few tools to build a base mesh.

Use the Rhino menu Mesh > Polygon Mesh Primitives and choose something like a Truncated Cone.

Explode that mesh and delete the top and bottom faces:

Use the ProjectToCPlane command make the mesh flat: Mirror the mesh and Join it together into one.

Press F10 to turn on the mesh vertices, then select two and use the Gumball to scale them with a value of 0. Do this for both side:

You can use the MeshRepair command to weld the mesh together, leaving only the outside edges as naked.

This mesh can be wired into the definition. It works nicely with Weaverbird to subdivide the mesh prior to running. More information on LunchBox and Weaverbird can be found in Working with Meshes in Rhino and Grasshopper.

This results in a structure like this:

When you Bake the output of the Kangaroo Solver is only lines. It doesn't output a mesh.

To convert it to a mesh you can use a Curve component to collect the lines then the Weaverbird Mesh From Lines (Weave Back) component to generate the mesh:

If you bake the output of the Mesh From Lines you'll get a proper Rhino mesh.

#### Draped Forms

You can also invert the force so it is downward. If you include some extra vertices you can simulate a draped fabric structure.

To do this use a Unit Z component with a negative Factor to push downward (in the example below, the expression on the Factor socket is -x which simply negates the value). The use a List Item component to pick out a few vertices to suspend from the center. Make sure to Merge these and Flatten the list before wiring into the Anchor component as shown.

The result is the form draping between the points which suspend it:

Note the components required if you want to convert this to a mesh. The O output of the Solver is a tree - one branch is the Anchor verts. The other are the entire mesh verts wired into Load. You need to grab only the Load verts:

You can do that with the Tree Branch component. This will isolate the mesh verts only. Then, wired into Weave Back you get a mesh.

### Tensile Forces

Similar to the example above, but not using a Load, you can explore some tensile forces on meshes.

By using a simple Kangaroo definition you can experiment with a surface that behaves a little like pulled, bending fabric. If you use the Grab component you can pull the points interactively to shape the form.

By holding the Alt key down and dragging on vertices in the mesh you can pull them as if pinching and pulling on that area of the fabric:

Another use for tensile forces is to smooth, or relax meshes. The second definition in the above file does that.

These work best with Quad meshes - meshes composed of four sided polygons rather than the triangle meshes normally generated by Rhino. I'd recommend using a better quad mesh modeler like 3ds max, Maya, modo or ZBrush. Or you can try the QuadrangulateMesh command in Rhino.

The original mesh is subdivided using Weaverbird. In this case using Catmull-Clark. The Weaverbird Join Meshes and Weld component makes sure there are no duplicate edges. The Weaverbird Mesh Edges component will output all the edges. These are wired into a Line component to ensure sure they are line entities. Then the Curve Length, multiplied by a factor, is wired into the Length goal (of Kangaroo).

The Weaverbird Naked Boundary component outputs all the open edge curves. These are Exploded then using the End Points component the start point of the lines are found. These become Anchor goals. By altering the strength of the anchor goal the form can remain closer to the original mesh or highly relaxed as shown below.

### Mesh Relaxation

You can use a technique called Mesh Relaxation to generate much more aesthetically pleasing meshes than those produced by regular tessellation of a form. Here's an example - this is roof of The British Museum in London. Kangaroo was used in this project. Here are photos of the roof - a beautiful, flowing arrangement of triangular panels:

The Great Court of the British Museum in London

Here's the mesh, prior to and after relaxation. Note the curving, flowing nature of the triangles. Rather than uniform, straight, hard lines there's a much more natural feel.

And here's the relaxed Kangaroo version in perspective. You can see how the roof bulges away from the edges to control the flow of water towards the edges. This is accomplished by pulling towards a second mesh which has the correct shape:

The goals in this definition are:
• OnCurve: Pulls a point towards the closets point on the specified curve.
• Anchor: This will keep a point in its original location. This is used to lock the corners of the mesh in place.
• Length: This goal tries to keep two points (line endpoints) at a given distance form each other. With a higher Strength specified the points move less.
• Show: To see the iterative solving of the mesh you use Show to see each step in the process.
• OnMesh: Pulls the vertices to be on the specified mesh.

### Circle Packing

This definition is a simple example that uses the SphereCollide Kangaroo component to pack circles onto a surface. The circles can be offset and extruded to achieve an effect similar to the one below. Note that in this definition all the circles have to be a uniform size. That's because the SphereCollide component is optimized for that condition.
Pavilion made of cardboard hoops by students at ETH, Zurich, Switzerland

Here's the same surface with 120 circle:

Here's a sample surface with 1000 circles:

You can add a few components to get a 3D effect. First each circle is offset using the Offset Curve component. The extrusion process is achieved by using the Weave component to create a list with a circle followed by its offset. So the list is doubled in length. But it runs in pairs of circles. The next component is Partition. That breaks up the list into a tree - where each branch contains a pair of inner and outer circles. Then Boundary Surface is used make a surface for these which contains the hole in the center. This is then Extruded along the normal of each circle pair. The Amplitude component lets you set the thickness.

### Planarize Hexagons

This definition will take a surface, panel it with hexagonal cells, and attempt to planarize each cell. The process of taking a surface, paneling it and making it ready for fabrication is called rationalizaion. One of the nicest methods is to planarize the panels. They can then be easily manufactured from sheet goods (for example plywood).

Here's an example that used planar panels and robotic fabrication for the edge joints.

Landesgartenschau Exhibition Hall / ICD/ITKE/IIGS University of Stuttgart

This definition relies on LunchBox for the initial hexagon paneling. It generates a flat list of hex cells.

The next section gets things ready for planar paneling solving. The Explode component breaks each cell into individual segments. The output is a tree. The End Points component output start or end points of each segment. In this case we are only worried about the start points.

The key goals used are:
• CoPlanar: Pulls a set of points towards their best fit plane. This can act on any number of points.
• ClampLength: Keeps the distance between 2 points between the specified limits, but applies no force when the distance is within these limits.
The start points are wired into the CoPlanar goal.

We need to limit the amount the segments are allowed to move. This keeps the segments all reasonably sized relative to one another. This is done by using the Curve Length and Average components to generate the average length. These are divided by constants 0.4 and 2.0 to set lower and upper length limits.

If the Kangaroo Solver is able to solve the system it outputs the planarized curves. The Boundary Surface component is then used to surface these polygons. The Is Planar component is used to check if the surfaces are indeed planar. It outputs True for all the surfaces which are; otherwise False.

It's important to note that not all surfaces cannot be paneled with planar cells - it simply doesn't work geometrically. But this definition is remarkably successful.

When the surface is anticlastic (as indicated by negative Guassian curvature using the CurvatureAnalysis command in Rhino) the hexagons will look more like bowties as seen above and shown below:

In this example the surface is entirely synclastic and all internal panels are hexagons - although some have very shallow angles and are nearly rectilinear.

### References and Help

As an easily accessible component reference here's the Kangaroo PDF Help File.

Here's an excellent collection of Kangaroo Sample Files. Many of the examples in this blog post are taken from those samples.

### General Links to Forums and Component Code

Here's the main discussion forum for Kangaroo topics. This is a great source to find the latest definitions posted by Daniel Piker.
http://www.grasshopper3d.com/group/kangaroo

Here are some videos by Daniel Piker using Kangaroo (often earlier versions - still useful):
https://vimeo.com/album/199263

Here's the source for additional examples:

## Friday, October 19, 2018

### File Preparation and Layout for CNC Routing

This topic discusses preparing a 3D model for 3-axis CNC routing. It's geared towards working with sheet goods (plywood) and toolpath programming in Mastercam. The comments about CAD software are specific to Rhino. It also is written assuming someone else is going to be routing your job (like the lab assistants in the Taubman Fab Lab).

Important Note: If you haven't read it already please see the topic CNC Router - Typical Machining Workflow for an overview of cutting on a router. This topic assumes you understand the basics of the overall workflow.

### Preparing to Model

Before you begin your final, accurate 3D model, you need to know the type of material and its thickness. So be sure to measure your material before you begin. For typical Baltic Birch plywood it's going to be around 18mm (0.71"). You need to know what your material is exactly.  This has a big impact on how your parts will fit together. I've seen it as low as 0.68" and as high as 0.72". That difference of 0.04 is huge if you want accurate results!

Make sure you use your measured value in your drawings. For example if you have a mortise sized to accept the material you'll want to draw it to the material thickness exactly. I recommend not 3D modeling joint tolerances (extra offsets to allow the parts to slide past one another). Model them as exact fits - then accomplish the offset in the 2D layout portion of the setup. More on this later...

Once you have your 3D model you'll need to generate 2D curves for the router to cut from.

### Commands for Laying Out for Nesting on the Sheet

There are some very useful commands in Rhino to extract the contour and pocket curves from your 3D model. These are:

• Orient3Pt: This command moves or copies and rotates objects using three reference and three target points. This lets you lay a 3D part down flat on the XY Plane, oriented correctly for curve extraction. Rhino Orient3Pt Help
• DupFaceBorder: This will give you back the curves from the entire face of a part. This includes the outer contour and any interior curves for joints. Rhino DupFaceBorder Help
• DupEdge: This creates curves from any selected edge of a surface or solid. Rhino DupEdge Help
• ProjectToCPlane: Use this command to flatten the selected entities directly onto the construction plane. This is useful to ensure everything is entirely flat (no control vertices or anything not at Z of 0.0. Rhino ProjectToCPlane Help
• Make2D: This can generate 2D projections of your 3D model. I personally find this less useful than using the commands above on a part by part basis. Rhino Make2D Help
• SelDup: Use this command, followed by the Delete key, to remove any duplicate lines. Those can confound Mastercam at times (it tries to chain them end to end). This is useful for example after you use Make2D which sometimes introduces lines on top of each other.

### Stock Setup

The stock is the volume of your actual material to route. To create it, draw a rectangular volume (box) for your sheet representing the size of your material. Place one corner at 0,0,0. Place the other corner at the size of your material, for example 4',4',-0.71". For orientation, the short axis of the Fab Lab 3-axis router is 4' and is X. The long axis is 8' and is Y. Note that the top of your material will be Z of 0.0. The stock box should go into negative Z. Everything you wish to route should exist inside your stock boundary.

The lab assistants are taught to program everything absolute. That means all the curves are drawn at Z of 0.0. And they program the router toolpaths to cut to the correct depths. Therefore your layer names should indicate the depths to cut to.

### Layer Naming

Have your Rhino file organized by layer based on the type of cutting that you'll do. You'll likely want Contour, Pocket and Drill layers. You should list the tool to use for that operation as well. You should also list the depth of cut in your layer names. That makes it perfectly clear to the toolpath programmer (lab assistant) what operation to use, what tool to use, and what depth to cut at. Here's an example - this is cutting the guitar body shown below:

The red box is the stock, notice that it is in -Z (the top is at 0 and it is 1.75" thick). There are pockets for the pickups, tone controls and neck, all at different depths. There are drill holes for the bridge and neck. And there's a contour to cut the body from the stock. Here are the layer names - note the operation, tool and depth of cut are listed.

In this case the contour will be cut with a 3/8" diameter compression bit, it cuts all the way through the body, so down to -1.75" (the stock thickness).

The pockets will all be cut with a 1/4" downshear bit. However the depth differs for each one.

Some drill holes are cut with an 1/8" brad point to a depth of -0.25". A larger drill will go all the way through to -1.75".

The 3D model is also included for reference. In this way you can measure the geometry to double-check the depths.

Having your file organized in this way will make it much easier to understand for the toolpath programmer who sets up your job to cut. And much less error prone.

### Corner Preparation

Cylindrical router bits can't cut square corners, as shown below. Picture a top view of two parts that need to slide together. If the router follows the blue line it won't reach into the corner since the bit is a cylinder.

The actual cut part would look like this:

You need to clear out that corner material. The usual method is to drill out the corners. Here the circles are the diameter of the drill bit. You can see they just clear the corner.

When drilled the finished parts would look like this, and would slide together.

I've written a utility that runs in Rhino for setting up the drill holes. You can find it here: CNC Corner Fix Utility. It lets you pick the lines which meet in a corner and it'll will add the drill point automatically.

You can also draw your geometry to move past the corners to remove material. This is usually more problematic because the router has to slow down to get into the corner and change direction. When the cutting slows down the friction goes up. So the cutters get hotter. This reduces tool life and can possibly result in the workpiece or spoilboard starting on fire.

### Offset for Fit

Here are a few example joints. In each one the offsets for getting a nice fit are discussed. Remember - if the parts are cut to the same size they won't slide past one another to fit. If they are cut too loose, you'll wind up with a weak joint. For a review of this idea see Tolerance Issues in Joint Fit.

#### Example Joint 1

This joint has two identical parts which half-lap over one another. This joint provides a lot of glue surface area and racking resistance.

The curves for cutting the joint are shown below.

There are two tenons which nest in two mortises. The tenons should be cut slightly smaller and the mortises slightly larger. This is due to the high degree of curvature in the joint. Thus both sides of the joint are offset slightly.

The orange lines are for a pocket. This should be cut slightly outside the line - outside the boundary of the curve. This will make the pocket slightly larger than the 3D model.

The blue lines are for the joint portion of the contour. These lines should be offset inside - towards the center of the piece. This will make the opening slightly larger than drawn.

#### Example Joint 2

This joint joins two boards together edge to edge using dovetails on each side of the parts.

The curves for cutting the joint are shown below.

When the parts are straight and simple the offset can be tighter. There is a high degree of curvature to this joint. There needs to be extra space between the parts (more than the usual 0.01"). Offset the mortise outside to make it larger by 0.01". Offset the tenon towards the inside to make them smaller by 0.01". This leaves a gap of 0.02" which is larger than usual but necessary to accommodate the curvature. The orange line is a pocket which is offset towards the outside marking larger mortises. The blue line is a contour which is offset towards the inside of the piece resulting in smaller tenons.

#### Example Joint 3

This corner joint features a dovetailed key to hold the parts together. There are three tenons and three mortises.

The tenons should be cut slightly smaller or the mortises slightly bigger. Because these parts are fairly simple without a lot of curvature a single offset on one of the mating parts of 0.01" is sufficient.

Many more specific wood joints can be found in this post: CNC Cut Wood Joinery.

### Preparing for Cutting

What info should you have before you visit the CNC assistant to set up cutting your job?

Make a list of tools you'll need. For a simple job it might look like this:
1. 3/8" compression bit
2. 1/4" downshear endmill
3. 1/8" brad point drill bit
4. 1/4" brad point drill bit
Inform them of your exact material thickness.

You need to be present when they are toolpath programming your job. Make sure you watch the simulation to verify that everything has been set up correctly.