Skip to content

Commit

Permalink
Add PointData::convertInternalData(elementTypeSpecifier)
Browse files Browse the repository at this point in the history
Allows converting the internal data from one data type to another.

Jeroen Eggermont suggested that it could be useful to convert data to a smaller data type, if possible. Example:

    // Converts internal data to std::vector<std::int8_t>
    pointData->convertInternalData(PointData::ElementTypeSpecifier::int8);
  • Loading branch information
N-Dekker committed Feb 6, 2024
1 parent 0c7eb69 commit 7b01a48
Showing 1 changed file with 43 additions and 0 deletions.
43 changes: 43 additions & 0 deletions HDPS/src/plugins/PointData/src/PointData.h
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,24 @@ class POINTDATA_EXPORT PointData : public mv::plugin::RawData
}


/// Converts the internal data if the specified `Index` has the same numerical value as the specified `elementTypeSpecifier`.
template<std::size_t Index>
void convertInternalDataIfIndexEqualsElementTypeSpecifier(const ElementTypeSpecifier elementTypeSpecifier)
{
if (static_cast<ElementTypeSpecifier>(Index) == elementTypeSpecifier)
{
convertInternalData<typename std::variant_alternative_t<Index, VariantOfVectors>::value_type>();
}
}

/// Converts the internal data as specified by the `elementTypeSpecifier`, using an `std::index_sequence`.
template<std::size_t... Indices>
void convertInternalDataUsingIndexSequence(const std::index_sequence<Indices...>, const ElementTypeSpecifier elementTypeSpecifier)
{
(convertInternalDataIfIndexEqualsElementTypeSpecifier<Indices>(elementTypeSpecifier), ...);
}


template <typename DimensionIndex>
void CheckDimensionIndex(const DimensionIndex& dimensionIndex) const
{
Expand Down Expand Up @@ -373,6 +391,31 @@ class POINTDATA_EXPORT PointData : public mv::plugin::RawData
_numDimensions = static_cast<std::uint32_t>(numDimensions);
}

/// Converts the internal data to the specified element data element type, by static_cast.
template <typename T>
void convertInternalData()
{
std::vector<T> convertedData(getSizeOfVector());

std::visit([&convertedData](const auto& vec)
{
std::transform(vec.cbegin(), vec.cend(), convertedData.begin(), [](const auto elem)
{
return static_cast<T>(elem);
});
},
_variantOfVectors);

_variantOfVectors = std::move(convertedData);
}


void convertInternalData(const ElementTypeSpecifier elementTypeSpecifier)
{
convertInternalDataUsingIndexSequence(std::make_index_sequence<std::variant_size_v<VariantOfVectors>>{}, elementTypeSpecifier);
}


/// Copies the specified data into the internal data, sets the number of
/// dimensions as specified, and sets the selected internal data type
/// according to the specified data type T.
Expand Down

0 comments on commit 7b01a48

Please sign in to comment.