home *** CD-ROM | disk | FTP | other *** search
/ The C Users' Group Library 1994 August / wc-cdrom-cusersgrouplibrary-1994-08.iso / listings / v_10_08 / qc.h < prev    next >
C/C++ Source or Header  |  1992-07-25  |  6KB  |  186 lines

  1. ///////////////////////////////////////////////////////
  2. //  Listing 1 - QC.H: Header file for quadcodes
  3. //  Written by:
  4. //    Kenneth Van Camp
  5. //    RR #1 Box 1255
  6. //    East Stroudsburg, PA  18301
  7. //    (717)223-8620
  8. ///////////////////////////////////////////////////////
  9.  
  10. #ifndef QC_H
  11. #define QC_H
  12.  
  13. #ifndef TRUE
  14. # define TRUE 1
  15. #endif
  16. #ifndef FALSE
  17. # define FALSE 0
  18. #endif
  19. #ifndef max
  20. #define max(a,b)        (((a) > (b)) ? (a) : (b))
  21. #define min(a,b)        (((a) < (b)) ? (a) : (b))
  22. #endif
  23.  
  24. typedef unsigned char BYTE;
  25. typedef long          COORD;
  26.  
  27. // The following should be defined on computers that 
  28. // store their most-significant byte first in integers:
  29. // #define MSB_FIRST
  30.  
  31. // Store (I,J) coordinates in long integers, so this is
  32. // the only limitation on the accuracy of storage:
  33. #define NBITS_COORD  sizeof(COORD) * 8
  34.  
  35. // Define the following to use static-length QuadCodes.
  36. // If not defined, dynamic allocation is used:
  37. #define STAT_QC
  38.  
  39. #ifdef STAT_QC
  40. # define NBYTE_QC   8    // # bytes to store a quadcode
  41. # define MAXQUITS   (min (NBITS_COORD, 4*NBYTE_QC))
  42. #else
  43. # define MAXQUITS   NBITS_COORD
  44. #endif
  45.  
  46. class Region;
  47. class RegionDisplay;
  48.  
  49. // class QuadCode: Store a single quadcode
  50. // (2 bits per quit).
  51. class QuadCode
  52. {
  53.   protected:
  54. #ifdef STAT_QC
  55.     BYTE qca[NBYTE_QC];   // storage area for quadcode
  56.     void FreeMem (void)   // free dynamic memory
  57.         { nquits=0; }
  58. #else
  59.     BYTE *qca;            // storage area for quadcode
  60.     void FreeMem (void);  // free dynamic memory
  61. #endif
  62.     void Init             // initializer from string
  63.         (const char *chqc);
  64.  
  65.   public:
  66.     int nquits;           // # of quits in qc
  67.     QuadCode (void)       // default constructor
  68.         { nquits = 0; }
  69.     QuadCode              // constructor from (I,J)
  70.         (COORD i, COORD j, int nq);
  71.     QuadCode              // constructor from string
  72.         (const char *chqc)
  73.         { Init (chqc); }
  74. #ifndef STAT_QC
  75.     ~QuadCode (void)      // destructor
  76.         { FreeMem(); }
  77. #endif
  78.     int GetQuit           // extract single quit
  79.         (int quit);
  80.     void SetQuit          // set single quit
  81.         (int quit, int val);
  82.     void ToIJ             // convert to (I,J)
  83.         (COORD &i, COORD &j, int &nq);
  84.     int Compare           // compare two quadcodes
  85.         (QuadCode &qc);
  86.     int Sibling           // is qc a sibling?
  87.         (QuadCode *qc);
  88.     int Contains          // does one qc contain other?
  89.         (QuadCode &qc);
  90.     void MakeParent       // make qc into its parent
  91.         (void);
  92.  
  93.     QuadCode &operator= (QuadCode &qc);
  94.     int operator< (QuadCode &qc)
  95.       { return (Compare (qc) < 0); }
  96.     int operator> (QuadCode &qc)
  97.       { return (Compare (qc) > 0); }
  98.     int operator<= (QuadCode &qc)
  99.       { return (Compare (qc) <= 0); }
  100.     int operator>= (QuadCode &qc)
  101.       { return (Compare (qc) >= 0); }
  102.     int operator== (QuadCode &qc)
  103.       { return (Compare (qc) == 0); }
  104.     int operator!= (QuadCode &qc)
  105.       { return (Compare (qc) != 0); }
  106.  
  107.     friend ostream &operator<<
  108.       (ostream &stream, QuadCode &qc);
  109.     friend istream &operator>>
  110.       (istream &stream, QuadCode &qc);
  111. }; // class QuadCode
  112.  
  113. // class QCNode: A node in a linked list of quadcodes.
  114. class QCNode: public QuadCode
  115. {
  116.   private:
  117.     QCNode  *next;        // next node in linked list
  118.     // Friend classes for access to 'next'.
  119.     friend Region;
  120.     friend RegionDisplay;
  121.     // Same for its "put to" operator.
  122.     friend ostream &operator<<
  123.       (ostream &stream, Region ®);
  124.  
  125.   public:
  126.     QCNode (void)         // default constructor
  127.         { next = NULL; }
  128.     QCNode                // constructor from (I,J)
  129.         (COORD i, COORD j, int nq):
  130.         QuadCode (i, j, nq)  // calls qc constr first
  131.         { next = NULL; }
  132. }; // class QCNode
  133.  
  134. // struct Point: One point on outline of region.
  135. struct Point
  136. {
  137.   COORD i, j;
  138. };
  139.  
  140. // struct PointListHeader: An array of points.
  141. struct PointListHeader
  142. {
  143.   int length;             // # of points in array
  144.   COORD ndiv;             // # (I,J) divisions in grid
  145.   struct Point *pointptr; // array of points
  146. };
  147.  
  148. // class Region: A linked list of QuadCodes
  149. class Region
  150. {
  151.   protected:
  152.     QCNode *first_qcnode; // start of linked list
  153.     COORD ndiv,           // # (I,J) divisions in grid
  154.           min_i,          // lowest I coord in region
  155.           max_i,          // highest I coord in region
  156.           current_i;      // current row being built
  157.     int   aborted;        // was the build aborted?
  158.     void ScanOutAET       // create qc's along one row
  159.         (COORD i_to_scan, int &nqc_compress, int nquits);
  160.     void AddRow           // add row of qc's
  161.         (COORD i, COORD j1, COORD j2, int nquits);
  162.     void AddQC            // add a single qc
  163.         (COORD i, COORD j, int nquits);
  164.     int MaxQuits (void);  // find max #quits in a qc
  165.     int PctComplete (void); // percent of region built
  166.  
  167.   public:
  168.     Region (void)         // default constructor
  169.       { first_qcnode = NULL; ndiv = 0; }
  170.     Region                // constructor from outline
  171.         (PointListHeader &vertext_list);
  172.     ~Region (void);       // destructor
  173.     int Compress (void);  // region compressor
  174.     int InRegion          // is qc in region?
  175.         (QuadCode &qc);
  176.     int NumQC (void);     // count quadcodes in region
  177.     friend ostream &operator<<
  178.       (ostream &stream, Region ®);
  179. }; // class Region
  180.  
  181. // The following allows the application to set a "yield"
  182. // function during execution:
  183. void SetRegionYieldFunc (int (*yield_func) (int pct_complete));
  184.  
  185. #endif // QC_H
  186.