WINDOWING AND CLIPPING - WordPress.com...COHEN SUTHERLAND LINE CLIPPING ALGORITHM SKNCOE Preapered...

39
WINDOWING AND CLIPPING SKNCOE Preapered by : Trupti Gurav 1 2/27/2014

Transcript of WINDOWING AND CLIPPING - WordPress.com...COHEN SUTHERLAND LINE CLIPPING ALGORITHM SKNCOE Preapered...

Page 1: WINDOWING AND CLIPPING - WordPress.com...COHEN SUTHERLAND LINE CLIPPING ALGORITHM SKNCOE Preapered by : Trupti 2/27/2014 Gurav 6 Cohen-Sutherland: World Division 1001 1000 1010 0001

WINDOWING AND CLIPPING

SKNCOE Preapered by : Trupti Gurav 1 2/27/2014

Page 2: WINDOWING AND CLIPPING - WordPress.com...COHEN SUTHERLAND LINE CLIPPING ALGORITHM SKNCOE Preapered by : Trupti 2/27/2014 Gurav 6 Cohen-Sutherland: World Division 1001 1000 1010 0001

Clipping

Clipping refers to the removal of part of a scene.

Window : it is the selected area of the picture. Usually it is rectangular in shape.

Internal clipping removes parts of a picture outside a given region.

External clipping removes parts inside a region.

(is important in multiwindow display environment.)

SKNCOE Preapered by : Trupti Gurav 2 2/27/2014

Page 3: WINDOWING AND CLIPPING - WordPress.com...COHEN SUTHERLAND LINE CLIPPING ALGORITHM SKNCOE Preapered by : Trupti 2/27/2014 Gurav 6 Cohen-Sutherland: World Division 1001 1000 1010 0001

Windowing

A scene is made up of a collection of objects specified in world coordinates

SKNCOE Preapered by : Trupti Gurav 3 2/27/2014

Page 4: WINDOWING AND CLIPPING - WordPress.com...COHEN SUTHERLAND LINE CLIPPING ALGORITHM SKNCOE Preapered by : Trupti 2/27/2014 Gurav 6 Cohen-Sutherland: World Division 1001 1000 1010 0001

When we display a scene only those objects within a particular window are displayed

wymax

wymin

wxmin wxmax

Window

World Coordinates SKNCOE Preapered by : Trupti

Gurav 4 2/27/2014

Page 5: WINDOWING AND CLIPPING - WordPress.com...COHEN SUTHERLAND LINE CLIPPING ALGORITHM SKNCOE Preapered by : Trupti 2/27/2014 Gurav 6 Cohen-Sutherland: World Division 1001 1000 1010 0001

Point Clipping

Easy - a point (x,y) is not clipped if:

wxmin ≤ x ≤ wxmax AND wymin ≤ y ≤ wymax

otherwise it is clipped

wymax

wymin

wxmin wxmax

Window

P1

P2

P5

P7

P10

P9

P4

P8

Clipped

Points Within the Window

are Not Clipped

Clipped

Clipped

Clipped

SKNCOE Preapered by : Trupti Gurav 5 2/27/2014

Page 6: WINDOWING AND CLIPPING - WordPress.com...COHEN SUTHERLAND LINE CLIPPING ALGORITHM SKNCOE Preapered by : Trupti 2/27/2014 Gurav 6 Cohen-Sutherland: World Division 1001 1000 1010 0001

COHEN SUTHERLAND LINE CLIPPING ALGORITHM

SKNCOE Preapered by : Trupti Gurav 6 2/27/2014

Page 7: WINDOWING AND CLIPPING - WordPress.com...COHEN SUTHERLAND LINE CLIPPING ALGORITHM SKNCOE Preapered by : Trupti 2/27/2014 Gurav 6 Cohen-Sutherland: World Division 1001 1000 1010 0001

Cohen-Sutherland: World Division

1001 1000 1010

0001 0000

Window 0010

0101 0100 0110

Space is divided into regions based on the window boundaries

Each region has a unique four bit region code

Region codes indicate the position of the regions with respect to the window

above below right left

3 2 1 0

Region Code Legend

SKNCOE Preapered by : Trupti Gurav 7 2/27/2014

Page 8: WINDOWING AND CLIPPING - WordPress.com...COHEN SUTHERLAND LINE CLIPPING ALGORITHM SKNCOE Preapered by : Trupti 2/27/2014 Gurav 6 Cohen-Sutherland: World Division 1001 1000 1010 0001

Cohen-Sutherland: Labelling

Every end-point of the line is labelled with the appropriate region code.

wymax

wymin

wxmin wxmax

Window

P3 [0001] P6 [0000]

P5 [0000]

P7 [0001]

P10 [0100]

P9 [0000]

P4 [1000]

P8 [0010]

P12 [0010]

P11 [1010]

P13 [0101] P14 [0110]

Three cases : Case1 : Lines inside the window Case2: lines outside the window Case3 : lines partially inside the window

SKNCOE Preapered by : Trupti Gurav 8 2/27/2014

Page 9: WINDOWING AND CLIPPING - WordPress.com...COHEN SUTHERLAND LINE CLIPPING ALGORITHM SKNCOE Preapered by : Trupti 2/27/2014 Gurav 6 Cohen-Sutherland: World Division 1001 1000 1010 0001

Case 1: Lines In The Window

Lines completely contained within the window boundaries have region code [0000] for both end-points so are not clipped

wymax

wymin

wxmin wxmax

Window

P3 [0001] P6 [0000]

P5 [0000]

P7 [0001]

P10 [0100]

P9 [0000]

P4 [1000]

P8 [0010]

P12 [0010]

P11 [1010]

P13 [0101] P14 [0110]

SKNCOE Preapered by : Trupti Gurav 9 2/27/2014

Page 10: WINDOWING AND CLIPPING - WordPress.com...COHEN SUTHERLAND LINE CLIPPING ALGORITHM SKNCOE Preapered by : Trupti 2/27/2014 Gurav 6 Cohen-Sutherland: World Division 1001 1000 1010 0001

Case 2:Lines Outside The Window

Take AND operation of the outcodes of the end points of the line.

If answer of AND operation is non zero means line is outside the window and hence discard those lines.

wymax

wymin

wxmin wxmax

Window

P3 [0001] P6 [0000]

P5 [0000]

P7 [0001]

P10 [0100]

P9 [0000]

P4 [1000]

P8 [0010]

P12 [0010]

P11 [1010]

P13 [0101] P14 [0110]

1010

0010 AND

0010

SKNCOE Preapered by : Trupti Gurav 10 2/27/2014

Page 11: WINDOWING AND CLIPPING - WordPress.com...COHEN SUTHERLAND LINE CLIPPING ALGORITHM SKNCOE Preapered by : Trupti 2/27/2014 Gurav 6 Cohen-Sutherland: World Division 1001 1000 1010 0001

Case3: Other Lines

Lines for which the AND operation of the end coordinates is ZERO . These are identified as partially visible lines.

These lines are processed as follows:

Find the intersection of those lines with the edges of window.

We can use the region codes to determine which window boundaries should be considered for intersection.eg: region code 1000 then line intersects with TOP boundary.Find intersection with that boundary only.

Check other end of the line to get other intersection.

Draw line between calculated intersection.

SKNCOE Preapered by : Trupti

Gurav 11 2/27/2014

Page 12: WINDOWING AND CLIPPING - WordPress.com...COHEN SUTHERLAND LINE CLIPPING ALGORITHM SKNCOE Preapered by : Trupti 2/27/2014 Gurav 6 Cohen-Sutherland: World Division 1001 1000 1010 0001

Calculating Line Intersections

Intersection points with the window boundaries are calculated using the line-equation parameters

Consider a line with the end-points (x1, y1) and (x2, y2)

The y-coordinate of an intersection with a vertical window boundary can be calculated using:

y = y1 + m (xboundary - x1)

where xboundary can be set to either wxmin or wxmax

SKNCOE Preapered by : Trupti Gurav 12 2/27/2014

Page 13: WINDOWING AND CLIPPING - WordPress.com...COHEN SUTHERLAND LINE CLIPPING ALGORITHM SKNCOE Preapered by : Trupti 2/27/2014 Gurav 6 Cohen-Sutherland: World Division 1001 1000 1010 0001

Calculating Line Intersections (cont…)

The x-coordinate of an intersection with a

horizontal window boundary can be calculated

using:

x = x1 + (yboundary - y1) / m

