3434#include < Core/Datatypes/SparseRowMatrix.h>
3535#include < Core/Datatypes/DatatypeFwd.h>
3636#include < Core/Datatypes/Legacy/Field/FieldInformation.h>
37+ #include < Core/Datatypes/DenseMatrix.h>
3738
38- #include < Core/Algorithms/Legacy/Fields/FieldData/GetFieldData.h>
39- #include < Core/Algorithms/Legacy/Fields/FieldData/SetFieldData.h>
39+ // #include <Core/Algorithms/Legacy/Fields/FieldData/GetFieldData.h>
40+ // #include <Core/Algorithms/Legacy/Fields/FieldData/SetFieldData.h>
4041
4142#include < Core/Algorithms/Base/AlgorithmVariableNames.h>
4243#include < Core/Algorithms/Base/AlgorithmPreconditions.h>
4344
4445using namespace SCIRun ::Core::Algorithms;
46+ using namespace SCIRun ::Core::Geometry;
4547using namespace SCIRun ::Core::Algorithms::Fields;
4648using namespace SCIRun ::Core::Algorithms::Fields::Parameters;
4749using namespace SCIRun ::Core::Datatypes;
4850using namespace SCIRun ;
4951
50- ALGORITHM_PARAMETER_DEF (Fields, PreserveScalar)
52+ ALGORITHM_PARAMETER_DEF (Fields, OutputFieldDataType);
5153
5254ConvertIndicesToFieldDataAlgo::ConvertIndicesToFieldDataAlgo ()
5355{
54- addParameter (Parameters::PreserveScalar, false );
56+ add_option (Parameters::OutputFieldDataType, " double,float,char,unsigned char, short, unsigned short, int, unsigned int " );
5557}
5658
5759bool
58- ConvertIndicesToFieldDataAlgo::runImpl (FieldHandle input_field, MatrixHandle input_matrix, FieldHandle& output_field, MatrixHandle& output_matrix ) const
60+ ConvertIndicesToFieldDataAlgo::runImpl (FieldHandle input_field, MatrixHandle input_matrix, FieldHandle& output_field) const
5961{
60- ScopedAlgorithmStatusReporter r (this , " SwapFieldDataWithMatrixEntriesAlgo " );
62+ ScopedAlgorithmStatusReporter r (this , " ConvertIndicesToFieldData " );
6163
6264 if (!input_field)
6365 {
@@ -66,53 +68,184 @@ ConvertIndicesToFieldDataAlgo::runImpl(FieldHandle input_field, MatrixHandle inp
6668 }
6769
6870 FieldInformation fi (input_field);
69- GetFieldDataAlgo get_algo_;
70- SetFieldDataAlgo set_algo_;
71-
72- const bool preserve_scalar = get (Parameters::PreserveScalar).toBool ();
7371 output_field = CreateField (fi);
72+ FieldInformation fo (output_field);
7473
75- if (input_matrix)
74+ /* if (input_matrix)
7675 {
77- output_matrix = get_algo_.run (input_field);
78- }
76+ output_field = get_algo_.run(input_field);
77+ }*/
7978 FieldHandle field_output_handle;
8079
81- if (input_matrix)
82- {
83- if (preserve_scalar)
84- {
85- set_algo_.set_option (set_algo_.keepTypeCheckBox , fi.get_data_type ());
86- }
87- size_type numVal = 0 ;
88- auto denseInput = matrix_convert::to_dense (input_matrix);
89- if (set_algo_.verify_input_data (input_field, denseInput, numVal, fi))
90- {
91- output_field = set_algo_.run (input_field, denseInput);
92- }
93- else
94- {
95- THROW_ALGORITHM_INPUT_ERROR (" Matrix dimensions do not match any of the fields dimensions" );
96- CopyProperties (*input_field, *output_field);
97- }
98- }
99- else
100- {
101- warning (" No input matrix passing the field through" );
102- output_field = input_field;
103- }
80+ if (fi.is_nonlinear ())
81+ {
82+ error (" This function has not yet been defined for non-linear elements" );
83+ return (false );
84+ }
10485
105- AlgorithmOutput output;
106- output[Variables::OutputField] = output_field;
86+ if (fi.is_nodata ())
87+ {
88+ error (" This function has not yet been defined for fields with no data" );
89+ return (false );
90+ }
91+
92+ if (fi.is_vector () || fi.is_tensor ())
93+ {
94+ error (" This function has not yet been defined for fields with vectors or tensors as indices" );
95+ return (false );
96+ }
97+
98+ size_type nrows = input_matrix->nrows ();
99+ size_type ncols = input_matrix->ncols ();
100+
101+ std::string algotype;
102+
103+ if (ncols == 1 )
104+ {
105+ algotype = " Scalar" ;
106+ }
107+ else if (ncols == 3 )
108+ {
109+ algotype = " Vector" ;
110+ }
111+ else if (ncols == 6 || ncols == 9 )
112+ {
113+ algotype = " Tensor" ;
114+ }
115+ else
116+ {
117+ if (nrows == 1 )
118+ {
119+ algotype = " Scalar" ;
120+ }
121+ else if (nrows == 3 )
122+ {
123+ algotype = " Vector" ;
124+ }
125+ else if (nrows == 6 || nrows == 9 )
126+ {
127+ algotype = " Tensor" ;
128+ }
129+ else
130+ {
131+ error (" Data does not have dimension of 1, 3, 6, or 9" );
132+ return (false );
133+ }
134+ }
135+
136+ if (algotype == " Scalar" )
137+ {
138+ std::string datatype;
139+ get_option (Parameters::OutputFieldDataType);
140+ fo.set_data_type (get_option (Parameters::OutputFieldDataType));
141+ }
142+ if (algotype == " Vector" ) fo.make_vector ();
143+ if (algotype == " Tensor" ) fo.make_tensor ();
144+
145+ // --------------------------------
146+ // VIRTUAL INTERFACE
147+
148+ output_field = CreateField (fo, input_field->mesh ());
149+ VField* vinput = input_field->vfield ();
150+ VField* voutput = output_field->vfield ();
151+ voutput->resize_fdata ();
152+
153+ DenseMatrixHandle dmh (boost::make_shared<DenseMatrix>(input_matrix));
154+ // DenseMatrix * dm = input_matrix->
155+ // MatrixHandle dm = dm;
156+
157+ if (algotype == " Scalar" )
158+ {
159+ int max_index = input_matrix->nrows () * input_matrix->ncols ();
160+ double *dataptr = dmh->data ();
161+ // double *dataptr = &dmh[0];
162+ VMesh::size_type sz = vinput->num_values ();
163+ for (VMesh::index_type r = 0 ; r<sz; r++)
164+ {
165+ int idx;
166+ vinput->get_value (idx, r);
167+ if ((idx < 0 ) || (idx >= max_index))
168+ {
169+ error (" Index exceeds matrix dimensions" );
170+ return (false );
171+ }
172+ voutput->set_value (dataptr[idx], r);
173+ }
174+ return (true );
175+ }
176+ else if (algotype == " Vector" )
177+ {
178+ if (input_matrix->ncols () != 3 )
179+ {
180+ // MatrixHandle temp = dmh;
181+ auto dmht = dmh->transpose ();
182+ DenseMatrixHandle dmh (boost::make_shared<DenseMatrix>(dmht));
183+ // dmh = dmht->dense();
184+ }
185+
186+ double *dataptr = dmh->data ();
187+ int max_index = dmh->nrows ();
188+
189+ VMesh::size_type sz = vinput->num_values ();
190+ for (VMesh::index_type r = 0 ; r<sz; r++)
191+ {
192+ int idx;
193+ vinput->get_value (idx, r);
194+ if ((idx < 0 ) || (idx >= max_index))
195+ {
196+ error (" Index exceeds matrix dimensions" );
197+ return (false );
198+ }
199+ voutput->set_value (Vector (dataptr[3 * idx], dataptr[3 * idx + 1 ], dataptr[3 * idx + 2 ]), r);
200+ }
201+ return (true );
202+ }
203+ else if (algotype == " Tensor" )
204+ {
205+ if ((input_matrix->ncols () != 6 ) && (input_matrix->ncols () != 9 ))
206+ {
207+ // MatrixHandle temp = dmh;
208+ // dmh = dm->make_transpose();
209+ auto dmht = dmh->transpose ();
210+ DenseMatrixHandle dmh (boost::make_shared<DenseMatrix>(dmht));
211+ // dm = dmh->dense();
212+ }
213+
214+ int max_index = dmh->nrows ();
215+ double *dataptr = dmh->data ();
216+ int ncols = dmh->ncols ();
217+
218+ VMesh::size_type sz = vinput->num_values ();
219+ for (VMesh::index_type r = 0 ; r<sz; r++)
220+ {
221+ int idx;
222+ vinput->get_value (idx, r);
223+ if ((idx < 0 ) || (idx >= max_index))
224+ {
225+ error (" Index exceeds matrix dimensions" );
226+ return (false );
227+ }
228+ if (ncols == 6 )
229+ {
230+ 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);
231+ }
232+ else
233+ {
234+ 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);
235+ }
236+ }
237+ return (true );
238+ }
107239
108- return true ;
240+ // keep the compiler happy:
241+ // it seems reasonable to return false if none of the cases apply (AK)
242+ return (false );
109243}
110244
111245bool
112246ConvertIndicesToFieldDataAlgo::runImpl (FieldHandle input, MatrixHandle input_matrix, FieldHandle& output) const
113247{
114- MatrixHandle dummy;
115- return runImpl (input, input_matrix, output, dummy);
248+ return runImpl (input, input_matrix, output);
116249}
117250
118251AlgorithmOutput ConvertIndicesToFieldDataAlgo::run_generic (const AlgorithmInput& input) const
0 commit comments