source: tags/release-1.0.5/src/Cubes/Merger.cc @ 1455

Last change on this file since 1455 was 175, checked in by Matthew Whiting, 18 years ago

Introduced some simple inline functions to duchamp.hh to make the printing of progress bars simpler and of a more unified fashion.

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