• No results found

Line and Polygon Clipping. Foley & Van Dam, Chapter 3

N/A
N/A
Protected

Academic year: 2021

Share "Line and Polygon Clipping. Foley & Van Dam, Chapter 3"

Copied!
28
0
0

Loading.... (view fulltext now)

Full text

(1)

Line and Polygon Clipping

Foley & Van Dam, Chapter 3

(2)

Topics

• Viewing Transformation Pipeline in 2D

• Line and polygon clipping

• Brute force analytic solution

• Cohen-Sutherland Line Clipping Algorithm

• Cyrus-Beck Line Clipping Algorithm

• Sutherland-Hodgman Polygon Clipping

• Sampling Theorem (Nyquist Frequency)

(3)

Viewing Transformation in 2D

x world y world

x view y view

World and Viewing Coordinates

Normalized Device Coordinates

1 1

x view y view

Clipping

Device Coordinates

(4)

Viewing Transformation in 2D

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

• Objects are given in world coordinates

• The world is viewed through a window

• The window is mapped onto a device viewport

(5)

Line and Polygon Clipping

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

(6)

Motivations

• Efficiency

• Display in portion of a screen

• Occlusions

clip rectangle

(7)

Line Clipping

• We will deal only with lines (segments)

• Our window can be described by two extreme points:

(x

min

,y

min

) and (x

max

,y

max

)

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

x

min

≤ x ≤ x

max

and y

min

≤ y ≤ y

max

(8)

Brute Force Analytic Solution

• The intersection of convex regions is always convex

• 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

(9)

Pseudo Code for Midpoint Line Drawing

Assume x1>x0 and 0 < slope ≤ 1

Line(x0,y0,x1,y1) begin

int dx, dy, x, y, d, E, ΝE ; x:= x0; y=y0;

dx := x1-x0; dy := y1-y0; d := 2*dy-dx;

E := 2*dy; ΝΕ:= 2*(dy-dx);

PlotPixel(x,y);

while(x < x1) do if (d < 0) then

d:=d+ E; x:=x+1;

end;

else

d:=d+ ΝE; x:=x+1;

y:=y+1;

end;

PlotPixel(x,y);

end;

end;

(10)

Line Clipping

Q E M NE

y = ymin x = xmin

(xmin,Round(mxmin+B)) (xmin, mxmin+B)

Midpoint Algorithm: Intersection with a vertical edge

(11)

Line Clipping

Midpoint Algorithm: Intersection with a horizontal edge

A

B y = ymin

x = xmin

y = ymin-1/2

(12)

Cohen-Sutherland for Line Clipping

• Clipping is performed by computing 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. The bit is 1 if the pixel is in the half-plane that does not contain W

(13)

Cohen-Sutherland for Line Clipping

bit 1 0

12 3

y < ymin y > ymax x > xmax

y ≥ ymin y ≤ ymax x ≤ xmax 4 x < xmin x ≥ xmin

0101 0001

0110 0100

1001

0010 1010 0000

ymin 1000 ymax

xmin xmax

1 3 2

4

(14)

Cohen-Sutherland for Line Clipping

Given a line segment S from p

0

=(x

0

,y

0

) to p

1

=(x

1

,y

1

) to be clipped against a window W

If code(p

0

) AND code(p

1

) is not zero, then S is trivially rejected

If code(p

0

) OR code(p

1

) is zero, then S is trivially accepted

0101 0001

0110 0100

1001

0010 1010 0000

1000

(15)

Cohen-Sutherland for Line Clipping

Otherwise: let assume w.l.o.g. that p

0

is outside W

• Find the intersection of S with the edge

corresponding to the MSB in code(p

0

) that is equal to 1. Call the intersection point p

2

.

• Run the procedure for the new segment (p

1

, p

2

).

A C B

D

E

F

GH I

1 2 3

4

(16)

Cyrus-Beck Line Clipping

V0 V1

inside

N U

Inside/Outside Test:

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

"inside" is to its right

• If V=(Vx,Vy), N is the normal to V, pointing outside, defined by N=(-Vy,Vx)

• Vector U points "outside" if N·U > 0

• Otherwise U points "inside"

