54
Einführung in Visual Computing 186.822 Clipping Werner Purgathofer

Clipping - TU Wien · position with respect to the clipping rectangle. Cohen-Sutherland Line Clipping. 1001 1000 1010 0001 0000 0010. 0101 0100 0110. Werner Purgathofer 10 “or”

  • Upload
    others

  • View
    3

  • Download
    0

Embed Size (px)

Citation preview

Page 1: Clipping - TU Wien · position with respect to the clipping rectangle. Cohen-Sutherland Line Clipping. 1001 1000 1010 0001 0000 0010. 0101 0100 0110. Werner Purgathofer 10 “or”

Einführung in Visual Computing186.822

Clipping

Werner Purgathofer

Page 2: Clipping - TU Wien · position with respect to the clipping rectangle. Cohen-Sutherland Line Clipping. 1001 1000 1010 0001 0000 0010. 0101 0100 0110. Werner Purgathofer 10 “or”

Viewing in the Rendering Pipeline

Werner Purgathofer 2raster image in pixel coordinates

clipping + homogenization

object capture/creation

modelingviewing

projection

rasterizationviewport transformation

shading

vertex stage(„vertex shader“)

pixel stage(„fragment shader“)

scene in normalized device coordinates

transformed vertices in clip space

scene objects in object space

Page 3: Clipping - TU Wien · position with respect to the clipping rectangle. Cohen-Sutherland Line Clipping. 1001 1000 1010 0001 0000 0010. 0101 0100 0110. Werner Purgathofer 10 “or”

Werner Purgathofer 3

line clippingpolygon clippingtriangle clipping

Overview: Clipping

Page 4: Clipping - TU Wien · position with respect to the clipping rectangle. Cohen-Sutherland Line Clipping. 1001 1000 1010 0001 0000 0010. 0101 0100 0110. Werner Purgathofer 10 “or”

Werner Purgathofer 4

partly visible or completely invisible partsmust not be ignored and must not be drawn

.

ignored?:

vertices edges

scrolled?:

Clipping

⇒ must be cut off (as early as possible)

Page 5: Clipping - TU Wien · position with respect to the clipping rectangle. Cohen-Sutherland Line Clipping. 1001 1000 1010 0001 0000 0010. 0101 0100 0110. Werner Purgathofer 10 “or”

Werner Purgathofer 5

remove objects outside a clip windowclip window: rectangle, polygon, curved boundariesapplied somewhere in the viewing pipelinecan be combined with scan conversionobjects to clip: points, lines, triangles, polygons, curves, text, ...

Clipping Operations

Page 6: Clipping - TU Wien · position with respect to the clipping rectangle. Cohen-Sutherland Line Clipping. 1001 1000 1010 0001 0000 0010. 0101 0100 0110. Werner Purgathofer 10 “or”

Werner Purgathofer 6

analytically in world coordinatesreduces WC → DC transformations

analytically in clip coordinatessimple comparisons

during raster conversation= as part of the rasterization algorithm

may be efficient for complex primitives

3 Principle Possibilities for Clipping

Page 7: Clipping - TU Wien · position with respect to the clipping rectangle. Cohen-Sutherland Line Clipping. 1001 1000 1010 0001 0000 0010. 0101 0100 0110. Werner Purgathofer 10 “or”

Werner Purgathofer 7

[line clipping against a rectangular clip window]

before clipping after clipping

Line Clipping (1)

P1 P6

P7

P8

P10

P9

P5

P4

P3

P2

P1 P6

P2

P'5

P'8

P'7

Page 8: Clipping - TU Wien · position with respect to the clipping rectangle. Cohen-Sutherland Line Clipping. 1001 1000 1010 0001 0000 0010. 0101 0100 0110. Werner Purgathofer 10 “or”

Werner Purgathofer 8

goalseliminate simple cases fastavoid intersection calculations

for endpoints (x0,y0), (xend,yend)intersect parametric representation

x = x0 + u·(xend - x0)y = y0 + u·(yend - y0)

with window borders:intersection ⇔ 0 < u < 1

Line Clipping (2)

Page 9: Clipping - TU Wien · position with respect to the clipping rectangle. Cohen-Sutherland Line Clipping. 1001 1000 1010 0001 0000 0010. 0101 0100 0110. Werner Purgathofer 10 “or”

