From 9efe13a70548436e17d949a7c5ac8dba9ef62eea Mon Sep 17 00:00:00 2001
From: ManifoldFR <wilson.jallet@polytechnique.org>
Date: Sat, 5 Nov 2022 11:52:41 +0100
Subject: [PATCH] update std-vector.hpp and test

std-map/std-vector: add mention to file heading
---
 include/eigenpy/std-map.hpp        |  7 ++--
 include/eigenpy/std-vector.hpp     | 58 +++++++++++++++++++++---------
 unittest/python/test_std_vector.py | 34 +++++++++---------
 3 files changed, 63 insertions(+), 36 deletions(-)

diff --git a/include/eigenpy/std-map.hpp b/include/eigenpy/std-map.hpp
index 4af4f3b0..b6748f57 100644
--- a/include/eigenpy/std-map.hpp
+++ b/include/eigenpy/std-map.hpp
@@ -1,6 +1,7 @@
-//
-// Copyright (c) 2020 INRIA
-//
+/// Copyright (c) 2016-2022 CNRS INRIA
+/// This file was taken from Pinocchio (header
+/// <pinocchio/bindings/python/utils/std-vector.hpp>)
+///
 
 #ifndef __eigenpy_utils_map_hpp__
 #define __eigenpy_utils_map_hpp__
diff --git a/include/eigenpy/std-vector.hpp b/include/eigenpy/std-vector.hpp
index 39612b15..9eb32ced 100644
--- a/include/eigenpy/std-vector.hpp
+++ b/include/eigenpy/std-vector.hpp
@@ -1,6 +1,7 @@
-//
-// Copyright (c) 2016-2022 CNRS INRIA
-//
+/// Copyright (c) 2016-2022 CNRS INRIA
+/// This file was taken from Pinocchio (header
+/// <pinocchio/bindings/python/utils/std-vector.hpp>)
+///
 
 #ifndef __eigenpy_utils_std_vector_hpp__
 #define __eigenpy_utils_std_vector_hpp__
@@ -77,7 +78,7 @@ struct build_list<vector_type, true> {
 template <typename Container>
 struct overload_base_get_item_for_std_vector
     : public boost::python::def_visitor<
-          overload_base_get_item_for_std_vector<Container> > {
+          overload_base_get_item_for_std_vector<Container>> {
   typedef typename Container::value_type value_type;
   typedef typename Container::value_type data_type;
   typedef size_t index_type;
@@ -130,15 +131,9 @@ struct overload_base_get_item_for_std_vector
 namespace boost {
 namespace python {
 
-/// \brief Specialization of the boost::python::extract struct for references to
-/// Eigen matrix objects.
-template <typename Scalar, int Rows, int Cols, int Options, int MaxRows,
-          int MaxCols>
-struct extract<Eigen::Matrix<Scalar, Rows, Cols, Options, MaxRows, MaxCols> &>
-    : converter::extract_rvalue<Eigen::Ref<
-          Eigen::Matrix<Scalar, Rows, Cols, Options, MaxRows, MaxCols> > > {
-  typedef Eigen::Matrix<Scalar, Rows, Cols, Options, MaxRows, MaxCols>
-      MatrixType;
+template <typename MatrixType>
+struct extract_to_eigen_ref
+    : converter::extract_rvalue<Eigen::Ref<MatrixType>> {
   typedef Eigen::Ref<MatrixType> RefType;
 
  private:
@@ -147,10 +142,41 @@ struct extract<Eigen::Matrix<Scalar, Rows, Cols, Options, MaxRows, MaxCols> &>
  public:
   typedef RefType result_type;
 
-  operator result_type() const { return (*this)(); }
+  operator result_type() const {
+    return (*this)();
+  }
+
+  extract_to_eigen_ref(PyObject *o) : base(o) {}
+  extract_to_eigen_ref(api::object const &o) : base(o.ptr()) {}
+};
+
+/// \brief Specialization of the boost::python::extract struct for references to
+/// Eigen matrix objects.
+template <typename Scalar, int Rows, int Cols, int Options, int MaxRows,
+          int MaxCols>
+struct extract<Eigen::Matrix<Scalar, Rows, Cols, Options, MaxRows, MaxCols> &>
+    : extract_to_eigen_ref<
+          Eigen::Matrix<Scalar, Rows, Cols, Options, MaxRows, MaxCols>> {
+  typedef Eigen::Matrix<Scalar, Rows, Cols, Options, MaxRows, MaxCols>
+      MatrixType;
+  typedef extract_to_eigen_ref<MatrixType> base;
+  using base::base;
+};
+
+template <typename Derived>
+struct extract<Eigen::MatrixBase<Derived> &>
+    : extract_to_eigen_ref<Eigen::MatrixBase<Derived>> {
+  typedef Eigen::MatrixBase<Derived> MatrixType;
+  typedef extract_to_eigen_ref<MatrixType> base;
+  using base::base;
+};
 
-  extract(PyObject *o) : base(o) {}
-  extract(api::object const &o) : base(o.ptr()) {}
+template <typename Derived>
+struct extract<Eigen::RefBase<Derived> &>
+    : extract_to_eigen_ref<Eigen::RefBase<Derived>> {
+  typedef Eigen::RefBase<Derived> MatrixType;
+  typedef extract_to_eigen_ref<MatrixType> base;
+  using base::base;
 };
 
 namespace converter {
diff --git a/unittest/python/test_std_vector.py b/unittest/python/test_std_vector.py
index 7bb74467..9e976ab5 100644
--- a/unittest/python/test_std_vector.py
+++ b/unittest/python/test_std_vector.py
@@ -9,7 +9,8 @@ np.random.seed(0)
 
 l1 = [np.random.randn(3), np.random.randn(2)]
 l2 = eigenpy.StdVec_VectorXd(l1)
-l3 = [np.random.randn(2, 2), np.random.randn(3, 1), np.random.randn(4, 2)]
+l3 = [np.random.randn(2, 2).T, np.random.randn(3, 1), np.random.randn(4, 2)]
+l4 = [np.random.randn(3, 3).T for _ in range(4)]
 
 
 def checkAllValues(li1, li2):
@@ -20,7 +21,6 @@ def checkAllValues(li1, li2):
 
 
 checkAllValues(l1, l2)
-print("firstcall")
 checkAllValues(l1, copyStdVector(l1))
 
 print(l2[0])
@@ -29,22 +29,20 @@ printVectorOfMatrix(l2)
 
 assert np.allclose(l2[0][:2], 0.0)
 
+print("l1")
 printVectorOfMatrix(l1)
-print()
+print("l2")
 printVectorOfMatrix(l2)
-print()
+print("l3")
 printVectorOfMatrix(l3)
-print()
 
 
-l4 = [np.random.randn(3, 3) for _ in range(4)]
-assert "StdVec_Mat3d" in printVectorOf3x3.__doc__
-printVectorOf3x3(l4)
-print()
-
 l4_copy = copyStdVector(l4)
 assert isinstance(l4_copy, eigenpy.StdVec_MatrixXd)
 
+assert "StdVec_Mat3d" in printVectorOf3x3.__doc__
+printVectorOf3x3(l4)
+
 l4_copy2 = vector.copyStdVec_3x3(l4)
 assert isinstance(l4_copy2, vector.StdVec_Mat3d)
 
@@ -67,11 +65,13 @@ pprint.pp(l2_py)
 checkZero(l2_py)
 print("-----------------")
 
-l3_std = eigenpy.StdVec_MatrixXd(l3)
-# l3_std = vector.StdVec_MatRef(l3)
-# l3_std = l3
+l3_copy = copyStdVector(l3)
+print("l3_std:")
+vector.setZero(l3_copy)
+pprint.pp(list(l3_copy))
+checkZero(l3_copy)
+
 print("l3:")
-vector.setZero(l3_std)
-print("result")
-pprint.pp(list(l3_std))
-checkZero(l3_std)
+vector.setZero(l3)
+pprint.pp(list(l3))
+# checkZero(l3)
-- 
GitLab