Skip to content
Snippets Groups Projects
Commit 056f9670 authored by Jaswant Panchumarti (Kitware)'s avatar Jaswant Panchumarti (Kitware) Committed by Kitware Robot
Browse files

Merge topic 'serialize-print-vtkabstract-array'


d418c80a Add convenient method to serialize and print tuples of vtkAbstractArray

Acked-by: default avatarKitware Robot <kwrobot@kitware.com>
Acked-by: default avatarbuildbot <buildbot@kitware.com>
Reviewed-by: default avatarDavid Thompson <david.thompson@kitware.com>
Merge-request: !11163
parents 28e93202 d418c80a
No related branches found
No related tags found
Loading
......@@ -3,8 +3,10 @@
#include "vtkAbstractArray.h"
#include "vtkArrayDispatch.h"
#include "vtkBitArray.h"
#include "vtkCharArray.h"
#include "vtkDataArrayRange.h"
#include "vtkDoubleArray.h"
#include "vtkFloatArray.h"
#include "vtkIdList.h"
......@@ -46,6 +48,11 @@
#include <iterator>
#include <set>
// clang-format off
#include "vtk_nlohmannjson.h"
#include VTK_NLOHMANN_JSON(json.hpp)
// clang-format on
VTK_ABI_NAMESPACE_BEGIN
vtkInformationKeyMacro(vtkAbstractArray, GUI_HIDE, Integer);
vtkInformationKeyMacro(vtkAbstractArray, PER_COMPONENT, InformationVector);
......@@ -785,6 +792,29 @@ void SampleProminentValues(std::vector<std::vector<vtkVariant>>& uniques, vtkIdT
std::copy(si->begin(), si->end(), bi);
}
}
struct WriteDataArrayWorker
{
WriteDataArrayWorker(nlohmann::json& result)
: m_result(result)
{
}
template <typename InArrayT>
void operator()(InArrayT* inArray)
{
using T = vtk::GetAPIType<InArrayT>;
const auto inRange = vtk::DataArrayValueRange(inArray);
T val;
for (const auto& value : inRange)
{
val = value;
m_result.push_back(val);
}
}
nlohmann::json& m_result;
};
} // End anonymous namespace.
VTK_ABI_NAMESPACE_BEGIN
......@@ -896,4 +926,34 @@ void vtkAbstractArray::UpdateDiscreteValueSet(double uncertainty, double minimum
params[1] = minimumProminence;
this->GetInformation()->Set(DISCRETE_VALUE_SAMPLE_PARAMETERS(), params, 2);
}
//------------------------------------------------------------------------------
nlohmann::json vtkAbstractArray::SerializeValues()
{
auto result = nlohmann::json::array();
if (auto* darr = vtkDataArray::SafeDownCast(this))
{
using Dispatcher = vtkArrayDispatch::DispatchByValueType<vtkArrayDispatch::AllTypes>;
WriteDataArrayWorker worker(result);
if (!Dispatcher::Execute(darr, worker))
{
worker(darr);
}
}
else
{
for (vtkIdType ii = 0; ii < this->GetNumberOfValues(); ++ii)
{
result.push_back(this->GetVariantValue(ii).ToString());
}
}
return result;
}
//------------------------------------------------------------------------------
void vtkAbstractArray::PrintValues(ostream& os)
{
os << this->SerializeValues().dump() << '\n';
}
VTK_ABI_NAMESPACE_END
......@@ -58,6 +58,9 @@
#include "vtkVariant.h" // for variant arguments
#include "vtkWrappingHints.h" // For VTK_MARSHALAUTO
#include "vtk_nlohmannjson.h"
#include VTK_NLOHMANN_JSON(json_fwd.hpp)
VTK_ABI_NAMESPACE_BEGIN
class vtkArrayIterator;
class vtkDataArray;
......@@ -75,6 +78,8 @@ class VTKCOMMONCORE_EXPORT VTK_MARSHALAUTO vtkAbstractArray : public vtkObject
public:
vtkTypeMacro(vtkAbstractArray, vtkObject);
void PrintSelf(ostream& os, vtkIndent indent) override;
void PrintValues(ostream& os);
nlohmann::json SerializeValues();
/**
* Allocate memory for this array. Delete old storage only if necessary.
......
# Add methods to printand serialize values of a vtkAbstractArray
You can now print the values of a `vtkAbstractArray` to a stream object by invoking
`PrintValues(stream)`.
Ex:
```c++
vtkNew<vtkFloatArray> array;
array->InsertNextValue(1.0);
array->PrintValues(std::cout);
```
You can also serialize the values of a `vtkAbstractArray` into `json` with
`nlohmann::json vtkAbstractArray::SerializeValues()`
......@@ -160,56 +160,6 @@ std::string dataTypeToString(int dataType)
return "unhandled";
}
struct WriteDataArrayWorker
{
WriteDataArrayWorker(nlohmann::json& result)
: m_result(result)
{
}
template <typename InArrayT>
void operator()(InArrayT* inArray)
{
using T = vtk::GetAPIType<InArrayT>;
const auto inRange = vtk::DataArrayValueRange(inArray);
T val;
for (const auto& value : inRange)
{
val = value;
m_result.push_back(val);
}
}
nlohmann::json& m_result;
};
nlohmann::json serializeArrayValues(vtkAbstractArray* arr)
{
auto result = nlohmann::json::array();
if (!arr)
{
return result;
}
if (auto* darr = vtkDataArray::SafeDownCast(arr))
{
using Dispatcher = vtkArrayDispatch::DispatchByValueType<vtkArrayDispatch::AllTypes>;
WriteDataArrayWorker worker(result);
if (!Dispatcher::Execute(darr, worker))
{
worker(darr);
}
}
else
{
for (vtkIdType ii = 0; ii < arr->GetNumberOfValues(); ++ii)
{
result.push_back(arr->GetVariantValue(ii).ToString());
}
}
return result;
}
void vtkCellGridWriter::WriteData()
{
if (!this->FileName || !this->FileName[0])
......@@ -290,7 +240,7 @@ void vtkCellGridWriter::WriteData()
arrayLocations[arr] = nlohmann::json::array({ groupName, arr->GetName() });
nlohmann::json arrayRecord{ { "name", arr->GetName() },
{ "tuples", arr->GetNumberOfTuples() }, { "components", arr->GetNumberOfComponents() },
{ "type", dataTypeToString(arr->GetDataType()) }, { "data", serializeArrayValues(arr) } };
{ "type", dataTypeToString(arr->GetDataType()) }, { "data", arr->SerializeValues() } };
if (arr == groupScalars)
{
arrayRecord["default_scalars"] = true;
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment