2d viewing - piazza

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

Upload: others

Post on 18-Dec-2021

4 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: 2D Viewing - Piazza

CG-IDC 1

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

Viewing Transformation pipe-line.Clipping:

Line ClippingPolygon Clipping

Page 2: 2D Viewing - Piazza

CG-IDC 2

3D Viewing Transformation Pipeline

World Coordinates Object in World

Viewing coordinates

3D:2D mapping

Device Coordinates

Viewport

2D:2D mapping

Page 3: 2D Viewing - Piazza

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 - Piazza

CG-IDC 4

Viewing Trans. in 2D

x world

y world

u

v

World and Viewing Coordinates

Normalized Device Coordinates

1

1

u

v

Clipping

Device Coordinates

Page 5: 2D Viewing - Piazza

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

u

v

(x0,y0)

(vx,vy).

(ux,uy).

÷÷÷

ø

ö

ççç

è

æ--

÷÷÷

ø

ö

ççç

è

æ=-

1001001

10000

0

0yx

vvuu

M yx

yx

vcwc

Page 6: 2D Viewing - Piazza

CG-IDC 6

Window to Viewport Transformation

(xmin,ymin)

(xmax,ymax)

Window is Viewing coordinates

Window translated to origin

Window scaled to Normalized

Viewport size

Window scaled and translated to Viewport

location in device coordinates

(umin,vmin)

(umaxn,vmaxn)

÷÷÷

ø

ö

ççç

è

æ--

÷÷÷÷÷÷

ø

ö

çççççç

è

æ

-

-

÷÷÷

ø

ö

ççç

è

æ-

-

÷÷÷

ø

ö

ççç

è

æ=-

1001001

100

010

001

1000000

1001001

min

min

minmax

minmax

minmax

minmax

min

min

yx

yy

xx

vvuu

vu

M dcvc

Normalized Device Coord.

Page 7: 2D Viewing - Piazza

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 - Piazza

CG-IDC 8

Motivation

• Efficiency.• Display in portion of a screen.• Occlusions.

clip rectangle

Page 9: 2D Viewing - Piazza

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

• Is it true for tilted window as well?

Page 10: 2D Viewing - Piazza

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 - Piazza

CG-IDC 11

Cohen-Sutherland Line Clipping

Page 12: 2D Viewing - Piazza

CG-IDC 12

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 13: 2D Viewing - Piazza

CG-IDC 13

bit 1 0

123

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 14: 2D Viewing - Piazza

CG-IDC 14

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 15: 2D Viewing - Piazza

CG-IDC 15

• 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

EF

GH

I

1

2

34

Page 16: 2D Viewing - Piazza

CG-IDC 16

Cyrus-Beck Line Clipping

Page 17: 2D Viewing - Piazza

CG-IDC 17

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=(-Vy,Vx) is a mormal to V pointing “outside”.

• Vector U points "outside" if

• Otherwise U points "inside".

N

U

0>×UN

Page 18: 2D Viewing - Piazza

CG-IDC 18

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 D=P1-P0 points "inside" if D·N<0. Otherwise it points "outside".

P0

inside

N Q

P1V(t)

L

Page 19: 2D Viewing - Piazza

CG-IDC 19

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 20: 2D Viewing - Piazza

CG-IDC 20

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 D=(P1-P0)

• Comment: If Ni· D=0, t has no solution. However, in this case V(t) ^ Ni

and there is no defined intersections.

Ni· (P0-Qi) -Ni· (P1-P0)

t =Ni· (P0-Qi)

-Ni· D=

Page 21: 2D Viewing - Piazza

CG-IDC 21

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· D, 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 22: 2D Viewing - Piazza

CG-IDC 22

p1

p0

PL

PE

p0

p1

PEPE

PL

PL

p1PL

PEp0

Page 23: 2D Viewing - Piazza

CG-IDC 23

Polygon Clipping

Page 24: 2D Viewing - Piazza

CG-IDC 24

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 25: 2D Viewing - Piazza

CG-IDC 25

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

pclip boundary

inside outside

s

p

clip boundary

inside outsidep si

sclip boundary

inside outside

pi

p added tooutput list

i added tooutput list no output i and p added to

output list

Page 26: 2D Viewing - Piazza

CG-IDC 26

Window

V3

V'2

V”2

V1

V’3

V2

V’1

RightClipping

BottomClipping

TopClipping

LeftClipping

V1V2V3

V1V2V’2V’3

Example:

V1V2V’2V’3

V’1V2V’2V”2

V’1V2V’2V”2