source: tags/release-1.1.5/src/Cubes/Merger.cc @ 1441

Last change on this file since 1441 was 423, checked in by MatthewWhiting, 16 years ago

Fixed bug that meant merging wasn't done for 2D images!

File size: 6.7 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/PixelMap/Object3D.hh>
35#include <duchamp/Cubes/cubes.hh>
36#include <duchamp/Detection/detection.hh>
37#include <duchamp/Utils/utils.hh>
38#include <duchamp/Utils/feedback.hh>
39
40using std::vector;
41using namespace PixelInfo;
42
43namespace duchamp
44{
45
46  void Cube::ObjectMerger()
47  {
48    /**
49     * A Function that takes a Cube's list of Detections and
50     * combines those that are close (according to the
51     * thresholds specified in the parameter list par).
52     * It also excludes those that do not make the minimum
53     * number of channels requirement.
54     * A front end to simpler functions mergeList and finaliseList,
55     *  with code to cover the option of growing objects.
56     */
57
58    int startSize = this->objectList->size();
59
60    if(startSize > 0){
61
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();
67
68      mergeList(currentList, this->par);
69
70      // Do growth stuff
71      if(this->par.getFlagGrowth()) {
72        vector <Detection> newList(currentList.size());
73        for(int i=0;i<currentList.size();i++){
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.
93        mergeList(currentList, this->par);
94      }
95
96      finaliseList(currentList, this->par);
97
98      //     *this->objectList = currentList;
99      this->objectList->resize(currentList.size());
100      for(int i=0;i<currentList.size();i++)
101        this->objectList->at(i) = currentList[i];
102   
103      currentList.clear();
104
105    }
106  }
107
108  void ObjectMerger(vector<Detection> &objList, Param &par)
109  {
110    /**
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     */
115    mergeList(objList, par);
116    finaliseList(objList, par);
117  }
118
119  void mergeList(vector<Detection> &objList, Param &par)
120  {
121    /**
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.
126     */
127
128    if(objList.size() > 0){
129
130      bool isVerb = par.isVerbose();
131      vector <Detection>::iterator iter;
132
133      int counter=0, compCounter;
134      while( counter < (objList.size()-1) ){
135        if(isVerb){
136          std::cout.setf(std::ios::right);
137          std::cout << "Merging: " << std::setw(6) << counter+1 << "/" ;
138          std::cout.unsetf(std::ios::right);
139          std::cout.setf(std::ios::left);
140          std::cout << std::setw(6) << objList.size();
141          printBackSpace(22);
142          std::cout << std::flush;
143          std::cout.unsetf(std::ios::left);
144        }
145
146        compCounter = counter + 1;
147
148        do {
149
150          Detection obj1 = objList[counter];
151          Detection obj2 = objList[compCounter];
152
153          bool close = areClose(obj1, obj2, par);
154
155          if(close){
156            obj1 = obj1 + obj2 ;
157            iter = objList.begin() + compCounter;
158            objList.erase(iter);
159            iter = objList.begin() + counter;
160            objList.erase(iter);
161            objList.push_back( obj1 );
162
163            if(isVerb){
164              std::cout.setf(std::ios::right);
165              std::cout << "Merging: "
166                        << std::setw(6) << counter << "/";
167              std::cout.unsetf(std::ios::right);
168              std::cout.setf(std::ios::left);
169              std::cout << std::setw(6) << objList.size();
170              printBackSpace(22);
171              std::cout << std::flush;
172              std::cout.unsetf(std::ios::left);
173            }
174
175            compCounter = counter + 1;
176
177          }
178          else compCounter++;
179
180        } while( (compCounter<objList.size()) );
181
182        counter++;
183
184      }  // end of while(counter<(objList.size()-1)) loop
185    }
186  }
187
188
189  void finaliseList(vector<Detection> &objList, Param &par)
190  {
191    /**
192     *  A function that looks at each object in the Detection vector
193     *    and determines whether is passes the requirements for the
194     *    minimum number of channels and spatial pixels, as provided by
195     *    the Param set par.
196     *   If it does not pass, it is removed from the list.
197     *   In the process, the object parameters are calculated and offsets
198     *    are added.
199     */
200
201    int listCounter = 0;
202
203    std::cout << "Rejecting:" << std::setw(6) << objList.size();
204    printSpace(6);
205    printBackSpace(22);
206    std::cout << std::flush;
207 
208    while(listCounter < objList.size()){
209
210      objList[listCounter].setOffsets(par);
211
212      if( (objList[listCounter].hasEnoughChannels(par.getMinChannels()))
213          && (objList[listCounter].getSpatialSize() >= par.getMinPix()) ){
214
215        listCounter++;
216
217      }     
218      else{
219     
220        objList.erase(objList.begin()+listCounter);
221        if(par.isVerbose()){
222          std::cout << "Rejecting:" << std::setw(6) << objList.size();
223          printSpace(6);
224          printBackSpace(22);
225          std::cout << std::flush;
226        }
227
228      }
229    }
230  }
231
232
233}
Note: See TracBrowser for help on using the repository browser.