computergraphics

36
7/21/2019 ComputerGraphics http://slidepdf.com/reader/full/computergraphics-56db938d1bf50 1/36 Computer Graphics Spring 2007, #6 Clipping

Upload: rajyalakshmi-jammalamadaka

Post on 06-Mar-2016

216 views

Category:

Documents


0 download

DESCRIPTION

material

TRANSCRIPT

Page 1: ComputerGraphics

7/21/2019 ComputerGraphics

http://slidepdf.com/reader/full/computergraphics-56db938d1bf50 1/36

Computer Graphics

Spring 2007, #6

Clipping

Page 2: ComputerGraphics

7/21/2019 ComputerGraphics

http://slidepdf.com/reader/full/computergraphics-56db938d1bf50 2/36

Contents• Clipping

 – Point clipping

 – Line clipping

• Cohen-Sutherland

• Liang-Barsky

• Nicholl-Lee-Nicholl

 – Polygon clipping – Curve clipping

Page 3: ComputerGraphics

7/21/2019 ComputerGraphics

http://slidepdf.com/reader/full/computergraphics-56db938d1bf50 3/36

Clipping• Computer graphics benefits from being

able to reduce the amount of work neededto draw objects.

• In principle we are interested in knowing

which objects are (partly) outside andinside a specified region of space =clipping

• Some objects will be inside the specifiedregion but not visible and thereforeskipped = (back-face) culling

Page 4: ComputerGraphics

7/21/2019 ComputerGraphics

http://slidepdf.com/reader/full/computergraphics-56db938d1bf50 4/36

Clipping• Clipping often connected with other

procedures, e.g. scanline filling:

xmin

ymin

ymax

xmax xmaxxmin

ymin

ymax

Page 5: ComputerGraphics

7/21/2019 ComputerGraphics

http://slidepdf.com/reader/full/computergraphics-56db938d1bf50 5/36

Point clipping• Check to see if the point (x,y) is inside or

outside the clipping rectangle:

xmin ≤ x ≤ xmax

ymin ≤ y ≤ ymax

If all four inqualities are satisfied then

accept the point, otherwise reject it.

Page 6: ComputerGraphics

7/21/2019 ComputerGraphics

http://slidepdf.com/reader/full/computergraphics-56db938d1bf50 6/36

Line Clipping• Typical cases involve

Page 7: ComputerGraphics

7/21/2019 ComputerGraphics

http://slidepdf.com/reader/full/computergraphics-56db938d1bf50 7/36

Line Clipping: Parametric Lines• From the parametric representation of a line

from (x1,y1) to (x2,y2)x = x1 + t∗(x2-x1)

y = y1 + t∗(y2-y1) t ∈ [0,1]

we check for intersection points with the fourboundaries x = xmin, x = xmax, y = ymin, y = ymax

(lines parallel to the edges treated separately)

• Computationally easier methods exist

Page 8: ComputerGraphics

7/21/2019 ComputerGraphics

http://slidepdf.com/reader/full/computergraphics-56db938d1bf50 8/36

Line Clipping: Cohen-Sutherland

• Basic idea: every point (x,y) is coded using

four bits to denote the position of the pointw.r.t the window: [above,below,right,left].For example: 1001 above and to the left

1001 1000

0001

0000

1010

0010

0101 0100 0110

window

Page 9: ComputerGraphics

7/21/2019 ComputerGraphics

http://slidepdf.com/reader/full/computergraphics-56db938d1bf50 9/36

Line Clipping: Cohen-Sutherland• We generate this 4-bit classification using

#define LEFT_EDGE 0x1

#define RIGHT_EDGE 0x2

#define BOTTOM_EDGE 0x4

#define TOP_EDGE 0x8

• For each point (x,y) we calculateunsigned char code = 0x0

if ( x < xmin ) code = code | LEFT_EDGE

if ( x > xmax ) code = code | RIGHT_EDGE

if ( y < ymin ) code = code | BOTTOM_EDGEif ( y > ymax ) code = code | TOP_EDGE

Page 10: ComputerGraphics

7/21/2019 ComputerGraphics

http://slidepdf.com/reader/full/computergraphics-56db938d1bf50 10/36

Line Clipping: Cohen-Sutherland

• Strategy:

1. Accept the line if both endpoints P1, P2 areinside: CODE(P1) == 0 && CODE(P2) == 0

