Skip to content

Commit 1e82362

Browse files
committed
Merge pull request #769 from benjaminlarson/benConvertFieldIndices
Closes #747. Test network to come from me
2 parents cb22d06 + bb79c84 commit 1e82362

14 files changed

+588
-43
lines changed

src/Core/Algorithms/Factory/HardCodedAlgorithmFactory.cc

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@
5656
#include <Core/Algorithms/Legacy/Fields/TransformMesh/ScaleFieldMeshAndData.h>
5757
#include <Core/Algorithms/Legacy/Fields/TransformMesh/ProjectPointsOntoMesh.h>
5858
#include <Core/Algorithms/Legacy/Fields/FieldData/SwapFieldDataWithMatrixEntriesAlgo.h>
59+
#include <Core/Algorithms/Legacy/Fields/FieldData/ConvertIndicesToFieldDataAlgo.h>
5960
#include <Core/Algorithms/Legacy/Fields/Mapping/BuildMappingMatrixAlgo.h>
6061
#include <Core/Algorithms/Math/AddKnownsToLinearSystem.h>
6162
#include <Core/Algorithms/Math/LinearSystem/SolveLinearSystemAlgo.h>
@@ -165,6 +166,7 @@ void HardCodedAlgorithmFactory::addToMakerMap()
165166
ADD_MODULE_ALGORITHM(FlipSurfaceNormals,FlipSurfaceNormalsAlgo)
166167
ADD_MODULE_ALGORITHM(BuildNoiseColumnMatrix,BuildNoiseColumnMatrixAlgorithm)
167168
ADD_MODULE_ALGORITHM(BuildMappingMatrix, BuildMappingMatrixAlgo)
169+
ADD_MODULE_ALGORITHM(ConvertIndicesToFieldData, ConvertIndicesToFieldDataAlgo)
168170
;
169171
}
170172
}

src/Core/Algorithms/Legacy/Fields/CMakeLists.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ SET(Core_Algorithms_Legacy_Fields_HEADERS
3535
FieldData/BuildMatrixOfSurfaceNormalsAlgo.h
3636
#Mapping/ApplyMappingMatrix.h
3737
#FieldData/CalculateFieldDataMetric.h
38-
#FieldData/ConvertIndicesToFieldData.h
38+
FieldData/ConvertIndicesToFieldDataAlgo.h
3939
FieldData/CalculateGradientsAlgo.h
4040
FieldData/CalculateVectorMagnitudesAlgo.h
4141
#FieldData/ConvertMappingMatrixToFieldData.h
@@ -116,7 +116,7 @@ SET(Core_Algorithms_Legacy_Fields_SRCS
116116
#FieldData/ConvertFieldDataType.cc
117117
FieldData/ConvertFieldBasisType.cc
118118
FieldData/SwapFieldDataWithMatrixEntriesAlgo.cc
119-
#FieldData/ConvertIndicesToFieldData.cc
119+
FieldData/ConvertIndicesToFieldDataAlgo.cc
120120
#FieldData/GetFieldData.cc
121121
#FieldData/ConvertMappingMatrixToFieldData.cc
122122
FieldData/SetFieldData.cc
Lines changed: 238 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,238 @@
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+
}
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
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+
30+
#ifndef CORE_ALGORITHMS_FIELDS_FIELDDATA_CONVERTINDICESTOFIELDDATAALGO_H
31+
#define CORE_ALGORITHMS_FIELDS_FIELDDATA_CONVERTINDICESTOFIELDDATAALGO_H 1
32+
33+
#include <Core/Algorithms/Base/AlgorithmBase.h>
34+
#include <Core/Algorithms/Legacy/Fields/share.h>
35+
36+
namespace SCIRun {
37+
namespace Core {
38+
namespace Algorithms {
39+
namespace Fields {
40+
41+
ALGORITHM_PARAMETER_DECL(OutputFieldDataType);
42+
43+
class SCISHARE ConvertIndicesToFieldDataAlgo : public AlgorithmBase
44+
{
45+
public:
46+
ConvertIndicesToFieldDataAlgo();
47+
48+
bool runImpl(FieldHandle input_field, Datatypes::DenseMatrixHandle input_matrix, FieldHandle& output_field)const;
49+
50+
// bool runImpl(FieldHandle input, Datatypes::MatrixHandle input_matrix, FieldHandle& output) const;
51+
52+
virtual AlgorithmOutput run_generic(const AlgorithmInput& input) const override;
53+
};
54+
}
55+
}
56+
}
57+
}
58+
59+
#endif
60+

src/Interface/Modules/Factory/ModuleDialogFactory.cc

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,7 @@
8080
#include <Interface/Modules/Fields/ConvertFieldBasisDialog.h>
8181
#include <Interface/Modules/Fields/SwapFieldDataWithMatrixEntriesDialog.h>
8282
#include <Interface/Modules/Fields/ConvertFieldBasisDialog.h>
83+
#include <Interface/Modules/Fields/ConvertIndicesToFieldDataDialog.h>
8384
#include <Interface/Modules/Forward/BuildBEMatrixDialog.h>
8485
#include <Interface/Modules/Inverse/SolveInverseProblemWithTikhonovDialog.h>
8586
#include <Interface/Modules/FiniteElements/ApplyFEMCurrentSourceDialog.h>
@@ -172,7 +173,8 @@ void ModuleDialogFactory::addDialogsToMakerMap1()
172173
ADD_MODULE_DIALOG(BuildNoiseColumnMatrix,BuildNoiseColumnMatrixDialog)
173174
ADD_MODULE_DIALOG(SwapFieldDataWithMatrixEntries, SwapFieldDataWithMatrixEntriesDialog)
174175
ADD_MODULE_DIALOG(BuildMappingMatrix, BuildMappingMatrixDialog)
175-
ADD_MODULE_DIALOG(EditMeshBoundingBox, EditMeshBoundingBoxDialog)
176+
ADD_MODULE_DIALOG(EditMeshBoundingBox, EditMeshBoundingBoxDialog)
177+
ADD_MODULE_DIALOG(ConvertIndicesToFieldData, ConvertIndicesToFieldDataDialog)
176178
ADD_MODULE_DIALOG(SolveInverseProblemWithTikhonov, SolveInverseProblemWithTikhonovDialog)
177179
;
178180
}

src/Interface/Modules/Fields/CMakeLists.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ SET(Interface_Modules_Fields_FORMS
5050
calculatedistancetofield.ui #TODO: fix case
5151
calculatedistancetofieldboundary.ui #TODO: fix case
5252
MapFieldDataOntoElems.ui
53+
ConvertIndicesToFieldData.ui
5354
MapFieldDataOntoNodes.ui
5455
ClipFieldByFunction.ui
5556
RefineMesh.ui
@@ -87,6 +88,7 @@ SET(Interface_Modules_Fields_HEADERS
8788
share.h
8889
SwapFieldDataWithMatrixEntriesDialog.h
8990
EditMeshBoundingBoxDialog.h
91+
ConvertIndicesToFieldDataDialog.h
9092
)
9193

9294
SET(Interface_Modules_Fields_SOURCES
@@ -117,6 +119,7 @@ SET(Interface_Modules_Fields_SOURCES
117119
SwapFieldDataWithMatrixEntriesDialog.cc
118120
RefineMeshDialog.cc
119121
EditMeshBoundingBoxDialog.cc
122+
ConvertIndicesToFieldDataDialog.cc
120123
)
121124

122125
IF(WITH_TETGEN)

0 commit comments

Comments
 (0)