[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> |
---|
| 30 | |
---|
[252] | 31 | namespace PixelInfo |
---|
[238] | 32 | { |
---|
| 33 | |
---|
[365] | 34 | Scan::Scan() |
---|
| 35 | { |
---|
| 36 | this->itsY=-1; |
---|
| 37 | this->itsX=-1; |
---|
| 38 | this->itsXLen=0; |
---|
| 39 | } |
---|
| 40 | |
---|
[270] | 41 | Scan::Scan(const Scan& s) |
---|
| 42 | { |
---|
[365] | 43 | operator=(s); |
---|
[270] | 44 | } |
---|
| 45 | //------------------------------------------------------ |
---|
| 46 | |
---|
| 47 | Scan& Scan::operator= (const Scan& s) |
---|
| 48 | { |
---|
| 49 | if(this == &s) return *this; |
---|
| 50 | this->itsY=s.itsY; |
---|
| 51 | this->itsX=s.itsX; |
---|
| 52 | this->itsXLen=s.itsXLen; |
---|
| 53 | return *this; |
---|
| 54 | } |
---|
| 55 | //------------------------------------------------------ |
---|
| 56 | |
---|
[252] | 57 | Scan nullScan() |
---|
| 58 | { |
---|
| 59 | /** |
---|
| 60 | * A simple way of returning a scan with zero length. |
---|
| 61 | */ |
---|
| 62 | Scan null(-1,-1,0); |
---|
| 63 | return null; |
---|
[243] | 64 | } |
---|
[252] | 65 | //------------------------------------------------------ |
---|
| 66 | |
---|
| 67 | Scan unite(Scan &scan1, Scan &scan2) |
---|
| 68 | { |
---|
| 69 | /** |
---|
| 70 | * Return a scan that includes all pixels from both scans, but |
---|
| 71 | * only if they overlap. If they do not, return the null scan. |
---|
| 72 | * |
---|
| 73 | */ |
---|
| 74 | |
---|
| 75 | // TEST FOR FAILURES: |
---|
| 76 | bool fail = false; |
---|
| 77 | fail = fail || (scan1.getY()!=scan2.getY()); |
---|
| 78 | fail = fail || ( (scan1.getX() < scan2.getX()) && |
---|
| 79 | (scan2.getX() > scan1.getXmax()+1) ); |
---|
| 80 | fail = fail || ( (scan2.getX() < scan1.getX()) && |
---|
| 81 | (scan1.getX() > scan2.getXmax()+1) ); |
---|
| 82 | Scan joined; |
---|
| 83 | if(fail){ |
---|
| 84 | // std::cerr << "Joining scans failed! (" |
---|
| 85 | // << scan1 <<"),("<<scan2<<") don't overlap\n"; |
---|
| 86 | joined = nullScan(); |
---|
| 87 | } |
---|
| 88 | else{ |
---|
| 89 | long y = scan1.getY(); |
---|
| 90 | long x = scan1.getX(); |
---|
| 91 | if(scan2.getX()<x) x=scan2.getX(); |
---|
| 92 | long xmax=scan1.getXmax(); |
---|
| 93 | if(scan2.getXmax()>xmax) xmax=scan2.getXmax(); |
---|
| 94 | joined.define(y,x,xmax-x+1); |
---|
| 95 | } |
---|
| 96 | return joined; |
---|
[238] | 97 | } |
---|
[244] | 98 | |
---|
[252] | 99 | //------------------------------------------------------ |
---|
[238] | 100 | |
---|
[252] | 101 | Scan intersect(Scan &scan1, Scan &scan2) |
---|
| 102 | { |
---|
| 103 | /** |
---|
| 104 | * Return a scan that includes all pixels that lie in both scans. |
---|
| 105 | * |
---|
| 106 | * If they do not overlap, return the null scan. |
---|
| 107 | * |
---|
| 108 | */ |
---|
[246] | 109 | |
---|
[252] | 110 | bool fail = false; |
---|
| 111 | fail = fail || (scan1.getY()!=scan2.getY()); |
---|
| 112 | fail = fail || ( (scan1.getX() < scan2.getX()) && |
---|
| 113 | (scan2.getX() > scan1.getXmax()+1) ); |
---|
| 114 | fail = fail || ( (scan2.getX() < scan1.getX()) && |
---|
| 115 | (scan1.getX() > scan2.getXmax()+1) ); |
---|
| 116 | |
---|
| 117 | Scan intersection; |
---|
| 118 | if(fail){ |
---|
| 119 | // std::cerr << "Intersecting scans failed! (" |
---|
| 120 | // << scan1 <<"),("<<scan2<<") don't overlap.\n"; |
---|
| 121 | intersection = nullScan(); |
---|
| 122 | } |
---|
| 123 | else{ |
---|
| 124 | long y = scan1.getY(); |
---|
| 125 | long x = scan1.getX(); |
---|
| 126 | if(scan2.getX()>x) x=scan2.getX(); |
---|
| 127 | long xmax=scan1.getXmax(); |
---|
| 128 | if(scan2.getXmax()<xmax) xmax=scan2.getXmax(); |
---|
| 129 | intersection.define(y,x,xmax-x+1); |
---|
| 130 | } |
---|
| 131 | return intersection; |
---|
[238] | 132 | } |
---|
[252] | 133 | //------------------------------------------------------ |
---|
[238] | 134 | |
---|
[252] | 135 | bool touching(Scan &scan1, Scan &scan2) |
---|
| 136 | { |
---|
| 137 | /** |
---|
| 138 | * Test whether two scans either overlap, or lie adjacent |
---|
| 139 | * (ie. there are no pixels lying between the two scans). |
---|
| 140 | * \return A bool value. |
---|
| 141 | */ |
---|
| 142 | if(scan1.getY()!=scan2.getY()) return false; |
---|
| 143 | else if(scan1.getX() <= scan2.getX()) |
---|
| 144 | return (scan2.getX() <= scan1.getXmax()+1); |
---|
| 145 | else |
---|
| 146 | return (scan1.getX() <= scan2.getXmax()+1); |
---|
[238] | 147 | |
---|
[252] | 148 | } |
---|
| 149 | //------------------------------------------------------ |
---|
[238] | 150 | |
---|
[252] | 151 | bool overlap(Scan &scan1, Scan &scan2) |
---|
| 152 | { |
---|
| 153 | /** |
---|
| 154 | * Test whether two scans overlap, ie. they have pixels in |
---|
| 155 | * common. |
---|
| 156 | * \return A bool value. |
---|
| 157 | */ |
---|
| 158 | if(scan1.getY()!=scan2.getY()) return false; |
---|
| 159 | else if(scan1.getX() <= scan2.getX()) |
---|
| 160 | return (scan2.getX() <= scan1.getXmax()); |
---|
| 161 | else |
---|
| 162 | return (scan1.getX() <= scan2.getXmax()); |
---|
[238] | 163 | |
---|
[252] | 164 | } |
---|
| 165 | //------------------------------------------------------ |
---|
[238] | 166 | |
---|
[252] | 167 | bool adjacent(Scan &scan1, Scan &scan2) |
---|
| 168 | { |
---|
| 169 | /** |
---|
| 170 | * Test whether two scans lie adjacent (ie. there are no pixels |
---|
| 171 | * lying between the two scans). If they overlap, return false. |
---|
| 172 | * \return A bool value. |
---|
| 173 | */ |
---|
| 174 | if(scan1.getY()!=scan2.getY()) return false; |
---|
| 175 | else if(scan1.getX() <= scan2.getX()) |
---|
| 176 | return (scan2.getX() == scan1.getXmax()+1); |
---|
| 177 | else |
---|
| 178 | return (scan1.getX() == scan2.getXmax()+1); |
---|
[238] | 179 | |
---|
[252] | 180 | } |
---|
| 181 | //------------------------------------------------------ |
---|
[238] | 182 | |
---|
[252] | 183 | std::ostream& operator<< ( std::ostream& theStream, Scan& scan) |
---|
| 184 | { |
---|
| 185 | /** |
---|
| 186 | * Output the three key parameters of the scan. |
---|
| 187 | */ |
---|
[418] | 188 | // theStream << scan.itsY; |
---|
| 189 | // theStream << " " << scan.itsX; |
---|
| 190 | // theStream << " " << scan.itsXLen; |
---|
| 191 | theStream << scan.itsX; |
---|
| 192 | theStream << "-" << scan.getXmax(); |
---|
| 193 | theStream << ", " << scan.itsY; |
---|
[270] | 194 | return theStream; |
---|
[252] | 195 | } |
---|
| 196 | //------------------------------------------------------ |
---|
[238] | 197 | |
---|
[252] | 198 | bool operator< (Scan lhs, Scan rhs) |
---|
| 199 | { |
---|
| 200 | /** |
---|
| 201 | * Test for less-than first on the y-values, and if they are |
---|
| 202 | * equal, test on the starting x-value, and then finally on the |
---|
| 203 | * length. |
---|
| 204 | * |
---|
| 205 | * This is necessary for sorting functions on lists of Scans (used |
---|
| 206 | * by the Object2D class). |
---|
| 207 | */ |
---|
| 208 | if(lhs.itsY != rhs.itsY) return (lhs.itsY < rhs.itsY); |
---|
| 209 | else if(lhs.itsX != rhs.itsX) return (lhs.itsX < rhs.itsX); |
---|
| 210 | else return (lhs.itsXLen < rhs.itsXLen); |
---|
| 211 | } |
---|
| 212 | //------------------------------------------------------ |
---|
[238] | 213 | |
---|
[252] | 214 | bool operator== (Scan lhs, Scan rhs) |
---|
| 215 | { |
---|
| 216 | /** |
---|
| 217 | * For two scans to be equal, all three parameters must be equal. |
---|
| 218 | */ |
---|
| 219 | return (lhs.itsY == rhs.itsY) && |
---|
| 220 | (lhs.itsX == rhs.itsX) && |
---|
| 221 | (lhs.itsXLen == rhs.itsXLen); |
---|
| 222 | } |
---|
| 223 | //------------------------------------------------------ |
---|
[238] | 224 | |
---|
[252] | 225 | bool Scan::isInScan(long x, long y) |
---|
| 226 | { |
---|
| 227 | return (y == this->itsY) && |
---|
| 228 | ( (x>= this->itsX) && (x < (this->itsXLen+this->itsX)) ); |
---|
| 229 | } |
---|
[253] | 230 | //------------------------------------------------------ |
---|
[252] | 231 | |
---|
[253] | 232 | void mergeList(std::vector<Scan> scanlist) |
---|
| 233 | { |
---|
| 234 | std::vector<Scan>::iterator iter; |
---|
| 235 | int counter=0,compCounter; |
---|
| 236 | while(counter<(scanlist.size()-1)){ |
---|
[252] | 237 | |
---|
[253] | 238 | |
---|
| 239 | compCounter = counter+1; |
---|
| 240 | |
---|
| 241 | do{ |
---|
| 242 | |
---|
| 243 | if(touching(scanlist[counter],scanlist[compCounter])){ |
---|
| 244 | Scan temp = unite(scanlist[counter],scanlist[compCounter]); |
---|
| 245 | iter = scanlist.begin()+compCounter; |
---|
| 246 | scanlist.erase(iter); |
---|
| 247 | iter = scanlist.begin()+counter; |
---|
| 248 | scanlist.erase(iter); |
---|
| 249 | scanlist.push_back(temp); |
---|
| 250 | } |
---|
| 251 | else compCounter ++; |
---|
| 252 | }while(compCounter < scanlist.size()); |
---|
| 253 | |
---|
| 254 | counter++; |
---|
| 255 | } |
---|
| 256 | |
---|
| 257 | } |
---|
| 258 | |
---|
| 259 | |
---|
[238] | 260 | } |
---|