| OpenMesh

- Main Page
- Related Pages
- Modules
- Namespaces
- Classes
- Files

- File List
- File Members

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
54 template<class Scalar, int N>
55 void expose_vec(const char *_name) {
56 typedef OpenMesh::VectorT<Scalar, N> Vector;
57
58 Scalar (Vector::*min_void)() const = &Vector::min;
59 Scalar (Vector::*max_void)() const = &Vector::max;
60
61 Vector (Vector::*max_vector)(const Vector&) const = &Vector::max;
62 Vector (Vector::*min_vector)(const Vector&) const = &Vector::min;
63
64 class_<Vector> classVector = class_<Vector>(_name);
65
66 classVector
67 .def("__setitem__", &set_item<Vector, Scalar>)
68 .def("__getitem__", &get_item<Vector, Scalar>)
69 .def(self == self)
70 .def(self != self)
71 .def(self *= Scalar())
72 .def(self /= Scalar())
73 .def(self * Scalar())
74 .def(Scalar() * self)
75 .def(self / Scalar())
76 .def(self *= self)
77 .def(self /= self)
78 .def(self -= self)
79 .def(self += self)
80 .def(self * self)
81 .def(self / self)
82 .def(self + self)
83 .def(self - self)
84 .def(-self)
85 .def(self | self)
86 .def("vectorize", &Vector::vectorize, return_internal_reference<>())
87 .def(self < self)
88
89 .def("norm", &Vector::norm)
90 .def("length", &Vector::length)
91 .def("sqrnorm", &Vector::sqrnorm)
92 .def("normalize", &Vector::normalize, return_internal_reference<>())
93 .def("normalized", &Vector::normalized)
94 .def("normalize_cond", &Vector::normalize_cond, return_internal_reference<>())
95
96 .def("l1_norm", &Vector::l1_norm)
97 .def("l8_norm", &Vector::l8_norm)
98
99 .def("max", max_void)
100 .def("max_abs", &Vector::max_abs)
101 .def("min", min_void)
102 .def("min_abs", &Vector::min_abs)
103 .def("mean", &Vector::mean)
104 .def("mean_abs", &Vector::mean_abs)
105 .def("minimize", &Vector::minimize, return_internal_reference<>())
106 .def("minimized", &Vector::minimized)
107 .def("maximize", &Vector::maximize, return_internal_reference<>())
108 .def("maximized", &Vector::maximized)
109 .def("min", min_vector)
110 .def("max", max_vector)
111
112 .def("size", &Vector::size)
113 .staticmethod("size")
114 .def("vectorized", &Vector::vectorized)
115 .staticmethod("vectorized")
116 ;
117
118 typedef OpenMesh::VectorT<Scalar, 2> Vector2;
119 typedef OpenMesh::VectorT<Scalar, 3> Vector3;
120 typedef OpenMesh::VectorT<Scalar, 4> Vector4;
121
122 struct Factory {
123 static Vector2 *vec2_default() {
124 return new Vector2(Scalar(), Scalar());
125 }
126 static Vector2 *vec2_user_defined(const Scalar& _v0, const Scalar& _v1) {
127 return new Vector2(_v0, _v1);
128 }
129 static Vector3 *vec3_default() {
130 return new Vector3(Scalar(), Scalar(), Scalar());
131 }
132 static Vector3 *vec3_user_defined(const Scalar& _v0, const Scalar& _v1, const Scalar& _v2) {
133 return new Vector3(_v0, _v1, _v2);
134 }
135 static Vector4 *vec4_default() {
136 return new Vector4(Scalar(), Scalar(), Scalar(), Scalar());
137 }
138 static Vector4 *vec4_user_defined(const Scalar& _v0, const Scalar& _v1, const Scalar& _v2, const Scalar& _v3) {
139 return new Vector4(_v0, _v1, _v2, _v3);
140 }
141 };
142
143 if (N == 2) {
144 classVector
145 .def("__init__", make_constructor(&Factory::vec2_default))
146 .def("__init__", make_constructor(&Factory::vec2_user_defined))
147 ;
148 }
149
150 if (N == 3) {
151 classVector
152 .def("__init__", make_constructor(&Factory::vec3_default))
153 .def("__init__", make_constructor(&Factory::vec3_user_defined))
154 .def("__mod__", &Vector3::operator%)
155 ;
156
157 def("cross", &Vector3::operator%);
158 }
159
160 if (N == 4) {
161 classVector
162 .def("__init__", make_constructor(&Factory::vec4_default))
163 .def("__init__", make_constructor(&Factory::vec4_user_defined))
164 ;
165 }
166
167 def("dot", &Vector::operator|);
168 }
169
170 } // namespace OpenMesh
171 } // namespace Python
172
173 #endif
OpenMesh::Python::expose_vec
void expose_vec(const char *_name)
Expose a vector type to Python.
Definition: Vector.hh:55
OpenMesh
Contains all the mesh ingredients like the polygonal mesh, the triangle mesh, different mesh kernels ...
Definition: MeshItems.hh:64
Bindings.hh