source: branches/pixelmap-refactor-branch/src/Cubes/Merger.cc @ 1441

Last change on this file since 1441 was 558, checked in by MatthewWhiting, 15 years ago

Making a few function calls more explicit in Detection class. Cleaning up some unwanted cerr outputs and commented-out code.

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