part iv 2d clipping - web.ti.bfh.chfrc1/www/cpvr7281/2dclipping-1x2.pdf · introduction to computer...

34
Introduction to Computer Graphics Part IV 2D Clipping Introduction to Computer Graphics Line segment clipping The Cohen-Sutherland algorithm The Liang-Barsky algorithm Polygon clipping The Sutherland-Hodgman algorithm The Weiler-Atherton algorithm Boolean operations on polygons

Upload: lythu

Post on 12-Mar-2018

217 views

Category:

Documents


1 download

TRANSCRIPT

Introduction to Computer Graphics

Part IV

2D Clipping

Introduction to Computer Graphics

Line segment clippingThe Cohen-Sutherland algorithmThe Liang-Barsky algorithm

Polygon clippingThe Sutherland-Hodgman algorithmThe Weiler-Atherton algorithm

Boolean operations on polygons

Introduction to Computer Graphics

Line segment clipping

Outline

Line segment clippingThe Cohen-Sutherland algorithmThe Liang-Barsky algorithm

Polygon clippingThe Sutherland-Hodgman algorithmThe Weiler-Atherton algorithm

Boolean operations on polygons

Introduction to Computer Graphics

Line segment clipping

Clipping

The goal of clipping is mainly to eliminate parts of the scene whichare not visible within a given viewport (e.g. window).

Introduction to Computer Graphics

Line segment clipping

Clipping (cont’d)

If the boundaries of the clipping rectangle are at(xmin, ymin); (xmax , ymax) the the four inequalities must be satisfiedfor a point (x , y) to be inside the clipping region:

1. xmin ≤ x

2. x ≤ xmax

3. ymin ≤ y

4. y ≤ ymax

Introduction to Computer Graphics

Line segment clipping

The Cohen-Sutherland algorithm

The Cohen-Sutherland algorithm

This algorithm performs some tests on lines to determine whetherclipping computation can be avoided. These tests say that:

1. If the line segment is totally inside the clipping region ⇒ donothing and accept the line segment

2. If the line segment is totally outside the clipping region ⇒ donothing and reject the line segment

3. If the line segment is partially inside the clipping region ⇒ clipthe line segment

Introduction to Computer Graphics

Line segment clipping

The Cohen-Sutherland algorithm

The Cohen-Sutherland algorithm (cont’d)

For these tests, the space is divided into 9 regions

{top,left}1001

{top}1000

{top, right}1010

{left}0001

∅0000

{right}0010

{bottom,left}0101

{bottom}0100

{bottom,right}0110

Introduction to Computer Graphics

Line segment clipping

The Cohen-Sutherland algorithm

The Cohen-Sutherland algorithm : example 1

A

B

A = ∅B = ∅A ∪ B = ∅⇒ accept

Introduction to Computer Graphics

Line segment clipping

The Cohen-Sutherland algorithm

The Cohen-Sutherland algorithm : example 2

E

F E = {left}F = {top}E ∪ F 6= ∅E ∩ F = ∅⇒ clip

Introduction to Computer Graphics

Line segment clipping

The Cohen-Sutherland algorithm

The Cohen-Sutherland algorithm : example 3

C

DC = ∅D = {top}C ∪ D 6= ∅C ∩ D = ∅⇒ clip

Introduction to Computer Graphics

Line segment clipping

The Cohen-Sutherland algorithm

The Cohen-Sutherland algorithm : example 4

I

J

I = {bottom}J = {right}I ∪ J 6= ∅I ∩ J = ∅⇒ clip

Introduction to Computer Graphics

Line segment clipping

The Cohen-Sutherland algorithm

The Cohen-Sutherland algorithm : example 5

K

L

K = {bottom, right}L = {right}K ∪ L 6= ∅K ∩ L 6= ∅⇒ reject

Introduction to Computer Graphics

Line segment clipping

The Cohen-Sutherland algorithm

The Cohen-Sutherland algorithm (cont’d)

