# Introduction to 3D Modeling. Section 2. Library of Shapes

### 2 Library of Shapes

Objectives:

• Create a variety of 2D and 3D shapes.
• Assign colors to objects.
• Create convex hulls and extrude 2D objects to 3D.

This section serves mainly as a database of shapes and it is intended for reference rather than for systematic study. You may want to browse through it quickly to get an overview what shapes are available, but our goal is to move to design projects quickly. Note in Subsection 2.2 how colors are assigned to objects. Your first project is awaiting you in Section 3 and it will only require cubes and boxes.

#### 2.1 Cube

The command CUBE(a) creates a cube of dimensions a × a × a. Every newly created object comes in a default steel color. To visualize objects, we use the SHOW command, that can be abbreviated by V. Type the two lines below into the PLaSM input cell and press the green arrow button:

c = CUBE(1)
SHOW(c) Fig. 12: Steel cube of dimensions a × a × a with a = 1.

Note that the cube is located in the first quadrant, with its edges aligned with the coordinate axes x (red), y (green) and z (blue). One of its vertices lies at the origin (0, 0, 0). Every cube created via the CUBE command will be positioned like this.

#### 2.2 Coloring objects

The command COLOR, possibly abbreviated as C, assigns a given RGB color to a 2D or 3D object. Its use is best illustrated on the following example, where we change the color of the cube from steel to brass:

c = CUBE(1)
COLOR(c, BRASS)
SHOW(c)

Note that PLaSM keywords are written in capital letters. This is to avoid conflicts with user-defined variables (names of objects, custom colors, etc.). For newcomers to programming – in the above program, c is a user-defined variable (intentionally chosen to be lowercase), and CUBE, COLOR, BRASS and SHOW are PLaSM keywords. After executing the script, you will see the following: Fig. 13: Same cube as before, but now in brass color.

Note that the axes in the grid are color-coded using the word "RGB" for convenience: x = R, y = G, z = B.

The keyword BRASS is just a predefined triplet of numbers [255, 250, 83]. PLaSM offers the following predefined colors:

GREY = [128, 128, 128]
GREEN = [0, 255, 0]
BLACK = [0, 0, 0]
BLUE = [0, 0, 255]
BROWN = [139, 69, 19]
CYAN = [0, 255, 255]
MAGENTA = [255, 0, 255]
PINK = [255, 0, 255]
ORANGE = [255, 153, 0]
PURPLE = [128, 0, 128]
WHITE = [255, 255, 255]
RED = [255, 0, 0]
YELLOW = [255, 255, 0]

and metallic colors:

STEEL = [255, 255, 255]
BRASS = [181, 166, 66]
COPPER = [184, 115, 51]
BRONZE = [140, 120, 83]
SILVER = [230, 232, 250]
GOLD = [226, 178, 39]

#### 2.3 Planar square

PLaSM makes it possible to work in a two-dimensional setting of the axes x and y (in addition to 3D). Here, the z axis is not present. The first 2D command that we will explore is SQUARE(a) which renders a square of edge length a. Its usage is illustrated by the following script:

s = SQUARE(3)
COLOR(s, BRASS)
SHOW(s)

The square is shown in Fig. 14. Again note where it is positioned - all newly created squares are positioned like this. Fig. 14: Brass square of dimensions a × a with a = 3.

#### 2.4 Square as thin 3D solid

The planar square introduced in the previous paragraph is a purely 2D object that cannot be combined with 3D objects. However, sometimes we need to do this – such as when constructing conic or cylindric sections. For this purpose, PLaSM has a command SQUARE3D(a) that creates a square of edge length a in the 3D space. Otherwise the square is located in the same way as the one created via the SQUARE(a) command. Sample script creating a square in the 3D space is

s = SQUARE3D(3)
COLOR(s, brass)
SHOW(s)

In reality, this object is a thin 3D solid (cube whose third dimension was scaled down to 0.001). Later we will learn how to rotate, move, and scale such objects, and how to intersect them with other 3D objects. The output of the above acript is shown in Fig. 15. Fig. 15: Brass square of dimensions a × a with a = 3, as a thin 3D solid.

#### 2.5 Box

The command BOX(a, b, c)creates a box of dimensions a, b and c. Let us change the code in the input cell to

