0% found this document useful (0 votes)
75 views

Unit 4 Clipping and 3D Primitives: Structure Page No

This document discusses 2D clipping algorithms and 3D modeling techniques in computer graphics. It introduces the concept of clipping to remove invisible objects from the viewing window. The Cohen-Sutherland line clipping algorithm is described, which categorizes line segments as trivially rejected, trivially selected, or partially visible and requires further processing. The algorithm assigns region codes to line endpoints to determine the clipping outcome. Bézier and B-spline curves are also covered as common surface rendering methods due to their ease of use and efficiency.

Uploaded by

nofiya yousuf
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
75 views

Unit 4 Clipping and 3D Primitives: Structure Page No

This document discusses 2D clipping algorithms and 3D modeling techniques in computer graphics. It introduces the concept of clipping to remove invisible objects from the viewing window. The Cohen-Sutherland line clipping algorithm is described, which categorizes line segments as trivially rejected, trivially selected, or partially visible and requires further processing. The algorithm assigns region codes to line endpoints to determine the clipping outcome. Bézier and B-spline curves are also covered as common surface rendering methods due to their ease of use and efficiency.

Uploaded by

nofiya yousuf
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 24

Clipping and 3D Primitives

UNIT 4 CLIPPING AND 3D PRIMITIVES


Structure Page No
4.1 Introduction 65
Objectives
4.2 2D Clipping Algorithms 66
4.3 Three Dimensional Concepts and Display Methods 72
4.4 Bézier and B-Spline Curves 75
4.5 Summary 79
4.6 Solutions/Answers 81
4.7 Practical Exercise 87

4.1 INTRODUCTION
This unit introduces you to three important concepts of Computer Graphics.
Sec. 4.2 discusses the concept of clipping, which means removal of objects not
in the window frame to be plotted on a display device. This simple concept has
far reaching consequences in computer animation and especially in video
games which is a fast growing industry. Good clipping strategy is critically
important for speeding up the rendering of the current scene and hence plays a
crucial role in maximizing game’s performance and visual quality.

In Sec. 4.3, we have discussed methods related to modelling and display of


three dimensional objects on a 2D display device. This gives an overview of
various techniques that are involved in achieving realism in 2D display of a 3D
scene. These techniques include inter-alia projection methods, depth
calculation and surface rendering.

For surface rendering most suited and common methods are based on Bézier
and B-spline representations for simple reasons–ease of use and efficiency.
Sec. 4.4 discusses the Bézier and B-spline curves.

Objectives
After reading this unit you should be able to:
• clip 2D line segments against a rectangular clipping boundary using one
of the two line clipping algorithms–Cohen Sutherland line clipping
algorithm and Liang Barsky line clipping algorithm;
• explain how a curve and a text string is clipped against a window
boundary;
• explain the concepts used in rendering and projecting a 3D scene onto a
2D display;
• use basic techniques and methods employed in 3D object modelling and
representation, which includes
 polygonal surfaces and their representation,
 Bézier and B-spline curves.

65
Computer Graphics
4.2 2D CLIPPING ALGORITHMS
Clipping is an operation that eliminates invisible objects from the view
window. To understand clipping, recall that when we take a snapshot of a
scene, we adjust our aperture and focus to bring only desirable objects within
the window and the final shot is taken once the window contains only those
objects within the frame. From the point of view of mathematics, the problem
of clipping looks simple. All that we have to do is to find out the extremities of
the object and check their intersection with the window. This however is a
critical problem from the point of view of processing in computer. In gaming
industry especially, when animation at a high speed has to be shown, time
complexity of these clipping methods are crucial. You will observe that due to
this reasons in most of the clipping algorithms, intersection calculations are
avoided as far as possible. The basic idea behind these algorithms is to
maximize the performance and efficiency by deciding the visibility/partial
visibility/invisibility of an object using the minimum number of intersection
calculations. In this section you will study clipping algorithms for the
following primitive objects
• Point
• Line
• Area
• Curve
• Text

Point clipping is very simple. All you need to check is whether a point is
inside the window extremes in x- and y-directions. For line clipping several
algorithms have been introduced over the years and many refinements have
also been suggested by people in order to improve the performance of the
algorithms. One of the first algorithms that is still being used in applications is
Cohen Sutherland algorithm. It categorizes line segments into three
categories (i) trivially rejected (ii) trivially selected (iii) may be partially visible
or totally invisible. For category (i) and (ii) no intersection calculations are
required whereas category (iii) contains all those line segments that need
further processing for determining if they are partially visible or totally
invisible. For the details about point clipping and line clipping read the
following sections of the book carefully.

Read Secs. 6-5 to 6-7, Chapter 6, pages 244-250 of the book upto
Cohen Sutherland line clipping.

Before proceeding further, we summarize the main steps of Cohen Sutherland


line clipping algorithm.

Recall that the rectangular window W has window boundaries parallel to the
coordinate axes and falling on the lines
x = xw min , x = xw max , y = yw min , y = yw max . The four window boundaries
are labelled in the given order by numbers 1, 2, 3, 4 . Let L be a line segment
with end points P1 = ( x 1 , y1 ), P2 = ( x 2 , y 2 ) . The algorithm proceeds as
follows.
66
1. Divide the entire plane into nine disjoint regions by extending the four Clipping and 3D Primitives
window boundaries indefinitely in both the directions
2. Assign a 4-bit long region code to each region and initialize it to be zero
(0000) for all the points. Region code helps you in identifying line
segments that can be (i) trivially rejected (Line segment AB in Fig. 1
below) (ii) trivially selected (Line segment CD) (iii) possibly partially
visible (Line segment EF). In case the line segment is neither trivially
rejected nor trivially selected, it is further processed for checking its
possible intersections with window boundaries. In this case also the
region code helps in identifying which window boundary can potentially
intersect the line segment (see Fig. 1).

