|
| 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/Legacy/Fields/FieldData/ConvertIndicesToFieldDataAlgo.h> |
| 30 | +#include <Core/Datatypes/Legacy/Field/Field.h> |
| 31 | +#include <Core/Datatypes/Legacy/Field/VField.h> |
| 32 | +#include <Core/Datatypes/Matrix.h> |
| 33 | +#include <Core/Datatypes/MatrixTypeConversions.h> |
| 34 | +#include <Core/Datatypes/SparseRowMatrix.h> |
| 35 | +#include <Core/Datatypes/DatatypeFwd.h> |
| 36 | +#include <Core/Datatypes/Legacy/Field/FieldInformation.h> |
| 37 | +#include <Core/Datatypes/DenseMatrix.h> |
| 38 | + |
| 39 | +#include <Core/Algorithms/Base/AlgorithmVariableNames.h> |
| 40 | +#include <Core/Algorithms/Base/AlgorithmPreconditions.h> |
| 41 | + |
| 42 | +using namespace SCIRun::Core::Algorithms; |
| 43 | +using namespace SCIRun::Core::Geometry; |
| 44 | +using namespace SCIRun::Core::Algorithms::Fields; |
| 45 | +using namespace SCIRun::Core::Algorithms::Fields::Parameters; |
| 46 | +using namespace SCIRun::Core::Datatypes; |
| 47 | +using namespace SCIRun; |
| 48 | + |
| 49 | +ALGORITHM_PARAMETER_DEF(Fields, OutputFieldDataType); |
| 50 | + |
| 51 | +ConvertIndicesToFieldDataAlgo::ConvertIndicesToFieldDataAlgo() |
| 52 | +{ |
| 53 | + add_option(Parameters::OutputFieldDataType, "double","double|float|char|unsigned char|short|unsigned short|int|unsigned int"); |
| 54 | +} |
| 55 | + |
| 56 | +bool |
| 57 | +ConvertIndicesToFieldDataAlgo::runImpl(FieldHandle input_field, DenseMatrixHandle input_matrix, FieldHandle& output_field) const |
| 58 | +{ |
| 59 | + ScopedAlgorithmStatusReporter r(this, "ConvertIndicesToFieldData"); |
| 60 | + |
| 61 | + if (!input_field) |
| 62 | + { |
| 63 | + error("No input field"); |
| 64 | + return (false); |
| 65 | + } |
| 66 | + |
| 67 | + FieldInformation fi(input_field); |
| 68 | + output_field = CreateField(fi); |
| 69 | + FieldInformation fo(output_field); |
| 70 | + |
| 71 | + if (fi.is_nonlinear()) |
| 72 | + { |
| 73 | + error("This function has not yet been defined for non-linear elements"); |
| 74 | + return (false); |
| 75 | + } |
| 76 | + |
| 77 | + if (fi.is_nodata()) |
| 78 | + { |
| 79 | + error("This function has not yet been defined for fields with no data"); |
| 80 | + return (false); |
| 81 | + } |
| 82 | + |
| 83 | + if (fi.is_vector() || fi.is_tensor()) |
| 84 | + { |
| 85 | + error("This function has not yet been defined for fields with vectors or tensors as indices"); |
| 86 | + return (false); |
| 87 | + } |
| 88 | + |
| 89 | + size_type nrows = input_matrix->nrows(); |
| 90 | + size_type ncols = input_matrix->ncols(); |
| 91 | + |
| 92 | + std::string algotype; |
| 93 | + |
| 94 | + if (ncols == 1) |
| 95 | + { |
| 96 | + algotype = "Scalar"; |
| 97 | + } |
| 98 | + else if (ncols == 3) |
| 99 | + { |
| 100 | + algotype = "Vector"; |
| 101 | + } |
| 102 | + else if (ncols == 6 || ncols == 9) |
| 103 | + { |
| 104 | + algotype = "Tensor"; |
| 105 | + } |
| 106 | + else |
| 107 | + { |
| 108 | + if (nrows == 1) |
| 109 | + { |
| 110 | + algotype = "Scalar"; |
| 111 | + } |
| 112 | + else if (nrows == 3) |
| 113 | + { |
| 114 | + algotype = "Vector"; |
| 115 | + } |
| 116 | + else if (nrows == 6 || nrows == 9) |
| 117 | + { |
| 118 | + algotype = "Tensor"; |
| 119 | + } |
| 120 | + else |
| 121 | + { |
| 122 | + error("Data does not have dimension of 1, 3, 6, or 9"); |
| 123 | + return (false); |
| 124 | + } |
| 125 | + } |
| 126 | + |
| 127 | + if (algotype == "Scalar") |
| 128 | + { |
| 129 | + std::string datatype; |
| 130 | + get_option(Parameters::OutputFieldDataType); |
| 131 | + fo.set_data_type(get_option(Parameters::OutputFieldDataType)); |
| 132 | + } |
| 133 | + if (algotype == "Vector") fo.make_vector(); |
| 134 | + if (algotype == "Tensor") fo.make_tensor(); |
| 135 | + |
| 136 | + //-------------------------------- |
| 137 | + // VIRTUAL INTERFACE |
| 138 | + |
| 139 | + output_field = CreateField(fo, input_field->mesh()); |
| 140 | + VField* vinput = input_field->vfield(); |
| 141 | + VField* voutput = output_field->vfield(); |
| 142 | + voutput->resize_fdata(); |
| 143 | + |
| 144 | + if (algotype == "Scalar") |
| 145 | + { |
| 146 | + int max_index = input_matrix->nrows() * input_matrix->ncols(); |
| 147 | + const double *dataptr = input_matrix->data(); |
| 148 | + VMesh::size_type sz = vinput->num_values(); |
| 149 | + for (VMesh::index_type r = 0; r<sz; r++) |
| 150 | + { |
| 151 | + int idx; |
| 152 | + vinput->get_value(idx, r); |
| 153 | + if ((idx < 0) || (idx >= max_index)) |
| 154 | + { |
| 155 | + error("Index exceeds matrix dimensions"); |
| 156 | + return (false); |
| 157 | + } |
| 158 | + voutput->set_value(dataptr[idx], r); |
| 159 | + } |
| 160 | + return (true); |
| 161 | + } |
| 162 | + else if (algotype == "Vector") |
| 163 | + { |
| 164 | + if (input_matrix->ncols() != 3) |
| 165 | + { |
| 166 | + input_matrix.reset(new DenseMatrix(input_matrix->transpose())); |
| 167 | + } |
| 168 | + |
| 169 | + const double *dataptr = input_matrix->data(); |
| 170 | + int max_index = input_matrix->nrows(); |
| 171 | + |
| 172 | + VMesh::size_type sz = vinput->num_values(); |
| 173 | + for (VMesh::index_type r = 0; r<sz; r++) |
| 174 | + { |
| 175 | + int idx; |
| 176 | + vinput->get_value(idx, r); |
| 177 | + if ((idx < 0) || (idx >= max_index)) |
| 178 | + { |
| 179 | + error("Index exceeds matrix dimensions"); |
| 180 | + return (false); |
| 181 | + } |
| 182 | + voutput->set_value(Vector(dataptr[3 * idx], dataptr[3 * idx + 1], dataptr[3 * idx + 2]), r); |
| 183 | + } |
| 184 | + return (true); |
| 185 | + } |
| 186 | + else if (algotype == "Tensor") |
| 187 | + { |
| 188 | + if ((input_matrix->ncols() != 6) && (input_matrix->ncols() != 9)) |
| 189 | + { |
| 190 | + input_matrix.reset(new DenseMatrix(input_matrix->transpose())); |
| 191 | + } |
| 192 | + |
| 193 | + int max_index = input_matrix->nrows(); |
| 194 | + const double *dataptr = input_matrix->data(); |
| 195 | + int ncols = input_matrix->ncols(); |
| 196 | + |
| 197 | + VMesh::size_type sz = vinput->num_values(); |
| 198 | + for (VMesh::index_type r = 0; r<sz; r++) |
| 199 | + { |
| 200 | + int idx; |
| 201 | + vinput->get_value(idx, r); |
| 202 | + if ((idx < 0) || (idx >= max_index)) |
| 203 | + { |
| 204 | + error("Index exceeds matrix dimensions"); |
| 205 | + return (false); |
| 206 | + } |
| 207 | + if (ncols == 6) |
| 208 | + { |
| 209 | + voutput->set_value(Tensor(dataptr[3 * idx], dataptr[3 * idx + 1], dataptr[3 * idx + 2], dataptr[3 * idx + 3], dataptr[3 * idx + 4], dataptr[3 * idx + 5]), r); |
| 210 | + } |
| 211 | + else |
| 212 | + { |
| 213 | + voutput->set_value(Tensor(dataptr[3 * idx], dataptr[3 * idx + 1], dataptr[3 * idx + 2], dataptr[3 * idx + 4], dataptr[3 * idx + 5], dataptr[3 * idx + 8]), r); |
| 214 | + } |
| 215 | + } |
| 216 | + AlgorithmOutput output; |
| 217 | + output[Variables::OutputField] = output_field; |
| 218 | + return (true); |
| 219 | + } |
| 220 | + |
| 221 | + // keep the compiler happy: |
| 222 | + // it seems reasonable to return false if none of the cases apply (AK) |
| 223 | + return (false); |
| 224 | +} |
| 225 | + |
| 226 | +AlgorithmOutput ConvertIndicesToFieldDataAlgo::run_generic(const AlgorithmInput& input) const |
| 227 | +{ |
| 228 | + auto field = input.get<Field>(Variables::InputField); |
| 229 | + auto inputmatrix = input.get<DenseMatrix>(Variables::InputMatrix); |
| 230 | + |
| 231 | + FieldHandle output_field; |
| 232 | + if (!runImpl(field, inputmatrix, output_field)) |
| 233 | + THROW_ALGORITHM_PROCESSING_ERROR("False returned on legacy run call."); |
| 234 | + |
| 235 | + AlgorithmOutput output; |
| 236 | + output[Variables::OutputField] = output_field; |
| 237 | + return output; |
| 238 | +} |
0 commit comments