Mercurial > pygame
annotate src/math.c @ 1849:301bc61643a9 math_module
merged changes from trunk
| author | lorenz |
|---|---|
| date | Mon, 16 Nov 2009 18:31:40 +0000 |
| parents | bbc21cd6faeb |
| children |
| rev | line source |
|---|---|
| 1834 | 1 /* |
| 2 pygame - Python Game Library | |
| 3 | |
| 4 This library is free software; you can redistribute it and/or | |
| 5 modify it under the terms of the GNU Library General Public | |
| 6 License as published by the Free Software Foundation; either | |
| 7 version 2 of the License, or (at your option) any later version. | |
| 8 | |
| 9 This library is distributed in the hope that it will be useful, | |
| 10 but WITHOUT ANY WARRANTY; without even the implied warranty of | |
| 11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
| 12 Library General Public License for more details. | |
| 13 | |
| 14 You should have received a copy of the GNU Library General Public | |
| 15 License along with this library; if not, write to the Free | |
| 16 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA | |
| 17 | |
| 18 */ | |
| 19 | |
| 20 #define PYGAMEAPI_MATH_INTERNAL | |
| 21 #include "pygame.h" | |
| 22 #include "pygamedocs.h" | |
| 23 #include "structmember.h" | |
| 24 #include "pgcompat.h" | |
| 25 #include <float.h> | |
| 26 #include <math.h> | |
| 27 | |
|
1842
5418dd349248
refactored the vector_elementwiseproxy functions and fixed some bugs along the way
lorenz
parents:
1841
diff
changeset
|
28 #define VECTOR_MAX_SIZE (4) |
| 1836 | 29 #define STRING_BUF_SIZE (100) |
| 1840 | 30 #define SWIZZLE_ERR_NO_ERR 0 |
| 31 #define SWIZZLE_ERR_DOUBLE_IDX 1 | |
| 32 #define SWIZZLE_ERR_EXTRACTION_ERR 2 | |
|
1845
7d2a16e6a1d3
rearanged math module. added forward declarations at the beginning.
lorenz
parents:
1844
diff
changeset
|
33 #define RETURN_ERROR 0 |
|
7d2a16e6a1d3
rearanged math module. added forward declarations at the beginning.
lorenz
parents:
1844
diff
changeset
|
34 #define RETURN_NO_ERROR 1 |
| 1834 | 35 |
| 1848 | 36 #define OP_ADD 1 |
| 37 #define OP_IADD 2 | |
| 38 #define OP_SUB 3 | |
| 39 #define OP_ISUB 4 | |
| 40 #define OP_MUL 5 | |
| 41 #define OP_IMUL 6 | |
| 42 #define OP_DIV 7 | |
| 43 #define OP_IDIV 8 | |
| 44 #define OP_FLOOR_DIV 9 | |
| 45 #define OP_IFLOOR_DIV 10 | |
| 46 #define OP_MOD 11 | |
| 47 #define OP_ARG_REVERSE 32 | |
| 48 #define OP_ARG_UNKNOWN 64 | |
| 49 #define OP_ARG_VECTOR 128 | |
| 50 #define OP_ARG_NUMBER 256 | |
| 51 | |
| 1834 | 52 static PyTypeObject PyVector2_Type; |
|
1844
a15f6323c39d
* implemented Vector3. there are no tests yet, so there are bound to be bugs
lorenz
parents:
1842
diff
changeset
|
53 static PyTypeObject PyVector3_Type; |
|
1841
a7dc31e50c46
* added the vetor_elementwiseproxy which lets you apply calculations on
lorenz
parents:
1840
diff
changeset
|
54 static PyTypeObject PyVectorElementwiseProxy_Type; |
|
1844
a15f6323c39d
* implemented Vector3. there are no tests yet, so there are bound to be bugs
lorenz
parents:
1842
diff
changeset
|
55 static PyTypeObject PyVectorIter_Type; |
| 1846 | 56 static PyTypeObject PyVector_SlerpIter_Type; |
|
1844
a15f6323c39d
* implemented Vector3. there are no tests yet, so there are bound to be bugs
lorenz
parents:
1842
diff
changeset
|
57 |
| 1834 | 58 #define PyVector2_Check(x) ((x)->ob_type == &PyVector2_Type) |
|
1844
a15f6323c39d
* implemented Vector3. there are no tests yet, so there are bound to be bugs
lorenz
parents:
1842
diff
changeset
|
59 #define PyVector3_Check(x) ((x)->ob_type == &PyVector3_Type) |
|
a15f6323c39d
* implemented Vector3. there are no tests yet, so there are bound to be bugs
lorenz
parents:
1842
diff
changeset
|
60 #define PyVector_Check(x) (PyVector2_Check(x) || PyVector3_Check(x)) |
|
1841
a7dc31e50c46
* added the vetor_elementwiseproxy which lets you apply calculations on
lorenz
parents:
1840
diff
changeset
|
61 #define vector_elementwiseproxy_Check(x) \ |
|
a7dc31e50c46
* added the vetor_elementwiseproxy which lets you apply calculations on
lorenz
parents:
1840
diff
changeset
|
62 ((x)->ob_type == &PyVectorElementwiseProxy_Type) |
| 1834 | 63 |
| 1836 | 64 #define DEG2RAD(angle) ((angle) * M_PI / 180.) |
| 65 #define RAD2DEG(angle) ((angle) * 180. / M_PI) | |
| 66 | |
|
1847
ed12994f377c
added "lerp()" to Vector2 and Vector3 to provide a linear interpolation.
lorenz
parents:
1846
diff
changeset
|
67 typedef struct |
|
ed12994f377c
added "lerp()" to Vector2 and Vector3 to provide a linear interpolation.
lorenz
parents:
1846
diff
changeset
|
68 { |
|
ed12994f377c
added "lerp()" to Vector2 and Vector3 to provide a linear interpolation.
lorenz
parents:
1846
diff
changeset
|
69 PyObject_HEAD |
|
ed12994f377c
added "lerp()" to Vector2 and Vector3 to provide a linear interpolation.
lorenz
parents:
1846
diff
changeset
|
70 double *coords; /* Coordinates */ |
|
ed12994f377c
added "lerp()" to Vector2 and Vector3 to provide a linear interpolation.
lorenz
parents:
1846
diff
changeset
|
71 unsigned int dim; /* Dimension of the vector */ |
|
ed12994f377c
added "lerp()" to Vector2 and Vector3 to provide a linear interpolation.
lorenz
parents:
1846
diff
changeset
|
72 double epsilon; /* Small value for comparisons */ |
|
ed12994f377c
added "lerp()" to Vector2 and Vector3 to provide a linear interpolation.
lorenz
parents:
1846
diff
changeset
|
73 } PyVector; |
|
ed12994f377c
added "lerp()" to Vector2 and Vector3 to provide a linear interpolation.
lorenz
parents:
1846
diff
changeset
|
74 |
|
1845
7d2a16e6a1d3
rearanged math module. added forward declarations at the beginning.
lorenz
parents:
1844
diff
changeset
|
75 typedef struct { |
|
7d2a16e6a1d3
rearanged math module. added forward declarations at the beginning.
lorenz
parents:
1844
diff
changeset
|
76 PyObject_HEAD |
|
7d2a16e6a1d3
rearanged math module. added forward declarations at the beginning.
lorenz
parents:
1844
diff
changeset
|
77 long it_index; |
|
7d2a16e6a1d3
rearanged math module. added forward declarations at the beginning.
lorenz
parents:
1844
diff
changeset
|
78 PyVector *vec; |
|
7d2a16e6a1d3
rearanged math module. added forward declarations at the beginning.
lorenz
parents:
1844
diff
changeset
|
79 } vectoriter; |
|
7d2a16e6a1d3
rearanged math module. added forward declarations at the beginning.
lorenz
parents:
1844
diff
changeset
|
80 |
|
7d2a16e6a1d3
rearanged math module. added forward declarations at the beginning.
lorenz
parents:
1844
diff
changeset
|
81 typedef struct { |
|
7d2a16e6a1d3
rearanged math module. added forward declarations at the beginning.
lorenz
parents:
1844
diff
changeset
|
82 PyObject_HEAD |
| 1846 | 83 long it_index; |
| 84 long steps; | |
| 85 long dim; | |
| 86 double coords[VECTOR_MAX_SIZE]; | |
| 87 double matrix[VECTOR_MAX_SIZE][VECTOR_MAX_SIZE]; | |
| 88 double radial_factor; | |
| 89 } vector_slerpiter; | |
| 90 | |
| 91 typedef struct { | |
| 92 PyObject_HEAD | |
|
1847
ed12994f377c
added "lerp()" to Vector2 and Vector3 to provide a linear interpolation.
lorenz
parents:
1846
diff
changeset
|
93 long it_index; |
|
ed12994f377c
added "lerp()" to Vector2 and Vector3 to provide a linear interpolation.
lorenz
parents:
1846
diff
changeset
|
94 long steps; |
|
ed12994f377c
added "lerp()" to Vector2 and Vector3 to provide a linear interpolation.
lorenz
parents:
1846
diff
changeset
|
95 long dim; |
|
ed12994f377c
added "lerp()" to Vector2 and Vector3 to provide a linear interpolation.
lorenz
parents:
1846
diff
changeset
|
96 double coords[VECTOR_MAX_SIZE]; |
|
ed12994f377c
added "lerp()" to Vector2 and Vector3 to provide a linear interpolation.
lorenz
parents:
1846
diff
changeset
|
97 double step_vec[VECTOR_MAX_SIZE]; |
|
ed12994f377c
added "lerp()" to Vector2 and Vector3 to provide a linear interpolation.
lorenz
parents:
1846
diff
changeset
|
98 } vector_lerpiter; |
|
ed12994f377c
added "lerp()" to Vector2 and Vector3 to provide a linear interpolation.
lorenz
parents:
1846
diff
changeset
|
99 |
|
ed12994f377c
added "lerp()" to Vector2 and Vector3 to provide a linear interpolation.
lorenz
parents:
1846
diff
changeset
|
100 typedef struct { |
|
ed12994f377c
added "lerp()" to Vector2 and Vector3 to provide a linear interpolation.
lorenz
parents:
1846
diff
changeset
|
101 PyObject_HEAD |
|
1845
7d2a16e6a1d3
rearanged math module. added forward declarations at the beginning.
lorenz
parents:
1844
diff
changeset
|
102 PyVector *vec; |
|
7d2a16e6a1d3
rearanged math module. added forward declarations at the beginning.
lorenz
parents:
1844
diff
changeset
|
103 } vector_elementwiseproxy; |
|
7d2a16e6a1d3
rearanged math module. added forward declarations at the beginning.
lorenz
parents:
1844
diff
changeset
|
104 |
|
7d2a16e6a1d3
rearanged math module. added forward declarations at the beginning.
lorenz
parents:
1844
diff
changeset
|
105 |
|
7d2a16e6a1d3
rearanged math module. added forward declarations at the beginning.
lorenz
parents:
1844
diff
changeset
|
106 /* further forward declerations */ |
|
7d2a16e6a1d3
rearanged math module. added forward declarations at the beginning.
lorenz
parents:
1844
diff
changeset
|
107 /* generic helper functions */ |
|
7d2a16e6a1d3
rearanged math module. added forward declarations at the beginning.
lorenz
parents:
1844
diff
changeset
|
108 static int RealNumber_Check(PyObject *obj); |
|
7d2a16e6a1d3
rearanged math module. added forward declarations at the beginning.
lorenz
parents:
1844
diff
changeset
|
109 static double PySequence_GetItem_AsDouble(PyObject *seq, Py_ssize_t index); |
|
7d2a16e6a1d3
rearanged math module. added forward declarations at the beginning.
lorenz
parents:
1844
diff
changeset
|
110 static int PySequence_AsVectorCoords(PyObject *seq, double *coords, const size_t size); |
|
7d2a16e6a1d3
rearanged math module. added forward declarations at the beginning.
lorenz
parents:
1844
diff
changeset
|
111 static int PyVectorCompatible_Check(PyObject *obj, int dim); |
|
7d2a16e6a1d3
rearanged math module. added forward declarations at the beginning.
lorenz
parents:
1844
diff
changeset
|
112 static double _scalar_product(const double *coords1, const double *coords2, int size); |
| 1846 | 113 static void _make_vector2_slerp_matrix(vector_slerpiter *it, |
| 114 const double *vec1_coords, | |
| 115 const double *vec2_coords, | |
| 116 double angle); | |
| 117 static void _make_vector3_slerp_matrix(vector_slerpiter *it, | |
| 118 const double *vec1_coords, | |
| 119 const double *vec2_coords, | |
| 120 double angle); | |
|
1845
7d2a16e6a1d3
rearanged math module. added forward declarations at the beginning.
lorenz
parents:
1844
diff
changeset
|
121 |
|
7d2a16e6a1d3
rearanged math module. added forward declarations at the beginning.
lorenz
parents:
1844
diff
changeset
|
122 /* generic vector functions */ |
|
7d2a16e6a1d3
rearanged math module. added forward declarations at the beginning.
lorenz
parents:
1844
diff
changeset
|
123 static PyObject *PyVector_NEW(int dim); |
|
7d2a16e6a1d3
rearanged math module. added forward declarations at the beginning.
lorenz
parents:
1844
diff
changeset
|
124 static void vector_dealloc(PyVector* self); |
| 1848 | 125 static PyObject *vector_generic_math(PyObject *o1, PyObject *o2, int op); |
|
1845
7d2a16e6a1d3
rearanged math module. added forward declarations at the beginning.
lorenz
parents:
1844
diff
changeset
|
126 static PyObject *vector_add(PyObject *o1, PyObject *o2); |
|
7d2a16e6a1d3
rearanged math module. added forward declarations at the beginning.
lorenz
parents:
1844
diff
changeset
|
127 static PyObject *vector_sub(PyObject *o1, PyObject *o2); |
|
7d2a16e6a1d3
rearanged math module. added forward declarations at the beginning.
lorenz
parents:
1844
diff
changeset
|
128 static PyObject *vector_mul(PyObject *o1, PyObject *o2); |
|
7d2a16e6a1d3
rearanged math module. added forward declarations at the beginning.
lorenz
parents:
1844
diff
changeset
|
129 static PyObject *vector_div(PyVector *self, PyObject *other); |
|
7d2a16e6a1d3
rearanged math module. added forward declarations at the beginning.
lorenz
parents:
1844
diff
changeset
|
130 static PyObject *vector_floor_div(PyVector *self, PyObject *other); |
|
7d2a16e6a1d3
rearanged math module. added forward declarations at the beginning.
lorenz
parents:
1844
diff
changeset
|
131 static PyObject *vector_inplace_add(PyVector *self, PyObject *other); |
|
7d2a16e6a1d3
rearanged math module. added forward declarations at the beginning.
lorenz
parents:
1844
diff
changeset
|
132 static PyObject *vector_inplace_sub(PyVector *self, PyObject *other); |
|
7d2a16e6a1d3
rearanged math module. added forward declarations at the beginning.
lorenz
parents:
1844
diff
changeset
|
133 static PyObject *vector_inplace_mul(PyVector *self, PyObject *other); |
|
7d2a16e6a1d3
rearanged math module. added forward declarations at the beginning.
lorenz
parents:
1844
diff
changeset
|
134 static PyObject *vector_inplace_div(PyVector *self, PyObject *other); |
|
7d2a16e6a1d3
rearanged math module. added forward declarations at the beginning.
lorenz
parents:
1844
diff
changeset
|
135 static PyObject *vector_inplace_floor_div(PyVector *self, PyObject *other); |
|
7d2a16e6a1d3
rearanged math module. added forward declarations at the beginning.
lorenz
parents:
1844
diff
changeset
|
136 static PyObject *vector_neg(PyVector *self); |
|
7d2a16e6a1d3
rearanged math module. added forward declarations at the beginning.
lorenz
parents:
1844
diff
changeset
|
137 static PyObject *vector_pos(PyVector *self); |
|
7d2a16e6a1d3
rearanged math module. added forward declarations at the beginning.
lorenz
parents:
1844
diff
changeset
|
138 static int vector_nonzero(PyVector *self); |
|
7d2a16e6a1d3
rearanged math module. added forward declarations at the beginning.
lorenz
parents:
1844
diff
changeset
|
139 static Py_ssize_t vector_len(PyVector *self); |
|
7d2a16e6a1d3
rearanged math module. added forward declarations at the beginning.
lorenz
parents:
1844
diff
changeset
|
140 static PyObject *vector_GetItem(PyVector *self, Py_ssize_t index); |
|
7d2a16e6a1d3
rearanged math module. added forward declarations at the beginning.
lorenz
parents:
1844
diff
changeset
|
141 static int vector_SetItem(PyVector *self, Py_ssize_t index, PyObject *value); |
|
7d2a16e6a1d3
rearanged math module. added forward declarations at the beginning.
lorenz
parents:
1844
diff
changeset
|
142 static PyObject *vector_GetSlice(PyVector *self, Py_ssize_t ilow, Py_ssize_t ihigh); |
|
7d2a16e6a1d3
rearanged math module. added forward declarations at the beginning.
lorenz
parents:
1844
diff
changeset
|
143 static int vector_SetSlice(PyVector *self, Py_ssize_t ilow, Py_ssize_t ihigh, PyObject *v); |
|
7d2a16e6a1d3
rearanged math module. added forward declarations at the beginning.
lorenz
parents:
1844
diff
changeset
|
144 static PyObject *vector_getx (PyVector *self, void *closure); |
|
7d2a16e6a1d3
rearanged math module. added forward declarations at the beginning.
lorenz
parents:
1844
diff
changeset
|
145 static PyObject *vector_gety (PyVector *self, void *closure); |
|
7d2a16e6a1d3
rearanged math module. added forward declarations at the beginning.
lorenz
parents:
1844
diff
changeset
|
146 static PyObject *vector_getz (PyVector *self, void *closure); |
|
7d2a16e6a1d3
rearanged math module. added forward declarations at the beginning.
lorenz
parents:
1844
diff
changeset
|
147 static PyObject *vector_getw (PyVector *self, void *closure); |
|
7d2a16e6a1d3
rearanged math module. added forward declarations at the beginning.
lorenz
parents:
1844
diff
changeset
|
148 static int vector_setx (PyVector *self, PyObject *value, void *closure); |
|
7d2a16e6a1d3
rearanged math module. added forward declarations at the beginning.
lorenz
parents:
1844
diff
changeset
|
149 static int vector_sety (PyVector *self, PyObject *value, void *closure); |
|
7d2a16e6a1d3
rearanged math module. added forward declarations at the beginning.
lorenz
parents:
1844
diff
changeset
|
150 static int vector_setz (PyVector *self, PyObject *value, void *closure); |
|
7d2a16e6a1d3
rearanged math module. added forward declarations at the beginning.
lorenz
parents:
1844
diff
changeset
|
151 static int vector_setw (PyVector *self, PyObject *value, void *closure); |
|
7d2a16e6a1d3
rearanged math module. added forward declarations at the beginning.
lorenz
parents:
1844
diff
changeset
|
152 static PyObject *vector_richcompare(PyVector *self, PyObject *other, int op); |
|
7d2a16e6a1d3
rearanged math module. added forward declarations at the beginning.
lorenz
parents:
1844
diff
changeset
|
153 static PyObject *vector_length(PyVector *self); |
|
7d2a16e6a1d3
rearanged math module. added forward declarations at the beginning.
lorenz
parents:
1844
diff
changeset
|
154 static PyObject *vector_length_squared(PyVector *self); |
|
7d2a16e6a1d3
rearanged math module. added forward declarations at the beginning.
lorenz
parents:
1844
diff
changeset
|
155 static PyObject *vector_normalize(PyVector *self); |
|
7d2a16e6a1d3
rearanged math module. added forward declarations at the beginning.
lorenz
parents:
1844
diff
changeset
|
156 static PyObject *vector_normalize_ip(PyVector *self); |
|
7d2a16e6a1d3
rearanged math module. added forward declarations at the beginning.
lorenz
parents:
1844
diff
changeset
|
157 static PyObject *vector_dot(PyVector *self, PyObject *other); |
|
7d2a16e6a1d3
rearanged math module. added forward declarations at the beginning.
lorenz
parents:
1844
diff
changeset
|
158 static PyObject *vector_scale_to_length(PyVector *self, PyObject *length); |
|
7d2a16e6a1d3
rearanged math module. added forward declarations at the beginning.
lorenz
parents:
1844
diff
changeset
|
159 static int _vector_reflect_helper(double *dst_coords, const double *src_coords, |
|
7d2a16e6a1d3
rearanged math module. added forward declarations at the beginning.
lorenz
parents:
1844
diff
changeset
|
160 PyObject *normal, int dim, double epsilon); |
|
7d2a16e6a1d3
rearanged math module. added forward declarations at the beginning.
lorenz
parents:
1844
diff
changeset
|
161 static PyObject *vector_reflect(PyVector *self, PyObject *normal); |
|
7d2a16e6a1d3
rearanged math module. added forward declarations at the beginning.
lorenz
parents:
1844
diff
changeset
|
162 static PyObject *vector_reflect_ip(PyVector *self, PyObject *normal); |
|
7d2a16e6a1d3
rearanged math module. added forward declarations at the beginning.
lorenz
parents:
1844
diff
changeset
|
163 static PyObject *vector_distance_to(PyVector *self, PyObject *other); |
|
7d2a16e6a1d3
rearanged math module. added forward declarations at the beginning.
lorenz
parents:
1844
diff
changeset
|
164 static PyObject *vector_distance_squared_to(PyVector *self, PyObject *other); |
|
7d2a16e6a1d3
rearanged math module. added forward declarations at the beginning.
lorenz
parents:
1844
diff
changeset
|
165 static PyObject *vector_getAttr_swizzle(PyVector *self, PyObject *attr_name); |
|
7d2a16e6a1d3
rearanged math module. added forward declarations at the beginning.
lorenz
parents:
1844
diff
changeset
|
166 static int vector_setAttr_swizzle(PyVector *self, PyObject *attr_name, PyObject *val); |
|
7d2a16e6a1d3
rearanged math module. added forward declarations at the beginning.
lorenz
parents:
1844
diff
changeset
|
167 static PyObject *vector_elementwise(PyVector *self); |
| 1846 | 168 static PyObject *vector_slerp(PyVector *self, PyObject *args); |
|
1847
ed12994f377c
added "lerp()" to Vector2 and Vector3 to provide a linear interpolation.
lorenz
parents:
1846
diff
changeset
|
169 static PyObject *vector_lerp(PyVector *self, PyObject *args); |
|
1845
7d2a16e6a1d3
rearanged math module. added forward declarations at the beginning.
lorenz
parents:
1844
diff
changeset
|
170 static PyObject *vector_repr(PyVector *self); |
|
7d2a16e6a1d3
rearanged math module. added forward declarations at the beginning.
lorenz
parents:
1844
diff
changeset
|
171 static PyObject *vector_str(PyVector *self); |
| 1846 | 172 /* |
| 173 static Py_ssize_t vector_readbuffer(PyVector *self, Py_ssize_t segment, void **ptrptr); | |
| 174 static Py_ssize_t vector_writebuffer(PyVector *self, Py_ssize_t segment, void **ptrptr); | |
| 175 static Py_ssize_t vector_segcount(PyVector *self, Py_ssize_t *lenp); | |
| 176 */ | |
|
1845
7d2a16e6a1d3
rearanged math module. added forward declarations at the beginning.
lorenz
parents:
1844
diff
changeset
|
177 |
|
7d2a16e6a1d3
rearanged math module. added forward declarations at the beginning.
lorenz
parents:
1844
diff
changeset
|
178 /* vector2 specific functions */ |
|
7d2a16e6a1d3
rearanged math module. added forward declarations at the beginning.
lorenz
parents:
1844
diff
changeset
|
179 static PyObject *vector2_new(PyTypeObject *type, PyObject *args, PyObject *kwds); |
|
7d2a16e6a1d3
rearanged math module. added forward declarations at the beginning.
lorenz
parents:
1844
diff
changeset
|
180 static int vector2_init(PyVector *self, PyObject *args, PyObject *kwds); |
|
7d2a16e6a1d3
rearanged math module. added forward declarations at the beginning.
lorenz
parents:
1844
diff
changeset
|
181 static void _vector2_do_rotate(double *dst_coords, const double *src_coords, |
|
7d2a16e6a1d3
rearanged math module. added forward declarations at the beginning.
lorenz
parents:
1844
diff
changeset
|
182 double angle, double epsilon); |
|
7d2a16e6a1d3
rearanged math module. added forward declarations at the beginning.
lorenz
parents:
1844
diff
changeset
|
183 static PyObject *vector2_rotate(PyVector *self, PyObject *args); |
|
7d2a16e6a1d3
rearanged math module. added forward declarations at the beginning.
lorenz
parents:
1844
diff
changeset
|
184 static PyObject *vector2_rotate_ip(PyVector *self, PyObject *args); |
|
7d2a16e6a1d3
rearanged math module. added forward declarations at the beginning.
lorenz
parents:
1844
diff
changeset
|
185 static PyObject *vector2_cross(PyVector *self, PyObject *other); |
|
7d2a16e6a1d3
rearanged math module. added forward declarations at the beginning.
lorenz
parents:
1844
diff
changeset
|
186 static PyObject *vector2_angle_to(PyVector *self, PyObject *other); |
| 1846 | 187 static PyObject *vector2_as_polar(PyVector *self); |
| 188 static PyObject *vector2_from_polar(PyVector *self, PyObject *args); | |
|
1845
7d2a16e6a1d3
rearanged math module. added forward declarations at the beginning.
lorenz
parents:
1844
diff
changeset
|
189 |
|
7d2a16e6a1d3
rearanged math module. added forward declarations at the beginning.
lorenz
parents:
1844
diff
changeset
|
190 /* vector3 specific functions */ |
|
7d2a16e6a1d3
rearanged math module. added forward declarations at the beginning.
lorenz
parents:
1844
diff
changeset
|
191 static PyObject *vector3_new(PyTypeObject *type, PyObject *args, PyObject *kwds); |
|
7d2a16e6a1d3
rearanged math module. added forward declarations at the beginning.
lorenz
parents:
1844
diff
changeset
|
192 static int vector3_init(PyVector *self, PyObject *args, PyObject *kwds); |
|
7d2a16e6a1d3
rearanged math module. added forward declarations at the beginning.
lorenz
parents:
1844
diff
changeset
|
193 static void _vector3_do_rotate(double *dst_coords, const double *src_coords, |
|
7d2a16e6a1d3
rearanged math module. added forward declarations at the beginning.
lorenz
parents:
1844
diff
changeset
|
194 const double *axis_coords, |
|
7d2a16e6a1d3
rearanged math module. added forward declarations at the beginning.
lorenz
parents:
1844
diff
changeset
|
195 double angle, double epsilon); |
|
7d2a16e6a1d3
rearanged math module. added forward declarations at the beginning.
lorenz
parents:
1844
diff
changeset
|
196 static PyObject *vector3_rotate(PyVector *self, PyObject *args); |
|
7d2a16e6a1d3
rearanged math module. added forward declarations at the beginning.
lorenz
parents:
1844
diff
changeset
|
197 static PyObject *vector3_rotate_ip(PyVector *self, PyObject *args); |
|
7d2a16e6a1d3
rearanged math module. added forward declarations at the beginning.
lorenz
parents:
1844
diff
changeset
|
198 static PyObject *vector3_cross(PyVector *self, PyObject *other); |
|
7d2a16e6a1d3
rearanged math module. added forward declarations at the beginning.
lorenz
parents:
1844
diff
changeset
|
199 static PyObject *vector3_angle_to(PyVector *self, PyObject *other); |
| 1846 | 200 static PyObject *vector3_as_spherical(PyVector *self); |
| 201 static PyObject *vector3_from_spherical(PyVector *self, PyObject *args); | |
|
1845
7d2a16e6a1d3
rearanged math module. added forward declarations at the beginning.
lorenz
parents:
1844
diff
changeset
|
202 |
|
7d2a16e6a1d3
rearanged math module. added forward declarations at the beginning.
lorenz
parents:
1844
diff
changeset
|
203 /* vector iterator functions */ |
|
7d2a16e6a1d3
rearanged math module. added forward declarations at the beginning.
lorenz
parents:
1844
diff
changeset
|
204 static void vectoriter_dealloc(vectoriter *it); |
|
7d2a16e6a1d3
rearanged math module. added forward declarations at the beginning.
lorenz
parents:
1844
diff
changeset
|
205 static PyObject *vectoriter_next(vectoriter *it); |
|
7d2a16e6a1d3
rearanged math module. added forward declarations at the beginning.
lorenz
parents:
1844
diff
changeset
|
206 static PyObject *vectoriter_len(vectoriter *it); |
|
7d2a16e6a1d3
rearanged math module. added forward declarations at the beginning.
lorenz
parents:
1844
diff
changeset
|
207 static PyObject *vector_iter(PyObject *vec); |
|
7d2a16e6a1d3
rearanged math module. added forward declarations at the beginning.
lorenz
parents:
1844
diff
changeset
|
208 |
|
7d2a16e6a1d3
rearanged math module. added forward declarations at the beginning.
lorenz
parents:
1844
diff
changeset
|
209 /* elementwiseproxy */ |
|
7d2a16e6a1d3
rearanged math module. added forward declarations at the beginning.
lorenz
parents:
1844
diff
changeset
|
210 static void vector_elementwiseproxy_dealloc(vector_elementwiseproxy *it); |
|
7d2a16e6a1d3
rearanged math module. added forward declarations at the beginning.
lorenz
parents:
1844
diff
changeset
|
211 static PyObject *vector_elementwiseproxy_richcompare(PyObject *o1, PyObject *o2, int op); |
| 1848 | 212 static PyObject *vector_elementwiseproxy_generic_math(PyObject *o1, |
| 213 PyObject *o2, int op); | |
|
1845
7d2a16e6a1d3
rearanged math module. added forward declarations at the beginning.
lorenz
parents:
1844
diff
changeset
|
214 static PyObject *vector_elementwiseproxy_add(PyObject *o1, PyObject *o2); |
|
7d2a16e6a1d3
rearanged math module. added forward declarations at the beginning.
lorenz
parents:
1844
diff
changeset
|
215 static PyObject *vector_elementwiseproxy_sub(PyObject *o1, PyObject *o2); |
|
7d2a16e6a1d3
rearanged math module. added forward declarations at the beginning.
lorenz
parents:
1844
diff
changeset
|
216 static PyObject *vector_elementwiseproxy_mul(PyObject *o1, PyObject *o2); |
|
7d2a16e6a1d3
rearanged math module. added forward declarations at the beginning.
lorenz
parents:
1844
diff
changeset
|
217 static PyObject *vector_elementwiseproxy_div(PyObject *o1, PyObject *o2); |
|
7d2a16e6a1d3
rearanged math module. added forward declarations at the beginning.
lorenz
parents:
1844
diff
changeset
|
218 static PyObject *vector_elementwiseproxy_floor_div(PyObject *o1, PyObject *o2); |
|
7d2a16e6a1d3
rearanged math module. added forward declarations at the beginning.
lorenz
parents:
1844
diff
changeset
|
219 static PyObject *vector_elementwiseproxy_pow(PyObject *baseObj, PyObject *expoObj, PyObject *mod); |
|
7d2a16e6a1d3
rearanged math module. added forward declarations at the beginning.
lorenz
parents:
1844
diff
changeset
|
220 static PyObject *vector_elementwiseproxy_mod(PyObject *o1, PyObject *o2); |
|
7d2a16e6a1d3
rearanged math module. added forward declarations at the beginning.
lorenz
parents:
1844
diff
changeset
|
221 static PyObject *vector_elementwiseproxy_abs(vector_elementwiseproxy *self); |
|
7d2a16e6a1d3
rearanged math module. added forward declarations at the beginning.
lorenz
parents:
1844
diff
changeset
|
222 static PyObject *vector_elementwiseproxy_neg(vector_elementwiseproxy *self); |
|
7d2a16e6a1d3
rearanged math module. added forward declarations at the beginning.
lorenz
parents:
1844
diff
changeset
|
223 static PyObject *vector_elementwiseproxy_pos(vector_elementwiseproxy *self); |
|
7d2a16e6a1d3
rearanged math module. added forward declarations at the beginning.
lorenz
parents:
1844
diff
changeset
|
224 static int vector_elementwiseproxy_nonzero(vector_elementwiseproxy *self); |
|
7d2a16e6a1d3
rearanged math module. added forward declarations at the beginning.
lorenz
parents:
1844
diff
changeset
|
225 static PyObject *vector_elementwise(PyVector *vec); |
|
7d2a16e6a1d3
rearanged math module. added forward declarations at the beginning.
lorenz
parents:
1844
diff
changeset
|
226 |
|
7d2a16e6a1d3
rearanged math module. added forward declarations at the beginning.
lorenz
parents:
1844
diff
changeset
|
227 |
|
7d2a16e6a1d3
rearanged math module. added forward declarations at the beginning.
lorenz
parents:
1844
diff
changeset
|
228 static int swizzling_enabled = 0; |
| 1834 | 229 |
| 230 | |
| 231 /******************************** | |
| 232 * Global helper functions | |
| 233 ********************************/ | |
| 234 | |
| 235 static int | |
|
1838
e4fd799c194b
added methods distance_to and distance_squared_to to the vector class
lorenz
parents:
1837
diff
changeset
|
236 RealNumber_Check(PyObject *obj) |
| 1834 | 237 { |
|
1838
e4fd799c194b
added methods distance_to and distance_squared_to to the vector class
lorenz
parents:
1837
diff
changeset
|
238 if (PyNumber_Check(obj) && !PyComplex_Check(obj)) |
|
e4fd799c194b
added methods distance_to and distance_squared_to to the vector class
lorenz
parents:
1837
diff
changeset
|
239 return 1; |
| 1834 | 240 return 0; |
| 241 } | |
| 242 | |
| 243 static double | |
| 244 PySequence_GetItem_AsDouble(PyObject *seq, Py_ssize_t index) | |
| 245 { | |
|
1838
e4fd799c194b
added methods distance_to and distance_squared_to to the vector class
lorenz
parents:
1837
diff
changeset
|
246 PyObject *item, *fltobj; |
| 1834 | 247 double value; |
|
1838
e4fd799c194b
added methods distance_to and distance_squared_to to the vector class
lorenz
parents:
1837
diff
changeset
|
248 |
| 1834 | 249 item = PySequence_GetItem(seq, index); |
| 1840 | 250 if (item == NULL) { |
| 251 PyErr_SetString(PyExc_TypeError, "a sequence is expected"); | |
| 252 return -1; | |
| 253 } | |
|
1838
e4fd799c194b
added methods distance_to and distance_squared_to to the vector class
lorenz
parents:
1837
diff
changeset
|
254 fltobj = PyNumber_Float(item); |
| 1840 | 255 Py_DECREF(item); |
|
1838
e4fd799c194b
added methods distance_to and distance_squared_to to the vector class
lorenz
parents:
1837
diff
changeset
|
256 if (!fltobj) { |
|
e4fd799c194b
added methods distance_to and distance_squared_to to the vector class
lorenz
parents:
1837
diff
changeset
|
257 PyErr_SetString(PyExc_TypeError, "a float is required"); |
|
e4fd799c194b
added methods distance_to and distance_squared_to to the vector class
lorenz
parents:
1837
diff
changeset
|
258 return -1; |
|
e4fd799c194b
added methods distance_to and distance_squared_to to the vector class
lorenz
parents:
1837
diff
changeset
|
259 } |
|
e4fd799c194b
added methods distance_to and distance_squared_to to the vector class
lorenz
parents:
1837
diff
changeset
|
260 value = PyFloat_AsDouble(fltobj); |
|
e4fd799c194b
added methods distance_to and distance_squared_to to the vector class
lorenz
parents:
1837
diff
changeset
|
261 Py_DECREF(fltobj); |
|
e4fd799c194b
added methods distance_to and distance_squared_to to the vector class
lorenz
parents:
1837
diff
changeset
|
262 return value; |
|
e4fd799c194b
added methods distance_to and distance_squared_to to the vector class
lorenz
parents:
1837
diff
changeset
|
263 } |
|
e4fd799c194b
added methods distance_to and distance_squared_to to the vector class
lorenz
parents:
1837
diff
changeset
|
264 |
|
e4fd799c194b
added methods distance_to and distance_squared_to to the vector class
lorenz
parents:
1837
diff
changeset
|
265 static int |
|
1844
a15f6323c39d
* implemented Vector3. there are no tests yet, so there are bound to be bugs
lorenz
parents:
1842
diff
changeset
|
266 PySequence_AsVectorCoords(PyObject *seq, double *coords, const size_t size) |
|
1838
e4fd799c194b
added methods distance_to and distance_squared_to to the vector class
lorenz
parents:
1837
diff
changeset
|
267 { |
|
e4fd799c194b
added methods distance_to and distance_squared_to to the vector class
lorenz
parents:
1837
diff
changeset
|
268 int i; |
|
e4fd799c194b
added methods distance_to and distance_squared_to to the vector class
lorenz
parents:
1837
diff
changeset
|
269 |
|
e4fd799c194b
added methods distance_to and distance_squared_to to the vector class
lorenz
parents:
1837
diff
changeset
|
270 if (PyVector_Check(seq)) { |
|
e4fd799c194b
added methods distance_to and distance_squared_to to the vector class
lorenz
parents:
1837
diff
changeset
|
271 memcpy(coords, ((PyVector *)seq)->coords, sizeof(double) * size); |
|
e4fd799c194b
added methods distance_to and distance_squared_to to the vector class
lorenz
parents:
1837
diff
changeset
|
272 return 1; |
|
e4fd799c194b
added methods distance_to and distance_squared_to to the vector class
lorenz
parents:
1837
diff
changeset
|
273 } |
|
e4fd799c194b
added methods distance_to and distance_squared_to to the vector class
lorenz
parents:
1837
diff
changeset
|
274 if (!PySequence_Check(seq) || PySequence_Length(seq) != size) { |
|
e4fd799c194b
added methods distance_to and distance_squared_to to the vector class
lorenz
parents:
1837
diff
changeset
|
275 coords = NULL; |
| 1836 | 276 return 0; |
|
1838
e4fd799c194b
added methods distance_to and distance_squared_to to the vector class
lorenz
parents:
1837
diff
changeset
|
277 } |
|
e4fd799c194b
added methods distance_to and distance_squared_to to the vector class
lorenz
parents:
1837
diff
changeset
|
278 |
|
e4fd799c194b
added methods distance_to and distance_squared_to to the vector class
lorenz
parents:
1837
diff
changeset
|
279 for (i = 0; i < size; ++i) { |
|
e4fd799c194b
added methods distance_to and distance_squared_to to the vector class
lorenz
parents:
1837
diff
changeset
|
280 coords[i] = PySequence_GetItem_AsDouble(seq, i); |
|
e4fd799c194b
added methods distance_to and distance_squared_to to the vector class
lorenz
parents:
1837
diff
changeset
|
281 if (PyErr_Occurred()) { |
|
e4fd799c194b
added methods distance_to and distance_squared_to to the vector class
lorenz
parents:
1837
diff
changeset
|
282 coords = NULL; |
|
e4fd799c194b
added methods distance_to and distance_squared_to to the vector class
lorenz
parents:
1837
diff
changeset
|
283 return 0; |
|
e4fd799c194b
added methods distance_to and distance_squared_to to the vector class
lorenz
parents:
1837
diff
changeset
|
284 } |
|
e4fd799c194b
added methods distance_to and distance_squared_to to the vector class
lorenz
parents:
1837
diff
changeset
|
285 } |
|
e4fd799c194b
added methods distance_to and distance_squared_to to the vector class
lorenz
parents:
1837
diff
changeset
|
286 return 1; |
| 1834 | 287 } |
| 288 | |
| 289 static int | |
|
1845
7d2a16e6a1d3
rearanged math module. added forward declarations at the beginning.
lorenz
parents:
1844
diff
changeset
|
290 PyVectorCompatible_Check(PyObject *obj, int dim) |
| 1834 | 291 { |
| 292 int i; | |
| 293 PyObject *tmp; | |
| 294 | |
| 295 switch(dim) { | |
| 296 case 2: | |
| 297 if (PyVector2_Check(obj)) { | |
| 298 return 1; | |
| 299 } | |
| 300 break; | |
| 301 case 3: | |
|
1845
7d2a16e6a1d3
rearanged math module. added forward declarations at the beginning.
lorenz
parents:
1844
diff
changeset
|
302 if (PyVector3_Check(obj)) { |
| 1834 | 303 return 1; |
| 304 } | |
| 305 break; | |
|
1844
a15f6323c39d
* implemented Vector3. there are no tests yet, so there are bound to be bugs
lorenz
parents:
1842
diff
changeset
|
306 /* |
| 1834 | 307 case 4: |
|
1845
7d2a16e6a1d3
rearanged math module. added forward declarations at the beginning.
lorenz
parents:
1844
diff
changeset
|
308 if (PyVector4_Check(obj)) { |
| 1834 | 309 return 1; |
| 310 } | |
| 311 break; | |
| 312 */ | |
| 313 default: | |
| 314 PyErr_SetString(PyExc_SystemError, | |
|
1845
7d2a16e6a1d3
rearanged math module. added forward declarations at the beginning.
lorenz
parents:
1844
diff
changeset
|
315 "Wrong internal call to PyVectorCompatible_Check."); |
| 1834 | 316 return 0; |
| 317 } | |
| 318 | |
| 319 if (!PySequence_Check(obj) || (PySequence_Length(obj) != dim)) { | |
| 320 return 0; | |
| 321 } | |
| 322 | |
| 323 for (i = 0; i < dim; ++i) { | |
| 324 tmp = PySequence_GetItem(obj, i); | |
|
1838
e4fd799c194b
added methods distance_to and distance_squared_to to the vector class
lorenz
parents:
1837
diff
changeset
|
325 if (!RealNumber_Check(tmp)) { |
| 1834 | 326 Py_DECREF(tmp); |
| 327 return 0; | |
| 328 } | |
| 329 Py_DECREF(tmp); | |
| 330 } | |
| 331 return 1; | |
| 332 } | |
| 333 | |
|
1838
e4fd799c194b
added methods distance_to and distance_squared_to to the vector class
lorenz
parents:
1837
diff
changeset
|
334 static double |
|
e4fd799c194b
added methods distance_to and distance_squared_to to the vector class
lorenz
parents:
1837
diff
changeset
|
335 _scalar_product(const double *coords1, const double *coords2, int size) |
|
e4fd799c194b
added methods distance_to and distance_squared_to to the vector class
lorenz
parents:
1837
diff
changeset
|
336 { |
|
e4fd799c194b
added methods distance_to and distance_squared_to to the vector class
lorenz
parents:
1837
diff
changeset
|
337 int i; |
|
e4fd799c194b
added methods distance_to and distance_squared_to to the vector class
lorenz
parents:
1837
diff
changeset
|
338 double product = 0; |
|
e4fd799c194b
added methods distance_to and distance_squared_to to the vector class
lorenz
parents:
1837
diff
changeset
|
339 for (i = 0; i < size; ++i) |
|
e4fd799c194b
added methods distance_to and distance_squared_to to the vector class
lorenz
parents:
1837
diff
changeset
|
340 product += coords1[i] * coords2[i]; |
|
e4fd799c194b
added methods distance_to and distance_squared_to to the vector class
lorenz
parents:
1837
diff
changeset
|
341 return product; |
|
e4fd799c194b
added methods distance_to and distance_squared_to to the vector class
lorenz
parents:
1837
diff
changeset
|
342 } |
|
e4fd799c194b
added methods distance_to and distance_squared_to to the vector class
lorenz
parents:
1837
diff
changeset
|
343 |
| 1834 | 344 |
| 345 static PyMemberDef vector_members[] = { | |
| 346 {"epsilon", T_DOUBLE, offsetof(PyVector, epsilon), 0, | |
| 347 "small value used in comparisons"}, | |
| 348 {NULL} /* Sentinel */ | |
| 349 }; | |
| 350 | |
| 351 | |
| 352 static PyObject* | |
| 353 PyVector_NEW(int dim) | |
| 354 { | |
| 355 PyVector *vec; | |
| 356 switch (dim) { | |
| 357 case 2: | |
| 358 vec = PyObject_New(PyVector, &PyVector2_Type); | |
| 359 break; | |
| 360 case 3: | |
| 361 vec = PyObject_New(PyVector, &PyVector3_Type); | |
| 362 break; | |
|
1844
a15f6323c39d
* implemented Vector3. there are no tests yet, so there are bound to be bugs
lorenz
parents:
1842
diff
changeset
|
363 /* |
| 1834 | 364 case 4: |
| 365 vec = PyObject_New(PyVector, &PyVector4_Type); | |
| 366 break; | |
| 367 */ | |
| 368 default: | |
| 369 PyErr_SetString(PyExc_SystemError, | |
| 370 "Wrong internal call to PyVector_NEW.\n"); | |
| 371 return NULL; | |
| 372 } | |
| 373 | |
| 374 if (vec != NULL) { | |
| 375 vec->dim = dim; | |
| 376 vec->epsilon = FLT_EPSILON; | |
| 377 vec->coords = PyMem_New(double, dim); | |
| 378 if (vec->coords == NULL) { | |
| 379 Py_DECREF(vec); | |
| 380 return PyErr_NoMemory(); | |
| 381 } | |
| 382 } | |
| 383 | |
| 384 return (PyObject *)vec; | |
| 385 } | |
| 386 | |
| 387 static void | |
| 388 vector_dealloc(PyVector* self) | |
| 389 { | |
| 390 PyMem_Del(self->coords); | |
| 391 self->ob_type->tp_free((PyObject*)self); | |
| 392 } | |
| 393 | |
| 394 | |
| 395 | |
| 396 | |
| 397 | |
| 398 | |
| 399 /********************************************** | |
| 400 * Generic vector PyNumber emulation routines | |
| 401 **********************************************/ | |
| 402 | |
| 403 static PyObject * | |
| 1848 | 404 vector_generic_math(PyObject *o1, PyObject *o2, int op) |
| 1834 | 405 { |
| 1848 | 406 int i, dim; |
| 407 double *vec_coords; | |
| 408 double tmp; | |
| 409 PyObject *other; | |
| 410 PyVector *vec, *ret; | |
| 1834 | 411 if (PyVector_Check(o1)) { |
| 1848 | 412 vec = (PyVector *)o1; |
| 413 other = o2; | |
| 1834 | 414 } |
| 415 else { | |
| 1848 | 416 vec = (PyVector *)o2; |
| 417 other = o1; | |
| 418 op |= OP_ARG_REVERSE; | |
| 1834 | 419 } |
| 1848 | 420 dim = vec->dim; |
| 421 vec_coords = vec->coords; | |
| 422 | |
| 423 if (PyVectorCompatible_Check(other, dim)) | |
| 424 op |= OP_ARG_VECTOR; | |
| 425 else if (RealNumber_Check(other)) | |
| 426 op |= OP_ARG_NUMBER; | |
| 427 else | |
| 428 op |= OP_ARG_UNKNOWN; | |
| 429 | |
| 430 switch (op) { | |
| 431 case OP_ADD | OP_ARG_VECTOR: | |
| 432 case OP_ADD | OP_ARG_VECTOR | OP_ARG_REVERSE: | |
| 433 ret = (PyVector*)PyVector_NEW(dim); | |
| 434 for (i = 0; i < dim; i++) { | |
| 435 ret->coords[i] = (vec_coords[i] + | |
| 436 PySequence_GetItem_AsDouble(other, i)); | |
| 437 } | |
| 438 return (PyObject*)ret; | |
| 439 case OP_IADD | OP_ARG_VECTOR: | |
| 440 for (i = 0; i < dim; i++) | |
| 441 vec_coords[i] += PySequence_GetItem_AsDouble(other, i); | |
| 442 return (PyObject*)vec; | |
| 443 case OP_SUB | OP_ARG_VECTOR: | |
| 444 ret = (PyVector*)PyVector_NEW(dim); | |
| 445 for (i = 0; i < dim; i++) { | |
| 446 ret->coords[i] = (vec_coords[i] - | |
| 447 PySequence_GetItem_AsDouble(other, i)); | |
| 448 } | |
| 449 return (PyObject*)ret; | |
| 450 case OP_SUB | OP_ARG_VECTOR | OP_ARG_REVERSE: | |
| 451 ret = (PyVector*)PyVector_NEW(dim); | |
| 452 for (i = 0; i < dim; i++) { | |
| 453 ret->coords[i] = (PySequence_GetItem_AsDouble(other, i) - | |
| 454 vec_coords[i]); | |
| 1834 | 455 } |
| 1848 | 456 return (PyObject*)ret; |
| 457 case OP_ISUB | OP_ARG_VECTOR: | |
| 458 for (i = 0; i < dim; i++) | |
| 459 vec_coords[i] -= PySequence_GetItem_AsDouble(other, i); | |
| 460 return (PyObject*)vec; | |
| 461 case OP_MUL | OP_ARG_VECTOR: | |
| 462 case OP_MUL | OP_ARG_VECTOR | OP_ARG_REVERSE: | |
| 463 tmp = 0.; | |
| 464 for (i = 0; i < dim; i++) | |
| 465 tmp += vec_coords[i] * PySequence_GetItem_AsDouble(other, i); | |
| 466 return PyFloat_FromDouble(tmp); | |
| 467 case OP_MUL | OP_ARG_NUMBER: | |
| 468 case OP_MUL | OP_ARG_NUMBER | OP_ARG_REVERSE: | |
| 469 ret = (PyVector*)PyVector_NEW(dim); | |
| 470 tmp = PyFloat_AsDouble(other); | |
| 471 for (i = 0; i < dim; i++) | |
| 472 ret->coords[i] = vec_coords[i] * tmp; | |
| 473 return (PyObject*)ret; | |
| 474 case OP_IMUL | OP_ARG_NUMBER: | |
| 475 tmp = PyFloat_AsDouble(other); | |
| 476 for (i = 0; i < dim; i++) | |
| 477 vec_coords[i] *= tmp; | |
| 478 return (PyObject*)vec; | |
| 479 case OP_DIV | OP_ARG_NUMBER: | |
| 480 tmp = 1. / PyFloat_AsDouble(other); | |
| 481 ret = (PyVector*)PyVector_NEW(dim); | |
| 482 for (i = 0; i < dim; i++) | |
| 483 ret->coords[i] = vec_coords[i] * tmp; | |
| 484 return (PyObject*)ret; | |
| 485 case OP_IDIV | OP_ARG_NUMBER: | |
| 486 tmp = 1. / PyFloat_AsDouble(other); | |
| 487 for (i = 0; i < dim; i++) | |
| 488 vec_coords[i] *= tmp; | |
| 489 return (PyObject*)vec; | |
| 490 case OP_FLOOR_DIV | OP_ARG_NUMBER: | |
| 491 tmp = 1. / PyFloat_AsDouble(other); | |
| 492 ret = (PyVector*)PyVector_NEW(dim); | |
| 493 for (i = 0; i < dim; i++) | |
| 494 ret->coords[i] = floor(vec_coords[i] * tmp); | |
| 495 return (PyObject*)ret; | |
| 496 case OP_IFLOOR_DIV | OP_ARG_NUMBER: | |
| 497 tmp = 1. / PyFloat_AsDouble(other); | |
| 498 for (i = 0; i < dim; i++) | |
| 499 vec->coords[i] = floor(vec_coords[i] * tmp); | |
| 500 return (PyObject*)vec; | |
| 501 default: | |
| 502 Py_INCREF(Py_NotImplemented); | |
| 503 return Py_NotImplemented; | |
| 1834 | 504 } |
| 505 } | |
| 506 | |
| 507 | |
| 508 static PyObject * | |
| 1848 | 509 vector_add(PyObject *o1, PyObject *o2) |
| 510 { | |
| 511 return vector_generic_math(o1, o2, OP_ADD); | |
| 512 } | |
| 513 static PyObject * | |
| 514 vector_inplace_add(PyVector *o1, PyObject *o2) | |
| 515 { | |
| 516 return vector_generic_math((PyObject*)o1, o2, OP_IADD); | |
| 517 } | |
| 518 static PyObject * | |
| 1834 | 519 vector_sub(PyObject *o1, PyObject *o2) |
| 520 { | |
| 1848 | 521 return vector_generic_math(o1, o2, OP_SUB); |
| 1834 | 522 } |
| 523 static PyObject * | |
| 1848 | 524 vector_inplace_sub(PyVector *o1, PyObject *o2) |
| 1834 | 525 { |
| 1848 | 526 return vector_generic_math((PyObject*)o1, o2, OP_ISUB); |
| 1834 | 527 } |
| 528 static PyObject * | |
| 529 vector_mul(PyObject *o1, PyObject *o2) | |
| 530 { | |
| 1848 | 531 return vector_generic_math(o1, o2, OP_MUL); |
| 1834 | 532 } |
| 533 static PyObject * | |
| 1848 | 534 vector_inplace_mul(PyVector *o1, PyObject *o2) |
| 1834 | 535 { |
| 1848 | 536 return vector_generic_math((PyObject*)o1, o2, OP_IMUL); |
| 537 } | |
| 538 static PyObject * | |
| 539 vector_div(PyVector *o1, PyObject *o2) | |
| 540 { | |
| 541 return vector_generic_math((PyObject*)o1, o2, OP_DIV); | |
| 1834 | 542 } |
| 543 static PyObject * | |
| 1848 | 544 vector_inplace_div(PyVector *o1, PyObject *o2) |
| 1834 | 545 { |
| 1848 | 546 return vector_generic_math((PyObject*)o1, o2, OP_IDIV); |
| 1834 | 547 } |
| 548 static PyObject * | |
| 1848 | 549 vector_floor_div(PyVector *o1, PyObject *o2) |
| 1834 | 550 { |
| 1848 | 551 return vector_generic_math((PyObject*)o1, o2, OP_FLOOR_DIV); |
| 1834 | 552 } |
| 553 static PyObject * | |
| 1848 | 554 vector_inplace_floor_div(PyVector *o1, PyObject *o2) |
| 1834 | 555 { |
| 1848 | 556 return vector_generic_math((PyObject*)o1, o2, OP_IFLOOR_DIV); |
| 1834 | 557 } |
| 558 | |
| 559 static PyObject * | |
| 560 vector_neg(PyVector *self) | |
| 561 { | |
| 562 int i; | |
| 563 PyVector *ret = (PyVector*)PyVector_NEW(self->dim); | |
| 564 for (i = 0; i < self->dim; i++) { | |
| 565 ret->coords[i] = -self->coords[i]; | |
| 566 } | |
| 567 return (PyObject*)ret; | |
| 568 } | |
| 569 | |
| 570 static PyObject * | |
| 571 vector_pos(PyVector *self) | |
| 572 { | |
| 573 int i; | |
| 574 PyVector *ret = (PyVector*)PyVector_NEW(self->dim); | |
| 575 memcpy(ret->coords, self->coords, sizeof(ret->coords[0]) * ret->dim); | |
| 576 return (PyObject*)ret; | |
| 577 } | |
| 578 | |
| 579 static int | |
| 580 vector_nonzero(PyVector *self) | |
| 581 { | |
| 582 int i; | |
| 583 for (i = 0; i < self->dim; i++) { | |
| 584 if (fabs(self->coords[i]) > self->epsilon) { | |
| 585 return 1; | |
| 586 } | |
| 587 } | |
| 588 return 0; | |
| 589 } | |
| 590 | |
|
1845
7d2a16e6a1d3
rearanged math module. added forward declarations at the beginning.
lorenz
parents:
1844
diff
changeset
|
591 static PyNumberMethods vector_as_number = { |
| 1834 | 592 (binaryfunc)vector_add, /* nb_add; __add__ */ |
| 593 (binaryfunc)vector_sub, /* nb_subtract; __sub__ */ | |
| 594 (binaryfunc)vector_mul, /* nb_multiply; __mul__ */ | |
| 595 (binaryfunc)vector_div, /* nb_divide; __div__ */ | |
| 596 (binaryfunc)0, /* nb_remainder; __mod__ */ | |
| 597 (binaryfunc)0, /* nb_divmod; __divmod__ */ | |
| 598 (ternaryfunc)0, /* nb_power; __pow__ */ | |
| 599 (unaryfunc)vector_neg, /* nb_negative; __neg__ */ | |
| 600 (unaryfunc)vector_pos, /* nb_positive; __pos__ */ | |
| 601 (unaryfunc)0, /* nb_absolute; __abs__ */ | |
| 602 (inquiry)vector_nonzero, /* nb_nonzero; __nonzero__ */ | |
| 603 (unaryfunc)0, /* nb_invert; __invert__ */ | |
| 604 (binaryfunc)0, /* nb_lshift; __lshift__ */ | |
| 605 (binaryfunc)0, /* nb_rshift; __rshift__ */ | |
| 606 (binaryfunc)0, /* nb_and; __and__ */ | |
| 607 (binaryfunc)0, /* nb_xor; __xor__ */ | |
| 608 (binaryfunc)0, /* nb_or; __or__ */ | |
| 609 (coercion)0, /* nb_coerce; __coerce__ */ | |
| 610 (unaryfunc)0, /* nb_int; __int__ */ | |
| 611 (unaryfunc)0, /* nb_long; __long__ */ | |
| 612 (unaryfunc)0, /* nb_float; __float__ */ | |
| 613 (unaryfunc)0, /* nb_oct; __oct__ */ | |
| 614 (unaryfunc)0, /* nb_hex; __hex__ */ | |
| 615 | |
| 616 /* Added in release 2.0 */ | |
| 617 (binaryfunc)vector_inplace_add, /* nb_inplace_add; __iadd__ */ | |
| 618 (binaryfunc)vector_inplace_sub, /* nb_inplace_subtract; __isub__ */ | |
| 619 (binaryfunc)vector_inplace_mul, /* nb_inplace_multiply; __imul__ */ | |
| 620 (binaryfunc)vector_inplace_div, /* nb_inplace_divide; __idiv__ */ | |
| 621 (binaryfunc)0, /* nb_inplace_remainder; __imod__ */ | |
| 622 (ternaryfunc)0, /* nb_inplace_power; __pow__ */ | |
| 623 (binaryfunc)0, /* nb_inplace_lshift; __ilshift__ */ | |
| 624 (binaryfunc)0, /* nb_inplace_rshift; __irshift__ */ | |
| 625 (binaryfunc)0, /* nb_inplace_and; __iand__ */ | |
| 626 (binaryfunc)0, /* nb_inplace_xor; __ixor__ */ | |
| 627 (binaryfunc)0, /* nb_inplace_or; __ior__ */ | |
| 628 | |
| 629 /* Added in release 2.2 */ | |
| 630 (binaryfunc)vector_floor_div, /* nb_floor_divide; __floor__ */ | |
| 631 (binaryfunc)vector_div, /* nb_true_divide; __truediv__ */ | |
| 632 (binaryfunc)vector_inplace_floor_div, /* nb_inplace_floor_divide; __ifloor__ */ | |
| 633 (binaryfunc)vector_inplace_div, /* nb_inplace_true_divide; __itruediv__ */ | |
| 634 | |
| 635 /* Added in release 2.5 */ | |
| 636 (unaryfunc)0, /* nb_index; __index__ */ | |
| 637 }; | |
| 638 | |
| 639 | |
| 640 | |
| 641 /************************************************* | |
| 642 * Generic vector PySequence emulation routines | |
| 643 *************************************************/ | |
| 644 | |
| 645 static Py_ssize_t | |
| 646 vector_len(PyVector *self) | |
| 647 { | |
| 648 return (Py_ssize_t)self->dim; | |
| 649 } | |
| 650 | |
| 651 static PyObject * | |
| 652 vector_GetItem(PyVector *self, Py_ssize_t index) | |
| 653 { | |
| 654 if (index < 0 || index >= self->dim) { | |
| 1836 | 655 PyErr_SetString(PyExc_IndexError, "subscript out of range."); |
| 656 return NULL; | |
| 1834 | 657 } |
| 658 return PyFloat_FromDouble(self->coords[index]); | |
| 659 } | |
| 660 | |
| 661 static int | |
| 662 vector_SetItem(PyVector *self, Py_ssize_t index, PyObject *value) | |
| 663 { | |
| 664 if (index < 0 || index >= self->dim) { | |
| 1836 | 665 PyErr_SetString(PyExc_IndexError, "subscript out of range."); |
| 1834 | 666 return -1; |
| 667 } | |
| 668 self->coords[index] = PyFloat_AsDouble(value); | |
| 669 return 0; | |
| 670 } | |
| 671 | |
| 1836 | 672 |
| 673 static PyObject * | |
| 674 vector_GetSlice(PyVector *self, Py_ssize_t ilow, Py_ssize_t ihigh) | |
| 675 { | |
| 676 /* some code was taken from the CPython source listobject.c */ | |
| 677 PyListObject *slice; | |
| 678 Py_ssize_t i, len; | |
| 679 double *src; | |
| 680 PyObject **dest; | |
| 681 | |
| 682 /* make sure boundaries are sane */ | |
| 683 if (ilow < 0) | |
| 684 ilow = 0; | |
| 685 else if (ilow > self->dim) | |
| 686 ilow = self->dim; | |
| 687 if (ihigh < ilow) | |
| 688 ihigh = ilow; | |
| 689 else if (ihigh > self->dim) | |
| 690 ihigh = self->dim; | |
| 691 | |
| 692 len = ihigh - ilow; | |
| 693 slice = (PyListObject *) PyList_New(len); | |
| 694 if (slice == NULL) | |
| 695 return NULL; | |
| 696 | |
| 697 src = self->coords + ilow; | |
| 698 dest = slice->ob_item; | |
| 699 for (i = 0; i < len; i++) { | |
| 700 dest[i] = PyFloat_FromDouble(src[i]); | |
| 701 } | |
| 702 return (PyObject *)slice; | |
| 703 } | |
| 704 | |
| 705 static int | |
| 706 vector_SetSlice(PyVector *self, Py_ssize_t ilow, Py_ssize_t ihigh, PyObject *v) | |
| 707 { | |
| 708 Py_ssize_t i, len; | |
| 709 | |
| 710 if (ilow < 0) | |
| 711 ilow = 0; | |
| 712 else if (ilow > self->dim) | |
| 713 ilow = self->dim; | |
| 714 if (ihigh < ilow) | |
| 715 ihigh = ilow; | |
| 716 else if (ihigh > self->dim) | |
| 717 ihigh = self->dim; | |
| 718 | |
| 719 len = ihigh - ilow; | |
| 720 if (len != PySequence_Length(v)) { | |
| 721 PyErr_SetString(PyExc_ValueError, | |
| 722 "Cannot assign slice of different length."); | |
| 723 return -1; | |
| 724 } | |
| 725 | |
| 726 /* TODO: better error checking. for example if v is not a | |
| 727 sequence or doesn't numbers. */ | |
| 728 for (i = 0; i < len; ++i) { | |
| 729 self->coords[i + ilow] = PySequence_GetItem_AsDouble(v, i); | |
| 730 } | |
| 731 return 0; | |
| 732 } | |
| 733 | |
| 734 | |
|
1845
7d2a16e6a1d3
rearanged math module. added forward declarations at the beginning.
lorenz
parents:
1844
diff
changeset
|
735 static PySequenceMethods vector_as_sequence = { |
| 1834 | 736 (lenfunc)vector_len, /* sq_length; __len__ */ |
| 737 (binaryfunc)0, /* sq_concat; __add__ */ | |
| 738 (ssizeargfunc)0, /* sq_repeat; __mul__ */ | |
| 739 (ssizeargfunc)vector_GetItem, /* sq_item; __getitem__ */ | |
| 1836 | 740 (ssizessizeargfunc)vector_GetSlice, /* sq_slice; __getslice__ */ |
| 1834 | 741 (ssizeobjargproc)vector_SetItem, /* sq_ass_item; __setitem__ */ |
| 1836 | 742 (ssizessizeobjargproc)vector_SetSlice, /* sq_ass_slice; __setslice__ */ |
| 1834 | 743 }; |
| 744 | |
| 745 static PyObject* | |
| 746 vector_getx (PyVector *self, void *closure) | |
| 747 { | |
| 748 return PyFloat_FromDouble(self->coords[0]); | |
| 749 } | |
| 750 | |
| 751 static int | |
| 752 vector_setx (PyVector *self, PyObject *value, void *closure) | |
| 753 { | |
| 754 if (value == NULL) { | |
| 755 PyErr_SetString(PyExc_TypeError, "Cannot delete the x attribute"); | |
| 756 return -1; | |
| 757 } | |
| 758 | |
| 759 self->coords[0] = PyFloat_AsDouble(value); | |
| 760 return 0; | |
| 761 } | |
| 762 | |
| 763 static PyObject* | |
| 764 vector_gety (PyVector *self, void *closure) | |
| 765 { | |
| 766 return PyFloat_FromDouble(self->coords[1]); | |
| 767 } | |
| 768 | |
| 769 static int | |
| 770 vector_sety (PyVector *self, PyObject *value, void *closure) | |
| 771 { | |
| 772 if (value == NULL) { | |
| 773 PyErr_SetString(PyExc_TypeError, "Cannot delete the y attribute"); | |
| 774 return -1; | |
| 775 } | |
| 776 | |
| 777 self->coords[1] = PyFloat_AsDouble(value); | |
| 778 return 0; | |
| 779 } | |
| 780 | |
|
1844
a15f6323c39d
* implemented Vector3. there are no tests yet, so there are bound to be bugs
lorenz
parents:
1842
diff
changeset
|
781 static PyObject* |
|
a15f6323c39d
* implemented Vector3. there are no tests yet, so there are bound to be bugs
lorenz
parents:
1842
diff
changeset
|
782 vector_getz (PyVector *self, void *closure) |
|
a15f6323c39d
* implemented Vector3. there are no tests yet, so there are bound to be bugs
lorenz
parents:
1842
diff
changeset
|
783 { |
|
a15f6323c39d
* implemented Vector3. there are no tests yet, so there are bound to be bugs
lorenz
parents:
1842
diff
changeset
|
784 return PyFloat_FromDouble(self->coords[2]); |
|
a15f6323c39d
* implemented Vector3. there are no tests yet, so there are bound to be bugs
lorenz
parents:
1842
diff
changeset
|
785 } |
|
a15f6323c39d
* implemented Vector3. there are no tests yet, so there are bound to be bugs
lorenz
parents:
1842
diff
changeset
|
786 |
|
a15f6323c39d
* implemented Vector3. there are no tests yet, so there are bound to be bugs
lorenz
parents:
1842
diff
changeset
|
787 static int |
|
a15f6323c39d
* implemented Vector3. there are no tests yet, so there are bound to be bugs
lorenz
parents:
1842
diff
changeset
|
788 vector_setz (PyVector *self, PyObject *value, void *closure) |
|
a15f6323c39d
* implemented Vector3. there are no tests yet, so there are bound to be bugs
lorenz
parents:
1842
diff
changeset
|
789 { |
|
a15f6323c39d
* implemented Vector3. there are no tests yet, so there are bound to be bugs
lorenz
parents:
1842
diff
changeset
|
790 if (value == NULL) { |
|
a15f6323c39d
* implemented Vector3. there are no tests yet, so there are bound to be bugs
lorenz
parents:
1842
diff
changeset
|
791 PyErr_SetString(PyExc_TypeError, "Cannot delete the z attribute"); |
|
a15f6323c39d
* implemented Vector3. there are no tests yet, so there are bound to be bugs
lorenz
parents:
1842
diff
changeset
|
792 return -1; |
|
a15f6323c39d
* implemented Vector3. there are no tests yet, so there are bound to be bugs
lorenz
parents:
1842
diff
changeset
|
793 } |
|
a15f6323c39d
* implemented Vector3. there are no tests yet, so there are bound to be bugs
lorenz
parents:
1842
diff
changeset
|
794 |
|
a15f6323c39d
* implemented Vector3. there are no tests yet, so there are bound to be bugs
lorenz
parents:
1842
diff
changeset
|
795 self->coords[2] = PyFloat_AsDouble(value); |
|
a15f6323c39d
* implemented Vector3. there are no tests yet, so there are bound to be bugs
lorenz
parents:
1842
diff
changeset
|
796 return 0; |
|
a15f6323c39d
* implemented Vector3. there are no tests yet, so there are bound to be bugs
lorenz
parents:
1842
diff
changeset
|
797 } |
|
a15f6323c39d
* implemented Vector3. there are no tests yet, so there are bound to be bugs
lorenz
parents:
1842
diff
changeset
|
798 |
|
a15f6323c39d
* implemented Vector3. there are no tests yet, so there are bound to be bugs
lorenz
parents:
1842
diff
changeset
|
799 static PyObject* |
|
a15f6323c39d
* implemented Vector3. there are no tests yet, so there are bound to be bugs
lorenz
parents:
1842
diff
changeset
|
800 vector_getw (PyVector *self, void *closure) |
|
a15f6323c39d
* implemented Vector3. there are no tests yet, so there are bound to be bugs
lorenz
parents:
1842
diff
changeset
|
801 { |
|
a15f6323c39d
* implemented Vector3. there are no tests yet, so there are bound to be bugs
lorenz
parents:
1842
diff
changeset
|
802 return PyFloat_FromDouble(self->coords[3]); |
|
a15f6323c39d
* implemented Vector3. there are no tests yet, so there are bound to be bugs
lorenz
parents:
1842
diff
changeset
|
803 } |
|
a15f6323c39d
* implemented Vector3. there are no tests yet, so there are bound to be bugs
lorenz
parents:
1842
diff
changeset
|
804 |
|
a15f6323c39d
* implemented Vector3. there are no tests yet, so there are bound to be bugs
lorenz
parents:
1842
diff
changeset
|
805 static int |
|
a15f6323c39d
* implemented Vector3. there are no tests yet, so there are bound to be bugs
lorenz
parents:
1842
diff
changeset
|
806 vector_setw (PyVector *self, PyObject *value, void *closure) |
|
a15f6323c39d
* implemented Vector3. there are no tests yet, so there are bound to be bugs
lorenz
parents:
1842
diff
changeset
|
807 { |
|
a15f6323c39d
* implemented Vector3. there are no tests yet, so there are bound to be bugs
lorenz
parents:
1842
diff
changeset
|
808 if (value == NULL) { |
|
a15f6323c39d
* implemented Vector3. there are no tests yet, so there are bound to be bugs
lorenz
parents:
1842
diff
changeset
|
809 PyErr_SetString(PyExc_TypeError, "Cannot delete the w attribute"); |
|
a15f6323c39d
* implemented Vector3. there are no tests yet, so there are bound to be bugs
lorenz
parents:
1842
diff
changeset
|
810 return -1; |
|
a15f6323c39d
* implemented Vector3. there are no tests yet, so there are bound to be bugs
lorenz
parents:
1842
diff
changeset
|
811 } |
|
a15f6323c39d
* implemented Vector3. there are no tests yet, so there are bound to be bugs
lorenz
parents:
1842
diff
changeset
|
812 |
|
a15f6323c39d
* implemented Vector3. there are no tests yet, so there are bound to be bugs
lorenz
parents:
1842
diff
changeset
|
813 self->coords[3] = PyFloat_AsDouble(value); |
|
a15f6323c39d
* implemented Vector3. there are no tests yet, so there are bound to be bugs
lorenz
parents:
1842
diff
changeset
|
814 return 0; |
|
a15f6323c39d
* implemented Vector3. there are no tests yet, so there are bound to be bugs
lorenz
parents:
1842
diff
changeset
|
815 } |
|
a15f6323c39d
* implemented Vector3. there are no tests yet, so there are bound to be bugs
lorenz
parents:
1842
diff
changeset
|
816 |
| 1834 | 817 |
| 818 | |
| 819 static PyObject * | |
| 820 vector_richcompare(PyVector *self, PyObject *other, int op) | |
| 821 { | |
| 822 int i; | |
|
1841
a7dc31e50c46
* added the vetor_elementwiseproxy which lets you apply calculations on
lorenz
parents:
1840
diff
changeset
|
823 double diff; |
|
1845
7d2a16e6a1d3
rearanged math module. added forward declarations at the beginning.
lorenz
parents:
1844
diff
changeset
|
824 if (!PyVectorCompatible_Check(other, self->dim)) { |
| 1834 | 825 if (op == Py_EQ) |
| 826 Py_RETURN_FALSE; | |
| 827 else if (op == Py_NE) | |
| 828 Py_RETURN_TRUE; | |
| 829 else { | |
| 830 Py_INCREF(Py_NotImplemented); | |
| 831 return Py_NotImplemented; | |
| 832 } | |
| 833 } | |
| 834 | |
| 835 switch(op) { | |
| 836 case Py_EQ: | |
| 837 for (i = 0; i < self->dim; i++) { | |
|
1841
a7dc31e50c46
* added the vetor_elementwiseproxy which lets you apply calculations on
lorenz
parents:
1840
diff
changeset
|
838 diff = self->coords[i] - PySequence_GetItem_AsDouble(other, i); |
|
a7dc31e50c46
* added the vetor_elementwiseproxy which lets you apply calculations on
lorenz
parents:
1840
diff
changeset
|
839 // test diff != diff to catch NaN |
|
a7dc31e50c46
* added the vetor_elementwiseproxy which lets you apply calculations on
lorenz
parents:
1840
diff
changeset
|
840 if ((diff != diff) || (fabs(diff) >= self->epsilon)) { |
| 1834 | 841 Py_RETURN_FALSE; |
| 842 } | |
| 843 } | |
| 844 Py_RETURN_TRUE; | |
| 845 case Py_NE: | |
| 846 for (i = 0; i < self->dim; i++) { | |
|
1841
a7dc31e50c46
* added the vetor_elementwiseproxy which lets you apply calculations on
lorenz
parents:
1840
diff
changeset
|
847 diff = self->coords[i] - PySequence_GetItem_AsDouble(other, i); |
|
a7dc31e50c46
* added the vetor_elementwiseproxy which lets you apply calculations on
lorenz
parents:
1840
diff
changeset
|
848 if ((diff != diff) || (fabs(diff) >= self->epsilon)) { |
| 1834 | 849 Py_RETURN_TRUE; |
| 850 } | |
| 851 } | |
| 852 Py_RETURN_FALSE; | |
| 853 default: | |
| 854 Py_INCREF(Py_NotImplemented); | |
| 855 return Py_NotImplemented; | |
| 856 } | |
| 857 } | |
| 858 | |
| 859 | |
| 860 | |
| 861 | |
| 862 | |
| 863 | |
| 864 | |
|
1845
7d2a16e6a1d3
rearanged math module. added forward declarations at the beginning.
lorenz
parents:
1844
diff
changeset
|
865 |
|
7d2a16e6a1d3
rearanged math module. added forward declarations at the beginning.
lorenz
parents:
1844
diff
changeset
|
866 static PyObject * |
|
7d2a16e6a1d3
rearanged math module. added forward declarations at the beginning.
lorenz
parents:
1844
diff
changeset
|
867 vector_length(PyVector *self) |
|
7d2a16e6a1d3
rearanged math module. added forward declarations at the beginning.
lorenz
parents:
1844
diff
changeset
|
868 { |
|
7d2a16e6a1d3
rearanged math module. added forward declarations at the beginning.
lorenz
parents:
1844
diff
changeset
|
869 double length_squared = _scalar_product(self->coords, self->coords, |
|
7d2a16e6a1d3
rearanged math module. added forward declarations at the beginning.
lorenz
parents:
1844
diff
changeset
|
870 self->dim); |
|
7d2a16e6a1d3
rearanged math module. added forward declarations at the beginning.
lorenz
parents:
1844
diff
changeset
|
871 return PyFloat_FromDouble(sqrt(length_squared)); |
|
7d2a16e6a1d3
rearanged math module. added forward declarations at the beginning.
lorenz
parents:
1844
diff
changeset
|
872 } |
|
7d2a16e6a1d3
rearanged math module. added forward declarations at the beginning.
lorenz
parents:
1844
diff
changeset
|
873 |
|
7d2a16e6a1d3
rearanged math module. added forward declarations at the beginning.
lorenz
parents:
1844
diff
changeset
|
874 static PyObject * |
|
7d2a16e6a1d3
rearanged math module. added forward declarations at the beginning.
lorenz
parents:
1844
diff
changeset
|
875 vector_length_squared(PyVector *self) |
|
7d2a16e6a1d3
rearanged math module. added forward declarations at the beginning.
lorenz
parents:
1844
diff
changeset
|
876 { |
|
7d2a16e6a1d3
rearanged math module. added forward declarations at the beginning.
lorenz
parents:
1844
diff
changeset
|
877 double length_squared = _scalar_product(self->coords, self->coords, |
|
7d2a16e6a1d3
rearanged math module. added forward declarations at the beginning.
lorenz
parents:
1844
diff
changeset
|
878 self->dim); |
|
7d2a16e6a1d3
rearanged math module. added forward declarations at the beginning.
lorenz
parents:
1844
diff
changeset
|
879 return PyFloat_FromDouble(length_squared); |
|
7d2a16e6a1d3
rearanged math module. added forward declarations at the beginning.
lorenz
parents:
1844
diff
changeset
|
880 } |
|
7d2a16e6a1d3
rearanged math module. added forward declarations at the beginning.
lorenz
parents:
1844
diff
changeset
|
881 |
|
7d2a16e6a1d3
rearanged math module. added forward declarations at the beginning.
lorenz
parents:
1844
diff
changeset
|
882 static PyObject * |
|
7d2a16e6a1d3
rearanged math module. added forward declarations at the beginning.
lorenz
parents:
1844
diff
changeset
|
883 vector_normalize(PyVector *self) |
|
7d2a16e6a1d3
rearanged math module. added forward declarations at the beginning.
lorenz
parents:
1844
diff
changeset
|
884 { |
|
7d2a16e6a1d3
rearanged math module. added forward declarations at the beginning.
lorenz
parents:
1844
diff
changeset
|
885 int i; |
|
7d2a16e6a1d3
rearanged math module. added forward declarations at the beginning.
lorenz
parents:
1844
diff
changeset
|
886 double length; |
|
7d2a16e6a1d3
rearanged math module. added forward declarations at the beginning.
lorenz
parents:
1844
diff
changeset
|
887 PyVector *ret; |
|
7d2a16e6a1d3
rearanged math module. added forward declarations at the beginning.
lorenz
parents:
1844
diff
changeset
|
888 |
|
7d2a16e6a1d3
rearanged math module. added forward declarations at the beginning.
lorenz
parents:
1844
diff
changeset
|
889 length = sqrt(_scalar_product(self->coords, self->coords, self->dim)); |
|
7d2a16e6a1d3
rearanged math module. added forward declarations at the beginning.
lorenz
parents:
1844
diff
changeset
|
890 if (length == 0) { |
|
7d2a16e6a1d3
rearanged math module. added forward declarations at the beginning.
lorenz
parents:
1844
diff
changeset
|
891 PyErr_SetString(PyExc_ZeroDivisionError, |
|
7d2a16e6a1d3
rearanged math module. added forward declarations at the beginning.
lorenz
parents:
1844
diff
changeset
|
892 "Can't normalize Vector of length Zero"); |
|
7d2a16e6a1d3
rearanged math module. added forward declarations at the beginning.
lorenz
parents:
1844
diff
changeset
|
893 return NULL; |
|
7d2a16e6a1d3
rearanged math module. added forward declarations at the beginning.
lorenz
parents:
1844
diff
changeset
|
894 } |
|
7d2a16e6a1d3
rearanged math module. added forward declarations at the beginning.
lorenz
parents:
1844
diff
changeset
|
895 |
|
7d2a16e6a1d3
rearanged math module. added forward declarations at the beginning.
lorenz
parents:
1844
diff
changeset
|
896 ret = (PyVector*)PyVector_NEW(self->dim); |
|
7d2a16e6a1d3
rearanged math module. added forward declarations at the beginning.
lorenz
parents:
1844
diff
changeset
|
897 for (i = 0; i < self->dim; ++i) |
|
7d2a16e6a1d3
rearanged math module. added forward declarations at the beginning.
lorenz
parents:
1844
diff
changeset
|
898 ret->coords[i] = self->coords[i] / length; |
|
7d2a16e6a1d3
rearanged math module. added forward declarations at the beginning.
lorenz
parents:
1844
diff
changeset
|
899 |
|
7d2a16e6a1d3
rearanged math module. added forward declarations at the beginning.
lorenz
parents:
1844
diff
changeset
|
900 return (PyObject *)ret; |
|
7d2a16e6a1d3
rearanged math module. added forward declarations at the beginning.
lorenz
parents:
1844
diff
changeset
|
901 } |
|
7d2a16e6a1d3
rearanged math module. added forward declarations at the beginning.
lorenz
parents:
1844
diff
changeset
|
902 |
|
7d2a16e6a1d3
rearanged math module. added forward declarations at the beginning.
lorenz
parents:
1844
diff
changeset
|
903 static PyObject * |
|
7d2a16e6a1d3
rearanged math module. added forward declarations at the beginning.
lorenz
parents:
1844
diff
changeset
|
904 vector_normalize_ip(PyVector *self) |
|
7d2a16e6a1d3
rearanged math module. added forward declarations at the beginning.
lorenz
parents:
1844
diff
changeset
|
905 { |
|
7d2a16e6a1d3
rearanged math module. added forward declarations at the beginning.
lorenz
parents:
1844
diff
changeset
|
906 int i; |
|
7d2a16e6a1d3
rearanged math module. added forward declarations at the beginning.
lorenz
parents:
1844
diff
changeset
|
907 double length; |
|
7d2a16e6a1d3
rearanged math module. added forward declarations at the beginning.
lorenz
parents:
1844
diff
changeset
|
908 |
|
7d2a16e6a1d3
rearanged math module. added forward declarations at the beginning.
lorenz
parents:
1844
diff
changeset
|
909 length = sqrt(_scalar_product(self->coords, self->coords, self->dim)); |
|
7d2a16e6a1d3
rearanged math module. added forward declarations at the beginning.
lorenz
parents:
1844
diff
changeset
|
910 |
|
7d2a16e6a1d3
rearanged math module. added forward declarations at the beginning.
lorenz
parents:
1844
diff
changeset
|
911 if (length == 0) { |
|
7d2a16e6a1d3
rearanged math module. added forward declarations at the beginning.
lorenz
parents:
1844
diff
changeset
|
912 PyErr_SetString(PyExc_ZeroDivisionError, |
|
7d2a16e6a1d3
rearanged math module. added forward declarations at the beginning.
lorenz
parents:
1844
diff
changeset
|
913 "Can't normalize Vector of length Zero"); |
|
7d2a16e6a1d3
rearanged math module. added forward declarations at the beginning.
lorenz
parents:
1844
diff
changeset
|
914 return NULL; |
|
7d2a16e6a1d3
rearanged math module. added forward declarations at the beginning.
lorenz
parents:
1844
diff
changeset
|
915 } |
|
7d2a16e6a1d3
rearanged math module. added forward declarations at the beginning.
lorenz
parents:
1844
diff
changeset
|
916 |
|
7d2a16e6a1d3
rearanged math module. added forward declarations at the beginning.
lorenz
parents:
1844
diff
changeset
|
917 for (i = 0; i < self->dim; ++i) |
|
7d2a16e6a1d3
rearanged math module. added forward declarations at the beginning.
lorenz
parents:
1844
diff
changeset
|
918 self->coords[i] /= length; |
|
7d2a16e6a1d3
rearanged math module. added forward declarations at the beginning.
lorenz
parents:
1844
diff
changeset
|
919 |
|
7d2a16e6a1d3
rearanged math module. added forward declarations at the beginning.
lorenz
parents:
1844
diff
changeset
|
920 Py_RETURN_NONE; |
|
7d2a16e6a1d3
rearanged math module. added forward declarations at the beginning.
lorenz
parents:
1844
diff
changeset
|
921 } |
|
7d2a16e6a1d3
rearanged math module. added forward declarations at the beginning.
lorenz
parents:
1844
diff
changeset
|
922 |
|
7d2a16e6a1d3
rearanged math module. added forward declarations at the beginning.
lorenz
parents:
1844
diff
changeset
|
923 static PyObject * |
|
7d2a16e6a1d3
rearanged math module. added forward declarations at the beginning.
lorenz
parents:
1844
diff
changeset
|
924 vector_is_normalized(PyVector *self) |
|
7d2a16e6a1d3
rearanged math module. added forward declarations at the beginning.
lorenz
parents:
1844
diff
changeset
|
925 { |
|
7d2a16e6a1d3
rearanged math module. added forward declarations at the beginning.
lorenz
parents:
1844
diff
changeset
|
926 double length_squared = _scalar_product(self->coords, self->coords, |
|
7d2a16e6a1d3
rearanged math module. added forward declarations at the beginning.
lorenz
parents:
1844
diff
changeset
|
927 self->dim); |
|
7d2a16e6a1d3
rearanged math module. added forward declarations at the beginning.
lorenz
parents:
1844
diff
changeset
|
928 if (fabs(length_squared - 1) < self->epsilon) |
|
7d2a16e6a1d3
rearanged math module. added forward declarations at the beginning.
lorenz
parents:
1844
diff
changeset
|
929 Py_RETURN_TRUE; |
|
7d2a16e6a1d3
rearanged math module. added forward declarations at the beginning.
lorenz
parents:
1844
diff
changeset
|
930 else |
|
7d2a16e6a1d3
rearanged math module. added forward declarations at the beginning.
lorenz
parents:
1844
diff
changeset
|
931 Py_RETURN_FALSE; |
|
7d2a16e6a1d3
rearanged math module. added forward declarations at the beginning.
lorenz
parents:
1844
diff
changeset
|
932 } |
|
7d2a16e6a1d3
rearanged math module. added forward declarations at the beginning.
lorenz
parents:
1844
diff
changeset
|
933 |
|
7d2a16e6a1d3
rearanged math module. added forward declarations at the beginning.
lorenz
parents:
1844
diff
changeset
|
934 static PyObject * |
|
7d2a16e6a1d3
rearanged math module. added forward declarations at the beginning.
lorenz
parents:
1844
diff
changeset
|
935 vector_dot(PyVector *self, PyObject *other) |
|
7d2a16e6a1d3
rearanged math module. added forward declarations at the beginning.
lorenz
parents:
1844
diff
changeset
|
936 { |
|
7d2a16e6a1d3
rearanged math module. added forward declarations at the beginning.
lorenz
parents:
1844
diff
changeset
|
937 double other_coords[VECTOR_MAX_SIZE]; |
|
7d2a16e6a1d3
rearanged math module. added forward declarations at the beginning.
lorenz
parents:
1844
diff
changeset
|
938 if (!PySequence_AsVectorCoords(other, other_coords, self->dim)) { |
|
7d2a16e6a1d3
rearanged math module. added forward declarations at the beginning.
lorenz
parents:
1844
diff
changeset
|
939 PyErr_SetString(PyExc_TypeError, "Cannot perform dot product with this type."); |
|
7d2a16e6a1d3
rearanged math module. added forward declarations at the beginning.
lorenz
parents:
1844
diff
changeset
|
940 return NULL; |
|
7d2a16e6a1d3
rearanged math module. added forward declarations at the beginning.
lorenz
parents:
1844
diff
changeset
|
941 } |
|
7d2a16e6a1d3
rearanged math module. added forward declarations at the beginning.
lorenz
parents:
1844
diff
changeset
|
942 return PyFloat_FromDouble(_scalar_product(self->coords, other_coords, |
|
7d2a16e6a1d3
rearanged math module. added forward declarations at the beginning.
lorenz
parents:
1844
diff
changeset
|
943 self->dim)); |
|
7d2a16e6a1d3
rearanged math module. added forward declarations at the beginning.
lorenz
parents:
1844
diff
changeset
|
944 } |
|
7d2a16e6a1d3
rearanged math module. added forward declarations at the beginning.
lorenz
parents:
1844
diff
changeset
|
945 |
|
7d2a16e6a1d3
rearanged math module. added forward declarations at the beginning.
lorenz
parents:
1844
diff
changeset
|
946 static PyObject * |
|
7d2a16e6a1d3
rearanged math module. added forward declarations at the beginning.
lorenz
parents:
1844
diff
changeset
|
947 vector_scale_to_length(PyVector *self, PyObject *length) |
|
7d2a16e6a1d3
rearanged math module. added forward declarations at the beginning.
lorenz
parents:
1844
diff
changeset
|
948 { |
|
7d2a16e6a1d3
rearanged math module. added forward declarations at the beginning.
lorenz
parents:
1844
diff
changeset
|
949 int i; |
|
7d2a16e6a1d3
rearanged math module. added forward declarations at the beginning.
lorenz
parents:
1844
diff
changeset
|
950 double new_length, old_length; |
|
7d2a16e6a1d3
rearanged math module. added forward declarations at the beginning.
lorenz
parents:
1844
diff
changeset
|
951 double fraction; |
|
7d2a16e6a1d3
rearanged math module. added forward declarations at the beginning.
lorenz
parents:
1844
diff
changeset
|
952 |
|
7d2a16e6a1d3
rearanged math module. added forward declarations at the beginning.
lorenz
parents:
1844
diff
changeset
|
953 if (!RealNumber_Check(length)) { |
|
7d2a16e6a1d3
rearanged math module. added forward declarations at the beginning.
lorenz
parents:
1844
diff
changeset
|
954 PyErr_SetString(PyExc_ValueError, "new length must be a number"); |
|
7d2a16e6a1d3
rearanged math module. added forward declarations at the beginning.
lorenz
parents:
1844
diff
changeset
|
955 return NULL; |
|
7d2a16e6a1d3
rearanged math module. added forward declarations at the beginning.
lorenz
parents:
1844
diff
changeset
|
956 } |
|
7d2a16e6a1d3
rearanged math module. added forward declarations at the beginning.
lorenz
parents:
1844
diff
changeset
|
957 new_length = PyFloat_AsDouble(length); |
|
7d2a16e6a1d3
rearanged math module. added forward declarations at the beginning.
lorenz
parents:
1844
diff
changeset
|
958 |
|
7d2a16e6a1d3
rearanged math module. added forward declarations at the beginning.
lorenz
parents:
1844
diff
changeset
|
959 old_length = 0; |
|
7d2a16e6a1d3
rearanged math module. added forward declarations at the beginning.
lorenz
parents:
1844
diff
changeset
|
960 for (i = 0; i < self->dim; ++i) |
|
7d2a16e6a1d3
rearanged math module. added forward declarations at the beginning.
lorenz
parents:
1844
diff
changeset
|
961 old_length += self->coords[i] * self->coords[i]; |
|
7d2a16e6a1d3
rearanged math module. added forward declarations at the beginning.
lorenz
parents:
1844
diff
changeset
|
962 old_length = sqrt(old_length); |
|
7d2a16e6a1d3
rearanged math module. added forward declarations at the beginning.
lorenz
parents:
1844
diff
changeset
|
963 |
|
7d2a16e6a1d3
rearanged math module. added forward declarations at the beginning.
lorenz
parents:
1844
diff
changeset
|
964 if (old_length < self->epsilon) { |
|
7d2a16e6a1d3
rearanged math module. added forward declarations at the beginning.
lorenz
parents:
1844
diff
changeset
|
965 PyErr_SetString(PyExc_ZeroDivisionError, "Cannot scale a vector with zero length"); |
|
7d2a16e6a1d3
rearanged math module. added forward declarations at the beginning.
lorenz
parents:
1844
diff
changeset
|
966 return NULL; |
|
7d2a16e6a1d3
rearanged math module. added forward declarations at the beginning.
lorenz
parents:
1844
diff
changeset
|
967 } |
|
7d2a16e6a1d3
rearanged math module. added forward declarations at the beginning.
lorenz
parents:
1844
diff
changeset
|
968 |
|
7d2a16e6a1d3
rearanged math module. added forward declarations at the beginning.
lorenz
parents:
1844
diff
changeset
|
969 fraction = new_length / old_length; |
|
7d2a16e6a1d3
rearanged math module. added forward declarations at the beginning.
lorenz
parents:
1844
diff
changeset
|
970 for (i = 0; i < self->dim; ++i) |
|
7d2a16e6a1d3
rearanged math module. added forward declarations at the beginning.
lorenz
parents:
1844
diff
changeset
|
971 self->coords[i] *= fraction; |
|
7d2a16e6a1d3
rearanged math module. added forward declarations at the beginning.
lorenz
parents:
1844
diff
changeset
|
972 |
|
7d2a16e6a1d3
rearanged math module. added forward declarations at the beginning.
lorenz
parents:
1844
diff
changeset
|
973 Py_RETURN_NONE; |
|
7d2a16e6a1d3
rearanged math module. added forward declarations at the beginning.
lorenz
parents:
1844
diff
changeset
|
974 } |
|
7d2a16e6a1d3
rearanged math module. added forward declarations at the beginning.
lorenz
parents:
1844
diff
changeset
|
975 |
|
7d2a16e6a1d3
rearanged math module. added forward declarations at the beginning.
lorenz
parents:
1844
diff
changeset
|
976 static int |
|
7d2a16e6a1d3
rearanged math module. added forward declarations at the beginning.
lorenz
parents:
1844
diff
changeset
|
977 _vector_reflect_helper(double *dst_coords, const double *src_coords, |
|
7d2a16e6a1d3
rearanged math module. added forward declarations at the beginning.
lorenz
parents:
1844
diff
changeset
|
978 PyObject *normal, int dim, double epsilon) |
|
7d2a16e6a1d3
rearanged math module. added forward declarations at the beginning.
lorenz
parents:
1844
diff
changeset
|
979 { |
|
7d2a16e6a1d3
rearanged math module. added forward declarations at the beginning.
lorenz
parents:
1844
diff
changeset
|
980 int i; |
|
7d2a16e6a1d3
rearanged math module. added forward declarations at the beginning.
lorenz
parents:
1844
diff
changeset
|
981 double dot_product; |
|
7d2a16e6a1d3
rearanged math module. added forward declarations at the beginning.
lorenz
parents:
1844
diff
changeset
|
982 double norm_length; |
|
7d2a16e6a1d3
rearanged math module. added forward declarations at the beginning.
lorenz
parents:
1844
diff
changeset
|
983 /* allocate enough space for 2, 3 and 4 dim vectors */ |
|
7d2a16e6a1d3
rearanged math module. added forward declarations at the beginning.
lorenz
parents:
1844
diff
changeset
|
984 double norm_coords[VECTOR_MAX_SIZE]; |
|
7d2a16e6a1d3
rearanged math module. added forward declarations at the beginning.
lorenz
parents:
1844
diff
changeset
|
985 |
|
7d2a16e6a1d3
rearanged math module. added forward declarations at the beginning.
lorenz
parents:
1844
diff
changeset
|
986 if (!PyVectorCompatible_Check(normal, dim)) { |
|
7d2a16e6a1d3
rearanged math module. added forward declarations at the beginning.
lorenz
parents:
1844
diff
changeset
|
987 PyErr_SetString(PyExc_TypeError, "Expected a vector."); |
|
7d2a16e6a1d3
rearanged math module. added forward declarations at the beginning.
lorenz
parents:
1844
diff
changeset
|
988 return RETURN_ERROR; |
|
7d2a16e6a1d3
rearanged math module. added forward declarations at the beginning.
lorenz
parents:
1844
diff
changeset
|
989 } |
|
7d2a16e6a1d3
rearanged math module. added forward declarations at the beginning.
lorenz
parents:
1844
diff
changeset
|
990 |
|
7d2a16e6a1d3
rearanged math module. added forward declarations at the beginning.
lorenz
parents:
1844
diff
changeset
|
991 /* normalize the normal */ |
|
7d2a16e6a1d3
rearanged math module. added forward declarations at the beginning.
lorenz
parents:
1844
diff
changeset
|
992 norm_length = 0; |
|
7d2a16e6a1d3
rearanged math module. added forward declarations at the beginning.
lorenz
parents:
1844
diff
changeset
|
993 for (i = 0; i < dim; ++i) { |
|
7d2a16e6a1d3
rearanged math module. added forward declarations at the beginning.
lorenz
parents:
1844
diff
changeset
|
994 norm_coords[i] = PySequence_GetItem_AsDouble(normal, i); |
|
7d2a16e6a1d3
rearanged math module. added forward declarations at the beginning.
lorenz
parents:
1844
diff
changeset
|
995 norm_length += norm_coords[i] * norm_coords[i]; |
|
7d2a16e6a1d3
rearanged math module. added forward declarations at the beginning.
lorenz
parents:
1844
diff
changeset
|
996 } |
|
7d2a16e6a1d3
rearanged math module. added forward declarations at the beginning.
lorenz
parents:
1844
diff
changeset
|
997 if (norm_length < epsilon) { |
|
7d2a16e6a1d3
rearanged math module. added forward declarations at the beginning.
lorenz
parents:
1844
diff
changeset
|
998 PyErr_SetString(PyExc_ZeroDivisionError, "Normal must not be of length zero."); |
|
7d2a16e6a1d3
rearanged math module. added forward declarations at the beginning.
lorenz
parents:
1844
diff
changeset
|
999 return RETURN_ERROR;; |
|
7d2a16e6a1d3
rearanged math module. added forward declarations at the beginning.
lorenz
parents:
1844
diff
changeset
|
1000 } |
|
7d2a16e6a1d3
rearanged math module. added forward declarations at the beginning.
lorenz
parents:
1844
diff
changeset
|
1001 if (norm_length != 1) { |
|
7d2a16e6a1d3
rearanged math module. added forward declarations at the beginning.
lorenz
parents:
1844
diff
changeset
|
1002 norm_length = sqrt(norm_length); |
|
7d2a16e6a1d3
rearanged math module. added forward declarations at the beginning.
lorenz
parents:
1844
diff
changeset
|
1003 for (i = 0; i < dim; ++i) |
|
7d2a16e6a1d3
rearanged math module. added forward declarations at the beginning.
lorenz
parents:
1844
diff
changeset
|
1004 norm_coords[i] /= norm_length; |
|
7d2a16e6a1d3
rearanged math module. added forward declarations at the beginning.
lorenz
parents:
1844
diff
changeset
|
1005 } |
|
7d2a16e6a1d3
rearanged math module. added forward declarations at the beginning.
lorenz
parents:
1844
diff
changeset
|
1006 |
|
7d2a16e6a1d3
rearanged math module. added forward declarations at the beginning.
lorenz
parents:
1844
diff
changeset
|
1007 /* calculate the dot_product for the projection */ |
|
7d2a16e6a1d3
rearanged math module. added forward declarations at the beginning.
lorenz
parents:
1844
diff
changeset
|
1008 dot_product = 0; |
|
7d2a16e6a1d3
rearanged math module. added forward declarations at the beginning.
lorenz
parents:
1844
diff
changeset
|
1009 for (i = 0; i < dim; ++i) |
|
7d2a16e6a1d3
rearanged math module. added forward declarations at the beginning.
lorenz
parents:
1844
diff
changeset
|
1010 dot_product += src_coords[i] * norm_coords[i]; |
|
7d2a16e6a1d3
rearanged math module. added forward declarations at the beginning.
lorenz
parents:
1844
diff
changeset
|
1011 |
|
7d2a16e6a1d3
rearanged math module. added forward declarations at the beginning.
lorenz
parents:
1844
diff
changeset
|
1012 for (i = 0; i < dim; ++i) |
|
7d2a16e6a1d3
rearanged math module. added forward declarations at the beginning.
lorenz
parents:
1844
diff
changeset
|
1013 dst_coords[i] = src_coords[i] - 2 * norm_coords[i] * dot_product; |
|
7d2a16e6a1d3
rearanged math module. added forward declarations at the beginning.
lorenz
parents:
1844
diff
changeset
|
1014 return RETURN_NO_ERROR; |
|
7d2a16e6a1d3
rearanged math module. added forward declarations at the beginning.
lorenz
parents:
1844
diff
changeset
|
1015 } |
|
7d2a16e6a1d3
rearanged math module. added forward declarations at the beginning.
lorenz
parents:
1844
diff
changeset
|
1016 |
|
7d2a16e6a1d3
rearanged math module. added forward declarations at the beginning.
lorenz
parents:
1844
diff
changeset
|
1017 static PyObject * |
|
7d2a16e6a1d3
rearanged math module. added forward declarations at the beginning.
lorenz
parents:
1844
diff
changeset
|
1018 vector_reflect(PyVector *self, PyObject *normal) |
|
7d2a16e6a1d3
rearanged math module. added forward declarations at the beginning.
lorenz
parents:
1844
diff
changeset
|
1019 { |
|
7d2a16e6a1d3
rearanged math module. added forward declarations at the beginning.
lorenz
parents:
1844
diff
changeset
|
1020 int error_code; |
|
7d2a16e6a1d3
rearanged math module. added forward declarations at the beginning.
lorenz
parents:
1844
diff
changeset
|
1021 PyVector *ret = (PyVector *)PyVector_NEW(self->dim); |
|
7d2a16e6a1d3
rearanged math module. added forward declarations at the beginning.
lorenz
parents:
1844
diff
changeset
|
1022 |
|
7d2a16e6a1d3
rearanged math module. added forward declarations at the beginning.
lorenz
parents:
1844
diff
changeset
|
1023 error_code = _vector_reflect_helper(ret->coords, self->coords, |
|
7d2a16e6a1d3
rearanged math module. added forward declarations at the beginning.
lorenz
parents:
1844
diff
changeset
|
1024 normal, self->dim, self->epsilon); |
|
7d2a16e6a1d3
rearanged math module. added forward declarations at the beginning.
lorenz
parents:
1844
diff
changeset
|
1025 if (error_code == RETURN_ERROR) |
|
7d2a16e6a1d3
rearanged math module. added forward declarations at the beginning.
lorenz
parents:
1844
diff
changeset
|
1026 return NULL; |
|
7d2a16e6a1d3
rearanged math module. added forward declarations at the beginning.
lorenz
parents:
1844
diff
changeset
|
1027 |
|
7d2a16e6a1d3
rearanged math module. added forward declarations at the beginning.
lorenz
parents:
1844
diff
changeset
|
1028 return (PyObject *)ret; |
|
7d2a16e6a1d3
rearanged math module. added forward declarations at the beginning.
lorenz
parents:
1844
diff
changeset
|
1029 } |
|
7d2a16e6a1d3
rearanged math module. added forward declarations at the beginning.
lorenz
parents:
1844
diff
changeset
|
1030 |
|
7d2a16e6a1d3
rearanged math module. added forward declarations at the beginning.
lorenz
parents:
1844
diff
changeset
|
1031 static PyObject * |
|
7d2a16e6a1d3
rearanged math module. added forward declarations at the beginning.
lorenz
parents:
1844
diff
changeset
|
1032 vector_reflect_ip(PyVector *self, PyObject *normal) |
|
7d2a16e6a1d3
rearanged math module. added forward declarations at the beginning.
lorenz
parents:
1844
diff
changeset
|
1033 { |
|
7d2a16e6a1d3
rearanged math module. added forward declarations at the beginning.
lorenz
parents:
1844
diff
changeset
|
1034 int error_code; |
|
7d2a16e6a1d3
rearanged math module. added forward declarations at the beginning.
lorenz
parents:
1844
diff
changeset
|
1035 double tmp_coords[VECTOR_MAX_SIZE]; |
|
7d2a16e6a1d3
rearanged math module. added forward declarations at the beginning.
lorenz
parents:
1844
diff
changeset
|
1036 |
|
7d2a16e6a1d3
rearanged math module. added forward declarations at the beginning.
lorenz
parents:
1844
diff
changeset
|
1037 error_code = _vector_reflect_helper(tmp_coords, self->coords, |
|
7d2a16e6a1d3
rearanged math module. added forward declarations at the beginning.
lorenz
parents:
1844
diff
changeset
|
1038 normal, self->dim, self->epsilon); |
|
7d2a16e6a1d3
rearanged math module. added forward declarations at the beginning.
lorenz
parents:
1844
diff
changeset
|
1039 if (error_code == RETURN_ERROR) |
|
7d2a16e6a1d3
rearanged math module. added forward declarations at the beginning.
lorenz
parents:
1844
diff
changeset
|
1040 return NULL; |
|
7d2a16e6a1d3
rearanged math module. added forward declarations at the beginning.
lorenz
parents:
1844
diff
changeset
|
1041 |
|
7d2a16e6a1d3
rearanged math module. added forward declarations at the beginning.
lorenz
parents:
1844
diff
changeset
|
1042 memcpy(self->coords, tmp_coords, self->dim * sizeof(tmp_coords[0])); |
|
7d2a16e6a1d3
rearanged math module. added forward declarations at the beginning.
lorenz
parents:
1844
diff
changeset
|
1043 Py_RETURN_NONE; |
|
7d2a16e6a1d3
rearanged math module. added forward declarations at the beginning.
lorenz
parents:
1844
diff
changeset
|
1044 } |
|
7d2a16e6a1d3
rearanged math module. added forward declarations at the beginning.
lorenz
parents:
1844
diff
changeset
|
1045 |
|
7d2a16e6a1d3
rearanged math module. added forward declarations at the beginning.
lorenz
parents:
1844
diff
changeset
|
1046 static PyObject * |
|
7d2a16e6a1d3
rearanged math module. added forward declarations at the beginning.
lorenz
parents:
1844
diff
changeset
|
1047 vector_distance_to(PyVector *self, PyObject *other) |
|
7d2a16e6a1d3
rearanged math module. added forward declarations at the beginning.
lorenz
parents:
1844
diff
changeset
|
1048 { |
|
7d2a16e6a1d3
rearanged math module. added forward declarations at the beginning.
lorenz
parents:
1844
diff
changeset
|
1049 int i; |
|
7d2a16e6a1d3
rearanged math module. added forward declarations at the beginning.
lorenz
parents:
1844
diff
changeset
|
1050 double distance_squared; |
|
7d2a16e6a1d3
rearanged math module. added forward declarations at the beginning.
lorenz
parents:
1844
diff
changeset
|
1051 |
|
7d2a16e6a1d3
rearanged math module. added forward declarations at the beginning.
lorenz
parents:
1844
diff
changeset
|
1052 distance_squared = 0; |
|
7d2a16e6a1d3
rearanged math module. added forward declarations at the beginning.
lorenz
parents:
1844
diff
changeset
|
1053 for (i = 0; i < self->dim; ++i) { |
|
7d2a16e6a1d3
rearanged math module. added forward declarations at the beginning.
lorenz
parents:
1844
diff
changeset
|
1054 double tmp = PySequence_GetItem_AsDouble(other, i) - self->coords[i]; |
|
7d2a16e6a1d3
rearanged math module. added forward declarations at the beginning.
lorenz
parents:
1844
diff
changeset
|
1055 distance_squared += tmp * tmp; |
|
7d2a16e6a1d3
rearanged math module. added forward declarations at the beginning.
lorenz
parents:
1844
diff
changeset
|
1056 } |
|
7d2a16e6a1d3
rearanged math module. added forward declarations at the beginning.
lorenz
parents:
1844
diff
changeset
|
1057 |
|
7d2a16e6a1d3
rearanged math module. added forward declarations at the beginning.
lorenz
parents:
1844
diff
changeset
|
1058 return PyFloat_FromDouble(sqrt(distance_squared)); |
|
7d2a16e6a1d3
rearanged math module. added forward declarations at the beginning.
lorenz
parents:
1844
diff
changeset
|
1059 } |
|
7d2a16e6a1d3
rearanged math module. added forward declarations at the beginning.
lorenz
parents:
1844
diff
changeset
|
1060 |
|
7d2a16e6a1d3
rearanged math module. added forward declarations at the beginning.
lorenz
parents:
1844
diff
changeset
|
1061 static PyObject * |
|
7d2a16e6a1d3
rearanged math module. added forward declarations at the beginning.
lorenz
parents:
1844
diff
changeset
|
1062 vector_distance_squared_to(PyVector *self, PyObject *other) |
|
7d2a16e6a1d3
rearanged math module. added forward declarations at the beginning.
lorenz
parents:
1844
diff
changeset
|
1063 { |
|
7d2a16e6a1d3
rearanged math module. added forward declarations at the beginning.
lorenz
parents:
1844
diff
changeset
|
1064 int i; |
|
7d2a16e6a1d3
rearanged math module. added forward declarations at the beginning.
lorenz
parents:
1844
diff
changeset
|
1065 double distance_squared; |
|
7d2a16e6a1d3
rearanged math module. added forward declarations at the beginning.
lorenz
parents:
1844
diff
changeset
|
1066 |
|
7d2a16e6a1d3
rearanged math module. added forward declarations at the beginning.
lorenz
parents:
1844
diff
changeset
|
1067 distance_squared = 0; |
|
7d2a16e6a1d3
rearanged math module. added forward declarations at the beginning.
lorenz
parents:
1844
diff
changeset
|
1068 for (i = 0; i < self->dim; ++i) { |
|
7d2a16e6a1d3
rearanged math module. added forward declarations at the beginning.
lorenz
parents:
1844
diff
changeset
|
1069 double tmp = PySequence_GetItem_AsDouble(other, i) - self->coords[i]; |
| 1848 | 1070 distance_squared += tmp * tmp; |
|
1845
7d2a16e6a1d3
rearanged math module. added forward declarations at the beginning.
lorenz
parents:
1844
diff
changeset
|
1071 } |
|
7d2a16e6a1d3
rearanged math module. added forward declarations at the beginning.
lorenz
parents:
1844
diff
changeset
|
1072 |
|
7d2a16e6a1d3
rearanged math module. added forward declarations at the beginning.
lorenz
parents:
1844
diff
changeset
|
1073 return PyFloat_FromDouble(distance_squared); |
|
7d2a16e6a1d3
rearanged math module. added forward declarations at the beginning.
lorenz
parents:
1844
diff
changeset
|
1074 } |
|
7d2a16e6a1d3
rearanged math module. added forward declarations at the beginning.
lorenz
parents:
1844
diff
changeset
|
1075 |
|
7d2a16e6a1d3
rearanged math module. added forward declarations at the beginning.
lorenz
parents:
1844
diff
changeset
|
1076 |
|
7d2a16e6a1d3
rearanged math module. added forward declarations at the beginning.
lorenz
parents:
1844
diff
changeset
|
1077 |
|
7d2a16e6a1d3
rearanged math module. added forward declarations at the beginning.
lorenz
parents:
1844
diff
changeset
|
1078 static PyObject * |
|
7d2a16e6a1d3
rearanged math module. added forward declarations at the beginning.
lorenz
parents:
1844
diff
changeset
|
1079 vector_repr(PyVector *self) |
|
7d2a16e6a1d3
rearanged math module. added forward declarations at the beginning.
lorenz
parents:
1844
diff
changeset
|
1080 { |
|
7d2a16e6a1d3
rearanged math module. added forward declarations at the beginning.
lorenz
parents:
1844
diff
changeset
|
1081 int i; |
|
7d2a16e6a1d3
rearanged math module. added forward declarations at the beginning.
lorenz
parents:
1844
diff
changeset
|
1082 int bufferIdx; |
|
7d2a16e6a1d3
rearanged math module. added forward declarations at the beginning.
lorenz
parents:
1844
diff
changeset
|
1083 char buffer[2][STRING_BUF_SIZE]; |
|
7d2a16e6a1d3
rearanged math module. added forward declarations at the beginning.
lorenz
parents:
1844
diff
changeset
|
1084 |
|
7d2a16e6a1d3
rearanged math module. added forward declarations at the beginning.
lorenz
parents:
1844
diff
changeset
|
1085 bufferIdx = 1; |
|
7d2a16e6a1d3
rearanged math module. added forward declarations at the beginning.
lorenz
parents:
1844
diff
changeset
|
1086 PyOS_snprintf(buffer[0], STRING_BUF_SIZE, "<Vector%d(", self->dim); |
|
7d2a16e6a1d3
rearanged math module. added forward declarations at the beginning.
lorenz
parents:
1844
diff
changeset
|
1087 for (i = 0; i < self->dim - 1; ++i) { |
|
7d2a16e6a1d3
rearanged math module. added forward declarations at the beginning.
lorenz
parents:
1844
diff
changeset
|
1088 PyOS_snprintf(buffer[bufferIdx % 2], STRING_BUF_SIZE, "%s%g, ", |
|
7d2a16e6a1d3
rearanged math module. added forward declarations at the beginning.
lorenz
parents:
1844
diff
changeset
|
1089 buffer[(bufferIdx + 1) % 2], self->coords[i]); |
|
7d2a16e6a1d3
rearanged math module. added forward declarations at the beginning.
lorenz
parents:
1844
diff
changeset
|
1090 bufferIdx++; |
|
7d2a16e6a1d3
rearanged math module. added forward declarations at the beginning.
lorenz
parents:
1844
diff
changeset
|
1091 } |
|
7d2a16e6a1d3
rearanged math module. added forward declarations at the beginning.
lorenz
parents:
1844
diff
changeset
|
1092 PyOS_snprintf(buffer[bufferIdx % 2], STRING_BUF_SIZE, "%s%g)>", |
|
7d2a16e6a1d3
rearanged math module. added forward declarations at the beginning.
lorenz
parents:
1844
diff
changeset
|
1093 buffer[(bufferIdx + 1) % 2], self->coords[i]); |
|
7d2a16e6a1d3
rearanged math module. added forward declarations at the beginning.
lorenz
parents:
1844
diff
changeset
|
1094 return PyString_FromString(buffer[bufferIdx % 2]); |
|
7d2a16e6a1d3
rearanged math module. added forward declarations at the beginning.
lorenz
parents:
1844
diff
changeset
|
1095 } |
|
7d2a16e6a1d3
rearanged math module. added forward declarations at the beginning.
lorenz
parents:
1844
diff
changeset
|
1096 |
|
7d2a16e6a1d3
rearanged math module. added forward declarations at the beginning.
lorenz
parents:
1844
diff
changeset
|
1097 static PyObject * |
|
7d2a16e6a1d3
rearanged math module. added forward declarations at the beginning.
lorenz
parents:
1844
diff
changeset
|
1098 vector_str(PyVector *self) |
|
7d2a16e6a1d3
rearanged math module. added forward declarations at the beginning.
lorenz
parents:
1844
diff
changeset
|
1099 { |
|
7d2a16e6a1d3
rearanged math module. added forward declarations at the beginning.
lorenz
parents:
1844
diff
changeset
|
1100 int i; |
|
7d2a16e6a1d3
rearanged math module. added forward declarations at the beginning.
lorenz
parents:
1844
diff
changeset
|
1101 int bufferIdx; |
|
7d2a16e6a1d3
rearanged math module. added forward declarations at the beginning.
lorenz
parents:
1844
diff
changeset
|
1102 char buffer[2][STRING_BUF_SIZE]; |
|
7d2a16e6a1d3
rearanged math module. added forward declarations at the beginning.
lorenz
parents:
1844
diff
changeset
|
1103 |
|
7d2a16e6a1d3
rearanged math module. added forward declarations at the beginning.
lorenz
parents:
1844
diff
changeset
|
1104 bufferIdx = 1; |
|
7d2a16e6a1d3
rearanged math module. added forward declarations at the beginning.
lorenz
parents:
1844
diff
changeset
|
1105 PyOS_snprintf(buffer[0], STRING_BUF_SIZE, "[", self->dim); |
|
7d2a16e6a1d3
rearanged math module. added forward declarations at the beginning.
lorenz
parents:
1844
diff
changeset
|
1106 for (i = 0; i < self->dim - 1; ++i) { |
|
7d2a16e6a1d3
rearanged math module. added forward declarations at the beginning.
lorenz
parents:
1844
diff
changeset
|
1107 PyOS_snprintf(buffer[bufferIdx % 2], STRING_BUF_SIZE, "%s%g, ", |
|
7d2a16e6a1d3
rearanged math module. added forward declarations at the beginning.
lorenz
parents:
1844
diff
changeset
|
1108 buffer[(bufferIdx + 1) % 2], self->coords[i]); |
|
7d2a16e6a1d3
rearanged math module. added forward declarations at the beginning.
lorenz
parents:
1844
diff
changeset
|
1109 bufferIdx++; |
|
7d2a16e6a1d3
rearanged math module. added forward declarations at the beginning.
lorenz
parents:
1844
diff
changeset
|
1110 } |
|
7d2a16e6a1d3
rearanged math module. added forward declarations at the beginning.
lorenz
parents:
1844
diff
changeset
|
1111 PyOS_snprintf(buffer[bufferIdx % 2], STRING_BUF_SIZE, "%s%g]", |
|
7d2a16e6a1d3
rearanged math module. added forward declarations at the beginning.
lorenz
parents:
1844
diff
changeset
|
1112 buffer[(bufferIdx + 1) % 2], self->coords[i]); |
|
7d2a16e6a1d3
rearanged math module. added forward declarations at the beginning.
lorenz
parents:
1844
diff
changeset
|
1113 return PyString_FromString(buffer[bufferIdx % 2]); |
|
7d2a16e6a1d3
rearanged math module. added forward declarations at the beginning.
lorenz
parents:
1844
diff
changeset
|
1114 } |
|
7d2a16e6a1d3
rearanged math module. added forward declarations at the beginning.
lorenz
parents:
1844
diff
changeset
|
1115 |
|
7d2a16e6a1d3
rearanged math module. added forward declarations at the beginning.
lorenz
parents:
1844
diff
changeset
|
1116 static PyObject* |
|
7d2a16e6a1d3
rearanged math module. added forward declarations at the beginning.
lorenz
parents:
1844
diff
changeset
|
1117 vector_getAttr_swizzle(PyVector *self, PyObject *attr_name) |
|
7d2a16e6a1d3
rearanged math module. added forward declarations at the beginning.
lorenz
parents:
1844
diff
changeset
|
1118 { |
|
7d2a16e6a1d3
rearanged math module. added forward declarations at the beginning.
lorenz
parents:
1844
diff
changeset
|
1119 PyObject *res = PyObject_GenericGetAttr((PyObject*)self, attr_name); |
|
7d2a16e6a1d3
rearanged math module. added forward declarations at the beginning.
lorenz
parents:
1844
diff
changeset
|
1120 /* if normal lookup failed try to swizzle */ |
| 1848 | 1121 if (swizzling_enabled && |
| 1122 PyErr_Occurred() && PyErr_ExceptionMatches(PyExc_AttributeError)) { | |
|
1845
7d2a16e6a1d3
rearanged math module. added forward declarations at the beginning.
lorenz
parents:
1844
diff
changeset
|
1123 Py_ssize_t len = PySequence_Length(attr_name); |
|
7d2a16e6a1d3
rearanged math module. added forward declarations at the beginning.
lorenz
parents:
1844
diff
changeset
|
1124 const char *attr = PyString_AsString(attr_name); |
|
7d2a16e6a1d3
rearanged math module. added forward declarations at the beginning.
lorenz
parents:
1844
diff
changeset
|
1125 double *coords = self->coords; |
|
7d2a16e6a1d3
rearanged math module. added forward declarations at the beginning.
lorenz
parents:
1844
diff
changeset
|
1126 Py_ssize_t i; |
|
7d2a16e6a1d3
rearanged math module. added forward declarations at the beginning.
lorenz
parents:
1844
diff
changeset
|
1127 res = (PyObject*)PyTuple_New(len); |
|
7d2a16e6a1d3
rearanged math module. added forward declarations at the beginning.
lorenz
parents:
1844
diff
changeset
|
1128 for (i = 0; i < len; i++) { |
|
7d2a16e6a1d3
rearanged math module. added forward declarations at the beginning.
lorenz
parents:
1844
diff
changeset
|
1129 switch (attr[i]) { |
|
7d2a16e6a1d3
rearanged math module. added forward declarations at the beginning.
lorenz
parents:
1844
diff
changeset
|
1130 case 'x': |
|
7d2a16e6a1d3
rearanged math module. added forward declarations at the beginning.
lorenz
parents:
1844
diff
changeset
|
1131 PyTuple_SetItem(res, i, PyFloat_FromDouble(coords[0])); |
|
7d2a16e6a1d3
rearanged math module. added forward declarations at the beginning.
lorenz
parents:
1844
diff
changeset
|
1132 break; |
|
7d2a16e6a1d3
rearanged math module. added forward declarations at the beginning.
lorenz
parents:
1844
diff
changeset
|
1133 case 'y': |
|
7d2a16e6a1d3
rearanged math module. added forward declarations at the beginning.
lorenz
parents:
1844
diff
changeset
|
1134 PyTuple_SetItem(res, i, PyFloat_FromDouble(coords[1])); |
|
7d2a16e6a1d3
rearanged math module. added forward declarations at the beginning.
lorenz
parents:
1844
diff
changeset
|
1135 break; |
|
7d2a16e6a1d3
rearanged math module. added forward declarations at the beginning.
lorenz
parents:
1844
diff
changeset
|
1136 default: |
|
7d2a16e6a1d3
rearanged math module. added forward declarations at the beginning.
lorenz
parents:
1844
diff
changeset
|
1137 /* swizzling failed! clean up and return NULL |
|
7d2a16e6a1d3
rearanged math module. added forward declarations at the beginning.
lorenz
parents:
1844
diff
changeset
|
1138 * the exception from PyObject_GenericGetAttr is still set */ |
|
7d2a16e6a1d3
rearanged math module. added forward declarations at the beginning.
lorenz
parents:
1844
diff
changeset
|
1139 Py_DECREF(res); |
|
7d2a16e6a1d3
rearanged math module. added forward declarations at the beginning.
lorenz
parents:
1844
diff
changeset
|
1140 return NULL; |
|
7d2a16e6a1d3
rearanged math module. added forward declarations at the beginning.
lorenz
parents:
1844
diff
changeset
|
1141 } |
|
7d2a16e6a1d3
rearanged math module. added forward declarations at the beginning.
lorenz
parents:
1844
diff
changeset
|
1142 } |
|
7d2a16e6a1d3
rearanged math module. added forward declarations at the beginning.
lorenz
parents:
1844
diff
changeset
|
1143 /* swizzling succeeded! clear the error and return result */ |
|
7d2a16e6a1d3
rearanged math module. added forward declarations at the beginning.
lorenz
parents:
1844
diff
changeset
|
1144 PyErr_Clear(); |
|
7d2a16e6a1d3
rearanged math module. added forward declarations at the beginning.
lorenz
parents:
1844
diff
changeset
|
1145 } |
|
7d2a16e6a1d3
rearanged math module. added forward declarations at the beginning.
lorenz
parents:
1844
diff
changeset
|
1146 return res; |
|
7d2a16e6a1d3
rearanged math module. added forward declarations at the beginning.
lorenz
parents:
1844
diff
changeset
|
1147 } |
|
7d2a16e6a1d3
rearanged math module. added forward declarations at the beginning.
lorenz
parents:
1844
diff
changeset
|
1148 |
|
7d2a16e6a1d3
rearanged math module. added forward declarations at the beginning.
lorenz
parents:
1844
diff
changeset
|
1149 static int |
|
7d2a16e6a1d3
rearanged math module. added forward declarations at the beginning.
lorenz
parents:
1844
diff
changeset
|
1150 vector_setAttr_swizzle(PyVector *self, PyObject *attr_name, PyObject *val) |
|
7d2a16e6a1d3
rearanged math module. added forward declarations at the beginning.
lorenz
parents:
1844
diff
changeset
|
1151 { |
|
7d2a16e6a1d3
rearanged math module. added forward declarations at the beginning.
lorenz
parents:
1844
diff
changeset
|
1152 const char *attr = PyString_AsString(attr_name); |
|
7d2a16e6a1d3
rearanged math module. added forward declarations at the beginning.
lorenz
parents:
1844
diff
changeset
|
1153 Py_ssize_t len = PySequence_Length(attr_name); |
|
7d2a16e6a1d3
rearanged math module. added forward declarations at the beginning.
lorenz
parents:
1844
diff
changeset
|
1154 double entry[self->dim]; |
|
7d2a16e6a1d3
rearanged math module. added forward declarations at the beginning.
lorenz
parents:
1844
diff
changeset
|
1155 int entry_was_set[self->dim]; |
|
7d2a16e6a1d3
rearanged math module. added forward declarations at the beginning.
lorenz
parents:
1844
diff
changeset
|
1156 int swizzle_err = SWIZZLE_ERR_NO_ERR; |
|
7d2a16e6a1d3
rearanged math module. added forward declarations at the beginning.
lorenz
parents:
1844
diff
changeset
|
1157 int i; |
|
7d2a16e6a1d3
rearanged math module. added forward declarations at the beginning.
lorenz
parents:
1844
diff
changeset
|
1158 |
|
7d2a16e6a1d3
rearanged math module. added forward declarations at the beginning.
lorenz
parents:
1844
diff
changeset
|
1159 /* if swizzling is disabled always default to generic implementation */ |
|
7d2a16e6a1d3
rearanged math module. added forward declarations at the beginning.
lorenz
parents:
1844
diff
changeset
|
1160 if (!swizzling_enabled) |
|
7d2a16e6a1d3
rearanged math module. added forward declarations at the beginning.
lorenz
parents:
1844
diff
changeset
|
1161 return PyObject_GenericSetAttr((PyObject*)self, attr_name, val); |
|
7d2a16e6a1d3
rearanged math module. added forward declarations at the beginning.
lorenz
parents:
1844
diff
changeset
|
1162 |
|
7d2a16e6a1d3
rearanged math module. added forward declarations at the beginning.
lorenz
parents:
1844
diff
changeset
|
1163 /* if swizzling is enabled first try swizzle */ |
|
7d2a16e6a1d3
rearanged math module. added forward declarations at the beginning.
lorenz
parents:
1844
diff
changeset
|
1164 for (i = 0; i < self->dim; ++i) |
|
7d2a16e6a1d3
rearanged math module. added forward declarations at the beginning.
lorenz
parents:
1844
diff
changeset
|
1165 entry_was_set[i] = 0; |
|
7d2a16e6a1d3
rearanged math module. added forward declarations at the beginning.
lorenz
parents:
1844
diff
changeset
|
1166 |
|
7d2a16e6a1d3
rearanged math module. added forward declarations at the beginning.
lorenz
parents:
1844
diff
changeset
|
1167 for (i = 0; i < len; ++i) { |
|
7d2a16e6a1d3
rearanged math module. added forward declarations at the beginning.
lorenz
parents:
1844
diff
changeset
|
1168 int idx; |
|
7d2a16e6a1d3
rearanged math module. added forward declarations at the beginning.
lorenz
parents:
1844
diff
changeset
|
1169 switch (attr[i]) { |
|
7d2a16e6a1d3
rearanged math module. added forward declarations at the beginning.
lorenz
parents:
1844
diff
changeset
|
1170 case 'x': |
|
7d2a16e6a1d3
rearanged math module. added forward declarations at the beginning.
lorenz
parents:
1844
diff
changeset
|
1171 case 'y': |
|
7d2a16e6a1d3
rearanged math module. added forward declarations at the beginning.
lorenz
parents:
1844
diff
changeset
|
1172 case 'z': |
|
7d2a16e6a1d3
rearanged math module. added forward declarations at the beginning.
lorenz
parents:
1844
diff
changeset
|
1173 idx = attr[i] - 'x'; |
|
7d2a16e6a1d3
rearanged math module. added forward declarations at the beginning.
lorenz
parents:
1844
diff
changeset
|
1174 break; |
|
7d2a16e6a1d3
rearanged math module. added forward declarations at the beginning.
lorenz
parents:
1844
diff
changeset
|
1175 case 'w': |
|
7d2a16e6a1d3
rearanged math module. added forward declarations at the beginning.
lorenz
parents:
1844
diff
changeset
|
1176 idx = 3; |
|
7d2a16e6a1d3
rearanged math module. added forward declarations at the beginning.
lorenz
parents:
1844
diff
changeset
|
1177 break; |
|
7d2a16e6a1d3
rearanged math module. added forward declarations at the beginning.
lorenz
parents:
1844
diff
changeset
|
1178 default: |
|
7d2a16e6a1d3
rearanged math module. added forward declarations at the beginning.
lorenz
parents:
1844
diff
changeset
|
1179 /* swizzle failed. attempt generic attribute setting */ |
|
7d2a16e6a1d3
rearanged math module. added forward declarations at the beginning.
lorenz
parents:
1844
diff
changeset
|
1180 return PyObject_GenericSetAttr((PyObject*)self, attr_name, val); |
|
7d2a16e6a1d3
rearanged math module. added forward declarations at the beginning.
lorenz
parents:
1844
diff
changeset
|
1181 } |
|
7d2a16e6a1d3
rearanged math module. added forward declarations at the beginning.
lorenz
parents:
1844
diff
changeset
|
1182 if (idx >= self->dim) |
|
7d2a16e6a1d3
rearanged math module. added forward declarations at the beginning.
lorenz
parents:
1844
diff
changeset
|
1183 /* swizzle failed. attempt generic attribute setting */ |
|
7d2a16e6a1d3
rearanged math module. added forward declarations at the beginning.
lorenz
parents:
1844
diff
changeset
|
1184 return PyObject_GenericSetAttr((PyObject*)self, attr_name, val); |
|
7d2a16e6a1d3
rearanged math module. added forward declarations at the beginning.
lorenz
parents:
1844
diff
changeset
|
1185 if (entry_was_set[idx]) |
|
7d2a16e6a1d3
rearanged math module. added forward declarations at the beginning.
lorenz
parents:
1844
diff
changeset
|
1186 swizzle_err = SWIZZLE_ERR_DOUBLE_IDX; |
|
7d2a16e6a1d3
rearanged math module. added forward declarations at the beginning.
lorenz
parents:
1844
diff
changeset
|
1187 if (swizzle_err == SWIZZLE_ERR_NO_ERR) { |
|
7d2a16e6a1d3
rearanged math module. added forward declarations at the beginning.
lorenz
parents:
1844
diff
changeset
|
1188 entry_was_set[idx] = 1; |
|
7d2a16e6a1d3
rearanged math module. added forward declarations at the beginning.
lorenz
parents:
1844
diff
changeset
|
1189 entry[idx] = PySequence_GetItem_AsDouble(val, i); |
|
7d2a16e6a1d3
rearanged math module. added forward declarations at the beginning.
lorenz
parents:
1844
diff
changeset
|
1190 if (PyErr_Occurred()) |
|
7d2a16e6a1d3
rearanged math module. added forward declarations at the beginning.
lorenz
parents:
1844
diff
changeset
|
1191 swizzle_err = SWIZZLE_ERR_EXTRACTION_ERR; |
|
7d2a16e6a1d3
rearanged math module. added forward declarations at the beginning.
lorenz
parents:
1844
diff
changeset
|
1192 } |
|
7d2a16e6a1d3
rearanged math module. added forward declarations at the beginning.
lorenz
parents:
1844
diff
changeset
|
1193 } |
|
7d2a16e6a1d3
rearanged math module. added forward declarations at the beginning.
lorenz
parents:
1844
diff
changeset
|
1194 switch (swizzle_err) { |
|
7d2a16e6a1d3
rearanged math module. added forward declarations at the beginning.
lorenz
parents:
1844
diff
changeset
|
1195 case SWIZZLE_ERR_NO_ERR: |
|
7d2a16e6a1d3
rearanged math module. added forward declarations at the beginning.
lorenz
parents:
1844
diff
changeset
|
1196 /* swizzle successful */ |
|
7d2a16e6a1d3
rearanged math module. added forward declarations at the beginning.
lorenz
parents:
1844
diff
changeset
|
1197 for (i = 0; i < self->dim; ++i) |
|
7d2a16e6a1d3
rearanged math module. added forward declarations at the beginning.
lorenz
parents:
1844
diff
changeset
|
1198 if (entry_was_set[i]) |
|
7d2a16e6a1d3
rearanged math module. added forward declarations at the beginning.
lorenz
parents:
1844
diff
changeset
|
1199 self->coords[i] = entry[i]; |
|
7d2a16e6a1d3
rearanged math module. added forward declarations at the beginning.
lorenz
parents:
1844
diff
changeset
|
1200 return 0; |
|
7d2a16e6a1d3
rearanged math module. added forward declarations at the beginning.
lorenz
parents:
1844
diff
changeset
|
1201 case SWIZZLE_ERR_DOUBLE_IDX: |
|
7d2a16e6a1d3
rearanged math module. added forward declarations at the beginning.
lorenz
parents:
1844
diff
changeset
|
1202 PyErr_SetString(PyExc_AttributeError, |
|
7d2a16e6a1d3
rearanged math module. added forward declarations at the beginning.
lorenz
parents:
1844
diff
changeset
|
1203 "Attribute assignment conflicts with swizzling."); |
|
7d2a16e6a1d3
rearanged math module. added forward declarations at the beginning.
lorenz
parents:
1844
diff
changeset
|
1204 return -1; |
|
7d2a16e6a1d3
rearanged math module. added forward declarations at the beginning.
lorenz
parents:
1844
diff
changeset
|
1205 case SWIZZLE_ERR_EXTRACTION_ERR: |
|
7d2a16e6a1d3
rearanged math module. added forward declarations at the beginning.
lorenz
parents:
1844
diff
changeset
|
1206 /* exception was set by PySequence_GetItem_AsDouble */ |
|
7d2a16e6a1d3
rearanged math module. added forward declarations at the beginning.
lorenz
parents:
1844
diff
changeset
|
1207 return -1; |
|
7d2a16e6a1d3
rearanged math module. added forward declarations at the beginning.
lorenz
parents:
1844
diff
changeset
|
1208 default: |
| 1848 | 1209 /* this should NEVER happen and means a bug in the code */ |
|
1845
7d2a16e6a1d3
rearanged math module. added forward declarations at the beginning.
lorenz
parents:
1844
diff
changeset
|
1210 PyErr_SetString(PyExc_RuntimeError, "Unhandled error in swizzle code"); |
|
7d2a16e6a1d3
rearanged math module. added forward declarations at the beginning.
lorenz
parents:
1844
diff
changeset
|
1211 return -1; |
|
7d2a16e6a1d3
rearanged math module. added forward declarations at the beginning.
lorenz
parents:
1844
diff
changeset
|
1212 } |
|
7d2a16e6a1d3
rearanged math module. added forward declarations at the beginning.
lorenz
parents:
1844
diff
changeset
|
1213 } |
|
7d2a16e6a1d3
rearanged math module. added forward declarations at the beginning.
lorenz
parents:
1844
diff
changeset
|
1214 |
| 1846 | 1215 /* |
| 1216 static Py_ssize_t | |
| 1217 vector_readbuffer(PyVector *self, Py_ssize_t segment, void **ptrptr) | |
| 1218 { | |
| 1219 if (segment != 0) { | |
| 1220 PyErr_SetString(PyExc_SystemError, | |
| 1221 "accessing non-existent vector segment"); | |
| 1222 return -1; | |
| 1223 } | |
| 1224 *ptrptr = self->coords; | |
| 1225 return self->dim; | |
| 1226 } | |
| 1227 | |
| 1228 static Py_ssize_t | |
| 1229 vector_writebuffer(PyVector *self, Py_ssize_t segment, void **ptrptr) | |
| 1230 { | |
| 1231 if (segment != 0) { | |
| 1232 PyErr_SetString(PyExc_SystemError, | |
| 1233 "accessing non-existent vector segment"); | |
| 1234 return -1; | |
| 1235 } | |
| 1236 *ptrptr = self->coords; | |
| 1237 return self->dim; | |
| 1238 } | |
| 1239 | |
| 1240 static Py_ssize_t | |
| 1241 vector_segcount(PyVector *self, Py_ssize_t *lenp) | |
| 1242 { | |
| 1243 if (lenp) { | |
| 1244 *lenp = self->dim * sizeof(self->coords[0]); | |
| 1245 } | |
| 1246 return 1; | |
| 1247 } | |
| 1248 | |
| 1249 static int | |
| 1250 vector_getbuffer(PyVector *self, Py_buffer *view, int flags) | |
| 1251 { | |
| 1252 int ret; | |
| 1253 void *ptr; | |
| 1254 if (view == NULL) { | |
| 1255 self->ob_exports++; | |
| 1256 return 0; | |
| 1257 } | |
| 1258 ptr = self->coords; | |
| 1259 ret = PyBuffer_FillInfo(view, (PyObject*)self, ptr, Py_SIZE(self), 0, flags); | |
| 1260 if (ret >= 0) { | |
| 1261 obj->ob_exports++; | |
| 1262 } | |
| 1263 return ret; | |
| 1264 } | |
| 1265 | |
| 1266 static void | |
| 1267 vector_releasebuffer(PyVector *self, Py_buffer *view) | |
| 1268 { | |
| 1269 self->ob_exports--; | |
| 1270 } | |
| 1271 | |
| 1272 | |
| 1273 static PyBufferProcs vector_as_buffer = { | |
| 1274 (readbufferproc)vector_readbuffer, | |
| 1275 (writebufferproc)vector_writebuffer, | |
| 1276 (segcountproc)vector_segcount, | |
| 1277 (charbufferproc)0, | |
| 1278 (getbufferproc)vector_getbuffer, | |
| 1279 (releasebufferproc)vector_releasebuffer, | |
| 1280 }; | |
| 1281 */ | |
|
1845
7d2a16e6a1d3
rearanged math module. added forward declarations at the beginning.
lorenz
parents:
1844
diff
changeset
|
1282 |
|
7d2a16e6a1d3
rearanged math module. added forward declarations at the beginning.
lorenz
parents:
1844
diff
changeset
|
1283 /********************************************************************* |
|
7d2a16e6a1d3
rearanged math module. added forward declarations at the beginning.
lorenz
parents:
1844
diff
changeset
|
1284 * vector2 specific functions |
|
7d2a16e6a1d3
rearanged math module. added forward declarations at the beginning.
lorenz
parents:
1844
diff
changeset
|
1285 *********************************************************************/ |
|
7d2a16e6a1d3
rearanged math module. added forward declarations at the beginning.
lorenz
parents:
1844
diff
changeset
|
1286 |
|
7d2a16e6a1d3
rearanged math module. added forward declarations at the beginning.
lorenz
parents:
1844
diff
changeset
|
1287 |
| 1834 | 1288 static PyObject * |
| 1289 vector2_new(PyTypeObject *type, PyObject *args, PyObject *kwds) | |
| 1290 { | |
| 1291 PyVector *vec = (PyVector *)type->tp_alloc(type, 0); | |
| 1292 | |
| 1293 if (vec != NULL) { | |
| 1294 vec->dim = 2; | |
| 1295 vec->epsilon = FLT_EPSILON; | |
| 1296 vec->coords = PyMem_New(double, vec->dim); | |
| 1297 if (vec->coords == NULL) { | |
| 1298 vec->ob_type->tp_free((PyObject*)vec); | |
| 1299 return NULL; | |
| 1300 } | |
| 1301 } | |
| 1302 | |
| 1303 return (PyObject *)vec; | |
| 1304 } | |
| 1305 | |
| 1306 static int | |
| 1307 vector2_init(PyVector *self, PyObject *args, PyObject *kwds) | |
| 1308 { | |
| 1309 PyObject *xOrSequence=NULL, *y=NULL, *z=NULL; | |
| 1310 static char *kwlist[] = {"x", "y", NULL}; | |
| 1311 | |
| 1836 | 1312 if (! PyArg_ParseTupleAndKeywords(args, kwds, "|OO:Vector2", kwlist, |
| 1834 | 1313 &xOrSequence, &y)) |
| 1314 return -1; | |
| 1315 | |
| 1316 if (xOrSequence) { | |
|
1838
e4fd799c194b
added methods distance_to and distance_squared_to to the vector class
lorenz
parents:
1837
diff
changeset
|
1317 if (RealNumber_Check(xOrSequence)) { |
| 1834 | 1318 self->coords[0] = PyFloat_AsDouble(xOrSequence); |
| 1319 } | |
|
1845
7d2a16e6a1d3
rearanged math module. added forward declarations at the beginning.
lorenz
parents:
1844
diff
changeset
|
1320 else if (PyVectorCompatible_Check(xOrSequence, self->dim)) { |
| 1834 | 1321 self->coords[0] = PySequence_GetItem_AsDouble(xOrSequence, 0); |
| 1322 self->coords[1] = PySequence_GetItem_AsDouble(xOrSequence, 1); | |
| 1836 | 1323 /* successful initialization from sequence type */ |
| 1834 | 1324 return 0; |
| 1325 } | |
| 1836 | 1326 else if (PyString_Check(xOrSequence)) { |
| 1327 /* This should make "Vector2(Vector2().__repr__())" possible */ | |
| 1328 char buffer[STRING_BUF_SIZE]; | |
| 1329 char *endptr; | |
| 1330 char *str = PyString_AsString(xOrSequence); | |
| 1331 if (strncmp(str, "<Vector2(", strlen("<Vector2(")) != 0) | |
| 1332 goto error; | |
| 1333 str += strlen("<Vector2("); | |
| 1334 self->coords[0] = PyOS_ascii_strtod(str, &endptr); | |
| 1335 if (endptr == str) { | |
| 1336 goto error; | |
| 1337 } | |
| 1338 str = endptr + strlen(", "); | |
| 1339 self->coords[1] = PyOS_ascii_strtod(str, &endptr); | |
| 1340 if (endptr == str) | |
| 1341 goto error; | |
| 1342 /* successful conversion from string */ | |
| 1343 return 0; | |
| 1344 } | |
| 1834 | 1345 else { |
| 1836 | 1346 goto error; |
| 1834 | 1347 } |
| 1348 } | |
| 1349 else { | |
| 1350 self->coords[0] = 0.; | |
| 1351 } | |
| 1352 | |
| 1353 if (y) { | |
|
1838
e4fd799c194b
added methods distance_to and distance_squared_to to the vector class
lorenz
parents:
1837
diff
changeset
|
1354 if (RealNumber_Check(y)) { |
| 1834 | 1355 self->coords[1] = PyFloat_AsDouble(y); |
| 1356 } | |
| 1357 else { | |
| 1836 | 1358 goto error; |
| 1834 | 1359 } |
| 1360 } | |
| 1361 else { | |
| 1362 self->coords[1] = 0.; | |
| 1363 } | |
| 1836 | 1364 /* success initialization */ |
| 1365 return 0; | |
| 1366 error: | |
| 1367 PyErr_SetString(PyExc_ValueError, | |
|
1838
e4fd799c194b
added methods distance_to and distance_squared_to to the vector class
lorenz
parents:
1837
diff
changeset
|
1368 "Vector2 must be initialized with 2 real numbers or a sequence of 2 real numbers"); |
| 1836 | 1369 return -1; |
| 1370 } | |
| 1834 | 1371 |
| 1836 | 1372 static void |
|
1844
a15f6323c39d
* implemented Vector3. there are no tests yet, so there are bound to be bugs
lorenz
parents:
1842
diff
changeset
|
1373 _vector2_do_rotate(double *dst_coords, const double *src_coords, |
|
a15f6323c39d
* implemented Vector3. there are no tests yet, so there are bound to be bugs
lorenz
parents:
1842
diff
changeset
|
1374 double angle, double epsilon) |
| 1836 | 1375 { |
| 1376 /* make sure angle is in range [0, 360) */ | |
| 1377 angle = fmod(angle, 360.); | |
| 1378 if (angle < 0) | |
| 1379 angle += 360.; | |
| 1380 | |
| 1381 /* special-case rotation by 0, 90, 180 and 270 degrees */ | |
| 1848 | 1382 if (fmod(angle + epsilon, 90.) < 2 * epsilon) { |
| 1383 switch ((int)((angle + epsilon) / 90)) { | |
| 1384 case 0: /* 0 degrees */ | |
| 1385 dst_coords[0] = src_coords[0]; | |
| 1386 dst_coords[1] = src_coords[1]; | |
| 1387 break; | |
| 1388 case 1: /* 90 degrees */ | |
| 1389 dst_coords[0] = -src_coords[1]; | |
| 1390 dst_coords[1] = src_coords[0]; | |
| 1391 break; | |
| 1392 case 2: /* 180 degrees */ | |
| 1393 dst_coords[0] = -src_coords[0]; | |
| 1394 dst_coords[1] = -src_coords[1]; | |
| 1395 break; | |
| 1396 case 3: /* 270 degrees */ | |
| 1397 dst_coords[0] = src_coords[1]; | |
| 1398 dst_coords[1] = -src_coords[0]; | |
| 1399 break; | |
| 1400 default: | |
| 1401 /* this should NEVER happen and means a bug in the code */ | |
| 1402 PyErr_SetString(PyExc_RuntimeError, "Please report this bug in vector2_do_rotate to the developers"); | |
| 1403 break; | |
| 1404 } | |
| 1836 | 1405 } |
| 1406 else { | |
| 1407 double sinValue, cosValue; | |
| 1408 | |
| 1409 angle = DEG2RAD(angle); | |
| 1410 sinValue = sin(angle); | |
| 1411 cosValue = cos(angle); | |
| 1412 | |
| 1413 dst_coords[0] = cosValue * src_coords[0] - sinValue * src_coords[1]; | |
| 1414 dst_coords[1] = sinValue * src_coords[0] + cosValue * src_coords[1]; | |
| 1415 } | |
| 1416 } | |
| 1417 | |
| 1418 static PyObject * | |
| 1419 vector2_rotate(PyVector *self, PyObject *args) | |
| 1420 { | |
| 1421 double angle; | |
| 1422 PyVector *ret; | |
| 1423 | |
| 1424 if (!PyArg_ParseTuple(args, "d:rotate", &angle)) { | |
| 1425 return NULL; | |
| 1426 } | |
| 1427 | |
| 1428 ret = (PyVector*)PyVector_NEW(self->dim); | |
|
1844
a15f6323c39d
* implemented Vector3. there are no tests yet, so there are bound to be bugs
lorenz
parents:
1842
diff
changeset
|
1429 _vector2_do_rotate(ret->coords, self->coords, angle, self->epsilon); |
| 1836 | 1430 return (PyObject*)ret; |
| 1431 } | |
| 1432 | |
| 1433 static PyObject * | |
| 1434 vector2_rotate_ip(PyVector *self, PyObject *args) | |
| 1435 { | |
| 1436 double angle; | |
| 1437 double tmp[2]; | |
| 1438 | |
| 1439 if (!PyArg_ParseTuple(args, "d:rotate_ip", &angle)) { | |
| 1440 return NULL; | |
| 1441 } | |
| 1442 | |
| 1443 tmp[0] = self->coords[0]; | |
| 1444 tmp[1] = self->coords[1]; | |
|
1844
a15f6323c39d
* implemented Vector3. there are no tests yet, so there are bound to be bugs
lorenz
parents:
1842
diff
changeset
|
1445 _vector2_do_rotate(self->coords, tmp, angle, self->epsilon); |
| 1836 | 1446 Py_RETURN_NONE; |
| 1447 } | |
| 1448 | |
| 1449 static PyObject * | |
| 1450 vector2_cross(PyVector *self, PyObject *other) | |
| 1451 { | |
|
1845
7d2a16e6a1d3
rearanged math module. added forward declarations at the beginning.
lorenz
parents:
1844
diff
changeset
|
1452 if (!PyVectorCompatible_Check(other, self->dim)) { |
| 1836 | 1453 PyErr_SetString(PyExc_TypeError, "cannot calculate cross Product"); |
| 1454 return NULL; | |
| 1455 } | |
| 1456 | |
| 1457 if (PyVector_Check(other)) { | |
| 1458 return PyFloat_FromDouble((self->coords[0] * ((PyVector *)other)->coords[1]) - | |
| 1459 (self->coords[1] * ((PyVector *)other)->coords[0])); | |
| 1460 } | |
| 1461 else { | |
| 1462 return PyFloat_FromDouble((self->coords[0] * PySequence_GetItem_AsDouble(other, 1)) - | |
| 1463 (self->coords[1] * PySequence_GetItem_AsDouble(other, 0))); | |
| 1464 } | |
| 1465 } | |
| 1466 | |
| 1467 static PyObject * | |
|
1844
a15f6323c39d
* implemented Vector3. there are no tests yet, so there are bound to be bugs
lorenz
parents:
1842
diff
changeset
|
1468 vector2_angle_to(PyVector *self, PyObject *other) |
| 1836 | 1469 { |
| 1470 double angle; | |
|
1845
7d2a16e6a1d3
rearanged math module. added forward declarations at the beginning.
lorenz
parents:
1844
diff
changeset
|
1471 if (!PyVectorCompatible_Check(other, self->dim)) { |
| 1836 | 1472 PyErr_SetString(PyExc_TypeError, "expected an vector."); |
| 1473 return NULL; | |
| 1474 } | |
| 1475 | |
| 1476 angle = (atan2(PySequence_GetItem_AsDouble(other, 1), | |
| 1477 PySequence_GetItem_AsDouble(other, 0)) - | |
| 1478 atan2(self->coords[1], self->coords[0])); | |
| 1479 return PyFloat_FromDouble(RAD2DEG(angle)); | |
| 1480 } | |
| 1481 | |
| 1846 | 1482 static PyObject * |
| 1483 vector2_as_polar(PyVector *self) | |
| 1484 { | |
| 1485 double r, phi; | |
| 1486 r = sqrt(_scalar_product(self->coords, self->coords, self->dim)); | |
| 1487 phi = atan2(self->coords[1], self->coords[0]); | |
| 1488 return Py_BuildValue("(dd)", r, phi); | |
| 1489 } | |
| 1490 | |
| 1491 static PyObject * | |
| 1492 vector2_from_polar(PyVector *self, PyObject *args) | |
| 1493 { | |
| 1494 PyObject *polar_obj, *r_obj, *phi_obj; | |
| 1495 double r, phi; | |
| 1496 if (!PyArg_ParseTuple(args, "O:Vector2.from_polar", &polar_obj)) { | |
| 1497 return NULL; | |
| 1498 } | |
| 1499 if (!PySequence_Check(polar_obj) || PySequence_Length(polar_obj) != 2) { | |
| 1500 PyErr_SetString(PyExc_TypeError, | |
| 1501 "2-tuple containing r and phi is expected."); | |
| 1502 return NULL; | |
| 1503 } | |
| 1504 r_obj = PySequence_GetItem(polar_obj, 0); | |
| 1505 phi_obj = PySequence_GetItem(polar_obj, 1); | |
| 1506 if (!PyNumber_Check(r_obj) || !PyNumber_Check(phi_obj)) { | |
| 1507 PyErr_SetString(PyExc_TypeError, | |
| 1508 "expected numerical values"); | |
| 1509 Py_DECREF(r_obj); | |
| 1510 Py_DECREF(phi_obj); | |
| 1511 return NULL; | |
| 1512 } | |
| 1513 r = PyFloat_AsDouble(r_obj); | |
| 1514 phi = PyFloat_AsDouble(phi_obj); | |
| 1515 Py_DECREF(r_obj); | |
| 1516 Py_DECREF(phi_obj); | |
| 1517 self->coords[0] = r * cos(phi); | |
| 1518 self->coords[1] = r * sin(phi); | |
| 1519 | |
| 1520 Py_RETURN_NONE; | |
| 1521 } | |
| 1522 | |
| 1523 | |
| 1524 | |
| 1834 | 1525 static PyMethodDef vector2_methods[] = { |
| 1837 | 1526 {"length", (PyCFunction)vector_length, METH_NOARGS, |
| 1527 "returns the length/magnitude of the vector." | |
| 1528 }, | |
| 1529 {"length_squared", (PyCFunction)vector_length_squared, METH_NOARGS, | |
| 1530 "returns the length/magnitude of the vector." | |
| 1531 }, | |
| 1836 | 1532 {"rotate", (PyCFunction)vector2_rotate, METH_VARARGS, |
| 1533 "returns a new vector rotated counterclockwise by the angle given in degrees." | |
| 1834 | 1534 }, |
| 1836 | 1535 {"rotate_ip", (PyCFunction)vector2_rotate_ip, METH_VARARGS, |
| 1536 "rotates the vector counterclockwise by the angle given in degrees." | |
| 1834 | 1537 }, |
| 1846 | 1538 {"slerp", (PyCFunction)vector_slerp, METH_VARARGS, |
|
1847
ed12994f377c
added "lerp()" to Vector2 and Vector3 to provide a linear interpolation.
lorenz
parents:
1846
diff
changeset
|
1539 "Interpolates spherically to a given vector in a given number of steps." |
|
ed12994f377c
added "lerp()" to Vector2 and Vector3 to provide a linear interpolation.
lorenz
parents:
1846
diff
changeset
|
1540 }, |
|
ed12994f377c
added "lerp()" to Vector2 and Vector3 to provide a linear interpolation.
lorenz
parents:
1846
diff
changeset
|
1541 {"lerp", (PyCFunction)vector_lerp, METH_VARARGS, |
|
ed12994f377c
added "lerp()" to Vector2 and Vector3 to provide a linear interpolation.
lorenz
parents:
1846
diff
changeset
|
1542 "Interpolates linear to a given vector in a given number of steps." |
| 1846 | 1543 }, |
| 1836 | 1544 {"normalize", (PyCFunction)vector_normalize, METH_NOARGS, |
| 1834 | 1545 "returns a vector that has length == 1 and the same direction as self." |
| 1546 }, | |
| 1836 | 1547 {"normalize_ip", (PyCFunction)vector_normalize_ip, METH_NOARGS, |
| 1834 | 1548 "Normalizes the vector so that it has length == 1." |
| 1549 }, | |
| 1836 | 1550 {"is_normalized", (PyCFunction)vector_is_normalized, METH_NOARGS, |
| 1834 | 1551 "returns True if the vector has length == 1. otherwise it returns False." |
| 1552 }, | |
| 1836 | 1553 {"cross", (PyCFunction)vector2_cross, METH_O, |
| 1834 | 1554 "calculates the cross product." |
| 1555 }, | |
| 1836 | 1556 {"dot", (PyCFunction)vector_dot, METH_O, |
| 1834 | 1557 "calculates the dot product." |
| 1558 }, | |
|
1844
a15f6323c39d
* implemented Vector3. there are no tests yet, so there are bound to be bugs
lorenz
parents:
1842
diff
changeset
|
1559 {"angle_to", (PyCFunction)vector2_angle_to, METH_O, |
| 1834 | 1560 "returns the angle between self and the given vector." |
| 1561 }, | |
| 1836 | 1562 {"scale_to_length", (PyCFunction)vector_scale_to_length, METH_O, |
| 1837 | 1563 "scales the vector to the given length." |
| 1564 }, | |
| 1565 {"reflect", (PyCFunction)vector_reflect, METH_O, | |
| 1566 "reflects the vector on the surface characterized by the given normal." | |
| 1567 }, | |
| 1568 {"reflect_ip", (PyCFunction)vector_reflect_ip, METH_O, | |
| 1569 "reflects the vector in-place on the surface characterized by the given normal." | |
| 1836 | 1570 }, |
|
1838
e4fd799c194b
added methods distance_to and distance_squared_to to the vector class
lorenz
parents:
1837
diff
changeset
|
1571 {"distance_to", (PyCFunction)vector_distance_to, METH_O, |
|
e4fd799c194b
added methods distance_to and distance_squared_to to the vector class
lorenz
parents:
1837
diff
changeset
|
1572 "returns the distance to the given vector." |
|
e4fd799c194b
added methods distance_to and distance_squared_to to the vector class
lorenz
parents:
1837
diff
changeset
|
1573 }, |
|
e4fd799c194b
added methods distance_to and distance_squared_to to the vector class
lorenz
parents:
1837
diff
changeset
|
1574 {"distance_squared_to", (PyCFunction)vector_distance_squared_to, METH_O, |
|
e4fd799c194b
added methods distance_to and distance_squared_to to the vector class
lorenz
parents:
1837
diff
changeset
|
1575 "returns the squared distance to the given vector." |
|
e4fd799c194b
added methods distance_to and distance_squared_to to the vector class
lorenz
parents:
1837
diff
changeset
|
1576 }, |
|
1841
a7dc31e50c46
* added the vetor_elementwiseproxy which lets you apply calculations on
lorenz
parents:
1840
diff
changeset
|
1577 {"elementwise", (PyCFunction)vector_elementwise, METH_NOARGS, |
|
a7dc31e50c46
* added the vetor_elementwiseproxy which lets you apply calculations on
lorenz
parents:
1840
diff
changeset
|
1578 "applies the following operation to each element of the vector." |
|
a7dc31e50c46
* added the vetor_elementwiseproxy which lets you apply calculations on
lorenz
parents:
1840
diff
changeset
|
1579 }, |
| 1846 | 1580 {"as_polar", (PyCFunction)vector2_as_polar, METH_NOARGS, |
| 1581 "returns a 2-tuple (r, phi) where r is the radial distance and phi is the angle to the positive x-axis." | |
| 1582 }, | |
| 1583 {"from_polar", (PyCFunction)vector2_from_polar, METH_VARARGS, | |
| 1584 "sets x and y from a 2-tuple (r, phi) where r is the radial distance and phi is the angle to the positive x-axis." | |
| 1585 }, | |
| 1836 | 1586 |
| 1834 | 1587 {NULL} /* Sentinel */ |
| 1588 }; | |
| 1589 | |
| 1590 | |
| 1591 static PyGetSetDef vector2_getsets[] = { | |
| 1592 { "x", (getter)vector_getx, (setter)vector_setx, NULL, NULL }, | |
| 1593 { "y", (getter)vector_gety, (setter)vector_sety, NULL, NULL }, | |
| 1594 { NULL, 0, NULL, NULL, NULL } /* Sentinel */ | |
| 1595 }; | |
| 1596 | |
| 1597 | |
| 1598 /******************************** | |
| 1837 | 1599 * PyVector2 type definition |
| 1834 | 1600 ********************************/ |
| 1601 | |
| 1602 static PyTypeObject PyVector2_Type = { | |
| 1603 PyObject_HEAD_INIT(NULL) | |
|
1838
e4fd799c194b
added methods distance_to and distance_squared_to to the vector class
lorenz
parents:
1837
diff
changeset
|
1604 0, /* ob_size */ |
|
e4fd799c194b
added methods distance_to and distance_squared_to to the vector class
lorenz
parents:
1837
diff
changeset
|
1605 "pygame.math.Vector2", /* tp_name */ |
|
e4fd799c194b
added methods distance_to and distance_squared_to to the vector class
lorenz
parents:
1837
diff
changeset
|
1606 sizeof(PyVector), /* tp_basicsize */ |
|
e4fd799c194b
added methods distance_to and distance_squared_to to the vector class
lorenz
parents:
1837
diff
changeset
|
1607 0, /* tp_itemsize */ |
| 1834 | 1608 /* Methods to implement standard operations */ |
|
1838
e4fd799c194b
added methods distance_to and distance_squared_to to the vector class
lorenz
parents:
1837
diff
changeset
|
1609 (destructor)vector_dealloc, /* tp_dealloc */ |
|
e4fd799c194b
added methods distance_to and distance_squared_to to the vector class
lorenz
parents:
1837
diff
changeset
|
1610 0, /* tp_print */ |
|
e4fd799c194b
added methods distance_to and distance_squared_to to the vector class
lorenz
parents:
1837
diff
changeset
|
1611 0, /* tp_getattr */ |
|
e4fd799c194b
added methods distance_to and distance_squared_to to the vector class
lorenz
parents:
1837
diff
changeset
|
1612 0, /* tp_setattr */ |
|
e4fd799c194b
added methods distance_to and distance_squared_to to the vector class
lorenz
parents:
1837
diff
changeset
|
1613 0, /* tp_compare */ |
|
e4fd799c194b
added methods distance_to and distance_squared_to to the vector class
lorenz
parents:
1837
diff
changeset
|
1614 (reprfunc)vector_repr, /* tp_repr */ |
| 1834 | 1615 /* Method suites for standard classes */ |
|
1838
e4fd799c194b
added methods distance_to and distance_squared_to to the vector class
lorenz
parents:
1837
diff
changeset
|
1616 &vector_as_number, /* tp_as_number */ |
|
e4fd799c194b
added methods distance_to and distance_squared_to to the vector class
lorenz
parents:
1837
diff
changeset
|
1617 &vector_as_sequence, /* tp_as_sequence */ |
|
e4fd799c194b
added methods distance_to and distance_squared_to to the vector class
lorenz
parents:
1837
diff
changeset
|
1618 0, /* tp_as_mapping */ |
| 1834 | 1619 /* More standard operations (here for binary compatibility) */ |
|
1838
e4fd799c194b
added methods distance_to and distance_squared_to to the vector class
lorenz
parents:
1837
diff
changeset
|
1620 0, /* tp_hash */ |
|
e4fd799c194b
added methods distance_to and distance_squared_to to the vector class
lorenz
parents:
1837
diff
changeset
|
1621 0, /* tp_call */ |
|
e4fd799c194b
added methods distance_to and distance_squared_to to the vector class
lorenz
parents:
1837
diff
changeset
|
1622 (reprfunc)vector_str, /* tp_str */ |
|
1845
7d2a16e6a1d3
rearanged math module. added forward declarations at the beginning.
lorenz
parents:
1844
diff
changeset
|
1623 (getattrofunc)vector_getAttr_swizzle, /* tp_getattro */ |
|
7d2a16e6a1d3
rearanged math module. added forward declarations at the beginning.
lorenz
parents:
1844
diff
changeset
|
1624 (setattrofunc)vector_setAttr_swizzle, /* tp_setattro */ |
| 1834 | 1625 /* Functions to access object as input/output buffer */ |
|
1838
e4fd799c194b
added methods distance_to and distance_squared_to to the vector class
lorenz
parents:
1837
diff
changeset
|
1626 0, /* tp_as_buffer */ |
| 1834 | 1627 /* Flags to define presence of optional/expanded features */ |
|
1838
e4fd799c194b
added methods distance_to and distance_squared_to to the vector class
lorenz
parents:
1837
diff
changeset
|
1628 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | |
| 1840 | 1629 Py_TPFLAGS_CHECKTYPES, /* tp_flags */ |
| 1834 | 1630 /* Documentation string */ |
|
1838
e4fd799c194b
added methods distance_to and distance_squared_to to the vector class
lorenz
parents:
1837
diff
changeset
|
1631 DOC_PYGAMEVECTOR2, /* tp_doc */ |
| 1834 | 1632 |
| 1633 /* Assigned meaning in release 2.0 */ | |
| 1634 /* call function for all accessible objects */ | |
| 1635 0, /* tp_traverse */ | |
| 1636 /* delete references to contained objects */ | |
| 1637 0, /* tp_clear */ | |
| 1638 | |
| 1639 /* Assigned meaning in release 2.1 */ | |
| 1640 /* rich comparisons */ | |
| 1641 (richcmpfunc)vector_richcompare, /* tp_richcompare */ | |
| 1642 /* weak reference enabler */ | |
| 1643 0, /* tp_weaklistoffset */ | |
| 1644 | |
| 1645 /* Added in release 2.2 */ | |
| 1646 /* Iterators */ | |
|
1838
e4fd799c194b
added methods distance_to and distance_squared_to to the vector class
lorenz
parents:
1837
diff
changeset
|
1647 vector_iter, /* tp_iter */ |
| 1834 | 1648 0, /* tp_iternext */ |
| 1649 /* Attribute descriptor and subclassing stuff */ | |
|
1838
e4fd799c194b
added methods distance_to and distance_squared_to to the vector class
lorenz
parents:
1837
diff
changeset
|
1650 vector2_methods, /* tp_methods */ |
|
e4fd799c194b
added methods distance_to and distance_squared_to to the vector class
lorenz
parents:
1837
diff
changeset
|
1651 vector_members, /* tp_members */ |
|
e4fd799c194b
added methods distance_to and distance_squared_to to the vector class
lorenz
parents:
1837
diff
changeset
|
1652 vector2_getsets, /* tp_getset */ |
| 1834 | 1653 0, /* tp_base */ |
| 1654 0, /* tp_dict */ | |
| 1655 0, /* tp_descr_get */ | |
| 1656 0, /* tp_descr_set */ | |
| 1657 0, /* tp_dictoffset */ | |
|
1838
e4fd799c194b
added methods distance_to and distance_squared_to to the vector class
lorenz
parents:
1837
diff
changeset
|
1658 (initproc)vector2_init, /* tp_init */ |
| 1834 | 1659 0, /* tp_alloc */ |
|
1838
e4fd799c194b
added methods distance_to and distance_squared_to to the vector class
lorenz
parents:
1837
diff
changeset
|
1660 (newfunc)vector2_new, /* tp_new */ |
| 1834 | 1661 0, /* tp_free */ |
| 1662 0, /* tp_is_gc */ | |
| 1663 0, /* tp_bases */ | |
| 1664 0, /* tp_mro */ | |
| 1665 0, /* tp_cache */ | |
| 1666 0, /* tp_subclasses */ | |
| 1667 0, /* tp_weaklist */ | |
| 1668 }; | |
| 1669 | |
| 1670 | |
|
1844
a15f6323c39d
* implemented Vector3. there are no tests yet, so there are bound to be bugs
lorenz
parents:
1842
diff
changeset
|
1671 |
|
a15f6323c39d
* implemented Vector3. there are no tests yet, so there are bound to be bugs
lorenz
parents:
1842
diff
changeset
|
1672 |
|
a15f6323c39d
* implemented Vector3. there are no tests yet, so there are bound to be bugs
lorenz
parents:
1842
diff
changeset
|
1673 |
|
a15f6323c39d
* implemented Vector3. there are no tests yet, so there are bound to be bugs
lorenz
parents:
1842
diff
changeset
|
1674 |
|
a15f6323c39d
* implemented Vector3. there are no tests yet, so there are bound to be bugs
lorenz
parents:
1842
diff
changeset
|
1675 |
|
a15f6323c39d
* implemented Vector3. there are no tests yet, so there are bound to be bugs
lorenz
parents:
1842
diff
changeset
|
1676 |
|
a15f6323c39d
* implemented Vector3. there are no tests yet, so there are bound to be bugs
lorenz
parents:
1842
diff
changeset
|
1677 |
|
a15f6323c39d
* implemented Vector3. there are no tests yet, so there are bound to be bugs
lorenz
parents:
1842
diff
changeset
|
1678 |
|
a15f6323c39d
* implemented Vector3. there are no tests yet, so there are bound to be bugs
lorenz
parents:
1842
diff
changeset
|
1679 /************************************************************* |
|
a15f6323c39d
* implemented Vector3. there are no tests yet, so there are bound to be bugs
lorenz
parents:
1842
diff
changeset
|
1680 * PyVector3 specific functions |
|
a15f6323c39d
* implemented Vector3. there are no tests yet, so there are bound to be bugs
lorenz
parents:
1842
diff
changeset
|
1681 *************************************************************/ |
|
a15f6323c39d
* implemented Vector3. there are no tests yet, so there are bound to be bugs
lorenz
parents:
1842
diff
changeset
|
1682 |
|
a15f6323c39d
* implemented Vector3. there are no tests yet, so there are bound to be bugs
lorenz
parents:
1842
diff
changeset
|
1683 |
|
a15f6323c39d
* implemented Vector3. there are no tests yet, so there are bound to be bugs
lorenz
parents:
1842
diff
changeset
|
1684 |
|
a15f6323c39d
* implemented Vector3. there are no tests yet, so there are bound to be bugs
lorenz
parents:
1842
diff
changeset
|
1685 static PyObject * |
|
a15f6323c39d
* implemented Vector3. there are no tests yet, so there are bound to be bugs
lorenz
parents:
1842
diff
changeset
|
1686 vector3_new(PyTypeObject *type, PyObject *args, PyObject *kwds) |
|
a15f6323c39d
* implemented Vector3. there are no tests yet, so there are bound to be bugs
lorenz
parents:
1842
diff
changeset
|
1687 { |
|
a15f6323c39d
* implemented Vector3. there are no tests yet, so there are bound to be bugs
lorenz
parents:
1842
diff
changeset
|
1688 PyVector *vec = (PyVector *)type->tp_alloc(type, 0); |
|
a15f6323c39d
* implemented Vector3. there are no tests yet, so there are bound to be bugs
lorenz
parents:
1842
diff
changeset
|
1689 |
|
a15f6323c39d
* implemented Vector3. there are no tests yet, so there are bound to be bugs
lorenz
parents:
1842
diff
changeset
|
1690 if (vec != NULL) { |
|
a15f6323c39d
* implemented Vector3. there are no tests yet, so there are bound to be bugs
lorenz
parents:
1842
diff
changeset
|
1691 vec->dim = 3; |
|
a15f6323c39d
* implemented Vector3. there are no tests yet, so there are bound to be bugs
lorenz
parents:
1842
diff
changeset
|
1692 vec->epsilon = FLT_EPSILON; |
|
a15f6323c39d
* implemented Vector3. there are no tests yet, so there are bound to be bugs
lorenz
parents:
1842
diff
changeset
|
1693 vec->coords = PyMem_New(double, vec->dim); |
|
a15f6323c39d
* implemented Vector3. there are no tests yet, so there are bound to be bugs
lorenz
parents:
1842
diff
changeset
|
1694 if (vec->coords == NULL) { |
|
a15f6323c39d
* implemented Vector3. there are no tests yet, so there are bound to be bugs
lorenz
parents:
1842
diff
changeset
|
1695 vec->ob_type->tp_free((PyObject*)vec); |
|
a15f6323c39d
* implemented Vector3. there are no tests yet, so there are bound to be bugs
lorenz
parents:
1842
diff
changeset
|
1696 return NULL; |
|
a15f6323c39d
* implemented Vector3. there are no tests yet, so there are bound to be bugs
lorenz
parents:
1842
diff
changeset
|
1697 } |
|
a15f6323c39d
* implemented Vector3. there are no tests yet, so there are bound to be bugs
lorenz
parents:
1842
diff
changeset
|
1698 } |
|
a15f6323c39d
* implemented Vector3. there are no tests yet, so there are bound to be bugs
lorenz
parents:
1842
diff
changeset
|
1699 |
|
a15f6323c39d
* implemented Vector3. there are no tests yet, so there are bound to be bugs
lorenz
parents:
1842
diff
changeset
|
1700 return (PyObject *)vec; |
|
a15f6323c39d
* implemented Vector3. there are no tests yet, so there are bound to be bugs
lorenz
parents:
1842
diff
changeset
|
1701 } |
|
a15f6323c39d
* implemented Vector3. there are no tests yet, so there are bound to be bugs
lorenz
parents:
1842
diff
changeset
|
1702 |
|
a15f6323c39d
* implemented Vector3. there are no tests yet, so there are bound to be bugs
lorenz
parents:
1842
diff
changeset
|
1703 static int |
|
a15f6323c39d
* implemented Vector3. there are no tests yet, so there are bound to be bugs
lorenz
parents:
1842
diff
changeset
|
1704 vector3_init(PyVector *self, PyObject *args, PyObject *kwds) |
|
a15f6323c39d
* implemented Vector3. there are no tests yet, so there are bound to be bugs
lorenz
parents:
1842
diff
changeset
|
1705 { |
|
a15f6323c39d
* implemented Vector3. there are no tests yet, so there are bound to be bugs
lorenz
parents:
1842
diff
changeset
|
1706 PyObject *xOrSequence=NULL, *y=NULL, *z=NULL; |
|
a15f6323c39d
* implemented Vector3. there are no tests yet, so there are bound to be bugs
lorenz
parents:
1842
diff
changeset
|
1707 static char *kwlist[] = {"x", "y", "z", NULL}; |
|
a15f6323c39d
* implemented Vector3. there are no tests yet, so there are bound to be bugs
lorenz
parents:
1842
diff
changeset
|
1708 |
|
a15f6323c39d
* implemented Vector3. there are no tests yet, so there are bound to be bugs
lorenz
parents:
1842
diff
changeset
|
1709 if (! PyArg_ParseTupleAndKeywords(args, kwds, "|OOO:Vector3", kwlist, |
|
a15f6323c39d
* implemented Vector3. there are no tests yet, so there are bound to be bugs
lorenz
parents:
1842
diff
changeset
|
1710 &xOrSequence, &y, &z)) |
|
a15f6323c39d
* implemented Vector3. there are no tests yet, so there are bound to be bugs
lorenz
parents:
1842
diff
changeset
|
1711 return -1; |
|
a15f6323c39d
* implemented Vector3. there are no tests yet, so there are bound to be bugs
lorenz
parents:
1842
diff
changeset
|
1712 |
|
a15f6323c39d
* implemented Vector3. there are no tests yet, so there are bound to be bugs
lorenz
parents:
1842
diff
changeset
|
1713 if (xOrSequence) { |
|
a15f6323c39d
* implemented Vector3. there are no tests yet, so there are bound to be bugs
lorenz
parents:
1842
diff
changeset
|
1714 if (RealNumber_Check(xOrSequence)) { |
|
a15f6323c39d
* implemented Vector3. there are no tests yet, so there are bound to be bugs
lorenz
parents:
1842
diff
changeset
|
1715 self->coords[0] = PyFloat_AsDouble(xOrSequence); |
|
a15f6323c39d
* implemented Vector3. there are no tests yet, so there are bound to be bugs
lorenz
parents:
1842
diff
changeset
|
1716 } |
|
1845
7d2a16e6a1d3
rearanged math module. added forward declarations at the beginning.
lorenz
parents:
1844
diff
changeset
|
1717 else if (PyVectorCompatible_Check(xOrSequence, self->dim)) { |
|
1844
a15f6323c39d
* implemented Vector3. there are no tests yet, so there are bound to be bugs
lorenz
parents:
1842
diff
changeset
|
1718 self->coords[0] = PySequence_GetItem_AsDouble(xOrSequence, 0); |
|
a15f6323c39d
* implemented Vector3. there are no tests yet, so there are bound to be bugs
lorenz
parents:
1842
diff
changeset
|
1719 self->coords[1] = PySequence_GetItem_AsDouble(xOrSequence, 1); |
|
a15f6323c39d
* implemented Vector3. there are no tests yet, so there are bound to be bugs
lorenz
parents:
1842
diff
changeset
|
1720 self->coords[2] = PySequence_GetItem_AsDouble(xOrSequence, 2); |
|
a15f6323c39d
* implemented Vector3. there are no tests yet, so there are bound to be bugs
lorenz
parents:
1842
diff
changeset
|
1721 /* successful initialization from sequence type */ |
|
a15f6323c39d
* implemented Vector3. there are no tests yet, so there are bound to be bugs
lorenz
parents:
1842
diff
changeset
|
1722 return 0; |
|
a15f6323c39d
* implemented Vector3. there are no tests yet, so there are bound to be bugs
lorenz
parents:
1842
diff
changeset
|
1723 } |
|
a15f6323c39d
* implemented Vector3. there are no tests yet, so there are bound to be bugs
lorenz
parents:
1842
diff
changeset
|
1724 else if (PyString_Check(xOrSequence)) { |
|
a15f6323c39d
* implemented Vector3. there are no tests yet, so there are bound to be bugs
lorenz
parents:
1842
diff
changeset
|
1725 /* This should make "Vector3(Vector3().__repr__())" possible */ |
|
a15f6323c39d
* implemented Vector3. there are no tests yet, so there are bound to be bugs
lorenz
parents:
1842
diff
changeset
|
1726 char buffer[STRING_BUF_SIZE]; |
|
a15f6323c39d
* implemented Vector3. there are no tests yet, so there are bound to be bugs
lorenz
parents:
1842
diff
changeset
|
1727 char *endptr; |
|
a15f6323c39d
* implemented Vector3. there are no tests yet, so there are bound to be bugs
lorenz
parents:
1842
diff
changeset
|
1728 char *str = PyString_AsString(xOrSequence); |
|
a15f6323c39d
* implemented Vector3. there are no tests yet, so there are bound to be bugs
lorenz
parents:
1842
diff
changeset
|
1729 if (strncmp(str, "<Vector3(", strlen("<Vector3(")) != 0) |
|
a15f6323c39d
* implemented Vector3. there are no tests yet, so there are bound to be bugs
lorenz
parents:
1842
diff
changeset
|
1730 goto error; |
|
a15f6323c39d
* implemented Vector3. there are no tests yet, so there are bound to be bugs
lorenz
parents:
1842
diff
changeset
|
1731 str += strlen("<Vector3("); |
|
a15f6323c39d
* implemented Vector3. there are no tests yet, so there are bound to be bugs
lorenz
parents:
1842
diff
changeset
|
1732 self->coords[0] = PyOS_ascii_strtod(str, &endptr); |
|
a15f6323c39d
* implemented Vector3. there are no tests yet, so there are bound to be bugs
lorenz
parents:
1842
diff
changeset
|
1733 if (endptr == str) { |
|
a15f6323c39d
* implemented Vector3. there are no tests yet, so there are bound to be bugs
lorenz
parents:
1842
diff
changeset
|
1734 goto error; |
|
a15f6323c39d
* implemented Vector3. there are no tests yet, so there are bound to be bugs
lorenz
parents:
1842
diff
changeset
|
1735 } |
|
a15f6323c39d
* implemented Vector3. there are no tests yet, so there are bound to be bugs
lorenz
parents:
1842
diff
changeset
|
1736 str = endptr + strlen(", "); |
|
a15f6323c39d
* implemented Vector3. there are no tests yet, so there are bound to be bugs
lorenz
parents:
1842
diff
changeset
|
1737 self->coords[1] = PyOS_ascii_strtod(str, &endptr); |
|
a15f6323c39d
* implemented Vector3. there are no tests yet, so there are bound to be bugs
lorenz
parents:
1842
diff
changeset
|
1738 if (endptr == str) |
|
a15f6323c39d
* implemented Vector3. there are no tests yet, so there are bound to be bugs
lorenz
parents:
1842
diff
changeset
|
1739 goto error; |
|
a15f6323c39d
* implemented Vector3. there are no tests yet, so there are bound to be bugs
lorenz
parents:
1842
diff
changeset
|
1740 str = endptr + strlen(", "); |
|
a15f6323c39d
* implemented Vector3. there are no tests yet, so there are bound to be bugs
lorenz
parents:
1842
diff
changeset
|
1741 self->coords[2] = PyOS_ascii_strtod(str, &endptr); |
|
a15f6323c39d
* implemented Vector3. there are no tests yet, so there are bound to be bugs
lorenz
parents:
1842
diff
changeset
|
1742 if (endptr == str) |
|
a15f6323c39d
* implemented Vector3. there are no tests yet, so there are bound to be bugs
lorenz
parents:
1842
diff
changeset
|
1743 goto error; |
|
a15f6323c39d
* implemented Vector3. there are no tests yet, so there are bound to be bugs
lorenz
parents:
1842
diff
changeset
|
1744 /* successful conversion from string */ |
|
a15f6323c39d
* implemented Vector3. there are no tests yet, so there are bound to be bugs
lorenz
parents:
1842
diff
changeset
|
1745 return 0; |
|
a15f6323c39d
* implemented Vector3. there are no tests yet, so there are bound to be bugs
lorenz
parents:
1842
diff
changeset
|
1746 } |
|
a15f6323c39d
* implemented Vector3. there are no tests yet, so there are bound to be bugs
lorenz
parents:
1842
diff
changeset
|
1747 else { |
|
a15f6323c39d
* implemented Vector3. there are no tests yet, so there are bound to be bugs
lorenz
parents:
1842
diff
changeset
|
1748 goto error; |
|
a15f6323c39d
* implemented Vector3. there are no tests yet, so there are bound to be bugs
lorenz
parents:
1842
diff
changeset
|
1749 } |
|
a15f6323c39d
* implemented Vector3. there are no tests yet, so there are bound to be bugs
lorenz
parents:
1842
diff
changeset
|
1750 } |
|
a15f6323c39d
* implemented Vector3. there are no tests yet, so there are bound to be bugs
lorenz
parents:
1842
diff
changeset
|
1751 else { |
|
a15f6323c39d
* implemented Vector3. there are no tests yet, so there are bound to be bugs
lorenz
parents:
1842
diff
changeset
|
1752 self->coords[0] = 0.; |
|
a15f6323c39d
* implemented Vector3. there are no tests yet, so there are bound to be bugs
lorenz
parents:
1842
diff
changeset
|
1753 } |
|
a15f6323c39d
* implemented Vector3. there are no tests yet, so there are bound to be bugs
lorenz
parents:
1842
diff
changeset
|
1754 |
|
a15f6323c39d
* implemented Vector3. there are no tests yet, so there are bound to be bugs
lorenz
parents:
1842
diff
changeset
|
1755 if (y) { |
|
a15f6323c39d
* implemented Vector3. there are no tests yet, so there are bound to be bugs
lorenz
parents:
1842
diff
changeset
|
1756 if (RealNumber_Check(y)) { |
|
a15f6323c39d
* implemented Vector3. there are no tests yet, so there are bound to be bugs
lorenz
parents:
1842
diff
changeset
|
1757 self->coords[1] = PyFloat_AsDouble(y); |
|
a15f6323c39d
* implemented Vector3. there are no tests yet, so there are bound to be bugs
lorenz
parents:
1842
diff
changeset
|
1758 } |
|
a15f6323c39d
* implemented Vector3. there are no tests yet, so there are bound to be bugs
lorenz
parents:
1842
diff
changeset
|
1759 else { |
|
a15f6323c39d
* implemented Vector3. there are no tests yet, so there are bound to be bugs
lorenz
parents:
1842
diff
changeset
|
1760 goto error; |
|
a15f6323c39d
* implemented Vector3. there are no tests yet, so there are bound to be bugs
lorenz
parents:
1842
diff
changeset
|
1761 } |
|
a15f6323c39d
* implemented Vector3. there are no tests yet, so there are bound to be bugs
lorenz
parents:
1842
diff
changeset
|
1762 } |
|
a15f6323c39d
* implemented Vector3. there are no tests yet, so there are bound to be bugs
lorenz
parents:
1842
diff
changeset
|
1763 else { |
|
a15f6323c39d
* implemented Vector3. there are no tests yet, so there are bound to be bugs
lorenz
parents:
1842
diff
changeset
|
1764 self->coords[1] = 0.; |
|
a15f6323c39d
* implemented Vector3. there are no tests yet, so there are bound to be bugs
lorenz
parents:
1842
diff
changeset
|
1765 } |
|
a15f6323c39d
* implemented Vector3. there are no tests yet, so there are bound to be bugs
lorenz
parents:
1842
diff
changeset
|
1766 |
|
a15f6323c39d
* implemented Vector3. there are no tests yet, so there are bound to be bugs
lorenz
parents:
1842
diff
changeset
|
1767 if (z) { |
|
a15f6323c39d
* implemented Vector3. there are no tests yet, so there are bound to be bugs
lorenz
parents:
1842
diff
changeset
|
1768 if (RealNumber_Check(z)) { |
|
a15f6323c39d
* implemented Vector3. there are no tests yet, so there are bound to be bugs
lorenz
parents:
1842
diff
changeset
|
1769 self->coords[2] = PyFloat_AsDouble(z); |
|
a15f6323c39d
* implemented Vector3. there are no tests yet, so there are bound to be bugs
lorenz
parents:
1842
diff
changeset
|
1770 } |
|
a15f6323c39d
* implemented Vector3. there are no tests yet, so there are bound to be bugs
lorenz
parents:
1842
diff
changeset
|
1771 else { |
|
a15f6323c39d
* implemented Vector3. there are no tests yet, so there are bound to be bugs
lorenz
parents:
1842
diff
changeset
|
1772 goto error; |
|
a15f6323c39d
* implemented Vector3. there are no tests yet, so there are bound to be bugs
lorenz
parents:
1842
diff
changeset
|
1773 } |
|
a15f6323c39d
* implemented Vector3. there are no tests yet, so there are bound to be bugs
lorenz
parents:
1842
diff
changeset
|
1774 } |
|
a15f6323c39d
* implemented Vector3. there are no tests yet, so there are bound to be bugs
lorenz
parents:
1842
diff
changeset
|
1775 else { |
|
a15f6323c39d
* implemented Vector3. there are no tests yet, so there are bound to be bugs
lorenz
parents:
1842
diff
changeset
|
1776 self->coords[2] = 0.; |
|
a15f6323c39d
* implemented Vector3. there are no tests yet, so there are bound to be bugs
lorenz
parents:
1842
diff
changeset
|
1777 } |
|
a15f6323c39d
* implemented Vector3. there are no tests yet, so there are bound to be bugs
lorenz
parents:
1842
diff
changeset
|
1778 /* success initialization */ |
|
a15f6323c39d
* implemented Vector3. there are no tests yet, so there are bound to be bugs
lorenz
parents:
1842
diff
changeset
|
1779 return 0; |
|
a15f6323c39d
* implemented Vector3. there are no tests yet, so there are bound to be bugs
lorenz
parents:
1842
diff
changeset
|
1780 error: |
|
a15f6323c39d
* implemented Vector3. there are no tests yet, so there are bound to be bugs
lorenz
parents:
1842
diff
changeset
|
1781 PyErr_SetString(PyExc_ValueError, |
|
a15f6323c39d
* implemented Vector3. there are no tests yet, so there are bound to be bugs
lorenz
parents:
1842
diff
changeset
|
1782 "Vector3 must be initialized with 3 real numbers or a sequence of 3 real numbers"); |
|
a15f6323c39d
* implemented Vector3. there are no tests yet, so there are bound to be bugs
lorenz
parents:
1842
diff
changeset
|
1783 return -1; |
|
a15f6323c39d
* implemented Vector3. there are no tests yet, so there are bound to be bugs
lorenz
parents:
1842
diff
changeset
|
1784 } |
|
a15f6323c39d
* implemented Vector3. there are no tests yet, so there are bound to be bugs
lorenz
parents:
1842
diff
changeset
|
1785 |
|
a15f6323c39d
* implemented Vector3. there are no tests yet, so there are bound to be bugs
lorenz
parents:
1842
diff
changeset
|
1786 |
|
a15f6323c39d
* implemented Vector3. there are no tests yet, so there are bound to be bugs
lorenz
parents:
1842
diff
changeset
|
1787 static void |
|
a15f6323c39d
* implemented Vector3. there are no tests yet, so there are bound to be bugs
lorenz
parents:
1842
diff
changeset
|
1788 _vector3_do_rotate(double *dst_coords, const double *src_coords, |
|
a15f6323c39d
* implemented Vector3. there are no tests yet, so there are bound to be bugs
lorenz
parents:
1842
diff
changeset
|
1789 const double *axis_coords, |
|
a15f6323c39d
* implemented Vector3. there are no tests yet, so there are bound to be bugs
lorenz
parents:
1842
diff
changeset
|
1790 double angle, double epsilon) |
|
a15f6323c39d
* implemented Vector3. there are no tests yet, so there are bound to be bugs
lorenz
parents:
1842
diff
changeset
|
1791 { |
|
a15f6323c39d
* implemented Vector3. there are no tests yet, so there are bound to be bugs
lorenz
parents:
1842
diff
changeset
|
1792 double sinValue, cosValue, cosComplement; |
|
a15f6323c39d
* implemented Vector3. there are no tests yet, so there are bound to be bugs
lorenz
parents:
1842
diff
changeset
|
1793 double normalizationFactor; |
|
a15f6323c39d
* implemented Vector3. there are no tests yet, so there are bound to be bugs
lorenz
parents:
1842
diff
changeset
|
1794 double axisLength2 = 0; |
|
a15f6323c39d
* implemented Vector3. there are no tests yet, so there are bound to be bugs
lorenz
parents:
1842
diff
changeset
|
1795 double axis[3]; |
|
a15f6323c39d
* implemented Vector3. there are no tests yet, so there are bound to be bugs
lorenz
parents:
1842
diff
changeset
|
1796 int i; |
|
a15f6323c39d
* implemented Vector3. there are no tests yet, so there are bound to be bugs
lorenz
parents:
1842
diff
changeset
|
1797 |
|
a15f6323c39d
* implemented Vector3. there are no tests yet, so there are bound to be bugs
lorenz
parents:
1842
diff
changeset
|
1798 /* make sure angle is in range [0, 360) */ |
|
a15f6323c39d
* implemented Vector3. there are no tests yet, so there are bound to be bugs
lorenz
parents:
1842
diff
changeset
|
1799 angle = fmod(angle, 360.); |
|
a15f6323c39d
* implemented Vector3. there are no tests yet, so there are bound to be bugs
lorenz
parents:
1842
diff
changeset
|
1800 if (angle < 0) |
|
a15f6323c39d
* implemented Vector3. there are no tests yet, so there are bound to be bugs
lorenz
parents:
1842
diff
changeset
|
1801 angle += 360.; |
|
a15f6323c39d
* implemented Vector3. there are no tests yet, so there are bound to be bugs
lorenz
parents:
1842
diff
changeset
|
1802 |
|
a15f6323c39d
* implemented Vector3. there are no tests yet, so there are bound to be bugs
lorenz
parents:
1842
diff
changeset
|
1803 for (i = 0; i < 3; ++i) { |
|
a15f6323c39d
* implemented Vector3. there are no tests yet, so there are bound to be bugs
lorenz
parents:
1842
diff
changeset
|
1804 axisLength2 += axis_coords[i] * axis_coords[i]; |
|
a15f6323c39d
* implemented Vector3. there are no tests yet, so there are bound to be bugs
lorenz
parents:
1842
diff
changeset
|
1805 axis[i] = axis_coords[i]; |
|
a15f6323c39d
* implemented Vector3. there are no tests yet, so there are bound to be bugs
lorenz
parents:
1842
diff
changeset
|
1806 } |
|
a15f6323c39d
* implemented Vector3. there are no tests yet, so there are bound to be bugs
lorenz
parents:
1842
diff
changeset
|
1807 |
|
a15f6323c39d
* implemented Vector3. there are no tests yet, so there are bound to be bugs
lorenz
parents:
1842
diff
changeset
|
1808 /* normalize the axis */ |
|
a15f6323c39d
* implemented Vector3. there are no tests yet, so there are bound to be bugs
lorenz
parents:
1842
diff
changeset
|
1809 if (axisLength2 - 1 > epsilon) { |
|
a15f6323c39d
* implemented Vector3. there are no tests yet, so there are bound to be bugs
lorenz
parents:
1842
diff
changeset
|
1810 normalizationFactor = 1. / sqrt(axisLength2); |
|
a15f6323c39d
* implemented Vector3. there are no tests yet, so there are bound to be bugs
lorenz
parents:
1842
diff
changeset
|
1811 for (i = 0; i < 3; ++i) |
|
a15f6323c39d
* implemented Vector3. there are no tests yet, so there are bound to be bugs
lorenz
parents:
1842
diff
changeset
|
1812 axis[i] *= normalizationFactor; |
|
a15f6323c39d
* implemented Vector3. there are no tests yet, so there are bound to be bugs
lorenz
parents:
1842
diff
changeset
|
1813 } |
|
a15f6323c39d
* implemented Vector3. there are no tests yet, so there are bound to be bugs
lorenz
parents:
1842
diff
changeset
|
1814 |
| 1848 | 1815 /* special-case rotation by 0, 90, 180 and 270 degrees */ |
| 1816 if (fmod(angle + epsilon, 90.) < 2 * epsilon) { | |
| 1817 switch ((int)((angle + epsilon) / 90)) { | |
| 1818 case 0: /* 0 degrees */ | |
| 1819 memcpy(dst_coords, src_coords, 3 * sizeof(src_coords[0])); | |
| 1820 break; | |
| 1821 case 1: /* 90 degrees */ | |
| 1822 dst_coords[0] = (src_coords[0] * (axis[0] * axis[0]) + | |
| 1823 src_coords[1] * (axis[0] * axis[1] - axis[2]) + | |
| 1824 src_coords[2] * (axis[0] * axis[2] + axis[1])); | |
| 1825 dst_coords[1] = (src_coords[0] * (axis[0] * axis[1] + axis[2]) + | |
| 1826 src_coords[1] * (axis[1] * axis[1]) + | |
| 1827 src_coords[2] * (axis[1] * axis[2] - axis[0])); | |
| 1828 dst_coords[2] = (src_coords[0] * (axis[0] * axis[2] - axis[1]) + | |
| 1829 src_coords[1] * (axis[1] * axis[2] + axis[0]) + | |
| 1830 src_coords[2] * (axis[2] * axis[2])); | |
| 1831 break; | |
| 1832 case 2: /* 180 degrees */ | |
| 1833 dst_coords[0] = (src_coords[0] * (-1 + axis[0] * axis[0] * 2) + | |
| 1834 src_coords[1] * (axis[0] * axis[1] * 2) + | |
| 1835 src_coords[2] * (axis[0] * axis[2] * 2)); | |
| 1836 dst_coords[1] = (src_coords[0] * (axis[0] * axis[1] * 2) + | |
| 1837 src_coords[1] * (-1 + axis[1] * axis[1] * 2) + | |
| 1838 src_coords[2] * (axis[1] * axis[2] * 2)); | |
| 1839 dst_coords[2] = (src_coords[0] * (axis[0] * axis[2] * 2) + | |
| 1840 src_coords[1] * (axis[1] * axis[2] * 2) + | |
| 1841 src_coords[2] * (-1 + axis[2] * axis[2] * 2)); | |
| 1842 break; | |
| 1843 case 3: /* 270 degrees */ | |
| 1844 dst_coords[0] = (src_coords[0] * (axis[0] * axis[0]) + | |
| 1845 src_coords[1] * (axis[0] * axis[1] + axis[2]) + | |
| 1846 src_coords[2] * (axis[0] * axis[2] - axis[1])); | |
| 1847 dst_coords[1] = (src_coords[0] * (axis[0] * axis[1] - axis[2]) + | |
| 1848 src_coords[1] * (axis[1] * axis[1]) + | |
| 1849 src_coords[2] * (axis[1] * axis[2] + axis[0])); | |
| 1850 dst_coords[2] = (src_coords[0] * (axis[0] * axis[2] + axis[1]) + | |
| 1851 src_coords[1] * (axis[1] * axis[2] - axis[0]) + | |
| 1852 src_coords[2] * (axis[2] * axis[2])); | |
| 1853 break; | |
| 1854 } | |
|
1844
a15f6323c39d
* implemented Vector3. there are no tests yet, so there are bound to be bugs
lorenz
parents:
1842
diff
changeset
|
1855 } |
|
a15f6323c39d
* implemented Vector3. there are no tests yet, so there are bound to be bugs
lorenz
parents:
1842
diff
changeset
|
1856 else { |
|
a15f6323c39d
* implemented Vector3. there are no tests yet, so there are bound to be bugs
lorenz
parents:
1842
diff
changeset
|
1857 angle = DEG2RAD(angle); |
|
a15f6323c39d
* implemented Vector3. there are no tests yet, so there are bound to be bugs
lorenz
parents:
1842
diff
changeset
|
1858 sinValue = sin(angle); |
|
a15f6323c39d
* implemented Vector3. there are no tests yet, so there are bound to be bugs
lorenz
parents:
1842
diff
changeset
|
1859 cosValue = cos(angle); |
|
a15f6323c39d
* implemented Vector3. there are no tests yet, so there are bound to be bugs
lorenz
parents:
1842
diff
changeset
|
1860 cosComplement = 1 - cosValue; |
|
a15f6323c39d
* implemented Vector3. there are no tests yet, so there are bound to be bugs
lorenz
parents:
1842
diff
changeset
|
1861 |
|
a15f6323c39d
* implemented Vector3. there are no tests yet, so there are bound to be bugs
lorenz
parents:
1842
diff
changeset
|
1862 dst_coords[0] = (src_coords[0] * (cosValue + axis[0] * axis[0] * cosComplement) + |
|
a15f6323c39d
* implemented Vector3. there are no tests yet, so there are bound to be bugs
lorenz
parents:
1842
diff
changeset
|
1863 src_coords[1] * (axis[0] * axis[1] * cosComplement - axis[2] * sinValue) + |
|
a15f6323c39d
* implemented Vector3. there are no tests yet, so there are bound to be bugs
lorenz
parents:
1842
diff
changeset
|
1864 src_coords[2] * (axis[0] * axis[2] * cosComplement + axis[1] * sinValue)); |
|
a15f6323c39d
* implemented Vector3. there are no tests yet, so there are bound to be bugs
lorenz
parents:
1842
diff
changeset
|
1865 dst_coords[1] = (src_coords[0] * (axis[0] * axis[1] * cosComplement + axis[2] * sinValue) + |
|
a15f6323c39d
* implemented Vector3. there are no tests yet, so there are bound to be bugs
lorenz
parents:
1842
diff
changeset
|
1866 src_coords[1] * (cosValue + axis[1] * axis[1] * cosComplement) + |
|
a15f6323c39d
* implemented Vector3. there are no tests yet, so there are bound to be bugs
lorenz
parents:
1842
diff
changeset
|
1867 src_coords[2] * (axis[1] * axis[2] * cosComplement - axis[0] * sinValue)); |
|
a15f6323c39d
* implemented Vector3. there are no tests yet, so there are bound to be bugs
lorenz
parents:
1842
diff
changeset
|
1868 dst_coords[2] = (src_coords[0] * (axis[0] * axis[2] * cosComplement - axis[1] * sinValue) + |
|
a15f6323c39d
* implemented Vector3. there are no tests yet, so there are bound to be bugs
lorenz
parents:
1842
diff
changeset
|
1869 src_coords[1] * (axis[1] * axis[2] * cosComplement + axis[0] * sinValue) + |
|
a15f6323c39d
* implemented Vector3. there are no tests yet, so there are bound to be bugs
lorenz
parents:
1842
diff
changeset
|
1870 src_coords[2] * (cosValue + axis[2] * axis[2] * cosComplement)); |
|
a15f6323c39d
* implemented Vector3. there are no tests yet, so there are bound to be bugs
lorenz
parents:
1842
diff
changeset
|
1871 } |
|
a15f6323c39d
* implemented Vector3. there are no tests yet, so there are bound to be bugs
lorenz
parents:
1842
diff
changeset
|
1872 } |
|
a15f6323c39d
* implemented Vector3. there are no tests yet, so there are bound to be bugs
lorenz
parents:
1842
diff
changeset
|
1873 |
|
a15f6323c39d
* implemented Vector3. there are no tests yet, so there are bound to be bugs
lorenz
parents:
1842
diff
changeset
|
1874 static PyObject * |
|
a15f6323c39d
* implemented Vector3. there are no tests yet, so there are bound to be bugs
lorenz
parents:
1842
diff
changeset
|
1875 vector3_rotate(PyVector *self, PyObject *args) |
|
a15f6323c39d
* implemented Vector3. there are no tests yet, so there are bound to be bugs
lorenz
parents:
1842
diff
changeset
|
1876 { |
|
a15f6323c39d
* implemented Vector3. there are no tests yet, so there are bound to be bugs
lorenz
parents:
1842
diff
changeset
|
1877 PyVector *ret; |
|
a15f6323c39d
* implemented Vector3. there are no tests yet, so there are bound to be bugs
lorenz
parents:
1842
diff
changeset
|
1878 PyObject *axis; |
|
1845
7d2a16e6a1d3
rearanged math module. added forward declarations at the beginning.
lorenz
parents:
1844
diff
changeset
|
1879 double axis_coords[3]; |
|
1844
a15f6323c39d
* implemented Vector3. there are no tests yet, so there are bound to be bugs
lorenz
parents:
1842
diff
changeset
|
1880 double angle; |
|
a15f6323c39d
* implemented Vector3. there are no tests yet, so there are bound to be bugs
lorenz
parents:
1842
diff
changeset
|
1881 |
|
a15f6323c39d
* implemented Vector3. there are no tests yet, so there are bound to be bugs
lorenz
parents:
1842
diff
changeset
|
1882 if (!PyArg_ParseTuple(args, "dO:rotate", &angle, &axis)) { |
|
a15f6323c39d
* implemented Vector3. there are no tests yet, so there are bound to be bugs
lorenz
parents:
1842
diff
changeset
|
1883 return NULL; |
|
a15f6323c39d
* implemented Vector3. there are no tests yet, so there are bound to be bugs
lorenz
parents:
1842
diff
changeset
|
1884 } |
|
1845
7d2a16e6a1d3
rearanged math module. added forward declarations at the beginning.
lorenz
parents:
1844
diff
changeset
|
1885 if (!PyVectorCompatible_Check(axis, self->dim)) { |
|
1844
a15f6323c39d
* implemented Vector3. there are no tests yet, so there are bound to be bugs
lorenz
parents:
1842
diff
changeset
|
1886 PyErr_SetString(PyExc_TypeError, "axis must be a 3D Vector"); |
|
a15f6323c39d
* implemented Vector3. there are no tests yet, so there are bound to be bugs
lorenz
parents:
1842
diff
changeset
|
1887 return NULL; |
|
a15f6323c39d
* implemented Vector3. there are no tests yet, so there are bound to be bugs
lorenz
parents:
1842
diff
changeset
|
1888 } |
|
a15f6323c39d
* implemented Vector3. there are no tests yet, so there are bound to be bugs
lorenz
parents:
1842
diff
changeset
|
1889 |
|
1845
7d2a16e6a1d3
rearanged math module. added forward declarations at the beginning.
lorenz
parents:
1844
diff
changeset
|
1890 PySequence_AsVectorCoords(axis, axis_coords, 3); |
|
1844
a15f6323c39d
* implemented Vector3. there are no tests yet, so there are bound to be bugs
lorenz
parents:
1842
diff
changeset
|
1891 ret = (PyVector*)PyVector_NEW(self->dim); |
|
1845
7d2a16e6a1d3
rearanged math module. added forward declarations at the beginning.
lorenz
parents:
1844
diff
changeset
|
1892 _vector3_do_rotate(ret->coords, self->coords, axis_coords, |
|
1844
a15f6323c39d
* implemented Vector3. there are no tests yet, so there are bound to be bugs
lorenz
parents:
1842
diff
changeset
|
1893 angle, self->epsilon); |
|
a15f6323c39d
* implemented Vector3. there are no tests yet, so there are bound to be bugs
lorenz
parents:
1842
diff
changeset
|
1894 return (PyObject*)ret; |
|
a15f6323c39d
* implemented Vector3. there are no tests yet, so there are bound to be bugs
lorenz
parents:
1842
diff
changeset
|
1895 } |
|
a15f6323c39d
* implemented Vector3. there are no tests yet, so there are bound to be bugs
lorenz
parents:
1842
diff
changeset
|
1896 |
|
a15f6323c39d
* implemented Vector3. there are no tests yet, so there are bound to be bugs
lorenz
parents:
1842
diff
changeset
|
1897 static PyObject * |
|
a15f6323c39d
* implemented Vector3. there are no tests yet, so there are bound to be bugs
lorenz
parents:
1842
diff
changeset
|
1898 vector3_rotate_ip(PyVector *self, PyObject *args) |
|
a15f6323c39d
* implemented Vector3. there are no tests yet, so there are bound to be bugs
lorenz
parents:
1842
diff
changeset
|
1899 { |
|
a15f6323c39d
* implemented Vector3. there are no tests yet, so there are bound to be bugs
lorenz
parents:
1842
diff
changeset
|
1900 PyObject *axis; |
|
1845
7d2a16e6a1d3
rearanged math module. added forward declarations at the beginning.
lorenz
parents:
1844
diff
changeset
|
1901 double axis_coords[3]; |
|
1844
a15f6323c39d
* implemented Vector3. there are no tests yet, so there are bound to be bugs
lorenz
parents:
1842
diff
changeset
|
1902 double angle; |
|
a15f6323c39d
* implemented Vector3. there are no tests yet, so there are bound to be bugs
lorenz
parents:
1842
diff
changeset
|
1903 double tmp[3]; |
|
a15f6323c39d
* implemented Vector3. there are no tests yet, so there are bound to be bugs
lorenz
parents:
1842
diff
changeset
|
1904 |
|
a15f6323c39d
* implemented Vector3. there are no tests yet, so there are bound to be bugs
lorenz
parents:
1842
diff
changeset
|
1905 if (!PyArg_ParseTuple(args, "dO:rotate_ip", &angle, &axis)) { |
|
a15f6323c39d
* implemented Vector3. there are no tests yet, so there are bound to be bugs
lorenz
parents:
1842
diff
changeset
|
1906 return NULL; |
|
a15f6323c39d
* implemented Vector3. there are no tests yet, so there are bound to be bugs
lorenz
parents:
1842
diff
changeset
|
1907 } |
|
1845
7d2a16e6a1d3
rearanged math module. added forward declarations at the beginning.
lorenz
parents:
1844
diff
changeset
|
1908 if (!PyVectorCompatible_Check(axis, self->dim)) { |
|
1844
a15f6323c39d
* implemented Vector3. there are no tests yet, so there are bound to be bugs
lorenz
parents:
1842
diff
changeset
|
1909 PyErr_SetString(PyExc_TypeError, "axis must be a 3D Vector"); |
|
a15f6323c39d
* implemented Vector3. there are no tests yet, so there are bound to be bugs
lorenz
parents:
1842
diff
changeset
|
1910 return NULL; |
|
a15f6323c39d
* implemented Vector3. there are no tests yet, so there are bound to be bugs
lorenz
parents:
1842
diff
changeset
|
1911 } |
|
a15f6323c39d
* implemented Vector3. there are no tests yet, so there are bound to be bugs
lorenz
parents:
1842
diff
changeset
|
1912 |
|
1845
7d2a16e6a1d3
rearanged math module. added forward declarations at the beginning.
lorenz
parents:
1844
diff
changeset
|
1913 PySequence_AsVectorCoords(axis, axis_coords, 3); |
|
1844
a15f6323c39d
* implemented Vector3. there are no tests yet, so there are bound to be bugs
lorenz
parents:
1842
diff
changeset
|
1914 memcpy(tmp, self->coords, 3 * sizeof(self->coords[0])); |
|
1845
7d2a16e6a1d3
rearanged math module. added forward declarations at the beginning.
lorenz
parents:
1844
diff
changeset
|
1915 _vector3_do_rotate(self->coords, tmp, axis_coords, angle, self->epsilon); |
|
1844
a15f6323c39d
* implemented Vector3. there are no tests yet, so there are bound to be bugs
lorenz
parents:
1842
diff
changeset
|
1916 Py_RETURN_NONE; |
|
a15f6323c39d
* implemented Vector3. there are no tests yet, so there are bound to be bugs
lorenz
parents:
1842
diff
changeset
|
1917 } |
|
a15f6323c39d
* implemented Vector3. there are no tests yet, so there are bound to be bugs
lorenz
parents:
1842
diff
changeset
|
1918 |
|
a15f6323c39d
* implemented Vector3. there are no tests yet, so there are bound to be bugs
lorenz
parents:
1842
diff
changeset
|
1919 static PyObject * |
|
1845
7d2a16e6a1d3
rearanged math module. added forward declarations at the beginning.
lorenz
parents:
1844
diff
changeset
|
1920 vector3_rotate_x(PyVector *self, PyObject *angleObject) |
|
7d2a16e6a1d3
rearanged math module. added forward declarations at the beginning.
lorenz
parents:
1844
diff
changeset
|
1921 { |
|
7d2a16e6a1d3
rearanged math module. added forward declarations at the beginning.
lorenz
parents:
1844
diff
changeset
|
1922 PyVector *ret; |
|
7d2a16e6a1d3
rearanged math module. added forward declarations at the beginning.
lorenz
parents:
1844
diff
changeset
|
1923 double sinValue, cosValue; |
|
7d2a16e6a1d3
rearanged math module. added forward declarations at the beginning.
lorenz
parents:
1844
diff
changeset
|
1924 double angle; |
|
7d2a16e6a1d3
rearanged math module. added forward declarations at the beginning.
lorenz
parents:
1844
diff
changeset
|
1925 |
|
7d2a16e6a1d3
rearanged math module. added forward declarations at the beginning.
lorenz
parents:
1844
diff
changeset
|
1926 if (!RealNumber_Check(angleObject)) { |
|
7d2a16e6a1d3
rearanged math module. added forward declarations at the beginning.
lorenz
parents:
1844
diff
changeset
|
1927 PyErr_SetString(PyExc_TypeError, "angle must be a real number"); |
|
7d2a16e6a1d3
rearanged math module. added forward declarations at the beginning.
lorenz
parents:
1844
diff
changeset
|
1928 return NULL; |
|
7d2a16e6a1d3
rearanged math module. added forward declarations at the beginning.
lorenz
parents:
1844
diff
changeset
|
1929 } |
|
7d2a16e6a1d3
rearanged math module. added forward declarations at the beginning.
lorenz
parents:
1844
diff
changeset
|
1930 angle = DEG2RAD(PyFloat_AsDouble(angleObject)); |
|
7d2a16e6a1d3
rearanged math module. added forward declarations at the beginning.
lorenz
parents:
1844
diff
changeset
|
1931 sinValue = sin(angle); |
|
7d2a16e6a1d3
rearanged math module. added forward declarations at the beginning.
lorenz
parents:
1844
diff
changeset
|
1932 cosValue = cos(angle); |
|
7d2a16e6a1d3
rearanged math module. added forward declarations at the beginning.
lorenz
parents:
1844
diff
changeset
|
1933 |
|
7d2a16e6a1d3
rearanged math module. added forward declarations at the beginning.
lorenz
parents:
1844
diff
changeset
|
1934 ret = (PyVector*)PyVector_NEW(self->dim); |
|
7d2a16e6a1d3
rearanged math module. added forward declarations at the beginning.
lorenz
parents:
1844
diff
changeset
|
1935 ret->coords[0] = self->coords[0]; |
|
7d2a16e6a1d3
rearanged math module. added forward declarations at the beginning.
lorenz
parents:
1844
diff
changeset
|
1936 ret->coords[1] = self->coords[1] * cosValue - self->coords[2] * sinValue; |
|
7d2a16e6a1d3
rearanged math module. added forward declarations at the beginning.
lorenz
parents:
1844
diff
changeset
|
1937 ret->coords[2] = self->coords[1] * sinValue + self->coords[2] * cosValue; |
|
7d2a16e6a1d3
rearanged math module. added forward declarations at the beginning.
lorenz
parents:
1844
diff
changeset
|
1938 return (PyObject*)ret; |
|
7d2a16e6a1d3
rearanged math module. added forward declarations at the beginning.
lorenz
parents:
1844
diff
changeset
|
1939 } |
|
7d2a16e6a1d3
rearanged math module. added forward declarations at the beginning.
lorenz
parents:
1844
diff
changeset
|
1940 |
|
7d2a16e6a1d3
rearanged math module. added forward declarations at the beginning.
lorenz
parents:
1844
diff
changeset
|
1941 static PyObject * |
|
7d2a16e6a1d3
rearanged math module. added forward declarations at the beginning.
lorenz
parents:
1844
diff
changeset
|
1942 vector3_rotate_x_ip(PyVector *self, PyObject *angleObject) |
|
7d2a16e6a1d3
rearanged math module. added forward declarations at the beginning.
lorenz
parents:
1844
diff
changeset
|
1943 { |
|
7d2a16e6a1d3
rearanged math module. added forward declarations at the beginning.
lorenz
parents:
1844
diff
changeset
|
1944 PyVector *ret; |
|
7d2a16e6a1d3
rearanged math module. added forward declarations at the beginning.
lorenz
parents:
1844
diff
changeset
|
1945 double tmp_coords[3]; |
|
7d2a16e6a1d3
rearanged math module. added forward declarations at the beginning.
lorenz
parents:
1844
diff
changeset
|
1946 double sinValue, cosValue; |
|
7d2a16e6a1d3
rearanged math module. added forward declarations at the beginning.
lorenz
parents:
1844
diff
changeset
|
1947 double angle; |
|
7d2a16e6a1d3
rearanged math module. added forward declarations at the beginning.
lorenz
parents:
1844
diff
changeset
|
1948 |
|
7d2a16e6a1d3
rearanged math module. added forward declarations at the beginning.
lorenz
parents:
1844
diff
changeset
|
1949 if (!RealNumber_Check(angleObject)) { |
|
7d2a16e6a1d3
rearanged math module. added forward declarations at the beginning.
lorenz
parents:
1844
diff
changeset
|
1950 PyErr_SetString(PyExc_TypeError, "angle must be a real number"); |
|
7d2a16e6a1d3
rearanged math module. added forward declarations at the beginning.
lorenz
parents:
1844
diff
changeset
|
1951 return NULL; |
|
7d2a16e6a1d3
rearanged math module. added forward declarations at the beginning.
lorenz
parents:
1844
diff
changeset
|
1952 } |
|
7d2a16e6a1d3
rearanged math module. added forward declarations at the beginning.
lorenz
parents:
1844
diff
changeset
|
1953 angle = DEG2RAD(PyFloat_AsDouble(angleObject)); |
|
7d2a16e6a1d3
rearanged math module. added forward declarations at the beginning.
lorenz
parents:
1844
diff
changeset
|
1954 sinValue = sin(angle); |
|
7d2a16e6a1d3
rearanged math module. added forward declarations at the beginning.
lorenz
parents:
1844
diff
changeset
|
1955 cosValue = cos(angle); |
|
7d2a16e6a1d3
rearanged math module. added forward declarations at the beginning.
lorenz
parents:
1844
diff
changeset
|
1956 memcpy(tmp_coords, self->coords, 3 * sizeof(tmp_coords[0])); |
|
7d2a16e6a1d3
rearanged math module. added forward declarations at the beginning.
lorenz
parents:
1844
diff
changeset
|
1957 |
|
7d2a16e6a1d3
rearanged math module. added forward declarations at the beginning.
lorenz
parents:
1844
diff
changeset
|
1958 self->coords[1] = tmp_coords[1] * cosValue - tmp_coords[2] * sinValue; |
|
7d2a16e6a1d3
rearanged math module. added forward declarations at the beginning.
lorenz
parents:
1844
diff
changeset
|
1959 self->coords[2] = tmp_coords[1] * sinValue + tmp_coords[2] * cosValue; |
|
7d2a16e6a1d3
rearanged math module. added forward declarations at the beginning.
lorenz
parents:
1844
diff
changeset
|
1960 return (PyObject*)ret; |
|
7d2a16e6a1d3
rearanged math module. added forward declarations at the beginning.
lorenz
parents:
1844
diff
changeset
|
1961 } |
|
7d2a16e6a1d3
rearanged math module. added forward declarations at the beginning.
lorenz
parents:
1844
diff
changeset
|
1962 |
|
7d2a16e6a1d3
rearanged math module. added forward declarations at the beginning.
lorenz
parents:
1844
diff
changeset
|
1963 static PyObject * |
|
7d2a16e6a1d3
rearanged math module. added forward declarations at the beginning.
lorenz
parents:
1844
diff
changeset
|
1964 vector3_rotate_y(PyVector *self, PyObject *angleObject) |
|
7d2a16e6a1d3
rearanged math module. added forward declarations at the beginning.
lorenz
parents:
1844
diff
changeset
|
1965 { |
|
7d2a16e6a1d3
rearanged math module. added forward declarations at the beginning.
lorenz
parents:
1844
diff
changeset
|
1966 PyVector *ret; |
|
7d2a16e6a1d3
rearanged math module. added forward declarations at the beginning.
lorenz
parents:
1844
diff
changeset
|
1967 double sinValue, cosValue; |
|
7d2a16e6a1d3
rearanged math module. added forward declarations at the beginning.
lorenz
parents:
1844
diff
changeset
|
1968 double angle; |
|
7d2a16e6a1d3
rearanged math module. added forward declarations at the beginning.
lorenz
parents:
1844
diff
changeset
|
1969 |
|
7d2a16e6a1d3
rearanged math module. added forward declarations at the beginning.
lorenz
parents:
1844
diff
changeset
|
1970 if (!RealNumber_Check(angleObject)) { |
|
7d2a16e6a1d3
rearanged math module. added forward declarations at the beginning.
lorenz
parents:
1844
diff
changeset
|
1971 PyErr_SetString(PyExc_TypeError, "angle must be a real number"); |
|
7d2a16e6a1d3
rearanged math module. added forward declarations at the beginning.
lorenz
parents:
1844
diff
changeset
|
1972 return NULL; |
|
7d2a16e6a1d3
rearanged math module. added forward declarations at the beginning.
lorenz
parents:
1844
diff
changeset
|
1973 } |
|
7d2a16e6a1d3
rearanged math module. added forward declarations at the beginning.
lorenz
parents:
1844
diff
changeset
|
1974 angle = DEG2RAD(PyFloat_AsDouble(angleObject)); |
|
7d2a16e6a1d3
rearanged math module. added forward declarations at the beginning.
lorenz
parents:
1844
diff
changeset
|
1975 sinValue = sin(angle); |
|
7d2a16e6a1d3
rearanged math module. added forward declarations at the beginning.
lorenz
parents:
1844
diff
changeset
|
1976 cosValue = cos(angle); |
|
7d2a16e6a1d3
rearanged math module. added forward declarations at the beginning.
lorenz
parents:
1844
diff
changeset
|
1977 |
|
7d2a16e6a1d3
rearanged math module. added forward declarations at the beginning.
lorenz
parents:
1844
diff
changeset
|
1978 ret = (PyVector*)PyVector_NEW(self->dim); |
|
7d2a16e6a1d3
rearanged math module. added forward declarations at the beginning.
lorenz
parents:
1844
diff
changeset
|
1979 ret->coords[0] = self->coords[0] * cosValue + self->coords[2] * sinValue; |
|
7d2a16e6a1d3
rearanged math module. added forward declarations at the beginning.
lorenz
parents:
1844
diff
changeset
|
1980 ret->coords[1] = self->coords[1]; |
|
7d2a16e6a1d3
rearanged math module. added forward declarations at the beginning.
lorenz
parents:
1844
diff
changeset
|
1981 ret->coords[2] = -self->coords[0] * sinValue + self->coords[2] * cosValue; |
|
7d2a16e6a1d3
rearanged math module. added forward declarations at the beginning.
lorenz
parents:
1844
diff
changeset
|
1982 |
|
7d2a16e6a1d3
rearanged math module. added forward declarations at the beginning.
lorenz
parents:
1844
diff
changeset
|
1983 return (PyObject*)ret; |
|
7d2a16e6a1d3
rearanged math module. added forward declarations at the beginning.
lorenz
parents:
1844
diff
changeset
|
1984 } |
|
7d2a16e6a1d3
rearanged math module. added forward declarations at the beginning.
lorenz
parents:
1844
diff
changeset
|
1985 |
|
7d2a16e6a1d3
rearanged math module. added forward declarations at the beginning.
lorenz
parents:
1844
diff
changeset
|
1986 static PyObject * |
|
7d2a16e6a1d3
rearanged math module. added forward declarations at the beginning.
lorenz
parents:
1844
diff
changeset
|
1987 vector3_rotate_y_ip(PyVector *self, PyObject *angleObject) |
|
7d2a16e6a1d3
rearanged math module. added forward declarations at the beginning.
lorenz
parents:
1844
diff
changeset
|
1988 { |
|
7d2a16e6a1d3
rearanged math module. added forward declarations at the beginning.
lorenz
parents:
1844
diff
changeset
|
1989 PyVector *ret; |
|
7d2a16e6a1d3
rearanged math module. added forward declarations at the beginning.
lorenz
parents:
1844
diff
changeset
|
1990 double tmp_coords[3]; |
|
7d2a16e6a1d3
rearanged math module. added forward declarations at the beginning.
lorenz
parents:
1844
diff
changeset
|
1991 double sinValue, cosValue; |
|
7d2a16e6a1d3
rearanged math module. added forward declarations at the beginning.
lorenz
parents:
1844
diff
changeset
|
1992 double angle; |
|
7d2a16e6a1d3
rearanged math module. added forward declarations at the beginning.
lorenz
parents:
1844
diff
changeset
|
1993 |
|
7d2a16e6a1d3
rearanged math module. added forward declarations at the beginning.
lorenz
parents:
1844
diff
changeset
|
1994 if (!RealNumber_Check(angleObject)) { |
|
7d2a16e6a1d3
rearanged math module. added forward declarations at the beginning.
lorenz
parents:
1844
diff
changeset
|
1995 PyErr_SetString(PyExc_TypeError, "angle must be a real number"); |
|
7d2a16e6a1d3
rearanged math module. added forward declarations at the beginning.
lorenz
parents:
1844
diff
changeset
|
1996 return NULL; |
|
7d2a16e6a1d3
rearanged math module. added forward declarations at the beginning.
lorenz
parents:
1844
diff
changeset
|
1997 } |
|
7d2a16e6a1d3
rearanged math module. added forward declarations at the beginning.
lorenz
parents:
1844
diff
changeset
|
1998 angle = DEG2RAD(PyFloat_AsDouble(angleObject)); |
|
7d2a16e6a1d3
rearanged math module. added forward declarations at the beginning.
lorenz
parents:
1844
diff
changeset
|
1999 sinValue = sin(angle); |
|
7d2a16e6a1d3
rearanged math module. added forward declarations at the beginning.
lorenz
parents:
1844
diff
changeset
|
2000 cosValue = cos(angle); |
|
7d2a16e6a1d3
rearanged math module. added forward declarations at the beginning.
lorenz
parents:
1844
diff
changeset
|
2001 memcpy(tmp_coords, self->coords, 3 * sizeof(tmp_coords[0])); |
|
7d2a16e6a1d3
rearanged math module. added forward declarations at the beginning.
lorenz
parents:
1844
diff
changeset
|
2002 |
|
7d2a16e6a1d3
rearanged math module. added forward declarations at the beginning.
lorenz
parents:
1844
diff
changeset
|
2003 self->coords[0] = tmp_coords[0] * cosValue + tmp_coords[2] * sinValue; |
|
7d2a16e6a1d3
rearanged math module. added forward declarations at the beginning.
lorenz
parents:
1844
diff
changeset
|
2004 self->coords[2] = -tmp_coords[0] * sinValue + tmp_coords[2] * cosValue; |
|
7d2a16e6a1d3
rearanged math module. added forward declarations at the beginning.
lorenz
parents:
1844
diff
changeset
|
2005 return (PyObject*)ret; |
|
7d2a16e6a1d3
rearanged math module. added forward declarations at the beginning.
lorenz
parents:
1844
diff
changeset
|
2006 } |
|
7d2a16e6a1d3
rearanged math module. added forward declarations at the beginning.
lorenz
parents:
1844
diff
changeset
|
2007 |
|
7d2a16e6a1d3
rearanged math module. added forward declarations at the beginning.
lorenz
parents:
1844
diff
changeset
|
2008 static PyObject * |
|
7d2a16e6a1d3
rearanged math module. added forward declarations at the beginning.
lorenz
parents:
1844
diff
changeset
|
2009 vector3_rotate_z(PyVector *self, PyObject *angleObject) |
|
7d2a16e6a1d3
rearanged math module. added forward declarations at the beginning.
lorenz
parents:
1844
diff
changeset
|
2010 { |
|
7d2a16e6a1d3
rearanged math module. added forward declarations at the beginning.
lorenz
parents:
1844
diff
changeset
|
2011 PyVector *ret; |
|
7d2a16e6a1d3
rearanged math module. added forward declarations at the beginning.
lorenz
parents:
1844
diff
changeset
|
2012 double sinValue, cosValue; |
|
7d2a16e6a1d3
rearanged math module. added forward declarations at the beginning.
lorenz
parents:
1844
diff
changeset
|
2013 double angle; |
|
7d2a16e6a1d3
rearanged math module. added forward declarations at the beginning.
lorenz
parents:
1844
diff
changeset
|
2014 |
|
7d2a16e6a1d3
rearanged math module. added forward declarations at the beginning.
lorenz
parents:
1844
diff
changeset
|
2015 if (!RealNumber_Check(angleObject)) { |
|
7d2a16e6a1d3
rearanged math module. added forward declarations at the beginning.
lorenz
parents:
1844
diff
changeset
|
2016 PyErr_SetString(PyExc_TypeError, "angle must be a real number"); |
|
7d2a16e6a1d3
rearanged math module. added forward declarations at the beginning.
lorenz
parents:
1844
diff
changeset
|
2017 return NULL; |
|
7d2a16e6a1d3
rearanged math module. added forward declarations at the beginning.
lorenz
parents:
1844
diff
changeset
|
2018 } |
|
7d2a16e6a1d3
rearanged math module. added forward declarations at the beginning.
lorenz
parents:
1844
diff
changeset
|
2019 angle = DEG2RAD(PyFloat_AsDouble(angleObject)); |
|
7d2a16e6a1d3
rearanged math module. added forward declarations at the beginning.
lorenz
parents:
1844
diff
changeset
|
2020 sinValue = sin(angle); |
|
7d2a16e6a1d3
rearanged math module. added forward declarations at the beginning.
lorenz
parents:
1844
diff
changeset
|
2021 cosValue = cos(angle); |
|
7d2a16e6a1d3
rearanged math module. added forward declarations at the beginning.
lorenz
parents:
1844
diff
changeset
|
2022 |
|
7d2a16e6a1d3
rearanged math module. added forward declarations at the beginning.
lorenz
parents:
1844
diff
changeset
|
2023 ret = (PyVector*)PyVector_NEW(self->dim); |
|
7d2a16e6a1d3
rearanged math module. added forward declarations at the beginning.
lorenz
parents:
1844
diff
changeset
|
2024 ret->coords[0] = self->coords[0] * cosValue - self->coords[1] * sinValue; |
|
7d2a16e6a1d3
rearanged math module. added forward declarations at the beginning.
lorenz
parents:
1844
diff
changeset
|
2025 ret->coords[1] = self->coords[0] * sinValue + self->coords[1] * cosValue; |
|
7d2a16e6a1d3
rearanged math module. added forward declarations at the beginning.
lorenz
parents:
1844
diff
changeset
|
2026 ret->coords[2] = self->coords[2]; |
|
7d2a16e6a1d3
rearanged math module. added forward declarations at the beginning.
lorenz
parents:
1844
diff
changeset
|
2027 |
|
7d2a16e6a1d3
rearanged math module. added forward declarations at the beginning.
lorenz
parents:
1844
diff
changeset
|
2028 return (PyObject*)ret; |
|
7d2a16e6a1d3
rearanged math module. added forward declarations at the beginning.
lorenz
parents:
1844
diff
changeset
|
2029 } |
|
7d2a16e6a1d3
rearanged math module. added forward declarations at the beginning.
lorenz
parents:
1844
diff
changeset
|
2030 |
|
7d2a16e6a1d3
rearanged math module. added forward declarations at the beginning.
lorenz
parents:
1844
diff
changeset
|
2031 static PyObject * |
|
7d2a16e6a1d3
rearanged math module. added forward declarations at the beginning.
lorenz
parents:
1844
diff
changeset
|
2032 vector3_rotate_z_ip(PyVector *self, PyObject *angleObject) |
|
7d2a16e6a1d3
rearanged math module. added forward declarations at the beginning.
lorenz
parents:
1844
diff
changeset
|
2033 { |
|
7d2a16e6a1d3
rearanged math module. added forward declarations at the beginning.
lorenz
parents:
1844
diff
changeset
|
2034 PyVector *ret; |
|
7d2a16e6a1d3
rearanged math module. added forward declarations at the beginning.
lorenz
parents:
1844
diff
changeset
|
2035 double tmp_coords[3]; |
|
7d2a16e6a1d3
rearanged math module. added forward declarations at the beginning.
lorenz
parents:
1844
diff
changeset
|
2036 double sinValue, cosValue; |
|
7d2a16e6a1d3
rearanged math module. added forward declarations at the beginning.
lorenz
parents:
1844
diff
changeset
|
2037 double angle; |
|
7d2a16e6a1d3
rearanged math module. added forward declarations at the beginning.
lorenz
parents:
1844
diff
changeset
|
2038 |
|
7d2a16e6a1d3
rearanged math module. added forward declarations at the beginning.
lorenz
parents:
1844
diff
changeset
|
2039 if (!RealNumber_Check(angleObject)) { |
|
7d2a16e6a1d3
rearanged math module. added forward declarations at the beginning.
lorenz
parents:
1844
diff
changeset
|
2040 PyErr_SetString(PyExc_TypeError, "angle must be a real number"); |
|
7d2a16e6a1d3
rearanged math module. added forward declarations at the beginning.
lorenz
parents:
1844
diff
changeset
|
2041 return NULL; |
|
7d2a16e6a1d3
rearanged math module. added forward declarations at the beginning.
lorenz
parents:
1844
diff
changeset
|
2042 } |
|
7d2a16e6a1d3
rearanged math module. added forward declarations at the beginning.
lorenz
parents:
1844
diff
changeset
|
2043 angle = DEG2RAD(PyFloat_AsDouble(angleObject)); |
|
7d2a16e6a1d3
rearanged math module. added forward declarations at the beginning.
lorenz
parents:
1844
diff
changeset
|
2044 sinValue = sin(angle); |
|
7d2a16e6a1d3
rearanged math module. added forward declarations at the beginning.
lorenz
parents:
1844
diff
changeset
|
2045 cosValue = cos(angle); |
|
7d2a16e6a1d3
rearanged math module. added forward declarations at the beginning.
lorenz
parents:
1844
diff
changeset
|
2046 memcpy(tmp_coords, self->coords, 3 * sizeof(tmp_coords[0])); |
|
7d2a16e6a1d3
rearanged math module. added forward declarations at the beginning.
lorenz
parents:
1844
diff
changeset
|
2047 |
|
7d2a16e6a1d3
rearanged math module. added forward declarations at the beginning.
lorenz
parents:
1844
diff
changeset
|
2048 self->coords[0] = tmp_coords[0] * cosValue - tmp_coords[1] * sinValue; |
|
7d2a16e6a1d3
rearanged math module. added forward declarations at the beginning.
lorenz
parents:
1844
diff
changeset
|
2049 self->coords[1] = tmp_coords[0] * sinValue + tmp_coords[1] * cosValue; |
|
7d2a16e6a1d3
rearanged math module. added forward declarations at the beginning.
lorenz
parents:
1844
diff
changeset
|
2050 return (PyObject*)ret; |
|
7d2a16e6a1d3
rearanged math module. added forward declarations at the beginning.
lorenz
parents:
1844
diff
changeset
|
2051 } |
|
7d2a16e6a1d3
rearanged math module. added forward declarations at the beginning.
lorenz
parents:
1844
diff
changeset
|
2052 |
| 1846 | 2053 |
|
1845
7d2a16e6a1d3
rearanged math module. added forward declarations at the beginning.
lorenz
parents:
1844
diff
changeset
|
2054 static PyObject * |
|
1844
a15f6323c39d
* implemented Vector3. there are no tests yet, so there are bound to be bugs
lorenz
parents:
1842
diff
changeset
|
2055 vector3_cross(PyVector *self, PyObject *other) |
|
a15f6323c39d
* implemented Vector3. there are no tests yet, so there are bound to be bugs
lorenz
parents:
1842
diff
changeset
|
2056 { |
|
a15f6323c39d
* implemented Vector3. there are no tests yet, so there are bound to be bugs
lorenz
parents:
1842
diff
changeset
|
2057 PyVector *ret; |
|
1845
7d2a16e6a1d3
rearanged math module. added forward declarations at the beginning.
lorenz
parents:
1844
diff
changeset
|
2058 double *ret_coords; |
|
7d2a16e6a1d3
rearanged math module. added forward declarations at the beginning.
lorenz
parents:
1844
diff
changeset
|
2059 |
|
7d2a16e6a1d3
rearanged math module. added forward declarations at the beginning.
lorenz
parents:
1844
diff
changeset
|
2060 if (!PyVectorCompatible_Check(other, self->dim)) { |
|
1844
a15f6323c39d
* implemented Vector3. there are no tests yet, so there are bound to be bugs
lorenz
parents:
1842
diff
changeset
|
2061 PyErr_SetString(PyExc_TypeError, "cannot calculate cross Product"); |
|
a15f6323c39d
* implemented Vector3. there are no tests yet, so there are bound to be bugs
lorenz
parents:
1842
diff
changeset
|
2062 return NULL; |
|
a15f6323c39d
* implemented Vector3. there are no tests yet, so there are bound to be bugs
lorenz
parents:
1842
diff
changeset
|
2063 } |
|
a15f6323c39d
* implemented Vector3. there are no tests yet, so there are bound to be bugs
lorenz
parents:
1842
diff
changeset
|
2064 |
|
a15f6323c39d
* implemented Vector3. there are no tests yet, so there are bound to be bugs
lorenz
parents:
1842
diff
changeset
|
2065 ret = (PyVector*)PyVector_NEW(self->dim); |
|
1845
7d2a16e6a1d3
rearanged math module. added forward declarations at the beginning.
lorenz
parents:
1844
diff
changeset
|
2066 ret_coords = ret->coords; |
|
1844
a15f6323c39d
* implemented Vector3. there are no tests yet, so there are bound to be bugs
lorenz
parents:
1842
diff
changeset
|
2067 if (PyVector_Check(other)) { |
|
1845
7d2a16e6a1d3
rearanged math module. added forward declarations at the beginning.
lorenz
parents:
1844
diff
changeset
|
2068 double *other_coords = ((PyVector *)other)->coords; |
|
7d2a16e6a1d3
rearanged math module. added forward declarations at the beginning.
lorenz
parents:
1844
diff
changeset
|
2069 ret_coords[0] = (self->coords[1] * other_coords[2]) - (self->coords[2] * other_coords[1]); |
|
7d2a16e6a1d3
rearanged math module. added forward declarations at the beginning.
lorenz
parents:
1844
diff
changeset
|
2070 ret_coords[1] = (self->coords[2] * other_coords[0]) - (self->coords[0] * other_coords[2]); |
|
7d2a16e6a1d3
rearanged math module. added forward declarations at the beginning.
lorenz
parents:
1844
diff
changeset
|
2071 ret_coords[2] = (self->coords[0] * other_coords[1]) - (self->coords[1] * other_coords[0]); |
|
1844
a15f6323c39d
* implemented Vector3. there are no tests yet, so there are bound to be bugs
lorenz
parents:
1842
diff
changeset
|
2072 } |
|
a15f6323c39d
* implemented Vector3. there are no tests yet, so there are bound to be bugs
lorenz
parents:
1842
diff
changeset
|
2073 else { |
|
1845
7d2a16e6a1d3
rearanged math module. added forward declarations at the beginning.
lorenz
parents:
1844
diff
changeset
|
2074 double other_coords[3]; |
|
7d2a16e6a1d3
rearanged math module. added forward declarations at the beginning.
lorenz
parents:
1844
diff
changeset
|
2075 PySequence_AsVectorCoords(other, other_coords, 3); |
|
7d2a16e6a1d3
rearanged math module. added forward declarations at the beginning.
lorenz
parents:
1844
diff
changeset
|
2076 ret_coords[0] = (self->coords[1] * other_coords[2]) - (self->coords[2] * other_coords[1]); |
|
7d2a16e6a1d3
rearanged math module. added forward declarations at the beginning.
lorenz
parents:
1844
diff
changeset
|
2077 ret_coords[1] = (self->coords[2] * other_coords[0]) - (self->coords[0] * other_coords[2]); |
|
7d2a16e6a1d3
rearanged math module. added forward declarations at the beginning.
lorenz
parents:
1844
diff
changeset
|
2078 ret_coords[2] = (self->coords[0] * other_coords[1]) - (self->coords[1] * other_coords[0]); |
|
1844
a15f6323c39d
* implemented Vector3. there are no tests yet, so there are bound to be bugs
lorenz
parents:
1842
diff
changeset
|
2079 } |
|
a15f6323c39d
* implemented Vector3. there are no tests yet, so there are bound to be bugs
lorenz
parents:
1842
diff
changeset
|
2080 return (PyObject*)ret; |
|
a15f6323c39d
* implemented Vector3. there are no tests yet, so there are bound to be bugs
lorenz
parents:
1842
diff
changeset
|
2081 } |
|
a15f6323c39d
* implemented Vector3. there are no tests yet, so there are bound to be bugs
lorenz
parents:
1842
diff
changeset
|
2082 |
|
a15f6323c39d
* implemented Vector3. there are no tests yet, so there are bound to be bugs
lorenz
parents:
1842
diff
changeset
|
2083 static PyObject * |
|
a15f6323c39d
* implemented Vector3. there are no tests yet, so there are bound to be bugs
lorenz
parents:
1842
diff
changeset
|
2084 vector3_angle_to(PyVector *self, PyObject *other) |
|
a15f6323c39d
* implemented Vector3. there are no tests yet, so there are bound to be bugs
lorenz
parents:
1842
diff
changeset
|
2085 { |
|
1845
7d2a16e6a1d3
rearanged math module. added forward declarations at the beginning.
lorenz
parents:
1844
diff
changeset
|
2086 int i; |
|
7d2a16e6a1d3
rearanged math module. added forward declarations at the beginning.
lorenz
parents:
1844
diff
changeset
|
2087 double angle, tmp1, tmp2; |
|
7d2a16e6a1d3
rearanged math module. added forward declarations at the beginning.
lorenz
parents:
1844
diff
changeset
|
2088 double other_coords[VECTOR_MAX_SIZE]; |
|
7d2a16e6a1d3
rearanged math module. added forward declarations at the beginning.
lorenz
parents:
1844
diff
changeset
|
2089 |
|
7d2a16e6a1d3
rearanged math module. added forward declarations at the beginning.
lorenz
parents:
1844
diff
changeset
|
2090 if (!PyVectorCompatible_Check(other, self->dim)) { |
|
1844
a15f6323c39d
* implemented Vector3. there are no tests yet, so there are bound to be bugs
lorenz
parents:
1842
diff
changeset
|
2091 PyErr_SetString(PyExc_TypeError, "expected an vector."); |
|
a15f6323c39d
* implemented Vector3. there are no tests yet, so there are bound to be bugs
lorenz
parents:
1842
diff
changeset
|
2092 return NULL; |
|
a15f6323c39d
* implemented Vector3. there are no tests yet, so there are bound to be bugs
lorenz
parents:
1842
diff
changeset
|
2093 } |
|
a15f6323c39d
* implemented Vector3. there are no tests yet, so there are bound to be bugs
lorenz
parents:
1842
diff
changeset
|
2094 |
|
1845
7d2a16e6a1d3
rearanged math module. added forward declarations at the beginning.
lorenz
parents:
1844
diff
changeset
|
2095 PySequence_AsVectorCoords(other, other_coords, self->dim); |
|
7d2a16e6a1d3
rearanged math module. added forward declarations at the beginning.
lorenz
parents:
1844
diff
changeset
|
2096 tmp1 = _scalar_product(self->coords, self->coords, self->dim); |
|
7d2a16e6a1d3
rearanged math module. added forward declarations at the beginning.
lorenz
parents:
1844
diff
changeset
|
2097 tmp2 = _scalar_product(other_coords, other_coords, self->dim); |
|
7d2a16e6a1d3
rearanged math module. added forward declarations at the beginning.
lorenz
parents:
1844
diff
changeset
|
2098 angle = acos(_scalar_product(self->coords, other_coords, self->dim) / |
|
7d2a16e6a1d3
rearanged math module. added forward declarations at the beginning.
lorenz
parents:
1844
diff
changeset
|
2099 sqrt(tmp1 * tmp2)); |
|
1844
a15f6323c39d
* implemented Vector3. there are no tests yet, so there are bound to be bugs
lorenz
parents:
1842
diff
changeset
|
2100 return PyFloat_FromDouble(RAD2DEG(angle)); |
|
a15f6323c39d
* implemented Vector3. there are no tests yet, so there are bound to be bugs
lorenz
parents:
1842
diff
changeset
|
2101 } |
|
a15f6323c39d
* implemented Vector3. there are no tests yet, so there are bound to be bugs
lorenz
parents:
1842
diff
changeset
|
2102 |
|
1845
7d2a16e6a1d3
rearanged math module. added forward declarations at the beginning.
lorenz
parents:
1844
diff
changeset
|
2103 static PyObject * |
|
7d2a16e6a1d3
rearanged math module. added forward declarations at the beginning.
lorenz
parents:
1844
diff
changeset
|
2104 vector3_as_spherical(PyVector *self) |
|
7d2a16e6a1d3
rearanged math module. added forward declarations at the beginning.
lorenz
parents:
1844
diff
changeset
|
2105 { |
| 1846 | 2106 double r, theta, phi; |
| 2107 r = sqrt(_scalar_product(self->coords, self->coords, self->dim)); | |
| 2108 theta = acos(self->coords[2] / r); | |
| 2109 phi = atan2(self->coords[1], self->coords[0]); | |
| 2110 return Py_BuildValue("(ddd)", r, theta, phi); | |
| 2111 } | |
| 2112 | |
| 2113 static PyObject * | |
| 2114 vector3_from_spherical(PyVector *self, PyObject *args) | |
| 2115 { | |
| 2116 PyObject *spherical_obj, *r_obj, *theta_obj, *phi_obj; | |
| 2117 double r, theta, phi; | |
| 2118 theta_obj = NULL; | |
| 2119 phi_obj = NULL; | |
| 2120 if (!PyArg_ParseTuple(args, "O:Vector3.from_spherical", &spherical_obj)) { | |
| 2121 return NULL; | |
| 2122 } | |
|
1845
7d2a16e6a1d3
rearanged math module. added forward declarations at the beginning.
lorenz
parents:
1844
diff
changeset
|
2123 |
| 1846 | 2124 if (!PySequence_Check(spherical_obj) || PySequence_Length(spherical_obj) != 3) { |
| 2125 PyErr_SetString(PyExc_TypeError, "3-tuple containing r, theta and phi is expected."); | |
| 2126 return NULL; | |
| 2127 } | |
| 2128 r_obj = PySequence_GetItem(spherical_obj, 0); | |
| 2129 theta_obj = PySequence_GetItem(spherical_obj, 1); | |
| 2130 phi_obj = PySequence_GetItem(spherical_obj, 2); | |
| 2131 if (!PyNumber_Check(r_obj) || !PyNumber_Check(theta_obj) || | |
| 2132 !PyNumber_Check(phi_obj)) { | |
| 2133 PyErr_SetString(PyExc_TypeError, | |
| 2134 "expected numerical values"); | |
| 2135 Py_DECREF(r_obj); | |
| 2136 Py_DECREF(theta_obj); | |
| 2137 Py_DECREF(phi_obj); | |
| 2138 return NULL; | |
| 2139 } | |
| 2140 r = PyFloat_AsDouble(r_obj); | |
| 2141 theta = PyFloat_AsDouble(theta_obj); | |
| 2142 phi = PyFloat_AsDouble(phi_obj); | |
| 2143 Py_DECREF(r_obj); | |
| 2144 Py_DECREF(theta_obj); | |
| 2145 Py_DECREF(phi_obj); | |
| 2146 | |
| 2147 self->coords[0] = r * sin(theta) * cos(phi); | |
| 2148 self->coords[1] = r * sin(theta) * sin(phi); | |
| 2149 self->coords[2] = r * cos(theta); | |
| 2150 | |
| 2151 Py_RETURN_NONE; | |
|
1845
7d2a16e6a1d3
rearanged math module. added forward declarations at the beginning.
lorenz
parents:
1844
diff
changeset
|
2152 } |
|
1844
a15f6323c39d
* implemented Vector3. there are no tests yet, so there are bound to be bugs
lorenz
parents:
1842
diff
changeset
|
2153 |
|
a15f6323c39d
* implemented Vector3. there are no tests yet, so there are bound to be bugs
lorenz
parents:
1842
diff
changeset
|
2154 static PyMethodDef vector3_methods[] = { |
|
a15f6323c39d
* implemented Vector3. there are no tests yet, so there are bound to be bugs
lorenz
parents:
1842
diff
changeset
|
2155 {"length", (PyCFunction)vector_length, METH_NOARGS, |
|
a15f6323c39d
* implemented Vector3. there are no tests yet, so there are bound to be bugs
lorenz
parents:
1842
diff
changeset
|
2156 "returns the length/magnitude of the vector." |
|
a15f6323c39d
* implemented Vector3. there are no tests yet, so there are bound to be bugs
lorenz
parents:
1842
diff
changeset
|
2157 }, |
|
a15f6323c39d
* implemented Vector3. there are no tests yet, so there are bound to be bugs
lorenz
parents:
1842
diff
changeset
|
2158 {"length_squared", (PyCFunction)vector_length_squared, METH_NOARGS, |
|
a15f6323c39d
* implemented Vector3. there are no tests yet, so there are bound to be bugs
lorenz
parents:
1842
diff
changeset
|
2159 "returns the length/magnitude of the vector." |
|
a15f6323c39d
* implemented Vector3. there are no tests yet, so there are bound to be bugs
lorenz
parents:
1842
diff
changeset
|
2160 }, |
|
a15f6323c39d
* implemented Vector3. there are no tests yet, so there are bound to be bugs
lorenz
parents:
1842
diff
changeset
|
2161 {"rotate", (PyCFunction)vector3_rotate, METH_VARARGS, |
|
a15f6323c39d
* implemented Vector3. there are no tests yet, so there are bound to be bugs
lorenz
parents:
1842
diff
changeset
|
2162 "returns a new vector rotated counterclockwise by the angle given in degrees." |
|
a15f6323c39d
* implemented Vector3. there are no tests yet, so there are bound to be bugs
lorenz
parents:
1842
diff
changeset
|
2163 }, |
|
a15f6323c39d
* implemented Vector3. there are no tests yet, so there are bound to be bugs
lorenz
parents:
1842
diff
changeset
|
2164 {"rotate_ip", (PyCFunction)vector3_rotate_ip, METH_VARARGS, |
|
a15f6323c39d
* implemented Vector3. there are no tests yet, so there are bound to be bugs
lorenz
parents:
1842
diff
changeset
|
2165 "rotates the vector counterclockwise by the angle given in degrees." |
|
a15f6323c39d
* implemented Vector3. there are no tests yet, so there are bound to be bugs
lorenz
parents:
1842
diff
changeset
|
2166 }, |
|
1845
7d2a16e6a1d3
rearanged math module. added forward declarations at the beginning.
lorenz
parents:
1844
diff
changeset
|
2167 {"rotate_x", (PyCFunction)vector3_rotate_x, METH_VARARGS, |
|
7d2a16e6a1d3
rearanged math module. added forward declarations at the beginning.
lorenz
parents:
1844
diff
changeset
|
2168 "returns a new vector rotated counterclockwise around the x-axis by the angle given in degrees." |
|
7d2a16e6a1d3
rearanged math module. added forward declarations at the beginning.
lorenz
parents:
1844
diff
changeset
|
2169 }, |
|
7d2a16e6a1d3
rearanged math module. added forward declarations at the beginning.
lorenz
parents:
1844
diff
changeset
|
2170 {"rotate_x_ip", (PyCFunction)vector3_rotate_x_ip, METH_VARARGS, |
|
7d2a16e6a1d3
rearanged math module. added forward declarations at the beginning.
lorenz
parents:
1844
diff
changeset
|
2171 "rotates the vector counterclockwise around the x-axis by the angle given in degrees." |
|
7d2a16e6a1d3
rearanged math module. added forward declarations at the beginning.
lorenz
parents:
1844
diff
changeset
|
2172 }, |
|
7d2a16e6a1d3
rearanged math module. added forward declarations at the beginning.
lorenz
parents:
1844
diff
changeset
|
2173 {"rotate_y", (PyCFunction)vector3_rotate_y, METH_VARARGS, |
|
7d2a16e6a1d3
rearanged math module. added forward declarations at the beginning.
lorenz
parents:
1844
diff
changeset
|
2174 "returns a new vector rotated counterclockwise around the y-axis by the angle given in degrees." |
|
7d2a16e6a1d3
rearanged math module. added forward declarations at the beginning.
lorenz
parents:
1844
diff
changeset
|
2175 }, |
|
7d2a16e6a1d3
rearanged math module. added forward declarations at the beginning.
lorenz
parents:
1844
diff
changeset
|
2176 {"rotate_y_ip", (PyCFunction)vector3_rotate_y_ip, METH_VARARGS, |
|
7d2a16e6a1d3
rearanged math module. added forward declarations at the beginning.
lorenz
parents:
1844
diff
changeset
|
2177 "rotates the vector counterclockwise around the y-axis by the angle given in degrees." |
|
7d2a16e6a1d3
rearanged math module. added forward declarations at the beginning.
lorenz
parents:
1844
diff
changeset
|
2178 }, |
|
7d2a16e6a1d3
rearanged math module. added forward declarations at the beginning.
lorenz
parents:
1844
diff
changeset
|
2179 {"rotate_z", (PyCFunction)vector3_rotate_z, METH_VARARGS, |
|
7d2a16e6a1d3
rearanged math module. added forward declarations at the beginning.
lorenz
parents:
1844
diff
changeset
|
2180 "returns a new vector rotated counterclockwise around the z-axis by the angle given in degrees." |
|
7d2a16e6a1d3
rearanged math module. added forward declarations at the beginning.
lorenz
parents:
1844
diff
changeset
|
2181 }, |
|
7d2a16e6a1d3
rearanged math module. added forward declarations at the beginning.
lorenz
parents:
1844
diff
changeset
|
2182 {"rotate_z_ip", (PyCFunction)vector3_rotate_z_ip, METH_VARARGS, |
|
7d2a16e6a1d3
rearanged math module. added forward declarations at the beginning.
lorenz
parents:
1844
diff
changeset
|
2183 "rotates the vector counterclockwise around the z-axis by the angle given in degrees." |
|
7d2a16e6a1d3
rearanged math module. added forward declarations at the beginning.
lorenz
parents:
1844
diff
changeset
|
2184 }, |
| 1846 | 2185 {"slerp", (PyCFunction)vector_slerp, METH_VARARGS, |
|
1847
ed12994f377c
added "lerp()" to Vector2 and Vector3 to provide a linear interpolation.
lorenz
parents:
1846
diff
changeset
|
2186 "Interpolates spherically to a given vector in a given number of steps." |
|
ed12994f377c
added "lerp()" to Vector2 and Vector3 to provide a linear interpolation.
lorenz
parents:
1846
diff
changeset
|
2187 }, |
|
ed12994f377c
added "lerp()" to Vector2 and Vector3 to provide a linear interpolation.
lorenz
parents:
1846
diff
changeset
|
2188 {"lerp", (PyCFunction)vector_lerp, METH_VARARGS, |
|
ed12994f377c
added "lerp()" to Vector2 and Vector3 to provide a linear interpolation.
lorenz
parents:
1846
diff
changeset
|
2189 "Interpolates linear to a given vector in a given number of steps." |
| 1846 | 2190 }, |
|
1844
a15f6323c39d
* implemented Vector3. there are no tests yet, so there are bound to be bugs
lorenz
parents:
1842
diff
changeset
|
2191 {"normalize", (PyCFunction)vector_normalize, METH_NOARGS, |
|
a15f6323c39d
* implemented Vector3. there are no tests yet, so there are bound to be bugs
lorenz
parents:
1842
diff
changeset
|
2192 "returns a vector that has length == 1 and the same direction as self." |
|
a15f6323c39d
* implemented Vector3. there are no tests yet, so there are bound to be bugs
lorenz
parents:
1842
diff
changeset
|
2193 }, |
|
a15f6323c39d
* implemented Vector3. there are no tests yet, so there are bound to be bugs
lorenz
parents:
1842
diff
changeset
|
2194 {"normalize_ip", (PyCFunction)vector_normalize_ip, METH_NOARGS, |
|
a15f6323c39d
* implemented Vector3. there are no tests yet, so there are bound to be bugs
lorenz
parents:
1842
diff
changeset
|
2195 "Normalizes the vector so that it has length == 1." |
|
a15f6323c39d
* implemented Vector3. there are no tests yet, so there are bound to be bugs
lorenz
parents:
1842
diff
changeset
|
2196 }, |
|
a15f6323c39d
* implemented Vector3. there are no tests yet, so there are bound to be bugs
lorenz
parents:
1842
diff
changeset
|
2197 {"is_normalized", (PyCFunction)vector_is_normalized, METH_NOARGS, |
|
a15f6323c39d
* implemented Vector3. there are no tests yet, so there are bound to be bugs
lorenz
parents:
1842
diff
changeset
|
2198 "returns True if the vector has length == 1. otherwise it returns False." |
|
a15f6323c39d
* implemented Vector3. there are no tests yet, so there are bound to be bugs
lorenz
parents:
1842
diff
changeset
|
2199 }, |
|
a15f6323c39d
* implemented Vector3. there are no tests yet, so there are bound to be bugs
lorenz
parents:
1842
diff
changeset
|
2200 {"cross", (PyCFunction)vector3_cross, METH_O, |
|
a15f6323c39d
* implemented Vector3. there are no tests yet, so there are bound to be bugs
lorenz
parents:
1842
diff
changeset
|
2201 "calculates the cross product." |
|
a15f6323c39d
* implemented Vector3. there are no tests yet, so there are bound to be bugs
lorenz
parents:
1842
diff
changeset
|
2202 }, |
|
a15f6323c39d
* implemented Vector3. there are no tests yet, so there are bound to be bugs
lorenz
parents:
1842
diff
changeset
|
2203 {"dot", (PyCFunction)vector_dot, METH_O, |
|
a15f6323c39d
* implemented Vector3. there are no tests yet, so there are bound to be bugs
lorenz
parents:
1842
diff
changeset
|
2204 "calculates the dot product." |
|
a15f6323c39d
* implemented Vector3. there are no tests yet, so there are bound to be bugs
lorenz
parents:
1842
diff
changeset
|
2205 }, |
|
a15f6323c39d
* implemented Vector3. there are no tests yet, so there are bound to be bugs
lorenz
parents:
1842
diff
changeset
|
2206 {"angle_to", (PyCFunction)vector3_angle_to, METH_O, |
|
a15f6323c39d
* implemented Vector3. there are no tests yet, so there are bound to be bugs
lorenz
parents:
1842
diff
changeset
|
2207 "returns the angle between self and the given vector." |
|
a15f6323c39d
* implemented Vector3. there are no tests yet, so there are bound to be bugs
lorenz
parents:
1842
diff
changeset
|
2208 }, |
|
a15f6323c39d
* implemented Vector3. there are no tests yet, so there are bound to be bugs
lorenz
parents:
1842
diff
changeset
|
2209 {"scale_to_length", (PyCFunction)vector_scale_to_length, METH_O, |
|
a15f6323c39d
* implemented Vector3. there are no tests yet, so there are bound to be bugs
lorenz
parents:
1842
diff
changeset
|
2210 "scales the vector to the given length." |
|
a15f6323c39d
* implemented Vector3. there are no tests yet, so there are bound to be bugs
lorenz
parents:
1842
diff
changeset
|
2211 }, |
|
a15f6323c39d
* implemented Vector3. there are no tests yet, so there are bound to be bugs
lorenz
parents:
1842
diff
changeset
|
2212 {"reflect", (PyCFunction)vector_reflect, METH_O, |
|
a15f6323c39d
* implemented Vector3. there are no tests yet, so there are bound to be bugs
lorenz
parents:
1842
diff
changeset
|
2213 "reflects the vector on the surface characterized by the given normal." |
|
a15f6323c39d
* implemented Vector3. there are no tests yet, so there are bound to be bugs
lorenz
parents:
1842
diff
changeset
|
2214 }, |
|
a15f6323c39d
* implemented Vector3. there are no tests yet, so there are bound to be bugs
lorenz
parents:
1842
diff
changeset
|
2215 {"reflect_ip", (PyCFunction)vector_reflect_ip, METH_O, |
|
a15f6323c39d
* implemented Vector3. there are no tests yet, so there are bound to be bugs
lorenz
parents:
1842
diff
changeset
|
2216 "reflects the vector in-place on the surface characterized by the given normal." |
|
a15f6323c39d
* implemented Vector3. there are no tests yet, so there are bound to be bugs
lorenz
parents:
1842
diff
changeset
|
2217 }, |
|
a15f6323c39d
* implemented Vector3. there are no tests yet, so there are bound to be bugs
lorenz
parents:
1842
diff
changeset
|
2218 {"distance_to", (PyCFunction)vector_distance_to, METH_O, |
|
a15f6323c39d
* implemented Vector3. there are no tests yet, so there are bound to be bugs
lorenz
parents:
1842
diff
changeset
|
2219 "returns the distance to the given vector." |
|
a15f6323c39d
* implemented Vector3. there are no tests yet, so there are bound to be bugs
lorenz
parents:
1842
diff
changeset
|
2220 }, |
|
a15f6323c39d
* implemented Vector3. there are no tests yet, so there are bound to be bugs
lorenz
parents:
1842
diff
changeset
|
2221 {"distance_squared_to", (PyCFunction)vector_distance_squared_to, METH_O, |
|
a15f6323c39d
* implemented Vector3. there are no tests yet, so there are bound to be bugs
lorenz
parents:
1842
diff
changeset
|
2222 "returns the squared distance to the given vector." |
|
a15f6323c39d
* implemented Vector3. there are no tests yet, so there are bound to be bugs
lorenz
parents:
1842
diff
changeset
|
2223 }, |
|
a15f6323c39d
* implemented Vector3. there are no tests yet, so there are bound to be bugs
lorenz
parents:
1842
diff
changeset
|
2224 {"elementwise", (PyCFunction)vector_elementwise, METH_NOARGS, |
|
a15f6323c39d
* implemented Vector3. there are no tests yet, so there are bound to be bugs
lorenz
parents:
1842
diff
changeset
|
2225 "applies the following operation to each element of the vector." |
|
a15f6323c39d
* implemented Vector3. there are no tests yet, so there are bound to be bugs
lorenz
parents:
1842
diff
changeset
|
2226 }, |
| 1846 | 2227 {"as_spherical", (PyCFunction)vector3_as_spherical, METH_NOARGS, |
| 2228 "returns a tuple (r, theta, phi) where r is the radial distance, theta is the inclination angle and phi is the azimutal angle." | |
| 2229 }, | |
| 2230 {"from_spherical", (PyCFunction)vector3_from_spherical, METH_VARARGS, | |
| 2231 "sets x, y and z from a tuple (r, theta, phi) where r is the radial distance, theta is the inclination angle and phi is the azimutal angle." | |
| 2232 }, | |
|
1844
a15f6323c39d
* implemented Vector3. there are no tests yet, so there are bound to be bugs
lorenz
parents:
1842
diff
changeset
|
2233 |
|
a15f6323c39d
* implemented Vector3. there are no tests yet, so there are bound to be bugs
lorenz
parents:
1842
diff
changeset
|
2234 {NULL} /* Sentinel */ |
|
a15f6323c39d
* implemented Vector3. there are no tests yet, so there are bound to be bugs
lorenz
parents:
1842
diff
changeset
|
2235 }; |
|
a15f6323c39d
* implemented Vector3. there are no tests yet, so there are bound to be bugs
lorenz
parents:
1842
diff
changeset
|
2236 |
|
a15f6323c39d
* implemented Vector3. there are no tests yet, so there are bound to be bugs
lorenz
parents:
1842
diff
changeset
|
2237 static PyGetSetDef vector3_getsets[] = { |
|
a15f6323c39d
* implemented Vector3. there are no tests yet, so there are bound to be bugs
lorenz
parents:
1842
diff
changeset
|
2238 { "x", (getter)vector_getx, (setter)vector_setx, NULL, NULL }, |
|
a15f6323c39d
* implemented Vector3. there are no tests yet, so there are bound to be bugs
lorenz
parents:
1842
diff
changeset
|
2239 { "y", (getter)vector_gety, (setter)vector_sety, NULL, NULL }, |
|
a15f6323c39d
* implemented Vector3. there are no tests yet, so there are bound to be bugs
lorenz
parents:
1842
diff
changeset
|
2240 { "z", (getter)vector_getz, (setter)vector_setz, NULL, NULL }, |
|
a15f6323c39d
* implemented Vector3. there are no tests yet, so there are bound to be bugs
lorenz
parents:
1842
diff
changeset
|
2241 { NULL, 0, NULL, NULL, NULL } /* Sentinel */ |
|
a15f6323c39d
* implemented Vector3. there are no tests yet, so there are bound to be bugs
lorenz
parents:
1842
diff
changeset
|
2242 }; |
|
a15f6323c39d
* implemented Vector3. there are no tests yet, so there are bound to be bugs
lorenz
parents:
1842
diff
changeset
|
2243 |
|
1838
e4fd799c194b
added methods distance_to and distance_squared_to to the vector class
lorenz
parents:
1837
diff
changeset
|
2244 /******************************** |
|
1844
a15f6323c39d
* implemented Vector3. there are no tests yet, so there are bound to be bugs
lorenz
parents:
1842
diff
changeset
|
2245 * PyVector3 type definition |
|
a15f6323c39d
* implemented Vector3. there are no tests yet, so there are bound to be bugs
lorenz
parents:
1842
diff
changeset
|
2246 ********************************/ |
|
a15f6323c39d
* implemented Vector3. there are no tests yet, so there are bound to be bugs
lorenz
parents:
1842
diff
changeset
|
2247 |
|
a15f6323c39d
* implemented Vector3. there are no tests yet, so there are bound to be bugs
lorenz
parents:
1842
diff
changeset
|
2248 static PyTypeObject PyVector3_Type = { |
|
a15f6323c39d
* implemented Vector3. there are no tests yet, so there are bound to be bugs
lorenz
parents:
1842
diff
changeset
|
2249 PyObject_HEAD_INIT(NULL) |
|
a15f6323c39d
* implemented Vector3. there are no tests yet, so there are bound to be bugs
lorenz
parents:
1842
diff
changeset
|
2250 0, /* ob_size */ |
|
a15f6323c39d
* implemented Vector3. there are no tests yet, so there are bound to be bugs
lorenz
parents:
1842
diff
changeset
|
2251 "pygame.math.Vector3", /* tp_name */ |
|
a15f6323c39d
* implemented Vector3. there are no tests yet, so there are bound to be bugs
lorenz
parents:
1842
diff
changeset
|
2252 sizeof(PyVector), /* tp_basicsize */ |
|
a15f6323c39d
* implemented Vector3. there are no tests yet, so there are bound to be bugs
lorenz
parents:
1842
diff
changeset
|
2253 0, /* tp_itemsize */ |
|
a15f6323c39d
* implemented Vector3. there are no tests yet, so there are bound to be bugs
lorenz
parents:
1842
diff
changeset
|
2254 /* Methods to implement standard operations */ |
|
a15f6323c39d
* implemented Vector3. there are no tests yet, so there are bound to be bugs
lorenz
parents:
1842
diff
changeset
|
2255 (destructor)vector_dealloc, /* tp_dealloc */ |
|
a15f6323c39d
* implemented Vector3. there are no tests yet, so there are bound to be bugs
lorenz
parents:
1842
diff
changeset
|
2256 0, /* tp_print */ |
|
a15f6323c39d
* implemented Vector3. there are no tests yet, so there are bound to be bugs
lorenz
parents:
1842
diff
changeset
|
2257 0, /* tp_getattr */ |
|
a15f6323c39d
* implemented Vector3. there are no tests yet, so there are bound to be bugs
lorenz
parents:
1842
diff
changeset
|
2258 0, /* tp_setattr */ |
|
a15f6323c39d
* implemented Vector3. there are no tests yet, so there are bound to be bugs
lorenz
parents:
1842
diff
changeset
|
2259 0, /* tp_compare */ |
|
a15f6323c39d
* implemented Vector3. there are no tests yet, so there are bound to be bugs
lorenz
parents:
1842
diff
changeset
|
2260 (reprfunc)vector_repr, /* tp_repr */ |
|
a15f6323c39d
* implemented Vector3. there are no tests yet, so there are bound to be bugs
lorenz
parents:
1842
diff
changeset
|
2261 /* Method suites for standard classes */ |
|
a15f6323c39d
* implemented Vector3. there are no tests yet, so there are bound to be bugs
lorenz
parents:
1842
diff
changeset
|
2262 &vector_as_number, /* tp_as_number */ |
|
a15f6323c39d
* implemented Vector3. there are no tests yet, so there are bound to be bugs
lorenz
parents:
1842
diff
changeset
|
2263 &vector_as_sequence, /* tp_as_sequence */ |
|
a15f6323c39d
* implemented Vector3. there are no tests yet, so there are bound to be bugs
lorenz
parents:
1842
diff
changeset
|
2264 0, /* tp_as_mapping */ |
|
a15f6323c39d
* implemented Vector3. there are no tests yet, so there are bound to be bugs
lorenz
parents:
1842
diff
changeset
|
2265 /* More standard operations (here for binary compatibility) */ |
|
a15f6323c39d
* implemented Vector3. there are no tests yet, so there are bound to be bugs
lorenz
parents:
1842
diff
changeset
|
2266 0, /* tp_hash */ |
|
a15f6323c39d
* implemented Vector3. there are no tests yet, so there are bound to be bugs
lorenz
parents:
1842
diff
changeset
|
2267 0, /* tp_call */ |
|
a15f6323c39d
* implemented Vector3. there are no tests yet, so there are bound to be bugs
lorenz
parents:
1842
diff
changeset
|
2268 (reprfunc)vector_str, /* tp_str */ |
|
1845
7d2a16e6a1d3
rearanged math module. added forward declarations at the beginning.
lorenz
parents:
1844
diff
changeset
|
2269 (getattrofunc)vector_getAttr_swizzle, /* tp_getattro */ |
|
7d2a16e6a1d3
rearanged math module. added forward declarations at the beginning.
lorenz
parents:
1844
diff
changeset
|
2270 (setattrofunc)vector_setAttr_swizzle, /* tp_setattro */ |
|
1844
a15f6323c39d
* implemented Vector3. there are no tests yet, so there are bound to be bugs
lorenz
parents:
1842
diff
changeset
|
2271 /* Functions to access object as input/output buffer */ |
|
a15f6323c39d
* implemented Vector3. there are no tests yet, so there are bound to be bugs
lorenz
parents:
1842
diff
changeset
|
2272 0, /* tp_as_buffer */ |
|
a15f6323c39d
* implemented Vector3. there are no tests yet, so there are bound to be bugs
lorenz
parents:
1842
diff
changeset
|
2273 /* Flags to define presence of optional/expanded features */ |
|
a15f6323c39d
* implemented Vector3. there are no tests yet, so there are bound to be bugs
lorenz
parents:
1842
diff
changeset
|
2274 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | |
|
a15f6323c39d
* implemented Vector3. there are no tests yet, so there are bound to be bugs
lorenz
parents:
1842
diff
changeset
|
2275 Py_TPFLAGS_CHECKTYPES, /* tp_flags */ |
|
a15f6323c39d
* implemented Vector3. there are no tests yet, so there are bound to be bugs
lorenz
parents:
1842
diff
changeset
|
2276 /* Documentation string */ |
|
a15f6323c39d
* implemented Vector3. there are no tests yet, so there are bound to be bugs
lorenz
parents:
1842
diff
changeset
|
2277 DOC_PYGAMEVECTOR3, /* tp_doc */ |
|
a15f6323c39d
* implemented Vector3. there are no tests yet, so there are bound to be bugs
lorenz
parents:
1842
diff
changeset
|
2278 |
|
a15f6323c39d
* implemented Vector3. there are no tests yet, so there are bound to be bugs
lorenz
parents:
1842
diff
changeset
|
2279 /* Assigned meaning in release 2.0 */ |
|
a15f6323c39d
* implemented Vector3. there are no tests yet, so there are bound to be bugs
lorenz
parents:
1842
diff
changeset
|
2280 /* call function for all accessible objects */ |
|
a15f6323c39d
* implemented Vector3. there are no tests yet, so there are bound to be bugs
lorenz
parents:
1842
diff
changeset
|
2281 0, /* tp_traverse */ |
|
a15f6323c39d
* implemented Vector3. there are no tests yet, so there are bound to be bugs
lorenz
parents:
1842
diff
changeset
|
2282 /* delete references to contained objects */ |
|
a15f6323c39d
* implemented Vector3. there are no tests yet, so there are bound to be bugs
lorenz
parents:
1842
diff
changeset
|
2283 0, /* tp_clear */ |
|
a15f6323c39d
* implemented Vector3. there are no tests yet, so there are bound to be bugs
lorenz
parents:
1842
diff
changeset
|
2284 |
|
a15f6323c39d
* implemented Vector3. there are no tests yet, so there are bound to be bugs
lorenz
parents:
1842
diff
changeset
|
2285 /* Assigned meaning in release 2.1 */ |
|
a15f6323c39d
* implemented Vector3. there are no tests yet, so there are bound to be bugs
lorenz
parents:
1842
diff
changeset
|
2286 /* rich comparisons */ |
|
a15f6323c39d
* implemented Vector3. there are no tests yet, so there are bound to be bugs
lorenz
parents:
1842
diff
changeset
|
2287 (richcmpfunc)vector_richcompare, /* tp_richcompare */ |
|
a15f6323c39d
* implemented Vector3. there are no tests yet, so there are bound to be bugs
lorenz
parents:
1842
diff
changeset
|
2288 /* weak reference enabler */ |
|
a15f6323c39d
* implemented Vector3. there are no tests yet, so there are bound to be bugs
lorenz
parents:
1842
diff
changeset
|
2289 0, /* tp_weaklistoffset */ |
|
a15f6323c39d
* implemented Vector3. there are no tests yet, so there are bound to be bugs
lorenz
parents:
1842
diff
changeset
|
2290 |
|
a15f6323c39d
* implemented Vector3. there are no tests yet, so there are bound to be bugs
lorenz
parents:
1842
diff
changeset
|
2291 /* Added in release 2.2 */ |
|
a15f6323c39d
* implemented Vector3. there are no tests yet, so there are bound to be bugs
lorenz
parents:
1842
diff
changeset
|
2292 /* Iterators */ |
|
a15f6323c39d
* implemented Vector3. there are no tests yet, so there are bound to be bugs
lorenz
parents:
1842
diff
changeset
|
2293 vector_iter, /* tp_iter */ |
|
a15f6323c39d
* implemented Vector3. there are no tests yet, so there are bound to be bugs
lorenz
parents:
1842
diff
changeset
|
2294 0, /* tp_iternext */ |
|
a15f6323c39d
* implemented Vector3. there are no tests yet, so there are bound to be bugs
lorenz
parents:
1842
diff
changeset
|
2295 /* Attribute descriptor and subclassing stuff */ |
|
a15f6323c39d
* implemented Vector3. there are no tests yet, so there are bound to be bugs
lorenz
parents:
1842
diff
changeset
|
2296 vector3_methods, /* tp_methods */ |
|
a15f6323c39d
* implemented Vector3. there are no tests yet, so there are bound to be bugs
lorenz
parents:
1842
diff
changeset
|
2297 vector_members, /* tp_members */ |
|
a15f6323c39d
* implemented Vector3. there are no tests yet, so there are bound to be bugs
lorenz
parents:
1842
diff
changeset
|
2298 vector3_getsets, /* tp_getset */ |
|
a15f6323c39d
* implemented Vector3. there are no tests yet, so there are bound to be bugs
lorenz
parents:
1842
diff
changeset
|
2299 0, /* tp_base */ |
|
a15f6323c39d
* implemented Vector3. there are no tests yet, so there are bound to be bugs
lorenz
parents:
1842
diff
changeset
|
2300 0, /* tp_dict */ |
|
a15f6323c39d
* implemented Vector3. there are no tests yet, so there are bound to be bugs
lorenz
parents:
1842
diff
changeset
|
2301 0, /* tp_descr_get */ |
|
a15f6323c39d
* implemented Vector3. there are no tests yet, so there are bound to be bugs
lorenz
parents:
1842
diff
changeset
|
2302 0, /* tp_descr_set */ |
|
a15f6323c39d
* implemented Vector3. there are no tests yet, so there are bound to be bugs
lorenz
parents:
1842
diff
changeset
|
2303 0, /* tp_dictoffset */ |
|
a15f6323c39d
* implemented Vector3. there are no tests yet, so there are bound to be bugs
lorenz
parents:
1842
diff
changeset
|
2304 (initproc)vector3_init, /* tp_init */ |
|
a15f6323c39d
* implemented Vector3. there are no tests yet, so there are bound to be bugs
lorenz
parents:
1842
diff
changeset
|
2305 0, /* tp_alloc */ |
|
a15f6323c39d
* implemented Vector3. there are no tests yet, so there are bound to be bugs
lorenz
parents:
1842
diff
changeset
|
2306 (newfunc)vector3_new, /* tp_new */ |
|
a15f6323c39d
* implemented Vector3. there are no tests yet, so there are bound to be bugs
lorenz
parents:
1842
diff
changeset
|
2307 0, /* tp_free */ |
|
a15f6323c39d
* implemented Vector3. there are no tests yet, so there are bound to be bugs
lorenz
parents:
1842
diff
changeset
|
2308 0, /* tp_is_gc */ |
|
a15f6323c39d
* implemented Vector3. there are no tests yet, so there are bound to be bugs
lorenz
parents:
1842
diff
changeset
|
2309 0, /* tp_bases */ |
|
a15f6323c39d
* implemented Vector3. there are no tests yet, so there are bound to be bugs
lorenz
parents:
1842
diff
changeset
|
2310 0, /* tp_mro */ |
|
a15f6323c39d
* implemented Vector3. there are no tests yet, so there are bound to be bugs
lorenz
parents:
1842
diff
changeset
|
2311 0, /* tp_cache */ |
|
a15f6323c39d
* implemented Vector3. there are no tests yet, so there are bound to be bugs
lorenz
parents:
1842
diff
changeset
|
2312 0, /* tp_subclasses */ |
|
a15f6323c39d
* implemented Vector3. there are no tests yet, so there are bound to be bugs
lorenz
parents:
1842
diff
changeset
|
2313 0, /* tp_weaklist */ |
|
a15f6323c39d
* implemented Vector3. there are no tests yet, so there are bound to be bugs
lorenz
parents:
1842
diff
changeset
|
2314 }; |
|
a15f6323c39d
* implemented Vector3. there are no tests yet, so there are bound to be bugs
lorenz
parents:
1842
diff
changeset
|
2315 |
|
a15f6323c39d
* implemented Vector3. there are no tests yet, so there are bound to be bugs
lorenz
parents:
1842
diff
changeset
|
2316 |
|
a15f6323c39d
* implemented Vector3. there are no tests yet, so there are bound to be bugs
lorenz
parents:
1842
diff
changeset
|
2317 |
|
a15f6323c39d
* implemented Vector3. there are no tests yet, so there are bound to be bugs
lorenz
parents:
1842
diff
changeset
|
2318 |
|
a15f6323c39d
* implemented Vector3. there are no tests yet, so there are bound to be bugs
lorenz
parents:
1842
diff
changeset
|
2319 |
|
a15f6323c39d
* implemented Vector3. there are no tests yet, so there are bound to be bugs
lorenz
parents:
1842
diff
changeset
|
2320 |
|
a15f6323c39d
* implemented Vector3. there are no tests yet, so there are bound to be bugs
lorenz
parents:
1842
diff
changeset
|
2321 |
|
a15f6323c39d
* implemented Vector3. there are no tests yet, so there are bound to be bugs
lorenz
parents:
1842
diff
changeset
|
2322 |
|
a15f6323c39d
* implemented Vector3. there are no tests yet, so there are bound to be bugs
lorenz
parents:
1842
diff
changeset
|
2323 |
|
a15f6323c39d
* implemented Vector3. there are no tests yet, so there are bound to be bugs
lorenz
parents:
1842
diff
changeset
|
2324 |
|
a15f6323c39d
* implemented Vector3. there are no tests yet, so there are bound to be bugs
lorenz
parents:
1842
diff
changeset
|
2325 |
|
a15f6323c39d
* implemented Vector3. there are no tests yet, so there are bound to be bugs
lorenz
parents:
1842
diff
changeset
|
2326 |
|
1845
7d2a16e6a1d3
rearanged math module. added forward declarations at the beginning.
lorenz
parents:
1844
diff
changeset
|
2327 /******************************************** |
|
1844
a15f6323c39d
* implemented Vector3. there are no tests yet, so there are bound to be bugs
lorenz
parents:
1842
diff
changeset
|
2328 * PyVectorIterator type definition |
|
1845
7d2a16e6a1d3
rearanged math module. added forward declarations at the beginning.
lorenz
parents:
1844
diff
changeset
|
2329 ********************************************/ |
|
1838
e4fd799c194b
added methods distance_to and distance_squared_to to the vector class
lorenz
parents:
1837
diff
changeset
|
2330 |
|
e4fd799c194b
added methods distance_to and distance_squared_to to the vector class
lorenz
parents:
1837
diff
changeset
|
2331 static void |
|
e4fd799c194b
added methods distance_to and distance_squared_to to the vector class
lorenz
parents:
1837
diff
changeset
|
2332 vectoriter_dealloc(vectoriter *it) |
|
e4fd799c194b
added methods distance_to and distance_squared_to to the vector class
lorenz
parents:
1837
diff
changeset
|
2333 { |
|
e4fd799c194b
added methods distance_to and distance_squared_to to the vector class
lorenz
parents:
1837
diff
changeset
|
2334 Py_XDECREF(it->vec); |
|
e4fd799c194b
added methods distance_to and distance_squared_to to the vector class
lorenz
parents:
1837
diff
changeset
|
2335 PyObject_Del(it); |
|
e4fd799c194b
added methods distance_to and distance_squared_to to the vector class
lorenz
parents:
1837
diff
changeset
|
2336 } |
|
e4fd799c194b
added methods distance_to and distance_squared_to to the vector class
lorenz
parents:
1837
diff
changeset
|
2337 |
|
e4fd799c194b
added methods distance_to and distance_squared_to to the vector class
lorenz
parents:
1837
diff
changeset
|
2338 static PyObject * |
|
e4fd799c194b
added methods distance_to and distance_squared_to to the vector class
lorenz
parents:
1837
diff
changeset
|
2339 vectoriter_next(vectoriter *it) |
|
e4fd799c194b
added methods distance_to and distance_squared_to to the vector class
lorenz
parents:
1837
diff
changeset
|
2340 { |
|
e4fd799c194b
added methods distance_to and distance_squared_to to the vector class
lorenz
parents:
1837
diff
changeset
|
2341 assert(it != NULL); |
|
e4fd799c194b
added methods distance_to and distance_squared_to to the vector class
lorenz
parents:
1837
diff
changeset
|
2342 if (it->vec == NULL) |
|
e4fd799c194b
added methods distance_to and distance_squared_to to the vector class
lorenz
parents:
1837
diff
changeset
|
2343 return NULL; |
|
e4fd799c194b
added methods distance_to and distance_squared_to to the vector class
lorenz
parents:
1837
diff
changeset
|
2344 assert(PyVector_Check(it->vec)); |
|
e4fd799c194b
added methods distance_to and distance_squared_to to the vector class
lorenz
parents:
1837
diff
changeset
|
2345 |
|
e4fd799c194b
added methods distance_to and distance_squared_to to the vector class
lorenz
parents:
1837
diff
changeset
|
2346 if (it->it_index < it->vec->dim) { |
|
e4fd799c194b
added methods distance_to and distance_squared_to to the vector class
lorenz
parents:
1837
diff
changeset
|
2347 double item = it->vec->coords[it->it_index]; |
|
e4fd799c194b
added methods distance_to and distance_squared_to to the vector class
lorenz
parents:
1837
diff
changeset
|
2348 ++(it->it_index); |
|
e4fd799c194b
added methods distance_to and distance_squared_to to the vector class
lorenz
parents:
1837
diff
changeset
|
2349 return PyFloat_FromDouble(item); |
|
e4fd799c194b
added methods distance_to and distance_squared_to to the vector class
lorenz
parents:
1837
diff
changeset
|
2350 } |
|
e4fd799c194b
added methods distance_to and distance_squared_to to the vector class
lorenz
parents:
1837
diff
changeset
|
2351 |
|
e4fd799c194b
added methods distance_to and distance_squared_to to the vector class
lorenz
parents:
1837
diff
changeset
|
2352 Py_DECREF(it->vec); |
|
e4fd799c194b
added methods distance_to and distance_squared_to to the vector class
lorenz
parents:
1837
diff
changeset
|
2353 it->vec = NULL; |
|
e4fd799c194b
added methods distance_to and distance_squared_to to the vector class
lorenz
parents:
1837
diff
changeset
|
2354 return NULL; |
|
e4fd799c194b
added methods distance_to and distance_squared_to to the vector class
lorenz
parents:
1837
diff
changeset
|
2355 } |
|
e4fd799c194b
added methods distance_to and distance_squared_to to the vector class
lorenz
parents:
1837
diff
changeset
|
2356 |
|
e4fd799c194b
added methods distance_to and distance_squared_to to the vector class
lorenz
parents:
1837
diff
changeset
|
2357 static PyObject * |
|
e4fd799c194b
added methods distance_to and distance_squared_to to the vector class
lorenz
parents:
1837
diff
changeset
|
2358 vectoriter_len(vectoriter *it) |
|
e4fd799c194b
added methods distance_to and distance_squared_to to the vector class
lorenz
parents:
1837
diff
changeset
|
2359 { |
| 1846 | 2360 Py_ssize_t len = 0; |
|
1838
e4fd799c194b
added methods distance_to and distance_squared_to to the vector class
lorenz
parents:
1837
diff
changeset
|
2361 if (it && it->vec) { |
|
e4fd799c194b
added methods distance_to and distance_squared_to to the vector class
lorenz
parents:
1837
diff
changeset
|
2362 len = it->vec->dim - it->it_index; |
|
e4fd799c194b
added methods distance_to and distance_squared_to to the vector class
lorenz
parents:
1837
diff
changeset
|
2363 } |
| 1846 | 2364 return PyInt_FromSsize_t(len); |
|
1838
e4fd799c194b
added methods distance_to and distance_squared_to to the vector class
lorenz
parents:
1837
diff
changeset
|
2365 } |
|
e4fd799c194b
added methods distance_to and distance_squared_to to the vector class
lorenz
parents:
1837
diff
changeset
|
2366 |
|
e4fd799c194b
added methods distance_to and distance_squared_to to the vector class
lorenz
parents:
1837
diff
changeset
|
2367 static PyMethodDef vectoriter_methods[] = { |
|
e4fd799c194b
added methods distance_to and distance_squared_to to the vector class
lorenz
parents:
1837
diff
changeset
|
2368 {"__length_hint__", (PyCFunction)vectoriter_len, METH_NOARGS, |
|
e4fd799c194b
added methods distance_to and distance_squared_to to the vector class
lorenz
parents:
1837
diff
changeset
|
2369 }, |
|
e4fd799c194b
added methods distance_to and distance_squared_to to the vector class
lorenz
parents:
1837
diff
changeset
|
2370 {NULL, NULL} /* sentinel */ |
|
e4fd799c194b
added methods distance_to and distance_squared_to to the vector class
lorenz
parents:
1837
diff
changeset
|
2371 }; |
|
e4fd799c194b
added methods distance_to and distance_squared_to to the vector class
lorenz
parents:
1837
diff
changeset
|
2372 |
|
e4fd799c194b
added methods distance_to and distance_squared_to to the vector class
lorenz
parents:
1837
diff
changeset
|
2373 static PyTypeObject PyVectorIter_Type = { |
|
e4fd799c194b
added methods distance_to and distance_squared_to to the vector class
lorenz
parents:
1837
diff
changeset
|
2374 PyObject_HEAD_INIT(NULL) |
|
e4fd799c194b
added methods distance_to and distance_squared_to to the vector class
lorenz
parents:
1837
diff
changeset
|
2375 0, /* ob_size */ |
|
e4fd799c194b
added methods distance_to and distance_squared_to to the vector class
lorenz
parents:
1837
diff
changeset
|
2376 "pygame.math.VectorIterator", /* tp_name */ |
|
e4fd799c194b
added methods distance_to and distance_squared_to to the vector class
lorenz
parents:
1837
diff
changeset
|
2377 sizeof(vectoriter), /* tp_basicsize */ |
|
e4fd799c194b
added methods distance_to and distance_squared_to to the vector class
lorenz
parents:
1837
diff
changeset
|
2378 0, /* tp_itemsize */ |
|
e4fd799c194b
added methods distance_to and distance_squared_to to the vector class
lorenz
parents:
1837
diff
changeset
|
2379 (destructor)vectoriter_dealloc, /* tp_dealloc */ |
|
e4fd799c194b
added methods distance_to and distance_squared_to to the vector class
lorenz
parents:
1837
diff
changeset
|
2380 0, /* tp_print */ |
|
e4fd799c194b
added methods distance_to and distance_squared_to to the vector class
lorenz
parents:
1837
diff
changeset
|
2381 0, /* tp_getattr */ |
|
e4fd799c194b
added methods distance_to and distance_squared_to to the vector class
lorenz
parents:
1837
diff
changeset
|
2382 0, /* tp_setattr */ |
|
e4fd799c194b
added methods distance_to and distance_squared_to to the vector class
lorenz
parents:
1837
diff
changeset
|
2383 0, /* tp_compare */ |
|
e4fd799c194b
added methods distance_to and distance_squared_to to the vector class
lorenz
parents:
1837
diff
changeset
|
2384 0, /* tp_repr */ |
|
e4fd799c194b
added methods distance_to and distance_squared_to to the vector class
lorenz
parents:
1837
diff
changeset
|
2385 0, /* tp_as_number */ |
|
e4fd799c194b
added methods distance_to and distance_squared_to to the vector class
lorenz
parents:
1837
diff
changeset
|
2386 0, /* tp_as_sequence */ |
|
e4fd799c194b
added methods distance_to and distance_squared_to to the vector class
lorenz
parents:
1837
diff
changeset
|
2387 0, /* tp_as_mapping */ |
|
e4fd799c194b
added methods distance_to and distance_squared_to to the vector class
lorenz
parents:
1837
diff
changeset
|
2388 0, /* tp_hash */ |
|
e4fd799c194b
added methods distance_to and distance_squared_to to the vector class
lorenz
parents:
1837
diff
changeset
|
2389 0, /* tp_call */ |
|
e4fd799c194b
added methods distance_to and distance_squared_to to the vector class
lorenz
parents:
1837
diff
changeset
|
2390 0, /* tp_str */ |
|
e4fd799c194b
added methods distance_to and distance_squared_to to the vector class
lorenz
parents:
1837
diff
changeset
|
2391 PyObject_GenericGetAttr, /* tp_getattro */ |
|
e4fd799c194b
added methods distance_to and distance_squared_to to the vector class
lorenz
parents:
1837
diff
changeset
|
2392 0, /* tp_setattro */ |
|
e4fd799c194b
added methods distance_to and distance_squared_to to the vector class
lorenz
parents:
1837
diff
changeset
|
2393 0, /* tp_as_buffer */ |
|
e4fd799c194b
added methods distance_to and distance_squared_to to the vector class
lorenz
parents:
1837
diff
changeset
|
2394 Py_TPFLAGS_DEFAULT, /* tp_flags */ |
|
e4fd799c194b
added methods distance_to and distance_squared_to to the vector class
lorenz
parents:
1837
diff
changeset
|
2395 0, /* tp_doc */ |
|
e4fd799c194b
added methods distance_to and distance_squared_to to the vector class
lorenz
parents:
1837
diff
changeset
|
2396 0, /* tp_traverse */ |
|
e4fd799c194b
added methods distance_to and distance_squared_to to the vector class
lorenz
parents:
1837
diff
changeset
|
2397 0, /* tp_clear */ |
|
e4fd799c194b
added methods distance_to and distance_squared_to to the vector class
lorenz
parents:
1837
diff
changeset
|
2398 0, /* tp_richcompare */ |
|
e4fd799c194b
added methods distance_to and distance_squared_to to the vector class
lorenz
parents:
1837
diff
changeset
|
2399 0, /* tp_weaklistoffset */ |
|
e4fd799c194b
added methods distance_to and distance_squared_to to the vector class
lorenz
parents:
1837
diff
changeset
|
2400 PyObject_SelfIter, /* tp_iter */ |
|
e4fd799c194b
added methods distance_to and distance_squared_to to the vector class
lorenz
parents:
1837
diff
changeset
|
2401 (iternextfunc)vectoriter_next, /* tp_iternext */ |
|
e4fd799c194b
added methods distance_to and distance_squared_to to the vector class
lorenz
parents:
1837
diff
changeset
|
2402 vectoriter_methods, /* tp_methods */ |
|
e4fd799c194b
added methods distance_to and distance_squared_to to the vector class
lorenz
parents:
1837
diff
changeset
|
2403 0, /* tp_members */ |
|
e4fd799c194b
added methods distance_to and distance_squared_to to the vector class
lorenz
parents:
1837
diff
changeset
|
2404 }; |
|
e4fd799c194b
added methods distance_to and distance_squared_to to the vector class
lorenz
parents:
1837
diff
changeset
|
2405 |
|
e4fd799c194b
added methods distance_to and distance_squared_to to the vector class
lorenz
parents:
1837
diff
changeset
|
2406 static PyObject * |
|
e4fd799c194b
added methods distance_to and distance_squared_to to the vector class
lorenz
parents:
1837
diff
changeset
|
2407 vector_iter(PyObject *vec) |
|
e4fd799c194b
added methods distance_to and distance_squared_to to the vector class
lorenz
parents:
1837
diff
changeset
|
2408 { |
|
e4fd799c194b
added methods distance_to and distance_squared_to to the vector class
lorenz
parents:
1837
diff
changeset
|
2409 vectoriter *it; |
|
e4fd799c194b
added methods distance_to and distance_squared_to to the vector class
lorenz
parents:
1837
diff
changeset
|
2410 if (!PyVector_Check(vec)) { |
|
e4fd799c194b
added methods distance_to and distance_squared_to to the vector class
lorenz
parents:
1837
diff
changeset
|
2411 PyErr_BadInternalCall(); |
|
e4fd799c194b
added methods distance_to and distance_squared_to to the vector class
lorenz
parents:
1837
diff
changeset
|
2412 return NULL; |
|
e4fd799c194b
added methods distance_to and distance_squared_to to the vector class
lorenz
parents:
1837
diff
changeset
|
2413 } |
|
e4fd799c194b
added methods distance_to and distance_squared_to to the vector class
lorenz
parents:
1837
diff
changeset
|
2414 |
|
e4fd799c194b
added methods distance_to and distance_squared_to to the vector class
lorenz
parents:
1837
diff
changeset
|
2415 it = PyObject_New(vectoriter, &PyVectorIter_Type); |
|
e4fd799c194b
added methods distance_to and distance_squared_to to the vector class
lorenz
parents:
1837
diff
changeset
|
2416 if (it == NULL) |
|
e4fd799c194b
added methods distance_to and distance_squared_to to the vector class
lorenz
parents:
1837
diff
changeset
|
2417 return NULL; |
|
e4fd799c194b
added methods distance_to and distance_squared_to to the vector class
lorenz
parents:
1837
diff
changeset
|
2418 it->it_index = 0; |
|
e4fd799c194b
added methods distance_to and distance_squared_to to the vector class
lorenz
parents:
1837
diff
changeset
|
2419 Py_INCREF(vec); |
|
e4fd799c194b
added methods distance_to and distance_squared_to to the vector class
lorenz
parents:
1837
diff
changeset
|
2420 it->vec = (PyVector *)vec; |
|
e4fd799c194b
added methods distance_to and distance_squared_to to the vector class
lorenz
parents:
1837
diff
changeset
|
2421 return (PyObject *)it; |
|
e4fd799c194b
added methods distance_to and distance_squared_to to the vector class
lorenz
parents:
1837
diff
changeset
|
2422 } |
|
e4fd799c194b
added methods distance_to and distance_squared_to to the vector class
lorenz
parents:
1837
diff
changeset
|
2423 |
|
e4fd799c194b
added methods distance_to and distance_squared_to to the vector class
lorenz
parents:
1837
diff
changeset
|
2424 |
|
e4fd799c194b
added methods distance_to and distance_squared_to to the vector class
lorenz
parents:
1837
diff
changeset
|
2425 |
| 1846 | 2426 /******************************************** |
| 2427 * PyVector_SlerpIterator type definition | |
| 2428 ********************************************/ | |
| 2429 static void | |
| 2430 vector_slerpiter_dealloc(vector_slerpiter *it) | |
| 2431 { | |
| 2432 PyObject_Del(it); | |
| 2433 } | |
| 2434 | |
| 2435 static PyObject * | |
| 2436 vector_slerpiter_next(vector_slerpiter *it) | |
| 2437 { | |
| 2438 int i, j; | |
| 2439 PyVector *ret; | |
| 2440 double tmp; | |
| 2441 assert(it != NULL); | |
| 2442 | |
| 2443 if (it->it_index < it->steps) { | |
| 2444 ret = (PyVector*)PyVector_NEW(it->dim); | |
|
1847
ed12994f377c
added "lerp()" to Vector2 and Vector3 to provide a linear interpolation.
lorenz
parents:
1846
diff
changeset
|
2445 if (ret != NULL) { |
|
ed12994f377c
added "lerp()" to Vector2 and Vector3 to provide a linear interpolation.
lorenz
parents:
1846
diff
changeset
|
2446 for (i = 0; i < it->dim; ++i) { |
|
ed12994f377c
added "lerp()" to Vector2 and Vector3 to provide a linear interpolation.
lorenz
parents:
1846
diff
changeset
|
2447 tmp = 0; |
|
ed12994f377c
added "lerp()" to Vector2 and Vector3 to provide a linear interpolation.
lorenz
parents:
1846
diff
changeset
|
2448 for (j = 0; j < it->dim; ++j) |
|
ed12994f377c
added "lerp()" to Vector2 and Vector3 to provide a linear interpolation.
lorenz
parents:
1846
diff
changeset
|
2449 tmp += it->coords[j] * it->matrix[j][i]; |
|
ed12994f377c
added "lerp()" to Vector2 and Vector3 to provide a linear interpolation.
lorenz
parents:
1846
diff
changeset
|
2450 ret->coords[i] = tmp * it->radial_factor; |
|
ed12994f377c
added "lerp()" to Vector2 and Vector3 to provide a linear interpolation.
lorenz
parents:
1846
diff
changeset
|
2451 } |
|
ed12994f377c
added "lerp()" to Vector2 and Vector3 to provide a linear interpolation.
lorenz
parents:
1846
diff
changeset
|
2452 memcpy(it->coords, ret->coords, sizeof(it->coords[0]) * it->dim); |
| 1846 | 2453 } |
| 2454 ++(it->it_index); | |
| 2455 return (PyObject*)ret; | |
| 2456 } | |
| 2457 | |
| 2458 return NULL; | |
| 2459 } | |
| 2460 | |
| 2461 static PyObject * | |
| 2462 vector_slerpiter_len(vector_slerpiter *it) | |
| 2463 { | |
| 2464 Py_ssize_t len = 0; | |
| 2465 if (it) { | |
| 2466 len = it->steps - it->it_index; | |
| 2467 } | |
| 2468 return PyInt_FromSsize_t(len); | |
| 2469 } | |
| 2470 | |
| 2471 static PyMethodDef vector_slerpiter_methods[] = { | |
| 2472 {"__length_hint__", (PyCFunction)vector_slerpiter_len, METH_NOARGS, | |
| 2473 }, | |
| 2474 {NULL, NULL} /* sentinel */ | |
| 2475 }; | |
| 2476 | |
| 2477 static PyTypeObject PyVector_SlerpIter_Type = { | |
| 2478 PyObject_HEAD_INIT(NULL) | |
| 2479 0, /* ob_size */ | |
| 2480 "pygame.math.VectorSlerpIterator", /* tp_name */ | |
| 2481 sizeof(vector_slerpiter), /* tp_basicsize */ | |
| 2482 0, /* tp_itemsize */ | |
| 2483 (destructor)vector_slerpiter_dealloc, /* tp_dealloc */ | |
| 2484 0, /* tp_print */ | |
| 2485 0, /* tp_getattr */ | |
| 2486 0, /* tp_setattr */ | |
| 2487 0, /* tp_compare */ | |
| 2488 0, /* tp_repr */ | |
| 2489 0, /* tp_as_number */ | |
| 2490 0, /* tp_as_sequence */ | |
| 2491 0, /* tp_as_mapping */ | |
| 2492 0, /* tp_hash */ | |
| 2493 0, /* tp_call */ | |
| 2494 0, /* tp_str */ | |
| 2495 PyObject_GenericGetAttr, /* tp_getattro */ | |
| 2496 0, /* tp_setattro */ | |
| 2497 0, /* tp_as_buffer */ | |
| 2498 Py_TPFLAGS_DEFAULT, /* tp_flags */ | |
| 2499 0, /* tp_doc */ | |
| 2500 0, /* tp_traverse */ | |
| 2501 0, /* tp_clear */ | |
| 2502 0, /* tp_richcompare */ | |
| 2503 0, /* tp_weaklistoffset */ | |
| 2504 PyObject_SelfIter, /* tp_iter */ | |
| 2505 (iternextfunc)vector_slerpiter_next, /* tp_iternext */ | |
| 2506 vector_slerpiter_methods, /* tp_methods */ | |
| 2507 0, /* tp_members */ | |
| 2508 }; | |
| 2509 | |
| 2510 static PyObject * | |
| 2511 vector_slerp(PyVector *self, PyObject *args) | |
| 2512 { | |
| 2513 vector_slerpiter *it; | |
| 2514 PyObject *other, *steps_object; | |
| 2515 long int steps; | |
| 2516 double vec1_coords[VECTOR_MAX_SIZE], vec2_coords[VECTOR_MAX_SIZE]; | |
| 2517 double angle, length1, length2; | |
| 2518 | |
|
1847
ed12994f377c
added "lerp()" to Vector2 and Vector3 to provide a linear interpolation.
lorenz
parents:
1846
diff
changeset
|
2519 if (!PyArg_ParseTuple(args, "OO:Vector.slerp", &other, &steps_object)) { |
| 1846 | 2520 return NULL; |
| 2521 } | |
| 2522 if (!PyInt_Check(steps_object)) { | |
| 2523 PyErr_SetString(PyExc_TypeError, "Expected Int as argument 2"); | |
| 2524 return NULL; | |
| 2525 } | |
|
1847
ed12994f377c
added "lerp()" to Vector2 and Vector3 to provide a linear interpolation.
lorenz
parents:
1846
diff
changeset
|
2526 if (!PySequence_AsVectorCoords((PyObject*)self, vec1_coords, self->dim)) { |
| 1846 | 2527 PyErr_BadInternalCall(); |
| 2528 return NULL; | |
| 2529 } | |
|
1847
ed12994f377c
added "lerp()" to Vector2 and Vector3 to provide a linear interpolation.
lorenz
parents:
1846
diff
changeset
|
2530 if (!PySequence_AsVectorCoords(other, vec2_coords, self->dim)) { |
| 1846 | 2531 PyErr_SetString(PyExc_TypeError, "Expected Vector as argument 1"); |
| 2532 return NULL; | |
| 2533 } | |
| 2534 | |
| 2535 it = PyObject_New(vector_slerpiter, &PyVector_SlerpIter_Type); | |
| 2536 if (it == NULL) | |
| 2537 return NULL; | |
| 2538 it->it_index = 0; | |
| 2539 it->dim = self->dim; | |
| 2540 memcpy(it->coords, vec1_coords, sizeof(vec1_coords[0]) * it->dim); | |
| 2541 | |
| 2542 length1 = sqrt(_scalar_product(vec1_coords, vec1_coords, it->dim)); | |
| 2543 length2 = sqrt(_scalar_product(vec2_coords, vec2_coords, it->dim)); | |
|
1847
ed12994f377c
added "lerp()" to Vector2 and Vector3 to provide a linear interpolation.
lorenz
parents:
1846
diff
changeset
|
2544 if ((length1 < self->epsilon) || (length2 < self->epsilon)) { |
| 1846 | 2545 PyErr_SetString(PyExc_ZeroDivisionError, |
| 2546 "can't use slerp with Zero-Vector"); | |
| 2547 Py_DECREF(it); | |
| 2548 return NULL; | |
| 2549 } | |
| 2550 angle = acos(_scalar_product(vec1_coords, vec2_coords, self->dim) / | |
| 2551 (length1 * length2)); | |
| 2552 it->steps = PyInt_AsLong(steps_object); | |
|
1847
ed12994f377c
added "lerp()" to Vector2 and Vector3 to provide a linear interpolation.
lorenz
parents:
1846
diff
changeset
|
2553 if (it->steps < 0) { |
| 1846 | 2554 angle -= 2 * M_PI; |
| 2555 it->steps = -it->steps; | |
| 2556 } | |
| 2557 angle /= it->steps; | |
| 2558 | |
| 1848 | 2559 if (fabs(length1 - length2) > self->epsilon) |
|
1847
ed12994f377c
added "lerp()" to Vector2 and Vector3 to provide a linear interpolation.
lorenz
parents:
1846
diff
changeset
|
2560 it->radial_factor = pow(length2 / length1, 1./it->steps); |
|
ed12994f377c
added "lerp()" to Vector2 and Vector3 to provide a linear interpolation.
lorenz
parents:
1846
diff
changeset
|
2561 else |
|
ed12994f377c
added "lerp()" to Vector2 and Vector3 to provide a linear interpolation.
lorenz
parents:
1846
diff
changeset
|
2562 it->radial_factor = 1; |
|
ed12994f377c
added "lerp()" to Vector2 and Vector3 to provide a linear interpolation.
lorenz
parents:
1846
diff
changeset
|
2563 |
| 1846 | 2564 switch (self->dim) { |
| 2565 case 2: | |
| 2566 _make_vector2_slerp_matrix(it, vec1_coords, vec2_coords, angle); | |
| 2567 break; | |
| 2568 case 3: | |
| 2569 _make_vector3_slerp_matrix(it, vec1_coords, vec2_coords, angle); | |
| 2570 break; | |
| 2571 default: | |
| 2572 PyErr_BadInternalCall(); | |
| 2573 Py_DECREF(it); | |
| 2574 return NULL; | |
| 2575 } | |
| 2576 return (PyObject *)it; | |
| 2577 } | |
| 2578 | |
| 2579 static void | |
| 2580 _make_vector2_slerp_matrix(vector_slerpiter *it, | |
| 2581 const double *vec1_coords, const double *vec2_coords, | |
| 2582 double angle) | |
| 2583 { | |
| 2584 double sin_value, cos_value; | |
|
1847
ed12994f377c
added "lerp()" to Vector2 and Vector3 to provide a linear interpolation.
lorenz
parents:
1846
diff
changeset
|
2585 if (vec1_coords[0] * vec2_coords[1] < vec1_coords[1] * vec2_coords[0]) |
|
ed12994f377c
added "lerp()" to Vector2 and Vector3 to provide a linear interpolation.
lorenz
parents:
1846
diff
changeset
|
2586 angle *= -1; |
| 1846 | 2587 cos_value = cos(angle); |
| 2588 sin_value = sin(angle); | |
| 2589 it->matrix[0][0] = cos_value; | |
| 2590 it->matrix[0][1] = sin_value; | |
| 2591 it->matrix[1][0] = -sin_value; | |
| 2592 it->matrix[1][1] = cos_value; | |
| 2593 } | |
| 2594 | |
| 2595 static void | |
| 2596 _make_vector3_slerp_matrix(vector_slerpiter *it, | |
| 2597 const double *vec1_coords, const double *vec2_coords, | |
| 2598 double angle) | |
| 2599 { | |
| 2600 int i; | |
| 2601 double axis[3]; | |
| 2602 double norm_factor, sin_value, cos_value, cos_complement; | |
| 2603 | |
| 2604 /* calculate rotation axis via cross-product */ | |
| 2605 for (i = 0; i < 3; ++i) | |
| 2606 axis[i] = ((vec1_coords[(i+1)%3] * vec2_coords[(i+2)%3]) - | |
| 2607 (vec1_coords[(i+2)%3] * vec2_coords[(i+1)%3])); | |
| 2608 /* normalize the rotation axis */ | |
| 2609 norm_factor = 1. / sqrt(_scalar_product(axis, axis, 3)); | |
| 2610 for (i = 0; i < 3; ++i) | |
| 2611 axis[i] *= norm_factor; | |
| 2612 | |
| 2613 sin_value = sin(angle); | |
| 2614 cos_value = cos(angle); | |
| 2615 cos_complement = 1 - cos_value; | |
| 2616 /* calculate the rotation matrix */ | |
| 2617 it->matrix[0][0] = cos_value + axis[0] * axis[0] * cos_complement; | |
| 2618 it->matrix[0][1] = axis[0] * axis[1] * cos_complement + axis[2] * sin_value; | |
| 2619 it->matrix[0][2] = axis[0] * axis[2] * cos_complement - axis[1] * sin_value; | |
| 2620 it->matrix[1][0] = axis[0] * axis[1] * cos_complement - axis[2] * sin_value; | |
| 2621 it->matrix[1][1] = cos_value + axis[1] * axis[1] * cos_complement; | |
| 2622 it->matrix[1][2] = axis[1] * axis[2] * cos_complement + axis[0] * sin_value; | |
| 2623 it->matrix[2][0] = axis[0] * axis[2] * cos_complement + axis[1] * sin_value; | |
| 2624 it->matrix[2][1] = axis[1] * axis[2] * cos_complement - axis[0] * sin_value; | |
| 2625 it->matrix[2][2] = cos_value + axis[2] * axis[2] * cos_complement; | |
| 2626 } | |
|
1841
a7dc31e50c46
* added the vetor_elementwiseproxy which lets you apply calculations on
lorenz
parents:
1840
diff
changeset
|
2627 |
| 1848 | 2628 /* |
| 2629 static PyObject * | |
| 2630 vector2_slerp(PyVector *self, PyObject *args) | |
| 2631 { | |
| 2632 PyObject *other; | |
| 2633 PyVector *ret; | |
| 2634 double *other_coords; | |
| 2635 double t; | |
| 2636 if (!PyArg_ParseTuple("Od:slerp", &other, &t)) { | |
| 2637 return NULL; | |
| 2638 } | |
| 2639 if (!PyVectorCompatible_Check(end_vector, self->dim)) { | |
| 2640 PyErr_SetString(PyExc_TypeError, "Argument 1 must be a vector."); | |
| 2641 return NULL; | |
| 2642 } | |
| 2643 if (!PySequence_AsVectorCoords(other, other_coords, self->dim)) { | |
| 2644 PyErr_SetString(PyExc_TypeError, "Argument 1 must be a vector."); | |
| 2645 return NULL; | |
| 2646 } | |
| 2647 if (fabs(t) > 1) { | |
| 2648 PyErr_SetString(PyExc_ValueError, "Argument 2 must be in range [-1, 1]."); | |
| 2649 return NULL; | |
| 2650 } | |
| 2651 | |
| 2652 length1 = sqrt(_scalar_product(self->coords, self->coords, self->dim)); | |
| 2653 length2 = sqrt(_scalar_product(other_coords, other_coords, self->dim)); | |
| 2654 if ((length1 < self->epsilon) || (length2 < self->epsilon)) { | |
| 2655 PyErr_SetString(PyExc_ZeroDivisionError, | |
| 2656 "can't use slerp with Zero-Vector"); | |
| 2657 return NULL; | |
| 2658 } | |
| 2659 angle = acos(_scalar_product(self->coords, other_coords, self->dim) / | |
| 2660 (length1 * length2)); | |
| 2661 if (t < 0) { | |
| 2662 angle -= 2 * M_PI; | |
| 2663 t = -t; | |
| 2664 } | |
| 2665 angle *= t; | |
| 2666 if (vec1_coords[0] * vec2_coords[1] < vec1_coords[1] * vec2_coords[0]) | |
| 2667 angle *= -1; | |
| 2668 | |
| 2669 if (fabs(length1 - length2) > self->epsilon) | |
| 2670 it->radial_factor = t * length2 / length1; | |
| 2671 else | |
| 2672 it->radial_factor = 1; | |
| 2673 | |
| 2674 ret = PyVector_NEW(self->dim); | |
| 2675 cos_value = cos(angle); | |
| 2676 sin_value = sin(angle); | |
| 2677 ret->coords[0] = cos_value * self->coords[0] - sin_value * self->coords[1]; | |
| 2678 ret->coords[1] = sin_value * self->coords[0] + cos_value * self->coords[1]; | |
| 2679 n1 = 1. / length1; | |
| 2680 n2 = 1. / length2; | |
| 2681 s0 = 1. / sin(angle); | |
| 2682 s1 = sin(angle * t); | |
| 2683 s2 = sin(angle * (1-t)); | |
| 2684 for (i = 0; i < self->dim; ++i) | |
| 2685 ret->coords[i] = (self->coords[i] * s1 + other_coords[i] * s2) * s0; | |
| 2686 return ret; | |
| 2687 } | |
| 2688 */ | |
|
1841
a7dc31e50c46
* added the vetor_elementwiseproxy which lets you apply calculations on
lorenz
parents:
1840
diff
changeset
|
2689 |
|
a7dc31e50c46
* added the vetor_elementwiseproxy which lets you apply calculations on
lorenz
parents:
1840
diff
changeset
|
2690 |
|
1847
ed12994f377c
added "lerp()" to Vector2 and Vector3 to provide a linear interpolation.
lorenz
parents:
1846
diff
changeset
|
2691 /******************************************** |
|
ed12994f377c
added "lerp()" to Vector2 and Vector3 to provide a linear interpolation.
lorenz
parents:
1846
diff
changeset
|
2692 * PyVector_LerpIterator type definition |
|
ed12994f377c
added "lerp()" to Vector2 and Vector3 to provide a linear interpolation.
lorenz
parents:
1846
diff
changeset
|
2693 ********************************************/ |
|
ed12994f377c
added "lerp()" to Vector2 and Vector3 to provide a linear interpolation.
lorenz
parents:
1846
diff
changeset
|
2694 static void |
|
ed12994f377c
added "lerp()" to Vector2 and Vector3 to provide a linear interpolation.
lorenz
parents:
1846
diff
changeset
|
2695 vector_lerpiter_dealloc(vector_lerpiter *it) |
|
ed12994f377c
added "lerp()" to Vector2 and Vector3 to provide a linear interpolation.
lorenz
parents:
1846
diff
changeset
|
2696 { |
|
ed12994f377c
added "lerp()" to Vector2 and Vector3 to provide a linear interpolation.
lorenz
parents:
1846
diff
changeset
|
2697 PyObject_Del(it); |
|
ed12994f377c
added "lerp()" to Vector2 and Vector3 to provide a linear interpolation.
lorenz
parents:
1846
diff
changeset
|
2698 } |
|
ed12994f377c
added "lerp()" to Vector2 and Vector3 to provide a linear interpolation.
lorenz
parents:
1846
diff
changeset
|
2699 |
|
ed12994f377c
added "lerp()" to Vector2 and Vector3 to provide a linear interpolation.
lorenz
parents:
1846
diff
changeset
|
2700 static PyObject * |
|
ed12994f377c
added "lerp()" to Vector2 and Vector3 to provide a linear interpolation.
lorenz
parents:
1846
diff
changeset
|
2701 vector_lerpiter_next(vector_lerpiter *it) |
|
ed12994f377c
added "lerp()" to Vector2 and Vector3 to provide a linear interpolation.
lorenz
parents:
1846
diff
changeset
|
2702 { |
|
ed12994f377c
added "lerp()" to Vector2 and Vector3 to provide a linear interpolation.
lorenz
parents:
1846
diff
changeset
|
2703 int i; |
|
ed12994f377c
added "lerp()" to Vector2 and Vector3 to provide a linear interpolation.
lorenz
parents:
1846
diff
changeset
|
2704 PyVector *ret; |
|
ed12994f377c
added "lerp()" to Vector2 and Vector3 to provide a linear interpolation.
lorenz
parents:
1846
diff
changeset
|
2705 assert(it != NULL); |
|
ed12994f377c
added "lerp()" to Vector2 and Vector3 to provide a linear interpolation.
lorenz
parents:
1846
diff
changeset
|
2706 |
|
ed12994f377c
added "lerp()" to Vector2 and Vector3 to provide a linear interpolation.
lorenz
parents:
1846
diff
changeset
|
2707 if (it->it_index < it->steps) { |
|
ed12994f377c
added "lerp()" to Vector2 and Vector3 to provide a linear interpolation.
lorenz
parents:
1846
diff
changeset
|
2708 ret = (PyVector*)PyVector_NEW(it->dim); |
|
ed12994f377c
added "lerp()" to Vector2 and Vector3 to provide a linear interpolation.
lorenz
parents:
1846
diff
changeset
|
2709 if (ret != NULL) { |
|
ed12994f377c
added "lerp()" to Vector2 and Vector3 to provide a linear interpolation.
lorenz
parents:
1846
diff
changeset
|
2710 for (i = 0; i < it->dim; ++i) { |
|
ed12994f377c
added "lerp()" to Vector2 and Vector3 to provide a linear interpolation.
lorenz
parents:
1846
diff
changeset
|
2711 it->coords[i] += it->step_vec[i]; |
|
ed12994f377c
added "lerp()" to Vector2 and Vector3 to provide a linear interpolation.
lorenz
parents:
1846
diff
changeset
|
2712 ret->coords[i] = it->coords[i]; |
|
ed12994f377c
added "lerp()" to Vector2 and Vector3 to provide a linear interpolation.
lorenz
parents:
1846
diff
changeset
|
2713 } |
|
ed12994f377c
added "lerp()" to Vector2 and Vector3 to provide a linear interpolation.
lorenz
parents:
1846
diff
changeset
|
2714 } |
|
ed12994f377c
added "lerp()" to Vector2 and Vector3 to provide a linear interpolation.
lorenz
parents:
1846
diff
changeset
|
2715 ++(it->it_index); |
|
ed12994f377c
added "lerp()" to Vector2 and Vector3 to provide a linear interpolation.
lorenz
parents:
1846
diff
changeset
|
2716 return (PyObject*)ret; |
|
ed12994f377c
added "lerp()" to Vector2 and Vector3 to provide a linear interpolation.
lorenz
parents:
1846
diff
changeset
|
2717 } |
|
ed12994f377c
added "lerp()" to Vector2 and Vector3 to provide a linear interpolation.
lorenz
parents:
1846
diff
changeset
|
2718 |
|
ed12994f377c
added "lerp()" to Vector2 and Vector3 to provide a linear interpolation.
lorenz
parents:
1846
diff
changeset
|
2719 return NULL; |
|
ed12994f377c
added "lerp()" to Vector2 and Vector3 to provide a linear interpolation.
lorenz
parents:
1846
diff
changeset
|
2720 } |
|
ed12994f377c
added "lerp()" to Vector2 and Vector3 to provide a linear interpolation.
lorenz
parents:
1846
diff
changeset
|
2721 |
|
ed12994f377c
added "lerp()" to Vector2 and Vector3 to provide a linear interpolation.
lorenz
parents:
1846
diff
changeset
|
2722 static PyObject * |
|
ed12994f377c
added "lerp()" to Vector2 and Vector3 to provide a linear interpolation.
lorenz
parents:
1846
diff
changeset
|
2723 vector_lerpiter_len(vector_lerpiter *it) |
|
ed12994f377c
added "lerp()" to Vector2 and Vector3 to provide a linear interpolation.
lorenz
parents:
1846
diff
changeset
|
2724 { |
|
ed12994f377c
added "lerp()" to Vector2 and Vector3 to provide a linear interpolation.
lorenz
parents:
1846
diff
changeset
|
2725 Py_ssize_t len = 0; |
|
ed12994f377c
added "lerp()" to Vector2 and Vector3 to provide a linear interpolation.
lorenz
parents:
1846
diff
changeset
|
2726 if (it) { |
|
ed12994f377c
added "lerp()" to Vector2 and Vector3 to provide a linear interpolation.
lorenz
parents:
1846
diff
changeset
|
2727 len = it->steps - it->it_index; |
|
ed12994f377c
added "lerp()" to Vector2 and Vector3 to provide a linear interpolation.
lorenz
parents:
1846
diff
changeset
|
2728 } |
|
ed12994f377c
added "lerp()" to Vector2 and Vector3 to provide a linear interpolation.
lorenz
parents:
1846
diff
changeset
|
2729 return PyInt_FromSsize_t(len); |
|
ed12994f377c
added "lerp()" to Vector2 and Vector3 to provide a linear interpolation.
lorenz
parents:
1846
diff
changeset
|
2730 } |
|
ed12994f377c
added "lerp()" to Vector2 and Vector3 to provide a linear interpolation.
lorenz
parents:
1846
diff
changeset
|
2731 |
|
ed12994f377c
added "lerp()" to Vector2 and Vector3 to provide a linear interpolation.
lorenz
parents:
1846
diff
changeset
|
2732 static PyMethodDef vector_lerpiter_methods[] = { |
|
ed12994f377c
added "lerp()" to Vector2 and Vector3 to provide a linear interpolation.
lorenz
parents:
1846
diff
changeset
|
2733 {"__length_hint__", (PyCFunction)vector_lerpiter_len, METH_NOARGS, |
|
ed12994f377c
added "lerp()" to Vector2 and Vector3 to provide a linear interpolation.
lorenz
parents:
1846
diff
changeset
|
2734 }, |
|
ed12994f377c
added "lerp()" to Vector2 and Vector3 to provide a linear interpolation.
lorenz
parents:
1846
diff
changeset
|
2735 {NULL, NULL} /* sentinel */ |
|
ed12994f377c
added "lerp()" to Vector2 and Vector3 to provide a linear interpolation.
lorenz
parents:
1846
diff
changeset
|
2736 }; |
|
ed12994f377c
added "lerp()" to Vector2 and Vector3 to provide a linear interpolation.
lorenz
parents:
1846
diff
changeset
|
2737 |
|
ed12994f377c
added "lerp()" to Vector2 and Vector3 to provide a linear interpolation.
lorenz
parents:
1846
diff
changeset
|
2738 static PyTypeObject PyVector_LerpIter_Type = { |
|
ed12994f377c
added "lerp()" to Vector2 and Vector3 to provide a linear interpolation.
lorenz
parents:
1846
diff
changeset
|
2739 PyObject_HEAD_INIT(NULL) |
|
ed12994f377c
added "lerp()" to Vector2 and Vector3 to provide a linear interpolation.
lorenz
parents:
1846
diff
changeset
|
2740 0, /* ob_size */ |
|
ed12994f377c
added "lerp()" to Vector2 and Vector3 to provide a linear interpolation.
lorenz
parents:
1846
diff
changeset
|
2741 "pygame.math.VectorLerpIterator", /* tp_name */ |
|
ed12994f377c
added "lerp()" to Vector2 and Vector3 to provide a linear interpolation.
lorenz
parents:
1846
diff
changeset
|
2742 sizeof(vector_lerpiter), /* tp_basicsize */ |
|
ed12994f377c
added "lerp()" to Vector2 and Vector3 to provide a linear interpolation.
lorenz
parents:
1846
diff
changeset
|
2743 0, /* tp_itemsize */ |
|
ed12994f377c
added "lerp()" to Vector2 and Vector3 to provide a linear interpolation.
lorenz
parents:
1846
diff
changeset
|
2744 (destructor)vector_lerpiter_dealloc, /* tp_dealloc */ |
|
ed12994f377c
added "lerp()" to Vector2 and Vector3 to provide a linear interpolation.
lorenz
parents:
1846
diff
changeset
|
2745 0, /* tp_print */ |
|
ed12994f377c
added "lerp()" to Vector2 and Vector3 to provide a linear interpolation.
lorenz
parents:
1846
diff
changeset
|
2746 0, /* tp_getattr */ |
|
ed12994f377c
added "lerp()" to Vector2 and Vector3 to provide a linear interpolation.
lorenz
parents:
1846
diff
changeset
|
2747 0, /* tp_setattr */ |
|
ed12994f377c
added "lerp()" to Vector2 and Vector3 to provide a linear interpolation.
lorenz
parents:
1846
diff
changeset
|
2748 0, /* tp_compare */ |
|
ed12994f377c
added "lerp()" to Vector2 and Vector3 to provide a linear interpolation.
lorenz
parents:
1846
diff
changeset
|
2749 0, /* tp_repr */ |
|
ed12994f377c
added "lerp()" to Vector2 and Vector3 to provide a linear interpolation.
lorenz
parents:
1846
diff
changeset
|
2750 0, /* tp_as_number */ |
|
ed12994f377c
added "lerp()" to Vector2 and Vector3 to provide a linear interpolation.
lorenz
parents:
1846
diff
changeset
|
2751 0, /* tp_as_sequence */ |
|
ed12994f377c
added "lerp()" to Vector2 and Vector3 to provide a linear interpolation.
lorenz
parents:
1846
diff
changeset
|
2752 0, /* tp_as_mapping */ |
|
ed12994f377c
added "lerp()" to Vector2 and Vector3 to provide a linear interpolation.
lorenz
parents:
1846
diff
changeset
|
2753 0, /* tp_hash */ |
|
ed12994f377c
added "lerp()" to Vector2 and Vector3 to provide a linear interpolation.
lorenz
parents:
1846
diff
changeset
|
2754 0, /* tp_call */ |
|
ed12994f377c
added "lerp()" to Vector2 and Vector3 to provide a linear interpolation.
lorenz
parents:
1846
diff
changeset
|
2755 0, /* tp_str */ |
|
ed12994f377c
added "lerp()" to Vector2 and Vector3 to provide a linear interpolation.
lorenz
parents:
1846
diff
changeset
|
2756 PyObject_GenericGetAttr, /* tp_getattro */ |
|
ed12994f377c
added "lerp()" to Vector2 and Vector3 to provide a linear interpolation.
lorenz
parents:
1846
diff
changeset
|
2757 0, /* tp_setattro */ |
|
ed12994f377c
added "lerp()" to Vector2 and Vector3 to provide a linear interpolation.
lorenz
parents:
1846
diff
changeset
|
2758 0, /* tp_as_buffer */ |
|
ed12994f377c
added "lerp()" to Vector2 and Vector3 to provide a linear interpolation.
lorenz
parents:
1846
diff
changeset
|
2759 Py_TPFLAGS_DEFAULT, /* tp_flags */ |
|
ed12994f377c
added "lerp()" to Vector2 and Vector3 to provide a linear interpolation.
lorenz
parents:
1846
diff
changeset
|
2760 0, /* tp_doc */ |
|
ed12994f377c
added "lerp()" to Vector2 and Vector3 to provide a linear interpolation.
lorenz
parents:
1846
diff
changeset
|
2761 0, /* tp_traverse */ |
|
ed12994f377c
added "lerp()" to Vector2 and Vector3 to provide a linear interpolation.
lorenz
parents:
1846
diff
changeset
|
2762 0, /* tp_clear */ |
|
ed12994f377c
added "lerp()" to Vector2 and Vector3 to provide a linear interpolation.
lorenz
parents:
1846
diff
changeset
|
2763 0, /* tp_richcompare */ |
|
ed12994f377c
added "lerp()" to Vector2 and Vector3 to provide a linear interpolation.
lorenz
parents:
1846
diff
changeset
|
2764 0, /* tp_weaklistoffset */ |
|
ed12994f377c
added "lerp()" to Vector2 and Vector3 to provide a linear interpolation.
lorenz
parents:
1846
diff
changeset
|
2765 PyObject_SelfIter, /* tp_iter */ |
|
ed12994f377c
added "lerp()" to Vector2 and Vector3 to provide a linear interpolation.
lorenz
parents:
1846
diff
changeset
|
2766 (iternextfunc)vector_lerpiter_next, /* tp_iternext */ |
|
ed12994f377c
added "lerp()" to Vector2 and Vector3 to provide a linear interpolation.
lorenz
parents:
1846
diff
changeset
|
2767 vector_lerpiter_methods, /* tp_methods */ |
|
ed12994f377c
added "lerp()" to Vector2 and Vector3 to provide a linear interpolation.
lorenz
parents:
1846
diff
changeset
|
2768 0, /* tp_members */ |
|
ed12994f377c
added "lerp()" to Vector2 and Vector3 to provide a linear interpolation.
lorenz
parents:
1846
diff
changeset
|
2769 }; |
|
ed12994f377c
added "lerp()" to Vector2 and Vector3 to provide a linear interpolation.
lorenz
parents:
1846
diff
changeset
|
2770 |
|
ed12994f377c
added "lerp()" to Vector2 and Vector3 to provide a linear interpolation.
lorenz
parents:
1846
diff
changeset
|
2771 |
|
ed12994f377c
added "lerp()" to Vector2 and Vector3 to provide a linear interpolation.
lorenz
parents:
1846
diff
changeset
|
2772 static PyObject * |
|
ed12994f377c
added "lerp()" to Vector2 and Vector3 to provide a linear interpolation.
lorenz
parents:
1846
diff
changeset
|
2773 vector_lerp(PyVector *self, PyObject *args) |
|
ed12994f377c
added "lerp()" to Vector2 and Vector3 to provide a linear interpolation.
lorenz
parents:
1846
diff
changeset
|
2774 { |
|
ed12994f377c
added "lerp()" to Vector2 and Vector3 to provide a linear interpolation.
lorenz
parents:
1846
diff
changeset
|
2775 vector_lerpiter *it; |
|
ed12994f377c
added "lerp()" to Vector2 and Vector3 to provide a linear interpolation.
lorenz
parents:
1846
diff
changeset
|
2776 PyObject *other, *steps_object; |
|
ed12994f377c
added "lerp()" to Vector2 and Vector3 to provide a linear interpolation.
lorenz
parents:
1846
diff
changeset
|
2777 long int i, steps; |
|
ed12994f377c
added "lerp()" to Vector2 and Vector3 to provide a linear interpolation.
lorenz
parents:
1846
diff
changeset
|
2778 double vec1_coords[VECTOR_MAX_SIZE], vec2_coords[VECTOR_MAX_SIZE]; |
|
ed12994f377c
added "lerp()" to Vector2 and Vector3 to provide a linear interpolation.
lorenz
parents:
1846
diff
changeset
|
2779 |
|
ed12994f377c
added "lerp()" to Vector2 and Vector3 to provide a linear interpolation.
lorenz
parents:
1846
diff
changeset
|
2780 if (!PyArg_ParseTuple(args, "OO:Vector.lerp", &other, &steps_object)) { |
|
ed12994f377c
added "lerp()" to Vector2 and Vector3 to provide a linear interpolation.
lorenz
parents:
1846
diff
changeset
|
2781 return NULL; |
|
ed12994f377c
added "lerp()" to Vector2 and Vector3 to provide a linear interpolation.
lorenz
parents:
1846
diff
changeset
|
2782 } |
|
ed12994f377c
added "lerp()" to Vector2 and Vector3 to provide a linear interpolation.
lorenz
parents:
1846
diff
changeset
|
2783 if (!PyInt_Check(steps_object)) { |
|
ed12994f377c
added "lerp()" to Vector2 and Vector3 to provide a linear interpolation.
lorenz
parents:
1846
diff
changeset
|
2784 PyErr_SetString(PyExc_TypeError, "Expected Int as argument 2"); |
|
ed12994f377c
added "lerp()" to Vector2 and Vector3 to provide a linear interpolation.
lorenz
parents:
1846
diff
changeset
|
2785 return NULL; |
|
ed12994f377c
added "lerp()" to Vector2 and Vector3 to provide a linear interpolation.
lorenz
parents:
1846
diff
changeset
|
2786 } |
|
ed12994f377c
added "lerp()" to Vector2 and Vector3 to provide a linear interpolation.
lorenz
parents:
1846
diff
changeset
|
2787 if (!PySequence_AsVectorCoords((PyObject*)self, vec1_coords, self->dim)) { |
|
ed12994f377c
added "lerp()" to Vector2 and Vector3 to provide a linear interpolation.
lorenz
parents:
1846
diff
changeset
|
2788 PyErr_BadInternalCall(); |
|
ed12994f377c
added "lerp()" to Vector2 and Vector3 to provide a linear interpolation.
lorenz
parents:
1846
diff
changeset
|
2789 return NULL; |
|
ed12994f377c
added "lerp()" to Vector2 and Vector3 to provide a linear interpolation.
lorenz
parents:
1846
diff
changeset
|
2790 } |
|
ed12994f377c
added "lerp()" to Vector2 and Vector3 to provide a linear interpolation.
lorenz
parents:
1846
diff
changeset
|
2791 if (!PySequence_AsVectorCoords(other, vec2_coords, self->dim)) { |
|
ed12994f377c
added "lerp()" to Vector2 and Vector3 to provide a linear interpolation.
lorenz
parents:
1846
diff
changeset
|
2792 PyErr_SetString(PyExc_TypeError, "Expected Vector as argument 1"); |
|
ed12994f377c
added "lerp()" to Vector2 and Vector3 to provide a linear interpolation.
lorenz
parents:
1846
diff
changeset
|
2793 return NULL; |
|
ed12994f377c
added "lerp()" to Vector2 and Vector3 to provide a linear interpolation.
lorenz
parents:
1846
diff
changeset
|
2794 } |
|
ed12994f377c
added "lerp()" to Vector2 and Vector3 to provide a linear interpolation.
lorenz
parents:
1846
diff
changeset
|
2795 |
|
ed12994f377c
added "lerp()" to Vector2 and Vector3 to provide a linear interpolation.
lorenz
parents:
1846
diff
changeset
|
2796 it = PyObject_New(vector_lerpiter, &PyVector_LerpIter_Type); |
|
ed12994f377c
added "lerp()" to Vector2 and Vector3 to provide a linear interpolation.
lorenz
parents:
1846
diff
changeset
|
2797 if (it == NULL) |
|
ed12994f377c
added "lerp()" to Vector2 and Vector3 to provide a linear interpolation.
lorenz
parents:
1846
diff
changeset
|
2798 return NULL; |
|
ed12994f377c
added "lerp()" to Vector2 and Vector3 to provide a linear interpolation.
lorenz
parents:
1846
diff
changeset
|
2799 it->it_index = 0; |
|
ed12994f377c
added "lerp()" to Vector2 and Vector3 to provide a linear interpolation.
lorenz
parents:
1846
diff
changeset
|
2800 it->dim = self->dim; |
|
ed12994f377c
added "lerp()" to Vector2 and Vector3 to provide a linear interpolation.
lorenz
parents:
1846
diff
changeset
|
2801 it->steps = PyInt_AsLong(steps_object); |
|
ed12994f377c
added "lerp()" to Vector2 and Vector3 to provide a linear interpolation.
lorenz
parents:
1846
diff
changeset
|
2802 memcpy(it->coords, vec1_coords, sizeof(vec1_coords[0]) * it->dim); |
|
ed12994f377c
added "lerp()" to Vector2 and Vector3 to provide a linear interpolation.
lorenz
parents:
1846
diff
changeset
|
2803 |
|
ed12994f377c
added "lerp()" to Vector2 and Vector3 to provide a linear interpolation.
lorenz
parents:
1846
diff
changeset
|
2804 for (i = 0; i < it->dim; ++i) |
|
ed12994f377c
added "lerp()" to Vector2 and Vector3 to provide a linear interpolation.
lorenz
parents:
1846
diff
changeset
|
2805 it->step_vec[i] = (vec2_coords[i] - vec1_coords[i]) / it->steps; |
|
ed12994f377c
added "lerp()" to Vector2 and Vector3 to provide a linear interpolation.
lorenz
parents:
1846
diff
changeset
|
2806 return (PyObject *)it; |
|
ed12994f377c
added "lerp()" to Vector2 and Vector3 to provide a linear interpolation.
lorenz
parents:
1846
diff
changeset
|
2807 } |
|
ed12994f377c
added "lerp()" to Vector2 and Vector3 to provide a linear interpolation.
lorenz
parents:
1846
diff
changeset
|
2808 |
|
ed12994f377c
added "lerp()" to Vector2 and Vector3 to provide a linear interpolation.
lorenz
parents:
1846
diff
changeset
|
2809 |
|
ed12994f377c
added "lerp()" to Vector2 and Vector3 to provide a linear interpolation.
lorenz
parents:
1846
diff
changeset
|
2810 |
|
ed12994f377c
added "lerp()" to Vector2 and Vector3 to provide a linear interpolation.
lorenz
parents:
1846
diff
changeset
|
2811 |
|
1841
a7dc31e50c46
* added the vetor_elementwiseproxy which lets you apply calculations on
lorenz
parents:
1840
diff
changeset
|
2812 |
|
a7dc31e50c46
* added the vetor_elementwiseproxy which lets you apply calculations on
lorenz
parents:
1840
diff
changeset
|
2813 |
|
a7dc31e50c46
* added the vetor_elementwiseproxy which lets you apply calculations on
lorenz
parents:
1840
diff
changeset
|
2814 |
|
a7dc31e50c46
* added the vetor_elementwiseproxy which lets you apply calculations on
lorenz
parents:
1840
diff
changeset
|
2815 |
|
a7dc31e50c46
* added the vetor_elementwiseproxy which lets you apply calculations on
lorenz
parents:
1840
diff
changeset
|
2816 |
|
1845
7d2a16e6a1d3
rearanged math module. added forward declarations at the beginning.
lorenz
parents:
1844
diff
changeset
|
2817 /***************************************** |
|
7d2a16e6a1d3
rearanged math module. added forward declarations at the beginning.
lorenz
parents:
1844
diff
changeset
|
2818 * elementwiseproxy |
|
7d2a16e6a1d3
rearanged math module. added forward declarations at the beginning.
lorenz
parents:
1844
diff
changeset
|
2819 *****************************************/ |
|
7d2a16e6a1d3
rearanged math module. added forward declarations at the beginning.
lorenz
parents:
1844
diff
changeset
|
2820 |
|
7d2a16e6a1d3
rearanged math module. added forward declarations at the beginning.
lorenz
parents:
1844
diff
changeset
|
2821 |
|
1841
a7dc31e50c46
* added the vetor_elementwiseproxy which lets you apply calculations on
lorenz
parents:
1840
diff
changeset
|
2822 |
|
a7dc31e50c46
* added the vetor_elementwiseproxy which lets you apply calculations on
lorenz
parents:
1840
diff
changeset
|
2823 static void |
|
a7dc31e50c46
* added the vetor_elementwiseproxy which lets you apply calculations on
lorenz
parents:
1840
diff
changeset
|
2824 vector_elementwiseproxy_dealloc(vector_elementwiseproxy *it) |
|
a7dc31e50c46
* added the vetor_elementwiseproxy which lets you apply calculations on
lorenz
parents:
1840
diff
changeset
|
2825 { |
|
a7dc31e50c46
* added the vetor_elementwiseproxy which lets you apply calculations on
lorenz
parents:
1840
diff
changeset
|
2826 Py_XDECREF(it->vec); |
|
a7dc31e50c46
* added the vetor_elementwiseproxy which lets you apply calculations on
lorenz
parents:
1840
diff
changeset
|
2827 PyObject_Del(it); |
|
a7dc31e50c46
* added the vetor_elementwiseproxy which lets you apply calculations on
lorenz
parents:
1840
diff
changeset
|
2828 } |
|
a7dc31e50c46
* added the vetor_elementwiseproxy which lets you apply calculations on
lorenz
parents:
1840
diff
changeset
|
2829 |
|
a7dc31e50c46
* added the vetor_elementwiseproxy which lets you apply calculations on
lorenz
parents:
1840
diff
changeset
|
2830 static PyObject * |
|
a7dc31e50c46
* added the vetor_elementwiseproxy which lets you apply calculations on
lorenz
parents:
1840
diff
changeset
|
2831 vector_elementwiseproxy_richcompare(PyObject *o1, PyObject *o2, int op) |
|
a7dc31e50c46
* added the vetor_elementwiseproxy which lets you apply calculations on
lorenz
parents:
1840
diff
changeset
|
2832 { |
|
a7dc31e50c46
* added the vetor_elementwiseproxy which lets you apply calculations on
lorenz
parents:
1840
diff
changeset
|
2833 int i, dim; |
|
1842
5418dd349248
refactored the vector_elementwiseproxy functions and fixed some bugs along the way
lorenz
parents:
1841
diff
changeset
|
2834 double diff; |
|
1841
a7dc31e50c46
* added the vetor_elementwiseproxy which lets you apply calculations on
lorenz
parents:
1840
diff
changeset
|
2835 PyVector *vec; |
|
1842
5418dd349248
refactored the vector_elementwiseproxy functions and fixed some bugs along the way
lorenz
parents:
1841
diff
changeset
|
2836 PyObject *other; |
|
5418dd349248
refactored the vector_elementwiseproxy functions and fixed some bugs along the way
lorenz
parents:
1841
diff
changeset
|
2837 |
|
1841
a7dc31e50c46
* added the vetor_elementwiseproxy which lets you apply calculations on
lorenz
parents:
1840
diff
changeset
|
2838 if (vector_elementwiseproxy_Check(o1)) { |
|
1842
5418dd349248
refactored the vector_elementwiseproxy functions and fixed some bugs along the way
lorenz
parents:
1841
diff
changeset
|
2839 vec = ((vector_elementwiseproxy*)o1)->vec; |
|
5418dd349248
refactored the vector_elementwiseproxy functions and fixed some bugs along the way
lorenz
parents:
1841
diff
changeset
|
2840 other = o2; |
|
5418dd349248
refactored the vector_elementwiseproxy functions and fixed some bugs along the way
lorenz
parents:
1841
diff
changeset
|
2841 } |
|
5418dd349248
refactored the vector_elementwiseproxy functions and fixed some bugs along the way
lorenz
parents:
1841
diff
changeset
|
2842 else { |
|
5418dd349248
refactored the vector_elementwiseproxy functions and fixed some bugs along the way
lorenz
parents:
1841
diff
changeset
|
2843 vec = ((vector_elementwiseproxy*)o2)->vec; |
|
5418dd349248
refactored the vector_elementwiseproxy functions and fixed some bugs along the way
lorenz
parents:
1841
diff
changeset
|
2844 other = o1; |
|
5418dd349248
refactored the vector_elementwiseproxy functions and fixed some bugs along the way
lorenz
parents:
1841
diff
changeset
|
2845 /* flip op */ |
|
5418dd349248
refactored the vector_elementwiseproxy functions and fixed some bugs along the way
lorenz
parents:
1841
diff
changeset
|
2846 if (op == Py_LT) |
|
5418dd349248
refactored the vector_elementwiseproxy functions and fixed some bugs along the way
lorenz
parents:
1841
diff
changeset
|
2847 op = Py_GE; |
|
5418dd349248
refactored the vector_elementwiseproxy functions and fixed some bugs along the way
lorenz
parents:
1841
diff
changeset
|
2848 else if (op == Py_LE) |
|
5418dd349248
refactored the vector_elementwiseproxy functions and fixed some bugs along the way
lorenz
parents:
1841
diff
changeset
|
2849 op = Py_GT; |
|
5418dd349248
refactored the vector_elementwiseproxy functions and fixed some bugs along the way
lorenz
parents:
1841
diff
changeset
|
2850 else if (op == Py_GT) |
|
5418dd349248
refactored the vector_elementwiseproxy functions and fixed some bugs along the way
lorenz
parents:
1841
diff
changeset
|
2851 op = Py_LE; |
|
5418dd349248
refactored the vector_elementwiseproxy functions and fixed some bugs along the way
lorenz
parents:
1841
diff
changeset
|
2852 else if (op == Py_GE) |
|
5418dd349248
refactored the vector_elementwiseproxy functions and fixed some bugs along the way
lorenz
parents:
1841
diff
changeset
|
2853 op = Py_LT; |
|
5418dd349248
refactored the vector_elementwiseproxy functions and fixed some bugs along the way
lorenz
parents:
1841
diff
changeset
|
2854 } |
|
5418dd349248
refactored the vector_elementwiseproxy functions and fixed some bugs along the way
lorenz
parents:
1841
diff
changeset
|
2855 if (vector_elementwiseproxy_Check(other)) |
|
5418dd349248
refactored the vector_elementwiseproxy functions and fixed some bugs along the way
lorenz
parents:
1841
diff
changeset
|
2856 other = (PyObject*)((vector_elementwiseproxy*)o2)->vec; |
|
5418dd349248
refactored the vector_elementwiseproxy functions and fixed some bugs along the way
lorenz
parents:
1841
diff
changeset
|
2857 dim = vec->dim; |
|
5418dd349248
refactored the vector_elementwiseproxy functions and fixed some bugs along the way
lorenz
parents:
1841
diff
changeset
|
2858 |
|
1845
7d2a16e6a1d3
rearanged math module. added forward declarations at the beginning.
lorenz
parents:
1844
diff
changeset
|
2859 if (PyVectorCompatible_Check(other, dim)) { |
|
1842
5418dd349248
refactored the vector_elementwiseproxy functions and fixed some bugs along the way
lorenz
parents:
1841
diff
changeset
|
2860 /* use diff == diff to check for NaN */ |
|
5418dd349248
refactored the vector_elementwiseproxy functions and fixed some bugs along the way
lorenz
parents:
1841
diff
changeset
|
2861 /* TODO: how should NaN be handled with LT/LE/GT/GE? */ |
|
5418dd349248
refactored the vector_elementwiseproxy functions and fixed some bugs along the way
lorenz
parents:
1841
diff
changeset
|
2862 switch (op) { |
|
5418dd349248
refactored the vector_elementwiseproxy functions and fixed some bugs along the way
lorenz
parents:
1841
diff
changeset
|
2863 case Py_EQ: |
|
5418dd349248
refactored the vector_elementwiseproxy functions and fixed some bugs along the way
lorenz
parents:
1841
diff
changeset
|
2864 for (i = 0; i < dim; i++) { |
|
5418dd349248
refactored the vector_elementwiseproxy functions and fixed some bugs along the way
lorenz
parents:
1841
diff
changeset
|
2865 diff = vec->coords[i] - PySequence_GetItem_AsDouble(other, i); |
|
5418dd349248
refactored the vector_elementwiseproxy functions and fixed some bugs along the way
lorenz
parents:
1841
diff
changeset
|
2866 if ((diff != diff) || (fabs(diff) >= vec->epsilon)) { |
|
5418dd349248
refactored the vector_elementwiseproxy functions and fixed some bugs along the way
lorenz
parents:
1841
diff
changeset
|
2867 Py_RETURN_FALSE; |
|
1841
a7dc31e50c46
* added the vetor_elementwiseproxy which lets you apply calculations on
lorenz
parents:
1840
diff
changeset
|
2868 } |
|
a7dc31e50c46
* added the vetor_elementwiseproxy which lets you apply calculations on
lorenz
parents:
1840
diff
changeset
|
2869 } |
|
1842
5418dd349248
refactored the vector_elementwiseproxy functions and fixed some bugs along the way
lorenz
parents:
1841
diff
changeset
|
2870 Py_RETURN_TRUE; |
|
5418dd349248
refactored the vector_elementwiseproxy functions and fixed some bugs along the way
lorenz
parents:
1841
diff
changeset
|
2871 case Py_NE: |
|
5418dd349248
refactored the vector_elementwiseproxy functions and fixed some bugs along the way
lorenz
parents:
1841
diff
changeset
|
2872 for (i = 0; i < dim; i++) { |
|
5418dd349248
refactored the vector_elementwiseproxy functions and fixed some bugs along the way
lorenz
parents:
1841
diff
changeset
|
2873 diff = vec->coords[i] - PySequence_GetItem_AsDouble(other, i); |
|
5418dd349248
refactored the vector_elementwiseproxy functions and fixed some bugs along the way
lorenz
parents:
1841
diff
changeset
|
2874 if ((diff == diff) && (fabs(diff) < vec->epsilon)) { |
|
5418dd349248
refactored the vector_elementwiseproxy functions and fixed some bugs along the way
lorenz
parents:
1841
diff
changeset
|
2875 Py_RETURN_FALSE; |
|
1841
a7dc31e50c46
* added the vetor_elementwiseproxy which lets you apply calculations on
lorenz
parents:
1840
diff
changeset
|
2876 } |
|
1842
5418dd349248
refactored the vector_elementwiseproxy functions and fixed some bugs along the way
lorenz
parents:
1841
diff
changeset
|
2877 } |
|
5418dd349248
refactored the vector_elementwiseproxy functions and fixed some bugs along the way
lorenz
parents:
1841
diff
changeset
|
2878 Py_RETURN_TRUE; |
|
5418dd349248
refactored the vector_elementwiseproxy functions and fixed some bugs along the way
lorenz
parents:
1841
diff
changeset
|
2879 case Py_LT: |
|
5418dd349248
refactored the vector_elementwiseproxy functions and fixed some bugs along the way
lorenz
parents:
1841
diff
changeset
|
2880 for (i = 0; i < dim; i++) { |
|
5418dd349248
refactored the vector_elementwiseproxy functions and fixed some bugs along the way
lorenz
parents:
1841
diff
changeset
|
2881 if (vec->coords[i] >= PySequence_GetItem_AsDouble(other, i)) |
|
5418dd349248
refactored the vector_elementwiseproxy functions and fixed some bugs along the way
lorenz
parents:
1841
diff
changeset
|
2882 Py_RETURN_FALSE; |
|
5418dd349248
refactored the vector_elementwiseproxy functions and fixed some bugs along the way
lorenz
parents:
1841
diff
changeset
|
2883 } |
|
5418dd349248
refactored the vector_elementwiseproxy functions and fixed some bugs along the way
lorenz
parents:
1841
diff
changeset
|
2884 Py_RETURN_TRUE; |
|
5418dd349248
refactored the vector_elementwiseproxy functions and fixed some bugs along the way
lorenz
parents:
1841
diff
changeset
|
2885 case Py_LE: |
|
5418dd349248
refactored the vector_elementwiseproxy functions and fixed some bugs along the way
lorenz
parents:
1841
diff
changeset
|
2886 for (i = 0; i < dim; i++) { |
|
5418dd349248
refactored the vector_elementwiseproxy functions and fixed some bugs along the way
lorenz
parents:
1841
diff
changeset
|
2887 if (vec->coords[i] > PySequence_GetItem_AsDouble(other, i)) |
|
5418dd349248
refactored the vector_elementwiseproxy functions and fixed some bugs along the way
lorenz
parents:
1841
diff
changeset
|
2888 Py_RETURN_FALSE; |
|
1841
a7dc31e50c46
* added the vetor_elementwiseproxy which lets you apply calculations on
lorenz
parents:
1840
diff
changeset
|
2889 } |
|
1842
5418dd349248
refactored the vector_elementwiseproxy functions and fixed some bugs along the way
lorenz
parents:
1841
diff
changeset
|
2890 Py_RETURN_TRUE; |
|
5418dd349248
refactored the vector_elementwiseproxy functions and fixed some bugs along the way
lorenz
parents:
1841
diff
changeset
|
2891 case Py_GT: |
|
5418dd349248
refactored the vector_elementwiseproxy functions and fixed some bugs along the way
lorenz
parents:
1841
diff
changeset
|
2892 for (i = 0; i < dim; i++) { |
|
5418dd349248
refactored the vector_elementwiseproxy functions and fixed some bugs along the way
lorenz
parents:
1841
diff
changeset
|
2893 if (vec->coords[i] <= PySequence_GetItem_AsDouble(other, i)) |
|
5418dd349248
refactored the vector_elementwiseproxy functions and fixed some bugs along the way
lorenz
parents:
1841
diff
changeset
|
2894 Py_RETURN_FALSE; |
|
5418dd349248
refactored the vector_elementwiseproxy functions and fixed some bugs along the way
lorenz
parents:
1841
diff
changeset
|
2895 } |
|
5418dd349248
refactored the vector_elementwiseproxy functions and fixed some bugs along the way
lorenz
parents:
1841
diff
changeset
|
2896 Py_RETURN_TRUE; |
|
5418dd349248
refactored the vector_elementwiseproxy functions and fixed some bugs along the way
lorenz
parents:
1841
diff
changeset
|
2897 case Py_GE: |
|
5418dd349248
refactored the vector_elementwiseproxy functions and fixed some bugs along the way
lorenz
parents:
1841
diff
changeset
|
2898 for (i = 0; i < dim; i++) { |
|
5418dd349248
refactored the vector_elementwiseproxy functions and fixed some bugs along the way
lorenz
parents:
1841
diff
changeset
|
2899 if (vec->coords[i] < PySequence_GetItem_AsDouble(other, i)) |
|
5418dd349248
refactored the vector_elementwiseproxy functions and fixed some bugs along the way
lorenz
parents:
1841
diff
changeset
|
2900 Py_RETURN_FALSE; |
|
5418dd349248
refactored the vector_elementwiseproxy functions and fixed some bugs along the way
lorenz
parents:
1841
diff
changeset
|
2901 } |
|
5418dd349248
refactored the vector_elementwiseproxy functions and fixed some bugs along the way
lorenz
parents:
1841
diff
changeset
|
2902 Py_RETURN_TRUE; |
|
1841
a7dc31e50c46
* added the vetor_elementwiseproxy which lets you apply calculations on
lorenz
parents:
1840
diff
changeset
|
2903 } |
|
a7dc31e50c46
* added the vetor_elementwiseproxy which lets you apply calculations on
lorenz
parents:
1840
diff
changeset
|
2904 } |
|
1842
5418dd349248
refactored the vector_elementwiseproxy functions and fixed some bugs along the way
lorenz
parents:
1841
diff
changeset
|
2905 else if (RealNumber_Check(other)) { |
|
5418dd349248
refactored the vector_elementwiseproxy functions and fixed some bugs along the way
lorenz
parents:
1841
diff
changeset
|
2906 double value = PyFloat_AsDouble(other); |
|
5418dd349248
refactored the vector_elementwiseproxy functions and fixed some bugs along the way
lorenz
parents:
1841
diff
changeset
|
2907 switch (op) { |
|
5418dd349248
refactored the vector_elementwiseproxy functions and fixed some bugs along the way
lorenz
parents:
1841
diff
changeset
|
2908 case Py_EQ: |
|
5418dd349248
refactored the vector_elementwiseproxy functions and fixed some bugs along the way
lorenz
parents:
1841
diff
changeset
|
2909 for (i = 0; i < dim; i++) { |
|
5418dd349248
refactored the vector_elementwiseproxy functions and fixed some bugs along the way
lorenz
parents:
1841
diff
changeset
|
2910 diff = vec->coords[i] - value; |
|
5418dd349248
refactored the vector_elementwiseproxy functions and fixed some bugs along the way
lorenz
parents:
1841
diff
changeset
|
2911 if (diff != diff || fabs(diff) >= vec->epsilon) { |
|
5418dd349248
refactored the vector_elementwiseproxy functions and fixed some bugs along the way
lorenz
parents:
1841
diff
changeset
|
2912 Py_RETURN_FALSE; |
|
1841
a7dc31e50c46
* added the vetor_elementwiseproxy which lets you apply calculations on
lorenz
parents:
1840
diff
changeset
|
2913 } |
|
1842
5418dd349248
refactored the vector_elementwiseproxy functions and fixed some bugs along the way
lorenz
parents:
1841
diff
changeset
|
2914 } |
|
5418dd349248
refactored the vector_elementwiseproxy functions and fixed some bugs along the way
lorenz
parents:
1841
diff
changeset
|
2915 Py_RETURN_TRUE; |
|
5418dd349248
refactored the vector_elementwiseproxy functions and fixed some bugs along the way
lorenz
parents:
1841
diff
changeset
|
2916 case Py_NE: |
|
5418dd349248
refactored the vector_elementwiseproxy functions and fixed some bugs along the way
lorenz
parents:
1841
diff
changeset
|
2917 for (i = 0; i < dim; i++) { |
|
5418dd349248
refactored the vector_elementwiseproxy functions and fixed some bugs along the way
lorenz
parents:
1841
diff
changeset
|
2918 diff = vec->coords[i] - value; |
|
5418dd349248
refactored the vector_elementwiseproxy functions and fixed some bugs along the way
lorenz
parents:
1841
diff
changeset
|
2919 if (diff == diff && fabs(diff) < vec->epsilon) { |
|
5418dd349248
refactored the vector_elementwiseproxy functions and fixed some bugs along the way
lorenz
parents:
1841
diff
changeset
|
2920 Py_RETURN_FALSE; |
|
1841
a7dc31e50c46
* added the vetor_elementwiseproxy which lets you apply calculations on
lorenz
parents:
1840
diff
changeset
|
2921 } |
|
a7dc31e50c46
* added the vetor_elementwiseproxy which lets you apply calculations on
lorenz
parents:
1840
diff
changeset
|
2922 } |
|
1842
5418dd349248
refactored the vector_elementwiseproxy functions and fixed some bugs along the way
lorenz
parents:
1841
diff
changeset
|
2923 Py_RETURN_TRUE; |
|
5418dd349248
refactored the vector_elementwiseproxy functions and fixed some bugs along the way
lorenz
parents:
1841
diff
changeset
|
2924 case Py_LT: |
|
5418dd349248
refactored the vector_elementwiseproxy functions and fixed some bugs along the way
lorenz
parents:
1841
diff
changeset
|
2925 for (i = 0; i < dim; i++) { |
|
5418dd349248
refactored the vector_elementwiseproxy functions and fixed some bugs along the way
lorenz
parents:
1841
diff
changeset
|
2926 if (vec->coords[i] >= value) |
|
5418dd349248
refactored the vector_elementwiseproxy functions and fixed some bugs along the way
lorenz
parents:
1841
diff
changeset
|
2927 Py_RETURN_FALSE; |
|
5418dd349248
refactored the vector_elementwiseproxy functions and fixed some bugs along the way
lorenz
parents:
1841
diff
changeset
|
2928 } |
|
5418dd349248
refactored the vector_elementwiseproxy functions and fixed some bugs along the way
lorenz
parents:
1841
diff
changeset
|
2929 Py_RETURN_TRUE; |
|
5418dd349248
refactored the vector_elementwiseproxy functions and fixed some bugs along the way
lorenz
parents:
1841
diff
changeset
|
2930 case Py_LE: |
|
5418dd349248
refactored the vector_elementwiseproxy functions and fixed some bugs along the way
lorenz
parents:
1841
diff
changeset
|
2931 for (i = 0; i < dim; i++) { |
|
5418dd349248
refactored the vector_elementwiseproxy functions and fixed some bugs along the way
lorenz
parents:
1841
diff
changeset
|
2932 if (vec->coords[i] > value) |
|
5418dd349248
refactored the vector_elementwiseproxy functions and fixed some bugs along the way
lorenz
parents:
1841
diff
changeset
|
2933 Py_RETURN_FALSE; |
|
1841
a7dc31e50c46
* added the vetor_elementwiseproxy which lets you apply calculations on
lorenz
parents:
1840
diff
changeset
|
2934 } |
|
1842
5418dd349248
refactored the vector_elementwiseproxy functions and fixed some bugs along the way
lorenz
parents:
1841
diff
changeset
|
2935 Py_RETURN_TRUE; |
|
5418dd349248
refactored the vector_elementwiseproxy functions and fixed some bugs along the way
lorenz
parents:
1841
diff
changeset
|
2936 case Py_GT: |
|
5418dd349248
refactored the vector_elementwiseproxy functions and fixed some bugs along the way
lorenz
parents:
1841
diff
changeset
|
2937 for (i = 0; i < dim; i++) { |
|
5418dd349248
refactored the vector_elementwiseproxy functions and fixed some bugs along the way
lorenz
parents:
1841
diff
changeset
|
2938 if (vec->coords[i] <= value) |
|
5418dd349248
refactored the vector_elementwiseproxy functions and fixed some bugs along the way
lorenz
parents:
1841
diff
changeset
|
2939 Py_RETURN_FALSE; |
|
5418dd349248
refactored the vector_elementwiseproxy functions and fixed some bugs along the way
lorenz
parents:
1841
diff
changeset
|
2940 } |
|
5418dd349248
refactored the vector_elementwiseproxy functions and fixed some bugs along the way
lorenz
parents:
1841
diff
changeset
|
2941 Py_RETURN_TRUE; |
|
5418dd349248
refactored the vector_elementwiseproxy functions and fixed some bugs along the way
lorenz
parents:
1841
diff
changeset
|
2942 case Py_GE: |
|
5418dd349248
refactored the vector_elementwiseproxy functions and fixed some bugs along the way
lorenz
parents:
1841
diff
changeset
|
2943 for (i = 0; i < dim; i++) { |
|
5418dd349248
refactored the vector_elementwiseproxy functions and fixed some bugs along the way
lorenz
parents:
1841
diff
changeset
|
2944 if (vec->coords[i] < value) |
|
5418dd349248
refactored the vector_elementwiseproxy functions and fixed some bugs along the way
lorenz
parents:
1841
diff
changeset
|
2945 Py_RETURN_FALSE; |
|
5418dd349248
refactored the vector_elementwiseproxy functions and fixed some bugs along the way
lorenz
parents:
1841
diff
changeset
|
2946 } |
|
5418dd349248
refactored the vector_elementwiseproxy functions and fixed some bugs along the way
lorenz
parents:
1841
diff
changeset
|
2947 Py_RETURN_TRUE; |
|
1841
a7dc31e50c46
* added the vetor_elementwiseproxy which lets you apply calculations on
lorenz
parents:
1840
diff
changeset
|
2948 } |
|
a7dc31e50c46
* added the vetor_elementwiseproxy which lets you apply calculations on
lorenz
parents:
1840
diff
changeset
|
2949 } |
|
1842
5418dd349248
refactored the vector_elementwiseproxy functions and fixed some bugs along the way
lorenz
parents:
1841
diff
changeset
|
2950 |
|
5418dd349248
refactored the vector_elementwiseproxy functions and fixed some bugs along the way
lorenz
parents:
1841
diff
changeset
|
2951 Py_INCREF(Py_NotImplemented); |
|
5418dd349248
refactored the vector_elementwiseproxy functions and fixed some bugs along the way
lorenz
parents:
1841
diff
changeset
|
2952 return Py_NotImplemented; |
|
1841
a7dc31e50c46
* added the vetor_elementwiseproxy which lets you apply calculations on
lorenz
parents:
1840
diff
changeset
|
2953 } |
|
a7dc31e50c46
* added the vetor_elementwiseproxy which lets you apply calculations on
lorenz
parents:
1840
diff
changeset
|
2954 |
|
a7dc31e50c46
* added the vetor_elementwiseproxy which lets you apply calculations on
lorenz
parents:
1840
diff
changeset
|
2955 |
|
a7dc31e50c46
* added the vetor_elementwiseproxy which lets you apply calculations on
lorenz
parents:
1840
diff
changeset
|
2956 |
|
a7dc31e50c46
* added the vetor_elementwiseproxy which lets you apply calculations on
lorenz
parents:
1840
diff
changeset
|
2957 |
|
1842
5418dd349248
refactored the vector_elementwiseproxy functions and fixed some bugs along the way
lorenz
parents:
1841
diff
changeset
|
2958 /******************************************************* |
|
5418dd349248
refactored the vector_elementwiseproxy functions and fixed some bugs along the way
lorenz
parents:
1841
diff
changeset
|
2959 * vector_elementwiseproxy PyNumber emulation routines |
|
5418dd349248
refactored the vector_elementwiseproxy functions and fixed some bugs along the way
lorenz
parents:
1841
diff
changeset
|
2960 *******************************************************/ |
|
1841
a7dc31e50c46
* added the vetor_elementwiseproxy which lets you apply calculations on
lorenz
parents:
1840
diff
changeset
|
2961 |
|
a7dc31e50c46
* added the vetor_elementwiseproxy which lets you apply calculations on
lorenz
parents:
1840
diff
changeset
|
2962 static PyObject * |
| 1848 | 2963 vector_elementwiseproxy_generic_math(PyObject *o1, PyObject *o2, int op) |
|
1841
a7dc31e50c46
* added the vetor_elementwiseproxy which lets you apply calculations on
lorenz
parents:
1840
diff
changeset
|
2964 { |
| 1848 | 2965 /* TODO: DIV, FLOOR_DIV and MOD should check for ZeroDivision */ |
|
1841
a7dc31e50c46
* added the vetor_elementwiseproxy which lets you apply calculations on
lorenz
parents:
1840
diff
changeset
|
2966 int i, dim; |
| 1848 | 2967 double tmp, mod; |
|
1841
a7dc31e50c46
* added the vetor_elementwiseproxy which lets you apply calculations on
lorenz
parents:
1840
diff
changeset
|
2968 PyObject *other; |
|
1842
5418dd349248
refactored the vector_elementwiseproxy functions and fixed some bugs along the way
lorenz
parents:
1841
diff
changeset
|
2969 PyVector *vec, *ret; |
|
1841
a7dc31e50c46
* added the vetor_elementwiseproxy which lets you apply calculations on
lorenz
parents:
1840
diff
changeset
|
2970 if (vector_elementwiseproxy_Check(o1)) { |
|
1842
5418dd349248
refactored the vector_elementwiseproxy functions and fixed some bugs along the way
lorenz
parents:
1841
diff
changeset
|
2971 vec = ((vector_elementwiseproxy*)o1)->vec; |
|
1841
a7dc31e50c46
* added the vetor_elementwiseproxy which lets you apply calculations on
lorenz
parents:
1840
diff
changeset
|
2972 other = o2; |
|
a7dc31e50c46
* added the vetor_elementwiseproxy which lets you apply calculations on
lorenz
parents:
1840
diff
changeset
|
2973 } |
|
a7dc31e50c46
* added the vetor_elementwiseproxy which lets you apply calculations on
lorenz
parents:
1840
diff
changeset
|
2974 else { |
|
a7dc31e50c46
* added the vetor_elementwiseproxy which lets you apply calculations on
lorenz
parents:
1840
diff
changeset
|
2975 other = o1; |
|
1842
5418dd349248
refactored the vector_elementwiseproxy functions and fixed some bugs along the way
lorenz
parents:
1841
diff
changeset
|
2976 vec = ((vector_elementwiseproxy*)o2)->vec; |
| 1848 | 2977 op |= OP_ARG_REVERSE; |
|
1841
a7dc31e50c46
* added the vetor_elementwiseproxy which lets you apply calculations on
lorenz
parents:
1840
diff
changeset
|
2978 } |
|
a7dc31e50c46
* added the vetor_elementwiseproxy which lets you apply calculations on
lorenz
parents:
1840
diff
changeset
|
2979 |
| 1848 | 2980 dim = vec->dim; |
| 2981 | |
| 2982 if (vector_elementwiseproxy_Check(other)) { | |
|
1842
5418dd349248
refactored the vector_elementwiseproxy functions and fixed some bugs along the way
lorenz
parents:
1841
diff
changeset
|
2983 other = (PyObject*)((vector_elementwiseproxy*)other)->vec; |
| 1848 | 2984 op |= OP_ARG_VECTOR; |
|
1841
a7dc31e50c46
* added the vetor_elementwiseproxy which lets you apply calculations on
lorenz
parents:
1840
diff
changeset
|
2985 } |
| 1848 | 2986 else if (PyVectorCompatible_Check(other, dim)) |
| 2987 op |= OP_ARG_VECTOR; | |
| 2988 else if (RealNumber_Check(other)) | |
| 2989 op |= OP_ARG_NUMBER; | |
| 2990 else | |
| 2991 op |= OP_ARG_UNKNOWN; | |
| 2992 | |
| 2993 switch (op) { | |
| 2994 case OP_ADD | OP_ARG_VECTOR: | |
| 2995 case OP_ADD | OP_ARG_VECTOR | OP_ARG_REVERSE: | |
| 2996 return vector_add((PyObject*)vec, other); | |
| 2997 case OP_ADD | OP_ARG_NUMBER: | |
| 2998 case OP_ADD | OP_ARG_NUMBER | OP_ARG_REVERSE: | |
| 2999 tmp = PyFloat_AsDouble(other); | |
| 3000 ret = (PyVector*)PyVector_NEW(dim); | |
| 3001 for (i = 0; i < dim; i++) | |
| 3002 ret->coords[i] = vec->coords[i] + tmp; | |
| 3003 return (PyObject*)ret; | |
| 3004 case OP_SUB | OP_ARG_VECTOR: | |
| 3005 return vector_sub((PyObject*)vec, other); | |
| 3006 case OP_SUB | OP_ARG_VECTOR | OP_ARG_REVERSE: | |
| 3007 return vector_sub(other, (PyObject*)vec); | |
| 3008 case OP_SUB | OP_ARG_NUMBER: | |
| 3009 tmp = PyFloat_AsDouble(other); | |
| 3010 ret = (PyVector*)PyVector_NEW(dim); | |
| 3011 for (i = 0; i < dim; i++) | |
| 3012 ret->coords[i] = vec->coords[i] - tmp; | |
| 3013 return (PyObject*)ret; | |
| 3014 case OP_SUB | OP_ARG_NUMBER | OP_ARG_REVERSE: | |
| 3015 tmp = PyFloat_AsDouble(other); | |
| 3016 ret = (PyVector*)PyVector_NEW(dim); | |
| 3017 for (i = 0; i < dim; i++) | |
| 3018 ret->coords[i] = tmp - vec->coords[i]; | |
| 3019 return (PyObject*)ret; | |
| 3020 case OP_MUL | OP_ARG_VECTOR: | |
| 3021 case OP_MUL | OP_ARG_VECTOR | OP_ARG_REVERSE: | |
|
1842
5418dd349248
refactored the vector_elementwiseproxy functions and fixed some bugs along the way
lorenz
parents:
1841
diff
changeset
|
3022 ret = (PyVector*)PyVector_NEW(vec->dim); |
|
5418dd349248
refactored the vector_elementwiseproxy functions and fixed some bugs along the way
lorenz
parents:
1841
diff
changeset
|
3023 for (i = 0; i < vec->dim; i++) { |
|
5418dd349248
refactored the vector_elementwiseproxy functions and fixed some bugs along the way
lorenz
parents:
1841
diff
changeset
|
3024 ret->coords[i] = (vec->coords[i] * |
|
1841
a7dc31e50c46
* added the vetor_elementwiseproxy which lets you apply calculations on
lorenz
parents:
1840
diff
changeset
|
3025 PySequence_GetItem_AsDouble(other, i)); |
|
a7dc31e50c46
* added the vetor_elementwiseproxy which lets you apply calculations on
lorenz
parents:
1840
diff
changeset
|
3026 } |
|
a7dc31e50c46
* added the vetor_elementwiseproxy which lets you apply calculations on
lorenz
parents:
1840
diff
changeset
|
3027 return (PyObject*)ret; |
| 1848 | 3028 case OP_MUL | OP_ARG_NUMBER: |
| 3029 case OP_MUL | OP_ARG_NUMBER | OP_ARG_REVERSE: | |
|
1842
5418dd349248
refactored the vector_elementwiseproxy functions and fixed some bugs along the way
lorenz
parents:
1841
diff
changeset
|
3030 return vector_mul((PyObject*)vec, other); |
| 1848 | 3031 case OP_DIV | OP_ARG_VECTOR: |
| 3032 ret = (PyVector*)PyVector_NEW(vec->dim); | |
| 3033 for (i = 0; i < vec->dim; i++) { | |
| 3034 ret->coords[i] = (vec->coords[i] / | |
| 3035 PySequence_GetItem_AsDouble(other, i)); | |
| 3036 } | |
| 3037 return (PyObject*)ret; | |
| 3038 case OP_DIV | OP_ARG_VECTOR | OP_ARG_REVERSE: | |
| 3039 ret = (PyVector*)PyVector_NEW(vec->dim); | |
| 3040 for (i = 0; i < vec->dim; i++) { | |
| 3041 ret->coords[i] = (PySequence_GetItem_AsDouble(o1, i) / | |
| 3042 vec->coords[i]); | |
| 3043 } | |
| 3044 return (PyObject*)ret; | |
| 3045 case OP_DIV | OP_ARG_NUMBER: | |
| 3046 return vector_div(vec, other); | |
| 3047 case OP_DIV | OP_ARG_NUMBER | OP_ARG_REVERSE: | |
| 3048 tmp = PyFloat_AsDouble(o1); | |
| 3049 ret = (PyVector*)PyVector_NEW(vec->dim); | |
| 3050 for (i = 0; i < vec->dim; i++) | |
| 3051 ret->coords[i] = tmp / vec->coords[i]; | |
| 3052 return (PyObject*)ret; | |
| 3053 case OP_FLOOR_DIV | OP_ARG_VECTOR: | |
| 3054 ret = (PyVector*)PyVector_NEW(vec->dim); | |
| 3055 for (i = 0; i < vec->dim; i++) { | |
| 3056 ret->coords[i] = floor(vec->coords[i] / | |
| 3057 PySequence_GetItem_AsDouble(other, i)); | |
| 3058 } | |
| 3059 return (PyObject*)ret; | |
| 3060 case OP_FLOOR_DIV | OP_ARG_VECTOR | OP_ARG_REVERSE: | |
| 3061 ret = (PyVector*)PyVector_NEW(vec->dim); | |
| 3062 for (i = 0; i < vec->dim; i++) { | |
| 3063 ret->coords[i] = floor(PySequence_GetItem_AsDouble(o1, i) / | |
| 3064 vec->coords[i]); | |
| 3065 } | |
| 3066 return (PyObject*)ret; | |
| 3067 case OP_FLOOR_DIV | OP_ARG_NUMBER: | |
| 3068 return vector_floor_div(vec, other); | |
| 3069 case OP_FLOOR_DIV | OP_ARG_NUMBER | OP_ARG_REVERSE: | |
| 3070 tmp = PyFloat_AsDouble(other); | |
| 3071 ret = (PyVector*)PyVector_NEW(vec->dim); | |
| 3072 for (i = 0; i < vec->dim; i++) | |
| 3073 ret->coords[i] = floor(tmp / vec->coords[i]); | |
| 3074 return (PyObject*)ret; | |
| 3075 case OP_MOD | OP_ARG_VECTOR: | |
| 3076 ret = (PyVector*)PyVector_NEW(vec->dim); | |
| 3077 for (i = 0; i < vec->dim; i++) { | |
| 3078 tmp = PySequence_GetItem_AsDouble(other, i); | |
| 3079 mod = fmod(vec->coords[i], tmp); | |
| 3080 /* note: checking mod*value < 0 is incorrect -- underflows to | |
| 3081 0 if value < sqrt(smallest nonzero double) */ | |
| 3082 if (mod && ((tmp < 0) != (mod < 0))) { | |
| 3083 mod += tmp; | |
| 3084 } | |
| 3085 ret->coords[i] = mod; | |
| 3086 } | |
| 3087 return (PyObject*)ret; | |
| 3088 case OP_MOD | OP_ARG_VECTOR | OP_ARG_REVERSE: | |
| 3089 ret = (PyVector*)PyVector_NEW(vec->dim); | |
| 3090 for (i = 0; i < vec->dim; i++) { | |
| 3091 tmp = PySequence_GetItem_AsDouble(other, i); | |
| 3092 mod = fmod(tmp, vec->coords[i]); | |
| 3093 /* note: see above */ | |
| 3094 if (mod && ((vec->coords[i] < 0) != (mod < 0))) { | |
| 3095 mod += vec->coords[i]; | |
| 3096 } | |
| 3097 ret->coords[i] = mod; | |
| 3098 } | |
| 3099 return (PyObject*)ret; | |
| 3100 case OP_MOD | OP_ARG_NUMBER: | |
| 3101 ret = (PyVector*)PyVector_NEW(vec->dim); | |
| 3102 tmp = PyFloat_AsDouble(other); | |
| 3103 for (i = 0; i < vec->dim; i++) { | |
| 3104 mod = fmod(vec->coords[i], tmp); | |
| 3105 /* note: see above */ | |
| 3106 if (mod && ((tmp < 0) != (mod < 0))) { | |
| 3107 mod += tmp; | |
| 3108 } | |
| 3109 ret->coords[i] = mod; | |
| 3110 } | |
| 3111 return (PyObject*)ret; | |
| 3112 case OP_MOD | OP_ARG_NUMBER | OP_ARG_REVERSE: | |
| 3113 ret = (PyVector*)PyVector_NEW(vec->dim); | |
| 3114 tmp = PyFloat_AsDouble(other); | |
| 3115 for (i = 0; i < vec->dim; i++) { | |
| 3116 mod = fmod(tmp, vec->coords[i]); | |
| 3117 /* note: see above */ | |
| 3118 if (mod && ((vec->coords[i] < 0) != (mod < 0))) { | |
| 3119 mod += vec->coords[i]; | |
| 3120 } | |
| 3121 ret->coords[i] = mod; | |
| 3122 } | |
| 3123 return (PyObject*)ret; | |
| 3124 default: | |
| 3125 Py_INCREF(Py_NotImplemented); | |
| 3126 return Py_NotImplemented; | |
|
1841
a7dc31e50c46
* added the vetor_elementwiseproxy which lets you apply calculations on
lorenz
parents:
1840
diff
changeset
|
3127 } |
| 1848 | 3128 } |
| 3129 | |
| 3130 static PyObject * | |
| 3131 vector_elementwiseproxy_add(PyObject *o1, PyObject *o2) | |
| 3132 { | |
| 3133 return vector_elementwiseproxy_generic_math(o1, o2, OP_ADD); | |
|
1841
a7dc31e50c46
* added the vetor_elementwiseproxy which lets you apply calculations on
lorenz
parents:
1840
diff
changeset
|
3134 } |
| 1848 | 3135 static PyObject * |
| 3136 vector_elementwiseproxy_sub(PyObject *o1, PyObject *o2) | |
| 3137 { | |
| 3138 return vector_elementwiseproxy_generic_math(o1, o2, OP_SUB); | |
| 3139 } | |
| 3140 static PyObject * | |
| 3141 vector_elementwiseproxy_mul(PyObject *o1, PyObject *o2) | |
| 3142 { | |
| 3143 return vector_elementwiseproxy_generic_math(o1, o2, OP_MUL); | |
| 3144 } | |
|
1841
a7dc31e50c46
* added the vetor_elementwiseproxy which lets you apply calculations on
lorenz
parents:
1840
diff
changeset
|
3145 static PyObject * |
|
a7dc31e50c46
* added the vetor_elementwiseproxy which lets you apply calculations on
lorenz
parents:
1840
diff
changeset
|
3146 vector_elementwiseproxy_div(PyObject *o1, PyObject *o2) |
|
a7dc31e50c46
* added the vetor_elementwiseproxy which lets you apply calculations on
lorenz
parents:
1840
diff
changeset
|
3147 { |
| 1848 | 3148 return vector_elementwiseproxy_generic_math(o1, o2, OP_DIV); |
|
1841
a7dc31e50c46
* added the vetor_elementwiseproxy which lets you apply calculations on
lorenz
parents:
1840
diff
changeset
|
3149 } |
|
a7dc31e50c46
* added the vetor_elementwiseproxy which lets you apply calculations on
lorenz
parents:
1840
diff
changeset
|
3150 static PyObject * |
|
a7dc31e50c46
* added the vetor_elementwiseproxy which lets you apply calculations on
lorenz
parents:
1840
diff
changeset
|
3151 vector_elementwiseproxy_floor_div(PyObject *o1, PyObject *o2) |
|
a7dc31e50c46
* added the vetor_elementwiseproxy which lets you apply calculations on
lorenz
parents:
1840
diff
changeset
|
3152 { |
| 1848 | 3153 return vector_elementwiseproxy_generic_math(o1, o2, OP_FLOOR_DIV); |
| 3154 } | |
| 3155 static PyObject * | |
| 3156 vector_elementwiseproxy_mod(PyObject *o1, PyObject *o2) | |
| 3157 { | |
| 3158 return vector_elementwiseproxy_generic_math(o1, o2, OP_MOD); | |
|
1841
a7dc31e50c46
* added the vetor_elementwiseproxy which lets you apply calculations on
lorenz
parents:
1840
diff
changeset
|
3159 } |
|
a7dc31e50c46
* added the vetor_elementwiseproxy which lets you apply calculations on
lorenz
parents:
1840
diff
changeset
|
3160 |
|
a7dc31e50c46
* added the vetor_elementwiseproxy which lets you apply calculations on
lorenz
parents:
1840
diff
changeset
|
3161 static PyObject * |
|
1842
5418dd349248
refactored the vector_elementwiseproxy functions and fixed some bugs along the way
lorenz
parents:
1841
diff
changeset
|
3162 vector_elementwiseproxy_pow(PyObject *baseObj, PyObject *expoObj, PyObject *mod) |
|
1841
a7dc31e50c46
* added the vetor_elementwiseproxy which lets you apply calculations on
lorenz
parents:
1840
diff
changeset
|
3163 { |
|
1842
5418dd349248
refactored the vector_elementwiseproxy functions and fixed some bugs along the way
lorenz
parents:
1841
diff
changeset
|
3164 int i, dim; |
|
5418dd349248
refactored the vector_elementwiseproxy functions and fixed some bugs along the way
lorenz
parents:
1841
diff
changeset
|
3165 double tmp; |
|
5418dd349248
refactored the vector_elementwiseproxy functions and fixed some bugs along the way
lorenz
parents:
1841
diff
changeset
|
3166 double bases[VECTOR_MAX_SIZE]; |
|
5418dd349248
refactored the vector_elementwiseproxy functions and fixed some bugs along the way
lorenz
parents:
1841
diff
changeset
|
3167 double expos[VECTOR_MAX_SIZE]; |
|
5418dd349248
refactored the vector_elementwiseproxy functions and fixed some bugs along the way
lorenz
parents:
1841
diff
changeset
|
3168 PyVector *ret, *vec; |
|
5418dd349248
refactored the vector_elementwiseproxy functions and fixed some bugs along the way
lorenz
parents:
1841
diff
changeset
|
3169 PyObject *base, *expo, *result; |
|
5418dd349248
refactored the vector_elementwiseproxy functions and fixed some bugs along the way
lorenz
parents:
1841
diff
changeset
|
3170 if (mod != Py_None) { |
|
5418dd349248
refactored the vector_elementwiseproxy functions and fixed some bugs along the way
lorenz
parents:
1841
diff
changeset
|
3171 PyErr_SetString(PyExc_TypeError, "pow() 3rd argument not " |
|
5418dd349248
refactored the vector_elementwiseproxy functions and fixed some bugs along the way
lorenz
parents:
1841
diff
changeset
|
3172 "allowed for vectors"); |
|
1841
a7dc31e50c46
* added the vetor_elementwiseproxy which lets you apply calculations on
lorenz
parents:
1840
diff
changeset
|
3173 return NULL; |
|
a7dc31e50c46
* added the vetor_elementwiseproxy which lets you apply calculations on
lorenz
parents:
1840
diff
changeset
|
3174 } |
|
1842
5418dd349248
refactored the vector_elementwiseproxy functions and fixed some bugs along the way
lorenz
parents:
1841
diff
changeset
|
3175 |
|
5418dd349248
refactored the vector_elementwiseproxy functions and fixed some bugs along the way
lorenz
parents:
1841
diff
changeset
|
3176 if (vector_elementwiseproxy_Check(baseObj)) { |
|
5418dd349248
refactored the vector_elementwiseproxy functions and fixed some bugs along the way
lorenz
parents:
1841
diff
changeset
|
3177 dim = ((vector_elementwiseproxy*)baseObj)->vec->dim; |
|
5418dd349248
refactored the vector_elementwiseproxy functions and fixed some bugs along the way
lorenz
parents:
1841
diff
changeset
|
3178 memcpy(bases, ((vector_elementwiseproxy*)baseObj)->vec->coords, |
|
5418dd349248
refactored the vector_elementwiseproxy functions and fixed some bugs along the way
lorenz
parents:
1841
diff
changeset
|
3179 sizeof(double) * dim); |
|
5418dd349248
refactored the vector_elementwiseproxy functions and fixed some bugs along the way
lorenz
parents:
1841
diff
changeset
|
3180 if (vector_elementwiseproxy_Check(expoObj)) { |
|
5418dd349248
refactored the vector_elementwiseproxy functions and fixed some bugs along the way
lorenz
parents:
1841
diff
changeset
|
3181 memcpy(expos, ((vector_elementwiseproxy*)expoObj)->vec->coords, |
|
5418dd349248
refactored the vector_elementwiseproxy functions and fixed some bugs along the way
lorenz
parents:
1841
diff
changeset
|
3182 sizeof(double) * dim); |
|
5418dd349248
refactored the vector_elementwiseproxy functions and fixed some bugs along the way
lorenz
parents:
1841
diff
changeset
|
3183 } |
|
1845
7d2a16e6a1d3
rearanged math module. added forward declarations at the beginning.
lorenz
parents:
1844
diff
changeset
|
3184 else if (PyVectorCompatible_Check(expoObj, dim)) { |
|
1842
5418dd349248
refactored the vector_elementwiseproxy functions and fixed some bugs along the way
lorenz
parents:
1841
diff
changeset
|
3185 PySequence_AsVectorCoords(expoObj, expos, dim); |
|
5418dd349248
refactored the vector_elementwiseproxy functions and fixed some bugs along the way
lorenz
parents:
1841
diff
changeset
|
3186 } |
|
5418dd349248
refactored the vector_elementwiseproxy functions and fixed some bugs along the way
lorenz
parents:
1841
diff
changeset
|
3187 else if (RealNumber_Check(expoObj)) { |
|
5418dd349248
refactored the vector_elementwiseproxy functions and fixed some bugs along the way
lorenz
parents:
1841
diff
changeset
|
3188 tmp = PyFloat_AsDouble(expoObj); |
|
5418dd349248
refactored the vector_elementwiseproxy functions and fixed some bugs along the way
lorenz
parents:
1841
diff
changeset
|
3189 for (i = 0; i < dim; i++) |
|
5418dd349248
refactored the vector_elementwiseproxy functions and fixed some bugs along the way
lorenz
parents:
1841
diff
changeset
|
3190 expos[i] = tmp; |
|
1841
a7dc31e50c46
* added the vetor_elementwiseproxy which lets you apply calculations on
lorenz
parents:
1840
diff
changeset
|
3191 } |
|
1842
5418dd349248
refactored the vector_elementwiseproxy functions and fixed some bugs along the way
lorenz
parents:
1841
diff
changeset
|
3192 else |
|
5418dd349248
refactored the vector_elementwiseproxy functions and fixed some bugs along the way
lorenz
parents:
1841
diff
changeset
|
3193 goto NOT_IMPLEMENTED; |
|
1841
a7dc31e50c46
* added the vetor_elementwiseproxy which lets you apply calculations on
lorenz
parents:
1840
diff
changeset
|
3194 } |
|
1842
5418dd349248
refactored the vector_elementwiseproxy functions and fixed some bugs along the way
lorenz
parents:
1841
diff
changeset
|
3195 else { |
|
5418dd349248
refactored the vector_elementwiseproxy functions and fixed some bugs along the way
lorenz
parents:
1841
diff
changeset
|
3196 dim = ((vector_elementwiseproxy*)expoObj)->vec->dim; |
|
5418dd349248
refactored the vector_elementwiseproxy functions and fixed some bugs along the way
lorenz
parents:
1841
diff
changeset
|
3197 memcpy(expos, ((vector_elementwiseproxy*)expoObj)->vec->coords, |
|
5418dd349248
refactored the vector_elementwiseproxy functions and fixed some bugs along the way
lorenz
parents:
1841
diff
changeset
|
3198 sizeof(double) * dim); |
|
1845
7d2a16e6a1d3
rearanged math module. added forward declarations at the beginning.
lorenz
parents:
1844
diff
changeset
|
3199 if (PyVectorCompatible_Check(baseObj, dim)) { |
|
1842
5418dd349248
refactored the vector_elementwiseproxy functions and fixed some bugs along the way
lorenz
parents:
1841
diff
changeset
|
3200 PySequence_AsVectorCoords(baseObj, bases, dim); |
|
5418dd349248
refactored the vector_elementwiseproxy functions and fixed some bugs along the way
lorenz
parents:
1841
diff
changeset
|
3201 } |
|
5418dd349248
refactored the vector_elementwiseproxy functions and fixed some bugs along the way
lorenz
parents:
1841
diff
changeset
|
3202 else if (RealNumber_Check(baseObj)) { |
|
5418dd349248
refactored the vector_elementwiseproxy functions and fixed some bugs along the way
lorenz
parents:
1841
diff
changeset
|
3203 tmp = PyFloat_AsDouble(baseObj); |
|
5418dd349248
refactored the vector_elementwiseproxy functions and fixed some bugs along the way
lorenz
parents:
1841
diff
changeset
|
3204 for (i = 0; i < dim; i++) |
|
5418dd349248
refactored the vector_elementwiseproxy functions and fixed some bugs along the way
lorenz
parents:
1841
diff
changeset
|
3205 bases[i] = tmp; |
|
5418dd349248
refactored the vector_elementwiseproxy functions and fixed some bugs along the way
lorenz
parents:
1841
diff
changeset
|
3206 } |
|
5418dd349248
refactored the vector_elementwiseproxy functions and fixed some bugs along the way
lorenz
parents:
1841
diff
changeset
|
3207 else |
|
5418dd349248
refactored the vector_elementwiseproxy functions and fixed some bugs along the way
lorenz
parents:
1841
diff
changeset
|
3208 goto NOT_IMPLEMENTED; |
|
5418dd349248
refactored the vector_elementwiseproxy functions and fixed some bugs along the way
lorenz
parents:
1841
diff
changeset
|
3209 } |
|
5418dd349248
refactored the vector_elementwiseproxy functions and fixed some bugs along the way
lorenz
parents:
1841
diff
changeset
|
3210 |
|
5418dd349248
refactored the vector_elementwiseproxy functions and fixed some bugs along the way
lorenz
parents:
1841
diff
changeset
|
3211 ret = (PyVector*)PyVector_NEW(dim); |
|
5418dd349248
refactored the vector_elementwiseproxy functions and fixed some bugs along the way
lorenz
parents:
1841
diff
changeset
|
3212 /* there are many special cases so we let python do the work for now */ |
|
5418dd349248
refactored the vector_elementwiseproxy functions and fixed some bugs along the way
lorenz
parents:
1841
diff
changeset
|
3213 for (i = 0; i < dim; i++) { |
|
5418dd349248
refactored the vector_elementwiseproxy functions and fixed some bugs along the way
lorenz
parents:
1841
diff
changeset
|
3214 base = PyFloat_FromDouble(bases[i]); |
|
5418dd349248
refactored the vector_elementwiseproxy functions and fixed some bugs along the way
lorenz
parents:
1841
diff
changeset
|
3215 expo = PyFloat_FromDouble(expos[i]); |
|
5418dd349248
refactored the vector_elementwiseproxy functions and fixed some bugs along the way
lorenz
parents:
1841
diff
changeset
|
3216 result = PyNumber_Power(base, expo, Py_None); |
|
5418dd349248
refactored the vector_elementwiseproxy functions and fixed some bugs along the way
lorenz
parents:
1841
diff
changeset
|
3217 if (!result) |
|
5418dd349248
refactored the vector_elementwiseproxy functions and fixed some bugs along the way
lorenz
parents:
1841
diff
changeset
|
3218 return NULL; |
|
5418dd349248
refactored the vector_elementwiseproxy functions and fixed some bugs along the way
lorenz
parents:
1841
diff
changeset
|
3219 ret->coords[i] = PyFloat_AsDouble(result); |
|
5418dd349248
refactored the vector_elementwiseproxy functions and fixed some bugs along the way
lorenz
parents:
1841
diff
changeset
|
3220 Py_DECREF(result); |
|
5418dd349248
refactored the vector_elementwiseproxy functions and fixed some bugs along the way
lorenz
parents:
1841
diff
changeset
|
3221 Py_DECREF(expo); |
|
5418dd349248
refactored the vector_elementwiseproxy functions and fixed some bugs along the way
lorenz
parents:
1841
diff
changeset
|
3222 Py_DECREF(base); |
|
1841
a7dc31e50c46
* added the vetor_elementwiseproxy which lets you apply calculations on
lorenz
parents:
1840
diff
changeset
|
3223 } |
|
a7dc31e50c46
* added the vetor_elementwiseproxy which lets you apply calculations on
lorenz
parents:
1840
diff
changeset
|
3224 return (PyObject*)ret; |
|
1842
5418dd349248
refactored the vector_elementwiseproxy functions and fixed some bugs along the way
lorenz
parents:
1841
diff
changeset
|
3225 |
|
5418dd349248
refactored the vector_elementwiseproxy functions and fixed some bugs along the way
lorenz
parents:
1841
diff
changeset
|
3226 NOT_IMPLEMENTED: |
|
5418dd349248
refactored the vector_elementwiseproxy functions and fixed some bugs along the way
lorenz
parents:
1841
diff
changeset
|
3227 Py_INCREF(Py_NotImplemented); |
|
5418dd349248
refactored the vector_elementwiseproxy functions and fixed some bugs along the way
lorenz
parents:
1841
diff
changeset
|
3228 return Py_NotImplemented; |
|
1841
a7dc31e50c46
* added the vetor_elementwiseproxy which lets you apply calculations on
lorenz
parents:
1840
diff
changeset
|
3229 } |
|
a7dc31e50c46
* added the vetor_elementwiseproxy which lets you apply calculations on
lorenz
parents:
1840
diff
changeset
|
3230 |
|
a7dc31e50c46
* added the vetor_elementwiseproxy which lets you apply calculations on
lorenz
parents:
1840
diff
changeset
|
3231 |
|
a7dc31e50c46
* added the vetor_elementwiseproxy which lets you apply calculations on
lorenz
parents:
1840
diff
changeset
|
3232 static PyObject * |
|
a7dc31e50c46
* added the vetor_elementwiseproxy which lets you apply calculations on
lorenz
parents:
1840
diff
changeset
|
3233 vector_elementwiseproxy_abs(vector_elementwiseproxy *self) |
|
a7dc31e50c46
* added the vetor_elementwiseproxy which lets you apply calculations on
lorenz
parents:
1840
diff
changeset
|
3234 { |
|
a7dc31e50c46
* added the vetor_elementwiseproxy which lets you apply calculations on
lorenz
parents:
1840
diff
changeset
|
3235 int i; |
|
a7dc31e50c46
* added the vetor_elementwiseproxy which lets you apply calculations on
lorenz
parents:
1840
diff
changeset
|
3236 PyVector *ret = (PyVector*)PyVector_NEW(self->vec->dim); |
|
a7dc31e50c46
* added the vetor_elementwiseproxy which lets you apply calculations on
lorenz
parents:
1840
diff
changeset
|
3237 for (i = 0; i < self->vec->dim; i++) { |
|
a7dc31e50c46
* added the vetor_elementwiseproxy which lets you apply calculations on
lorenz
parents:
1840
diff
changeset
|
3238 ret->coords[i] = fabs(self->vec->coords[i]); |
|
a7dc31e50c46
* added the vetor_elementwiseproxy which lets you apply calculations on
lorenz
parents:
1840
diff
changeset
|
3239 } |
|
a7dc31e50c46
* added the vetor_elementwiseproxy which lets you apply calculations on
lorenz
parents:
1840
diff
changeset
|
3240 return (PyObject*)ret; |
|
a7dc31e50c46
* added the vetor_elementwiseproxy which lets you apply calculations on
lorenz
parents:
1840
diff
changeset
|
3241 } |
|
a7dc31e50c46
* added the vetor_elementwiseproxy which lets you apply calculations on
lorenz
parents:
1840
diff
changeset
|
3242 |
|
a7dc31e50c46
* added the vetor_elementwiseproxy which lets you apply calculations on
lorenz
parents:
1840
diff
changeset
|
3243 static PyObject * |
|
a7dc31e50c46
* added the vetor_elementwiseproxy which lets you apply calculations on
lorenz
parents:
1840
diff
changeset
|
3244 vector_elementwiseproxy_neg(vector_elementwiseproxy *self) |
|
a7dc31e50c46
* added the vetor_elementwiseproxy which lets you apply calculations on
lorenz
parents:
1840
diff
changeset
|
3245 { |
|
a7dc31e50c46
* added the vetor_elementwiseproxy which lets you apply calculations on
lorenz
parents:
1840
diff
changeset
|
3246 int i; |
|
a7dc31e50c46
* added the vetor_elementwiseproxy which lets you apply calculations on
lorenz
parents:
1840
diff
changeset
|
3247 PyVector *ret = (PyVector*)PyVector_NEW(self->vec->dim); |
|
a7dc31e50c46
* added the vetor_elementwiseproxy which lets you apply calculations on
lorenz
parents:
1840
diff
changeset
|
3248 for (i = 0; i < self->vec->dim; i++) { |
|
a7dc31e50c46
* added the vetor_elementwiseproxy which lets you apply calculations on
lorenz
parents:
1840
diff
changeset
|
3249 ret->coords[i] = -self->vec->coords[i]; |
|
a7dc31e50c46
* added the vetor_elementwiseproxy which lets you apply calculations on
lorenz
parents:
1840
diff
changeset
|
3250 } |
|
a7dc31e50c46
* added the vetor_elementwiseproxy which lets you apply calculations on
lorenz
parents:
1840
diff
changeset
|
3251 return (PyObject*)ret; |
|
a7dc31e50c46
* added the vetor_elementwiseproxy which lets you apply calculations on
lorenz
parents:
1840
diff
changeset
|
3252 } |
|
a7dc31e50c46
* added the vetor_elementwiseproxy which lets you apply calculations on
lorenz
parents:
1840
diff
changeset
|
3253 |
|
a7dc31e50c46
* added the vetor_elementwiseproxy which lets you apply calculations on
lorenz
parents:
1840
diff
changeset
|
3254 static PyObject * |
|
a7dc31e50c46
* added the vetor_elementwiseproxy which lets you apply calculations on
lorenz
parents:
1840
diff
changeset
|
3255 vector_elementwiseproxy_pos(vector_elementwiseproxy *self) |
|
a7dc31e50c46
* added the vetor_elementwiseproxy which lets you apply calculations on
lorenz
parents:
1840
diff
changeset
|
3256 { |
|
a7dc31e50c46
* added the vetor_elementwiseproxy which lets you apply calculations on
lorenz
parents:
1840
diff
changeset
|
3257 int i; |
|
a7dc31e50c46
* added the vetor_elementwiseproxy which lets you apply calculations on
lorenz
parents:
1840
diff
changeset
|
3258 PyVector *ret = (PyVector*)PyVector_NEW(self->vec->dim); |
|
a7dc31e50c46
* added the vetor_elementwiseproxy which lets you apply calculations on
lorenz
parents:
1840
diff
changeset
|
3259 memcpy(ret->coords, self->vec->coords, sizeof(ret->coords[0]) * ret->dim); |
|
a7dc31e50c46
* added the vetor_elementwiseproxy which lets you apply calculations on
lorenz
parents:
1840
diff
changeset
|
3260 return (PyObject*)ret; |
|
a7dc31e50c46
* added the vetor_elementwiseproxy which lets you apply calculations on
lorenz
parents:
1840
diff
changeset
|
3261 } |
|
a7dc31e50c46
* added the vetor_elementwiseproxy which lets you apply calculations on
lorenz
parents:
1840
diff
changeset
|
3262 |
|
a7dc31e50c46
* added the vetor_elementwiseproxy which lets you apply calculations on
lorenz
parents:
1840
diff
changeset
|
3263 static int |
|
a7dc31e50c46
* added the vetor_elementwiseproxy which lets you apply calculations on
lorenz
parents:
1840
diff
changeset
|
3264 vector_elementwiseproxy_nonzero(vector_elementwiseproxy *self) |
|
a7dc31e50c46
* added the vetor_elementwiseproxy which lets you apply calculations on
lorenz
parents:
1840
diff
changeset
|
3265 { |
|
a7dc31e50c46
* added the vetor_elementwiseproxy which lets you apply calculations on
lorenz
parents:
1840
diff
changeset
|
3266 int i; |
|
a7dc31e50c46
* added the vetor_elementwiseproxy which lets you apply calculations on
lorenz
parents:
1840
diff
changeset
|
3267 for (i = 0; i < self->vec->dim; i++) { |
|
a7dc31e50c46
* added the vetor_elementwiseproxy which lets you apply calculations on
lorenz
parents:
1840
diff
changeset
|
3268 if (fabs(self->vec->coords[i]) > self->vec->epsilon) { |
|
a7dc31e50c46
* added the vetor_elementwiseproxy which lets you apply calculations on
lorenz
parents:
1840
diff
changeset
|
3269 return 1; |
|
a7dc31e50c46
* added the vetor_elementwiseproxy which lets you apply calculations on
lorenz
parents:
1840
diff
changeset
|
3270 } |
|
a7dc31e50c46
* added the vetor_elementwiseproxy which lets you apply calculations on
lorenz
parents:
1840
diff
changeset
|
3271 } |
|
a7dc31e50c46
* added the vetor_elementwiseproxy which lets you apply calculations on
lorenz
parents:
1840
diff
changeset
|
3272 return 0; |
|
a7dc31e50c46
* added the vetor_elementwiseproxy which lets you apply calculations on
lorenz
parents:
1840
diff
changeset
|
3273 } |
|
a7dc31e50c46
* added the vetor_elementwiseproxy which lets you apply calculations on
lorenz
parents:
1840
diff
changeset
|
3274 |
|
1845
7d2a16e6a1d3
rearanged math module. added forward declarations at the beginning.
lorenz
parents:
1844
diff
changeset
|
3275 static PyNumberMethods vector_elementwiseproxy_as_number = { |
|
1841
a7dc31e50c46
* added the vetor_elementwiseproxy which lets you apply calculations on
lorenz
parents:
1840
diff
changeset
|
3276 (binaryfunc)vector_elementwiseproxy_add, /* nb_add; __add__ */ |
|
a7dc31e50c46
* added the vetor_elementwiseproxy which lets you apply calculations on
lorenz
parents:
1840
diff
changeset
|
3277 (binaryfunc)vector_elementwiseproxy_sub, /* nb_subtract; __sub__ */ |
|
a7dc31e50c46
* added the vetor_elementwiseproxy which lets you apply calculations on
lorenz
parents:
1840
diff
changeset
|
3278 (binaryfunc)vector_elementwiseproxy_mul, /* nb_multiply; __mul__ */ |
|
a7dc31e50c46
* added the vetor_elementwiseproxy which lets you apply calculations on
lorenz
parents:
1840
diff
changeset
|
3279 (binaryfunc)vector_elementwiseproxy_div, /* nb_divide; __div__ */ |
|
a7dc31e50c46
* added the vetor_elementwiseproxy which lets you apply calculations on
lorenz
parents:
1840
diff
changeset
|
3280 (binaryfunc)vector_elementwiseproxy_mod, /* nb_remainder; __mod__ */ |
|
a7dc31e50c46
* added the vetor_elementwiseproxy which lets you apply calculations on
lorenz
parents:
1840
diff
changeset
|
3281 (binaryfunc)0, /* nb_divmod; __divmod__ */ |
|
a7dc31e50c46
* added the vetor_elementwiseproxy which lets you apply calculations on
lorenz
parents:
1840
diff
changeset
|
3282 (ternaryfunc)vector_elementwiseproxy_pow, /* nb_power; __pow__ */ |
|
a7dc31e50c46
* added the vetor_elementwiseproxy which lets you apply calculations on
lorenz
parents:
1840
diff
changeset
|
3283 (unaryfunc)vector_elementwiseproxy_neg, /* nb_negative; __neg__ */ |
|
a7dc31e50c46
* added the vetor_elementwiseproxy which lets you apply calculations on
lorenz
parents:
1840
diff
changeset
|
3284 (unaryfunc)vector_elementwiseproxy_pos, /* nb_positive; __pos__ */ |
|
a7dc31e50c46
* added the vetor_elementwiseproxy which lets you apply calculations on
lorenz
parents:
1840
diff
changeset
|
3285 (unaryfunc)vector_elementwiseproxy_abs, /* nb_absolute; __abs__ */ |
|
a7dc31e50c46
* added the vetor_elementwiseproxy which lets you apply calculations on
lorenz
parents:
1840
diff
changeset
|
3286 (inquiry)vector_elementwiseproxy_nonzero, /* nb_nonzero; __nonzero__ */ |
|
a7dc31e50c46
* added the vetor_elementwiseproxy which lets you apply calculations on
lorenz
parents:
1840
diff
changeset
|
3287 (unaryfunc)0, /* nb_invert; __invert__ */ |
|
a7dc31e50c46
* added the vetor_elementwiseproxy which lets you apply calculations on
lorenz
parents:
1840
diff
changeset
|
3288 (binaryfunc)0, /* nb_lshift; __lshift__ */ |
|
a7dc31e50c46
* added the vetor_elementwiseproxy which lets you apply calculations on
lorenz
parents:
1840
diff
changeset
|
3289 (binaryfunc)0, /* nb_rshift; __rshift__ */ |
|
a7dc31e50c46
* added the vetor_elementwiseproxy which lets you apply calculations on
lorenz
parents:
1840
diff
changeset
|
3290 (binaryfunc)0, /* nb_and; __and__ */ |
|
a7dc31e50c46
* added the vetor_elementwiseproxy which lets you apply calculations on
lorenz
parents:
1840
diff
changeset
|
3291 (binaryfunc)0, /* nb_xor; __xor__ */ |
|
a7dc31e50c46
* added the vetor_elementwiseproxy which lets you apply calculations on
lorenz
parents:
1840
diff
changeset
|
3292 (binaryfunc)0, /* nb_or; __or__ */ |
|
a7dc31e50c46
* added the vetor_elementwiseproxy which lets you apply calculations on
lorenz
parents:
1840
diff
changeset
|
3293 (coercion)0, /* nb_coerce; __coerce__ */ |
|
a7dc31e50c46
* added the vetor_elementwiseproxy which lets you apply calculations on
lorenz
parents:
1840
diff
changeset
|
3294 (unaryfunc)0, /* nb_int; __int__ */ |
|
a7dc31e50c46
* added the vetor_elementwiseproxy which lets you apply calculations on
lorenz
parents:
1840
diff
changeset
|
3295 (unaryfunc)0, /* nb_long; __long__ */ |
|
a7dc31e50c46
* added the vetor_elementwiseproxy which lets you apply calculations on
lorenz
parents:
1840
diff
changeset
|
3296 (unaryfunc)0, /* nb_float; __float__ */ |
|
a7dc31e50c46
* added the vetor_elementwiseproxy which lets you apply calculations on
lorenz
parents:
1840
diff
changeset
|
3297 (unaryfunc)0, /* nb_oct; __oct__ */ |
|
a7dc31e50c46
* added the vetor_elementwiseproxy which lets you apply calculations on
lorenz
parents:
1840
diff
changeset
|
3298 (unaryfunc)0, /* nb_hex; __hex__ */ |
|
a7dc31e50c46
* added the vetor_elementwiseproxy which lets you apply calculations on
lorenz
parents:
1840
diff
changeset
|
3299 |
|
a7dc31e50c46
* added the vetor_elementwiseproxy which lets you apply calculations on
lorenz
parents:
1840
diff
changeset
|
3300 /* Added in release 2.0 */ |
|
a7dc31e50c46
* added the vetor_elementwiseproxy which lets you apply calculations on
lorenz
parents:
1840
diff
changeset
|
3301 (binaryfunc)0, /* nb_inplace_add; __iadd__ */ |
|
a7dc31e50c46
* added the vetor_elementwiseproxy which lets you apply calculations on
lorenz
parents:
1840
diff
changeset
|
3302 (binaryfunc)0, /* nb_inplace_subtract; __isub__ */ |
|
a7dc31e50c46
* added the vetor_elementwiseproxy which lets you apply calculations on
lorenz
parents:
1840
diff
changeset
|
3303 (binaryfunc)0, /* nb_inplace_multiply; __imul__ */ |
|
a7dc31e50c46
* added the vetor_elementwiseproxy which lets you apply calculations on
lorenz
parents:
1840
diff
changeset
|
3304 (binaryfunc)0, /* nb_inplace_divide; __idiv__ */ |
|
a7dc31e50c46
* added the vetor_elementwiseproxy which lets you apply calculations on
lorenz
parents:
1840
diff
changeset
|
3305 (binaryfunc)0, /* nb_inplace_remainder; __imod__ */ |
|
a7dc31e50c46
* added the vetor_elementwiseproxy which lets you apply calculations on
lorenz
parents:
1840
diff
changeset
|
3306 (ternaryfunc)0, /* nb_inplace_power; __pow__ */ |
|
a7dc31e50c46
* added the vetor_elementwiseproxy which lets you apply calculations on
lorenz
parents:
1840
diff
changeset
|
3307 (binaryfunc)0, /* nb_inplace_lshift; __ilshift__ */ |
|
a7dc31e50c46
* added the vetor_elementwiseproxy which lets you apply calculations on
lorenz
parents:
1840
diff
changeset
|
3308 (binaryfunc)0, /* nb_inplace_rshift; __irshift__ */ |
|
a7dc31e50c46
* added the vetor_elementwiseproxy which lets you apply calculations on
lorenz
parents:
1840
diff
changeset
|
3309 (binaryfunc)0, /* nb_inplace_and; __iand__ */ |
|
a7dc31e50c46
* added the vetor_elementwiseproxy which lets you apply calculations on
lorenz
parents:
1840
diff
changeset
|
3310 (binaryfunc)0, /* nb_inplace_xor; __ixor__ */ |
|
a7dc31e50c46
* added the vetor_elementwiseproxy which lets you apply calculations on
lorenz
parents:
1840
diff
changeset
|
3311 (binaryfunc)0, /* nb_inplace_or; __ior__ */ |
|
a7dc31e50c46
* added the vetor_elementwiseproxy which lets you apply calculations on
lorenz
parents:
1840
diff
changeset
|
3312 |
|
a7dc31e50c46
* added the vetor_elementwiseproxy which lets you apply calculations on
lorenz
parents:
1840
diff
changeset
|
3313 /* Added in release 2.2 */ |
|
a7dc31e50c46
* added the vetor_elementwiseproxy which lets you apply calculations on
lorenz
parents:
1840
diff
changeset
|
3314 (binaryfunc)vector_elementwiseproxy_floor_div, /* nb_floor_divide; __floor__ */ |
|
a7dc31e50c46
* added the vetor_elementwiseproxy which lets you apply calculations on
lorenz
parents:
1840
diff
changeset
|
3315 (binaryfunc)vector_elementwiseproxy_div, /* nb_true_divide; __truediv__ */ |
|
a7dc31e50c46
* added the vetor_elementwiseproxy which lets you apply calculations on
lorenz
parents:
1840
diff
changeset
|
3316 (binaryfunc)0, /* nb_inplace_floor_divide; __ifloor__ */ |
|
a7dc31e50c46
* added the vetor_elementwiseproxy which lets you apply calculations on
lorenz
parents:
1840
diff
changeset
|
3317 (binaryfunc)0, /* nb_inplace_true_divide; __itruediv__ */ |
|
a7dc31e50c46
* added the vetor_elementwiseproxy which lets you apply calculations on
lorenz
parents:
1840
diff
changeset
|
3318 |
|
a7dc31e50c46
* added the vetor_elementwiseproxy which lets you apply calculations on
lorenz
parents:
1840
diff
changeset
|
3319 /* Added in release 2.5 */ |
|
a7dc31e50c46
* added the vetor_elementwiseproxy which lets you apply calculations on
lorenz
parents:
1840
diff
changeset
|
3320 (unaryfunc)0, /* nb_index; __index__ */ |
|
a7dc31e50c46
* added the vetor_elementwiseproxy which lets you apply calculations on
lorenz
parents:
1840
diff
changeset
|
3321 }; |
|
a7dc31e50c46
* added the vetor_elementwiseproxy which lets you apply calculations on
lorenz
parents:
1840
diff
changeset
|
3322 |
|
a7dc31e50c46
* added the vetor_elementwiseproxy which lets you apply calculations on
lorenz
parents:
1840
diff
changeset
|
3323 |
|
a7dc31e50c46
* added the vetor_elementwiseproxy which lets you apply calculations on
lorenz
parents:
1840
diff
changeset
|
3324 |
|
1842
5418dd349248
refactored the vector_elementwiseproxy functions and fixed some bugs along the way
lorenz
parents:
1841
diff
changeset
|
3325 |
|
1841
a7dc31e50c46
* added the vetor_elementwiseproxy which lets you apply calculations on
lorenz
parents:
1840
diff
changeset
|
3326 static PyTypeObject PyVectorElementwiseProxy_Type = { |
|
a7dc31e50c46
* added the vetor_elementwiseproxy which lets you apply calculations on
lorenz
parents:
1840
diff
changeset
|
3327 PyObject_HEAD_INIT(NULL) |
|
a7dc31e50c46
* added the vetor_elementwiseproxy which lets you apply calculations on
lorenz
parents:
1840
diff
changeset
|
3328 0, /* ob_size */ |
|
a7dc31e50c46
* added the vetor_elementwiseproxy which lets you apply calculations on
lorenz
parents:
1840
diff
changeset
|
3329 "pygame.math.VectorElementwiseProxy", /* tp_name */ |
|
a7dc31e50c46
* added the vetor_elementwiseproxy which lets you apply calculations on
lorenz
parents:
1840
diff
changeset
|
3330 sizeof(vector_elementwiseproxy), /* tp_basicsize */ |
|
a7dc31e50c46
* added the vetor_elementwiseproxy which lets you apply calculations on
lorenz
parents:
1840
diff
changeset
|
3331 0, /* tp_itemsize */ |
|
a7dc31e50c46
* added the vetor_elementwiseproxy which lets you apply calculations on
lorenz
parents:
1840
diff
changeset
|
3332 /* Methods to implement standard operations */ |
|
a7dc31e50c46
* added the vetor_elementwiseproxy which lets you apply calculations on
lorenz
parents:
1840
diff
changeset
|
3333 (destructor)vector_elementwiseproxy_dealloc, /* tp_dealloc */ |
|
a7dc31e50c46
* added the vetor_elementwiseproxy which lets you apply calculations on
lorenz
parents:
1840
diff
changeset
|
3334 0, /* tp_print */ |
|
a7dc31e50c46
* added the vetor_elementwiseproxy which lets you apply calculations on
lorenz
parents:
1840
diff
changeset
|
3335 0, /* tp_getattr */ |
|
a7dc31e50c46
* added the vetor_elementwiseproxy which lets you apply calculations on
lorenz
parents:
1840
diff
changeset
|
3336 0, /* tp_setattr */ |
|
a7dc31e50c46
* added the vetor_elementwiseproxy which lets you apply calculations on
lorenz
parents:
1840
diff
changeset
|
3337 0, /* tp_compare */ |
|
a7dc31e50c46
* added the vetor_elementwiseproxy which lets you apply calculations on
lorenz
parents:
1840
diff
changeset
|
3338 (reprfunc)0, /* tp_repr */ |
|
a7dc31e50c46
* added the vetor_elementwiseproxy which lets you apply calculations on
lorenz
parents:
1840
diff
changeset
|
3339 /* Method suites for standard classes */ |
|
1842
5418dd349248
refactored the vector_elementwiseproxy functions and fixed some bugs along the way
lorenz
parents:
1841
diff
changeset
|
3340 &vector_elementwiseproxy_as_number, /* tp_as_number */ |
|
1841
a7dc31e50c46
* added the vetor_elementwiseproxy which lets you apply calculations on
lorenz
parents:
1840
diff
changeset
|
3341 0, /* tp_as_sequence */ |
|
a7dc31e50c46
* added the vetor_elementwiseproxy which lets you apply calculations on
lorenz
parents:
1840
diff
changeset
|
3342 0, /* tp_as_mapping */ |
|
a7dc31e50c46
* added the vetor_elementwiseproxy which lets you apply calculations on
lorenz
parents:
1840
diff
changeset
|
3343 /* More standard operations (here for binary compatibility) */ |
|
a7dc31e50c46
* added the vetor_elementwiseproxy which lets you apply calculations on
lorenz
parents:
1840
diff
changeset
|
3344 0, /* tp_hash */ |
|
a7dc31e50c46
* added the vetor_elementwiseproxy which lets you apply calculations on
lorenz
parents:
1840
diff
changeset
|
3345 0, /* tp_call */ |
|
a7dc31e50c46
* added the vetor_elementwiseproxy which lets you apply calculations on
lorenz
parents:
1840
diff
changeset
|
3346 (reprfunc)0, /* tp_str */ |
|
a7dc31e50c46
* added the vetor_elementwiseproxy which lets you apply calculations on
lorenz
parents:
1840
diff
changeset
|
3347 (getattrofunc)0, /* tp_getattro */ |
|
a7dc31e50c46
* added the vetor_elementwiseproxy which lets you apply calculations on
lorenz
parents:
1840
diff
changeset
|
3348 (setattrofunc)0, /* tp_setattro */ |
|
a7dc31e50c46
* added the vetor_elementwiseproxy which lets you apply calculations on
lorenz
parents:
1840
diff
changeset
|
3349 /* Functions to access object as input/output buffer */ |
|
a7dc31e50c46
* added the vetor_elementwiseproxy which lets you apply calculations on
lorenz
parents:
1840
diff
changeset
|
3350 0, /* tp_as_buffer */ |
|
a7dc31e50c46
* added the vetor_elementwiseproxy which lets you apply calculations on
lorenz
parents:
1840
diff
changeset
|
3351 /* Flags to define presence of optional/expanded features */ |
|
a7dc31e50c46
* added the vetor_elementwiseproxy which lets you apply calculations on
lorenz
parents:
1840
diff
changeset
|
3352 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_CHECKTYPES, /* tp_flags */ |
|
a7dc31e50c46
* added the vetor_elementwiseproxy which lets you apply calculations on
lorenz
parents:
1840
diff
changeset
|
3353 /* Documentation string */ |
|
a7dc31e50c46
* added the vetor_elementwiseproxy which lets you apply calculations on
lorenz
parents:
1840
diff
changeset
|
3354 0, /* tp_doc */ |
|
a7dc31e50c46
* added the vetor_elementwiseproxy which lets you apply calculations on
lorenz
parents:
1840
diff
changeset
|
3355 |
|
a7dc31e50c46
* added the vetor_elementwiseproxy which lets you apply calculations on
lorenz
parents:
1840
diff
changeset
|
3356 /* Assigned meaning in release 2.0 */ |
|
a7dc31e50c46
* added the vetor_elementwiseproxy which lets you apply calculations on
lorenz
parents:
1840
diff
changeset
|
3357 /* call function for all accessible objects */ |
|
a7dc31e50c46
* added the vetor_elementwiseproxy which lets you apply calculations on
lorenz
parents:
1840
diff
changeset
|
3358 0, /* tp_traverse */ |
|
a7dc31e50c46
* added the vetor_elementwiseproxy which lets you apply calculations on
lorenz
parents:
1840
diff
changeset
|
3359 /* delete references to contained objects */ |
|
a7dc31e50c46
* added the vetor_elementwiseproxy which lets you apply calculations on
lorenz
parents:
1840
diff
changeset
|
3360 0, /* tp_clear */ |
|
a7dc31e50c46
* added the vetor_elementwiseproxy which lets you apply calculations on
lorenz
parents:
1840
diff
changeset
|
3361 |
|
a7dc31e50c46
* added the vetor_elementwiseproxy which lets you apply calculations on
lorenz
parents:
1840
diff
changeset
|
3362 /* Assigned meaning in release 2.1 */ |
|
a7dc31e50c46
* added the vetor_elementwiseproxy which lets you apply calculations on
lorenz
parents:
1840
diff
changeset
|
3363 /* rich comparisons */ |
|
a7dc31e50c46
* added the vetor_elementwiseproxy which lets you apply calculations on
lorenz
parents:
1840
diff
changeset
|
3364 (richcmpfunc)vector_elementwiseproxy_richcompare, /* tp_richcompare */ |
|
a7dc31e50c46
* added the vetor_elementwiseproxy which lets you apply calculations on
lorenz
parents:
1840
diff
changeset
|
3365 /* weak reference enabler */ |
|
a7dc31e50c46
* added the vetor_elementwiseproxy which lets you apply calculations on
lorenz
parents:
1840
diff
changeset
|
3366 0, /* tp_weaklistoffset */ |
|
a7dc31e50c46
* added the vetor_elementwiseproxy which lets you apply calculations on
lorenz
parents:
1840
diff
changeset
|
3367 |
|
a7dc31e50c46
* added the vetor_elementwiseproxy which lets you apply calculations on
lorenz
parents:
1840
diff
changeset
|
3368 /* Added in release 2.2 */ |
|
a7dc31e50c46
* added the vetor_elementwiseproxy which lets you apply calculations on
lorenz
parents:
1840
diff
changeset
|
3369 /* Iterators */ |
|
a7dc31e50c46
* added the vetor_elementwiseproxy which lets you apply calculations on
lorenz
parents:
1840
diff
changeset
|
3370 0, /* tp_iter */ |
|
a7dc31e50c46
* added the vetor_elementwiseproxy which lets you apply calculations on
lorenz
parents:
1840
diff
changeset
|
3371 0, /* tp_iternext */ |
|
a7dc31e50c46
* added the vetor_elementwiseproxy which lets you apply calculations on
lorenz
parents:
1840
diff
changeset
|
3372 /* Attribute descriptor and subclassing stuff */ |
|
a7dc31e50c46
* added the vetor_elementwiseproxy which lets you apply calculations on
lorenz
parents:
1840
diff
changeset
|
3373 0, /* tp_methods */ |
|
a7dc31e50c46
* added the vetor_elementwiseproxy which lets you apply calculations on
lorenz
parents:
1840
diff
changeset
|
3374 0, /* tp_members */ |
|
a7dc31e50c46
* added the vetor_elementwiseproxy which lets you apply calculations on
lorenz
parents:
1840
diff
changeset
|
3375 0, /* tp_getset */ |
|
a7dc31e50c46
* added the vetor_elementwiseproxy which lets you apply calculations on
lorenz
parents:
1840
diff
changeset
|
3376 0, /* tp_base */ |
|
a7dc31e50c46
* added the vetor_elementwiseproxy which lets you apply calculations on
lorenz
parents:
1840
diff
changeset
|
3377 0, /* tp_dict */ |
|
a7dc31e50c46
* added the vetor_elementwiseproxy which lets you apply calculations on
lorenz
parents:
1840
diff
changeset
|
3378 0, /* tp_descr_get */ |
|
a7dc31e50c46
* added the vetor_elementwiseproxy which lets you apply calculations on
lorenz
parents:
1840
diff
changeset
|
3379 0, /* tp_descr_set */ |
|
a7dc31e50c46
* added the vetor_elementwiseproxy which lets you apply calculations on
lorenz
parents:
1840
diff
changeset
|
3380 0, /* tp_dictoffset */ |
|
a7dc31e50c46
* added the vetor_elementwiseproxy which lets you apply calculations on
lorenz
parents:
1840
diff
changeset
|
3381 (initproc)0, /* tp_init */ |
|
a7dc31e50c46
* added the vetor_elementwiseproxy which lets you apply calculations on
lorenz
parents:
1840
diff
changeset
|
3382 0, /* tp_alloc */ |
|
a7dc31e50c46
* added the vetor_elementwiseproxy which lets you apply calculations on
lorenz
parents:
1840
diff
changeset
|
3383 (newfunc)0, /* tp_new */ |
|
a7dc31e50c46
* added the vetor_elementwiseproxy which lets you apply calculations on
lorenz
parents:
1840
diff
changeset
|
3384 0, /* tp_free */ |
|
a7dc31e50c46
* added the vetor_elementwiseproxy which lets you apply calculations on
lorenz
parents:
1840
diff
changeset
|
3385 0, /* tp_is_gc */ |
|
a7dc31e50c46
* added the vetor_elementwiseproxy which lets you apply calculations on
lorenz
parents:
1840
diff
changeset
|
3386 0, /* tp_bases */ |
|
a7dc31e50c46
* added the vetor_elementwiseproxy which lets you apply calculations on
lorenz
parents:
1840
diff
changeset
|
3387 0, /* tp_mro */ |
|
a7dc31e50c46
* added the vetor_elementwiseproxy which lets you apply calculations on
lorenz
parents:
1840
diff
changeset
|
3388 0, /* tp_cache */ |
|
a7dc31e50c46
* added the vetor_elementwiseproxy which lets you apply calculations on
lorenz
parents:
1840
diff
changeset
|
3389 0, /* tp_subclasses */ |
|
a7dc31e50c46
* added the vetor_elementwiseproxy which lets you apply calculations on
lorenz
parents:
1840
diff
changeset
|
3390 0, /* tp_weaklist */ |
|
a7dc31e50c46
* added the vetor_elementwiseproxy which lets you apply calculations on
lorenz
parents:
1840
diff
changeset
|
3391 }; |
|
a7dc31e50c46
* added the vetor_elementwiseproxy which lets you apply calculations on
lorenz
parents:
1840
diff
changeset
|
3392 |
|
a7dc31e50c46
* added the vetor_elementwiseproxy which lets you apply calculations on
lorenz
parents:
1840
diff
changeset
|
3393 |
|
a7dc31e50c46
* added the vetor_elementwiseproxy which lets you apply calculations on
lorenz
parents:
1840
diff
changeset
|
3394 |
|
a7dc31e50c46
* added the vetor_elementwiseproxy which lets you apply calculations on
lorenz
parents:
1840
diff
changeset
|
3395 static PyObject * |
|
a7dc31e50c46
* added the vetor_elementwiseproxy which lets you apply calculations on
lorenz
parents:
1840
diff
changeset
|
3396 vector_elementwise(PyVector *vec) |
|
a7dc31e50c46
* added the vetor_elementwiseproxy which lets you apply calculations on
lorenz
parents:
1840
diff
changeset
|
3397 { |
|
a7dc31e50c46
* added the vetor_elementwiseproxy which lets you apply calculations on
lorenz
parents:
1840
diff
changeset
|
3398 vector_elementwiseproxy *proxy; |
|
a7dc31e50c46
* added the vetor_elementwiseproxy which lets you apply calculations on
lorenz
parents:
1840
diff
changeset
|
3399 if (!PyVector_Check(vec)) { |
|
a7dc31e50c46
* added the vetor_elementwiseproxy which lets you apply calculations on
lorenz
parents:
1840
diff
changeset
|
3400 PyErr_BadInternalCall(); |
|
a7dc31e50c46
* added the vetor_elementwiseproxy which lets you apply calculations on
lorenz
parents:
1840
diff
changeset
|
3401 return NULL; |
|
a7dc31e50c46
* added the vetor_elementwiseproxy which lets you apply calculations on
lorenz
parents:
1840
diff
changeset
|
3402 } |
|
a7dc31e50c46
* added the vetor_elementwiseproxy which lets you apply calculations on
lorenz
parents:
1840
diff
changeset
|
3403 |
|
a7dc31e50c46
* added the vetor_elementwiseproxy which lets you apply calculations on
lorenz
parents:
1840
diff
changeset
|
3404 proxy = PyObject_New(vector_elementwiseproxy, |
|
a7dc31e50c46
* added the vetor_elementwiseproxy which lets you apply calculations on
lorenz
parents:
1840
diff
changeset
|
3405 &PyVectorElementwiseProxy_Type); |
|
a7dc31e50c46
* added the vetor_elementwiseproxy which lets you apply calculations on
lorenz
parents:
1840
diff
changeset
|
3406 if (proxy == NULL) |
|
a7dc31e50c46
* added the vetor_elementwiseproxy which lets you apply calculations on
lorenz
parents:
1840
diff
changeset
|
3407 return NULL; |
|
a7dc31e50c46
* added the vetor_elementwiseproxy which lets you apply calculations on
lorenz
parents:
1840
diff
changeset
|
3408 Py_INCREF(vec); |
|
a7dc31e50c46
* added the vetor_elementwiseproxy which lets you apply calculations on
lorenz
parents:
1840
diff
changeset
|
3409 proxy->vec = (PyVector *)vec; |
|
a7dc31e50c46
* added the vetor_elementwiseproxy which lets you apply calculations on
lorenz
parents:
1840
diff
changeset
|
3410 return (PyObject *)proxy; |
|
a7dc31e50c46
* added the vetor_elementwiseproxy which lets you apply calculations on
lorenz
parents:
1840
diff
changeset
|
3411 } |
|
a7dc31e50c46
* added the vetor_elementwiseproxy which lets you apply calculations on
lorenz
parents:
1840
diff
changeset
|
3412 |
|
a7dc31e50c46
* added the vetor_elementwiseproxy which lets you apply calculations on
lorenz
parents:
1840
diff
changeset
|
3413 |
|
a7dc31e50c46
* added the vetor_elementwiseproxy which lets you apply calculations on
lorenz
parents:
1840
diff
changeset
|
3414 |
|
a7dc31e50c46
* added the vetor_elementwiseproxy which lets you apply calculations on
lorenz
parents:
1840
diff
changeset
|
3415 |
|
a7dc31e50c46
* added the vetor_elementwiseproxy which lets you apply calculations on
lorenz
parents:
1840
diff
changeset
|
3416 |
|
a7dc31e50c46
* added the vetor_elementwiseproxy which lets you apply calculations on
lorenz
parents:
1840
diff
changeset
|
3417 |
|
a7dc31e50c46
* added the vetor_elementwiseproxy which lets you apply calculations on
lorenz
parents:
1840
diff
changeset
|
3418 |
|
1845
7d2a16e6a1d3
rearanged math module. added forward declarations at the beginning.
lorenz
parents:
1844
diff
changeset
|
3419 static PyObject * |
|
7d2a16e6a1d3
rearanged math module. added forward declarations at the beginning.
lorenz
parents:
1844
diff
changeset
|
3420 math_enable_swizzling(PyVector *self) |
|
7d2a16e6a1d3
rearanged math module. added forward declarations at the beginning.
lorenz
parents:
1844
diff
changeset
|
3421 { |
|
7d2a16e6a1d3
rearanged math module. added forward declarations at the beginning.
lorenz
parents:
1844
diff
changeset
|
3422 swizzling_enabled = 1; |
|
7d2a16e6a1d3
rearanged math module. added forward declarations at the beginning.
lorenz
parents:
1844
diff
changeset
|
3423 Py_RETURN_NONE; |
|
7d2a16e6a1d3
rearanged math module. added forward declarations at the beginning.
lorenz
parents:
1844
diff
changeset
|
3424 } |
|
7d2a16e6a1d3
rearanged math module. added forward declarations at the beginning.
lorenz
parents:
1844
diff
changeset
|
3425 |
|
7d2a16e6a1d3
rearanged math module. added forward declarations at the beginning.
lorenz
parents:
1844
diff
changeset
|
3426 static PyObject * |
|
7d2a16e6a1d3
rearanged math module. added forward declarations at the beginning.
lorenz
parents:
1844
diff
changeset
|
3427 math_disable_swizzling(PyVector *self) |
|
7d2a16e6a1d3
rearanged math module. added forward declarations at the beginning.
lorenz
parents:
1844
diff
changeset
|
3428 { |
|
7d2a16e6a1d3
rearanged math module. added forward declarations at the beginning.
lorenz
parents:
1844
diff
changeset
|
3429 swizzling_enabled = 0; |
|
7d2a16e6a1d3
rearanged math module. added forward declarations at the beginning.
lorenz
parents:
1844
diff
changeset
|
3430 Py_RETURN_NONE; |
|
7d2a16e6a1d3
rearanged math module. added forward declarations at the beginning.
lorenz
parents:
1844
diff
changeset
|
3431 } |
|
1841
a7dc31e50c46
* added the vetor_elementwiseproxy which lets you apply calculations on
lorenz
parents:
1840
diff
changeset
|
3432 |
| 1834 | 3433 static PyMethodDef _math_methods[] = |
| 3434 { | |
|
1845
7d2a16e6a1d3
rearanged math module. added forward declarations at the beginning.
lorenz
parents:
1844
diff
changeset
|
3435 {"enable_swizzling", (PyCFunction)math_enable_swizzling, METH_NOARGS, |
|
7d2a16e6a1d3
rearanged math module. added forward declarations at the beginning.
lorenz
parents:
1844
diff
changeset
|
3436 "enables swizzling." |
|
7d2a16e6a1d3
rearanged math module. added forward declarations at the beginning.
lorenz
parents:
1844
diff
changeset
|
3437 }, |
|
7d2a16e6a1d3
rearanged math module. added forward declarations at the beginning.
lorenz
parents:
1844
diff
changeset
|
3438 {"disable_swizzling", (PyCFunction)math_disable_swizzling, METH_NOARGS, |
|
7d2a16e6a1d3
rearanged math module. added forward declarations at the beginning.
lorenz
parents:
1844
diff
changeset
|
3439 "disables swizzling." |
|
7d2a16e6a1d3
rearanged math module. added forward declarations at the beginning.
lorenz
parents:
1844
diff
changeset
|
3440 }, |
| 1834 | 3441 {NULL, NULL, 0, NULL} |
| 3442 }; | |
| 3443 | |
| 3444 /* DOC */ static char _math_doc[] = | |
| 3445 /* DOC */ "Module for various math related classes and functions\n"; | |
| 3446 | |
| 3447 /**************************** | |
| 3448 * Module init function | |
| 3449 ****************************/ | |
| 3450 | |
| 3451 MODINIT_DEFINE (math) | |
| 3452 { | |
| 3453 PyObject *module, *apiobj; | |
| 3454 static void *c_api[PYGAMEAPI_MATH_NUMSLOTS]; | |
| 3455 | |
| 3456 #if PY3 | |
| 3457 static struct PyModuleDef _module = { | |
| 3458 PyModuleDef_HEAD_INIT, | |
| 3459 "math", | |
| 3460 _math_doc, | |
| 3461 -1, | |
| 3462 _math_methods, | |
| 3463 NULL, NULL, NULL, NULL | |
| 3464 }; | |
| 3465 #endif | |
| 3466 | |
| 3467 /* initialize the extension types */ | |
|
1844
a15f6323c39d
* implemented Vector3. there are no tests yet, so there are bound to be bugs
lorenz
parents:
1842
diff
changeset
|
3468 if ((PyType_Ready(&PyVector2_Type) < 0) || |
| 1846 | 3469 (PyType_Ready(&PyVector3_Type) < 0) || |
| 3470 (PyType_Ready(&PyVectorElementwiseProxy_Type) < 0) || | |
| 3471 (PyType_Ready(&PyVectorIter_Type) < 0) || | |
| 3472 (PyType_Ready(&PyVector_SlerpIter_Type) < 0) /*|| | |
| 1834 | 3473 (PyType_Ready(&PyVector4_Type) < 0)*/) { |
| 3474 MODINIT_ERROR; | |
| 3475 } | |
| 3476 | |
| 3477 /* initialize the module */ | |
| 3478 #if PY3 | |
| 3479 module = PyModule_Create (&_module); | |
| 3480 #else | |
| 3481 module = Py_InitModule3 (MODPREFIX "math", _math_methods, _math_doc); | |
| 3482 #endif | |
| 3483 | |
| 3484 if (module == NULL) { | |
| 3485 MODINIT_ERROR; | |
| 3486 } | |
| 3487 | |
| 3488 /* add extension types to module */ | |
| 3489 Py_INCREF(&PyVector2_Type); | |
|
1844
a15f6323c39d
* implemented Vector3. there are no tests yet, so there are bound to be bugs
lorenz
parents:
1842
diff
changeset
|
3490 Py_INCREF(&PyVector3_Type); |
| 1846 | 3491 Py_INCREF(&PyVector3_Type); |
| 3492 Py_INCREF(&PyVectorElementwiseProxy_Type); | |
| 3493 Py_INCREF(&PyVectorIter_Type); | |
| 3494 Py_INCREF(&PyVector_SlerpIter_Type); | |
| 1834 | 3495 // Py_INCREF(&PyVector4_Type); |
|
1844
a15f6323c39d
* implemented Vector3. there are no tests yet, so there are bound to be bugs
lorenz
parents:
1842
diff
changeset
|
3496 if ((PyModule_AddObject(module, "Vector2", (PyObject *)&PyVector2_Type) != 0) || |
| 1846 | 3497 (PyModule_AddObject(module, "Vector3", (PyObject *)&PyVector3_Type) != 0) || |
| 3498 (PyModule_AddObject(module, "VectorElementwiseProxy", (PyObject *)&PyVectorElementwiseProxy_Type) != 0) || | |
| 3499 (PyModule_AddObject(module, "VectorIterator", (PyObject *)&PyVectorIter_Type) != 0) || | |
| 1848 | 3500 (PyModule_AddObject(module, "VectorSlerpIterator", (PyObject *)&PyVector_SlerpIter_Type) != 0) /*|| |
| 1834 | 3501 (PyModule_AddObject(module, "Vector4", (PyObject *)&PyVector4_Type) != 0)*/) { |
| 3502 Py_DECREF(&PyVector2_Type); | |
|
1844
a15f6323c39d
* implemented Vector3. there are no tests yet, so there are bound to be bugs
lorenz
parents:
1842
diff
changeset
|
3503 Py_DECREF(&PyVector3_Type); |
| 1846 | 3504 Py_DECREF(&PyVectorElementwiseProxy_Type); |
| 3505 Py_DECREF(&PyVectorIter_Type); | |
| 3506 Py_DECREF(&PyVector_SlerpIter_Type); | |
| 1834 | 3507 // Py_DECREF(&PyVector4_Type); |
| 3508 DECREF_MOD (module); | |
| 3509 MODINIT_ERROR; | |
| 3510 } | |
| 3511 | |
| 3512 /* export the C api */ | |
| 3513 c_api[0] = &PyVector2_Type; | |
|
1844
a15f6323c39d
* implemented Vector3. there are no tests yet, so there are bound to be bugs
lorenz
parents:
1842
diff
changeset
|
3514 c_api[1] = &PyVector3_Type; |
| 1834 | 3515 // c_api[2] = &PyVector4_Type; |
| 3516 // c_api[3] = PyVector_NEW; | |
|
1845
7d2a16e6a1d3
rearanged math module. added forward declarations at the beginning.
lorenz
parents:
1844
diff
changeset
|
3517 // c_api[4] = PyVectorCompatible_Check; |
| 1834 | 3518 apiobj = PyCObject_FromVoidPtr(c_api, NULL); |
| 3519 if (apiobj == NULL) { | |
| 3520 DECREF_MOD (module); | |
| 3521 MODINIT_ERROR; | |
| 3522 } | |
| 3523 | |
| 3524 if (PyModule_AddObject(module, PYGAMEAPI_LOCAL_ENTRY, apiobj) != 0) { | |
| 1836 | 3525 Py_DECREF (apiobj); |
| 1834 | 3526 DECREF_MOD (module); |
| 3527 MODINIT_ERROR; | |
| 3528 } | |
| 3529 | |
| 3530 MODINIT_RETURN (module); | |
| 3531 } |
