Sunday, October 23, 2016

2D Patterns in Grasshopper

This post covers building Grasshopper definitions for generating parametric 2D patterns. This includes using the built in grids Rectangular, Triangular, Radial, and Hexagonal. It also covers Voronoi patterns. Grid manipulation using attractor points, and attractor curves is covered. Finally, use of the Graph Mapper and Image Sampler is covered.

Rectangular, Triangle, Hexagonal and Radial Grids

This section describes a number of components for creating grid.

Rectangular Grid

A very common arrangement is the rectangular grid. This is created with the Rectangular component in Grasshopper. You specify a plane for the grid, sizes in X and Y for each cell, and extents in X and Y (the number of cells in each direction).

The cells are output as a data tree. For more information on Data Trees and their usage please see Data Matching and Data Trees in Grasshopper.

You can find the center of each cell using the Area component. It computes the area of each cell and also outputs the center point. This component is very useful in many pattern making definitions.

Triangular Grid

You can generate 2D grids with triangular cells using the Triangular component. The parameters are the same as the rectangular grid. 

Hexagonal Grid

You can use the Hexagonal component to make 2D grids composed of hexagons. Same parameters as above.

Radial Grid

The grid cells of radial grids build outward from a center point and radiate in a circle. 

Voronoi Diagram

A Voronoi diagram starts with a group of points in a plane. These points are called the seeds. The cells in the diagram are drawn such that all the points contained within a cell are closer to the seed point in the cell than to any other seed points.

You can use the Rhino Points command to place points on a plane then use the Grasshopper Voronoi component build the cells. Here's a simple example:

The Voronoi diagram is a dual of its Delaunay triangulation. This can be created with the Delaunay Mesh component. Make sure Display > Preview Mesh Edges is selected in the Grasshopper drop-down menus. The Delaunay triangle mesh is shown below in green.

You can automatically generate random points using the Pop2D component. There are sockets for the number of points and also a seed value. Different seeds result in different randomized point arrangements.

There is a socket for a Boundary which will form an outer edge to the cells. A convieient component for this is the Bounding Box component. Make sure that the Union Box option is checked:

Smooth Voronoi

The Voronoi cells are degree 1 curves (polylines). You can use the control points to build new curves which are smooth. This gives a different effect to the diagram.

The definition follows. It simply divides the Voronoi cell polylines into the specified number of points. Then these are used as input to create a new, smooth NURBS curve. You can add an additional Offset curve component to get some more space between them if you like.

Attractor Point

A popular pattern in architectural modeling and fabrication is the use of attractor points and curves. These distort or influence a pattern by exerting a force (often a scale) on the geometry based on the proximity of the parts of the pattern to a point or curve.
The Hexagonal component generates a hexagon grid. The Points output socket returns the centroid (center point) of each hexagon. This is used in the distance measurements and as the center of scaling of the hexagons.

The Distance component is the key to this definition. It measures the distance from the attractor point to the center of each cell. It outputs this distance as a list, one value for each hexagon in the grid. Normally, you want the geometry to scale smaller near the attractor point.

You also need to adjust the influence of the effect. This is done with the Division component. It divides the distance by a factor, shrinking its effect or area of influence.

Finally, you usually want to limit the effect to shrinking the hexagons rather than enlarging them (which visually breaks the grid). So a Minimum component is used to return the smaller value – the scale factor or 1. So anything larger than 1 will be set to 1 exactly. This keeps the distant hexagons beyond the range of influence at their original size.

Attractor Curve

Rather than a point, a curve can be used. It is the proximity of each cell to a curve which determines the scaling.

The essential component in this definition is Curve Closest Point. This takes a list of points as input (these are the center of each hexagon) and a curve. It returns a list of the distance of a hexagon center to the closest point on the curve for that center point.

In the provided sample Grasshopper file the curve is saved in the GH file. Therefore you can't edit it. However you can draw your own curve and then right-click on the Curve component and "Set One Curve" to use your own. Then you can alter it as you wish.

