CSCE 441 Computer Graphics: Clipping Lines Jinxiang Chai

Slides:



Advertisements
Similar presentations
Computer Graphics Inf4/MSc 1 Computer Graphics Lecture 7 Scanline algorithm and polygon clipping Taku Komura.
Advertisements

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.
CS 352: Computer Graphics Chapter 7: The Rendering Pipeline.
CS 551 / 645: Introductory Computer Graphics Clipping Lines and Polygons.
Dr. Scott Schaefer Clipping Lines. 2/94 Why Clip? We do not want to waste time drawing objects that are outside of viewing window (or clipping window)
Clipping Lines Lecture 7 Wed, Sep 10, The Graphics Pipeline From time to time we will discuss the graphics pipeline. The graphics pipeline is the.
Computer Graphics : Clipping
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.
University of British Columbia CPSC 314 Computer Graphics Jan-Apr 2005 Tamara Munzner Interpolation Clipping.
1 Computer Graphics Chapter 4 2D Viewing Algorithms.
CSCE 441: Computer Graphics Scan Conversion of Polygons
1 CSCE 441 Computer Graphics: Clipping Lines Jinxiang Chai.
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.
1 CSCE 441: Computer Graphics Scan Conversion of Polygons Jinxiang Chai.
2-Dimension Viewing and Clipping
CGMB 314 Intro to Computer Graphics Fill Area Primitives.
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)
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 Lecture 20 Fasih ur Rehman. Last Class Clipping – What is clipping – Why we do clipping – How clipping is done.
1Computer Graphics Implementation 1 Lecture 15 John Shearer Culture Lab – space 2
Graphics Graphics & Graphical Programming Lecture 23 - Viewing & Clipping.
1 CSCE 441: Computer Graphics Scan Conversion of Polygons Jinxiang Chai.
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
Concepts, algorithms for clipping
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 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.
(c) 2002 University of Wisconsin, CS559
CS U540 Computer Graphics Prof. Harriet Fell Spring 2007
Clipping Clipping Sutherland-Hodgman Clipping
CS U540 Computer Graphics Prof. Harriet Fell Spring 2007
Type to enter a caption. Computer Graphics Week 4 Lecture 2.
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:

CSCE 441 Computer Graphics: Clipping Lines Jinxiang Chai

OpenGL Geometric Primitives All geometric primitives are specified by vertices GL_LINE_STRIP GL_LINE_LOOP GL_POLYGON GL_LINES GL_POINTS GL_TRIANGLE_STRIP GL_QUAD_STRIP GL_TRIANGLES GL_QUADS GL_TRIANGLE_FAN

Why Clip? We do not want to waste time drawing objects that are outside of viewing window (or clipping window)

Clipping Points Given a point (x, y) and clipping window (xmin, ymin), (xmax, ymax), determine if the point should be drawn (xmax,ymax) (x,y) (xmin,ymin)

Clipping Points Given a point (x, y) and clipping window (xmin, ymin), (xmax, ymax), determine if the point should be drawn (xmax,ymax) (x,y) xmin=<x<=xmax? (xmin,ymin) ymin=<y<=ymax?

Clipping Points Given a point (x, y) and clipping window (xmin, ymin), (xmax, ymax), determine if the point should be drawn (xmax,ymax) (x2,y2) (x1,y1) xmin=<x<=xmax? (xmin,ymin) ymin=<y<=ymax?

Clipping Points Given a point (x, y) and clipping window (xmin, ymin), (xmax, ymax), determine if the point should be drawn (xmax,ymax) (x2,y2) (x1,y1) xmin=<x1<=xmax Yes (xmin,ymin) ymin=<y1<=ymax Yes

Clipping Points Given a point (x, y) and clipping window (xmin, ymin), (xmax, ymax), determine if the point should be drawn (xmax,ymax) (x2,y2) (x1,y1) xmin=<x2<=xmax No (xmin,ymin) ymin=<y2<=ymax Yes

