Skip to content
Snippets Groups Projects
Commit 0c74d6e3 authored by Rainer Kartmann's avatar Rainer Kartmann
Browse files

Changes to OpenCVMat - will be deleted later anyway

parent 111c8093
No related branches found
No related tags found
No related merge requests found
......@@ -89,7 +89,8 @@ module armarx
// to be removed:
class AronIVTCByteImage extends AronType { }
class AronOpenCVMat extends AronType { }
class AronOpenCVMat extends AronType { AronIntSequence shape; string typeName; }
class AronPCLPointCloud extends AronType { string typeName; }
class AronPosition extends AronType { }
class AronOrientation extends AronType { }
......
......@@ -32,49 +32,78 @@ namespace armarx::aron::cppserializer::serializer
const std::map<std::string, std::pair<std::string, int>> OpenCVMatSerializer::ACCEPTED_TYPES =
{
{"CV_8U", {"CV_8U", 1}},
{"CV_8S", {"CV_8S", 1}},
{"CV_16U", {"CV_16U", 2}},
{"CV_16S", {"CV_16S", 2}},
{"CV_32S", {"CV_32S", 4}},
{"CV_32F", {"CV_32F", 4}},
{"CV_64F", {"CV_64F", 8}},
{ "8u", {"CV_8U", 1}},
{ "8s", {"CV_8S", 1}},
{"16u", {"CV_16U", 2}},
{"16s", {"CV_16S", 2}},
{"32s", {"CV_32S", 4}},
{"32f", {"CV_32F", 4}},
{"64f", {"CV_64F", 8}},
};
// constructors
OpenCVMatSerializer::OpenCVMatSerializer(const typenavigator::OpenCVMatNavigatorPtr& n) :
detail::NDArraySerializerBase<typenavigator::OpenCVMatNavigator, OpenCVMatSerializer>("cv::Mat",
simox::meta::get_type_name(typeid(data::AronNDArray)),
simox::meta::get_type_name(typeid(type::AronOpenCVMat)), n)
detail::NDArraySerializerBase<typenavigator::OpenCVMatNavigator, OpenCVMatSerializer>(
"cv::Mat",
simox::meta::get_type_name<data::AronNDArray>(),
simox::meta::get_type_name<type::AronOpenCVMat>(),
n)
{
ARMARX_CHECK_NOT_NULL(typenavigator);
}
std::pair<CppBlockPtr, std::string> OpenCVMatSerializer::getDimensionsFromAccessor(const std::string& accessor) const
std::pair<CppBlockPtr, std::string> OpenCVMatSerializer::getShapeFromAccessor(const std::string& accessor) const
{
CppBlockPtr b = CppBlockPtr(new CppBlock());
std::string escaped_accessor = EscapeAccessor(accessor);
std::string accessor_dimensions = escaped_accessor + DIMENSION_ACCESSOR;
std::string accessor_shape = escaped_accessor + SHAPE_ACCESSOR;
std::string accessor_iterator = escaped_accessor + ITERATOR_ACCESSOR;
b->addLine("std::vector<int> " + accessor_dimensions + ";");
b->addLine("std::vector<int> " + accessor_shape + ";");
b->addLine("for (int " + accessor_iterator + " = 0; " + accessor_iterator + " < " + accessor + nextEl() + "dims; ++" + accessor_iterator + ")");
CppBlockPtr b2 = CppBlockPtr(new CppBlock());
b2->addLine(accessor_dimensions + ".push_back(" + accessor + nextEl() + "size[" + accessor_iterator + "]);");
b2->addLine(accessor_shape + ".push_back(" + accessor + nextEl() + "size[" + accessor_iterator + "]);");
b->addBlock(b2);
return {b, accessor_dimensions};
return {b, accessor_shape};
}
CppBlockPtr OpenCVMatSerializer::getResetHardBlock(const std::string& accessor) const
{
CppBlockPtr block_if_data = CppBlockPtr(new CppBlock());
std::string sizes;
{
const std::vector<int> shape = typenavigator->getShape();
sizes = simox::alg::join(simox::alg::multi_to_string(shape), ", ");
sizes = "std::vector<int>{ " + sizes + " }";
}
std::string type = typenavigator->getTypeName();
{
type = simox::alg::to_lower(type);
type = simox::alg::remove_prefix(type, "cv_");
if (auto it = ACCEPTED_TYPES.find(type); it != ACCEPTED_TYPES.end())
{
type = it->second.first;
}
}
block_if_data->addLine(accessor + " = " + this->getFullTypenameGenerator()
+ "(" + sizes + ", " + type + ");");
return this->ResolveMaybeResetHardBlock(accessor, block_if_data, this->typenavigator);
}
CppBlockPtr OpenCVMatSerializer::getResetSoftBlock(const std::string& accessor) const
{
CppBlockPtr block_if_data = CppBlockPtr(new CppBlock());
auto [get_dim_block, accessor_dimensions] = getDimensionsFromAccessor(accessor);
auto [get_dim_block, accessor_shape] = getShapeFromAccessor(accessor);
block_if_data->appendBlock(get_dim_block);
block_if_data->addLine("if (!" + accessor_dimensions + ".empty())");
block_if_data->addLine("if (!" + accessor_shape + ".empty())");
CppBlockPtr b2 = CppBlockPtr(new CppBlock());
b2->addLine(accessor + " = " + getFullCppTypename() + "(" + accessor_dimensions + ", " + accessor + nextEl() + "type());");
b2->addLine(accessor + " = " + getFullCppTypename() + "(" + accessor_shape + ", " + accessor + nextEl() + "type());");
block_if_data->addBlock(b2);
return ResolveMaybeResetSoftBlock(accessor, block_if_data, typenavigator);
......@@ -93,10 +122,10 @@ namespace armarx::aron::cppserializer::serializer
std::string escaped_accessor = EscapeAccessor(accessor);
auto [get_dim_block, accessor_dimensions] = getDimensionsFromAccessor(accessor);
auto [get_dim_block, accessor_shape] = getShapeFromAccessor(accessor);
block_if_data->appendBlock(get_dim_block);
block_if_data->addLine(accessor_dimensions + ".push_back(" + accessor + nextEl() + "elemSize());");
block_if_data->addLine("w.writeNDArray(" + accessor_dimensions + ", std::to_string(" + accessor + nextEl() + "type()), reinterpret_cast<const unsigned char*>(" + accessor + nextEl() + "data)); // of " + accessor);
block_if_data->addLine(accessor_shape + ".push_back(" + accessor + nextEl() + "elemSize());");
block_if_data->addLine("w.writeNDArray(" + accessor_shape + ", std::to_string(" + accessor + nextEl() + "type()), reinterpret_cast<const unsigned char*>(" + accessor + nextEl() + "data)); // of " + accessor);
return ResolveMaybeWriteBlock(accessor, block_if_data, typenavigator);
}
......
......@@ -35,20 +35,18 @@
namespace armarx::aron::cppserializer::serializer
{
class OpenCVMatSerializer;
typedef std::shared_ptr<OpenCVMatSerializer> AronOpenCVMatTypeCppSerializerPtr;
class OpenCVMatSerializer :
virtual public detail::NDArraySerializerBase<typenavigator::OpenCVMatNavigator, OpenCVMatSerializer>
{
public:
using PointerType = AronOpenCVMatTypeCppSerializerPtr;
using PointerType = std::shared_ptr<OpenCVMatSerializer>;
public:
// constructors
OpenCVMatSerializer(const typenavigator::OpenCVMatNavigatorPtr& n);
// virtual implementations
virtual CppBlockPtr getResetHardBlock(const std::string& accessor) const override;
virtual CppBlockPtr getResetSoftBlock(const std::string& accessor) const override;
virtual CppBlockPtr getWriteTypeBlock(const std::string&) const override;
virtual CppBlockPtr getWriteBlock(const std::string&) const override;
......@@ -56,13 +54,14 @@ namespace armarx::aron::cppserializer::serializer
virtual CppBlockPtr getEqualsBlock(const std::string&, const std::string&) const override;
private:
std::pair<CppBlockPtr, std::string> getDimensionsFromAccessor(const std::string& accessor) const;
std::pair<CppBlockPtr, std::string> getShapeFromAccessor(const std::string& accessor) const;
private:
// members
static const std::map<std::string, std::pair<std::string, int>> ACCEPTED_TYPES;
static constexpr const char* ITERATOR_ACCESSOR = "_iterator";
static constexpr const char* DIMENSION_ACCESSOR = "_dimensions";
static constexpr const char* NUM_DIMENSION_ACCESSOR = "_num_dimensions";
static constexpr const char* SHAPE_ACCESSOR = "_shape";
static constexpr const char* NDIM_ACCESSOR = "_ndim";
};
}
......@@ -74,6 +74,7 @@ namespace armarx::aron::xmltypereader
static constexpr const char* ROWS_ATTRIBUTE_NAME = "rows";
static constexpr const char* COLS_ATTRIBUTE_NAME = "cols";
static constexpr const char* DIMENSIONS_ATTRIBUTE_NAME = "dimensions";
static constexpr const char* SHAPE_ATTRIBUTE_NAME = "shape";
static constexpr const char* OPTIONAL_NAME = "optional";
static constexpr const char* RAW_PTR_NAME = "raw_ptr";
static constexpr const char* SHARED_PTR_NAME = "shared_ptr";
......
......@@ -346,6 +346,25 @@ namespace armarx::aron::xmltypereader
Data::EnforceTagName(node, Data::GENERATE_OPENCV_MAT_MEMBER_TAG);
typenavigator::OpenCVMatNavigatorPtr complex(new typenavigator::OpenCVMatNavigator(path));
std::vector<int> shape;
{
std::string shapeStr = node.attribute_value(Data::SHAPE_ATTRIBUTE_NAME);
const bool trim = true, removeEmpty = false;
std::vector<std::string> split = simox::alg::split(shapeStr, ",", trim, removeEmpty);
shape.reserve(split.size());
for (const std::string& entry : split)
{
// What happens if this throws an error?
int size = std::stoi(entry);
shape.push_back(size);
}
}
complex->setShape(shape);
std::string typeName = node.attribute_value(Data::TYPE_ATTRIBUTE_NAME);
complex->setTypeName(typeName);
return complex;
}
......
......@@ -26,18 +26,21 @@
namespace armarx::aron::typenavigator
{
// constructors
OpenCVMatNavigator::OpenCVMatNavigator(const Path& path) :
aron::Navigator<type::Descriptor, type::AronType>::Navigator(type::Descriptor::eOpenCVMat, path)
{
}
OpenCVMatNavigator::OpenCVMatNavigator(const type::AronOpenCVMatPtr& o, const Path& path) :
aron::Navigator<type::Descriptor, type::AronType>::Navigator(type::Descriptor::eOpenCVMat, path),
detail::NavigatorBase<type::AronOpenCVMat, OpenCVMatNavigator>(o)
{
}
// operators
bool OpenCVMatNavigator::operator==(const OpenCVMatNavigator& other) const
{
......@@ -48,15 +51,42 @@ namespace armarx::aron::typenavigator
return true;
}
type::AronOpenCVMatPtr OpenCVMatNavigator::toAronOpenCVMatPtr() const
{
return this->aron;
}
std::vector<int> OpenCVMatNavigator::getShape() const
{
return this->aron->shape;
}
void OpenCVMatNavigator::setShape(const std::vector<int>& shape) const
{
this->aron->shape = shape;
}
std::string OpenCVMatNavigator::getTypeName() const
{
return this->aron->typeName;
}
void OpenCVMatNavigator::setTypeName(const std::string& typeName)
{
this->aron->typeName = typeName;
}
// virtual implementations
std::string OpenCVMatNavigator::getName() const
{
return "AronOpenCVMatType";
}
}
......@@ -30,6 +30,7 @@
// Base class
#include "../detail/NDArrayNavigatorBase.h"
namespace armarx::aron::typenavigator
{
class OpenCVMatNavigator;
......@@ -49,9 +50,16 @@ namespace armarx::aron::typenavigator
// public member functions
type::AronOpenCVMatPtr toAronOpenCVMatPtr() const;
std::vector<int> getShape() const;
void setShape(const std::vector<int>& shape) const;
std::string getTypeName() const;
void setTypeName(const std::string& typeName);
// virtual implementations
virtual std::string getName() const override;
public:
const std::map<std::string, std::vector<std::string>> ACCEPTED_TYPES =
{
......
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