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