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

Last change on this file since 272 was 271, checked in by Matthew Whiting, 17 years ago
  • Range of changes to improve the way 2D & 1D fits files are dealt with. FitsHeader? now has an naxis parameter to record the number of axes, and this is used to help defining sizes of arrays and so on.
  • Moved implementations of Stats functions to Statistics.cc
  • Adding use of isVerbose() flags in screen outputs.
  • Some clearing up of unneeded comments.
File size: 5.0 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      vector <Detection> *newList = new vector <Detection>;
42      for(int i=0;i<currentList->size();i++){
43        std::cout.setf(std::ios::right);
44        std::cout << "Growing: " << std::setw(6) << i+1 << "/";   
45        std::cout.unsetf(std::ios::right);
46        std::cout.setf(std::ios::left);
47        std::cout << std::setw(6) << currentList->size() << std::flush;
48        printBackSpace(22);
49        std::cout << std::flush;
50        Detection *obj = new Detection;
51        *obj = (*currentList)[i];
52        growObject(*obj,*this);
53        newList->push_back(*obj);
54        delete obj;
55      }
56      delete currentList;
57      currentList = newList;
58      std::cout.unsetf(std::ios::left);
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 << "Merging: " << 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(22);
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 << "Merging: "
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(22);
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
169  std::cout << "Rejecting:" << std::setw(6) << objList.size();
170  printSpace(6);
171  printBackSpace(22);
172  std::cout << std::flush;
173 
174  while(listCounter < objList.size()){
175
176    objList[listCounter].setOffsets(par);
177
178    if( (objList[listCounter].hasEnoughChannels(par.getMinChannels()))
179        && (objList[listCounter].getSpatialSize() >= par.getMinPix()) ){
180
181      listCounter++;
182
183    }     
184    else{
185     
186      objList.erase(objList.begin()+listCounter);
187      if(par.isVerbose()){
188        std::cout << "Rejecting:" << std::setw(6) << objList.size();
189        printSpace(6);
190        printBackSpace(22);
191        std::cout << std::flush;
192      }
193
194    }
195  }
196}
197
198
Note: See TracBrowser for help on using the repository browser.