source: trunk/src/Detection/sorting.cc @ 258

Last change on this file since 258 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: 3.3 KB
Line 
1#include <vector>
2#include <algorithm>
3#include <Detection/detection.hh>
4
5using std::vector;
6
7/**
8 * A class to match things pair-wise (useful for sorting).
9 *
10 * This class is deigned to match two quantities to each other.  It
11 * was devised to find a way of taking a pair of lists that are
12 * matched, and sorting one list while keeping the second matched
13 * pair-wise.
14 *
15 * The elements are currently just assumed to be floats. This could be
16 * extended by templating, but at this stage we don't need to...
17 */
18class Pair
19{
20public:
21  Pair(){};
22  virtual ~Pair(){};
23  friend bool operator< (const Pair& lhs, const Pair& rhs){
24    /**
25     *  A comparison operator for pairs.  Compare the primary elements
26     *  of the two pairs, using the basic < operator.
27     */
28    return (lhs.primary < rhs.primary);
29  };
30  void define(float p, float m){
31    /** Basic assignment function. */
32    primary=p; matching=m;
33  };
34  float get1(){return primary;};
35  float get2(){return matching;};
36private:
37  float primary;  ///< The main element -- this will be the one that
38                  ///can be compared.
39  float matching; ///< The secondary element -- this cannot be
40                  ///compared with other objects, it just tracks the
41                  ///primary.
42};
43
44//======================================================================
45
46
47void SortByZ(vector <Detection> &inputList)
48{
49  /**
50   * A Function that takes a list of Detections and sorts them in
51   * order of increasing z-pixel value.  Upon return, the inputList
52   * is sorted.
53   *
54   * We use the std::stable_sort function, so that the order of
55   * objects with the same z-value is preserved.
56   * \param inputList List of Detections to be sorted.
57   * \return The inputList is returned with the elements sorted.
58   */
59
60  long size = inputList.size();
61  Pair *info = new Pair[size];
62 
63  for(int i=0;i<size;i++) info[i].define(inputList[i].getZcentre(), float(i));
64
65  std::stable_sort(info,info+size);
66 
67  vector <Detection> sorted;
68  for(int i=0;i<size;i++) sorted.push_back( inputList[int(info[i].get2())] );
69
70  delete [] info;
71
72  inputList.clear();
73  for(int i=0;i<size;i++) inputList.push_back( sorted[i] );
74  sorted.clear();
75 
76}
77
78void SortByVel(vector <Detection> &inputList)
79{
80  /**
81   * A Function that takes a list of Detections and sorts them in
82   *  order of increasing velocity.
83   * Every member of the vector needs to have WCS defined, (and if so,
84   *   then vel is assumed to be defined for all), otherwise no sorting
85   *   is done.
86   *
87   * We use the std::stable_sort function, so that the order of
88   * objects with the same z-value is preserved.
89   *
90   * \param inputList List of Detections to be sorted.
91   * \return The inputList is returned with the elements sorted,
92   * unless the WCS is not good for at least one element, in which
93   * case it is returned unaltered.
94   */
95
96  bool isGood = true;
97  for(int i=0;i<inputList.size();i++) isGood = isGood && inputList[i].isWCS();
98
99  if(isGood){
100
101    long size = inputList.size();
102    Pair *info = new Pair[size];
103   
104    for(int i=0;i<size;i++) info[i].define(inputList[i].getVel(), float(i));
105
106    std::stable_sort(info, info+size);
107 
108    vector <Detection> sorted;
109    for(int i=0;i<size;i++) sorted.push_back( inputList[int(info[i].get2())] );
110
111    delete [] info;
112
113    inputList.clear();
114    for(int i=0;i<size;i++) inputList.push_back( sorted[i] );
115    sorted.clear();
116 
117  }
118
119
Note: See TracBrowser for help on using the repository browser.