Upload
others
View
12
Download
0
Embed Size (px)
Citation preview
Introduction Point Clipping Line Clipping Polygon Clipping Assignment Recap
Computer GraphicsLecture 8
Dr. Marc Eduard Fr̂ıncuWest University of Timisoara
May 5, 2020
1 / 17
Introduction Point Clipping Line Clipping Polygon Clipping Assignment Recap
Outline
1 Introduction
2 Point Clipping
3 Line Clipping
4 Polygon Clipping
5 Assignment
6 Recap
2 / 17
Introduction Point Clipping Line Clipping Polygon Clipping Assignment Recap
Image Physics
Clipping
render only parts of the primitiveslocated inside the viewport
avoids cost of futuretransformations of the “invisible”primitive parts
Clipping methods
analytic, e.g., for lines & polygons
during the scan conversion, e.g.,for other primitives
copy pixels from one memoryregion to another
3 / 17
Introduction Point Clipping Line Clipping Polygon Clipping Assignment Recap
Image Physics
Clipping
render only parts of the primitiveslocated inside the viewport
avoids cost of futuretransformations of the “invisible”primitive parts
Clipping methods
analytic, e.g., for lines & polygons
during the scan conversion, e.g.,for other primitives
copy pixels from one memoryregion to another
3 / 17
Introduction Point Clipping Line Clipping Polygon Clipping Assignment Recap
Point Clipping
Point clipping
– assume window to have left bottom corner (l , b) and right top corner (r , t) ⇒P(x , y) is visible ⇔ x ∈ [l , r ] and y ∈ [b, t]
4 / 17
Introduction Point Clipping Line Clipping Polygon Clipping Assignment Recap
Line Clipping
Line clipping methods
Cohen-Sutherland (encoding based) – oldest & most common
Liang-Barsky (parametric) – more efficient
Nicholl-Lee-Nicholl (encoding based) – more efficient than the first one
5 / 17
Introduction Point Clipping Line Clipping Polygon Clipping Assignment Recap
Line Clipping
Cohen-Sutherland algorithm
– encode end points using 4 bit code:bit0 = 1 point is left of window
bit1 = 1 point is right of window
bit2 = 1 point is bellow of window
bit3 = 1 point is above of window
1 if C1 ∧ C2 6= 0⇒ reject2 if C1 ∨ C2 = 0⇒ accept3 otherwise subdivide segment & go
to 1):if C1 6= 0 ⇒ P1 not in viewport⇒let i = minj{bitj 6= 0} ⇒new P1 =
⋂segment with edge
i (left=0, right=1, bottom=2,top=3)and new C1 = 0 (is on the edgeof the viewport ⇔ is inside)otherwise if C2 6= 0 applyprevious logic on C2
– where C2 and C1 represent the bitcodes for the endpoints of the line seg-ment
6 / 17
Introduction Point Clipping Line Clipping Polygon Clipping Assignment Recap
Line Clipping
Cohen-Sutherland algorithm
– encode end points using 4 bit code:bit0 = 1 point is left of window
bit1 = 1 point is right of window
bit2 = 1 point is bellow of window
bit3 = 1 point is above of window
1 if C1 ∧ C2 6= 0⇒ reject2 if C1 ∨ C2 = 0⇒ accept3 otherwise subdivide segment & go
to 1):if C1 6= 0 ⇒ P1 not in viewport⇒let i = minj{bitj 6= 0} ⇒new P1 =
⋂segment with edge
i (left=0, right=1, bottom=2,top=3)and new C1 = 0 (is on the edgeof the viewport ⇔ is inside)otherwise if C2 6= 0 applyprevious logic on C2
– where C2 and C1 represent the bitcodes for the endpoints of the line seg-ment
6 / 17
Introduction Point Clipping Line Clipping Polygon Clipping Assignment Recap
Line Clipping
Cohen-Sutherland algorithm (cont.)
does unnecessary clipping
not efficient
clipping & testing are done in fixed order
efficient when most lines are either rejected or accepted
easy to program
parametric clipping is more efficient
7 / 17
Introduction Point Clipping Line Clipping Polygon Clipping Assignment Recap
Line Clipping
Line parametric form
a line segment with endpoints (x1, y1) and (x2, y2) ⇒ (parametric form){x = x1 + u∆x
y = y1 + u∆y– with u ∈ [0, 1], ∆x = x2 − x1 and ∆y = y2 − y1
Liang-Barsky algorithm
– line inside the clipping region:
{l ≤ x1 + u∆x ≤ r
b ≤ y1 + u∆y ≤ tor:
upk ≤ qk , k = 1, 2, 3, 4 and we further need uk = qkpk
with:
p1 = −∆x q1 = x1 − l
p2 = ∆x q2 = r − x1
p3 = −∆y q3 = y1 − b
p4 = ∆y q4 = t − y1
8 / 17
Introduction Point Clipping Line Clipping Polygon Clipping Assignment Recap
Line Clipping
Line parametric form
a line segment with endpoints (x1, y1) and (x2, y2) ⇒ (parametric form){x = x1 + u∆x
y = y1 + u∆y– with u ∈ [0, 1], ∆x = x2 − x1 and ∆y = y2 − y1
Liang-Barsky algorithm
– line inside the clipping region:
{l ≤ x1 + u∆x ≤ r
b ≤ y1 + u∆y ≤ tor:
upk ≤ qk , k = 1, 2, 3, 4 and we further need uk = qkpk
with:
p1 = −∆x q1 = x1 − l
p2 = ∆x q2 = r − x1
p3 = −∆y q3 = y1 − b
p4 = ∆y q4 = t − y1
8 / 17
Introduction Point Clipping Line Clipping Polygon Clipping Assignment Recap
Line Clipping
Liang-Barsky algorithm (cont.)
if pk < 0 as u increases ⇒ line enters viewport
if pk > 0 as u increases ⇒ line exits viewport
if pk = 0 ⇒ line parallel to an edge of the viewport
if qk < 0 line completely outside (discard it)
for pk 6= 0 uk gives the intersection point
1 compute the uk values
2 take umin = max{0, uk} and umax = min{uk , 1}3 if (umin > umax ) reject line as being outside viewport
4 otherwise draw line from:(x1 + ∆xumin, y1 + ∆yumin)to(x1 + ∆xumax , y1 + ∆yumax )
9 / 17
Introduction Point Clipping Line Clipping Polygon Clipping Assignment Recap
Line Clipping
Nicholl-Lee-Nicholl algorithm
least number of comparisons & divisionsdoes not extend well to 3D (as the previous two do)
Algorithm overview
P1 needs to be in three predetermined areas (could require translations orrotations to achieve this):
in the viewportin a “corner region”in an “edge region”
1 do a region testing like Cohen-Sutherland to see if a line segment can beaccepted/rejected:
to determine regions draw lines from P1 through all four corners of the viewport
2 determine the region with the property that no matter where in the region P2 is,the segment will have to be intersected with the same boundaries of the viewport(i.e., compare slopes of segment with the four lines through the corners)
3 determine intersection points (if existing) and draw segment
10 / 17
Introduction Point Clipping Line Clipping Polygon Clipping Assignment Recap
Line Clipping
Nicholl-Lee-Nicholl algorithm
least number of comparisons & divisionsdoes not extend well to 3D (as the previous two do)
Algorithm overview
P1 needs to be in three predetermined areas (could require translations orrotations to achieve this):
in the viewportin a “corner region”in an “edge region”
1 do a region testing like Cohen-Sutherland to see if a line segment can beaccepted/rejected:
to determine regions draw lines from P1 through all four corners of the viewport
2 determine the region with the property that no matter where in the region P2 is,the segment will have to be intersected with the same boundaries of the viewport(i.e., compare slopes of segment with the four lines through the corners)
3 determine intersection points (if existing) and draw segment
10 / 17
Introduction Point Clipping Line Clipping Polygon Clipping Assignment Recap
Line Clipping
Nicholl-Lee-Nicholl for the left edge region
LeftEdgeRegionCase(x1, y1, x2, y2, visible)algorithm:
Require: x1, y1 start point of the lineRequire: x2, y2 end point of the line1: {end point on the left side of the left edge of the viewport}2: if x2 < l then3: visible→ false4: else5: if y2 < b then6: LeftBottom(l, b, r, t, x1, y1, x2, y2, visible)7: else8: if y2 > t then9: {use symmetry to reduce to LeftBottom case}10: y1 = −y111: y2 = −y212: LeftBottom(l,−t, r,−b, x1, y1, x2, y2, visible)13: {reflect back}14: y1 = −y115: y2 = −y216: else17: ∆x = x2 − x118: ∆y = y2 − y119: if x2 > r then20: y2 = y1 + ∆y(r − x1)/∆x21: x2 = r22: end if23: y1 = y1 + ∆y(l − x1)/∆x24: x1 = l25: visible = true26: end if27: end if28: end if
LeftBottom(l , b, r , t, x1, y1, x2, y2, visible)algorithm:
Require: x1, y1 start point of the lineRequire: x2, y2 end point of the line1: ∆x = x2 − x12: ∆x = y2 − y13: a = (l − x1)∆y4: b = (b − y1)∆x5: {slope of P1P2 ¡ slope from P1 to (l, b)}6: if b > a then7: visible→ false8: else9: visible→ true10: if x2 < r then11: x2 = x1 + b/∆y12: y2 = b13: else14: c = (r − x1)/∆y15: if b > c then16: {P2 between line from P1 to (l, b) and
(r, b)}17: x2 = x1 + b/∆y18: y2 = b19: else20: y2 = y1 + c/∆x21: x2 = r22: end if23: end if24: end if
11 / 17
Introduction Point Clipping Line Clipping Polygon Clipping Assignment Recap
Polygon Clipping
Polygon clipping methods
Suterland-Hodgeman
Weiler-Atherton
12 / 17
Introduction Point Clipping Line Clipping Polygon Clipping Assignment Recap
Polygon Clipping
Sutherland-Hodgeman
– one of the earliest polygon clipping al-gorithms (1974)
1 clip the polygon against an edge ofthe viewport, i.e., the halfplanedetermined by the edge whichcontains the clip polygon
2 then clip the remaining polygonagainst the next viewport edge
3 repeat until all viewport edges havebeen considered
Four test cases for the polygon edges:
first vertex inside and second outsideviewport
both vertices inside
first vertex outside and second inside
both vertices outside
Concave polygons may be displayed prop-erly with extra lines
13 / 17
Introduction Point Clipping Line Clipping Polygon Clipping Assignment Recap
Polygon Clipping
Weiler-Atherton algorithm
Based on the Suterland-Hodgeman algorithm– for ClockWise (CW) polygons example:
outside-to-inside edge ⇒ follow the polygon boundaryinside-to-outside edge ⇒ follow the window boundary in a CW direction
Clips concave polygons correctly
Algorithm overview
1 the inside of the polygon is always to the right as it gets traversed in a CW2 find intersections of the viewport with the polygon to be clipped & insert vertices
at those points into the polygons3 separate the polygon countours that are outside the clip polygon from those
inside of it4 separate intersection vertices into two lists:
entering list: consists of vertices where the polygon edges enter the clip polygonleaving list: consists of vertices where the polygon edges exit the clip polygon
5 clip(a) remove an intersection vertex from the entering list. If none we are done(b) follow the polygon edges to the next intersection(c) jump to the clip polygon vertex list(d) follow the clip polygon vertices to the next intersection(e) jump back to the polygon vertex list(f) repeat (b)-(e) until we are back to the starting point
14 / 17
Introduction Point Clipping Line Clipping Polygon Clipping Assignment Recap
Polygon Clipping
Weiler-Atherton algorithm
Based on the Suterland-Hodgeman algorithm– for ClockWise (CW) polygons example:
outside-to-inside edge ⇒ follow the polygon boundaryinside-to-outside edge ⇒ follow the window boundary in a CW direction
Clips concave polygons correctly
Algorithm overview
1 the inside of the polygon is always to the right as it gets traversed in a CW2 find intersections of the viewport with the polygon to be clipped & insert vertices
at those points into the polygons3 separate the polygon countours that are outside the clip polygon from those
inside of it4 separate intersection vertices into two lists:
entering list: consists of vertices where the polygon edges enter the clip polygonleaving list: consists of vertices where the polygon edges exit the clip polygon
5 clip(a) remove an intersection vertex from the entering list. If none we are done(b) follow the polygon edges to the next intersection(c) jump to the clip polygon vertex list(d) follow the clip polygon vertices to the next intersection(e) jump back to the polygon vertex list(f) repeat (b)-(e) until we are back to the starting point
14 / 17
Introduction Point Clipping Line Clipping Polygon Clipping Assignment Recap
Polygon Clipping
15 / 17
Introduction Point Clipping Line Clipping Polygon Clipping Assignment Recap
Assignment
ASSIGNMENT 5:
Implement the following clipping algorithms and test them on lines and polygons (ifapplicable):
Cohen Sutherland line clipping algorithm
Liang-Barsky line clipping algorithm
Sutherland-Hodgeman polygon clipping algorithm
Hint: use Java2D for loading and manipulating pixels.
Deadline: Beginning of Lecture 10
16 / 17
Introduction Point Clipping Line Clipping Polygon Clipping Assignment Recap
Lecture Recap
clipping algorithms: reduces costs by rendering only the visible parts of theprimitives
point, line & polygon clipping:Cohen-Sutherland (lines)Liang-Barsky (lines)Nicholl-Lee-Nicholl (lines)Sutherland-Hodgeman (polygon)Weiler-Atherton (polygon)
17 / 17