source: trunk/src/PixelMap/Object2D.cc @ 365

Last change on this file since 365 was 365, checked in by MatthewWhiting, 17 years ago
  • Mainly fixing up copy constructors & assignment operators
  • Improved the testing of whether there is a spectral or third axis, including giving FitsHeader? a new function. Also improved the tabular output of VEL/WVEL.
  • Improved the F_int column -- made sure its width is always calculated, and gave it default units
  • Improved the printSpace etc functions, to accept an arbitrary stream
  • Removed some unnecessary comments
File size: 12.4 KB
Line 
1// -----------------------------------------------------------------------
2// Object2D.cc: Member functions for the Object2D 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 <PixelMap/Object2D.hh>
30#include <PixelMap/Voxel.hh>
31#include <iostream>
32#include <vector>
33
34namespace PixelInfo
35{
36
37  Object2D::Object2D()
38  {
39    this->numPix = 0;
40  }
41
42  Object2D::Object2D(const Object2D& o)
43  {
44    operator=(o);
45  } 
46
47  Object2D& Object2D::operator= (const Object2D& o)
48  {
49    if(this == &o) return *this;
50    this->scanlist = o.scanlist;
51    this->numPix   = o.numPix;
52    this->xSum     = o.xSum;
53    this->ySum     = o.ySum;
54    this->xmin     = o.xmin;
55    this->ymin     = o.ymin;
56    this->xmax     = o.xmax;
57    this->ymax     = o.ymax;
58    return *this;
59  } 
60  //------------------------------------------------------
61
62  void Object2D::addPixel(long x, long y)
63  {
64    /**
65     *  This function has three parts to it:
66     *  <ul><li> First, it searches through the existing scans to see if
67     *            a) there is a scan of the same y-value present, and
68     *            b) whether the (x,y) pixel lies in or next to an existing
69     *            Scan. If so, it is added and the Scan is grown if need be.
70     *            If this isn't the case, a new Scan of length 1 is added to
71     *            the list.
72     *      <li> If the Scan list was altered, all are checked to see whether
73     *            there is now a case of Scans touching. If so, they are
74     *            combined and added to the end of the list.
75     *      <li> If the pixel was added, the parameters are updated and the
76     *           pixel counter incremented.
77     *  </ul>
78     */
79
80
81    bool flagDone=false,flagChanged=false, isNew=false;
82
83    int scanCount = 0;
84    while(!flagDone && (scanCount<this->scanlist.size()) ){
85      Scan scan = this->scanlist[scanCount];   
86      if(y == scan.itsY){ // if the y value is already in the list
87        if(scan.isInScan(x,y)) flagDone = true; // pixel already here.
88        else{ // it's a new pixel!
89          if((x==(scan.itsX-1)) || (x == (scan.getXmax()+1)) ){
90            // if it is adjacent to the existing range, add to that range
91            if(x==(scan.itsX-1)) this->scanlist[scanCount].growLeft();
92            else                 this->scanlist[scanCount].growRight();
93            flagDone = true;
94            flagChanged = true;
95            isNew = true;
96          }
97        }     
98      }
99      scanCount++;
100    }
101
102    if(!flagDone){
103      Scan newOne(y,x,1);
104      this->scanlist.push_back(newOne);
105      isNew = true;
106    }
107 
108    if(flagChanged){
109      // this is true only if one of the pre-existing scans has changed
110      //
111      // need to clean up, to see if there is a case of two scans when
112      // there should be one.
113      //
114      // because there has been only one pixel added, only 2 AT MOST
115      // scans will need to be combined, so once we meet a pair that
116      // overlap, we can finish.
117
118      std::vector <Scan>::iterator iter;
119      bool combined = false;
120      int size = this->scanlist.size();
121      int count1=0;
122      while(!combined && count1<size){
123        int count2 = count1 + 1;
124        while(!combined && count2<size){
125          Scan first = this->scanlist[count1];
126          Scan second = this->scanlist[count2];
127          if(y==first.itsY && y==second.itsY){
128            combined = touching(first, second);
129            if(combined){
130              Scan newOne = unite(first,second);;
131              iter = this->scanlist.begin() + count2;
132              this->scanlist.erase(iter);
133              iter = this->scanlist.begin() + count1;
134              this->scanlist.erase(iter);
135              this->scanlist.push_back(newOne);
136            }
137          }     
138          count2++;
139        }
140        count1++;
141      }
142
143    }
144
145    if(isNew){ 
146      // update the centres, mins, maxs and increment the pixel counter
147      if(this->numPix==0){
148        this->xSum = this->xmin = this->xmax = x;
149        this->ySum = this->ymin = this->ymax = y;
150      }
151      else{
152        this->xSum += x;
153        this->ySum += y;
154        if(x<this->xmin) this->xmin = x;
155        if(x>this->xmax) this->xmax = x;
156        if(y<this->ymin) this->ymin = y;
157        if(y>this->ymax) this->ymax = y;
158      }
159      this->numPix++;
160    }
161
162  }
163  //------------------------------------------------------
164
165  void Object2D::addScan(Scan scan)
166  {
167    bool flagDone=false,flagChanged=false;;
168    long y = scan.itsY;
169    int scanCount = 0;
170    std::vector <Scan>::iterator iter;
171
172    while(!flagDone && (scanCount<this->scanlist.size()) ){
173      Scan existing = this->scanlist[scanCount];   
174      if(y==existing.itsY){ //ie. if the y value has a scan present
175        if(scan == existing) flagDone = true; // the scan is already there
176        else if(touching(scan,existing)){ // if it overlaps or is next to an existing scan
177          //    Scan joined = unite(scan,existing);
178          //    iter = this->scanlist.begin() + scanCount;
179          //    this->scanlist.erase(iter);
180          //    this->scanlist.push_back(joined);
181          //    flagDone = true;
182          flagChanged = true;
183        }
184      }
185      scanCount++;
186    }
187
188    // if it is unconnected with any existing scan, add it to the end of
189    // the list
190    if(!flagDone) this->scanlist.push_back(scan);
191
192    // if the scan has been added, we need to change the centres, mins,
193    // maxs etc. First add all the pixels from the scan. We then remove
194    // any doubled up ones later.
195    if( (!flagDone) || flagChanged ){
196      if(this->numPix==0){
197        this->ySum = y*scan.itsXLen;
198        this->ymin = this->ymax = y;
199        this->xmin = scan.itsX;
200        this->xmax = scan.getXmax();
201        this->xSum = scan.itsX;
202        for(int x=scan.itsX+1;x<=scan.getXmax();x++) this->xSum += x;
203        this->numPix = scan.itsXLen;
204      }
205      else{
206        this->ySum += y*scan.itsXLen;
207        for(int x=scan.itsX; x<=scan.getXmax(); x++) this->xSum += x;
208        if(y<this->ymin) this->ymin = y;
209        if(y>this->ymax) this->ymax = y;
210        if(scan.itsX<this->xmin)      this->xmin = scan.itsX;
211        if(scan.getXmax()>this->xmax) this->xmax = scan.getXmax();
212        this->numPix += scan.itsXLen;
213      }
214    }
215
216    if(flagChanged){
217      // this is true only if one of the pre-existing scans has changed
218      //
219      // In this case, we are adding a scan, and the possibility exists
220      // that more than one other scan could be affected. We therefore
221      // need to look over all scans, not just stopping at the first
222      // match we come across (as for addPixel() above).
223      int count1=0;
224      while(count1<this->scanlist.size()-1){
225        int count2 = count1 + 1;
226        do {
227          Scan first = this->scanlist[count1];
228          Scan second = this->scanlist[count2];
229          if(y==first.itsY && y==second.itsY){
230            // only look at the y-value where there would have been a change.
231            if(touching(first,second)){
232              Scan newOne = unite(first,second);
233              iter = this->scanlist.begin() + count2;
234              this->scanlist.erase(iter);
235              iter = this->scanlist.begin() + count1;
236              this->scanlist.erase(iter);
237              this->scanlist.push_back(newOne);
238           
239              count2 = count1 + 1;
240
241              // Need to remove the doubled-up pixels from the info
242              Scan overlap = intersect(first,second);
243              this->ySum -= overlap.itsY*overlap.itsXLen;
244              for(int x=overlap.itsX; x<=overlap.getXmax(); x++)
245                this->xSum -= x;
246              this->numPix -= overlap.itsXLen;
247
248            }
249            else count2++;
250          }     
251          else count2++;
252        } while(count2 < this->scanlist.size());
253
254        count1++;
255      }
256    }
257
258  }
259  //------------------------------------------------------
260
261  bool Object2D::isInObject(long x, long y)
262  {
263    bool result = false;
264    long scanCount = 0;
265    do{
266
267      if(y == this->scanlist[scanCount].itsY){
268        // if the y value is already in the list
269
270        if((x>=this->scanlist[scanCount].itsX)&&
271           (x<this->scanlist[scanCount].getXlen())){
272          // if the x value is already in the range, the pixel is
273          //  already stored, so return true
274          result = true;
275        }
276
277      }
278
279      scanCount++;
280
281    }while( !result && (scanCount<this->scanlist.size()) );
282
283    return result;
284
285  }
286  //------------------------------------------------------
287
288  // long Object2D::getSize()
289  // {
290  //   long size=0;
291  //   for(int i=0;i<this->scanlist.size();i++){
292  //     size += this->scanlist[i].getXlen();
293  //   }
294  //   return size;
295  // }
296  //------------------------------------------------------
297
298  std::ostream& operator<< ( std::ostream& theStream, Object2D& obj)
299  {
300    for(int i=0;i<obj.scanlist.size();i++)
301      theStream << obj.scanlist[i] << "\n";
302    theStream<<"---\n";
303    return theStream;
304
305  }
306  //------------------------------------------------------
307
308  void Object2D::calcParams()
309  {
310    this->xSum = 0;
311    this->ySum = 0;
312    int count = 0;
313    for(int s=0;s<this->scanlist.size();s++){
314
315      if(s==0){
316        this->ymin = this->ymax = this->scanlist[s].itsY;
317        this->xmin = this->scanlist[s].itsX;
318        this->xmax = this->scanlist[s].getXmax();
319      }
320      else{
321        if(this->ymin>this->scanlist[s].itsY)    this->ymin = this->scanlist[s].itsY;
322        if(this->xmin>this->scanlist[s].itsX)    this->xmin = this->scanlist[s].itsX;
323        if(this->ymax<this->scanlist[s].itsY)    this->ymax = this->scanlist[s].itsY;
324        if(this->xmax<this->scanlist[s].getXmax()) this->xmax = this->scanlist[s].getXmax();
325      }
326
327      count += this->scanlist[s].getXlen();
328      this->ySum += this->scanlist[s].itsY*this->scanlist[s].getXlen();
329      for(int x=this->scanlist[s].itsX;x<this->scanlist[s].getXmax();x++)
330        this->xSum += x;
331
332    }
333
334  }
335  //------------------------------------------------------
336
337  void Object2D::cleanup()
338  {
339    int counter=0, compCounter=1;
340    std::vector<Scan>::iterator iter;
341    while(counter < this->scanlist.size())
342      {
343        compCounter = counter + 1;
344     
345        while( compCounter < this->scanlist.size() ) {
346
347          Scan scan1 = this->scanlist[counter];
348          Scan scan2 = this->scanlist[compCounter];
349          if(overlap(scan1,scan2)){
350            Scan newscan = unite(scan1,scan2);
351            iter = this->scanlist.begin()+compCounter;
352            this->scanlist.erase(iter);
353            iter = this->scanlist.begin()+counter;
354            this->scanlist.erase(iter);
355            this->scanlist.push_back(newscan);
356            compCounter = counter + 1;
357          }
358          else compCounter++;
359
360        }
361
362        counter++;
363      }
364
365    //   this->order();
366
367  }
368  //------------------------------------------------------
369
370  long Object2D::getNumDistinctY()
371  {
372    std::vector<long> ylist;
373    if(this->scanlist.size()==0) return 0;
374    if(this->scanlist.size()==1) return 1;
375    ylist.push_back(this->scanlist[0].itsY);
376    for(int i=1;i<this->scanlist.size();i++){
377      bool inList = false;
378      int j=0;
379      long y = this->scanlist[i].itsY;
380      while(!inList && j<ylist.size()){
381        if(y==ylist[j]) inList=true;
382        j++;
383      };
384      if(!inList) ylist.push_back(y);
385    }
386    return ylist.size();
387  }
388  //------------------------------------------------------
389
390  long Object2D::getNumDistinctX()
391  {
392    std::vector<long> xlist;
393    if(this->scanlist.size()==0) return 0;
394    if(this->scanlist.size()==1) return 1;
395    for(int x=this->scanlist[0].itsX;x<this->scanlist[0].getXmax();x++)
396      xlist.push_back(x);
397    for(int i=1;i<this->scanlist.size();i++){
398      for(int x=this->scanlist[0].itsX;x<this->scanlist[0].getXmax();x++){
399        bool inList = false;
400        int j=0;
401        while(!inList && j<xlist.size()){
402          if(x==xlist[j]) inList=true;
403          j++;
404        };
405        if(!inList) xlist.push_back(x);
406      }
407    }
408    return xlist.size();
409  }
410  //------------------------------------------------------
411
412  bool Object2D::scanOverlaps(Scan scan)
413  {
414    bool returnval = false;
415    int scanCount=0;
416    while(!returnval && scanCount<this->scanlist.size()){
417      returnval = returnval || overlap(scan,this->scanlist[scanCount++]);
418    }
419    return returnval;
420  }
421
422}
Note: See TracBrowser for help on using the repository browser.