source: branches/pixel-map-branch/src/PixelMap/Object3D.cc @ 1339

Last change on this file since 1339 was 252, checked in by Matthew Whiting, 17 years ago
  • Have put all classes in the files in src/PixelMap/ into a PixelInfo? namespace.
  • Added "using namespace PixelInfo?" to all necessary files.
  • Removed "friend class Detection" from Voxel and Object3D classes -- not necessary and complicated now by them being in the namespace
  • Various minor changes, including fixing up commentary.
File size: 11.7 KB
Line 
1#include <iostream>
2#include <PixelMap/Voxel.hh>
3#include <PixelMap/Scan.hh>
4#include <PixelMap/Object2D.hh>
5#include <PixelMap/Object3D.hh>
6#include <vector>
7
8namespace PixelInfo
9{
10
11  ChanMap::ChanMap(const ChanMap& m){
12    this->itsZ=m.itsZ;
13    this->itsObject=m.itsObject;
14  }
15  //--------------------------------------------
16
17  ChanMap& ChanMap::operator= (const ChanMap& m){
18    this->itsZ=m.itsZ;
19    this->itsObject=m.itsObject;
20  }
21  //--------------------------------------------
22  //============================================
23  //--------------------------------------------
24
25  Object3D::Object3D(const Object3D& o){
26    this->maplist = o.maplist;
27    this->numVox  = o.numVox;
28    this->xSum    = o.xSum;
29    this->ySum    = o.ySum;
30    this->zSum    = o.zSum;
31    this->xmin    = o.xmin;
32    this->ymin    = o.ymin;
33    this->zmin    = o.zmin;
34    this->xmax    = o.xmax;
35    this->ymax    = o.ymax;
36    this->zmax    = o.zmax;
37  }
38  //--------------------------------------------
39
40  Object3D& Object3D::operator= (const Object3D& o){
41    this->maplist = o.maplist;
42    this->numVox  = o.numVox;
43    this->xSum    = o.xSum;
44    this->ySum    = o.ySum;
45    this->zSum    = o.zSum;
46    this->xmin    = o.xmin;
47    this->ymin    = o.ymin;
48    this->zmin    = o.zmin;
49    this->xmax    = o.xmax;
50    this->ymax    = o.ymax;
51    this->zmax    = o.zmax;
52  }
53  //--------------------------------------------
54 
55  bool Object3D::isInObject(long x, long y, long z)
56  {
57    bool returnval = false;
58    int mapCount = 0;
59    while(!returnval && mapCount < this->maplist.size()){
60      if(z == this->maplist[mapCount].itsZ){
61        returnval = returnval ||
62          this->maplist[mapCount].itsObject.isInObject(x,y);
63      }
64      mapCount++;
65    }
66    return returnval;
67  }
68  //--------------------------------------------
69
70  void Object3D::addPixel(long x, long y, long z)
71  {
72    // first test to see if we have a chanmap of same z
73    bool haveZ = false;
74    int mapCount = 0;
75    while(!haveZ && mapCount < this->maplist.size()){
76      haveZ = haveZ || (z==this->maplist[mapCount++].itsZ);
77    }
78
79    if(!haveZ){ // need to add a new ChanMap
80      ChanMap newMap(z);
81      newMap.itsObject.addPixel(x,y);
82      this->maplist.push_back(newMap);
83      this->order();
84      // update the centres, min & max, as well as the number of voxels
85      if(this->numVox==0){
86        this->xSum = this->xmin = this->xmax = x;
87        this->ySum = this->ymin = this->ymax = y;
88        this->zSum = this->zmin = this->zmax = z;
89      }
90      else{
91        this->xSum += x;
92        this->ySum += y;
93        this->zSum += z;
94        if(x<this->xmin) this->xmin = x;
95        if(x>this->xmax) this->xmax = x;
96        if(y<this->ymin) this->ymin = y;
97        if(y>this->ymax) this->ymax = y;
98        // since we've ordered the maplist, the min & max z fall out
99        // naturally
100        this->zmin = this->maplist[0].itsZ;
101        this->zmax = this->maplist[this->maplist.size()-1].itsZ;
102      }
103      this->numVox++;   
104    }
105    else{
106      // there is a ChanMap of matching z. Find it..
107      mapCount=0;
108      while(this->maplist[mapCount].itsZ!=z) mapCount++;
109
110      // Remove that channel's information from the Object's information
111      long oldChanSize = this->maplist[mapCount].itsObject.numPix;
112      this->xSum -= this->maplist[mapCount].itsObject.xSum;
113      this->ySum -= this->maplist[mapCount].itsObject.ySum;
114      this->zSum -= z*oldChanSize;
115
116      // Add the channel
117      this->maplist[mapCount].itsObject.addPixel(x,y);
118   
119      // and update the information...
120      // This method deals with the case of a new pixel being added AND
121      // with the new pixel already existing in the Object2D
122      long newChanSize = this->maplist[mapCount].itsObject.numPix;
123   
124      this->numVox += (newChanSize - oldChanSize);
125      this->xSum += this->maplist[mapCount].itsObject.xSum;
126      this->ySum += this->maplist[mapCount].itsObject.ySum;
127      this->zSum += z*newChanSize;
128      if(x<this->xmin) this->xmin = x;
129      if(x>this->xmax) this->xmax = x;
130      if(y<this->ymin) this->ymin = y;
131      if(y>this->ymax) this->ymax = y;
132      // don't need to do anything to zmin/zmax -- the z-value is
133      // already in the list
134    }
135
136  }
137  //--------------------------------------------
138
139  void Object3D::addChannel(ChanMap channel)
140  {
141    // first test to see if we have a chanmap of same z
142    bool haveZ = false;
143    int mapCount = 0;
144    while( !haveZ && (mapCount < this->maplist.size()) ){
145      haveZ = haveZ || (channel.itsZ==this->maplist[mapCount].itsZ);
146      mapCount++;
147    }
148
149    if(!haveZ){ // need to add a new ChanMap
150      this->maplist.push_back(channel);
151      this->order();
152      // update the centres, min & max, as well as the number of voxels
153      if(this->numVox==0){ // ie. if it is the only channel map
154        this->xSum = channel.itsObject.xSum;
155        this->xmin = channel.itsObject.xmin;
156        this->xmax = channel.itsObject.xmax;
157        this->ySum = channel.itsObject.ySum;
158        this->ymin = channel.itsObject.ymin;
159        this->ymax = channel.itsObject.ymax;
160        this->zSum = channel.itsObject.numPix*channel.itsZ;
161        this->zmin = this->zmax = channel.itsZ;
162      }
163      else{
164        this->xSum += channel.itsObject.xSum;
165        this->ySum += channel.itsObject.ySum;
166        this->zSum += channel.itsZ*channel.itsObject.numPix;
167        if(channel.itsObject.xmin<this->xmin)
168          this->xmin = channel.itsObject.xmin;
169        if(channel.itsObject.xmax>this->xmax)
170          this->xmax = channel.itsObject.xmax;
171        if(channel.itsObject.ymin<this->ymin)
172          this->ymin = channel.itsObject.ymin;
173        if(channel.itsObject.ymax>this->ymax)
174          this->ymax = channel.itsObject.ymax;
175        // since we've ordered the maplist, the min & max z fall out
176        // naturally
177        this->zmin = this->maplist[0].itsZ;
178        this->zmax = this->maplist[this->maplist.size()-1].itsZ;
179      }
180      this->numVox += channel.itsObject.numPix;
181    }
182    else{
183      // there is a ChanMap of matching z. Find it and add the channel
184      // map to the correct existing channel map
185      mapCount=0; 
186      while(this->maplist[mapCount].itsZ!=channel.itsZ) mapCount++;
187
188      // Remove the new channel's information from the Object's information
189      long oldChanSize = this->maplist[mapCount].itsObject.numPix;
190      this->xSum -= this->maplist[mapCount].itsObject.xSum;
191      this->ySum -= this->maplist[mapCount].itsObject.ySum;
192      this->zSum -= this->maplist[mapCount].itsZ*oldChanSize;
193
194      // Delete the old map and add the new one on the end. Order the list.
195      ChanMap newMap = this->maplist[mapCount] + channel;
196      std::vector <ChanMap>::iterator iter;
197      iter = this->maplist.begin() + mapCount;
198      this->maplist.erase(iter);
199      this->maplist.push_back(newMap);
200      this->order();   
201
202      // and update the information...
203      // This method deals correctly with all cases of adding an object.
204      long newChanSize = newMap.itsObject.numPix;
205      this->numVox += (newChanSize - oldChanSize);
206      this->xSum += newMap.itsObject.xSum;
207      this->ySum += newMap.itsObject.ySum;
208      this->zSum += newMap.itsZ*newChanSize;
209      if(channel.itsObject.xmin<this->xmin) this->xmin = channel.itsObject.xmin;
210      if(channel.itsObject.xmax>this->xmax) this->xmax = channel.itsObject.xmax;
211      if(channel.itsObject.ymin<this->ymin) this->ymin = channel.itsObject.ymin;
212      if(channel.itsObject.ymax>this->ymax) this->ymax = channel.itsObject.ymax;
213      // don't need to do anything to zmin/zmax -- the z-value is
214      // already in the list
215
216    }
217
218  }
219  //--------------------------------------------
220
221  // long Object3D::getSize()
222  // {
223  //   long size=0;
224  //   for(int i=0;i<this->maplist.size();i++)
225  //     size+=this->maplist[i].itsObject.getSize();
226  //   return size;
227  // }
228  //--------------------------------------------
229
230  long Object3D::getNumDistinctZ()
231  {
232    return this->maplist.size();
233  }
234  //--------------------------------------------
235
236  long Object3D::getSpatialSize()
237  {
238    Object2D spatialMap;
239    for(int i=0;i<this->maplist.size();i++){
240      for(int s=0;s<this->maplist[i].itsObject.getNumScan();s++){
241        spatialMap.addScan(this->maplist[i].itsObject.getScan(s));
242      }
243    }
244    return spatialMap.getSize();
245  }
246  //--------------------------------------------
247
248  Object2D Object3D::getSpatialMap()
249  {
250    Object2D spatialMap = this->maplist[0].itsObject;
251    for(int i=1;i<this->maplist.size();i++){
252      spatialMap = spatialMap + this->maplist[i].itsObject;
253    }
254    return spatialMap;
255  }
256  //--------------------------------------------
257 
258  void Object3D::calcParams()
259  {
260    this->xSum = 0;
261    this->ySum = 0;
262    this->zSum = 0;
263    int count = 0;
264    for(int m=0;m<this->maplist.size();m++){
265
266      this->maplist[m].itsObject.calcParams();
267
268      if(m==0){
269        this->xmin = this->maplist[m].itsObject.getXmin();
270        this->xmax = this->maplist[m].itsObject.getXmax();
271        this->ymin = this->maplist[m].itsObject.getYmin();
272        this->ymax = this->maplist[m].itsObject.getYmax();
273        this->zmin = this->zmax = this->maplist[m].itsZ;
274      }
275      else{
276        if(this->xmin>this->maplist[m].itsObject.getXmin())
277          this->xmin = this->maplist[m].itsObject.getXmin();
278        if(this->xmax<this->maplist[m].itsObject.getXmax())
279          this->xmax = this->maplist[m].itsObject.getXmax();
280        if(this->ymin>this->maplist[m].itsObject.getYmin())
281          this->ymin = this->maplist[m].itsObject.getYmin();
282        if(this->ymax<this->maplist[m].itsObject.getYmax())
283          this->ymax = this->maplist[m].itsObject.getYmax();
284        if(this->zmin>this->maplist[m].itsZ) this->zmin = this->maplist[m].itsZ;
285        if(this->zmax<this->maplist[m].itsZ) this->zmax = this->maplist[m].itsZ;
286      }
287
288      long size = this->maplist[m].itsObject.getSize();
289      this->xSum += this->maplist[m].itsObject.xSum;
290      this->ySum += this->maplist[m].itsObject.ySum;
291      this->zSum += this->maplist[m].itsZ * size;
292      count += size;
293
294    }
295
296  }
297  //------------------------------------------------------
298
299  std::ostream& operator<< ( std::ostream& theStream, Object3D& obj)
300  {
301    for(int m=0;m<obj.maplist.size();m++){
302      Object2D tempObject = obj.maplist[m].getObject();
303      for(int s=0;s<tempObject.getNumScan();s++){
304        Scan tempscan = tempObject.getScan(s);
305        //      theStream << obj.maplist[m].getZ() << " " << tempscan << "\n";
306        for(int x=tempscan.getX();x<=tempscan.getXmax();x++){
307          theStream << x                     << " "
308                    << tempscan.getY()       << " "
309                    << obj.maplist[m].getZ() << "\n";
310        }
311      }
312    } 
313    theStream << "\n";
314  }
315  //--------------------------------------------
316
317  Voxel Object3D::getPixel(int pixNum)
318  {
319    Voxel returnVox(-1,-1,-1,0.);
320    if((pixNum<0)||(pixNum>=this->getSize())) return returnVox;
321    int count1=0;
322    for(int m=0;m<this->maplist.size();m++)
323      {
324        if(pixNum-count1<this->maplist[m].itsObject.getSize())
325          {
326            returnVox.setZ(this->maplist[m].getZ());
327            int count2=0;
328            for(int s=0;s<this->maplist[m].getNumScan();s++)
329              {
330                if(pixNum-count1-count2<this->maplist[m].getScan(s).getXlen())
331                  {
332                    returnVox.setY(this->maplist[m].getScan(s).getY());
333                    returnVox.setX(this->maplist[m].getScan(s).getX()
334                                   + pixNum - count1 - count2);
335                    return returnVox;
336                  }
337                count2+=this->maplist[m].getScan(s).getXlen();
338              }
339          }
340        count1+=this->maplist[m].itsObject.getSize();     
341      }
342  }
343  //--------------------------------------------------------------------
344
345  std::vector<Voxel> Object3D::getPixelSet()
346  {
347    //   long size = this->getSize();
348    std::vector<Voxel> voxList(this->numVox);
349    long count = 0;
350    for(int m=0;m<this->maplist.size();m++){
351      Object2D obj = this->maplist[m].itsObject;
352      long z = this->maplist[m].itsZ;
353      for(int s=0;s<obj.getNumScan();s++){
354        Scan scn = obj.getScan(s);
355        long y = scn.getY();
356        for(long x=scn.getX(); x<=scn.getXmax(); x++){
357          voxList[count].setXYZF(x,y,z,0);
358          count++;
359        }
360      }
361    }
362    if(count != this->numVox) {
363      std::cerr << "****** " << count << " -- " << this->numVox <<"\n";
364      std::cerr << *this << "\n";
365    }
366    return voxList;
367
368  }
369
370}
Note: See TracBrowser for help on using the repository browser.