1001 1000 1010


A
C

0001 0000 0010


F
D

B 0101 0100 E 0110

Fig. 1.

3. For each end-point P in the plane, assign it a code as per the following
rule.
i) If P is to the left of the line x = xw min , first (least significant) bit is
set to 1.
ii) If P is to the right of the line x = xw max , second bit is set to 1.
iii) If P is below the line y = yw min , third bit is set to 1.
iv) If P is above the line y = yw max , fourth (most significant bit) is set
to 1.
(This way we have unique identification code for each region. For
example, if the point P is on the top right region of the window then it has
a code 1010.)
4. For each line segment L , determine the region codes-code(P1) and
code(P2) of its end points P1, P2.
5. If code(P1) = 0 and code(P2) = 0, then the line is completely visible.
Select the line and store it in the frame buffer. Else
6. Find out bitwise intersection of code(P1) and code(P2). If intersection is
a non-zero code, then line segment L is completely outside the window
and is rejected. Else there are two possibilities
i) Line may be partially visible.
ii) Line may be completely invisible.
For example, if the two endpoints are such that code(P1) = 0101
and code(P2) = 1010, then the line segment may or may not
intersect the window. See the two line segments AB and CD in
67
Computer Graphics Fig. 2 below. Both satisfy the conditions given above. However, CD
is partially visible while AB is completely invisible.
B

D
1001 1000 1010

0001 0000
0010

0101 0100 0110


A
C

Fig. 2.

Now proceed as follows:


7. Check if code(P1) or code(P2) is zero. In this case line is partially
visible. If code (P1) = 0 then find out the non-zero bits present in
code(P2). Find out the intersection of L with the corresponding window
boundaries. For example, if code (P 2) = 1010 , then find out the
intersection points Int1 with right boundary. If Int1 is inside the window
boundary, replace P2 with Int1, else find out the intersection point Int2
with top window boundary and replace P with Int2. Select the updated
line segment L and send it to the frame buffer.
8. If code (P2) = 0 swap P1 and P2 and repeat step 7.
9. If both code(P1) and code(P2) are non-zero then for i = 1, 2, 3, 4 find out
intersection of L with i th window boundary. One way to implement it is
as given below.
10. For end point P1, find out if it is beyond the i th window boundary. If
yes, find out intersection point and replace P1 with the point. Repeat the
same for P2.
11. If after step 10, updated L is inside the window boundary, send it to the
frame buffer, else L is completely invisible and is rejected.

You may observe here that for totally or partially visible lines, algorithm works
quite efficiently. However for some invisible lines one has to calculate
intersection points with all the four window boundaries to finally decide about
its rejection. For example, consider an invisible line having one end point in
the region 0110 and the other end point in the region 1001, intersection with all
the four window boundaries has to be calculated in order to assert that the line
is invisible.

You may now test your understanding by trying the following exercises.

E1) Let W be a window having two diagonally opposite corners at (1, 1) and
(5, 4) . Trace Cohen Sutherland line clipping algorithm for the line
segment having two end points (0, 0) and (4, 5) .

68
E2) Let W be the window as given in E1). Clip a triangle with vertices Clipping and 3D Primitives
(0, 0), (4, 5) and (6, 1) against the window by tracing Cohen Sutherland
line clipping algorithm for each of the edges.

Another important algorithm known as Liang Barsky algorithm is based on


parametric form of the line segments and is faster than the Cohen Sutherland
line clipping algorithm in general. For the details of this algorithm, you may
read the following.

Read Sec. 6-7, Chapter 6, pages 250-252 of the book.

Before you apply the Liang Barsky algorithm to a particular problem, let us
recapitulate the main steps involved in Liang-Barsky algorithm.

Recall that ∆x = x 2 − x1, ∆y = y 2 − y1 . Parametric equation of the line


segment L is given by x = x1 + u ∆x ; y = y1 + u ∆y, 0 ≤ u ≤ 1 . The
algorithm proceeds as follows:

1. Consider the infinite extension of the i th window boundary


(i = 1, 2, 3, 4), which divides the entire plane into two parts – The inside
region that contains the window and the outside region corresponding to
that window boundary. For each line segment L proceed as follows:
2. Define the constants pk , qk (k = 1, 2, 3, 4) as follows:
i) p1 = − ∆x , p 2 = ∆x , p3 = −∆y, p 4 = ∆y
ii) q1 = x1 − xw min, q 2 = xw max − x1, q3 = y1 − yw min, q 4 = yw max − y1.

The above constants are computed initally and will be used in most of the
computations right from deciding the visibility of the line segment to
calculating the intersection with window boundaries. Note that p1 = – p2,
still the authors of the algorithm have used them as independent
constants. This helps inter-alia in efficiently defining the loops and in
managing the intersection points with corresponding window boundary.

3. Initialize the parameters u1 = 0 and u 2 = 1 that define the part of line


segment L that lies within the window boundary for u1 ≤ u ≤ u 2 . These
parameter values will be updated as and when required.
4. For k = 1, … , 4 .
i) if pk = 0 then there are two possibilities.
a) qk < 0 . In this case the line segment is completely outside the
window boundary and is rejected.
b) qk ≥ 0 . In this case the line segment is inside the i th window
boundary and is kept for further processing.

Fig. 3 on the next page gives you an idea of such line segments. For Line AB
and CD p1 = p2 = 0 . But in case of k = 1, q1 < 0 for AB while q1 ≥ 0 for CD.

69
Computer Graphics Hence AB is rejected when k = 1 while CD is kept for further processing. When
k takes value 2, q 2 < 0 for CD and it is rejected.

D
B
k=4

k=3

A k=1 k=2
C

Fig. 3.

ii) If pk < 0 , the infinite extension of the directed line segment L