Graph Mapper

The Graph Mapper component is very useful for transforming a list of input values using a graph function. The following examples make this clear.

This definition uses a Series component to generate a list of values in the range 0 to 0.9. This list of values is fed into the Graph Mapper. It takes each value (which can be thought of as the X axis on the graph) and returns a list of values where they hit the Y axis on the graph. In this example below the graph is a straight line at 45 degrees. So the output value matches the input value. To make this graphically clear the X value is fed into a Construct Point component. The output of the Graph Mapper is fed into the Y value.

You can see the resulting points in the viewport - a straight line which matches the graph:

If you hook up a Nurbs Curve component it will draw a curve through the points as shown below.

Also the graph type has been changed. This is done by right-clicking the Graph Mapper and choosing a new type from the Graph Type fly-out:

Graphs usually have grips (small circles) which can be used to modify the variables that control the graph. Below a sine wave graph type was used. The grips have been pulled a bit to reshape the graph. The resulting curve drawn in the Rhino viewport shows the matching result.

The power of the Graph Mapper comes from its ability to map any range of values. You can double click the component to edit the expected input range as well as the desired output range:

Alternatively you can remap the input range to 0 to 1 and not have to change the range. You can do this using the Bounds and ReMap Numbers components as shown below:

You can compare the values in the two Panels. Note how the input range (0 to 18.36) has been remapped to the Target range (which defaults to 0.0 to 1.0). The Mapped value could then be fed into the Graph Mapper.

Colorize Cell Based Patterns

By adding three components it's possible to colorize the pattern. These components are Boundary Surfaces, Gradient, and Custom Preview. The idea is to create a planar surface from the closed curves (cells), generate a color for each one, then preview it in the viewport. Here's an example which also uses a Graph Mapper:

Boundary Surfaces is given the scaled geometry. It outputs a surface which is needed so color can be assigned. The Gradient component is fed the distance values as modified by the Graph Mapper and generates a color accordingly. Much like the Graph Mapper the Gradient expects values with a specified range. In its case between it's Lower Limit and Upper Limit. The color is then generated and fed into the Custom Preview which shows it in the viewport.

You can use the right-click menu on the Gradient to change to different built in color gradients.

Image Sampling to Modify Cells

The Image Sampler component lets you use an image file (bitmap) to generate data. In our case, this data can be used to modify a grid. Here's an example image and modified rectangular grid:

The grayscale value in the image is used as a scale factor for the cells in the image. White pixels generate a scale factor of 1.0. Black pixels scale to 0.0.

A grid of points is needed to sample the image. By default these need to be in the range 0.0 to 1.0 in both X and Y. Various aspects of the image can be sampled, for example individual Red, Green or Blue values or the grayscale value.

You can simply drag and drop an image file onto the Grasshopper canvas to create an Image Sampler with that image assigned. Double click the component to bring up its settings dialog: 

Here you can set the expected range of values in X and Y. You can control how values outside that range behave (clamped, tiled, etc). You also set the channel used to sample in this dialog. If you check the "Save in file" option the image is saved in the GH file.

Getting Edge Curves from a Complex Pattern

It's common to have a set of overlapping curves which you'd like to break up into individual non-overlapping regions. Here's an example - some overlapping curves:

Here they are, broken into non-overlapping regions, assigned random colors to make it easy to visualize:

Here's the GH components necessary. The key component is Surface Split. You simply wire in the curves and it breaks them up into surface fragments bounded by the lines. In the example below the bounding box is used to put a square region around everything. Then they are previewed using Random, Gradient and Custom Preview.

Here's an example with many overlapping curves:

And here they are broken into 460 individual regions:

Each region is its own surface. If you wanted to get the edge curves you can use Brep Edges. Then use Join Curves to join each branch of edges into its own closed curve. Then you can Flatten into a single list of closed curves.