Clipping Lines

Clipping Lines

Clipping Lines Given a line with end-points (x0, y0), (x1, y1) and clipping window (xmin, ymin), (xmax, ymax), determine if line should be drawn and clipped end-points of line to draw. (xmax,ymax) (x1, y1) (x0, y0) (xmin,ymin)

Clipping Lines

Outline Simple line clipping algorithm Cohen-Sutherland Liang-Barsky Required readings: HB 8-5, 8-6, 8-7 and 8-9

Clipping Lines

Clipping Lines – Simple Algorithm If both end-points inside rectangle, draw line If one end-point outside, intersect line with all edges of rectangle clip that point and repeat test

Clipping Lines – Simple Algorithm

Clipping Lines – Simple Algorithm

Intersecting Two Lines

Intersecting Two Lines

Intersecting Two Lines

Intersecting Two Lines

Intersecting Two Lines

Intersecting Two Lines Substitute t or s back into equation to find intersection

Clipping Lines – Simple Algorithm

Clipping Lines – Simple Algorithm

Clipping Lines – Simple Algorithm

Clipping Lines – Simple Algorithm

Clipping Lines – Simple Algorithm

Clipping Lines – Simple Algorithm

Clipping Lines – Simple Algorithm

Clipping Lines – Simple Algorithm

Clipping Lines – Simple Algorithm

Clipping Lines – Simple Algorithm

Clipping Lines – Simple Algorithm

Clipping Lines – Simple Algorithm

Clipping Lines – Simple Algorithm

Clipping Lines – Simple Algorithm

Clipping Lines – Simple Algorithm

Clipping Lines – Simple Algorithm Lots of intersection tests makes algorithm expensive Complicated tests to determine if intersecting rectangle Is there a better way?

Trivial Accepts Big Optimization: trivial accepts/rejects How can we quickly decide whether line segment is entirely inside window Answer: test both endpoints

Trivial Accepts Big Optimization: trivial accepts/rejects How can we quickly decide whether line segment is entirely inside window Answer: test both endpoints

Trivial Rejects How can we know a line is outside of the window Answer:

Trivial Rejects How can we know a line is outside of the window Answer: Wrong side

Trivial Rejects How can we know a line is outside of the window Answer: both endpoints on wrong side of same edge, can trivially reject the line

Cohen-Sutherland Algorithm Classify p0, p1 using region codes c0, c1 If , trivially reject If , trivially accept Otherwise reduce to trivial cases by splitting into two segments

Cohen-Sutherland Algorithm Every end point is assigned to a four-digit binary value, i.e. Region code Each bit position indicates whether the point is inside or outside of a specific window edges bit 4 bit 3 bit 2 bit 1 top bottom right left

Cohen-Sutherland Algorithm bit 4 bit 3 bit 2 bit 1 top bottom right left top right left Region code? bottom

Cohen-Sutherland Algorithm bit 4 bit 3 bit 2 bit 1 top bottom right left top right left 0010 bottom ?

Cohen-Sutherland Algorithm bit 4 bit 3 bit 2 bit 1 top bottom right left top right left 0010 bottom 0100

Cohen-Sutherland Algorithm

Cohen-Sutherland Algorithm

Cohen-Sutherland Algorithm Classify p0, p1 using region codes c0, c1 If , trivially reject because both points located the wrong side of one edge. If , trivially accept because both points are not in the wrong side of any edge. Otherwise reduce to trivial cases by splitting into two segments

Cohen-Sutherland Algorithm Bitwise and

Cohen-Sutherland Algorithm Bitwise and Bitwise or

Cohen-Sutherland Algorithm Bitwise and Bitwise or

Cohen-Sutherland Algorithm Classify p0, p1 using region codes c0, c1 If , trivially reject If , trivially accept Otherwise reduce to trivial cases by splitting into two segments

