[238] | 1 | #ifndef SCAN_H |
---|
| 2 | #define SCAN_H |
---|
| 3 | |
---|
| 4 | #include <iostream> |
---|
[253] | 5 | #include <vector> |
---|
[238] | 6 | |
---|
[252] | 7 | /** |
---|
| 8 | * This namespace will store all the classes and functions necessary |
---|
| 9 | * to encode shapes and objects in 1-, 2- and 3-dimensions. |
---|
| 10 | */ |
---|
| 11 | namespace PixelInfo |
---|
[238] | 12 | { |
---|
| 13 | |
---|
[252] | 14 | /** |
---|
| 15 | * A class to store the basic unit of pixel information, a scan |
---|
| 16 | * encoded by an (x,y) starting point, and a length (in the |
---|
| 17 | * x-direction). |
---|
| 18 | * |
---|
| 19 | * This class is used by other classes to store objects in 2- and |
---|
| 20 | * 3-dimensions. |
---|
| 21 | */ |
---|
[240] | 22 | |
---|
[252] | 23 | class Scan |
---|
| 24 | { |
---|
| 25 | public: |
---|
| 26 | Scan(){itsY=-1;itsX=-1;itsXLen=0;}; |
---|
| 27 | Scan(long y, long x, long xl){itsY=y; itsX=x; itsXLen=xl;}; |
---|
| 28 | Scan(const Scan& s){itsY=s.itsY; itsX=s.itsX; itsXLen=s.itsXLen;}; |
---|
| 29 | Scan& operator= (const Scan& s){itsY=s.itsY; itsX=s.itsX; itsXLen=s.itsXLen;}; |
---|
| 30 | virtual ~Scan(){}; |
---|
[238] | 31 | |
---|
[252] | 32 | /** Define a Scan by providing the three key parameters. */ |
---|
| 33 | void define(long y, long x, long xl){itsY=y; itsX=x; itsXLen=xl;}; |
---|
[238] | 34 | |
---|
[252] | 35 | /** Set the Scan to the null values, with the length=0. */ |
---|
| 36 | void clear(){itsY=-1;itsX=-1;itsXLen=0;}; |
---|
[238] | 37 | |
---|
[252] | 38 | // Accessor functions -- obvious. |
---|
| 39 | long getY(){return itsY;}; |
---|
| 40 | void setY(long l){itsY=l;}; |
---|
| 41 | long getX(){return itsX;}; |
---|
| 42 | void setX(long l){itsX=l;}; |
---|
| 43 | long getXlen(){return itsXLen;}; |
---|
| 44 | void setXlen(long l){itsXLen=l;}; |
---|
[238] | 45 | |
---|
[252] | 46 | /** An easy way to get the maximum x-value */ |
---|
| 47 | long getXmax(){return itsX+itsXLen-1;}; |
---|
[238] | 48 | |
---|
[252] | 49 | /** A way of setting the length by proxy, giving the maximum x-value. */ |
---|
| 50 | void setXmax(long l){itsXLen = l-itsX+1;}; |
---|
| 51 | |
---|
| 52 | /** Add a point to the left of the scan (ie.\ add the point itsX-1).*/ |
---|
| 53 | void growLeft(){itsX--;itsXLen++;}; |
---|
| 54 | |
---|
| 55 | /** Add a point to the right of the scan (ie.\ add the point xmax+1).*/ |
---|
| 56 | void growRight(){itsXLen++;}; |
---|
| 57 | |
---|
| 58 | /** Add values to the x- and y-axes. */ |
---|
| 59 | void addOffsets(long xoff, long yoff){itsY+=yoff; itsX+=xoff;}; |
---|
| 60 | |
---|
| 61 | /** Tests whether a given (x,y) point is in the scan.*/ |
---|
| 62 | bool isInScan(long x, long y); |
---|
| 63 | |
---|
| 64 | /** Stream output operator for the Scan */ |
---|
| 65 | friend std::ostream& operator<< ( std::ostream& theStream, Scan& scan); |
---|
| 66 | |
---|
| 67 | /** Less-than operator for Scans */ |
---|
| 68 | friend bool operator< (Scan lhs, Scan rhs); |
---|
| 69 | |
---|
| 70 | /** Test whether one scan is equal to another. */ |
---|
| 71 | friend bool operator== (Scan lhs, Scan rhs); |
---|
| 72 | |
---|
| 73 | friend class Object2D; ///< Enable Object2D to see the private members. |
---|
| 74 | |
---|
| 75 | private: |
---|
| 76 | long itsY; ///< The y-value of each point in the scan. |
---|
| 77 | long itsX; ///< The x-value of the start (left-hand end) of the scan. |
---|
| 78 | long itsXLen; ///< The length of the scan (number of pixels in the scan). |
---|
| 79 | |
---|
| 80 | }; |
---|
| 81 | |
---|
| 82 | /** Combine two scans into one. */ |
---|
| 83 | Scan unite(Scan &s1, Scan &s2); |
---|
| 84 | |
---|
| 85 | /** Keep only the pixels in both the two scans. */ |
---|
| 86 | Scan intersect(Scan &s1, Scan &s2); |
---|
| 87 | |
---|
| 88 | /** Test whether two scans either overlap or are adjacent. */ |
---|
| 89 | bool touching(Scan &s1, Scan &s2); |
---|
| 90 | |
---|
| 91 | /** Test whether two scans have pixels in common */ |
---|
| 92 | bool overlap(Scan &s1, Scan &s2); |
---|
| 93 | |
---|
| 94 | /** Test whether two scans lie adjacent to each other (but not |
---|
| 95 | overlapping).*/ |
---|
| 96 | bool adjacent(Scan &scan1, Scan &scan2); |
---|
| 97 | |
---|
| 98 | /** Return the null scan, y=-1, x=-1, xlen=0.*/ |
---|
| 99 | Scan nullScan(); |
---|
| 100 | |
---|
[253] | 101 | /** Examine a vector list of Scans and merge any that are touching. */ |
---|
| 102 | void mergeList(std::vector<Scan> scanlist); |
---|
| 103 | |
---|
[252] | 104 | } |
---|
| 105 | |
---|
[238] | 106 | #endif //SCAN_H |
---|