3#include <vtkInformation.h>
4#include <vtkObjectFactory.h>
6#include <vtkAbstractArray.h>
7#include <vtkDataArray.h>
9#include <vtkFieldData.h>
10#include <vtkSmartPointer.h>
11#include <vtkStringArray.h>
13#include <vtkCommand.h>
14#include <vtkDataArraySelection.h>
23 this->SetNumberOfInputPorts(2);
24 this->SetNumberOfOutputPorts(1);
26 for(
int cc = 0; cc < vtkDataObject::NUMBER_OF_ASSOCIATIONS; ++cc) {
27 if(cc != vtkDataObject::FIELD_ASSOCIATION_POINTS_THEN_CELLS) {
29 this->ArraySelections[cc]->AddObserver(
30 vtkCommand::ModifiedEvent,
this, &ttkArrayEditor::Modified);
32 this->ArraySelections[cc] =
nullptr;
40 if(association >= 0 && association < vtkDataObject::NUMBER_OF_ASSOCIATIONS) {
41 return this->ArraySelections[association];
48 if(port == 0 || port == 1) {
49 info->Set(vtkAlgorithm::INPUT_REQUIRED_DATA_TYPE(),
"vtkDataObject");
51 info->Set(vtkAlgorithm::INPUT_IS_OPTIONAL(), 1);
65template <
typename VTK_T1,
typename VTK_T2>
67 auto targetData = ttkUtils::GetPointer<VTK_T1>(target);
68 auto copyData = ttkUtils::GetPointer<VTK_T2>(copy);
69 for(
size_t i = 0, n = target->GetNumberOfValues(); i < n; i++) {
71 copyData[i] = (VTK_T2)(targetData[i]);
80 vtkInformationVector **inputVector,
81 vtkInformationVector *outputVector) {
83 std::string associationNames[3] = {
"point",
"cell",
"field"};
86 auto target = vtkDataObject::GetData(inputVector[0], 0);
87 auto output = vtkDataObject::GetData(outputVector, 0);
88 output->ShallowCopy(target);
93 switch(this->EditorMode) {
95 this->
printMsg(
"Input string: '" + this->DataString +
"'",
99 if(this->DataString.length() > 0) {
102 int targetAssociation
103 = this->TargetAssociation < 0 ? 2 : this->TargetAssociation;
104 auto outputAtt = output->GetAttributesAsFieldData(targetAssociation);
106 this->
printErr(
"Target does not have requested attribute type.");
111 this->
printMsg(
"Adding parsed arrays from string to "
112 + associationNames[targetAssociation] +
" data",
115 std::string finalExpressionString;
117 std::string errorMsg;
119 output->GetFieldData(),
120 finalExpressionString, errorMsg)) {
121 std::stringstream msg;
129 std::stringstream ss(finalExpressionString);
131 while(getline(ss, line,
'\n')) {
136 if(this->ReplaceExistingArrays
137 || !outputAtt->HasArray(array->GetName()))
138 outputAtt->AddArray(array);
142 this->
printMsg(
"Adding parsed arrays from string to "
143 + associationNames[targetAssociation] +
" data",
152 const std::string modeS
153 = isAddMode ?
"Adding point/cell/field arrays from source"
154 :
"Filter point/cell/field arrays from source";
157 auto source = vtkDataObject::GetData(inputVector[1], 0);
159 this->
printErr(
"Unable to retrieve source vtkDataObject.");
163 for(
int association = 0;
164 association < vtkDataObject::NUMBER_OF_ASSOCIATIONS; ++association) {
165 if(association == vtkDataObject::FIELD_ASSOCIATION_POINTS_THEN_CELLS)
169 auto sourceFD = source->GetAttributesAsFieldData(association);
171 if(!sourceFD || !selection)
175 int targetAssociation
176 = this->TargetAssociation < 0 ? association : this->TargetAssociation;
177 auto outputAtt = output->GetAttributesAsFieldData(targetAssociation);
183 for(
int i = 0; i < sourceFD->GetNumberOfArrays(); i++) {
184 auto array = sourceFD->GetAbstractArray(i);
187 if(selection->ArrayIsEnabled(array->GetName()))
188 outputAtt->AddArray(array);
190 if(!selection->ArrayIsEnabled(array->GetName()))
191 outputAtt->RemoveArray(array->GetName());
202 auto targetArray = this->GetInputArrayToProcess(0, inputVector);
204 return !this->
printErr(
"Unable to retrieve input array.");
206 int targetArrayAssociation
207 = this->GetInputArrayAssociation(0, inputVector);
209 this->
printMsg(
"Editing '" + std::string(targetArray->GetName()) +
"' "
210 + associationNames[targetArrayAssociation]
216 if(this->TargetArrayType >= 0 || this->TargetArrayIndexation[0] >= 0
217 || this->TargetArrayIndexation[1] >= 0) {
220 this->TargetArrayType < 0 ? targetArray->GetDataType()
221 : this->TargetArrayType));
223 size_t nComponents = this->TargetArrayIndexation[1] >= 0
224 ? this->TargetArrayIndexation[1]
225 : this->TargetArrayIndexation[0] >= 0
226 ? targetArray->GetNumberOfValues()
227 / this->TargetArrayIndexation[0]
228 : targetArray->GetNumberOfComponents();
230 size_t nTuples = this->TargetArrayIndexation[0] >= 0
231 ? this->TargetArrayIndexation[0]
232 : this->TargetArrayIndexation[1] >= 0
233 ? targetArray->GetNumberOfValues()
234 / this->TargetArrayIndexation[1]
235 : targetArray->GetNumberOfTuples();
236 copy->Allocate(nTuples * nComponents);
237 copy->SetNumberOfComponents(nComponents);
238 copy->SetNumberOfTuples(nTuples);
240 switch(vtkTemplate2PackMacro(
241 targetArray->GetDataType(), copy->GetDataType())) {
242 vtkTemplate2Macro((copyArrayData<VTK_T1, VTK_T2>(targetArray, copy)));
246 copy->ShallowCopy(targetArray);
248 copy->SetName(this->TargetArrayName.compare(
"") == 0
249 ? targetArray->GetName()
250 : this->TargetArrayName.data());
253 int targetAssociation = this->TargetAssociation < 0
254 ? targetArrayAssociation
255 : this->TargetAssociation;
256 auto outputAtt = output->GetAttributesAsFieldData(targetAssociation);
258 this->
printErr(
"Target does not have requested attribute type.");
261 outputAtt->AddArray(copy);
263 this->
printMsg(
"Editing '" + std::string(targetArray->GetName()) +
"' "
264 + associationNames[targetArrayAssociation]
#define ttkNotUsed(x)
Mark function/method parameters that are not used in the function body at all.
static vtkInformationIntegerKey * SAME_DATA_TYPE_AS_INPUT_PORT()
TTK VTK-filter that edit arrays of a vtkDataObject.
int RequestData(vtkInformation *request, vtkInformationVector **inputVector, vtkInformationVector *outputVector) override
vtkDataArraySelection * GetArraySelection(int association)
~ttkArrayEditor() override
int FillInputPortInformation(int port, vtkInformation *info) override
@ FILTER_ARRAYS_FROM_SOURCE
int FillOutputPortInformation(int port, vtkInformation *info) override
static int replaceVariables(const std::string &iString, vtkFieldData *fieldData, std::string &oString, std::string &errorMsg)
static vtkSmartPointer< vtkAbstractArray > csvToVtkArray(const std::string &line)
void setDebugMsgPrefix(const std::string &prefix)
int printMsg(const std::string &msg, const debug::Priority &priority=debug::Priority::INFO, const debug::LineMode &lineMode=debug::LineMode::NEW, std::ostream &stream=std::cout) const
int printErr(const std::string &msg, const debug::LineMode &lineMode=debug::LineMode::NEW, std::ostream &stream=std::cerr) const
int copyArrayData(vtkDataArray *target, vtkDataArray *copy)
vtkStandardNewMacro(ttkArrayEditor)