Cohen-Sutherland Algorithm Classify p0, p1 using region codes c0, c1 If , trivially reject If , trivially accept Otherwise reduce to trivial cases by splitting into two segments Line is outside the window! reject

Cohen-Sutherland Algorithm Classify p0, p1 using region codes c0, c1 If , trivially reject If , trivially accept Otherwise reduce to trivial cases by splitting into two segments Line is outside the window! reject

Cohen-Sutherland Algorithm Classify p0, p1 using region codes c0, c1 If , trivially reject If , trivially accept Otherwise reduce to trivial cases by splitting into two segments Line is inside the window! draw

Cohen-Sutherland Algorithm Classify p0, p1 using region codes c0, c1 If , trivially reject If , trivially accept Otherwise reduce to trivial cases by splitting into two segments

Window Intersection (x1, y1), (x2, y2) intersect with vertical edge at xright yintersect = y1 + m(xright – x1) where m=(y2-y1)/(x2-x1) (x1, y1), (x2, y2) intersect with horizontal edge at ybottom xintersect = x1 + (ybottom – y1)/m

Example 1

Example 1

Example 1

Example 1

Example 2

Example 2

Example 2

Example 2

Example 2

Example 2

Example 2

Example 3

Example 3

Example 3

Example 3

Cohen-Sutherland Algorithm Extends easily to 3D line clipping 27 regions 6 bits

Cohen-Sutherland Algorithm Use region codes to quickly eliminate/include lines Best algorithm when trivial accepts/rejects are common Must compute viewing window clipping of remaining lines Non-trivial clipping cost More efficient algorithms exist

Liang-Barsky Algorithm Parametric definition of a line: x = x1 + uΔx y = y1 + uΔy Δx = (x2-x1), Δy = (y2-y1), 0<=u<=1 Lines are oriented: classify lines as moving inside to out or outside to in Goal: find range of u for which x and y both inside the viewing window

Liang-Barsky Algorithm Current clipped line (u1=0,u2=1) (2,1) (-.5,-.5)

Liang-Barsky Algorithm Current clipped line (u1=0.333,u2=0.6) (2,1) (-.5,-.5)

Liang-Barsky Algorithm For lines starting outside of boundary, update its starting point (u1) For lines starting inside of boundary, update end point (u2) For lines paralleling the boundaries and outside window, reject it.

Liang-Barsky Algorithm For lines starting outside of boundary, update its starting point (u1) For lines starting inside of boundary, update end point (u2) For lines paralleling the boundaries and outside window, reject it.

Liang-Barsky Algorithm Mathematically: xmin <= x1 + uΔx <= xmax ymin <= y1 + uΔy <= ymax Rearranged 1: u*(-Δx) <= (x1 – xmin) 2: u*(Δx) <= (xmax – x1) 3: u*(-Δy) <= (y1 – ymin) 4: u*(Δy) <= (ymax – y1) gen: u*(pk) <= (qk), k=1,2,3,4

Liang-Barsky Algorithm Mathematically: xmin <= x1 + uΔx <= xmax ymin <= y1 + uΔy <= ymax Rearranged 1: u*(-Δx) <= (x1 – xmin) // left edge 2: u*(Δx) <= (xmax – x1) 3: u*(-Δy) <= (y1 – ymin) 4: u*(Δy) <= (ymax – y1) gen: u*(pk) <= (qk), k=1,2,3,4

Liang-Barsky Algorithm Mathematically: xmin <= x1 + uΔx <= xmax ymin <= y1 + uΔy <= ymax Rearranged 1: u*(-Δx) <= (x1 – xmin) 2: u*(Δx) <= (xmax – x1) // right edge 3: u*(-Δy) <= (y1 – ymin) 4: u*(Δy) <= (ymax – y1) gen: u*(pk) <= (qk), k=1,2,3,4

Liang-Barsky Algorithm Mathematically: xmin <= x1 + uΔx <= xmax ymin <= y1 + uΔy <= ymax Rearranged 1: u*(-Δx) <= (x1 – xmin) 2: u*(Δx) <= (xmax – x1) 3: u*(-Δy) <= (y1 – ymin) // bottom edge 4: u*(Δy) <= (ymax – y1) gen: u*(pk) <= (qk), k=1,2,3,4

Liang-Barsky Algorithm Mathematically: xmin <= x1 + uΔx <= xmax ymin <= y1 + uΔy <= ymax Rearranged 1: u*(-Δx) <= (x1 – xmin) 2: u*(Δx) <= (xmax – x1) 3: u*(-Δy) <= (y1 – ymin) 4: u*(Δy) <= (ymax – y1) // top edge gen: u*(pk) <= (qk), k=1,2,3,4

Liang-Barsky Algorithm Mathematically: xmin <= x1 + uΔx <= xmax ymin <= y1 + uΔy <= ymax Rearranged 1: u*(-Δx) <= (x1 – xmin) 2: u*(Δx) <= (xmax – x1) 3: u*(-Δy) <= (y1 – ymin) 4: u*(Δy) <= (ymax – y1) Gen: u*(pk) <= (qk), k=1,2,3,4

Liang-Barsky Algorithm Rules: pk = 0: the line is parallel to boundaries If for that same k, qk < 0, it’s outside Otherwise it’s inside pk < 0: the line starts outside this boundary rk = qk/pk u1 = max(0, rk, u1) /**update starting point**/ pk > 0: the line starts inside the boundary u2 = min(1, rk, u2) /** update end point**/ If u1 > u2, the line is completely outside

Liang-Barsky Algorithm Current clipped line (u1=0,u2=1) (2,1) (-.5,-.5)

Liang-Barsky Algorithm Current clipped line (u1=0,u2=1) (2,1) Δx = (x2-x1)=2.5 Δy = (y2-y1)=1.5 p1 = -Δx =-2.5<0 (-.5,-.5) q1 = (x1-xmin)=-.5 r1 = q1/p1=0.2

Liang-Barsky Algorithm Current clipped line (u1=0,u2=1) (2,1) Δx = (x2-x1)=2.5 Δy = (y2-y1)=1.5 p1 = -Δx =-2.5<0 (-.5,-.5) q1 = (x1-xmin)=-.5 line starts outside this boundary r1 = q1/p1=0.2

Liang-Barsky Algorithm Current clipped line (u1=0,u2=1) (2,1) Δx = (x2-x1)=2.5 Δy = (y2-y1)=1.5 p1 = -Δx =-2.5<0 (-.5,-.5) q1 = (x1-xmin)=-.5 r1 = q1/p1=0.2

Liang-Barsky Algorithm Current clipped line (u1=0,u2=1) (2,1) Δx = (x2-x1)=2.5 Δy = (y2-y1)=1.5 p1 = -Δx =-2.5<0 (-.5,-.5) q1 = (x1-xmin)=-.5 r1 = q1/p1=0.2

Liang-Barsky Algorithm Current clipped line (u1=0,u2=1) (2,1) Δx = (x2-x1)=2.5 Δy = (y2-y1)=1.5 p1 = -Δx =-2.5<0 (-.5,-.5) q1 = (x1-xmin)=-.5 u1 = max(0, r1, u1) r1 = q1/p1=0.2

Liang-Barsky Algorithm Current clipped line (u1=0.2,u2=1) (2,1) u1<u2? yes: continue no: the line is completely outside window (-.5,-.5)

Liang-Barsky Algorithm Current clipped line (u1=0.2,u2=1) (2,1) (-.5,-.5)

Liang-Barsky Algorithm Current clipped line (u1=0.2,u2=1) (2,1) Δx = (x2-x1)=2.5 Δy = (y2-y1)=1.5 p2 = Δx =2.5>0 (-.5,-.5) q2 = (xmax-x1)=1.5 r2 = q2/p2=0.6

