Presentation is loading. Please wait.

Presentation is loading. Please wait.

2-Dimension Viewing and Clipping

Similar presentations


Presentation on theme: "2-Dimension Viewing and Clipping"— Presentation transcript:

1 2-Dimension Viewing and Clipping
Chapter 6 Except Text and Curve clipping Some of the material in these slides may have been adapted from University of Virginia, MIT, and Åbo Akademi University

2 Viewing Pipeline World Co-ordinates Window: Viewport
(x,y) values of all points in a single reference frame Window: It is the area of the world that was selected to be viewed Viewport It is the area on the display device on which the window is mapped View Transformation It is the process of mapping part of a scene in the world co-ordinate to an area on the display device Windows and viewports are usually rectangular In general, they can be anything (circular, oval, hexagona)

3 Viewing Pipeline Construct scene in world coordinate
using primitives and modeling Viewing coordinates usually defined within unit square Separates transformations from device specifics Convert world coordinate To viewing coordinates Map viewing coordinates to Normalized Viewing coordinates Map Normalized View port To device coordinates

4 Clipping Window and Viewport

5 Normalized Viewport

6 Screen Viewport

7

8 Why Viewing Pipeline Need to use any primitives, techniques, and models to construct scenes Need to construct scenes in standard coordinates Need to view from any angle and zoom Need to be able to display on any display device

9 Clipping We’ve been assuming that all primitives lie entirely within the viewport In general, this assumption does not hold

10 Clipping What is clipping?
Analytically calculating the portions of primitives within the viewport Primitives can be anything: point, line, polygon, circle, curves,…., etc.

11 Why Clip? Computer graphics benefits from being able to reduce the amount of work needed to draw objects. Objects will be inside the specified region but not visible and therefore skipped Bad idea to rasterize outside of framebuffer bounds Also, don’t waste time scan converting pixels outside window

12 Point Clipping Trivial xmin £ x £ xmax ymin£ y £ ymax
Required when scene is constructed from points (e.g. Explosion, sea foam,…, etc.) Not common (we usually do lines, polygons, curves,..,etc)

13 Line Clipping Typical cases
First, lets attempt to do a simple pre-processing Quickly determine complete acceptance or complete rejection

14 Trivial Accepts Big optimization: trivial accept/rejects
How can we quickly determine whether a line segment is entirely inside the viewport? A: test both endpoints. xmin xmax H ymax C A E F F G D B ymin

15 Trivial Accepts If both endpoints are within the view port
Completely accept xmin xmax H ymax C A E F F G D B ymin

16 Trivial Rejects How can we know a line is entirely outside viewport?
R: if both endpoints on wrong side of same edge, can trivially reject line xmin xmax H ymax C A E F F G D B ymin

17 Simple Line Clipping All other lines have end points (x1,y1) (x2,y2) such that one or both endpoints are outside the clipping boundaries Need to determine intersection points with boundaries Represent the line in parametric form x = x1 + t*(x2 - x1) y = y1 + t*(y2 - y1) t Î [0,1] Compute intersection points with the four boundaries xmin, xmax, ymin ,ymax (how can we do that quickly?) How can we quickly determine whether a line segment intersects a clipping boundary?

18 Simple Line Clipping If only one endpoint is inside
Display the segment from the boundary Intersection to the inside endpoint If both endpoints are outside Display the segment between the two intersections Disadvantages Computationally intensive More efficient approaches exist

19 Cohen-Sutherland Line Clipping
Divide viewplane into regions defined by viewport edges Assign each region a 4-bit outcode: xmin xmax 1001 1000 1010 ymax 0001 0000 0010 ymin 0101 0100 0110

20 Cohen-Sutherland Line Clipping
We generate this 4-bit classification using #define LEFT_EDGE 0x1 // 0001 #define RIGHT_EDGE 0x2 // 0010 #define BOTTOM_EDGE 0x4 // 0100 #define TOP_EDGE 0x8 // 1000 For each point (x,y) we calculate unsigned char code = 0x0 if ( x < xmin ) code = code | LEFT_EDGE if ( x > xmax ) code = code | RIGHT_EDGE if ( y < ymin ) code = code | BOTTOM_EDGE if ( y > ymax ) code = code | TOP_EDGE At the end, we have assigned a code for every endpoint

21 Cohen-Sutherland Line Clipping
Trivial accept Accept if both endpoints are inside How can we do that efficiently? Trivial reject Reject if both endpoints are on the wrong side of an edge How can we determine which wrong side?

22 Cohen-Sutherland Line Clipping
Trivial accept Accept if both endpoints are inside How can we do that efficiently? Trivial reject Reject of both endpoints are on the wrong side of an edge How can we do that efficiently How can we determine which wrong side? Use bitwise AND between the codes of the endpoints

23 Cohen-Sutherland Line Clipping Example
xmax xmin 1001 1000 H 1010 ymax C A E F F 0001 0000 0010 G D B ymin 0101 0100 0110

24 Cohen-Sutherland Line Clipping Algorithm
Given a line segment with endpoint p1(x1,y1) and p2(x2,y2) Compute the 4-bit codes for each endpoint. If both codes are 0000,(bitwise OR of the codes yields 0000 ) line lies completely inside the window: pass the endpoints to the draw routine. If both codes have a 1 in the same bit position (bitwise AND of the codes is not 0000), the line lies outside the window. It can be trivially rejected.

25 Cohen-Sutherland Line Clipping Algorithm
Lines that cannot be trivially accepted or rejected have one or both endpoints outside Examine one of the endpoints, say P1=(x1,y1). Read P1's 4-bit code in order: Left-to-Right, Bottom-to-Top. When a set bit (1) is found, compute the intersection I of the corresponding window edge with the line from P1 to P2. Replace P1 with I and repeat the algorithm. Discard portion on wrong side of edge and assign outcode to new vertex Apply trivial accept/reject tests; Repeat if necessary until the entire line is finished Proceed to the next line

26 Cohen-Sutherland Line Clipping Example
Pick J  Clip JI  Pick G  Clip GH Pick E  Clip ED  Pick A  Clip AB  Pick B  Clip BC E J xmax xmin 1001 1000 1010 ymax D I H 0001 0000 0010 G C ymin 0101 0100 0110 B A

27 Cohen-Sutherland Line Clipping
We have accepted/discarded lines inside/outside the window We have chosen the subsection of the line which is inside the window Algorithm can be modified for 3D Still not efficient enough Need to compute up to 4 intersections per line Fundamentally more efficient algorithms: Liang-Barsky uses parametric lines Nicholl-Lee-Nicholl

28 Polygon Clipping Convex and Concave Splitting Concave Polygon
Sutherland-Hodgman Clipping

29 Convex and Concave Definition: A closed shape (not necessarily a polygon) is said to be convex if the straight line between two internal points lies completely inside the shape`

30 Splitting Concave Polygon
Detecting concavity: Calculate cross products of consecutive polygon edges If there is at least one change of sign in z-component, then the polygon is concave How to detect concavity on a continuous curve?

31 Splitting Concave Polygon
Some polygon clipping algorithm assume convex polygon Concave polygons can be decomposed into convex polygons. There are Several solutions! Vector Method Rational Method Both method work non-intersecting polygons only

32 Splitting Concave Polygon Vector Method
Walk around the polygon in counter-clockwise direction Cross product every consecutive edges When the sign of (En x En+1) changes, split along En Calculate new edge and the new polygon

33 Splitting Concave Polygon Rational Method
Walk the polygon counter-clockwise For each two consecutive edges E1, E2 consisting of V1, V2, V3 Translate V1 to origin Rotate clockwise such that V1,V2 is on the x-axis If line is below X-axis (I.e. y3 < 0) Polygon is concave Split along x-axis

34 Clipping Polygons Clipping polygons is more complex than clipping the individual lines Input: polygon Output: polygon, or nothing Simply applying line clipping would not work !! We do not want broken lines. We want a polygon

35 Why Is Polygon Clipping Hard?
Many possible outcomes: triangletriangle trianglequad triangle5-gon triangle6-gon triangle7-gon

36 Why Is Polygon Clipping Hard?
A really tough case:

37 Why Is Polygon Clipping Hard?
A really tough case: concave polygonmultiple polygons

38 Why Is Polygon Clipping Hard?
Not easy to trivially reject Even if most of lines can be trivially rejected, we may still need to display a polygon

39 Sutherland-Hodgman Clipping Basic Idea
Initial Input: Polygon = {ordered list of vertices} Process only against one clipping boundary at a time! Clip each line in the polygon against each clip boundary in turn: left, right, bottom, top. Basic steps Input: Polygon = {ordered list of vertices} After finishing each boundary, the output vertex list is generated The output vertex list is the new polygon input to the next boundary Final output list of vertices is the clipped polygon

40 Sutherland-Hodgman Clipping Rules to generate the Vertex list
Apply the following rules for every pair of vertices If the first vertex is outside and the second is inside, then move the first vertex to the clipping boundary and accept both vertices. Calculate intersection point with the boundary Replace the first vertex with the intersection point Put the new first vertex and the second vertex in the output list If both vertices are inside, put the second vertex in the output list. If the first vertex is inside and the second is outside, then move the second to the clipping boundary and accept the second. Replace the second vertex with the intersection point Put the new second vertex in the output list If both vertices are outside, reject both. How to determine wither a point is inside or outside a given boundary?

41 Sutherland-Hodgman Clipping How to determine Inside and Outside
Lets say a line endpoint has the co-rdinates (x,y) Inside Xmin means x > Xmin Inside Xmax means x < Xmax Inside Ymin means y > Ymin Inside Ymax means y < Ymax

42 Sutherland-Hodgman Clipping Illustration
outin Save V’1, V2 inin Save V2 inout Save V’1 outout Save non

43 Sutherland-Hodgman Clipping Example
V3 V2 V1

44 Sutherland-Hodgman Clipping Example
V2V3V2’, V3 V3V1V1 V1V2V1’ Final List: V2’,V3,V1,V1’ V3 V’2 V2 V’1 V1

45 Sutherland-Hodgman Clipping Example
V2’V3V3 V3V1V3’ V1V1’ V1’V2’V2’’V2’ Final List: V3,V3’,V2’’,V2’ V3 V’2 V’’2 V’3 V’1 V1

46 Sutherland-Hodgman Clipping Example
V2’V3V3 V3V1V3’ V1V1’ V1’V2’V2’’V2’ Final List: V3,V3’,V2’’,V2’ V3 V’2 V’’2 V’3 V’1 V1

47 Sutherland-Hodgman Clipping Another Example

48 Sutherland-Hodgman Clipping Another Example

49 Sutherland-Hodgman Clipping Another Example

50 Sutherland-Hodgman Clipping

51 Sutherland-Hodgman Clipping

52 Sutherland-Hodgman Clipping

53 Sutherland-Hodgman Clipping

54 Sutherland-Hodgman Clipping

55 Sutherland-Hodgman Clipping

56 Sutherland-Hodgman Clipping

57 Sutherland-Hodgman Clipping
Convex polygons correctly processed Concave polygons may produce extraneous lines We only have one list Last point is always joined to first We need two vertex lists because the polygon is divided into two polygons 6 6 5 5 4 4 3 3 1 2 1 2

58


Download ppt "2-Dimension Viewing and Clipping"

Similar presentations


Ads by Google