[301] | 1 | // ----------------------------------------------------------------------- |
---|
| 2 | // Scan.cc: Member functions for the Scan class. |
---|
| 3 | // ----------------------------------------------------------------------- |
---|
| 4 | // Copyright (C) 2006, Matthew Whiting, ATNF |
---|
| 5 | // |
---|
| 6 | // This program is free software; you can redistribute it and/or modify it |
---|
| 7 | // under the terms of the GNU General Public License as published by the |
---|
| 8 | // Free Software Foundation; either version 2 of the License, or (at your |
---|
| 9 | // option) any later version. |
---|
| 10 | // |
---|
| 11 | // Duchamp is distributed in the hope that it will be useful, but WITHOUT |
---|
| 12 | // ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
---|
| 13 | // FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
---|
| 14 | // for more details. |
---|
| 15 | // |
---|
| 16 | // You should have received a copy of the GNU General Public License |
---|
| 17 | // along with Duchamp; if not, write to the Free Software Foundation, |
---|
| 18 | // Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA |
---|
| 19 | // |
---|
| 20 | // Correspondence concerning Duchamp may be directed to: |
---|
| 21 | // Internet email: Matthew.Whiting [at] atnf.csiro.au |
---|
| 22 | // Postal address: Dr. Matthew Whiting |
---|
| 23 | // Australia Telescope National Facility, CSIRO |
---|
| 24 | // PO Box 76 |
---|
| 25 | // Epping NSW 1710 |
---|
| 26 | // AUSTRALIA |
---|
| 27 | // ----------------------------------------------------------------------- |
---|
[393] | 28 | #include <duchamp/PixelMap/Scan.hh> |
---|
[238] | 29 | #include <iostream> |
---|
[505] | 30 | #include <math.h> |
---|
[238] | 31 | |
---|
[252] | 32 | namespace PixelInfo |
---|
[238] | 33 | { |
---|
| 34 | |
---|
[365] | 35 | Scan::Scan() |
---|
| 36 | { |
---|
| 37 | this->itsY=-1; |
---|
| 38 | this->itsX=-1; |
---|
| 39 | this->itsXLen=0; |
---|
| 40 | } |
---|
| 41 | |
---|
[770] | 42 | Scan::Scan(long y, long x, long xl): |
---|
| 43 | itsY(y), itsX(x),itsXLen(xl) |
---|
| 44 | { |
---|
| 45 | } |
---|
| 46 | |
---|
[270] | 47 | Scan::Scan(const Scan& s) |
---|
| 48 | { |
---|
[365] | 49 | operator=(s); |
---|
[270] | 50 | } |
---|
| 51 | //------------------------------------------------------ |
---|
| 52 | |
---|
[770] | 53 | Scan& Scan::operator= (const Scan& s) |
---|
| 54 | { |
---|
| 55 | if(this == &s) return *this; |
---|
| 56 | this->itsY=s.itsY; |
---|
| 57 | this->itsX=s.itsX; |
---|
| 58 | this->itsXLen=s.itsXLen; |
---|
| 59 | return *this; |
---|
| 60 | } |
---|
[270] | 61 | //------------------------------------------------------ |
---|
| 62 | |
---|
[773] | 63 | bool Scan::addScan(const Scan &other) |
---|
| 64 | { |
---|
| 65 | bool altered=this->touches(other); |
---|
| 66 | if(altered){ |
---|
| 67 | long x = std::min(this->itsX,other.itsX); |
---|
| 68 | long xmax = std::max(this->itsX+this->itsXLen-1,other.itsX+other.itsXLen-1); |
---|
[776] | 69 | this->itsX=x; |
---|
| 70 | this->itsXLen=xmax-x+1; |
---|
[773] | 71 | } |
---|
| 72 | return altered; |
---|
| 73 | } |
---|
| 74 | |
---|
| 75 | |
---|
[252] | 76 | Scan nullScan() |
---|
| 77 | { |
---|
[528] | 78 | /// A simple way of returning a scan with zero length. |
---|
[252] | 79 | Scan null(-1,-1,0); |
---|
| 80 | return null; |
---|
[243] | 81 | } |
---|
[527] | 82 | |
---|
| 83 | bool Scan::isNull() |
---|
| 84 | { |
---|
| 85 | return (itsY==-1 && itsX==-1 && itsXLen==0); |
---|
| 86 | } |
---|
| 87 | |
---|
[252] | 88 | //------------------------------------------------------ |
---|
| 89 | |
---|
| 90 | Scan unite(Scan &scan1, Scan &scan2) |
---|
| 91 | { |
---|
[528] | 92 | /// Return a scan that includes all pixels from both scans, but |
---|
| 93 | /// only if they overlap. If they do not, return the null scan. |
---|
[252] | 94 | |
---|
| 95 | Scan joined; |
---|
[527] | 96 | if(!touching(scan1,scan2)){ |
---|
[252] | 97 | joined = nullScan(); |
---|
| 98 | } |
---|
| 99 | else{ |
---|
| 100 | long y = scan1.getY(); |
---|
[527] | 101 | long x = std::min(scan1.getX(),scan2.getX()); |
---|
| 102 | long xmax = std::max(scan1.getXmax(),scan2.getXmax()); |
---|
[252] | 103 | joined.define(y,x,xmax-x+1); |
---|
| 104 | } |
---|
| 105 | return joined; |
---|
[238] | 106 | } |
---|
[244] | 107 | |
---|
[252] | 108 | //------------------------------------------------------ |
---|
[238] | 109 | |
---|
[252] | 110 | Scan intersect(Scan &scan1, Scan &scan2) |
---|
| 111 | { |
---|
[528] | 112 | /// Return a scan that includes all pixels that lie in both scans. |
---|
| 113 | /// |
---|
| 114 | /// If they do not overlap, return the null scan. |
---|
[246] | 115 | |
---|
[252] | 116 | Scan intersection; |
---|
[770] | 117 | if(!scan1.overlaps(scan2)){ |
---|
[252] | 118 | intersection = nullScan(); |
---|
| 119 | } |
---|
| 120 | else{ |
---|
| 121 | long y = scan1.getY(); |
---|
[527] | 122 | long x = std::max(scan1.getX(),scan2.getX()); |
---|
| 123 | long xmax = std::min(scan1.getXmax(),scan2.getXmax()); |
---|
[252] | 124 | intersection.define(y,x,xmax-x+1); |
---|
| 125 | } |
---|
| 126 | return intersection; |
---|
[238] | 127 | } |
---|
[252] | 128 | //------------------------------------------------------ |
---|
[238] | 129 | |
---|
[773] | 130 | bool Scan::touches(const Scan &other) |
---|
[770] | 131 | { |
---|
| 132 | return this->overlaps(other) || this->isAdjacentTo(other); |
---|
| 133 | } |
---|
| 134 | |
---|
[773] | 135 | bool Scan::overlaps(const Scan &other) |
---|
[770] | 136 | { |
---|
| 137 | if(this->itsY != other.itsY) return false; |
---|
| 138 | else if(this->itsX <= other.itsX){ |
---|
| 139 | return (other.itsX < (this->itsX+this->itsXLen)); |
---|
| 140 | } |
---|
| 141 | else{ |
---|
| 142 | return (this->itsX < (other.itsX+other.itsXLen)); |
---|
| 143 | } |
---|
| 144 | } |
---|
| 145 | |
---|
[773] | 146 | bool Scan::isAdjacentTo(const Scan &other) |
---|
[770] | 147 | { |
---|
| 148 | if(this->itsY != other.itsY) return false; |
---|
| 149 | else if(this->itsX <= other.itsX){ |
---|
[776] | 150 | return (other.itsX == (this->itsX+this->itsXLen)); |
---|
[770] | 151 | } |
---|
| 152 | else{ |
---|
[776] | 153 | return (this->itsX == (other.itsX+other.itsXLen)); |
---|
[770] | 154 | } |
---|
| 155 | } |
---|
| 156 | //------------------------------------------------------ |
---|
| 157 | |
---|
[252] | 158 | bool touching(Scan &scan1, Scan &scan2) |
---|
| 159 | { |
---|
[528] | 160 | /// Test whether two scans either overlap, or lie adjacent |
---|
| 161 | /// (ie. there are no pixels lying between the two scans). |
---|
| 162 | /// \return A bool value. |
---|
| 163 | |
---|
[527] | 164 | return overlap(scan1,scan2) || adjacent(scan1,scan2); |
---|
[238] | 165 | |
---|
[252] | 166 | } |
---|
| 167 | //------------------------------------------------------ |
---|
[238] | 168 | |
---|
[252] | 169 | bool overlap(Scan &scan1, Scan &scan2) |
---|
| 170 | { |
---|
[528] | 171 | /// Test whether two scans overlap, ie. they have pixels in |
---|
| 172 | /// common. |
---|
| 173 | /// \return A bool value. |
---|
| 174 | |
---|
[252] | 175 | if(scan1.getY()!=scan2.getY()) return false; |
---|
| 176 | else if(scan1.getX() <= scan2.getX()) |
---|
| 177 | return (scan2.getX() <= scan1.getXmax()); |
---|
| 178 | else |
---|
| 179 | return (scan1.getX() <= scan2.getXmax()); |
---|
[238] | 180 | |
---|
[252] | 181 | } |
---|
| 182 | //------------------------------------------------------ |
---|
[238] | 183 | |
---|
[252] | 184 | bool adjacent(Scan &scan1, Scan &scan2) |
---|
| 185 | { |
---|
[528] | 186 | /// Test whether two scans lie adjacent (ie. there are no pixels |
---|
| 187 | /// lying between the two scans). If they overlap, return false. |
---|
| 188 | /// \return A bool value. |
---|
| 189 | |
---|
[252] | 190 | if(scan1.getY()!=scan2.getY()) return false; |
---|
| 191 | else if(scan1.getX() <= scan2.getX()) |
---|
| 192 | return (scan2.getX() == scan1.getXmax()+1); |
---|
| 193 | else |
---|
| 194 | return (scan1.getX() == scan2.getXmax()+1); |
---|
[238] | 195 | |
---|
[252] | 196 | } |
---|
| 197 | //------------------------------------------------------ |
---|
[238] | 198 | |
---|
[252] | 199 | std::ostream& operator<< ( std::ostream& theStream, Scan& scan) |
---|
| 200 | { |
---|
[528] | 201 | /// Output the three key parameters of the scan. |
---|
| 202 | |
---|
[527] | 203 | if(scan.isNull()) theStream << "NULL"; |
---|
| 204 | else{ |
---|
| 205 | theStream << scan.itsX; |
---|
| 206 | theStream << "-" << scan.getXmax(); |
---|
| 207 | theStream << ", " << scan.itsY; |
---|
| 208 | } |
---|
[270] | 209 | return theStream; |
---|
[252] | 210 | } |
---|
| 211 | //------------------------------------------------------ |
---|
[238] | 212 | |
---|
[252] | 213 | bool operator< (Scan lhs, Scan rhs) |
---|
| 214 | { |
---|
[528] | 215 | /// Test for less-than first on the y-values, and if they are |
---|
| 216 | /// equal, test on the starting x-value, and then finally on the |
---|
| 217 | /// length. |
---|
| 218 | /// |
---|
| 219 | /// This is necessary for sorting functions on lists of Scans (used |
---|
| 220 | /// by the Object2D class). |
---|
| 221 | |
---|
[252] | 222 | if(lhs.itsY != rhs.itsY) return (lhs.itsY < rhs.itsY); |
---|
| 223 | else if(lhs.itsX != rhs.itsX) return (lhs.itsX < rhs.itsX); |
---|
| 224 | else return (lhs.itsXLen < rhs.itsXLen); |
---|
| 225 | } |
---|
| 226 | //------------------------------------------------------ |
---|
[238] | 227 | |
---|
[252] | 228 | bool operator== (Scan lhs, Scan rhs) |
---|
| 229 | { |
---|
[528] | 230 | /// For two scans to be equal, all three parameters must be equal. |
---|
| 231 | |
---|
[252] | 232 | return (lhs.itsY == rhs.itsY) && |
---|
| 233 | (lhs.itsX == rhs.itsX) && |
---|
| 234 | (lhs.itsXLen == rhs.itsXLen); |
---|
| 235 | } |
---|
| 236 | //------------------------------------------------------ |
---|
[238] | 237 | |
---|
[252] | 238 | bool Scan::isInScan(long x, long y) |
---|
| 239 | { |
---|
| 240 | return (y == this->itsY) && |
---|
| 241 | ( (x>= this->itsX) && (x < (this->itsXLen+this->itsX)) ); |
---|
| 242 | } |
---|
[253] | 243 | //------------------------------------------------------ |
---|
[252] | 244 | |
---|
[505] | 245 | float minSep(Scan &s1, Scan &s2) |
---|
| 246 | { |
---|
| 247 | |
---|
| 248 | if(s1.getX() > s2.getXmax()) return hypot(s1.getX()-s2.getXmax(),s1.getY()-s2.getY()); |
---|
| 249 | else if(s2.getX() > s1.getXmax()) return hypot(s2.getX()-s1.getXmax(),s1.getY()-s2.getY()); |
---|
| 250 | else return fabsf(s1.getY()-s2.getY()); |
---|
| 251 | |
---|
| 252 | } |
---|
| 253 | //------------------------------------------------------ |
---|
| 254 | |
---|
| 255 | |
---|
[253] | 256 | void mergeList(std::vector<Scan> scanlist) |
---|
| 257 | { |
---|
| 258 | std::vector<Scan>::iterator iter; |
---|
[541] | 259 | unsigned int counter=0,compCounter; |
---|
[253] | 260 | while(counter<(scanlist.size()-1)){ |
---|
[252] | 261 | |
---|
[253] | 262 | |
---|
| 263 | compCounter = counter+1; |
---|
| 264 | |
---|
| 265 | do{ |
---|
| 266 | |
---|
| 267 | if(touching(scanlist[counter],scanlist[compCounter])){ |
---|
| 268 | Scan temp = unite(scanlist[counter],scanlist[compCounter]); |
---|
| 269 | iter = scanlist.begin()+compCounter; |
---|
| 270 | scanlist.erase(iter); |
---|
| 271 | iter = scanlist.begin()+counter; |
---|
| 272 | scanlist.erase(iter); |
---|
| 273 | scanlist.push_back(temp); |
---|
| 274 | } |
---|
| 275 | else compCounter ++; |
---|
| 276 | }while(compCounter < scanlist.size()); |
---|
| 277 | |
---|
| 278 | counter++; |
---|
| 279 | } |
---|
| 280 | |
---|
| 281 | } |
---|
| 282 | |
---|
| 283 | |
---|
[238] | 284 | } |
---|