Concepts, algorithms for clipping

Slides:



Advertisements
Similar presentations
CS123 | INTRODUCTION TO COMPUTER GRAPHICS Andries van Dam © Clipping Concepts, Algorithms for line clipping 1 of 16 Clipping - 10/16/12.
Advertisements

Computer Graphics Inf4/MSc 1 Computer Graphics Lecture 7 Scanline algorithm and polygon clipping Taku Komura.
Rezanje črt in poligonov. World window & viewport window viewport screen window world window.
I N T R O D U C T I O N T O C O M P U T E R G R A P H I C S Andries van Dam September 30, D Clipping 1/14 Clipping (pages , )
Computer Graphics CLIPPING.
Objectives Define Clipping Various clipping methods. Line clipping methods.
Line clipping: Line clipping algorithm is method of eliminate lines of outside area of the object,so outside of object viewing is Removed. Typically, any.
Course Website: Computer Graphics 4: Viewing In 2D.
Computer Graphics Viewing.
Clipping CSE 403 Computer Graphics Cohen Sutherland Algorithm (Line)
Viewing & Clipping In 2D. 2 of 44 Contents Windowing Concepts Clipping –Introduction –Brute Force –Cohen-Sutherland Clipping Algorithm Area Clipping –Sutherland-Hodgman.
CMPE 466 COMPUTER GRAPHICS Chapter 8 2D Viewing Instructor: D. Arifler Material based on - Computer Graphics with OpenGL ®, Fourth Edition by Donald Hearn,
Introduction to Computer Graphics Chapter 6 – 2D Viewing Pt 2 1.
Informationsteknologi Thursday, November 22, 2007Computer Graphics - Class 111 Today’s class Clipping Parametric and point-normal form of lines Intersecting.
Vertices and Fragments I CS4395: Computer Graphics 1 Mohan Sridharan Based on slides created by Edward Angel.
1 CSCE 441 Computer Graphics: Clipping Lines Jinxiang Chai.
1 Angel: Interactive Computer Graphics 5E © Addison-Wesley 2009 Clipping.
University College Dublin1 Clipping u Clipping is the removal of all objects or part of objects in a modelled scene that are outside the real-world window.
1 Angel: Interactive Computer Graphics 4E © Addison-Wesley 2005 Implementation I Ed Angel Professor of Computer Science, Electrical and Computer Engineering,
2-Dimension Viewing and Clipping
Windowing and clipping
CS 376 Introduction to Computer Graphics 02 / 12 / 2007 Instructor: Michael Eckmann.
Clipping: Clipping is a process of dividing an object into visible and invisible positions and displaying the visible portion and discarding the invisible.
CS 480/680 Computer Graphics Shading in OpenGL Dr. Frederick C Harris, Jr. Fall 2013.
CSE Real Time Rendering Week 9. Post Geometry Shaders Courtesy: E. Angel and D. Shreiner – Interactive Computer Graphics 6E © Addison-Wesley 2012.
Clipping Computer Graphics Cohen Sutherland Algorithm (Line)
Windows, Viewports, and Clipping
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.
1Computer Graphics Implementation 1 Lecture 15 John Shearer Culture Lab – space 2
Graphics Graphics & Graphical Programming Lecture 23 - Viewing & Clipping.
Lecture 9 From Vertices to Fragments. Objectives Introduce basic implementation strategies Clipping Rasterization hidden-surface removal.
CS552: Computer Graphics Lecture 12: 3D Clipping.
Two-Dimensional Viewing Hearn & Baker Chapter 6
CENG 538 Advanced Graphics and UIs
Computer Graphics Lecture 14 CLIPPING I Taqdees A. Siddiqi
Computer Graphic 2 D Viewing.
Computer Graphics Clipping.
Introduction to Computer Graphics with WebGL
Computer Graphics CC416 Week 13 Clipping.
Transformations contd.
CS 551 / 645: Introductory Computer Graphics
Computer Graphics Shading in OpenGL
Concepts, Algorithms for line clipping
Clipping Aaron Bloomfield CS 445: Introduction to Graphics Fall 2006
CSCE 441 Computer Graphics: Clipping Polygons Jinxiang Chai
Implementation I Ed Angel
Graphics Pipeline Clipping
WINDOWING AND CLIPPING
Computer Graphics : Viewing In 2D
Clipping Computer Graphics Cohen Sutherland Algorithm (Line)
CSCE 441 Computer Graphics: Clipping Lines Jinxiang Chai
CSCE 441 Computer Graphics: Clipping Polygons Jinxiang Chai
CSCE 441 Computer Graphics: Clipping Lines Jinxiang Chai
WINDOWING AND CLIPPING
Lecture 13 Clipping & Scan Conversion
CSCE 441 Computer Graphics: Clipping Polygons Jinxiang Chai
Joshua Barczak CMSC 435 UMBC
Two Dimensional Viewing and Clipping.
Segment Clipping Simple algorithm. For each segment compute the intersection with the four sides of the rectangle, and then determine which sub-segment.
© University of Wisconsin, CS559 Fall 2004
(c) 2002 University of Wisconsin, CS559
CS U540 Computer Graphics Prof. Harriet Fell Spring 2007
Clipping Polygons Dr. Scott Schaefer.
Clipping Clipping Sutherland-Hodgman Clipping
CS U540 Computer Graphics Prof. Harriet Fell Spring 2007
Computer Graphics Viewing. 2 of 30 Viewing in 2D Window in world coordinates. 45  250  Viewport in Device coords 250 x 250 Pixels.
Clipping University of British Columbia CPSC 314 Computer Graphics
Implementation I Ed Angel Professor Emeritus of Computer Science
CS U540 Computer Graphics Prof. Harriet Fell Spring 2007
COMPUTER GRAPHICS Clipping
Presentation transcript:

Concepts, algorithms for clipping

Overview Clipping Point Line clipping algorithms Cohen-Sutherland Liang-Barsky Polygon clipping algorithms Hodgman-Sutherland Weiler-Atherton

What is Clipping? The process of determining the portions of a primitive lying within a region called the ‘clipping region’ Performed in 2D (against window) and 3D (against volume) Types of clipping Point clipping Line clipping Polygon clipping

Clipping The purpose of clipping? For preventing Actively in one window from affecting pixels in other windows Mathematical overflow and underflow from primitives passing behind the eye point or at great distances (in 3D) Rasterisation is computationally expensive More or less linear with number of pixels created After clipping – only rasterise that which is in the viewable region A few clipping operations will save many other operations later on

Point Clipping Okay, this one’s easy! Given (x, y) check Note: Viewport does not necessarily have to be the same as screen size

Point Clipping Example application Used in scenes that are modelled with particle systems (e.g. clouds, smoke, explosions. etc.)

Line Clipping in 2D Shortened line passed to rasterisation stage There must be no difference in the final image

Line Clipping Four possible cases A. Both points inside B. Only one point inside C. Both points outside D. Both points outside, but part of line inside Case A is easy, but what about B, C, and D? Brute force approach Determine intersection of line segment with clipping window edge

Line Clipping First determine whether line segment completely inside or outside clipping window Use point-clipping test to test endpoints against all 4 clipping window boundaries If endpoints are both inside, then keep If both outside (the same clipping boundary), then remove If test fails Means that the line segment intersects at least one clipping boundary and may or may not cross into the interior of the clipping window

Parametric Line Formulation for Clipping A line segment can be represented as x = x0 + u(xend – x0) y = y0 + u(yend – y0) 0 ≤ u ≤ 1 For each clipping window edge Test whether line crossed that edge, by assigning that edge’s coordinate value to either x or y and solve for u If u outside range 0 to 1, line does not intersect window border If u within the range 0 to 1, then part of the line is inside that border Process this until entire line is clipped

Parametric Line Clipping x = x0 + u(xend – x0) y = y0 + u(yend – y0) 0 ≤ u ≤ 1 Example: Substitute xwindow min into equation and solve for u If u range within [0,1] then line intersects border Use u to find y intersection value

Line Clipping Intersection computations are expensive! A major goal of any line clipping algorithm is to minimise the intersection calculations Cohen-Sutherland clipping algorithm One of the earliest algorithms for fast line clipping Variations of this method are widely used Processing time reduced by performing more tests before resorting to intersection calculations Can be extended to 3D

Cohen-Sutherland Clipping For each endpoint, say (x, y) of a line segment assign a 4- bit region code If (x < xleft), bit 0 = 1 If (x > xlright), bit 1 = 1 If (y < ybottom), bit 2 = 1 If (y > ytop), bit 3 = 1

Cohen-Sutherland Clipping Cohen-Sutherland clipping algorithm Then go through these steps 1. If both endpoints 0000 (i.e. code 1 || code 2 = 0), ACCEPT (|| is bitwise OR) 2. Else If (code 1 && code 2) != 0, REJECT (&& is bitwise AND) 3. Else {Clip line against one viewport boundary 4. Assign the new endpoint with a 4-bit region code 5. Goto 1}

Cohen-Sutherland Clipping Algorithm If both endpoints 0000, ACCEPT Else If (code 1 && code 2) != 0, REJECT Else, { Clip line against one viewport boundary Assign the new endpoint with a 4-bit region code Goto 1}

Cohen-Sutherland Clipping Algorithm If both endpoints 0000, ACCEPT Else If (code 1 && code 2) != 0, REJECT Else, { Clip line against one viewport boundary Assign the new endpoint with a 4-bit region code Goto 1} Line clipped and new endpoints Generated in the following order: A’, B’, A”, then B”

Cohen-Sutherland Clipping in 3D Need 6-bit region codes Clip line segment against planes

Cohen-Sutherland Clipping in 3D Similar algorithm for using 3D outcodes to clip against canonical parallel view volume: xmin = ymin = -1; xmax = ymax = 1; else if LEFT then zmin = -1; zmax = 0; y = y0 + (y1 – y0) * (xmin – x0) / (x1 – x0); z = z0 + (z1 – z0) * (xmin – x0) / (x1 – x0); ComputeOutCode(x0, y0, z0, outcode0); x = xmin; ComputeOutCode(x1, y1, z1, outcode1); else if NEAR then repeat x = x0 + (x1 – x0) * (zmax – z0) / (z1 – z0); check for trivial reject or trivial accept y = y0 + (y1 – y0) * (zmax – z0) / (z1 – z0); pick the point that is outside the clip rectangle z = zmax; if TOP then else if FAR then x = x0 + (x1 – x0) * (ymax – y0) / (y1 – y0); x = x0 + (x1 – x0) * (zmin – z0) / (z1 – z0); z = z0 + (z1 – z0) * (ymax – y0) / (y1 – y0); y = y0 + (y1 – y0) * (zmin – z0) / (z1 – z0); y = ymax; z = zmin; else if BOTTOM then x = x0 + (x1 – x0) * (ymin – y0) / (y1 – y0); if (x0, y0, z0 is the outer point) then z = z0 + (z1 – z0) * (ymin – y0) / (y1 – y0); x0 = x; y0 = y; z0 = z; y = ymin; ComputeOutCode(x0, y0, z0, outcode0) else if RIGHT then else y = y0 + (y1 – y0) * (xmax – x0) / (x1 – x0); x1 = x; y1 = y; z1 = z; z = z0 + (z1 – z0) * (xmax – x0) / (x1 – x0); ComputeOutCode(x1, y1, z1, outcode1) x = xmax; until done

Liang-Barsky Clipping Parametric definition of a line segment with endpoints (x1, y1) and (x2, y2) x = x1 + u ∆x 0 ≤ u ≤ 1 y = y1 + u ∆y ∆x = (x2 + x1) ∆y = (y2 + y1) The goal is to find range of u for which x and y are both inside the viewport

