OpenMesh
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
Vector.hh
1 #ifndef OPENMESH_PYTHON_VECTOR_HH
2 #define OPENMESH_PYTHON_VECTOR_HH
3 
4 #include "Python/Bindings.hh"
5 
6 namespace OpenMesh {
7 namespace Python {
8 
9 template <class Vector, class Scalar>
10 void set_item(Vector& _vec, int _index, Scalar _value) {
11  if (_index < 0) {
12  _index += _vec.size();
13  }
14 
15  if ((size_t)_index < _vec.size()) {
16  _vec[_index] = _value;
17  }
18  else {
19  PyErr_SetString(PyExc_IndexError, "Index out of range.");
20  throw_error_already_set();
21  }
22 }
23 
24 template <class Vector, class Scalar>
25 Scalar get_item(Vector& _vec, int _index) {
26  if (_index < 0) {
27  _index += _vec.size();
28  }
29 
30  if ((size_t)_index < _vec.size()) {
31  return _vec[_index];
32  }
33  else {
34  PyErr_SetString(PyExc_IndexError, "Index out of range.");
35  throw_error_already_set();
36  }
37 
38  return 0.0;
39 }
40 
41 namespace {
42 template<class Scalar>
43 struct Factory {
44  typedef OpenMesh::VectorT<Scalar, 2> Vector2;
45  typedef OpenMesh::VectorT<Scalar, 3> Vector3;
46  typedef OpenMesh::VectorT<Scalar, 4> Vector4;
47 
48  static Vector2 *vec2_default() {
49  return new Vector2(Scalar(), Scalar());
50  }
51  static Vector2 *vec2_user_defined(const Scalar& _v0, const Scalar& _v1) {
52  return new Vector2(_v0, _v1);
53  }
54  static Vector3 *vec3_default() {
55  return new Vector3(Scalar(), Scalar(), Scalar());
56  }
57  static Vector3 *vec3_user_defined(const Scalar& _v0, const Scalar& _v1, const Scalar& _v2) {
58  return new Vector3(_v0, _v1, _v2);
59  }
60  static Vector4 *vec4_default() {
61  return new Vector4(Scalar(), Scalar(), Scalar(), Scalar());
62  }
63  static Vector4 *vec4_user_defined(const Scalar& _v0, const Scalar& _v1, const Scalar& _v2, const Scalar& _v3) {
64  return new Vector4(_v0, _v1, _v2, _v3);
65  }
66 };
67 }
68 
69 template<class Scalar>
70 void defInitMod(class_< OpenMesh::VectorT<Scalar, 2> > &classVector) {
71  classVector
72  .def("__init__", make_constructor(&Factory<Scalar>::vec2_default))
73  .def("__init__", make_constructor(&Factory<Scalar>::vec2_user_defined))
74  ;
75 }
76 template<class Scalar>
77 void defInitMod(class_< OpenMesh::VectorT<Scalar, 3> > &classVector) {
78  classVector
79  .def("__init__", make_constructor(&Factory<Scalar>::vec3_default))
80  .def("__init__", make_constructor(&Factory<Scalar>::vec3_user_defined))
81 #if (_MSC_VER >= 1900 || __cplusplus > 199711L || defined(__GXX_EXPERIMENTAL_CXX0X__)) && !defined(OPENMESH_VECTOR_LEGACY)
82  .def("__mod__", &Factory<Scalar>::Vector3::template operator%<Scalar>)
83  ;
84  def("cross", &Factory<Scalar>::Vector3::template operator%<Scalar>);
85 #else
86  .def("__mod__", &Factory<Scalar>::Vector3::operator%)
87  ;
88  def("cross", &Factory<Scalar>::Vector3::operator%);
89 #endif
90 }
91 template<class Scalar>
92 void defInitMod(class_< OpenMesh::VectorT<Scalar, 4> > &classVector) {
93  classVector
94  .def("__init__", make_constructor(&Factory<Scalar>::vec4_default))
95  .def("__init__", make_constructor(&Factory<Scalar>::vec4_user_defined))
96  ;
97 }
98 
112 template<class Scalar, int N>
113 void expose_vec(const char *_name) {
114  typedef OpenMesh::VectorT<Scalar, N> Vector;
115 
116  Scalar (Vector::*min_void)() const = &Vector::min;
117  Scalar (Vector::*max_void)() const = &Vector::max;
118 
119  Vector (Vector::*max_vector)(const Vector&) const = &Vector::max;
120  Vector (Vector::*min_vector)(const Vector&) const = &Vector::min;
121 
122  class_<Vector> classVector = class_<Vector>(_name);
123 
124  classVector
125  .def("__setitem__", &set_item<Vector, Scalar>)
126  .def("__getitem__", &get_item<Vector, Scalar>)
127  .def(self == self)
128  .def(self != self)
129  .def(self *= Scalar())
130  .def(self /= Scalar())
131  .def(self * Scalar())
132  .def(Scalar() * self)
133  .def(self / Scalar())
134  .def(self *= self)
135  .def(self /= self)
136  .def(self -= self)
137  .def(self += self)
138  .def(self * self)
139  .def(self / self)
140  .def(self + self)
141  .def(self - self)
142  .def(-self)
143  .def(self | self)
144  .def("vectorize", &Vector::vectorize, return_internal_reference<>())
145  .def(self < self)
146 
147 #if (_MSC_VER >= 1900 || __cplusplus > 199711L || defined(__GXX_EXPERIMENTAL_CXX0X__)) && !defined(OPENMESH_VECTOR_LEGACY)
148  .def("dot", &Vector::template operator|<Scalar>)
149  .def("norm", &Vector::template norm<Scalar>)
150  .def("length", &Vector::template length<Scalar>)
151  .def("sqrnorm", &Vector::template sqrnorm<Scalar>)
152  .def("normalize", &Vector::template normalize<Scalar>, return_internal_reference<>())
153  .def("normalized", &Vector::template normalized<Scalar>)
154  .def("normalize_cond", &Vector::template normalize_cond<Scalar>, return_internal_reference<>())
155 #else
156  .def("dot", &Vector::operator|)
157  .def("norm", &Vector::norm)
158  .def("length", &Vector::length)
159  .def("sqrnorm", &Vector::sqrnorm)
160  .def("normalize", &Vector::normalize, return_internal_reference<>())
161  .def("normalized", &Vector::normalized)
162  .def("normalize_cond", &Vector::normalize_cond, return_internal_reference<>())
163 #endif
164 
165  .def("l1_norm", &Vector::l1_norm)
166  .def("l8_norm", &Vector::l8_norm)
167 
168  .def("max", max_void)
169  .def("max_abs", &Vector::max_abs)
170  .def("min", min_void)
171  .def("min_abs", &Vector::min_abs)
172  .def("mean", &Vector::mean)
173  .def("mean_abs", &Vector::mean_abs)
174  .def("minimize", &Vector::minimize, return_internal_reference<>())
175  .def("minimized", &Vector::minimized)
176  .def("maximize", &Vector::maximize, return_internal_reference<>())
177  .def("maximized", &Vector::maximized)
178  .def("min", min_vector)
179  .def("max", max_vector)
180 
181  .def("size", &Vector::size)
182  .staticmethod("size")
183  .def("vectorized", &Vector::vectorized)
184  .staticmethod("vectorized")
185  ;
186 
187  defInitMod<Scalar>(classVector);
188 }
189 
190 } // namespace OpenMesh
191 } // namespace Python
192 
193 #endif
void expose_vec(const char *_name)
Expose a vector type to Python.
Definition: Vector.hh:113
Definition: Vector11T.hh:83
Contains all the mesh ingredients like the polygonal mesh, the triangle mesh, different mesh kernels ...
Definition: MeshItems.hh:64

Project OpenMesh, ©  Computer Graphics Group, RWTH Aachen. Documentation generated using doxygen .