Liang-Barsky Algorithm Current clipped line (u1=0.2,u2=1) (2,1) Δx = (x2-x1)=2.5 Δy = (y2-y1)=1.5 p2 = Δx =2.5>0 (-.5,-.5) q2 = (xmax-x1)=1.5 u2 = min(1, r2, u2) r2 = q2/p2=0.6

Liang-Barsky Algorithm Current clipped line (u1=0.2,u2=0.6) (2,1) Δx = (x2-x1)=2.5 Δy = (y2-y1)=1.5 p2 = Δx =2.5>0 (-.5,-.5) q2 = (xmax-x1)=1.5 r2 = q2/p2=0.6

Liang-Barsky Algorithm Current clipped line (u1=0.2,u2=0.6) (2,1) u1<u2? yes: continue no: the line is completely outside window (-.5,-.5)

Liang-Barsky Algorithm Current clipped line (u1=0.2,u2=0.6) (2,1) Δx = (x2-x1)=2.5 Δy = (y2-y1)=1.5 p3 = -Δy =-1.5<0 (-.5,-.5) q3 = (y1-ymin)=0.5 r3 = q3/p3=0.333

Liang-Barsky Algorithm Current clipped line (u1=0.2,u2=0.6) (2,1) Δx = (x2-x1)=2.5 Δy = (y2-y1)=1.5 p3 = -Δy =-1.5<0 (-.5,-.5) q3 = (y1-ymin)=0.5 u1 = max(0, r3, u1) r3 = q3/p3=0.333

Liang-Barsky Algorithm Current clipped line (u1=0.333,u2=0.6) (2,1) Δx = (x2-x1)=2.5 Δy = (y2-y1)=1.5 p3 = -Δy =-1.5<0 (-.5,-.5) q3 = (y1-ymin)=0.5 r3 = q3/p3=0.333

Liang-Barsky Algorithm Current clipped line (u1=0.333,u2=0.6) (2,1) (-.5,-.5)

Liang-Barsky Algorithm Current clipped line (u1=0.333,u2=0.6) (2,1) Δx = (x2-x1)=2.5 Δy = (y2-y1)=1.5 p3 = -Δy =-1.5<0 (-.5,-.5) q3 = (y1-ymin)=0.5 r3 = q3/p3=0.333

Liang-Barsky Algorithm Current clipped line (u1=0.333,u2=0.6) (2,1) Δx = (x2-x1)=2.5 Δy = (y2-y1)=1.5 p4 = Δy =1.5>0 (-.5,-.5) q4 = (ymax-y1)=1.5 r4 = q4/p4=1

Liang-Barsky Algorithm Current clipped line (u1=0.333,u2=0.6) (2,1) Δx = (x2-x1)=2.5 Δy = (y2-y1)=1.5 p4 = Δy =1.5>0 (-.5,-.5) q4 = (ymax-y1)=1.5 u2 = min(1, r4, u2) r4 = q4/p4=1

Liang-Barsky Algorithm Check the left edge (u1=0.333,u2=0.6) (2,1) u1<u2 (-.5,-.5) xnew1=x1+Δx*u1 xnew2=x2+Δx*u2 ynew1=y1+Δy*u1 ynew2=y2+Δy*u2

Liang-Barsky Algorithm Faster than Cohen-Sutherland Extension to 3D is easy - Parametric representation for 3D lines - Compute u1,u2 based on the intersection between line and plane

Comparison Cohen-Sutherland Liang-Barsky Repeated clipping is expensive Best used when trivial acceptance and rejection is possible for most lines Liang-Barsky Computation of t-intersections is cheap (only one division) Computation of (x,y) clip points is only done once Algorithm doesn’t consider trivial accepts/rejects Best when many lines must be clipped

Curve Clipping

Curve Clipping

Curve Clipping Approximate a curve using a set of straight-line segments Apply line clipping for curve clipping

Next Lecture Polygon fill-area clipping

Next Lecture Polygon fill-area clipping