2. If both endpoints are to the left or above or ..

then reject the line:CODE(P1) & CODE(P2) ≠ 0

3. Compare an outside endpoint to a clipping

boundary in order to see how much of theline can be discarded. Sometimes the wholeline is discarded. Proceed in boundary order

left, right, bottom, top

Page 11: ComputerGraphics

7/21/2019 ComputerGraphics

http://slidepdf.com/reader/full/computergraphics-56db938d1bf50 11/36

Line Clipping: Cohen-Sutherland

• Given xclip and m = (y2-y1)/(x2-x1) calculate

y = y1 + m(xclip-x1)• Given yclip calculate

x = x1 + (yclip-y1)/mP2

P’2P’’2

P’1

P1

P’3

P3

P4

Page 12: ComputerGraphics

7/21/2019 ComputerGraphics

http://slidepdf.com/reader/full/computergraphics-56db938d1bf50 12/36

Line Clipping: Cohen-Sutherland

• Final result:

 – We have discarded lines totally outside thewindow

 – We have chosen the subsection of the linewhich is inside the window

 – Algorithm can be modified for 3D

 – May require calculation of several intersectionpoints with clipping boundaries before

discarded or final values are known

Page 13: ComputerGraphics

7/21/2019 ComputerGraphics

http://slidepdf.com/reader/full/computergraphics-56db938d1bf50 13/36

Line Clipping: Liang-Barsky• Basic idea: Using the parametric represen-

tation of the line we solve for theparameter to see if and where the lineintersects the window boundaries.

• Parametric line from (x1,y1) → (x2,y2):

x = x1 + u ∆x ∆x = x2-x1

y = y1 + u ∆y ∆y = y2 –y1

u ∈ [0,1]

Page 14: ComputerGraphics

7/21/2019 ComputerGraphics

http://slidepdf.com/reader/full/computergraphics-56db938d1bf50 14/36

Line Clipping: Liang-Barsky• Point clipping in parameter form:

xwmin  ≤ x1 + u ∆x ≤ xwmax

ywmin  ≤ y1 + u ∆y ≤ ywmax

• Solve for u and obtain equivalently

upk  ≤  qk k = 1,2,3,4k=1 (left): p1 = -∆x q1 = x1-xwmin

k=2 (right): p2 = ∆x q2 = xwmax-x1

k=3 (bottom): p3 = -∆y q3 = y1-ywmin

k=4 (top): p4 = ∆y q4 = ywmax - y1

Page 15: ComputerGraphics

7/21/2019 ComputerGraphics

http://slidepdf.com/reader/full/computergraphics-56db938d1bf50 15/36

Line Clipping: Liang-Barsky• Strategy:

1. If pk = 0 for some k then the line is parallel to aclipping boundary. Now test qk :

if one qk < 0 for these k then line is outsideif all qk  ≥ 0 then line is inside

2. For all pk < 0 calculate u1 = max (0, {qk /pk}) todetermine intersection point with the possiblyextended clipping boundary k and obtain a newstarting point for the line at u1.

3. For all pk > 0 calculate u2 = min (1, {qk /pk}) to deter-mine intersection points with extended clippingboundary k and obtain a new end point at u2.

4. If u1 > u2 then discard the line5. The line is now between [u

1,u

2]

Page 16: ComputerGraphics

7/21/2019 ComputerGraphics

http://slidepdf.com/reader/full/computergraphics-56db938d1bf50 16/36

Line Clipping: Liang-Barsky• Example 1: p1 < 0 , p2 > 0 , p3 < 0, p4 > 0. For

k=1,3 calculate q1 /p1 (P1) and q3 /p3 (P3) andchoose u1 = max (0, q1 /p1, q3 /p3 ) = q1 /p1

• Likewise for k=2,4 calculate q2 /p2 (P2) and q4 /p4

(P4) and choose u2 = min (1, q2 /p2, q4 /p4 ) = q4 /p4

xmin

ymin

ymax

xmax

(x1,y1)

(x2,y2)

P1P3

P4

P2

Page 17: ComputerGraphics

7/21/2019 ComputerGraphics

http://slidepdf.com/reader/full/computergraphics-56db938d1bf50 17/36

Line Clipping: Liang-Barsky• Example 2: Consider horizontal lines with

∆y = 0, p3 = p4 = 0.• For line 1, q4 < 0 and will be discarded.

• For line 2, p1 < 0, p2 > 0, q3 > 0 and q4 >0. We proceed to calculate u1 and u2.

xmin

ymin

ymax

xmax

(x1,y1) (x2,y2)

2

1

Page 18: ComputerGraphics

7/21/2019 ComputerGraphics

http://slidepdf.com/reader/full/computergraphics-56db938d1bf50 18/36

Line Clipping: Liang-Barsky• Example 2: p1 < 0, note that q1 > 0 hence

q1 /p1 < 0, and u1 = max{0,q1 /p1} = 0• p2 > 0, calculate q2 /p2 and choose u2 = min

(q2 /p2, 1) = q2 /p2.

• u1 < u2 and the line is between [u1,u2]

xmin

ymin

ymax

xmax

(x1,y1) (x2,y2)

Page 19: ComputerGraphics

7/21/2019 ComputerGraphics

http://slidepdf.com/reader/full/computergraphics-56db938d1bf50 19/36

Line Clipping: Liang-Barsky• Example 3:

 – p1 < 0, p2 > 0 , p3 < 0 and p4 > 0.

 – calculate q1 /p1 (P1) and q3 /p3 (P3) and choose

u1 = max (0, q1 /p1, q3 /p3 ) = q1 /p1

 – calculate q2 /p2 (P2) and q4 /p4 (P4) and choose

u2 = min (1, q2 /p2, q4 /p4 ) = q4 /p4

 – u1 > u2 hence discard line

xmin

ymin

ymax

xmax(x1,y1)

(x2,y2)

P1

P3

P4

P2

Page 20: ComputerGraphics

7/21/2019 ComputerGraphics

http://slidepdf.com/reader/full/computergraphics-56db938d1bf50 20/36

YALCAYet Another Line Clipping Algorithm

• Outline of idea:

 – based on midpoint subdivision

1. if line is clearly totally inside then accept

2. if line is clearly totally outside then reject

3. divide line into two and return to 1:

xnew = (x1+x2)/2, ynew = (y1+y2)/2,

Page 21: ComputerGraphics

7/21/2019 ComputerGraphics

http://slidepdf.com/reader/full/computergraphics-56db938d1bf50 21/36

Line Clipping: Nicholl-Lee-Nicholl

• Basic idea: more intelligent choices are

made before intersection points withwindow boundaries are calculated

• Computationally more effective thanCohen-Sutherland and Liang-Barsky butunlike these cannot easily be extended to

3D.

Page 22: ComputerGraphics

7/21/2019 ComputerGraphics

http://slidepdf.com/reader/full/computergraphics-56db938d1bf50 22/36

Line Clipping: Nicholl-Lee-Nicholl

• Consider a line from P1 to P2. P1 may be

postioned topologically in 3 different areas:all other are symmtrical w.r.t one of these

P1 P1

P1

Page 23: ComputerGraphics

7/21/2019 ComputerGraphics

http://slidepdf.com/reader/full/computergraphics-56db938d1bf50 23/36

Nicholl-Lee-Nicholl: Topology 1• Case 1: P2 is in the window then accept line

• Case 2: P2 is not in the window, then determinein which subdirection the line is: T, R, B or L

• Once we know the subdirection calculate

intersection point with corresponding clippingboundary

P1 R

T

L

B

Page 24: ComputerGraphics

7/21/2019 ComputerGraphics

http://slidepdf.com/reader/full/computergraphics-56db938d1bf50 24/36

Nicholl-Lee-Nicholl: Topology 2• Determine in which subarea the line is: LT,

LR, LB or L• If P2 is in one of these subareas we now

how to clip the line. If P2

is outside theseareas we discard the line.

P1

LT

LR

LB

LL

L

Page 25: ComputerGraphics

7/21/2019 ComputerGraphics

http://slidepdf.com/reader/full/computergraphics-56db938d1bf50 25/36

Nicholl-Lee-Nicholl: Topology 3• Two subcases: Again determine in which

subarea P2 is located.

P1

P1

L L T

T

TR

TBLB

L

TTR

LR

LB

Page 26: ComputerGraphics

7/21/2019 ComputerGraphics

http://slidepdf.com/reader/full/computergraphics-56db938d1bf50 26/36

Nicholl-Lee-Nicholl• To find out in which area P2 is we compare the

slope of the line P1P2 with the slopes of the linesforming the boundaries of the areas. E.g. fortopology 2, if P2 is not in L or to the left of the left

window boundary, P2 is in area LT ifslope(P1PTR) < slope(P1P2) < slope(P1PTL)

that is

(yT-y1)/(xR-x1) < (y2-y1)/(x2-x1) < (yT-y1)/(xL-x1)

Page 27: ComputerGraphics

7/21/2019 ComputerGraphics

http://slidepdf.com/reader/full/computergraphics-56db938d1bf50 27/36

Nicholl-Lee-Nicholl• Coordinate differences and product

calculations are saved during calculations• The parametric line equations are used to

calculate intersection points.

Page 28: ComputerGraphics

7/21/2019 ComputerGraphics

http://slidepdf.com/reader/full/computergraphics-56db938d1bf50 28/36

Polygons, concave• Some polygon clipping algorithms assume

the polygon is convex• Concave polygons with non-intersecting

edges can be decomposed into convexpolygons. Note: Several solutions!

Page 29: ComputerGraphics

7/21/2019 ComputerGraphics

http://slidepdf.com/reader/full/computergraphics-56db938d1bf50 29/36

Polygons, concave• Detecting concavity: Calculate cross

products of consecutive polygon edgesand check for changes of sign in z-component

x

y

E1 E2

E3

E4

E5

E6

(E1 x E2)z > 0

(E2 x E3)z > 0

(E3 x E4)z < 0(E4 x E5)z > 0

(E5 x E6)z > 0

(E6

x E1

)z

> 0

Page 30: ComputerGraphics

7/21/2019 ComputerGraphics

http://slidepdf.com/reader/full/computergraphics-56db938d1bf50 30/36

Splitting Concave Polygons• Vector method to split concave polygons:

when the sign of (En x En+1)z changes, splitalong En

• Calculate new edge and the new polygon

x

y

E1 E2

E3

E4

E5

E6

Page 31: ComputerGraphics

7/21/2019 ComputerGraphics

http://slidepdf.com/reader/full/computergraphics-56db938d1bf50 31/36

Splitting Concave Polygons• Rotational method: rotate each edge along

the x-axis and reposition it to start from theorigin. Note the sign of the y-coordinate forthe next vertex:

x

y

E1

E2

E3 E4

E5

E6

Page 32: ComputerGraphics

7/21/2019 ComputerGraphics

http://slidepdf.com/reader/full/computergraphics-56db938d1bf50 32/36

Polygon clipping• The process of clipping edges of a

polygon may leave us with a collection ofunconnected lines:

xmin

ymin

ymax

xmax xmaxxmin

ymin

ymax

Page 33: ComputerGraphics

7/21/2019 ComputerGraphics

http://slidepdf.com/reader/full/computergraphics-56db938d1bf50 33/36

Polygon clipping:

Sutherland-Hodgeman• Basic idea: Clip polygon = {ordered list of

vertices} against each clip boundary in turn: left,right, bottom, top. Change the list of vertices byprocessing edges: – If the first vertex is outside and the second is inside,

then move the first vertex to the clipping boundaryand accept both vertices.

 – If both vertices are inside, accept the second. – If the first vertex is inside and the second is outside,

then move the second to the clipping boundary andaccept the second.

 – if both vertices are outside, reject both.

• Note: process only against one clipping

boundary at a time!

Page 34: ComputerGraphics

7/21/2019 ComputerGraphics

http://slidepdf.com/reader/full/computergraphics-56db938d1bf50 34/36

Polygon clipping:

Sutherland-Hodgeman• Polygon vertices are saved or discarded:

V1 V’1 V2 V1

V2 V’1V2 V1

V2

V1

Save V’1, V2 Save V2 Save V’1 Save none

Page 35: ComputerGraphics

7/21/2019 ComputerGraphics

http://slidepdf.com/reader/full/computergraphics-56db938d1bf50 35/36

Polygon clipping:

Sutherland-Hodgeman• Example:

V’’2

V3

V’2

V2

V’1

V’3

V1

Page 36: ComputerGraphics

7/21/2019 ComputerGraphics

http://slidepdf.com/reader/full/computergraphics-56db938d1bf50 36/36

Polygon Clipping

Weiler-Atherton• Works for polygons that are splitted into

several polygons• Basic idea:

 – for outside-to-inside pair of vertices, followthe polygon boundary

 – for inside-to-outside pair of vertices, follow the

polygon boundary