28
June 10, 2022 June 10, 2022 SCG 3023 SCG 3023 1 Line Clipping in 2D Line Clipping in 2D

Line Clipping in 2D

  • Upload
    amish

  • View
    73

  • Download
    0

Embed Size (px)

DESCRIPTION

Line Clipping in 2D. Why would we clip?. We clip objects to our view before rasterization. Why? To avoid unnecessary work: pixel coordinate calculations parameter interpolation Any other reasons?. 2D Viewing. What do we want out of clipping?. Clipping window. (xwmin, ywmax). - PowerPoint PPT Presentation

Citation preview

Page 1: Line Clipping in 2D

April 22, 2023April 22, 2023 SCG 3023SCG 3023 11

Line Clipping in 2DLine Clipping in 2D

Page 2: Line Clipping in 2D

April 22, 2023April 22, 2023 SCG 3023SCG 3023 22

Why would we clip?Why would we clip?

We clip objects to our view before rasterization. Why?We clip objects to our view before rasterization. Why?

To avoid unnecessary work:To avoid unnecessary work:pixel coordinate calculationspixel coordinate calculationsparameter interpolationparameter interpolation

Any other reasons?Any other reasons?

Page 3: Line Clipping in 2D

April 22, 2023April 22, 2023 SCG 3023SCG 3023 33

2D Viewing2D Viewing

Page 4: Line Clipping in 2D

April 22, 2023April 22, 2023 SCG 3023SCG 3023 44

What do we want out of clipping?What do we want out of clipping?

Clipping window

(xwmin, ywmin)

(xwmin, ywmax)

(xwmax, ywmin)

(xwmax, ywmax)

Page 5: Line Clipping in 2D

April 22, 2023April 22, 2023 SCG 3023SCG 3023 55

What are the basic steps?What are the basic steps?

1.1. Determine if the line needs clippingDetermine if the line needs clippingMay be able to trivially accept or reject some linesMay be able to trivially accept or reject some lines

2.2. Find intersections of line with viewportFind intersections of line with viewportWe can use We can use y = y = mmxx + b + b to do this to do this

We want to determine which edges of the viewport to test lines We want to determine which edges of the viewport to test lines against and avoid unnecessary tests.against and avoid unnecessary tests.

We’ll start by categorizing the regions around the display.We’ll start by categorizing the regions around the display.

Page 6: Line Clipping in 2D

April 22, 2023April 22, 2023 SCG 3023SCG 3023 66

Cohen-Sutherland line clippingCohen-Sutherland line clipping

InsideInside

BBottomottom

TTopop

LLefteft RRightight

BBottom-ottom-RRightight

TTop-op-RRightightTTop-op-LLefteft

BBottom-ottom-LLefteft

TBRL

Page 7: Line Clipping in 2D

April 22, 2023April 22, 2023 SCG 3023SCG 3023 77

Cohen-Sutherland line clippingCohen-Sutherland line clipping

00000000

01000100

10001000

00010001 00100010

01100110

1010101010011001

01010101

T B R LRegion codesBit 1 2 3 4

Page 8: Line Clipping in 2D

April 22, 2023April 22, 2023 SCG 3023SCG 3023 88

Region codingRegion coding

How would you decide which region an endpoint is in?How would you decide which region an endpoint is in?

e.ge.g if (x<xwmin)&&(y>ymax) if (x<xwmin)&&(y>ymax) the point is at the Top-Left the point is at the Top-Left

Are there cases we can trivially accept or reject?Are there cases we can trivially accept or reject?

How would you test for those?How would you test for those?

Page 9: Line Clipping in 2D

April 22, 2023April 22, 2023 SCG 3023SCG 3023 99

algorithmalgorithm

1.1. Assign a region code for each endpoints.Assign a region code for each endpoints.2.2. If both endpoints have a region code 0000 ---If both endpoints have a region code 0000 --- trivially accept trivially accept

these line.these line.3.3. Else, perform the logical AND operation for both region codes.Else, perform the logical AND operation for both region codes.