The rules for accepting (or rejecting) a line segment into theclipping algorithm are:

1. If P1 ∪ P2 = ∅ then accept. The line segment is completelyinside the clipping region.

2. If P1 ∩ P2 6= ∅ then reject. The line segment is completelyoutside the clipping region.

3. If P1 ∩ P2 = ∅ then clip. The line segment should be clipped.

Introduction to Computer Graphics

Line segment clipping

The Cohen-Sutherland algorithm

The Cohen-Sutherland algorithm (cont’d)

P1

P2

P ′1

P ′′1

P ′′2

P ′2

P1 = {bottom, left}P2 = {top, right}P1 ∪ P2 = {bottom, top, left, right}P1 ∩ P2 = {}⇒ do clipping

m :=P2.y − P1.y

P2.x − P1.x

Introduction to Computer Graphics

Line segment clipping

The Cohen-Sutherland algorithm

The Cohen-Sutherland algorithm (cont’d)

P1

P2

P ′1

P ′′1

P ′′2

P ′2 Is {left} subset of P1 ?

Yes ⇒P ′1.y =(winLeft− P1.x) ·m + P1.y

P ′1.x = winLeft

Introduction to Computer Graphics

Line segment clipping

The Cohen-Sutherland algorithm

The Cohen-Sutherland algorithm (cont’d)

P1

P2

P ′1

P ′′1

P ′′2

P ′2 Is {bottom} subset of P1 ?

Yes ⇒P ′′1 .x :=(winBottom− P ′1.y)/m + P ′1.x

P ′′1 .y = winBottom

Introduction to Computer Graphics

Line segment clipping

The Cohen-Sutherland algorithm

The Cohen-Sutherland algorithm (cont’d)

Similarly

P1

P2

P ′1

P ′′1

P ′′2

P ′2

P1

P2

P ′1

P ′′1

P ′′2

P ′2

Introduction to Computer Graphics

Line segment clipping

The Cohen-Sutherland algorithm

The Cohen-Sutherland algorithm (cont’d)

And finally:

P1

P2

P ′1

P ′′1

P ′′2

P ′2

Introduction to Computer Graphics

Line segment clipping

The Cohen-Sutherland algorithm

Pseudo-code for the Cohen-Sutherland algorithm

Require: a rectangular axis aligned clipping windowRequire: a segment given by two distinct pointsEnsure: a clipped segment that is totally inside the clipping window

if trivial accept thenreturn the segment unmodified

end ifif trivial reject then

return an empty segmentend iffor all point ∈ segmentEnd do

if point in the BOTTOM area thenclip point against BOTTOM boundary

end ifif point in the TOP area then

clip point against TOP boundaryend ifif point in the LEFT area then

clip point against LEFT boundaryend ifif point in the RIGHT area then

clip point against RIGHT boundaryend if

end forreturn segment

Introduction to Computer Graphics

Line segment clipping

The Liang-Barsky algorithm

2D Liang-Barsky Clipping algorithm

The main idea of the Liang-Barsky clipping algorithm is to do asmuch testing as possible before computing line intersection.Let s1 be line segment defined by its extremities (x0; y0) and(x1; y1). The parametric form for this line segment is given by:

x = x0 + λ(x1 − x0) = x0 + λ ∆x

y = y0 + λ(y1 − y0) = y0 + λ ∆y

with 0 < λ ≤ 1.

Introduction to Computer Graphics

Line segment clipping

The Liang-Barsky algorithm

2D Liang-Barsky Clipping algorithm (cont’d)A point is in the clip window if:

xmin ≤ x0 + λ ∆x ≤ xmax

ymin ≤ y0 + λ ∆y ≤ ymax

which can be expressed by the 4 inequalities :

λ pk ≤ qk k = 1, 2, 3, 4

where

p1 = −∆x q1 = x0 − xmin

p2 = ∆x q2 = xmax − x0

p3 = −∆y q3 = y0 − ymin

p4 = ∆y q4 = ymax − y0

