Вы находитесь на странице: 1из 14

A window defines a rectangular area in world coordinates. A window can be defined with a GWINDOW statement.

The window can be defined to be larger than, the same size as or smaller than the actual range of data values, depending on whether we want to show all of the data or only part of the data. A viewport defines in normalized coordinates a rectangular area on the display device where the image of the data appears. A viewport is defined with the GPORT command. We can have our graph take up the entire display device or show it in only a portion, say the upper-right part. A window and a viewport are related by the linear transformation that maps the window onto the viewport. A line segment in the window is mapped to a line segment in the viewport such that the relative positions are preserved. Any procedure which identifies that portion of a picture which is either inside or outside a region is referred to as a clipping algorithm or clipping. The region against which an object is to be clipped is called clipping window. In 2D graphics for example, if the user of an image editing program is modifying an image and has "zoomed in" the view to display only the top half of the image, there is no need for the program to spend any CPU time doing any of the calculations or memory moves needed to display the bottom half. By clipping the bottom half of the image and avoiding these calculations, the program runs faster. In 3D graphics, in a city street scene the computer may have model, texture, and shader data in memory for every building in the city; but since the camera viewing the scene only sees things within, say, a 90 angle, or field of view, the computer does not need to transform, texture, and shade the buildings that are behind the camera, nor those which are far enough to the side that they are off the screen. The clipping algorithm lets the rendering code skip all consideration of those buildings, and the program runs faster. Non-triviality Clipping is non-trivial, especially for 3D animations: if the objects are built up of polygons, a routine is needed that determines for each polygon whether it is visible within theviewport (i.e. the part of the 3D "world" displayed) or cut off the borders. Special care is needed for the case of polygons intersected by the viewport border as their shape has to be adjusted. While the term "clipping" is generally used to mean avoiding the drawing of things outside the camera's field of view, a related technique is occlusion culling, in which polygonswithin the field of view are not drawn if they would be occluded by other

polygons. For example, there is no need to render the polygons composing a car if a building stands between the observer and the car, completely obscuring it; the cars' polygons are all completely occluded by the building. Hence the software can save significant rendering time by doing an occlusion culling pass before deciding which polygons to draw. The clipping, back-face and occlusion culling optimizations both present interesting problems in scenes with a reflective surface visible. For example, problems would ensue if the 3D scene contained a mirror that showed the reflection of a building that had been clipped because the building was behind the camera. To deal with 'true' reflective surfaces (as opposed to the 'fake' reflections of environment maps), the clipper might do a clipping and rendering pass from the point of view of the mirror, and then the normal clipping pass for the camera. Importance of clipping in video games Good clipping strategy is important in the development of video games in order to maximize the game's frame rate and visual quality. Despite GPU chips that are faster every year, it remains computationally expensive to transform, texture, and shade polygons, especially with the multiple texture and shading passes common today. Hence, game developersmust live within a certain "budget" of polygons that can be drawn each video frame. To maximize the game's visual quality, developers prefer to establish the highest possible polygon budget; therefore, every optimization of the graphics pipeline benefits the polygon budget and therefore the game. In video games, then, clipping is a critically important optimization that speeds up the rendering of the current scene, and therefore allows the developer to increase the renderer's polygon budget. Programmers often devise clever heuristics to speed up the clipper, as it would be computationally prohibitive to use line casting or ray tracing to determine with 100% accuracy which polygons are and are not within the camera's field of view. One of the most popular methods for optimization is the use of octrees to partition scenes into rendered and non-rendered areas. The clipping problems introduced by reflective surfaces are generally avoided in games as of 2005 by simulating reflections without actually doing all the calculations that would be necessary for accurate reflections.

Due to the use of the term 'no clipping' to refer to turning off collision detection, the two are often confused. In 3D gaming, "to clip through" is also sometimes used to refer to the situation in which part of a model passes through part of another in an unnatural manner, like a leg passing through a cape when running. Algorithms Several clipping algorithms have been devised.

Line clipping algorithms: CohenSutherland LiangBarsky Fast-clipping CyrusBeck NichollLeeNicholl Fast culling for 2D side-scrolling games Skala O(lg N) Algorithm

Circle and B-Splines clipping algorithms; Patrick-Gilles Maillot's Thesis on computer Graphics, pages 52 (circle), 53 (B-Splines) and respective algorithms pages 95 and 97.

Polygon clipping algorithms: SutherlandHodgman WeilerAtherton Vatti Patrick-Gilles Maillot 2D polygon clipping using integer or floating point - Sun Microsystems' patent #5079719, 1989.

Polygon Structures clipping algorithms: Triangle Strips and Quad-Meshes 3D Homogeneous Clipping (PatrickGilles Maillot)

CohenSutherland algorithm From Wikipedia, the free encyclopedia (Redirected from CohenSutherland) The CohenSutherland algorithm is a computer graphics algorithm used for line clipping. The algorithm divides a two-dimensional space into 9 regions (or a threedimensional space into 27 regions), and then efficiently determines the lines and portions of lines that are visible in the center region of interest (the viewport). The algorithm was developed in 1967 during flight simulator work by Danny Cohen and Ivan Sutherland.[1] Contents [hide] 1 The algorithm
o

1.1 Example C/C++ implementation 2 Notes 3 See also 4 References 5 External links

The algorithm[edit] The algorithm includes, excludes or partially includes the line based on where:

Both endpoints are in the viewport region (bitwise OR of endpoints == 0): trivial accept. Both endpoints share at least one non-visible region which implies that the line does not cross the visible region. (bitwise AND of endpoints != 0): trivial reject. Both endpoints are in different regions: In case of this nontrivial situation the algorithm finds one of the two points that is outside the viewport region (there will be at least one point outside). The intersection of the outpoint and extended viewport border is then calculated (i.e. with the parametric equation for the line) and this new point replaces the outpoint. The algorithm repeats until a trivial accept or reject occurs.

The numbers in the figure below are called outcodes. An outcode is computed for each of the two points in the line. The outcode will have four bits for two-dimensional clipping, or six bits in the three-dimensional case. The first bit is set to 1 if the point is above the viewport. The bits in the 2D outcode represent: Top, Bottom, Right, Left. For example the outcode 1010 represents a point that is top-right of the viewport. Note that the outcodes for endpoints must be recalculated on each iteration after the clipping occurs. 1001 1000 1010 0001 0000 0010 0101 0100 0110 The CohenSutherland algorithm can be used only on a rectangular clipping area. For other convex polygon clipping windows, use the CyrusBeck algorithm. Example C/C++ implementation[edit] typedef int OutCode; const int INSIDE = 0; // 0000 const int LEFT = 1; // 0001 const int RIGHT = 2; // 0010 const int BOTTOM = 4; // 0100 const int TOP = 8; // 1000 // Compute the bit code for a point (x, y) using the clip rectangle // bounded diagonally by (xmin, ymin), and (xmax, ymax) // ASSUME THAT xmax, xmin, ymax and ymin are global constants. OutCode ComputeOutCode(double x, double y) { OutCode code; code = INSIDE; // initialised as being inside of clip window code = INSIDE; // initialised as being inside of clip window code |= LEFT; else if (x > xmax) // to the right of clip window code |= RIGHT; if (y < ymin) // below the clip window code |= BOTTOM; else if (y > ymax) // above the clip window code |= TOP; return code; } // CohenSutherland clipping algorithm clips a line from // P0 = (x0, y0) to P1 = (x1, y1) against a rectangle with // diagonal from (xmin, ymin) to (xmax, ymax). void CohenSutherlandLineClipAndDraw(double x0, double y0, double x1, double y1)