3.1 if the result is not 0000 -3.1 if the result is not 0000 - trivially reject the line. trivially reject the line.3.2 else – (result = 0000, need clipping)3.2 else – (result = 0000, need clipping)

3.2.1. Choose an endpoint of the line that is outside the window.3.2.1. Choose an endpoint of the line that is outside the window. 3.2.2. Find the intersection point at the window boundary (base on 3.2.2. Find the intersection point at the window boundary (base on region code). region code). 3.2.3. Replace endpoint with the intersection point and update the 3.2.3. Replace endpoint with the intersection point and update the region code.region code. 3.2.4. Repeat step 2 until we find a clipped line either trivially accepted 3.2.4. Repeat step 2 until we find a clipped line either trivially accepted or trivially rejected.or trivially rejected.

4. Repeat step 1 for other lines.4. Repeat step 1 for other lines.

Page 10: Line Clipping in 2D

April 22, 2023April 22, 2023 SCG 3023SCG 3023 1010

How to check for intersection?How to check for intersection?if bit 1 = 1 if bit 1 = 1 there is intersection on TOP boundary. there is intersection on TOP boundary.if bit 2 = 1 if bit 2 = 1 .. .. .. .. BOTTOM .. .. .. .. .. BOTTOM ..if bit 3 = 1 if bit 3 = 1 .. .. .. .. RIGHT .. .. .. .. .. RIGHT ..

If bit 4 = 1 If bit 4 = 1 .. .. .. .. LEFT .. .. .. .. .. LEFT .. How to find intersection point?How to find intersection point?use line equationuse line equation

intersection with LEFT or RIGHT boundary.intersection with LEFT or RIGHT boundary.x = xwmin (LEFT) x = xwmax (RIGHT)x = xwmin (LEFT) x = xwmax (RIGHT)

y = y1 + m(x –x1)y = y1 + m(x –x1)intersection with BOTTOM or TOP boundary.intersection with BOTTOM or TOP boundary.y = ywmin (BOTTOM) y = ywmax (TOP)y = ywmin (BOTTOM) y = ywmax (TOP)

x = x1 + (y –y1)/mx = x1 + (y –y1)/m

Page 11: Line Clipping in 2D

April 22, 2023April 22, 2023 SCG 3023SCG 3023 1111

Trivial accept & rejectTrivial accept & reject

00000000

01000100

10001000

00010001 00100010

01100110

1010101010011001

01010101

A1

A2

B1

D1

C1

B2

C2

D2

Page 12: Line Clipping in 2D

April 22, 2023April 22, 2023 SCG 3023SCG 3023 1212

ExampleExample

00000000

01000100

10001000

00010001 00100010

01100110

1010101010011001

01010101

A1

A2

1. A1=0000,A2=00002. (both 0000) – Yes -> accept & draw3. 3.1 3.2 3.2.1 3.2.2 3.2.3 3.2.4

algorithm1. A1=0000,A2=00002. (both 0000) – Yes -> accept & draw3. 3.1 3.2 3.2.1 3.2.2 3.2.3 3.2.4

Page 13: Line Clipping in 2D

April 22, 2023April 22, 2023 SCG 3023SCG 3023 1313

ExampleExample

00000000

01000100

00010001 00100010

0110011001010101

A1

A2

B1

B21. B1=1001,B2=10102. (both 0000) – No3. AND Operation

B1 1001B2 1010Result 1000

3.1 (not 0000) – Yes reject 3.2 3.2.1 3.2.2 3.2.3 3.2.4

algorithm1. B1=1001,B2=10102. (both 0000) – No3. AND Operation

B1 1001B2 1010Result 1000

3.1 (not 0000) – Yes reject 3.2 3.2.1 3.2.2 3.2.3 3.2.4

1. B1=1001,B2=10102. (both 0000) – No3. AND Operation

B1 1001B2 1010Result 1000

3.1 (not 0000) – Yes reject 3.2 3.2.1 3.2.2 3.2.3 3.2.4

1. B1=1001,B2=10102. (both 0000) – No3. AND Operation

B1 1001B2 1010Result 1000

3.1 (not 0000) – Yes reject 3.2 3.2.1 3.2.2 3.2.3 3.2.4

