Presentation is loading. Please wait.

Presentation is loading. Please wait.

CSCE 441 Computer Graphics: Clipping Lines Jinxiang Chai

Similar presentations


Presentation on theme: "CSCE 441 Computer Graphics: Clipping Lines Jinxiang Chai"— Presentation transcript:

1 CSCE 441 Computer Graphics: Clipping Lines Jinxiang Chai

2 OpenGL Geometric Primitives
All geometric primitives are specified by vertices GL_LINE_STRIP GL_LINE_LOOP GL_POLYGON GL_LINES GL_POINTS GL_TRIANGLE_STRIP GL_QUAD_STRIP GL_TRIANGLES GL_QUADS GL_TRIANGLE_FAN

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

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

5 Clipping Points Given a point (x, y) and clipping window (xmin, ymin), (xmax, ymax), determine if the point should be drawn (xmax,ymax) (x,y) xmin=<x<=xmax? (xmin,ymin) ymin=<y<=ymax?

6 Clipping Points Given a point (x, y) and clipping window (xmin, ymin), (xmax, ymax), determine if the point should be drawn (xmax,ymax) (x2,y2) (x1,y1) xmin=<x<=xmax? (xmin,ymin) ymin=<y<=ymax?

7 Clipping Points Given a point (x, y) and clipping window (xmin, ymin), (xmax, ymax), determine if the point should be drawn (xmax,ymax) (x2,y2) (x1,y1) xmin=<x1<=xmax Yes (xmin,ymin) ymin=<y1<=ymax Yes

8 Clipping Points Given a point (x, y) and clipping window (xmin, ymin), (xmax, ymax), determine if the point should be drawn (xmax,ymax) (x2,y2) (x1,y1) xmin=<x2<=xmax No (xmin,ymin) ymin=<y2<=ymax Yes

9 Clipping Lines

10 Clipping Lines

11 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. (xmax,ymax) (x1, y1) (x0, y0) (xmin,ymin)

12 Clipping Lines

13 Outline Simple line clipping algorithm Cohen-Sutherland Liang-Barsky
Required readings: HB 8-5, 8-6, 8-7 and 8-9

14 Clipping Lines

15 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

16 Clipping Lines – Simple Algorithm

17 Clipping Lines – Simple Algorithm

18 Intersecting Two Lines

19 Intersecting Two Lines

20 Intersecting Two Lines

21 Intersecting Two Lines

22 Intersecting Two Lines

23 Intersecting Two Lines
Substitute t or s back into equation to find intersection

24 Clipping Lines – Simple Algorithm

25 Clipping Lines – Simple Algorithm

26 Clipping Lines – Simple Algorithm

27 Clipping Lines – Simple Algorithm

28 Clipping Lines – Simple Algorithm

29 Clipping Lines – Simple Algorithm

30 Clipping Lines – Simple Algorithm

31 Clipping Lines – Simple Algorithm

32 Clipping Lines – Simple Algorithm

33 Clipping Lines – Simple Algorithm

34 Clipping Lines – Simple Algorithm

35 Clipping Lines – Simple Algorithm

36 Clipping Lines – Simple Algorithm

37 Clipping Lines – Simple Algorithm

38 Clipping Lines – Simple Algorithm

39 Clipping Lines – Simple Algorithm
Lots of intersection tests makes algorithm expensive Complicated tests to determine if intersecting rectangle Is there a better way?

40 Trivial Accepts Big Optimization: trivial accepts/rejects
How can we quickly decide whether line segment is entirely inside window Answer: test both endpoints

41 Trivial Accepts Big Optimization: trivial accepts/rejects
How can we quickly decide whether line segment is entirely inside window Answer: test both endpoints

42 Trivial Rejects How can we know a line is outside of the window
Answer:

43 Trivial Rejects How can we know a line is outside of the window
Answer: Wrong side

44 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

45 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

46 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 top bottom right left

47 Cohen-Sutherland Algorithm
bit 4 bit 3 bit 2 bit 1 top bottom right left top right left Region code? bottom

48 Cohen-Sutherland Algorithm
bit 4 bit 3 bit 2 bit 1 top bottom right left top right left 0010 bottom ?

49 Cohen-Sutherland Algorithm
bit 4 bit 3 bit 2 bit 1 top bottom right left top right left 0010 bottom 0100

50 Cohen-Sutherland Algorithm

51 Cohen-Sutherland Algorithm

52 Cohen-Sutherland Algorithm
Classify p0, p1 using region codes c0, c1 If , trivially reject because both points located the wrong side of one edge. If , trivially accept because both points are not in the wrong side of any edge. Otherwise reduce to trivial cases by splitting into two segments

53 Cohen-Sutherland Algorithm
Bitwise and

54 Cohen-Sutherland Algorithm
Bitwise and Bitwise or

55 Cohen-Sutherland Algorithm
Bitwise and Bitwise or

56 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

57 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 Line is outside the window! reject

58 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 Line is outside the window! reject

59 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 Line is inside the window! draw

60 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

61 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

62 Example 1

63 Example 1

64 Example 1

65 Example 1

66 Example 2

67 Example 2

68 Example 2

69 Example 2

70 Example 2

71 Example 2

72 Example 2

73 Example 3

74 Example 3

75 Example 3

76 Example 3

77 Cohen-Sutherland Algorithm
Extends easily to 3D line clipping 27 regions 6 bits

78 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

79 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

80 Liang-Barsky Algorithm
Current clipped line (u1=0,u2=1) (2,1) (-.5,-.5)

81 Liang-Barsky Algorithm
Current clipped line (u1=0.333,u2=0.6) (2,1) (-.5,-.5)

82 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.

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

85 Liang-Barsky Algorithm
Mathematically: xmin <= x1 + uΔx <= xmax ymin <= y1 + uΔy <= ymax Rearranged 1: u*(-Δx) <= (x1 – xmin) // left edge 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 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) // right edge 3: u*(-Δy) <= (y1 – ymin) 4: u*(Δy) <= (ymax – y1) gen: u*(pk) <= (qk), k=1,2,3,4

87 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) // bottom edge 4: u*(Δy) <= (ymax – y1) gen: u*(pk) <= (qk), k=1,2,3,4

88 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) // top edge gen: u*(pk) <= (qk), k=1,2,3,4

89 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

90 Liang-Barsky Algorithm
Rules: pk = 0: the line is parallel to boundaries If for that same k, qk < 0, it’s outside Otherwise it’s inside 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 u2 = min(1, rk, u2) /** update end point**/ If u1 > u2, the line is completely outside

91 Liang-Barsky Algorithm
Current clipped line (u1=0,u2=1) (2,1) (-.5,-.5)

92 Liang-Barsky Algorithm
Current clipped line (u1=0,u2=1) (2,1) Δx = (x2-x1)=2.5 Δy = (y2-y1)=1.5 p1 = -Δx =-2.5<0 (-.5,-.5) q1 = (x1-xmin)=-.5 r1 = q1/p1=0.2

93 Liang-Barsky Algorithm
Current clipped line (u1=0,u2=1) (2,1) Δx = (x2-x1)=2.5 Δy = (y2-y1)=1.5 p1 = -Δx =-2.5<0 (-.5,-.5) q1 = (x1-xmin)=-.5 line starts outside this boundary r1 = q1/p1=0.2

94 Liang-Barsky Algorithm
Current clipped line (u1=0,u2=1) (2,1) Δx = (x2-x1)=2.5 Δy = (y2-y1)=1.5 p1 = -Δx =-2.5<0 (-.5,-.5) q1 = (x1-xmin)=-.5 r1 = q1/p1=0.2

95 Liang-Barsky Algorithm
Current clipped line (u1=0,u2=1) (2,1) Δx = (x2-x1)=2.5 Δy = (y2-y1)=1.5 p1 = -Δx =-2.5<0 (-.5,-.5) q1 = (x1-xmin)=-.5 r1 = q1/p1=0.2

96 Liang-Barsky Algorithm
Current clipped line (u1=0,u2=1) (2,1) Δx = (x2-x1)=2.5 Δy = (y2-y1)=1.5 p1 = -Δx =-2.5<0 (-.5,-.5) q1 = (x1-xmin)=-.5 u1 = max(0, r1, u1) r1 = q1/p1=0.2

97 Liang-Barsky Algorithm
Current clipped line (u1=0.2,u2=1) (2,1) u1<u2? yes: continue no: the line is completely outside window (-.5,-.5)