where yboundary can be set to either wymin or wymax

m is the slope of the line in question and can be

calculated as m = (y2 - y1) / (x2 - x1)

SKNCOE Preapered by : Trupti Gurav 13 2/27/2014

Page 14: WINDOWING AND CLIPPING - WordPress.com...COHEN SUTHERLAND LINE CLIPPING ALGORITHM SKNCOE Preapered by : Trupti 2/27/2014 Gurav 6 Cohen-Sutherland: World Division 1001 1000 1010 0001

Algorithm 1. Read two endpoints of the line say P1 (x1,y1) and

p2(x2,y2)

2. Read two corners of the window (left –top ) and (right-bottom) say (Wx1,Wy1) and (Wx2, Wy2)

3. Asssign the region codes to the end co-or of P1. 1. Set Bit 1 – if (x<Wx1)

2. Set Bit 2 – if (x >Wx2)

3. Set Bit 3 – if (y<Wy1)

4. Set Bit 4 – if (y<Wy2)

4. Apply visibility check on the line.

SKNCOE Preapered by : Trupti

Gurav 14 2/27/2014

Page 15: WINDOWING AND CLIPPING - WordPress.com...COHEN SUTHERLAND LINE CLIPPING ALGORITHM SKNCOE Preapered by : Trupti 2/27/2014 Gurav 6 Cohen-Sutherland: World Division 1001 1000 1010 0001

Algorithm conti…

5. If line is partially visible then- 1. If region codes for the both end points are non-zero , find

intersection points p1’ and p2’ with boundery edges by checking the region codes.

2. If region code for any one end point is non-zero then find intersection point p1’ or p2’ with the boundary edge.

6. Divide the line segments considering the intersection points.

7. Draw the line segment

8. Stop.

SKNCOE Preapered by : Trupti

Gurav 15 2/27/2014

Page 16: WINDOWING AND CLIPPING - WordPress.com...COHEN SUTHERLAND LINE CLIPPING ALGORITHM SKNCOE Preapered by : Trupti 2/27/2014 Gurav 6 Cohen-Sutherland: World Division 1001 1000 1010 0001

Sutherland-Hodgman Polygon Clipping Algorithm

SKNCOE Preapered by : Trupti Gurav 16 2/27/2014

Page 17: WINDOWING AND CLIPPING - WordPress.com...COHEN SUTHERLAND LINE CLIPPING ALGORITHM SKNCOE Preapered by : Trupti 2/27/2014 Gurav 6 Cohen-Sutherland: World Division 1001 1000 1010 0001

Sutherland-Hodgman Polygon Clipping Algorithm

A technique for clipping areas developed by Sutherland & Hodgman

Put simply the polygon is clipped by comparing it against each boundary in turn.

Original Area Clip Left Clip Right Clip Top Clip Bottom SKNCOE Preapered by : Trupti Gurav 17 2/27/2014

Page 18: WINDOWING AND CLIPPING - WordPress.com...COHEN SUTHERLAND LINE CLIPPING ALGORITHM SKNCOE Preapered by : Trupti 2/27/2014 Gurav 6 Cohen-Sutherland: World Division 1001 1000 1010 0001

Input/output for algorithm:

• Input: list of polygon vertices in order.

• Output: list of clipped poygon vertices consisting of old vertices (maybe) and new vertices (maybe)

Sutherland-Hodgeman Clipping

SKNCOE Preapered by : Trupti Gurav 18 2/27/2014

Page 19: WINDOWING AND CLIPPING - WordPress.com...COHEN SUTHERLAND LINE CLIPPING ALGORITHM SKNCOE Preapered by : Trupti 2/27/2014 Gurav 6 Cohen-Sutherland: World Division 1001 1000 1010 0001

Sutherland-Hodgeman Clipping

Sutherland-Hodgman does four tests on every edge of the

polygon:

Inside – Inside ( I-I)

Inside –Outside (I-O)

Outside –Outside (O-O)

Outside-Inside(O-I)

Output co-ordinate list is created by doing these tests on

every edge of poly.

SKNCOE Preapered by : Trupti Gurav 19 2/27/2014

Page 20: WINDOWING AND CLIPPING - WordPress.com...COHEN SUTHERLAND LINE CLIPPING ALGORITHM SKNCOE Preapered by : Trupti 2/27/2014 Gurav 6 Cohen-Sutherland: World Division 1001 1000 1010 0001

Sutherland-Hodgeman Clipping

Edge from S to P takes one of four cases:

inside outside

s

p

Save p

inside outside

s

p

Save nothing

inside outside

s p

Save I

inside outside

s p

Save I

and P

I

SKNCOE Preapered by : Trupti Gurav 20 2/27/2014

Page 21: WINDOWING AND CLIPPING - WordPress.com...COHEN SUTHERLAND LINE CLIPPING ALGORITHM SKNCOE Preapered by : Trupti 2/27/2014 Gurav 6 Cohen-Sutherland: World Division 1001 1000 1010 0001

Sutherland-Hodgeman Clipping

Four cases:

1. S inside plane and P inside plane

Save p in the output-List

2. S inside plane and P outside plane

Find intersection point i

Save i in the output-List

3. S outside plane and P outside plane

Save nothing

4. S outside plane and P inside plane

Find intersection point i

Save i in the output-List, followed by P

SKNCOE Preapered by : Trupti Gurav 21 2/27/2014

Page 22: WINDOWING AND CLIPPING - WordPress.com...COHEN SUTHERLAND LINE CLIPPING ALGORITHM SKNCOE Preapered by : Trupti 2/27/2014 Gurav 6 Cohen-Sutherland: World Division 1001 1000 1010 0001

Generalized clipping

We have used four clipping routines; one for each boundary. But these routines are identical. They differ only in their tests for determining whether a point is inside or outside the boundary.

Can we make it general ??

It is possible to write one single routine and pass information about boundary as parameter. This change allows to clip along any line(not just horizontal or vertical boundaries).

SKNCOE Preapered by : Trupti Gurav 22 2/27/2014

Page 23: WINDOWING AND CLIPPING - WordPress.com...COHEN SUTHERLAND LINE CLIPPING ALGORITHM SKNCOE Preapered by : Trupti 2/27/2014 Gurav 6 Cohen-Sutherland: World Division 1001 1000 1010 0001

Such general routine can be then used to clip along an arbitrary convex polygon.

Fig :A window with 6 clipping boundaries

SKNCOE Preapered by : Trupti Gurav 23 2/27/2014

Page 24: WINDOWING AND CLIPPING - WordPress.com...COHEN SUTHERLAND LINE CLIPPING ALGORITHM SKNCOE Preapered by : Trupti 2/27/2014 Gurav 6 Cohen-Sutherland: World Division 1001 1000 1010 0001

Position relative to an arbitrary line

A line divides the plane into two half planes.

Suppose we have a line specified by the points(x1,y1) and (x2,y2) then

For point (x,y) Case 1 : (x-x2) (y1-y2) = (y –y2) (x1-x2)

Means point (x,y) is on the line

Case 2: (x-x2) (y1-y2) > (y –y2) (x1-x2)

Point lies on one side

Case 3: (x-x2) (y1-y2) < (y –y2) (x1-x2)

Point lies on the other side

Thus this test may be used in a clipping algorithm to determine if a point lies inside or outside an arbitrary boundary line.

< > =

Fig: Deciding on which side of line a point lies

Boundary

SKNCOE Preapered by : Trupti Gurav 24 2/27/2014

Page 25: WINDOWING AND CLIPPING - WordPress.com...COHEN SUTHERLAND LINE CLIPPING ALGORITHM SKNCOE Preapered by : Trupti 2/27/2014 Gurav 6 Cohen-Sutherland: World Division 1001 1000 1010 0001

Normalization Transformation

Different display devices have different screen sizes and resolutions, which is measured in pixels.

Picture on a screen with high resolution appears small in size, where as same pic on screen with less resolution appears big in size.

To avoid this we make our program device independent.

So picture coordinates have to be represented in some units other than pixels.

Device independent units are called as Normalized device coordinates.

In this screen measures 1 unit wide and 1 unit length.

SKNCOE Preapered by : Trupti Gurav 25 2/27/2014

Page 26: WINDOWING AND CLIPPING - WordPress.com...COHEN SUTHERLAND LINE CLIPPING ALGORITHM SKNCOE Preapered by : Trupti 2/27/2014 Gurav 6 Cohen-Sutherland: World Division 1001 1000 1010 0001

Interpreter is used to normalized co-ordinates into device co-ordinates. A simple linear formula is used for this.

(0,0) (1,0)

(1,1) (0,1)

SKNCOE Preapered by : Trupti Gurav 26 2/27/2014

Page 27: WINDOWING AND CLIPPING - WordPress.com...COHEN SUTHERLAND LINE CLIPPING ALGORITHM SKNCOE Preapered by : Trupti 2/27/2014 Gurav 6 Cohen-Sutherland: World Division 1001 1000 1010 0001

x = xn * Xw

y = yn * Yw

Where : x= actual device x co-or

y= actual device y co-or

xn = Normalized x co-or

yn = Normalized y co-or

Xw =Width of actual screen in Pixels

Yw = Height of actual screen in Pixels

SKNCOE Preapered by : Trupti Gurav 27 2/27/2014

Page 28: WINDOWING AND CLIPPING - WordPress.com...COHEN SUTHERLAND LINE CLIPPING ALGORITHM SKNCOE Preapered by : Trupti 2/27/2014 Gurav 6 Cohen-Sutherland: World Division 1001 1000 1010 0001

Viewing Transformation

Generally, an image is defined in a world coordinate system, WC.

• World Coordinate system: application-specific

• Example: drawing dimensions may be in meters, km, feet, etc.

But when picture is represented on display device,it is measured in physical device coordinate system corresponding to the display device.

SKNCOE Preapered by : Trupti Gurav 28 2/27/2014

Page 29: WINDOWING AND CLIPPING - WordPress.com...COHEN SUTHERLAND LINE CLIPPING ALGORITHM SKNCOE Preapered by : Trupti 2/27/2014 Gurav 6 Cohen-Sutherland: World Division 1001 1000 1010 0001

Definition: World Window

•World Window: rectangular region of drawing (in world coordinates) to be drawn (it specifies what to display)

•Defined by W.L, W.R, W.B, W.T

W.L W.R

W.B

W.T

SKNCOE Preapered by : Trupti Gurav 29 2/27/2014

Page 30: WINDOWING AND CLIPPING - WordPress.com...COHEN SUTHERLAND LINE CLIPPING ALGORITHM SKNCOE Preapered by : Trupti 2/27/2014 Gurav 6 Cohen-Sutherland: World Division 1001 1000 1010 0001

Definition: Viewport

•Rectangular region in the screen used to display drawing(it specifies where to display)

•Defined in screen coordinate system

V.L V.R

V.B

V.T

SKNCOE Preapered by : Trupti Gurav 30 2/27/2014

Page 31: WINDOWING AND CLIPPING - WordPress.com...COHEN SUTHERLAND LINE CLIPPING ALGORITHM SKNCOE Preapered by : Trupti 2/27/2014 Gurav 6 Cohen-Sutherland: World Division 1001 1000 1010 0001

Window to Viewport Mapping

Would like to:

Specify drawing in world coordinates

Display in screen coordinates

Need some sort of mapping

Called Window-to-viewport mapping

Basic W-to-V mapping steps:

Define a world window

Define a viewport

Compute a mapping from window to viewport

SKNCOE Preapered by : Trupti Gurav 31 2/27/2014

Page 32: WINDOWING AND CLIPPING - WordPress.com...COHEN SUTHERLAND LINE CLIPPING ALGORITHM SKNCOE Preapered by : Trupti 2/27/2014 Gurav 6 Cohen-Sutherland: World Division 1001 1000 1010 0001

Window to Viewport Mapping

You are given:

World Window: W.R, W.L, W.T, W.B

Viewport: V.L, V.R, V.B, V.T

A point (x,y) in the world

Required: Calculate corresponding point (s.x, s.y) in screen coordinates

This window to viewport transformation is called as workstation transformation.

SKNCOE Preapered by : Trupti Gurav 32 2/27/2014

Page 33: WINDOWING AND CLIPPING - WordPress.com...COHEN SUTHERLAND LINE CLIPPING ALGORITHM SKNCOE Preapered by : Trupti 2/27/2014 Gurav 6 Cohen-Sutherland: World Division 1001 1000 1010 0001

Window to Viewport Mapping

How is window-to-viewport mapping done?

Window-to-Viewport mapping is done by performing following steps:

1. The object together its window is translated until lower left corner of the window is at the origin.