10011001 10001000 10101010

Page 14: Line Clipping in 2D

April 22, 2023April 22, 2023 SCG 3023SCG 3023 1414

ExampleExample

00000000

01000100

10001000

00100010

01100110

1010101010011001

01010101

A1

A2C1

C2

algorithm1. C1=0001,C2=00002. (both 0000) – No 3. AND Operation

0001 0000

result 0000 3.1(not 0000) - No 3.2. (0000)-Yes 3.2.1. choose C1 3.2.2. Intersection point, C1’ at LEFT 3.2.3 C1 <- C1’ C1 = 0000 3.2.4 repeat 2

1. C1=0001,C2=00002. (both 0000) – No 3. AND Operation

0001 0000

result 0000 3.1(not 0000) - No 3.2. (0000)-Yes 3.2.1. choose C1 3.2.2. Intersection point, C1’ at LEFT 3.2.3 C1 <- C1’ C1 = 0000 3.2.4 repeat 2

1. C1=0001,C2=00002. (both 0000) – No 3. AND Operation

0001 0000

result 0000 3.1(not 0000) - No 3.2. (0000)-Yes 3.2.1. choose C1 3.2.2. Intersection point, C1’ at LEFT 3.2.3 C1 <- C1’ C1 = 0000 3.2.4 repeat 2

C1’

1. C1=0001,C2=00002. (both 0000) – No 3. AND Operation

0001 0000

result 0000 3.1(not 0000) - No 3.2. (0000)-Yes 3.2.1. choose C1 3.2.2. Intersection point, C1’ at LEFT 3.2.3 C1 <- C1’ C1 = 0000 3.2.4 repeat 2

1. C1=0001,C2=00002. (both 0000) – No 3. AND Operation

0001 0000

result 0000 3.1(not 0000) - No 3.2. (0000)-Yes 3.2.1. choose C1 3.2.2. Intersection point, C1’ at LEFT 3.2.3 C1 <- C1’ C1 = 0000 3.2.4 repeat 2

C1’

C2

00010001

1. C1=0001,C2=00002. (both 0000) – Yes -> accept & draw3. 3.1 3.2 3.2.1 3.2.2 3.2.3 3.2.4

Page 15: Line Clipping in 2D

April 22, 2023April 22, 2023 SCG 3023SCG 3023 1515

ExampleExample

00000000

01000100

10001000

00100010

01100110

1010101010011001

01010101

A1

A2

C1’

C2

algorithm

00010001

D1

D2

D1’

D2

D1’’

D2D2’

D1’’

D2’’

D1’’

Page 16: Line Clipping in 2D

April 22, 2023April 22, 2023 SCG 3023SCG 3023 1616

ExampleExample

(10, 10)

(150, 100)

Diberi tetingkap ketipan seperti di atas. Sekiranya titik P1 ialah (0, 120) dan titik P2(130, 5) . Dapatkan titik-titik persilangan yang membentuk garisan selepas proses ketipan. Gunakan algoritma Cohen-Sutherland

Page 17: Line Clipping in 2D

April 22, 2023April 22, 2023 SCG 3023SCG 3023 1717

answeranswer

1. P1=1001, P2=01002. (both 0000) – No3. AND Operation

B1 1001B2 0100Result 0000

3.1 (not 0000) – no 3.2 (0000) yes 3.2.1choose P1 3.2.2 intersection with LEFT boundary m = (5-120)/(130-0) = -0.8846m = (5-120)/(130-0) = -0.8846• y = y1 + m(x –x1) where x = 10;y = y1 + m(x –x1) where x = 10;• y = 120 + -0.8846(10-0) = 111.15 = 111y = 120 + -0.8846(10-0) = 111.15 = 111• P1’ = (10, 111)P1’ = (10, 111) 3.2.3 update region code P1’ = 1000 (TOP) 3.2.4 repeat step 2

1. P1=1001, P2=01002. (both 0000) – No3. AND Operation

B1 1000B2 0100Result 0000