Window

assignment of region codes to line vertices

bit1: leftbit2: rightbit3: belowbit4: above

Werner Purgathofer 9

binary region codes assigned to line endpoints according to relative position with respect to the clipping rectangle

Cohen-Sutherland Line Clipping

1001 1000 1010

0001 0000 0010

0101 0100 0110

Page 10: Clipping - TU Wien · position with respect to the clipping rectangle. Cohen-Sutherland Line Clipping. 1001 1000 1010 0001 0000 0010. 0101 0100 0110. Werner Purgathofer 10 “or”

Werner Purgathofer 10

“or” of codes of both points= 0000 ⇒ line entirely visible

“and” of codes of both points≠ 0000 ⇒ line entirely invisible

all others ⇒ intersect!

Cohen-Sutherland Line Clipping

Window

1001 1000 1010

0001 0000 0010

0101 0100 0110

Page 11: Clipping - TU Wien · position with respect to the clipping rectangle. Cohen-Sutherland Line Clipping. 1001 1000 1010 0001 0000 0010. 0101 0100 0110. Werner Purgathofer 10 “or”

Werner Purgathofer 11

lines extending from one coordinate region to another may pass through the clip window, or they may intersect clipping boundaries without entering the window

Cohen-Sutherland Line Clipping

window

P2

P3

P1P4

P"2

P'1

P'2

P'3

Page 12: Clipping - TU Wien · position with respect to the clipping rectangle. Cohen-Sutherland Line Clipping. 1001 1000 1010 0001 0000 0010. 0101 0100 0110. Werner Purgathofer 10 “or”

remaining linesintersection test with bounding lines of clipping windowleft, right, bottom, topdiscard an outside partrepeat intersection test up to four times

(x0,y0)

xwmin

Werner Purgathofer 12

Cohen-Sutherland Line Clipping

vertical: y = y0 + m(xwmin – x0), y = y0 + m(xwmax – x0)horizontal: x = x0 + (ywmin – y0)/m, x = x0 + (ywmax – y0)/m

(xwmin,y)

(xwmin–x0)m(xwmin–x0)vertical: (x = xwmin)

y = y0 + m(xwmin – x0)

Page 13: Clipping - TU Wien · position with respect to the clipping rectangle. Cohen-Sutherland Line Clipping. 1001 1000 1010 0001 0000 0010. 0101 0100 0110. Werner Purgathofer 10 “or”

Werner Purgathofer 13

passes through clipping window

intersects boundaries without entering clipping window

Cohen-Sutherland Line Clipping

vertical: y = y0 + m(xwmin – x0), y = y0 + m(xwmax – x0)horizontal: x = x0 + (ywmin – y0)/m, x = x0 + (ywmax – y0)/m

window

P2

P1

P"2

P'1

P'2

P'3

P3

P4

Page 14: Clipping - TU Wien · position with respect to the clipping rectangle. Cohen-Sutherland Line Clipping. 1001 1000 1010 0001 0000 0010. 0101 0100 0110. Werner Purgathofer 10 “or”

Werner Purgathofer 14

modification of line clippinggoal: one or more closed areas

display of a correctly clipped polygon

display of a polygon processed by a line-clipping algorithm

Polygon Clipping

Page 15: Clipping - TU Wien · position with respect to the clipping rectangle. Cohen-Sutherland Line Clipping. 1001 1000 1010 0001 0000 0010. 0101 0100 0110. Werner Purgathofer 10 “or”

Werner Purgathofer 15

clipping a polygon against successive window boundaries

originalpolygon

clip left clip right clip bottom clip top

Sutherland-Hodgman Polygon Clipping

processing polygon boundary as a whole against each window edgeoutput: list of vertices

Page 16: Clipping - TU Wien · position with respect to the clipping rectangle. Cohen-Sutherland Line Clipping. 1001 1000 1010 0001 0000 0010. 0101 0100 0110. Werner Purgathofer 10 “or”

Werner Purgathofer 16

four possible edge cases

successive processing of pairs of polygon vertices against the left window boundary

out → inoutput: Vnew, V2

in → inV2

in → outVnew

out → outno output

Sutherland-Hodgman Polygon Clipping

