an efficient model for line clipping operation

74
An Efficient Model for Line Clipping Operation Kasun Ranga Wijeweera ([email protected])

Upload: kasun-ranga-wijeweera

Post on 18-Dec-2014

270 views

Category:

Science


1 download

DESCRIPTION

Final year research project: part 1

TRANSCRIPT

Page 1: An Efficient Model for Line Clipping Operation

An Efficient Model for

Line Clipping Operation

Kasun Ranga Wijeweera

([email protected])

Page 2: An Efficient Model for Line Clipping Operation

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.

Page 3: An Efficient Model for Line Clipping Operation

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

Page 4: An Efficient Model for Line Clipping Operation

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

Page 5: An Efficient Model for Line Clipping Operation

Clipping Window : Rectangular

x

y

minx maxx

miny

maxy

Page 6: An Efficient Model for Line Clipping Operation

Clipping Volume : Cuboidal

x

y

minx maxx

miny

maxy

z

minz

maxz

Page 7: An Efficient Model for Line Clipping Operation

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

Page 8: An Efficient Model for Line Clipping Operation

The Scissoring Method(Against a Rectangular Window)

Condition:( x >= minx ) AND ( x <= maxx )

AND

( y >= miny ) AND ( y <= maxy )

Page 9: An Efficient Model for Line Clipping Operation

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 )

Page 10: An Efficient Model for Line Clipping Operation

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?

Page 11: An Efficient Model for Line Clipping Operation

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

Page 12: An Efficient Model for Line Clipping Operation

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

Page 13: An Efficient Model for Line Clipping Operation

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

Page 14: An Efficient Model for Line Clipping Operation

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

Page 15: An Efficient Model for Line Clipping Operation

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;

Page 16: An Efficient Model for Line Clipping Operation

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;

Page 17: An Efficient Model for Line Clipping Operation

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;

Page 18: An Efficient Model for Line Clipping Operation

Proposed Model against a Rectangle(Declined)

Intersection with x = p line,

t = ( p –x1) / L;

x = p;

y = y1 + M * t;

Page 19: An Efficient Model for Line Clipping Operation

Proposed Model against a Rectangle(Declined)

Intersection with y = q line,

t = ( q – y1) / M;

x = x1 + L * t;

y = q;

Page 20: An Efficient Model for Line Clipping Operation

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

Page 21: An Efficient Model for Line Clipping Operation

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

Page 22: An Efficient Model for Line Clipping Operation

Proposed Model against a Rectangle(Declined)

Example 1: Line segment is completely inside

B

A

Page 23: An Efficient Model for Line Clipping Operation

Proposed Model against a Rectangle(Declined)

Example 2: Line segment is partially inside

B

A

B’B’’

Page 24: An Efficient Model for Line Clipping Operation

Proposed Model against a Rectangle(Declined)

Example 3: Line segment intersects clipping window

B

A

B’

A’

A’’

B’’

Page 25: An Efficient Model for Line Clipping Operation

Proposed Model against a Rectangle(Declined)

Example 4: Line segment is completely outside

B

A

A’

A’’

B’

B’’

Page 26: An Efficient Model for Line Clipping Operation

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

Page 27: An Efficient Model for Line Clipping Operation

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

Page 28: An Efficient Model for Line Clipping Operation

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

Page 29: An Efficient Model for Line Clipping Operation

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

Page 30: An Efficient Model for Line Clipping Operation

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

Page 31: An Efficient Model for Line Clipping Operation

Proposed Model against a Rectangle(Declined)

Example 2: End points are in the regions 2 and 4B

A

Subclass 1

A’

A’’B’

Page 32: An Efficient Model for Line Clipping Operation

Proposed Model against a Rectangle(Declined)

Example 2: End points are in the regions 2 and 4

B

A

Subclass 2 A’

B’

Page 33: An Efficient Model for Line Clipping Operation

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 !

Page 34: An Efficient Model for Line Clipping Operation

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

Page 35: An Efficient Model for Line Clipping Operation

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

