source: tags/release-1.1.5/src/PixelMap/Scan.cc @ 1441

Last change on this file since 1441 was 418, checked in by MatthewWhiting, 16 years ago

A range of changes from CONRAD development:

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