Introduction to Computer Graphics

Line segment clipping

The Liang-Barsky algorithm

2D Liang-Barsky Clipping algorithm (cont’d)

1. A line parallel to a clipping window edge has pk = 0 for thatboundary.

2. If for the same value of k , qk < 0, the line is completelyoutside and can be eliminated.

3. When pk < 0 the line proceeds outside to inside the clipwindow and when pk > 0 the line proceeds inside to outside.

4. For a non-zero pk , the value λk = qk/pk gives the intersectionpoint with the window boundaries

5. For each line segment calculate λ1 and λ2. For λ1 look at theboundaries for which pk < 0 and set λ1 = max(0, qk/pk). Forλ2 look an boundaries for which pk > 0 and setλ1 = min(1, qk/pk). If λ1 > λ2 the line is outside andtherefore rejected.

Introduction to Computer Graphics

Line segment clipping

The Liang-Barsky algorithm

2D Liang-Barsky Clipping : an example

P1 = (30; 20)

P2 = (280; 160)

70 230

60

150

Introduction to Computer Graphics

Line segment clipping

The Liang-Barsky algorithm

2D Liang-Barsky Clipping : an example

P1 = (30, 20)

P2 = (280, 160)

u1 = 0.16

u2 = 0.28

u3 = 0.8 u4 = 0.928

Introduction to Computer Graphics

Line segment clipping

The Liang-Barsky algorithm

Polygon clipping

I Most of the graphics package support only fill area that arepolygons

I Sometimes only convex polygons are accepted

I A standard line clipping (e.g. Cohen-Sutherland) will producea set of disjoint lines

I One need an algorithm which returns a closed polygon or aset of closed polygons.

Introduction to Computer Graphics

Polygon clipping

Outline

Line segment clippingThe Cohen-Sutherland algorithmThe Liang-Barsky algorithm

Polygon clippingThe Sutherland-Hodgman algorithmThe Weiler-Atherton algorithm

Boolean operations on polygons

Introduction to Computer Graphics

Polygon clipping

Polygon clipping using line clipping algorithm

Introduction to Computer Graphics

Polygon clipping

Polygon clipping

The correct solution should be:

Introduction to Computer Graphics

Polygon clipping

Polygon clipping

I Basically one can process using the same approach as in a lineclipping algorithm

I First one can check if a polygon can be totally saved or totallyrejected by testing it coordinates extends (bounding box).

I The simplest bounding box is the Axis Aligned Bounding Box(AABB) which can be computed finding the minimal xcoordinate of all vertices of the polygon, the maximal xcoordinate, the minimal y coordinate and the maximal ycoordinate.

Introduction to Computer Graphics

Polygon clipping

Polygon clipping

Introduction to Computer Graphics

Polygon clipping

Polygon clipping

If the fill area is not completely inside or completely outside, oneneed to locate the polygon intersection with the clippingboundaries.

P1

P2

P3

P ′1 P ′′

1

P ′2 P ′′

2

P ′3

P ′′3

P1

P2

P3

P ′1 P ′′

1

P ′2 P ′′

2

P ′3

P ′′3

Introduction to Computer Graphics

Polygon clipping

The Sutherland-Hodgman algorithm

The Sutherland-Hodgman algorithmThe basic idea of the algorithm is :

I Consider each edge of the viewport individually

I Clip the polygon against the edge equation

I After doing all planes, the polygon is fully clipped

Introduction to Computer Graphics

Polygon clipping

The Sutherland-Hodgman algorithm

The Sutherland-Hodgman algorithm (cont’d)

Introduction to Computer Graphics

Polygon clipping

The Sutherland-Hodgman algorithm

The Sutherland-Hodgman algorithm (cont’d)

Introduction to Computer Graphics

Polygon clipping

The Sutherland-Hodgman algorithm

The Sutherland-Hodgman algorithm (cont’d)

Introduction to Computer Graphics

Polygon clipping

The Sutherland-Hodgman algorithm

