Line Clipping against Arbitrary Polygonal Window
-
Upload
vuonghuong -
Category
Documents
-
view
248 -
download
5
Transcript of Line Clipping against Arbitrary Polygonal Window
International Journal of Computer Graphics
Vol. 6, No.1 (2015), pp.1-12
http://dx.doi.org/10.14257/ijcg.2015.6.1.01
ISSN: 2093-9663 IJCG
Copyright ⓒ 2015 SERSC
Line Clipping against Arbitrary Polygonal Window
Bimal Kumar Ray
School of Information Technology & Engineering
VIT University, Vellore – 632014, India
[email protected], [email protected]
Abstract
The Cyrus-Beck algorithm clips line against convex polygonal window, but it fails to
clip line against concave and self-intersecting window because of the underlying
assumption made in the design of the algorithm. This paper extends Skala`s algorithm to
design and develop a line clipping algorithm that clips a line against arbitrary polygonal
window – any number of sides, convex, concave and self-intersecting and in contrast to
the Cyrus-Beck algorithm, it does not compute false intersection.
Keywords: Line clipping, arbitrary polygonal window, function of separation
1. Introduction
Removing a portion of a line against a region is known as line clipping. Since a
graphics image may consist of line segments, it is necessary to device algorithm that
performs line clipping. Line clipping has its application in geographic information system,
VLSI circuits design, designing building architecture – to mention a few. The region
(window) against which clipping is performed is usually rectangular with sides parallel to
the coordinate axes but clipping against arbitrary window is also of interest and this is
why the Cyrus-Beck algorithm was extended to clip a line against arbitrary convex
polygonal window.
A number of algorithms for line and line segment clipping had been developed for
rectangular as well as polygonal convex window. The Cohen-Sutherland algorithm [1]
clips a line segment against rectangular upright window after coding the end points of the
line segment using four-bit vector. Unfortunately, it computes false intersection point and
cannot be extended to clip line segment against arbitrary window (any number of sides,
convex, concave, self-intersecting). The Cyrus-Beck algorithm [2] clips line and line
segment against convex window. It computes intersection of the clipping object with
every side of the clipping window and consequently the maximum number of false
intersection points is as large as the number of sides of the clipping window. Moreover,
this algorithm cannot clip a line against concave and self-intersecting window. An
efficient version of the Cyrus-Beck algorithm was proposed by Liang and Barsky [3] but
this algorithm too has the same drawback as that of Cyrus-Beck. The Nicholl-Lee-Nicholl
algorithm [4] introduced ′test and intersect′ approach instead of ′intersect and test′ and
thereby avoided computing false intersection. The algorithm operates on upright
rectangular clipping window and cannot be extended to clipping window of arbitrary
polygonal shape. Skala proposed an algorithm [5] for line clipping showing improvement
over the Cyrus-Beck algorithm. Skala [6] also proposed an O(lg N) algorithm for line
clipping against convex window. An O(1) algorithm for line clipping in E2 was also
proposed by him [7]. Bui and Skala [8] suggested modifications of the Cohen-Sutherland
algorithm for clipping lines and line segments based on coding the direction of line and
coding the end points of a line segment. In [9], Skala used homogeneous coordinates and
developed line and line segment clipping algorithm. For line segment clipping, outcode is
used to encode the end points of the line segment. Apart from other advantages, the
International Journal of Computer Graphics
Vol. 6, No.1 (2015)
2 Copyright ⓒ 2015 SERSC
algorithm does not require division operation, if the input and output are in homogeneous
coordinates.
This paper modifies Skala′s algorithm [9] to show that though the Cyrus-Beck
algorithm cannot clip a line against arbitrary clipping window but a modification of
Skala′s algorithm [9] can clip a line against arbitrary window – convex, concave and self-
intersecting. For the sake of convenience of the reader, an overview of the Cyrus-Beck
technique is presented in the next section.
2. Technique of Cyrus-Beck
If a clipping convex polygon consists of m vertices designated by the position vectors
xk (xk, yk), where k = 1 to m, then the inward normal vector nk of the kth edge ek = xkxk+1 of
the polygon is defined by nk = (yk+1 – yk, xk – xk+1) and the equation of the kth edge is
determined by nkx + ck = 0, where ck = xk+1yk – yk+1xk and the vector x represents an
arbitrary point on the line containing the normal. A line L passing through two points
xA(xA, yA) and xB(xB, yB) is defined in parametric form as x = xA + st, where the vector s =
xB – xA and t is a scalar that lies in the interval (−∞, ∞) for a line and in the interval [0, 1]
for a line segment Ls with end points xA(xA, yA) and xB(xB, yB). The intersection of the line
L with the kth edge is determined by tk = − (xA.nk + ck)/(nk.s). Since the polygon is convex
and the line L is infinite hence there exist exactly two true intersections of the line with
the polygon, in case the line intersects the polygon (excluding the case when either a line
grazes along a side of the polygon or passes through a vertex of the polygon, in either
case it is considered that there is no intersection of the edges of the polygon with the line).
If the dot product nk.s is positive (the edge ek and the vector s have opposite directions)
then one of the intersection points is determined by the minimum of tk values (denoted by
tmin) as tk is computed against each of the edges of the polygon. On the other hand, if the
dot product nk.s is negative then the other intersection point is determined by the
maximum of tk values (denoted by tmax). If tmin > tmax then the line does not intersect the
clipping polygon, otherwise the intersection points are evaluated by xA = xA + stmin and xB
= xA + stmax. If the dot product nk.s vanishes, that happens when the line L is parallel to
the edge ek, then this case should be reckoned as a special case and treated accordingly.
An outline of the principles of Cyrus-Beck technique is presented below in the form of an
algorithm. The inputs to the algorithm are two point xA and xB and the outputs are the end
points of the clipped segment. The same symbols are used for representing input as well
as output.
Algorithm Cyrus-Beck
begin
s := xB − xA;
tmin := −∞;
tmax := ∞;
for i := 1 to m do
begin
q := nk.s;
if q > 0 then
begin
tk := −(nk.xA + ck)/q;
tmin := max (t, tmin);
end
else if q < 0 then
begin
tk := −(nk.xA + ck)/q;
tmax := min(t, tmax);
International Journal of Computer Graphics
Vol. 6, No.1 (2015)
Copyright ⓒ 2015 SERSC 3
end
else solve a special case;
end;
if tmin < tmax then
begin
xA := xA + stmin;
xB := xA + stmax;
output(xA, xB)
end
end.
The basic requirement for the Cyrus-Beck algorithm is that the clipping window should
be convex, because the normal to all the edges needs to be inward. The algorithm fails to
clip a line if the clipping window is concave and self-intersecting. Moreover, t values are
computed for all the edges of the window though a convex polygon is intersected by a
line at exactly two points, in case intersection exists.
3. Skala`s modification of Cyrus-Beck algorithm
Skala [9] uses a `test and evaluate` instead of `evaluate and test` approach to determine
the edges of the window that are actually intersected by a line and evaluates t when
intersection exists. A function of separation is used to code the position of a line to be
clipped with respect to the position of the vertices of the clipping window. For the sake of
convenience of the reader, a brief description (detail can be found in [9]) of the technique
of coding position of a line with respect to an upright rectangular clipping window is
presented here. Homogeneous coordinates are used so that the algorithm can exploit
computational advantage of computing vector cross product on a GPU; however, the
procedure holds as well in Cartesian coordinates.
3.1. Clipping against Upright Rectangular Window
Consider an upright rectangular window with vertices xk(xk, yk, wk) (k = 1, 4) in
homogeneous coordinates with wk ≠ 0, xk being the position vector of the kth vertex of the
window and the components of xk are (xk, yk, wk). The edge ek of the polygon directed
from the vertex xk towards xk+1 is defined by
ek = xk × xk+1 = (ykwk+1 – yk+1wk, xk+1wk – xkwk+1, xkyk+1 – ykxk+1).
The equation of a line passing through two points xA(xA, yA, wA) and xB(xB, yB, wB) is
defined by
ax + by + cw = 0 (w ≠ 0), where a (a, b, c) = xA × xB.
Consider a 4-bit vector v defined by (assuming that the kth bit of v is denoted by vk)
vk = 0, if axk + byk + cwk < 0 and vk = 1, otherwise.
In [9], two one dimensional arrays, called TAB1 and TAB2 are pre-computed to
determine the edges of the polygon intersected by a line. Some of the elements of TAB1
and TAB2 are found to be inapplicable. Excluding these elements, the edges that intersect
the clipping window are found by indexing TAB1 and TAB2 with the decimal value of
the bit vector v. The procedure, called CL, is reproduced here from [9]. The input (as well
as output) xA and xB of the procedure are in homogeneous coordinates.
International Journal of Computer Graphics
Vol. 6, No.1 (2015)
4 Copyright ⓒ 2015 SERSC
Algorithm CL
begin
a := xA × xB;
for k := 1 to 4 do
begin
if a.xk < 0 then vk := 0;
else vk := 1;
end
if v ≠ (0, …, 0) and v ≠ (1, …, 1) then
begin
i := TAB1[v];
j := TAB2[v];
xA := a × ei;
xB := a × ej;
output(xA, xB);
end
end.
3.2. Clipping against <−1, 1> Rectangular Window
As already mentioned, this paper is an extension of the work presented in [9]. The
algorithm CL, presented in the last section, is transformed into an algorithm (Algorithm
CL_11 presented later in this section) so that a line can be clipped against an <−1, 1>
window reducing the cost for computation of vector cross product, the same algorithm
(CL) is also generalized to clip a line against convex polygon with an arbitrary number of
edges through on the fly comparison of the successive bits of the line code in a round
robin fashion (Algorithm CL1 presented later in section 3.3). A modification of the
algorithm CL1 enables line clipping against arbitrary concave and self-intersecting
polygonal window (Algorithm CL2 presented in section 4).
If the parameters of an upright rectangular clipping window (Left, Right, Bottom, Top)
Figure 1. A Rectangular Axis-Aligned Clipping Window. The Vertices are Labeled Starting from the Top-Left Corner and Moving in the Counter-
Clockwise Direction
be xL, xR,yB, yT with constraints xL < xR and yB < yT then the position vector of the vertices
of the window in homogeneous coordinates with w = 1 are x1 = (xL, yT, 1), x2 = (xL, yB, 1),
x3 = (xR, yB, 1) and x4 = (xR, yT, 1) (Figure 1). The vectors e1, e2, e3 and e4 are the edge
vectors defined by ei = xi × xi+1, i = 1, 4 (with modulo 4 on i) and are given by
e1 = (yT – yB, 0, xLyB – xLyT), e2 = (0, xR – xL, xLyB – xRyB),
e3 = (yB – yT, 0, xRyT – xRyB), e4 = (0, xL – xR, xRyT – xLyT).
The transformation formulae to transform an upright rectangular clipping window into
an <−1, 1> window (using prime to indicate transformed coordinates) are,
International Journal of Computer Graphics
Vol. 6, No.1 (2015)
Copyright ⓒ 2015 SERSC 5
x′ = xsx + tz, y′ = ysy + ty,
where sx = 2/(xR – xL), sy = 2/(yT – yB), tx = −(xR + xL)/(xR – xL), ty = − (yT +
yB)/(yT − yB).
The edge vectors in <−1, 1> window in homogeneous coordinates with w = 1 are
e′1 = (1, 0, 1), e′2 = (0, 1, 1), e′3 = (−1, 0, 1) and e′4 = (0, –1, 1).
The coefficients of the equation of a line passing through two points with position
vector x′A(x′A, y′A, 1) and x′B(x′B, y′B, 1) in homogeneous coordinates with w = 1 and
under <−1, 1> window, are components of the vector a′(a′, b′, c′) where
a′ = yA′ – yB′, b′ = xB′ – xA′ and c′ = xA′yB′ – xB′yA′.
The intersection of the line with the edges e′1, e′2, e′3 and e′4 are respectively
a′ × e′1 = (b′, c′ − a′, −b′), a′ × e′2 = (b − c, −a, a),
a′ × e′3 = (−b′, c′ + a′, −b′), a′ × e′4 = (b′ + c′, −a′, −a′).
The pseudo code of the procedure is presented below in the form of the algorithm CL_11.
The primed symbols used in the above analysis do not contain prime in the algorithm to
simplify the look of the same.
Algorithm CL_11
begin
a := xA × xB;
for k := 1 to 4 do
begin
if a.xk < 0 then vk := 0;
else vk := 1;
end
if v ≠ (0, …, 0) and v ≠ (1, …, 1) then
begin
i := TAB1[v];
j := TAB2[v];
if i = 1 then xA := (b, c – a, −b);
else if i = 2 then xA := (b − c, −a, a);
else if i = 3 then xA := (−b, c + a, −b);
else if i = 4 then xA := (−b − c, a, a);
if j = 1 then xB := (b, c – a, −b);
else if j = 2 then xB := (b − c, −a, a);
else if j = 3 then xB := (−b, c + a, −b);
else if j = 4 then xB := (−b − c, a, a);
output(xA, xB);
end
end.
3.3. Clipping against Arbitrary Convex Window
If the clipping polygon consists of an arbitrary number (m) of vertices then the v vector
consists of m bits denoted by vk, k = 1,…, m. If two successive bits vk and vk+1 are
unequal then the kth and (k + 1)
st vertices are on the opposite sides of the line to be clipped
and hence the line intersects the kth edge of the polygon and the point of intersection is
given by a × ek. If the polygon has a convex topology then the line intersects (if it
intersects at all) the polygon at exactly two points (Figure 2), excluding the singular cases.
In singular cases a line may pass through a vertex of the clipping polygon and in this case
International Journal of Computer Graphics
Vol. 6, No.1 (2015)
6 Copyright ⓒ 2015 SERSC
the intersection is excluded and if a line grazes along a side of the polygon then the line is
said not to have intersected the polygon. (Note that these singular cases are also
applicable to clipping against upright rectangular window and are dealt in with similar
logic). A Boolean flag is used to indicate whether there exists a point of intersection
between a line and the clipping polygon. An initial value of the flag is flipped once an
intersection is found. The pseudo code of the procedure is presented below in the form of
Algorithm CL1. The arithmetic on array index is in modulo m. The identifier ′visible′
used in the algorithm is a Boolean flag initialized with false value indicating that the first
intersection point is yet to be found.
Figure 2. A Convex Polygonal Window and a Line. A Line Intersects a Convex Window at Exactly Two Points, if it Intersects the Window
Algorithm CL1
begin
a := xA × xB;
for k := 1 to m do
begin
if a.xk < 0 then vk := 0;
else vk := 1;
end
for k := 1 to m do
begin
if vk ≠ vk+1 then
begin
if visible is false then
begin
xA := a × ek;
visible := true;
end
else
xB := a × ek;
end
end
if visible is true then output (xA, xB);
end.
4. Clipping against Concave and Self-Intersecting Window
If the clipping polygon is of concave topology (Figure 3) (may be self-intersecting too,
Figure 4) then if the line to be clipped intersects the clipping polygon then it does so at
even number of points (singular cases are excluded by suitable modification in the
computation of the v vector). In this case, in order to find the clipped line, it is necessary
to sort the points of intersection, pair the intersection points and then join each pair by a
line segment. Sorting can be performed based on either x or y coordinates. But this sorting
is not robust because of floating point comparison among x (or y) values, which is not
International Journal of Computer Graphics
Vol. 6, No.1 (2015)
Copyright ⓒ 2015 SERSC 7
stable for nearly vertical (or horizontal) line. So the equation of the line is used in
parametric form and is defined by L: x = xA + st,
−∞ < t < ∞ and s = (xB − xA).
As in the case of convex polygon, here also the line is coded using a function of
separation
ax + by + cw = 0, where a = [a, b, c] = xA × xB,
yielding an m-bit vector v. The successive bits vk and vk+1 of the vector are tested for
inequality and on success, the t value of the point of intersection is computed using
t = −(xA.ek)/(ek.s)
in homogeneous coordinates. A collection of t values holds the intersection parameters for
different points of intersection. These values are sorted (in increasing order, say), are
paired in the form (tk, tk+1), where tk+1 > tk and intersection coordinates for the pair are
computed using
xA = xA + stk and xB = xA + stk+1, where k = 1, … , m, subscript k being in modulo
n.
Each such pair of points is joined by a line segment to form the clipped line. The
algorithm CL2, shown below, depicts the procedure. It clips a line against a concave
window with arbitrary number of vertices; it can clip a line against self-intersecting
window too. The algorithm can also be used for line clipping against convex window
though call to sorting of t values will be redundant. The identifier l used in the algorithm
is the number of points of intersection of the line with the window and is initialized by
zero to indicate that the first intersection point is yet to be found.
(a) (b)
Figure 3. (a) A Concave Planar Clipping Window and a Line. (b) Any such Window Intersects a Line at an Even Number of Points, if Intersection
Exists
(a) (b)
Figure 4. (a) A Self-Intersecting Polygonal Clipping Window and a Line. (b) Any such Window Intersects A Line at an Even Number of Points, if
Intersection Exists
International Journal of Computer Graphics
Vol. 6, No.1 (2015)
8 Copyright ⓒ 2015 SERSC
Algorithm CL2
begin
a := xA × xB;
for k := 1 to m do
begin
if a.xk < 0 then vk := 0;
else vk := 1;
end
for k := 1 to m do
begin
if vk ≠ vk+1 then
begin
l := l + 1;
t[l] := −(xA.ek)/(ek.s);
visible := true;
end
end
Sort the elements of t array in increasing order.
Compute the coordinates of the point of intersection.
Join every pair of consecutive points of intersection by line segment.
end.
5. Performance Measurement
The algorithm developed here has been implemented in C++ and its performance is
tested and compared with an implementation of the Cyrus-Beck algorithm in Cartesian
coordinates (algorithm Cyrus-Beck presented in Section 2). The actual execution time on
Intel Core 2 Duo 2GHz processor on one hundred runs, each run consisting of five million
random lines is computed for the implementation of the Cyrus-Beck algorithm, CL1 and
CL2. The mean (μ), standard deviation (σ) and the three-sigma limits of the execution
time of the lines for Cyrus-Beck and CL1 are displayed in the Table I for convex polygon
with different number of sides (n). The two points on random lines are generated using
random number generator and so also the vertices of the clipping polygon.
Table I. The Mean (μ), Standard Deviation (σ) and Three-Sigma Limits of Time to Clip a Line against Polygon with Different Number of Sides (n)
n
CyrusBeck CL1
μ σ μ – 3σ μ + 3σ μ σ μ – 3σ μ + 3σ
3 1.88e−07 6.82e-08 −1.67e−08 3.93e−07 2.74e−07 9.66e−08 −1.57e−08 5.64e−07
4 2.20e−07 6.0e−08 4.00e−08 4.00e−07 3.46e−07 9.32e−08 6.64e−08 6.26e−07
5 2.48e−07 8.54e−08 −8.25e−09 5.04e−07 4.06e−07 5.97e−08 2.27e−08 5.85e−07
6 2.74e−07 9.66e−08 −1.57e−08 5.64e−07 4.78e−07 9.75e−08 1.85e−07 7.71e−07
8 3.50e−07 8.66e−08 9.02e−08 6.09e−07 6.14e−07 5.83e−08 4.39e−07 7.89e−07
From the results in the Table I it may be observed that the performance of the proposed
algorithm CL1 is not better than that of the Cyrus-Beck algorithm. But Cyrus-Beck
algorithm computes false intersection points whereas CL1 does not.
The Table II shows the mean, standard deviation and the three-sigma limits of the
execution time of the lines for the implementation of CL2 for convex polygon.
Comparing these results with the Cyrus-Beck algorithm shows that as long as convex
polygonal clipping window is used CL2 does not show improvement over the Cyrus-Beck
algorithm.
International Journal of Computer Graphics
Vol. 6, No.1 (2015)
Copyright ⓒ 2015 SERSC 9
Table II. The Mean (μ), Standard Deviation (σ) and Three-Sigma Limits of Time to Clip a line against arbitrary polygon (convex, concave, self-
intersecting) with Different Number of Sides (n)
n
CL2
μ σ μ – 3σ μ + 3σ
3 2.60e−07 9.17e−08 −1.50e−08 5.35e−07
4 3.36e−07 9.33e−08 5.61e−08 6.16e−07
5 4.46e−07 8.42e−08 1.94e−07 6.98e−07
6 5.34e−07 9.40e−08 2.52e−07 8.16e−07
8 7.38e−07 9.25e−08 4.61e−07 1.02e−06
The advantage of the proposed algorithm CL2 is that it can clip a line against concave
and self-intersecting polygonal window whereas Cyrus-Beck can clip a line if the clipping
window is convex but it cannot clip a line if the clipping window is concave and self-
intersecting.
The algorithm CL2 was tested widely along with the Cyrus-Beck algorithm for a wide
variety of clipping window with arbitrary number of sides and arbitrary topology –
convex, concave and self-intersecting – with lines after generating the clipping window
and the lines randomly. The experiments show that the Cyrus-Beck algorithm fails to clip
a line whenever the clipping window is concave and self-intersecting. The Figure 5 shows
some of these results for concave and self-intersecting clipping window with vertices
varying from five to twelve. The dashed line shows the input line and the continuous line
segment(s) is/are the output segment(s). The left column of the figure shows the output
produced by the Cyrus-Beck algorithm and the right one shows that produced by CL2. It
may be observed from these figures that the Cyrus-Beck algorithm does not return any
results as the clipping polygons are concave and/or self-intersecting.
International Journal of Computer Graphics
Vol. 6, No.1 (2015)
10 Copyright ⓒ 2015 SERSC
International Journal of Computer Graphics
Vol. 6, No.1 (2015)
Copyright ⓒ 2015 SERSC 11
Figure 5. Clipping Window, Line to be Clipped (shown in Dashed Line) and Clipped Line Segment(s) (shown as Continuous Line. The Left Column Shows the Output of the Cyrus-Beck Algorithm and the Right Column
Shows the Output of the Proposed Algorithm CL2. The Clipping Polygons with Number of Sides Varying from Five to Twelve Only are shown here
6. Conclusion
Skala`s algorithm [9] has been extended successfully for clipping a line against
polygonal window of arbitrary topology (convex, concave, self-intersecting). Though the
performance of the algorithm CL1 and CL2 is not better than that of the Cyrus-Beck
algorithm, but the Cyrus-Beck algorithm cannot clip a line whenever the clipping polygon
is concave and self-intersecting. Moreover, the Cyrus-Beck algorithm detects false
intersection points whereas the proposed algorithm by virtue of its ′test and evaluate′
approach does not compute false intersection point. The algorithm CL2 is found to work
consistently on a wide variety of clipping windows with arbitrary number of sides and of
arbitrary shapes. The algorithm will benefit in an increase in speed if vector – vector
operations and/or parallel processing is used. The cross-product processor too will
increase the performance significantly.
References
[1] J. D. Foley, A. van Dam, S. K. Finer and J. F. Hughes, Computer Graphics: Principles and Practice,
Addison-Wesley, (2nd edition in C), (1996).
[2] M. Cyrus and J. Beck, Generalized two-and three-dimensional clipping, Computers & Graphics, 2(1),
pp. 23 – 28, (1978).
[3] Y. Liang and B. Barsky, A new concept and method for line clipping, ACM Transactions on Graphics
3(1), pp. 1 – 22, (1984).
[4] T. M. Nicholl, D. T. Lee and R. A. Nicholl, An efficient new algorithm for 2-D line clipping: its
development and analysis. In : SIGGRAPH Proceedings 21(4), pp. 253–262, (1987).
[5] V. Skala, An efficient algorithm for line clipping by convex polygon. Computers & Graphics, 17(4), pp.
417–421, (1993).
[6] V. Skala, O(lg N) line clipping algorithm in E2, Computers & Graphics, 18(4), pp. 517–524, (1994).
[7] V. Skala, Line Clipping in E2 with O(1) Processing Complexity, Computers & Graphics 20(4), pp. 523–
530, (1996).
International Journal of Computer Graphics
Vol. 6, No.1 (2015)
12 Copyright ⓒ 2015 SERSC
[8] D. H. Bui and V. Skala, Fast algorithms for clipping lines and line segments in E2, The Visual
Computer, 14(1), pp. 31 – 37, (1998).
[9] V. Skala, A new approach to line and line segment clipping in homogeneous coordinates, Visual
Computer, 21, pp. 905–914 (2005).
Author
Bimal Kumar Ray, received B.Sc. Honours degree in
Mathematics from St. Xavier`s College, Kolkata, M. Sc.
degree in Applied Mathematics from Calcutta University,
Kolkata and Ph.D. degree in Computer Science from Indian
Statistical Institute, Kolkata. His areas of interests are
Computer Graphics, Vision and Image Processing. He had
published a number of papers in peer-reviewed journals.