source: tags/release-1.1/src/PixelMap/Object2D.cc @ 1391

Last change on this file since 1391 was 301, checked in by Matthew Whiting, 17 years ago

Mostly adding the distribution text to the start of files, with a few additional comments added too.

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