b = BOX(3.0, 2.0, 1.0)
COLOR(b, COPPER)
SHOW(b)

and press the green arrow button. The result is displayed in Fig. 16. Fig. 16: Copper box of dimensions 3, 2 and 1.

#### 2.6 Planar rectangle

Planar rectangle of dimensions a, b can be created with the command RECTANGLE(a, b). For illustration, the output of a sample script

r = RECTANGLE(3, 1)
COLOR(r, COPPER)
SHOW(r)

is shown in Fig. 17. Note that the rectangle is located in the first quadrant with its edges aligned with coordinate axes, and that its bottom-left vertex lies at the origin (0, 0). Fig. 17: Copper rectangle of dimensions 3 and 1.

#### 2.7 Rectangle as thin 3D solid

To operate with rectangles in the 3D space, we have the command RECTANGLE3D(a, b) which works analogously to the command SQUARE3D(a). Its usage can be illustrated using the script

r = RECTANGLE3D(3, 1)
COLOR(r, COPPER)
SHOW(r)

that creates the same rectangle as before but renders it as thin 3D solid. The rectangle is again located in the first quadrant, with one vertex at the origin (0, 0, 0), and its edges are aligned with the coordinate axes x and y. In reality, this is a box whose third dimension is 0.001. The output is shown in Fig. 18. Fig. 18: Copper rectangle of dimensions a × b with a = 3, b = 1, as a thin 3D solid.

#### 2.8 Tetrahedron

Tetrahedron in PLaSM is defined using four 3D points that do not lie in the same plane. Every 3D point in PLaSM is a triplet of real numbers enclosed in square brackets, such as [0, 0, 0] (the origin). To keep our script readable, it is a good idea to introduce new variables for points. Such as, the points [-2, 0, 0], [1, 0, 0], [0, 4, 1], [0, 1, 2] can be called a, b, c and d. Then the command TETRAHEDRON(a, b, c, d) creates a tetrahedron with the vertices a, b, c and d. The output of the code

a = [-2, 0, 0]
b = [1, 0, 0]
c = [0, 4, 1]
d = [0, 1, 2]
tet = TETRAHEDRON(a, b, c, d)
COLOR(tet, BRONZE)
SHOW(tet)

is shown in Fig. 19. Fig. 19: Bronze tetrahedron with the vertices [-2, 0, 0], [1, 0, 0], [0, 4, 1] and [0, 1, 2].

#### 2.9 Planar triangle

Planar triangles are created using the command TRIANGLE(a, b, c) where a, b, c are 2D points (pairs of real numbers enclosed in square brackets). For example, the triangle with vertices [-1, 0], [1, 0], [0, 2] is created using the following code:

a = [-1, 0]
b = [1, 0]
c = [0, 2]
tria = TRIANGLE(a, b, c)
COLOR(tria, BRONZE)
SHOW(tria)

The output is shown in Fig. 20. Fig. 20: Bronze triangle with the vertices [-1, 0], [1, 0] and [0, 2].

#### 2.10 Triangle as thin 3D solid

Knowing how squares and rectangles work in the 3D setting, you will not be surprized by learning that the command TRIANGLE3D(a, b, c) creates a 3D triangle. The points a, b, c are 2D points though, and the triangle will lie in the xy-plane. Let us create the same triangle as in the previous paragraph:

a = [-1, 0]
b = [1, 0]
c = [0, 2]
tria = TRIANGLE3D(a, b, c)
COLOR(tria, BRONZE)
SHOW(tria)

The output is shown in Fig. 21. Fig. 21: Bronze triangle with the vertices [-1, 0], [1, 0] and [0, 2], as a thin 3D solid.

#### 2.11 Sphere

Sphere with radius r and center at (0, 0, 0) is created using the command SPHERE(r):

s = SPHERE(3.0)
COLOR(s, GOLD)
SHOW(s)

The output is shown in Fig. 22. Fig. 22: Gold sphere with radius r = 3 and center at (0, 0, 0).

Notice that the surface is approximated using small flat sections. We will discuss this in more detail in Subsection 2.14 where we will also show how the spherical surface can be made smoother or coarser.

#### 2.12 Planar circle

Planar circle with radius r and center at (0, 0) can be defined using the command CIRCLE(r):

c = CIRCLE(3.0)
COLOR(c, GOLD)
SHOW(c)