The Sutherland-Hodgman algorithm (cont’d)

Introduction to Computer Graphics

Polygon clipping

The Sutherland-Hodgman algorithm

The Sutherland-Hodgman algorithm (cont’d)

Introduction to Computer Graphics

Polygon clipping

The Sutherland-Hodgman algorithm

The Sutherland-Hodgman algorithm (cont’d)

I The Sutherland-Hodgman algorithm is an efficient algorithmto clip convex polygon

I For the non convex polygon, the result may be wrong sincethe algorithm always return a single polygon

I The general strategies is to sent the pair of endpoints for eachsuccessive polygon edges through the series of clipper (left,right, bottom, top).

I The different clipper may work in parrallel.

Introduction to Computer Graphics

Polygon clipping

The Sutherland-Hodgman algorithm

The Sutherland-Hodgman algorithm (cont’d)

Each clipper should consider 4 situations:

1. Both the start point and the end point of the edge as insidethe clipping region

2. The start point is inside the clipping region and the end pointis outside

3. Both vertices are outside the clipping region

4. The start point is outside the clipping region and the endpoint is inside

Introduction to Computer Graphics

Polygon clipping

The Sutherland-Hodgman algorithm

The Sutherland-Hodgman algorithm (cont’d)

Each of the successive clipper generates an output to the nextclipper according to:

1. If start point is outside and end point inside⇒ output = {intersection with the border, end point}

2. If both points are inside⇒ output = {end point}

3. If start point is inside and end point outside⇒ output = {intersection with the border}

4. If both points are outside⇒ output = {}

Introduction to Computer Graphics

Polygon clipping

The Sutherland-Hodgman algorithm

The Sutherland-Hodgman algorithm (cont’d)

V1V ′1

V2

Input : {outside, inside}Output : {V ′1, V2}

V2

V3

Input : {inside, inside}Output : {V3}

Introduction to Computer Graphics

Polygon clipping

The Sutherland-Hodgman algorithm

The Sutherland-Hodgman algorithm (cont’d)

V3V ′3

V4

Input : {inside, outside}Output : {V ′3}

V1

V4

Input : {outside, outside}Output : {}

Introduction to Computer Graphics

Polygon clipping

The Sutherland-Hodgman algorithm

The Sutherland-Hodgman algorithm (cont’d)

1

1’

22’

2”

3

3’

Introduction to Computer Graphics

Polygon clipping

The Sutherland-Hodgman algorithm

The Sutherland-Hodgman algorithm (cont’d)

Left clipper Right clipper Bottom clipper Top clipper

{1,2} →{2}{2,3} →{2’} {2,2’}→{2’}{3,1}→{3’,1} {2’,3’} →{3’} {2’,3’} →{2”}

{3’,1} →{1} {3’,1} →{}{1,2} →{2} {1,2} →{1’,2} {2”,1’} →{1’}

{2,2’} →{2’} {1’,2}→{2}{2,2’} →{2’}{2’,2”} →{2”}

Introduction to Computer Graphics

Polygon clipping

The Sutherland-Hodgman algorithm

The Sutherland-Hodgman algorithm (cont’d)

For non-convex polygon, the Sutherland-Hodgman may producewrong results:

Introduction to Computer Graphics

Polygon clipping

The Weiler-Atherton algorithm

The Weiler-Atherton algorithm

1. It is a general clipping algorithm which can clip non convexpolygon against arbitrary clipping window.

2. This algorithm is also used in 3D for hidden surface removal.

Introduction to Computer Graphics

Polygon clipping

The Weiler-Atherton algorithm

The Weiler-Atherton algorithm (cont’d)Main algorithm strategy : walk along the polygon and windowedges counterclockwise to find the clipped regions

Introduction to Computer Graphics

Polygon clipping

The Weiler-Atherton algorithm

The Weiler-Atherton algorithm (cont’d)First find all intersection points between the edges of polygon andclipping window (clipping window may be non-rectangular).

Introduction to Computer Graphics

