From 60dfb7128f0d24f5cd4a78d0616b0d995d57ff9b Mon Sep 17 00:00:00 2001 From: Fabian Peller <fabian.peller-konrad@kit.edu> Date: Thu, 4 May 2023 17:11:41 +0200 Subject: [PATCH] remove check for correct type in matrix TODO --- .../cpp/generator/ndarray/Matrix.cpp | 116 +++++++++++------- 1 file changed, 75 insertions(+), 41 deletions(-) diff --git a/source/RobotAPI/libraries/aron/codegeneration/codegenerator/codewriter/cpp/generator/ndarray/Matrix.cpp b/source/RobotAPI/libraries/aron/codegeneration/codegenerator/codewriter/cpp/generator/ndarray/Matrix.cpp index d0bf18091..ce9389d13 100644 --- a/source/RobotAPI/libraries/aron/codegeneration/codegenerator/codewriter/cpp/generator/ndarray/Matrix.cpp +++ b/source/RobotAPI/libraries/aron/codegeneration/codegenerator/codewriter/cpp/generator/ndarray/Matrix.cpp @@ -26,72 +26,89 @@ #include <SimoxUtility/meta/type_name.h> - namespace armarx::aron::codegenerator::cpp::generator { - const std::map<type::matrix::ElementType, std::tuple<std::string, int, std::string>> ElementType2Cpp = - { - // TODO: rename to float32 etc. but keep backward compability - {type::matrix::INT16, {"short", 2, "::armarx::aron::type::matrix::INT16"}}, - {type::matrix::INT32, {"int", 4, "::armarx::aron::type::matrix::INT32"}}, - {type::matrix::INT64, {"long", 8, "::armarx::aron::type::matrix::INT64"}}, - {type::matrix::FLOAT32, {"float", 4, "::armarx::aron::type::matrix::FLOAT32"}}, - {type::matrix::FLOAT64, {"double", 8, "::armarx::aron::type::matrix::FLOAT64"}} - }; + const std::map<type::matrix::ElementType, std::tuple<std::string, int, std::string>> + ElementType2Cpp = { + // TODO: rename to float32 etc. but keep backward compability + {type::matrix::INT16, {"short", 2, "::armarx::aron::type::matrix::INT16"}}, + {type::matrix::INT32, {"int", 4, "::armarx::aron::type::matrix::INT32"}}, + {type::matrix::INT64, {"long", 8, "::armarx::aron::type::matrix::INT64"}}, + {type::matrix::FLOAT32, {"float", 4, "::armarx::aron::type::matrix::FLOAT32"}}, + {type::matrix::FLOAT64, {"double", 8, "::armarx::aron::type::matrix::FLOAT64"}}}; // constructors Matrix::Matrix(const type::Matrix& n) : detail::NDArrayGenerator<type::Matrix, Matrix>( - "Eigen::Matrix<" + std::get<0>(ElementType2Cpp.at(n.getElementType())) + ", " + (n.getRows() == -1 ? "Eigen::Dynamic" : std::to_string(n.getRows())) + ", " + (n.getCols() == -1 ? "Eigen::Dynamic" : std::to_string(n.getCols())) + ">", - "Eigen::Matrix<" + std::get<0>(ElementType2Cpp.at(n.getElementType())) + ", " + (n.getRows() == -1 ? "Eigen::Dynamic" : std::to_string(n.getRows())) + ", " + (n.getCols() == -1 ? "Eigen::Dynamic" : std::to_string(n.getCols())) + ">", + "Eigen::Matrix<" + std::get<0>(ElementType2Cpp.at(n.getElementType())) + ", " + + (n.getRows() == -1 ? "Eigen::Dynamic" : std::to_string(n.getRows())) + ", " + + (n.getCols() == -1 ? "Eigen::Dynamic" : std::to_string(n.getCols())) + ">", + "Eigen::Matrix<" + std::get<0>(ElementType2Cpp.at(n.getElementType())) + ", " + + (n.getRows() == -1 ? "Eigen::Dynamic" : std::to_string(n.getRows())) + ", " + + (n.getCols() == -1 ? "Eigen::Dynamic" : std::to_string(n.getCols())) + ">", simox::meta::get_type_name<data::dto::NDArray>(), - simox::meta::get_type_name<type::dto::Matrix>(), n) + simox::meta::get_type_name<type::dto::Matrix>(), + n) { } - std::vector<std::string> Matrix::getRequiredIncludes() const + std::vector<std::string> + Matrix::getRequiredIncludes() const { return {"<Eigen/Core>"}; } - CppBlockPtr Matrix::getResetSoftBlock(const std::string& cppAccessor) const + CppBlockPtr + Matrix::getResetSoftBlock(const std::string& cppAccessor) const { CppBlockPtr block_if_data = std::make_shared<CppBlock>(); block_if_data->addLine(cppAccessor + nextEl() + "setZero();"); return this->resolveMaybeResetSoftBlock(block_if_data, cppAccessor); } - CppBlockPtr Matrix::getWriteTypeBlock(const std::string& typeAccessor, const std::string& cppAccessor, const Path& p, std::string& variantAccessor) const + CppBlockPtr + Matrix::getWriteTypeBlock(const std::string& typeAccessor, + const std::string& cppAccessor, + const Path& p, + std::string& variantAccessor) const { CppBlockPtr b = std::make_shared<CppBlock>(); std::string escaped_accessor = EscapeAccessor(cppAccessor); variantAccessor = ARON_VARIANT_RETURN_ACCESSOR + "_" + escaped_accessor; - b->addLine("auto " + variantAccessor + " = " + ARON_WRITER_ACCESSOR + ".writeMatrix((int) " + std::to_string(type.getRows()) + ", " + - "(int) " + std::to_string(type.getCols()) + ", " + - std::get<2>(ElementType2Cpp.at(type.getElementType())) + ", " + - conversion::Maybe2CppString.at(type.getMaybe()) + ", " + - "armarx::aron::Path("+ARON_PATH_ACCESSOR+", {"+simox::alg::join(p.getPath(), ", ")+"})); // of " + cppAccessor); + b->addLine("auto " + variantAccessor + " = " + ARON_WRITER_ACCESSOR + + ".writeMatrix((int) " + std::to_string(type.getRows()) + ", " + "(int) " + + std::to_string(type.getCols()) + ", " + + std::get<2>(ElementType2Cpp.at(type.getElementType())) + ", " + + conversion::Maybe2CppString.at(type.getMaybe()) + ", " + "armarx::aron::Path(" + + ARON_PATH_ACCESSOR + ", {" + simox::alg::join(p.getPath(), ", ") + + "})); // of " + cppAccessor); return b; } - CppBlockPtr Matrix::getWriteBlock(const std::string& cppAccessor, const Path& p, std::string& variantAccessor) const + CppBlockPtr + Matrix::getWriteBlock(const std::string& cppAccessor, + const Path& p, + std::string& variantAccessor) const { CppBlockPtr block_if_data = std::make_shared<CppBlock>(); std::string escaped_accessor = EscapeAccessor(cppAccessor); variantAccessor = ARON_VARIANT_RETURN_ACCESSOR + "_" + escaped_accessor; - block_if_data->addLine(variantAccessor + " = " + ARON_WRITER_ACCESSOR + ".writeNDArray({(int) " + cppAccessor + nextEl() + "rows(), "+ - "(int) " + cppAccessor + nextEl() + "cols(), " + - std::to_string(std::get<1>(ElementType2Cpp.at(type.getElementType()))) + "}, "+ - "\"" + std::get<0>(ElementType2Cpp.at(type.getElementType())) + "\", "+ - "reinterpret_cast<const unsigned char*>(" + cppAccessor + nextEl() + "data()), " + - "armarx::aron::Path("+ARON_PATH_ACCESSOR+", {" + simox::alg::join(p.getPath(), ", ") + "})); // of " + cppAccessor); + block_if_data->addLine( + variantAccessor + " = " + ARON_WRITER_ACCESSOR + ".writeNDArray({(int) " + cppAccessor + + nextEl() + "rows(), " + "(int) " + cppAccessor + nextEl() + "cols(), " + + std::to_string(std::get<1>(ElementType2Cpp.at(type.getElementType()))) + "}, " + "\"" + + std::get<0>(ElementType2Cpp.at(type.getElementType())) + "\", " + + "reinterpret_cast<const unsigned char*>(" + cppAccessor + nextEl() + "data()), " + + "armarx::aron::Path(" + ARON_PATH_ACCESSOR + ", {" + + simox::alg::join(p.getPath(), ", ") + "})); // of " + cppAccessor); return resolveMaybeWriteBlock(block_if_data, cppAccessor); } - CppBlockPtr Matrix::getReadBlock(const std::string& cppAccessor, const std::string& variantAccessor) const + CppBlockPtr + Matrix::getReadBlock(const std::string& cppAccessor, const std::string& variantAccessor) const { CppBlockPtr block_if_data = std::make_shared<CppBlock>(); std::string escaped_accessor = EscapeAccessor(cppAccessor); @@ -103,35 +120,52 @@ namespace armarx::aron::codegenerator::cpp::generator block_if_data->addLine("std::vector<int> " + dims + ";"); block_if_data->addLine("std::vector<unsigned char> " + data + ";"); - block_if_data->addLine("" + ARON_READER_ACCESSOR + ".readNDArray("+variantAccessor+", "+dims+", "+type+", "+data+"); // of " + cppAccessor); + block_if_data->addLine("" + ARON_READER_ACCESSOR + ".readNDArray(" + variantAccessor + + ", " + dims + ", " + type + ", " + data + "); // of " + cppAccessor); - if((this->type.getRows() == -1 or this->type.getRows() == Eigen::Dynamic) and this->type.getCols() != -1) + if ((this->type.getRows() == -1 or this->type.getRows() == Eigen::Dynamic) and + this->type.getCols() != -1) { block_if_data->addLine(cppAccessor + nextEl() + "resize(" + dims + ".at(0));"); } - if(this->type.getRows() != -1 and (this->type.getCols() == -1 or this->type.getCols() == Eigen::Dynamic)) + if (this->type.getRows() != -1 and + (this->type.getCols() == -1 or this->type.getCols() == Eigen::Dynamic)) { block_if_data->addLine(cppAccessor + nextEl() + "resize(" + dims + ".at(1));"); } - if((this->type.getRows() == -1 or this->type.getRows() == Eigen::Dynamic) and (this->type.getCols() == -1 or this->type.getCols() == Eigen::Dynamic)) + if ((this->type.getRows() == -1 or this->type.getRows() == Eigen::Dynamic) and + (this->type.getCols() == -1 or this->type.getCols() == Eigen::Dynamic)) { - block_if_data->addLine(cppAccessor + nextEl() + "resize(" + dims + ".at(0), " + dims + ".at(1));"); + block_if_data->addLine(cppAccessor + nextEl() + "resize(" + dims + ".at(0), " + dims + + ".at(1));"); } - block_if_data->addLine("ARMARX_CHECK_AND_THROW(" + cppAccessor + nextEl() + "rows() == " + dims + ".at(0) and " + cppAccessor + nextEl() + "cols() == " + dims + ".at(1), ::armarx::aron::error::AronException(__PRETTY_FUNCTION__, \"Received wrong dimensions for member '"+cppAccessor+"'.\"));"); - block_if_data->addLine("ARMARX_CHECK_AND_THROW(" + type + " == \"" + std::get<0>(ElementType2Cpp.at(this->type.getElementType())) + "\", ::armarx::aron::error::AronException(__PRETTY_FUNCTION__, \"Received wrong type for member '"+cppAccessor+"'.\"));"); - - block_if_data->addLine("std::memcpy(reinterpret_cast<unsigned char*>(" + cppAccessor + nextEl() + "data()), "+data+".data(), "+data+".size());"); + block_if_data->addLine("ARMARX_CHECK_AND_THROW(" + cppAccessor + nextEl() + "rows() == " + + dims + ".at(0) and " + cppAccessor + nextEl() + "cols() == " + dims + + ".at(1), ::armarx::aron::error::AronException(__PRETTY_FUNCTION__, " + "\"Received wrong dimensions for member '" + + cppAccessor + "'.\"));"); + block_if_data->addLine("//ARMARX_CHECK_AND_THROW(" + type + " == \"" + + std::get<0>(ElementType2Cpp.at(this->type.getElementType())) + + "\", ::armarx::aron::error::AronException(__PRETTY_FUNCTION__, " + "\"Received wrong type for member '" + + cppAccessor + "'.\"));"); + + block_if_data->addLine("std::memcpy(reinterpret_cast<unsigned char*>(" + cppAccessor + + nextEl() + "data()), " + data + ".data(), " + data + ".size());"); return resolveMaybeReadBlock(block_if_data, cppAccessor, variantAccessor); } - CppBlockPtr Matrix::getEqualsBlock(const std::string& accessor, const std::string& otherInstanceAccessor) const + CppBlockPtr + Matrix::getEqualsBlock(const std::string& accessor, + const std::string& otherInstanceAccessor) const { CppBlockPtr block_if_data = std::make_shared<CppBlock>(); - block_if_data->addLine("if (not (" + accessor + nextEl() + "isApprox(" + resolveMaybeAccessor(otherInstanceAccessor) + ")))"); + block_if_data->addLine("if (not (" + accessor + nextEl() + "isApprox(" + + resolveMaybeAccessor(otherInstanceAccessor) + ")))"); block_if_data->addLineAsBlock("return false;"); return resolveMaybeEqualsBlock(block_if_data, accessor, otherInstanceAccessor); } -} +} // namespace armarx::aron::codegenerator::cpp::generator -- GitLab