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

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

Mainly changes to improve the execution speed and reliability of the searching:

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