V1

V2 V1

V2 V1

V2

V1V2Vnew

Vnew

Page 17: Clipping - TU Wien · position with respect to the clipping rectangle. Cohen-Sutherland Line Clipping. 1001 1000 1010 0001 0000 0010. 0101 0100 0110. Werner Purgathofer 10 “or”

Werner Purgathofer 17

V2:= 1st vertex

V1:=V2V2:=next vertex

V2 visible?no

V1 visible?no

for 1 edge:

Sutherland-Hodgman Polygon Clipping

V2

V1

Page 18: Clipping - TU Wien · position with respect to the clipping rectangle. Cohen-Sutherland Line Clipping. 1001 1000 1010 0001 0000 0010. 0101 0100 0110. Werner Purgathofer 10 “or”

Werner Purgathofer 18

V2:= 1st vertex

V1:=V2V2:=next vertex

V2 visible?no

V1 visible?no

Vnew = clip edge ∩ V1V2 → result list

yes

for 1 edge:

Sutherland-Hodgman Polygon Clipping

V1

V2Vnew

Page 19: Clipping - TU Wien · position with respect to the clipping rectangle. Cohen-Sutherland Line Clipping. 1001 1000 1010 0001 0000 0010. 0101 0100 0110. Werner Purgathofer 10 “or”

Werner Purgathofer 19

V2:= 1st vertex

V1:=V2V2:=next vertex

V2 visible?no

V1 visible?

yes

yesV1 visible?

no

V’1 = clip edge ∩ V1V2 → result list

yes

V2 → result list

for 1 edge:

Sutherland-Hodgman Polygon Clipping

V1

V2

Page 20: Clipping - TU Wien · position with respect to the clipping rectangle. Cohen-Sutherland Line Clipping. 1001 1000 1010 0001 0000 0010. 0101 0100 0110. Werner Purgathofer 10 “or”

Werner Purgathofer 20

V2:= 1st vertex

V1:=V2V2:=next vertex

V2 visible?no

V1 visible?

yes

yesV1 visible?

no

V’1 = clip edge ∩ V1V2 → result list

yes no

V2 → result list

for 1 edge:

Sutherland-Hodgman Polygon Clipping

V1

V2Vnew

Page 21: Clipping - TU Wien · position with respect to the clipping rectangle. Cohen-Sutherland Line Clipping. 1001 1000 1010 0001 0000 0010. 0101 0100 0110. Werner Purgathofer 10 “or”

window

61

2

3

5

4

Werner Purgathofer 21

clipping a polygon against the left boundary of a window, starting with vertex 1.

primed numbers are used to label the points in the output vertex list for this window boundary

Sutherland-Hodgman Polygon Clipping

1' 2'

5'4'

3'

Page 22: Clipping - TU Wien · position with respect to the clipping rectangle. Cohen-Sutherland Line Clipping. 1001 1000 1010 0001 0000 0010. 0101 0100 0110. Werner Purgathofer 10 “or”

Werner Purgathofer 22

Polygon Clip: Combination of 4 Passes

the polygon is clipped against each of the 4 borders separately, that would produce 3 intermediate results.

by calling the 4 tests recursively,(or by using a clipping pipeline)every result point is immediately processed on, so that only one result list is produced

Page 23: Clipping - TU Wien · position with respect to the clipping rectangle. Cohen-Sutherland Line Clipping. 1001 1000 1010 0001 0000 0010. 0101 0100 0110. Werner Purgathofer 10 “or”

Werner Purgathofer 23

pipeline of boundary clippers to avoid intermediate vertex lists