2. Object and window are scaled until the window has the dimensions of the viewport.

3. Translate the viewport to its correct position on the screen.

DC Translate Scale Retranslate

Figure : Viewing Transformation

SKNCOE Preapered by : Trupti Gurav 33 2/27/2014

Page 34: WINDOWING AND CLIPPING - WordPress.com...COHEN SUTHERLAND LINE CLIPPING ALGORITHM SKNCOE Preapered by : Trupti 2/27/2014 Gurav 6 Cohen-Sutherland: World Division 1001 1000 1010 0001

FIG: A WINDOW TO VIEW ONLY PART OF THE OBJECT

FIG: A VIEWPORT TO DEFINE THE PART OF THE SCREEN TO BE USED

SKNCOE Preapered by : Trupti Gurav 34 2/27/2014

Page 35: WINDOWING AND CLIPPING - WordPress.com...COHEN SUTHERLAND LINE CLIPPING ALGORITHM SKNCOE Preapered by : Trupti 2/27/2014 Gurav 6 Cohen-Sutherland: World Division 1001 1000 1010 0001

STEPS IN VIEWING TRANSFORMATION

OBJECT

WINDOW

TRANSLATE

TRANSLATE

SCALE

STEP - 1

STEP -2

STEP -3 STEP- 4

SKNCOE Preapered by : Trupti Gurav 35 2/27/2014

Page 36: WINDOWING AND CLIPPING - WordPress.com...COHEN SUTHERLAND LINE CLIPPING ALGORITHM SKNCOE Preapered by : Trupti 2/27/2014 Gurav 6 Cohen-Sutherland: World Division 1001 1000 1010 0001

Matrices

W = T* S* T-1

T=

S = Sx=

SY =

1 0 0

0 1 0 -Xwmin - Ywmin 1

SX 0 0

0 SY 0

0 0 1

Xvmax- Xvmin

xwmax- Xwmin

Yvmax-Yvmin

Ywmax- Ywmin

1 0 0

0 1 0

Xvmin Yvmin 1 T-1=

SKNCOE Preapered by : Trupti Gurav 36 2/27/2014

Page 37: WINDOWING AND CLIPPING - WordPress.com...COHEN SUTHERLAND LINE CLIPPING ALGORITHM SKNCOE Preapered by : Trupti 2/27/2014 Gurav 6 Cohen-Sutherland: World Division 1001 1000 1010 0001

=

Applications of Viewing transformation:

1. By changing the position of the viewport we can view objects at different locations of display area.

2. We can achieve the zooming effects.

Sx 0 1

0 Sy 0

Xvmin -Xwmi .Sx Yvmin -Ywmin . Sy 1

SKNCOE Preapered by : Trupti Gurav 37 2/27/2014

Page 38: WINDOWING AND CLIPPING - WordPress.com...COHEN SUTHERLAND LINE CLIPPING ALGORITHM SKNCOE Preapered by : Trupti 2/27/2014 Gurav 6 Cohen-Sutherland: World Division 1001 1000 1010 0001

Example

WINDOW: left-lower corner (3,0)

right –upper corner(5,4)

Viewport : left-lower corner (0.5,1)

right –upper corner(0.5,1)

1. First translation matrix would be :

2. Computing the sx value : Sx = (Xvmax- Xvmin / xwmax- Xwmin )

Length of the window in x-direction is (5-3) = 2

Length of viewport in x-direction is (1.0-0.5)= 0.5

Therefore scaling factor is 0.5/2 = 0.25

1 0 0

0 1 0

-3 0 1

SKNCOE Preapered by : Trupti Gurav 38 2/27/2014

Page 39: WINDOWING AND CLIPPING - WordPress.com...COHEN SUTHERLAND LINE CLIPPING ALGORITHM SKNCOE Preapered by : Trupti 2/27/2014 Gurav 6 Cohen-Sutherland: World Division 1001 1000 1010 0001

Computing Sy =(Yvmax-Yvmin / Ywmax- Ywmin )

Sy = 0.5 /4 = 0.125

The scaling matrix will be :

Finally to position the viewport requires a translation of

0.25 0 0

0 0.125 0

0 0 1

1 0 0

0 1 0

0.5 0.5 1

SKNCOE Preapered by : Trupti Gurav 39 2/27/2014