1 CSCE 441 Computer Graphics: Clipping Lines Jinxiang Chai.

113
1 CSCE 441 Computer Graphics: Clipping Lines Jinxiang Chai
  • date post

    20-Dec-2015
  • Category

    Documents

  • view

    219
  • download

    0

Transcript of 1 CSCE 441 Computer Graphics: Clipping Lines Jinxiang Chai.

1

CSCE 441 Computer Graphics:Clipping Lines

Jinxiang Chai

2/135

OpenGL Geometric Primitives

All geometric primitives are specified by vertices

GL_QUAD_STRIPGL_QUAD_STRIP

GL_POLYGONGL_POLYGON

GL_TRIANGLE_STRIPGL_TRIANGLE_STRIP

GL_TRIANGLE_FANGL_TRIANGLE_FAN

GL_POINTSGL_POINTSGL_LINESGL_LINES

GL_LINE_LOOPGL_LINE_LOOPGL_LINE_STRIPGL_LINE_STRIP

GL_TRIANGLESGL_TRIANGLES

GL_QUADSGL_QUADS

3/135

Review: Drawing General Polygons

How to draw every interior pixels?

?

Scanline conversion of polygons!

4/135

Review: Curved Boundaries

How to deal with curved boundaries?

Boundary fill algorithm!

5/135

Review: How to deal with this?

Multiple color boundaries?

Flood fill algorithm!

6/135

Why Clip?

We do not want to waste time drawing objects that are outside of viewing window (or clipping window)

7/135

Clipping Points

Given a point (x, y) and clipping window (xmin, ymin), (xmax, ymax), determine if the point should be drawn

(xmin,ymin)

(xmax,ymax)

(x,y)

8/135

Clipping Points

Given a point (x, y) and clipping window (xmin, ymin), (xmax, ymax), determine if the point should be drawn

(xmin,ymin)

(xmax,ymax)

(x,y)

xmin=<x<=xmax?ymin=<y<=ymax?

9/135

Clipping Points

Given a point (x, y) and clipping window (xmin, ymin), (xmax, ymax), determine if the point should be drawn

(x1,y1)

(x2,y2)

(xmin,ymin)

(xmax,ymax)

xmin=<x<=xmax?ymin=<y<=ymax?

10/135

Clipping Points

Given a point (x, y) and clipping window (xmin, ymin), (xmax, ymax), determine if the point should be drawn

(x1,y1)

(x2,y2)

(xmin,ymin)

(xmax,ymax)

xmin=<x1<=xmax Yes

ymin=<y1<=ymax Yes

11/135

Clipping Points

Given a point (x, y) and clipping window (xmin, ymin), (xmax, ymax), determine if the point should be drawn

(x1,y1)

(x2,y2)

(xmin,ymin)

(xmax,ymax)

xmin=<x2<=xmax Noymin=<y2<=ymax No

12/135

Clipping Lines

1P

2P

3P4P

5P

6P

7P

8P 9P

10P

13/135

Clipping Lines

5P

6̂P

7P

8P

9̂P

10P̂

14/135

Clipping Lines

Given a line with end-points (x0, y0), (x1, y1)

and clipping window (xmin, ymin), (xmax, ymax),

determine if line should be drawn and clipped end-points of line to draw.

(x0, y0)

(x1, y1)

(xmin,ymin)

(xmax,ymax)

15/135

Clipping Lines

1P

2P

3P4P

5P

6P

7P

8P 9P

10P

16/135

Outline

Simple line clipping algorithm Cohen-Sutherland Liang-Barsky

17/135

Clipping Lines

1P

2P

3P4P

5P

6P

7P

8P 9P

10P

18/135

Clipping Lines – Simple Algorithm

If both end-points inside rectangle, draw line If one end-point outside,

intersect line with all edges of rectangle

clip that point and repeat test

19/135

