Skip to content

Commit ffc605f

Browse files
committed
Add PointData::convertInternalData(elementTypeSpecifier)
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); Which is equivalent to: pointData->convertInternalData<std::int8_t>();
1 parent b83c803 commit ffc605f

File tree

1 file changed

+46
-0
lines changed

1 file changed

+46
-0
lines changed

HDPS/src/plugins/PointData/src/PointData.h

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,24 @@ class POINTDATA_EXPORT PointData : public mv::plugin::RawData
179179
}
180180

181181

182+
/// Converts the internal data if the specified `Index` has the same numerical value as the specified `elementTypeSpecifier`.
183+
template<std::size_t Index>
184+
void convertInternalDataIfIndexEqualsElementTypeSpecifier(const ElementTypeSpecifier elementTypeSpecifier)
185+
{
186+
if (static_cast<ElementTypeSpecifier>(Index) == elementTypeSpecifier)
187+
{
188+
convertInternalData<typename std::variant_alternative_t<Index, VariantOfVectors>::value_type>();
189+
}
190+
}
191+
192+
/// Converts the internal data as specified by the `elementTypeSpecifier`, using an `std::index_sequence`.
193+
template<std::size_t... Indices>
194+
void convertInternalDataUsingIndexSequence(const std::index_sequence<Indices...>, const ElementTypeSpecifier elementTypeSpecifier)
195+
{
196+
(convertInternalDataIfIndexEqualsElementTypeSpecifier<Indices>(elementTypeSpecifier), ...);
197+
}
198+
199+
182200
template <typename DimensionIndex>
183201
void CheckDimensionIndex(const DimensionIndex& dimensionIndex) const
184202
{
@@ -373,6 +391,34 @@ class POINTDATA_EXPORT PointData : public mv::plugin::RawData
373391
_numDimensions = static_cast<std::uint32_t>(numDimensions);
374392
}
375393

394+
/// Converts the internal data to the specified element data element type, by static_cast.
395+
template <typename T>
396+
void convertInternalData()
397+
{
398+
if (!std::holds_alternative<std::vector<T>>(_variantOfVectors))
399+
{
400+
std::vector<T> convertedData(getSizeOfVector());
401+
402+
std::visit([&convertedData](const auto& vec)
403+
{
404+
std::transform(vec.cbegin(), vec.cend(), convertedData.begin(), [](const auto elem)
405+
{
406+
return static_cast<T>(elem);
407+
});
408+
},
409+
_variantOfVectors);
410+
411+
_variantOfVectors = std::move(convertedData);
412+
}
413+
}
414+
415+
416+
void convertInternalData(const ElementTypeSpecifier elementTypeSpecifier)
417+
{
418+
convertInternalDataUsingIndexSequence(std::make_index_sequence<std::variant_size_v<VariantOfVectors>>{}, elementTypeSpecifier);
419+
}
420+
421+
376422
/// Copies the specified data into the internal data, sets the number of
377423
/// dimensions as specified, and sets the selected internal data type
378424
/// according to the specified data type T.

0 commit comments

Comments
 (0)