# 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:

SHOW(c)

After a moment, the following image should appear in your web browser:

#### 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:

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:

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:

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:

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:

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.

#### 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

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.

#### 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

COLOR(b, COPPER)

SHOW(b)

and press the green arrow button. The result is displayed in Fig. 16.

#### 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

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).

#### 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

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.

#### 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

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.

#### 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:

b = [1, 0]

c = [0, 2]

tria = TRIANGLE(a, b, c)

COLOR(tria, BRONZE)

SHOW(tria)

The output is shown in Fig. 20.

#### 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:

b = [1, 0]

c = [0, 2]

tria = TRIANGLE3D(a, b, c)

COLOR(tria, BRONZE)

SHOW(tria)

The output is shown in Fig. 21.

#### 2.11 Sphere

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

COLOR(s, GOLD)

SHOW(s)

The output is shown in Fig. 22.

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):

COLOR(c, GOLD)

SHOW(c)

The output is shown in Fig. 23.

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:

COLOR(c, GOLD)

SHOW(c)

The output is shown in Fig. 24.

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

is equivalent to

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,

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:

COLOR(s, GOLD)

SHOW(s)

The output is shown in Fig. 25.

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

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:

#### 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:

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

h = 1.0

c = CYL(r, h)

COLOR(c, GOLD)

SHOW(c)

is shown in Fig. 29.

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

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

r2 = 0.25

h = 1.0

t = TUBE(r1, r2, h)

COLOR(t, GOLD)

SHOW(t)

is shown in Fig. 30.

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

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

h = 10

c = CONE(r, h)

COLOR(c, GOLD)

SHOW(c)

is shown in Fig. 31.

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

The linear surface representation can be used to render pyramids, as shown in Fig. 32.

#### 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

r2 = 2

h = 5

t = TCONE(r1, r2, h)

COLOR(t, GOLD)

SHOW(t)

The output is shown in Fig. 33.

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

The output is shown in Fig. 34.

#### 2.20 Torus

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

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.

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

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. 37 shows the output of the command TORUS(3, 5, [4, 64]).

#### 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.

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:

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.

The code

[-1, 1, 0], [0, 0, 1])

COLOR(o, GOLD)

SHOW(o)

creates a 3D object shown in Fig. 40.

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

then just insert it into the CHULL command:

#### 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:

Output:

#### 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

The object is shown in Fig. 42.

#### 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:

h = 0.5

o = PRISM(b, h)

The output is shown in Fig. 43.

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,

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

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

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.

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

Then the result of the Cartesian product

is a 3D geometry shown in Fig. 45.

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

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.