annotate src/math.c @ 3194:9fcb7abf3185 default tip

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