source: trunk/src/PixelMap/Scan.cc

Last change on this file 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
RevLine 
[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>
[1443]31#include <stdlib.h>
[238]32
[252]33namespace PixelInfo
[238]34{
35
[365]36  Scan::Scan()
37  {
38    this->itsY=-1;
39    this->itsX=-1;
40    this->itsXLen=0;
41  }
42
[770]43  Scan::Scan(long y, long x, long xl):
44    itsY(y), itsX(x),itsXLen(xl)
45  {
46  }
47
[270]48  Scan::Scan(const Scan& s)
49  {
[365]50    operator=(s);
[270]51  }
52  //------------------------------------------------------
53 
[770]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  }
[270]62  //------------------------------------------------------
63
[773]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);
[776]70      this->itsX=x;
71      this->itsXLen=xmax-x+1;
[773]72    }
73    return altered;
74  }
75
76
[252]77  Scan nullScan()
78  {
[528]79    /// A simple way of returning a scan with zero length.
[252]80    Scan null(-1,-1,0);
81    return null;
[243]82  }
[527]83
84  bool Scan::isNull()
85  {
86    return (itsY==-1 && itsX==-1 && itsXLen==0);
87  }
88
[252]89  //------------------------------------------------------
90
91  Scan unite(Scan &scan1, Scan &scan2)
92  {
[528]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.
[252]95
96    Scan joined;
[527]97    if(!touching(scan1,scan2)){
[252]98      joined = nullScan();
99    }
100    else{
101      long y = scan1.getY();
[527]102      long x = std::min(scan1.getX(),scan2.getX());
103      long xmax = std::max(scan1.getXmax(),scan2.getXmax());
[252]104      joined.define(y,x,xmax-x+1);
105    }
106    return joined;
[238]107  }
[244]108
[252]109  //------------------------------------------------------
[238]110
[252]111  Scan intersect(Scan &scan1, Scan &scan2)
112  {
[528]113    /// Return a scan that includes all pixels that lie in both scans.
114    ///
115    /// If they do not overlap, return the null scan.
[246]116
[252]117    Scan intersection;
[770]118    if(!scan1.overlaps(scan2)){
[252]119      intersection = nullScan();
120    }
121    else{
122      long y = scan1.getY();
[527]123      long x = std::max(scan1.getX(),scan2.getX());
124      long xmax = std::min(scan1.getXmax(),scan2.getXmax());
[252]125      intersection.define(y,x,xmax-x+1);
126    }
127    return intersection;
[238]128  }
[252]129  //------------------------------------------------------
[238]130
[773]131  bool Scan::touches(const Scan &other)
[770]132  {
133    return this->overlaps(other) || this->isAdjacentTo(other);
134  }
135
[773]136  bool Scan::overlaps(const Scan &other)
[770]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
[773]147  bool Scan::isAdjacentTo(const Scan &other)
[770]148  {
149    if(this->itsY != other.itsY) return false;
150    else if(this->itsX <= other.itsX){
[776]151      return (other.itsX == (this->itsX+this->itsXLen));
[770]152    }
153    else{
[776]154      return (this->itsX == (other.itsX+other.itsXLen));
[770]155    }
156  }
157  //------------------------------------------------------
158
[252]159  bool touching(Scan &scan1, Scan &scan2)
160  {
[528]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
[527]165    return overlap(scan1,scan2) || adjacent(scan1,scan2);
[238]166 
[252]167  }
168  //------------------------------------------------------
[238]169
[252]170  bool overlap(Scan &scan1, Scan &scan2)
171  {
[528]172    ///  Test whether two scans overlap, ie. they have pixels in
173    ///  common.
174    /// \return A bool value.
175
[252]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());
[238]181 
[252]182  }
183  //------------------------------------------------------
[238]184
[252]185  bool adjacent(Scan &scan1, Scan &scan2)
186  {
[528]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
[252]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);
[238]196 
[252]197  }
198  //------------------------------------------------------
[238]199
[252]200  std::ostream& operator<< ( std::ostream& theStream, Scan& scan)
201  {
[528]202    ///  Output the three key parameters of the scan.
203
[527]204    if(scan.isNull()) theStream << "NULL";
205    else{
206      theStream << scan.itsX;
207      theStream << "-" << scan.getXmax();
208      theStream << ", " << scan.itsY;
209    }
[270]210    return theStream;
[252]211  }
212  //------------------------------------------------------
[238]213
[252]214  bool operator< (Scan lhs, Scan rhs)
215  {
[528]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
[252]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  //------------------------------------------------------
[238]228
[252]229  bool operator== (Scan lhs, Scan rhs)
230  {
[528]231    /// For two scans to be equal, all three parameters must be equal.
232
[252]233    return (lhs.itsY == rhs.itsY) &&
234      (lhs.itsX == rhs.itsX) &&
235      (lhs.itsXLen == rhs.itsXLen);
236  }
237  //------------------------------------------------------
[238]238
[252]239  bool Scan::isInScan(long x, long y)
240  {
241    return (y == this->itsY) &&
242      ( (x>= this->itsX) && (x < (this->itsXLen+this->itsX)) );
243  }
[253]244  //------------------------------------------------------
[252]245
[505]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());
[1443]251    else return abs(s1.getY()-s2.getY());
[505]252   
253  }
254  //------------------------------------------------------
255 
256 
[253]257  void mergeList(std::vector<Scan> scanlist)
258  {
259    std::vector<Scan>::iterator iter;
[541]260    unsigned int counter=0,compCounter;
[253]261    while(counter<(scanlist.size()-1)){
[252]262
[253]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
[238]285}
Note: See TracBrowser for help on using the repository browser.