Clipping Lines – Simple Algorithm

),( 00 yx

),( minmin yx

),( maxmax yx

),( 11 yx

20/135

Clipping Lines – Simple Algorithm

),( 00 yx

),( minmin yx

),( maxmax yx

),( 11 yx

21/135

Intersecting Two Lines

),( 00 yx

),( 22 yx

),( 11 yx

),( 33 yx

22/135

Intersecting Two Lines

),( 00 yx

),( 22 yx

),( 11 yxtxxxtx )()( 010 ),( 33 yx

tyyyty )()( 010

10 t

23/135

Intersecting Two Lines

),( 00 yx

),( 22 yx

),( 11 yxtxxxtx )()( 010 ),( 33 yx

tyyyty )()( 010

0)0( xx 0)0( yy

1)1( xx 1)1( yy

24/135

Intersecting Two Lines

),( 00 yx

),( 22 yx

),( 11 yx

sxxxtxxx )()( 232010

),( 33 yx

syyytyyy )()( 232010

25/135

Intersecting Two Lines

),( 00 yx

),( 22 yx

),( 11 yx

02

02

3201

3201

yy

xx

s

t

yyyy

xxxx

),( 33 yx

26/135

Intersecting Two Lines

),( 00 yx

),( 22 yx

),( 11 yx

02

02

3201

3201

yy

xx

s

t

yyyy

xxxx

),( 33 yx

Substitute t or s back into equation to find intersection

27/135

Clipping Lines – Simple Algorithm

),( 00 yx

),( minmin yx

),( maxmax yx

),( 11 yx

28/135

Clipping Lines – Simple Algorithm

),( 00 yx

),( minmin yx

),( maxmax yx

),( 11 yx

29/135

Clipping Lines – Simple Algorithm

),( 00 yx

),( minmin yx

),( maxmax yx

),( 11 yx

30/135

Clipping Lines – Simple Algorithm

),( 00 yx

),( minmin yx

),( maxmax yx

),( 11 yx

31/135

Clipping Lines – Simple Algorithm

)ˆ,ˆ( 00 yx

),( minmin yx

),( maxmax yx

),( 11 yx

32/135

Clipping Lines – Simple Algorithm

)ˆ,ˆ( 00 yx

),( minmin yx

),( maxmax yx

),( 11 yx

33/135

Clipping Lines – Simple Algorithm

)ˆ,ˆ( 00 yx

),( minmin yx

),( maxmax yx

),( 11 yx

34/135

Clipping Lines – Simple Algorithm

)ˆ,ˆ( 00 yx

),( minmin yx

),( maxmax yx

),( 11 yx

35/135

Clipping Lines – Simple Algorithm

),( 00 yx

),( minmin yx

),( maxmax yx

),( 11 yx

36/135

Clipping Lines – Simple Algorithm

),( 00 yx

),( minmin yx

),( maxmax yx

),( 11 yx

37/135

Clipping Lines – Simple Algorithm

)ˆ,ˆ( 00 yx

),( minmin yx

),( maxmax yx

),( 11 yx

38/135

Clipping Lines – Simple Algorithm

)ˆ,ˆ( 00 yx

),( minmin yx

),( maxmax yx

),( 11 yx

39/135

Clipping Lines – Simple Algorithm

)ˆ,ˆ( 00 yx

),( minmin yx

),( maxmax yx

),( 11 yx

40/135

Clipping Lines – Simple Algorithm

)ˆ,ˆ( 00 yx

),( minmin yx

),( maxmax yx

)ˆ,ˆ( 11 yx

41/135

Clipping Lines – Simple Algorithm

)ˆ,ˆ( 00 yx

),( minmin yx

),( maxmax yx

)ˆ,ˆ( 11 yx

42/135

Clipping Lines – Simple Algorithm

Lots of intersection tests makes algorithm expensive

Complicated tests to determine if intersecting rectangle

Is there a better way?

43/135

Trivial Accepts

Big Optimization: trivial accepts/rejects How can we quickly decide whether line

segment is entirely inside window Answer: test both endpoints

44/135

Trivial Accepts

Big Optimization: trivial accepts/rejects How can we quickly decide whether line

segment is entirely inside window Answer: test both endpoints

45/135

Trivial Rejects

How can we know a line is outside of the window

Answer:

46/135

Trivial Rejects

How can we know a line is outside of the window

Answer:

Wrong side

47/135

Trivial Rejects

How can we know a line is outside of the window

Answer: both endpoints on wrong side of same edge, can trivially reject the line

48/135

Cohen-Sutherland Algorithm

Classify p0, p1 using region codes c0, c1

If , trivially reject If , trivially accept Otherwise reduce to trivial cases by splitting

into two segments

010 cc

010 cc

49/135

Cohen-Sutherland Algorithm

Every end point is assigned to a four-digit binary value, i.e. Region code

Each bit position indicates whether the point is inside or outside of a specific window edges

bit 4 bit 3 bit 2 bit 1

leftrightbottomtop

50/135

Cohen-Sutherland Algorithm

bit 4 bit 3 bit 2 bit 1

leftrightbottomtop

top

rightleft

bottom

Region code?

51/135

Cohen-Sutherland Algorithm

bit 4 bit 3 bit 2 bit 1

leftrightbottomtop

top

rightleft

bottom

0010

?

52/135

Cohen-Sutherland Algorithm

bit 4 bit 3 bit 2 bit 1

leftrightbottomtop

top

rightleft

bottom

0010

0100

53/135

Cohen-Sutherland Algorithm

00000001

0101

1001 1000 1010

0010

01100100

54/135

Cohen-Sutherland Algorithm

00000001

0101

1001 1000 1010

0010

01100100

),( 00 yx

),( 11 yx

55/135

Cohen-Sutherland Algorithm

Classify p0, p1 using region codes c0, c1

If , trivially reject If , trivially accept Otherwise reduce to trivial cases by splitting

into two segments

010 cc

010 cc

56/135

Cohen-Sutherland Algorithm

Classify p0, p1 using region codes c0, c1

If , trivially reject If , trivially accept Otherwise reduce to trivial cases by splitting

into two segments

010 cc

010 cc

57/135

Cohen-Sutherland Algorithm

Classify p0, p1 using region codes c0, c1

If , trivially reject If , trivially accept Otherwise reduce to trivial cases by splitting

into two segments

010 cc

010 cc

00000001

0101

1001 1000 1010

0010

01100100

Line is outside the window! reject

58/135

Cohen-Sutherland Algorithm

Classify p0, p1 using region codes c0, c1

If , trivially reject If , trivially accept Otherwise reduce to trivial cases by splitting

into two segments

010 cc

010 cc

00000001

0101

1001 1000 1010

0010

01100100

Line is outside the window! reject

59/135

Cohen-Sutherland Algorithm

Classify p0, p1 using region codes c0, c1

If , trivially reject If , trivially accept Otherwise reduce to trivial cases by splitting

into two segments

010 cc

010 cc

00000001

0101

1001 1000 1010

0010

01100100

Line is inside the window! draw

60/135

Cohen-Sutherland Algorithm

Classify p0, p1 using region codes c0, c1

If , trivially reject If , trivially accept Otherwise reduce to trivial cases by splitting

into two segments

010 cc

010 cc

00000001

0101

1001 1000 1010

0010

01100100

61/135

Window Intersection

(x1, y1), (x2, y2) intersect with vertical edge at xright

yintersect = y1 + m(xright – x1)

• where m=(y2-y1)/(x2-x1)

(x1, y1), (x2, y2) intersect with horizontal edge at ybottom

xintersect = x1 + (ybottom – y1)/m

• where m=(y2-y1)/(x2-x1)

62/135

Example 1

00000001

0101

1001 1000 1010

0010

01100100

63/135

Example 1

00000001

0101

1001 1000 1010

0010

01100100

64/135

Example 1

00000001

0101

1001 1000 1010

0010

01100100

65/135

Example 1

00000001

0101

1001 1000 1010

0010

01100100

66/135

Example 2

00000001

0101

1001 1000 1010

0010

01100100

67/135

Example 2

00000001

0101

1001 1000 1010

0010

01100100

68/135

Example 2

00000001

0101

1001 1000 1010

0010

01100100

69/135

Example 2

00000001

0101

1001 1000 1010

0010

01100100

70/135

Example 2

00000001

0101

1001 1000 1010

0010

01100100

71/135

Example 2

00000001

0101

1001 1000 1010

0010

01100100

72/135

Example 2

00000001

0101

1001 1000 1010

0010

01100100

73/135

Example 3

00000001

0101

1001 1000 1010

0010

01100100

74/135

Example 3

00000001

0101

1001 1000 1010

0010

01100100

75/135

Example 3

00000001

0101

1001 1000 1010

0010

01100100

76/135

Example 3

00000001

0101

1001 1000 1010

0010

01100100

77/135

Cohen Sutherland

Java applet: click here

78/135

Cohen-Sutherland Algorithm

Extends easily to 3D line clipping27 regions6 bits

79/135

Cohen-Sutherland Algorithm

Use region codes to quickly eliminate/include lines

Best algorithm when trivial accepts/rejects are common

Must compute viewing window clipping of remaining lines

Non-trivial clipping cost More efficient algorithms exist

80/135

Liang-Barsky Algorithm

Parametric definition of a line: x = x1 + uΔx

y = y1 + uΔy

Δx = (x2-x1), Δy = (y2-y1), 0<=u<=1

Lines are oriented: classify lines as moving inside to out or outside to in

Goal: find range of u for which x and y both inside the viewing window

81/135

Liang-Barsky Algorithm

Current clipped line (u1=0,u2=1)

(-.5,-.5)

(2,1)

82/135

Liang-Barsky Algorithm

Current clipped line (u1=0.333,u2=0.6)

(-.5,-.5)

(2,1)

83/135

Liang-Barsky Algorithm

For lines starting outside of boundary, update its starting point (u1)

For lines starting inside of boundary, update end point (u2)

For lines paralleling the boundaries and outside window,

reject it.

84/135

Liang-Barsky Algorithm

For lines starting outside of boundary, update its starting point (u1)

For lines starting inside of boundary, update end point (u2)

For lines paralleling the boundaries and outside window,

reject it.

85/135

Liang-Barsky Algorithm

Mathematically: xmin <= x1 + uΔx <= xmax

ymin <= y1 + uΔy <= ymax

Rearranged 1: u*(-Δx) <= (x1 – xmin) 2: u*(Δx) <= (xmax – x1) 3: u*(-Δy) <= (y1 – ymin) 4: u*(Δy) <= (ymax – y1) gen: u*(pk) <= (qk), k=1,2,3,4

86/135

Liang-Barsky Algorithm

Rules:

1) pk = 0: the line is parallel to boundaries If for that same k, qk < 0, it’s outside Otherwise it’s inside

