Skip to content
Snippets Groups Projects
Commit 23f0cd0f authored by Dmitry Yu. Naumov's avatar Dmitry Yu. Naumov
Browse files

Merge branch 'VtkIntegerArrayConversion' into 'master'

Vtk mesh converter: safely convert all integer types.

See merge request ogs/ogs!3857
parents 0ad843c8 4121a83f
No related branches found
No related tags found
No related merge requests found
......@@ -14,6 +14,8 @@
#include "VtkMeshConverter.h"
#include <cstdint>
#include "MeshLib/Elements/Elements.h"
#include "MeshLib/Mesh.h"
#include "MeshLib/Node.h"
......@@ -28,10 +30,12 @@
#include <vtkLongArray.h>
#include <vtkLongLongArray.h>
#include <vtkPointData.h>
#include <vtkShortArray.h>
#include <vtkSmartPointer.h>
#include <vtkUnsignedCharArray.h>
#include <vtkUnsignedLongArray.h>
#include <vtkUnsignedLongLongArray.h>
#include <vtkUnsignedShortArray.h>
// Conversion from vtkUnstructuredGrid
#include <vtkCell.h>
......@@ -281,52 +285,144 @@ void VtkMeshConverter::convertArray(vtkDataArray& array,
return;
}
if (vtkIntArray::SafeDownCast(&array))
{
VtkMeshConverter::convertTypedArray<int>(array, properties, type);
return;
}
if (vtkBitArray::SafeDownCast(&array))
{
VtkMeshConverter::convertTypedArray<bool>(array, properties, type);
return;
}
// This also covers the vtkTypeInt8Array type, which is derived from the
// vtkCharArray type.
if (vtkCharArray::SafeDownCast(&array))
{
if constexpr (sizeof(std::int8_t) != sizeof(char))
{
OGS_FATAL(
"Array '{:s}' in VTU file uses unsupported data type '{:s}' "
"not convertible to char.",
array.GetName(), array.GetDataTypeAsString());
}
VtkMeshConverter::convertTypedArray<char>(array, properties, type);
return;
}
if (vtkLongArray::SafeDownCast(&array))
// This also covers the vtkTypeInt16Array type, which is derived from the
// vtkShortArray type.
if (vtkShortArray::SafeDownCast(&array))
{
VtkMeshConverter::convertTypedArray<long>(array, properties, type);
if constexpr (sizeof(std::int16_t) != sizeof(short))
{
OGS_FATAL(
"Array '{:s}' in VTU file uses unsupported data type '{:s}' "
"not convertible to short.",
array.GetName(), array.GetDataTypeAsString());
}
VtkMeshConverter::convertTypedArray<short>(array, properties, type);
return;
}
if (vtkLongLongArray::SafeDownCast(&array))
// This also covers the vtkTypeInt32Array type, which is derived from the
// vtkIntArray type.
if (vtkIntArray::SafeDownCast(&array))
{
VtkMeshConverter::convertTypedArray<long long>(array, properties, type);
if constexpr (sizeof(std::int32_t) != sizeof(int))
{
OGS_FATAL(
"Array '{:s}' in VTU file uses unsupported data type '{:s}' "
"not convertible to int.",
array.GetName(), array.GetDataTypeAsString());
}
VtkMeshConverter::convertTypedArray<int>(array, properties, type);
return;
}
if (vtkUnsignedLongArray::SafeDownCast(&array))
// This is not a unique conversion depending on the sizes of int, long, and
// long long. Converting to the apparently smaller type.
if (vtkLongArray::SafeDownCast(&array))
{
if constexpr (sizeof(long) == sizeof(int))
{
VtkMeshConverter::convertTypedArray<int>(array, properties, type);
return;
}
if constexpr (sizeof(long long) == sizeof(long))
{
VtkMeshConverter::convertTypedArray<long>(array, properties, type);
return;
}
OGS_FATAL(
"Array '{:s}' in VTU file uses unsupported data type '{:s}' ({:d}) "
"not convertible to int ({:d}), or long ({:d}) types.",
array.GetName(), array.GetDataTypeAsString(),
array.GetDataTypeSize(), sizeof(int), sizeof(long));
}
// Ensure, vtkTypeInt64Array and vtkLongLongArray are using the same type.
static_assert(sizeof(std::int64_t) == sizeof(long long));
// This also covers the vtkTypeInt64Array type, which is derived from the
// vtkLongLongArray type.
// Converting to the apparently smaller type.
if (vtkLongLongArray::SafeDownCast(&array))
{
if constexpr (sizeof(long long) == sizeof(long))
{
VtkMeshConverter::convertTypedArray<long>(array, properties, type);
return;
}
else // ll > l. Other cases are not possible because ll >= l in c++.
{
VtkMeshConverter::convertTypedArray<long long>(array, properties,
type);
return;
}
}
// This also covers the vtkTypeUInt8Array type, which is derived from the
// vtkUnsignedCharArray type.
if (vtkUnsignedCharArray::SafeDownCast(&array))
{
VtkMeshConverter::convertTypedArray<unsigned long>(array, properties,
if constexpr (sizeof(std::uint8_t) != sizeof(unsigned char))
{
OGS_FATAL(
"Array '{:s}' in VTU file uses unsupported data type '{:s}' "
"not convertible to unsigned char.",
array.GetName(), array.GetDataTypeAsString());
}
VtkMeshConverter::convertTypedArray<unsigned char>(array, properties,
type);
return;
}
if (vtkUnsignedLongLongArray::SafeDownCast(&array))
// This also covers the vtkTypeUInt16Array type, which is derived from the
// vtkUnsignedShortArray type.
if (vtkUnsignedShortArray::SafeDownCast(&array))
{
VtkMeshConverter::convertTypedArray<unsigned long long>(
array, properties, type);
if constexpr (sizeof(std::uint16_t) != sizeof(unsigned short))
{
OGS_FATAL(
"Array '{:s}' in VTU file uses unsupported data type '{:s}' "
"not convertible to unsigned short.",
array.GetName(), array.GetDataTypeAsString());
}
VtkMeshConverter::convertTypedArray<unsigned short>(array, properties,
type);
return;
}
// This also covers the vtkTypeUInt32Array type, which is derived from the
// vtkUnsignedIntArray type.
if (vtkUnsignedIntArray::SafeDownCast(&array))
{
if constexpr (sizeof(std::uint32_t) != sizeof(unsigned))
{
OGS_FATAL(
"Array '{:s}' in VTU file uses unsupported data type '{:s}' "
"not convertible to unsigned.",
array.GetName(), array.GetDataTypeAsString());
}
// MaterialIDs are assumed to be integers
if (std::strncmp(array.GetName(), "MaterialIDs", 11) == 0)
{
......@@ -341,10 +437,60 @@ void VtkMeshConverter::convertArray(vtkDataArray& array,
return;
}
// This is not a unique conversion depending on the sizes of unsigned,
// unsigned long, and unsigned long long. Converting to the apparently
// smaller type.
if (vtkUnsignedLongArray::SafeDownCast(&array))
{
if constexpr (sizeof(unsigned long) == sizeof(unsigned))
{
VtkMeshConverter::convertTypedArray<unsigned>(array, properties,
type);
return;
}
if constexpr (sizeof(unsigned long long) == sizeof(unsigned long))
{
VtkMeshConverter::convertTypedArray<unsigned long>(
array, properties, type);
return;
}
OGS_FATAL(
"Array '{:s}' in VTU file uses unsupported data type '{:s}' ({:d}) "
"not convertible to unsigned ({:d}), or unsigned long ({:d}) "
"types.",
array.GetName(), array.GetDataTypeAsString(),
array.GetDataTypeSize(), sizeof(unsigned), sizeof(unsigned long));
}
// Ensure, vtkTypeUInt64Array and vtkUnsignedLongLongArray are using the
// same type.
static_assert(sizeof(std::uint64_t) == sizeof(unsigned long long));
// This also covers the vtkTypeUInt64Array type, which is derived from the
// vtkUnsignedLongLongArray type.
// Converting to the apparently smaller type.
if (vtkUnsignedLongLongArray::SafeDownCast(&array))
{
if constexpr (sizeof(unsigned long long) == sizeof(unsigned long))
{
VtkMeshConverter::convertTypedArray<unsigned long>(
array, properties, type);
return;
}
else // ull > ul. Other cases are not possible because ull >= ul in
// c++.
{
VtkMeshConverter::convertTypedArray<unsigned long long>(
array, properties, type);
return;
}
}
WARN(
"Array '{:s}' in VTU file uses unsupported data type '{:s}'. The data "
"array will not be available.",
array.GetName(), array.GetDataTypeAsString());
"Array '{:s}' in VTU file uses unsupported data type '{:s}' of size "
"{:d}. The data array will not be available.",
array.GetName(), array.GetDataTypeAsString(), array.GetDataTypeSize());
}
} // end namespace MeshLib
......@@ -104,6 +104,23 @@ private:
MeshLib::Properties& properties,
MeshLib::MeshItemType type)
{
// Keep for debugging purposes, since the vtk array type and the end
// type T are not always the same.
INFO(
"Converting a vtk array '{:s}' with data type '{:s}' of "
"size {:d} to a type '{:s}' of size {:d}.",
array.GetName(), array.GetDataTypeAsString(),
array.GetDataTypeSize(), typeid(T).name(), sizeof(T));
if (sizeof(T) != array.GetDataTypeSize())
{
OGS_FATAL(
"Trying to convert a vtk array '{:s}' with data type '{:s}' of "
"size {:d} to a different sized type '{:s}' of size {:d}.",
array.GetName(), array.GetDataTypeAsString(),
array.GetDataTypeSize(), typeid(T).name(), sizeof(T));
}
vtkIdType const nTuples(array.GetNumberOfTuples());
int const nComponents(array.GetNumberOfComponents());
char const* const array_name(array.GetName());
......
......@@ -38,14 +38,22 @@ void checkParametersOfDirichletBoundaryCondition(
dof_table_bulk.getNumberOfVariableComponents(variable_id));
}
if (!bc_mesh.getProperties().hasPropertyVector("bulk_node_ids"))
{
OGS_FATAL(
"The required bulk node ids map does not exist in the boundary "
"mesh '{:s}'.",
bc_mesh.getName());
}
if (!bc_mesh.getProperties().existsPropertyVector<std::size_t>(
"bulk_node_ids"))
{
OGS_FATAL(
"The required bulk node ids map does not exist in the boundary "
"mesh '{:s}' or has the wrong data type (should be equivalent to "
"C++ data type std::size_t which is an unsigned integer of size "
"{:d} or UInt64 in vtk terminology).",
"The required bulk node ids map exist in the boundary mesh '{:s}' "
"but has wrong data type (should be equivalent to C++ data type "
"std::size_t which is an unsigned integer of size {:d} or UInt64 "
"in vtk terminology).",
bc_mesh.getName(), sizeof(std::size_t));
}
......
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