2D Viewing - BIUu.cs.biu.ac.il/~kapaho/CG/05_view2d.pdf · CG-IDC 3 • Objects are given in world...

24
CG-IDC 1 2D Viewing Chapt. 3 in FVD, Chapt. 6 in HB Viewing Transformation pipe-line. Clipping: Line Clipping Polygon Clipping

Transcript of 2D Viewing - BIUu.cs.biu.ac.il/~kapaho/CG/05_view2d.pdf · CG-IDC 3 • Objects are given in world...

Page 1: 2D Viewing - BIUu.cs.biu.ac.il/~kapaho/CG/05_view2d.pdf · CG-IDC 3 • Objects are given in world coordinates. • The world is viewed through a window. • The window is mapped

CG-IDC 1

2D ViewingChapt. 3 in FVD, Chapt. 6 in HB

Viewing Transformation pipe-line.

Clipping:

Line Clipping

Polygon Clipping

Page 2: 2D Viewing - BIUu.cs.biu.ac.il/~kapaho/CG/05_view2d.pdf · CG-IDC 3 • Objects are given in world coordinates. • The world is viewed through a window. • The window is mapped

CG-IDC 2

3D Viewing Transformation Pipeline

Device Coordinates

Viewport

World CoordinatesObject in World

Viewing coordinates

2D:2D mapping

Page 3: 2D Viewing - BIUu.cs.biu.ac.il/~kapaho/CG/05_view2d.pdf · CG-IDC 3 • Objects are given in world coordinates. • The world is viewed through a window. • The window is mapped

CG-IDC 3

• Objects are given in world coordinates.

• The world is viewed through a window.

• The window is mapped onto a device

viewport.

A scene in World-

Coordinates

World-Coordinates to

Viewing Coordinates

Viewing Coordinates to

Normalized Device

Coordinates

Normalized Device

Coordinates to Device

Coordinates

WC

VC

NDC

DC

Clipping

Page 4: 2D Viewing - BIUu.cs.biu.ac.il/~kapaho/CG/05_view2d.pdf · CG-IDC 3 • Objects are given in world coordinates. • The world is viewed through a window. • The window is mapped

CG-IDC 4

Viewing Trans. in 2D

x world

y world

World and Viewing

Coordinates

Normalized Device

Coordinates

1

1

Clipping

Device Coordinates

Page 5: 2D Viewing - BIUu.cs.biu.ac.il/~kapaho/CG/05_view2d.pdf · CG-IDC 3 • Objects are given in world coordinates. • The world is viewed through a window. • The window is mapped

CG-IDC 5

World to Viewing

Coordinates

• In order to define the viewing window

we have to specify:

– Windowing-coordinate origin P0=(x0,y0).

– View vector up v=(vx,vy).

• Using v, we can find u: u=v x (0,0,1)

• Transformation from World coordinates

to Viewing coordinates is:

x world

y world

(x0,y0)

(vx,vy).

(ux,uy).

100

10

01

100

0

0

0

0

y

x

vv

uu

M yx

yx

vcwc

Page 6: 2D Viewing - BIUu.cs.biu.ac.il/~kapaho/CG/05_view2d.pdf · CG-IDC 3 • Objects are given in world coordinates. • The world is viewed through a window. • The window is mapped

CG-IDC 6

Window to Viewport

Transformation

(xmin,ymin)

(xmax,ymax)

Window is Viewing

coordinatesWindow translated

to origin

Window scaled to

Normalized

Viewport size

Window scaled and

translated to Viewport

location in device

coordinates

(umin,vmin)

(umaxn,vmaxn)

100

10

01

100

010

001

100

00

00

100

10

01

min

min

minmax

minmax

minmax

minmax

min

min

y

x

yy

xx

vv

uu

v

u

M dcvc

Normalized Device Coord.

Page 7: 2D Viewing - BIUu.cs.biu.ac.il/~kapaho/CG/05_view2d.pdf · CG-IDC 3 • Objects are given in world coordinates. • The world is viewed through a window. • The window is mapped

CG-IDC 7

The problem:

Given a set of 2D lines or

polygons and a window, clip the

lines or polygons to their regions

that are inside the window.

Line / Polygon Clipping(Chapt 6 in HB, Chapt. 3 in FVD)

Page 8: 2D Viewing - BIUu.cs.biu.ac.il/~kapaho/CG/05_view2d.pdf · CG-IDC 3 • Objects are given in world coordinates. • The world is viewed through a window. • The window is mapped

CG-IDC 8

Motivation

• Efficiency.

• Display in portion of a screen.

