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
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
1834
d31cd03677fb added an incomplete Vector2 to the math module.
lorenz
parents:
diff changeset
1 /*
d31cd03677fb added an incomplete Vector2 to the math module.
lorenz
parents:
diff changeset
2 pygame - Python Game Library
d31cd03677fb added an incomplete Vector2 to the math module.
lorenz
parents:
diff changeset
3
d31cd03677fb added an incomplete Vector2 to the math module.
lorenz
parents:
diff changeset
4 This library is free software; you can redistribute it and/or
d31cd03677fb added an incomplete Vector2 to the math module.
lorenz
parents:
diff changeset
5 modify it under the terms of the GNU Library General Public
d31cd03677fb added an incomplete Vector2 to the math module.
lorenz
parents:
diff changeset
6 License as published by the Free Software Foundation; either
d31cd03677fb added an incomplete Vector2 to the math module.
lorenz
parents:
diff changeset
7 version 2 of the License, or (at your option) any later version.
d31cd03677fb added an incomplete Vector2 to the math module.
lorenz
parents:
diff changeset
8
d31cd03677fb added an incomplete Vector2 to the math module.
lorenz
parents:
diff changeset
9 This library is distributed in the hope that it will be useful,
d31cd03677fb added an incomplete Vector2 to the math module.
lorenz
parents:
diff changeset
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
d31cd03677fb added an incomplete Vector2 to the math module.
lorenz
parents:
diff changeset
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
d31cd03677fb added an incomplete Vector2 to the math module.
lorenz
parents:
diff changeset
12 Library General Public License for more details.
d31cd03677fb added an incomplete Vector2 to the math module.
lorenz
parents:
diff changeset
13
d31cd03677fb added an incomplete Vector2 to the math module.
lorenz
parents:
diff changeset
14 You should have received a copy of the GNU Library General Public
d31cd03677fb added an incomplete Vector2 to the math module.
lorenz
parents:
diff changeset
15 License along with this library; if not, write to the Free
d31cd03677fb added an incomplete Vector2 to the math module.
lorenz
parents:
diff changeset
16 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
d31cd03677fb added an incomplete Vector2 to the math module.
lorenz
parents:
diff changeset
17
d31cd03677fb added an incomplete Vector2 to the math module.
lorenz
parents:
diff changeset
18 */
d31cd03677fb added an incomplete Vector2 to the math module.
lorenz
parents:
diff changeset
19
d31cd03677fb added an incomplete Vector2 to the math module.
lorenz
parents:
diff changeset
20 #define PYGAMEAPI_MATH_INTERNAL
d31cd03677fb added an incomplete Vector2 to the math module.
lorenz
parents:
diff changeset
21 #include "pygame.h"
d31cd03677fb added an incomplete Vector2 to the math module.
lorenz
parents:
diff changeset
22 #include "pygamedocs.h"
d31cd03677fb added an incomplete Vector2 to the math module.
lorenz
parents:
diff changeset
23 #include "structmember.h"
d31cd03677fb added an incomplete Vector2 to the math module.
lorenz
parents:
diff changeset
24 #include "pgcompat.h"
d31cd03677fb added an incomplete Vector2 to the math module.
lorenz
parents:
diff changeset
25 #include <float.h>
d31cd03677fb added an incomplete Vector2 to the math module.
lorenz
parents:
diff changeset
26 #include <math.h>
d31cd03677fb added an incomplete Vector2 to the math module.
lorenz
parents:
diff changeset
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
18eefb9bd5a2 updates to the pygame.math module
lorenz
parents: 1835
diff changeset
29 #define STRING_BUF_SIZE (100)
1840
0959bd773ffd added support for swizzling.
lorenz
parents: 1838
diff changeset
30 #define SWIZZLE_ERR_NO_ERR 0
0959bd773ffd added support for swizzling.
lorenz
parents: 1838
diff changeset
31 #define SWIZZLE_ERR_DOUBLE_IDX 1
0959bd773ffd added support for swizzling.
lorenz
parents: 1838
diff changeset
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
d31cd03677fb added an incomplete Vector2 to the math module.
lorenz
parents:
diff changeset
35
1848
bbc21cd6faeb some last minute changes before I merge to trunk.
lorenz
parents: 1847
diff changeset
36 #define OP_ADD 1
bbc21cd6faeb some last minute changes before I merge to trunk.
lorenz
parents: 1847
diff changeset
37 #define OP_IADD 2
bbc21cd6faeb some last minute changes before I merge to trunk.
lorenz
parents: 1847
diff changeset
38 #define OP_SUB 3
bbc21cd6faeb some last minute changes before I merge to trunk.
lorenz
parents: 1847
diff changeset
39 #define OP_ISUB 4
bbc21cd6faeb some last minute changes before I merge to trunk.
lorenz
parents: 1847
diff changeset
40 #define OP_MUL 5
bbc21cd6faeb some last minute changes before I merge to trunk.
lorenz
parents: 1847
diff changeset
41 #define OP_IMUL 6
bbc21cd6faeb some last minute changes before I merge to trunk.
lorenz
parents: 1847
diff changeset
42 #define OP_DIV 7
bbc21cd6faeb some last minute changes before I merge to trunk.
lorenz
parents: 1847
diff changeset
43 #define OP_IDIV 8
bbc21cd6faeb some last minute changes before I merge to trunk.
lorenz
parents: 1847
diff changeset
44 #define OP_FLOOR_DIV 9
bbc21cd6faeb some last minute changes before I merge to trunk.
lorenz
parents: 1847
diff changeset
45 #define OP_IFLOOR_DIV 10
bbc21cd6faeb some last minute changes before I merge to trunk.
lorenz
parents: 1847
diff changeset
46 #define OP_MOD 11
bbc21cd6faeb some last minute changes before I merge to trunk.
lorenz
parents: 1847
diff changeset
47 #define OP_ARG_REVERSE 32
bbc21cd6faeb some last minute changes before I merge to trunk.
lorenz
parents: 1847
diff changeset
48 #define OP_ARG_UNKNOWN 64
bbc21cd6faeb some last minute changes before I merge to trunk.
lorenz
parents: 1847
diff changeset
49 #define OP_ARG_VECTOR 128
bbc21cd6faeb some last minute changes before I merge to trunk.
lorenz
parents: 1847
diff changeset
50 #define OP_ARG_NUMBER 256
bbc21cd6faeb some last minute changes before I merge to trunk.
lorenz
parents: 1847
diff changeset
51
1834
d31cd03677fb added an incomplete Vector2 to the math module.
lorenz
parents:
diff changeset
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
1e46afbd7b54 implemented new methods in the math module.
lorenz
parents: 1845
diff changeset
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
d31cd03677fb added an incomplete Vector2 to the math module.
lorenz
parents:
diff changeset
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
d31cd03677fb added an incomplete Vector2 to the math module.
lorenz
parents:
diff changeset
63
1836
18eefb9bd5a2 updates to the pygame.math module
lorenz
parents: 1835
diff changeset
64 #define DEG2RAD(angle) ((angle) * M_PI / 180.)
18eefb9bd5a2 updates to the pygame.math module
lorenz
parents: 1835
diff changeset
65 #define RAD2DEG(angle) ((angle) * 180. / M_PI)
18eefb9bd5a2 updates to the pygame.math module
lorenz
parents: 1835
diff changeset
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
1e46afbd7b54 implemented new methods in the math module.
lorenz
parents: 1845
diff changeset
83 long it_index;
1e46afbd7b54 implemented new methods in the math module.
lorenz
parents: 1845
diff changeset
84 long steps;
1e46afbd7b54 implemented new methods in the math module.
lorenz
parents: 1845
diff changeset
85 long dim;
1e46afbd7b54 implemented new methods in the math module.
lorenz
parents: 1845
diff changeset
86 double coords[VECTOR_MAX_SIZE];
1e46afbd7b54 implemented new methods in the math module.
lorenz
parents: 1845
diff changeset
87 double matrix[VECTOR_MAX_SIZE][VECTOR_MAX_SIZE];
1e46afbd7b54 implemented new methods in the math module.
lorenz
parents: 1845
diff changeset
88 double radial_factor;
1e46afbd7b54 implemented new methods in the math module.
lorenz
parents: 1845
diff changeset
89 } vector_slerpiter;
1e46afbd7b54 implemented new methods in the math module.
lorenz
parents: 1845
diff changeset
90
1e46afbd7b54 implemented new methods in the math module.
lorenz
parents: 1845
diff changeset
91 typedef struct {
1e46afbd7b54 implemented new methods in the math module.
lorenz
parents: 1845
diff changeset
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
1e46afbd7b54 implemented new methods in the math module.
lorenz
parents: 1845
diff changeset
113 static void _make_vector2_slerp_matrix(vector_slerpiter *it,
1e46afbd7b54 implemented new methods in the math module.
lorenz
parents: 1845
diff changeset
114 const double *vec1_coords,
1e46afbd7b54 implemented new methods in the math module.
lorenz
parents: 1845
diff changeset
115 const double *vec2_coords,
1e46afbd7b54 implemented new methods in the math module.
lorenz
parents: 1845
diff changeset
116 double angle);
1e46afbd7b54 implemented new methods in the math module.
lorenz
parents: 1845
diff changeset
117 static void _make_vector3_slerp_matrix(vector_slerpiter *it,
1e46afbd7b54 implemented new methods in the math module.
lorenz
parents: 1845
diff changeset
118 const double *vec1_coords,
1e46afbd7b54 implemented new methods in the math module.
lorenz
parents: 1845
diff changeset
119 const double *vec2_coords,
1e46afbd7b54 implemented new methods in the math module.
lorenz
parents: 1845
diff changeset
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
bbc21cd6faeb some last minute changes before I merge to trunk.
lorenz
parents: 1847
diff changeset
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
1e46afbd7b54 implemented new methods in the math module.
lorenz
parents: 1845
diff changeset
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
1e46afbd7b54 implemented new methods in the math module.
lorenz
parents: 1845
diff changeset
172 /*
1e46afbd7b54 implemented new methods in the math module.
lorenz
parents: 1845
diff changeset
173 static Py_ssize_t vector_readbuffer(PyVector *self, Py_ssize_t segment, void **ptrptr);
1e46afbd7b54 implemented new methods in the math module.
lorenz
parents: 1845
diff changeset
174 static Py_ssize_t vector_writebuffer(PyVector *self, Py_ssize_t segment, void **ptrptr);
1e46afbd7b54 implemented new methods in the math module.
lorenz
parents: 1845
diff changeset
175 static Py_ssize_t vector_segcount(PyVector *self, Py_ssize_t *lenp);
1e46afbd7b54 implemented new methods in the math module.
lorenz
parents: 1845
diff changeset
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
1e46afbd7b54 implemented new methods in the math module.
lorenz
parents: 1845
diff changeset
187 static PyObject *vector2_as_polar(PyVector *self);
1e46afbd7b54 implemented new methods in the math module.
lorenz
parents: 1845
diff changeset
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
1e46afbd7b54 implemented new methods in the math module.
lorenz
parents: 1845
diff changeset
200 static PyObject *vector3_as_spherical(PyVector *self);
1e46afbd7b54 implemented new methods in the math module.
lorenz
parents: 1845
diff changeset
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
bbc21cd6faeb some last minute changes before I merge to trunk.
lorenz
parents: 1847
diff changeset
212 static PyObject *vector_elementwiseproxy_generic_math(PyObject *o1,
bbc21cd6faeb some last minute changes before I merge to trunk.
lorenz
parents: 1847
diff changeset
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
d31cd03677fb added an incomplete Vector2 to the math module.
lorenz
parents:
diff changeset
229
d31cd03677fb added an incomplete Vector2 to the math module.
lorenz
parents:
diff changeset
230
d31cd03677fb added an incomplete Vector2 to the math module.
lorenz
parents:
diff changeset
231 /********************************
d31cd03677fb added an incomplete Vector2 to the math module.
lorenz
parents:
diff changeset
232 * Global helper functions
d31cd03677fb added an incomplete Vector2 to the math module.
lorenz
parents:
diff changeset
233 ********************************/
d31cd03677fb added an incomplete Vector2 to the math module.
lorenz
parents:
diff changeset
234
d31cd03677fb added an incomplete Vector2 to the math module.
lorenz
parents:
diff changeset
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
d31cd03677fb added an incomplete Vector2 to the math module.
lorenz
parents:
diff changeset
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
d31cd03677fb added an incomplete Vector2 to the math module.
lorenz
parents:
diff changeset
240 return 0;
d31cd03677fb added an incomplete Vector2 to the math module.
lorenz
parents:
diff changeset
241 }
d31cd03677fb added an incomplete Vector2 to the math module.
lorenz
parents:
diff changeset
242
d31cd03677fb added an incomplete Vector2 to the math module.
lorenz
parents:
diff changeset
243 static double
d31cd03677fb added an incomplete Vector2 to the math module.
lorenz
parents:
diff changeset
244 PySequence_GetItem_AsDouble(PyObject *seq, Py_ssize_t index)
d31cd03677fb added an incomplete Vector2 to the math module.
lorenz
parents:
diff changeset
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
d31cd03677fb added an incomplete Vector2 to the math module.
lorenz
parents:
diff changeset
247 double value;
1838
e4fd799c194b added methods distance_to and distance_squared_to to the vector class
lorenz
parents: 1837
diff changeset
248
1834
d31cd03677fb added an incomplete Vector2 to the math module.
lorenz
parents:
diff changeset
249 item = PySequence_GetItem(seq, index);
1840
0959bd773ffd added support for swizzling.
lorenz
parents: 1838
diff changeset
250 if (item == NULL) {
0959bd773ffd added support for swizzling.
lorenz
parents: 1838
diff changeset
251 PyErr_SetString(PyExc_TypeError, "a sequence is expected");
0959bd773ffd added support for swizzling.
lorenz
parents: 1838
diff changeset
252 return -1;
0959bd773ffd added support for swizzling.
lorenz
parents: 1838
diff changeset
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
0959bd773ffd added support for swizzling.
lorenz
parents: 1838
diff changeset
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
18eefb9bd5a2 updates to the pygame.math module
lorenz
parents: 1835
diff changeset
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
d31cd03677fb added an incomplete Vector2 to the math module.
lorenz
parents:
diff changeset
287 }
d31cd03677fb added an incomplete Vector2 to the math module.
lorenz
parents:
diff changeset
288
d31cd03677fb added an incomplete Vector2 to the math module.
lorenz
parents:
diff changeset
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
d31cd03677fb added an incomplete Vector2 to the math module.
lorenz
parents:
diff changeset
291 {
d31cd03677fb added an incomplete Vector2 to the math module.
lorenz
parents:
diff changeset
292 int i;
d31cd03677fb added an incomplete Vector2 to the math module.
lorenz
parents:
diff changeset
293 PyObject *tmp;
d31cd03677fb added an incomplete Vector2 to the math module.
lorenz
parents:
diff changeset
294
d31cd03677fb added an incomplete Vector2 to the math module.
lorenz
parents:
diff changeset
295 switch(dim) {
d31cd03677fb added an incomplete Vector2 to the math module.
lorenz
parents:
diff changeset
296 case 2:
d31cd03677fb added an incomplete Vector2 to the math module.
lorenz
parents:
diff changeset
297 if (PyVector2_Check(obj)) {
d31cd03677fb added an incomplete Vector2 to the math module.
lorenz
parents:
diff changeset
298 return 1;
d31cd03677fb added an incomplete Vector2 to the math module.
lorenz
parents:
diff changeset
299 }
d31cd03677fb added an incomplete Vector2 to the math module.
lorenz
parents:
diff changeset
300 break;
d31cd03677fb added an incomplete Vector2 to the math module.
lorenz
parents:
diff changeset
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
d31cd03677fb added an incomplete Vector2 to the math module.
lorenz
parents:
diff changeset
303 return 1;
d31cd03677fb added an incomplete Vector2 to the math module.
lorenz
parents:
diff changeset
304 }
d31cd03677fb added an incomplete Vector2 to the math module.
lorenz
parents:
diff changeset
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
d31cd03677fb added an incomplete Vector2 to the math module.
lorenz
parents:
diff changeset
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
d31cd03677fb added an incomplete Vector2 to the math module.
lorenz
parents:
diff changeset
309 return 1;
d31cd03677fb added an incomplete Vector2 to the math module.
lorenz
parents:
diff changeset
310 }
d31cd03677fb added an incomplete Vector2 to the math module.
lorenz
parents:
diff changeset
311 break;
d31cd03677fb added an incomplete Vector2 to the math module.
lorenz
parents:
diff changeset
312 */
d31cd03677fb added an incomplete Vector2 to the math module.
lorenz
parents:
diff changeset
313 default:
d31cd03677fb added an incomplete Vector2 to the math module.
lorenz
parents:
diff changeset
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
d31cd03677fb added an incomplete Vector2 to the math module.
lorenz
parents:
diff changeset
316 return 0;
d31cd03677fb added an incomplete Vector2 to the math module.
lorenz
parents:
diff changeset
317 }
d31cd03677fb added an incomplete Vector2 to the math module.
lorenz
parents:
diff changeset
318
d31cd03677fb added an incomplete Vector2 to the math module.
lorenz
parents:
diff changeset
319 if (!PySequence_Check(obj) || (PySequence_Length(obj) != dim)) {
d31cd03677fb added an incomplete Vector2 to the math module.
lorenz
parents:
diff changeset
320 return 0;
d31cd03677fb added an incomplete Vector2 to the math module.
lorenz
parents:
diff changeset
321 }
d31cd03677fb added an incomplete Vector2 to the math module.
lorenz
parents:
diff changeset
322
d31cd03677fb added an incomplete Vector2 to the math module.
lorenz
parents:
diff changeset
323 for (i = 0; i < dim; ++i) {
d31cd03677fb added an incomplete Vector2 to the math module.
lorenz
parents:
diff changeset
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
d31cd03677fb added an incomplete Vector2 to the math module.
lorenz
parents:
diff changeset
326 Py_DECREF(tmp);
d31cd03677fb added an incomplete Vector2 to the math module.
lorenz
parents:
diff changeset
327 return 0;
d31cd03677fb added an incomplete Vector2 to the math module.
lorenz
parents:
diff changeset
328 }
d31cd03677fb added an incomplete Vector2 to the math module.
lorenz
parents:
diff changeset
329 Py_DECREF(tmp);
d31cd03677fb added an incomplete Vector2 to the math module.
lorenz
parents:
diff changeset
330 }
d31cd03677fb added an incomplete Vector2 to the math module.
lorenz
parents:
diff changeset
331 return 1;
d31cd03677fb added an incomplete Vector2 to the math module.
lorenz
parents:
diff changeset
332 }
d31cd03677fb added an incomplete Vector2 to the math module.
lorenz
parents:
diff changeset
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
d31cd03677fb added an incomplete Vector2 to the math module.
lorenz
parents:
diff changeset
344
d31cd03677fb added an incomplete Vector2 to the math module.
lorenz
parents:
diff changeset
345 static PyMemberDef vector_members[] = {
d31cd03677fb added an incomplete Vector2 to the math module.
lorenz
parents:
diff changeset
346 {"epsilon", T_DOUBLE, offsetof(PyVector, epsilon), 0,
d31cd03677fb added an incomplete Vector2 to the math module.
lorenz
parents:
diff changeset
347 "small value used in comparisons"},
d31cd03677fb added an incomplete Vector2 to the math module.
lorenz
parents:
diff changeset
348 {NULL} /* Sentinel */
d31cd03677fb added an incomplete Vector2 to the math module.
lorenz
parents:
diff changeset
349 };
d31cd03677fb added an incomplete Vector2 to the math module.
lorenz
parents:
diff changeset
350
d31cd03677fb added an incomplete Vector2 to the math module.
lorenz
parents:
diff changeset
351
d31cd03677fb added an incomplete Vector2 to the math module.
lorenz
parents:
diff changeset
352 static PyObject*
d31cd03677fb added an incomplete Vector2 to the math module.
lorenz
parents:
diff changeset
353 PyVector_NEW(int dim)
d31cd03677fb added an incomplete Vector2 to the math module.
lorenz
parents:
diff changeset
354 {
d31cd03677fb added an incomplete Vector2 to the math module.
lorenz
parents:
diff changeset
355 PyVector *vec;
d31cd03677fb added an incomplete Vector2 to the math module.
lorenz
parents:
diff changeset
356 switch (dim) {
d31cd03677fb added an incomplete Vector2 to the math module.
lorenz
parents:
diff changeset
357 case 2:
d31cd03677fb added an incomplete Vector2 to the math module.
lorenz
parents:
diff changeset
358 vec = PyObject_New(PyVector, &PyVector2_Type);
d31cd03677fb added an incomplete Vector2 to the math module.
lorenz
parents:
diff changeset
359 break;
d31cd03677fb added an incomplete Vector2 to the math module.
lorenz
parents:
diff changeset
360 case 3:
d31cd03677fb added an incomplete Vector2 to the math module.
lorenz
parents:
diff changeset
361 vec = PyObject_New(PyVector, &PyVector3_Type);
d31cd03677fb added an incomplete Vector2 to the math module.
lorenz
parents:
diff changeset
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
d31cd03677fb added an incomplete Vector2 to the math module.
lorenz
parents:
diff changeset
364 case 4:
d31cd03677fb added an incomplete Vector2 to the math module.
lorenz
parents:
diff changeset
365 vec = PyObject_New(PyVector, &PyVector4_Type);
d31cd03677fb added an incomplete Vector2 to the math module.
lorenz
parents:
diff changeset
366 break;
d31cd03677fb added an incomplete Vector2 to the math module.
lorenz
parents:
diff changeset
367 */
d31cd03677fb added an incomplete Vector2 to the math module.
lorenz
parents:
diff changeset
368 default:
d31cd03677fb added an incomplete Vector2 to the math module.
lorenz
parents:
diff changeset
369 PyErr_SetString(PyExc_SystemError,
d31cd03677fb added an incomplete Vector2 to the math module.
lorenz
parents:
diff changeset
370 "Wrong internal call to PyVector_NEW.\n");
d31cd03677fb added an incomplete Vector2 to the math module.
lorenz
parents:
diff changeset
371 return NULL;
d31cd03677fb added an incomplete Vector2 to the math module.
lorenz
parents:
diff changeset
372 }
d31cd03677fb added an incomplete Vector2 to the math module.
lorenz
parents:
diff changeset
373
d31cd03677fb added an incomplete Vector2 to the math module.
lorenz
parents:
diff changeset
374 if (vec != NULL) {
d31cd03677fb added an incomplete Vector2 to the math module.
lorenz
parents:
diff changeset
375 vec->dim = dim;
d31cd03677fb added an incomplete Vector2 to the math module.
lorenz
parents:
diff changeset
376 vec->epsilon = FLT_EPSILON;
d31cd03677fb added an incomplete Vector2 to the math module.
lorenz
parents:
diff changeset
377 vec->coords = PyMem_New(double, dim);
d31cd03677fb added an incomplete Vector2 to the math module.
lorenz
parents:
diff changeset
378 if (vec->coords == NULL) {
d31cd03677fb added an incomplete Vector2 to the math module.
lorenz
parents:
diff changeset
379 Py_DECREF(vec);
d31cd03677fb added an incomplete Vector2 to the math module.
lorenz
parents:
diff changeset
380 return PyErr_NoMemory();
d31cd03677fb added an incomplete Vector2 to the math module.
lorenz
parents:
diff changeset
381 }
d31cd03677fb added an incomplete Vector2 to the math module.
lorenz
parents:
diff changeset
382 }
d31cd03677fb added an incomplete Vector2 to the math module.
lorenz
parents:
diff changeset
383
d31cd03677fb added an incomplete Vector2 to the math module.
lorenz
parents:
diff changeset
384 return (PyObject *)vec;
d31cd03677fb added an incomplete Vector2 to the math module.
lorenz
parents:
diff changeset
385 }
d31cd03677fb added an incomplete Vector2 to the math module.
lorenz
parents:
diff changeset
386
d31cd03677fb added an incomplete Vector2 to the math module.
lorenz
parents:
diff changeset
387 static void
d31cd03677fb added an incomplete Vector2 to the math module.
lorenz
parents:
diff changeset
388 vector_dealloc(PyVector* self)
d31cd03677fb added an incomplete Vector2 to the math module.
lorenz
parents:
diff changeset
389 {
d31cd03677fb added an incomplete Vector2 to the math module.
lorenz
parents:
diff changeset
390 PyMem_Del(self->coords);
d31cd03677fb added an incomplete Vector2 to the math module.
lorenz
parents:
diff changeset
391 self->ob_type->tp_free((PyObject*)self);
d31cd03677fb added an incomplete Vector2 to the math module.
lorenz
parents:
diff changeset
392 }
d31cd03677fb added an incomplete Vector2 to the math module.
lorenz
parents:
diff changeset
393
d31cd03677fb added an incomplete Vector2 to the math module.
lorenz
parents:
diff changeset
394
d31cd03677fb added an incomplete Vector2 to the math module.
lorenz
parents:
diff changeset
395
d31cd03677fb added an incomplete Vector2 to the math module.
lorenz
parents:
diff changeset
396
d31cd03677fb added an incomplete Vector2 to the math module.
lorenz
parents:
diff changeset
397
d31cd03677fb added an incomplete Vector2 to the math module.
lorenz
parents:
diff changeset
398
d31cd03677fb added an incomplete Vector2 to the math module.
lorenz
parents:
diff changeset
399 /**********************************************
d31cd03677fb added an incomplete Vector2 to the math module.
lorenz
parents:
diff changeset
400 * Generic vector PyNumber emulation routines
d31cd03677fb added an incomplete Vector2 to the math module.
lorenz
parents:
diff changeset
401 **********************************************/
d31cd03677fb added an incomplete Vector2 to the math module.
lorenz
parents:
diff changeset
402
d31cd03677fb added an incomplete Vector2 to the math module.
lorenz
parents:
diff changeset
403 static PyObject *
1848
bbc21cd6faeb some last minute changes before I merge to trunk.
lorenz
parents: 1847
diff changeset
404 vector_generic_math(PyObject *o1, PyObject *o2, int op)
1834
d31cd03677fb added an incomplete Vector2 to the math module.
lorenz
parents:
diff changeset
405 {
1848
bbc21cd6faeb some last minute changes before I merge to trunk.
lorenz
parents: 1847
diff changeset
406 int i, dim;
bbc21cd6faeb some last minute changes before I merge to trunk.
lorenz
parents: 1847
diff changeset
407 double *vec_coords;
bbc21cd6faeb some last minute changes before I merge to trunk.
lorenz
parents: 1847
diff changeset
408 double tmp;
bbc21cd6faeb some last minute changes before I merge to trunk.
lorenz
parents: 1847
diff changeset
409 PyObject *other;
bbc21cd6faeb some last minute changes before I merge to trunk.
lorenz
parents: 1847
diff changeset
410 PyVector *vec, *ret;
1834
d31cd03677fb added an incomplete Vector2 to the math module.
lorenz
parents:
diff changeset
411 if (PyVector_Check(o1)) {
1848
bbc21cd6faeb some last minute changes before I merge to trunk.
lorenz
parents: 1847
diff changeset
412 vec = (PyVector *)o1;
bbc21cd6faeb some last minute changes before I merge to trunk.
lorenz
parents: 1847
diff changeset
413 other = o2;
1834
d31cd03677fb added an incomplete Vector2 to the math module.
lorenz
parents:
diff changeset
414 }
d31cd03677fb added an incomplete Vector2 to the math module.
lorenz
parents:
diff changeset
415 else {
1848
bbc21cd6faeb some last minute changes before I merge to trunk.
lorenz
parents: 1847
diff changeset
416 vec = (PyVector *)o2;
bbc21cd6faeb some last minute changes before I merge to trunk.
lorenz
parents: 1847
diff changeset
417 other = o1;
bbc21cd6faeb some last minute changes before I merge to trunk.
lorenz
parents: 1847
diff changeset
418 op |= OP_ARG_REVERSE;
1834
d31cd03677fb added an incomplete Vector2 to the math module.
lorenz
parents:
diff changeset
419 }
1848
bbc21cd6faeb some last minute changes before I merge to trunk.
lorenz
parents: 1847
diff changeset
420 dim = vec->dim;
bbc21cd6faeb some last minute changes before I merge to trunk.
lorenz
parents: 1847
diff changeset
421 vec_coords = vec->coords;
bbc21cd6faeb some last minute changes before I merge to trunk.
lorenz
parents: 1847
diff changeset
422
bbc21cd6faeb some last minute changes before I merge to trunk.
lorenz
parents: 1847
diff changeset
423 if (PyVectorCompatible_Check(other, dim))
bbc21cd6faeb some last minute changes before I merge to trunk.
lorenz
parents: 1847
diff changeset
424 op |= OP_ARG_VECTOR;
bbc21cd6faeb some last minute changes before I merge to trunk.
lorenz
parents: 1847
diff changeset
425 else if (RealNumber_Check(other))
bbc21cd6faeb some last minute changes before I merge to trunk.
lorenz
parents: 1847
diff changeset
426 op |= OP_ARG_NUMBER;
bbc21cd6faeb some last minute changes before I merge to trunk.
lorenz
parents: 1847
diff changeset
427 else
bbc21cd6faeb some last minute changes before I merge to trunk.
lorenz
parents: 1847
diff changeset
428 op |= OP_ARG_UNKNOWN;
bbc21cd6faeb some last minute changes before I merge to trunk.
lorenz
parents: 1847
diff changeset
429
bbc21cd6faeb some last minute changes before I merge to trunk.
lorenz
parents: 1847
diff changeset
430 switch (op) {
bbc21cd6faeb some last minute changes before I merge to trunk.
lorenz
parents: 1847
diff changeset
431 case OP_ADD | OP_ARG_VECTOR:
bbc21cd6faeb some last minute changes before I merge to trunk.
lorenz
parents: 1847
diff changeset
432 case OP_ADD | OP_ARG_VECTOR | OP_ARG_REVERSE:
bbc21cd6faeb some last minute changes before I merge to trunk.
lorenz
parents: 1847
diff changeset
433 ret = (PyVector*)PyVector_NEW(dim);
bbc21cd6faeb some last minute changes before I merge to trunk.
lorenz
parents: 1847
diff changeset
434 for (i = 0; i < dim; i++) {
bbc21cd6faeb some last minute changes before I merge to trunk.
lorenz
parents: 1847
diff changeset
435 ret->coords[i] = (vec_coords[i] +
bbc21cd6faeb some last minute changes before I merge to trunk.
lorenz
parents: 1847
diff changeset
436 PySequence_GetItem_AsDouble(other, i));
bbc21cd6faeb some last minute changes before I merge to trunk.
lorenz
parents: 1847
diff changeset
437 }
bbc21cd6faeb some last minute changes before I merge to trunk.
lorenz
parents: 1847
diff changeset
438 return (PyObject*)ret;
bbc21cd6faeb some last minute changes before I merge to trunk.
lorenz
parents: 1847
diff changeset
439 case OP_IADD | OP_ARG_VECTOR:
bbc21cd6faeb some last minute changes before I merge to trunk.
lorenz
parents: 1847
diff changeset
440 for (i = 0; i < dim; i++)
bbc21cd6faeb some last minute changes before I merge to trunk.
lorenz
parents: 1847
diff changeset
441 vec_coords[i] += PySequence_GetItem_AsDouble(other, i);
bbc21cd6faeb some last minute changes before I merge to trunk.
lorenz
parents: 1847
diff changeset
442 return (PyObject*)vec;
bbc21cd6faeb some last minute changes before I merge to trunk.
lorenz
parents: 1847
diff changeset
443 case OP_SUB | OP_ARG_VECTOR:
bbc21cd6faeb some last minute changes before I merge to trunk.
lorenz
parents: 1847
diff changeset
444 ret = (PyVector*)PyVector_NEW(dim);
bbc21cd6faeb some last minute changes before I merge to trunk.
lorenz
parents: 1847
diff changeset
445 for (i = 0; i < dim; i++) {
bbc21cd6faeb some last minute changes before I merge to trunk.
lorenz
parents: 1847
diff changeset
446 ret->coords[i] = (vec_coords[i] -
bbc21cd6faeb some last minute changes before I merge to trunk.
lorenz
parents: 1847
diff changeset
447 PySequence_GetItem_AsDouble(other, i));
bbc21cd6faeb some last minute changes before I merge to trunk.
lorenz
parents: 1847
diff changeset
448 }
bbc21cd6faeb some last minute changes before I merge to trunk.
lorenz
parents: 1847
diff changeset
449 return (PyObject*)ret;
bbc21cd6faeb some last minute changes before I merge to trunk.
lorenz
parents: 1847
diff changeset
450 case OP_SUB | OP_ARG_VECTOR | OP_ARG_REVERSE:
bbc21cd6faeb some last minute changes before I merge to trunk.
lorenz
parents: 1847
diff changeset
451 ret = (PyVector*)PyVector_NEW(dim);
bbc21cd6faeb some last minute changes before I merge to trunk.
lorenz
parents: 1847
diff changeset
452 for (i = 0; i < dim; i++) {
bbc21cd6faeb some last minute changes before I merge to trunk.
lorenz
parents: 1847
diff changeset
453 ret->coords[i] = (PySequence_GetItem_AsDouble(other, i) -
bbc21cd6faeb some last minute changes before I merge to trunk.
lorenz
parents: 1847
diff changeset
454 vec_coords[i]);
1834
d31cd03677fb added an incomplete Vector2 to the math module.
lorenz
parents:
diff changeset
455 }
1848
bbc21cd6faeb some last minute changes before I merge to trunk.
lorenz
parents: 1847
diff changeset
456 return (PyObject*)ret;
bbc21cd6faeb some last minute changes before I merge to trunk.
lorenz
parents: 1847
diff changeset
457 case OP_ISUB | OP_ARG_VECTOR:
bbc21cd6faeb some last minute changes before I merge to trunk.
lorenz
parents: 1847
diff changeset
458 for (i = 0; i < dim; i++)
bbc21cd6faeb some last minute changes before I merge to trunk.
lorenz
parents: 1847
diff changeset
459 vec_coords[i] -= PySequence_GetItem_AsDouble(other, i);
bbc21cd6faeb some last minute changes before I merge to trunk.
lorenz
parents: 1847
diff changeset
460 return (PyObject*)vec;
bbc21cd6faeb some last minute changes before I merge to trunk.
lorenz
parents: 1847
diff changeset
461 case OP_MUL | OP_ARG_VECTOR:
bbc21cd6faeb some last minute changes before I merge to trunk.
lorenz
parents: 1847
diff changeset
462 case OP_MUL | OP_ARG_VECTOR | OP_ARG_REVERSE:
bbc21cd6faeb some last minute changes before I merge to trunk.
lorenz
parents: 1847
diff changeset
463 tmp = 0.;
bbc21cd6faeb some last minute changes before I merge to trunk.
lorenz
parents: 1847
diff changeset
464 for (i = 0; i < dim; i++)
bbc21cd6faeb some last minute changes before I merge to trunk.
lorenz
parents: 1847
diff changeset
465 tmp += vec_coords[i] * PySequence_GetItem_AsDouble(other, i);
bbc21cd6faeb some last minute changes before I merge to trunk.
lorenz
parents: 1847
diff changeset
466 return PyFloat_FromDouble(tmp);
bbc21cd6faeb some last minute changes before I merge to trunk.
lorenz
parents: 1847
diff changeset
467 case OP_MUL | OP_ARG_NUMBER:
bbc21cd6faeb some last minute changes before I merge to trunk.
lorenz
parents: 1847
diff changeset
468 case OP_MUL | OP_ARG_NUMBER | OP_ARG_REVERSE:
bbc21cd6faeb some last minute changes before I merge to trunk.
lorenz
parents: 1847
diff changeset
469 ret = (PyVector*)PyVector_NEW(dim);
bbc21cd6faeb some last minute changes before I merge to trunk.
lorenz
parents: 1847
diff changeset
470 tmp = PyFloat_AsDouble(other);
bbc21cd6faeb some last minute changes before I merge to trunk.
lorenz
parents: 1847
diff changeset
471 for (i = 0; i < dim; i++)
bbc21cd6faeb some last minute changes before I merge to trunk.
lorenz
parents: 1847
diff changeset
472 ret->coords[i] = vec_coords[i] * tmp;
bbc21cd6faeb some last minute changes before I merge to trunk.
lorenz
parents: 1847
diff changeset
473 return (PyObject*)ret;
bbc21cd6faeb some last minute changes before I merge to trunk.
lorenz
parents: 1847
diff changeset
474 case OP_IMUL | OP_ARG_NUMBER:
bbc21cd6faeb some last minute changes before I merge to trunk.
lorenz
parents: 1847
diff changeset
475 tmp = PyFloat_AsDouble(other);
bbc21cd6faeb some last minute changes before I merge to trunk.
lorenz
parents: 1847
diff changeset
476 for (i = 0; i < dim; i++)
bbc21cd6faeb some last minute changes before I merge to trunk.
lorenz
parents: 1847
diff changeset
477 vec_coords[i] *= tmp;
bbc21cd6faeb some last minute changes before I merge to trunk.
lorenz
parents: 1847
diff changeset
478 return (PyObject*)vec;
bbc21cd6faeb some last minute changes before I merge to trunk.
lorenz
parents: 1847
diff changeset
479 case OP_DIV | OP_ARG_NUMBER:
bbc21cd6faeb some last minute changes before I merge to trunk.
lorenz
parents: 1847
diff changeset
480 tmp = 1. / PyFloat_AsDouble(other);
bbc21cd6faeb some last minute changes before I merge to trunk.
lorenz
parents: 1847
diff changeset
481 ret = (PyVector*)PyVector_NEW(dim);
bbc21cd6faeb some last minute changes before I merge to trunk.
lorenz
parents: 1847
diff changeset
482 for (i = 0; i < dim; i++)
bbc21cd6faeb some last minute changes before I merge to trunk.
lorenz
parents: 1847
diff changeset
483 ret->coords[i] = vec_coords[i] * tmp;
bbc21cd6faeb some last minute changes before I merge to trunk.
lorenz
parents: 1847
diff changeset
484 return (PyObject*)ret;
bbc21cd6faeb some last minute changes before I merge to trunk.
lorenz
parents: 1847
diff changeset
485 case OP_IDIV | OP_ARG_NUMBER:
bbc21cd6faeb some last minute changes before I merge to trunk.
lorenz
parents: 1847
diff changeset
486 tmp = 1. / PyFloat_AsDouble(other);
bbc21cd6faeb some last minute changes before I merge to trunk.
lorenz
parents: 1847
diff changeset
487 for (i = 0; i < dim; i++)
bbc21cd6faeb some last minute changes before I merge to trunk.
lorenz
parents: 1847
diff changeset
488 vec_coords[i] *= tmp;
bbc21cd6faeb some last minute changes before I merge to trunk.
lorenz
parents: 1847
diff changeset
489 return (PyObject*)vec;
bbc21cd6faeb some last minute changes before I merge to trunk.
lorenz
parents: 1847
diff changeset
490 case OP_FLOOR_DIV | OP_ARG_NUMBER:
bbc21cd6faeb some last minute changes before I merge to trunk.
lorenz
parents: 1847
diff changeset
491 tmp = 1. / PyFloat_AsDouble(other);
bbc21cd6faeb some last minute changes before I merge to trunk.
lorenz
parents: 1847
diff changeset
492 ret = (PyVector*)PyVector_NEW(dim);
bbc21cd6faeb some last minute changes before I merge to trunk.
lorenz
parents: 1847
diff changeset
493 for (i = 0; i < dim; i++)
bbc21cd6faeb some last minute changes before I merge to trunk.
lorenz
parents: 1847
diff changeset
494 ret->coords[i] = floor(vec_coords[i] * tmp);
bbc21cd6faeb some last minute changes before I merge to trunk.
lorenz
parents: 1847
diff changeset
495 return (PyObject*)ret;
bbc21cd6faeb some last minute changes before I merge to trunk.
lorenz
parents: 1847
diff changeset
496 case OP_IFLOOR_DIV | OP_ARG_NUMBER:
bbc21cd6faeb some last minute changes before I merge to trunk.
lorenz
parents: 1847
diff changeset
497 tmp = 1. / PyFloat_AsDouble(other);
bbc21cd6faeb some last minute changes before I merge to trunk.
lorenz
parents: 1847
diff changeset
498 for (i = 0; i < dim; i++)
bbc21cd6faeb some last minute changes before I merge to trunk.
lorenz
parents: 1847
diff changeset
499 vec->coords[i] = floor(vec_coords[i] * tmp);
bbc21cd6faeb some last minute changes before I merge to trunk.
lorenz
parents: 1847
diff changeset
500 return (PyObject*)vec;
bbc21cd6faeb some last minute changes before I merge to trunk.
lorenz
parents: 1847
diff changeset
501 default:
bbc21cd6faeb some last minute changes before I merge to trunk.
lorenz
parents: 1847
diff changeset
502 Py_INCREF(Py_NotImplemented);
bbc21cd6faeb some last minute changes before I merge to trunk.
lorenz
parents: 1847
diff changeset
503 return Py_NotImplemented;
1834
d31cd03677fb added an incomplete Vector2 to the math module.
lorenz
parents:
diff changeset
504 }
d31cd03677fb added an incomplete Vector2 to the math module.
lorenz
parents:
diff changeset
505 }
d31cd03677fb added an incomplete Vector2 to the math module.
lorenz
parents:
diff changeset
506
d31cd03677fb added an incomplete Vector2 to the math module.
lorenz
parents:
diff changeset
507
d31cd03677fb added an incomplete Vector2 to the math module.
lorenz
parents:
diff changeset
508 static PyObject *
1848
bbc21cd6faeb some last minute changes before I merge to trunk.
lorenz
parents: 1847
diff changeset
509 vector_add(PyObject *o1, PyObject *o2)
bbc21cd6faeb some last minute changes before I merge to trunk.
lorenz
parents: 1847
diff changeset
510 {
bbc21cd6faeb some last minute changes before I merge to trunk.
lorenz
parents: 1847
diff changeset
511 return vector_generic_math(o1, o2, OP_ADD);
bbc21cd6faeb some last minute changes before I merge to trunk.
lorenz
parents: 1847
diff changeset
512 }
bbc21cd6faeb some last minute changes before I merge to trunk.
lorenz
parents: 1847
diff changeset
513 static PyObject *
bbc21cd6faeb some last minute changes before I merge to trunk.
lorenz
parents: 1847
diff changeset
514 vector_inplace_add(PyVector *o1, PyObject *o2)
bbc21cd6faeb some last minute changes before I merge to trunk.
lorenz
parents: 1847
diff changeset
515 {
bbc21cd6faeb some last minute changes before I merge to trunk.
lorenz
parents: 1847
diff changeset
516 return vector_generic_math((PyObject*)o1, o2, OP_IADD);
bbc21cd6faeb some last minute changes before I merge to trunk.
lorenz
parents: 1847
diff changeset
517 }
bbc21cd6faeb some last minute changes before I merge to trunk.
lorenz
parents: 1847
diff changeset
518 static PyObject *
1834
d31cd03677fb added an incomplete Vector2 to the math module.
lorenz
parents:
diff changeset
519 vector_sub(PyObject *o1, PyObject *o2)
d31cd03677fb added an incomplete Vector2 to the math module.
lorenz
parents:
diff changeset
520 {
1848
bbc21cd6faeb some last minute changes before I merge to trunk.
lorenz
parents: 1847
diff changeset
521 return vector_generic_math(o1, o2, OP_SUB);
1834
d31cd03677fb added an incomplete Vector2 to the math module.
lorenz
parents:
diff changeset
522 }
d31cd03677fb added an incomplete Vector2 to the math module.
lorenz
parents:
diff changeset
523 static PyObject *
1848
bbc21cd6faeb some last minute changes before I merge to trunk.
lorenz
parents: 1847
diff changeset
524 vector_inplace_sub(PyVector *o1, PyObject *o2)
1834
d31cd03677fb added an incomplete Vector2 to the math module.
lorenz
parents:
diff changeset
525 {
1848
bbc21cd6faeb some last minute changes before I merge to trunk.
lorenz
parents: 1847
diff changeset
526 return vector_generic_math((PyObject*)o1, o2, OP_ISUB);
1834
d31cd03677fb added an incomplete Vector2 to the math module.
lorenz
parents:
diff changeset
527 }
d31cd03677fb added an incomplete Vector2 to the math module.
lorenz
parents:
diff changeset
528 static PyObject *
d31cd03677fb added an incomplete Vector2 to the math module.
lorenz
parents:
diff changeset
529 vector_mul(PyObject *o1, PyObject *o2)
d31cd03677fb added an incomplete Vector2 to the math module.
lorenz
parents:
diff changeset
530 {
1848
bbc21cd6faeb some last minute changes before I merge to trunk.
lorenz
parents: 1847
diff changeset
531 return vector_generic_math(o1, o2, OP_MUL);
1834
d31cd03677fb added an incomplete Vector2 to the math module.
lorenz
parents:
diff changeset
532 }
d31cd03677fb added an incomplete Vector2 to the math module.
lorenz
parents:
diff changeset
533 static PyObject *
1848
bbc21cd6faeb some last minute changes before I merge to trunk.
lorenz
parents: 1847
diff changeset
534 vector_inplace_mul(PyVector *o1, PyObject *o2)
1834
d31cd03677fb added an incomplete Vector2 to the math module.
lorenz
parents:
diff changeset
535 {
1848
bbc21cd6faeb some last minute changes before I merge to trunk.
lorenz
parents: 1847
diff changeset
536 return vector_generic_math((PyObject*)o1, o2, OP_IMUL);
bbc21cd6faeb some last minute changes before I merge to trunk.
lorenz
parents: 1847
diff changeset
537 }
bbc21cd6faeb some last minute changes before I merge to trunk.
lorenz
parents: 1847
diff changeset
538 static PyObject *
bbc21cd6faeb some last minute changes before I merge to trunk.
lorenz
parents: 1847
diff changeset
539 vector_div(PyVector *o1, PyObject *o2)
bbc21cd6faeb some last minute changes before I merge to trunk.
lorenz
parents: 1847
diff changeset
540 {
bbc21cd6faeb some last minute changes before I merge to trunk.
lorenz
parents: 1847
diff changeset
541 return vector_generic_math((PyObject*)o1, o2, OP_DIV);
1834
d31cd03677fb added an incomplete Vector2 to the math module.
lorenz
parents:
diff changeset
542 }
d31cd03677fb added an incomplete Vector2 to the math module.
lorenz
parents:
diff changeset
543 static PyObject *
1848
bbc21cd6faeb some last minute changes before I merge to trunk.
lorenz
parents: 1847
diff changeset
544 vector_inplace_div(PyVector *o1, PyObject *o2)
1834
d31cd03677fb added an incomplete Vector2 to the math module.
lorenz
parents:
diff changeset
545 {
1848
bbc21cd6faeb some last minute changes before I merge to trunk.
lorenz
parents: 1847
diff changeset
546 return vector_generic_math((PyObject*)o1, o2, OP_IDIV);
1834
d31cd03677fb added an incomplete Vector2 to the math module.
lorenz
parents:
diff changeset
547 }
d31cd03677fb added an incomplete Vector2 to the math module.
lorenz
parents:
diff changeset
548 static PyObject *
1848
bbc21cd6faeb some last minute changes before I merge to trunk.
lorenz
parents: 1847
diff changeset
549 vector_floor_div(PyVector *o1, PyObject *o2)
1834
d31cd03677fb added an incomplete Vector2 to the math module.
lorenz
parents:
diff changeset
550 {
1848
bbc21cd6faeb some last minute changes before I merge to trunk.
lorenz
parents: 1847
diff changeset
551 return vector_generic_math((PyObject*)o1, o2, OP_FLOOR_DIV);
1834
d31cd03677fb added an incomplete Vector2 to the math module.
lorenz
parents:
diff changeset
552 }
d31cd03677fb added an incomplete Vector2 to the math module.
lorenz
parents:
diff changeset
553 static PyObject *
1848
bbc21cd6faeb some last minute changes before I merge to trunk.
lorenz
parents: 1847
diff changeset
554 vector_inplace_floor_div(PyVector *o1, PyObject *o2)
1834
d31cd03677fb added an incomplete Vector2 to the math module.
lorenz
parents:
diff changeset
555 {
1848
bbc21cd6faeb some last minute changes before I merge to trunk.
lorenz
parents: 1847
diff changeset
556 return vector_generic_math((PyObject*)o1, o2, OP_IFLOOR_DIV);
1834
d31cd03677fb added an incomplete Vector2 to the math module.
lorenz
parents:
diff changeset
557 }
d31cd03677fb added an incomplete Vector2 to the math module.
lorenz
parents:
diff changeset
558
d31cd03677fb added an incomplete Vector2 to the math module.
lorenz
parents:
diff changeset
559 static PyObject *
d31cd03677fb added an incomplete Vector2 to the math module.
lorenz
parents:
diff changeset
560 vector_neg(PyVector *self)
d31cd03677fb added an incomplete Vector2 to the math module.
lorenz
parents:
diff changeset
561 {
d31cd03677fb added an incomplete Vector2 to the math module.
lorenz
parents:
diff changeset
562 int i;
d31cd03677fb added an incomplete Vector2 to the math module.
lorenz
parents:
diff changeset
563 PyVector *ret = (PyVector*)PyVector_NEW(self->dim);
d31cd03677fb added an incomplete Vector2 to the math module.
lorenz
parents:
diff changeset
564 for (i = 0; i < self->dim; i++) {
d31cd03677fb added an incomplete Vector2 to the math module.
lorenz
parents:
diff changeset
565 ret->coords[i] = -self->coords[i];
d31cd03677fb added an incomplete Vector2 to the math module.
lorenz
parents:
diff changeset
566 }
d31cd03677fb added an incomplete Vector2 to the math module.
lorenz
parents:
diff changeset
567 return (PyObject*)ret;
d31cd03677fb added an incomplete Vector2 to the math module.
lorenz
parents:
diff changeset
568 }
d31cd03677fb added an incomplete Vector2 to the math module.
lorenz
parents:
diff changeset
569
d31cd03677fb added an incomplete Vector2 to the math module.
lorenz
parents:
diff changeset
570 static PyObject *
d31cd03677fb added an incomplete Vector2 to the math module.
lorenz
parents:
diff changeset
571 vector_pos(PyVector *self)
d31cd03677fb added an incomplete Vector2 to the math module.
lorenz
parents:
diff changeset
572 {
d31cd03677fb added an incomplete Vector2 to the math module.
lorenz
parents:
diff changeset
573 int i;
d31cd03677fb added an incomplete Vector2 to the math module.
lorenz
parents:
diff changeset
574 PyVector *ret = (PyVector*)PyVector_NEW(self->dim);
d31cd03677fb added an incomplete Vector2 to the math module.
lorenz
parents:
diff changeset
575 memcpy(ret->coords, self->coords, sizeof(ret->coords[0]) * ret->dim);
d31cd03677fb added an incomplete Vector2 to the math module.
lorenz
parents:
diff changeset
576 return (PyObject*)ret;
d31cd03677fb added an incomplete Vector2 to the math module.
lorenz
parents:
diff changeset
577 }
d31cd03677fb added an incomplete Vector2 to the math module.
lorenz
parents:
diff changeset
578
d31cd03677fb added an incomplete Vector2 to the math module.
lorenz
parents:
diff changeset
579 static int
d31cd03677fb added an incomplete Vector2 to the math module.
lorenz
parents:
diff changeset
580 vector_nonzero(PyVector *self)
d31cd03677fb added an incomplete Vector2 to the math module.
lorenz
parents:
diff changeset
581 {
d31cd03677fb added an incomplete Vector2 to the math module.
lorenz
parents:
diff changeset
582 int i;
d31cd03677fb added an incomplete Vector2 to the math module.
lorenz
parents:
diff changeset
583 for (i = 0; i < self->dim; i++) {
d31cd03677fb added an incomplete Vector2 to the math module.
lorenz
parents:
diff changeset
584 if (fabs(self->coords[i]) > self->epsilon) {
d31cd03677fb added an incomplete Vector2 to the math module.
lorenz
parents:
diff changeset
585 return 1;
d31cd03677fb added an incomplete Vector2 to the math module.
lorenz
parents:
diff changeset
586 }
d31cd03677fb added an incomplete Vector2 to the math module.
lorenz
parents:
diff changeset
587 }
d31cd03677fb added an incomplete Vector2 to the math module.
lorenz
parents:
diff changeset
588 return 0;
d31cd03677fb added an incomplete Vector2 to the math module.
lorenz
parents:
diff changeset
589 }
d31cd03677fb added an incomplete Vector2 to the math module.
lorenz
parents:
diff changeset
590
1845
7d2a16e6a1d3 rearanged math module. added forward declarations at the beginning.
lorenz
parents: 1844
diff changeset
591 static PyNumberMethods vector_as_number = {
1834
d31cd03677fb added an incomplete Vector2 to the math module.
lorenz
parents:
diff changeset
592 (binaryfunc)vector_add, /* nb_add; __add__ */
d31cd03677fb added an incomplete Vector2 to the math module.
lorenz
parents:
diff changeset
593 (binaryfunc)vector_sub, /* nb_subtract; __sub__ */
d31cd03677fb added an incomplete Vector2 to the math module.
lorenz
parents:
diff changeset
594 (binaryfunc)vector_mul, /* nb_multiply; __mul__ */
d31cd03677fb added an incomplete Vector2 to the math module.
lorenz
parents:
diff changeset
595 (binaryfunc)vector_div, /* nb_divide; __div__ */
d31cd03677fb added an incomplete Vector2 to the math module.
lorenz
parents:
diff changeset
596 (binaryfunc)0, /* nb_remainder; __mod__ */
d31cd03677fb added an incomplete Vector2 to the math module.
lorenz
parents:
diff changeset
597 (binaryfunc)0, /* nb_divmod; __divmod__ */
d31cd03677fb added an incomplete Vector2 to the math module.
lorenz
parents:
diff changeset
598 (ternaryfunc)0, /* nb_power; __pow__ */
d31cd03677fb added an incomplete Vector2 to the math module.
lorenz
parents:
diff changeset
599 (unaryfunc)vector_neg, /* nb_negative; __neg__ */
d31cd03677fb added an incomplete Vector2 to the math module.
lorenz
parents:
diff changeset
600 (unaryfunc)vector_pos, /* nb_positive; __pos__ */
d31cd03677fb added an incomplete Vector2 to the math module.
lorenz
parents:
diff changeset
601 (unaryfunc)0, /* nb_absolute; __abs__ */
d31cd03677fb added an incomplete Vector2 to the math module.
lorenz
parents:
diff changeset
602 (inquiry)vector_nonzero, /* nb_nonzero; __nonzero__ */
d31cd03677fb added an incomplete Vector2 to the math module.
lorenz
parents:
diff changeset
603 (unaryfunc)0, /* nb_invert; __invert__ */
d31cd03677fb added an incomplete Vector2 to the math module.
lorenz
parents:
diff changeset
604 (binaryfunc)0, /* nb_lshift; __lshift__ */
d31cd03677fb added an incomplete Vector2 to the math module.
lorenz
parents:
diff changeset
605 (binaryfunc)0, /* nb_rshift; __rshift__ */
d31cd03677fb added an incomplete Vector2 to the math module.
lorenz
parents:
diff changeset
606 (binaryfunc)0, /* nb_and; __and__ */
d31cd03677fb added an incomplete Vector2 to the math module.
lorenz
parents:
diff changeset
607 (binaryfunc)0, /* nb_xor; __xor__ */
d31cd03677fb added an incomplete Vector2 to the math module.
lorenz
parents:
diff changeset
608 (binaryfunc)0, /* nb_or; __or__ */
d31cd03677fb added an incomplete Vector2 to the math module.
lorenz
parents:
diff changeset
609 (coercion)0, /* nb_coerce; __coerce__ */
d31cd03677fb added an incomplete Vector2 to the math module.
lorenz
parents:
diff changeset
610 (unaryfunc)0, /* nb_int; __int__ */
d31cd03677fb added an incomplete Vector2 to the math module.
lorenz
parents:
diff changeset
611 (unaryfunc)0, /* nb_long; __long__ */
d31cd03677fb added an incomplete Vector2 to the math module.
lorenz
parents:
diff changeset
612 (unaryfunc)0, /* nb_float; __float__ */
d31cd03677fb added an incomplete Vector2 to the math module.
lorenz
parents:
diff changeset
613 (unaryfunc)0, /* nb_oct; __oct__ */
d31cd03677fb added an incomplete Vector2 to the math module.
lorenz
parents:
diff changeset
614 (unaryfunc)0, /* nb_hex; __hex__ */
d31cd03677fb added an incomplete Vector2 to the math module.
lorenz
parents:
diff changeset
615
d31cd03677fb added an incomplete Vector2 to the math module.
lorenz
parents:
diff changeset
616 /* Added in release 2.0 */
d31cd03677fb added an incomplete Vector2 to the math module.
lorenz
parents:
diff changeset
617 (binaryfunc)vector_inplace_add, /* nb_inplace_add; __iadd__ */
d31cd03677fb added an incomplete Vector2 to the math module.
lorenz
parents:
diff changeset
618 (binaryfunc)vector_inplace_sub, /* nb_inplace_subtract; __isub__ */
d31cd03677fb added an incomplete Vector2 to the math module.
lorenz
parents:
diff changeset
619 (binaryfunc)vector_inplace_mul, /* nb_inplace_multiply; __imul__ */
d31cd03677fb added an incomplete Vector2 to the math module.
lorenz
parents:
diff changeset
620 (binaryfunc)vector_inplace_div, /* nb_inplace_divide; __idiv__ */
d31cd03677fb added an incomplete Vector2 to the math module.
lorenz
parents:
diff changeset
621 (binaryfunc)0, /* nb_inplace_remainder; __imod__ */
d31cd03677fb added an incomplete Vector2 to the math module.
lorenz
parents:
diff changeset
622 (ternaryfunc)0, /* nb_inplace_power; __pow__ */
d31cd03677fb added an incomplete Vector2 to the math module.
lorenz
parents:
diff changeset
623 (binaryfunc)0, /* nb_inplace_lshift; __ilshift__ */
d31cd03677fb added an incomplete Vector2 to the math module.
lorenz
parents:
diff changeset
624 (binaryfunc)0, /* nb_inplace_rshift; __irshift__ */
d31cd03677fb added an incomplete Vector2 to the math module.
lorenz
parents:
diff changeset
625 (binaryfunc)0, /* nb_inplace_and; __iand__ */
d31cd03677fb added an incomplete Vector2 to the math module.
lorenz
parents:
diff changeset
626 (binaryfunc)0, /* nb_inplace_xor; __ixor__ */
d31cd03677fb added an incomplete Vector2 to the math module.
lorenz
parents:
diff changeset
627 (binaryfunc)0, /* nb_inplace_or; __ior__ */
d31cd03677fb added an incomplete Vector2 to the math module.
lorenz
parents:
diff changeset
628
d31cd03677fb added an incomplete Vector2 to the math module.
lorenz
parents:
diff changeset
629 /* Added in release 2.2 */
d31cd03677fb added an incomplete Vector2 to the math module.
lorenz
parents:
diff changeset
630 (binaryfunc)vector_floor_div, /* nb_floor_divide; __floor__ */
d31cd03677fb added an incomplete Vector2 to the math module.
lorenz
parents:
diff changeset
631 (binaryfunc)vector_div, /* nb_true_divide; __truediv__ */
d31cd03677fb added an incomplete Vector2 to the math module.
lorenz
parents:
diff changeset
632 (binaryfunc)vector_inplace_floor_div, /* nb_inplace_floor_divide; __ifloor__ */
d31cd03677fb added an incomplete Vector2 to the math module.
lorenz
parents:
diff changeset
633 (binaryfunc)vector_inplace_div, /* nb_inplace_true_divide; __itruediv__ */
d31cd03677fb added an incomplete Vector2 to the math module.
lorenz
parents:
diff changeset
634
d31cd03677fb added an incomplete Vector2 to the math module.
lorenz
parents:
diff changeset
635 /* Added in release 2.5 */
d31cd03677fb added an incomplete Vector2 to the math module.
lorenz
parents:
diff changeset
636 (unaryfunc)0, /* nb_index; __index__ */
d31cd03677fb added an incomplete Vector2 to the math module.
lorenz
parents:
diff changeset
637 };
d31cd03677fb added an incomplete Vector2 to the math module.
lorenz
parents:
diff changeset
638
d31cd03677fb added an incomplete Vector2 to the math module.
lorenz
parents:
diff changeset
639
d31cd03677fb added an incomplete Vector2 to the math module.
lorenz
parents:
diff changeset
640
d31cd03677fb added an incomplete Vector2 to the math module.
lorenz
parents:
diff changeset
641 /*************************************************
d31cd03677fb added an incomplete Vector2 to the math module.
lorenz
parents:
diff changeset
642 * Generic vector PySequence emulation routines
d31cd03677fb added an incomplete Vector2 to the math module.
lorenz
parents:
diff changeset
643 *************************************************/
d31cd03677fb added an incomplete Vector2 to the math module.
lorenz
parents:
diff changeset
644
d31cd03677fb added an incomplete Vector2 to the math module.
lorenz
parents:
diff changeset
645 static Py_ssize_t
d31cd03677fb added an incomplete Vector2 to the math module.
lorenz
parents:
diff changeset
646 vector_len(PyVector *self)
d31cd03677fb added an incomplete Vector2 to the math module.
lorenz
parents:
diff changeset
647 {
d31cd03677fb added an incomplete Vector2 to the math module.
lorenz
parents:
diff changeset
648 return (Py_ssize_t)self->dim;
d31cd03677fb added an incomplete Vector2 to the math module.
lorenz
parents:
diff changeset
649 }
d31cd03677fb added an incomplete Vector2 to the math module.
lorenz
parents:
diff changeset
650
d31cd03677fb added an incomplete Vector2 to the math module.
lorenz
parents:
diff changeset
651 static PyObject *
d31cd03677fb added an incomplete Vector2 to the math module.
lorenz
parents:
diff changeset
652 vector_GetItem(PyVector *self, Py_ssize_t index)
d31cd03677fb added an incomplete Vector2 to the math module.
lorenz
parents:
diff changeset
653 {
d31cd03677fb added an incomplete Vector2 to the math module.
lorenz
parents:
diff changeset
654 if (index < 0 || index >= self->dim) {
1836
18eefb9bd5a2 updates to the pygame.math module
lorenz
parents: 1835
diff changeset
655 PyErr_SetString(PyExc_IndexError, "subscript out of range.");
18eefb9bd5a2 updates to the pygame.math module
lorenz
parents: 1835
diff changeset
656 return NULL;
1834
d31cd03677fb added an incomplete Vector2 to the math module.
lorenz
parents:
diff changeset
657 }
d31cd03677fb added an incomplete Vector2 to the math module.
lorenz
parents:
diff changeset
658 return PyFloat_FromDouble(self->coords[index]);
d31cd03677fb added an incomplete Vector2 to the math module.
lorenz
parents:
diff changeset
659 }
d31cd03677fb added an incomplete Vector2 to the math module.
lorenz
parents:
diff changeset
660
d31cd03677fb added an incomplete Vector2 to the math module.
lorenz
parents:
diff changeset
661 static int
d31cd03677fb added an incomplete Vector2 to the math module.
lorenz
parents:
diff changeset
662 vector_SetItem(PyVector *self, Py_ssize_t index, PyObject *value)
d31cd03677fb added an incomplete Vector2 to the math module.
lorenz
parents:
diff changeset
663 {
d31cd03677fb added an incomplete Vector2 to the math module.
lorenz
parents:
diff changeset
664 if (index < 0 || index >= self->dim) {
1836
18eefb9bd5a2 updates to the pygame.math module
lorenz
parents: 1835
diff changeset
665 PyErr_SetString(PyExc_IndexError, "subscript out of range.");
1834
d31cd03677fb added an incomplete Vector2 to the math module.
lorenz
parents:
diff changeset
666 return -1;
d31cd03677fb added an incomplete Vector2 to the math module.
lorenz
parents:
diff changeset
667 }
d31cd03677fb added an incomplete Vector2 to the math module.
lorenz
parents:
diff changeset
668 self->coords[index] = PyFloat_AsDouble(value);
d31cd03677fb added an incomplete Vector2 to the math module.
lorenz
parents:
diff changeset
669 return 0;
d31cd03677fb added an incomplete Vector2 to the math module.
lorenz
parents:
diff changeset
670 }
d31cd03677fb added an incomplete Vector2 to the math module.
lorenz
parents:
diff changeset
671
1836
18eefb9bd5a2 updates to the pygame.math module
lorenz
parents: 1835
diff changeset
672
18eefb9bd5a2 updates to the pygame.math module
lorenz
parents: 1835
diff changeset
673 static PyObject *
18eefb9bd5a2 updates to the pygame.math module
lorenz
parents: 1835
diff changeset
674 vector_GetSlice(PyVector *self, Py_ssize_t ilow, Py_ssize_t ihigh)
18eefb9bd5a2 updates to the pygame.math module
lorenz
parents: 1835
diff changeset
675 {
18eefb9bd5a2 updates to the pygame.math module
lorenz
parents: 1835
diff changeset
676 /* some code was taken from the CPython source listobject.c */
18eefb9bd5a2 updates to the pygame.math module
lorenz
parents: 1835
diff changeset
677 PyListObject *slice;
18eefb9bd5a2 updates to the pygame.math module
lorenz
parents: 1835
diff changeset
678 Py_ssize_t i, len;
18eefb9bd5a2 updates to the pygame.math module
lorenz
parents: 1835
diff changeset
679 double *src;
18eefb9bd5a2 updates to the pygame.math module
lorenz
parents: 1835
diff changeset
680 PyObject **dest;
18eefb9bd5a2 updates to the pygame.math module
lorenz
parents: 1835
diff changeset
681
18eefb9bd5a2 updates to the pygame.math module
lorenz
parents: 1835
diff changeset
682 /* make sure boundaries are sane */
18eefb9bd5a2 updates to the pygame.math module
lorenz
parents: 1835
diff changeset
683 if (ilow < 0)
18eefb9bd5a2 updates to the pygame.math module
lorenz
parents: 1835
diff changeset
684 ilow = 0;
18eefb9bd5a2 updates to the pygame.math module
lorenz
parents: 1835
diff changeset
685 else if (ilow > self->dim)
18eefb9bd5a2 updates to the pygame.math module
lorenz
parents: 1835
diff changeset
686 ilow = self->dim;
18eefb9bd5a2 updates to the pygame.math module
lorenz
parents: 1835
diff changeset
687 if (ihigh < ilow)
18eefb9bd5a2 updates to the pygame.math module
lorenz
parents: 1835
diff changeset
688 ihigh = ilow;
18eefb9bd5a2 updates to the pygame.math module
lorenz
parents: 1835
diff changeset
689 else if (ihigh > self->dim)
18eefb9bd5a2 updates to the pygame.math module
lorenz
parents: 1835
diff changeset
690 ihigh = self->dim;
18eefb9bd5a2 updates to the pygame.math module
lorenz
parents: 1835
diff changeset
691
18eefb9bd5a2 updates to the pygame.math module
lorenz
parents: 1835
diff changeset
692 len = ihigh - ilow;
18eefb9bd5a2 updates to the pygame.math module
lorenz
parents: 1835
diff changeset
693 slice = (PyListObject *) PyList_New(len);
18eefb9bd5a2 updates to the pygame.math module
lorenz
parents: 1835
diff changeset
694 if (slice == NULL)
18eefb9bd5a2 updates to the pygame.math module
lorenz
parents: 1835
diff changeset
695 return NULL;
18eefb9bd5a2 updates to the pygame.math module
lorenz
parents: 1835
diff changeset
696
18eefb9bd5a2 updates to the pygame.math module
lorenz
parents: 1835
diff changeset
697 src = self->coords + ilow;
18eefb9bd5a2 updates to the pygame.math module
lorenz
parents: 1835
diff changeset
698 dest = slice->ob_item;
18eefb9bd5a2 updates to the pygame.math module
lorenz
parents: 1835
diff changeset
699 for (i = 0; i < len; i++) {
18eefb9bd5a2 updates to the pygame.math module
lorenz
parents: 1835
diff changeset
700 dest[i] = PyFloat_FromDouble(src[i]);
18eefb9bd5a2 updates to the pygame.math module
lorenz
parents: 1835
diff changeset
701 }
18eefb9bd5a2 updates to the pygame.math module
lorenz
parents: 1835
diff changeset
702 return (PyObject *)slice;
18eefb9bd5a2 updates to the pygame.math module
lorenz
parents: 1835
diff changeset
703 }
18eefb9bd5a2 updates to the pygame.math module
lorenz
parents: 1835
diff changeset
704
18eefb9bd5a2 updates to the pygame.math module
lorenz
parents: 1835
diff changeset
705 static int
18eefb9bd5a2 updates to the pygame.math module
lorenz
parents: 1835
diff changeset
706 vector_SetSlice(PyVector *self, Py_ssize_t ilow, Py_ssize_t ihigh, PyObject *v)
18eefb9bd5a2 updates to the pygame.math module
lorenz
parents: 1835
diff changeset
707 {
18eefb9bd5a2 updates to the pygame.math module
lorenz
parents: 1835
diff changeset
708 Py_ssize_t i, len;
18eefb9bd5a2 updates to the pygame.math module
lorenz
parents: 1835
diff changeset
709
18eefb9bd5a2 updates to the pygame.math module
lorenz
parents: 1835
diff changeset
710 if (ilow < 0)
18eefb9bd5a2 updates to the pygame.math module
lorenz
parents: 1835
diff changeset
711 ilow = 0;
18eefb9bd5a2 updates to the pygame.math module
lorenz
parents: 1835
diff changeset
712 else if (ilow > self->dim)
18eefb9bd5a2 updates to the pygame.math module
lorenz
parents: 1835
diff changeset
713 ilow = self->dim;
18eefb9bd5a2 updates to the pygame.math module
lorenz
parents: 1835
diff changeset
714 if (ihigh < ilow)
18eefb9bd5a2 updates to the pygame.math module
lorenz
parents: 1835
diff changeset
715 ihigh = ilow;
18eefb9bd5a2 updates to the pygame.math module
lorenz
parents: 1835
diff changeset
716 else if (ihigh > self->dim)
18eefb9bd5a2 updates to the pygame.math module
lorenz
parents: 1835
diff changeset
717 ihigh = self->dim;
18eefb9bd5a2 updates to the pygame.math module
lorenz
parents: 1835
diff changeset
718
18eefb9bd5a2 updates to the pygame.math module
lorenz
parents: 1835
diff changeset
719 len = ihigh - ilow;
18eefb9bd5a2 updates to the pygame.math module
lorenz
parents: 1835
diff changeset
720 if (len != PySequence_Length(v)) {
18eefb9bd5a2 updates to the pygame.math module
lorenz
parents: 1835
diff changeset
721 PyErr_SetString(PyExc_ValueError,
18eefb9bd5a2 updates to the pygame.math module
lorenz
parents: 1835
diff changeset
722 "Cannot assign slice of different length.");
18eefb9bd5a2 updates to the pygame.math module
lorenz
parents: 1835
diff changeset
723 return -1;
18eefb9bd5a2 updates to the pygame.math module
lorenz
parents: 1835
diff changeset
724 }
18eefb9bd5a2 updates to the pygame.math module
lorenz
parents: 1835
diff changeset
725
18eefb9bd5a2 updates to the pygame.math module
lorenz
parents: 1835
diff changeset
726 /* TODO: better error checking. for example if v is not a
18eefb9bd5a2 updates to the pygame.math module
lorenz
parents: 1835
diff changeset
727 sequence or doesn't numbers. */
18eefb9bd5a2 updates to the pygame.math module
lorenz
parents: 1835
diff changeset
728 for (i = 0; i < len; ++i) {
18eefb9bd5a2 updates to the pygame.math module
lorenz
parents: 1835
diff changeset
729 self->coords[i + ilow] = PySequence_GetItem_AsDouble(v, i);
18eefb9bd5a2 updates to the pygame.math module
lorenz
parents: 1835
diff changeset
730 }
18eefb9bd5a2 updates to the pygame.math module
lorenz
parents: 1835
diff changeset
731 return 0;
18eefb9bd5a2 updates to the pygame.math module
lorenz
parents: 1835
diff changeset
732 }
18eefb9bd5a2 updates to the pygame.math module
lorenz
parents: 1835
diff changeset
733
18eefb9bd5a2 updates to the pygame.math module
lorenz
parents: 1835
diff changeset
734
1845
7d2a16e6a1d3 rearanged math module. added forward declarations at the beginning.
lorenz
parents: 1844
diff changeset
735 static PySequenceMethods vector_as_sequence = {
1834
d31cd03677fb added an incomplete Vector2 to the math module.
lorenz
parents:
diff changeset
736 (lenfunc)vector_len, /* sq_length; __len__ */
d31cd03677fb added an incomplete Vector2 to the math module.
lorenz
parents:
diff changeset
737 (binaryfunc)0, /* sq_concat; __add__ */
d31cd03677fb added an incomplete Vector2 to the math module.
lorenz
parents:
diff changeset
738 (ssizeargfunc)0, /* sq_repeat; __mul__ */
d31cd03677fb added an incomplete Vector2 to the math module.
lorenz
parents:
diff changeset
739 (ssizeargfunc)vector_GetItem, /* sq_item; __getitem__ */
1836
18eefb9bd5a2 updates to the pygame.math module
lorenz
parents: 1835
diff changeset
740 (ssizessizeargfunc)vector_GetSlice, /* sq_slice; __getslice__ */
1834
d31cd03677fb added an incomplete Vector2 to the math module.
lorenz
parents:
diff changeset
741 (ssizeobjargproc)vector_SetItem, /* sq_ass_item; __setitem__ */
1836
18eefb9bd5a2 updates to the pygame.math module
lorenz
parents: 1835
diff changeset
742 (ssizessizeobjargproc)vector_SetSlice, /* sq_ass_slice; __setslice__ */
1834
d31cd03677fb added an incomplete Vector2 to the math module.
lorenz
parents:
diff changeset
743 };
d31cd03677fb added an incomplete Vector2 to the math module.
lorenz
parents:
diff changeset
744
d31cd03677fb added an incomplete Vector2 to the math module.
lorenz
parents:
diff changeset
745 static PyObject*
d31cd03677fb added an incomplete Vector2 to the math module.
lorenz
parents:
diff changeset
746 vector_getx (PyVector *self, void *closure)
d31cd03677fb added an incomplete Vector2 to the math module.
lorenz
parents:
diff changeset
747 {
d31cd03677fb added an incomplete Vector2 to the math module.
lorenz
parents:
diff changeset
748 return PyFloat_FromDouble(self->coords[0]);
d31cd03677fb added an incomplete Vector2 to the math module.
lorenz
parents:
diff changeset
749 }
d31cd03677fb added an incomplete Vector2 to the math module.
lorenz
parents:
diff changeset
750
d31cd03677fb added an incomplete Vector2 to the math module.
lorenz
parents:
diff changeset
751 static int
d31cd03677fb added an incomplete Vector2 to the math module.
lorenz
parents:
diff changeset
752 vector_setx (PyVector *self, PyObject *value, void *closure)
d31cd03677fb added an incomplete Vector2 to the math module.
lorenz
parents:
diff changeset
753 {
d31cd03677fb added an incomplete Vector2 to the math module.
lorenz
parents:
diff changeset
754 if (value == NULL) {
d31cd03677fb added an incomplete Vector2 to the math module.
lorenz
parents:
diff changeset
755 PyErr_SetString(PyExc_TypeError, "Cannot delete the x attribute");
d31cd03677fb added an incomplete Vector2 to the math module.
lorenz
parents:
diff changeset
756 return -1;
d31cd03677fb added an incomplete Vector2 to the math module.
lorenz
parents:
diff changeset
757 }
d31cd03677fb added an incomplete Vector2 to the math module.
lorenz
parents:
diff changeset
758
d31cd03677fb added an incomplete Vector2 to the math module.
lorenz
parents:
diff changeset
759 self->coords[0] = PyFloat_AsDouble(value);
d31cd03677fb added an incomplete Vector2 to the math module.
lorenz
parents:
diff changeset
760 return 0;
d31cd03677fb added an incomplete Vector2 to the math module.
lorenz
parents:
diff changeset
761 }
d31cd03677fb added an incomplete Vector2 to the math module.
lorenz
parents:
diff changeset
762
d31cd03677fb added an incomplete Vector2 to the math module.
lorenz
parents:
diff changeset
763 static PyObject*
d31cd03677fb added an incomplete Vector2 to the math module.
lorenz
parents:
diff changeset
764 vector_gety (PyVector *self, void *closure)
d31cd03677fb added an incomplete Vector2 to the math module.
lorenz
parents:
diff changeset
765 {
d31cd03677fb added an incomplete Vector2 to the math module.
lorenz
parents:
diff changeset
766 return PyFloat_FromDouble(self->coords[1]);
d31cd03677fb added an incomplete Vector2 to the math module.
lorenz
parents:
diff changeset
767 }
d31cd03677fb added an incomplete Vector2 to the math module.
lorenz
parents:
diff changeset
768
d31cd03677fb added an incomplete Vector2 to the math module.
lorenz
parents:
diff changeset
769 static int
d31cd03677fb added an incomplete Vector2 to the math module.
lorenz
parents:
diff changeset
770 vector_sety (PyVector *self, PyObject *value, void *closure)
d31cd03677fb added an incomplete Vector2 to the math module.
lorenz
parents:
diff changeset
771 {
d31cd03677fb added an incomplete Vector2 to the math module.
lorenz
parents:
diff changeset
772 if (value == NULL) {
d31cd03677fb added an incomplete Vector2 to the math module.
lorenz
parents:
diff changeset
773 PyErr_SetString(PyExc_TypeError, "Cannot delete the y attribute");
d31cd03677fb added an incomplete Vector2 to the math module.
lorenz
parents:
diff changeset
774 return -1;
d31cd03677fb added an incomplete Vector2 to the math module.
lorenz
parents:
diff changeset
775 }
d31cd03677fb added an incomplete Vector2 to the math module.
lorenz
parents:
diff changeset
776
d31cd03677fb added an incomplete Vector2 to the math module.
lorenz
parents:
diff changeset
777 self->coords[1] = PyFloat_AsDouble(value);
d31cd03677fb added an incomplete Vector2 to the math module.
lorenz
parents:
diff changeset
778 return 0;
d31cd03677fb added an incomplete Vector2 to the math module.
lorenz
parents:
diff changeset
779 }
d31cd03677fb added an incomplete Vector2 to the math module.
lorenz
parents:
diff changeset
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
d31cd03677fb added an incomplete Vector2 to the math module.
lorenz
parents:
diff changeset
817
d31cd03677fb added an incomplete Vector2 to the math module.
lorenz
parents:
diff changeset
818
d31cd03677fb added an incomplete Vector2 to the math module.
lorenz
parents:
diff changeset
819 static PyObject *
d31cd03677fb added an incomplete Vector2 to the math module.
lorenz
parents:
diff changeset
820 vector_richcompare(PyVector *self, PyObject *other, int op)
d31cd03677fb added an incomplete Vector2 to the math module.
lorenz
parents:
diff changeset
821 {
d31cd03677fb added an incomplete Vector2 to the math module.
lorenz
parents:
diff changeset
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
d31cd03677fb added an incomplete Vector2 to the math module.
lorenz
parents:
diff changeset
825 if (op == Py_EQ)
d31cd03677fb added an incomplete Vector2 to the math module.
lorenz
parents:
diff changeset
826 Py_RETURN_FALSE;
d31cd03677fb added an incomplete Vector2 to the math module.
lorenz
parents:
diff changeset
827 else if (op == Py_NE)
d31cd03677fb added an incomplete Vector2 to the math module.
lorenz
parents:
diff changeset
828 Py_RETURN_TRUE;
d31cd03677fb added an incomplete Vector2 to the math module.
lorenz
parents:
diff changeset
829 else {
d31cd03677fb added an incomplete Vector2 to the math module.
lorenz
parents:
diff changeset
830 Py_INCREF(Py_NotImplemented);
d31cd03677fb added an incomplete Vector2 to the math module.
lorenz
parents:
diff changeset
831 return Py_NotImplemented;
d31cd03677fb added an incomplete Vector2 to the math module.
lorenz
parents:
diff changeset
832 }
d31cd03677fb added an incomplete Vector2 to the math module.
lorenz
parents:
diff changeset
833 }
d31cd03677fb added an incomplete Vector2 to the math module.
lorenz
parents:
diff changeset
834
d31cd03677fb added an incomplete Vector2 to the math module.
lorenz
parents:
diff changeset
835 switch(op) {
d31cd03677fb added an incomplete Vector2 to the math module.
lorenz
parents:
diff changeset
836 case Py_EQ:
d31cd03677fb added an incomplete Vector2 to the math module.
lorenz
parents:
diff changeset
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
d31cd03677fb added an incomplete Vector2 to the math module.
lorenz
parents:
diff changeset
841 Py_RETURN_FALSE;
d31cd03677fb added an incomplete Vector2 to the math module.
lorenz
parents:
diff changeset
842 }
d31cd03677fb added an incomplete Vector2 to the math module.
lorenz
parents:
diff changeset
843 }
d31cd03677fb added an incomplete Vector2 to the math module.
lorenz
parents:
diff changeset
844 Py_RETURN_TRUE;
d31cd03677fb added an incomplete Vector2 to the math module.
lorenz
parents:
diff changeset
845 case Py_NE:
d31cd03677fb added an incomplete Vector2 to the math module.
lorenz
parents:
diff changeset
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
d31cd03677fb added an incomplete Vector2 to the math module.
lorenz
parents:
diff changeset
849 Py_RETURN_TRUE;
d31cd03677fb added an incomplete Vector2 to the math module.
lorenz
parents:
diff changeset
850 }
d31cd03677fb added an incomplete Vector2 to the math module.
lorenz
parents:
diff changeset
851 }
d31cd03677fb added an incomplete Vector2 to the math module.
lorenz
parents:
diff changeset
852 Py_RETURN_FALSE;
d31cd03677fb added an incomplete Vector2 to the math module.
lorenz
parents:
diff changeset
853 default:
d31cd03677fb added an incomplete Vector2 to the math module.
lorenz
parents:
diff changeset
854 Py_INCREF(Py_NotImplemented);
d31cd03677fb added an incomplete Vector2 to the math module.
lorenz
parents:
diff changeset
855 return Py_NotImplemented;
d31cd03677fb added an incomplete Vector2 to the math module.
lorenz
parents:
diff changeset
856 }
d31cd03677fb added an incomplete Vector2 to the math module.
lorenz
parents:
diff changeset
857 }
d31cd03677fb added an incomplete Vector2 to the math module.
lorenz
parents:
diff changeset
858
d31cd03677fb added an incomplete Vector2 to the math module.
lorenz
parents:
diff changeset
859
d31cd03677fb added an incomplete Vector2 to the math module.
lorenz
parents:
diff changeset
860
d31cd03677fb added an incomplete Vector2 to the math module.
lorenz
parents:
diff changeset
861
d31cd03677fb added an incomplete Vector2 to the math module.
lorenz
parents:
diff changeset
862
d31cd03677fb added an incomplete Vector2 to the math module.
lorenz
parents:
diff changeset
863
d31cd03677fb added an incomplete Vector2 to the math module.
lorenz
parents:
diff changeset
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
bbc21cd6faeb some last minute changes before I merge to trunk.
lorenz
parents: 1847
diff changeset
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
bbc21cd6faeb some last minute changes before I merge to trunk.
lorenz
parents: 1847
diff changeset
1121 if (swizzling_enabled &&
bbc21cd6faeb some last minute changes before I merge to trunk.
lorenz
parents: 1847
diff changeset
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
bbc21cd6faeb some last minute changes before I merge to trunk.
lorenz
parents: 1847
diff changeset
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
1e46afbd7b54 implemented new methods in the math module.
lorenz
parents: 1845
diff changeset
1215 /*
1e46afbd7b54 implemented new methods in the math module.
lorenz
parents: 1845
diff changeset
1216 static Py_ssize_t
1e46afbd7b54 implemented new methods in the math module.
lorenz
parents: 1845
diff changeset
1217 vector_readbuffer(PyVector *self, Py_ssize_t segment, void **ptrptr)
1e46afbd7b54 implemented new methods in the math module.
lorenz
parents: 1845
diff changeset
1218 {
1e46afbd7b54 implemented new methods in the math module.
lorenz
parents: 1845
diff changeset
1219 if (segment != 0) {
1e46afbd7b54 implemented new methods in the math module.
lorenz
parents: 1845
diff changeset
1220 PyErr_SetString(PyExc_SystemError,
1e46afbd7b54 implemented new methods in the math module.
lorenz
parents: 1845
diff changeset
1221 "accessing non-existent vector segment");
1e46afbd7b54 implemented new methods in the math module.
lorenz
parents: 1845
diff changeset
1222 return -1;
1e46afbd7b54 implemented new methods in the math module.
lorenz
parents: 1845
diff changeset
1223 }
1e46afbd7b54 implemented new methods in the math module.
lorenz
parents: 1845
diff changeset
1224 *ptrptr = self->coords;
1e46afbd7b54 implemented new methods in the math module.
lorenz
parents: 1845
diff changeset
1225 return self->dim;
1e46afbd7b54 implemented new methods in the math module.
lorenz
parents: 1845
diff changeset
1226 }
1e46afbd7b54 implemented new methods in the math module.
lorenz
parents: 1845
diff changeset
1227
1e46afbd7b54 implemented new methods in the math module.
lorenz
parents: 1845
diff changeset
1228 static Py_ssize_t
1e46afbd7b54 implemented new methods in the math module.
lorenz
parents: 1845
diff changeset
1229 vector_writebuffer(PyVector *self, Py_ssize_t segment, void **ptrptr)
1e46afbd7b54 implemented new methods in the math module.
lorenz
parents: 1845
diff changeset
1230 {
1e46afbd7b54 implemented new methods in the math module.
lorenz
parents: 1845
diff changeset
1231 if (segment != 0) {
1e46afbd7b54 implemented new methods in the math module.
lorenz
parents: 1845
diff changeset
1232 PyErr_SetString(PyExc_SystemError,
1e46afbd7b54 implemented new methods in the math module.
lorenz
parents: 1845
diff changeset
1233 "accessing non-existent vector segment");
1e46afbd7b54 implemented new methods in the math module.
lorenz
parents: 1845
diff changeset
1234 return -1;
1e46afbd7b54 implemented new methods in the math module.
lorenz
parents: 1845
diff changeset
1235 }
1e46afbd7b54 implemented new methods in the math module.
lorenz
parents: 1845
diff changeset
1236 *ptrptr = self->coords;
1e46afbd7b54 implemented new methods in the math module.
lorenz
parents: 1845
diff changeset
1237 return self->dim;
1e46afbd7b54 implemented new methods in the math module.
lorenz
parents: 1845
diff changeset
1238 }
1e46afbd7b54 implemented new methods in the math module.
lorenz
parents: 1845
diff changeset
1239
1e46afbd7b54 implemented new methods in the math module.
lorenz
parents: 1845
diff changeset
1240 static Py_ssize_t
1e46afbd7b54 implemented new methods in the math module.
lorenz
parents: 1845
diff changeset
1241 vector_segcount(PyVector *self, Py_ssize_t *lenp)
1e46afbd7b54 implemented new methods in the math module.
lorenz
parents: 1845
diff changeset
1242 {
1e46afbd7b54 implemented new methods in the math module.
lorenz
parents: 1845
diff changeset
1243 if (lenp) {
1e46afbd7b54 implemented new methods in the math module.
lorenz
parents: 1845
diff changeset
1244 *lenp = self->dim * sizeof(self->coords[0]);
1e46afbd7b54 implemented new methods in the math module.
lorenz
parents: 1845
diff changeset
1245 }
1e46afbd7b54 implemented new methods in the math module.
lorenz
parents: 1845
diff changeset
1246 return 1;
1e46afbd7b54 implemented new methods in the math module.
lorenz
parents: 1845
diff changeset
1247 }
1e46afbd7b54 implemented new methods in the math module.
lorenz
parents: 1845
diff changeset
1248
1e46afbd7b54 implemented new methods in the math module.
lorenz
parents: 1845
diff changeset
1249 static int
1e46afbd7b54 implemented new methods in the math module.
lorenz
parents: 1845
diff changeset
1250 vector_getbuffer(PyVector *self, Py_buffer *view, int flags)
1e46afbd7b54 implemented new methods in the math module.
lorenz
parents: 1845
diff changeset
1251 {
1e46afbd7b54 implemented new methods in the math module.
lorenz
parents: 1845
diff changeset
1252 int ret;
1e46afbd7b54 implemented new methods in the math module.
lorenz
parents: 1845
diff changeset
1253 void *ptr;
1e46afbd7b54 implemented new methods in the math module.
lorenz
parents: 1845
diff changeset
1254 if (view == NULL) {
1e46afbd7b54 implemented new methods in the math module.
lorenz
parents: 1845
diff changeset
1255 self->ob_exports++;
1e46afbd7b54 implemented new methods in the math module.
lorenz
parents: 1845
diff changeset
1256 return 0;
1e46afbd7b54 implemented new methods in the math module.
lorenz
parents: 1845
diff changeset
1257 }
1e46afbd7b54 implemented new methods in the math module.
lorenz
parents: 1845
diff changeset
1258 ptr = self->coords;
1e46afbd7b54 implemented new methods in the math module.
lorenz
parents: 1845
diff changeset
1259 ret = PyBuffer_FillInfo(view, (PyObject*)self, ptr, Py_SIZE(self), 0, flags);
1e46afbd7b54 implemented new methods in the math module.
lorenz
parents: 1845
diff changeset
1260 if (ret >= 0) {
1e46afbd7b54 implemented new methods in the math module.
lorenz
parents: 1845
diff changeset
1261 obj->ob_exports++;
1e46afbd7b54 implemented new methods in the math module.
lorenz
parents: 1845
diff changeset
1262 }
1e46afbd7b54 implemented new methods in the math module.
lorenz
parents: 1845
diff changeset
1263 return ret;
1e46afbd7b54 implemented new methods in the math module.
lorenz
parents: 1845
diff changeset
1264 }
1e46afbd7b54 implemented new methods in the math module.
lorenz
parents: 1845
diff changeset
1265
1e46afbd7b54 implemented new methods in the math module.
lorenz
parents: 1845
diff changeset
1266 static void
1e46afbd7b54 implemented new methods in the math module.
lorenz
parents: 1845
diff changeset
1267 vector_releasebuffer(PyVector *self, Py_buffer *view)
1e46afbd7b54 implemented new methods in the math module.
lorenz
parents: 1845
diff changeset
1268 {
1e46afbd7b54 implemented new methods in the math module.
lorenz
parents: 1845
diff changeset
1269 self->ob_exports--;
1e46afbd7b54 implemented new methods in the math module.
lorenz
parents: 1845
diff changeset
1270 }
1e46afbd7b54 implemented new methods in the math module.
lorenz
parents: 1845
diff changeset
1271
1e46afbd7b54 implemented new methods in the math module.
lorenz
parents: 1845
diff changeset
1272
1e46afbd7b54 implemented new methods in the math module.
lorenz
parents: 1845
diff changeset
1273 static PyBufferProcs vector_as_buffer = {
1e46afbd7b54 implemented new methods in the math module.
lorenz
parents: 1845
diff changeset
1274 (readbufferproc)vector_readbuffer,
1e46afbd7b54 implemented new methods in the math module.
lorenz
parents: 1845
diff changeset
1275 (writebufferproc)vector_writebuffer,
1e46afbd7b54 implemented new methods in the math module.
lorenz
parents: 1845
diff changeset
1276 (segcountproc)vector_segcount,
1e46afbd7b54 implemented new methods in the math module.
lorenz
parents: 1845
diff changeset
1277 (charbufferproc)0,
1e46afbd7b54 implemented new methods in the math module.
lorenz
parents: 1845
diff changeset
1278 (getbufferproc)vector_getbuffer,
1e46afbd7b54 implemented new methods in the math module.
lorenz
parents: 1845
diff changeset
1279 (releasebufferproc)vector_releasebuffer,
1e46afbd7b54 implemented new methods in the math module.
lorenz
parents: 1845
diff changeset
1280 };
1e46afbd7b54 implemented new methods in the math module.
lorenz
parents: 1845
diff changeset
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
d31cd03677fb added an incomplete Vector2 to the math module.
lorenz
parents:
diff changeset
1288 static PyObject *
d31cd03677fb added an incomplete Vector2 to the math module.
lorenz
parents:
diff changeset
1289 vector2_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
d31cd03677fb added an incomplete Vector2 to the math module.
lorenz
parents:
diff changeset
1290 {
d31cd03677fb added an incomplete Vector2 to the math module.
lorenz
parents:
diff changeset
1291 PyVector *vec = (PyVector *)type->tp_alloc(type, 0);
d31cd03677fb added an incomplete Vector2 to the math module.
lorenz
parents:
diff changeset
1292
d31cd03677fb added an incomplete Vector2 to the math module.
lorenz
parents:
diff changeset
1293 if (vec != NULL) {
d31cd03677fb added an incomplete Vector2 to the math module.
lorenz
parents:
diff changeset
1294 vec->dim = 2;
d31cd03677fb added an incomplete Vector2 to the math module.
lorenz
parents:
diff changeset
1295 vec->epsilon = FLT_EPSILON;
d31cd03677fb added an incomplete Vector2 to the math module.
lorenz
parents:
diff changeset
1296 vec->coords = PyMem_New(double, vec->dim);
d31cd03677fb added an incomplete Vector2 to the math module.
lorenz
parents:
diff changeset
1297 if (vec->coords == NULL) {
d31cd03677fb added an incomplete Vector2 to the math module.
lorenz
parents:
diff changeset
1298 vec->ob_type->tp_free((PyObject*)vec);
d31cd03677fb added an incomplete Vector2 to the math module.
lorenz
parents:
diff changeset
1299 return NULL;
d31cd03677fb added an incomplete Vector2 to the math module.
lorenz
parents:
diff changeset
1300 }
d31cd03677fb added an incomplete Vector2 to the math module.
lorenz
parents:
diff changeset
1301 }
d31cd03677fb added an incomplete Vector2 to the math module.
lorenz
parents:
diff changeset
1302
d31cd03677fb added an incomplete Vector2 to the math module.
lorenz
parents:
diff changeset
1303 return (PyObject *)vec;
d31cd03677fb added an incomplete Vector2 to the math module.
lorenz
parents:
diff changeset
1304 }
d31cd03677fb added an incomplete Vector2 to the math module.
lorenz
parents:
diff changeset
1305
d31cd03677fb added an incomplete Vector2 to the math module.
lorenz
parents:
diff changeset
1306 static int
d31cd03677fb added an incomplete Vector2 to the math module.
lorenz
parents:
diff changeset
1307 vector2_init(PyVector *self, PyObject *args, PyObject *kwds)
d31cd03677fb added an incomplete Vector2 to the math module.
lorenz
parents:
diff changeset
1308 {
d31cd03677fb added an incomplete Vector2 to the math module.
lorenz
parents:
diff changeset
1309 PyObject *xOrSequence=NULL, *y=NULL, *z=NULL;
d31cd03677fb added an incomplete Vector2 to the math module.
lorenz
parents:
diff changeset
1310 static char *kwlist[] = {"x", "y", NULL};
d31cd03677fb added an incomplete Vector2 to the math module.
lorenz
parents:
diff changeset
1311
1836
18eefb9bd5a2 updates to the pygame.math module
lorenz
parents: 1835
diff changeset
1312 if (! PyArg_ParseTupleAndKeywords(args, kwds, "|OO:Vector2", kwlist,
1834
d31cd03677fb added an incomplete Vector2 to the math module.
lorenz
parents:
diff changeset
1313 &xOrSequence, &y))
d31cd03677fb added an incomplete Vector2 to the math module.
lorenz
parents:
diff changeset
1314 return -1;
d31cd03677fb added an incomplete Vector2 to the math module.
lorenz
parents:
diff changeset
1315
d31cd03677fb added an incomplete Vector2 to the math module.
lorenz
parents:
diff changeset
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
d31cd03677fb added an incomplete Vector2 to the math module.
lorenz
parents:
diff changeset
1318 self->coords[0] = PyFloat_AsDouble(xOrSequence);
d31cd03677fb added an incomplete Vector2 to the math module.
lorenz
parents:
diff changeset
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
d31cd03677fb added an incomplete Vector2 to the math module.
lorenz
parents:
diff changeset
1321 self->coords[0] = PySequence_GetItem_AsDouble(xOrSequence, 0);
d31cd03677fb added an incomplete Vector2 to the math module.
lorenz
parents:
diff changeset
1322 self->coords[1] = PySequence_GetItem_AsDouble(xOrSequence, 1);
1836
18eefb9bd5a2 updates to the pygame.math module
lorenz
parents: 1835
diff changeset
1323 /* successful initialization from sequence type */
1834
d31cd03677fb added an incomplete Vector2 to the math module.
lorenz
parents:
diff changeset
1324 return 0;
d31cd03677fb added an incomplete Vector2 to the math module.
lorenz
parents:
diff changeset
1325 }
1836
18eefb9bd5a2 updates to the pygame.math module
lorenz
parents: 1835
diff changeset
1326 else if (PyString_Check(xOrSequence)) {
18eefb9bd5a2 updates to the pygame.math module
lorenz
parents: 1835
diff changeset
1327 /* This should make "Vector2(Vector2().__repr__())" possible */
18eefb9bd5a2 updates to the pygame.math module
lorenz
parents: 1835
diff changeset
1328 char buffer[STRING_BUF_SIZE];
18eefb9bd5a2 updates to the pygame.math module
lorenz
parents: 1835
diff changeset
1329 char *endptr;
18eefb9bd5a2 updates to the pygame.math module
lorenz
parents: 1835
diff changeset
1330 char *str = PyString_AsString(xOrSequence);
18eefb9bd5a2 updates to the pygame.math module
lorenz
parents: 1835
diff changeset
1331 if (strncmp(str, "<Vector2(", strlen("<Vector2(")) != 0)
18eefb9bd5a2 updates to the pygame.math module
lorenz
parents: 1835
diff changeset
1332 goto error;
18eefb9bd5a2 updates to the pygame.math module
lorenz
parents: 1835
diff changeset
1333 str += strlen("<Vector2(");
18eefb9bd5a2 updates to the pygame.math module
lorenz
parents: 1835
diff changeset
1334 self->coords[0] = PyOS_ascii_strtod(str, &endptr);
18eefb9bd5a2 updates to the pygame.math module
lorenz
parents: 1835
diff changeset
1335 if (endptr == str) {
18eefb9bd5a2 updates to the pygame.math module
lorenz
parents: 1835
diff changeset
1336 goto error;
18eefb9bd5a2 updates to the pygame.math module
lorenz
parents: 1835
diff changeset
1337 }
18eefb9bd5a2 updates to the pygame.math module
lorenz
parents: 1835
diff changeset
1338 str = endptr + strlen(", ");
18eefb9bd5a2 updates to the pygame.math module
lorenz
parents: 1835
diff changeset
1339 self->coords[1] = PyOS_ascii_strtod(str, &endptr);
18eefb9bd5a2 updates to the pygame.math module
lorenz
parents: 1835
diff changeset
1340 if (endptr == str)
18eefb9bd5a2 updates to the pygame.math module
lorenz
parents: 1835
diff changeset
1341 goto error;
18eefb9bd5a2 updates to the pygame.math module
lorenz
parents: 1835
diff changeset
1342 /* successful conversion from string */
18eefb9bd5a2 updates to the pygame.math module
lorenz
parents: 1835
diff changeset
1343 return 0;
18eefb9bd5a2 updates to the pygame.math module
lorenz
parents: 1835
diff changeset
1344 }
1834
d31cd03677fb added an incomplete Vector2 to the math module.
lorenz
parents:
diff changeset
1345 else {
1836
18eefb9bd5a2 updates to the pygame.math module
lorenz
parents: 1835
diff changeset
1346 goto error;
1834
d31cd03677fb added an incomplete Vector2 to the math module.
lorenz
parents:
diff changeset
1347 }
d31cd03677fb added an incomplete Vector2 to the math module.
lorenz
parents:
diff changeset
1348 }
d31cd03677fb added an incomplete Vector2 to the math module.
lorenz
parents:
diff changeset
1349 else {
d31cd03677fb added an incomplete Vector2 to the math module.
lorenz
parents:
diff changeset
1350 self->coords[0] = 0.;
d31cd03677fb added an incomplete Vector2 to the math module.
lorenz
parents:
diff changeset
1351 }
d31cd03677fb added an incomplete Vector2 to the math module.
lorenz
parents:
diff changeset
1352
d31cd03677fb added an incomplete Vector2 to the math module.
lorenz
parents:
diff changeset
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
d31cd03677fb added an incomplete Vector2 to the math module.
lorenz
parents:
diff changeset
1355 self->coords[1] = PyFloat_AsDouble(y);
d31cd03677fb added an incomplete Vector2 to the math module.
lorenz
parents:
diff changeset
1356 }
d31cd03677fb added an incomplete Vector2 to the math module.
lorenz
parents:
diff changeset
1357 else {
1836
18eefb9bd5a2 updates to the pygame.math module
lorenz
parents: 1835
diff changeset
1358 goto error;
1834
d31cd03677fb added an incomplete Vector2 to the math module.
lorenz
parents:
diff changeset
1359 }
d31cd03677fb added an incomplete Vector2 to the math module.
lorenz
parents:
diff changeset
1360 }
d31cd03677fb added an incomplete Vector2 to the math module.
lorenz
parents:
diff changeset
1361 else {
d31cd03677fb added an incomplete Vector2 to the math module.
lorenz
parents:
diff changeset
1362 self->coords[1] = 0.;
d31cd03677fb added an incomplete Vector2 to the math module.
lorenz
parents:
diff changeset
1363 }
1836
18eefb9bd5a2 updates to the pygame.math module
lorenz
parents: 1835
diff changeset
1364 /* success initialization */
18eefb9bd5a2 updates to the pygame.math module
lorenz
parents: 1835
diff changeset
1365 return 0;
18eefb9bd5a2 updates to the pygame.math module
lorenz
parents: 1835
diff changeset
1366 error:
18eefb9bd5a2 updates to the pygame.math module
lorenz
parents: 1835
diff changeset
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
18eefb9bd5a2 updates to the pygame.math module
lorenz
parents: 1835
diff changeset
1369 return -1;
18eefb9bd5a2 updates to the pygame.math module
lorenz
parents: 1835
diff changeset
1370 }
1834
d31cd03677fb added an incomplete Vector2 to the math module.
lorenz
parents:
diff changeset
1371
1836
18eefb9bd5a2 updates to the pygame.math module
lorenz
parents: 1835
diff changeset
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
18eefb9bd5a2 updates to the pygame.math module
lorenz
parents: 1835
diff changeset
1375 {
18eefb9bd5a2 updates to the pygame.math module
lorenz
parents: 1835
diff changeset
1376 /* make sure angle is in range [0, 360) */
18eefb9bd5a2 updates to the pygame.math module
lorenz
parents: 1835
diff changeset
1377 angle = fmod(angle, 360.);
18eefb9bd5a2 updates to the pygame.math module
lorenz
parents: 1835
diff changeset
1378 if (angle < 0)
18eefb9bd5a2 updates to the pygame.math module
lorenz
parents: 1835
diff changeset
1379 angle += 360.;
18eefb9bd5a2 updates to the pygame.math module
lorenz
parents: 1835
diff changeset
1380
18eefb9bd5a2 updates to the pygame.math module
lorenz
parents: 1835
diff changeset
1381 /* special-case rotation by 0, 90, 180 and 270 degrees */
1848
bbc21cd6faeb some last minute changes before I merge to trunk.
lorenz
parents: 1847
diff changeset
1382 if (fmod(angle + epsilon, 90.) < 2 * epsilon) {
bbc21cd6faeb some last minute changes before I merge to trunk.
lorenz
parents: 1847
diff changeset
1383 switch ((int)((angle + epsilon) / 90)) {
bbc21cd6faeb some last minute changes before I merge to trunk.
lorenz
parents: 1847
diff changeset
1384 case 0: /* 0 degrees */
bbc21cd6faeb some last minute changes before I merge to trunk.
lorenz
parents: 1847
diff changeset
1385 dst_coords[0] = src_coords[0];
bbc21cd6faeb some last minute changes before I merge to trunk.
lorenz
parents: 1847
diff changeset
1386 dst_coords[1] = src_coords[1];
bbc21cd6faeb some last minute changes before I merge to trunk.
lorenz
parents: 1847
diff changeset
1387 break;
bbc21cd6faeb some last minute changes before I merge to trunk.
lorenz
parents: 1847
diff changeset
1388 case 1: /* 90 degrees */
bbc21cd6faeb some last minute changes before I merge to trunk.
lorenz
parents: 1847
diff changeset
1389 dst_coords[0] = -src_coords[1];
bbc21cd6faeb some last minute changes before I merge to trunk.
lorenz
parents: 1847
diff changeset
1390 dst_coords[1] = src_coords[0];
bbc21cd6faeb some last minute changes before I merge to trunk.
lorenz
parents: 1847
diff changeset
1391 break;
bbc21cd6faeb some last minute changes before I merge to trunk.
lorenz
parents: 1847
diff changeset
1392 case 2: /* 180 degrees */
bbc21cd6faeb some last minute changes before I merge to trunk.
lorenz
parents: 1847
diff changeset
1393 dst_coords[0] = -src_coords[0];
bbc21cd6faeb some last minute changes before I merge to trunk.
lorenz
parents: 1847
diff changeset
1394 dst_coords[1] = -src_coords[1];
bbc21cd6faeb some last minute changes before I merge to trunk.
lorenz
parents: 1847
diff changeset
1395 break;
bbc21cd6faeb some last minute changes before I merge to trunk.
lorenz
parents: 1847
diff changeset
1396 case 3: /* 270 degrees */
bbc21cd6faeb some last minute changes before I merge to trunk.
lorenz
parents: 1847
diff changeset
1397 dst_coords[0] = src_coords[1];
bbc21cd6faeb some last minute changes before I merge to trunk.
lorenz
parents: 1847
diff changeset
1398 dst_coords[1] = -src_coords[0];
bbc21cd6faeb some last minute changes before I merge to trunk.
lorenz
parents: 1847
diff changeset
1399 break;
bbc21cd6faeb some last minute changes before I merge to trunk.
lorenz
parents: 1847
diff changeset
1400 default:
bbc21cd6faeb some last minute changes before I merge to trunk.
lorenz
parents: 1847
diff changeset
1401 /* this should NEVER happen and means a bug in the code */
bbc21cd6faeb some last minute changes before I merge to trunk.
lorenz
parents: 1847
diff changeset
1402 PyErr_SetString(PyExc_RuntimeError, "Please report this bug in vector2_do_rotate to the developers");
bbc21cd6faeb some last minute changes before I merge to trunk.
lorenz
parents: 1847
diff changeset
1403 break;
bbc21cd6faeb some last minute changes before I merge to trunk.
lorenz
parents: 1847
diff changeset
1404 }
1836
18eefb9bd5a2 updates to the pygame.math module
lorenz
parents: 1835
diff changeset
1405 }
18eefb9bd5a2 updates to the pygame.math module
lorenz
parents: 1835
diff changeset
1406 else {
18eefb9bd5a2 updates to the pygame.math module
lorenz
parents: 1835
diff changeset
1407 double sinValue, cosValue;
18eefb9bd5a2 updates to the pygame.math module
lorenz
parents: 1835
diff changeset
1408
18eefb9bd5a2 updates to the pygame.math module
lorenz
parents: 1835
diff changeset
1409 angle = DEG2RAD(angle);
18eefb9bd5a2 updates to the pygame.math module
lorenz
parents: 1835
diff changeset
1410 sinValue = sin(angle);
18eefb9bd5a2 updates to the pygame.math module
lorenz
parents: 1835
diff changeset
1411 cosValue = cos(angle);
18eefb9bd5a2 updates to the pygame.math module
lorenz
parents: 1835
diff changeset
1412
18eefb9bd5a2 updates to the pygame.math module
lorenz
parents: 1835
diff changeset
1413 dst_coords[0] = cosValue * src_coords[0] - sinValue * src_coords[1];
18eefb9bd5a2 updates to the pygame.math module
lorenz
parents: 1835
diff changeset
1414 dst_coords[1] = sinValue * src_coords[0] + cosValue * src_coords[1];
18eefb9bd5a2 updates to the pygame.math module
lorenz
parents: 1835
diff changeset
1415 }
18eefb9bd5a2 updates to the pygame.math module
lorenz
parents: 1835
diff changeset
1416 }
18eefb9bd5a2 updates to the pygame.math module
lorenz
parents: 1835
diff changeset
1417
18eefb9bd5a2 updates to the pygame.math module
lorenz
parents: 1835
diff changeset
1418 static PyObject *
18eefb9bd5a2 updates to the pygame.math module
lorenz
parents: 1835
diff changeset
1419 vector2_rotate(PyVector *self, PyObject *args)
18eefb9bd5a2 updates to the pygame.math module
lorenz
parents: 1835
diff changeset
1420 {
18eefb9bd5a2 updates to the pygame.math module
lorenz
parents: 1835
diff changeset
1421 double angle;
18eefb9bd5a2 updates to the pygame.math module
lorenz
parents: 1835
diff changeset
1422 PyVector *ret;
18eefb9bd5a2 updates to the pygame.math module
lorenz
parents: 1835
diff changeset
1423
18eefb9bd5a2 updates to the pygame.math module
lorenz
parents: 1835
diff changeset
1424 if (!PyArg_ParseTuple(args, "d:rotate", &angle)) {
18eefb9bd5a2 updates to the pygame.math module
lorenz
parents: 1835
diff changeset
1425 return NULL;
18eefb9bd5a2 updates to the pygame.math module
lorenz
parents: 1835
diff changeset
1426 }
18eefb9bd5a2 updates to the pygame.math module
lorenz
parents: 1835
diff changeset
1427
18eefb9bd5a2 updates to the pygame.math module
lorenz
parents: 1835
diff changeset
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
18eefb9bd5a2 updates to the pygame.math module
lorenz
parents: 1835
diff changeset
1430 return (PyObject*)ret;
18eefb9bd5a2 updates to the pygame.math module
lorenz
parents: 1835
diff changeset
1431 }
18eefb9bd5a2 updates to the pygame.math module
lorenz
parents: 1835
diff changeset
1432
18eefb9bd5a2 updates to the pygame.math module
lorenz
parents: 1835
diff changeset
1433 static PyObject *
18eefb9bd5a2 updates to the pygame.math module
lorenz
parents: 1835
diff changeset
1434 vector2_rotate_ip(PyVector *self, PyObject *args)
18eefb9bd5a2 updates to the pygame.math module
lorenz
parents: 1835
diff changeset
1435 {
18eefb9bd5a2 updates to the pygame.math module
lorenz
parents: 1835
diff changeset
1436 double angle;
18eefb9bd5a2 updates to the pygame.math module
lorenz
parents: 1835
diff changeset
1437 double tmp[2];
18eefb9bd5a2 updates to the pygame.math module
lorenz
parents: 1835
diff changeset
1438
18eefb9bd5a2 updates to the pygame.math module
lorenz
parents: 1835
diff changeset
1439 if (!PyArg_ParseTuple(args, "d:rotate_ip", &angle)) {
18eefb9bd5a2 updates to the pygame.math module
lorenz
parents: 1835
diff changeset
1440 return NULL;
18eefb9bd5a2 updates to the pygame.math module
lorenz
parents: 1835
diff changeset
1441 }
18eefb9bd5a2 updates to the pygame.math module
lorenz
parents: 1835
diff changeset
1442
18eefb9bd5a2 updates to the pygame.math module
lorenz
parents: 1835
diff changeset
1443 tmp[0] = self->coords[0];
18eefb9bd5a2 updates to the pygame.math module
lorenz
parents: 1835
diff changeset
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
18eefb9bd5a2 updates to the pygame.math module
lorenz
parents: 1835
diff changeset
1446 Py_RETURN_NONE;
18eefb9bd5a2 updates to the pygame.math module
lorenz
parents: 1835
diff changeset
1447 }
18eefb9bd5a2 updates to the pygame.math module
lorenz
parents: 1835
diff changeset
1448
18eefb9bd5a2 updates to the pygame.math module
lorenz
parents: 1835
diff changeset
1449 static PyObject *
18eefb9bd5a2 updates to the pygame.math module
lorenz
parents: 1835
diff changeset
1450 vector2_cross(PyVector *self, PyObject *other)
18eefb9bd5a2 updates to the pygame.math module
lorenz
parents: 1835
diff changeset
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
18eefb9bd5a2 updates to the pygame.math module
lorenz
parents: 1835
diff changeset
1453 PyErr_SetString(PyExc_TypeError, "cannot calculate cross Product");
18eefb9bd5a2 updates to the pygame.math module
lorenz
parents: 1835
diff changeset
1454 return NULL;
18eefb9bd5a2 updates to the pygame.math module
lorenz
parents: 1835
diff changeset
1455 }
18eefb9bd5a2 updates to the pygame.math module
lorenz
parents: 1835
diff changeset
1456
18eefb9bd5a2 updates to the pygame.math module
lorenz
parents: 1835
diff changeset
1457 if (PyVector_Check(other)) {
18eefb9bd5a2 updates to the pygame.math module
lorenz
parents: 1835
diff changeset
1458 return PyFloat_FromDouble((self->coords[0] * ((PyVector *)other)->coords[1]) -
18eefb9bd5a2 updates to the pygame.math module
lorenz
parents: 1835
diff changeset
1459 (self->coords[1] * ((PyVector *)other)->coords[0]));
18eefb9bd5a2 updates to the pygame.math module
lorenz
parents: 1835
diff changeset
1460 }
18eefb9bd5a2 updates to the pygame.math module
lorenz
parents: 1835
diff changeset
1461 else {
18eefb9bd5a2 updates to the pygame.math module
lorenz
parents: 1835
diff changeset
1462 return PyFloat_FromDouble((self->coords[0] * PySequence_GetItem_AsDouble(other, 1)) -
18eefb9bd5a2 updates to the pygame.math module
lorenz
parents: 1835
diff changeset
1463 (self->coords[1] * PySequence_GetItem_AsDouble(other, 0)));
18eefb9bd5a2 updates to the pygame.math module
lorenz
parents: 1835
diff changeset
1464 }
18eefb9bd5a2 updates to the pygame.math module
lorenz
parents: 1835
diff changeset
1465 }
18eefb9bd5a2 updates to the pygame.math module
lorenz
parents: 1835
diff changeset
1466
18eefb9bd5a2 updates to the pygame.math module
lorenz
parents: 1835
diff changeset
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
18eefb9bd5a2 updates to the pygame.math module
lorenz
parents: 1835
diff changeset
1469 {
18eefb9bd5a2 updates to the pygame.math module
lorenz
parents: 1835
diff changeset
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
18eefb9bd5a2 updates to the pygame.math module
lorenz
parents: 1835
diff changeset
1472 PyErr_SetString(PyExc_TypeError, "expected an vector.");
18eefb9bd5a2 updates to the pygame.math module
lorenz
parents: 1835
diff changeset
1473 return NULL;
18eefb9bd5a2 updates to the pygame.math module
lorenz
parents: 1835
diff changeset
1474 }
18eefb9bd5a2 updates to the pygame.math module
lorenz
parents: 1835
diff changeset
1475
18eefb9bd5a2 updates to the pygame.math module
lorenz
parents: 1835
diff changeset
1476 angle = (atan2(PySequence_GetItem_AsDouble(other, 1),
18eefb9bd5a2 updates to the pygame.math module
lorenz
parents: 1835
diff changeset
1477 PySequence_GetItem_AsDouble(other, 0)) -
18eefb9bd5a2 updates to the pygame.math module
lorenz
parents: 1835
diff changeset
1478 atan2(self->coords[1], self->coords[0]));
18eefb9bd5a2 updates to the pygame.math module
lorenz
parents: 1835
diff changeset
1479 return PyFloat_FromDouble(RAD2DEG(angle));
18eefb9bd5a2 updates to the pygame.math module
lorenz
parents: 1835
diff changeset
1480 }
18eefb9bd5a2 updates to the pygame.math module
lorenz
parents: 1835
diff changeset
1481
1846
1e46afbd7b54 implemented new methods in the math module.
lorenz
parents: 1845
diff changeset
1482 static PyObject *
1e46afbd7b54 implemented new methods in the math module.
lorenz
parents: 1845
diff changeset
1483 vector2_as_polar(PyVector *self)
1e46afbd7b54 implemented new methods in the math module.
lorenz
parents: 1845
diff changeset
1484 {
1e46afbd7b54 implemented new methods in the math module.
lorenz
parents: 1845
diff changeset
1485 double r, phi;
1e46afbd7b54 implemented new methods in the math module.
lorenz
parents: 1845
diff changeset
1486 r = sqrt(_scalar_product(self->coords, self->coords, self->dim));
1e46afbd7b54 implemented new methods in the math module.
lorenz
parents: 1845
diff changeset
1487 phi = atan2(self->coords[1], self->coords[0]);
1e46afbd7b54 implemented new methods in the math module.
lorenz
parents: 1845
diff changeset
1488 return Py_BuildValue("(dd)", r, phi);
1e46afbd7b54 implemented new methods in the math module.
lorenz
parents: 1845
diff changeset
1489 }
1e46afbd7b54 implemented new methods in the math module.
lorenz
parents: 1845
diff changeset
1490
1e46afbd7b54 implemented new methods in the math module.
lorenz
parents: 1845
diff changeset
1491 static PyObject *
1e46afbd7b54 implemented new methods in the math module.
lorenz
parents: 1845
diff changeset
1492 vector2_from_polar(PyVector *self, PyObject *args)
1e46afbd7b54 implemented new methods in the math module.
lorenz
parents: 1845
diff changeset
1493 {
1e46afbd7b54 implemented new methods in the math module.
lorenz
parents: 1845
diff changeset
1494 PyObject *polar_obj, *r_obj, *phi_obj;
1e46afbd7b54 implemented new methods in the math module.
lorenz
parents: 1845
diff changeset
1495 double r, phi;
1e46afbd7b54 implemented new methods in the math module.
lorenz
parents: 1845
diff changeset
1496 if (!PyArg_ParseTuple(args, "O:Vector2.from_polar", &polar_obj)) {
1e46afbd7b54 implemented new methods in the math module.
lorenz
parents: 1845
diff changeset
1497 return NULL;
1e46afbd7b54 implemented new methods in the math module.
lorenz
parents: 1845
diff changeset
1498 }
1e46afbd7b54 implemented new methods in the math module.
lorenz
parents: 1845
diff changeset
1499 if (!PySequence_Check(polar_obj) || PySequence_Length(polar_obj) != 2) {
1e46afbd7b54 implemented new methods in the math module.
lorenz
parents: 1845
diff changeset
1500 PyErr_SetString(PyExc_TypeError,
1e46afbd7b54 implemented new methods in the math module.
lorenz
parents: 1845
diff changeset
1501 "2-tuple containing r and phi is expected.");
1e46afbd7b54 implemented new methods in the math module.
lorenz
parents: 1845
diff changeset
1502 return NULL;
1e46afbd7b54 implemented new methods in the math module.
lorenz
parents: 1845
diff changeset
1503 }
1e46afbd7b54 implemented new methods in the math module.
lorenz
parents: 1845
diff changeset
1504 r_obj = PySequence_GetItem(polar_obj, 0);
1e46afbd7b54 implemented new methods in the math module.
lorenz
parents: 1845
diff changeset
1505 phi_obj = PySequence_GetItem(polar_obj, 1);
1e46afbd7b54 implemented new methods in the math module.
lorenz
parents: 1845
diff changeset
1506 if (!PyNumber_Check(r_obj) || !PyNumber_Check(phi_obj)) {
1e46afbd7b54 implemented new methods in the math module.
lorenz
parents: 1845
diff changeset
1507 PyErr_SetString(PyExc_TypeError,
1e46afbd7b54 implemented new methods in the math module.
lorenz
parents: 1845
diff changeset
1508 "expected numerical values");
1e46afbd7b54 implemented new methods in the math module.
lorenz
parents: 1845
diff changeset
1509 Py_DECREF(r_obj);
1e46afbd7b54 implemented new methods in the math module.
lorenz
parents: 1845
diff changeset
1510 Py_DECREF(phi_obj);
1e46afbd7b54 implemented new methods in the math module.
lorenz
parents: 1845
diff changeset
1511 return NULL;
1e46afbd7b54 implemented new methods in the math module.
lorenz
parents: 1845
diff changeset
1512 }
1e46afbd7b54 implemented new methods in the math module.
lorenz
parents: 1845
diff changeset
1513 r = PyFloat_AsDouble(r_obj);
1e46afbd7b54 implemented new methods in the math module.
lorenz
parents: 1845
diff changeset
1514 phi = PyFloat_AsDouble(phi_obj);
1e46afbd7b54 implemented new methods in the math module.
lorenz
parents: 1845
diff changeset
1515 Py_DECREF(r_obj);
1e46afbd7b54 implemented new methods in the math module.
lorenz
parents: 1845
diff changeset
1516 Py_DECREF(phi_obj);
1e46afbd7b54 implemented new methods in the math module.
lorenz
parents: 1845
diff changeset
1517 self->coords[0] = r * cos(phi);
1e46afbd7b54 implemented new methods in the math module.
lorenz
parents: 1845
diff changeset
1518 self->coords[1] = r * sin(phi);
1e46afbd7b54 implemented new methods in the math module.
lorenz
parents: 1845
diff changeset
1519
1e46afbd7b54 implemented new methods in the math module.
lorenz
parents: 1845
diff changeset
1520 Py_RETURN_NONE;
1e46afbd7b54 implemented new methods in the math module.
lorenz
parents: 1845
diff changeset
1521 }
1e46afbd7b54 implemented new methods in the math module.
lorenz
parents: 1845
diff changeset
1522
1e46afbd7b54 implemented new methods in the math module.
lorenz
parents: 1845
diff changeset
1523
1e46afbd7b54 implemented new methods in the math module.
lorenz
parents: 1845
diff changeset
1524
1834
d31cd03677fb added an incomplete Vector2 to the math module.
lorenz
parents:
diff changeset
1525 static PyMethodDef vector2_methods[] = {
1837
80abf33a8764 added the following methods to Vector2
lorenz
parents: 1836
diff changeset
1526 {"length", (PyCFunction)vector_length, METH_NOARGS,
80abf33a8764 added the following methods to Vector2
lorenz
parents: 1836
diff changeset
1527 "returns the length/magnitude of the vector."
80abf33a8764 added the following methods to Vector2
lorenz
parents: 1836
diff changeset
1528 },
80abf33a8764 added the following methods to Vector2
lorenz
parents: 1836
diff changeset
1529 {"length_squared", (PyCFunction)vector_length_squared, METH_NOARGS,
80abf33a8764 added the following methods to Vector2
lorenz
parents: 1836
diff changeset
1530 "returns the length/magnitude of the vector."
80abf33a8764 added the following methods to Vector2
lorenz
parents: 1836
diff changeset
1531 },
1836
18eefb9bd5a2 updates to the pygame.math module
lorenz
parents: 1835
diff changeset
1532 {"rotate", (PyCFunction)vector2_rotate, METH_VARARGS,
18eefb9bd5a2 updates to the pygame.math module
lorenz
parents: 1835
diff changeset
1533 "returns a new vector rotated counterclockwise by the angle given in degrees."
1834
d31cd03677fb added an incomplete Vector2 to the math module.
lorenz
parents:
diff changeset
1534 },
1836
18eefb9bd5a2 updates to the pygame.math module
lorenz
parents: 1835
diff changeset
1535 {"rotate_ip", (PyCFunction)vector2_rotate_ip, METH_VARARGS,
18eefb9bd5a2 updates to the pygame.math module
lorenz
parents: 1835
diff changeset
1536 "rotates the vector counterclockwise by the angle given in degrees."
1834
d31cd03677fb added an incomplete Vector2 to the math module.
lorenz
parents:
diff changeset
1537 },
1846
1e46afbd7b54 implemented new methods in the math module.
lorenz
parents: 1845
diff changeset
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
1e46afbd7b54 implemented new methods in the math module.
lorenz
parents: 1845
diff changeset
1543 },
1836
18eefb9bd5a2 updates to the pygame.math module
lorenz
parents: 1835
diff changeset
1544 {"normalize", (PyCFunction)vector_normalize, METH_NOARGS,
1834
d31cd03677fb added an incomplete Vector2 to the math module.
lorenz
parents:
diff changeset
1545 "returns a vector that has length == 1 and the same direction as self."
d31cd03677fb added an incomplete Vector2 to the math module.
lorenz
parents:
diff changeset
1546 },
1836
18eefb9bd5a2 updates to the pygame.math module
lorenz
parents: 1835
diff changeset
1547 {"normalize_ip", (PyCFunction)vector_normalize_ip, METH_NOARGS,
1834
d31cd03677fb added an incomplete Vector2 to the math module.
lorenz
parents:
diff changeset
1548 "Normalizes the vector so that it has length == 1."
d31cd03677fb added an incomplete Vector2 to the math module.
lorenz
parents:
diff changeset
1549 },
1836
18eefb9bd5a2 updates to the pygame.math module
lorenz
parents: 1835
diff changeset
1550 {"is_normalized", (PyCFunction)vector_is_normalized, METH_NOARGS,
1834
d31cd03677fb added an incomplete Vector2 to the math module.
lorenz
parents:
diff changeset
1551 "returns True if the vector has length == 1. otherwise it returns False."
d31cd03677fb added an incomplete Vector2 to the math module.
lorenz
parents:
diff changeset
1552 },
1836
18eefb9bd5a2 updates to the pygame.math module
lorenz
parents: 1835
diff changeset
1553 {"cross", (PyCFunction)vector2_cross, METH_O,
1834
d31cd03677fb added an incomplete Vector2 to the math module.
lorenz
parents:
diff changeset
1554 "calculates the cross product."
d31cd03677fb added an incomplete Vector2 to the math module.
lorenz
parents:
diff changeset
1555 },
1836
18eefb9bd5a2 updates to the pygame.math module
lorenz
parents: 1835
diff changeset
1556 {"dot", (PyCFunction)vector_dot, METH_O,
1834
d31cd03677fb added an incomplete Vector2 to the math module.
lorenz
parents:
diff changeset
1557 "calculates the dot product."
d31cd03677fb added an incomplete Vector2 to the math module.
lorenz
parents:
diff changeset
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
d31cd03677fb added an incomplete Vector2 to the math module.
lorenz
parents:
diff changeset
1560 "returns the angle between self and the given vector."
d31cd03677fb added an incomplete Vector2 to the math module.
lorenz
parents:
diff changeset
1561 },
1836
18eefb9bd5a2 updates to the pygame.math module
lorenz
parents: 1835
diff changeset
1562 {"scale_to_length", (PyCFunction)vector_scale_to_length, METH_O,
1837
80abf33a8764 added the following methods to Vector2
lorenz
parents: 1836
diff changeset
1563 "scales the vector to the given length."
80abf33a8764 added the following methods to Vector2
lorenz
parents: 1836
diff changeset
1564 },
80abf33a8764 added the following methods to Vector2
lorenz
parents: 1836
diff changeset
1565 {"reflect", (PyCFunction)vector_reflect, METH_O,
80abf33a8764 added the following methods to Vector2
lorenz
parents: 1836
diff changeset
1566 "reflects the vector on the surface characterized by the given normal."
80abf33a8764 added the following methods to Vector2
lorenz
parents: 1836
diff changeset
1567 },
80abf33a8764 added the following methods to Vector2
lorenz
parents: 1836
diff changeset
1568 {"reflect_ip", (PyCFunction)vector_reflect_ip, METH_O,
80abf33a8764 added the following methods to Vector2
lorenz
parents: 1836
diff changeset
1569 "reflects the vector in-place on the surface characterized by the given normal."
1836
18eefb9bd5a2 updates to the pygame.math module
lorenz
parents: 1835
diff changeset
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
1e46afbd7b54 implemented new methods in the math module.
lorenz
parents: 1845
diff changeset
1580 {"as_polar", (PyCFunction)vector2_as_polar, METH_NOARGS,
1e46afbd7b54 implemented new methods in the math module.
lorenz
parents: 1845
diff changeset
1581 "returns a 2-tuple (r, phi) where r is the radial distance and phi is the angle to the positive x-axis."
1e46afbd7b54 implemented new methods in the math module.
lorenz
parents: 1845
diff changeset
1582 },
1e46afbd7b54 implemented new methods in the math module.
lorenz
parents: 1845
diff changeset
1583 {"from_polar", (PyCFunction)vector2_from_polar, METH_VARARGS,
1e46afbd7b54 implemented new methods in the math module.
lorenz
parents: 1845
diff changeset
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."
1e46afbd7b54 implemented new methods in the math module.
lorenz
parents: 1845
diff changeset
1585 },
1836
18eefb9bd5a2 updates to the pygame.math module
lorenz
parents: 1835
diff changeset
1586
1834
d31cd03677fb added an incomplete Vector2 to the math module.
lorenz
parents:
diff changeset
1587 {NULL} /* Sentinel */
d31cd03677fb added an incomplete Vector2 to the math module.
lorenz
parents:
diff changeset
1588 };
d31cd03677fb added an incomplete Vector2 to the math module.
lorenz
parents:
diff changeset
1589
d31cd03677fb added an incomplete Vector2 to the math module.
lorenz
parents:
diff changeset
1590
d31cd03677fb added an incomplete Vector2 to the math module.
lorenz
parents:
diff changeset
1591 static PyGetSetDef vector2_getsets[] = {
d31cd03677fb added an incomplete Vector2 to the math module.
lorenz
parents:
diff changeset
1592 { "x", (getter)vector_getx, (setter)vector_setx, NULL, NULL },
d31cd03677fb added an incomplete Vector2 to the math module.
lorenz
parents:
diff changeset
1593 { "y", (getter)vector_gety, (setter)vector_sety, NULL, NULL },
d31cd03677fb added an incomplete Vector2 to the math module.
lorenz
parents:
diff changeset
1594 { NULL, 0, NULL, NULL, NULL } /* Sentinel */
d31cd03677fb added an incomplete Vector2 to the math module.
lorenz
parents:
diff changeset
1595 };
d31cd03677fb added an incomplete Vector2 to the math module.
lorenz
parents:
diff changeset
1596
d31cd03677fb added an incomplete Vector2 to the math module.
lorenz
parents:
diff changeset
1597
d31cd03677fb added an incomplete Vector2 to the math module.
lorenz
parents:
diff changeset
1598 /********************************
1837
80abf33a8764 added the following methods to Vector2
lorenz
parents: 1836
diff changeset
1599 * PyVector2 type definition
1834
d31cd03677fb added an incomplete Vector2 to the math module.
lorenz
parents:
diff changeset
1600 ********************************/
d31cd03677fb added an incomplete Vector2 to the math module.
lorenz
parents:
diff changeset
1601
d31cd03677fb added an incomplete Vector2 to the math module.
lorenz
parents:
diff changeset
1602 static PyTypeObject PyVector2_Type = {
d31cd03677fb added an incomplete Vector2 to the math module.
lorenz
parents:
diff changeset
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
d31cd03677fb added an incomplete Vector2 to the math module.
lorenz
parents:
diff changeset
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
d31cd03677fb added an incomplete Vector2 to the math module.
lorenz
parents:
diff changeset
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
d31cd03677fb added an incomplete Vector2 to the math module.
lorenz
parents:
diff changeset
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
d31cd03677fb added an incomplete Vector2 to the math module.
lorenz
parents:
diff changeset
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
d31cd03677fb added an incomplete Vector2 to the math module.
lorenz
parents:
diff changeset
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
0959bd773ffd added support for swizzling.
lorenz
parents: 1838
diff changeset
1629 Py_TPFLAGS_CHECKTYPES, /* tp_flags */
1834
d31cd03677fb added an incomplete Vector2 to the math module.
lorenz
parents:
diff changeset
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
d31cd03677fb added an incomplete Vector2 to the math module.
lorenz
parents:
diff changeset
1632
d31cd03677fb added an incomplete Vector2 to the math module.
lorenz
parents:
diff changeset
1633 /* Assigned meaning in release 2.0 */
d31cd03677fb added an incomplete Vector2 to the math module.
lorenz
parents:
diff changeset
1634 /* call function for all accessible objects */
d31cd03677fb added an incomplete Vector2 to the math module.
lorenz
parents:
diff changeset
1635 0, /* tp_traverse */
d31cd03677fb added an incomplete Vector2 to the math module.
lorenz
parents:
diff changeset
1636 /* delete references to contained objects */
d31cd03677fb added an incomplete Vector2 to the math module.
lorenz
parents:
diff changeset
1637 0, /* tp_clear */
d31cd03677fb added an incomplete Vector2 to the math module.
lorenz
parents:
diff changeset
1638
d31cd03677fb added an incomplete Vector2 to the math module.
lorenz
parents:
diff changeset
1639 /* Assigned meaning in release 2.1 */
d31cd03677fb added an incomplete Vector2 to the math module.
lorenz
parents:
diff changeset
1640 /* rich comparisons */
d31cd03677fb added an incomplete Vector2 to the math module.
lorenz
parents:
diff changeset
1641 (richcmpfunc)vector_richcompare, /* tp_richcompare */
d31cd03677fb added an incomplete Vector2 to the math module.
lorenz
parents:
diff changeset
1642 /* weak reference enabler */
d31cd03677fb added an incomplete Vector2 to the math module.
lorenz
parents:
diff changeset
1643 0, /* tp_weaklistoffset */
d31cd03677fb added an incomplete Vector2 to the math module.
lorenz
parents:
diff changeset
1644
d31cd03677fb added an incomplete Vector2 to the math module.
lorenz
parents:
diff changeset
1645 /* Added in release 2.2 */
d31cd03677fb added an incomplete Vector2 to the math module.
lorenz
parents:
diff changeset
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
d31cd03677fb added an incomplete Vector2 to the math module.
lorenz
parents:
diff changeset
1648 0, /* tp_iternext */
d31cd03677fb added an incomplete Vector2 to the math module.
lorenz
parents:
diff changeset
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
d31cd03677fb added an incomplete Vector2 to the math module.
lorenz
parents:
diff changeset
1653 0, /* tp_base */
d31cd03677fb added an incomplete Vector2 to the math module.
lorenz
parents:
diff changeset
1654 0, /* tp_dict */
d31cd03677fb added an incomplete Vector2 to the math module.
lorenz
parents:
diff changeset
1655 0, /* tp_descr_get */
d31cd03677fb added an incomplete Vector2 to the math module.
lorenz
parents:
diff changeset
1656 0, /* tp_descr_set */
d31cd03677fb added an incomplete Vector2 to the math module.
lorenz
parents:
diff changeset
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
d31cd03677fb added an incomplete Vector2 to the math module.
lorenz
parents:
diff changeset
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
d31cd03677fb added an incomplete Vector2 to the math module.
lorenz
parents:
diff changeset
1661 0, /* tp_free */
d31cd03677fb added an incomplete Vector2 to the math module.
lorenz
parents:
diff changeset
1662 0, /* tp_is_gc */
d31cd03677fb added an incomplete Vector2 to the math module.
lorenz
parents:
diff changeset
1663 0, /* tp_bases */
d31cd03677fb added an incomplete Vector2 to the math module.
lorenz
parents:
diff changeset
1664 0, /* tp_mro */
d31cd03677fb added an incomplete Vector2 to the math module.
lorenz
parents:
diff changeset
1665 0, /* tp_cache */
d31cd03677fb added an incomplete Vector2 to the math module.
lorenz
parents:
diff changeset
1666 0, /* tp_subclasses */
d31cd03677fb added an incomplete Vector2 to the math module.
lorenz
parents:
diff changeset
1667 0, /* tp_weaklist */
d31cd03677fb added an incomplete Vector2 to the math module.
lorenz
parents:
diff changeset
1668 };
d31cd03677fb added an incomplete Vector2 to the math module.
lorenz
parents:
diff changeset
1669
d31cd03677fb added an incomplete Vector2 to the math module.
lorenz
parents:
diff changeset
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
bbc21cd6faeb some last minute changes before I merge to trunk.
lorenz
parents: 1847
diff changeset
1815 /* special-case rotation by 0, 90, 180 and 270 degrees */
bbc21cd6faeb some last minute changes before I merge to trunk.
lorenz
parents: 1847
diff changeset
1816 if (fmod(angle + epsilon, 90.) < 2 * epsilon) {
bbc21cd6faeb some last minute changes before I merge to trunk.
lorenz
parents: 1847
diff changeset
1817 switch ((int)((angle + epsilon) / 90)) {
bbc21cd6faeb some last minute changes before I merge to trunk.
lorenz
parents: 1847
diff changeset
1818 case 0: /* 0 degrees */
bbc21cd6faeb some last minute changes before I merge to trunk.
lorenz
parents: 1847
diff changeset
1819 memcpy(dst_coords, src_coords, 3 * sizeof(src_coords[0]));
bbc21cd6faeb some last minute changes before I merge to trunk.
lorenz
parents: 1847
diff changeset
1820 break;
bbc21cd6faeb some last minute changes before I merge to trunk.
lorenz
parents: 1847
diff changeset
1821 case 1: /* 90 degrees */
bbc21cd6faeb some last minute changes before I merge to trunk.
lorenz
parents: 1847
diff changeset
1822 dst_coords[0] = (src_coords[0] * (axis[0] * axis[0]) +
bbc21cd6faeb some last minute changes before I merge to trunk.
lorenz
parents: 1847
diff changeset
1823 src_coords[1] * (axis[0] * axis[1] - axis[2]) +
bbc21cd6faeb some last minute changes before I merge to trunk.
lorenz
parents: 1847
diff changeset
1824 src_coords[2] * (axis[0] * axis[2] + axis[1]));
bbc21cd6faeb some last minute changes before I merge to trunk.
lorenz
parents: 1847
diff changeset
1825 dst_coords[1] = (src_coords[0] * (axis[0] * axis[1] + axis[2]) +
bbc21cd6faeb some last minute changes before I merge to trunk.
lorenz
parents: 1847
diff changeset
1826 src_coords[1] * (axis[1] * axis[1]) +
bbc21cd6faeb some last minute changes before I merge to trunk.
lorenz
parents: 1847
diff changeset
1827 src_coords[2] * (axis[1] * axis[2] - axis[0]));
bbc21cd6faeb some last minute changes before I merge to trunk.
lorenz
parents: 1847
diff changeset
1828 dst_coords[2] = (src_coords[0] * (axis[0] * axis[2] - axis[1]) +
bbc21cd6faeb some last minute changes before I merge to trunk.
lorenz
parents: 1847
diff changeset
1829 src_coords[1] * (axis[1] * axis[2] + axis[0]) +
bbc21cd6faeb some last minute changes before I merge to trunk.
lorenz
parents: 1847
diff changeset
1830 src_coords[2] * (axis[2] * axis[2]));
bbc21cd6faeb some last minute changes before I merge to trunk.
lorenz
parents: 1847
diff changeset
1831 break;
bbc21cd6faeb some last minute changes before I merge to trunk.
lorenz
parents: 1847
diff changeset
1832 case 2: /* 180 degrees */
bbc21cd6faeb some last minute changes before I merge to trunk.
lorenz
parents: 1847
diff changeset
1833 dst_coords[0] = (src_coords[0] * (-1 + axis[0] * axis[0] * 2) +
bbc21cd6faeb some last minute changes before I merge to trunk.
lorenz
parents: 1847
diff changeset
1834 src_coords[1] * (axis[0] * axis[1] * 2) +
bbc21cd6faeb some last minute changes before I merge to trunk.
lorenz
parents: 1847
diff changeset
1835 src_coords[2] * (axis[0] * axis[2] * 2));
bbc21cd6faeb some last minute changes before I merge to trunk.
lorenz
parents: 1847
diff changeset
1836 dst_coords[1] = (src_coords[0] * (axis[0] * axis[1] * 2) +
bbc21cd6faeb some last minute changes before I merge to trunk.
lorenz
parents: 1847
diff changeset
1837 src_coords[1] * (-1 + axis[1] * axis[1] * 2) +
bbc21cd6faeb some last minute changes before I merge to trunk.
lorenz
parents: 1847
diff changeset
1838 src_coords[2] * (axis[1] * axis[2] * 2));
bbc21cd6faeb some last minute changes before I merge to trunk.
lorenz
parents: 1847
diff changeset
1839 dst_coords[2] = (src_coords[0] * (axis[0] * axis[2] * 2) +
bbc21cd6faeb some last minute changes before I merge to trunk.
lorenz
parents: 1847
diff changeset
1840 src_coords[1] * (axis[1] * axis[2] * 2) +
bbc21cd6faeb some last minute changes before I merge to trunk.
lorenz
parents: 1847
diff changeset
1841 src_coords[2] * (-1 + axis[2] * axis[2] * 2));
bbc21cd6faeb some last minute changes before I merge to trunk.
lorenz
parents: 1847
diff changeset
1842 break;
bbc21cd6faeb some last minute changes before I merge to trunk.
lorenz
parents: 1847
diff changeset
1843 case 3: /* 270 degrees */
bbc21cd6faeb some last minute changes before I merge to trunk.
lorenz
parents: 1847
diff changeset
1844 dst_coords[0] = (src_coords[0] * (axis[0] * axis[0]) +
bbc21cd6faeb some last minute changes before I merge to trunk.
lorenz
parents: 1847
diff changeset
1845 src_coords[1] * (axis[0] * axis[1] + axis[2]) +
bbc21cd6faeb some last minute changes before I merge to trunk.
lorenz
parents: 1847
diff changeset
1846 src_coords[2] * (axis[0] * axis[2] - axis[1]));
bbc21cd6faeb some last minute changes before I merge to trunk.
lorenz
parents: 1847
diff changeset
1847 dst_coords[1] = (src_coords[0] * (axis[0] * axis[1] - axis[2]) +
bbc21cd6faeb some last minute changes before I merge to trunk.
lorenz
parents: 1847
diff changeset
1848 src_coords[1] * (axis[1] * axis[1]) +
bbc21cd6faeb some last minute changes before I merge to trunk.
lorenz
parents: 1847
diff changeset
1849 src_coords[2] * (axis[1] * axis[2] + axis[0]));
bbc21cd6faeb some last minute changes before I merge to trunk.
lorenz
parents: 1847
diff changeset
1850 dst_coords[2] = (src_coords[0] * (axis[0] * axis[2] + axis[1]) +
bbc21cd6faeb some last minute changes before I merge to trunk.
lorenz
parents: 1847
diff changeset
1851 src_coords[1] * (axis[1] * axis[2] - axis[0]) +
bbc21cd6faeb some last minute changes before I merge to trunk.
lorenz
parents: 1847
diff changeset
1852 src_coords[2] * (axis[2] * axis[2]));
bbc21cd6faeb some last minute changes before I merge to trunk.
lorenz
parents: 1847
diff changeset
1853 break;
bbc21cd6faeb some last minute changes before I merge to trunk.
lorenz
parents: 1847
diff changeset
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
1e46afbd7b54 implemented new methods in the math module.
lorenz
parents: 1845
diff changeset
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
1e46afbd7b54 implemented new methods in the math module.
lorenz
parents: 1845
diff changeset
2106 double r, theta, phi;
1e46afbd7b54 implemented new methods in the math module.
lorenz
parents: 1845
diff changeset
2107 r = sqrt(_scalar_product(self->coords, self->coords, self->dim));
1e46afbd7b54 implemented new methods in the math module.
lorenz
parents: 1845
diff changeset
2108 theta = acos(self->coords[2] / r);
1e46afbd7b54 implemented new methods in the math module.
lorenz
parents: 1845
diff changeset
2109 phi = atan2(self->coords[1], self->coords[0]);
1e46afbd7b54 implemented new methods in the math module.
lorenz
parents: 1845
diff changeset
2110 return Py_BuildValue("(ddd)", r, theta, phi);
1e46afbd7b54 implemented new methods in the math module.
lorenz
parents: 1845
diff changeset
2111 }
1e46afbd7b54 implemented new methods in the math module.
lorenz
parents: 1845
diff changeset
2112
1e46afbd7b54 implemented new methods in the math module.
lorenz
parents: 1845
diff changeset
2113 static PyObject *
1e46afbd7b54 implemented new methods in the math module.
lorenz
parents: 1845
diff changeset
2114 vector3_from_spherical(PyVector *self, PyObject *args)
1e46afbd7b54 implemented new methods in the math module.
lorenz
parents: 1845
diff changeset
2115 {
1e46afbd7b54 implemented new methods in the math module.
lorenz
parents: 1845
diff changeset
2116 PyObject *spherical_obj, *r_obj, *theta_obj, *phi_obj;
1e46afbd7b54 implemented new methods in the math module.
lorenz
parents: 1845
diff changeset
2117 double r, theta, phi;
1e46afbd7b54 implemented new methods in the math module.
lorenz
parents: 1845
diff changeset
2118 theta_obj = NULL;
1e46afbd7b54 implemented new methods in the math module.
lorenz
parents: 1845
diff changeset
2119 phi_obj = NULL;
1e46afbd7b54 implemented new methods in the math module.
lorenz
parents: 1845
diff changeset
2120 if (!PyArg_ParseTuple(args, "O:Vector3.from_spherical", &spherical_obj)) {
1e46afbd7b54 implemented new methods in the math module.
lorenz
parents: 1845
diff changeset
2121 return NULL;
1e46afbd7b54 implemented new methods in the math module.
lorenz
parents: 1845
diff changeset
2122 }
1845
7d2a16e6a1d3 rearanged math module. added forward declarations at the beginning.
lorenz
parents: 1844
diff changeset
2123
1846
1e46afbd7b54 implemented new methods in the math module.
lorenz
parents: 1845
diff changeset
2124 if (!PySequence_Check(spherical_obj) || PySequence_Length(spherical_obj) != 3) {
1e46afbd7b54 implemented new methods in the math module.
lorenz
parents: 1845
diff changeset
2125 PyErr_SetString(PyExc_TypeError, "3-tuple containing r, theta and phi is expected.");
1e46afbd7b54 implemented new methods in the math module.
lorenz
parents: 1845
diff changeset
2126 return NULL;
1e46afbd7b54 implemented new methods in the math module.
lorenz
parents: 1845
diff changeset
2127 }
1e46afbd7b54 implemented new methods in the math module.
lorenz
parents: 1845
diff changeset
2128 r_obj = PySequence_GetItem(spherical_obj, 0);
1e46afbd7b54 implemented new methods in the math module.
lorenz
parents: 1845
diff changeset
2129 theta_obj = PySequence_GetItem(spherical_obj, 1);
1e46afbd7b54 implemented new methods in the math module.
lorenz
parents: 1845
diff changeset
2130 phi_obj = PySequence_GetItem(spherical_obj, 2);
1e46afbd7b54 implemented new methods in the math module.
lorenz
parents: 1845
diff changeset
2131 if (!PyNumber_Check(r_obj) || !PyNumber_Check(theta_obj) ||
1e46afbd7b54 implemented new methods in the math module.
lorenz
parents: 1845
diff changeset
2132 !PyNumber_Check(phi_obj)) {
1e46afbd7b54 implemented new methods in the math module.
lorenz
parents: 1845
diff changeset
2133 PyErr_SetString(PyExc_TypeError,
1e46afbd7b54 implemented new methods in the math module.
lorenz
parents: 1845
diff changeset
2134 "expected numerical values");
1e46afbd7b54 implemented new methods in the math module.
lorenz
parents: 1845
diff changeset
2135 Py_DECREF(r_obj);
1e46afbd7b54 implemented new methods in the math module.
lorenz
parents: 1845
diff changeset
2136 Py_DECREF(theta_obj);
1e46afbd7b54 implemented new methods in the math module.
lorenz
parents: 1845
diff changeset
2137 Py_DECREF(phi_obj);
1e46afbd7b54 implemented new methods in the math module.
lorenz
parents: 1845
diff changeset
2138 return NULL;
1e46afbd7b54 implemented new methods in the math module.
lorenz
parents: 1845
diff changeset
2139 }
1e46afbd7b54 implemented new methods in the math module.
lorenz
parents: 1845
diff changeset
2140 r = PyFloat_AsDouble(r_obj);
1e46afbd7b54 implemented new methods in the math module.
lorenz
parents: 1845
diff changeset
2141 theta = PyFloat_AsDouble(theta_obj);
1e46afbd7b54 implemented new methods in the math module.
lorenz
parents: 1845
diff changeset
2142 phi = PyFloat_AsDouble(phi_obj);
1e46afbd7b54 implemented new methods in the math module.
lorenz
parents: 1845
diff changeset
2143 Py_DECREF(r_obj);
1e46afbd7b54 implemented new methods in the math module.
lorenz
parents: 1845
diff changeset
2144 Py_DECREF(theta_obj);
1e46afbd7b54 implemented new methods in the math module.
lorenz
parents: 1845
diff changeset
2145 Py_DECREF(phi_obj);
1e46afbd7b54 implemented new methods in the math module.
lorenz
parents: 1845
diff changeset
2146
1e46afbd7b54 implemented new methods in the math module.
lorenz
parents: 1845
diff changeset
2147 self->coords[0] = r * sin(theta) * cos(phi);
1e46afbd7b54 implemented new methods in the math module.
lorenz
parents: 1845
diff changeset
2148 self->coords[1] = r * sin(theta) * sin(phi);
1e46afbd7b54 implemented new methods in the math module.
lorenz
parents: 1845
diff changeset
2149 self->coords[2] = r * cos(theta);
1e46afbd7b54 implemented new methods in the math module.
lorenz
parents: 1845
diff changeset
2150
1e46afbd7b54 implemented new methods in the math module.
lorenz
parents: 1845
diff changeset
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
1e46afbd7b54 implemented new methods in the math module.
lorenz
parents: 1845
diff changeset
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
1e46afbd7b54 implemented new methods in the math module.
lorenz
parents: 1845
diff changeset
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
1e46afbd7b54 implemented new methods in the math module.
lorenz
parents: 1845
diff changeset
2227 {"as_spherical", (PyCFunction)vector3_as_spherical, METH_NOARGS,
1e46afbd7b54 implemented new methods in the math module.
lorenz
parents: 1845
diff changeset
2228 "returns a tuple (r, theta, phi) where r is the radial distance, theta is the inclination angle and phi is the azimutal angle."
1e46afbd7b54 implemented new methods in the math module.
lorenz
parents: 1845
diff changeset
2229 },
1e46afbd7b54 implemented new methods in the math module.
lorenz
parents: 1845
diff changeset
2230 {"from_spherical", (PyCFunction)vector3_from_spherical, METH_VARARGS,
1e46afbd7b54 implemented new methods in the math module.
lorenz
parents: 1845
diff changeset
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."
1e46afbd7b54 implemented new methods in the math module.
lorenz
parents: 1845
diff changeset
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
1e46afbd7b54 implemented new methods in the math module.
lorenz
parents: 1845
diff changeset
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
1e46afbd7b54 implemented new methods in the math module.
lorenz
parents: 1845
diff changeset
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
1e46afbd7b54 implemented new methods in the math module.
lorenz
parents: 1845
diff changeset
2426 /********************************************
1e46afbd7b54 implemented new methods in the math module.
lorenz
parents: 1845
diff changeset
2427 * PyVector_SlerpIterator type definition
1e46afbd7b54 implemented new methods in the math module.
lorenz
parents: 1845
diff changeset
2428 ********************************************/
1e46afbd7b54 implemented new methods in the math module.
lorenz
parents: 1845
diff changeset
2429 static void
1e46afbd7b54 implemented new methods in the math module.
lorenz
parents: 1845
diff changeset
2430 vector_slerpiter_dealloc(vector_slerpiter *it)
1e46afbd7b54 implemented new methods in the math module.
lorenz
parents: 1845
diff changeset
2431 {
1e46afbd7b54 implemented new methods in the math module.
lorenz
parents: 1845
diff changeset
2432 PyObject_Del(it);
1e46afbd7b54 implemented new methods in the math module.
lorenz
parents: 1845
diff changeset
2433 }
1e46afbd7b54 implemented new methods in the math module.
lorenz
parents: 1845
diff changeset
2434
1e46afbd7b54 implemented new methods in the math module.
lorenz
parents: 1845
diff changeset
2435 static PyObject *
1e46afbd7b54 implemented new methods in the math module.
lorenz
parents: 1845
diff changeset
2436 vector_slerpiter_next(vector_slerpiter *it)
1e46afbd7b54 implemented new methods in the math module.
lorenz
parents: 1845
diff changeset
2437 {
1e46afbd7b54 implemented new methods in the math module.
lorenz
parents: 1845
diff changeset
2438 int i, j;
1e46afbd7b54 implemented new methods in the math module.
lorenz
parents: 1845
diff changeset
2439 PyVector *ret;
1e46afbd7b54 implemented new methods in the math module.
lorenz
parents: 1845
diff changeset
2440 double tmp;
1e46afbd7b54 implemented new methods in the math module.
lorenz
parents: 1845
diff changeset
2441 assert(it != NULL);
1e46afbd7b54 implemented new methods in the math module.
lorenz
parents: 1845
diff changeset
2442
1e46afbd7b54 implemented new methods in the math module.
lorenz
parents: 1845
diff changeset
2443 if (it->it_index < it->steps) {
1e46afbd7b54 implemented new methods in the math module.
lorenz
parents: 1845
diff changeset
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
1e46afbd7b54 implemented new methods in the math module.
lorenz
parents: 1845
diff changeset
2453 }
1e46afbd7b54 implemented new methods in the math module.
lorenz
parents: 1845
diff changeset
2454 ++(it->it_index);
1e46afbd7b54 implemented new methods in the math module.
lorenz
parents: 1845
diff changeset
2455 return (PyObject*)ret;
1e46afbd7b54 implemented new methods in the math module.
lorenz
parents: 1845
diff changeset
2456 }
1e46afbd7b54 implemented new methods in the math module.
lorenz
parents: 1845
diff changeset
2457
1e46afbd7b54 implemented new methods in the math module.
lorenz
parents: 1845
diff changeset
2458 return NULL;
1e46afbd7b54 implemented new methods in the math module.
lorenz
parents: 1845
diff changeset
2459 }
1e46afbd7b54 implemented new methods in the math module.
lorenz
parents: 1845
diff changeset
2460
1e46afbd7b54 implemented new methods in the math module.
lorenz
parents: 1845
diff changeset
2461 static PyObject *
1e46afbd7b54 implemented new methods in the math module.
lorenz
parents: 1845
diff changeset
2462 vector_slerpiter_len(vector_slerpiter *it)
1e46afbd7b54 implemented new methods in the math module.
lorenz
parents: 1845
diff changeset
2463 {
1e46afbd7b54 implemented new methods in the math module.
lorenz
parents: 1845
diff changeset
2464 Py_ssize_t len = 0;
1e46afbd7b54 implemented new methods in the math module.
lorenz
parents: 1845
diff changeset
2465 if (it) {
1e46afbd7b54 implemented new methods in the math module.
lorenz
parents: 1845
diff changeset
2466 len = it->steps - it->it_index;
1e46afbd7b54 implemented new methods in the math module.
lorenz
parents: 1845
diff changeset
2467 }
1e46afbd7b54 implemented new methods in the math module.
lorenz
parents: 1845
diff changeset
2468 return PyInt_FromSsize_t(len);
1e46afbd7b54 implemented new methods in the math module.
lorenz
parents: 1845
diff changeset
2469 }
1e46afbd7b54 implemented new methods in the math module.
lorenz
parents: 1845
diff changeset
2470
1e46afbd7b54 implemented new methods in the math module.
lorenz
parents: 1845
diff changeset
2471 static PyMethodDef vector_slerpiter_methods[] = {
1e46afbd7b54 implemented new methods in the math module.
lorenz
parents: 1845
diff changeset
2472 {"__length_hint__", (PyCFunction)vector_slerpiter_len, METH_NOARGS,
1e46afbd7b54 implemented new methods in the math module.
lorenz
parents: 1845
diff changeset
2473 },
1e46afbd7b54 implemented new methods in the math module.
lorenz
parents: 1845
diff changeset
2474 {NULL, NULL} /* sentinel */
1e46afbd7b54 implemented new methods in the math module.
lorenz
parents: 1845
diff changeset
2475 };
1e46afbd7b54 implemented new methods in the math module.
lorenz
parents: 1845
diff changeset
2476
1e46afbd7b54 implemented new methods in the math module.
lorenz
parents: 1845
diff changeset
2477 static PyTypeObject PyVector_SlerpIter_Type = {
1e46afbd7b54 implemented new methods in the math module.
lorenz
parents: 1845
diff changeset
2478 PyObject_HEAD_INIT(NULL)
1e46afbd7b54 implemented new methods in the math module.
lorenz
parents: 1845
diff changeset
2479 0, /* ob_size */
1e46afbd7b54 implemented new methods in the math module.
lorenz
parents: 1845
diff changeset
2480 "pygame.math.VectorSlerpIterator", /* tp_name */
1e46afbd7b54 implemented new methods in the math module.
lorenz
parents: 1845
diff changeset
2481 sizeof(vector_slerpiter), /* tp_basicsize */
1e46afbd7b54 implemented new methods in the math module.
lorenz
parents: 1845
diff changeset
2482 0, /* tp_itemsize */
1e46afbd7b54 implemented new methods in the math module.
lorenz
parents: 1845
diff changeset
2483 (destructor)vector_slerpiter_dealloc, /* tp_dealloc */
1e46afbd7b54 implemented new methods in the math module.
lorenz
parents: 1845
diff changeset
2484 0, /* tp_print */
1e46afbd7b54 implemented new methods in the math module.
lorenz
parents: 1845
diff changeset
2485 0, /* tp_getattr */
1e46afbd7b54 implemented new methods in the math module.
lorenz
parents: 1845
diff changeset
2486 0, /* tp_setattr */
1e46afbd7b54 implemented new methods in the math module.
lorenz
parents: 1845
diff changeset
2487 0, /* tp_compare */
1e46afbd7b54 implemented new methods in the math module.
lorenz
parents: 1845
diff changeset
2488 0, /* tp_repr */
1e46afbd7b54 implemented new methods in the math module.
lorenz
parents: 1845
diff changeset
2489 0, /* tp_as_number */
1e46afbd7b54 implemented new methods in the math module.
lorenz
parents: 1845
diff changeset
2490 0, /* tp_as_sequence */
1e46afbd7b54 implemented new methods in the math module.
lorenz
parents: 1845
diff changeset
2491 0, /* tp_as_mapping */
1e46afbd7b54 implemented new methods in the math module.
lorenz
parents: 1845
diff changeset
2492 0, /* tp_hash */
1e46afbd7b54 implemented new methods in the math module.
lorenz
parents: 1845
diff changeset
2493 0, /* tp_call */
1e46afbd7b54 implemented new methods in the math module.
lorenz
parents: 1845
diff changeset
2494 0, /* tp_str */
1e46afbd7b54 implemented new methods in the math module.
lorenz
parents: 1845
diff changeset
2495 PyObject_GenericGetAttr, /* tp_getattro */
1e46afbd7b54 implemented new methods in the math module.
lorenz
parents: 1845
diff changeset
2496 0, /* tp_setattro */
1e46afbd7b54 implemented new methods in the math module.
lorenz
parents: 1845
diff changeset
2497 0, /* tp_as_buffer */
1e46afbd7b54 implemented new methods in the math module.
lorenz
parents: 1845
diff changeset
2498 Py_TPFLAGS_DEFAULT, /* tp_flags */
1e46afbd7b54 implemented new methods in the math module.
lorenz
parents: 1845
diff changeset
2499 0, /* tp_doc */
1e46afbd7b54 implemented new methods in the math module.
lorenz
parents: 1845
diff changeset
2500 0, /* tp_traverse */
1e46afbd7b54 implemented new methods in the math module.
lorenz
parents: 1845
diff changeset
2501 0, /* tp_clear */
1e46afbd7b54 implemented new methods in the math module.
lorenz
parents: 1845
diff changeset
2502 0, /* tp_richcompare */
1e46afbd7b54 implemented new methods in the math module.
lorenz
parents: 1845
diff changeset
2503 0, /* tp_weaklistoffset */
1e46afbd7b54 implemented new methods in the math module.
lorenz
parents: 1845
diff changeset
2504 PyObject_SelfIter, /* tp_iter */
1e46afbd7b54 implemented new methods in the math module.
lorenz
parents: 1845
diff changeset
2505 (iternextfunc)vector_slerpiter_next, /* tp_iternext */
1e46afbd7b54 implemented new methods in the math module.
lorenz
parents: 1845
diff changeset
2506 vector_slerpiter_methods, /* tp_methods */
1e46afbd7b54 implemented new methods in the math module.
lorenz
parents: 1845
diff changeset
2507 0, /* tp_members */
1e46afbd7b54 implemented new methods in the math module.
lorenz
parents: 1845
diff changeset
2508 };
1e46afbd7b54 implemented new methods in the math module.
lorenz
parents: 1845
diff changeset
2509
1e46afbd7b54 implemented new methods in the math module.
lorenz
parents: 1845
diff changeset
2510 static PyObject *
1e46afbd7b54 implemented new methods in the math module.
lorenz
parents: 1845
diff changeset
2511 vector_slerp(PyVector *self, PyObject *args)
1e46afbd7b54 implemented new methods in the math module.
lorenz
parents: 1845
diff changeset
2512 {
1e46afbd7b54 implemented new methods in the math module.
lorenz
parents: 1845
diff changeset
2513 vector_slerpiter *it;
1e46afbd7b54 implemented new methods in the math module.
lorenz
parents: 1845
diff changeset
2514 PyObject *other, *steps_object;
1e46afbd7b54 implemented new methods in the math module.
lorenz
parents: 1845
diff changeset
2515 long int steps;
1e46afbd7b54 implemented new methods in the math module.
lorenz
parents: 1845
diff changeset
2516 double vec1_coords[VECTOR_MAX_SIZE], vec2_coords[VECTOR_MAX_SIZE];
1e46afbd7b54 implemented new methods in the math module.
lorenz
parents: 1845
diff changeset
2517 double angle, length1, length2;
1e46afbd7b54 implemented new methods in the math module.
lorenz
parents: 1845
diff changeset
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
1e46afbd7b54 implemented new methods in the math module.
lorenz
parents: 1845
diff changeset
2520 return NULL;
1e46afbd7b54 implemented new methods in the math module.
lorenz
parents: 1845
diff changeset
2521 }
1e46afbd7b54 implemented new methods in the math module.
lorenz
parents: 1845
diff changeset
2522 if (!PyInt_Check(steps_object)) {
1e46afbd7b54 implemented new methods in the math module.
lorenz
parents: 1845
diff changeset
2523 PyErr_SetString(PyExc_TypeError, "Expected Int as argument 2");
1e46afbd7b54 implemented new methods in the math module.
lorenz
parents: 1845
diff changeset
2524 return NULL;
1e46afbd7b54 implemented new methods in the math module.
lorenz
parents: 1845
diff changeset
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
1e46afbd7b54 implemented new methods in the math module.
lorenz
parents: 1845
diff changeset
2527 PyErr_BadInternalCall();
1e46afbd7b54 implemented new methods in the math module.
lorenz
parents: 1845
diff changeset
2528 return NULL;
1e46afbd7b54 implemented new methods in the math module.
lorenz
parents: 1845
diff changeset
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
1e46afbd7b54 implemented new methods in the math module.
lorenz
parents: 1845
diff changeset
2531 PyErr_SetString(PyExc_TypeError, "Expected Vector as argument 1");
1e46afbd7b54 implemented new methods in the math module.
lorenz
parents: 1845
diff changeset
2532 return NULL;
1e46afbd7b54 implemented new methods in the math module.
lorenz
parents: 1845
diff changeset
2533 }
1e46afbd7b54 implemented new methods in the math module.
lorenz
parents: 1845
diff changeset
2534
1e46afbd7b54 implemented new methods in the math module.
lorenz
parents: 1845
diff changeset
2535 it = PyObject_New(vector_slerpiter, &PyVector_SlerpIter_Type);
1e46afbd7b54 implemented new methods in the math module.
lorenz
parents: 1845
diff changeset
2536 if (it == NULL)
1e46afbd7b54 implemented new methods in the math module.
lorenz
parents: 1845
diff changeset
2537 return NULL;
1e46afbd7b54 implemented new methods in the math module.
lorenz
parents: 1845
diff changeset
2538 it->it_index = 0;
1e46afbd7b54 implemented new methods in the math module.
lorenz
parents: 1845
diff changeset
2539 it->dim = self->dim;
1e46afbd7b54 implemented new methods in the math module.
lorenz
parents: 1845
diff changeset
2540 memcpy(it->coords, vec1_coords, sizeof(vec1_coords[0]) * it->dim);
1e46afbd7b54 implemented new methods in the math module.
lorenz
parents: 1845
diff changeset
2541
1e46afbd7b54 implemented new methods in the math module.
lorenz
parents: 1845
diff changeset
2542 length1 = sqrt(_scalar_product(vec1_coords, vec1_coords, it->dim));
1e46afbd7b54 implemented new methods in the math module.
lorenz
parents: 1845
diff changeset
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
1e46afbd7b54 implemented new methods in the math module.
lorenz
parents: 1845
diff changeset
2545 PyErr_SetString(PyExc_ZeroDivisionError,
1e46afbd7b54 implemented new methods in the math module.
lorenz
parents: 1845
diff changeset
2546 "can't use slerp with Zero-Vector");
1e46afbd7b54 implemented new methods in the math module.
lorenz
parents: 1845
diff changeset
2547 Py_DECREF(it);
1e46afbd7b54 implemented new methods in the math module.
lorenz
parents: 1845
diff changeset
2548 return NULL;
1e46afbd7b54 implemented new methods in the math module.
lorenz
parents: 1845
diff changeset
2549 }
1e46afbd7b54 implemented new methods in the math module.
lorenz
parents: 1845
diff changeset
2550 angle = acos(_scalar_product(vec1_coords, vec2_coords, self->dim) /
1e46afbd7b54 implemented new methods in the math module.
lorenz
parents: 1845
diff changeset
2551 (length1 * length2));
1e46afbd7b54 implemented new methods in the math module.
lorenz
parents: 1845
diff changeset
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
1e46afbd7b54 implemented new methods in the math module.
lorenz
parents: 1845
diff changeset
2554 angle -= 2 * M_PI;
1e46afbd7b54 implemented new methods in the math module.
lorenz
parents: 1845
diff changeset
2555 it->steps = -it->steps;
1e46afbd7b54 implemented new methods in the math module.
lorenz
parents: 1845
diff changeset
2556 }
1e46afbd7b54 implemented new methods in the math module.
lorenz
parents: 1845
diff changeset
2557 angle /= it->steps;
1e46afbd7b54 implemented new methods in the math module.
lorenz
parents: 1845
diff changeset
2558
1848
bbc21cd6faeb some last minute changes before I merge to trunk.
lorenz
parents: 1847
diff changeset
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
1e46afbd7b54 implemented new methods in the math module.
lorenz
parents: 1845
diff changeset
2564 switch (self->dim) {
1e46afbd7b54 implemented new methods in the math module.
lorenz
parents: 1845
diff changeset
2565 case 2:
1e46afbd7b54 implemented new methods in the math module.
lorenz
parents: 1845
diff changeset
2566 _make_vector2_slerp_matrix(it, vec1_coords, vec2_coords, angle);
1e46afbd7b54 implemented new methods in the math module.
lorenz
parents: 1845
diff changeset
2567 break;
1e46afbd7b54 implemented new methods in the math module.
lorenz
parents: 1845
diff changeset
2568 case 3:
1e46afbd7b54 implemented new methods in the math module.
lorenz
parents: 1845
diff changeset
2569 _make_vector3_slerp_matrix(it, vec1_coords, vec2_coords, angle);
1e46afbd7b54 implemented new methods in the math module.
lorenz
parents: 1845
diff changeset
2570 break;
1e46afbd7b54 implemented new methods in the math module.
lorenz
parents: 1845
diff changeset
2571 default:
1e46afbd7b54 implemented new methods in the math module.
lorenz
parents: 1845
diff changeset
2572 PyErr_BadInternalCall();
1e46afbd7b54 implemented new methods in the math module.
lorenz
parents: 1845
diff changeset
2573 Py_DECREF(it);
1e46afbd7b54 implemented new methods in the math module.
lorenz
parents: 1845
diff changeset
2574 return NULL;
1e46afbd7b54 implemented new methods in the math module.
lorenz
parents: 1845
diff changeset
2575 }
1e46afbd7b54 implemented new methods in the math module.
lorenz
parents: 1845
diff changeset
2576 return (PyObject *)it;
1e46afbd7b54 implemented new methods in the math module.
lorenz
parents: 1845
diff changeset
2577 }
1e46afbd7b54 implemented new methods in the math module.
lorenz
parents: 1845
diff changeset
2578
1e46afbd7b54 implemented new methods in the math module.
lorenz
parents: 1845
diff changeset
2579 static void
1e46afbd7b54 implemented new methods in the math module.
lorenz
parents: 1845
diff changeset
2580 _make_vector2_slerp_matrix(vector_slerpiter *it,
1e46afbd7b54 implemented new methods in the math module.
lorenz
parents: 1845
diff changeset
2581 const double *vec1_coords, const double *vec2_coords,
1e46afbd7b54 implemented new methods in the math module.
lorenz
parents: 1845
diff changeset
2582 double angle)
1e46afbd7b54 implemented new methods in the math module.
lorenz
parents: 1845
diff changeset
2583 {
1e46afbd7b54 implemented new methods in the math module.
lorenz
parents: 1845
diff changeset
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
1e46afbd7b54 implemented new methods in the math module.
lorenz
parents: 1845
diff changeset
2587 cos_value = cos(angle);
1e46afbd7b54 implemented new methods in the math module.
lorenz
parents: 1845
diff changeset
2588 sin_value = sin(angle);
1e46afbd7b54 implemented new methods in the math module.
lorenz
parents: 1845
diff changeset
2589 it->matrix[0][0] = cos_value;
1e46afbd7b54 implemented new methods in the math module.
lorenz
parents: 1845
diff changeset
2590 it->matrix[0][1] = sin_value;
1e46afbd7b54 implemented new methods in the math module.
lorenz
parents: 1845
diff changeset
2591 it->matrix[1][0] = -sin_value;
1e46afbd7b54 implemented new methods in the math module.
lorenz
parents: 1845
diff changeset
2592 it->matrix[1][1] = cos_value;
1e46afbd7b54 implemented new methods in the math module.
lorenz
parents: 1845
diff changeset
2593 }
1e46afbd7b54 implemented new methods in the math module.
lorenz
parents: 1845
diff changeset
2594
1e46afbd7b54 implemented new methods in the math module.
lorenz
parents: 1845
diff changeset
2595 static void
1e46afbd7b54 implemented new methods in the math module.
lorenz
parents: 1845
diff changeset
2596 _make_vector3_slerp_matrix(vector_slerpiter *it,
1e46afbd7b54 implemented new methods in the math module.
lorenz
parents: 1845
diff changeset
2597 const double *vec1_coords, const double *vec2_coords,
1e46afbd7b54 implemented new methods in the math module.
lorenz
parents: 1845
diff changeset
2598 double angle)
1e46afbd7b54 implemented new methods in the math module.
lorenz
parents: 1845
diff changeset
2599 {
1e46afbd7b54 implemented new methods in the math module.
lorenz
parents: 1845
diff changeset
2600 int i;
1e46afbd7b54 implemented new methods in the math module.
lorenz
parents: 1845
diff changeset
2601 double axis[3];
1e46afbd7b54 implemented new methods in the math module.
lorenz
parents: 1845
diff changeset
2602 double norm_factor, sin_value, cos_value, cos_complement;
1e46afbd7b54 implemented new methods in the math module.
lorenz
parents: 1845
diff changeset
2603
1e46afbd7b54 implemented new methods in the math module.
lorenz
parents: 1845
diff changeset
2604 /* calculate rotation axis via cross-product */
1e46afbd7b54 implemented new methods in the math module.
lorenz
parents: 1845
diff changeset
2605 for (i = 0; i < 3; ++i)
1e46afbd7b54 implemented new methods in the math module.
lorenz
parents: 1845
diff changeset
2606 axis[i] = ((vec1_coords[(i+1)%3] * vec2_coords[(i+2)%3]) -
1e46afbd7b54 implemented new methods in the math module.
lorenz
parents: 1845
diff changeset
2607 (vec1_coords[(i+2)%3] * vec2_coords[(i+1)%3]));
1e46afbd7b54 implemented new methods in the math module.
lorenz
parents: 1845
diff changeset
2608 /* normalize the rotation axis */
1e46afbd7b54 implemented new methods in the math module.
lorenz
parents: 1845
diff changeset
2609 norm_factor = 1. / sqrt(_scalar_product(axis, axis, 3));
1e46afbd7b54 implemented new methods in the math module.
lorenz
parents: 1845
diff changeset
2610 for (i = 0; i < 3; ++i)
1e46afbd7b54 implemented new methods in the math module.
lorenz
parents: 1845
diff changeset
2611 axis[i] *= norm_factor;
1e46afbd7b54 implemented new methods in the math module.
lorenz
parents: 1845
diff changeset
2612
1e46afbd7b54 implemented new methods in the math module.
lorenz
parents: 1845
diff changeset
2613 sin_value = sin(angle);
1e46afbd7b54 implemented new methods in the math module.
lorenz
parents: 1845
diff changeset
2614 cos_value = cos(angle);
1e46afbd7b54 implemented new methods in the math module.
lorenz
parents: 1845
diff changeset
2615 cos_complement = 1 - cos_value;
1e46afbd7b54 implemented new methods in the math module.
lorenz
parents: 1845
diff changeset
2616 /* calculate the rotation matrix */
1e46afbd7b54 implemented new methods in the math module.
lorenz
parents: 1845
diff changeset
2617 it->matrix[0][0] = cos_value + axis[0] * axis[0] * cos_complement;
1e46afbd7b54 implemented new methods in the math module.
lorenz
parents: 1845
diff changeset
2618 it->matrix[0][1] = axis[0] * axis[1] * cos_complement + axis[2] * sin_value;
1e46afbd7b54 implemented new methods in the math module.
lorenz
parents: 1845
diff changeset
2619 it->matrix[0][2] = axis[0] * axis[2] * cos_complement - axis[1] * sin_value;
1e46afbd7b54 implemented new methods in the math module.
lorenz
parents: 1845
diff changeset
2620 it->matrix[1][0] = axis[0] * axis[1] * cos_complement - axis[2] * sin_value;
1e46afbd7b54 implemented new methods in the math module.
lorenz
parents: 1845
diff changeset
2621 it->matrix[1][1] = cos_value + axis[1] * axis[1] * cos_complement;
1e46afbd7b54 implemented new methods in the math module.
lorenz
parents: 1845
diff changeset
2622 it->matrix[1][2] = axis[1] * axis[2] * cos_complement + axis[0] * sin_value;
1e46afbd7b54 implemented new methods in the math module.
lorenz
parents: 1845
diff changeset
2623 it->matrix[2][0] = axis[0] * axis[2] * cos_complement + axis[1] * sin_value;
1e46afbd7b54 implemented new methods in the math module.
lorenz
parents: 1845
diff changeset
2624 it->matrix[2][1] = axis[1] * axis[2] * cos_complement - axis[0] * sin_value;
1e46afbd7b54 implemented new methods in the math module.
lorenz
parents: 1845
diff changeset
2625 it->matrix[2][2] = cos_value + axis[2] * axis[2] * cos_complement;
1e46afbd7b54 implemented new methods in the math module.
lorenz
parents: 1845
diff changeset
2626 }
1841
a7dc31e50c46 * added the vetor_elementwiseproxy which lets you apply calculations on
lorenz
parents: 1840
diff changeset
2627
1848
bbc21cd6faeb some last minute changes before I merge to trunk.
lorenz
parents: 1847
diff changeset
2628 /*
bbc21cd6faeb some last minute changes before I merge to trunk.
lorenz
parents: 1847
diff changeset
2629 static PyObject *
bbc21cd6faeb some last minute changes before I merge to trunk.
lorenz
parents: 1847
diff changeset
2630 vector2_slerp(PyVector *self, PyObject *args)
bbc21cd6faeb some last minute changes before I merge to trunk.
lorenz
parents: 1847
diff changeset
2631 {
bbc21cd6faeb some last minute changes before I merge to trunk.
lorenz
parents: 1847
diff changeset
2632 PyObject *other;
bbc21cd6faeb some last minute changes before I merge to trunk.
lorenz
parents: 1847
diff changeset
2633 PyVector *ret;
bbc21cd6faeb some last minute changes before I merge to trunk.
lorenz
parents: 1847
diff changeset
2634 double *other_coords;
bbc21cd6faeb some last minute changes before I merge to trunk.
lorenz
parents: 1847
diff changeset
2635 double t;
bbc21cd6faeb some last minute changes before I merge to trunk.
lorenz
parents: 1847
diff changeset
2636 if (!PyArg_ParseTuple("Od:slerp", &other, &t)) {
bbc21cd6faeb some last minute changes before I merge to trunk.
lorenz
parents: 1847
diff changeset
2637 return NULL;
bbc21cd6faeb some last minute changes before I merge to trunk.
lorenz
parents: 1847
diff changeset
2638 }
bbc21cd6faeb some last minute changes before I merge to trunk.
lorenz
parents: 1847
diff changeset
2639 if (!PyVectorCompatible_Check(end_vector, self->dim)) {
bbc21cd6faeb some last minute changes before I merge to trunk.
lorenz
parents: 1847
diff changeset
2640 PyErr_SetString(PyExc_TypeError, "Argument 1 must be a vector.");
bbc21cd6faeb some last minute changes before I merge to trunk.
lorenz
parents: 1847
diff changeset
2641 return NULL;
bbc21cd6faeb some last minute changes before I merge to trunk.
lorenz
parents: 1847
diff changeset
2642 }
bbc21cd6faeb some last minute changes before I merge to trunk.
lorenz
parents: 1847
diff changeset
2643 if (!PySequence_AsVectorCoords(other, other_coords, self->dim)) {
bbc21cd6faeb some last minute changes before I merge to trunk.
lorenz
parents: 1847
diff changeset
2644 PyErr_SetString(PyExc_TypeError, "Argument 1 must be a vector.");
bbc21cd6faeb some last minute changes before I merge to trunk.
lorenz
parents: 1847
diff changeset
2645 return NULL;
bbc21cd6faeb some last minute changes before I merge to trunk.
lorenz
parents: 1847
diff changeset
2646 }
bbc21cd6faeb some last minute changes before I merge to trunk.
lorenz
parents: 1847
diff changeset
2647 if (fabs(t) > 1) {
bbc21cd6faeb some last minute changes before I merge to trunk.
lorenz
parents: 1847
diff changeset
2648 PyErr_SetString(PyExc_ValueError, "Argument 2 must be in range [-1, 1].");
bbc21cd6faeb some last minute changes before I merge to trunk.
lorenz
parents: 1847
diff changeset
2649 return NULL;
bbc21cd6faeb some last minute changes before I merge to trunk.
lorenz
parents: 1847
diff changeset
2650 }
bbc21cd6faeb some last minute changes before I merge to trunk.
lorenz
parents: 1847
diff changeset
2651
bbc21cd6faeb some last minute changes before I merge to trunk.
lorenz
parents: 1847
diff changeset
2652 length1 = sqrt(_scalar_product(self->coords, self->coords, self->dim));
bbc21cd6faeb some last minute changes before I merge to trunk.
lorenz
parents: 1847
diff changeset
2653 length2 = sqrt(_scalar_product(other_coords, other_coords, self->dim));
bbc21cd6faeb some last minute changes before I merge to trunk.
lorenz
parents: 1847
diff changeset
2654 if ((length1 < self->epsilon) || (length2 < self->epsilon)) {
bbc21cd6faeb some last minute changes before I merge to trunk.
lorenz
parents: 1847
diff changeset
2655 PyErr_SetString(PyExc_ZeroDivisionError,
bbc21cd6faeb some last minute changes before I merge to trunk.
lorenz
parents: 1847
diff changeset
2656 "can't use slerp with Zero-Vector");
bbc21cd6faeb some last minute changes before I merge to trunk.
lorenz
parents: 1847
diff changeset
2657 return NULL;
bbc21cd6faeb some last minute changes before I merge to trunk.
lorenz
parents: 1847
diff changeset
2658 }
bbc21cd6faeb some last minute changes before I merge to trunk.
lorenz
parents: 1847
diff changeset
2659 angle = acos(_scalar_product(self->coords, other_coords, self->dim) /
bbc21cd6faeb some last minute changes before I merge to trunk.
lorenz
parents: 1847
diff changeset
2660 (length1 * length2));
bbc21cd6faeb some last minute changes before I merge to trunk.
lorenz
parents: 1847
diff changeset
2661 if (t < 0) {
bbc21cd6faeb some last minute changes before I merge to trunk.
lorenz
parents: 1847
diff changeset
2662 angle -= 2 * M_PI;
bbc21cd6faeb some last minute changes before I merge to trunk.
lorenz
parents: 1847
diff changeset
2663 t = -t;
bbc21cd6faeb some last minute changes before I merge to trunk.
lorenz
parents: 1847
diff changeset
2664 }
bbc21cd6faeb some last minute changes before I merge to trunk.
lorenz
parents: 1847
diff changeset
2665 angle *= t;
bbc21cd6faeb some last minute changes before I merge to trunk.
lorenz
parents: 1847
diff changeset
2666 if (vec1_coords[0] * vec2_coords[1] < vec1_coords[1] * vec2_coords[0])
bbc21cd6faeb some last minute changes before I merge to trunk.
lorenz
parents: 1847
diff changeset
2667 angle *= -1;
bbc21cd6faeb some last minute changes before I merge to trunk.
lorenz
parents: 1847
diff changeset
2668
bbc21cd6faeb some last minute changes before I merge to trunk.
lorenz
parents: 1847
diff changeset
2669 if (fabs(length1 - length2) > self->epsilon)
bbc21cd6faeb some last minute changes before I merge to trunk.
lorenz
parents: 1847
diff changeset
2670 it->radial_factor = t * length2 / length1;
bbc21cd6faeb some last minute changes before I merge to trunk.
lorenz
parents: 1847
diff changeset
2671 else
bbc21cd6faeb some last minute changes before I merge to trunk.
lorenz
parents: 1847
diff changeset
2672 it->radial_factor = 1;
bbc21cd6faeb some last minute changes before I merge to trunk.
lorenz
parents: 1847
diff changeset
2673
bbc21cd6faeb some last minute changes before I merge to trunk.
lorenz
parents: 1847
diff changeset
2674 ret = PyVector_NEW(self->dim);
bbc21cd6faeb some last minute changes before I merge to trunk.
lorenz
parents: 1847
diff changeset
2675 cos_value = cos(angle);
bbc21cd6faeb some last minute changes before I merge to trunk.
lorenz
parents: 1847
diff changeset
2676 sin_value = sin(angle);
bbc21cd6faeb some last minute changes before I merge to trunk.
lorenz
parents: 1847
diff changeset
2677 ret->coords[0] = cos_value * self->coords[0] - sin_value * self->coords[1];
bbc21cd6faeb some last minute changes before I merge to trunk.
lorenz
parents: 1847
diff changeset
2678 ret->coords[1] = sin_value * self->coords[0] + cos_value * self->coords[1];
bbc21cd6faeb some last minute changes before I merge to trunk.
lorenz
parents: 1847
diff changeset
2679 n1 = 1. / length1;
bbc21cd6faeb some last minute changes before I merge to trunk.
lorenz
parents: 1847
diff changeset
2680 n2 = 1. / length2;
bbc21cd6faeb some last minute changes before I merge to trunk.
lorenz
parents: 1847
diff changeset
2681 s0 = 1. / sin(angle);
bbc21cd6faeb some last minute changes before I merge to trunk.
lorenz
parents: 1847
diff changeset
2682 s1 = sin(angle * t);
bbc21cd6faeb some last minute changes before I merge to trunk.
lorenz
parents: 1847
diff changeset
2683 s2 = sin(angle * (1-t));
bbc21cd6faeb some last minute changes before I merge to trunk.
lorenz
parents: 1847
diff changeset
2684 for (i = 0; i < self->dim; ++i)
bbc21cd6faeb some last minute changes before I merge to trunk.
lorenz
parents: 1847
diff changeset
2685 ret->coords[i] = (self->coords[i] * s1 + other_coords[i] * s2) * s0;
bbc21cd6faeb some last minute changes before I merge to trunk.
lorenz
parents: 1847
diff changeset
2686 return ret;
bbc21cd6faeb some last minute changes before I merge to trunk.
lorenz
parents: 1847
diff changeset
2687 }
bbc21cd6faeb some last minute changes before I merge to trunk.
lorenz
parents: 1847
diff changeset
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
bbc21cd6faeb some last minute changes before I merge to trunk.
lorenz
parents: 1847
diff changeset
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
bbc21cd6faeb some last minute changes before I merge to trunk.
lorenz
parents: 1847
diff changeset
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
bbc21cd6faeb some last minute changes before I merge to trunk.
lorenz
parents: 1847
diff changeset
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
bbc21cd6faeb some last minute changes before I merge to trunk.
lorenz
parents: 1847
diff changeset
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
bbc21cd6faeb some last minute changes before I merge to trunk.
lorenz
parents: 1847
diff changeset
2980 dim = vec->dim;
bbc21cd6faeb some last minute changes before I merge to trunk.
lorenz
parents: 1847
diff changeset
2981
bbc21cd6faeb some last minute changes before I merge to trunk.
lorenz
parents: 1847
diff changeset
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
bbc21cd6faeb some last minute changes before I merge to trunk.
lorenz
parents: 1847
diff changeset
2984 op |= OP_ARG_VECTOR;
1841
a7dc31e50c46 * added the vetor_elementwiseproxy which lets you apply calculations on
lorenz
parents: 1840
diff changeset
2985 }
1848
bbc21cd6faeb some last minute changes before I merge to trunk.
lorenz
parents: 1847
diff changeset
2986 else if (PyVectorCompatible_Check(other, dim))
bbc21cd6faeb some last minute changes before I merge to trunk.
lorenz
parents: 1847
diff changeset
2987 op |= OP_ARG_VECTOR;
bbc21cd6faeb some last minute changes before I merge to trunk.
lorenz
parents: 1847
diff changeset
2988 else if (RealNumber_Check(other))
bbc21cd6faeb some last minute changes before I merge to trunk.
lorenz
parents: 1847
diff changeset
2989 op |= OP_ARG_NUMBER;
bbc21cd6faeb some last minute changes before I merge to trunk.
lorenz
parents: 1847
diff changeset
2990 else
bbc21cd6faeb some last minute changes before I merge to trunk.
lorenz
parents: 1847
diff changeset
2991 op |= OP_ARG_UNKNOWN;
bbc21cd6faeb some last minute changes before I merge to trunk.
lorenz
parents: 1847
diff changeset
2992
bbc21cd6faeb some last minute changes before I merge to trunk.
lorenz
parents: 1847
diff changeset
2993 switch (op) {
bbc21cd6faeb some last minute changes before I merge to trunk.
lorenz
parents: 1847
diff changeset
2994 case OP_ADD | OP_ARG_VECTOR:
bbc21cd6faeb some last minute changes before I merge to trunk.
lorenz
parents: 1847
diff changeset
2995 case OP_ADD | OP_ARG_VECTOR | OP_ARG_REVERSE:
bbc21cd6faeb some last minute changes before I merge to trunk.
lorenz
parents: 1847
diff changeset
2996 return vector_add((PyObject*)vec, other);
bbc21cd6faeb some last minute changes before I merge to trunk.
lorenz
parents: 1847
diff changeset
2997 case OP_ADD | OP_ARG_NUMBER:
bbc21cd6faeb some last minute changes before I merge to trunk.
lorenz
parents: 1847
diff changeset
2998 case OP_ADD | OP_ARG_NUMBER | OP_ARG_REVERSE:
bbc21cd6faeb some last minute changes before I merge to trunk.
lorenz
parents: 1847
diff changeset
2999 tmp = PyFloat_AsDouble(other);
bbc21cd6faeb some last minute changes before I merge to trunk.
lorenz
parents: 1847
diff changeset
3000 ret = (PyVector*)PyVector_NEW(dim);
bbc21cd6faeb some last minute changes before I merge to trunk.
lorenz
parents: 1847
diff changeset
3001 for (i = 0; i < dim; i++)
bbc21cd6faeb some last minute changes before I merge to trunk.
lorenz
parents: 1847
diff changeset
3002 ret->coords[i] = vec->coords[i] + tmp;
bbc21cd6faeb some last minute changes before I merge to trunk.
lorenz
parents: 1847
diff changeset
3003 return (PyObject*)ret;
bbc21cd6faeb some last minute changes before I merge to trunk.
lorenz
parents: 1847
diff changeset
3004 case OP_SUB | OP_ARG_VECTOR:
bbc21cd6faeb some last minute changes before I merge to trunk.
lorenz
parents: 1847
diff changeset
3005 return vector_sub((PyObject*)vec, other);
bbc21cd6faeb some last minute changes before I merge to trunk.
lorenz
parents: 1847
diff changeset
3006 case OP_SUB | OP_ARG_VECTOR | OP_ARG_REVERSE:
bbc21cd6faeb some last minute changes before I merge to trunk.
lorenz
parents: 1847
diff changeset
3007 return vector_sub(other, (PyObject*)vec);
bbc21cd6faeb some last minute changes before I merge to trunk.
lorenz
parents: 1847
diff changeset
3008 case OP_SUB | OP_ARG_NUMBER:
bbc21cd6faeb some last minute changes before I merge to trunk.
lorenz
parents: 1847
diff changeset
3009 tmp = PyFloat_AsDouble(other);
bbc21cd6faeb some last minute changes before I merge to trunk.
lorenz
parents: 1847
diff changeset
3010 ret = (PyVector*)PyVector_NEW(dim);
bbc21cd6faeb some last minute changes before I merge to trunk.
lorenz
parents: 1847
diff changeset
3011 for (i = 0; i < dim; i++)
bbc21cd6faeb some last minute changes before I merge to trunk.
lorenz
parents: 1847
diff changeset
3012 ret->coords[i] = vec->coords[i] - tmp;
bbc21cd6faeb some last minute changes before I merge to trunk.
lorenz
parents: 1847
diff changeset
3013 return (PyObject*)ret;
bbc21cd6faeb some last minute changes before I merge to trunk.
lorenz
parents: 1847
diff changeset
3014 case OP_SUB | OP_ARG_NUMBER | OP_ARG_REVERSE:
bbc21cd6faeb some last minute changes before I merge to trunk.
lorenz
parents: 1847
diff changeset
3015 tmp = PyFloat_AsDouble(other);
bbc21cd6faeb some last minute changes before I merge to trunk.
lorenz
parents: 1847
diff changeset
3016 ret = (PyVector*)PyVector_NEW(dim);
bbc21cd6faeb some last minute changes before I merge to trunk.
lorenz
parents: 1847
diff changeset
3017 for (i = 0; i < dim; i++)
bbc21cd6faeb some last minute changes before I merge to trunk.
lorenz
parents: 1847
diff changeset
3018 ret->coords[i] = tmp - vec->coords[i];
bbc21cd6faeb some last minute changes before I merge to trunk.
lorenz
parents: 1847
diff changeset
3019 return (PyObject*)ret;
bbc21cd6faeb some last minute changes before I merge to trunk.
lorenz
parents: 1847
diff changeset
3020 case OP_MUL | OP_ARG_VECTOR:
bbc21cd6faeb some last minute changes before I merge to trunk.
lorenz
parents: 1847
diff changeset
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
bbc21cd6faeb some last minute changes before I merge to trunk.
lorenz
parents: 1847
diff changeset
3028 case OP_MUL | OP_ARG_NUMBER:
bbc21cd6faeb some last minute changes before I merge to trunk.
lorenz
parents: 1847
diff changeset
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
bbc21cd6faeb some last minute changes before I merge to trunk.
lorenz
parents: 1847
diff changeset
3031 case OP_DIV | OP_ARG_VECTOR:
bbc21cd6faeb some last minute changes before I merge to trunk.
lorenz
parents: 1847
diff changeset
3032 ret = (PyVector*)PyVector_NEW(vec->dim);
bbc21cd6faeb some last minute changes before I merge to trunk.
lorenz
parents: 1847
diff changeset
3033 for (i = 0; i < vec->dim; i++) {
bbc21cd6faeb some last minute changes before I merge to trunk.
lorenz
parents: 1847
diff changeset
3034 ret->coords[i] = (vec->coords[i] /
bbc21cd6faeb some last minute changes before I merge to trunk.
lorenz
parents: 1847
diff changeset
3035 PySequence_GetItem_AsDouble(other, i));
bbc21cd6faeb some last minute changes before I merge to trunk.
lorenz
parents: 1847
diff changeset
3036 }
bbc21cd6faeb some last minute changes before I merge to trunk.
lorenz
parents: 1847
diff changeset
3037 return (PyObject*)ret;
bbc21cd6faeb some last minute changes before I merge to trunk.
lorenz
parents: 1847
diff changeset
3038 case OP_DIV | OP_ARG_VECTOR | OP_ARG_REVERSE:
bbc21cd6faeb some last minute changes before I merge to trunk.
lorenz
parents: 1847
diff changeset
3039 ret = (PyVector*)PyVector_NEW(vec->dim);
bbc21cd6faeb some last minute changes before I merge to trunk.
lorenz
parents: 1847
diff changeset
3040 for (i = 0; i < vec->dim; i++) {
bbc21cd6faeb some last minute changes before I merge to trunk.
lorenz
parents: 1847
diff changeset
3041 ret->coords[i] = (PySequence_GetItem_AsDouble(o1, i) /
bbc21cd6faeb some last minute changes before I merge to trunk.
lorenz
parents: 1847
diff changeset
3042 vec->coords[i]);
bbc21cd6faeb some last minute changes before I merge to trunk.
lorenz
parents: 1847
diff changeset
3043 }
bbc21cd6faeb some last minute changes before I merge to trunk.
lorenz
parents: 1847
diff changeset
3044 return (PyObject*)ret;
bbc21cd6faeb some last minute changes before I merge to trunk.
lorenz
parents: 1847
diff changeset
3045 case OP_DIV | OP_ARG_NUMBER:
bbc21cd6faeb some last minute changes before I merge to trunk.
lorenz
parents: 1847
diff changeset
3046 return vector_div(vec, other);
bbc21cd6faeb some last minute changes before I merge to trunk.
lorenz
parents: 1847
diff changeset
3047 case OP_DIV | OP_ARG_NUMBER | OP_ARG_REVERSE:
bbc21cd6faeb some last minute changes before I merge to trunk.
lorenz
parents: 1847
diff changeset
3048 tmp = PyFloat_AsDouble(o1);
bbc21cd6faeb some last minute changes before I merge to trunk.
lorenz
parents: 1847
diff changeset
3049 ret = (PyVector*)PyVector_NEW(vec->dim);
bbc21cd6faeb some last minute changes before I merge to trunk.
lorenz
parents: 1847
diff changeset
3050 for (i = 0; i < vec->dim; i++)
bbc21cd6faeb some last minute changes before I merge to trunk.
lorenz
parents: 1847
diff changeset
3051 ret->coords[i] = tmp / vec->coords[i];
bbc21cd6faeb some last minute changes before I merge to trunk.
lorenz
parents: 1847
diff changeset
3052 return (PyObject*)ret;
bbc21cd6faeb some last minute changes before I merge to trunk.
lorenz
parents: 1847
diff changeset
3053 case OP_FLOOR_DIV | OP_ARG_VECTOR:
bbc21cd6faeb some last minute changes before I merge to trunk.
lorenz
parents: 1847
diff changeset
3054 ret = (PyVector*)PyVector_NEW(vec->dim);
bbc21cd6faeb some last minute changes before I merge to trunk.
lorenz
parents: 1847
diff changeset
3055 for (i = 0; i < vec->dim; i++) {
bbc21cd6faeb some last minute changes before I merge to trunk.
lorenz
parents: 1847
diff changeset
3056 ret->coords[i] = floor(vec->coords[i] /
bbc21cd6faeb some last minute changes before I merge to trunk.
lorenz
parents: 1847
diff changeset
3057 PySequence_GetItem_AsDouble(other, i));
bbc21cd6faeb some last minute changes before I merge to trunk.
lorenz
parents: 1847
diff changeset
3058 }
bbc21cd6faeb some last minute changes before I merge to trunk.
lorenz
parents: 1847
diff changeset
3059 return (PyObject*)ret;
bbc21cd6faeb some last minute changes before I merge to trunk.
lorenz
parents: 1847
diff changeset
3060 case OP_FLOOR_DIV | OP_ARG_VECTOR | OP_ARG_REVERSE:
bbc21cd6faeb some last minute changes before I merge to trunk.
lorenz
parents: 1847
diff changeset
3061 ret = (PyVector*)PyVector_NEW(vec->dim);
bbc21cd6faeb some last minute changes before I merge to trunk.
lorenz
parents: 1847
diff changeset
3062 for (i = 0; i < vec->dim; i++) {
bbc21cd6faeb some last minute changes before I merge to trunk.
lorenz
parents: 1847
diff changeset
3063 ret->coords[i] = floor(PySequence_GetItem_AsDouble(o1, i) /
bbc21cd6faeb some last minute changes before I merge to trunk.
lorenz
parents: 1847
diff changeset
3064 vec->coords[i]);
bbc21cd6faeb some last minute changes before I merge to trunk.
lorenz
parents: 1847
diff changeset
3065 }
bbc21cd6faeb some last minute changes before I merge to trunk.
lorenz
parents: 1847
diff changeset
3066 return (PyObject*)ret;
bbc21cd6faeb some last minute changes before I merge to trunk.
lorenz
parents: 1847
diff changeset
3067 case OP_FLOOR_DIV | OP_ARG_NUMBER:
bbc21cd6faeb some last minute changes before I merge to trunk.
lorenz
parents: 1847
diff changeset
3068 return vector_floor_div(vec, other);
bbc21cd6faeb some last minute changes before I merge to trunk.
lorenz
parents: 1847
diff changeset
3069 case OP_FLOOR_DIV | OP_ARG_NUMBER | OP_ARG_REVERSE:
bbc21cd6faeb some last minute changes before I merge to trunk.
lorenz
parents: 1847
diff changeset
3070 tmp = PyFloat_AsDouble(other);
bbc21cd6faeb some last minute changes before I merge to trunk.
lorenz
parents: 1847
diff changeset
3071 ret = (PyVector*)PyVector_NEW(vec->dim);
bbc21cd6faeb some last minute changes before I merge to trunk.
lorenz
parents: 1847
diff changeset
3072 for (i = 0; i < vec->dim; i++)
bbc21cd6faeb some last minute changes before I merge to trunk.
lorenz
parents: 1847
diff changeset
3073 ret->coords[i] = floor(tmp / vec->coords[i]);
bbc21cd6faeb some last minute changes before I merge to trunk.
lorenz
parents: 1847
diff changeset
3074 return (PyObject*)ret;
bbc21cd6faeb some last minute changes before I merge to trunk.
lorenz
parents: 1847
diff changeset
3075 case OP_MOD | OP_ARG_VECTOR:
bbc21cd6faeb some last minute changes before I merge to trunk.
lorenz
parents: 1847
diff changeset
3076 ret = (PyVector*)PyVector_NEW(vec->dim);
bbc21cd6faeb some last minute changes before I merge to trunk.
lorenz
parents: 1847
diff changeset
3077 for (i = 0; i < vec->dim; i++) {
bbc21cd6faeb some last minute changes before I merge to trunk.
lorenz
parents: 1847
diff changeset
3078 tmp = PySequence_GetItem_AsDouble(other, i);
bbc21cd6faeb some last minute changes before I merge to trunk.
lorenz
parents: 1847
diff changeset
3079 mod = fmod(vec->coords[i], tmp);
bbc21cd6faeb some last minute changes before I merge to trunk.
lorenz
parents: 1847
diff changeset
3080 /* note: checking mod*value < 0 is incorrect -- underflows to
bbc21cd6faeb some last minute changes before I merge to trunk.
lorenz
parents: 1847
diff changeset
3081 0 if value < sqrt(smallest nonzero double) */
bbc21cd6faeb some last minute changes before I merge to trunk.
lorenz
parents: 1847
diff changeset
3082 if (mod && ((tmp < 0) != (mod < 0))) {
bbc21cd6faeb some last minute changes before I merge to trunk.
lorenz
parents: 1847
diff changeset
3083 mod += tmp;
bbc21cd6faeb some last minute changes before I merge to trunk.
lorenz
parents: 1847
diff changeset
3084 }
bbc21cd6faeb some last minute changes before I merge to trunk.
lorenz
parents: 1847
diff changeset
3085 ret->coords[i] = mod;
bbc21cd6faeb some last minute changes before I merge to trunk.
lorenz
parents: 1847
diff changeset
3086 }
bbc21cd6faeb some last minute changes before I merge to trunk.
lorenz
parents: 1847
diff changeset
3087 return (PyObject*)ret;
bbc21cd6faeb some last minute changes before I merge to trunk.
lorenz
parents: 1847
diff changeset
3088 case OP_MOD | OP_ARG_VECTOR | OP_ARG_REVERSE:
bbc21cd6faeb some last minute changes before I merge to trunk.
lorenz
parents: 1847
diff changeset
3089 ret = (PyVector*)PyVector_NEW(vec->dim);
bbc21cd6faeb some last minute changes before I merge to trunk.
lorenz
parents: 1847
diff changeset
3090 for (i = 0; i < vec->dim; i++) {
bbc21cd6faeb some last minute changes before I merge to trunk.
lorenz
parents: 1847
diff changeset
3091 tmp = PySequence_GetItem_AsDouble(other, i);
bbc21cd6faeb some last minute changes before I merge to trunk.
lorenz
parents: 1847
diff changeset
3092 mod = fmod(tmp, vec->coords[i]);
bbc21cd6faeb some last minute changes before I merge to trunk.
lorenz
parents: 1847
diff changeset
3093 /* note: see above */
bbc21cd6faeb some last minute changes before I merge to trunk.
lorenz
parents: 1847
diff changeset
3094 if (mod && ((vec->coords[i] < 0) != (mod < 0))) {
bbc21cd6faeb some last minute changes before I merge to trunk.
lorenz
parents: 1847
diff changeset
3095 mod += vec->coords[i];
bbc21cd6faeb some last minute changes before I merge to trunk.
lorenz
parents: 1847
diff changeset
3096 }
bbc21cd6faeb some last minute changes before I merge to trunk.
lorenz
parents: 1847
diff changeset
3097 ret->coords[i] = mod;
bbc21cd6faeb some last minute changes before I merge to trunk.
lorenz
parents: 1847
diff changeset
3098 }
bbc21cd6faeb some last minute changes before I merge to trunk.
lorenz
parents: 1847
diff changeset
3099 return (PyObject*)ret;
bbc21cd6faeb some last minute changes before I merge to trunk.
lorenz
parents: 1847
diff changeset
3100 case OP_MOD | OP_ARG_NUMBER:
bbc21cd6faeb some last minute changes before I merge to trunk.
lorenz
parents: 1847
diff changeset
3101 ret = (PyVector*)PyVector_NEW(vec->dim);
bbc21cd6faeb some last minute changes before I merge to trunk.
lorenz
parents: 1847
diff changeset
3102 tmp = PyFloat_AsDouble(other);
bbc21cd6faeb some last minute changes before I merge to trunk.
lorenz
parents: 1847
diff changeset
3103 for (i = 0; i < vec->dim; i++) {
bbc21cd6faeb some last minute changes before I merge to trunk.
lorenz
parents: 1847
diff changeset
3104 mod = fmod(vec->coords[i], tmp);
bbc21cd6faeb some last minute changes before I merge to trunk.
lorenz
parents: 1847
diff changeset
3105 /* note: see above */
bbc21cd6faeb some last minute changes before I merge to trunk.
lorenz
parents: 1847
diff changeset
3106 if (mod && ((tmp < 0) != (mod < 0))) {
bbc21cd6faeb some last minute changes before I merge to trunk.
lorenz
parents: 1847
diff changeset
3107 mod += tmp;
bbc21cd6faeb some last minute changes before I merge to trunk.
lorenz
parents: 1847
diff changeset
3108 }
bbc21cd6faeb some last minute changes before I merge to trunk.
lorenz
parents: 1847
diff changeset
3109 ret->coords[i] = mod;
bbc21cd6faeb some last minute changes before I merge to trunk.
lorenz
parents: 1847
diff changeset
3110 }
bbc21cd6faeb some last minute changes before I merge to trunk.
lorenz
parents: 1847
diff changeset
3111 return (PyObject*)ret;
bbc21cd6faeb some last minute changes before I merge to trunk.
lorenz
parents: 1847
diff changeset
3112 case OP_MOD | OP_ARG_NUMBER | OP_ARG_REVERSE:
bbc21cd6faeb some last minute changes before I merge to trunk.
lorenz
parents: 1847
diff changeset
3113 ret = (PyVector*)PyVector_NEW(vec->dim);
bbc21cd6faeb some last minute changes before I merge to trunk.
lorenz
parents: 1847
diff changeset
3114 tmp = PyFloat_AsDouble(other);
bbc21cd6faeb some last minute changes before I merge to trunk.
lorenz
parents: 1847
diff changeset
3115 for (i = 0; i < vec->dim; i++) {
bbc21cd6faeb some last minute changes before I merge to trunk.
lorenz
parents: 1847
diff changeset
3116 mod = fmod(tmp, vec->coords[i]);
bbc21cd6faeb some last minute changes before I merge to trunk.
lorenz
parents: 1847
diff changeset
3117 /* note: see above */
bbc21cd6faeb some last minute changes before I merge to trunk.
lorenz
parents: 1847
diff changeset
3118 if (mod && ((vec->coords[i] < 0) != (mod < 0))) {
bbc21cd6faeb some last minute changes before I merge to trunk.
lorenz
parents: 1847
diff changeset
3119 mod += vec->coords[i];
bbc21cd6faeb some last minute changes before I merge to trunk.
lorenz
parents: 1847
diff changeset
3120 }
bbc21cd6faeb some last minute changes before I merge to trunk.
lorenz
parents: 1847
diff changeset
3121 ret->coords[i] = mod;
bbc21cd6faeb some last minute changes before I merge to trunk.
lorenz
parents: 1847
diff changeset
3122 }
bbc21cd6faeb some last minute changes before I merge to trunk.
lorenz
parents: 1847
diff changeset
3123 return (PyObject*)ret;
bbc21cd6faeb some last minute changes before I merge to trunk.
lorenz
parents: 1847
diff changeset
3124 default:
bbc21cd6faeb some last minute changes before I merge to trunk.
lorenz
parents: 1847
diff changeset
3125 Py_INCREF(Py_NotImplemented);
bbc21cd6faeb some last minute changes before I merge to trunk.
lorenz
parents: 1847
diff changeset
3126 return Py_NotImplemented;
1841
a7dc31e50c46 * added the vetor_elementwiseproxy which lets you apply calculations on
lorenz
parents: 1840
diff changeset
3127 }
1848
bbc21cd6faeb some last minute changes before I merge to trunk.
lorenz
parents: 1847
diff changeset
3128 }
bbc21cd6faeb some last minute changes before I merge to trunk.
lorenz
parents: 1847
diff changeset
3129
bbc21cd6faeb some last minute changes before I merge to trunk.
lorenz
parents: 1847
diff changeset
3130 static PyObject *
bbc21cd6faeb some last minute changes before I merge to trunk.
lorenz
parents: 1847
diff changeset
3131 vector_elementwiseproxy_add(PyObject *o1, PyObject *o2)
bbc21cd6faeb some last minute changes before I merge to trunk.
lorenz
parents: 1847
diff changeset
3132 {
bbc21cd6faeb some last minute changes before I merge to trunk.
lorenz
parents: 1847
diff changeset
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
bbc21cd6faeb some last minute changes before I merge to trunk.
lorenz
parents: 1847
diff changeset
3135 static PyObject *
bbc21cd6faeb some last minute changes before I merge to trunk.
lorenz
parents: 1847
diff changeset
3136 vector_elementwiseproxy_sub(PyObject *o1, PyObject *o2)
bbc21cd6faeb some last minute changes before I merge to trunk.
lorenz
parents: 1847
diff changeset
3137 {
bbc21cd6faeb some last minute changes before I merge to trunk.
lorenz
parents: 1847
diff changeset
3138 return vector_elementwiseproxy_generic_math(o1, o2, OP_SUB);
bbc21cd6faeb some last minute changes before I merge to trunk.
lorenz
parents: 1847
diff changeset
3139 }
bbc21cd6faeb some last minute changes before I merge to trunk.
lorenz
parents: 1847
diff changeset
3140 static PyObject *
bbc21cd6faeb some last minute changes before I merge to trunk.
lorenz
parents: 1847
diff changeset
3141 vector_elementwiseproxy_mul(PyObject *o1, PyObject *o2)
bbc21cd6faeb some last minute changes before I merge to trunk.
lorenz
parents: 1847
diff changeset
3142 {
bbc21cd6faeb some last minute changes before I merge to trunk.
lorenz
parents: 1847
diff changeset
3143 return vector_elementwiseproxy_generic_math(o1, o2, OP_MUL);
bbc21cd6faeb some last minute changes before I merge to trunk.
lorenz
parents: 1847
diff changeset
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
bbc21cd6faeb some last minute changes before I merge to trunk.
lorenz
parents: 1847
diff changeset
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
bbc21cd6faeb some last minute changes before I merge to trunk.
lorenz
parents: 1847
diff changeset
3153 return vector_elementwiseproxy_generic_math(o1, o2, OP_FLOOR_DIV);
bbc21cd6faeb some last minute changes before I merge to trunk.
lorenz
parents: 1847
diff changeset
3154 }
bbc21cd6faeb some last minute changes before I merge to trunk.
lorenz
parents: 1847
diff changeset
3155 static PyObject *
bbc21cd6faeb some last minute changes before I merge to trunk.
lorenz
parents: 1847
diff changeset
3156 vector_elementwiseproxy_mod(PyObject *o1, PyObject *o2)
bbc21cd6faeb some last minute changes before I merge to trunk.
lorenz
parents: 1847
diff changeset
3157 {
bbc21cd6faeb some last minute changes before I merge to trunk.
lorenz
parents: 1847
diff changeset
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
d31cd03677fb added an incomplete Vector2 to the math module.
lorenz
parents:
diff changeset
3433 static PyMethodDef _math_methods[] =
d31cd03677fb added an incomplete Vector2 to the math module.
lorenz
parents:
diff changeset
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
d31cd03677fb added an incomplete Vector2 to the math module.
lorenz
parents:
diff changeset
3441 {NULL, NULL, 0, NULL}
d31cd03677fb added an incomplete Vector2 to the math module.
lorenz
parents:
diff changeset
3442 };
d31cd03677fb added an incomplete Vector2 to the math module.
lorenz
parents:
diff changeset
3443
d31cd03677fb added an incomplete Vector2 to the math module.
lorenz
parents:
diff changeset
3444 /* DOC */ static char _math_doc[] =
d31cd03677fb added an incomplete Vector2 to the math module.
lorenz
parents:
diff changeset
3445 /* DOC */ "Module for various math related classes and functions\n";
d31cd03677fb added an incomplete Vector2 to the math module.
lorenz
parents:
diff changeset
3446
d31cd03677fb added an incomplete Vector2 to the math module.
lorenz
parents:
diff changeset
3447 /****************************
d31cd03677fb added an incomplete Vector2 to the math module.
lorenz
parents:
diff changeset
3448 * Module init function
d31cd03677fb added an incomplete Vector2 to the math module.
lorenz
parents:
diff changeset
3449 ****************************/
d31cd03677fb added an incomplete Vector2 to the math module.
lorenz
parents:
diff changeset
3450
d31cd03677fb added an incomplete Vector2 to the math module.
lorenz
parents:
diff changeset
3451 MODINIT_DEFINE (math)
d31cd03677fb added an incomplete Vector2 to the math module.
lorenz
parents:
diff changeset
3452 {
d31cd03677fb added an incomplete Vector2 to the math module.
lorenz
parents:
diff changeset
3453 PyObject *module, *apiobj;
d31cd03677fb added an incomplete Vector2 to the math module.
lorenz
parents:
diff changeset
3454 static void *c_api[PYGAMEAPI_MATH_NUMSLOTS];
d31cd03677fb added an incomplete Vector2 to the math module.
lorenz
parents:
diff changeset
3455
d31cd03677fb added an incomplete Vector2 to the math module.
lorenz
parents:
diff changeset
3456 #if PY3
d31cd03677fb added an incomplete Vector2 to the math module.
lorenz
parents:
diff changeset
3457 static struct PyModuleDef _module = {
d31cd03677fb added an incomplete Vector2 to the math module.
lorenz
parents:
diff changeset
3458 PyModuleDef_HEAD_INIT,
d31cd03677fb added an incomplete Vector2 to the math module.
lorenz
parents:
diff changeset
3459 "math",
d31cd03677fb added an incomplete Vector2 to the math module.
lorenz
parents:
diff changeset
3460 _math_doc,
d31cd03677fb added an incomplete Vector2 to the math module.
lorenz
parents:
diff changeset
3461 -1,
d31cd03677fb added an incomplete Vector2 to the math module.
lorenz
parents:
diff changeset
3462 _math_methods,
d31cd03677fb added an incomplete Vector2 to the math module.
lorenz
parents:
diff changeset
3463 NULL, NULL, NULL, NULL
d31cd03677fb added an incomplete Vector2 to the math module.
lorenz
parents:
diff changeset
3464 };
d31cd03677fb added an incomplete Vector2 to the math module.
lorenz
parents:
diff changeset
3465 #endif
d31cd03677fb added an incomplete Vector2 to the math module.
lorenz
parents:
diff changeset
3466
d31cd03677fb added an incomplete Vector2 to the math module.
lorenz
parents:
diff changeset
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
1e46afbd7b54 implemented new methods in the math module.
lorenz
parents: 1845
diff changeset
3469 (PyType_Ready(&PyVector3_Type) < 0) ||
1e46afbd7b54 implemented new methods in the math module.
lorenz
parents: 1845
diff changeset
3470 (PyType_Ready(&PyVectorElementwiseProxy_Type) < 0) ||
1e46afbd7b54 implemented new methods in the math module.
lorenz
parents: 1845
diff changeset
3471 (PyType_Ready(&PyVectorIter_Type) < 0) ||
1e46afbd7b54 implemented new methods in the math module.
lorenz
parents: 1845
diff changeset
3472 (PyType_Ready(&PyVector_SlerpIter_Type) < 0) /*||
1834
d31cd03677fb added an incomplete Vector2 to the math module.
lorenz
parents:
diff changeset
3473 (PyType_Ready(&PyVector4_Type) < 0)*/) {
d31cd03677fb added an incomplete Vector2 to the math module.
lorenz
parents:
diff changeset
3474 MODINIT_ERROR;
d31cd03677fb added an incomplete Vector2 to the math module.
lorenz
parents:
diff changeset
3475 }
d31cd03677fb added an incomplete Vector2 to the math module.
lorenz
parents:
diff changeset
3476
d31cd03677fb added an incomplete Vector2 to the math module.
lorenz
parents:
diff changeset
3477 /* initialize the module */
d31cd03677fb added an incomplete Vector2 to the math module.
lorenz
parents:
diff changeset
3478 #if PY3
d31cd03677fb added an incomplete Vector2 to the math module.
lorenz
parents:
diff changeset
3479 module = PyModule_Create (&_module);
d31cd03677fb added an incomplete Vector2 to the math module.
lorenz
parents:
diff changeset
3480 #else
d31cd03677fb added an incomplete Vector2 to the math module.
lorenz
parents:
diff changeset
3481 module = Py_InitModule3 (MODPREFIX "math", _math_methods, _math_doc);
d31cd03677fb added an incomplete Vector2 to the math module.
lorenz
parents:
diff changeset
3482 #endif
d31cd03677fb added an incomplete Vector2 to the math module.
lorenz
parents:
diff changeset
3483
d31cd03677fb added an incomplete Vector2 to the math module.
lorenz
parents:
diff changeset
3484 if (module == NULL) {
d31cd03677fb added an incomplete Vector2 to the math module.
lorenz
parents:
diff changeset
3485 MODINIT_ERROR;
d31cd03677fb added an incomplete Vector2 to the math module.
lorenz
parents:
diff changeset
3486 }
d31cd03677fb added an incomplete Vector2 to the math module.
lorenz
parents:
diff changeset
3487
d31cd03677fb added an incomplete Vector2 to the math module.
lorenz
parents:
diff changeset
3488 /* add extension types to module */
d31cd03677fb added an incomplete Vector2 to the math module.
lorenz
parents:
diff changeset
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
1e46afbd7b54 implemented new methods in the math module.
lorenz
parents: 1845
diff changeset
3491 Py_INCREF(&PyVector3_Type);
1e46afbd7b54 implemented new methods in the math module.
lorenz
parents: 1845
diff changeset
3492 Py_INCREF(&PyVectorElementwiseProxy_Type);
1e46afbd7b54 implemented new methods in the math module.
lorenz
parents: 1845
diff changeset
3493 Py_INCREF(&PyVectorIter_Type);
1e46afbd7b54 implemented new methods in the math module.
lorenz
parents: 1845
diff changeset
3494 Py_INCREF(&PyVector_SlerpIter_Type);
1834
d31cd03677fb added an incomplete Vector2 to the math module.
lorenz
parents:
diff changeset
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
1e46afbd7b54 implemented new methods in the math module.
lorenz
parents: 1845
diff changeset
3497 (PyModule_AddObject(module, "Vector3", (PyObject *)&PyVector3_Type) != 0) ||
1e46afbd7b54 implemented new methods in the math module.
lorenz
parents: 1845
diff changeset
3498 (PyModule_AddObject(module, "VectorElementwiseProxy", (PyObject *)&PyVectorElementwiseProxy_Type) != 0) ||
1e46afbd7b54 implemented new methods in the math module.
lorenz
parents: 1845
diff changeset
3499 (PyModule_AddObject(module, "VectorIterator", (PyObject *)&PyVectorIter_Type) != 0) ||
1848
bbc21cd6faeb some last minute changes before I merge to trunk.
lorenz
parents: 1847
diff changeset
3500 (PyModule_AddObject(module, "VectorSlerpIterator", (PyObject *)&PyVector_SlerpIter_Type) != 0) /*||
1834
d31cd03677fb added an incomplete Vector2 to the math module.
lorenz
parents:
diff changeset
3501 (PyModule_AddObject(module, "Vector4", (PyObject *)&PyVector4_Type) != 0)*/) {
d31cd03677fb added an incomplete Vector2 to the math module.
lorenz
parents:
diff changeset
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
1e46afbd7b54 implemented new methods in the math module.
lorenz
parents: 1845
diff changeset
3504 Py_DECREF(&PyVectorElementwiseProxy_Type);
1e46afbd7b54 implemented new methods in the math module.
lorenz
parents: 1845
diff changeset
3505 Py_DECREF(&PyVectorIter_Type);
1e46afbd7b54 implemented new methods in the math module.
lorenz
parents: 1845
diff changeset
3506 Py_DECREF(&PyVector_SlerpIter_Type);
1834
d31cd03677fb added an incomplete Vector2 to the math module.
lorenz
parents:
diff changeset
3507 // Py_DECREF(&PyVector4_Type);
d31cd03677fb added an incomplete Vector2 to the math module.
lorenz
parents:
diff changeset
3508 DECREF_MOD (module);
d31cd03677fb added an incomplete Vector2 to the math module.
lorenz
parents:
diff changeset
3509 MODINIT_ERROR;
d31cd03677fb added an incomplete Vector2 to the math module.
lorenz
parents:
diff changeset
3510 }
d31cd03677fb added an incomplete Vector2 to the math module.
lorenz
parents:
diff changeset
3511
d31cd03677fb added an incomplete Vector2 to the math module.
lorenz
parents:
diff changeset
3512 /* export the C api */
d31cd03677fb added an incomplete Vector2 to the math module.
lorenz
parents:
diff changeset
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
d31cd03677fb added an incomplete Vector2 to the math module.
lorenz
parents:
diff changeset
3515 // c_api[2] = &PyVector4_Type;
d31cd03677fb added an incomplete Vector2 to the math module.
lorenz
parents:
diff changeset
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
d31cd03677fb added an incomplete Vector2 to the math module.
lorenz
parents:
diff changeset
3518 apiobj = PyCObject_FromVoidPtr(c_api, NULL);
d31cd03677fb added an incomplete Vector2 to the math module.
lorenz
parents:
diff changeset
3519 if (apiobj == NULL) {
d31cd03677fb added an incomplete Vector2 to the math module.
lorenz
parents:
diff changeset
3520 DECREF_MOD (module);
d31cd03677fb added an incomplete Vector2 to the math module.
lorenz
parents:
diff changeset
3521 MODINIT_ERROR;
d31cd03677fb added an incomplete Vector2 to the math module.
lorenz
parents:
diff changeset
3522 }
d31cd03677fb added an incomplete Vector2 to the math module.
lorenz
parents:
diff changeset
3523
d31cd03677fb added an incomplete Vector2 to the math module.
lorenz
parents:
diff changeset
3524 if (PyModule_AddObject(module, PYGAMEAPI_LOCAL_ENTRY, apiobj) != 0) {
1836
18eefb9bd5a2 updates to the pygame.math module
lorenz
parents: 1835
diff changeset
3525 Py_DECREF (apiobj);
1834
d31cd03677fb added an incomplete Vector2 to the math module.
lorenz
parents:
diff changeset
3526 DECREF_MOD (module);
d31cd03677fb added an incomplete Vector2 to the math module.
lorenz
parents:
diff changeset
3527 MODINIT_ERROR;
d31cd03677fb added an incomplete Vector2 to the math module.
lorenz
parents:
diff changeset
3528 }
d31cd03677fb added an incomplete Vector2 to the math module.
lorenz
parents:
diff changeset
3529
d31cd03677fb added an incomplete Vector2 to the math module.
lorenz
parents:
diff changeset
3530 MODINIT_RETURN (module);
d31cd03677fb added an incomplete Vector2 to the math module.
lorenz
parents:
diff changeset
3531 }