2) pk < 0: the line starts outside this boundary rk = qk/pk

u1 = max(0, rk, u1) /**update starting point**/ pk > 0: the line starts inside the boundary

rk = qk/pk

u2 = min(1, rk, u2) /** update end point**/ If u1 > u2, the line is completely outside

87/135

Liang-Barsky Algorithm

Current clipped line (u1=0,u2=1)

(-.5,-.5)

(2,1)

88/135

Liang-Barsky Algorithm

Current clipped line (u1=0,u2=1)

(-.5,-.5)

(2,1)

Δx = (x2-x1)=2.5

Δy = (y2-y1)=1.5

p1 = -Δx =-2.5<0

q1 = (x1-xmin)=-.5

r1 = q1/p1=0.2

89/135

Liang-Barsky Algorithm

Current clipped line (u1=0,u2=1)

(-.5,-.5)

(2,1)

Δx = (x2-x1)=2.5

Δy = (y2-y1)=1.5

p1 = -Δx =-2.5<0

q1 = (x1-xmin)=-.5

r1 = q1/p1=0.2line starts outside this boundary

90/135

Liang-Barsky Algorithm

Current clipped line (u1=0,u2=1)

(-.5,-.5)

(2,1)

Δx = (x2-x1)=2.5

Δy = (y2-y1)=1.5

