From b869723e779408bd0d05d4f44569aef8f41afdd5 Mon Sep 17 00:00:00 2001
From: jcarpent <jcarpent@laas.fr>
Date: Mon, 11 Jun 2018 18:58:42 +0200
Subject: [PATCH] [Conversion] Handle zero-size vector

---
 include/eigenpy/map.hpp        |  2 ++
 unittest/matrix.cpp            | 17 ++++++++++++++++-
 unittest/python/test_matrix.py |  8 ++++++++
 3 files changed, 26 insertions(+), 1 deletion(-)

diff --git a/include/eigenpy/map.hpp b/include/eigenpy/map.hpp
index 71e6c4d2..7de53f5c 100644
--- a/include/eigenpy/map.hpp
+++ b/include/eigenpy/map.hpp
@@ -94,6 +94,8 @@ namespace eigenpy
 
       int rowMajor;
       if(  PyArray_NDIM(pyArray)==1 ) rowMajor = 0;
+      else if (PyArray_DIMS(pyArray)[0] == 0) rowMajor = 0; // handle zero-size vector
+      else if (PyArray_DIMS(pyArray)[1] == 0) rowMajor = 1; // handle zero-size vector
       else rowMajor = (PyArray_DIMS(pyArray)[0]>PyArray_DIMS(pyArray)[1])?0:1;
 
       assert( (PyArray_DIMS(pyArray)[rowMajor]< INT_MAX)
diff --git a/unittest/matrix.cpp b/unittest/matrix.cpp
index 8eff2946..b65d2f87 100644
--- a/unittest/matrix.cpp
+++ b/unittest/matrix.cpp
@@ -1,5 +1,5 @@
 /*
- * Copyright 2014, Nicolas Mansard, LAAS-CNRS
+ * Copyright 2014,2018, Nicolas Mansard, Justin Carpentier, LAAS-CNRS
  *
  * This file is part of eigenpy.
  * eigenpy is free software: you can redistribute it and/or
@@ -17,6 +17,18 @@
 #include "eigenpy/eigenpy.hpp"
 #include <iostream>
 
+Eigen::VectorXd emptyVector()
+{
+  Eigen::VectorXd vec;
+  vec.resize(0);
+  return vec;
+}
+
+Eigen::MatrixXd emptyMatrix()
+{
+  return Eigen::MatrixXd(0,0);
+}
+
 Eigen::MatrixXd naturals(int R,int C,bool verbose)
 {
   Eigen::MatrixXd mat(R,C);
@@ -77,4 +89,7 @@ BOOST_PYTHON_MODULE(matrix)
   bp::def("reflexV", reflex<Eigen::VectorXd>);
   bp::def("reflex33", reflex<Eigen::Matrix3d>);
   bp::def("reflex3", reflex<Eigen::Vector3d>);
+
+  bp::def("emptyVector", emptyVector);
+  bp::def("emptyMatrix", emptyMatrix);
 }
diff --git a/unittest/python/test_matrix.py b/unittest/python/test_matrix.py
index ad5490c0..847bb94a 100644
--- a/unittest/python/test_matrix.py
+++ b/unittest/python/test_matrix.py
@@ -5,6 +5,14 @@ import matrix as eigenpy
 
 verbose = True
 
+if verbose: print("===> From empty MatrixXd to Py")
+M = eigenpy.emptyMatrix()
+assert M.shape == (0,0)
+
+if verbose: print("===> From empty VectorXd to Py")
+v = eigenpy.emptyVector()
+assert v.shape == (0,1)
+
 if verbose: print("===> From MatrixXd to Py")
 M = eigenpy.naturals(3,3,verbose)
 Mcheck = np.reshape(np.matrix(range(9),np.double),[3,3])
-- 
GitLab