source: trunk/src/Detection/areClose.cc @ 321

Last change on this file since 321 was 300, checked in by Matthew Whiting, 17 years ago
  • Fixed a bug that was incorrectly evaluating the integrated flux. It wasn't getting multiplied by the number of spatial pixels, so the values were way off.
  • Also fixed wcsIO.cc so that when a 2D section of a cube is given, the number of axes stored in the fitsHeader class is 2.
  • Changed the full results printing so that the FTOT column is printed as well. The results written to the screen are the same.
  • Some distribution text at the start of files.
File size: 6.9 KB
Line 
1// -----------------------------------------------------------------------
2// areClose.cc: Determine whether two Detections are close enough to
3//              be merged.
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 <math.h>
30#include <Detection/detection.hh>
31#include <PixelMap/Scan.hh>
32#include <PixelMap/Object3D.hh>
33#include <param.hh>
34
35using namespace PixelInfo;
36
37bool areAdj(Object2D &obj1, Object2D &obj2);
38bool areClose(Object2D &obj1, Object2D &obj2, float threshold);
39
40bool areClose(Detection &obj1, Detection &obj2, Param &par)
41{
42
43  /**
44   * A Function to test whether object1 and object2 are within the
45   * spatial and velocity thresholds specified in the parameter set par.
46   * Returns true if at least one pixel of object1 is close to
47   * at least one pixel of object2.
48   */
49
50 
51  bool close = false;   // this will be the value returned
52
53  //
54  // First, check to see if the objects are nearby.  We will only do
55  // the pixel-by-pixel comparison if their pixel ranges overlap.
56  // This saves a bit of time if the objects are big and are nowhere
57  // near one another.
58  //
59
60  bool flagAdj = par.getFlagAdjacent();
61  float threshS = par.getThreshS();
62  float threshV = par.getThreshV();
63
64  long gap;
65  if(flagAdj) gap = 1;
66  else gap = long( ceil(threshS) );
67
68  Scan test1,test2;
69
70  // Test X ranges
71  test1.define(0,obj1.getXmin()-gap,obj1.getXmax()-obj1.getXmin()+2*gap+1);
72  test2.define(0,obj2.getXmin(),obj2.getXmax()-obj2.getXmin()+1);
73  bool areNear = overlap(test1,test2);
74
75  // Test Y ranges
76  test1.define(0,obj1.getYmin()-gap,obj1.getYmax()-obj1.getYmin()+2*gap+1);
77  test2.define(0,obj2.getYmin(),obj2.getYmax()-obj2.getYmin()+1);
78  areNear = areNear && overlap(test1,test2);
79 
80  // Test Z ranges
81  gap = long(ceil(threshV));
82  test1.define(0,obj1.getZmin()-gap,obj1.getZmax()-obj1.getZmin()+2*gap+1);
83  test2.define(0,obj2.getZmin(),obj2.getZmax()-obj2.getZmin()+1);
84  areNear = areNear && overlap(test1,test2);
85//   Scan commonZ = intersect(test1,test2);
86
87  if(areNear){
88    //
89    // If we get to here, the pixel ranges overlap -- so we do a
90    // pixel-by-pixel comparison to make sure they are actually
91    // "close" according to the thresholds.  Otherwise, close=false,
92    // and so don't need to do anything else before returning.
93    //
94
95    long nchan1 = obj1.getNumChannels();
96    long nchan2 = obj2.getNumChannels();
97
98    for(int chanct1=0; (!close && (chanct1<nchan1)); chanct1++){
99      ChanMap map1=obj1.pixels().getChanMap(chanct1);
100//       if(commonZ.isInScan(map1.getZ(),0)){
101       
102        for(int chanct2=0; (!close && (chanct2<nchan2)); chanct2++){
103          ChanMap map2=obj2.pixels().getChanMap(chanct2);
104//        if(commonZ.isInScan(map2.getZ(),0)){
105       
106            if(abs(map1.getZ()-map2.getZ())<=threshV){
107             
108              Object2D temp1 = map1.getObject();
109              Object2D temp2 = map2.getObject();
110             
111              if(flagAdj) gap = 1;
112              else gap = long( ceil(threshS) );
113              test1.define(0, temp1.getXmin()-gap,
114                           temp1.getXmax()-temp1.getXmin()+2*gap+1);
115              test2.define(0, temp2.getXmin(),
116                           temp2.getXmax()-temp2.getXmin()+1);
117              areNear = overlap(test1,test2);
118              test1.define(0, temp1.getYmin()-gap,
119                           temp1.getYmax()-temp1.getYmin()+2*gap+1);
120              test2.define(0, temp2.getYmin(),
121                           temp2.getYmax()-temp2.getYmin()+1);
122              areNear = areNear && overlap(test1,test2);
123             
124              if(areNear){
125                if(flagAdj) close = close || areAdj(temp1,temp2);
126                else close = close || areClose(temp1,temp2,threshS);
127              }
128            }
129//        }
130
131        }
132//       }
133
134    }
135
136  }
137
138  return close;
139
140}
141
142
143bool areClose(Object2D &obj1, Object2D &obj2, float threshold)
144{
145  bool close = false;
146
147  long nscan1 = obj1.getNumScan();
148  long nscan2 = obj2.getNumScan();
149
150  Scan temp1(0, obj1.getYmin()-int(threshold),
151             obj1.getYmax()-obj1.getYmin()+1+2*int(threshold));
152  Scan temp2(0, obj2.getYmin(),obj2.getYmax()-obj2.getYmin()+1);
153  Scan overlap = intersect(temp1,temp2);
154
155  if(overlap.getXlen()>0){
156    overlap.growLeft();
157    overlap.growRight();
158
159    for(int scanct1=0; (!close && (scanct1<nscan1)); scanct1++){
160      temp1 = obj1.getScan(scanct1);
161      if(overlap.isInScan(temp1.getY(),0)){
162        long y1 = temp1.getY();
163
164        for(int scanct2=0; (!close && (scanct2<nscan2)); scanct2++){
165          temp2 = obj2.getScan(scanct2);
166          if(overlap.isInScan(temp2.getY(),0)){
167            long dy = abs(y1 - temp2.getY());
168
169            if(dy<=threshold){
170
171              int gap = int(sqrt(threshold*threshold - dy*dy));
172              Scan temp3(temp2.getY(),temp1.getX()-gap,temp1.getXlen()+2*gap);
173              if(touching(temp3,temp2)) close = true;
174
175            } // end of if(dy<thresh)
176
177          }// if overlap.isIn(temp2)
178        } // end of scanct2 loop
179
180      } // if overlap.isIn(temp1)
181
182    } // end of scanct1 loop
183
184  } //end of if(overlap.getXlen()>0)
185
186  return close;
187}
188
189bool areAdj(Object2D &obj1, Object2D &obj2)
190{
191  bool close = false;
192
193  long nscan1 = obj1.getNumScan();
194  long nscan2 = obj2.getNumScan();
195
196  Scan temp1(0, obj1.getYmin()-1,obj1.getYmax()-obj1.getYmin()+3);
197  Scan temp2(0, obj2.getYmin(),obj2.getYmax()-obj2.getYmin()+1);
198  Scan temp3;
199  Scan commonY = intersect(temp1,temp2);
200  if(commonY.getXlen()>0){
201    commonY.growLeft();
202    commonY.growRight();
203    //    std::cerr << temp1 << " " << temp2 << " " << commonY << "\n";
204
205    for(int scanct1=0;(!close && scanct1 < nscan1);scanct1++){
206      temp1 = obj1.getScan(scanct1);
207      if(commonY.isInScan(temp1.getY(),0)){
208        long y1 = temp1.getY();
209
210        for(int scanct2=0; (!close && scanct2 < nscan2); scanct2++){
211          temp2 = obj2.getScan(scanct2);
212          if(commonY.isInScan(temp2.getY(),0)){     
213            long dy = abs(y1 - temp2.getY());
214
215            if(dy<= 1){
216
217              temp3.define(temp2.getY(),temp1.getX(),temp1.getXlen());
218              if(touching(temp3,temp2)) close = true;
219
220            }
221          }
222        } // end of for loop over scanct2
223     
224      }
225     
226    } // end of for loop over scanct1
227
228  }
229  return close;
230}
Note: See TracBrowser for help on using the repository browser.