Processing the polygon vertices through a boundary-clipping pipeline. After all vertices are processed through the pipeline, the vertex list for the clipped polygon is {1', 2, 2', 2"}

Sutherland-Hodgman Clipping Example

3

1

2 1st clip:left

2nd clip:bottom

window

2'

3'

2"1'

Page 24: Clipping - TU Wien · position with respect to the clipping rectangle. Cohen-Sutherland Line Clipping. 1001 1000 1010 0001 0000 0010. 0101 0100 0110. Werner Purgathofer 10 “or”

Werner Purgathofer 24

extraneous lines for concave polygons:split into separate parts orfinal check of output vertex list

Sutherland-Hodgman Polygon Clipping

clipping the concave polygon with the Sutherland-Hodgeman clipper produces three connected areas

Page 25: Clipping - TU Wien · position with respect to the clipping rectangle. Cohen-Sutherland Line Clipping. 1001 1000 1010 0001 0000 0010. 0101 0100 0110. Werner Purgathofer 10 “or”

Werner Purgathofer 25

often b-reps are “triangle soups”clipping a triangle triangle(s)4 possible cases:

insideoutsidetrianglequadrilateral 2 triangles

Clipping of Triangles

(inside)

Page 26: Clipping - TU Wien · position with respect to the clipping rectangle. Cohen-Sutherland Line Clipping. 1001 1000 1010 0001 0000 0010. 0101 0100 0110. Werner Purgathofer 10 “or”

Werner Purgathofer 26

corner cases need no extra handling!

Clipping of Triangles

Page 27: Clipping - TU Wien · position with respect to the clipping rectangle. Cohen-Sutherland Line Clipping. 1001 1000 1010 0001 0000 0010. 0101 0100 0110. Werner Purgathofer 10 “or”

From Object Space to Screen Space

Werner Purgathofer 27

object space world space camera space

clip space screen space

„view frustum“modeling

transformationcamera

transformation

projectiontransformation

viewporttransformation

Page 28: Clipping - TU Wien · position with respect to the clipping rectangle. Cohen-Sutherland Line Clipping. 1001 1000 1010 0001 0000 0010. 0101 0100 0110. Werner Purgathofer 10 “or”

Werner Purgathofer 28

clipping against x = ± 1, y = ± 1, z = ± 1(x,y,z) inside? only compare one value per border!

Clipping in Clip-Space

is done before homogenization:x = ± h, y = ± h, z = ± hclips points that are behind the camera!reduces homogenization divisions

Page 29: Clipping - TU Wien · position with respect to the clipping rectangle. Cohen-Sutherland Line Clipping. 1001 1000 1010 0001 0000 0010. 0101 0100 0110. Werner Purgathofer 10 “or”

Einführung in Visual Computing186.822

Antialiasing

Werner Purgathofer

Page 30: Clipping - TU Wien · position with respect to the clipping rectangle. Cohen-Sutherland Line Clipping. 1001 1000 1010 0001 0000 0010. 0101 0100 0110. Werner Purgathofer 10 “or”

Antialiasing in the Rendering Pipeline

Werner Purgathofer 30raster image in pixel coordinates

clipping + homogenization

object capture/creation

modelingviewing

projection

rasterizationviewport transformation

shading

vertex stage(„vertex shader“)

pixel stage(„fragment shader“)

scene in normalized device coordinates

transformed vertices in clip space

scene objects in object space

Page 31: Clipping - TU Wien · position with respect to the clipping rectangle. Cohen-Sutherland Line Clipping. 1001 1000 1010 0001 0000 0010. 0101 0100 0110. Werner Purgathofer 10 “or”

Werner Purgathofer 31

what is aliasing? ['eiliæsiη]

what is the reason for aliasing?

what can we do against it?

Aliasing and Antialiasing

Page 32: Clipping - TU Wien · position with respect to the clipping rectangle. Cohen-Sutherland Line Clipping. 1001 1000 1010 0001 0000 0010. 0101 0100 0110. Werner Purgathofer 10 “or”

Werner Purgathofer 32

too bad resolutiontoo few colorstoo few images / secgeometric errorsnumeric errors

errors that are caused by the discretization of analog data to digital data

What is Aliasing?

Page 33: Clipping - TU Wien · position with respect to the clipping rectangle. Cohen-Sutherland Line Clipping. 1001 1000 1010 0001 0000 0010. 0101 0100 0110. Werner Purgathofer 10 “or”

Werner Purgathofer 33

Aliasing: Staircase Effect

Page 34: Clipping - TU Wien · position with respect to the clipping rectangle. Cohen-Sutherland Line Clipping. 1001 1000 1010 0001 0000 0010. 0101 0100 0110. Werner Purgathofer 10 “or”

Werner Purgathofer 34

Various Aliasing Effects

Page 35: Clipping - TU Wien · position with respect to the clipping rectangle. Cohen-Sutherland Line Clipping. 1001 1000 1010 0001 0000 0010. 0101 0100 0110. Werner Purgathofer 10 “or”

Werner Purgathofer 35

artificial color borders can appear

Aliasing from too few Colors

Page 36: Clipping - TU Wien · position with respect to the clipping rectangle. Cohen-Sutherland Line Clipping. 1001 1000 1010 0001 0000 0010. 0101 0100 0110. Werner Purgathofer 10 “or”

Werner Purgathofer 36

jumping images"worming“

backwards rotating wheels

Aliasing in Animations

t

Page 37: Clipping - TU Wien · position with respect to the clipping rectangle. Cohen-Sutherland Line Clipping. 1001 1000 1010 0001 0000 0010. 0101 0100 0110. Werner Purgathofer 10 “or”

Werner Purgathofer 37

1. improve the deviceshigher resolutionmore color levelsfaster image sequence

2. improve the images = antialiasingpostprocessingprefiltering !

expensiveor

incompatible

software !

Solutions against Aliasing?

Page 38: Clipping - TU Wien · position with respect to the clipping rectangle. Cohen-Sutherland Line Clipping. 1001 1000 1010 0001 0000 0010. 0101 0100 0110. Werner Purgathofer 10 “or”

a signal can only be reconstructed without information lossif the sampling frequency is at least

twice the highest frequency of the signal

this border frequency is called "Nyquist Limit"

Werner Purgathofer 38

Nyquist-Shannon Sampling Theorem

Page 39: Clipping - TU Wien · position with respect to the clipping rectangle. Cohen-Sutherland Line Clipping. 1001 1000 1010 0001 0000 0010. 0101 0100 0110. Werner Purgathofer 10 “or”

Werner Purgathofer 39

Nyquist-Shannon Sampling Theorem

Nyquistsamplinginterval

sampling rate ∆

original signal

reconstructed signal∆

Page 40: Clipping - TU Wien · position with respect to the clipping rectangle. Cohen-Sutherland Line Clipping. 1001 1000 1010 0001 0000 0010. 0101 0100 0110. Werner Purgathofer 10 “or”

Werner Purgathofer 40

maxcycle /1xwith2 fxx cycles =∆∆=∆

i.e. sampling interval ≤ one-half cycle interval

max2 ff s =Nyquist sampling frequency:

Antialiasing: Nyquist Sampling Frequency

a signal can only be reconstructed without information lossif the sampling frequency is at least

twice the highest frequency of the signal

Page 41: Clipping - TU Wien · position with respect to the clipping rectangle. Cohen-Sutherland Line Clipping. 1001 1000 1010 0001 0000 0010. 0101 0100 0110. Werner Purgathofer 10 “or”

Werner Purgathofer 41

supersampling straight-line segmentssubpixel weighting masksarea sampling straight-line segmentsfiltering techniquescompensating for line-intensity differencesantialiasing area boundaries

(adjusting boundary pixel positions)adjusting boundary pixel intensity

Antialiasing Strategies

Page 42: Clipping - TU Wien · position with respect to the clipping rectangle. Cohen-Sutherland Line Clipping. 1001 1000 1010 0001 0000 0010. 0101 0100 0110. Werner Purgathofer 10 “or”

Werner Purgathofer 42

3 = max. intensity...0 = min. intensity

9 = max. intensity...0 = min. intensity

mathematical line line of finite width

Antialiasing: Supersampling Lines

0 0 10 2 23 1 0

0 0 41 6 88 5 0

Page 43: Clipping - TU Wien · position with respect to the clipping rectangle. Cohen-Sutherland Line Clipping. 1001 1000 1010 0001 0000 0010. 0101 0100 0110. Werner Purgathofer 10 “or”

Werner Purgathofer 43

Antialiasing

Page 44: Clipping - TU Wien · position with respect to the clipping rectangle. Cohen-Sutherland Line Clipping. 1001 1000 1010 0001 0000 0010. 0101 0100 0110. Werner Purgathofer 10 “or”

Werner Purgathofer 44

calculate the pixel coverage exactlycan be done with incremental schemes

Antialiasing: Area Sampling Lines

0% 0% 43%15% 71% 84%

90% 52% 3%

Page 45: Clipping - TU Wien · position with respect to the clipping rectangle. Cohen-Sutherland Line Clipping. 1001 1000 1010 0001 0000 0010. 0101 0100 0110. Werner Purgathofer 10 “or”

Werner Purgathofer 45

relative weights for a grid of 3x3 subpixels

more weight for center subpixelsmust be divided by sum of weightssubpixel grids can also include some neighboring pixels

Antialiasing: Pixel Weighting Masks

1 2 1

2 4 2

1 2 1

Page 46: Clipping - TU Wien · position with respect to the clipping rectangle. Cohen-Sutherland Line Clipping. 1001 1000 1010 0001 0000 0010. 0101 0100 0110. Werner Purgathofer 10 “or”

Werner Purgathofer 46

box filter cone filter Gaussian filter

continuous overlapping weighting functions to calculate the antialiased values with integrals

Antialiasing: Filtering Techniques

Page 47: Clipping - TU Wien · position with respect to the clipping rectangle. Cohen-Sutherland Line Clipping. 1001 1000 1010 0001 0000 0010. 0101 0100 0110. Werner Purgathofer 10 “or”

Werner Purgathofer 47

unequal line lengths displayed with the same number of pixels in each line/row have different intensitiesproper antialiasing compensates for that!

Antialiasing: Intensity Differences

1

1,41421

Page 48: Clipping - TU Wien · position with respect to the clipping rectangle. Cohen-Sutherland Line Clipping. 1001 1000 1010 0001 0000 0010. 0101 0100 0110. Werner Purgathofer 10 “or”

Werner Purgathofer 48

Antialiasing Area Boundaries

Page 49: Clipping - TU Wien · position with respect to the clipping rectangle. Cohen-Sutherland Line Clipping. 1001 1000 1010 0001 0000 0010. 0101 0100 0110. Werner Purgathofer 10 “or”

Werner Purgathofer 49

adjusting pixel intensities along an area boundary

alternative 1:supersampling

Antialiasing Area Boundaries (1)

Page 50: Clipping - TU Wien · position with respect to the clipping rectangle. Cohen-Sutherland Line Clipping. 1001 1000 1010 0001 0000 0010. 0101 0100 0110. Werner Purgathofer 10 “or”

Werner Purgathofer 50

alternative 2: similar to Bresenham algorithmp' = y − ymid = [m(xk + 1) + b] − (yk + 0.5)

p'<0 ⇒ y closer to ykp'>0 ⇒ y closer to yk+1

p = p' + (1−m) :p<1−m ⇒ y closer to ykp>1−m ⇒ y closer to yk+1

( and p ∈ [0,1] )

Antialiasing Area Boundaries (2)

ymid

y = mx + b

yk + 1yk + 0.5

yk

xk + 1xk

y

Page 51: Clipping - TU Wien · position with respect to the clipping rectangle. Cohen-Sutherland Line Clipping. 1001 1000 1010 0001 0000 0010. 0101 0100 0110. Werner Purgathofer 10 “or”

Werner Purgathofer 51

p = p'+ (1−m) = [m(xk + 1) + b] − (yk + 0.5) + (1 − m)= mxk + b − yk + 0.5

=

Antialiasing Area Boundaries (3)

xk xk+1

yk

yk+ 0.5

mp for next pixel= overlap areafor current pixel

p'

p p1-m

p'

= mxk + b − (yk − 0.5)

Page 52: Clipping - TU Wien · position with respect to the clipping rectangle. Cohen-Sutherland Line Clipping. 1001 1000 1010 0001 0000 0010. 0101 0100 0110. Werner Purgathofer 10 “or”

Werner Purgathofer 52

Antialiasing Area Boundaries (4)

Page 53: Clipping - TU Wien · position with respect to the clipping rectangle. Cohen-Sutherland Line Clipping. 1001 1000 1010 0001 0000 0010. 0101 0100 0110. Werner Purgathofer 10 “or”

Werner Purgathofer 53

Antialiasing Examples

Page 54: Clipping - TU Wien · position with respect to the clipping rectangle. Cohen-Sutherland Line Clipping. 1001 1000 1010 0001 0000 0010. 0101 0100 0110. Werner Purgathofer 10 “or”

Werner Purgathofer 54

Antialiasing Examples