source: tags/release-1.1.2/src/Detection/areClose.cc

Last change on this file was 393, checked in by MatthewWhiting, 17 years ago

Fixed up headers for trunk as well.

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