• Occlusions.

clip rectangle

Page 9: 2D Viewing - BIUu.cs.biu.ac.il/~kapaho/CG/05_view2d.pdf · CG-IDC 3 • Objects are given in world coordinates. • The world is viewed through a window. • The window is mapped

CG-IDC 9

Clipping (cont.)

• We will deal only with lines

(segments).

• Our window can be described by

two extreme points:

(xmin,ymin) and (xmax,ymax)

• A point (x,y) is in the window iff:

xmin x xmax and ymin y ymax

Page 10: 2D Viewing - BIUu.cs.biu.ac.il/~kapaho/CG/05_view2d.pdf · CG-IDC 3 • Objects are given in world coordinates. • The world is viewed through a window. • The window is mapped

CG-IDC 10

Analytic Solution

• The intersection of two convex regions is

always convex (why?).

• Since both W and S are convex, their

intersection is convex, i.e a single

connected segment of S.

• Question: Can the boundary of two

convex shapes intersect more than twice?

0, 1, or 2 intersections between a line and a window

W W W

S S S

Page 11: 2D Viewing - BIUu.cs.biu.ac.il/~kapaho/CG/05_view2d.pdf · CG-IDC 3 • Objects are given in world coordinates. • The world is viewed through a window. • The window is mapped

CG-IDC 11

Cohen-Sutherland for Line

Clipping

• Clipping is performed by the

computation of the intersections with

four boundary segments of the window:

Li, i=1,2,3,4

• Purpose: Fast treatment of lines that are

trivially inside/outside the window.

• Let P=(x,y) be a point to be classified

against window W.

• Idea: Assign P a binary code consisting

of a bit for each edge of W, whose value

is determined according to the following

table:

Page 12: 2D Viewing - BIUu.cs.biu.ac.il/~kapaho/CG/05_view2d.pdf · CG-IDC 3 • Objects are given in world coordinates. • The world is viewed through a window. • The window is mapped

CG-IDC 12

bit 1 0

1

2

3

y < ymin

y > ymax

x > xmax

y ymin

y ymax

x xmax

4 x < xmin x xmin

0101

0001

01100100

1001

0010

1010

0000

1000

ymin

ymax

xmin xmax

1

2

34

Page 13: 2D Viewing - BIUu.cs.biu.ac.il/~kapaho/CG/05_view2d.pdf · CG-IDC 3 • Objects are given in world coordinates. • The world is viewed through a window. • The window is mapped

CG-IDC 13

Cohen-Sutherland (cont.)

• Given a line segment S from p0=(x0,y0)

to p1=(x1,y1) to be clipped against a

window W.

• If code(p0) AND code(p1) is not zero -

then S is trivially rejected.

• If code(p0) OR code(p1) is zero - then

S is trivially accepted.

0101

0001

01100100

1001

0010

1010

0000

1000

Page 14: 2D Viewing - BIUu.cs.biu.ac.il/~kapaho/CG/05_view2d.pdf · CG-IDC 3 • Objects are given in world coordinates. • The world is viewed through a window. • The window is mapped

CG-IDC 14

• Otherwise: let assume w.l.o.g. that p0 is

outside the window W.

– Find the intersection of S with the

edge corresponding to the MSB in

code(p0) that equal to 1. Call the

intersection point p2.

– Rerun the procedure for the new

segment (p1 , p2).

A

BC

D

E

F

GH

I

1

2

34

Page 15: 2D Viewing - BIUu.cs.biu.ac.il/~kapaho/CG/05_view2d.pdf · CG-IDC 3 • Objects are given in world coordinates. • The world is viewed through a window. • The window is mapped

CG-IDC 15

V0

V1

Background: Inside/Outside Test

inside

• Assume WLOG that V=(V1-V0) is the

border vector where "inside" is to its

right.

• If V=(Vx,Vy), N is a vector pointing

outside, where we define:

N=(-Vy,Vx)

• Vector U points "outside" if

• Otherwise U points "inside".

N

U

0UN

Page 16: 2D Viewing - BIUu.cs.biu.ac.il/~kapaho/CG/05_view2d.pdf · CG-IDC 3 • Objects are given in world coordinates. • The world is viewed through a window. • The window is mapped

CG-IDC 16

Background: Segment-Line Intersection

• The parametric line P(t)=P0+(P1-P0)t

• The parametric vector V(t)=P(t)-Q

• The segment P0P1 intersects the line L at t0

satisfying V(t0)·N=0.

• The intersection point is P(t0).

• The vector =P1-P0 points "inside" if (P1-

P0)·N<0. Otherwise it points "outside".