p1 = -Δx =-2.5<0

q1 = (x1-xmin)=-.5

r1 = q1/p1=0.2

91/135

Liang-Barsky Algorithm

Current clipped line (u1=0,u2=1)

(-.5,-.5)

(2,1)

Δx = (x2-x1)=2.5

Δy = (y2-y1)=1.5

p1 = -Δx =-2.5<0

q1 = (x1-xmin)=-.5

r1 = q1/p1=0.2

92/135

Liang-Barsky Algorithm

Current clipped line (u1=0,u2=1)

(-.5,-.5)

(2,1)

Δx = (x2-x1)=2.5

Δy = (y2-y1)=1.5

p1 = -Δx =-2.5<0

q1 = (x1-xmin)=-.5

r1 = q1/p1=0.2u1 = max(0, r1, u1)

93/135

Liang-Barsky Algorithm

Current clipped line (u1=0.2,u2=1)

(-.5,-.5)

(2,1)

u1<u2?

- yes: continue

- no: the line is completely outside window

94/135

Liang-Barsky Algorithm

Current clipped line (u1=0.2,u2=1)

(-.5,-.5)

(2,1)

95/135

Liang-Barsky Algorithm

Current clipped line (u1=0.2,u2=1)

(-.5,-.5)

(2,1)

Δx = (x2-x1)=2.5

Δy = (y2-y1)=1.5