proceeds from outside to inside region of the i th window boundary.
If pk > 0 , the infinite extension of L proceeds from inside to
outside of the i th window boundary. For any such non-zero value
of pk , the intersection point will have parameter value u = rk ,
where rk = qk / pk . Thus, refresh u1 and u 2 as follows:
a) if pk < 0 , refresh the value of u1 by u1 = max{0, rk}
b) if pk > 0 , refresh the value of u 2 by u 2 = min{1, rk} .
5. If u1 > u 2, L is invisible and reject it else store L in the frame buffer for
plotting for parameter values u such that u1 ≤ u ≤ u 2 .

Fig. 4 and Fig. 5 describe various categories of line segments as specified in


the algorithm.

Line L1 : p 1 = 0 , q 1 < 0
Line L2 : p 4 = 0 , q 4 < 0

P1 L2 P2
P2
L3
L1
P2
P1

P1
Line L3 : p 1 < 0 Line proceeds from outside to inside
for first window boundary.
p 1 > 0 Line proceeds from inside to outside
for second window boundary.
Fig. 4.

70
Clipping and 3D Primitives

u =1
P2

u1
u2
u=0
P1 u 1 > u 2 , reject line

Fig. 5.

You may now test your understanding of the algorithm by trying the following
exercise.

E3) Repeat E2) using Liang Barsky line clipping algorithm.

After attempting E3) you must have felt that the Liang-Barsky algorithm
reduces intersection calculations and is more efficient than the Cohen-
Sutherland algorithm.

As you proceed further, you would see that the methods for curve clipping and
character clipping have also been developed based on bounding rectangles.
This means for a given 2D object, you can find out the bounding rectangle with
edges parallel to the coordinate axes (parallel to the sides of clipping window).
In case the bounding rectangle does not intersect the clipping window, reject
the object. Otherwise, find out partial/ total visible objects. Intersection
calculations with the window boundary are performed for partial visible
objects, whenever essential. You may now read the following.

Read Secs. 6-9 and 6-10, Chapter 6, page 264 of the book.

You may try the following exercises before moving to the next section.

Do the exercises no. 6-10, 6-21, 6-22, Chapter 6 on pages 268-269 of the
book.

Real challenge in Computer Graphics is to transform a 3D scene into a 2D


image with realism as far as possible. This transformation involves various
steps before an object model is ready for display. Methods used in this
processing are called 3D display methods. In the next section we shall discuss
some of the aspects covered under 3D display methods of modelling, rendering
and animation for 3D real life objects.

71
Computer Graphics
4.3 THREE DIMENSIONAL CONCEPTS AND
DISPLAY METHODS
Imagine yourself taking a picture by a camera. What do you normally do?
You specify a viewpoint and view direction and then set up a view window.
Once that is done you take a snap and the image of 3D scene/object is captured
in a 2D film. Basic idea behind this is projection from 3D to 2D. Projections
are broadly classified under two names (i) Parallel (ii) Perspective. When
objects are projected onto the display plane along parallel lines, we call it
parallel projection. In this case a view direction is specified and image of
points on the object are obtained by taking lines parallel to the view direction
and finding their intersection with the display plane. If you have seen an
architectural drawing of a building plan, you would have noticed that they
show three different plots, top view, front view and side view normally. This
actually is an example of a parallel projection of the building to be constructed.
On the other hand, perspective projection corresponds more closely to the way
a human eye perceives a 3D scene. A viewpoint is specified and points from
the object are projected to the view plane by considering lines emanating from
the points on the object and converging to the viewpoint. Intersections of these
converging lines with the display plane make the image of the object under
perspective projection.

To have a better understanding of projections from 3D to 2D, read the


following.

Read Sec. 9-1, Chapter 9, pages 317-323 of the book.

Common practice in modelling and rendering a surface is to approximate it by


a polygonal surface. By a polygonal surface we mean a surface which is made
up of polygons. Main reason for converting a surface to a polygonal surface is
to simplify representation for speeding up the surface rendering. This is
possible as all the polygons are described by linear equations.

You may now read the following.

Read Sec. 10-1, Chapter 10, pages 325-330 of the book.

Remember in polygonalization of the surface, following rules must be


followed. Any two polygons
(i) share a common edge,
(ii) share a common vertext,
(iii) are disjoint.

Fig. 6(a) on the next page, gives an example of an admissible approximation of


a surface using triangles, called triangulation. Further, Figs. 6(b) and 6(c) give
examples of some cases that are not admissible as per the rules given above.
This means an edge of a polygon cannot be a part of an edge of another
polygon (see Fig. 6(b)).

