Skip to content

Commit 201f6b4

Browse files
committed
[C++][Cmake] Add option to make eigen and numpy objects aligned
1 parent a1ea4c2 commit 201f6b4

File tree

4 files changed

+47
-14
lines changed

4 files changed

+47
-14
lines changed

CMakeLists.txt

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,15 @@ IF(APPLE)
2323
SET(CMAKE_MACOSX_RPATH TRUE)
2424
ENDIF(APPLE)
2525

26+
# ----------------------------------------------------
27+
# --- OPTIONS ---------------------------------------
28+
# ----------------------------------------------------
29+
OPTION (EIGEN_NUMPY_ALIGNED "Directly aligned data between Numpy and Eigen" OFF)
30+
31+
IF(EIGEN_NUMPY_ALIGNED)
32+
ADD_DEFINITIONS(-DEIGENPY_ALIGNED)
33+
ENDIF(EIGEN_NUMPY_ALIGNED)
34+
2635
# ----------------------------------------------------
2736
# --- DEPENDANCIES -----------------------------------
2837
# ----------------------------------------------------
@@ -102,6 +111,9 @@ ADD_LIBRARY(geometry SHARED unittest/geometry.cpp)
102111
TARGET_LINK_LIBRARIES(geometry ${Boost_LIBRARIES} ${PROJECT_NAME})
103112
SET_TARGET_PROPERTIES(geometry PROPERTIES PREFIX "")
104113

114+
IF(EIGEN_NUMPY_ALIGNED)
115+
PKG_CONFIG_APPEND_CFLAGS("-DEIGENPY_ALIGNED")
116+
ENDIF(EIGEN_NUMPY_ALIGNED)
105117
PKG_CONFIG_APPEND_CFLAGS("-I${PYTHON_INCLUDE_DIRS}")
106118
PKG_CONFIG_APPEND_CFLAGS("-I${NUMPY_INCLUDE_DIRS}")
107119
PKG_CONFIG_APPEND_LIBS("boost_python")

src/details.hpp

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -190,20 +190,26 @@ namespace eigenpy
190190
void enableEigenPySpecific()
191191
{
192192
import_array();
193-
194-
#ifdef EIGEN_DONT_VECTORIZE
193+
194+
#ifdef EIGEN_DONT_VECTORIZE
195+
195196
boost::python::to_python_converter<MatType,
196-
eigenpy::EigenToPy<MatType,MatType> >();
197+
eigenpy::EigenToPy<MatType,MatType> >();
197198
eigenpy::EigenFromPy<MatType,MatType>();
198-
#else
199-
typedef typename eigenpy::UnalignedEquivalent<MatType>::type MatTypeDontAlign;
199+
#else
200200

201201
boost::python::to_python_converter<MatType,
202202
eigenpy::EigenToPy<MatType,MatType> >();
203+
eigenpy::EigenFromPy<MatType,MatType>();
204+
205+
typedef typename eigenpy::UnalignedEquivalent<MatType>::type MatTypeDontAlign;
206+
#ifndef EIGENPY_ALIGNED
203207
boost::python::to_python_converter<MatTypeDontAlign,
204208
eigenpy::EigenToPy<MatTypeDontAlign,MatTypeDontAlign> >();
205209
eigenpy::EigenFromPy<MatTypeDontAlign,MatTypeDontAlign>();
206210
#endif
211+
#endif
212+
207213

208214
}
209215

src/fwd.hpp

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -27,11 +27,15 @@ namespace eigenpy
2727
{
2828
typedef Eigen::MatrixBase<D> MatType;
2929
typedef Eigen::Matrix<typename D::Scalar,
30-
D::RowsAtCompileTime,
31-
D::ColsAtCompileTime,
32-
D::Options | Eigen::DontAlign,
33-
D::MaxRowsAtCompileTime,
34-
D::MaxColsAtCompileTime> type;
30+
D::RowsAtCompileTime,
31+
D::ColsAtCompileTime,
32+
#ifndef EIGENPY_ALIGNED
33+
D::Options | Eigen::DontAlign,
34+
#else
35+
D::Options,
36+
#endif
37+
D::MaxRowsAtCompileTime,
38+
D::MaxColsAtCompileTime> type;
3539
};
3640

3741
} // namespace eigenpy

src/quaternion.hpp

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,11 @@ namespace eigenpy
3939
template<>
4040
struct UnalignedEquivalent<Eigen::Quaterniond>
4141
{
42-
typedef Eigen::Quaternion<double,Eigen::DontAlign> type;
42+
#ifndef EIGENPY_ALIGNED
43+
typedef Eigen::Quaternion<Eigen::Quaterniond::Scalar,Eigen::DontAlign> type;
44+
#else
45+
typedef Eigen::Quaterniond type;
46+
#endif
4347
};
4448

4549
namespace bp = boost::python;
@@ -51,10 +55,15 @@ namespace eigenpy
5155
typedef Eigen::QuaternionBase<Quaternion> QuaternionBase;
5256
typedef typename eigenpy::UnalignedEquivalent<Quaternion>::type QuaternionUnaligned;
5357

54-
typedef typename QuaternionUnaligned::Scalar Scalar;
55-
typedef Eigen::Matrix<Scalar,3,1,Eigen::DontAlign> Vector3;
58+
typedef typename QuaternionBase::Scalar Scalar;
5659
typedef typename QuaternionUnaligned::Coefficients Coefficients;
60+
#ifndef EIGENPY_ALIGNED
61+
typedef Eigen::Matrix<Scalar,3,1,Eigen::DontAlign> Vector3;
5762
typedef Eigen::Matrix<Scalar,3,3,Eigen::DontAlign> Matrix3;
63+
#else
64+
typedef Eigen::Matrix<Scalar,3,1,0> Vector3;
65+
typedef Eigen::Matrix<Scalar,3,3,0> Matrix3;
66+
#endif
5867

5968
typedef Eigen::AngleAxis<Scalar> AngleAxisUnaligned;
6069

@@ -169,8 +178,10 @@ namespace eigenpy
169178
bp::init<>())
170179
.def(QuaternionVisitor<Quaternion>())
171180
;
172-
181+
182+
#ifndef EIGENPY_ALIGNED
173183
bp::to_python_converter< Quaternion,QuaternionVisitor<Quaternion> >();
184+
#endif
174185
}
175186

176187
};

0 commit comments

Comments
 (0)