source: tags/release-1.1/src/PixelMap/Scan.cc @ 1391

Last change on this file since 1391 was 301, checked in by Matthew Whiting, 17 years ago

Mostly adding the distribution text to the start of files, with a few additional comments added too.

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