The output is shown in Fig. 23. Fig. 23: Gold planar circle with radius r = 3 and center at (0, 0).

In fact this is a polygon with many edges. In Subsection 2.14 we will show how to reduce the number of edges in order to obtain various equilateral polygons.

#### 2.13 Circle as thin 3D solid

Finally, the command CIRCLE3D(r) creates a 3D circle with center at the origin (0, 0) that lies in the xy-plane. This is the last in the series of 2D objects created as thin 3D solid, and it is constructed via the following script:

c = CIRCLE3D(3.0)
COLOR(c, GOLD)
SHOW(c)

The output is shown in Fig. 24. Fig. 24: Gold circle with radius r = 3 and center at (0, 0), as a thin 3D solid.

As all the square, rectangle, and triangle in the 3D space also the circle is in reality a cylinder of height 0.001. The knowledge of this technical detail will be useful for performing operations such as intersection, union, difference and xor of these 2D objects.

#### 2.14 Approximation of curved surfaces

In 3D computer graphics, curved surfaces are always approximated using small linear triangular segments. This is how computer hardware is built. Also PLaSM works in this way. Each curved object comes with a default division that the user can adjust if needed. For the SPHERE command, the default divisions are 64 in the angular direction and 32 in the z-direction. This yields a uniformly spaced grid. The division can be used as an optional argument. If there are two divisions, they are always enclosed in square brackets. Hence, the command

s = SPHERE(3.0)

is equivalent to

s = SPHERE(3.0, [32, 64])

It is easy to calculate that the default sphere is approximated using 2 32 64 = 4, 096 linear triangles. Therefore its rendering will take longer than a cone which has only around 100, and much longer than cube which only has 12. If you decide to make it look better by using twice finer division in both directions,

s = SPHERE(3.0, [64, 128])

then the number of linear triangles jumps to 2 64 128 = 16, 384. This is a lot, and you are likely to wait.

Making the number of linear triangles large means nicer image
but also more computing, more data transfer, and a longer wait.

The knowledge of the subdivisions can be used to create various interesting objects such as a diamond:

s = SPHERE(3.0, [2, 8])
COLOR(s, GOLD)
SHOW(s)

The output is shown in Fig. 25. Fig. 25: Using SPHERE with subdivisions [2, 8] yields a diamond.

Another object that can be created using the SPHERE command with subdivisions [64, 8] is a baloon shown in Fig. 26. Fig. 26: Baloon (SPHERE with subdivisions [64, 8]).

in 2D, the CIRCLE(r) command is equivalent to CIRCLE(r, 64) and it approximates the circular boundary using an equilateral polygon with 64 edges. Various equilateral polygons can be created by using a number less than 64: Fig. 27: CIRCLE with subdivisions 3, 4, 5 and 6.

#### 2.15 Prism

Given any 2D polygon b and a positive number h, the command PRISM(b, h) creates a prism with the base b and height h. For illustration, prisms of height h = 3 corresponding to the polygons shown in Fig. 27 are depicted below: Fig. 28: Prisms with h = 3 corresponding to polygons from Fig. 27.

The basis also can be a SQUARE, RECTANGLE, TRIANGLE and even a CIRCLE. In the last case, we obtain a cylinder. Since cylinders are used very often, PLaSM provides a separate command for them:.

#### 2.16 Cylinder

Cylinder with radius r and height h can be defined using the command CYLINDER(r, h), possibly abbreviated as CYL(r, h). The cylinder’s axis is in the z-direction, and the midpoint of its base circle lies at the origin (0, 0, 0). The output of the sample code

r = 0.25
h = 1.0
c = CYL(r, h)
COLOR(c, GOLD)
SHOW(c)

is shown in Fig. 29. Fig. 29: Cylinder with radius r = 0.25 and height h = 1.

By default, the curved surface is split into 64 vertical linear segments. To change the division to another integer number m, use

c = CYL(r, h, m)

This can be used to create prisms. By using m = 3, 4, 5, 6 we obtain the prisms from Fig. 28.

#### 2.17 Tube

Tube of inner radius r1, outer radius r2 and height h is rendered using the command TUBE(r1, r2, h). The tube is positioned in the global coordinate system same as the cylinder – its axis coincides with the z-axis and the center of its base is at (0, 0, 0). The output of the sample code

