Main Page | Namespace List | Class Hierarchy | Class List | File List | Namespace Members | Class Members | Related Pages

region.h

00001 // This interface is based on the one in the file region.h in gtk+.
00002 // That file didn't contain license info, so is presumably under the
00003 // same LGPL license as the rest of that library.
00004 
00005 // Stolen from Gtk+ and C++-ized by Ron Steinke, January 2003
00006 
00007 #ifndef _WFTK_REGION_H_
00008 #define _WFTK_REGION_H_
00009 
00010 #include <SDL/SDL.h>
00011 
00012 #include <wftk/point.h>
00013 #include <wftk/rect.h>
00014 #include <vector>
00015 
00016 namespace wftk { 
00017 
00020 class Region
00021 {
00022  public:
00024   Region();
00026   Region(const Region&);
00028   enum FillRule
00029   {
00030     EVEN_ODD_RULE, 
00031     WINDING_RULE   
00032   };
00034   Region(const std::vector<Point>, FillRule);
00036   Region(const SDL_Rect&);
00038   Region(const Point&);
00039 
00040   ~Region() {delete [] rects;}
00041 
00043   Region& operator=(const Region&);
00044 
00046   Rect getClipbox() const
00047     {return Rect(extents.x1, extents.y1,
00048         extents.x2 - extents.x1, extents.y2 - extents.y1);}
00049 
00051   class RectList {
00052    public:
00054     RectList(unsigned n) : rects_(new SDL_Rect[n]), nrects_(n) {}
00056     RectList(const RectList& rl) : rects_(0), nrects_(0) {operator=(rl);}
00057     ~RectList() {delete [] rects_;}
00058 
00060     RectList& operator=(const RectList& rl);
00061 
00063     SDL_Rect* rects() const {return rects_;}
00065     unsigned nrects() const {return nrects_;}
00066 
00067     // The 0 index for overflow is safe, but kind of silly
00069     SDL_Rect& operator[](unsigned n) {return rects_[n < nrects_ ? n : 0];}
00070 
00071    private:
00072     SDL_Rect* rects_;
00073     unsigned nrects_;
00074   };
00075 
00077   RectList getRectangles() const;
00078 
00080   bool empty() const {return numRects == 0;}
00082   bool operator==(const Region&) const;
00084   bool operator!=(const Region& r) const {return !operator==(r);}
00086   bool operator<(const Region&) const;
00088   bool contains(const Point&) const;
00090   enum OverlapType
00091   {
00092     OVERLAP_IN,  
00093     OVERLAP_OUT, 
00094     OVERLAP_PART 
00095   };
00097   OverlapType overlap(const SDL_Rect&) const;
00098 
00100   void clear();
00101 
00103   friend Region operator|(const Region& r, const Region& r2)
00104     {Region out(r); out |= r2; return out;}
00106   friend Region operator&(const Region& r, const Region& r2)
00107     {Region out(r); out &= r2; return out;}
00109   friend Region operator-(const Region& r, const Region& r2)
00110     {Region out(r); out -= r2; return out;}
00112   friend Region operator^(const Region& r, const Region& r2)
00113     {Region out(r); out ^= r2; return out;}
00114 
00116   void offset(int dx, int dy);
00118   void offset(const Point& p) {offset(p.x, p.y);}
00125   void shrink(int dx, int dy);
00126 
00128   Region& operator|=(const Region&);
00130   Region& operator&=(const Region&);
00132   Region& operator-=(const Region&);
00134   Region& operator^=(const Region&);
00135 
00137 
00143   Region boundary(bool inside) const;
00144 
00145  private:
00146   long size, numRects;
00147   struct RegionBox
00148   {
00149     int x1, y1, x2, y2;
00150 
00151     // used in Region::getRectangles()
00152     operator Rect() const {return Rect(x1, y1, x2-x1, y2-y1);}
00153 
00154   } *rects, extents;
00155 
00156   typedef void (Region::*overlapFunc) (
00157                    RegionBox *r1,
00158                    RegionBox *r1End,
00159                    RegionBox *r2,
00160                    RegionBox *r2End,
00161                    int          y1,
00162                    int          y2);
00163   typedef void (Region::*nonOverlapFunc) (
00164                   RegionBox *r,
00165                   RegionBox *rEnd,
00166                   int          y1,
00167                   int          y2);
00168   /*
00169    * used to allocate buffers for points and link
00170    * the buffers together
00171    */
00172   struct POINTBLOCK {
00173     /*
00174      * number of points to buffer before sending them off
00175      * to scanlines() :  Must be an even number
00176      */
00177     static const int NUMPTSTOBUFFER = 200;
00178     wftk::Point pts[NUMPTSTOBUFFER];
00179     POINTBLOCK *next;
00180   };
00181 
00182   void miSetExtents();
00183   void Compress(
00184      Region *s,
00185      Region *t,
00186      unsigned int      dx,
00187      int        xdir,
00188      int        grow);
00189   void miIntersectO (
00190           RegionBox *r1,
00191           RegionBox *r1End,
00192           RegionBox *r2,
00193           RegionBox *r2End,
00194           int          y1,
00195           int          y2);
00196   int
00197   miCoalesce (
00198         int       prevStart,
00199         int       curStart);
00200   void
00201   miRegionOp(
00202        const Region *reg1,
00203        const Region *reg2,
00204        overlapFunc    overlapFn,
00205        nonOverlapFunc nonOverlap1Fn,
00206        nonOverlapFunc nonOverlap2Fn);
00207   void
00208   miUnionNonO (
00209          RegionBox *r,
00210          RegionBox *rEnd,
00211          int          y1,
00212          int          y2);
00213   void
00214   miUnionO (
00215       RegionBox *r1,
00216       RegionBox *r1End,
00217       RegionBox *r2,
00218       RegionBox *r2End,
00219       int          y1,
00220       int          y2);
00221   void
00222   miSubtractNonO1 (
00223          RegionBox *r,
00224          RegionBox *rEnd,
00225          int          y1,
00226          int          y2);
00227   void
00228   miSubtractO(
00229          RegionBox *r1,
00230          RegionBox *r1End,
00231          RegionBox *r2,
00232          RegionBox *r2End,
00233          int          y1,
00234          int          y2);
00235   int PtsToRegion(
00236     int  numFullPtBlocks, int iCurPtBlock,
00237     POINTBLOCK *FirstPtBlock);
00238 };
00239 
00240 } // namespace
00241 
00242 #endif // _WFTK_REGION_H_
00243 

Generated Thu Mar 18 20:06:04 2004.
Copyright © 1998-2003 by the respective authors.

This document is licensed under the terms of the GNU Free Documentation License and may be freely distributed under the conditions given by this license.