user_type.cpp
Go to the documentation of this file.
1 /*
2  * Copyright 2020 INRIA
3  */
4 
5 #include "eigenpy/eigenpy.hpp"
6 #include "eigenpy/user-type.hpp"
7 #include "eigenpy/ufunc.hpp"
8 
9 #include <iostream>
10 #include <sstream>
11 
12 template<typename Scalar>
13 struct CustomType
14 {
16 
17  explicit CustomType(const Scalar & value)
18  : m_value(value)
19  {}
20 
21  CustomType operator*(const CustomType & other) const { return CustomType(m_value * other.m_value); }
22  CustomType operator+(const CustomType & other) const { return CustomType(m_value + other.m_value); }
23  CustomType operator-(const CustomType & other) const { return CustomType(m_value - other.m_value); }
24  CustomType operator/(const CustomType & other) const { return CustomType(m_value / other.m_value); }
25 
26  void operator+=(const CustomType & other) { m_value += other.m_value; }
27  void operator-=(const CustomType & other) { m_value -= other.m_value; }
28  void operator*=(const CustomType & other) { m_value *= other.m_value; }
29  void operator/=(const CustomType & other) { m_value /= other.m_value; }
30 
31  void operator=(const Scalar & value) { m_value = value; }
32 
33  bool operator==(const CustomType & other) const { return m_value == other.m_value; }
34  bool operator!=(const CustomType & other) const { return m_value != other.m_value; }
35 
36  bool operator<=(const CustomType & other) const { return m_value <= other.m_value; }
37  bool operator<(const CustomType & other) const { return m_value < other.m_value; }
38  bool operator>=(const CustomType & other) const { return m_value >= other.m_value; }
39  bool operator>(const CustomType & other) const { return m_value > other.m_value; }
40 
41  CustomType operator-() const { return CustomType(-m_value); }
42 
43  std::string print() const
44  {
45  std::stringstream ss;
46  ss << "value: " << m_value << std::endl;
47  return ss.str();
48  }
49 
50 protected:
51 
52  Scalar m_value;
53 };
54 
55 template<typename Scalar>
56 Eigen::Matrix<CustomType<Scalar>,Eigen::Dynamic,Eigen::Dynamic> create(int rows, int cols)
57 {
58  typedef Eigen::Matrix<CustomType<Scalar>,Eigen::Dynamic,Eigen::Dynamic> Matrix;
59  return Matrix(rows,cols);
60 }
61 
62 template<typename Scalar>
63 Eigen::Matrix<Scalar,Eigen::Dynamic,Eigen::Dynamic> build_matrix(int rows, int cols)
64 {
65  typedef Eigen::Matrix<Scalar,Eigen::Dynamic,Eigen::Dynamic> Matrix;
66  return Matrix(rows,cols);
67 }
68 
69 template<typename Scalar>
70 void expose_custom_type(const std::string & name)
71 {
72  using namespace Eigen;
73  namespace bp = boost::python;
74 
75  typedef CustomType<Scalar> Type;
76 
77  bp::class_<Type>(name.c_str(),bp::init<Scalar>(bp::arg("value")))
78 
79  .def(bp::self + bp::self)
80  .def(bp::self - bp::self)
81  .def(bp::self * bp::self)
82  .def(bp::self / bp::self)
83 
84  .def(bp::self += bp::self)
85  .def(bp::self -= bp::self)
86  .def(bp::self *= bp::self)
87  .def(bp::self /= bp::self)
88 
89  .def("__repr__",&Type::print)
90  ;
91 
92  int code = eigenpy::registerNewType<Type>();
93  std::cout << "code: " << code << std::endl;
94  eigenpy::registerCommonUfunc<Type>();
95 }
96 
98 {
99  using namespace Eigen;
100  namespace bp = boost::python;
102 
103  expose_custom_type<double>("CustomDouble");
104  typedef CustomType<double> DoubleType;
105  typedef Eigen::Matrix<DoubleType,Eigen::Dynamic,Eigen::Dynamic> DoubleMatrix;
107  bp::def("create_double",create<double>);
108 
109  expose_custom_type<float>("CustomFloat");
110  typedef CustomType<float> FloatType;
111  typedef Eigen::Matrix<FloatType,Eigen::Dynamic,Eigen::Dynamic> FloatMatrix;
113  bp::def("create_float",create<float>);
114 
115  bp::def("build_matrix",build_matrix<double>);
116 }
void operator/=(const CustomType &other)
Definition: user_type.cpp:29
void operator-=(const CustomType &other)
Definition: user_type.cpp:27
void operator*=(const CustomType &other)
Definition: user_type.cpp:28
Eigen::Matrix< Scalar, Eigen::Dynamic, Eigen::Dynamic > build_matrix(int rows, int cols)
Definition: user_type.cpp:63
Definition: complex.cpp:7
void operator=(const Scalar &value)
Definition: user_type.cpp:31
BOOST_PYTHON_MODULE(user_type)
Definition: user_type.cpp:97
void EIGENPY_DLLAPI enableEigenPy()
Definition: eigenpy.cpp:29
bool operator==(const CustomType &other) const
Definition: user_type.cpp:33
CustomType operator/(const CustomType &other) const
Definition: user_type.cpp:24
bool operator>(const CustomType &other) const
Definition: user_type.cpp:39
std::string print() const
Definition: user_type.cpp:43
CustomType operator-() const
Definition: user_type.cpp:41
bool operator>=(const CustomType &other) const
Definition: user_type.cpp:38
Scalar m_value
Definition: user_type.cpp:52
bool operator!=(const CustomType &other) const
Definition: user_type.cpp:34
Eigen::Matrix< CustomType< Scalar >, Eigen::Dynamic, Eigen::Dynamic > create(int rows, int cols)
Definition: user_type.cpp:56
bool operator<(const CustomType &other) const
Definition: user_type.cpp:37
CustomType(const Scalar &value)
Definition: user_type.cpp:17
CustomType operator-(const CustomType &other) const
Definition: user_type.cpp:23
CustomType operator*(const CustomType &other) const
Definition: user_type.cpp:21
void expose_custom_type(const std::string &name)
Definition: user_type.cpp:70
bool operator<=(const CustomType &other) const
Definition: user_type.cpp:36
CustomType operator+(const CustomType &other) const
Definition: user_type.cpp:22
void operator+=(const CustomType &other)
Definition: user_type.cpp:26


eigenpy
Author(s): Justin Carpentier, Nicolas Mansard
autogenerated on Sat Apr 17 2021 02:37:59