72
Similarly, no two polygons can overlap leading to a common non-empty Clipping and 3D Primitives
interior (see Fig. 6(c).

Common Edge

Common
Vertex (b)

Disjoint Polygons

(a) A triangulation (c)

Fig. 6: A Triangulation of a surface.

After reading Sec. 10-1 of the book, you would have acquired sufficient
theoretical background of how a polygonal surface is stored and rendered in
graphics. To understand and implement the ideas about the polygonal surfaces,
consider a simple example of the standard cube. The cube consists of eight
vertices (0, 0, 0), (1, 0, 0), (1, 1, 0), (0, 1, 0), (0, 0, 1), (1, 0, 1), (1, 1, 1), (0, 1, 1) .
So the vertex table is formed as shown in Table-1.

Table-1

Vertex Table
V1 0 0 0
V2 1 0 0
V3 1 1 0
V4 0 1 0
V5 0 0 1
V6 1 0 1
V7 1 1 1
V8 0 1 1

There are twelve edges in the cube. Once the vertices are labeled, we can form
the edge table as given in Table-2.
Table-2

Edge Table
E1 V1 V2
E2 V2 V3
E3 V3 V4
E4 V4 V1
E5 V5 V6
E6 V6 V7
E7 V7 V8
E8 V8 V5
E9 V2 V6
E10 V3 V7
E11 V4 V8
E12 V1 V5
73
Computer Graphics Finally, the Polygon-Surface table formed is given in Table-3.

Table-3

Polygon-Surface Table
S1 E1 E2 E3 E4
S2 E1 E9 E5 E12
S3 E2 E10 E6 E9
S4 E3 E11 E7 E10
S5 E4 E12 E8 E11
S6 E5 E12 E7 E8

The cube conceived as a polygonal surface is shown in Fig. 7 below.


E8

E5 V8= (0,1,1)
V5 = (0,0,1) E7

V6= (1,0,1) V7= (1,1,1)


E12
E11 E4
E9 E6 E10

V1= (0,0,0) V4 = (0,1,0)


E1
V2= (1,0,0)1 V3= (1,1,0)
E3

E2
Fig. 7: A cube as a polygonal surface.

In many graphics packages and APIs like OpenGL, one can skip forming the
edge table and can straightaway form polygons using vertices.

Apart from polygonal surfaces, polygonal meshes are also used extensively in
3D geometric modelling. A mesh is essentially a polygonal approximation,
where the polygons are hollow, that means you only consider the boundary of
polygons and not the filled polygons. Once a polygonal mesh is formed,
different surface filling patterns within the meshes are used as per the
requirement and/or suitability of the application in which the resulting surface
is being used. You will study a little later a variety of surface primitives that
can be used for such filling applications. Triangular or quadrilateral meshes
are most common. A triangular mesh (or a triangulation) is a polygonal mesh
in which all (or almost all) the polygons are triangles. Similarly, for
quadrilateral meshes all the polygons have to be quadrilaterals.

While constructing a polygonal mesh you must again follow the rules as
described on page 72 for construction of polygonal surfaces.

It is now time to check your understanding of what you have studied above.

Do the exercise no. 10-1, 10-7, 10-8 Chapter 10 on pages 424-425 of


the book.

74
We next turn to study some of the important curve and surface generation Clipping and 3D Primitives
techniques. More precisely, we shall study Bezier and B-spline curves and
surfaces with emphasis on their properties suitable for geometric modelling.

4.4 BEZIER AND B-SPLINE CURVES


You may start with reading the following.

Read Sec. 10-8, Chapter 10, pages 347-354 of the book.

We are giving you here another very popular iterative algorithm for generating
Bezier curves known as deCasteljau algorithm. The algorithm uses repeated
linear interpolation and works as follows:

Given n + 1 control points Pk , k = 0, 1, …, n , define


Pkr (u) = (1 − u) Pkr-1(u) + u Pkr-+11(u), i = 0, 1, ..., n − r and r = 1, 2, ..., n,
where Pk0 (u) = Pk and the curve at point u is evaluated as P(u ) = P0n (u) .
Fig. 8 demonstrates the algorithm for n = 3.

P11 (u) P2
P1

P12 (u)
P21 (u)

P03 (u)

P01 (u)
P02 (u)

P0 P3
Fig. 8: A cube as a polygonal surface.

Lower degree curves have found tremendous applications in geometric


modelling and graphics because of the ease of implementation of such curves
in various applications. Before proceeding further you may try the following
exercises.

Do the exercise no. 10-14, 10-17, Chapter 10 on page 425 of the book.

Bezier surfaces are the natural extension of Bezier curves. There are two types
of Bezier surfaces (i) Tensor product surfaces (ii) Triangular surfaces. Let us
understand here the construction of tensor product Bézier surfaces.

A parametric surface is expressed in the form P (u , v) = ( x ( u , v), y( u, v))


where u , v are two independent parameters such that u ∈ [a , b] and v ∈ [c, d ] .
As an example we express a sphere of radius r with centre at the origin as

75
Computer Graphics S(θ, ϕ) = r (cos θ cos ϕ, cos θ sin ϕ, sin θ) where, θ ϕ ∈ [0, π] .

Tensor product Bezier surface and composite surface: Consider the square
[0, 1] 2 = [0, 1] × [0, 1] . The tensor product Bezier surface of degree (n,m) is
defined by the map P : [0, 1]2 → R 3 given by
m n
P ( u , v) = ∑ ∑ p j,k BEZ j,m (v) BEZ k ,n (u),
j=0 k =0

where p j,k ∈ R 3 and BEZ k ,n (u ) are as defined on page 347 of the book. Points
p j,k are called control vertices or control points of the tensor product Bezier
surface and the quadrilateral mesh formed by these points is called control net
of the surface. Notice that for each fixed u the surface reduces to a Bezier
curve in v and vice versa. For example, on the boundary of the square u = 0 ,
the surface reduces to the Bezier curve
m
P(0, v) = ∑ p j,0 BEZ j,m ( v),
j=0

because BEZ k ,n (0) = 0 for k ≠ 0 and BEZ 0,n (0) = 1 . Similarly, for other three
boundaries.

In order to construct a tensor product composite Bezier surface all you need to
do is join two Bezier surfaces in such a way that the boundary curves match
identically. This will make the composite surface continuous. For higher order
smoothness notice that if the boundary curves are matching then their
derivatives (upto any order ) in the same direction will also match. So all that
you need to ensure for C1 continuity is the continuity of normal derivative.
Normal derivative of a surface at a point means the directional derivative in the
direction normal to the tangent plane. To make this point clear, let us consider
a composite Bezier surface s(u , v) defined on [0, 2] × [0, 1] with two tensor
product Bezier pieces P1 and P2 . While P1 is defined on [0, 1] × [0, 1], P2 is
defined on [1, 2] × [0,1] . Let p j,k and q j,k be the control points for P1 and P2 ,
respectively. Notice that we have defined Bezier surface only for the domain
[0, 1]2 . Therefore to define the Bezier surface corresponding to the square :
[1, 2] × [0, 1] , you need to establish a local parameterization for [1, 2] × [0, 1]
by mapping [1, 2] to [0, 1] using t = u − 1 . Then define the piece P2 using the
local parameter t as follows:
m n
P2 ( t, v) = ∑ ∑ q j,k BEZ j,m (v)BEZ k ,n (t ), t = u − 1.
j=0 k =0

Actually here you have performed a change of domain parameter


transformation. The patch P2 remains the same but is now viewed as map from
[ 0,1]2 to R3 as shown in Fig. 9 on the next page.

76
Clipping and 3D Primitives

(1,1)
(0,1) (1,2) (2,2)

(0,0) (1,0) (1,1) (2,1)

Fig. 9: Domain reparametrization of a Tensor product Bezier surface.

On the common boundary of the two squares at u = 1 , you have to match the
boundary curves of P1 and P2 as follows:
m m
P1 (1, v) = ∑ p j,n BEZ j,m ( v) = P2 ( 0 , v) = ∑ q j,0 BEZ j,m ( v) .
j=0 j=0

P1 and its control


net
P2 and its control
net
(0,1) (1,1) (2,2)

(0,0) (2,1)
Fig. 10: A composite tensor product Bezier surface.

The two boundary curves will match identically if and only if the boundary
control points p j, n = q j, 0 for j = 0, 1, … , m . This follows because Bernstein
basis polynomials are linearly independent. All you need to do for continuity
of the composite patch is to keep the same control points for the two Bezier
surfaces on the common boundary (see Fig. 10). Next, for the continuity of the
first derivatives, recall that a Bezier surface is a polynomial surface and hence
possesses continuous derivatives of all orders. Therefore we only need to
check for the C1 continuity of the composite derivatives at the joining curve of
the two pieces. This requires matching of partial derivatives of P1 and P2 in u
and v directions. In the direction u , since the two pieces have a common
boundary curve, their derivatives of all orders in that direction will also be
matching. Therefore you only need to find out conditions under which partial
derivatives of P1 and P2 match in the direction v . Using the formula for the

77
Computer Graphics first derivative of Bezier curve, you can write down the v (-directional) partial
derivatives of P1 and P2 as follows:
m n
∂P1
( t , v) = ∑ ∑ ∆1p j,k BEZ j,m −1 ( v)BEZ k ,n (u ),
∂v j= 0 k =0
m n
∂P 2
( t , v) = m ∑ ∑ ∆1q j,k BEZ j,m−1 (v)BEZ k ,n (t ), t = u − 1.
∂v j=0 k =0

Along the common boundary the two derivatives reduce to


m
∂P1
(1, v) = m∑ ∆ 1p j,n BEZ j,m −1 ( v) ,
∂v j=0
m
∂P 2
(0, v) = m∑ ∆1q j,0 BEZ j,m −1 ( v).
∂v j=0
Matching the two derivatives, we get the condition:
∆1p j,n = ∆1q j, 0 or,
p j + 1, n − p j, n = q j + 1, 0 − q j, 0 .
Geometric interpretation of these conditions say that the control points
p j + 1, n , p j, n , q j + 1, 0 are collinear and the ratio in which p j, n divides the line
segment joining p j + 1 , n and q j + 1, 0 is 1 : 1 . Fig. 11 below gives you the clear
picture of control point positioning near the boundary control polygon for C1
continuity requirement.

Fig. 11: Red, Green and Blue control points near the boundary fall on a line.

There is another notion of continuity called geometric continuity. Although


the idea existed in differential geometry, the concept was introduced for
geometric modelling and graphics applications in mid 80s. In one variable it
essentially means matching of the directions of the derivatives of the adjacent
curves. In other words, this means matching the derivatives when the two
curves have been defined with respect to arc length as the parameter. This
provides flexibility in the shape modelling. Lot of research has been done in
this direction. Please refer to pages 338-339 of the book for information on
geometric continuity.

B-spline curves are piecewise polynomial cubes with one or more polynomial
pieces with a minimum smoothness requirement. For example, a C 2 cubic
B-spline curve has one or more cubic pieces which are joined in such a way
that the resulting composite curve is twice continuously differentiable.

78
B-spline curves have a very elegant expression similar to the Bezier Clipping and 3D Primitives
representation with basis function as B-spline functions in place of Bernstein
basis polynomials. B-spline basis functions can be computed iteratively using
de Boor algorithm. Sec. 10.9 of the book discusses in detail about B-spline
curves and surfaces which you may read now.

Read Sec. 10-9, Chapter 10, pages 354-365 of the book.

You may try the following exercises.

Do the exercise no. 10-20, 10-21, Chapter 10 on page 425 of the book.

The following exercises will further help you to test your understanding of the
concepts learnt above.

E4) A curve shape has three quadratic Bézier curve segments. The curves
have been joined sequentially so that continuity of the first derivative of
the resulting curve shape is maintained. What is the minimum number of
control points that will be required as input to produce the curve? Justify
your answer.

