gpu gems 2 - programming techniques for high-performance graphics and general-purpose computation

Upload: angelo-cutolo

Post on 07-Apr-2018

222 views

Category:

Documents


2 download

TRANSCRIPT

  • 8/6/2019 Gpu Gems 2 - Programming Techniques for High-Performance Graphics and General-Purpose Computation

    1/488

  • 8/6/2019 Gpu Gems 2 - Programming Techniques for High-Performance Graphics and General-Purpose Computation

    2/488

    This is a volume in

    The Graphics Gems SeriesThe Graphics Gems SeriesThe Graphics Gems SeriesThe Graphics Gems SeriesThe Graphics Gems Series

    A Collect ion of Pract ical Techniques f or t he Comput er Graphics Progr ammer

    Series Edit or

    Andrew S. Glassner Xerox Palo Alt o Research Cent er Palo Alt o, Calif omia

  • 8/6/2019 Gpu Gems 2 - Programming Techniques for High-Performance Graphics and General-Purpose Computation

    3/488

    GGGGGRAPHICSRAPHICSRAPHICSRAPHICSRAPHICSGGGGGEMSEMSEMSEMSEMSIIIIIIIIII

    edited by

    JAMES ARVO

    Prog ram of Comput er GraphicsProg ram of Comput er GraphicsProg ram of Comput er GraphicsProg ram of Comput er GraphicsProg ram of Comput er Graphics

    Cornell UniversityCornell UniversityCornell UniversityCornell UniversityCornell UniversityIthaca, New YorkIthaca, New YorkIthaca, New YorkIthaca, New YorkIthaca, New York

    AP PROFESSIONALBost on San Diego NewYork

    London Sydney Tokyo Toront o

  • 8/6/2019 Gpu Gems 2 - Programming Techniques for High-Performance Graphics and General-Purpose Computation

    4/488

  • 8/6/2019 Gpu Gems 2 - Programming Techniques for High-Performance Graphics and General-Purpose Computation

    5/488

    v

    About t he CoverAbout t he CoverAbout t he CoverAbout t he CoverAbout t he CoverThe cover image is the second in the Gems theme that I began last year. AndrewGlassner and I bounced some ideas back and forth, and the design solidied prettyquickly. The gems themselves are the same as in last year s image.The picture wasgenerated at Pacic Data Images using their in-house software. All of the textures and

    models are procedurally generated. The sand texture, the sand surface shape, thewoodgrain, the sea foam, and the starsh texture are all derived from fractal noisepatterns. I spent most of my time on the water, making it look all shiny and wet andtransparent, but n ot too transparent. The foam on the w ater s surface was also very t ime-consuming. Another challenge was to get the gems to all pile on top of each otherconvincingly. I wrote a program that d ropped them, one at a t ime, and as they fell , theywere rotated to the angle that moved them the furthest down without intersectinganything that w as there already. This program took a coup le of hour s to run, but i t wa smuch faster than trying to place them by hand. The picture was rendered with a ray-tracing prog ram and took 50 hou rs on an Silicon Grap hics 4D25 comp uter a t a resolutionof 2250 3000 pixels with four samples per pixel.

    Thaddeus BeierSilicon Graphics Computer Systems

    When Andrew asked if I wanted to do the cover forGraphics Gems II , I said Sure . .. we can reuse th e software w e built last year forGraphics Gems. While it wasnt quitethat simple, i t was much easier to produce this cover than the rst one. As before, theimage was designed on a color monitor producing red, green and blue pixels. Forprinting, we needed to convert these pixels to cyan, magenta, yellow, and black pixels(printer color space). Once in this form, the image was processed commercially toproduce half-toned lm suitable for printing. This nal step was performed at Kedie-Orent, Sunnyvale, California, on their Croseld digital prepress system.

    As was the case with the rst Gems p icture, man y of the original image colors couldnot be exactly reprod uced in p rint form . The colors had to be modied to m ap into theset of colors that can be produced by the printer, its gamut. The trick is to do themod ication wh ile m aintaining th e ap pearan ce of the image. In th is picture, the colorsin the sand, shells, and water were mostly inside the printer gamut. However, some of the gem colors, particularly the bright blue-greens, were far outside the gamut. Thetransformation we ap plied was similar to the one we d esigned forGraphics Gems; colorsoutside of the gamu t were d esaturated to colors of the same lightness wh ile maintainingthe same hue. If one color of a particular hue needed to be desaturated, all colors of the same hu e wou ld be desatur ated to p reserve shad ing detail . How ever, colors outsideof the gamut move more than colors inside of the gamut to maintain the overallsaturation.

    The colors of theGraphics Gems II cover are l ighter and more d elicate than the coverof Graphics Gems, and mu ch more of the image lies in the interior of the printer gamu t.We tun ed th e transformation for this image to minimize the change to the less saturatedcolors, preserving the subtle shading in the sand and water.

    Thanks to Bill Wallace, who wrote the original gamut mapping software forGraphicsGems, and to Ken Fishkin, who helped with the production of the cover this year.

    Maureen Stone Xerox Palo Alto Research Center

  • 8/6/2019 Gpu Gems 2 - Programming Techniques for High-Performance Graphics and General-Purpose Computation

    6/488

    v i

  • 8/6/2019 Gpu Gems 2 - Programming Techniques for High-Performance Graphics and General-Purpose Computation

    7/488

    vii

    CCCCCONTENTSONTENTSONTENTSONTENTSONTENTS

    The symbol C denotes gems that have accompanying C implementations on disk.

    Foreword By A ndrew Glassner xvii

    Preface xix

    M athematical N otat ion xxi

    Pseudo-Code xxiii

    Contributors xxix

    IIIII22222 D GEOMETRY AND ALGORITHMSD GEOMETRY AND ALGORITHMSD GEOMETRY AND ALGORITHMSD GEOMETRY AND ALGORITHMSD GEOMETRY AND ALGORITHMS

    In trod u ction 3

    1. The Area of a Sim p le Polygon 5Jon Rokne

    2. Intersection of Line SegmentsC

    7 Mukesh Prasad

    3. Distance from a Poin t to a Line 10Jack C. M orrison

    4. An Easy Bou nd ing Circle 14Jon Rokne

  • 8/6/2019 Gpu Gems 2 - Programming Techniques for High-Performance Graphics and General-Purpose Computation

    8/488

    viii

    5. The Smallest Circle Containing the IntersectionCof Tw o Circles 17

    Jon Rokne

    6. Ap p olon iu ss 10th Problem 19Jon Rokne

    7. A Peano Curve Generation AlgorithmC 25Ken Musgrave

    8. Space-Filling Curves and a Measure of CoherenceC 26Douglas Voorhies

    9. Scan line Coherent Shap e Algebra 31

    Jonathan E. Steinhart

    IIIIIIIIIIIIIIIMAGE PROCESSINGMAGE PROCESSINGMAGE PROCESSINGMAGE PROCESSINGMAGE PROCESSING

    In trod u ction 49

    1. Image Smoothing and Sharpening by DiscreteConvolu tion 50Dale A. Schumacher

    2. A Comparison of Digital Halftoning TechniquesC 75Dale A. Schumacher

    3. Color Dither ingC 72Spencer W. Thomas and Rod G. Bogart

    4. Fast Anam orp hic Image Scaling 78Dale A. Schumacher

    5. Real Pixels 80Greg Ward

    6. A Fast 90-Degree Bitmap RotatorC 84Sue-Ken Yap

    CONTENTS

  • 8/6/2019 Gpu Gems 2 - Programming Techniques for High-Performance Graphics and General-Purpose Computation

    9/488

    ix

    CONTENTS

    7. Rotation of Run-Length Encoded Image DataC 86Jeff Holt

    8. Ad ap tive Ru n-Length Encod ing 89Andrew S. Glassner

    9. Image File Com p ression Mad e Easy 93Alan W. Paeth

    10. An Op tim al Filter for Im age Reconstru ction 101 Nelson Max

    11. N oise Threshold ing in Ed ge Im ages 105John Schlag

    12. Computing the Area, the Circumference, and the Genusof a Binary Digital ImageC 107Hanspeter Bieri and A ndreas Kohler

    IIIIIIIIIIIIIIIFFFFFRAMRAMRAMRAMRAMEEEEE BUFFER TECHBUFFER TECHBUFFER TECHBUFFER TECHBUFFER TECHNNNNNIQUESIQUESIQUESIQUESIQUES

    In trod u ction 115

    1. Efficient Inverse Color Map ComputationC 116Spencer W. Thomas

    2. Efcient Statistical Computations for Optimal ColorQu antization 126

    Xiaolin Wu

    3. A Random Color Map Animation AlgorithmC 134Ken Musgrave

    4. A Fast Ap p roach to PH IGS PLUS Pseu d o Color 138MappingJames Hall and Terence Lindgren

    5. Map p ing RGB Trip les onto 16 Distinct Valu es 143

  • 8/6/2019 Gpu Gems 2 - Programming Techniques for High-Performance Graphics and General-Purpose Computation

    10/488

    x

    6. Television Color Encoding and Hot BroadcastColors C 147

    David M artin dale and A lan W . Paeth

    7. An Inexpensive Method of Setting the MonitorWhite Poin t 159Gary W. Meyer

    8. Some Tips for Making Color Hardcopy 163Ken Musgrave

    IVIVIVIVIV33333 D GEOMETRY AND ALGORITHMSD GEOMETRY AND ALGORITHMSD GEOMETRY AND ALGORITHMSD GEOMETRY AND ALGORITHMSD GEOMETRY AND ALGORITHMS

    In trod u ction 169

    1. Area of Plan ar Polygon s an d Volu m e of Polyh ed r a 170Ronald N. Goldman

    2. Getting Arou nd on a Sp here 172Clifford A. Shaffer

    3. Exact Dihed ral Metrics for Comm on Polyhed ra 174Alan W. Paeth

    4. A Simp le View ing Geometry 179Andrew S. Glassner

    5. View CorrelationC 181Rod G. Bogart

    6. Maintain ing Winged -Ed ge Mod els 191Andrew S. Glassner

    7. Qu adtree/ Octree-to-Boun dar y Conversion 202Claudio Montani and Roberto Scopigno

    8. Three-Dimensional Homogeneous Clippingof Triangle StripsC 219Patrick-Gilles M aillot

    CONTENTS

  • 8/6/2019 Gpu Gems 2 - Programming Techniques for High-Performance Graphics and General-Purpose Computation

    11/488

    xi

    9. In ter Ph on g Sh ad in gC 232 Nadia Magnenat Thalmann, Daniel Thalmann,and Hong Tong Minh

    VVVVVRRRRRAY TRACINGAY TRACINGAY TRACINGAY TRACINGAY TRACING

    In trod u ction 245

    1. Fast RayConvex Polyhedron Intersect ionC 247 Eric Haines

    2. Intersect ing a Ray with an Ell ipt ical TorusC 251 Joseph M . Cy chosz

    3. RayTriangle Intersection Using Binary RecursiveSu bd ivision 257

    Douglas Voorhies and David Kirk

    4. Im proved Ray Taggin g for Voxel-Based Ray Tracin g 264 David Kirk and James Arvo

    5. Efciency Improvements for Hierarchy Traversalin Ray Tracing 267

    Eric Haines

    6. A Recursive Shad ow Voxel Cache for RayTracing C 273

    A ndrew Pearce

    7. Avoiding Incorrect Shadow Intersect ions for RayTracing 275

    A ndrew Pearce

    8. A Body Color Model: Absorption of Light throughTranslu cent Med ia 277 M ark E. Lee and Samuel P. Uselton

    9. More Shadow Attenuation for Ray Tracing Transparentor Translu cent Objects 283

    M ark E. Lee and Samuel P. Uselton

    CONTENTS

  • 8/6/2019 Gpu Gems 2 - Programming Techniques for High-Performance Graphics and General-Purpose Computation

    12/488

    xii

    CONTENTS

    V lV lV lV lV lRRRRRADIOSITYADIOSITYADIOSITYADIOSITYADIOSITY

    In trod u ction 293

    1. Imp lementing Progressive Radiosity with User-Provided Polygon Display RoutinesC 295Shenchang Eric Chen

    2. A Cubic Tetrahedral Adap tation of the Hemi-CubeAlgorithm 299

    Jeffrey C. Beran-Koehn and Mark J. Pavicic

    3. Fast Vertex Radios ity UpdateC 303

    Filippo Tampieri4. Rad iosity via Ray Tracing 306

    Peter Shirley

    5. Detect ion of Shadow Bound aries for Adaptive Meshingin Rad iosity 311Franois Sillion

    VllVllVllVllVllMMMMMATRIX TECHNIQUESATRIX TECHNIQUESATRIX TECHNIQUESATRIX TECHNIQUESATRIX TECHNIQUES

    In trod u ction 319

    1. Decomposing a Matrix in to SimpleTransformations C 320Spencer W. Thomas

    2. Recovering the Data from th e Transform ation Matrix 324 Ronald N. Goldman

    3. Transformations as Exp onentials 332 Ronald N. Goldman

    4. More Matrices and Transformations: Shear andPseu d o-Persp ective 338

    Ronald N. Goldman

  • 8/6/2019 Gpu Gems 2 - Programming Techniques for High-Performance Graphics and General-Purpose Computation

    13/488

    xiii

    5. Fast Matr ix Inversion C 342Kevin Wu

    6. Q uater nion s an d 4 4 Matrices 351Ken Shoemake

    7. Random Rota tion Matr icesC 355 James Arvo

    8. Classifying Small Sparse MatricesC 357 James A rvo

    VlllVlllVll lVll lVll lNNNNNUMERICAL AND PROGRAMMINGUMERICAL AND PROGRAMMINGUMERICAL AND PROGRAMMINGUMERICAL AND PROGRAMMINGUMERICAL AND PROGRAMMING

    TTTTTECHNIQUESECHNIQUESECHNIQUESECHNIQUESECHNIQUESIn trod u ction 365

    1. Bit Picking 366Ken Shoemake

    2. Faster Fou rier Transform 368Ken Shoemake

    3. Of In tegers , Fie lds , and Bit Count ingC 371 Alan W. Paeth and David Schilling

    4. Using Geometric Construct ions to InterpolateOrientation w ith Qu atern ions 377

    John Schlag

    5. A H alf-Angle Id entity for Digital Com p u tation : 381The Joys of the Halved Tangent

    Alan W. Paeth

    6. An In teger Square Root AlgorithmC 387Christopher J. Musial

    7. Fast Ap p roximation to the Arctangen t 389 Ron Capelli

    CONTENTS

  • 8/6/2019 Gpu Gems 2 - Programming Techniques for High-Performance Graphics and General-Purpose Computation

    14/488

  • 8/6/2019 Gpu Gems 2 - Programming Techniques for High-Performance Graphics and General-Purpose Computation

    15/488

    PSEUDO-CODE

    .

  • 8/6/2019 Gpu Gems 2 - Programming Techniques for High-Performance Graphics and General-Purpose Computation

    16/488

    PSEUDO-CODE

    .

  • 8/6/2019 Gpu Gems 2 - Programming Techniques for High-Performance Graphics and General-Purpose Computation

    17/488

    xviiGRAPHICS GEMS II Edit ed by JAMES ARVO xvii

    FOREWORD

    FFFFFOREWORDOREWORDOREWORDOREWORDOREWORDby Andrew Glassner

    Welcome toGraphics Gems II, a new collection of practical techniques

    and methods for the computer graphics programmer. This book is acollection of contributions from many p eople, most of wh om w ork ind e-pen dently of each other. Yet throu gh th ese volumes w e are able to shareour observations and insights with each other and our readers, andtogether bu ild a commu nal library of graph ics program ming techniques.

    In the preface to the originalGraphics Gems, I wrote that Thisbook . . . concludes one turn of a cycle of discovery, documentation,editing, publishing, and read ing, which will in turn lead to new d iscover-ies. I am delighted that we have completed another loop around the

    cycle, and emerged with another strong collection of program ming tech-niques. As with its pred ecessor, the articles in this book are p rimar ily notresearch resu lts; those can be found in the formal professional literatu re.Rather, theGraphics Gems books are a conduit for practical informa-tion. Much of the detailed information in these volumes would be inap-propriate for a technical paper, but is invaluable to the implementor.

    This volume has been edited by Jim Arvo, who is well known in thegraphics community as a skilled researcher and teacher. Jim and I havetaught several courses together at SIGGRAPH conferences and other

    venues, and I have always been imp ressed w ith h is insight and eagernessto share his knowledge. The ne selection and organization of material inthis volume demonstrates his abilities well.

    There may be more volumes in theGraphics Gems series. We willcontinu e to solicit and collect contributions, and organize them into newbooks, as long as the qu ality remains h igh and the results u seful. Each

  • 8/6/2019 Gpu Gems 2 - Programming Techniques for High-Performance Graphics and General-Purpose Computation

    18/488

  • 8/6/2019 Gpu Gems 2 - Programming Techniques for High-Performance Graphics and General-Purpose Computation

    19/488

    xixGRAPHICS GEMS II Edit ed by JAMES ARVO xix

    PREFACE

    PPPPPREFACEREFACEREFACEREFACEREFACE

    Continuing in the spirit of the inauguralGraphics Gems volume,Graphics Gems II represents the collective expertise of a large andd iverse group of people. The comm on thread shared by all the contribu-

    tors is that each has devised or rened useful ideas which can benetother computer graphics practitioners, and they have graciously agreed todisseminate them. The resulting amalgam has a character quite distinctfrom any text book on the subject, as it reects ideas and approachesevery bit as diverse and unique as the people behind them.

    In the eld of computer graphics, as with almost any endeavor, there israrely a best way to do anyth ing. Therefore, this volum e shares a recur-ring theme with the original volume by presenting techniques for doingwell-known operations faster or easier. Some present a new way of

    looking at an old problem while others provide useful mathematicalmachinery with broad application.There are several ways to use this book. First, it can be a source of

    solutions to specic problems. If one of the gems in this collectionaddresses a particular problem of interest to you, the idea can beemployed with little ado. This is facilitated by the program listingsprovided with many of the gems. A second way to use this book is tosimply browse, learning clever solutions to problems you may not haveencountered or even considered yet. Often the ideas behind the gems can

    be app lied in contexts mu ch broad er than th ose shown, or they may serveas the seeds to get you thinking along new lines. In any event, there ismu ch to be gained by looking over the shou lders of experienced grap hicsprogrammers as they apply the subtle tricks of the trade.

    The overall structure, mathematical notation and pseudo-code usedhere are the same as in the rst volum e. The scope an d n am es of some

  • 8/6/2019 Gpu Gems 2 - Programming Techniques for High-Performance Graphics and General-Purpose Computation

    20/488

    xxGRAPHICS GEMS II Edit ed by JAMES ARVO xx

    PREFACE

    parts hav e been changed slightly to best accomm odate this new collectionof gems. In ord er to make this book as self-contained as possible we haveinclud ed several of the imp ortan t sections verbatim from th e rst volum efor easy reference; there are the sections entitled Mathematical Nota-tion, Pseudo-Code, and the listings Graphics Gems C Header File

    and 2D and 3D Vector Library in Ap pen d ix I. The last of these containsseveral corrections and extensions. Only the part titled Radiosity hasno counterpart in the rst volume. This new part has been added tocomp lement the p art on ray tracing an d to reect current research trend s.

    The gems comprising each part all relate to some common theme.Gems that share something more fundamental are linked via the Seealso section at the end of each gem. Gems identied by gem number arecontained in this volume; gems id entied by page n um ber are in the rstvolume of Graphics Gems. The mathematical background which is as-

    sumed in most of the gems is limited to elementary calculus and linearalgebra, the staples of computer graphics.The C programm ing langu age has been used for all the program listings

    in the app end ix because it is widely used and is among the m ost portableof languages. It is also a favorite among graphics programmers becauseof its exibility. These consid erations mad e it the obvious choice. All theC cod e in this book is in the p ublic domain, and is yours to stu dy, mod ify,and use. As of this wr iting, all code listings are available via an onym ousftp transfer from the machine weedeater.math.yale.edu (internet ad-dress 130.132.23.17). When you connect to this machine using ftp, log inas anonymou s, and give your own last name as the password . Then u sethe cd comm and to move to the directory pub/ GraphicsGems.Download an d read the le called README to learn about where the codeis kept, and how to report bugs.

    Thanks are du e to the man y p eople who m ade th is book possible. Firstand foremost Id like to than k all the gem contributor s, whose expertiseand insight is wh at th is book is all about. Also, Id like to thank AndrewGlassner for pioneering this whole adventure and for assisting in everyaspect along the w ay. Special than ks to Terry Lind gren for histremend ously helpful inpu t where it w as most need ed, and to GregWard, John Francis, Paul Heckbert, and Eric Haines for their reviews,expert opinions, and helpful suggestions. Finally, Id like to thank CraigKolb for providing a safe haven for the public domain C code.

  • 8/6/2019 Gpu Gems 2 - Programming Techniques for High-Performance Graphics and General-Purpose Computation

    21/488

    xxiGRAPHICS GEMS II Edit ed by JAMES ARVO xxi

    MATHEMATICAL NOTATION

    MMMMMATHEMATICALATHEMATICALATHEMATICALATHEMATICALATHEMATICALNNNNNOTATIONOTATIONOTATIONOTATIONOTATION

    Geometric ObjectsGeometric ObjectsGeometric ObjectsGeometric ObjectsGeometric Objects

    0 the nu m ber 0, the zero vector, the p oin t (0, 0), the

    point (0, 0, 0)a, b, c the real numbers (lowercase italics)P, Q points (upper-case italics)l , m lines (lower-case bold)A, B vectors (upper-case bold)(components Ai)M matrix (upper-case bold), angles (lower-case greek)

    Derived Object sDerived Object sDerived Object sDerived Object sDerived Object s

    A the vector perpendicular toA (valid only in 2D, whereA = (Ay, Ax)

    M -1 the inverse of matrixMM T the transpose of matrixM

    M * the adjoint of matrixM

    M 1 = Mdet M( )

    | M | d eterminant of Mdet(M ) same as aboveM i,j element from row i, column j of matrix M (top-left is

    (0, 0)M i, all of row i of matrixM

  • 8/6/2019 Gpu Gems 2 - Programming Techniques for High-Performance Graphics and General-Purpose Computation

    22/488

    xxiiGRAPHICS GEMS II Edit ed by JAMES ARVO xxii

    MATHEMATICAL NOTATION

    M ,j all of column j of Matrix ABC triangle formed by points A, B, C

    A BC angle formed by points A, B, C with vertex at B

    Basic Operat orsBasic Operat orsBasic Operat orsBasic Operat orsBasic Operat ors +, , / , standard math operators

    the dot (or inner or scalar) product the cross (or outer or vector) product

    Basic Expressions and FunctionsBasic Expressions and FunctionsBasic Expressions and FunctionsBasic Expressions and FunctionsBasic Expressions and Functions

    x floor of x (largest integer not greater than x) x ceiling of x (smallest integer not smaller than x)a| b mod u lo arithmetic; rem aind er of a ba mod b same as above Bi

    n t( ) Bernstein polynomial =n

    i

    t

    i 1 t( )n i , i =0L n

    n

    i

    binomial coefficient n!n

    i

    ( )!i!

  • 8/6/2019 Gpu Gems 2 - Programming Techniques for High-Performance Graphics and General-Purpose Computation

    23/488

    xxiii

    PSEUDO-CODE

    GRAPHICS GEMS II Edit ed by JAMES ARVO xxiii

    PPPPPSEUDO-CODESEUDO-CODESEUDO-CODESEUDO-CODESEUDO-CODE

    Declarations (not required)Declarations (not required)Declarations (not required)Declarations (not required)Declarations (not required)

    name: TYPE initialValue;examples: :real 3.14159;v: array [0..3] of integer [0, 1, 2, 3];

    Primit ive Dat a TypesPrimit ive Dat a TypesPrimit ive Dat a TypesPrimit ive Dat a TypesPrimit ive Dat a Types

    array [lowerBound..upperBound]of TYPE;booleancharintegerrealdoublepointvector

    matrix3equivalent to:matrix3: record [array [0..2] of array [0..2] of real;];example: m:Matrix3 [[1.0, 2.0, 3.0], [4.0, 5.0, 6.0], [7.0, 8.0, 9.0]];m[2][1] is 8.0m[0][2] 3.3; assigns 3.3 to upper-right corner of matrix

  • 8/6/2019 Gpu Gems 2 - Programming Techniques for High-Performance Graphics and General-Purpose Computation

    24/488

    xxiv

    PSEUDO-CODE

    GRAPHICS GEMS II Edit ed by JAMES ARVO xxiv

    matrix4equivalent to:matrix4: record [array [0..3] of array [0..3] of real;];example: m: Matrix4 [

    [1.0, 2.0, 3.0, 4.0],

    [5.0, 6.0, 7.0, 8.0],[9.0, 10.0, 11.0, 12.0],[13.0, 14.0, 15.0, 16.0]];

    m[3][1] is 14.0m[0][3] 3.3; assigns 3.3 t o upper-right corner of matrix

    Records (Structures)Records (Structures)Records (Structures)Records (Structures)Records (Structures)

    Record denition:Box: record [

    left, right, top, bottom:integer ;];

    newBox: Box new [Box];dynamically allocate a new instance of Box and return a pointer to it

    newBox.left 10;this same notation is appropriate whether newBox is a pointer or structure

    ArraysArraysArraysArraysArrays

    v:array [0..3]of integer [0, 1, 2, 3];v is a four-element array of integers

    v[2] 5; assign to third element of v

    CommentsCommentsCommentsCommentsComments

    A comment may appear anywhere it is indicated by italics

  • 8/6/2019 Gpu Gems 2 - Programming Techniques for High-Performance Graphics and General-Purpose Computation

    25/488

  • 8/6/2019 Gpu Gems 2 - Programming Techniques for High-Performance Graphics and General-Purpose Computation

    26/488

    xxvi

    PSEUDO-CODE

    GRAPHICS GEMS II Edit ed by JAMES ARVO xxvi

    Logical ConnectivesLogical ConnectivesLogical ConnectivesLogical ConnectivesLogical Connectives

    or, and, not, xor

    Bit wise Operat orsBit wise Operat orsBit wise Operat orsBit wise Operat orsBit wise Operat ors

    bit-or, bit-and, bit-xor

    RelationsRelationsRelationsRelationsRelations

    =, , >, ,

  • 8/6/2019 Gpu Gems 2 - Programming Techniques for High-Performance Graphics and General-Purpose Computation

    27/488

    xxvii

    PSEUDO-CODE

    GRAPHICS GEMS II Edit ed by JAMES ARVO xxvii

    clamp(v, l, h) return l if v < l, else h if v > h, else v : min (h,max (l,v))

    oor (x) or x round x towards 0 to rst integer ceiling(x) or x round x away from 0 to rst integer round(x) round x to nearest integer, if frac(x) = .5, round towards

    0frac(x) fractional part of x

  • 8/6/2019 Gpu Gems 2 - Programming Techniques for High-Performance Graphics and General-Purpose Computation

    28/488

    xxix

    CONTRIBUTORS

    GRAPHICS GEMS II Edit ed by JAMES ARVO xxix

    CONTRIBUTORSCONTRIBUTORSCONTRIBUTORSCONTRIBUTORSCONTRIBUTORS

    Numbers in parentheses indicate pages on which authors gems begin.

    James Arvo (264, 355, 357),Cornell University, Program of Computer Graphics, Ithaca, N ew York, 14853, [email protected]

    Thaddeus Beier (cover),Silicon Graphics Computer Systems, 2011 Shoreline Boule-vard, Mountain View, California, 94043

    Jeffrey C. Beran-Koehn (299), Minard 300, North Dakota State University, Fargo, N orth Dakota, 58105, [email protected] odak.edu

    H an sp eter Bieri (107), Instit ut fu r Informatik und angewandte M athematik der Uni-versitt Bern, Langgass Strae 51, CH-3012, Bern, Switzerland

    Rod G. Bogart (72),Chrysler Center, Room 229, 2121 Bonisteel Boulevard, Ann Arbor; Michigan 48109, rgbcaen.engin.umich.edu

    Ronald B. Capelli (389), IBM Corporation, Department C13, Building 703-2, P.0. Box950, Poughkeepsie, N ew York 12602

    Shen chan g Eric Chen (293), Apple Compu ter, Inc., M S60W , 20707 Valley Green D rive,Cupertino, California 95014, [email protected]

    Josep h M. Cychosz (251),Purdue University CADLAB, Potter Engineering Center, 520 Evergreen, West Lafayette, Indiana 47907, [email protected]

    Andrew S. Glassner (89, 179, 191), Xerox PARC, 3335 Coyote Hill Road, Palo Alto,California 94304, glassner. pa@xerox. com

    Ronald N. Goldman (170, 324, 332, 338), Department of Computer Science, RiceUniversity, P.0. Box 1892, Houston, Texas 77251

    Eric Haines (247, 267),3D/Eye, Inc., 2359 North Triphammer Road, Ithaca, NewYork 14850, [email protected]

  • 8/6/2019 Gpu Gems 2 - Programming Techniques for High-Performance Graphics and General-Purpose Computation

    29/488

    xxx

    CONTRIBUTORS

    GRAPHICS GEMS II Edit ed by JAMES ARVO xxx

    James Hall (138),Prime/Computer Vision, 14 Cosby Drive, Bedford, Massachusetts,01730

    Steve Hollasch (467),1437 West 6th Street, Tempe, Arizona 85281-3205Jeff Holt (86), Intergraph Corporation, 4515 Bonnell Drive, #25H, Huntsville, Al-

    abama 35894-0001, UUNET!INGR!HOLT!HOLT

    David Kirk (257, 264),CA LTECH Comput er Science 256-80, Pasadena, California91125, [email protected]

    Andreas Kohler (107), Institut fr Informatik und angewandte Mathematik der Uni-versitt Bern, CH-3012 BERNE, Langgassstrasse 51, Switzerland

    Ma rk E. Lee (277, 283), A moco Product ion Company, Tu lsa Research Center, P.0. Box3385, Tulsa, Oklahoma 74102, [email protected]

    Terence Lindgren (138, 420),Prime/Computer Vision, 14 Cosby Drive, Bedford, Massachusetts, 01730

    Patrick-Gilles Maillot (219),Sun MicroSystems, Inc., 2550 Garcia Avenue, MTV 21-04, Mountain View, California 94043

    David Martind ale (147), Imax Systems Corporation, 2525 Speakman Drive, SheridanPark, Ontario, Canada, L5K Ibl, , labrea!adobe!uunet!utai!imax!dave

    Nelson Max (101), L-301, Lawrence Liv ermore N ational Laboratory, P.0. Box 808, Livermore, California 94550, [email protected]

    Gary W. Meyer (159), Department of Computer and Information Science, Universityof Oregon, Eugene, Oregon 97403, [email protected]

    Hong Tong Minh (232),c/o Daniel Thalmann, Computer Graphics Lab., Swiss Fed-eral Institute of Technology, CH-1015 Lausanne, Switzerland, [email protected]

    Clau d io Montan i (202), Inst itut o di Elaborazione delInformazione, Consiglio Nazionaledelle Ricerche, Via Sant a Maria 46, 56126 Pisa, Italy, [email protected]

    Doug Moore (406), Department of Computer Science, Rice University, P.0. Box 1892, Houston, Texas 77254, [email protected]

    Jack C. Morrison (10),5654 South Jackpine Road, Evergeen, Colorado 80439,ncar!boulder!agcsun!jackm

    F. Ken Musgrave (25, 163), Departments of Mathematics and Computer Science, YaleUniversi ty, Box 2155 Yale Station, New Haven, Connecticut 06520,[email protected]

    Christopher J. Musial (387, 435),801 Leonard Drive, Rock Hill, Missouri 63119

    Ala n W. Pae th (93, 143, 147, 174, 371, 381, 440), NeuralWare, In c., Penn Cent er West Building 4, Suite 227, Pittsburgh, Pennsylvania 15276

  • 8/6/2019 Gpu Gems 2 - Programming Techniques for High-Performance Graphics and General-Purpose Computation

    30/488

    xxxi

    CONTRIBUTORS

    GRAPHICS GEMS II Edit ed by JAMES ARVO xxxi

    Mark J. Pavicic (299), Minard 300, North Dakota State University, Fargo, North Dakota 58105, [email protected]

    Andrew Pearce (273, 275), A lias Research, #500, 110 R ichmond S treet E., Toront o,Ont ario, Canada M5C IPI, [email protected] UCP

    Mukesh Prasad (7) , Meta Mind, Inc., Two Trolley Square, Suite 146, East Haven,Connecticut 06512

    Jack Ritter (392),SHO Graphics, 1890 N. Shoreline Boulevard, Mountain View,California, 94043

    Jon Rokne (5, 14, 17, 19), Department of Computer Science, University of Calgary,Calgary, A lbert a, Canada T2N 1N 4, [email protected]

    David Schilling (371),4609 148th Avenue NE II201, Bellevue, Washington 98007 John F. Schlag (105, 377, 417),105-A Fairview Avenue, Capitola, California 95010

    Dale A. Schumacher (50, 57, 78),399 Beacon Street, St. Paul, Minnesota 55104,dal@syntel. mn. org

    Roberto Scopigno (202), Instituto CNUCE Consiglio Nazionale delle Ricerche, ViaSanta Maria, 36, 56126 Pisa, Italy, [email protected]

    H an s-Pete r Seidel (424, 428), Department of Computer Science, University of Waterloo,W aterloo, O nt ario, Canada N2L 3G1, [email protected]

    Clifford A. Sha ffer (172), Department of Comput er Science, Virgin ia Tech, Blacksburg,Virginia 24061, [email protected]

    Peter Shirley (306),Compu ter Science Departm ent, 101 Lindley H all, Indiana Univ ersity,

    Bloomington, Indiana 47405-4101, [email protected] Shoemake (351, 366, 368, 394, 412), Xerox PARC, 3333 Coyote Hill Road,

    Palo A lto, California 94304

    Franois Sillion (311),Cornell University, Program of Computer Graphics, Ithaca, N ew York, 14853, fx s@fsatu rn .graphics.cornell.edu

    Jonathan E. Steinhart (31),20180 Baker Creek Road, M cM innv ille, Oregon 97128-8016

    Filippo Tampieri, (303),Program of Computer Graphics, 580ETC Building, CornellUniversit y, It haca, New York, 14853, fx [email protected]

    Daniel Thalmann (232),Computer Graphics Laboratory, Swiss Federal Institute of Technology, CH-1015 Lausanne, Switzerland, [email protected]

    Nadia Magnenat Thalmann (232), MIRALab, CUI, University of Geneva, 12 rue dulac, C 1207, Geneva, Sw itz erland, t halmann @un i2a.un ige.ch

    Spencer W. Thomas(72, 116, 320), EECS Department, University of Michigan, 1101 Beal Av enue, A nn A rbor, M ichigan 48109-2110, [email protected] ich.edu

  • 8/6/2019 Gpu Gems 2 - Programming Techniques for High-Performance Graphics and General-Purpose Computation

    31/488

    xxxii

    CONTRIBUTORS

    GRAPHICS GEMS II Edit ed by JAMES ARVO xxxii

    Samuel P. Uselton (277, 283), NASA Ames Research Center, M/S T 045-1, Moffett Field, California 94035, [email protected]

    Douglas Voorhies (26, 257), Apollo Systems Division, Hewlett-Packard, 300 Apollo Driv e, Chelmsford, M assachusett s 01824, [email protected]

    Greg Ward (80, 396), Lawrence Berkeley Laboratory, 1 Cyclotron Road, 90-3111, Berkeley, California, 94720, [email protected]

    Joe Warren (406), Department of Computer Science, Rice University, P.0. Box 1892, Houston, Texas 77254, [email protected]

    Kevin Wu (342),Su n M icroSy stems Inc., 2550 Garcia Avenue, M TV 21-04, M oun tainView, California 94043, [email protected]

    Xiaolin Wu (126), Department of Computer Science, University of Western Ontario, London, Ontario, Canada N6A 5B7, [email protected]

    Sue-Ken Yap (84),CSIRO Division of Information Technology, P.0. Box 664, Can-berra, A CT 2601, A us tralia, [email protected]

  • 8/6/2019 Gpu Gems 2 - Programming Techniques for High-Performance Graphics and General-Purpose Computation

    32/488

    I2D GEOMETRYAND

    ALGORITHMS

  • 8/6/2019 Gpu Gems 2 - Programming Techniques for High-Performance Graphics and General-Purpose Computation

    33/488

    3GRAPHICS GEMS II Edit ed by JAMES ARVO 3

    2D GEOMETRY AND ALGORITHMS

    IIIII

    2 D GEOMETRY2 D GEOMETRY2 D GEOMETRY2 D GEOMETRY2 D GEOMETRYANDANDANDANDANDALGORITHMSALGORITHMSALGORITHMSALGORITHMSALGORITHMS

    Two-dimensional geometry pervades computer graphics. For that reaa graphics programmer requires tools for solving a wide variety ofgeometrical problems efficiently. Solutions to several typical 2D probare presented in this Part: com pu ting the area of a plan ar figu re, the d istfrom a p oint to a line, and find ing the m inimal enclosing area for somof objects.

    Problems of this type ar ise p artly becau se of the inherent tw o-dimen sinature of the screen or surface on which graphics output is displaAnother reason 2D geometry is important is that larger problems in tdimensions are frequently attacked by red ucing th em to smaller and simpr oblems in one or tw o d imensions. This tactic of breaking a large p rointo little palatable ones is perhaps nowhere more evident than in lcomputer programs. Graphics, in particular, tends to reduce ultimatela large nu m ber of sm all 2D problems, increasing the imp ortan ce of handeach one robustly and efficiently.

    The first six gems of this Part address some common 2D operationscan serve as basic building blocks for more complex tasks. The ntwo gems deal with generat ing and ut i l izing a class of space-fillingcurves. These curves are members of an infinite family of related cuthat possess an interesting property: If the steps in their constructioncarried out a sufficiently large number of times, the resulting curves come arbitrarily close to every point in some region of space. Althothis concept is equally valid in spaces of any dimension, space-filcurves are most immediately useful and accessible for computer grap

  • 8/6/2019 Gpu Gems 2 - Programming Techniques for High-Performance Graphics and General-Purpose Computation

    34/488

    4GRAPHICS GEMS II Edit ed by JAMES ARVO 4

    2D GEOMETRY AND ALGORITHMS

    when they are in two dimensions. For instance, their convoluted pprovide an interesting order in which to generate the pixels of an imas described in Gem number 1.8. The final gem in this Part describun iform fram ework that p rovides m any u seful operations on two-dimensfigures.

  • 8/6/2019 Gpu Gems 2 - Programming Techniques for High-Performance Graphics and General-Purpose Computation

    35/488

    5

    I.1 THE AREA OF A SIMPLE POLYGON

    GRAPHICS GEMS II Edit ed by JAMES ARVO 5

    I.1I.1I.1I.1I.1

    TTTTTHE AREA OF A SIMPLEHE AREA OF A SIMPLEHE AREA OF A SIMPLEHE AREA OF A SIMPLEHE AREA OF A SIMPLE

    PPPPPOLYGONOLYGONOLYGONOLYGONOLYGONJon Rokne

    Universit y of Calgary Calgary, Alb ert a, Canada

    The formula for the area of a triangle is given in Triangles (Goldml990b). This was generalized by Stone (1986) to a formula for a simpolygon that is easy to remember.

    Le t P i = (x i , y i), i = 1, . . . , n be the counterclockwise enumeration the vertices of the polygon as in Fig. 1.The area of the polygon is then

    A = 1

    2

    x 1 x 2 x n x 1

    y 1 y 2 y n y 1 ,

    where the interpretation of

    x1 x2 xn x1

    y1 y2 yn y1

    is the summing of the products of the downwards diagonals subtraction of the product of the upwards diagonals.

    A specic example serves to clarify the formula. Consider the polyin Fig. 2.

    The area of this polygon is

    A =1

    2

    6 5 2 4 2 6

    2 4 3 3 1 2

    = (6 4 + 5 3 + 2 3 + 4 1 + 2 2

    5 2 2 4 4 3 2 3 6 1)/ 2

    = 7.5.

  • 8/6/2019 Gpu Gems 2 - Programming Techniques for High-Performance Graphics and General-Purpose Computation

    36/488

    6

    I.1 THE AREA OF A SIMPLE POLYGON

    GRAPHICS GEMS II Edit ed by JAMES ARVO 6

    Figure 2.

    See also IV.1 The Area of Planar Polygons and Volume Polyhedra, Ronald N. Goldman

  • 8/6/2019 Gpu Gems 2 - Programming Techniques for High-Performance Graphics and General-Purpose Computation

    37/488

    7

    I.2 INTERSECTION OF LINE SEGMENTS

    GRAPHICS GEMS II Edit ed by JAMES ARVO 7

    I.2I.2I.2I.2I.2

    IIIIINTERSECTION OFNTERSECTION OFNTERSECTION OFNTERSECTION OFNTERSECTION OF

    LLLLLINE SEGMENTSINE SEGMENTSINE SEGMENTSINE SEGMENTSINE SEGMENTSMukesh PrasadMeta Mind, Inc.

    East Haven, Connect icut

    ProblemProblemProblemProblemProblem

    Given two line segments in 2D space, determine whether they intersenot. If they intersect, then determine the point of intersection.

    Algori thmAlgori thmAlgori thmAlgori thmAlgori thm

    The following ap proa ch is par ticularly efcient if the lines often d o nofact, intersectsince it works from partial results. It also is very adable to integer-only computation.

    Let the lineL12 connect points ( x1, y1) and ( x2, y2).

    Let the lineL34 connect points ( x3, y3) and ( x4, y4).

    Let the equation of L12 be F ( x, y) = 0, and that of L34 be G( x, y) = 0.Then this approach consists of the following steps:

    1. From the equation of L12, by substituting x3 and y3 for x and y,compute r 3 = F ( x3, y3).

    2. Com p u ter 4 = F ( x4, y4).

    3. If (i) r 3 is not equal to 0, (ii)r 4 is not equal to 0, and (iii) the signof r 3 and r 4 are the same (either both positive or both negativthe lines do not intersect. Terminate algorithm.

  • 8/6/2019 Gpu Gems 2 - Programming Techniques for High-Performance Graphics and General-Purpose Computation

    38/488

    8

    I.2 INTERSECTION OF LINE SEGMENTS

    GRAPHICS GEMS II Edit ed by JAMES ARVO 8

    4. Fro m the eq u at ion of L34, computer 1 = G(x 1 , y 1).

    5. Com p u ter 2 = G ( x2, y2).

    6. If (i) r 1 is not equal to 0, (ii)r 2 is not equal to 0, and (iii) the signof r 1 and r 2 are the same, the lines do not intersect. Terminaalgorithm.

    7. Lines intersect (or they are collinear). Comp ute p oint of intertion

    NotesNotesNotesNotesNotes The algorithm is determining if:

    ( x 3, y 3) and (x 4 , y 4) lie on the same side of L 12. If they do,the line segments obviously do not intersect.

    ( x 1, y1) an d ( x 2, y2) lie on the same side of L 34. If they do,the line segments obviously do not intersect.

    Otherwise, the lines must intersect, or be collinear.

    Comp arison of signs usu ally is very efcient. The C Implementationthis gem (Appendix 2) assumes a 2s complement number represetion, and uses the XOR operation to determine efciently whethersigns are the same or not.

    The interm ediate results obtained are useful in the nal stepcpute point of intersectionimproving the algorithm efciency.

    The algorithm , obviously, will work in oating point coordinates; ball x i and y i are available as integers (as is usually the case in bitmgraphics) and the result is required on an integer grid, only intearithm etic is required . How ever, for integer-only ar ithmetic, care shbe taken to guard against the possibility of overow. The polynomevaluations in the C Implementation (Appendix 2) are at mostdegree 3; therefore, using 32-bit integers, a range of 0-1023 in bot X and Y directions can be handled. For larger ranges, oating pooperations may be necessary, or a rearrangement of the intersectpoint computations may be necessary.

  • 8/6/2019 Gpu Gems 2 - Programming Techniques for High-Performance Graphics and General-Purpose Computation

    39/488

    9

    I.2 INTERSECTION OF LINE SEGMENTS

    GRAPHICS GEMS II Edit ed by JAMES ARVO 9

    For further efciency, the cases of r 1 , r 2 , r 3 , or r 4 being 0 can behandled separately. In these situations, one of the end points is point of intersection, and need not actually be computed

    If a collinearity check needs to be efcient, it can also be determ

    from the fact that bothr 3 and r 4 (or both r 1 and r 2) will be 0 in thiscase. The lines may or may not overlap in this case, and if necessthat can be easily determined by comparing extents.

    See also (49)A Fast 2D Point-on-Line Test, Alan W. Paeth;(304) Intersection of Two Lines in Three-Space, Ronald Goldman

  • 8/6/2019 Gpu Gems 2 - Programming Techniques for High-Performance Graphics and General-Purpose Computation

    40/488

    1 0

    I.3 DISTANCE FROM A POINT TO A LINE

    GRAPHICS GEMS II Edit ed by JAMES ARVO 1 0

    I.3I.3I.3I.3I.3

    DDDDDISTANCE FROMISTANCE FROMISTANCE FROMISTANCE FROMISTANCE FROM

    AAAAA POINT TO A LINEPOINT TO A LINEPOINT TO A LINEPOINT TO A LINEPOINT TO A LINEJack C. Morrison

    Evergreen, Colorado

    This Gem gives efcient formulae for the distanced l between a pointPand the line dened by points A and B, and the distanced

    2between P

    and the line segmentAB . (See Fig. 1.) An example of this application an interactive program searching for a displayed line segment nearea cursor being moved by the user. For this type of operation, coordinusually are integers, and computationally expensive functions need tavoided to provide rapid response. Often, nding d21 or d22 is sufcient.

    The distance from a point to a line is the length of the segmentPQperpendicular to l ineAB . Start with the formula for the area of triang ABP,

    12 | A x B y + B xP y + P x A y A y B x B yP x P y A x|.

    Rearranging terms to savemultiplies gives the following formula fora2,twice the signed area of ABP:

    a2 = (P y A y)(B x A x) (P x A x)(B y A y).

    Since the area of ABP is also given by

    12 d 1

    |AB |,

  • 8/6/2019 Gpu Gems 2 - Programming Techniques for High-Performance Graphics and General-Purpose Computation

    41/488

    1 1

    I.3 DISTANCE FROM A POINT TO A LINE

    GRAPHICS GEMS II Edit ed by JAMES ARVO 1 1

    Figure 1.

    the square of the desired distance can be computed as

    12d = a2

    2

    B x A x( )2 + B y A y( )2 .

    The differences in the denominator already were computed fora2, so thetotal operation count is sixadds, ve multiplies, and one divide. If d lis needed directly, a square root also is necessary; but if an approxtion will do, we can apply Alan Paeth'sGraphics Gem shortcut for |AB |to get

    a 2

    B x A x + B y A y 12

    min B x A x , B y A y( )for an approximate distance with only twomultiplies and one divide.

    d 1 =

  • 8/6/2019 Gpu Gems 2 - Programming Techniques for High-Performance Graphics and General-Purpose Computation

    42/488

    1 2

    I.3 DISTANCE FROM A POINT TO A LINE

    GRAPHICS GEMS II Edit ed by JAMES ARVO 1 2

    When the perpendicular intersection pointQ is outside the segment A B, d l is shorter thand 2, the distance fromP to the nearest point of thesegment. For ap plications only interested in segmen ts within som e maxid istance fromP , a simple bounding box test can be used to quickly rejsegments that are too far away:

    min( A x , B x) margin

  • 8/6/2019 Gpu Gems 2 - Programming Techniques for High-Performance Graphics and General-Purpose Computation

    43/488

    1 3

    I.3 DISTANCE FROM A POINT TO A LINE

    GRAPHICS GEMS II Edit ed by JAMES ARVO 1 3

    The coordinate differences again can be reused, and an approximation|AP | and |BP | may be sufcient.

    With integer coordinates for A , B, and P , these formulae can becomputed without using oating point , al though care is requiredprevent overow. For example, the worst case for three points i

    512 512 region gives a value of 262,144 fora2, whose square exceeds32 bits.

    See also (3) Useful 2D Geometry, Andrew S. Glassner

  • 8/6/2019 Gpu Gems 2 - Programming Techniques for High-Performance Graphics and General-Purpose Computation

    44/488

    1 4

    I.4 AN EASY BOUNDING CIRCLE

    GRAPHICS GEMS II Edit ed by JAMES ARVO 1 4

    I.4I.4I.4I.4I.4

    AAAAAN EASY BOUNDINGN EASY BOUNDINGN EASY BOUNDINGN EASY BOUNDINGN EASY BOUNDING

    CCCCCIRCLEIRCLEIRCLEIRCLEIRCLEJon RokneUniversit y of Calgary

    Calgary, Albert a, Canada

    In Graphics Gems (Ritter, 1990), we nd a near-optimal algorithm fcomputing a bounding sphere for N points in 3D space. Here, we presena simple exact algorithm for computing the smallest bounding circle

    N points P l, . . . , P N in 2D. It was devised for a numerical analysproblem by E. Grassmann, who met with a fatal climbing accident wthe paper (Grassmann and Rokne, 1979) was about to be published.

    We use the conven t ion tha t a po in tP in 2D can be wri t ten asP = (P x, P y) , where P x i s the x coord ina te o f P an d P y i s the ycoordinate of P . Also, |P Q | is the Euclidean distance betweenPand Q .

    From geometry, it follows that the smallest bounding circle is deby two or three points of the pointset. See, for example, Rademacher

    Toeplitz (1957).Case 1. The circle is determined by two points,P and Q . The center of the circle is at (P + Q )/ 2.0 and the radius is|(P Q )/ 2.0|.

    Case 2. The circle is determined by three points,P , Q , and R , forminga triangle with acute angles.

    The two cases are shown in Fig. 1.In both cases, there may be other points of the pointset lying on

    circle.The algorithm proceeds as follows. First, determine a pointP with the

    smallest P y. Then nd a pointQ such that the angle of the line segmenPQ with the x axis is minimal. Now nd R such that the absolute valueof the angle PR Q is minimal.

    If this angle is acute, then the circleC l determined byP, Q , and Rcontainsaccording to elementary geometryall the points, since t

  • 8/6/2019 Gpu Gems 2 - Programming Techniques for High-Performance Graphics and General-Purpose Computation

    45/488

    1 5

    I.4 AN EASY BOUNDING CIRCLE

    GRAPHICS GEMS II Edit ed by JAMES ARVO 1 5

    Figure 1.

    Figure 2.

  • 8/6/2019 Gpu Gems 2 - Programming Techniques for High-Performance Graphics and General-Purpose Computation

    46/488

    1 6

    I.4 AN EASY BOUNDING CIRCLE

    GRAPHICS GEMS II Edit ed by JAMES ARVO 1 6

    are no points to the right of the directed segmentPQ . If the trianglePQ Rhas acute angles only, we are nished. If the angle at R is obtuse, thenthe center of the circle we are looking for is (P + Q)/ 2.0 and the radiusis |(P Q )/ 2.0|.

    If the angle atP or Q is obtuse, say atQ, we replace Q by R and

    repeat to get a circleC 2. See Fig. 2.From elementary geometry, it follows that the center of C l is to the leftof the directed line segmentPR , since the angle atQ otherwise would beacute. Since the radius of C 2 at most is as large as that of C l, it followsthat all the points insideC l that are to the right of the segmentPR alsoare insideC 2. No points are lost, therefore, andC 2 contains all the pointsof the pointset.

    We claim that after nitely many repetitions, we get the minimal cirTo show this, we only need to observe that a point to the right of the

    segment PR stays to the right, and therefore, we have at most N 2iterations.The algorithm has a worst-case complexity of O ( N 2).

    See also (301) An Efcient Bounding Sphere, Jack Ritter

  • 8/6/2019 Gpu Gems 2 - Programming Techniques for High-Performance Graphics and General-Purpose Computation

    47/488

    1 7

    I.5 THE SMALLEST CIRCLE CONTAINING THE INTERSECTION OF TWO CI

    GRAPHICS GEMS II Edit ed by JAMES ARVO 1 7

    I.5I.5I.5I.5I.5

    TTTTTHE SMALLEST CIRCLEHE SMALLEST CIRCLEHE SMALLEST CIRCLEHE SMALLEST CIRCLEHE SMALLEST CIRCLE

    CCCCCONTAINING THE INTERSECTIONONTAINING THE INTERSECTIONONTAINING THE INTERSECTIONONTAINING THE INTERSECTIONONTAINING THE INTERSECTIONOOOOOF TWO CIRCLESF TWO CIRCLESF TWO CIRCLESF TWO CIRCLESF TWO CIRCLESJon Rokne

    Universit y of Calgary Calgary, Alb ert a, Canada

    In Graphics Gems (Thompson, 1990), an algorithm is given for the a

    of intersection of two circles. Here we give a simple pseudo-codending the center and radius of the smallest circle containing the insection of two circles.

    We use the convention that a point in 2D can be written asP = (P x, Py )where P x is the x coordinate of P and P y is the y coordinate of P. Also,|P Q | is the Euclidean distance betweenP and Q .

    The two circles are (P, r ) and (Q , s) and the circle containing thintersection is ( R, t ).

    In Fig. 1, we show the case of non-trivial intersection.

    Let l = |P Q |. Then from the geometry, we have for this case the tequations,

    z 2 + t 2 = r 2,

    (l z)2 + t 2 = s2 ,

    which can be solved for

    z = (l2

    + r 2

    s2

    )/ (2l),

    t = r 2 z 2 .

    The o ther cases a re (P, r ) (Q , s ), (Q , s ) (P, r ), an d (P, r )(Q, s ) = .

  • 8/6/2019 Gpu Gems 2 - Programming Techniques for High-Performance Graphics and General-Purpose Computation

    48/488

    1 8

    I.5 THE SMALLEST CIRCLE CONTAINING THE INTERSECTION OF TWO CI

    GRAPHICS GEMS II Edit ed by JAMES ARVO 1 8

    Pseudo-CodePseudo-CodePseudo-CodePseudo-CodePseudo-Code

    l |P Q|;If l > r + s then intersection empty

    e lse i f l + r < s then C P , t r ;e lse i f l + s < r then C q, t s;

    e lse begin z (l2 + r 2 s)/(2l);t r 2 z 2 ;

    R P + (P Q ) z / l;end

    endif endif

    endif

    See also 1.6 App oloniuss 10th Problem, Jon Ro ne

    Figure 1.

  • 8/6/2019 Gpu Gems 2 - Programming Techniques for High-Performance Graphics and General-Purpose Computation

    49/488

    1 9

    I.6 APPOLONIUSS 10TH PROBLEM

    GRAPHICS GEMS II Edit ed by JAMES ARVO 1 9

    I.6I.6I.6I.6I.6

    AAAAAPPOLONIUSS IOTHPPOLONIUSS IOTHPPOLONIUSS IOTHPPOLONIUSS IOTHPPOLONIUSS IOTH

    PPPPPROBLEMROBLEMROBLEMROBLEMROBLEMJon RokneUniversit y of Calgary

    Calgary, Alb ert a, Canada

    A problem that occurs in computer graphics and CAD is the construcof the smallest circle touching three given circles from the outside. problem already was considered by Appolonius (c. 262-190 B.C.).

    The solution given here was used in a numerical analysis prob(Grassmann and Rokne, 1979).

    It should be pointed out that the problem of nding the smallest spcontaining three given spheres also is reduced to the problem of Appnius by considering the plane passing through the centers of the sph

    We note rst that the problem may have no solution; i.e., there mnot exist a circle touching the given circles from the outside. See Figand 2.

    In Fig. 1, there is no circle that touches the given circles, whereaFig. 2, at least one of the circles has to be outside. The decisprocedure for the case in Fig. 1 is obvious, whereas the decision prdure for the case in Fig. 2 requires the computation of the outscommon tangents of the largest and the smallest circle as well as forlargest and the middle circle. See Glassner (1990b).

    It is convenient to use some geometric properties of complex analto develop the algorithm. These are not stressed in the usual coursecomplex analysis in North America; hence, we refer to Behnkeet al.,(1983), where the required formulas are given.We identify points in the plane with complex quantities; that is, if Z isa point in 2D, then we wri te the x coordinate as Re Z and the ycoordinate as Im Z , and we also think of it as a complex quantit

    Z = Re Z + i Im Z . An overline means reection with the real axis fo

  • 8/6/2019 Gpu Gems 2 - Programming Techniques for High-Performance Graphics and General-Purpose Computation

    50/488

    2 0

    I.6 APPOLONIUSS 10TH PROBLEM

    GRAPHICS GEMS II Edit ed by JAMES ARVO 2 0

    Figure 1.

    Figure 2.

  • 8/6/2019 Gpu Gems 2 - Programming Techniques for High-Performance Graphics and General-Purpose Computation

    51/488

    2 1

    I.6 APPOLONIUSS 10TH PROBLEM

    GRAPHICS GEMS II Edit ed by JAMES ARVO 2 1

    mapping the Z plane into aW plane.This maps the unknown circle into a straight line, the two other cir

    into circles with centers and radii (W i, i), where (as shown by Gargantinand Henrici (1972), Eq. (2.1), or any textbook describing the Moe

    transformation)

    Figure 3.

    W i =

    Z i Z 3( ) Z i Z 3

    2 r i( )2

    i =

    r i Z i Z 3

    2 r i( )2

    i = 1, 2. (1)

    , ,

    point in 2D and the complex conjugate when we consider the point a complex quantity.

    Let ( Z i , r i), i = 1, 2, 3 be the center and radii of the circles. First, subtrthe smal les t rad ius , sayr 3, f rom the o ther two , ge t t ingr i*=r i r 3.

    If we can find a circle ( Z, r ) that passes through Z3 and touches ( Z i , r i*),i = 1, 2 from outside, then the disc ( Z, r + r 3) will be the solution to the

    original problem. The geometry of this is shown in Fig. 3.To find this circle, we use the transformation,

    W = ( Z ) =

    1

    Z Z 3,

  • 8/6/2019 Gpu Gems 2 - Programming Techniques for High-Performance Graphics and General-Purpose Computation

    52/488

    2 2

    I.6 APPOLONIUSS 10TH PROBLEM

    GRAPHICS GEMS II Edit ed by JAMES ARVO 2 2

    Note that i < 0 if Z 3 is contained in disci, i = 1, 2, and that any circlethrough Z3 in the Z plane is mapped into a l ine in theW plane.

    The problem now is to nd a common tangent l ine,l: Re UW = c, inthe W plane. This line mapped back into the Z plane will provide thedesired circle.

    If l separates the centers from the origin, the corresponding circlethe Z plane will separate the centers from, i.e., contain the given discinside.

    The constantsU and c are determined by the following method.We can assume that U = 1. Then the distance formula gives

    Re UW i c = i , i= 1, 2. (2)Subtracting, we get

    Re U (W 1 W 2) = 1 2,and calling

    = arg U (W 1 W 2),

    we get

    cos =

    1 2W 1 W 2

    (3)

    and

    sin = 1 cos 2 , (4)

    so

    cos + i sin =U W 1 W 2( )

    W 1 W 2,

    and multiplying both sides by

    W 1 W 2( )W 1 W 2

    ,

  • 8/6/2019 Gpu Gems 2 - Programming Techniques for High-Performance Graphics and General-Purpose Computation

    53/488

    2 3

    I.6 APPOLONIUSS 10TH PROBLEM

    GRAPHICS GEMS II Edit ed by JAMES ARVO 2 3

    we get

    U = (cos + i sin )

    W 1 W 2( )W 1 W 2

    . (5)

    The ambiguity of the root in Eq. (2) gives the two solutions,U l, U 2,expected from geometry. (The geometric meaning of is the anglebetween the normal of l and that of the line joiningW l and W 2.) The factthat we have positive

    i has the geometric meaning that the normalsU jpoint towards the centers, and the line does not separate the centers

    Using the rst of the equations, (2), we get the correspondingc j,

    c j = Re W 1U j i .

    The line separates theW i from the origin if c j > 0.We shall need the points of contact as well. They are

    T i j = W i i U j , i = 1 , 2 , j = 1, 2. (6)

    We now transform the equations, ReU jW = c j , back into the Z planeand get

    ReU j

    Z Z 3 = 1

    Z Z 32 Re U

    j( Z Z

    3) = c

    j,

    and wri t ing Z Z 3 = + i , U j = m j + in j , we get

    m j + n j = c j( 2 + 2),

    or since, |U j| = 1,

    m j

    2c j

    2

    + n j

    2c j

    2

    =1

    4c j2

    ,

    i.e., the circle has center Z = U j / (2c j) + Z 3 and radiusr = 1/ (2c j).We can see that we need only the line for whichc j is greater, since we

    are interested in the circle of minimal radius.

  • 8/6/2019 Gpu Gems 2 - Programming Techniques for High-Performance Graphics and General-Purpose Computation

    54/488

    2 4

    I.6 APPOLONIUSS 10TH PROBLEM

    GRAPHICS GEMS II Edit ed by JAMES ARVO 2 4

    The points of contact are

    T ij =

    1T ij

    * + Z 3 .

    The computations are quite simple as shown by the following pseucode.

    Pseudo-CodePseudo-CodePseudo-CodePseudo-CodePseudo-Code

    Compute 1r , 2r .Compute W

    1 , W

    2 ,

    1 ,

    2from (1).

    Compute cos from (3) and sin from (4).Compute c 1 , c 2 from (5).Choose c = max (c 1 , c 2) and corresponding U from (5).The required circle is t hen Z = U / (2c) + Z3 with radius r = 1/ (2c).

    See also 1.5 The Smallest Circle Containing the Intersect ion Two Circles, Jon Rokne

  • 8/6/2019 Gpu Gems 2 - Programming Techniques for High-Performance Graphics and General-Purpose Computation

    55/488

    2 5

    I.7 A PEANO CURVE GENERATION ALGORITHM

    GRAPHICS GEMS II Edit ed by JAMES ARVO 2 5

    I.7I.7I.7I.7I.7

    AAAAA PEANO CURVEPEANO CURVEPEANO CURVEPEANO CURVEPEANO CURVE

    GGGGGENERATION ALGORITHMENERATION ALGORITHMENERATION ALGORITHMENERATION ALGORITHMENERATION ALGORITHMKen MusgraveYale Universit y

    New Haven, Connect icut

    One occasionally may desire a space-llingPeano curve in n dimensions(Peano, 1980). The Peano curve is a fractal with integer-fractal dimsion. This curve provides a continuous mapping from the integern-space, with the properties that all points in the domain are close inrange, and most points that are close in the range are close in the domApplications we have found include image compression, color quantion (Stevenset al., 1983; Whitten and Neal, 1982), and debugging interactive four-dimensional rotation routines.

    We have implemented Butzs algorithm (1971) for generating ann -dimensional Peano curve tom bits of precision, that is, with 2m seg-ments per dimension,2 m n segments total. It is based on bitwise booleoperations, and is the only algorithm we have ever coded where we hhad absolutely no insight into how it works. But the codes coroperation has been veried in two, three and four dimensions. See gand 2 (color insert) for an example in 2-dimensions.

    The algorithm, but not i ts derivation, is described in Butz (19Unfortunately, no insight is offered there for the logic of the algorit

    See also 1.8 Space-Filling Curves and a Measure of Coherence,Douglas Voorhies

  • 8/6/2019 Gpu Gems 2 - Programming Techniques for High-Performance Graphics and General-Purpose Computation

    56/488

    1.7 Figures 1 and 2. These plates show the Peano curve in two dimensions with 8 subdivisions, or bits of precision, at 512 by 512 resolution. Thus alternating pixels are covered by the curve. The two images representthe same curve, as seen at different points in a colormap animation performed by ran_ramp. Note the changingpatterns created by the colormap animation.

  • 8/6/2019 Gpu Gems 2 - Programming Techniques for High-Performance Graphics and General-Purpose Computation

    57/488

  • 8/6/2019 Gpu Gems 2 - Programming Techniques for High-Performance Graphics and General-Purpose Computation

    58/488

  • 8/6/2019 Gpu Gems 2 - Programming Techniques for High-Performance Graphics and General-Purpose Computation

    59/488

  • 8/6/2019 Gpu Gems 2 - Programming Techniques for High-Performance Graphics and General-Purpose Computation

    60/488

    2 9

    I.8 SPACE-FILLING CURVES AND A MEASURE OF COHERENCE

    GRAPHICS GEMS II Edit ed by JAMES ARVO 2 9

    Figu re 3. Average coherence of Peano an d H ilbert traver sal sequ ences for objeradii of .5 to 100.

  • 8/6/2019 Gpu Gems 2 - Programming Techniques for High-Performance Graphics and General-Purpose Computation

    61/488

    3 0

    I.8 SPACE-FILLING CURVES AND A MEASURE OF COHERENCE

    GRAPHICS GEMS II Edit ed by JAMES ARVO 3 0

    plus the recently touched ones. In this context,high coherence meansstaying within the current object area (or nearby ones) most of the tand venturing outside rarely, until all the current object area pixels hbeen visited.

    To make this concrete, I assume that an imaginary object projects

    circle in screen space. Choosing a circle favors no traversal direction another. Coherence of a traversal sequence then is the current obcircles diameter divided by the number of t imes the sequence ethe circle. Hysteresis is added by only counting paths that exit the ject circle and then venture another radius away; this avoids penalismall excursions beyond the circle that immediately return. See Fig.

    By this pragmatic measure, object areas not touching the screen edhave a coherence measure of 1.00 for normal scanline traversal, sincsequences that penetrate the circle exit and continue all the way to

    screen edge and thus are counted. For Fig. 3, a 10241024 screen istraversed by three methods: normal scanline traversal, a Peano curve,a Hilbert curve. (Since the Peano curve did not t evenly into 1021024, it was clipped.) The coherence of 1000 circles centered at ranon-screen positions is averaged for a range of radii.

    Both the Peano and Hilbert curves far exceed thecoherence = 1.00measure of conventional scanline traversal. The Hilbert curve appsuperior, and is easier to generate. Better performance from a renderer can be expected simply by choosing either of these alternatraversal sequences.

    See also 1.7 A Peano Curve Generation Algorithm,Ken Musgrave

  • 8/6/2019 Gpu Gems 2 - Programming Techniques for High-Performance Graphics and General-Purpose Computation

    62/488

  • 8/6/2019 Gpu Gems 2 - Programming Techniques for High-Performance Graphics and General-Purpose Computation

    63/488

    3 2

    I.9 SCANLINE COHERENT SHAPE ALGEBRA

    GRAPHICS GEMS II Edit ed by JAMES ARVO 3 2

    Figure 1. Basic shape algebra operations.

    First, the bit mask is expensive to maintain for wide scan lines. Secthere is an unquantied legal expense involved in the utilization of patent.

    Two unpublished papers exist that describe portions of the appropresented here, Gosling (1986) and Donato and Rocchetti (1988). Eaversions of this work appear in Steinhartet al. (1989, 1990).

    Data StructuresData StructuresData StructuresData StructuresData Structures

    Our app roach is to represent arbitrary shap es as y-sorted lists of x-sorted,non-overlapping rectangles. The decomposition of an ellipse with a angular hole into our shape representation is shown in Fig. 2.

    We store shape representations as linked lists. Although packed a

    formats such as the one discussed in Steinhartet al. (1989) are morecompact, a signicant amount of data copying can occur in the differoperation. Also, i t is difcult to manage the amount of array sprequired, since the worst-case combination of a shape containinmrectangles with a shape containingn rectangles is a shape containinm n rectangles.

    Figure 2. Sample shape decomposition.

  • 8/6/2019 Gpu Gems 2 - Programming Techniques for High-Performance Graphics and General-Purpose Computation

    64/488

    3 3

    I.9 SCANLINE COHERENT SHAPE ALGEBRA

    GRAPHICS GEMS II Edit ed by JAMES ARVO 3 3

    Figure 3. Representation of a sample shape.

    The choice of memory management techniques used for the dstructures can have a dramatic impact on the performance of the arithms. This gem does not go into the details of memory managemsince the choice of technique is very dependent on the operating envment.

    A shape is a vert ical ly ordered set of horizontalspans. Each spancontains a horizontally ordered set of segments. The span data structurecontains the y coordinate of the top of the span, a pointer to the nspan, and a pointer to the segment list. Each element of the segmentcontains an x coordinate and a pointer to the next segment. Figureshows a sample shape and the data structures that represent i t .

    There are three spans in the structure. The rst one describes rectangle ((10, 10),(50, 20)). The second sp an d escribes th e two recgles, ((10, 20),(20, 30)) an d ((40, 20),(50, 30)). The th ird sp an d escriberectangles and merely provides the upper y bound for the second span.

    Note that we place the pixels between the grid lines as shown in 4a, not on the grid lines as shown in Fig. 4b.

  • 8/6/2019 Gpu Gems 2 - Programming Techniques for High-Performance Graphics and General-Purpose Computation

    65/488

    3 4

    I.9 SCANLINE COHERENT SHAPE ALGEBRA

    GRAPHICS GEMS II Edit ed by JAMES ARVO 3 4

    Figure 4. Pixel locations.

    Spans and structures are constructed from instances of thespan andsegment data structures, dened as follows:

    segment:record [ sp an: record [next: segm ent; next: span;x: integer ; x: segment;

    ]; y: integer ;];

    We allow a NIL pointer to represent an empty shape for convenienc

    The Basic AlgorithmThe Basic AlgorithmThe Basic AlgorithmThe Basic AlgorithmThe Basic Algorithm

    The shape algebra routines alternately process spans in an outer land segments in an inner loop. Let us look at what happens with opt ions on tw o s imp le sha p es , the rectang les ( (10, 10) ,(20 , 20)) ((15, 15),(25, 25)), as sh ow n in Fig. 5.

    The outer loop contains two pointers, one for each span list . Opointer is advancedor both areeach time through the loop. The whose y value is lowest has i ts pointer advanced; both pointers advanced if both spans have the same y value. The effect is to generate new set of span y values as shown in Fig. 6.

    Figure 5. Two simple shapes.

  • 8/6/2019 Gpu Gems 2 - Programming Techniques for High-Performance Graphics and General-Purpose Computation

    66/488

    3 5

    I.9 SCANLINE COHERENT SHAPE ALGEBRA

    GRAPHICS GEMS II Edit ed by JAMES ARVO 3 5

    Each iteration through the outer loop produces a y coordinate and apair of segments to be operated on by the inner loop. Note that the oloop supplies an empty segment in cases where one shape begins or before the other.

    The inner loop processes the segments in much the same mannethe outer loop processes the spans. A bit is used to keep track of the of each segment. Each x value toggles this state bit. The state bits froeach segment are combined into a state code as shown in Fig. 7.

    Figure 7. State code generation.

    Figure 6. Span processing.

  • 8/6/2019 Gpu Gems 2 - Programming Techniques for High-Performance Graphics and General-Purpose Computation

    67/488

    3 6

    I.9 SCANLINE COHERENT SHAPE ALGEBRA

    GRAPHICS GEMS II Edit ed by JAMES ARVO 3 6

    Figure 8. Shape algebra opcodes.

    One segment is advancedor both areeach time through the inloop. The segment that ends at the lower x coordinate is advanced; bothsegments are advanced if they both end at the same place. The state

    is monitored; x values that cause the state code to change to or from opcode are added to the output. The effect of each opcode is shownFig. 8. Note that code 1 produces AB; a code of 2 would produce BA.

    Th e x coordinates in segments come in pairs. The rst coordinateeach pair is a transition from off to on for the coordinate value. second coordinate in each pair is a transition from on to off occurs after the coordinate value.

    The basic shape algebra routine is presented as follows. Althougsingle routine could be used to implement al l of the operat ionsconsiderable amount of time can be wasted using the opcode to chthe appropriate special case handling code. Substantial performabenets can be realized by using optimized code for each opcode. special case handling code is discussed in the following sections.

    procedure shape_operation(shape1, shape2, opcode)begin

    result x1 x2 NIL;handle special cases that occur before the shapes overlap in ywhi le shape1 N IL an d shape2 N IL dobegin

    test shape1.y shape2.y;i f test 0 then

    begin y shape1.y; x1 shape1.x; shape1 shape1.next;end ;

  • 8/6/2019 Gpu Gems 2 - Programming Techniques for High-Performance Graphics and General-Purpose Computation

    68/488

    3 7

    I.9 SCANLINE COHERENT SHAPE ALGEBRA

    GRAPHICS GEMS II Edit ed by JAMES ARVO 3 7

    i f test 0 thenbegin y shape2.y; x2 shape2.x; shape2 shape2.next;end ;

    ag old 0;segment NIL;handle special cases that occur before the shapes overlap in xp 1 x1; p2 x2;whi le p 1 N IL an d p 2 N IL dobegin

    test p1.x p2.x;i f test 0 then

    begin x p1.x; ag ag bit-xor 1; p1 p1.next; end ;i f test 0 then

    begin x p2.x; ag ag bit-xor 2; p2 p2.next; end ;

    i f ag = opcodeor old = opcode thenbegin append a new element to segment with ualue xend ;

    old ag;end ;

    handle special cases that occur after the shapes overlap in xi f segment NIL or result N IL then

    append a new element to result with values y and segment end ;

    handle special cases that occur after the shapes overlap in yreturn [result];end ;

    IntersectionIntersectionIntersectionIntersectionIntersection

    The intersection algorithm takes advantage of the fact that the resuempty if one operand is empty. Only one piece of special case codnecessary; the remaining pieces are empty.

    special case code for before the shapes overlap in yi f shape1 = N IL or shape2 = N ILreturn [NIL];

  • 8/6/2019 Gpu Gems 2 - Programming Techniques for High-Performance Graphics and General-Purpose Computation

    69/488

    3 8

    I.9 SCANLINE COHERENT SHAPE ALGEBRA

    GRAPHICS GEMS II Edit ed by JAMES ARVO 3 8

    UnionUnionUnionUnionUnion

    Both shap e operand s mu st be traversed to the end in the un ion algoriWe can get some performance improvement by avoiding the tests handling the cases where one span or segment is empty as straighward copies.

    special case code for before the shapes overlap in yi f shape1 = NILthen

    begin return [a copy of shape2 ]; end ;i f shape2 = N IL then

    begin return [a copy of shape1 ]; end ;i f shape1.y < shape2.ythenbegin

    copy the span from shape1 and append it to result x1 shape1.x;shape1 shape1.next;end ;

    e lse i f shape2.y < shape1.ythenbegin

    copy the span from shape2 and append it to result x2 shape2.x;shape2 shape2.next;end ;

    special case code for before the shapes overlap in xi f x1 N IL then

    begin copy x1; end ;e lse i f x2 N IL then

    begin copy x2; end ;e lse

    do the normal inner loop processing

    special case code for after the shapes overlap in xi f x1 N IL then

    begin copy the remainder of x1; end ;e lse i f x2 N IL then

    begin copy the remainder of x2; end ;

  • 8/6/2019 Gpu Gems 2 - Programming Techniques for High-Performance Graphics and General-Purpose Computation

    70/488

    3 9

    I.9 SCANLINE COHERENT SHAPE ALGEBRA

    GRAPHICS GEMS II Edit ed by JAMES ARVO 3 9

    special case code for after the shapes overlap in yi f shape1 N IL then

    begin copy the remainder of shape1 to result end ;e lse

    begin copy the remainder of shape2 to result end ;

    DifferenceDifferenceDifferenceDifferenceDifference

    Only the rst shape operand must be traversed to the end in the dience algorithm. We can get some performance improvement by avoithe tests and handling the cases where one span or segment is emptstraightforward copies.

    special case code for before the shapes overlap in yi f shape1 = NILthen

    return [NIL];i f shape2 = NILthen

    begin return [a copy of shape1 ]; end ;i f shape1.y < shape2.ythenbegin

    copy the span from shape1 and append it to result x1 shape1.x;shape1 shape1.next;end ;

    e lse i f shape2.y < shape1.ythenbegin x2 shape2.x; shape2 shape2.next;end ;

    special case code for before the shapes overlap in xif x1 = NIL then

    begin segment is NIL; end ;e lse i f x2 = NIL then

    begin copy x1 ; end ;e lsedo the normal inner loop processing

    special case code for after the shapes overlap in xif x1 N IL then

    begin copy the remainder of x1 ; end ;

  • 8/6/2019 Gpu Gems 2 - Programming Techniques for High-Performance Graphics and General-Purpose Computation

    71/488

  • 8/6/2019 Gpu Gems 2 - Programming Techniques for High-Performance Graphics and General-Purpose Computation

    72/488

    4 1

    I.9 SCANLINE COHERENT SHAPE ALGEBRA

    GRAPHICS GEMS II Edit ed by JAMES ARVO 4 1

    for each segment in the current span doadd a new segment to the new span with the x from the current segment

    endreturn [result];

    end ;The third utility translates a shape by a relative x, y offset.

    procedure shape_translate(shape, x, y)begin

    for each span in shape dobegin

    span.y span.y + y;

    for each segment in the current span dobegin segment.x segment.x + x;endend

    return ;end ;

    The nal utility deallocates the storage consumed by a shape descrip

    procedure shape_free(shape)begin

    whi le shape NIL dobegin

    x shape.x;whi le x N IL dobegin tmpx x; x x.next; free_memory(tmpx);endtmpy shape; shape shape.next; free_memory(tmpy);end

    return ;end ;

    ApplicationsApplicationsApplicationsApplicationsApplications

    Shape algebra is useful for computing and representing the visible arewindows in window management systems. Let us assume that we ha

  • 8/6/2019 Gpu Gems 2 - Programming Techniques for High-Performance Graphics and General-Purpose Computation

    73/488

    4 2

    I.9 SCANLINE COHERENT SHAPE ALGEBRA

    GRAPHICS GEMS II Edit ed by JAMES ARVO 4 2

    hierarchical window system in which descendent windows are clippetheir ancestors boundaries . Thewindow da ta s t ruc tu re shown nexcontains pointers to other windows in the window tree, the size position of the window, and the visible area description.

    window: record [parent: window; parent windowchild: window; highest priority (frontmost) child)n_sib: window; next lowest priority siblingp_sib: window; next highest priority siblingx: integer ; x coordinate of upper left corner y: integer ; y coordinate of upper left corner width : integer ; width of window

    height: integer ; height of windowvisible: span; shape that describes visible area];

    Figure 9 shows how the window pointers describe the relationship window with other windows in the window tree.

    The visible area for each window in the tree can be computed usingfollowing algorithm.

    Figure 9. Window tree

  • 8/6/2019 Gpu Gems 2 - Programming Techniques for High-Performance Graphics and General-Purpose Computation

    74/488

    4 3

    I.9 SCANLINE COHERENT SHAPE ALGEBRA

    GRAPHICS GEMS II Edit ed by JAMES ARVO 4 3

    procedure compute_window_area( )begin

    root.visible shape_box(root.x, root.y, root.width, root.height);compute _subwindow_area(root);returnend

    procedure compute_subwindow_area(parent)begin

    for each child in order from front to back dobegin

    shape_free(child.visible);child .visible = shap e_intersect(parent.visible,shape_tmp_box(child.x, child.y, child.width, child.height));tmp = parent.visible;parent.visible = shape_difference(parent.visible, child.visible)shape_free(tmp);compute_subwindow_area(child);end

    returnend

    Complex graphical objects such as fat curves can be generated ushape algebra. One technique for the generation of fat curves to drpen along the curve trajectory. An example of this technique is showFig. 10. The line marks the actual trajectory; the dashed boxes showpen shape description.

    Figure 10. Fat curve generation.

  • 8/6/2019 Gpu Gems 2 - Programming Techniques for High-Performance Graphics and General-Purpose Computation

    75/488

    4 4

    I.9 SCANLINE COHERENT SHAPE ALGEBRA

    GRAPHICS GEMS II Edit ed by JAMES ARVO 4 4

    a) Shap e to dr aw b) Clipp ing shape c) Clipped draw n shapeFigure 11. Complex clipping.

    The following algorithm can be used to generate fat curves. Note thspecial union routine that took pen offset parameters would be mefficient than the repeated translation of the pen shape description.

    curve_shape N ILfor each point in the curve dobegin

    translated_pen shape_translate(pen, point.x, point.y);curve_shape = shape_union(curve_shape, translated_pen);shape_free(translated_shape);end

    Another application of shape algebra is complex clipping such as used by P O S T S C R I P T and by window management systems. The algrithms for the rasterization of many primitives, including polygons (Fet al., 1990) and fat curves, produce output that essentially is a y-sortedlist of x -sorted rectangles. These primitives can be clipped easilyarbitrary shapes using intersection as shown in Fig. 11.

    An efcient variation on the intersection routine for clipping isinter-sect and ll. This variation invokes an actual drawing function each ta new rectangle would be added to the result shape instead of buildthe shape.

    ImprovementsImprovementsImprovementsImprovementsImprovements

    An examination of shape algebra applications reveals a pattern; temrary shapes are being created, used, and then destroyed. This g

  • 8/6/2019 Gpu Gems 2 - Programming Techniques for High-Performance Graphics and General-Purpose Computation

    76/488

    4 5

    I.9 SCANLINE COHERENT SHAPE ALGEBRA

    GRAPHICS GEMS II Edit ed by JAMES ARVO 4 5

    memory management a real workout. The following type of operatiotypical:

    shape1 shape1 op shape2;

    The amount of overlap between shape operands often is minimal w

    compared to the total size of the shapes in operations such as fat cdrawing. The shape_union an d shape_difference routines can be modi-ed to substantially reduce memory management overhead in such apcations.

    procedure shap e_operation(shap e1, shap e2, op cod e)begin

    for any part of shape1 that comes before any part of shape2 dodetach spans from shape1 and attach them to the result

    do the normal operation

    for any part of shape1 that comes after any part of shape2 dodetach spans from shape1 and attach them to the result

    free the spans in shape1 that overlapped shape2

    This is effective only for the union and difference operations as non-overlapping parts of shapes are discarded for intersection operat

    Shape algebra operat ions can produce fragmented shapes in whmore than the minimal number of rectangles is used to represent shape. Such shapes can be compressed into minimal representatialthough this typically consumes more time than it saves. Compressioa worthwhile optimization only in situations where a computed shapused for a long period of t ime without being changed.

    AcknowledgmentsAcknowledgmentsAcknowledgmentsAcknowledgmentsAcknowledgments

    I would like to thank Julie Donnelly, Robert Reed, and Ken Rhodestheir proofreading, without which this gem would have been awed

  • 8/6/2019 Gpu Gems 2 - Programming Techniques for High-Performance Graphics and General-Purpose Computation

    77/488

    I IIIMAGEPROCESSING

  • 8/6/2019 Gpu Gems 2 - Programming Techniques for High-Performance Graphics and General-Purpose Computation

    78/488

    GRAPHICS GEMS II Edit ed by JAMES ARVO 4 9

    IIIIIIIIII

    IIIIIMAGE PROCESSINGMAGE PROCESSINGMAGE PROCESSINGMAGE PROCESSINGMAGE PROCESSING

    The output of every graphics program is animage. Images are typically

    represented as a two-dimensional array of pixels, or some encodinthat information. When the input and output of a program are bimages, we say the program is performingimage processing. All thegems of this part involve some form of image transformation or changrepresentation. These include accurate pixel representations, scalingsharpening, rotation, mapping an image into a nearly equivalent imwith fewer colors or shades of gray, and compression.

    This last topic, compression, is driven by a very common problempicture may be worth a thousand words, but as many a graphics prog

    mer will attest , i t frequently takes many more than a thousand wordstore one. In fact, the storing of images is notorious for eating up amounts of secondary storage, such as disk or tape. Fortunately, mimages contain a large amount of redundant information that cansqueezed out for compact storage. Two of the gems in this part presimple approaches for doing just that.

  • 8/6/2019 Gpu Gems 2 - Programming Techniques for High-Performance Graphics and General-Purpose Computation

    79/488

    II.1 IMAGE SMOOTHING AND SHARPENING BY DISCRETE CONVOLUTI

    GRAPHICS GEMS II Edit ed by JAMES ARVO 5 0

    II.1II.1II.1II.1II.1

    IIIIIMAGE SMOOTHING ANDMAGE SMOOTHING ANDMAGE SMOOTHING ANDMAGE SMOOTHING ANDMAGE SMOOTHING AND

    SHARPENING BY DISCRETESHARPENING BY DISCRETESHARPENING BY DISCRETESHARPENING BY DISCRETESHARPENING BY DISCRETECONVOLUTIONCONVOLUTIONCONVOLUTIONCONVOLUTIONCONVOLUTIONDale A. SchumacherSt Paul, Minnesot a

    One of the reasons for capturing an image digitally is to allow umanipulate it to better serve our needs. Often this will include tryin

    improve the subjective appearance of an image through smoothingrainy features or sharpening of indistinct features. These goals sotimes can be accomplished through the use of adiscrete convolutionoperation (also calleddigital filtering).

    Discrete convolution determines a new value for each pixel in an imby computing some function of that pixel and its neighbors. Often function sim ply is a weighted sum of pixel values in a sm all neighborhof the source pixel. These weights can be represented by a small mthat sometimes is called aconvolution kernel. The dimensions of the

    matrix must be odd so there will be a central cell to represent the weof the original value of the pixel for which we are computing a new vThe new value is computed by multiplying each pixel value in neighborhood of the central pixel by the corresponding weight inmatrix, summing all the weighted values, and dividing by the sumthe weights in the matrix. The following pseudo-code shows this comtation for a 3 3 convolution kernel.

    input : array [1..width]of array [1..height]of pixel ;ou tpu t : array [1..width]of array [1..height]of pixel ;kernel: array [1..1] of array [1..1] of in teger ;sum, ksum:in teger ;

  • 8/6/2019 Gpu Gems 2 - Programming Techniques for High-Performance Graphics and General-Purpose Computation

    80/488

    II.1 IMAGE SMOOTHING AND SHARPENING BY DISCRETE CONVOLUTI

    GRAPHICS GEMS II Edit ed by JAMES ARVO 5 1

    compute_output_value(x:integer , y : integer );begin

    su m 0;ksum 0;for j: integer 1, j j + 1, j

  • 8/6/2019 Gpu Gems 2 - Programming Techniques for High-Performance Graphics and General-Purpose Computation

    81/488

    II.1 IMAGE SMOOTHING AND SHARPENING BY DISCRETE CONVOLUTI

    GRAPHICS GEMS II Edit ed by JAMES ARVO 5 2

    Figure 1. Origin al testimage

    Figu re 2. Figu re 3. Figu re 4.

    Figu re 5. Figu re 6. Figu re 7.

    1 1 11 1 11 1 1

    1 1 11 0 11 1 1

    0 1 01 2 10 1 0

    0 1 01 4 10 1 0

    1 2 12 5 21 2 1

    1 1 18 16 81 1 1

  • 8/6/2019 Gpu Gems 2 - Programming Techniques for High-Performance Graphics and General-Purpose Computation

    82/488

    II.1 IMAGE SMOOTHING AND SHARPENING BY DISCRETE CONVOLUTI

    GRAPHICS GEMS II Edit ed by JAMES ARVO 5 3

    sequence of spatially separated gaussian-shaped samples; thus, usingaussian weighted lter should accurately emulate such sampling.

    Up to now, the lters shown have been horizontally and verticsymmetrical. Sometimes, an asymmetric lter is desired, such as pr ocessing im ages taken from interlaced video. Since two elds of a v

    frame are spaced 1/ 60th of a second ap art in time, the image oshows much more coherence in the horizontal direction, along the sline, than in the vertical. The lter shown in Fig. 7 often works welcaptured video. Vertical lines are smoothed, but horizontal lines distinct.

    Smooth ing reduces no ise byspreading it out over a l a rger a rea ,making it more diffuse. This also has the same effect on any shtransitions in a source image. Some detail will be lost by smoothingyou must take care in how you apply this operation. Smoothing w

    best on images where the smallest detail of interest is at least as largthe ltering kernel, such as the 3 3 pixels in the examples given thufar. Smoothing also can be used to improve the performance of scompression and dithering algorithms, by making the changes in pixels values more gradual.

    On the other hand, there are many times when we would rather perfthe inverse of smoothing to sharpen an image, attempting to bringd etails that are ind istinct. Interestingly enou gh, it is possible to do exthat with a carefully chosen ltering kernel.

    If we look at smoothing as a way of enhancing the similarity betwnearby pixels, sharpening should enhance the differences. If we takweighted average of the pixels surrounding a given pixel, excludingcentral pixel (as in Fig. 3), and take that average away from the valuthe central pixel, we enhance the difference between that pixel andneighborhood. The kernel in Fig. 9, sometimes called adiscrete lapla-cian lter, computes this difference. By weighting the neighboring pixnegatively, we take their value aw ay rath er than ad d it to the central pThis produces the desired edge-sharpening effect. Note that the weighthe central pixel is 1 greater than the sum of the negative weights. Iflter is applied to a large area of all the same value, the weight ofneighbors cancels four times the center value, leaving one times center value remaining, and the pixel thus retains its original value.

    If we want our sharpening lter to have a little larger radius, we needene a larger kernel. The kernel shown in Fig. 10 is the sharpen

  • 8/6/2019 Gpu Gems 2 - Programming Techniques for High-Performance Graphics and General-Purpose Computation

    83/488

    II.1 IMAGE SMOOTHING AND SHARPENING BY DISCRETE CONVOLUTI

    GRAPHICS GEMS II Edit ed by JAMES ARVO 5 4

    Figure 8. Smoothed test image.

    Figu re 9. Figu re 10. Figu re 11.

    Figure 12. Ideal test image.

    0 1 01 5 1

    0 1 0

    1 2 12 13 21 2 1

    1 2 12 16 21 2 1

  • 8/6/2019 Gpu Gems 2 - Programming Techniques for High-Performance Graphics and General-Purpose Computation

    84/488

    II.1 IMAGE SMOOTHING AND SHARPENING BY DISCRETE CONVOLUTI

    GRAPHICS GEMS II Edit ed by JAMES ARVO 5 5

    (a) Or iginal im age, (b) p rocessed version .

    Figure 13.

    counterp