[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 | |
---|
| 32 | using std::vector; |
---|
| 33 | |
---|
[528] | 34 | /// @brief |
---|
| 35 | /// A class to match things pair-wise (useful for sorting). |
---|
| 36 | /// |
---|
| 37 | /// @details This class is deigned to match two quantities to each |
---|
| 38 | /// other. It was devised to find a way of taking a pair of lists |
---|
| 39 | /// that are matched, and sorting one list while keeping the second |
---|
| 40 | /// matched pair-wise. |
---|
| 41 | /// |
---|
| 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... |
---|
[212] | 44 | class Pair |
---|
| 45 | { |
---|
| 46 | public: |
---|
| 47 | Pair(){}; |
---|
| 48 | virtual ~Pair(){}; |
---|
| 49 | friend bool operator< (const Pair& lhs, const Pair& rhs){ |
---|
[528] | 50 | /// A comparison operator for pairs. Compare the primary elements |
---|
| 51 | /// of the two pairs, using the basic < operator. |
---|
[212] | 52 | return (lhs.primary < rhs.primary); |
---|
| 53 | }; |
---|
[221] | 54 | void define(float p, float m){ |
---|
[528] | 55 | /// Basic assignment function. |
---|
[221] | 56 | primary=p; matching=m; |
---|
| 57 | }; |
---|
[212] | 58 | float get1(){return primary;}; |
---|
| 59 | float get2(){return matching;}; |
---|
| 60 | private: |
---|
[258] | 61 | float primary; ///< The main element -- this will be the one that |
---|
| 62 | ///can be compared. |
---|
| 63 | float matching; ///< The secondary element -- this cannot be |
---|
| 64 | ///compared with other objects, it just tracks the |
---|
| 65 | ///primary. |
---|
[212] | 66 | }; |
---|
| 67 | |
---|
[221] | 68 | //====================================================================== |
---|
| 69 | |
---|
[378] | 70 | namespace duchamp |
---|
[3] | 71 | { |
---|
| 72 | |
---|
[378] | 73 | void SortByZ(vector <Detection> &inputList) |
---|
| 74 | { |
---|
[528] | 75 | /// A Function that takes a list of Detections and sorts them in |
---|
| 76 | /// order of increasing z-pixel value. Upon return, the inputList |
---|
| 77 | /// is sorted. |
---|
| 78 | /// |
---|
| 79 | /// We use the std::stable_sort function, so that the order of |
---|
| 80 | /// objects with the same z-value is preserved. |
---|
| 81 | /// \param inputList List of Detections to be sorted. |
---|
| 82 | /// \return The inputList is returned with the elements sorted. |
---|
[378] | 83 | |
---|
| 84 | long size = inputList.size(); |
---|
| 85 | Pair *info = new Pair[size]; |
---|
[3] | 86 | |
---|
[378] | 87 | for(int i=0;i<size;i++) info[i].define(inputList[i].getZcentre(), float(i)); |
---|
[3] | 88 | |
---|
[378] | 89 | std::stable_sort(info,info+size); |
---|
[3] | 90 | |
---|
[378] | 91 | vector <Detection> sorted; |
---|
| 92 | for(int i=0;i<size;i++) sorted.push_back( inputList[int(info[i].get2())] ); |
---|
[3] | 93 | |
---|
[378] | 94 | delete [] info; |
---|
[3] | 95 | |
---|
[378] | 96 | inputList.clear(); |
---|
| 97 | for(int i=0;i<size;i++) inputList.push_back( sorted[i] ); |
---|
| 98 | sorted.clear(); |
---|
[3] | 99 | |
---|
[378] | 100 | } |
---|
[3] | 101 | |
---|
[378] | 102 | //====================================================================== |
---|
[301] | 103 | |
---|
[378] | 104 | void SortByVel(vector <Detection> &inputList) |
---|
| 105 | { |
---|
[528] | 106 | /// @details |
---|
| 107 | /// A Function that takes a list of Detections and sorts them in |
---|
| 108 | /// order of increasing velocity. |
---|
| 109 | /// Every member of the vector needs to have WCS defined, (and if so, |
---|
| 110 | /// then vel is assumed to be defined for all), otherwise no sorting |
---|
| 111 | /// is done. |
---|
| 112 | /// |
---|
| 113 | /// We use the std::stable_sort function, so that the order of |
---|
| 114 | /// objects with the same z-value is preserved. |
---|
| 115 | /// |
---|
| 116 | /// \param inputList List of Detections to be sorted. |
---|
| 117 | /// \return The inputList is returned with the elements sorted, |
---|
| 118 | /// unless the WCS is not good for at least one element, in which |
---|
| 119 | /// case it is returned unaltered. |
---|
[3] | 120 | |
---|
[378] | 121 | bool isGood = true; |
---|
[536] | 122 | for(uint i=0;i<inputList.size();i++) isGood = isGood && inputList[i].isWCS(); |
---|
[3] | 123 | |
---|
[378] | 124 | if(isGood){ |
---|
[3] | 125 | |
---|
[378] | 126 | long size = inputList.size(); |
---|
| 127 | Pair *info = new Pair[size]; |
---|
[212] | 128 | |
---|
[378] | 129 | for(int i=0;i<size;i++) info[i].define(inputList[i].getVel(), float(i)); |
---|
[3] | 130 | |
---|
[378] | 131 | std::stable_sort(info, info+size); |
---|
[3] | 132 | |
---|
[378] | 133 | vector <Detection> sorted; |
---|
| 134 | for(int i=0;i<size;i++) sorted.push_back( inputList[int(info[i].get2())] ); |
---|
[3] | 135 | |
---|
[378] | 136 | delete [] info; |
---|
[3] | 137 | |
---|
[378] | 138 | inputList.clear(); |
---|
| 139 | for(int i=0;i<size;i++) inputList.push_back( sorted[i] ); |
---|
| 140 | sorted.clear(); |
---|
[3] | 141 | |
---|
[378] | 142 | } |
---|
[3] | 143 | |
---|
[378] | 144 | } |
---|
| 145 | |
---|
| 146 | } |
---|