E5) If the spacing between the knot sequence is uniformly doubled, will the
shape of the resulting B-spline curve change? Justify your answer.

E6) A B-spline curve of degree 5 having all control points on a straight line
segment will lie on the same straight line segment. True or false? Give
reasons in support of your answer.

We now end this unit by giving a summary of what we have covered in it.

4.5 SUMMARY
In this unit, we have covered the following:
1. The concept of clipping 2D objects.
2. Clipping of line segments against a 2D rectangular window.
3. Cohen Sutherland line clipping algorithm: The algorithm uses the
following main steps
• Divide the entire plane into nine disjoint regions using the four
window boundaries of the window.
• Give a unique four bit region code to each region.
• Find out the code of two end points.
• Use the code to check (i) If the line segment can be trivially selected
(ii) trivially rejected (iii) needs further processing.
• Find out the intersection with the window boundaries for line
segments which need further processing.

79
Computer Graphics 4. Liang Barsky line clipping algorithm: The algorithm uses parametric
form of the line segment. Four inequalities are created using the
parametric form of the line segments. These inequalities are used for
processing the line segment in an efficient way.
5. Three dimensional display methods: Among the simplest three
dimensional surface representations are the polygonal surfaces.
• A polygonal surface is described by vertices, edges and polygons.
• Vertex, edge and polygon tables are formed for the convenience of
organization of the polygonal surfaces. This is done because the
polygonal surface representation of most of the real life surfaces
require thousands of polygons to make a good approximation.
6. For computation of Bézier curves an iterative algorithm known as de
Casteljau algorithm is used. The algorithm uses repeated linear
interpolation.
7. Bézier surfaces are simple extensions of Bézier curves.
8. Tensor product Bézier surface is having the parametric form-
m n
P ( u , v) = ∑ ∑ p j,k BEZ j,m (v)BEZ k ,n (u ),
j=0 k =0

