Extrusions

_images/extrusion-shape-vs-path.png

Left (cyan): a circular shape extruded along a rectangular path. Right (magenta): A rectangular shape extruded along a circular path.

Basics of Extrusions

An extrusion object is produced by extruding a 2D shape along a 3D path.

myext = extrusion( shape=myshape, path=mypath )
Parameters:
  • shape (list) – A list of x-y pairs, each itself a list, describing a closed figure in the xy plane.

  • path (list) – A list of 3D vectors denoting a path.

The following code creates a triangle shape in the xy plane and extrudes it along a straight line path in the +z direction, making a long wedge:

triangleshape = [ [2,0], [0,4], [-2,0], [2,0] ]
linepath = [ vec(0,0,-4), vec(0,0,4) ]
wedge = extrusion( shape=triangleshape, path=linepath, color=color.magenta)
_images/extrusion2.png

Other attributes of extrusion include:

myext = extrusion( shape=myshape, path=mypath, color=color.red, smooth=0.9, scale=3, twist=0.1 )
Parameters:
  • color (vector) – Color of extrusion. Default is color.white

  • smooth (scalar) – If the cosine of the angle between adjacent path elements is greater than this number, smooth the joint between them. Default: 0.95 (an angle of 18 degrees).

  • scale (scalar or list) – If scale is 3, all joints are enlarged by a factor of 3. If instead you give a list of numbers, such as “scale = [2, 3, 0.5.]”, the initial cross section is scaled by a factor of 2, the second by a factor of 3, and the third by a factor of 0.5. For a list, the number of scale factors must be the same as the number of points along the path.

  • xscale (scalar or list) – Same as scale, but in x-direction only.

  • yscale (scalar or list) – Same as scale, but in y-direction only.

  • twist (scalar or list) – Angle in radians (or list of angles) by which subsequent segments should be rotated about path. For a list, the number of twist factors must be the same as the number of points along the path.

  • start_normal (vector) – Outward-going normal to the starting face. By default this is determined by the direction of the path from the starting point to the next segment on the path. This attribute has no effect if the path is closed.

  • end_normal (vector) – Same as start_normal but for end face.

  • show_start_face (boolean) – If False starting face is left open. Default True.

  • show_end_face (boolean) – Same as show_start_face, but for end face of extrusion.

  • smooth_joints (list) – A list of path points, by position in path, whose joints should be smoothed, regardless of overall setting. First position in path is 0. Setting smooth_joints=[2,5] turns on smoothing for these joints.

  • sharp_joints (list) – Effect opposite to smooth_joints, with same syntax.

  • group (object) – The group to which this object belongs.

_images/extrusion-twist.png

Above: A rectangle extruded along a linear path with a twist.

Objects with Holes

A path is always a single set of points. A shape, however, can have holes in it. If the shape has holes, it will contain multiple sub-lists: the first list denoting the outer perimter of the shape, the others each specifying the perimeter of a hole within the outer perimeter. Holes must not overlap each other.

outershape =  [[0.5, -0.5], [0.5, 0.5], [-0.5, 0.5], [-0.5, -0.5], [0.5, -0.5]]
innershape = [[0.35, -0.20], [0, 0.40], [-0.35, -0.20], [0.35, -0.20]]
zpath = [ vec(0,0,0), vec(0,0,0.5) ]
thing = extrusion( shape=[ outershape, innershape ], path=zpath, color=color.yellow )
_images/extrusion_1hole.png

Common Shapes and Paths

VPython provides a library of commonly used shapes: closed figures in the xy plane. Each shape is a list of precalculated x-y pairs. You can use these in making an extrusion:

myextrusion = extrusion( shape=shapes.rectangle(width=10, height=6), path=mypath )

The paths library in VPython contains the same figures as does the shapes library. Each path, however, is a list of precalculated 3D vector positions.

To extrude a triangular shape along a circular path, you can employ these libraries:

trishape = shapes.triangle(xscale=0.5, yscale=0.3)
circpath = paths.circle(scale=2)
oddring = extrusion(shape=trishape, path=circpath, color=color.yellow)
_images/extrusion3.png

For detailed documentation on these libraries, see Shapes and Paths Libraries

Color Blending

As with a compound object, setting the color of an extrusion that was made with a color other than white makes a multiplicative blend of the overall color with the original color. For example, if the color of the extrusion was originally <0,1,1> (cyan), and you later set the color to <1,1,0> (yellow), the blend results in <0*1, 1*1, 0*0> or <0,1,0>, which is green. If you plan to vary the color after creating the extrusion object, start with color.white, which is the default.

Hemispheres

There are two ways to make a hemisphere using an extrusion. The first involves extruding a quarter-circle arc shape along a circular path using an offset (pos):

hshape = shapes.arc(angle1=0, angle2=0.999*pi/2, radius=0.5, thickness=0.01, pos=[-0.5,0] )
hpath = paths.circle(radius=0.5)
demihemi = extrusion( shape=hshape, path=hpath, color=color.green)
_images/extrusion-hemisphere-1.png

The second way involves extruding a circle shape along a linear path, but adjusting the scale factor for the circle at each step along the path:

rr = 1
N = 400
x = 0
dx = 1/(N)
scalef = []
hpath = []
for i in range(0,N+1,1):
    rad = sqrt(rr**2 - x**2)
    scalef.append(rad)
    x = i*dx
    hpath.append(vec(0,x,0))
hem = extrusion(path=hpath, shape=shapes.circle(radius=rr), scale=scalef,
      show_start_face = False, color=color.magenta )
_images/extrusion-hemisphere-2.png