98 Liang-Barsky Algorithm
Current clipped line (u1=0.2,u2=1) (2,1) (-.5,-.5)

99 Liang-Barsky Algorithm
Current clipped line (u1=0.2,u2=1) (2,1) Δx = (x2-x1)=2.5 Δy = (y2-y1)=1.5 p2 = Δx =2.5>0 (-.5,-.5) q2 = (xmax-x1)=1.5 r2 = q2/p2=0.6

100 Liang-Barsky Algorithm
Current clipped line (u1=0.2,u2=1) (2,1) Δx = (x2-x1)=2.5 Δy = (y2-y1)=1.5 p2 = Δx =2.5>0 (-.5,-.5) q2 = (xmax-x1)=1.5 u2 = min(1, r2, u2) r2 = q2/p2=0.6

101 Liang-Barsky Algorithm
Current clipped line (u1=0.2,u2=0.6) (2,1) Δx = (x2-x1)=2.5 Δy = (y2-y1)=1.5 p2 = Δx =2.5>0 (-.5,-.5) q2 = (xmax-x1)=1.5 r2 = q2/p2=0.6

102 Liang-Barsky Algorithm
Current clipped line (u1=0.2,u2=0.6) (2,1) u1<u2? yes: continue no: the line is completely outside window (-.5,-.5)

103 Liang-Barsky Algorithm
Current clipped line (u1=0.2,u2=0.6) (2,1) Δx = (x2-x1)=2.5 Δy = (y2-y1)=1.5 p3 = -Δy =-1.5<0 (-.5,-.5) q3 = (y1-ymin)=0.5 r3 = q3/p3=0.333

104 Liang-Barsky Algorithm
Current clipped line (u1=0.2,u2=0.6) (2,1) Δx = (x2-x1)=2.5 Δy = (y2-y1)=1.5 p3 = -Δy =-1.5<0 (-.5,-.5) q3 = (y1-ymin)=0.5 u1 = max(0, r3, u1) r3 = q3/p3=0.333

105 Liang-Barsky Algorithm
Current clipped line (u1=0.333,u2=0.6) (2,1) Δx = (x2-x1)=2.5 Δy = (y2-y1)=1.5 p3 = -Δy =-1.5<0 (-.5,-.5) q3 = (y1-ymin)=0.5 r3 = q3/p3=0.333

106 Liang-Barsky Algorithm
Current clipped line (u1=0.333,u2=0.6) (2,1) (-.5,-.5)

107 Liang-Barsky Algorithm
Current clipped line (u1=0.333,u2=0.6) (2,1) Δx = (x2-x1)=2.5 Δy = (y2-y1)=1.5 p3 = -Δy =-1.5<0 (-.5,-.5) q3 = (y1-ymin)=0.5 r3 = q3/p3=0.333

108 Liang-Barsky Algorithm
Current clipped line (u1=0.333,u2=0.6) (2,1) Δx = (x2-x1)=2.5 Δy = (y2-y1)=1.5 p4 = Δy =1.5>0 (-.5,-.5) q4 = (ymax-y1)=1.5 r4 = q4/p4=1

109 Liang-Barsky Algorithm
Current clipped line (u1=0.333,u2=0.6) (2,1) Δx = (x2-x1)=2.5 Δy = (y2-y1)=1.5 p4 = Δy =1.5>0 (-.5,-.5) q4 = (ymax-y1)=1.5 u2 = min(1, r4, u2) r4 = q4/p4=1

110 Liang-Barsky Algorithm
Check the left edge (u1=0.333,u2=0.6) (2,1) u1<u2 (-.5,-.5) xnew1=x1+Δx*u1 xnew2=x2+Δx*u2 ynew1=y1+Δy*u1 ynew2=y2+Δy*u2

111 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

112 Comparison Cohen-Sutherland Liang-Barsky
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

113 Curve Clipping

114 Curve Clipping

115 Curve Clipping Approximate a curve using a set of straight-line segments Apply line clipping for curve clipping

116 Next Lecture Polygon fill-area clipping

117 Next Lecture Polygon fill-area clipping


Download ppt "CSCE 441 Computer Graphics: Clipping Lines Jinxiang Chai"

Similar presentations


Ads by Google