Skip to content
Snippets Groups Projects
Commit c42f3fe2 authored by Fabian Reister's avatar Fabian Reister
Browse files

nd array reading

parent b2fddaec
No related branches found
No related tags found
No related merge requests found
......@@ -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
......
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