1 CS 430/536 Computer Graphics I Polygon Clipping and Filling Week 3, Lecture 5 David Breen, William Regli and Maxim Peysakhov Geometric and Intelligent.

Slides:



Advertisements
Similar presentations
Polygon Scan Conversion – 11b
Advertisements

Computer Graphics Inf4/MSc 1 Computer Graphics Lecture 7 Scanline algorithm and polygon clipping Taku Komura.
CS 450: COMPUTER GRAPHICS FILLING POLYGONS SPRING 2015 DR. MICHAEL J. REALE.
10/10/02 (c) 2002 University of Wisconsin, CS 559 Last Time Finished viewing: Now you know how to: –Define a region of space that you wish to view – the.
CS 551 / 645: Introductory Computer Graphics Clipping Lines and Polygons.
3/2/04© University of Wisconsin, CS559 Spring 2004 Last Time General Perspective –Methods for specifying the view volume As a set of clip planes As a field.
David Breen, William Regli and Maxim Peysakhov
Course Website: Computer Graphics 4: Viewing In 2D.
Computer Graphics Viewing.
Viewing & Clipping In 2D. 2 of 44 Contents Windowing Concepts Clipping –Introduction –Brute Force –Cohen-Sutherland Clipping Algorithm Area Clipping –Sutherland-Hodgman.
Introduction to Computer Graphics Chapter 6 – 2D Viewing Pt 2 1.
Different types of Polygons Simple Convex Simple Concave Non-simple : self-intersecting With holes ConvexConcaveSelf-intersecting.
Vertices and Fragments I CS4395: Computer Graphics 1 Mohan Sridharan Based on slides created by Edward Angel.
CSCE 441: Computer Graphics Scan Conversion of Polygons
Graphics Programming: Polygon Filling
1 CSCE 441 Computer Graphics: Clipping Lines Jinxiang Chai.
CS 454 Computer graphics Polygon Filling
CS 4731: Computer Graphics Lecture 22: Raster Graphics Part 3 Emmanuel Agu.
Rasterization Foley, Ch: 3. Pixels are represented as disjoint circles centered on uniform grid Each (x,y) of the grid has a pixel Y X (1,1) (1,2) (0,0)
1 Angel: Interactive Computer Graphics 5E © Addison-Wesley 2009 Clipping.
1 Angel: Interactive Computer Graphics 4E © Addison-Wesley 2005 Implementation I Ed Angel Professor of Computer Science, Electrical and Computer Engineering,
Graphics Pipeline Clipping CMSC 435/634. Graphics Pipeline Object-order approach to rendering Sequence of operations – Vertex processing – Transforms.
TOPIC 4 PART III FILL AREA PRIMITIVES POLYGON FILL AREAS CGMB214: Introduction to Computer Graphics.
1 CSCE 441: Computer Graphics Scan Conversion of Polygons Jinxiang Chai.
2-Dimension Viewing and Clipping
Clipping Apart from clipping to the view volume, clipping is a basic operation in many other algorithms –Breaking space up into chunks –2D drawing and.
CGMB 314 Intro to Computer Graphics Fill Area Primitives.
Polygon Scan Conversion and Z-Buffering
Basics of Rendering Pipeline Based Rendering –Objects in the scene are rendered in a sequence of steps that form the Rendering Pipeline. Ray-Tracing –A.
1 CS 430/536 Computer Graphics I Circle Drawing and Clipping Week 3, Lecture 6 David Breen, William Regli and Maxim Peysakhov Geometric and Intelligent.
3/4/04© University of Wisconsin, CS559 Spring 2004 Last Time Clipping Lines –Cohen-Sutherland: The use of outcodes and early reject/accept tests –Liang-Barsky:
CS 480/680 Computer Graphics Shading in OpenGL Dr. Frederick C Harris, Jr. Fall 2013.
1 CS 430/536 Computer Graphics I 3D Transformations World Window to Viewport Transformation Week 2, Lecture 4 David Breen, William Regli and Maxim Peysakhov.
10/15/02 (c) 2002 University of Wisconsin, CS559 Last Time Clipping.
Graphics Graphics Korea University cgvr.korea.ac.kr 1 2D Viewing 고려대학교 컴퓨터 그래픽스 연구실.
2D Output Primitives Points Lines Circles Ellipses Other curves Filling areas Text Patterns Polymarkers.
1 CS 430/536 Computer Graphics I Curve Drawing Algorithms Week 4, Lecture 8 David Breen, William Regli and Maxim Peysakhov Geometric and Intelligent Computing.
Introduction to Computer Graphics CS 445 / 645 Lecture 9 Chapter 4: Rasterization L. McMillan MIT notes (Fall ’98)
10/15/02 (c) 2002 University of Wisconsin, CS559 Who Am I? Prof Stephen Chenney These notes will be online after the lecture – in fact they’re online already.
10/19/04© University of Wisconsin, CS559 Fall 2004 Last Time Clipping –Why we care –Sutherland-Hodgman –Cohen-Sutherland –Intuition for Liang-Barsky Homework.
EEL Introduction to Computer Graphics
1 Computer Graphics Clipping Fall FCC Line Clipping What happens when one or both endpoints of a line segment are not inside the specified drawing.
Computer Graphics Filling. Filling Polygons So we can figure out how to draw lines and circles How do we go about drawing polygons? We use an incremental.
2D Output Primitives Points Lines Circles Ellipses Other curves Filling areas Text Patterns Polymarkers.
1Computer Graphics Implementation 1 Lecture 15 John Shearer Culture Lab – space 2
Lecture 15: Raster Graphics and Scan Conversion
Clipping Primitives. Clipping line Clipping rectangle: – x min to x max – y min to y max A point (x,y) lies within a clip rectangle and thus displayed.
Graphics Graphics & Graphical Programming Lecture 23 - Viewing & Clipping.
1 CSCE 441: Computer Graphics Scan Conversion of Polygons Jinxiang Chai.
Rasterization Overview Raster Display Device. Scan Conversion / Rasterization: converting vector graphics into raster graphics (determining pixels in.
Computer Graphics CC416 Week 14 Filling Algorithms.
Computer Graphics Lecture 08 Taqdees A. Siddiqi Computer Graphics Filled Area Primitives I Lecture 08 Taqdees A. Siddiqi
Computer Graphics Viewing. 2 of 30 Viewing in 2D Window in world coordinates. 45  250  Viewport in Device coords 250 x 250 Pixels.
OUTPUT PRIMITIVES CEng 477 Computer Graphics METU, 2004.
CS552: Computer Graphics Lecture 16: Polygon Filling.
Computer Graphics Filling.
Scan Conversion of Polygons
Introduction to Polygons
Concepts, Algorithms for line clipping
(c) 2002 University of Wisconsin, CS559
Computer Graphics Filled Area Primitives II Lecture 09 Taqdees A
CSCE 441 Computer Graphics: Clipping Polygons Jinxiang Chai
Fill Area Algorithms Jan
Graphics Pipeline Clipping
Computer Graphics : Viewing In 2D
Agenda Polygon Terminology Types of polygons Inside Test
CSCE 441 Computer Graphics: Clipping Polygons Jinxiang Chai
Prepared by Narendra V G CSE MIT
Agenda Polygon Terminology Types of polygons Inside Test
CSCE 441 Computer Graphics: Clipping Polygons Jinxiang Chai
Presentation transcript:

1 CS 430/536 Computer Graphics I Polygon Clipping and Filling Week 3, Lecture 5 David Breen, William Regli and Maxim Peysakhov Geometric and Intelligent Computing Laboratory Department of Computer Science Drexel University

2 Outline Polygon clipping –Sutherland-Hodgman, –Weiler-Atherton Polygon filling –Scan filling polygons –Flood filling polygons Introduction and discussion of homework #2

3 Polygon Ordered set of vertices (points) –Usually counter-clockwise Two consecutive vertices define an edge Left side of edge is inside Right side is outside Last vertex implicitly connected to first In 3D vertices are co-planar

4 Polygon Clipping Lots of different cases Issues –Edges of polygon need to be tested against clipping rectangle –May need to add new edges –Edges discarded or divided –Multiple polygons can result from a single polygon 1994 Foley/VanDam/Finer/Huges/Phillips ICG

5 The Sutherland-Hodgman Polygon-Clipping Algorithm Divide and Conquer Idea: –Clip single polygon using single infinite clip edge –Repeat 4 times Note the generality: –2D convex n-gons can clip arbitrary n-gons –3D convex polyhedra can clip arbitrary polyhedra 1994 Foley/VanDam/Finer/Huges/Phillips ICG

6 Sutherland-Hodgman Algorithm Input: –v 1, v 2, … v n the vertices defining the polygon –Single infinite clip edge w/ inside/outside info Output: –v’ 1, v’ 2, … v’ m, vertices of the clipped polygon Do this 4 (or n e ) times Traverse vertices (edges) Add vertices one-at-a-time to output polygon –Use inside/outside info –Edge intersections

7 Can be done incrementally If first point inside add. If outside, don’t add Move around polygon from v 1 to v n and back to v 1 Check v i,v i+1 wrt the clip edge Need v i,v i+1 ‘s inside/outside status Add vertex one at a time. There are 4 cases: 1994 Foley/VanDam/Finer/Huges/Phillips ICG Sutherland-Hodgman Algorithm

8 foreach polygon P P’ = P –foreach clipping edge (there are 4) { Clip polygon P’ to clipping edge –foreach edge in polygon P’ »Check clipping cases (there are 4) »Case 1 : Output v i+1 »Case 2 : Output intersection point »Case 3 : No output »Case 4 : Output intersection point & v i+1 } Sutherland-Hodgman Algorithm

9 Animated by Max Drexel University

10 Sutherland-Hodgman Algorithm Animated by Max Drexel University

11 Final Result Note: Edges XY and ZW!

12 Clipping a concave polygon Can produce two CONNECTED areas 1994 Foley/VanDam/Finer/Huges/Phillips ICG Issues with Sutherland- Hodgman Algorithm

13 General clipping algorithm for concave polygons with holes Produces multiple polygons (with holes) Make linked list data structure Traverse to make new polygon(s) 1994 Foley/VanDam/Finer/Huges/Phillips ICG Weiler-Atherton Algorithm

14 Weiler-Atherton Algorithm Given polygons A and B as linked list of vertices (counter-clockwise order) Find all edge intersections & place in list Insert as “intersection” nodes Nodes point to A & B Determine in/out status of vertices

15 Intersection Special Cases If “intersecting” edges are parallel, ignore Intersection point is a vertex –Vertex of A lies on a vertex or edge of B –Edge of A runs through a vertex of B –Replace vertex with an intersection node

16 Weiler-Atherton Algorithm: Union Find a vertex of A outside of B Traverse linked list At each intersection point switch to other polygon Do until return to starting vertex All visited vertices and nodes define union’ed polygon

17 Weiler-Atherton Algorithm: 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 All visited vertices and nodes define and’ed polygon

18 Boolean Special Cases If polygons don’t intersect –Union If one inside the other, return polygon that surrounds the other Else, return both polygons –Intersection If one inside the other, return polygon inside the other Else, return no polygons

19 Point P Inside a Polygon? Connect P with another point P` that you know is outside polygon Intersect segment PP` with polygon edges Watch out for vertices! If # intersections is even (or 0)  Outside If odd  Inside

20 Edge clipping Re-use line clipping from HW1 –Similar triangles method –Cyrus-Beck line clipping Yet another technique

21 Do two edges intersect? Cyrus-Beck Algorithm Line P(t) = P 0 +t(P 1 -P 0 ) Point on the edge P Ei Make sure 1.D  0, or P 1  P 0 2.N i  D  0, i.e. lines are not parallel 1994 Foley/VanDam/Finer/Huges/Phillips ICG Is 0 ≤ t ≤ 1 for both edges?

22 Calculating N i N i for window edges WT: (0,1) WB: (0, -1) WL: (-1,0) WR: (1,0) N i for arbitrary edges Calculate edge direction –E = (V 1 - V 0 ) / |V 1 - V 0 | –Be sure to process edges in CCW order Rotate direction vector -90° N x = E y N y = -E x

23 Calculating N i Calculate edge direction –E = (V 1 - V 0 ) / |V 1 - V 0 | –Be sure to process edges in CCW order Rotate direction vector -90° N x = E y N y = -E x

24 Intersecting Two Edges (1) Edge 0 : (P 0,P 1 ) Edge 2 : (P 2,P 3 ) E 0 = P 0 + t 0  (P 1 -P 0 ) D 0  (P 1 -P 0 ) E 2 = P 2 + t 2  (P 3 -P 2 ) D 2  (P 3 -P 2 ) P 0 + t 0  D 0 = P 2 + t 2  D 2 x 0 +dx 0  t 0 = x 2 +dx 2  t 2 y 0 +dy 0  t 0 = y 2 +dy 2  t 2

25 Intersecting Two Edges (2) Solve for t’s t 0 = ((x 0 - x 2 )  dy 2 + (y 2 - y 0 )  dx 2 ) / (dy 0  dx 2 - dx 0  dy 2 ) t 2 = ((x 2 - x 0 )  dy 0 + (y 0 - y 2 )  dx 0 ) / (dy 2  dx 0 - dx 2  dy 0 ) See for derivation Edges intersect if 0  t 0,t 2  1 Edges are parallel if denominator = 0

26 Filling Primitives: Rectangles, Polygons & Circles Two part process –Which pixels to fill? –What values to fill them with? Idea: Coherence –Spatial: pixels are the same from pixel-to-pixel and scan-line to scan line; –Span: all pixels on a span get the same value –Scan-line: consecutive scan lines are the same –Edge: pixels are the same along edges

27 Scan Filling Primitives: Rectangles Easy algorithm –Fill from x min to x max Fill from y min to y max Issues –What if two adjacent rectangles share an edge? –Color the boundary pixels twice? –Rules: Color only interior pixels Color left and bottom edges

28 Scan Filling Primitives: Polygons Observe: –FA, DC intersections are integer –FE, ED intersections are not integer For each scan line, how to figure out which pixels are inside the polygon? 1994 Foley/VanDam/Finer/Huges/Phillips ICG

29 Scan Filling Polygons Idea #1: use midpoint algo on each edge, fill in between extrema points Note: many extrema pixels lie outside the polygon Why: midpoint algo has no sense of in/out 1994 Foley/VanDam/Finer/Huges/Phillips ICG

30 Scan Filling Polygons Idea #2: draw pixels only strictly inside –Find intersections of scan line with edges –Sort intersections by increasing x coordinate –Fill pixels on inside based on a parity bit B p initially even (off) Invert at each intersect Draw with odd, do not draw when even 1994 Foley/VanDam/Finer/Huges/Phillips ICG

31 Scan Filling Polygons Issues with Idea #2: –If at a fractional x value, how to pick which pixels are in interior? –Intersections at integer vertex coordinates? –Shared vertices? –Vertices that define a horizontal edge?

32 How to handle vertices? Problem: –vertices are counted twice Solution: –If both neighboring vertices are on the same side of the scan line, don’t count it –If both neighboring vertices are on different sides of a scan line, count it once –Compare current y value with y value of neighboring vertices

33 How to handle horizontal edges? Idea: don’t count their vertices Apply open and closed status to vertices to other edges –y min vertex closed –y max vertex is open On AB, A is at y min for JA; AB does not contribute, B p is odd and draw AB Edge BC has y min at B, but AB does not contribute, B p becomes even and drawing stops 1994 Foley/VanDam/Finer/Huges/Phillips ICG

34 How to handle horizontal edges? Start drawing at IJ ( B p becomes odd). C is y max (open) for BC. B p doesn’t change. Ignore CD. D is y min (closed) for DE. B p becomes even. Stop drawing. I is y max (open) for IJ. No drawing. Ignore IH. H is y min (closed) for GH. B p becomes odd. Draw to FE. Ignore GF. No drawing 1994 Foley/VanDam/Finer/Huges/Phillips ICG

35 Polygon Filling Algorithm Create a list for each scan-line y with the polygon edges that intersect the scan-line –For each edge, examine y min and y max –If edge is horizontal, ignore it –If y max on scan-line, ignore it –If y min <= y < y max add edge to scan-line list For each scan-line –Calculate intersections with edges on list –Sort intersections in x Perform parity-bit scan-line filling –Check for double intersection special case This also solves overlapping vertex problem

36 Polygon Filling Algorithm For each polygon –For each edge, mark each scan-line that the edge crosses by examining its y min and y max If edge is horizontal, ignore it If y max on scan-line, ignore it If y min <= y < y max add edge to scan-line y ‘s edge list –For each scan-line between polygon’s y min and y max Calculate intersections with edges on list Sort intersections in x Perform parity-bit scan-line filling Check for double intersection special case –Clear scan-lines’ edge list

37 How to handle slivers? When the scan area does not have an “interior” Solution: use anti-aliasing But, to do so will require softening the rules about drawing only interior pixels 1994 Foley/VanDam/Finer/Huges/Phillips ICG

38 Scan-Filling a Polygon Animated by Max Drexel University

39 Scan Filling Curved Objects Hard in general case Easier for circles and ellipses. Use midpoint Alg to generate boundary points. Fill in horizontal pixel spans Use symmetry 1994 Foley/VanDam/Finer/Huges/Phillips ICG

40 Boundary-Fill Algorithm Start with some internal point (x,y) Color it Check neighbors for filled or border color Color neighbors if OK Continue recursively

41 4 Connected Boundary-Fill Alg Void BoundaryFill4( int x, int y, int fill, int bnd) { If Color(x,y) != fill and Color(x,y) != bnd { SetColor(x,y) = fill; BoundaryFill4(x+1, y, fill, bnd); BoundaryFill4(x, y +1, fill, bnd); BoundaryFill4(x-1, y, fill, bnd); BoundaryFill4(x, y -1, fill, bnd); }

42 Boundary-Fill Algorithm Issues with recursive boundary-fill algorithm: –May make mistakes if parts of the space already filled with the Fill color –Requires very big stack size More efficient algorithms –First color contiguous span along one scan line –Only stack beginning positions of neighboring scan lines 1994 Foley/VanDam/Finer/Huges/Phillips ICG

43 Pattern Filling Via Scan Conversion –Copy pixel in pattern template to location in polygon –The entire screen is ‘tiled’ and the polygon is a window Without Scan Conversion –Create patterned template in a rectangular work area (off the screen) –Copy this template as needed Handle overwrites with masking 1994 Foley/VanDam/Finer/Huges/Phillips ICG

44 Course Status So far everything straight lines! How to model 2D curved objects? –Representation Circles Types of 2D Curves Parametric Cubic Curves Bézier Curves, (non)uniform, (non)rational NURBS –Drawing of 2D Curves Line drawing algorithms for complex curves DeCasteljeau, Subdivision, De Boor

45 Homework #2 Modify homework #1 Add “moveto” and “lineto” commands They define closed polygons Clip polygons against window with Sutherland-Hodgman algorithm Display edges with HW1 line-drawing code