r1 = 0.2
r2 = 0.25
h = 1.0
t = TUBE(r1, r2, h)
COLOR(t, GOLD)
SHOW(t)

is shown in Fig. 30. Fig. 30: Tube of inner radius r1 = 0.2, outer radius r2 = 0.25 and height h = 1.

By default, the inner and outer curved surfaces are split each into 64 vertical linear segments. To change the division to another integer number m, use

t = TUBE(r1, r2, h, m)

This can be used to create tubes with various polygonal cross-sections.

#### 2.18 Cone

Cone with radius r and height h is created using the command CONE(r, h). The cone’s axis coincides with the z-axis and the center of the base circle lies at the origin (0, 0, 0). The output of the sample code

r = 5
h = 10
c = CONE(r, h)
COLOR(c, GOLD)
SHOW(c)

is shown in Fig. 31. Fig. 31: Cone with radius r = 5 and height h = 10.

By default, the curved surface is split into 64 linear triangles. To change the division to another integer number m, use

c = CONE(r, h, m)

The linear surface representation can be used to render pyramids, as shown in Fig. 32. Fig. 32: Pyramids created using the CONE command with r = 5, h = 10 and m = 3, 4, 5, 6.

#### 2.19 Truncated cone

Truncated cone with bottom radius r1, top radius r2 and height h is created using the command TCONE(r1, r2, h). The cone’s axis coincides with the z-axis and the center of the base circle lies at the origin (0, 0, 0). The output of the sample code

r1 = 5
r2 = 2
h = 5
t = TCONE(r1, r2, h)
COLOR(t, GOLD)
SHOW(t)

The output is shown in Fig. 33. Fig. 33: Truncated cone with bottom radius r1 = 5, top radius r2 = 2 and height h = 5.

By default, the curved surface is split into 64 trapezoidal linear segments. To change the division to another integer number m, use

t = TCONE(r1, r2, h, m)

The output is shown in Fig. 34. Fig. 34: Truncated pyramids with r1 = 5, r2 = 2, h = 5 and m = 3, 4, 5 and 6.

#### 2.20 Torus

Command TORUS(r1, r2) creates a torus (donut) with inner radius r1 and outer radius r2. It is used as follows:

r1 = 3
r2 = 5
t = TORUS(r1, r2)
COLOR(t, GOLD)
SHOW(t)

The center of the torus is at the origin (0, 0, 0) and its axis is the z-axis. This is illustrated in Fig. 35. Fig. 35: Torus with inner radius r1 = 3, outer radius r2 = 5 and center at the origin (0, 0, 0).

By default, the major (large) circle is split into 64 linear segments and the minor (small) one into 32. Thus the default command TORUS(r1, r2) is equivalent to TORUS(r1, r2, [64, 32]). To change the divisions to other integer numbers [m, n], use

t = TORUS(r1, r2, [m, n])

Also here, the piecewise-linear surface representation can be used to create interesting objects. Fig. 36 shows the output of the command TORUS(3, 5, [64, 4]). Fig. 36: Hollow disc.

Fig. 37 shows the output of the command TORUS(3, 5, [4, 64]). Fig. 37: Round pipe frame.

#### 2.21 Convex hull

Constructing convex hulls of finite point sets in 2D and 3D is one of the most important algorithms in computational geometry. A 2D or 3D object is convex if for any two points located inside, the straight line connecting the points lies entirely inside. For illustration, Fig. 38 shows a convex object on the left and a non-convex one on the right. Fig. 38: Convex 2D object (left) and a non-convex one (right), along with sample straight lines connecting interior points.

By convex hull of a set of 2D or 3D points we mean the smallest convext set that contains all the points. To construct convex hull of a large number of points efficiently is not a trivial task at all. It can be proven mathematically that the complexity of finding a convex hull of n points is always at least (n log n) where n is the number of points

The complexity of more recent convex hull algorithms can be characterized in terms of both input size n and the output size h (the number of vertices of the hull). Such algorithms are called output-sensitive algorithms. They are often asymptotically more efficient than (n log n) algorithms in cases when h is much lower than n.

The earliest output-sensitive algorithm was introduced by Kirkpatrick and Seidel in 1986 (who called it "the ultimate convex hull algorithm"). A much simpler algorithm was developed by Chan in 1996, and is called Chan’s algorithm. That’s the one used in PLaSM.