Liang-Barsky Clipping Liang-Barsky clipping algorithm (cont.) Combine parametric equations with the point clipping tests x = x1 + u ∆x y = y1 + u ∆y This gives xleft ≤ x ≤ xright and ybottom ≤ y ≤ ytop xleft ≤ x1 + u ∆x ≤ xright ybottom ≤ y1 + u ∆y ≤ ytop

Liang-Barsky Clipping These four inequalities can be expressed as xleft ≤ x1 + u ∆x ≤ xright ybottom ≤ y1 + u ∆y ≤ ytop u pk ≤ qk , for k = 0, 1, 2, 3 4 inequalities p0 = - ∆x , q0 = x1 + xleft p1 = ∆x , q1 = xright + x1 p2 = - ∆y , q2 = y1 + ybottom p3 = ∆y , q3 = ytop + y1 (from xleft ≤ x1 + u ∆x ) (from x1 + u ∆x ≤ xright ) (from ybottom ≤ y1 + u ∆y ) (from y1 + u ∆y ≤ ytop )

Line Clipping Liang-Barsky clipping algorithm (cont.) Case 1: pk = 0, line is parallel to a corresponding viewpoint boundary Where k = 0, 1, 2, 3, correspond to left, right, bottom and top boundaries For the same value of k If qk < 0, then the line is completely outside Otherwise if qk ≥ 0, then line is inside u pk ≤ qk , for k = 0, 1, 2, 3

Line Clipping Liang-Barsky clipping algorithm (cont.) Case 2: pk ≠ 0, calculate rk = qk / pk for all boundaries (rk is the intersection of the (extended) line with the (extended) boundary) pk < 0 , line starts outside this boundary u1 = max (0, rk) pk > 0 , line starts inside this boundary u2 = min (1, rk) If u1 > u2 , the line is completely outside Else, endpoints calculated from u1 and u2 u pk ≤ qk , for k = 0, 1, 2, 3

Line Clipping Both the Cohen-Sutherland and Liang-Barsky clipping algorithms can be extended to 3D There are a number of other line clipping algorithms E.g. the Nicholl-Lee-Nicholl line clipping algorithm Even faster than the above mentioned methods, however this algorithm only works well for 2D

Area Clipping in 2D Not as simple as line segment clipping Clipping a line segment yields at most one line segment Clipping a polygon can yield multiple polygons However, clipping a convex polygon can yield at most one other polygon

Area Clipping Not as simple as line segment clipping Line clipping Polygon clipping

Area Clipping Sutherland-Hodgman polygon clipping Parse all polygon edges (either in clockwise or counter- clockwise direction) Clip edges against one boundary Repeat for other 3 boundaries 4 possible cases Out In : Save intersection point and endpoint In In : Save endpoint In Out : Save intersection point Out Out : Save nothing [Note: For intersection points, create intermediate vertices (these might be replaced later on)]

Sutherland-Hodgman Polygon Clipping

Sutherland-Hodgman Clipping

Sutherland-Hodgman Polygon Clipping

Sutherland-Hodgman Area Clipping Problem: Algorithm works fine for convex polygons, however extraneous lines may be displayed when clipping concave polygons

Area Clipping Weiler-Atherton polygon clipping More general, can be used to clip either convex or concave area filled polygons Instead of just tracing the polygon’s perimeter (i.e. the previous algorithm) When exit-intersection point encountered take a detour and trace along the clipping boundary Need to remember exit-intersection point in order to continue tracing from here later Edge traversal can be clockwise or counter-clockwise but must maintain same direction

Weiler-Atherton Clipping Process edges until [in out] pair of vertices encountered From exit-intersection point, trace clipping boundary until another intersection point If point was previously processed, goto next step Else (i.e. new intersection point) continue processing edges until a previously processed vertex is encountered Form the vertex list for this section of clipped fill area Return to the exit-intersection point and continue processing the polygon edges

Weiler-Atherton Polygon Clipping

Weiler-Atherton Clipping Can also be used to clip area filled polygons with Hollow areas Against a polygon-shaped clipping window

Thanks.. Questions?