source: tags/release-1.1.7/src/PixelMap/Scan.cc

Last change on this file was 536, checked in by MatthewWhiting, 15 years ago

Including the recent minor changes to 1.1.7.

File size: 6.9 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 <duchamp/PixelMap/Scan.hh>
29#include <iostream>
30#include <math.h>
31
32namespace PixelInfo
33{
34
35  Scan::Scan()
36  {
37    this->itsY=-1;
38    this->itsX=-1;
39    this->itsXLen=0;
40  }
41
42  Scan::Scan(const Scan& s)
43  {
44    operator=(s);
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
58  Scan nullScan()
59  {
60    /// A simple way of returning a scan with zero length.
61    Scan null(-1,-1,0);
62    return null;
63  }
64
65  bool Scan::isNull()
66  {
67    return (itsY==-1 && itsX==-1 && itsXLen==0);
68  }
69
70  //------------------------------------------------------
71
72  Scan unite(Scan &scan1, Scan &scan2)
73  {
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.
76
77    Scan joined;
78    if(!touching(scan1,scan2)){
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 = std::min(scan1.getX(),scan2.getX());
86      long xmax = std::max(scan1.getXmax(),scan2.getXmax());
87      joined.define(y,x,xmax-x+1);
88    }
89    return joined;
90  }
91
92  //------------------------------------------------------
93
94  Scan intersect(Scan &scan1, Scan &scan2)
95  {
96    /// Return a scan that includes all pixels that lie in both scans.
97    ///
98    /// If they do not overlap, return the null scan.
99
100    Scan intersection;
101    if(!overlap(scan1,scan2)){
102      //     std::cerr << "Intersecting scans failed! ("
103      //              << scan1 <<"),("<<scan2<<") don't overlap.\n";
104      intersection = nullScan();
105    }
106    else{
107      long y = scan1.getY();
108      long x = std::max(scan1.getX(),scan2.getX());
109      long xmax = std::min(scan1.getXmax(),scan2.getXmax());
110      intersection.define(y,x,xmax-x+1);
111    }
112    return intersection;
113  }
114  //------------------------------------------------------
115
116  bool touching(Scan &scan1, Scan &scan2)
117  {
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
122    return overlap(scan1,scan2) || adjacent(scan1,scan2);
123 
124  }
125  //------------------------------------------------------
126
127  bool overlap(Scan &scan1, Scan &scan2)
128  {
129    ///  Test whether two scans overlap, ie. they have pixels in
130    ///  common.
131    /// \return A bool value.
132
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());
138 
139  }
140  //------------------------------------------------------
141
142  bool adjacent(Scan &scan1, Scan &scan2)
143  {
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
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);
153 
154  }
155  //------------------------------------------------------
156
157  std::ostream& operator<< ( std::ostream& theStream, Scan& scan)
158  {
159    ///  Output the three key parameters of the scan.
160
161    if(scan.isNull()) theStream << "NULL";
162    else{
163      theStream << scan.itsX;
164      theStream << "-" << scan.getXmax();
165      theStream << ", " << scan.itsY;
166    }
167    return theStream;
168  }
169  //------------------------------------------------------
170
171  bool operator< (Scan lhs, Scan rhs)
172  {
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
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  //------------------------------------------------------
185
186  bool operator== (Scan lhs, Scan rhs)
187  {
188    /// For two scans to be equal, all three parameters must be equal.
189
190    return (lhs.itsY == rhs.itsY) &&
191      (lhs.itsX == rhs.itsX) &&
192      (lhs.itsXLen == rhs.itsXLen);
193  }
194  //------------------------------------------------------
195
196  bool Scan::isInScan(long x, long y)
197  {
198    return (y == this->itsY) &&
199      ( (x>= this->itsX) && (x < (this->itsXLen+this->itsX)) );
200  }
201  //------------------------------------------------------
202
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 
214  void mergeList(std::vector<Scan> scanlist)
215  {
216    std::vector<Scan>::iterator iter;
217    uint counter=0,compCounter;
218    while(counter<(scanlist.size()-1)){
219
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
242}
Note: See TracBrowser for help on using the repository browser.