{ // compute outcodes for P0, P1, and whatever point lies outside the clip rectangle OutCode outcode0 = ComputeOutCode(x0, y0); OutCode outcode1 = ComputeOutCode(x1, y1); bool accept = false; while (true) { if (!(outcode0 | outcode1)) { // Bitwise OR is 0. Trivially accept and get out of loop accept = true; break; } else if (outcode0 & outcode1) { // Bitwise AND is not 0. Trivially reject and get out of loop break; } else { // failed both tests, so calculate the line segment to clip // from an outside point to an intersection with clip edge double x, y; // At least one endpoint is outside the clip rectangle; pick it. OutCode outcodeOut = outcode0 ? outcode0 : outcode1; // Now find the intersection point; // use formulas y = y0 + slope * (x - x0), x = x0 + (1 / slope) * (y - y0) if (outcodeOut & TOP) { // point is above the clip rectangle x = x0 + (x1 - x0) * (ymax - y0) / (y1 - y0); y = ymax; } else if (outcodeOut & BOTTOM) { // point is below the clip rectangle x = x0 + (x1 - x0) * (ymin - y0) / (y1 - y0); y = ymin; } else if (outcodeOut & RIGHT) { // point is to the right of clip rectangle y = y0 + (y1 - y0) * (xmax - x0) / (x1 - x0); x = xmax; } else if (outcodeOut & LEFT) { // point is to the left of clip rectangle y = y0 + (y1 - y0) * (xmin - x0) / (x1 - x0); x = xmin; } // Now we move outside point to intersection point to clip // and get ready for next pass. if (outcodeOut == outcode0) { x0 = x; y0 = y; outcode0 = ComputeOutCode(x0, y0); } else { x1 = x; y1 = y; outcode1 = ComputeOutCode(x1, y1); }

} } if (accept) { // Following functions are left for implementation by user based on // their platform (OpenGL/graphics.h etc.) DrawRectangle(xmin, ymin, xmax, ymax); LineSegment(x0, y0, x1, y1); } } LiangBarsky algorithm From Wikipedia, the free encyclopedia (Redirected from LiangBarsky)

The idea of the Liang-Barsky clipping algorithm is to do as much testing as possible before computing line intersections. Consider first the usual parametric form of a straight line:

A point is in the clip window, if

and , which can be expressed as the 4 inequalities , where (left) (right) (bottom) (top) To compute the final line segment: 1. A line parallel to a clipping window edge has for that boundary.

2. If for that , , the line is completely outside and can be eliminated. 3. When the line proceeds outside to inside the clip window and when , the line proceeds inside to outside. 4. For nonzero , gives the intersection point. 5. For each line, calculate and . For , look at boundaries for which (outside -> in). Take to be the largest among for which . For , look at boundaries (inside -> out). Take ,

to be the minimum of . If the line is outside and therefore rejected. CyrusBeck algorithm From Wikipedia, the free encyclopedia The CyrusBeck algorithm is a generalized line clipping algorithm. It was designed to be more efficient than the SutherlandCohen algorithm which uses repetitive clipping. [1] CyrusBeck is a general algorithm and can be used with a convex polygon clipping window unlike Sutherland-Cohen that can be used only on a rectangular clipping area. Here the parametric equation of a line in the view plane is:

where

Now to find intersection point with the clipping window we calculate value of dot product. Let pE be a point on the clipping plane E. Calculate .

if > 0 vector pointed towards interior

if = 0 vector pointed parallel to plane containing p if < 0 vector pointed away from interior Here n stands for normal of the current clipping plane (pointed away from interior). By this we select the point of intersection of line and clipping window where (dot product = 0 ) and hence clip the line. The NichollLeeNicholl algorithm is a fast line clipping algorithm that reduces the chances of clipping a single line segment multiple times, as may happen in the CohenSutherland algorithm. Description[edit] Using the NichollLeeNicholl algorithm, the area around the clipping window is divided into a number of different areas, depending on the position of the initial point of the line to be clipped. This initial point should be in three predetermined areas; thus the line may have to be translated and/or rotated to bring it into the desired region. The line segment may then be re-translated and/or re-rotated to bring it to the original position. After that, straight line segments are drawn from the line end point, passing through the corners of the clipping window. These areas are then designated as L, LT, LB, or TR, depending on the location of the initial point. Then the other end point of the line is checked against these areas. If a line starts in the L area and finishes in the LT area then the algorithm concludes that the line should be clipped at xw (max). Thus the number of clipping points is reduced to one, compared to other algorithms that may require two or more clipping points. Fast clipping[edit] This algorithm has similarities with Cohen-Sutherland. The start and end positions are classified by which portion of the 9 area grid they occupy. A large switch statement jumps to a specialized handler for that case. In contrast, Cohen-Sutherland may have to iterate several times to handle the same case.[2] O(lg N) algorithm This algorithm classifies vertices against the given line in the implicit form p: ax+by+c=0. As the polygon is assumed to be convex and vertices are ordered clockwise or anti-clockwise binary search can be applied and leads to a O(lg N) run time complexity.[3] Skala

This algorithm is based on homogeneous coordinates and duality.[4] It can be used for line or line segment clipping against a rectangular window as well as against a convex polygon. The algorithm is based on classifying a vertex of the clipping window against a half-space given by a line p: ax+by+c=0. The result of the classification determines the edges intersected by the line p. The algorithm is simple, easy to implement and extensible to a convex window as well. The line or line segment p can be computed from points x1,x2 given in homogeneous coordinates directly using the cross product as p = x1 x x2 = [x1,y1,w1] x [x2,y2,w2] or as p = x1 x x2 = [x1,y1,1] x [x2,y2,1] if given in the Euclidean coordinates.

SutherlandHodgman algorithm From Wikipedia, the free encyclopedia (Redirected from SutherlandHodgman) Jump to: navigation, search The SutherlandHodgman algorithm is used for clipping polygons. It works by extending each line of the convex clip polygon in turn and selecting only vertices from the subject polygon that are on the visible side. Contents

1 Description 2 Pseudo code 3 See also 4 References 5 External links

Description The algorithm begins with an input list of all vertices in the subject polygon. Next, one side of the clip polygon is extended infinitely in both directions, and the path of the subject polygon is traversed. Vertices from the input list are inserted into an output list if they lie on the visible side of the extended clip polygon line, and new vertices are added to the output list where the subject polygon path crosses the extended clip polygon line. This process is repeated iteratively for each clip polygon side, using the output list from one stage as the input list for the next. Once all sides of the clip polygon have been processed, the final generated list of vertices defines a new single polygon that is entirely visible. Note that if the subject polygon was concave at vertices outside the clipping

polygon, the new polygon may have coincident (i.e. overlapping) edges this is acceptable for rendering, but not for other applications such as computing shadows.

All steps of clipping concave polygon 'W' by 5-sided convex polygon The WeilerAtherton algorithm overcomes this by returning a set of divided polygons, but is more complex and computationally more expensive, so SutherlandHodgman is used for many rendering applications. SutherlandHodgman can also be extended into 3D space by clipping the polygon paths based on the boundaries of planes defined by the viewing space Pseudo code Given a list of edges in a clip polygon, and a list of vertices in a subject polygon, the following procedure clips the subject polygon against the clip polygon. List outputList = subjectPolygon; for (Edge clipEdge in clipPolygon) do List inputList = outputList; outputList.clear(); Point S = inputList.last; for (Point E in inputList) do if (E inside clipEdge) then if (S not inside clipEdge) then outputList.add(ComputeIntersection(S,E,clipEdge));

end if outputList.add(E); else if (S inside clipEdge) then outputList.add(ComputeIntersection(S,E,clipEdge)); end if S = E; done done The vertices of the clipped polygon are to be found in outputList when the algorithm terminates. Note that a point is defined as being inside an edge if it lies on the same side of the edge as the remainder of the polygon. If the vertices of the clip polygon are consistently listed in a clockwise direction, then this is equivalent to testing whether the point lies to the left of the line (left means inside, while right means outside), and can be implemented simply by using a cross product. ComputeIntersection is a trivial function, omitted here for clarity, which returns the intersection of a line segment and an infinite edge. Note that it is only called if such an intersection is known to exist, and hence can simply treat both lines as being infinitely long.

Implementation Sutherland-Hodgman algorithm to remove false edges. Implementation is not optimal but working, GPL v2 Ashuha Arseniy Moscow Technical University http://dumpz.org/551873/

WeilerAtherton clipping algorithm From Wikipedia, the free encyclopedia (Redirected from WeilerAtherton) Jump to: navigation, search The WeilerAtherton clipping algorithm is used in computer graphics. It allows clipping of a subject polygon by an arbitrarily shaped clip polygon window. It is

generally applicable only in 2D. However, it can be used in 3D through visible surface determination and with improved efficiency through Z-ordering.[1] Description

Subdivision with the Weiler-Atherton algorithm The algorithm requires polygons to be clockwise and not reentrant (self intersecting). The algorithm can support holes (as counter-clockwise polygons wholly inside their parent polygon), but requires additional algorithms to decide which polygons are holes. Merging of polygons can also be performed by a variant of the algorithm. Two lists are created from the coordinates of each polygons A and B, where A is the clip region and B is the polygon to be clipped. The list entries are labelled as either inside or outside the other polygon. Various strategies can be used to improve the speed of this labelling, and to avoid needing to proceed further. All the polygon intersections are then found and are inserted into both lists, linking the lists at the intersections. Care will be needed where the polygons share an edge. If there are no intersections then one of three situations exist: 1. A is inside B - return A for clipping, B for merging. 2. B is inside A - return B for clipping, A for merging. 3. A and B do not overlap - return None for clipping or A & B for merging. A list of inbound intersections is then generated. Each intersection in the list is then followed clockwise around the linked lists until the start position is found. One or more concave polygons may produce more than one intersecting polygon. Convex polygons will only have one intersecting polygon. The same algorithm can be used for merging two polygons by starting at the outbound intersections rather than the inbound ones. However this can produce counter-clockwise holes.

Some polygon combinations may be difficult to resolve, especially when holes are allowed. Points very close to the edge of the other polygon may be considered as both in and out until their status can be confirmed after all the intersections have been found and verified, however this increases the complexity.

Вам также может понравиться