source: trunk/src/PixelMap/Scan.cc @ 1455

Last change on this file since 1455 was 1443, checked in by MatthewWhiting, 7 years ago

Fixing some build errors to make 1.6.2 more widely useful.

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