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, int N>
70 void defInitMod(class_< OpenMesh::VectorT<Scalar, N> > &classVector);
71 
72 template<class Scalar>
73 void defInitMod(class_< OpenMesh::VectorT<Scalar, 2> > &classVector) {
74  classVector
75  .def("__init__", make_constructor(&Factory<Scalar>::vec2_default))
76  .def("__init__", make_constructor(&Factory<Scalar>::vec2_user_defined))
77  ;
78 
79  typedef OpenMesh::VectorT<Scalar, 2> Vector;
80  def("dot", &Vector::operator|);
81 }
82 template<class Scalar>
83 void defInitMod(class_< OpenMesh::VectorT<Scalar, 3> > &classVector) {
84  classVector
85  .def("__init__", make_constructor(&Factory<Scalar>::vec3_default))
86  .def("__init__", make_constructor(&Factory<Scalar>::vec3_user_defined))
87  .def("__mod__", &Factory<Scalar>::Vector3::operator%)
88  ;
89 
90  def("cross", &Factory<Scalar>::Vector3::operator%);
91 
92  typedef OpenMesh::VectorT<Scalar, 3> Vector;
93  def("dot", &Vector::operator|);
94 }
95 template<class Scalar>
96 void defInitMod(class_< OpenMesh::VectorT<Scalar, 4> > &classVector) {
97  classVector
98  .def("__init__", make_constructor(&Factory<Scalar>::vec4_default))
99  .def("__init__", make_constructor(&Factory<Scalar>::vec4_user_defined))
100  ;
101 
102  typedef OpenMesh::VectorT<Scalar, 4> Vector;
103  def("dot", &Vector::operator|);
104 }
105 
119 template<class Scalar, int N>
120 void expose_vec(const char *_name) {
121  typedef OpenMesh::VectorT<Scalar, N> Vector;
122 
123  Scalar (Vector::*min_void)() const = &Vector::min;
124  Scalar (Vector::*max_void)() const = &Vector::max;
125 
126  Vector (Vector::*max_vector)(const Vector&) const = &Vector::max;
127  Vector (Vector::*min_vector)(const Vector&) const = &Vector::min;
128 
129  class_<Vector> classVector = class_<Vector>(_name);
130 
131  classVector
132  .def("__setitem__", &set_item<Vector, Scalar>)
133  .def("__getitem__", &get_item<Vector, Scalar>)
134  .def(self == self)
135  .def(self != self)
136  .def(self *= Scalar())
137  .def(self /= Scalar())
138  .def(self * Scalar())
139  .def(Scalar() * self)
140  .def(self / Scalar())
141  .def(self *= self)
142  .def(self /= self)
143  .def(self -= self)
144  .def(self += self)
145  .def(self * self)
146  .def(self / self)
147  .def(self + self)
148  .def(self - self)
149  .def(-self)
150  .def(self | self)
151  .def("vectorize", &Vector::vectorize, return_internal_reference<>())
152  .def(self < self)
153 
154  .def("norm", &Vector::norm)
155  .def("length", &Vector::length)
156  .def("sqrnorm", &Vector::sqrnorm)
157  .def("normalize", &Vector::normalize, return_internal_reference<>())
158  .def("normalized", &Vector::normalized)
159  .def("normalize_cond", &Vector::normalize_cond, return_internal_reference<>())
160 
161  .def("l1_norm", &Vector::l1_norm)
162  .def("l8_norm", &Vector::l8_norm)
163 
164  .def("max", max_void)
165  .def("max_abs", &Vector::max_abs)
166  .def("min", min_void)
167  .def("min_abs", &Vector::min_abs)
168  .def("mean", &Vector::mean)
169  .def("mean_abs", &Vector::mean_abs)
170  .def("minimize", &Vector::minimize, return_internal_reference<>())
171  .def("minimized", &Vector::minimized)
172  .def("maximize", &Vector::maximize, return_internal_reference<>())
173  .def("maximized", &Vector::maximized)
174  .def("min", min_vector)
175  .def("max", max_vector)
176 
177  .def("size", &Vector::size)
178  .staticmethod("size")
179  .def("vectorized", &Vector::vectorized)
180  .staticmethod("vectorized")
181  ;
182 
183  typedef OpenMesh::VectorT<Scalar, 2> Vector2;
184  typedef OpenMesh::VectorT<Scalar, 3> Vector3;
185  typedef OpenMesh::VectorT<Scalar, 4> Vector4;
186 
187  struct Factory {
188  static Vector2 *vec2_default() {
189  return new Vector2(Scalar(), Scalar());
190  }
191  static Vector2 *vec2_user_defined(const Scalar& _v0, const Scalar& _v1) {
192  return new Vector2(_v0, _v1);
193  }
194  static Vector3 *vec3_default() {
195  return new Vector3(Scalar(), Scalar(), Scalar());
196  }
197  static Vector3 *vec3_user_defined(const Scalar& _v0, const Scalar& _v1, const Scalar& _v2) {
198  return new Vector3(_v0, _v1, _v2);
199  }
200  static Vector4 *vec4_default() {
201  return new Vector4(Scalar(), Scalar(), Scalar(), Scalar());
202  }
203  static Vector4 *vec4_user_defined(const Scalar& _v0, const Scalar& _v1, const Scalar& _v2, const Scalar& _v3) {
204  return new Vector4(_v0, _v1, _v2, _v3);
205  }
206  };
207 
208  if (N == 2) {
209  classVector
210  .def("__init__", make_constructor(&Factory::vec2_default))
211  .def("__init__", make_constructor(&Factory::vec2_user_defined))
212  ;
213  }
214 
215  if (N == 3) {
216  classVector
217  .def("__init__", make_constructor(&Factory::vec3_default))
218  .def("__init__", make_constructor(&Factory::vec3_user_defined))
219  .def("__mod__", &Vector3::operator%)
220  ;
221 
222  def("cross", &Vector3::operator%);
223  }
224 
225  if (N == 4) {
226  classVector
227  .def("__init__", make_constructor(&Factory::vec4_default))
228  .def("__init__", make_constructor(&Factory::vec4_user_defined))
229  ;
230  }
231 
232  def("dot", &Vector::operator|);
233 }
234 
235 } // namespace OpenMesh
236 } // namespace Python
237 
238 #endif
void expose_vec(const char *_name)
Expose a vector type to Python.
Definition: Vector.hh:120
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 .