From d65dab6ee6c3c61b1c7cb212749c5a94e28f0bc7 Mon Sep 17 00:00:00 2001
From: Fabian Reister <fabian.reister@kit.edu>
Date: Mon, 26 Apr 2021 08:26:11 +0200
Subject: [PATCH] aron converter: common converter + vector converter; cmake
 aliases and interface library

---
 .../libraries/aron/converter/CMakeLists.txt   | 15 ++++
 .../aron/converter/common/CMakeLists.txt      | 23 ++++++
 .../aron/converter/common/Converter.cpp       |  1 +
 .../aron/converter/common/Converter.h         | 71 +++++++++++++++++++
 .../aron/converter/common/VectorConverter.cpp |  1 +
 .../aron/converter/common/VectorConverter.h   | 69 ++++++++++++++++++
 .../aron/converter/eigen/CMakeLists.txt       |  5 +-
 .../aron/converter/ivt/CMakeLists.txt         | 11 ++-
 .../aron/converter/opencv/CMakeLists.txt      | 11 ++-
 .../aron/converter/pcl/CMakeLists.txt         | 10 ++-
 10 files changed, 208 insertions(+), 9 deletions(-)
 create mode 100644 source/RobotAPI/libraries/aron/converter/common/CMakeLists.txt
 create mode 100644 source/RobotAPI/libraries/aron/converter/common/Converter.cpp
 create mode 100644 source/RobotAPI/libraries/aron/converter/common/Converter.h
 create mode 100644 source/RobotAPI/libraries/aron/converter/common/VectorConverter.cpp
 create mode 100644 source/RobotAPI/libraries/aron/converter/common/VectorConverter.h

diff --git a/source/RobotAPI/libraries/aron/converter/CMakeLists.txt b/source/RobotAPI/libraries/aron/converter/CMakeLists.txt
index 637345f2b..a99bfccfd 100644
--- a/source/RobotAPI/libraries/aron/converter/CMakeLists.txt
+++ b/source/RobotAPI/libraries/aron/converter/CMakeLists.txt
@@ -1,4 +1,19 @@
+add_subdirectory(common)
 add_subdirectory(ivt)
 add_subdirectory(pcl)
 add_subdirectory(eigen)
 add_subdirectory(opencv)
+
+
+add_library(AronConverter INTERFACE)
+
+target_link_libraries(AronConverter
+    INTERFACE
+        RobotAPI::aron::converter::common    
+        RobotAPI::aron::converter::ivt    
+        RobotAPI::aron::converter::pcl    
+        RobotAPI::aron::converter::eigen    
+        RobotAPI::aron::converter::opencv    
+)
+
+add_library(RobotAPI::aron::converter ALIAS AronConverter)
diff --git a/source/RobotAPI/libraries/aron/converter/common/CMakeLists.txt b/source/RobotAPI/libraries/aron/converter/common/CMakeLists.txt
new file mode 100644
index 000000000..ad1ddc366
--- /dev/null
+++ b/source/RobotAPI/libraries/aron/converter/common/CMakeLists.txt
@@ -0,0 +1,23 @@
+set(LIB_NAME aroncommonconverter)
+
+armarx_component_set_name("${LIB_NAME}")
+armarx_set_target("Library: ${LIB_NAME}")
+
+set(LIBS
+    aron
+)
+
+set(LIB_FILES
+    Converter.cpp
+    VectorConverter.cpp
+)
+
+set(LIB_HEADERS
+    Converter.h
+    VectorConverter.h
+)
+
+armarx_add_library("${LIB_NAME}" "${LIB_FILES}" "${LIB_HEADERS}" "${LIBS}")
+
+
+add_library(RobotAPI::aron::converter::common ALIAS aroncommonconverter)
diff --git a/source/RobotAPI/libraries/aron/converter/common/Converter.cpp b/source/RobotAPI/libraries/aron/converter/common/Converter.cpp
new file mode 100644
index 000000000..c5161e17c
--- /dev/null
+++ b/source/RobotAPI/libraries/aron/converter/common/Converter.cpp
@@ -0,0 +1 @@
+#include "Converter.h"
\ No newline at end of file
diff --git a/source/RobotAPI/libraries/aron/converter/common/Converter.h b/source/RobotAPI/libraries/aron/converter/common/Converter.h
new file mode 100644
index 000000000..570344ab6
--- /dev/null
+++ b/source/RobotAPI/libraries/aron/converter/common/Converter.h
@@ -0,0 +1,71 @@
+/*
+ * This file is part of ArmarX.
+ *
+ * ArmarX is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License version 2 as
+ * published by the Free Software Foundation.
+ *
+ * ArmarX is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ *
+ * @author     Fabian Reister ( fabian dot reister at kit dot edu )
+ * @date       2021
+ * @copyright  http://www.gnu.org/licenses/gpl-2.0.txt
+ *             GNU General Public License
+ */
+
+#pragma once
+
+#include <algorithm>
+#include <vector>
+
+namespace armarx::aron
+{
+
+    /**
+     * @brief Converter function for vector of aron elements to plain cpp type
+     *
+     *  You have to provide a converter function for the element with the signature
+     *
+     *      PlainCppType fromAron(const AronType&)
+     * 
+     * @tparam T the aron vector element
+     * @param v the vector of elements
+     * @return the vector of aron elements
+     */
+    template <typename T> 
+    auto fromAron(const std::vector<T>& v) -> std::vector<decltype(fromAron(T()))>
+    {
+        std::vector<decltype(fromAron(T()))> r;
+        r.reserve(v.size());
+
+        std::transform(v.begin(), v.end(), std::back_inserter(r),
+                       [](const T & t)
+        {
+            return fromAron(t);
+        });
+
+        return r;
+    }
+
+
+    template <typename T> auto toAron(const std::vector<T>& v)
+    {
+        std::vector<decltype(toAron(T()))> r;
+        r.reserve(v.size());
+
+        std::transform(v.begin(), v.end(), std::back_inserter(r),
+                       [](const T & t)
+        {
+            return toAron(t);
+        });
+
+        return r;
+    }
+
+} // namespace armarx::aron
diff --git a/source/RobotAPI/libraries/aron/converter/common/VectorConverter.cpp b/source/RobotAPI/libraries/aron/converter/common/VectorConverter.cpp
new file mode 100644
index 000000000..5f5406cc2
--- /dev/null
+++ b/source/RobotAPI/libraries/aron/converter/common/VectorConverter.cpp
@@ -0,0 +1 @@
+#include "VectorConverter.h"
\ No newline at end of file
diff --git a/source/RobotAPI/libraries/aron/converter/common/VectorConverter.h b/source/RobotAPI/libraries/aron/converter/common/VectorConverter.h
new file mode 100644
index 000000000..17475a3b9
--- /dev/null
+++ b/source/RobotAPI/libraries/aron/converter/common/VectorConverter.h
@@ -0,0 +1,69 @@
+/*
+ * This file is part of ArmarX.
+ *
+ * ArmarX is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License version 2 as
+ * published by the Free Software Foundation.
+ *
+ * ArmarX is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ *
+ * @author     Fabian Reister ( fabian dot reister at kit dot edu )
+ * @date       2021
+ * @copyright  http://www.gnu.org/licenses/gpl-2.0.txt
+ *             GNU General Public License
+ */
+
+#pragma once
+
+// STD/STL
+#include <memory>
+#include <string>
+#include <numeric>
+
+// ArmarX
+#include <ArmarXCore/core/exceptions/local/ExpressionException.h>
+
+#include <RobotAPI/interface/aron.h>
+#include <RobotAPI/libraries/aron/core/navigator/data/complex/NDArray.h>
+
+
+namespace armarx::aron::converter
+{
+    class AronVectorConverter
+    {
+    public:
+        AronVectorConverter() = delete;
+       
+        template<typename T>
+        static std::vector<T> ConvertToVector(const datanavigator::NDArrayNavigatorPtr& nav)
+        {
+            ARMARX_CHECK_NOT_NULL(nav);
+
+            const auto& dims = nav->getDimensions();
+
+            if(dims.size() != 2)
+            {
+                throw error::AronException("AronVectorConverter", "ConvertToVector", "The NDArray must have two dimensions.", nav->getPath());
+            }
+            
+            if(dims.at(1) != sizeof(T))
+            {
+                throw error::AronException("AronVectorConverter", "ConvertToVector", "Dimension 1 of the array has to match the element size.", nav->getPath());
+            }
+            
+            const int size = std::accumulate(std::begin(dims), std::end(dims), 1, std::multiplies<>());
+
+            std::vector<T> v(dims.at(0));
+            memcpy(v.data(), nav->getData(), size);
+
+            return v;
+        }
+    };
+
+}  // namespace armarx::aron::converter
diff --git a/source/RobotAPI/libraries/aron/converter/eigen/CMakeLists.txt b/source/RobotAPI/libraries/aron/converter/eigen/CMakeLists.txt
index 44b70e2e8..d4f9008ea 100644
--- a/source/RobotAPI/libraries/aron/converter/eigen/CMakeLists.txt
+++ b/source/RobotAPI/libraries/aron/converter/eigen/CMakeLists.txt
@@ -8,6 +8,7 @@ armarx_build_if(Eigen3_FOUND "Eigen3 not available")
 
 set(LIBS
     aron
+    Eigen3::Eigen
 )
 
 set(LIB_FILES
@@ -20,6 +21,4 @@ set(LIB_HEADERS
 
 armarx_add_library("${LIB_NAME}" "${LIB_FILES}" "${LIB_HEADERS}" "${LIBS}")
 
-if(Eigen3_FOUND)
-    include_directories(${Eigen3_INCLUDE_DIR})
-endif()
+add_library(RobotAPI::aron::converter::eigen ALIAS aroneigenconverter)
diff --git a/source/RobotAPI/libraries/aron/converter/ivt/CMakeLists.txt b/source/RobotAPI/libraries/aron/converter/ivt/CMakeLists.txt
index a7651058d..376a1ae7b 100644
--- a/source/RobotAPI/libraries/aron/converter/ivt/CMakeLists.txt
+++ b/source/RobotAPI/libraries/aron/converter/ivt/CMakeLists.txt
@@ -7,7 +7,9 @@ find_package(IVT COMPONENTS ivt ivtopencv QUIET)
 armarx_build_if(IVT_FOUND "IVT not available")
 
 set(LIBS
-    aron ivt ivtopencv
+    aron 
+    ivt 
+    ivtopencv
 )
 
 set(LIB_FILES
@@ -21,5 +23,10 @@ set(LIB_HEADERS
 armarx_add_library("${LIB_NAME}" "${LIB_FILES}" "${LIB_HEADERS}" "${LIBS}")
 
 if(IVT_FOUND)
-    include_directories(${IVT_INCLUDE_DIRS})
+    target_include_directories(aronivtconverter
+        SYSTEM PUBLIC 
+            ${IVT_INCLUDE_DIRS}
+    )
 endif()
+
+add_library(RobotAPI::aron::converter::ivt ALIAS aronivtconverter)
diff --git a/source/RobotAPI/libraries/aron/converter/opencv/CMakeLists.txt b/source/RobotAPI/libraries/aron/converter/opencv/CMakeLists.txt
index 1b54988b2..5cbe0acad 100644
--- a/source/RobotAPI/libraries/aron/converter/opencv/CMakeLists.txt
+++ b/source/RobotAPI/libraries/aron/converter/opencv/CMakeLists.txt
@@ -7,7 +7,8 @@ find_package(OpenCV QUIET)
 armarx_build_if(OpenCV_FOUND "OpenCV not available")
 
 set(LIBS
-    aron ${OpenCV_LIBRARIES}
+    aron 
+    ${OpenCV_LIBRARIES}
 )
 
 set(LIB_FILES
@@ -21,5 +22,11 @@ set(LIB_HEADERS
 armarx_add_library("${LIB_NAME}" "${LIB_FILES}" "${LIB_HEADERS}" "${LIBS}")
 
 if(OpenCV_FOUND)
-    include_directories(${OpenCV_INCLUDE_DIRS})
+    target_include_directories(aronopencvconverter
+        SYSTEM PUBLIC  
+            ${OpenCV_INCLUDE_DIRS}
+    )
 endif()
+
+
+add_library(RobotAPI::aron::converter::opencv ALIAS aronopencvconverter)
diff --git a/source/RobotAPI/libraries/aron/converter/pcl/CMakeLists.txt b/source/RobotAPI/libraries/aron/converter/pcl/CMakeLists.txt
index 0a1c4532d..c14d50a5f 100644
--- a/source/RobotAPI/libraries/aron/converter/pcl/CMakeLists.txt
+++ b/source/RobotAPI/libraries/aron/converter/pcl/CMakeLists.txt
@@ -7,7 +7,8 @@ find_package(PCL QUIET)
 armarx_build_if(PCL_FOUND "PCL not available")
 
 set(LIBS
-    aron ${PCL_COMMON_LIBRARIES}
+    aron 
+    ${PCL_COMMON_LIBRARIES}
 )
 
 set(LIB_FILES
@@ -21,5 +22,10 @@ set(LIB_HEADERS
 armarx_add_library("${LIB_NAME}" "${LIB_FILES}" "${LIB_HEADERS}" "${LIBS}")
 
 if(PCL_FOUND)
-    include_directories(${PCL_INCLUDE_DIRS})
+    target_include_directories(aronpclconverter 
+        SYSTEM PUBLIC
+            ${PCL_INCLUDE_DIRS}
+    )
 endif()
+
+add_library(RobotAPI::aron::converter::pcl ALIAS aronpclconverter)
-- 
GitLab