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

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

A large set of changes, each of which small ones from compiling with the -Wall flag (plus the -Wno-sign-compare flag -- as we don't care about warnings about comparing ints and unsigned ints).

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