teaching machines

Extrude

June 1, 2021 by . Filed under public, slai-2021.

This post is part of a course on geometric modeling at the Summer Liberal Arts Institute for Computer Science held at Carleton College in 2021.

You’ve now made two flat shapes. Suppose you took one of the shapes and stretched it into the third dimension to produce a solid rod or dowel. That stretching is called an extrusion. If you cut through an extruded solid, the exposed cross section will always be the original shape.

In this exercise you will write a function that will take in any flat-shape that’s expressed in counter-clockwise order and extrude it along some axis.

Draw

On your paper, draw a simple polygon, perhaps a hexagon. Label the vertices with their indices, starting at 0.

Pretend the paper has a third dimension. Draw a second instance of this polygon “behind” it and label its indices, starting at whatever number you left off in the first polygon.

Connect the corresponding vertices with lines. Between two neighboring lines you find a rectangle. Divide each rectangle into two triangles.

This connection between cross sections forms the extrusion.

Function

Write a function named extrude. Have it accept these parameters:

Create your empty positions and triangles arrays. Return them in an object.

Positions

One end of the extruded surface will have positions identical to the positions of the flat shape. Copy these positions into your extruded shape’s positions array using JavaScript’s spread operator:

const positions = [...shape.positions];

The other end of the extruded surface will have similar vertex positions, but they will be pushed along the axis. Loop over the original positions and append a shifted version of each to your positions array.

Render your shape as points. You should see the two cross sections of your extrusion.

Cap Triangles

The first end of the extruded shape will have the same triangles as the original flat shape. Copy over these triangles much as you copied over the flat shape’s positions.

The second end of the extruded shape will have triangles similar to the first end, but they must use the indices of the vertices at the extruded end. What is the arithmetic relationship between the indices of the original end and the indices of the extruded end? Write code to append this second set of triangles, deriving each new triangle from its associated triangle from the first set.

The winding order of these faces must also be flipped since the triangle faces the opposite direction. Test the two ends before moving on.

Side Triangles

The vertices on the two ends must also be connected to each other to form the side walls of the extrusion. Loop through the original set of vertex indices. Build your triangles off of each of those indices.