• If L is vertical, intersection can be

computed using the explicit equation.

P0

inside

NQ

P1V(t)

L

Page 17: 2D Viewing - BIUu.cs.biu.ac.il/~kapaho/CG/05_view2d.pdf · CG-IDC 3 • Objects are given in world coordinates. • The world is viewed through a window. • The window is mapped

CG-IDC 17

Cyrus-Beck Line Clipping

• Denote p(t)=p0+(p1-p0)t t[0..1]

• Let Qi be a point on the edge Li with

outside pointing normal Ni.

• V(t) = p(t)-Qi is a parameterized

vector from Qi to the segment P(t).

• Ni· V(t) = 0 iff V(t) Ni

• We are looking for t satisfying the

above equation:

p0

p1

Ni Qi

Page 18: 2D Viewing - BIUu.cs.biu.ac.il/~kapaho/CG/05_view2d.pdf · CG-IDC 3 • Objects are given in world coordinates. • The world is viewed through a window. • The window is mapped

CG-IDC 18

Cyrus-Beck Clipping (cont.)

0 = Ni· V(t)

= Ni· (p(t)-Qi)

= Ni· (p0+(p1-p0)t-Qi)

= Ni· (p0-Qi) + Ni· (p1-p0)t

Solving for t we get:

where =(p1-p0)

• Comment: If Ni· =0, t has no

solution. However, in this case V(t) Ni

and there is no intersection.

Ni· (p0-Qi)

-Ni· (p1-p0)t =

Ni· (p0-Qi)

-Ni· =

Page 19: 2D Viewing - BIUu.cs.biu.ac.il/~kapaho/CG/05_view2d.pdf · CG-IDC 3 • Objects are given in world coordinates. • The world is viewed through a window. • The window is mapped

CG-IDC 19

Cyrus-Beck Algorithm:

• The intersection of p(t) with all four

edges Li is computed, resulting in up to

four ti values.

• If ti<0 or ti>1 , ti can be discarded.

• Based on the sign of Ni· , each

intersection point is classified as PE

(potentially entering) or PL

(potentially leaving).

• PE with the largest t and PL with the

smallest t provide the domain of p(t)

inside W.

• The domain, if inverted, signals that

p(t) is totally outside.

Page 20: 2D Viewing - BIUu.cs.biu.ac.il/~kapaho/CG/05_view2d.pdf · CG-IDC 3 • Objects are given in world coordinates. • The world is viewed through a window. • The window is mapped

CG-IDC 20

p1

p0

PL

PE

p0

p1

PE

PE

PL

PL

p1

PL

PEp0

Page 21: 2D Viewing - BIUu.cs.biu.ac.il/~kapaho/CG/05_view2d.pdf · CG-IDC 3 • Objects are given in world coordinates. • The world is viewed through a window. • The window is mapped

CG-IDC 21

Polygon Clipping

Page 22: 2D Viewing - BIUu.cs.biu.ac.il/~kapaho/CG/05_view2d.pdf · CG-IDC 3 • Objects are given in world coordinates. • The world is viewed through a window. • The window is mapped

CG-IDC 22

Sutherland-Hodgman

Polygon-Clipping Algorithm

Clip a polygon by successively clipping

against each (infinite) clip edge.

After each clipping a new set of vertices is

produced.

right clip boundary bottom clip boundary

left clip boundary top clip boundary

Page 23: 2D Viewing - BIUu.cs.biu.ac.il/~kapaho/CG/05_view2d.pdf · CG-IDC 3 • Objects are given in world coordinates. • The world is viewed through a window. • The window is mapped

CG-IDC 23

For each clip edge - scan the polygon and

consider the relation between successive

vertices of the polygon:

Assume vertex s has been dealt with, vertex

p follows:

clip boundary

inside outsides

p

clip boundary

inside outside

s

p

clip boundary

inside outsidep si

s

clip boundary

inside outside

pi

p added tooutput list

i added tooutput list no output i and p added to

output list

Page 24: 2D Viewing - BIUu.cs.biu.ac.il/~kapaho/CG/05_view2d.pdf · CG-IDC 3 • Objects are given in world coordinates. • The world is viewed through a window. • The window is mapped

CG-IDC 24

Window

V3

V'2

V”2

V1

V’3

V2

V’1

Right

Clipping

Bottom

Clipping

Top

Clipping

Left

Clipping

V1

V2

V3

V1

V2

V’2

V’3

Example:

V1

V2

V’2

V’3

V’1

V2

V’2

V”2

V’1

V2

V’2

V”2