source: trunk/src/Cubes/Merger.cc @ 160

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

Generally tidying up code and adding more useful warning/error messages.
Some improvement of the constructor and save array tasks in cubes.cc,
adding tests for negative sizes and changes in array size.

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