From 201f6b4bd9ca3f82f6412fde94de95eb5dd9202e Mon Sep 17 00:00:00 2001
From: jcarpent <jcarpent@laas.fr>
Date: Sun, 27 Dec 2015 12:02:59 +0100
Subject: [PATCH] [C++][Cmake] Add option to make eigen and numpy objects
 aligned

---
 CMakeLists.txt     | 12 ++++++++++++
 src/details.hpp    | 16 +++++++++++-----
 src/fwd.hpp        | 14 +++++++++-----
 src/quaternion.hpp | 19 +++++++++++++++----
 4 files changed, 47 insertions(+), 14 deletions(-)

diff --git a/CMakeLists.txt b/CMakeLists.txt
index fbfbe15..2b9d0b4 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -23,6 +23,15 @@ IF(APPLE)
   SET(CMAKE_MACOSX_RPATH TRUE)
 ENDIF(APPLE)
 
+# ----------------------------------------------------
+# --- OPTIONS  ---------------------------------------
+# ----------------------------------------------------
+OPTION (EIGEN_NUMPY_ALIGNED "Directly aligned data between Numpy and Eigen" OFF)
+
+IF(EIGEN_NUMPY_ALIGNED)
+  ADD_DEFINITIONS(-DEIGENPY_ALIGNED)
+ENDIF(EIGEN_NUMPY_ALIGNED)
+
 # ----------------------------------------------------
 # --- DEPENDANCIES -----------------------------------
 # ----------------------------------------------------
@@ -102,6 +111,9 @@ ADD_LIBRARY(geometry SHARED unittest/geometry.cpp)
 TARGET_LINK_LIBRARIES(geometry ${Boost_LIBRARIES} ${PROJECT_NAME})
 SET_TARGET_PROPERTIES(geometry PROPERTIES PREFIX "")
 
+IF(EIGEN_NUMPY_ALIGNED)
+  PKG_CONFIG_APPEND_CFLAGS("-DEIGENPY_ALIGNED")
+ENDIF(EIGEN_NUMPY_ALIGNED)
 PKG_CONFIG_APPEND_CFLAGS("-I${PYTHON_INCLUDE_DIRS}")
 PKG_CONFIG_APPEND_CFLAGS("-I${NUMPY_INCLUDE_DIRS}")
 PKG_CONFIG_APPEND_LIBS("boost_python")
diff --git a/src/details.hpp b/src/details.hpp
index 0cef1a3..8989f30 100644
--- a/src/details.hpp
+++ b/src/details.hpp
@@ -190,20 +190,26 @@ namespace eigenpy
   void enableEigenPySpecific()
   {
     import_array();
-
- #ifdef EIGEN_DONT_VECTORIZE
+    
+#ifdef EIGEN_DONT_VECTORIZE
+    
     boost::python::to_python_converter<MatType,
-				       eigenpy::EigenToPy<MatType,MatType> >();
+                                      eigenpy::EigenToPy<MatType,MatType> >();
     eigenpy::EigenFromPy<MatType,MatType>();
- #else 
-    typedef typename eigenpy::UnalignedEquivalent<MatType>::type MatTypeDontAlign;
+#else
     
     boost::python::to_python_converter<MatType,
 				       eigenpy::EigenToPy<MatType,MatType> >();
+    eigenpy::EigenFromPy<MatType,MatType>();
+    
+    typedef typename eigenpy::UnalignedEquivalent<MatType>::type MatTypeDontAlign;
+#ifndef EIGENPY_ALIGNED
     boost::python::to_python_converter<MatTypeDontAlign,
 				       eigenpy::EigenToPy<MatTypeDontAlign,MatTypeDontAlign> >();
     eigenpy::EigenFromPy<MatTypeDontAlign,MatTypeDontAlign>();
 #endif
+#endif
+
 
   }
 
diff --git a/src/fwd.hpp b/src/fwd.hpp
index b137f1d..37aa6a2 100644
--- a/src/fwd.hpp
+++ b/src/fwd.hpp
@@ -27,11 +27,15 @@ namespace eigenpy
   {
     typedef Eigen::MatrixBase<D> MatType;
     typedef Eigen::Matrix<typename D::Scalar,
-			  D::RowsAtCompileTime,
-			  D::ColsAtCompileTime,
-			  D::Options | Eigen::DontAlign,
-			  D::MaxRowsAtCompileTime,
-			  D::MaxColsAtCompileTime>      type;
+             D::RowsAtCompileTime,
+             D::ColsAtCompileTime,
+#ifndef EIGENPY_ALIGNED
+             D::Options | Eigen::DontAlign,
+#else
+             D::Options,
+#endif
+             D::MaxRowsAtCompileTime,
+             D::MaxColsAtCompileTime> type;
   };
 
 } // namespace eigenpy
diff --git a/src/quaternion.hpp b/src/quaternion.hpp
index 279bdea..f93c30e 100644
--- a/src/quaternion.hpp
+++ b/src/quaternion.hpp
@@ -39,7 +39,11 @@ namespace eigenpy
   template<>
   struct UnalignedEquivalent<Eigen::Quaterniond>
   {
-    typedef Eigen::Quaternion<double,Eigen::DontAlign> type;
+#ifndef EIGENPY_ALIGNED
+    typedef Eigen::Quaternion<Eigen::Quaterniond::Scalar,Eigen::DontAlign> type;
+#else
+    typedef Eigen::Quaterniond type;
+#endif
   };
 
   namespace bp = boost::python;
@@ -51,10 +55,15 @@ namespace eigenpy
     typedef Eigen::QuaternionBase<Quaternion> QuaternionBase;
     typedef typename eigenpy::UnalignedEquivalent<Quaternion>::type QuaternionUnaligned;
 
-    typedef typename QuaternionUnaligned::Scalar Scalar;
-    typedef Eigen::Matrix<Scalar,3,1,Eigen::DontAlign> Vector3;
+    typedef typename QuaternionBase::Scalar Scalar;
     typedef typename QuaternionUnaligned::Coefficients Coefficients;
+#ifndef EIGENPY_ALIGNED
+    typedef Eigen::Matrix<Scalar,3,1,Eigen::DontAlign> Vector3;
     typedef Eigen::Matrix<Scalar,3,3,Eigen::DontAlign> Matrix3;
+#else
+    typedef Eigen::Matrix<Scalar,3,1,0> Vector3;
+    typedef Eigen::Matrix<Scalar,3,3,0> Matrix3;
+#endif
 
     typedef Eigen::AngleAxis<Scalar> AngleAxisUnaligned;
 
@@ -169,8 +178,10 @@ namespace eigenpy
 				      bp::init<>())
 	.def(QuaternionVisitor<Quaternion>())
 	;
-    
+   
+#ifndef EIGENPY_ALIGNED
       bp::to_python_converter< Quaternion,QuaternionVisitor<Quaternion> >();
+#endif
     }
 
   };
-- 
GitLab