Page 36: An Efficient Model for Line Clipping Operation

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

Page 37: An Efficient Model for Line Clipping Operation

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 ]

Page 38: An Efficient Model for Line Clipping Operation

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

Page 39: An Efficient Model for Line Clipping Operation

Proposed Model against a Rectangle(Better Approximation)

p = FLOOR ( p + 0.5 )

Example:

10 10.59.5 119

Page 40: An Efficient Model for Line Clipping Operation

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

Page 41: An Efficient Model for Line Clipping Operation

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

Page 42: An Efficient Model for Line Clipping Operation

Proposed Model against a Rectangle(Parallel to x-axis)

Example 1: Line segment is completely inside

BA

Page 43: An Efficient Model for Line Clipping Operation

Proposed Model against a Rectangle(Parallel to x-axis)

Example 2: Line segment is partially inside

BA B’

Page 44: An Efficient Model for Line Clipping Operation

Proposed Model against a Rectangle(Parallel to x-axis)

Example 3: Line segment intersects clipping window

BA B’A’

Page 45: An Efficient Model for Line Clipping Operation

Proposed Model against a Rectangle(Parallel to x-axis)

Example 4: Line segment is completely outside 1

BA B’A’

Page 46: An Efficient Model for Line Clipping Operation

Proposed Model against a Rectangle(Parallel to x-axis)

Example 5: Line segment is completely outside 2

A B

Page 47: An Efficient Model for Line Clipping Operation

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

Page 48: An Efficient Model for Line Clipping Operation

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

Page 49: An Efficient Model for Line Clipping Operation

Proposed Model against a Rectangle(Parallel to y-axis)

Example 1: Line segment is completely inside

B

A

Page 50: An Efficient Model for Line Clipping Operation

Proposed Model against a Rectangle(Parallel to y-axis)

Example 2: Line segment is partially insideB

A

B’

Page 51: An Efficient Model for Line Clipping Operation

Proposed Model against a Rectangle(Parallel to y-axis)

Example 3: Line segment intersects clipping windowB

A

B’

A’

Page 52: An Efficient Model for Line Clipping Operation

Proposed Model against a Rectangle(Parallel to y-axis)

Example 4: Line segment is completely outside 1

B

A

B’A’

Page 53: An Efficient Model for Line Clipping Operation

Proposed Model against a Rectangle(Parallel to y-axis)

Example 5: Line segment is completely outside 2

B

A

Page 54: An Efficient Model for Line Clipping Operation

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

Page 55: An Efficient Model for Line Clipping Operation

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 !

Page 56: An Efficient Model for Line Clipping Operation

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

Page 57: An Efficient Model for Line Clipping Operation

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;

Page 58: An Efficient Model for Line Clipping Operation

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

Page 59: An Efficient Model for Line Clipping Operation

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

Page 60: An Efficient Model for Line Clipping Operation

Proposed Model against a Rectangle(Experimental Comparison)

Average Ratio =

Clock cycles for traditional algorithm

Clock cycles for proposed algorithm

Page 61: An Efficient Model for Line Clipping Operation

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

Page 62: An Efficient Model for Line Clipping Operation

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

Page 63: An Efficient Model for Line Clipping Operation

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;

Page 64: An Efficient Model for Line Clipping Operation

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;

Page 65: An Efficient Model for Line Clipping Operation

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;

Page 66: An Efficient Model for Line Clipping Operation

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;

Page 67: An Efficient Model for Line Clipping Operation

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;

Page 68: An Efficient Model for Line Clipping Operation

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;

Page 69: An Efficient Model for Line Clipping Operation

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

Page 70: An Efficient Model for Line Clipping Operation

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

Page 71: An Efficient Model for Line Clipping Operation

Proposed Model against a Cuboid(Proof)

• Consider all the possible test cases• Proof is done as we did for a rectangular clipping

window

Page 72: An Efficient Model for Line Clipping Operation

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

Page 73: An Efficient Model for Line Clipping Operation

Any Questions?

Page 74: An Efficient Model for Line Clipping Operation

Thank You !