where (u, v) ∈ [0, 1]2 , p j, k ∈ R 3 are the Bézier control points


and BEZ k ,n (u ) is the standard k th Bernstein polynomial of degree n .
9. Conditions for smoothness require that the Bézier control points of the
adjacent surfaces satisfy certain collinearity constraints.
10. B-spline curves are piecewise smooth polynomial curves.
• B-spline curves are defined over an interval which has been
partitioned into sub-intervals. On each subinterval B-spline curve
reduces to a polynomial curve. The curve pieces are joined in such
a way that the composite curve satisfies certain smoothness
conditions specified in terms of matching of derivatives of certain
orders. Points defining the partition of the interval are called knots
or knot points. This is because at this common domain point of the
interval, two polynomial curve segments are joined to make the
composite curve.
• B-spline curves as well as blending functions are computed using
the iterative de Boor algorithm.
• B-splines satisfy the important properties suitable for geometric
modellling in computer Graphics. Some of these include (i) local
control (ii) smoothness (iii) degree of spline curve does not depend
on the number of control points (iv) convex hull property (v)
convenient blending functions.
• Uniform B-splines are B-spline curves with uniform spacing
between the knots.
• Uniform B-splines give periodic blending functions. This means all
blending functions are translated versions of a single B-spline.

80
Clipping and 3D Primitives
4.6 SOLUTIONS/ANSWERS
Exercises on pages 68-69 of this unit.

E1) Proceed step by step through the algorithm. Let P1 = (0, 0) and
P 2 = (4, 5) .
i) Find code (P1) = 0101 , code (P 2) = 1000 , where code (P) means
region code of point P.
ii) code (P1) ≠ 0 and code (P 2) ≠= 0 . Also code (P1) ∩ code (P 2) = 0 .
Therefore the line segment requires further processing.
iii) For left end point P1 , first find out the intersection with the left
boundary. The intersection point is (1, 5 / 4) which is on the
window boundary. Accept the point and update P1 by this point.
That means now, P1 = (1, 5 / 4) . You need not further process it for
finding intersection with bottom window boundary.
iv) For the right end point, find out the intersection with the top
window boundary and obtain the point (16 / 5, 4) . Update the point
P 2 by this point on the top boundary of the window.
v) Draw the clipped line segment now from P1 to P 2 .

E2) Label the edges as follows:


e1 : (0, 0) − (4, 5), e 2 : (4, 5) − (6, 1), e3 : (6,1) − (0, 0) . Proceed as in E1).
i) The edge e1 is already processed in the previous solution E1).
ii) The edge e2 has two end points both having non-zero codes.
Processing in the same way as in E1), you need to calculate
intersection with top and right window boundaries and finally
obtain the updated and clipped edge e2 and e3 as
follows: e2 : (9 / 2, 4) − (5, 3) .
iii) e3 : totally clipped since none of the intersection points will be on
the rectangle boundary.

Exercises on page 71 of this unit.

E3) Parametric form of the line segment is as follows. x = 4u , y = 5u .


i) Four inequalities are obtained as follows:
− 4 u ≤ −1 (k = 1)
4u ≤ 5 (k = 2)
− 5u ≤ − 1 (k = 3)
5u ≤ 4 (k = 4) .
ii) Initialize u 1 = 0 and u 2 = 1 .
iii) Process line against first window boundary (k − 1), p1 = − 4 < 0 , so
the infinite extension of line segment proceeds from outside to
inside. Further r1 = q 1 / p1 = 1 / 4 . Hence you need to update u1 by
this value of r1 = q 1 / p1 = 1 / 4 .
81
Computer Graphics iv) Second window boundary (k = 2) . p 2 = 4 > 0 , so the infinite
extension of line segment proceeds from inside to outside.
Calculate r2 = q 2 / p 2 = 5 / 4 > 1 . No need to update u 2 .
v) Third window boundary (k = 3). p 3 = −5 < 0 . Further,
r3 = q 3 / p 3 = 1 / 5 . Since the previous value of u 1 = 1 / 4 > r3 , no
need to update u 1 . (Recall that u 2 = min{1, ri : p i > 0} .)
vi) Fourth window boundary (k = 4). p 4 = 5 > 0 . Further,
r4 = q 4 / p 4 = 4 / 5 . Update u 2 = 5 / 4 . (Recall that
u 2 = min{1, ri : p i > 0} .)
vii) Now the updated values of u 1 = 1 / 5 and u 2 = 4 / 5 give you the
section of the line segment that is inside the window boundary. Plot
the line segment for value of u such that 1 / 5 ≤ u ≤ 4 / 5 .
viii) Proceed in a similar way for other two edges.

Exercises given on pages 268-269 of the book.

6-10 Consider here line segments with three different slopes. Let the line
segments be labelled L1, L 2, L3 , as shown in Fig. 12. Denote the
addition/ subtraction by A , multiplication/division by M . Use the
notations given in the book for subsequent discussions.

