source: branches/pixel-map-branch/src/Cubes/Merger.cc @ 1441

Last change on this file since 1441 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: 4.9 KB
Line 
1#include <iostream>
2#include <fstream>
3#include <iomanip>
4#include <math.h>
5#include <vector>
6#include <PixelMap/Object3D.hh>
7#include <Cubes/cubes.hh>
8#include <Detection/detection.hh>
9#include <Utils/utils.hh>
10#include <Utils/feedback.hh>
11
12using std::vector;
13using namespace PixelInfo;
14
15void Cube::ObjectMerger()
16{
17  /**
18   * A Function that takes a Cube's list of Detections and
19   * combines those that are close (according to the
20   * thresholds specified in the parameter list par).
21   * It also excludes those that do not make the minimum
22   * number of channels requirement.
23   * A front end to simpler functions mergeList and finaliseList,
24   *  with code to cover the option of growing objects.
25   */
26
27  int startSize = this->objectList.size();
28
29  if(startSize > 0){
30
31    // make a vector "currentList", which starts as a copy of the Cube's
32    //  objectList, but is the one worked on.
33    vector <Detection> *currentList = new vector <Detection>(startSize);
34    *currentList = this->objectList;
35    this->objectList.clear();
36
37    mergeList(*currentList, this->par);
38
39    // Do growth stuff
40    if(this->par.getFlagGrowth()) {
41      std::cout << "Growing objects...     "<< std::flush;
42      vector <Detection> *newList = new vector <Detection>;
43      printBackSpace(23);
44      std::cout << " Growing object #      "<< std::flush;
45      std::cout.setf(std::ios::left);
46      for(int i=0;i<currentList->size();i++){
47        printBackSpace(6);
48        std::cout << std::setw(6) << i+1 << std::flush;
49        Detection *obj = new Detection;
50        *obj = (*currentList)[i];
51        growObject(*obj,*this);
52        newList->push_back(*obj);
53        delete obj;
54      }
55      delete currentList;
56      currentList = newList;
57      std::cout.unsetf(std::ios::left);
58      printBackSpace(23);
59      std::cout << std::flush;
60
61      // and do the merging again to pick up objects that have
62      //  grown into each other.
63      mergeList(*currentList, this->par);
64    }
65
66    finaliseList(*currentList, this->par);
67
68    this->objectList = *currentList;
69    currentList->clear();
70    delete currentList;
71
72  }
73}
74
75void ObjectMerger(vector<Detection> &objList, Param &par)
76{
77  /**
78   *   A simple front-end to the mergeList() and finaliseList() functions,
79   *    so that if you want to merge a single list, it will
80   *    do both the merging and the cleaning up afterwards.
81   */
82  mergeList(objList, par);
83  finaliseList(objList, par);
84}
85
86void mergeList(vector<Detection> &objList, Param &par)
87{
88  /**
89   *   A function that merges any objects in the list of
90   *    Detections that are within stated threshold distances.
91   *   Determination of whether objects are close is done by
92   *    the function areClose.
93   */
94
95  if(objList.size() > 0){
96
97    bool isVerb = par.isVerbose();
98    vector <Detection>::iterator iter;
99
100    int counter=0, compCounter;
101    while( counter < (objList.size()-1) ){
102      if(isVerb){
103        std::cout.setf(std::ios::right);
104        std::cout << "Progress: " << std::setw(6) << counter+1 << "/" ;
105        std::cout.unsetf(std::ios::right);
106        std::cout.setf(std::ios::left);
107        std::cout << std::setw(6) << objList.size();
108        printBackSpace(23);
109        std::cout << std::flush;
110        std::cout.unsetf(std::ios::left);
111      }
112
113      compCounter = counter + 1;
114
115      do {
116
117        Detection obj1 = objList[counter];
118        Detection obj2 = objList[compCounter];
119
120        bool close = areClose(obj1, obj2, par);
121
122        if(close){
123          obj1 = obj1 + obj2 ;
124          iter = objList.begin() + compCounter;
125          objList.erase(iter);
126          iter = objList.begin() + counter;
127          objList.erase(iter);
128          objList.push_back( obj1 );
129
130          if(isVerb){
131            std::cout.setf(std::ios::right);
132            std::cout << "Progress: "
133                      << std::setw(6) << counter << "/";
134            std::cout.unsetf(std::ios::right);
135            std::cout.setf(std::ios::left);
136            std::cout << std::setw(6) << objList.size();
137            printBackSpace(23);
138            std::cout << std::flush;
139            std::cout.unsetf(std::ios::left);
140          }
141
142          compCounter = counter + 1;
143
144        }
145        else compCounter++;
146
147      } while( (compCounter<objList.size()) );
148
149      counter++;
150
151    }  // end of while(counter<(objList.size()-1)) loop
152  }
153}
154
155
156void finaliseList(vector<Detection> &objList, Param &par)
157{
158  /**
159   *  A function that looks at each object in the Detection vector
160   *    and determines whether is passes the requirements for the
161   *    minimum number of channels and spatial pixels, as provided by
162   *    the Param set par.
163   *   If it does not pass, it is removed from the list.
164   *   In the process, the object parameters are calculated and offsets
165   *    are added.
166   */
167
168  int listCounter = 0;
169  while(listCounter < objList.size()){
170
171    objList[listCounter].setOffsets(par);
172
173    if( (objList[listCounter].hasEnoughChannels(par.getMinChannels()))
174        && (objList[listCounter].getSpatialSize() >= par.getMinPix()) ){
175
176      listCounter++;
177      if(par.isVerbose()){
178        std::cout << "Final total:" << std::setw(5) << listCounter;
179        printSpace(6);
180        printBackSpace(23);
181        std::cout << std::flush;
182      }
183    }     
184    else objList.erase(objList.begin()+listCounter);
185
186  }
187}
188
189
Note: See TracBrowser for help on using the repository browser.