|
| 1 | +/* |
| 2 | + For more information, please see: http://software.sci.utah.edu |
| 3 | +
|
| 4 | + The MIT License |
| 5 | +
|
| 6 | + Copyright (c) 2009 Scientific Computing and Imaging Institute, |
| 7 | + University of Utah. |
| 8 | +
|
| 9 | + |
| 10 | + Permission is hereby granted, free of charge, to any person obtaining a |
| 11 | + copy of this software and associated documentation files (the "Software"), |
| 12 | + to deal in the Software without restriction, including without limitation |
| 13 | + the rights to use, copy, modify, merge, publish, distribute, sublicense, |
| 14 | + and/or sell copies of the Software, and to permit persons to whom the |
| 15 | + Software is furnished to do so, subject to the following conditions: |
| 16 | +
|
| 17 | + The above copyright notice and this permission notice shall be included |
| 18 | + in all copies or substantial portions of the Software. |
| 19 | +
|
| 20 | + THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS |
| 21 | + OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 22 | + FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL |
| 23 | + THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 24 | + LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING |
| 25 | + FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER |
| 26 | + DEALINGS IN THE SOFTWARE. |
| 27 | +*/ |
| 28 | + |
| 29 | +#include <Core/Algorithms/Math/SortMatrixAlgo.h> |
| 30 | +#include <Core/Datatypes/MatrixTypeConversions.h> |
| 31 | +#include <Core/Math/MiscMath.h> |
| 32 | + |
| 33 | +using namespace SCIRun; |
| 34 | +using namespace SCIRun::Core::Datatypes; |
| 35 | +using namespace SCIRun::Core::Algorithms; |
| 36 | +using namespace SCIRun::Core::Algorithms::Math; |
| 37 | + |
| 38 | +SortMatrixAlgo::SortMatrixAlgo() |
| 39 | +{ |
| 40 | + //set parameter defaults for UI |
| 41 | + addParameter(Variables::Method, 0); |
| 42 | +} |
| 43 | + |
| 44 | + |
| 45 | +AlgorithmOutput SortMatrixAlgo::run_generic(const AlgorithmInput& input) const |
| 46 | +{ |
| 47 | + auto input_matrix = input.get<Matrix>(Variables::InputMatrix); |
| 48 | + AlgorithmOutput output; |
| 49 | + |
| 50 | + //sparse support not fully implemented yet. |
| 51 | + if (!matrix_is::dense(input_matrix)) |
| 52 | + { |
| 53 | + //TODO implement something with sparse |
| 54 | + error("SortMatrix: Currently only works with dense matrices"); |
| 55 | + output[Variables::OutputMatrix] = 0; |
| 56 | + return output; |
| 57 | + } |
| 58 | + auto mat = matrix_cast::as_dense (input_matrix); |
| 59 | + DenseMatrixHandle return_matrix; |
| 60 | + |
| 61 | + //pull parameter from UI |
| 62 | + auto method = get(Variables::Method).toInt(); |
| 63 | + |
| 64 | + Sort(mat,return_matrix,method); |
| 65 | + output[Variables::OutputMatrix] = return_matrix; |
| 66 | + return output; |
| 67 | +} |
| 68 | + |
| 69 | + |
| 70 | +bool |
| 71 | +SortMatrixAlgo::Sort(DenseMatrixHandle input, DenseMatrixHandle& output,int method) const |
| 72 | +{ |
| 73 | + if (!input) |
| 74 | + { |
| 75 | + error("SortAscending: no input matrix found"); |
| 76 | + return false; |
| 77 | + } |
| 78 | + //get size of original matrix |
| 79 | + size_type nrows = input->nrows(); |
| 80 | + size_type ncols = input->ncols(); |
| 81 | + //copy original matrix for processing |
| 82 | + output.reset(new DenseMatrix(*input)); |
| 83 | + //pointer to matrix data |
| 84 | + double *data = output->data(); |
| 85 | + |
| 86 | + if (!output) |
| 87 | + { |
| 88 | + error("ApplyRowOperation: could not create output matrix"); |
| 89 | + return false; |
| 90 | + } |
| 91 | + |
| 92 | + size_type n = nrows*ncols; |
| 93 | + //call the sorting functions |
| 94 | + Quicksort(data,0,n-1); |
| 95 | + |
| 96 | + if (method==1) |
| 97 | + { |
| 98 | + //if set to descending, reverse the order. |
| 99 | + output.reset(new DenseMatrix(output -> reverse())); |
| 100 | + } |
| 101 | + return true; |
| 102 | +} |
| 103 | + |
| 104 | +bool |
| 105 | +SortMatrixAlgo::Quicksort(double* input, index_type lo, index_type hi) const |
| 106 | +{ |
| 107 | + //splits matrix based on Partition function |
| 108 | + index_type ind; |
| 109 | + if (lo<hi) |
| 110 | + { |
| 111 | + ind=Partition(input,lo,hi); |
| 112 | + Quicksort(input,lo,ind-1); |
| 113 | + Quicksort(input,ind+1,hi); |
| 114 | + } |
| 115 | + return true; |
| 116 | +} |
| 117 | + |
| 118 | +index_type |
| 119 | +SortMatrixAlgo::Partition(double* input, index_type lo, index_type hi) const |
| 120 | +{ |
| 121 | + // places the last entry in its proper place in relation to the other |
| 122 | + // entries, ie, smaller values before and larger values after. |
| 123 | + index_type ind=lo; |
| 124 | + |
| 125 | + double pivot = input[hi]; |
| 126 | + double tmp; |
| 127 | + for (index_type k=lo;k<hi;k++) |
| 128 | + { |
| 129 | + if (input[k]<=pivot) |
| 130 | + { |
| 131 | + tmp=input[ind]; |
| 132 | + input[ind]=input[k]; |
| 133 | + input[k]=tmp; |
| 134 | + ind+=1; |
| 135 | + } |
| 136 | + } |
| 137 | + tmp=input[ind]; |
| 138 | + input[ind]=input[hi]; |
| 139 | + input[hi]=tmp; |
| 140 | + return ind; |
| 141 | +} |
| 142 | + |
| 143 | + |
| 144 | + |
| 145 | + |
| 146 | + |
0 commit comments