Skip to content

Commit be5ef8c

Browse files
committed
Clean up and fix ConvertMatrixType
1 parent 8be9d53 commit be5ef8c

File tree

11 files changed

+167
-312
lines changed

11 files changed

+167
-312
lines changed
Lines changed: 72 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,29 @@
11
/*
2-
For more information, please see: http://software.sci.utah.edu
3-
4-
The MIT License
5-
6-
Copyright (c) 2012 Scientific Computing and Imaging Institute,
7-
University of Utah.
8-
9-
License for the specific language governing rights and limitations under
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.
2+
For more information, please see: http://software.sci.utah.edu
3+
4+
The MIT License
5+
6+
Copyright (c) 2012 Scientific Computing and Imaging Institute,
7+
University of Utah.
8+
9+
License for the specific language governing rights and limitations under
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.
2727
*/
2828

2929
#include <Core/Algorithms/Math/ConvertMatrixType.h>
@@ -38,14 +38,13 @@ using namespace SCIRun::Core::Algorithms;
3838
using namespace SCIRun::Core::Algorithms::Math;
3939
using namespace SCIRun::Core::Datatypes;
4040

41+
ALGORITHM_PARAMETER_DEF(Math, OutputMatrixType);
42+
4143
/// @class ConvertMatrixType
4244
/// @image html ConvertMatrixType.png
4345
ConvertMatrixTypeAlgorithm::ConvertMatrixTypeAlgorithm()
4446
{
45-
addParameter(PassThrough(), true);
46-
addParameter(ConvertToColumnMatrix(), false);
47-
addParameter(ConvertToDenseMatrix(), false);
48-
addParameter(ConvertToSparseRowMatrix(), false);
47+
add_option(Parameters::OutputMatrixType, "passThrough", "passThrough|dense|column|sparse");
4948
}
5049

5150
MatrixHandle ConvertMatrixTypeAlgorithm::run(MatrixHandle input_matrix) const
@@ -54,88 +53,86 @@ MatrixHandle ConvertMatrixTypeAlgorithm::run(MatrixHandle input_matrix) const
5453
{
5554
THROW_ALGORITHM_INPUT_ERROR("No input matrix");
5655
}
57-
56+
5857
MatrixHandle omH;
5958
std::ostringstream ostr1,ostr2;
60-
59+
6160
ostr1 << "Dimensions: (" << input_matrix->nrows() << "," << input_matrix->ncols() << ")";
6261
remark(ostr1.str());
63-
62+
6463
if(matrix_is::dense(input_matrix))
6564
{
6665
ostr2 << "Input Matrix Type: DENSE MATRIX";
67-
} else
68-
if (matrix_is::column(input_matrix))
66+
}
67+
else if (matrix_is::column(input_matrix))
6968
{
7069
ostr2 << "Input Matrix Type: COLUMN MATRIX";
71-
} else
72-
if (matrix_is::sparse(input_matrix))
70+
}
71+
else if (matrix_is::sparse(input_matrix))
7372
{
7473
ostr2 << "Input Matrix Type: SPARSE MATRIX";
75-
} else
74+
}
75+
else
7676
{
7777
THROW_ALGORITHM_INPUT_ERROR("Unknown input matrix type");
7878
}
79-
79+
8080
remark(ostr2.str());
81-
82-
if (get(PassThrough()).toBool())
81+
82+
auto outputType = get_option(Parameters::OutputMatrixType);
83+
84+
if ("passThrough" == outputType)
8385
{
84-
return input_matrix;
85-
} else
86-
if (get(ConvertToColumnMatrix()).toBool() && !matrix_is::column(input_matrix))
86+
return input_matrix;
87+
}
88+
else
8789
{
88-
if (input_matrix->ncols()!=1)
89-
{
90-
THROW_ALGORITHM_INPUT_ERROR("Input matrix needs to have a single column to be converted to column matrix type.");
91-
}
92-
DenseColumnMatrixHandle output = matrix_convert::to_column(input_matrix);
93-
if (!output)
90+
if ("column" == outputType && !matrix_is::column(input_matrix))
91+
{
92+
if (input_matrix->ncols()!=1)
93+
{
94+
THROW_ALGORITHM_INPUT_ERROR("Input matrix needs to have a single column to be converted to column matrix type.");
95+
}
96+
DenseColumnMatrixHandle output = matrix_convert::to_column(input_matrix);
97+
if (!output)
9498
{
95-
THROW_ALGORITHM_INPUT_ERROR("Conversion to column matrix failed.");
99+
THROW_ALGORITHM_INPUT_ERROR("Conversion to column matrix failed.");
96100
}
97101
return output;
98-
} else
99-
if (get(ConvertToDenseMatrix()).toBool() && !matrix_is::dense(input_matrix))
100-
{
101-
auto output = matrix_convert::to_dense(input_matrix);
102-
if (!output)
102+
}
103+
else if ("dense" == outputType && !matrix_is::dense(input_matrix))
104+
{
105+
auto output = matrix_convert::to_dense(input_matrix);
106+
if (!output)
103107
{
104-
THROW_ALGORITHM_INPUT_ERROR("Conversion to dense matrix failed.");
108+
THROW_ALGORITHM_INPUT_ERROR("Conversion to dense matrix failed.");
105109
}
106-
return output;
107-
} else
108-
if (get(ConvertToSparseRowMatrix()).toBool() && !matrix_is::sparse(input_matrix))
110+
return output;
111+
}
112+
else if ("sparse" == outputType && !matrix_is::sparse(input_matrix))
109113
{
110-
auto output = matrix_convert::to_sparse(input_matrix);
111-
if (!output)
114+
auto output = matrix_convert::to_sparse(input_matrix);
115+
if (!output)
112116
{
113-
THROW_ALGORITHM_INPUT_ERROR("Conversion to sparse matrix failed.");
117+
THROW_ALGORITHM_INPUT_ERROR("Conversion to sparse matrix failed.");
114118
}
115-
return output;
119+
return output;
116120
}
117121
{
118-
remark(" Datatype unknown or input and output data type are equal. Passing input matrix through. ");
119-
return input_matrix;
122+
remark(" Datatype unknown or input and output data type are equal. Passing input matrix through. ");
123+
return input_matrix;
120124
}
121-
125+
}
122126
}
123127

124-
AlgorithmInputName ConvertMatrixTypeAlgorithm::InputMatrix("InputMatrix");
125-
AlgorithmOutputName ConvertMatrixTypeAlgorithm::ResultMatrix("ResultMatrix");
126-
AlgorithmParameterName ConvertMatrixTypeAlgorithm::PassThrough() { return AlgorithmParameterName("PassThrough"); }
127-
AlgorithmParameterName ConvertMatrixTypeAlgorithm::ConvertToColumnMatrix() { return AlgorithmParameterName("ConvertToColumnMatrix"); }
128-
AlgorithmParameterName ConvertMatrixTypeAlgorithm::ConvertToDenseMatrix() { return AlgorithmParameterName("ConvertToDenseMatrix"); }
129-
AlgorithmParameterName ConvertMatrixTypeAlgorithm::ConvertToSparseRowMatrix() { return AlgorithmParameterName("ConvertToSparseRowMatrix"); }
130-
131128
AlgorithmOutput ConvertMatrixTypeAlgorithm::run_generic(const AlgorithmInput& input) const
132129
{
133130
auto input_matrix = input.get<Matrix>(Variables::InputMatrix);
134131

135132
MatrixHandle output_matrix = run(input_matrix);
136-
133+
137134
AlgorithmOutput output;
138135
output[Variables::ResultMatrix] = output_matrix;
139-
136+
140137
return output;
141138
}

src/Core/Algorithms/Math/ConvertMatrixType.h

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -31,23 +31,18 @@
3131

3232
#include <Core/Datatypes/MatrixFwd.h>
3333
#include <Core/Algorithms/Base/AlgorithmBase.h>
34-
#include <Core/Algorithms/Base/AlgorithmPreconditions.h>
3534
#include <Core/Algorithms/Math/share.h>
3635

3736
namespace SCIRun {
3837
namespace Core {
3938
namespace Algorithms {
4039
namespace Math {
4140

41+
ALGORITHM_PARAMETER_DECL(OutputMatrixType);
42+
4243
class SCISHARE ConvertMatrixTypeAlgorithm : public AlgorithmBase
4344
{
4445
public:
45-
static AlgorithmInputName InputMatrix;
46-
static AlgorithmOutputName ResultMatrix;
47-
static AlgorithmParameterName PassThrough();
48-
static AlgorithmParameterName ConvertToColumnMatrix();
49-
static AlgorithmParameterName ConvertToDenseMatrix();
50-
static AlgorithmParameterName ConvertToSparseRowMatrix();
5146
Datatypes::MatrixHandle run(Datatypes::MatrixHandle input_matrix) const;
5247
ConvertMatrixTypeAlgorithm();
5348
AlgorithmOutput run_generic(const AlgorithmInput& input) const;

0 commit comments

Comments
 (0)