1000
y = ywmax L3

0001 L1 0000 0010

y = ywmin
L2 0100
x = xwmin x = xwmax

Fig. 12.

Comparative table for each line segment is given below.


Line1: Trivial Reject Case
Cohen Sutherland Algorithm Liang Barsky Algorithm

8 Comparisons + 4 AND operations. This 4A + 2M . 4A to compute


is to compute the code of each end point the parameters
by comparing the point location with p k , q k , k = 1, 2, 3, 4 . Then
respect to boundaries. No arithmetic 2M , to compute
operation. You only need to have AND q k / p k , k = 1, 2 . This is in
operation of endpoint codes for rejecting
addition to Comparisons
the line segment.
operations.

82
Line2: AND operation of endpoint codes = 0. Clipping and 3D Primitives

Cohen Sutherland Algorithm Liang Barsky Algorithm


4 Comparisons, 4 AND 4A + 4M . 4A to compute the
operations, then 2A + 2M to parameters p k , q k , k = 1, 2, 3, 4 .
calculate the intersection point Then 4M , to compute q k / p k , for
∆x each k . This is in addition to
x = x1 + ( y − y1 ) .
∆y Comparisons operations.

Line3: Line to be rejected after intersection calculations.

Cohen Sutherland Algorithm Liang Barsky Algorithm


6A + 6M , to calculate one 4A + 4M . 4A to compute the
intersection point for each non parameters p k , q k , k = 1, 2, 3, 4 .
zero bit of the codes of the Then 4M, to compute q k / p k , for each
endpoints. After this line will k . This is in addition to Comparisons
be rejected operations.

Compare the arithmetic operations for the rest of the line segments in a
similar way.

6-21 For the sake of simplicity, assume that both the ellipse and the window
have axes/edges parallel to the coordinate axes. Suppose the ellipse E
has centre ( xc, yc) with major and minor axes of lengths 2rx and 2ry
respectively. Further, let the window W have two extreme corners
( xw min, yw min) and ( xw max, yw max) as given in the book. Step by
step algorithm, based on which you may write a routine for clipping an
ellipse is as follows:

Define the bounding box R of E as the smallest rectangle that contains


E . It is the rectangle with two extreme corners as ( xc − rx , yc − ry) and
( xc + rx , yc + ry) .

G
E

A B
D

F
C

Fig. 13.

Step 1: Trivial Reject Case: (Ellipse A in Fig. 13)


Check if R is disjoint from W . This amounts to performing the
following test– if ( xc + rx < xw min or xc − rx > xw max), E does not
overlap with W . Reject E , as it is invisible. Stop. Else if
( yc + ry < yw min or yc − ry > yw max), E does not overlap with W .
Reject E. Stop. Else goto next step.
83
Computer Graphics Step 2: Trivial Select Case: (Ellipse B in Fig. 13) This is when R is
inside W. To check this, you need to perform the following test.

if ( xc − rx ≥ xw min and xc + rx ≤ xw max and yc − ry ≥ yw min and


yc + ry ≤ yw max), E is completely inside W . Send E to the frame
buffer for plotting. Else, goto next step.

Step 3: E may be partially visible (All ellipses except ellipses A, B and


C). You need to calculate intersections with boundaries. Process for each
window boundary.

Intersection with left boundary: if xc − rx ≤ xw min, E may cross the


left boundary. In this case, find out the intersection with the left boundary
by solving the following pair of equations.
ry2 ( x − x c ) 2 + rx2 ( y − y c ) 2 = rx2 ry2 ; x = xw min .

Obtain two solutions of this pair of equations.


i) If both the points are inside (Ellipse D in Fig. 13), send the portion
of the ellipse to the frame buffer which has these two points on the
boundary. Remember that here you need to apply a test to find out
which portion of the ellipse is inside and which is outside. There
are several ways of finding out the selected arc of the ellipse. One
could be to find out whether the intersection point ( x , y) lies in
Region 1 or Region 2. ( x , y) is in region 1 if ry2 x < rx2 y . This will
enable you to decide points on the ellipse to be sent to the frame
buffer.
ii) If one point is in W (Ellipse E in Fig. 13), select this point on the
ellipse as first point of intersection. Else the ellipse may be outside
the window boundary (Ellipse F, G in Fig. 13). Keep the ellipse for
the next boundary.

Process in a similar way for other window boundaries. If you get two
intersection points of the ellipse with window boundaries, plot your
ellipse accordingly. Else, reject it.

Important Note: Make sure before sending the selected segment of the
ellipse to the frame buffer, that you are sending the right portion and not
the clipped away portion. Using the above steps, write your routine.

6-22 All or none character clipping


i) Define the rectangular window boundaries.
ii) Define N. (No. of characters in the given string).
iii) Define width w of each character in the string.
iv) Define the bounding range for each character on the string using its
lower left corner. For example, a character 'G' in the string
OPENGL is at raster position (100, 50) , then its bounding range
would be (100, 50) and (100 + w , 50) (ref. Fig. 14 on the next
page).

84
v) Generate the 4-bit code(above,below,right,left) for the lower left Clipping and 3D Primitives

corner LL(i) of the i th character, i = 1, 2, … , N .


vi) Initialize i = 1 ;
vii) while(i < = N)
if LL(i) ≠ 0,
Continue;
else Display Character;

1000 1000

y = ywmax y = ywmax

0001 0000 0010 0001 0000 0010


O P E N G L
E N G L
y = ywmin y = ywmin
0100 0100
x = xwmin x = xwmin
x = xwmax
Before Clipping After Clipping
Fig. 14.

Exercises given on pages 424-425 of the book.

10-1 Let the cube be labelled as shown in Fig. 15.

V5 = (0, 0, 1) V8= (0, 1, 1)

V6= (1, 0, 1) V7= (1, 1, 1)

