source: tags/release-1.1/src/Cubes/Merger.cc @ 1323

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

Adding distribution text at the start of each file...

File size: 6.5 KB
Line 
1// -----------------------------------------------------------------------
2// Merger.cc: Merging a list of Detections, and rejecting on the basis
3//            of number of channels or pixels.
4// -----------------------------------------------------------------------
5// Copyright (C) 2006, Matthew Whiting, ATNF
6//
7// This program is free software; you can redistribute it and/or modify it
8// under the terms of the GNU General Public License as published by the
9// Free Software Foundation; either version 2 of the License, or (at your
10// option) any later version.
11//
12// Duchamp is distributed in the hope that it will be useful, but WITHOUT
13// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14// FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
15// for more details.
16//
17// You should have received a copy of the GNU General Public License
18// along with Duchamp; if not, write to the Free Software Foundation,
19// Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA
20//
21// Correspondence concerning Duchamp may be directed to:
22//    Internet email: Matthew.Whiting [at] atnf.csiro.au
23//    Postal address: Dr. Matthew Whiting
24//                    Australia Telescope National Facility, CSIRO
25//                    PO Box 76
26//                    Epping NSW 1710
27//                    AUSTRALIA
28// -----------------------------------------------------------------------
29#include <iostream>
30#include <fstream>
31#include <iomanip>
32#include <math.h>
33#include <vector>
34#include <PixelMap/Object3D.hh>
35#include <Cubes/cubes.hh>
36#include <Detection/detection.hh>
37#include <Utils/utils.hh>
38#include <Utils/feedback.hh>
39
40using std::vector;
41using namespace PixelInfo;
42
43void Cube::ObjectMerger()
44{
45  /**
46   * A Function that takes a Cube's list of Detections and
47   * combines those that are close (according to the
48   * thresholds specified in the parameter list par).
49   * It also excludes those that do not make the minimum
50   * number of channels requirement.
51   * A front end to simpler functions mergeList and finaliseList,
52   *  with code to cover the option of growing objects.
53   */
54
55  int startSize = this->objectList->size();
56
57  if(startSize > 0){
58
59    // make a vector "currentList", which starts as a copy of the Cube's
60    //  objectList, but is the one worked on.
61    vector <Detection> currentList(startSize);
62    for(int i=0;i<startSize;i++) currentList[i] = this->objectList->at(i);
63    this->objectList->clear();
64
65    mergeList(currentList, this->par);
66
67    // Do growth stuff
68    if(this->par.getFlagGrowth()) {
69      vector <Detection> newList(currentList.size());
70      for(int i=0;i<currentList.size();i++){
71        std::cout.setf(std::ios::right);
72        std::cout << "Growing: " << std::setw(6) << i+1 << "/";   
73        std::cout.unsetf(std::ios::right);
74        std::cout.setf(std::ios::left);
75        std::cout << std::setw(6) << currentList.size() << std::flush;
76        printBackSpace(22);
77        std::cout << std::flush;
78        Detection *obj = new Detection;
79        *obj = currentList[i];
80        growObject(*obj,*this);
81        newList[i] = *obj;
82        delete obj;
83      }
84      currentList.clear();
85      currentList = newList;
86      std::cout.unsetf(std::ios::left);
87
88      // and do the merging again to pick up objects that have
89      //  grown into each other.
90      mergeList(currentList, this->par);
91    }
92
93    finaliseList(currentList, this->par);
94
95//     *this->objectList = currentList;
96    this->objectList->resize(currentList.size());
97    for(int i=0;i<currentList.size();i++)
98      this->objectList->at(i) = currentList[i];
99   
100    currentList.clear();
101
102  }
103}
104
105void ObjectMerger(vector<Detection> &objList, Param &par)
106{
107  /**
108   *   A simple front-end to the mergeList() and finaliseList() functions,
109   *    so that if you want to merge a single list, it will
110   *    do both the merging and the cleaning up afterwards.
111   */
112  mergeList(objList, par);
113  finaliseList(objList, par);
114}
115
116void mergeList(vector<Detection> &objList, Param &par)
117{
118  /**
119   *   A function that merges any objects in the list of
120   *    Detections that are within stated threshold distances.
121   *   Determination of whether objects are close is done by
122   *    the function areClose.
123   */
124
125  if(objList.size() > 0){
126
127    bool isVerb = par.isVerbose();
128    vector <Detection>::iterator iter;
129
130    int counter=0, compCounter;
131    while( counter < (objList.size()-1) ){
132      if(isVerb){
133        std::cout.setf(std::ios::right);
134        std::cout << "Merging: " << std::setw(6) << counter+1 << "/" ;
135        std::cout.unsetf(std::ios::right);
136        std::cout.setf(std::ios::left);
137        std::cout << std::setw(6) << objList.size();
138        printBackSpace(22);
139        std::cout << std::flush;
140        std::cout.unsetf(std::ios::left);
141      }
142
143      compCounter = counter + 1;
144
145      do {
146
147        Detection obj1 = objList[counter];
148        Detection obj2 = objList[compCounter];
149
150        bool close = areClose(obj1, obj2, par);
151
152        if(close){
153          obj1 = obj1 + obj2 ;
154          iter = objList.begin() + compCounter;
155          objList.erase(iter);
156          iter = objList.begin() + counter;
157          objList.erase(iter);
158          objList.push_back( obj1 );
159
160          if(isVerb){
161            std::cout.setf(std::ios::right);
162            std::cout << "Merging: "
163                      << std::setw(6) << counter << "/";
164            std::cout.unsetf(std::ios::right);
165            std::cout.setf(std::ios::left);
166            std::cout << std::setw(6) << objList.size();
167            printBackSpace(22);
168            std::cout << std::flush;
169            std::cout.unsetf(std::ios::left);
170          }
171
172          compCounter = counter + 1;
173
174        }
175        else compCounter++;
176
177      } while( (compCounter<objList.size()) );
178
179      counter++;
180
181    }  // end of while(counter<(objList.size()-1)) loop
182  }
183}
184
185
186void finaliseList(vector<Detection> &objList, Param &par)
187{
188  /**
189   *  A function that looks at each object in the Detection vector
190   *    and determines whether is passes the requirements for the
191   *    minimum number of channels and spatial pixels, as provided by
192   *    the Param set par.
193   *   If it does not pass, it is removed from the list.
194   *   In the process, the object parameters are calculated and offsets
195   *    are added.
196   */
197
198  int listCounter = 0;
199
200  std::cout << "Rejecting:" << std::setw(6) << objList.size();
201  printSpace(6);
202  printBackSpace(22);
203  std::cout << std::flush;
204 
205  while(listCounter < objList.size()){
206
207    objList[listCounter].setOffsets(par);
208
209    if( (objList[listCounter].hasEnoughChannels(par.getMinChannels()))
210        && (objList[listCounter].getSpatialSize() >= par.getMinPix()) ){
211
212      listCounter++;
213
214    }     
215    else{
216     
217      objList.erase(objList.begin()+listCounter);
218      if(par.isVerbose()){
219        std::cout << "Rejecting:" << std::setw(6) << objList.size();
220        printSpace(6);
221        printBackSpace(22);
222        std::cout << std::flush;
223      }
224
225    }
226  }
227}
228
229
Note: See TracBrowser for help on using the repository browser.