Commit 84738315 authored by Ben Boeckel's avatar Ben Boeckel Committed by Utkarsh Ayachit

improvements to vtkSplitColumnComponents

vtkSplitColumnComponents now passes information about which arrays were
split out from a multi component array in the input by adding keys to
the output arrays. This provides potentially useful information for
downstream items.
parents 6220c70f 8d40ed96
......@@ -13,6 +13,7 @@
=========================================================================*/
#include "vtkInformation.h"
#include "vtkIntArray.h"
#include "vtkNew.h"
#include "vtkSmartPointer.h"
......@@ -141,5 +142,15 @@ int TestTableSplitColumnComponents(int, char*[])
return EXIT_FAILURE;
}
auto a1info = arrays[1]->GetInformation();
if (!a1info->Has(vtkSplitColumnComponents::ORIGINAL_ARRAY_NAME()) ||
strcmp(a1info->Get(vtkSplitColumnComponents::ORIGINAL_ARRAY_NAME()), "Multi") != 0 ||
!a1info->Has(vtkSplitColumnComponents::ORIGINAL_COMPONENT_NUMBER()) ||
a1info->Get(vtkSplitColumnComponents::ORIGINAL_COMPONENT_NUMBER()) != 0)
{
vtkGenericWarningMacro("Missing array information about original name and component!");
return EXIT_FAILURE;
}
return EXIT_SUCCESS;
}
......@@ -15,23 +15,29 @@
#include "vtkSplitColumnComponents.h"
#include "vtkAbstractArray.h"
#include "vtkFieldData.h"
#include "vtkInformation.h"
#include "vtkInformationIntegerKey.h"
#include "vtkInformationStringKey.h"
#include "vtkInformationVector.h"
#include "vtkIntArray.h"
#include "vtkObjectFactory.h"
#include "vtkStdString.h"
#include "vtkStringArray.h"
#include "vtkTable.h"
#include <sstream>
#include <cmath>
#include <sstream>
vtkStandardNewMacro(vtkSplitColumnComponents);
vtkInformationKeyMacro(vtkSplitColumnComponents, ORIGINAL_ARRAY_NAME, String);
vtkInformationKeyMacro(vtkSplitColumnComponents, ORIGINAL_COMPONENT_NUMBER, Integer);
//---------------------------------------------------------------------------
vtkSplitColumnComponents::vtkSplitColumnComponents()
: CalculateMagnitudes(true)
, NamingMode(vtkSplitColumnComponents::NUMBERS_WITH_PARENS)
{
this->SetNumberOfInputPorts(1);
this->SetNumberOfOutputPorts(1);
this->CalculateMagnitudes = true;
this->NamingMode = NUMBERS_WITH_PARENS;
}
//---------------------------------------------------------------------------
......@@ -90,6 +96,12 @@ int vtkSplitColumnComponents::RequestData(
for (int i = 0; i < table->GetNumberOfColumns(); ++i)
{
vtkAbstractArray* col = table->GetColumn(i);
if (col->GetName() == nullptr)
{
vtkWarningMacro("Skipping column with no name!");
continue;
}
int components = col->GetNumberOfComponents();
if (components == 1)
{
......@@ -101,7 +113,7 @@ int vtkSplitColumnComponents::RequestData(
int colSize = col->GetNumberOfTuples();
for (int j = 0; j < components; ++j)
{
vtkStdString component_label = this->GetComponentLabel(col, j);
const std::string component_label = this->GetComponentLabel(col, j);
vtkAbstractArray* newCol = vtkAbstractArray::CreateArray(col->GetDataType());
newCol->SetName(component_label.c_str());
newCol->SetNumberOfTuples(colSize);
......@@ -118,14 +130,18 @@ int vtkSplitColumnComponents::RequestData(
static_cast<VTK_TT*>(newCol->GetVoidPointer(0)),
components, j, colSize));
}
if (auto info = newCol->GetInformation())
{
info->Set(ORIGINAL_ARRAY_NAME(), col->GetName());
info->Set(ORIGINAL_COMPONENT_NUMBER(), j);
}
output->AddColumn(newCol);
newCol->Delete();
}
// Add a magnitude column and calculate values if requested
if (this->CalculateMagnitudes && col->IsA("vtkDataArray"))
{
vtkStdString component_label = this->GetComponentLabel(
col, -1 /* for magnitude */);
std::string component_label = this->GetComponentLabel(col, -1 /* for magnitude */);
vtkAbstractArray* newCol = vtkAbstractArray::CreateArray(col->GetDataType());
newCol->SetName(component_label.c_str());
newCol->SetNumberOfTuples(colSize);
......@@ -137,13 +153,16 @@ int vtkSplitColumnComponents::RequestData(
static_cast<VTK_TT*>(newCol->GetVoidPointer(0)),
components, colSize));
}
if (auto info = newCol->GetInformation())
{
info->Set(ORIGINAL_ARRAY_NAME(), col->GetName());
info->Set(ORIGINAL_COMPONENT_NUMBER(), -1); // for magnitude
}
output->AddColumn(newCol);
newCol->Delete();
}
}
}
return 1;
}
......@@ -190,8 +209,7 @@ namespace
};
//---------------------------------------------------------------------------
vtkStdString vtkSplitColumnComponents::GetComponentLabel(
vtkAbstractArray* array, int component_no)
std::string vtkSplitColumnComponents::GetComponentLabel(vtkAbstractArray* array, int component_no)
{
std::ostringstream stream;
switch (this->NamingMode)
......
......@@ -40,7 +40,11 @@
#include "vtkFiltersGeneralModule.h" // For export macro
#include "vtkTableAlgorithm.h"
class vtkStdString;
#include <string> // for std::strin
class vtkInformationIntegerKey;
class vtkInformationStringKey;
class VTKFILTERSGENERAL_EXPORT vtkSplitColumnComponents : public vtkTableAlgorithm
{
public:
......@@ -56,6 +60,7 @@ public:
*/
vtkSetMacro(CalculateMagnitudes, bool);
vtkGetMacro(CalculateMagnitudes, bool);
vtkBooleanMacro(CalculateMagnitudes, bool);
//@}
enum
......@@ -83,6 +88,21 @@ public:
vtkGetMacro(NamingMode, int);
//@}
//@{
/**
* These are keys that get added to each output array to make it easier for
* downstream filters to know which output array were extracted from which
* input array.
*
* If either of these keys are missing, then the array was not extracted at
* all.
*
* `ORIGINAL_COMPONENT_NUMBER` of -1 indicates magnitude.
*/
static vtkInformationStringKey* ORIGINAL_ARRAY_NAME();
static vtkInformationIntegerKey* ORIGINAL_COMPONENT_NUMBER();
//@}
protected:
vtkSplitColumnComponents();
~vtkSplitColumnComponents() override;
......@@ -91,19 +111,19 @@ protected:
* Returns the label to use for the specific component in the array based on
* this->NamingMode. Use component_no==-1 for magnitude.
*/
vtkStdString GetComponentLabel(vtkAbstractArray* array, int component_no);
bool CalculateMagnitudes;
std::string GetComponentLabel(vtkAbstractArray* array, int component_no);
int RequestData(
vtkInformation*,
vtkInformationVector**,
vtkInformationVector*) override;
int NamingMode;
private:
vtkSplitColumnComponents(const vtkSplitColumnComponents&) = delete;
void operator=(const vtkSplitColumnComponents&) = delete;
bool CalculateMagnitudes;
int NamingMode;
};
#endif
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment