From c42f3fe21489b6c5eb74083dab8216e2897f2278 Mon Sep 17 00:00:00 2001 From: Fabian Reister <fabian.reister@kit.edu> Date: Fri, 10 Feb 2023 08:43:06 +0100 Subject: [PATCH] nd array reading --- .../NlohmannJSONReaderWithoutTypeCheck.cpp | 60 ++++++++++++++++++- 1 file changed, 59 insertions(+), 1 deletion(-) diff --git a/source/RobotAPI/libraries/aron/core/data/rw/reader/nlohmannJSON/NlohmannJSONReaderWithoutTypeCheck.cpp b/source/RobotAPI/libraries/aron/core/data/rw/reader/nlohmannJSON/NlohmannJSONReaderWithoutTypeCheck.cpp index 994663799..e9249e757 100644 --- a/source/RobotAPI/libraries/aron/core/data/rw/reader/nlohmannJSON/NlohmannJSONReaderWithoutTypeCheck.cpp +++ b/source/RobotAPI/libraries/aron/core/data/rw/reader/nlohmannJSON/NlohmannJSONReaderWithoutTypeCheck.cpp @@ -19,13 +19,21 @@ */ // STD/STL +#include <cmath> +#include <cstdint> +#include <cstring> #include <memory> #include <numeric> +#include <SimoxUtility/algorithm/get_map_keys_values.h> + +#include "ArmarXCore/core/logging/Logging.h" + // Header #include "NlohmannJSONReaderWithoutTypeCheck.h" // ArmarX +#include <RobotAPI/interface/aron/Aron.h> #include <RobotAPI/libraries/aron/core/Exception.h> #include <RobotAPI/libraries/aron/core/data/visitor/nlohmannJSON/NlohmannJSONVisitor.h> @@ -56,6 +64,26 @@ namespace armarx::aron::data::reader elements = input.get<std::map<std::string, nlohmann::json>>(); } + + const std::map<std::string, type::matrix::ElementType> ElementTypeAsString = { + {"short", ::armarx::aron::type::matrix::INT16}, + {"int", ::armarx::aron::type::matrix::INT32}, + {"long", ::armarx::aron::type::matrix::INT64}, + {"float", ::armarx::aron::type::matrix::FLOAT32}, + {"double", ::armarx::aron::type::matrix::FLOAT64}}; + + template <typename T> + void + readTo(const nlohmann::json& input, std::vector<unsigned char>& data) + { + const std::vector<T> d = input.at("data").get<std::vector<T>>(); + + const std::size_t bufferLen = d.size() * sizeof(T); + + data.resize(bufferLen); + memcpy(data.data(), d.data(), bufferLen); + } + void NlohmannJSONReaderWithoutTypeCheck::readNDArray(const nlohmann::json& input, std::vector<int>& shape, @@ -64,7 +92,37 @@ namespace armarx::aron::data::reader Path& p) { shape = input.at("dims").get<std::vector<int>>(); - data = input.at("data").get<std::vector<unsigned char>>(); + + typeAsString = input.at("type").get<std::string>(); + + ARMARX_CHECK(ElementTypeAsString.count(typeAsString) > 0) + << "Invalid element `" << typeAsString << "`. Valid elements are " + << simox::alg::get_keys(ElementTypeAsString) << "."; + + const type::matrix::ElementType elementType = ElementTypeAsString.at(typeAsString); + + // as we need to return a vector<uchar>, we first need to read the json array as the specific type + // and then convert it to the vector<uchar> + switch (elementType) + { + case type::matrix::INT16: + readTo<std::int16_t>(input, data); + break; + case type::matrix::INT32: + readTo<std::int32_t>(input, data); + break; + case type::matrix::INT64: + readTo<std::int64_t>(input, data); + break; + case type::matrix::FLOAT32: + readTo<std::float_t>(input, data); + break; + case type::matrix::FLOAT64: + readTo<std::double_t>(input, data); + break; + } + + ARMARX_INFO << VAROUT(typeAsString); } void -- GitLab