source: trunk/Detection/areClose.cc @ 3

Last change on this file since 3 was 3, checked in by Matthew Whiting, 18 years ago

This is the first full import of all working code to
the Duchamp repository.
Made three directories at top level:

branches/ tags/ trunk/

and trunk/ has the full set of code:
ATrous/ Cubes/ Detection/ InputComplete? InputExample? README Utils/ docs/ mainDuchamp.cc param.cc param.hh

File size: 5.6 KB
Line 
1#include <math.h>
2#include <Detection/detection.hh>
3#include <param.hh>
4
5/**
6 * areClose(Detection &, Detection &, Param &):
7 *   A Function to test whether object1 and object2 are
8 *   within the spatial and velocity thresholds specified
9 *   in the parameter set par.
10 *   Returns true if at least pixel of object1 is close to
11 *   at least one pixel of object2.
12 */
13
14bool areClose(Detection &obj1, Detection &obj2, Param &par)
15{
16 
17  // In each case, for two pixels to be close they need to satisfy *both*
18  // thresholds, hence the && in the definition of "thisOneClose".
19  // For an overall match between the objects, we only require one
20  // matching pair of pixels, hence the || in the "close" definition.
21 
22  bool close = false;
23
24  /* */
25  // Check to see if the objects are nearby.
26  //  .. Only do the pixel-by-pixel comparison if their pixel ranges overlap.
27
28  bool *flagAdj = new bool;
29  *flagAdj = par.getFlagAdjacent();
30  float *threshS = new float;
31  *threshS = par.getThreshS();
32  float *threshV = new float;
33  *threshV = par.getThreshV();
34
35  int *gap = new int;
36  if(*flagAdj) *gap = 1;
37  else *gap = int( ceilf(*threshS) );
38  long *min1 = new long;
39  long *min2 = new long;
40  long *max1 = new long;
41  long *max2 = new long;
42  *min1 = obj1.getXmin();
43  *min2 = obj2.getXmin();
44  *max1 = obj1.getXmax();
45  *max2 = obj2.getXmax();
46  // Test X ranges
47  bool *areNear = new bool;
48  *areNear =
49    ((*min1-*min2+*gap)*(*min1-*max2-*gap) <= 0 ) ||
50    ((*max1-*min2+*gap)*(*max1-*max2-*gap) <= 0 ) ||
51    ((*min2-*min1+*gap)*(*min2-*max1-*gap) <= 0 ) ||
52    ((*max2-*min1+*gap)*(*max2-*max1-*gap) <= 0 ) ;
53
54  // Test Y ranges
55  *min1 = obj1.getYmin();
56  *min2 = obj2.getYmin();
57  *max1 = obj1.getYmax();
58  *max2 = obj2.getYmax();
59  *areNear = *areNear &&
60    ( ((*min1-*min2+*gap)*(*min1-*max2-*gap) <= 0 ) ||
61      ((*max1-*min2+*gap)*(*max1-*max2-*gap) <= 0 ) ||
62      ((*min2-*min1+*gap)*(*min2-*max1-*gap) <= 0 ) ||
63      ((*max2-*min1+*gap)*(*max2-*max1-*gap) <= 0 ) );
64 
65  // Test Z ranges
66  *min1 = obj1.getZmin();
67  *min2 = obj2.getZmin();
68  *max1 = obj1.getZmax();
69  *max2 = obj2.getZmax();
70  *areNear = *areNear &&
71    ( ((*min1-*min2+*threshV)*(*min1-*max2-*threshV) <= 0 ) ||
72      ((*max1-*min2+*threshV)*(*max1-*max2-*threshV) <= 0 ) ||
73      ((*min2-*min1+*threshV)*(*min2-*max1-*threshV) <= 0 ) ||
74      ((*max2-*min1+*threshV)*(*max2-*max1-*threshV) <= 0 ) );
75
76  delete gap;
77  delete min1;
78  delete min2;
79  delete max1;
80  delete max2;
81
82  if(*areNear){
83    // pixel ranges overlap -- so do pixel-by-pixel comparison to make sure.
84    // otherwise close=false, and so don't need to do anything before returning.
85
86    float *first = new float[3];  //just store x,y,z positions of objects.
87    float *second = new float[3];
88    int *counter = new int;
89    int *countermax = new int;
90    int *size2 = new int;
91    *counter = 0;
92    *countermax = obj1.getSize()*obj2.getSize();
93    *size2 = obj2.getSize();
94
95    while(!close && *counter<*countermax ){
96      // run this until we run out of pixels or we find a close pair.
97   
98      first[0]  = obj1.getX(*counter/(*size2));
99      first[1]  = obj1.getY(*counter/(*size2));
100      first[2]  = obj1.getZ(*counter/(*size2));
101      second[0] = obj2.getX(*counter%(*size2));
102      second[1] = obj2.getY(*counter%(*size2));
103      second[2] = obj2.getZ(*counter%(*size2));
104
105      if(*flagAdj){
106        //This step just tests to see if there is a pair of *adjacent* pixels spatially,
107        //   and if the velocity pixels are within the threshold.
108        close = close ||
109          ( (fabsf(first[0]-second[0]) <= 1.)                     //X vals adjacent?
110            && (fabsf(first[1]-second[1]) <= 1.)                  //Y vals adjacent?
111            && (fabsf(first[2]-second[2]) <= *threshV)            //Z vals close?
112            );
113      }
114      else{
115        close = close ||
116          ( (hypot(first[0]-second[0],first[1]-second[1])<=*threshS)
117            && (fabsf(first[2]-second[2]) <= *threshV )
118            );
119      }
120   
121      (*counter)++;
122    }
123
124    delete [] first;
125    delete [] second;
126    delete counter;
127    delete countermax;
128    delete size2;
129
130  }
131
132  delete areNear;
133  delete flagAdj;
134  delete threshS;
135  delete threshV;
136
137  return close;
138
139}
140
141  /*
142//////// OLD STUFF ////////////////////
143
144  for(int i=0; i<obj1.getSize(); i++){
145    Voxel *first = new Voxel;
146    *first = obj1.getPixel(i);
147
148    for(int j=0; j<obj2.getSize(); j++){     
149      Voxel *second = new Voxel;
150      *second = obj2.getPixel(j);
151
152      if(par.getFlagAdjacent()){
153        //This step just tests to see if there is a pair of *adjacent* pixels.
154//      thisOneClose = (abs(first->getX()-second->getX())<=1) &&
155//        (abs(first->getY()-second->getY())<=1) &&
156//        (abs(first->getZ()-second->getZ())<=1);
157        //This step just tests to see if there is a pair of *adjacent* pixels spatially,
158        //   and if the velocity pixels are within the threshold.
159
160        close = close ||
161          ( (abs(first->getX()-second->getX()) <= 1)                     //X vals adjacent?
162            && (abs(first->getY()-second->getY()) <= 1)                  //Y vals adjacent?
163            && (abs(first->getZ()-second->getZ()) <= par.getThreshV())   //Z vals close?
164            );
165      }
166      else{
167        // This tests to see if the pixels are within the spatial and velocity thresholds.
168//      spatialSep = hypot( first->getX() - second->getX(),
169//                          first->getY() - second->getY());
170       
171//      freqSep = abs( first->getZ() - second->getZ() );
172       
173//      thisOneClose = ( spatialSep <= par.getThreshS() ) &&
174//        ( freqSep <= par.getThreshV() );
175
176        close = close ||
177          ( (hypot(first->getX()-second->getX(),first->getY()-second->getY())<=par.getThreshS())
178            && (abs(first->getZ()-second->getZ()) <= par.getThreshV() )
179            );
180      }
181
182      //      close = close || thisOneClose;
183
184      delete second;
185    }
186    delete first;
187  }
188
189    */
190
191
Note: See TracBrowser for help on using the repository browser.