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

Last change on this file since 943 was 943, checked in by MatthewWhiting, 12 years ago

Changing minChannels and minPix to unsigned int. Making sure Cubes/Merger?.cc (which makes use of their accessors) actually includes param.hh

File size: 7.3 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 <duchamp/param.hh>
35#include <duchamp/PixelMap/Object3D.hh>
36#include <duchamp/Cubes/cubes.hh>
37#include <duchamp/Cubes/cubeUtils.hh>
38#include <duchamp/Detection/detection.hh>
39#include <duchamp/Detection/ObjectGrower.hh>
40#include <duchamp/Utils/utils.hh>
41#include <duchamp/Utils/feedback.hh>
42
43using std::vector;
44using namespace PixelInfo;
45
46namespace duchamp
47{
48
49  void Cube::ObjectMerger()
50  {
51    /// @details
52    /// A Function that takes a Cube's list of Detections and
53    /// combines those that are close (according to the
54    /// thresholds specified in the parameter list par).
55    /// It also excludes those that do not make the minimum
56    /// number of channels requirement.
57    /// A front end to simpler functions mergeList and finaliseList,
58    ///  with code to cover the option of growing objects.
59
60    int startSize = this->objectList->size();
61
62    if(startSize > 0){
63
64      // make a vector "currentList", which starts as a copy of the Cube's
65      //  objectList, but is the one worked on.
66      vector <Detection> currentList(startSize);
67      for(int i=0;i<startSize;i++) currentList[i] = this->objectList->at(i);
68      this->objectList->clear();
69
70      if(this->par.getFlagRejectBeforeMerge())
71        finaliseList(currentList, this->par);
72
73      mergeList(currentList, this->par);
74
75      // Do growth stuff
76      if(this->par.getFlagGrowth()) {
77        ObjectGrower grower;
78        grower.define(this);
79        for(size_t i=0;i<currentList.size();i++){
80          if(this->par.isVerbose()){
81            std::cout.setf(std::ios::right);
82            std::cout << "Growing: " << std::setw(6) << i+1 << "/";       
83            std::cout.unsetf(std::ios::right);
84            std::cout.setf(std::ios::left);
85            std::cout << std::setw(6) << currentList.size() << std::flush;
86            printBackSpace(22);
87            std::cout << std::flush;
88          }
89          grower.grow(&currentList[i]);
90        }
91        std::cout.unsetf(std::ios::left);
92
93        // and do the merging again to pick up objects that have
94        //  grown into each other.
95        mergeList(currentList, this->par);
96      }
97
98      if(!this->par.getFlagRejectBeforeMerge())
99        finaliseList(currentList, this->par);
100
101      //     *this->objectList = currentList;
102      this->objectList->resize(currentList.size());
103      for(size_t i=0;i<currentList.size();i++)
104        this->objectList->at(i) = currentList[i];
105   
106      currentList.clear();
107
108    }
109  }
110
111  void ObjectMerger(vector<Detection> &objList, Param &par)
112  {
113    /// @details
114    ///   A simple front-end to the mergeList() and finaliseList() functions,
115    ///    so that if you want to merge a single list, it will
116    ///    do both the merging and the cleaning up afterwards.
117
118    mergeList(objList, par);
119    finaliseList(objList, par);
120  }
121
122  void mergeList(vector<Detection> &objList, Param &par)
123  {
124    /// @details
125    ///   A function that merges any objects in the list of
126    ///    Detections that are within stated threshold distances.
127    ///   Determination of whether objects are close is done by
128    ///    the function areClose.
129
130    if(objList.size() > 0){
131
132      bool isVerb = par.isVerbose();
133      vector <Detection>::iterator iter;
134     
135      std::vector<bool> isValid(objList.size(),true);
136      int numRemoved=0;
137
138      size_t counter=0, compCounter,goodCounter=0;
139
140      while( counter < (objList.size()-1) ){
141        if(isVerb){
142          std::cout.setf(std::ios::right);
143          std::cout << "Merging: " << std::setw(6) << goodCounter+1 << "/" ;
144          std::cout.unsetf(std::ios::right);
145          std::cout.setf(std::ios::left);
146          std::cout << std::setw(6) << objList.size()-numRemoved;
147          printBackSpace(22);
148          std::cout << std::flush;
149          std::cout.unsetf(std::ios::left);
150        }
151
152        if(isValid[counter]){
153
154          compCounter = counter + 1;
155
156          do {
157
158            if(isValid[compCounter]){
159
160              bool close = objList[counter].canMerge(objList[compCounter], par);
161
162              if(close){
163                objList[counter].addDetection(objList[compCounter]);
164                isValid[compCounter]=false;
165                numRemoved++;
166
167                if(isVerb){
168                  std::cout.setf(std::ios::right);
169                  std::cout << "Merging: "
170                            << std::setw(6) << goodCounter+1 << "/";
171                  std::cout.unsetf(std::ios::right);
172                  std::cout.setf(std::ios::left);
173                  std::cout << std::setw(6) << objList.size()-numRemoved;
174                  printBackSpace(22);
175                  std::cout << std::flush;
176                  std::cout.unsetf(std::ios::left);
177                }
178               
179                compCounter = counter + 1;
180               
181              }
182              else compCounter++;
183            }
184            else compCounter++;
185
186          } while( (compCounter<objList.size()) );
187
188        }
189        counter++;
190        if(isValid[counter]) goodCounter++;
191
192      }  // end of while(counter<(objList.size()-1)) loop
193
194      std::vector<Detection> newlist(objList.size()-numRemoved);
195      size_t ct=0;
196      for(size_t i=0;i<objList.size();i++){
197        if(isValid[i]) newlist[ct++]=objList[i];
198      }
199      objList.clear();
200      objList=newlist;
201
202    }
203  }
204
205
206  void finaliseList(vector<Detection> &objList, Param &par)
207  {
208    /// @details
209    ///  A function that looks at each object in the Detection vector
210    ///    and determines whether is passes the requirements for the
211    ///    minimum number of channels and spatial pixels, as provided by
212    ///    the Param set par.
213    ///   If it does not pass, it is removed from the list.
214    ///   In the process, the offsets are set.
215
216    if(par.isVerbose()){
217      std::cout << "Rejecting:" << std::setw(6) << objList.size();
218      printSpace(6);
219      printBackSpace(22);
220      std::cout << std::flush;
221    }
222
223    std::vector<Detection> newlist;
224   
225    std::vector<Detection>::iterator obj = objList.begin();
226    int numRej=0;
227    for(;obj<objList.end();obj++){
228      obj->setOffsets(par);
229     
230      if( (obj->hasEnoughChannels(par.getMinChannels()))
231          && (obj->getSpatialSize() >= par.getMinPix())
232          && (obj->getSize() >= par.getMinVoxels() ) ){
233
234        newlist.push_back(*obj);
235
236      }     
237      else{
238        numRej++;
239        if(par.isVerbose()){
240          std::cout << "Rejecting:" << std::setw(6) << objList.size()-numRej;
241          printSpace(6);
242          printBackSpace(22);
243          std::cout << std::flush;
244        }
245
246      }
247    }
248
249    objList.clear();
250    objList = newlist;
251
252  }
253
254
255}
Note: See TracBrowser for help on using the repository browser.