The CONVEXHULL command (possibly abbreviated as CHULL or CH) takes a set of points and creates their convex hull:

o = CHULL([-1, 0], [1, 0], [0.5, 1], [-0.5, 1])
COLOR(o, GOLD)
SHOW(o)

This code renders a 2D trapezoid spanning the points (-1, 0), (1, 0), (0.5, 1) and (-0.5, 1) as shown in Fig. 39. Fig. 39: Convex hull of four 2D points.

The code

o = CHULL([-1, -1, 0], [1, -1, 0], [1, 1, 0],
[-1, 1, 0], [0, 0, 1])
COLOR(o, GOLD)
SHOW(o)

creates a 3D object shown in Fig. 40. Fig. 40: 3D object constructed via convex hull of 6 points.

If you already have a list of points, defined for example as

L = [[-1, -1, 0], [1, -1, 0], [1, 1, 0], [-1, 1, 0], [0, 0, 1]]

then just insert it into the CHULL command:

out = CHULL(L)

#### 2.22 Dodecahedron

A (regular) dodecahedron is an object with 12 identical pentagonal faces. It is one of the Platonic solids – convex symmetric 3D objects whose faces are regular polygons. There are only five of them – the (regular) tetrahedron, cube, octahedron, dodecahedron and icosahedron. For their extraordinary symmetry, these objects have been studied by geometers for thousands of years. In PLaSM, a dodecahedron is created simply as:

o = DODECAHEDRON

Output: Fig. 41: Dodecahedron.

#### 2.23 Icosahedron

A (regular) icosahedron is an object with 20 triangular faces that also belongs to the five Platonic solids. In PLaSM it is created via

o = ICOSAHEDRON

The object is shown in Fig. 42. Fig. 42: Icosahedron.

#### 2.24 Extrusion of 2D objects to 3D

An arbitrary 2D object that lies in the xy-plane can be extruded to 3D using the PRISM command that we already know from Subsection 2.15. For illustration, let us extrude a sample 2D polygon obtained as convex hull:

b = CHULL([0, 0], [2, 0], [1, 2], [-1, 2])
h = 0.5
o = PRISM(b, h)

The output is shown in Fig. 43. Fig. 43: Extrusion of a quadrilateral to 3D.

The command PRISM(b, h) uses a single interval in the z-direction. For future reference, let us also introduce the command EXTRUDE(b, h, angle, n) that with angle = 0 yields the same extruded geometry as the PRISM command, but it subdivides the extrusion height h into n equally long subintervals. This will be practical for working with curved shapes.

#### 2.25 Grid and Cartesian product

PLaSM also provides useful commands GRID and PRODUCT that make it easy to work with grids and Cartesian product geometries. The command GRID takes a list of positive and negative numbers where the positive ones stand for intervals (more precisely for their lengths) and negative for the lengths of spaces between the intervals. The intervals and spaces are placed on the right of the origin in the real axis. For example,

g1 = GRID(5)

creates a single interval (0, 5). The command

g2 = GRID(1, -0.5, 1, -0.5, 1)

creates three intervals (0, 1), (1.5, 2.5) and (3, 4), leaving empty spaces of length 0.5 between them. There is no limit on the number of intervals. Next, the command

G = PRODUCT(g1, g2)

creates a 2D object G in the xy-plane which is the Cartesian product of the two grids g1 and g2. This object is shown in Fig. 44. Fig. 44: Cartesian product of the grids g1 and g2.

This 2D object can be multiplied with a third grid in the z-direction. Let’s say that the third grid is

g3 = GRID(0.5, -1, 0.5, -1, 0.5)

Then the result of the Cartesian product

H = PRODUCT(G, g3)

is a 3D geometry shown in Fig. 45. Fig. 45: Cartesian product of the grids G and g3.

Just to show one more application of GRID and PRODUCT, let’s do:

g1 = GRID(3, -1, 3, -1, 3, -1, 3)
g2 = GRID(2, -1, 2, -1, 2)
g3 = GRID(1, -1, 1)
planar_grid = PRODUCT(g1, g2)
o = PRODUCT(planar_grid, g3)

The object is shown in Fig. 46. Fig. 46: Cartesian product of the grids g1, g2 and g3.