2D Clipping - PowerPoint PPT Presentation

1 / 24
About This Presentation
Title:

2D Clipping

Description:

Just as with scan-conversion algorithms, clipping algorithms are called whenever ... easy for thick and filled primitives as part of scan line fill ... – PowerPoint PPT presentation

Number of Views:149
Avg rating:3.0/5.0
Slides: 25
Provided by: david1453
Category:
Tags: clipping | scan

less

Transcript and Presenter's Notes

Title: 2D Clipping


1
2D Clipping
  • CS 4451

2
Motivation
  • Programmers describe graphics primitives
  • The graphics library
  • scan converts or rasterizes the primitive
  • primitive is also clipped to the clip rectangle
  • Graphics primitives are displayed on the screen

3
What is this clip rectangle?
  • By default, the clip rectangle is the entire
    canvas
  • not necessary to draw outside the canvas
  • for some devices, it is damaging (plotters)
  • Sometimes it is convenient to restrict the clip
    rectangle to a smaller portion of the canvas
  • partial canvas redraw for menus, dialog boxes,
    other obscuration

4
Sophisticated Clipping Regions
  • Postscript and similar graphics APIs allow
    multiple clipping regions of arbitrary shape
  • Seen commercially in printers,
  • also NeXT DisplayPostscript
  • Mac OS X DisplayPDF
  • NeWS, an early competitor to X

5
Efficiency
  • Just as with scan-conversion algorithms, clipping
    algorithms are called whenever primitives are
    rendered.
  • Important to do a little extra thinking to
    increase efficiency.
  • Moores Law is not a license for laziness

6
How to do Clipping
  • Clip it before you scan convert it
  • used mostly for lines, rectangles, and polygons,
    where clipping algorithms are simple and
    efficient
  • analytical clipping

7
How to do Clipping (cont.)
  • Clip it during scan conversion
  • scissoring, a brute force technique
  • scan convert the primitive, only write pixels if
    inside the clipping region
  • easy for thick and filled primitives as part of
    scan line fill
  • if primitive is not much larger than clip region,
    most pixels will fall inside
  • can be more efficient than analytical clipping

8
How to do Clipping (cont.)
  • Clip it after scan conversion
  • render everything onto a temporary canvas and
    copy the clipping region
  • wasteful, but simple and easy,
  • often used for text

9
How to Choose
  • Foley and van Dam suggest the following
  • for floating point graphics libraries, try to use
    analytical clipping
  • for integer graphics libraries
  • analytical clipping for lines and polygons
  • others, do during scan conversion
  • sometimes both analytical and raster clipping
    performed

10
Analytical Line Clipping
  • First consider endpoints
  • xminltxltxmax
  • yminltyltymax

ymax
ymin
xmin
xmax
11
Analytical Line Clipping
  • Both endpoints inside, line trivially accepted
  • One in and one out, line is partially inside
  • Both outside, might be partially inside
  • How to handle non-trivial cases?

ymax
ymin
xmin
xmax
12
Simultaneous Equations
  • Determine if there are line intersections with
    clipping region edges
  • Use parametric equations
  • xx0t(x1-x0)
  • yy0t(y1-y0)
  • Slope interface form not useful for line segments
  • Intersection if both t values in 0,1
  • We can do better

ymax
ymin
xmin
xmax
13
Cohen-Sutherland
  • First, check endpoints for trivial acceptance
  • perform region checks for trivial rejection
  • subdivide lines so one segment can be trivially
    rejected
  • continued until remainder can be trivially
    accepted or rejected
  • performs well for common cases
  • accepting most everything, large clip region
  • rejecting most everything, small clip region

14
Outcodes
  • First Bit
  • ygtymax sign of ymax-y
  • Second Bit
  • yltymin sign of y-ymin
  • Third Bit
  • xgtxmax sign of xmax-x
  • Fourth Bit
  • xltxmin sign of x-xmin
  • If logical AND of endpointsis not zero, trivial
    reject,
  • else, subdivide, using consistent
  • edge order (here top to bottom, left to right)

1000
1001
1010
ymax
0000
0001
0010
ymin
0100
0101
0110
xmin
xmax
15
Other Algorithms
  • Cohen-Sutherland sometimes performs a lot of
    fruitless clipping due to external intersections,
    but oldest, widely published, most common
  • Nicholl, Lee, and Nicholl (1987) algorithm
    subdivides plane into more regions.
  • Cyrus-Beck (1978) and Liang-Barsky (1984) are
    more efficient

16
Cyrus-Beck/Liang-Barsky
  • C-B is a parametic line-clipping algorithm
  • can be used for 2D line clipping against
    arbitrary convex polygons
  • or 3D lines against arbitrary convex polyhedron
  • L-B adds efficient trivial rejection tests

17
C-B Overview
  • Using parametric equations, compute line segment
    intersections (actually, just values of t) with
    clipping region edges
  • Determine if the four values of t actually
    correspond to real intersections
  • Then calculate x and y values of the
    intersections
  • Faster than Cohen-Sutherland, does not need to
    iterate
  • L-B examines values of t for earlier reject

18
Parametric Intersection
  • Equation of line segment
  • P(t)P0 (P1 - P0)t
  • Perpendicular
  • NiP(t)-PEi0
  • NiP0(P1-P0)t-PEi0
  • NiP0-PEi NiP1-P0t0
  • Let DP1-P0
  • tNiP0-PEi/-NiD

Ei
PEi
P1
NiP(t)-PEilt0
NiP(t)-PEi0
NiP(t)-PEigt0
P0
Ni
19
Parametric Intersection
  • tNiP0-PEi/-NiD
  • denominator must be non-zero
  • Ni?0 normal is not zero (mistake)
  • D?0 line is not a single point
  • NiD?0 edge and line not parallel

20
Classifying Intersections
  • Each line segment will have four values of t, one
    for each edge
  • Can we just chose values of t in 0,1?
  • Can we chose intermediate intersections?
  • We must classify

21
Classifying Intersections
  • PE-Potentially Entering
  • PL-Potentially Leaving
  • can be classified by angle between P0P1 and Ni,
    sign of NiD
  • PL if lt90, NiDgt0
  • PE if gt90, NiDlt0
  • Calculation done while calculating t

PL
PE
P0 is to the left in this example
22
Classifying Intersections
  • PE-Potentially Entering
  • PL-Potentially Leaving
  • must find an appropriate (te, tl)
  • if tegttl, then reject
  • otherwise chose highest te and lowest tl

PL
PE
P0 is to the left in this example
23
Line Clipping Conclusion
  • For upright (axis aligned) clipping rectangles,
    further simplifications possible
  • L-B compares new te and tl to old tl or te values
    as they are computed for trivial rejection
  • L-B also can reject parallel segments outside the
    clipping region

24
Line Clipping Conclusion
  • Cohen-Sutherland
  • most common, uses outcodes, loops on some
    segments
  • Cyrus-Beck
  • parametric, generalizes to 3D, no looping
  • Liang-Barsky
  • rejection, etc. enhancements to C-B
  • Nicholl, et al.
  • Fastest, but doesnt generalize to 3D
Write a Comment
User Comments (0)
About PowerShow.com