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

Last change on this file since 269 was 258, checked in by Matthew Whiting, 17 years ago

Merging pixel-map-branch revisions 236:257 back into trunk.
The use of the PixelMap? functions is sorted now, so we put everything back into a uniform location.

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