Professional Documents
Culture Documents
Computer Graphics
• Polygon clipping
– Sutherland-Hodgman, • Ordered set of vertices (points)
Polygon Clipping and Filling – Weiler-Atherton – Usually counter-clockwise
Week 3, Lecture 5
• Polygon filling • Two consecutive vertices define an edge
David Breen, William Regli and Maxim Peysakhov – Scan filling polygons
– Flood filling polygons • Left side of edge is inside
Department of Computer Science
Drexel University • Introduction and discussion of homework #2 • Right side is outside
• Last vertex implicitly connected to first
• In 3D vertices should be co-planar
1 2 3
1 2 3
4 5 6
1
Sutherland-Hodgman Algorithm Sutherland-Hodgman Algorithm
Sutherland-Hodgman Algorithm
• Can be done incrementally
• Given polygon P P’ = P
• If first point inside add. If outside, don’t add – foreach clipping edge (there are 4) {
• Move around polygon from v1 to vn and back to v1 • Clip polygon P’ to clipping edge
• Check vi,vi+1 wrt the clip edge
–foreach edge in polygon P’
• Need vi,vi+1‘s inside/outside status
» Check clipping cases (there are 4)
• Add vertex one at a time. There are 4 cases:
» Case 1 : Output vi+1 to P’’
» Case 2 : Output intersection point to P’’
» Case 3 : No output
» Case 4 : Output intersection point & vi+1 to P’’
• P’ = P’’
7
1994 Foley/VanDam/Finer/Huges/Phillips ICG
} 8 9
Animated by Max Peysakhov @ Drexel University
7 8 9
11 12 13
1994 Foley/VanDam/Finer/Huges/Phillips ICG 1994 Foley/VanDam/Finer/Huges/Phillips ICG
11 12 13
2
Intersection Special Cases
Weiler-Atherton Algorithm
Linked
• Given polygons A and B as linked list of List Data • If “intersecting” edges are parallel, ignore
vertices (counter-clockwise order) • Intersection point is a vertex
Structure
• Find all edge intersections & place in list – Vertex of A lies on a vertex or edge of B
• Insert as “intersection” nodes – Edge of A runs through a vertex of B
Intersection – Replace vertex with an intersection node
• Nodes point to A & B
Nodes
• Determine in/out
status of vertices
14 15 16
14 15 16
17 18 19
3
Weiler-Atherton Algorithm:
Result Example: Intersection
Intersection
• Start at intersection point
– If connected to an “inside” vertex, go there
– Else step to an intersection point
– If neither, stop
• Traverse linked list
• At each intersection point
switch to other polygon and
remove intersection point from
list
• Do until return to starting intersection point
• If intersection list not empty, pick another one
{P1, V7, P0}, {P3, V5, P2}
• All visited vertices and nodes define and’ed polygon
20 21 22
20 21 22
23 24 25
4
Edge clipping Intersecting Two Edges (1) Intersecting Two Edges (2)
• Re-use line clipping from HW1 • Edge 0 : (P0,P1) • Solve for t’s
– Similar triangles method • Edge 2 : (P2,P3) • t0 = ((x0 - x2) * dy2+ (y2 - y0) * dx2) /
– Cyrus-Beck line clipping • E0 = P0 + t0*(P1-P0) D0 º (P1-P0) (dy0 * dx2- dx0 * dy2)
• t2 = ((x2 - x0) * dy0+ (y0 - y2) * dx0) /
• Yet another technique • E2 = P2 + t2*(P3-P2) D2 º (P3-P2)
(dy2 * dx0- dx2 * dy0)
• P0 + t0*D0 = P2 + t2*D2 • See http://www.vb-helper.com/howto_intersect_lines.html
• x0 +dx0 * t0 = x2 +dx2 * t2 for derivation
• y0 +dy0 * t0 = y2 +dy2 * t2 • Edges intersect if 0 £ t0,t2 £ 1
• Edges are parallel if denominator = 0
26 30 31
26 30 31
32 33 34
5
Scan Filling Primitives:
Scan Filling Polygons Scan Filling Polygons
Polygons
• Observe: • Idea #1: use midpoint • Idea #2: draw pixels only strictly inside
– FA, DC intersections algo on each edge, fill – Find intersections of
are integer in between extrema scan line with edges
– FE, ED intersections points – Sort intersections by
increasing x coordinate
are not integer • Note: many extrema
– Fill pixels on inside
• For each scan line, pixels lie outside the based on a parity bit
how to figure out polygon • Bp initially even (off)
which pixels are • Invert at each intersect
• Why: midpoint algo • Draw when odd,
inside the polygon? has no sense of in/out do not draw when even
35 36 37
1994 Foley/VanDam/Finer/Huges/Phillips ICG 1994 Foley/VanDam/Finer/Huges/Phillips ICG 1994 Foley/VanDam/Finer/Huges/Phillips ICG
35 36 37
38 39 40
6
How to handle horizontal edges? How to handle horizontal edges? Polygon Filling Algorithm
• Start drawing at IJ • For each polygon
• Idea: don’t count their vertices (Bp becomes odd). – For each edge, mark each scan-line y that the
• Apply open and closed status • C is ymax (open) for BC. edge crosses by examining its ymin and ymax
to vertices to other edges
– ymin vertex closed
Bp doesn’t change. • If edge is horizontal, ignore it
• Ignore CD. D is ymin (closed) • If ymax on scan-line, ignore it
– ymax vertex is open • If ymin <= y < ymax add edge to scan-line y‘s edge list
for DE. Bp becomes even.
• On AB, A is at ymin for JA; AB – For each scan-line between polygon’s ymin and ymax
Stop drawing.
does not contribute, Bp is odd • Calculate intersections with edges on list
and draw AB • I is ymax (open) for IJ.
• Sort intersections in x
• Edge BC has ymin at B, but AB No drawing.
• Perform parity-bit scan-line filling
does not contribute, Bp • Ignore IH. H is ymin (closed) • Apply floor on first xsect and ceiling on second xsect
becomes even and drawing for GH. Bp becomes odd. • Check for double intersection special case
stops Draw to FE.
– Clear scan-lines’ edge list
41
1994 Foley/VanDam/Finer/Huges/Phillips ICG
• Ignore GF. No drawing 42
1994 Foley/VanDam/Finer/Huges/Phillips ICG
44
41 42 44
Example
How to handle slivers? Scan Filling Curved Objects
• When the scan area does • Hard in general case
not have an “interior” • Easier for circles and
• Solution: use anti-aliasing ellipses.
• But, to do so will require • Use midpoint Alg to
softening the rules about generate boundary points.
drawing only interior • Fill in horizontal pixel spans
pixels
• Use symmetry
45 46 47
1994 Foley/VanDam/Finer/Huges/Phillips ICG 1994 Foley/VanDam/Finer/Huges/Phillips ICG
45 46 47
7
4 Connected Boundary-Fill Alg Boundary-Fill Algorithm
Boundary-Fill Algorithm Void BoundaryFill4( int x, int y, int fill,
int bnd)
• Issues with recursive boundary-fill algorithm:
• Start with some – May make mistakes if parts of the space already filled
{ with the Fill color
internal point (x,y) If Color(x,y) != fill and Color(x,y) != bnd
– Requires very big stack size
• Color it {
• Check neighbors for SetColor(x,y) = fill;
• More efficient algorithms
filled or border color BoundaryFill4(x+1, y, fill, bnd);
– First color contiguous span along one scan line
• Color neighbors if OK BoundaryFill4(x, y +1, fill, bnd);
– Only stack beginning positions of neighboring scan
BoundaryFill4(x-1, y, fill, bnd);
• Continue recursively lines
BoundaryFill4(x, y -1, fill, bnd);
}
48 } 49 50
1994 Foley/VanDam/Finer/Huges/Phillips ICG
48 49 50
51 52 53
8
Homework #2
Course Status Programming assignment 3
So far everything straight lines! • Modify homework #1
• Input PostScript-like file.
• How to model 2D curved objects? • Add reading “moveto” and “lineto” • Output B/W PBM.
– Representation commands • Implement viewports.
• Circles
• Types of 2D Curves • They define closed polygons • Use HW2 for polygon clipping.
• Parametric Cubic Curves
• Transform polygon vertices • Implement scanline polygon filling. (You can
• Bézier Curves, (non)uniform, (non)rational not use flood filling algorithms)
• NURBS
• Clip polygons against window with
– Drawing of 2D Curves
• Line drawing algorithms for complex curves Sutherland-Hodgman algorithm
• DeCasteljeau, Subdivision, De Boor
• Display edges with HW1 line-drawing
55
code 56 57
55 56 57