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