Polygon clipping

The Weiler-Atherton algorithm

The Weiler-Atherton algorithm (cont’d)Mark points where polygon enters clipping window (green here) asthe walk is taken along the polygon

Introduction to Computer Graphics

Polygon clipping

The Weiler-Atherton algorithm

The Weiler-Atherton algorithm (cont’d)While there is still an unprocessed entering intersection : Walk”polygon/window boundary

Introduction to Computer Graphics

Polygon clipping

The Weiler-Atherton algorithm

The Weiler-Atherton algorithm (cont’d)If intersection point is out → in:

I Record clipped pointI Follow polygon boundary

Introduction to Computer Graphics

Polygon clipping

The Weiler-Atherton algorithm

The Weiler-Atherton algorithm (cont’d)If intersection point is in → out:

I Record clipped pointI Follow window boundary

Introduction to Computer Graphics

Polygon clipping

The Weiler-Atherton algorithm

The Weiler-Atherton algorithm (cont’d)

Introduction to Computer Graphics

Polygon clipping

The Weiler-Atherton algorithm

The Weiler-Atherton algorithm (cont’d)

Introduction to Computer Graphics

Polygon clipping

The Weiler-Atherton algorithm

The Weiler-Atherton algorithm (cont’d)Repeat until the clipped-polygon is closed

Introduction to Computer Graphics

Polygon clipping

The Weiler-Atherton algorithm

The Weiler-Atherton algorithm (cont’d)Find the next unprocessed entering point

Introduction to Computer Graphics

Polygon clipping

The Weiler-Atherton algorithm

The Weiler-Atherton algorithm (cont’d)Compute the next clipped subpolygon

Introduction to Computer Graphics

Polygon clipping

The Weiler-Atherton algorithm

The Weiler-Atherton algorithm (cont’d)

Introduction to Computer Graphics

Polygon clipping

The Weiler-Atherton algorithm

The Weiler-Atherton algorithm (cont’d)Repeat until there is no more unprocessed entering point

Introduction to Computer Graphics

Polygon clipping

The Weiler-Atherton algorithm

The Weiler-Atherton algorithm (cont’d)Importance of good adjacency data structure (Here we can simplylist the oriented edges)

Introduction to Computer Graphics

Polygon clipping

The Weiler-Atherton algorithm

The Weiler-Atherton algorithm (cont’d)I What if a vertex is on the boundary?I What happens if it is ”almost” on the boundary?

I Problem with floating point precisionI Welcome to the real world of geometry!

Introduction to Computer Graphics

Boolean operations on polygons

Outline

Line segment clippingThe Cohen-Sutherland algorithmThe Liang-Barsky algorithm

Polygon clippingThe Sutherland-Hodgman algorithmThe Weiler-Atherton algorithm

Boolean operations on polygons

Introduction to Computer Graphics

Boolean operations on polygons

Boolean operations on polygons

I Sometimes one need to perform some boolean operation onpolygon (e.g. CAD systems).

I The meaning of these operations is the same as for theoperations on sets

I The methods and techniques to choose which part of thepolygon should be considered are the same as those used forpolygon clipping.

I These algorithms expect that the input polygons are simple(or Jordan) which mean that the edges does not cross outsidethe polygon vertices.

Introduction to Computer Graphics

Boolean operations on polygons

Boolean operations on polygons (cont’d)

Let P1 and P2 be the two polygons:

Introduction to Computer Graphics

Boolean operations on polygons

Boolean operations on polygons (cont’d)

The intersection of these two polygons is defined as:

Introduction to Computer Graphics

Boolean operations on polygons

Boolean operations on polygons (cont’d)

The union of these two polygons is defined as:

Introduction to Computer Graphics

Boolean operations on polygons

Boolean operations on polygons (cont’d)

The difference of these two polygons is defined as:

Introduction to Computer Graphics

Boolean operations on polygons

Boolean operations on polygons (cont’d)

The symmetrical difference (XOR) of these two polygons is definedas: