Download presentation
Presentation is loading. Please wait.
Published byErica Ritter Modified over 6 years ago
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
Review: Drawing General Polygons
How to draw every interior pixels? ? Scanline conversion of polygons!
4
Review: Curved Boundaries
How to deal with curved boundaries? Boundary fill algorithm!
5
Review: How to deal with this?
Multiple color boundaries? Flood fill algorithm!
6
Why Clip? We do not want to waste time drawing objects that are outside of viewing window (or clipping window)
7
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)
8
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?
9
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?
10
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
11
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 No
12
Clipping Lines
13
Clipping Lines
14
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)
15
Clipping Lines
16
Outline Simple line clipping algorithm Cohen-Sutherland Liang-Barsky
17
Clipping Lines
18
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
Clipping Lines – Simple Algorithm
20
Clipping Lines – Simple Algorithm
21
Intersecting Two Lines
22
Intersecting Two Lines
23
Intersecting Two Lines
24
Intersecting Two Lines
25
Intersecting Two Lines
26
Intersecting Two Lines
Substitute t or s back into equation to find intersection
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
40
Clipping Lines – Simple Algorithm
41
Clipping Lines – Simple Algorithm
42
Clipping Lines – Simple Algorithm
Lots of intersection tests makes algorithm expensive Complicated tests to determine if intersecting rectangle Is there a better way?
43
Trivial Accepts Big Optimization: trivial accepts/rejects
How can we quickly decide whether line segment is entirely inside window Answer: test both endpoints
44
Trivial Accepts Big Optimization: trivial accepts/rejects
How can we quickly decide whether line segment is entirely inside window Answer: test both endpoints
45
Trivial Rejects How can we know a line is outside of the window
Answer:
46
Trivial Rejects How can we know a line is outside of the window
Answer: Wrong side
47
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
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
49
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
50
Cohen-Sutherland Algorithm
bit 4 bit 3 bit 2 bit 1 top bottom right left top right left Region code? bottom
51
Cohen-Sutherland Algorithm
bit 4 bit 3 bit 2 bit 1 top bottom right left top right left 0010 bottom ?
52
Cohen-Sutherland Algorithm
bit 4 bit 3 bit 2 bit 1 top bottom right left top right left 0010 bottom 0100
53
Cohen-Sutherland Algorithm
54
Cohen-Sutherland Algorithm
55
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
56
Cohen-Sutherland Algorithm
Bitwise and
57
Cohen-Sutherland Algorithm
Bitwise and Bitwise or
58
Cohen-Sutherland Algorithm
Bitwise and Bitwise or
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
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 Line is outside the window! reject
61
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
62
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
63
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
64
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
65
Example 1
66
Example 1
67
Example 1
68
Example 1
69
Example 2
70
Example 2
71
Example 2
72
Example 2
73
Example 2
74
Example 2
75
Example 2
76
Example 3
77
Example 3
78
Example 3
79
Example 3
80
Cohen Sutherland Java applet: click here
81
Cohen-Sutherland Algorithm
Extends easily to 3D line clipping 27 regions 6 bits
82
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
83
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
84
Liang-Barsky Algorithm
Current clipped line (u1=0,u2=1) (2,1) (-.5,-.5)
85
Liang-Barsky Algorithm
Current clipped line (u1=0.333,u2=0.6) (2,1) (-.5,-.5)
86
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.
87
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.
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) 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) // 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
90
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
91
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
92
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
93
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
94
Liang-Barsky Algorithm
Current clipped line (u1=0,u2=1) (2,1) (-.5,-.5)
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 line starts outside this boundary r1 = q1/p1=0.2
97
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
98
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
99
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
100
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)
101
Liang-Barsky Algorithm
Current clipped line (u1=0.2,u2=1) (2,1) (-.5,-.5)
102
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
103
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
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 p2 = Δx =2.5>0 (-.5,-.5) q2 = (xmax-x1)=1.5 r2 = q2/p2=0.6
105
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)
106
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
107
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
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 p3 = -Δy =-1.5<0 (-.5,-.5) q3 = (y1-ymin)=0.5 r3 = q3/p3=0.333
109
Liang-Barsky Algorithm
Current clipped line (u1=0.333,u2=0.6) (2,1) (-.5,-.5)
110
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
111
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
112
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
113
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
114
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
115
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
116
Curve Clipping
117
Curve Clipping
118
Curve Clipping Approximate a curve using a set of straight-line segments Apply line clipping for curve clipping
119
Next Lecture Polygon fill-area clipping
120
Next Lecture Polygon fill-area clipping
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.