@@ -57,16 +57,41 @@ namespace eigenpy
5757 }
5858 else
5959 {
60- return NumpyAllocator<MatType>::allocate (mat. derived () ,nd,shape);
60+ return NumpyAllocator<MatType>::allocate (mat,nd,shape);
6161 }
6262 }
6363 };
6464
6565#if EIGEN_VERSION_AT_LEAST(3,2,0)
6666
6767 template <typename MatType, int Options, typename Stride>
68- struct NumpyAllocator <Eigen::Ref<MatType,Options,Stride> > : NumpyAllocator<MatType &>
68+ struct NumpyAllocator <Eigen::Ref<MatType,Options,Stride> >
6969 {
70+ typedef Eigen::Ref<MatType,Options,Stride> RefType;
71+
72+ static PyArrayObject * allocate (RefType & mat,
73+ npy_intp nd, npy_intp * shape)
74+ {
75+ typedef typename RefType::Scalar Scalar;
76+ enum { NPY_ARRAY_MEMORY_CONTIGUOUS = RefType::IsRowMajor ? NPY_ARRAY_CARRAY : NPY_ARRAY_FARRAY };
77+
78+ if (NumpyType::sharedMemory ())
79+ {
80+ const int Scalar_type_code = Register::getTypeCode<Scalar>();
81+ PyArrayObject * pyArray = (PyArrayObject*) call_PyArray_New (getPyArrayType (),
82+ static_cast <int >(nd),
83+ shape,
84+ Scalar_type_code,
85+ mat.data (),
86+ NPY_ARRAY_MEMORY_CONTIGUOUS | NPY_ARRAY_ALIGNED);
87+
88+ return pyArray;
89+ }
90+ else
91+ {
92+ return NumpyAllocator<MatType>::allocate (mat,nd,shape);
93+ }
94+ }
7095 };
7196
7297#endif
@@ -88,23 +113,49 @@ namespace eigenpy
88113 static_cast <int >(nd),
89114 shape,
90115 Scalar_type_code,
91- const_cast <SimilarMatrixType & >(mat.derived ()). data (),
116+ const_cast <Scalar * >(mat.data () ),
92117 NPY_ARRAY_MEMORY_CONTIGUOUS_RO | NPY_ARRAY_ALIGNED);
93118
94119 return pyArray;
95120 }
96121 else
97122 {
98- return NumpyAllocator<MatType>::allocate (mat. derived () ,nd,shape);
123+ return NumpyAllocator<MatType>::allocate (mat,nd,shape);
99124 }
100125 }
101126 };
102127
103128#if EIGEN_VERSION_AT_LEAST(3,2,0)
104129
105130 template <typename MatType, int Options, typename Stride>
106- struct NumpyAllocator <const Eigen::Ref<const MatType,Options,Stride> > : NumpyAllocator< const MatType &>
131+ struct NumpyAllocator <const Eigen::Ref<const MatType,Options,Stride> >
107132 {
133+ typedef const Eigen::Ref<const MatType,Options,Stride> RefType;
134+
135+ template <typename SimilarMatrixType>
136+ static PyArrayObject * allocate (RefType & mat,
137+ npy_intp nd, npy_intp * shape)
138+ {
139+ typedef typename SimilarMatrixType::Scalar Scalar;
140+ enum { NPY_ARRAY_MEMORY_CONTIGUOUS_RO = SimilarMatrixType::IsRowMajor ? NPY_ARRAY_CARRAY_RO : NPY_ARRAY_FARRAY_RO };
141+
142+ if (NumpyType::sharedMemory ())
143+ {
144+ const int Scalar_type_code = Register::getTypeCode<Scalar>();
145+ PyArrayObject * pyArray = (PyArrayObject*) call_PyArray_New (getPyArrayType (),
146+ static_cast <int >(nd),
147+ shape,
148+ Scalar_type_code,
149+ const_cast <Scalar *>(mat.data ()),
150+ NPY_ARRAY_MEMORY_CONTIGUOUS_RO | NPY_ARRAY_ALIGNED);
151+
152+ return pyArray;
153+ }
154+ else
155+ {
156+ return NumpyAllocator<MatType>::allocate (mat,nd,shape);
157+ }
158+ }
108159 };
109160
110161#endif
0 commit comments