source: tags/release-1.1.2/src/Detection/sorting.cc @ 1391

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

Fixed up headers for trunk as well.

File size: 4.9 KB
RevLine 
[301]1// -----------------------------------------------------------------------
2// sorting.cc: Sort the list of Detections by channel or velocity.
3// -----------------------------------------------------------------------
4// Copyright (C) 2006, Matthew Whiting, ATNF
5//
6// This program is free software; you can redistribute it and/or modify it
7// under the terms of the GNU General Public License as published by the
8// Free Software Foundation; either version 2 of the License, or (at your
9// option) any later version.
10//
11// Duchamp is distributed in the hope that it will be useful, but WITHOUT
12// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13// FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
14// for more details.
15//
16// You should have received a copy of the GNU General Public License
17// along with Duchamp; if not, write to the Free Software Foundation,
18// Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA
19//
20// Correspondence concerning Duchamp may be directed to:
21//    Internet email: Matthew.Whiting [at] atnf.csiro.au
22//    Postal address: Dr. Matthew Whiting
23//                    Australia Telescope National Facility, CSIRO
24//                    PO Box 76
25//                    Epping NSW 1710
26//                    AUSTRALIA
27// -----------------------------------------------------------------------
[3]28#include <vector>
[212]29#include <algorithm>
[393]30#include <duchamp/Detection/detection.hh>
[3]31
32using std::vector;
33
[221]34/**
35 * A class to match things pair-wise (useful for sorting).
36 *
[258]37 * This class is deigned to match two quantities to each other.  It
38 * was devised to find a way of taking a pair of lists that are
39 * matched, and sorting one list while keeping the second matched
40 * pair-wise.
[221]41 *
[258]42 * The elements are currently just assumed to be floats. This could be
43 * extended by templating, but at this stage we don't need to...
[221]44 */
[212]45class Pair
46{
47public:
48  Pair(){};
49  virtual ~Pair(){};
50  friend bool operator< (const Pair& lhs, const Pair& rhs){
[221]51    /**
[258]52     *  A comparison operator for pairs.  Compare the primary elements
53     *  of the two pairs, using the basic < operator.
[221]54     */
[212]55    return (lhs.primary < rhs.primary);
56  };
[221]57  void define(float p, float m){
58    /** Basic assignment function. */
59    primary=p; matching=m;
60  };
[212]61  float get1(){return primary;};
62  float get2(){return matching;};
63private:
[258]64  float primary;  ///< The main element -- this will be the one that
65                  ///can be compared.
66  float matching; ///< The secondary element -- this cannot be
67                  ///compared with other objects, it just tracks the
68                  ///primary.
[212]69};
70
[221]71//======================================================================
72
[378]73namespace duchamp
[3]74{
75
[378]76  void SortByZ(vector <Detection> &inputList)
77  {
78    /**
79     * A Function that takes a list of Detections and sorts them in
80     * order of increasing z-pixel value.  Upon return, the inputList
81     * is sorted.
82     *
83     * We use the std::stable_sort function, so that the order of
84     * objects with the same z-value is preserved.
85     * \param inputList List of Detections to be sorted.
86     * \return The inputList is returned with the elements sorted.
87     */
88
89    long size = inputList.size();
90    Pair *info = new Pair[size];
[3]91 
[378]92    for(int i=0;i<size;i++) info[i].define(inputList[i].getZcentre(), float(i));
[3]93
[378]94    std::stable_sort(info,info+size);
[3]95 
[378]96    vector <Detection> sorted;
97    for(int i=0;i<size;i++) sorted.push_back( inputList[int(info[i].get2())] );
[3]98
[378]99    delete [] info;
[3]100
[378]101    inputList.clear();
102    for(int i=0;i<size;i++) inputList.push_back( sorted[i] );
103    sorted.clear();
[3]104 
[378]105  }
[3]106
[378]107  //======================================================================
[301]108
[378]109  void SortByVel(vector <Detection> &inputList)
110  {
111    /**
112     * A Function that takes a list of Detections and sorts them in
113     *  order of increasing velocity.
114     * Every member of the vector needs to have WCS defined, (and if so,
115     *   then vel is assumed to be defined for all), otherwise no sorting
116     *   is done.
117     *
118     * We use the std::stable_sort function, so that the order of
119     * objects with the same z-value is preserved.
120     *
121     * \param inputList List of Detections to be sorted.
122     * \return The inputList is returned with the elements sorted,
123     * unless the WCS is not good for at least one element, in which
124     * case it is returned unaltered.
125     */
[3]126
[378]127    bool isGood = true;
128    for(int i=0;i<inputList.size();i++) isGood = isGood && inputList[i].isWCS();
[3]129
[378]130    if(isGood){
[3]131
[378]132      long size = inputList.size();
133      Pair *info = new Pair[size];
[212]134   
[378]135      for(int i=0;i<size;i++) info[i].define(inputList[i].getVel(), float(i));
[3]136
[378]137      std::stable_sort(info, info+size);
[3]138 
[378]139      vector <Detection> sorted;
140      for(int i=0;i<size;i++) sorted.push_back( inputList[int(info[i].get2())] );
[3]141
[378]142      delete [] info;
[3]143
[378]144      inputList.clear();
145      for(int i=0;i<size;i++) inputList.push_back( sorted[i] );
146      sorted.clear();
[3]147 
[378]148    }
[3]149
[378]150  } 
151
152}
Note: See TracBrowser for help on using the repository browser.