Developer Documentation
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;
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, class Vector>
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 
77 template<class Scalar, class Vector>
78 void defInitMod(class_< OpenMesh::VectorT<Scalar, 3> > &classVector) {
79  Vector (Vector::*cross)(const Vector&) const = &Vector::operator%;
80  classVector
81  .def("__init__", make_constructor(&Factory<Scalar>::vec3_default))
82  .def("__init__", make_constructor(&Factory<Scalar>::vec3_user_defined))
83  .def("__mod__", cross)
84  ;
85  def("cross", cross);
86 }
87 
88 template<class Scalar, class Vector>
89 void defInitMod(class_< OpenMesh::VectorT<Scalar, 4> > &classVector) {
90  classVector
91  .def("__init__", make_constructor(&Factory<Scalar>::vec4_default))
92  .def("__init__", make_constructor(&Factory<Scalar>::vec4_user_defined))
93  ;
94 }
95 
109 template<class Scalar, int N>
110 void expose_vec(const char *_name) {
112 
113  Scalar (Vector::*min_void)() const = &Vector::min;
114  Scalar (Vector::*max_void)() const = &Vector::max;
115 
116  Vector (Vector::*max_vector)(const Vector&) const = &Vector::max;
117  Vector (Vector::*min_vector)(const Vector&) const = &Vector::min;
118 
119  Scalar (Vector::*dot )(const Vector&) const = &Vector::operator|;
120  Scalar (Vector::*norm )(void ) const = &Vector::norm;
121  Scalar (Vector::*length )(void ) const = &Vector::length;
122  Scalar (Vector::*sqrnorm )(void ) const = &Vector::sqrnorm;
123  Vector& (Vector::*normalize )(void ) = &Vector::normalize;
124  Vector& (Vector::*normalize_cond)(void ) = &Vector::normalize_cond;
125 
126 #if (_MSC_VER >= 1900 || __cplusplus > 199711L || defined(__GXX_EXPERIMENTAL_CXX0X__)) && !defined(OPENMESH_VECTOR_LEGACY)
127  Vector (Vector::*normalized)() const = &Vector::normalized;
128 #else
129  const Vector (Vector::*normalized)() const = &Vector::normalized;
130 #endif
131 
132  class_<Vector> classVector = class_<Vector>(_name);
133 
134  classVector
135  .def("__setitem__", &set_item<Vector, Scalar>)
136  .def("__getitem__", &get_item<Vector, Scalar>)
137  .def(self == self)
138  .def(self != self)
139  .def(self *= Scalar())
140  .def(self /= Scalar())
141  .def(self * Scalar())
142  .def(Scalar() * self)
143  .def(self / Scalar())
144  .def(self *= self)
145  .def(self /= self)
146  .def(self -= self)
147  .def(self += self)
148  .def(self * self)
149  .def(self / self)
150  .def(self + self)
151  .def(self - self)
152  .def(-self)
153  .def(self | self)
154  .def("vectorize", &Vector::vectorize, return_internal_reference<>())
155  .def(self < self)
156 
157  .def("dot", dot)
158  .def("norm", norm)
159  .def("length", length)
160  .def("sqrnorm", sqrnorm)
161  .def("normalized", normalized)
162  .def("normalize", normalize, return_internal_reference<>())
163  .def("normalize_cond", normalize_cond, return_internal_reference<>())
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, Vector>(classVector);
188 }
189 
190 } // namespace OpenMesh
191 } // namespace Python
192 
193 #endif
auto length() const -> decltype(std::declval< VectorT< S, DIM >>().norm())
compute squared euclidean norm
Definition: Vector11T.hh:417
static constexpr size_t size()
returns dimension of the vector
Definition: Vector11T.hh:107
ACG::Vec4d Vector4
Standard Type for 4d Vector used for scripting.
Definition: DataTypes.hh:190
Scalar l8_norm() const
compute l8_norm
Definition: Vector11T.hh:480
Scalar min() const
return the minimal component
Definition: Vector11T.hh:506
auto normalized() const -> decltype(*this/std::declval< VectorT< S, DIM >>().norm())
Definition: Vector11T.hh:439
void expose_vec(const char *_name)
Definition: Vector.hh:110
Scalar max_abs() const
return the maximal absolute component
Definition: Vector11T.hh:497
vector_type & maximize(const vector_type &_rhs)
maximize values: same as *this = max(*this, _rhs), but faster
Definition: Vector11T.hh:562
bool maximized(const vector_type &_rhs)
maximize values and signalize coordinate maximization
Definition: Vector11T.hh:573
bool minimized(const vector_type &_rhs)
minimize values and signalize coordinate minimization
Definition: Vector11T.hh:545
Scalar min_abs() const
return the minimal absolute component
Definition: Vector11T.hh:511
Scalar mean() const
return arithmetic mean
Definition: Vector11T.hh:520
ACG::Vec3d Vector
Standard Type for 3d Vector used for scripting.
Definition: DataTypes.hh:187
Scalar mean_abs() const
return absolute arithmetic mean
Definition: Vector11T.hh:525
auto normalize() -> decltype(*this/=std::declval< VectorT< S, DIM >>().norm())
Definition: Vector11T.hh:428
static vector_type vectorized(const Scalar &_s)
store the same value in each component
Definition: Vector11T.hh:619
auto norm() const -> decltype(std::sqrt(std::declval< VectorT< S, DIM >>().sqrnorm()))
compute euclidean norm
Definition: Vector11T.hh:408
decltype(std::declval< S >()*std::declval< S >()) sqrnorm() const
compute squared euclidean norm
Definition: Vector11T.hh:396
osg::Vec3f::ValueType dot(const osg::Vec3f &_v1, const osg::Vec3f &_v2)
Adapter for osg vector member computing a scalar product.
osg::Vec3f cross(const osg::Vec3f &_v1, const osg::Vec3f &_v2)
Adapter for osg vector member computing a scalar product.
Scalar max() const
return the maximal component
Definition: Vector11T.hh:492
vector_type & vectorize(const Scalar &_s)
store the same value in each component (e.g. to clear all entries)
Definition: Vector11T.hh:613
vector_type &::type normalize_cond()
compute squared euclidean norm
Definition: Vector11T.hh:455
vector_type & minimize(const vector_type &_rhs)
minimize values: same as *this = min(*this, _rhs), but faster
Definition: Vector11T.hh:534
Scalar l1_norm() const
compute L1 (Manhattan) norm
Definition: Vector11T.hh:474