source: trunk/src/PixelMap/Object3D.cc @ 775

Last change on this file since 775 was 775, checked in by MatthewWhiting, 14 years ago

No functional change, just removing old commented-out code.

File size: 13.7 KB
Line 
1// -----------------------------------------------------------------------
2// Object3D.cc: Member functions for Object3D 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 <iostream>
29#include <sstream>
30#include <duchamp/PixelMap/Voxel.hh>
31#include <duchamp/PixelMap/Scan.hh>
32#include <duchamp/PixelMap/Object2D.hh>
33#include <duchamp/PixelMap/Object3D.hh>
34#include <vector>
35#include <map>
36
37namespace PixelInfo
38{
39  Object3D::Object3D()
40  {
41    this->numVox=0;
42    this->xSum = 0;
43    this->ySum = 0;
44    this->zSum = 0;
45    this->xmin = this->xmax = this->ymin = this->ymax = this->zmin = this->zmax = -1;
46  }
47  //--------------------------------------------
48
49  Object3D::Object3D(const Object3D& o)
50  {
51    operator=(o);
52  }
53  //--------------------------------------------
54
55  Object3D& Object3D::operator= (const Object3D& o)
56  {
57    if(this == &o) return *this;
58    this->chanlist = o.chanlist;
59    this->numVox  = o.numVox;
60    this->xSum    = o.xSum;
61    this->ySum    = o.ySum;
62    this->zSum    = o.zSum;
63    this->xmin    = o.xmin;
64    this->ymin    = o.ymin;
65    this->zmin    = o.zmin;
66    this->xmax    = o.xmax;
67    this->ymax    = o.ymax;
68    this->zmax    = o.zmax;
69    return *this;
70  }
71  //--------------------------------------------
72
73  Object3D operator+ (Object3D lhs, Object3D rhs)
74  {
75    Object3D output = lhs;
76    for(std::map<long, Object2D>::iterator it = rhs.chanlist.begin(); it!=rhs.chanlist.end();it++)
77      output.addChannel(it->first, it->second);
78      //      output.addChannel(*it);
79    return output;
80  }
81
82  //--------------------------------------------
83  float Object3D::getXaverage()
84  {
85    if(numVox>0) return xSum/float(numVox);
86    else return 0.;
87  }
88  //--------------------------------------------
89
90  float Object3D::getYaverage()
91  {
92    if(numVox>0) return ySum/float(numVox);
93    else return 0.;
94  }
95  //--------------------------------------------
96
97  float Object3D::getZaverage()
98  {
99    if(numVox>0) return zSum/float(numVox);
100    else return 0.;
101  }
102  //--------------------------------------------
103 
104  bool Object3D::isInObject(long x, long y, long z)
105  {
106    std::map<long,Object2D>::iterator it=this->chanlist.begin();
107    while(it!=this->chanlist.end() && it->first!=z) it++;
108
109    if(it==this->chanlist.end()) return false;
110    else                         return it->second.isInObject(x,y);
111  }
112  //--------------------------------------------
113
114  void Object3D::addPixel(long x, long y, long z)
115  {
116 
117    std::map<long,Object2D>::iterator it=this->chanlist.begin();
118    while(it!=this->chanlist.end() && it->first!=z) it++;
119
120    if(it==this->chanlist.end()){ //new channel
121      Object2D obj;
122      obj.addPixel(x,y);
123      chanlist.insert( std::pair<int,Object2D>(z,obj) );
124      // update the centres, min & max, as well as the number of voxels
125      if(this->numVox==0){
126        this->xSum = this->xmin = this->xmax = x;
127        this->ySum = this->ymin = this->ymax = y;
128        this->zSum = this->zmin = this->zmax = z;
129      }
130      else{
131        this->xSum += x;
132        this->ySum += y;
133        this->zSum += z;
134        if(x<this->xmin) this->xmin = x;
135        if(x>this->xmax) this->xmax = x;
136        if(y<this->ymin) this->ymin = y;
137        if(y>this->ymax) this->ymax = y;
138        // since we've ordered the maplist, the min & max z fall out naturally
139        this->zmin = this->chanlist.begin()->first;
140        this->zmax = this->chanlist.rbegin()->first;
141      }
142      this->numVox++;   
143    }
144    else{ // existing channel
145      // This method deals with the case of a new pixel being added AND
146      // with the new pixel already existing in the Object2D
147 
148      // Remove that channel's information from the Object's information
149      long oldChanSize = it->second.numPix;
150      this->xSum -= it->second.xSum;
151      this->ySum -= it->second.ySum;
152      this->zSum -= z*oldChanSize;
153
154      // Add the pixel
155      it->second.addPixel(x,y);
156   
157      // and update the information...
158     long newChanSize = it->second.numPix;
159   
160      this->numVox += (newChanSize - oldChanSize);
161      this->xSum += it->second.xSum;
162      this->ySum += it->second.ySum;
163      this->zSum += z*newChanSize;
164      if(x<this->xmin) this->xmin = x;
165      if(x>this->xmax) this->xmax = x;
166      if(y<this->ymin) this->ymin = y;
167      if(y>this->ymax) this->ymax = y;
168      // don't need to do anything to zmin/zmax -- the z-value is
169      // already in the list
170    }
171
172  }
173  //--------------------------------------------
174
175  void Object3D::addScan(Scan s, long z)
176  {
177    long y=s.getY();
178    for(int x=s.getX(); x<=s.getXmax(); x++)
179      this->addPixel(x,y,z);
180  }
181
182  //--------------------------------------------
183
184  // void Object3D::addChannel(const std::pair<long, Object2D> *chan)
185  // {
186  //   long z = chan->first;
187  //   Object2D
188  // }
189
190
191  void Object3D::addChannel(const long &z, Object2D &obj)
192  {
193
194    std::map<long,Object2D>::iterator it=this->chanlist.begin();
195    while(it!=this->chanlist.end() && it->first!=z) it++;
196
197    if(it == this->chanlist.end()){ // channel z is not already in object, so add it.
198      this->chanlist.insert(std::pair<long,Object2D>(z,obj));
199      if(this->numVox == 0){ // if there are no other pixels, so initialise mins,maxs,sums
200        this->xmin = obj.xmin;
201        this->xmax = obj.xmax;
202        this->ymin = obj.ymin;
203        this->ymax = obj.ymax;
204        this->zmin = this->zmax = z;
205        this->xSum = obj.xSum;
206        this->ySum = obj.ySum;
207        this->zSum = z * obj.getSize();
208      }
209      else{ // there are other pixels in other channels, so update mins, maxs, sums
210        if(obj.xmin<this->xmin) this->xmin = obj.xmin;
211        if(obj.xmax>this->xmax) this->xmax = obj.xmax;
212        if(obj.ymin<this->ymin) this->ymin = obj.ymin;
213        if(obj.ymax>this->ymax) this->ymax = obj.ymax;
214        if(z<this->zmin) this->zmin = z;
215        if(z>this->zmax) this->zmax = z;
216        this->xSum += obj.xSum;
217        this->ySum += obj.ySum;
218        this->zSum += z * obj.getSize();
219      }
220      this->numVox += obj.getSize();
221    }
222    else{ // channel is already present, so need to combine objects.
223      this->xSum -= it->second.xSum;
224      this->ySum -= it->second.ySum;
225      this->zSum -= z*it->second.getSize();
226      this->numVox -= it->second.getSize();
227      it->second = it->second + obj;
228      this->xSum += it->second.xSum;
229      this->ySum += it->second.ySum;
230      this->zSum += z*it->second.getSize();
231      this->numVox += it->second.getSize();
232      if(obj.xmin<this->xmin) this->xmin = obj.xmin;
233      if(obj.xmax>this->xmax) this->xmax = obj.xmax;
234      if(obj.ymin<this->ymin) this->ymin = obj.ymin;
235      if(obj.ymax>this->ymax) this->ymax = obj.ymax;
236    }
237  }
238
239  //--------------------------------------------
240
241  unsigned long Object3D::getSpatialSize()
242  {
243    Object2D spatialMap = this->getSpatialMap();
244    return spatialMap.getSize();
245  }
246  //--------------------------------------------
247
248  Object2D Object3D::getSpatialMap()
249  {
250    Object2D spatialMap;
251    for(std::map<long, Object2D>::iterator it = this->chanlist.begin();
252        it!=this->chanlist.end();it++){
253      spatialMap = spatialMap + it->second;
254    }
255    return spatialMap;
256  }
257  //--------------------------------------------
258 
259  void Object3D::calcParams()
260  {
261    this->xSum = 0;
262    this->ySum = 0;
263    this->zSum = 0;
264    this->numVox = 0;
265
266    this->zmin = this->chanlist.begin()->first;
267    this->zmax = this->chanlist.rbegin()->first;
268    for(std::map<long, Object2D>::iterator it = this->chanlist.begin();
269        it!=this->chanlist.end();it++){
270
271      it->second.calcParams();
272
273      if(it==this->chanlist.begin()){
274        this->xmin = it->second.getXmin();
275        this->xmax = it->second.getXmax();
276        this->ymin = it->second.getYmin();
277        this->ymax = it->second.getYmax();
278      }
279      else{
280        if(it->second.xmin<this->xmin) this->xmin = it->second.xmin;
281        if(it->second.xmax>this->xmax) this->xmax = it->second.xmax;
282        if(it->second.ymin<this->ymin) this->ymin = it->second.ymin;
283        if(it->second.ymax>this->ymax) this->ymax = it->second.ymax;
284      }
285
286      this->xSum += it->second.xSum;
287      this->ySum += it->second.ySum;
288      this->zSum += it->first * it->second.getSize();
289      this->numVox += it->second.getSize();
290
291    }
292
293  }
294  //------------------------------------------------------
295
296  void Object3D::print(std::ostream& theStream)
297  {
298    for(std::map<long, Object2D>::iterator it = this->chanlist.begin();
299        it!=this->chanlist.end();it++){
300      for(std::vector<Scan>::iterator s=it->second.scanlist.begin();s!=it->second.scanlist.end();s++){
301        theStream << *s << "," << it->first << "\n";
302      }
303    } 
304    theStream << "\n";
305  }
306
307
308  std::ostream& operator<< ( std::ostream& theStream, Object3D& obj)
309  {
310    obj.print(theStream);
311    return theStream;
312  }
313  //--------------------------------------------
314
315  std::vector<Voxel> Object3D::getPixelSet()
316  {
317    /// @details Returns a vector of the Voxels in the object. All
318    /// flux values are set to 0.
319
320    std::vector<Voxel> voxList(this->numVox);
321    long count = 0;
322    for(std::map<long, Object2D>::iterator it = this->chanlist.begin();
323        it!=this->chanlist.end();it++){
324      long z = it->first;
325      for(std::vector<Scan>::iterator s=it->second.scanlist.begin();s!=it->second.scanlist.end();s++){
326        long y = s->getY();
327        for(long x=s->getX(); x<=s->getXmax(); x++){
328          voxList[count].setXYZF(x,y,z,0);
329          count++;
330        }
331      }
332    }
333    return voxList;
334
335  }
336
337  //--------------------------------------------------------------------
338
339  std::vector<Voxel> Object3D::getPixelSet(float *array, long *dim)
340  {
341    /// @details Returns a vector of Voxels with the flux values for each voxel
342    /// taken from the array provided. No check is made as to whether
343    /// the pixels fall in the array boundaries
344    /// @param array Array of pixel values
345    /// @param dim Array of axis dimensions
346
347    std::vector<Voxel> voxList(this->numVox);
348    long count = 0;
349    for(std::map<long, Object2D>::iterator it = this->chanlist.begin();
350        it!=this->chanlist.end();it++){
351      long z = it->first;
352      for(std::vector<Scan>::iterator s=it->second.scanlist.begin();s!=it->second.scanlist.end();s++){
353        long y = s->getY();
354        for(long x=s->getX(); x<=s->getXmax(); x++){
355          voxList[count].setXYZF(x,y,z,array[x+dim[0]*y+dim[0]*dim[1]*z]);
356          count++;
357        }
358      }
359    }
360    return voxList;
361
362  }
363
364  //--------------------------------------------------------------------
365
366  std::vector<long> Object3D::getChannelList()
367  {
368
369    std::vector<long> chanlist;
370    for(std::map<long, Object2D>::iterator it = this->chanlist.begin();
371        it != this->chanlist.end(); it++){
372      chanlist.push_back(it->first);
373    }
374    return chanlist;
375  }
376
377  //--------------------------------------------------------------------
378
379  Object2D Object3D::getChanMap(long z)
380  {
381    Object2D obj;
382    std::map<long,Object2D>::iterator it=this->chanlist.begin();
383    while(it!=this->chanlist.end() && it->first!=z) it++;
384
385    if(it==this->chanlist.end()) obj = Object2D();
386    else obj = it->second;
387
388    return obj;
389  }
390
391  //--------------------------------------------------------------------
392
393  int Object3D::getMaxAdjacentChannels()
394  {
395    /// @details Find the maximum number of contiguous channels in the
396    /// object. Since there can be gaps in the channels included in an
397    /// object, we run through the list of channels and keep track of
398    /// sizes of contiguous segments. Then return the largest size.
399
400    int maxnumchan=0;
401    int zcurrent=0, zprevious,zcount=0;
402    std::map<long, Object2D>::iterator it;
403    for(it = this->chanlist.begin(); it!=this->chanlist.end();it++)
404      {
405        if(it==this->chanlist.begin()){
406          zcount++;
407          zcurrent = it->first;
408        }
409        else{
410          zprevious = zcurrent;
411          zcurrent = it->first;
412          if(zcurrent-zprevious>1){
413            maxnumchan = std::max(maxnumchan, zcount);
414            zcount=1;
415          }
416          else zcount++;
417        }
418      }
419    maxnumchan = std::max(maxnumchan,zcount);
420    return maxnumchan;
421  }
422
423  //--------------------------------------------------------------------
424
425  void Object3D::addOffsets(long xoff, long yoff, long zoff)
426  {
427    std::map<long,Object2D> newmap;
428    for(std::map<long, Object2D>::iterator it = this->chanlist.begin(); it!=this->chanlist.end();it++){
429      std::pair<long,Object2D> newOne(it->first+zoff, it->second);
430      newOne.second.addOffsets(xoff,yoff);
431      newmap.insert(newOne);
432    }
433    this->chanlist.clear();
434    this->chanlist = newmap;
435    if(this->numVox>0){
436      this->xSum += xoff*numVox;
437      this->xmin += xoff; this->xmax += xoff;
438      this->ySum += yoff*numVox;
439      this->ymin += yoff; this->ymax += yoff;
440      this->zSum += zoff*numVox;
441      this->zmin += zoff; this->zmax += zoff;
442    }
443  }
444
445
446  duchamp::Section Object3D::getBoundingSection(int boundary)
447  {
448    std::stringstream ss;
449    ss << "[" << this->xmin-boundary <<":"<<this->xmax+boundary<<","<< this->ymin-boundary <<":"<<this->ymax+boundary<<","<< this->zmin-boundary <<":"<<this->zmax+boundary<<"]";
450    std::string sec=ss.str();
451    duchamp::Section section(sec);
452    return section;
453  }
454
455}
Note: See TracBrowser for help on using the repository browser.