p2 = Δx =2.5>0

q2 = (xmax-x1)=1.5

r2 = q2/p2=0.6

96/135

Liang-Barsky Algorithm

Current clipped line (u1=0.2,u2=1)

(-.5,-.5)

(2,1)

Δx = (x2-x1)=2.5

Δy = (y2-y1)=1.5

p2 = Δx =2.5>0

q2 = (xmax-x1)=1.5

r2 = q2/p2=0.6u2 = min(1, r2, u2)

97/135

Liang-Barsky Algorithm

Current clipped line (u1=0.2,u2=0.6)

(-.5,-.5)

(2,1)

Δx = (x2-x1)=2.5

Δy = (y2-y1)=1.5

p2 = Δx =2.5>0

q2 = (xmax-x1)=1.5

r2 = q2/p2=0.6

98/135

Liang-Barsky Algorithm

Current clipped line (u1=0.2,u2=0.6)

(-.5,-.5)

(2,1)

u1<u2?

- yes: continue

- no: the line is completely outside window

99/135

Liang-Barsky Algorithm

Current clipped line (u1=0.2,u2=0.6)

(-.5,-.5)

(2,1)

Δx = (x2-x1)=2.5

Δy = (y2-y1)=1.5

p3 = -Δy =-1.5<0

q3 = (y1-ymin)=0.5

r3 = q3/p3=0.333

100/135

Liang-Barsky Algorithm

Current clipped line (u1=0.2,u2=0.6)

(-.5,-.5)

(2,1)

Δx = (x2-x1)=2.5

Δy = (y2-y1)=1.5

p3 = -Δy =-1.5<0

q3 = (y1-ymin)=0.5

r3 = q3/p3=0.333u1 = max(0, r3, u1)

101/135

Liang-Barsky Algorithm

Current clipped line (u1=0.333,u2=0.6)

(-.5,-.5)

(2,1)

Δx = (x2-x1)=2.5

Δy = (y2-y1)=1.5

p3 = -Δy =-1.5<0

q3 = (y1-ymin)=0.5

r3 = q3/p3=0.333

102/135

Liang-Barsky Algorithm

Current clipped line (u1=0.333,u2=0.6)

(-.5,-.5)

(2,1)

103/135

Liang-Barsky Algorithm

Current clipped line (u1=0.333,u2=0.6)

(-.5,-.5)

(2,1)

Δx = (x2-x1)=2.5

Δy = (y2-y1)=1.5

p3 = -Δy =-1.5<0

q3 = (y1-ymin)=0.5

r3 = q3/p3=0.333

104/135

Liang-Barsky Algorithm

Current clipped line (u1=0.333,u2=0.6)

(-.5,-.5)

(2,1)

Δx = (x2-x1)=2.5

Δy = (y2-y1)=1.5

p4 = Δy =1.5>0

q4 = (ymax-y1)=1.5

r4 = q4/p4=1

105/135

Liang-Barsky Algorithm

Current clipped line (u1=0.333,u2=0.6)

(-.5,-.5)

(2,1)

Δx = (x2-x1)=2.5

Δy = (y2-y1)=1.5

p4 = Δy =1.5>0

q4 = (ymax-y1)=1.5

r4 = q4/p4=1u2 = min(1, r4, u2)

106/135

Liang-Barsky Algorithm

Check the left edge (u1=0.333,u2=0.6)

(-.5,-.5)

(2,1)

u1<u2

xnew1=x1+Δx*u1

ynew1=y1+Δy*u1

xnew2=x2+Δx*u2

ynew2=y2+Δy*u2

107/135

Liang-Barsky Algorithm

Faster than Cohen-Sutherland

Extension to 3D is easy

- Parametric representation for 3D lines

- Compute u1,u2 based on the intersection between line and plane

108/135

Comparison

Cohen-Sutherland Repeated clipping is expensive Best used when trivial acceptance and rejection is

possible for most lines Liang-Barsky

Computation of t-intersections is cheap (only one division)

Computation of (x,y) clip points is only done once

Algorithm doesn’t consider trivial accepts/rejects Best when many lines must be clipped

109/135

Curve Clipping

110/135

Curve Clipping

111/135

Curve Clipping

Approximate a curve using a set of straight-line segments

Apply line clipping for curve clipping

112/135

Next Lecture

Polygon fill-area clipping

113/135

Next Lecture

Polygon fill-area clipping