V1= (0, 0, 0) V4 = (0, 1, 0)

V2= (1, 0, 0) V3= (1, 1, 0)

Fig. 15.

Vertex Edge Table Polygon Surface


Table E1 : V1,V2 Table
V1 : 0,0,0 E2 : V2,V3 S1 : E4,E3,E2,E1
V2 : 1,0,0 E3 : V3,V4 S2 : E9,E10,E11,E12
V3 : 1,1,0 E4 : V4,V1 S3 : E1,E6,E9,E5
V4 : 0,1,0 E5 : V1,V5 S4 : E2,E7,E10,E6
V5 : 0,0,1 E6 : V2,V6 S5 : E3,E8,E11,E7
V6 : 1,0,1 E7 : V3,V7 S6 : E4,E5,E12,E8
V7 : 1,1,1 E8 : V4,V8
V8 : 0,1,1 E9 : V5,V6
E10 : V6,V7
E11 : V7,V8
E12 : V8,V5

85
Computer Graphics 10-7 Assume that the coordinate system is right handed coordinate system.
Let O be an object having N polygonal surfaces Si , i = 1 to n , in its
definition. Each polygonal surface is on a plane Pi with equation of plane
given by

A i x + B i , y + C i z + D i = 0, i = 1 to n .
Let P = ( x , y, z) be the given point.

A pseudo code is given below.


i) Boolean IN = true;
ii) int i = 1 ;
iii) while (i <= N )
if A *i x + B*i y + C *i z + D > 0)
IN = false;
break;

iv) if (IN) print “point is inside”;


else print “point is outside”;

10-8 In the left handed coordinate system, z will be replaced by − z in the


Exercise 10-7 above.

10-14 Quadratic Bézier blending functions − n = 2 .

BEZ k ,n (u ) = (1 − u )BEZ k ,n −1 (u ) + uBEZ k −1,n −1 (u )


k = 0 : BEZ 0,1 (u ) = (1 − u ); BEZ −1,1 (u ) = 0 .
This gives BEZ 0, 2 (u ) = (1 − u ) 2 .

k = 1 : BEZ 0,1 (u ) = (1 − u ); BEZ1,1 (u ) = u .


So, BEZ1, 2 (u ) = (1 − u ) × u + u × (1 − u ) = 2(1 − u )u .

k = 2 : BEZ1,1 (u ) = u; BEZ 2,1 (u ) = 0 .


This gives BEZ 2, 2 (u ) = u 2 .

Maximum of BEZ 0, 2 is at u = 0 , while that of BEZ 2, 2 is at u = 1 .


1
Further for BEZ1, 2 maximum is at u = . Write a program to plot
2
these functions.

10-17 The cubic Bézier curve shapes must meet in joints in such a way that
first order continuity is maintained. Suppose you need to plot N
segments, each of which is a cubic Bézier curve. Then each segment
has 4 control points. Let the i th curve segment s i have control points -
P3i , P3i+1 , P3i+ 2 , P3i+3 , i = 0, 1, … , N − 1 . To ensure continuity of the
first derivative, use formula (10-47) on page 350 of the book with
n = 3 . Apply the formula between consecutive pieces. When the first
piece finishes, its local parameter value becomes 1, while for the second
86
piece this is the starting point and therefore local parameter for the Clipping and 3D Primitives
second piece becomes 1. Hence the continuity of derivative between
the first and second piece is guaranteed if s '0 (1) = s1' (0) . This
essentially means

(P3 − P2 ) = (P4 − P3 )
Or,
P3 = (P2 + P4 ) / 2 .
Similarly, continuity of the first derivative is guaranteed between pieces
i and i + 1 , provided
Pi = (Pi−1 + Pi +1 ) / 2 .

Write your program using the Program of Bézier curves.

10-20 Use formula (10-55) on page 355 of the book recursively to compute
higher degree blending functions. For d = 2 , it goes as follows:

u − uk u k +2 − u
B k ,2 (u ) = B k ,1 (u ) + B k +1,1 (u ) .
u k +1 − u k u k + 2 − u k +1

Since B k ,1 (u ) = 1 on u ∈ [u k , u k +1 ) and is 0 otherwise, the above


formula gives following form of B k , 2 (u ) .

 u − uk
 u − u u ∈[u k , u k +1 )
B k ,2 (u) =  k +1 k
u k +2 − u
 u ∈ [u k +1 , u k + 2 ).
 u k + 2 − u k +1

These are well known Hat functions. Proceeding in this way, calculate
for d = 3 and d = 4 .

10-21 Use the result of the Exercise 10-20 and formula (10-55) of the book
again.

Exercises on page 79 of this unit.

E4) You need 5 control points to completely define the curve. Assume that
the curve shape C has three segments C1 , C 2 , C 3 . Each segment is a
quadratic Bézier curve and requires three control points. Let the control
points for first piece be P0 , P1 , P2 . Since first derivative of C is
continuous, it is natural that segments make a continuous piece. Hence
the initial point for the second piece will be P2 . Let the curve C 2 has
control points P2 , P3 , P4 while that of C 3 be given by P4 , P5 , P6 . Then
by virtue of continuity of derivative and formula (10-47) on page 350 of
the book, you get P2 = (P1 + P3 ) / 2, P4 = (P3 + P5 ) / 2 .

87
Computer Graphics E5) No change in the curve. Computation of B-spline blending functions uses
ratios only, which remain unchanged.

E6) True. Curve points are convex combination of its control points. This is
because the blending functions are non-negative and the sum of the
blending functions is 1 (see Eqn.(10-56) on page 356 of the book).

4.7 PRACTICAL EXERCISE


Session 6

1. Implement Cohen Sutherland and Liang Barsky line clipping algorithms


in C-language. Test your code for line segments with end points falling
in various regions..

***

88

You might also like