3.1 (not 0000) – no 3.2 (0000) yes 3.2.1choose P1’ 3.2.2 intersection with TOP boundary m = (5-120)/(130-0) = -0.8846m = (5-120)/(130-0) = -0.8846• x = x1 + (y –y1)/m where yx = x1 + (y –y1)/m where y = 100; = 100;• x = 10 + (100-111)/ -0.8846 = 22.44 = 22x = 10 + (100-111)/ -0.8846 = 22.44 = 22• P1’’ = (22, 100)P1’’ = (22, 100) 3.2.3 update region code P1’’ = 0000 3.2.4 repeat step 2

1. P1=1001, P2=01002. (both 0000) – No3. AND Operation

B1 0000B2 0100Result 0000

3.1 (not 0000) – no 3.2 (0000) yes 3.2.1choose P2 3.2.2 intersection with BOTTOM boundary m = (5-120)/(130-0) = -0.8846m = (5-120)/(130-0) = -0.8846• x = x1 + (y –y1)/m where yx = x1 + (y –y1)/m where y = 10; = 10;• x = 130 + (10-5)/ -0.8846 = 124.35 = 124x = 130 + (10-5)/ -0.8846 = 124.35 = 124• P2’ = (124, 10)P2’ = (124, 10) 3.2.3 update region code P2’ = 0000 3.2.4 repeat step 2

1. P1=1001, P2=01002. (both 0000) – yes ACCEPT & DRAW

Endpoints after clippingP1’’ = (22, 100) P2’ = 124, 10)

Page 18: Line Clipping in 2D

April 22, 2023April 22, 2023 SCG 3023SCG 3023 1818

The good and the badThe good and the bad

What’s the maximum number of clips for an accepted line?What’s the maximum number of clips for an accepted line?

What’s the maximum number of clips for a rejected line?What’s the maximum number of clips for a rejected line?

Good:Good:Easy to implementEasy to implementEarly accept/reject testsEarly accept/reject tests

Bad:Bad:Slow for many clipped linesSlow for many clipped lines

Page 19: Line Clipping in 2D

April 22, 2023April 22, 2023 SCG 3023SCG 3023 1919

Liang-Barsky Line ClippingLiang-Barsky Line Clipping

• Based on parametric equation of a line:Based on parametric equation of a line:x = xx = x11 + u. + u.xxy = yy = y11 + u. + u.yy

• Similarly, the clipping window is represented by:Similarly, the clipping window is represented by:xwxwminmin x x11 + u. + u.x x xw xwmaxmax

ywywminmin y y11 + u. + u.y y yw ywmaxmax

… … or,or,u. pu. pkk q qkk k = 1, 2, 3, 4k = 1, 2, 3, 4

• where:where:

0 0 u u 1 1

pp11 = - = - x ,x , qq11 = x = x11 – xw – xwminmin

pp22 = = x ,x , qq22 = xw = xwmaxmax- x- x11

pp33 = - = - y ,y , qq33 = y = y11 – yw – ywminmin

pp44 = = y ,y , qq44 = yw = ywmax max - y- y11

Page 20: Line Clipping in 2D

April 22, 2023April 22, 2023 SCG 3023SCG 3023 2020

Liang-Barsky (continued)Liang-Barsky (continued)

• Clipped line will be:Clipped line will be:xx11’ = x’ = x11 + u + u11. . x;x; uu11 0 0yy11’ = y’ = y11 + u + u11. . y;y;

xx22’ = x’ = x11 + u + u22. . x; x; uu22 1 1 yy22’ = y’ = y11 + u + u22. . y;y;

• Reject line with pReject line with pkk = 0 and q = 0 and qkk < 0. < 0.• Calculate uCalculate uk k

uk = qk/pk

Page 21: Line Clipping in 2D

April 22, 2023April 22, 2023 SCG 3023SCG 3023 2121

Liang-Barsky (continued)Liang-Barsky (continued)

• uu11 : maximum value between 0 and u (for p : maximum value between 0 and u (for pkk < 0), where < 0), where starting value for ustarting value for u11 is 0 (u is 0 (u1 1 =0)=0)• uu22 : minimum value between u and 1 (for p : minimum value between u and 1 (for pkk > 0), where > 0), where

starting value for ustarting value for u2 2 is 1 (uis 1 (u2 2 = 1)= 1)

• Consider our previous example where:Consider our previous example where:xwxwminmin = 0, = 0, xwxwmax max = 100 = 100

ywywminmin = 0, = 0, ywywmax max = 50 = 50

And the line we want to clip connects PAnd the line we want to clip connects P11(10, 10) and P(10, 10) and P22(110, 40)(110, 40)

Page 22: Line Clipping in 2D

April 22, 2023April 22, 2023 SCG 3023SCG 3023 2222

Liang-Barsky (example)Liang-Barsky (example)

• Lets construct a table:Lets construct a table:

kk ppkk qqkk uukk

11xx= -(110-10)= -(110-10)= -100= -100

xx11 – xw – xwminmin

= 10-0 = 10= 10-0 = 10

22xx=110-10=100=110-10=100

xwxwmaxmax- x- x11

= 100 – 10 = 90= 100 – 10 = 90

33yy= -(40-10)= -(40-10)=-30=-30

yy11 – yw – ywminmin

= 10–0 = 10= 10–0 = 10

44yy= 40-10=30= 40-10=30

ywywmax max - y- y11 = 50 – 10 = 40= 50 – 10 = 40

Page 23: Line Clipping in 2D

April 22, 2023April 22, 2023 SCG 3023SCG 3023 2323

Liang-Barsky (example)Liang-Barsky (example)

• Lets construct a table:Lets construct a table:

kk ppkk qqkk uukk

11xx= -(110-10)= -(110-10)= -100= -100

xx11 – xw – xwminmin

= 10-0 = 10= 10-0 = 10

22xx=110-10=100=110-10=100

xwxwmaxmax- x- x11

= 100 – 10 = 90= 100 – 10 = 90

33yy= -(40-10)= -(40-10)=-30=-30

yy11 – yw – ywminmin

= 10–0 = 10= 10–0 = 10

44yy= 40-10=30= 40-10=30

ywywmax max - y- y11 = 50 – 10 = 40= 50 – 10 = 40

uu11

uu11

Since ppk k < 0

Page 24: Line Clipping in 2D

April 22, 2023April 22, 2023 SCG 3023SCG 3023 2424

Liang-Barsky (example)Liang-Barsky (example)

• uu11 : maximum value between 0 and u (for p : maximum value between 0 and u (for pkk < 0)! < 0)!

kk ppkk qqkk uukk

11xx= -(110-10)= -(110-10)= -100= -100

xx11 – xw – xwminmin

= 10-0 = 10= 10-0 = 10u=10/(-100)u=10/(-100)=-1/10=-1/10

22xx=110-10=100=110-10=100

xwxwmaxmax- x- x11

= 100 – 10 = 90= 100 – 10 = 90

33yy= -(40-10)= -(40-10)=-30=-30

yy11 – yw – ywminmin

= 10–0 = 10= 10–0 = 10u=10/(-30)u=10/(-30)=-1/3=-1/3

44yy= 40-10=30= 40-10=30

ywywmax max - y- y11 = 50 – 10 = 40= 50 – 10 = 40

uu11

uu11

We opt We opt uu11 =0, =0,

Page 25: Line Clipping in 2D

April 22, 2023April 22, 2023 SCG 3023SCG 3023 2525

Liang-Barsky (example)Liang-Barsky (example)

• uu22 : minimum value between u (for p : minimum value between u (for pkk > 0) and 1 > 0) and 1

kk ppkk qqkk uukk

11xx= -(110-10)= -(110-10)= -100= -100

xx11 – xw – xwminmin

= 10-0 = 10= 10-0 = 10u=10/(-100)u=10/(-100)=-1/10=-1/10

22xx=110-10=100=110-10=100

xwxwmaxmax- x- x11

= 100 – 10 = 90= 100 – 10 = 90

33yy= -(40-10)= -(40-10)=-30=-30

yy11 – yw – ywminmin

= 10–0 = 10= 10–0 = 10u=10/(-30)u=10/(-30)=-1/3=-1/3

44yy= 40-10=30= 40-10=30

ywywmax max - y- y11 = 50 – 10 = 40= 50 – 10 = 40

uu22

uu22

Since ppk k > 0

We opt We opt uu11 =0, =0,

Page 26: Line Clipping in 2D

April 22, 2023April 22, 2023 SCG 3023SCG 3023 2626

Liang-Barsky (example)Liang-Barsky (example)

• uu22 : minimum value between u (for p : minimum value between u (for pkk > 0) and 1 > 0) and 1

kk ppkk qqkk uukk

11xx= -(110-10)= -(110-10)= -100= -100

xx11 – xw – xwminmin

= 10-0 = 10= 10-0 = 10u=10/(-100)u=10/(-100)=-1/10=-1/10

22xx=110-10=100=110-10=100

xwxwmaxmax- x- x11

= 100 – 10 = 90= 100 – 10 = 90u=90/100u=90/100=9/10=9/10

33yy= -(40-10)= -(40-10)=-30=-30

yy11 – yw – ywminmin

= 10–0 = 10= 10–0 = 10u=10/(-30)u=10/(-30)=-1/3=-1/3

44yy= 40-10=30= 40-10=30

ywywmax max - y- y11 = 50 – 10 = 40= 50 – 10 = 40

u=40/30)u=40/30)=4/3=4/3

uu22

uu22

We opt We opt uu22 = 0.9 = 0.9

We opt We opt uu11 =0, =0,

Page 27: Line Clipping in 2D

April 22, 2023April 22, 2023 SCG 3023SCG 3023 2727

Liang-Barsky (example)Liang-Barsky (example)

• If uIf u11 > u > u22 then then reject reject line (completely outside clipping line (completely outside clipping window!)window!)

• Clipped line will be:Clipped line will be:xx11’ = x’ = x11 + u + u11. . xx (u(u11 = 0) = 0) = 10 + 0.(100) = 10= 10 + 0.(100) = 10yy11’ = y’ = y11 + u + u11. . yy = 10 + 0.(30) = 10= 10 + 0.(30) = 10

xx22’ = x’ = x11 + u + u22. . x x (u(u22 = 9/10) = 9/10) = 10 + 0.9(100) = 100= 10 + 0.9(100) = 100 yy22’ = y’ = y22 + u + u22. . yy = 10 + 0.9(30) = 37= 10 + 0.9(30) = 37**Homework: Use different values of xwxwminmin,, xwxwmaxmax, , ywywminmin , ,

ywywmax max , P, P11 andand P P22 for exercise.for exercise.

Page 28: Line Clipping in 2D

April 22, 2023April 22, 2023 SCG 3023SCG 3023 2828

algorithmalgorithm• 1.1.  Initial value  Initial value : u1 = 0, u2 = 1 : u1 = 0, u2 = 1• 2.2.            For k = 1, 2, 3, 4; For k = 1, 2, 3, 4;

– 2.1 calculate P2.1 calculate Pk k dan q dan qkk

– 2.2 calculate r2.2 calculate rkk = q = qkk/ P/ Pkk

– 2.2 if (P2.2 if (Pk k <0) –find u1 ( if (r<0) –find u1 ( if (rkk>u1), u1=r>u1), u1=rk k ))– 2.3 if (P(Pkk > 0)– find u2 ( if (r > 0)– find u2 ( if (rkk<u2), u2=r<u2), u2=rkk ) )– 2.4 if2.4 if (P(Pkk = 0) and (q = 0) and (qkk< 0) ; reject the line; goto step 6< 0) ; reject the line; goto step 6  

• 3.3.    If     If (u1> u2) ; reject the line; goto step 6(u1> u2) ; reject the line; goto step 6• 5.5.        Find the clipped lineFind the clipped line•   • x1’ = x1 + u1. x1’ = x1 + u1. xx• y1’ = y1 + u1. y1’ = y1 + u1. yy•   • x2’ = x1 + u2. x2’ = x1 + u2. xx• y2’ = y1 + u2. y2’ = y1 + u2. yy•   • 6.6.            Repeat step 1 –5 for other lines.Repeat step 1 –5 for other lines.•