(17)

Cyrus-Beck Line Clipping

P0

inside

N Q

P1 V(t)

L

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)

∆=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

(18)

Cyrus-Beck Line Clipping

p0

p1 Ni Qi

• Denote p(t)=p

0

+(p

1

-p

0

)t t∈[0..1]

• Let Q

i

be a point on the edge L

i

with outside pointing normal N

i

• V(t) = p(t)-Q

i

is a parameterized vector from Q

i

to the segment P(t)

• N

i

· V(t) = 0 iff V(t) ⊥ N

i

• We are looking for t satisfying N

i

· V(t) = 0

(19)

Cyrus-Beck Line Clipping

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 (V(t) ⊥ Ni) Ni·(p0-Qi)

-Ni·(p1-p0)

t = Ni·(p0-Qi)

-Ni·∆

=

(20)

Cyrus-Beck Line Clipping

p1

p0

PL

PE

p0

p1

PE

PE

PL PL

p1 PL

PE p0

(21)

Cyrus-Beck Line Clipping

• The intersection of p(t) with all four edges L

i

is computed, resulting in up to four t

i

values

• If t

i

<0 or t

i

>1 , t

i

can be discarded

• Based on the sign of N

i

·∆, 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

(22)

Sutherland-Hodgman

Polygon-Clipping Algorithm

(23)

Sutherland-Hodgman

Polygon-Clipping Algorithm

right clip boundary bottom clip boundary

left clip boundary top clip boundary

Idea: Clip a polygon by successively clipping against each (infinite) clip edge

After each clipping a new set of vertices is produced.

(24)

Sutherland-Hodgman

Polygon-Clipping Algorithm

clip boundary inside outside

s

clip boundary inside outside

s p

clip boundary inside outside

p i s

s clip

boundary inside outside

i p

p added to

output list i added to

output list no output i and p added to output list

p

For each clip edge - scan the polygon and consider the relation between successive vertices of the polygon

Each iteration adds 0, 1 or 2 new vertices

Assume vertex s has been dealt with, vertex p follows:

(25)

Sutherland-Hodgman

Polygon-Clipping Algorithm

Window V3

V'2

V”2

V1 V’3

V2

V’1

Left Clipping

Right Clipping

Bottom Clipping

Top Clipping V1

V2 V3

V1 V2 V’2 V’3

V1 V2 V’2 V’3

V’1 V2 V’2 V”2

(26)

Sampling Theorem

Question: How dense should be the pixel grid in order to draw properly a drawn object?

Given a sampling at intervals equal to d then one may recover frequencies of wavelength > 2d

Aliasing: If the sampling interval is more than 1/2 the wavelength, erroneous frequencies may be

produced

(27)

Sampling Theorem

1D Example:

0 π 2π 3π 4π 5π 6π

Rule of Thumb: To observe details of size

d

one must sample at

d/2

intervals

To observe details at frequency

f

(=1/d) one must sample at frequency

2f

. The Frequency

2f

is the NYQUIST frequency

(28)

Sampling Theorem

2D Example: Moire’ Effect

References

Related documents

Data were collected for two seasons All the spray distribution tests were done during the sprayer was working in the paddy field by measuring the nozzle height, nozzle

But with the rapid development of the Internet industry, Internet users are surging, they got many methods to get access to information, the portal market gradually saturated,

STEP _USER_ID NUMBER NOT NULL PRIMARY KEY,.. Colla boration Porta l fo r Rese a rchers at E I U.

In the article, the RF algorithm is applied to the combined prediction of short-term wind power [21,22], and an improved RF model based on the two-stage

We have looked at recent developments on the fiscal policy front, which emanate from an attempt to change a number of relevant assumptions. Most important of these assumptions are

Draft – This is the status during the preparation or drafting of an IECEx CoC by the ExCB. Only the ExCB that is creating the Draft and the On-Line System Administrator may view Draft

Insurers can leverage data from connected home devices to assess and mitigate risk, increase pricing sophistication, and offer new products, all of which help drive

under this Limited Warranty shall not exceed the lesser of: the Final Sales Price of the Home as listed on the Application For Warranty form or as other- wise provided to the