[301] | 1 | // ----------------------------------------------------------------------- |
---|
| 2 | // Object2D.hh: Definition of Object2D, a class to hold pixel |
---|
| 3 | // information for a 2-dimensional object. |
---|
| 4 | // ----------------------------------------------------------------------- |
---|
| 5 | // Copyright (C) 2006, Matthew Whiting, ATNF |
---|
| 6 | // |
---|
| 7 | // This program is free software; you can redistribute it and/or modify it |
---|
| 8 | // under the terms of the GNU General Public License as published by the |
---|
| 9 | // Free Software Foundation; either version 2 of the License, or (at your |
---|
| 10 | // option) any later version. |
---|
| 11 | // |
---|
| 12 | // Duchamp is distributed in the hope that it will be useful, but WITHOUT |
---|
| 13 | // ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
---|
| 14 | // FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
---|
| 15 | // for more details. |
---|
| 16 | // |
---|
| 17 | // You should have received a copy of the GNU General Public License |
---|
| 18 | // along with Duchamp; if not, write to the Free Software Foundation, |
---|
| 19 | // Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA |
---|
| 20 | // |
---|
| 21 | // Correspondence concerning Duchamp may be directed to: |
---|
| 22 | // Internet email: Matthew.Whiting [at] atnf.csiro.au |
---|
| 23 | // Postal address: Dr. Matthew Whiting |
---|
| 24 | // Australia Telescope National Facility, CSIRO |
---|
| 25 | // PO Box 76 |
---|
| 26 | // Epping NSW 1710 |
---|
| 27 | // AUSTRALIA |
---|
| 28 | // ----------------------------------------------------------------------- |
---|
[238] | 29 | #ifndef OBJECT2D_H |
---|
| 30 | #define OBJECT2D_H |
---|
| 31 | |
---|
| 32 | #include <iostream> |
---|
| 33 | #include <algorithm> |
---|
| 34 | #include <vector> |
---|
[393] | 35 | #include <duchamp/PixelMap/Scan.hh> |
---|
| 36 | #include <duchamp/PixelMap/Voxel.hh> |
---|
[238] | 37 | |
---|
[252] | 38 | namespace PixelInfo |
---|
[238] | 39 | { |
---|
[252] | 40 | |
---|
| 41 | /** |
---|
| 42 | * A class to store the pixel information for an object defined in a |
---|
| 43 | * 2-dimensional plane. |
---|
| 44 | * |
---|
| 45 | * The object consists of a set of Scans (stored as a std::vector), |
---|
| 46 | * together with basic information on the maximum, minimum, and |
---|
[265] | 47 | * average values (actually stored as xSum, ySum) for the x and y |
---|
[252] | 48 | * pixel values. |
---|
| 49 | */ |
---|
| 50 | |
---|
| 51 | class Object2D |
---|
| 52 | { |
---|
| 53 | public: |
---|
[365] | 54 | Object2D(); |
---|
[252] | 55 | Object2D(const Object2D& o); |
---|
| 56 | Object2D& operator= (const Object2D& o); |
---|
| 57 | virtual ~Object2D(){}; |
---|
[238] | 58 | |
---|
[252] | 59 | /** Clear the Object and set the number of pixels to zero.*/ |
---|
| 60 | void clear(){scanlist.clear(); numPix=0;}; |
---|
[238] | 61 | |
---|
[252] | 62 | /** Make sure there are no overlapping or adjacent scans in the |
---|
| 63 | Scan list. */ |
---|
| 64 | void cleanup(); |
---|
[240] | 65 | |
---|
[252] | 66 | /** Add a pixel to the Object, making sure no scans overlap |
---|
| 67 | afterwards. */ |
---|
| 68 | void addPixel(long x, long y); |
---|
| 69 | /** Add the (x,y) part of a Voxel to the Object, using |
---|
| 70 | addPixel(long,long)*/ |
---|
| 71 | void addPixel(Voxel v){this->addPixel(v.getX(),v.getY());}; |
---|
| 72 | /** Add a Pixel to the Object, using addPixel(long,long)*/ |
---|
| 73 | void addPixel(Pixel p){this->addPixel(p.getX(),p.getY());}; |
---|
[238] | 74 | |
---|
[252] | 75 | /** Add a full Scan to the Object, making sure there are no |
---|
| 76 | overlapping scans afterwards. */ |
---|
| 77 | void addScan(Scan scan); |
---|
| 78 | |
---|
| 79 | /** Test whether a pixel (x,y) is in the Object. */ |
---|
| 80 | bool isInObject(long x, long y); |
---|
| 81 | /** Test whether the (x,y) part of a Voxel is in the Object.*/ |
---|
| 82 | bool isInObject(Voxel v){return this->isInObject(v.getX(),v.getY());}; |
---|
| 83 | /** Test whether a Pixel is in the Object.*/ |
---|
| 84 | bool isInObject(Pixel p){return this->isInObject(p.getX(),p.getY());}; |
---|
| 85 | |
---|
| 86 | /** Test whether a given Scan overlaps with any pixels in the Object. */ |
---|
| 87 | bool scanOverlaps(Scan scan); |
---|
| 88 | |
---|
| 89 | /** Return the number of pixels in the Object. */ |
---|
| 90 | long getSize(){return numPix;}; |
---|
| 91 | |
---|
| 92 | /** Return the number of Scans in the Object. */ |
---|
| 93 | long getNumScan(){return scanlist.size();}; |
---|
| 94 | |
---|
| 95 | /** Get the i-th Scan in the Scan list. */ |
---|
| 96 | Scan getScan(int i){return scanlist[i];}; |
---|
| 97 | |
---|
| 98 | /** Order the Scans in the list, using the < operator for Scans. */ |
---|
| 99 | void order(){std::stable_sort(scanlist.begin(),scanlist.end());}; |
---|
| 100 | |
---|
| 101 | /** Add values to the x- and y-axes, making sure to add the |
---|
| 102 | offsets to the sums and min/max values. */ |
---|
| 103 | void addOffsets(long xoff, long yoff){ |
---|
| 104 | for(int i=0;i<scanlist.size();i++) scanlist[i].addOffsets(xoff,yoff); |
---|
| 105 | xSum += xoff*numPix; |
---|
| 106 | xmin += xoff; xmax += xoff; |
---|
| 107 | ySum += yoff*numPix; |
---|
| 108 | ymin += yoff; ymax += yoff; |
---|
| 109 | }; |
---|
| 110 | |
---|
| 111 | /** Calculate the sums, mins, maxs for x&y -- should not be |
---|
| 112 | necessary as these are done when pixels & Scans are added. */ |
---|
| 113 | void calcParams(); |
---|
| 114 | |
---|
| 115 | /** Return the average x-value. */ |
---|
| 116 | float getXcentre(){return xSum/float(numPix);}; |
---|
| 117 | |
---|
| 118 | /** Return the average y-value. */ |
---|
| 119 | float getYcentre(){return ySum/float(numPix);}; |
---|
| 120 | |
---|
| 121 | long getXmin(){return xmin;}; |
---|
| 122 | long getYmin(){return ymin;}; |
---|
| 123 | long getXmax(){return xmax;}; |
---|
| 124 | long getYmax(){return ymax;}; |
---|
| 125 | |
---|
| 126 | /** Return the number of distinct y-values.*/ |
---|
| 127 | long getNumDistinctY(); |
---|
| 128 | /** Return the number of distinct x-values.*/ |
---|
| 129 | long getNumDistinctX(); |
---|
[238] | 130 | |
---|
[252] | 131 | /** A stream output operator. */ |
---|
| 132 | friend std::ostream& operator<< ( std::ostream& theStream, Object2D& obj); |
---|
[238] | 133 | |
---|
[252] | 134 | /** Adding two Objects together. */ |
---|
| 135 | friend Object2D operator+ (Object2D lhs, Object2D rhs){ |
---|
| 136 | Object2D output = lhs; |
---|
| 137 | for(int s=0;s<rhs.scanlist.size();s++) output.addScan(rhs.scanlist[s]); |
---|
| 138 | return output; |
---|
| 139 | } |
---|
[240] | 140 | |
---|
[252] | 141 | friend class Object3D; |
---|
| 142 | friend class ChanMap; |
---|
[246] | 143 | |
---|
[252] | 144 | private: |
---|
| 145 | std::vector<Scan> scanlist; ///< The list of Scans |
---|
| 146 | long numPix; ///< Number of pixels in the Object |
---|
| 147 | float xSum; ///< Sum of x values |
---|
| 148 | float ySum; ///< Sum of y values |
---|
| 149 | long xmin,xmax; ///< min and max x-values of object |
---|
| 150 | long ymin,ymax; ///< min and max y-values of object |
---|
| 151 | }; |
---|
[238] | 152 | |
---|
[252] | 153 | } |
---|
| 154 | |
---|
[238] | 155 | #endif //OBJECT2D_H |
---|