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