an efficient model for line clipping operation
DESCRIPTION
Final year research project: part 1TRANSCRIPT
This Presentation is Based on Following Research Papers
• S. R. Kodituwakku, K. R. Wijeweera, M. A. P. Chamikara (2013), An Efficient Algorithm for Line Clipping in Computer Graphics Programming, Ceylon Journal of Science (Physical Sciences), Volume 17, pp. 1-7.
• S. R. Kodituwakku, K. R. Wijeweera, M. A. P. Chamikara (2012), An Efficient Line Clipping Algorithm for 3D Space, International Journal of Advanced Research in Computer Science and Software Engineering, Volume 2 (Issue 5), pp. 96-101.
What is Clipping ?Any procedure which identifies that portion of a scene which is either inside or outside a region is referred to as a clipping algorithm or clipping
Line Clipping Algorithms• In computer graphics, line clipping is the process of
removing lines or portions of lines outside of a region of interest
• Well known algorithms– Cohen Sutherland 2D Algorithm– Cohen Sutherland 3D Algorithm– Liang Barsky 2D Algorithm– Liang Barsky 3D Algorithm
Clipping Window : Rectangular
x
y
minx maxx
miny
maxy
Clipping Volume : Cuboidal
x
y
minx maxx
miny
maxy
z
minz
maxz
The Scissoring MethodFOR each point ( x, y ) on the line segment
IF ( x, y ) is inside the region
Save ( x, y )
END IF
END FOR
The Scissoring Method(Against a Rectangular Window)
Condition:( x >= minx ) AND ( x <= maxx )
AND
( y >= miny ) AND ( y <= maxy )
The Scissoring Method(Against a Cuboidal Volume)
Condition:( x >= minx ) AND ( x <= maxx )
AND
( y >= miny ) AND ( y <= maxy )
AND
( z >= minz ) AND ( z <= maxz )
Problem with the Scissoring Method Computational cost =
(Cost to evaluate the condition)
*
(Number of points on the line segment)
Problem:
What will happen in high resolution systems?
Proposed Model • The simplest model ever invented• Faster than well known clipping algorithms• Low memory consumption• Flexible with different shapes of clipping regions• Easy to extend to higher dimensions• A single mathematical model
Proposed Model against a Rectangle• Line segments are divided into four classes • Let ( x1, y1 ) and ( x2, y2 ) be the end points of the line
segment
x1 = x2 y1 = y2 Nature of the line segment
F F Declined
F T Parallel to x-axis
T F Parallel to y-axis
T T A point
Proposed Model against a Rectangle(Separation into Classes)
IF { ( x1 != x2 ) AND ( y1 != y2 ) }
// Declined
ELSE IF ( x1 != x2 )
// Parallel to x-axis
ELSE IF ( y1 != y2 )
// Parallel to y-axis
ELSE
// A point
END IF
Proposed Model against a Rectangle(Separation into Classes)
IF { ( x1 != x2 ) AND ( y1 != y2 ) }
// Declined
ELSE IF ( x1 != x2 )
// Parallel to x-axis
ELSE IF ( y1 != y2 )
// Parallel to y-axis
ELSE
// A point
END IF
Proposed Model against a Rectangle(Declined)
Equation of the line segment
( x – x1) / (x2 – x1 ) = ( y – y1 ) / ( y2 – y1 );
Let’s take
L = x2 – x1;
M = y2 – y1;
Proposed Model against a Rectangle(Declined)
Using the parameter t,
( x – x1) / (x2 – x1 ) = ( y – y1 ) / ( y2 – y1 );
( x – x1) / (x2 – x1 ) = t t = ( x – x1) / L;
( y – y1 ) / ( y2 – y1 ) = t t = ( y – y1 ) / M;
Proposed Model against a Rectangle(Declined)
Using the parameter t,
( x – x1) / (x2 – x1 ) = ( y – y1 ) / ( y2 – y1 );
( x – x1) / (x2 – x1 ) = t x = x1 + L * t;
( y – y1 ) / ( y2 – y1 ) = t y = y1 + M * t;
Proposed Model against a Rectangle(Declined)
Intersection with x = p line,
t = ( p –x1) / L;
x = p;
y = y1 + M * t;
Proposed Model against a Rectangle(Declined)
Intersection with y = q line,
t = ( q – y1) / M;
x = x1 + L * t;
y = q;
Proposed Model against a Rectangle(Declined)
L = x2 – x1; M = y2 – y1;
FOR each end point ( x, y ) of the line segment
IF ( x < minx )
t = ( minx – x1 ) / L; x = minx; y = y1 + M * t;
ELSE IF ( x > maxx )
t = ( maxx – x1 ) / L; x = maxx; y = y1 + M * t;
IF ( y < miny )
t = ( miny – y1 ) / M; x = x1 + L * t; y = miny;
ELSE IF ( y > maxy )
t = ( maxy – y1 ) / M; x = x1 + L * t; y = maxy;
END IF
END FOR
Proposed Model against a Rectangle(Declined)
An important observation after code is executed!
IF line segment is completely outside
Both end points become a single point
ELSE
End points are the end points of clipped line segment
END IF
Proposed Model against a Rectangle(Declined)
Example 1: Line segment is completely inside
B
A
Proposed Model against a Rectangle(Declined)
Example 2: Line segment is partially inside
B
A
B’B’’
Proposed Model against a Rectangle(Declined)
Example 3: Line segment intersects clipping window
B
A
B’
A’
A’’
B’’
Proposed Model against a Rectangle(Declined)
Example 4: Line segment is completely outside
B
A
A’
A’’
B’
B’’
Proposed Model against a Rectangle(Declined)
How can we prove this result?
By considering all the possible test cases
1 2 3
4 5 6
7 8 9
Proposed Model against a Rectangle(Declined)
• One end point can be in 9 regions• Therefore both the end points can be in 9*9 (= 81)
ways• Now the problem can be divided into 81 classes• Each class can be further divided into subclasses
based on dissimilarities• Since the result is true for all the test cases then the
result is true for all the possible cases
Proposed Model against a Rectangle(Declined)
Example 1: Both end points are in the region 1
BA
B’A’
Due to “minx”:Subclass 1
A’’
B’’
Proposed Model against a Rectangle(Declined)
Example 1: Both end points are in the region 1
B
A
B’A’Due to “minx”:Subclass 2
Proposed Model against a Rectangle(Declined)
Example 1: Both end points are in the region 1
B
A
B’A’
Due to “minx”:Subclass 3
A’’B’’
Proposed Model against a Rectangle(Declined)
Example 2: End points are in the regions 2 and 4B
A
Subclass 1
A’
A’’B’
Proposed Model against a Rectangle(Declined)
Example 2: End points are in the regions 2 and 4
B
A
Subclass 2 A’
B’
Proposed Model against a Rectangle(Declined)
An important observation after code is executed!
IF line segment is completely outside
Both end points become a single point
ELSE
End points are the end points of clipped line segment
END IF
Now we have proved the result !
Proposed Model against a Rectangle(Declined)
IF{ [ abs ( L ) < 1 ] AND [ abs ( M ) < 1 ] }
// Ignore
ELSE
FOR each end point (x, y) of the line segment
x = FLOOR ( x + 0.5 );
y = FLOOR ( y + 0.5 );
END FOR
Line ( x1, y1, x2, y2 );
END IF
Proposed Model against a Rectangle(Precision Error)
Example 1:
1 / 3 = 0.33333…
• But in computer it is stored as 0.333333• Therefore checking the exact equality causes a
problem
Proposed Model against a Rectangle(Precision Error)
Example 2:
• Mathematically; 1 / 3 = 1 - ( 2 / 3 )• In computer
1 / 3 = 0.333333
2 / 3 = 0.666666
1 - ( 2 / 3 ) = 0.333334 Not equal
Proposed Model against a Rectangle(Dealing with Precision Error)
• Seek approximate equality instead of exact equality
( x1 = x2 ) AND ( y1 = y2)
is replaced with
[ abs ( L ) < 1 ] AND [ abs ( M ) < 1 ]
Proposed Model against a Rectangle(Integer Inputs to Line Drawing Algorithms)
• Efficient line drawing algorithms deal only with integers– Example: Bresenham line drawing algorithm
• If floating point inputs were provided then they would be truncated
• Truncation may badly affect the quality of graphics– Example: ( 3.9, 4.0 ) ( 3, 4 )
• Therefore proper approximation should be done when converting into integers
Proposed Model against a Rectangle(Better Approximation)
p = FLOOR ( p + 0.5 )
Example:
10 10.59.5 119
Proposed Model against a Rectangle(Separation into Classes)
IF { ( x1 != x2 ) AND ( y1 != y2 ) }
// Declined
ELSE IF ( x1 != x2 )
// Parallel to x-axis
ELSE IF ( y1 != y2 )
// Parallel to y-axis
ELSE
// A point
END IF
Proposed Model against a Rectangle(Parallel to x-axis)
IF { ( y1 >= miny ) AND ( y1 =< maxy ) }
FOR each end point (x, y) of the line segment
IF ( x < minx )
x = minx;
ELSE IF ( x > maxx )
x = maxx;
END IF
END FOR
IF ( x1 != x2 )
Line ( x1, y1, x2, y2 );
END IF
END IF
Proposed Model against a Rectangle(Parallel to x-axis)
Example 1: Line segment is completely inside
BA
Proposed Model against a Rectangle(Parallel to x-axis)
Example 2: Line segment is partially inside
BA B’
Proposed Model against a Rectangle(Parallel to x-axis)
Example 3: Line segment intersects clipping window
BA B’A’
Proposed Model against a Rectangle(Parallel to x-axis)
Example 4: Line segment is completely outside 1
BA B’A’
Proposed Model against a Rectangle(Parallel to x-axis)
Example 5: Line segment is completely outside 2
A B
Proposed Model against a Rectangle(Separation into Classes)
IF { ( x1 != x2 ) AND ( y1 != y2 ) }
// Declined
ELSE IF ( x1 != x2 )
// Parallel to x-axis
ELSE IF ( y1 != y2 )
// Parallel to y-axis
ELSE
// A point
END IF
Proposed Model against a Rectangle(Parallel to y-axis)
IF { ( x1 >= minx ) AND ( x1 =< maxx ) }
FOR each end point ( x, y ) of the line segment
IF ( y < miny )
y = miny;
ELSE IF ( y > maxy )
y = maxy;
END IF
END FOR
IF ( y1 != y2 )
Line ( x1, y1, x2, y2 );
END IF
END IF
Proposed Model against a Rectangle(Parallel to y-axis)
Example 1: Line segment is completely inside
B
A
Proposed Model against a Rectangle(Parallel to y-axis)
Example 2: Line segment is partially insideB
A
B’
Proposed Model against a Rectangle(Parallel to y-axis)
Example 3: Line segment intersects clipping windowB
A
B’
A’
Proposed Model against a Rectangle(Parallel to y-axis)
Example 4: Line segment is completely outside 1
B
A
B’A’
Proposed Model against a Rectangle(Parallel to y-axis)
Example 5: Line segment is completely outside 2
B
A
Proposed Model against a Rectangle(Separation into Classes)
IF { ( x1 != x2 ) AND ( y1 != y2 ) }
// Declined
ELSE IF ( x1 != x2 )
// Parallel to x-axis
ELSE IF ( y1 != y2 )
// Parallel to y-axis
ELSE
// A point
END IF
Proposed Model against a Rectangle(A Point)
IF <condition>
PutPixel ( x, y );
END IF
Condition:
( x >= minx ) AND ( x <= maxx )
AND
( y >= miny ) AND ( y <= maxy )
Similar to Scissoring Method !
Proposed Model against a Rectangle(Separation into Classes)
IF { ( x1 != x2 ) AND ( y1 != y2 ) }
// Declined
ELSE IF ( x1 != x2 )
// Parallel to x-axis
ELSE IF ( y1 != y2 )
// Parallel to y-axis
ELSE
// A point
END IF
Proposed Model against a Rectangle(Experimental Comparison)
• Programming Language:
C++;• Computer:
Intel(R) Pentium(R) Dual CPU;
E2180 @ 2.00 GHz;
2.00 GHz, 0.98 GB RAM;• IDE Details:
Turbo C++;
Version 3.0;
Copyright(c) 1990, 1992 by Borland International, Inc;
Proposed Model against a Rectangle(Experimental Comparison)
Method:• Clip window with values minx = miny = 100 and maxx =
maxy = 300 was used• Random points were generated in the range 0 - 399
– randomize() function
• Those random points were used to generate random line segments
• Number of clock cycles taken by each algorithm to clip 100000000 random line segments were counted– clock() function
Proposed Model against a Rectangle(Experimental Results)
Step Cohen-Sutherland
Liang-Barsky
Proposed Algorithm
1 2596 2452 2296
2 2593 2452 2296
3 2594 2452 2296
4 2595 2452 2296
5 2596 2451 2296
6 2593 2451 2296
7 2593 2452 2296
8 2592 2451 2296
9 2593 2452 2296
10 2594 2451 2296
Proposed Model against a Rectangle(Experimental Comparison)
Average Ratio =
Clock cycles for traditional algorithm
Clock cycles for proposed algorithm
Proposed Model against a Rectangle(Experimental Comparison)
Average Ratios
Cohen Sutherland: Proposed = 1.1297
Liang Barsky: Proposed = 1.0677
The proposed algorithm is 1.13 times faster than
Cohen Sutherland and 1.07 times faster than
Liang Barsky
Proposed Model against a Cuboid(Separation into Classes)
• Let ( x1, y1, z1 ) and ( x2, y2, z2 ) be the end points of the line segment
IF { ( x1 != x2 ) AND ( y1 != y2 ) AND ( z1 != z2 ) }
// Declined
ELSE
// Simply a 2D clipping problem
END IF
Proposed Model against a Cuboid(Declined)
Equation of the line segment
( x – x1) / (x2 – x1 ) = ( y – y1 ) / ( y2 – y1 ) = ( z – z1) / ( z2 – z1 );
Let’s take
L = x2 – x1;
M = y2 – y1;
N = z2 – z1;
Proposed Model against a Cuboid(Declined)
Using the parameter t,
( x – x1) / (x2 – x1 ) = ( y – y1 ) / ( y2 – y1 ) = ( z – z1) / ( z2 – z1 );
( x – x1) / (x2 – x1 ) = t t = ( x – x1) / L;
( y – y1 ) / ( y2 – y1 ) = t t = ( y – y1 ) / M;
( z – z1) / ( z2 – z1 ) = t t = ( z – z1) / N;
Proposed Model against a Cuboid(Declined)
Using the parameter t,
( x – x1) / (x2 – x1 ) = ( y – y1 ) / ( y2 – y1 ) = ( z – z1) / ( z2 – z1 );
( x – x1) / (x2 – x1 ) = t x = x1 + L * t;
( y – y1 ) / ( y2 – y1 ) = t y = y1 + M * t;
( z – z1) / ( z2 – z1 ) = t z = z1 + N * t;
Proposed Model against a Cuboid(Declined)
Intersection with x = p plane,
t = ( p –x1) / L;
x = p;
y = y1 + M * t;
z = z1 + N * t;
Proposed Model against a Cuboid(Declined)
Intersection with y = q plane,
t = ( q – y1) / M;
x = x1 + L * t;
y = q;
z = z1 + N * t;
Proposed Model against a Cuboid(Declined)
Intersection with z = r plane,
t = ( r –z1) / N;
x = x1 + L * t;
y = y1 + M * t;
z = r;
Proposed Model against a Cuboid(Declined)
L = x1 – x2; M = y1 – y2; N = z1 – z2;
FOR each end point ( x, y ) of the line segment
IF ( x < minx )
t = ( minx –x1) / L; x = minx; y = y1 + M * t; z = z1 + N * t;
ELSE IF ( x > maxx )
t = ( maxx –x1) / L; x = maxx; y = y1 + M * t; z = z1 + N * t;
IF ( y < miny )
t = ( miny – y1) / M; x = x1 + L * t; y = miny; z = z1 + N * t;
ELSE IF ( y > maxy )
t = ( maxy – y1) / M; x = x1 + L * t; y = maxy; z = z1 + N * t;
IF (z < minz )
t = ( minz –z1) / N; x = x1 + L * t; y = y1 + M * t; z = minz;
ELSE IF ( z > maxz )
t = ( maxz –z1) / N; x = x1 + L * t; y = y1 + M * t; z = maxz;
END IF
END FOR
Proposed Model against a Cuboid(Declined)
IF { [ abs ( L ) < 1 ] AND [ abs ( M ) < 1 ] AND [ abs ( N ) < 1 ] }
// Ignore
ELSE
FOR each end point ( x, y, z ) of the line segment
x = FLOOR ( x + 0.5 );
y = FLOOR ( y + 0.5 );
z = FLOOR ( z + 0.5 );
END FOR
Line ( x1, y1, z1, x2, y2, z2 );
END IF
Proposed Model against a Cuboid(Proof)
• Consider all the possible test cases• Proof is done as we did for a rectangular clipping
window
Proposed Model against a Cuboid(Experimental Comparison)
Average Ratios
Cohen Sutherland: Proposed = 1.1268
Liang Barsky: Proposed = 1.0651
The proposed algorithm is 1.13 times faster than
Cohen Sutherland and 1.07 times faster than
Liang Barsky
Any Questions?
Thank You !