Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Fix Vector subclass methods to return the correct subtype instance#3088

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Merged
ankith26 merged 18 commits intopygame:mainfromandrewhong04:vector-subclass
Apr 23, 2022
Merged
Show file tree
Hide file tree
Changes fromall commits
Commits
Show all changes
18 commits
Select commitHold shift + click to select a range
8d9f050
created _vector_subtype_new()
andrewhong04Mar 21, 2022
5e64858
code cleanup
andrewhong04Mar 24, 2022
0d419f7
added unittests + docs + black formatting
andrewhong04Mar 24, 2022
0538972
add more subtyping
andrewhong04Apr 5, 2022
03678e5
github workflow tests fix
andrewhong04Apr 10, 2022
bb08ffd
Commented out pgVector_NEW()
andrewhong04Apr 11, 2022
6091fbd
hopefully, this fixes everythinf
andrewhong04Apr 14, 2022
ee89fcc
Fixed segfault
andrewhong04Apr 19, 2022
289458a
Update math.c
andrewhong04Apr 19, 2022
8a23a8e
Update math.c
andrewhong04Apr 19, 2022
ee34e7a
Update math.c
andrewhong04Apr 19, 2022
f84c76b
Update math.c
andrewhong04Apr 19, 2022
064e7b4
segfault fixes and etc
andrewhong04Apr 10, 2022
55cc9a2
Bring back pgVector_NEW() & swizzling return fix
andrewhong04Apr 20, 2022
48ce90b
Merge branch 'vector-subclass' of https://github.com/novialriptide/py…
andrewhong04Apr 20, 2022
b57feef
code cleanup
andrewhong04Apr 21, 2022
583f9b4
more tests
andrewhong04Apr 22, 2022
0953c24
Update src_c/math.c
andrewhong04Apr 23, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletionsdocs/reST/ref/math.rst
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -60,6 +60,10 @@ Multiple coordinates can be set using slices or swizzling

Some general information about the ``Vector2`` class.

.. versionchanged:: 2.1.3
Inherited methods of vector subclasses now correctly return an instance of the
subclass instead of the superclass

.. method:: dot

| :sl:`calculates the dot- or scalar-product with the other vector`
Expand DownExpand Up@@ -435,6 +439,10 @@ Multiple coordinates can be set using slices or swizzling

Some general information about the Vector3 class.

.. versionchanged:: 2.1.3
Inherited methods of vector subclasses now correctly return an instance of the
subclass instead of the superclass

.. method:: dot

| :sl:`calculates the dot- or scalar-product with the other vector`
Expand Down
106 changes: 71 additions & 35 deletionssrc_c/math.c
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -133,6 +133,8 @@ _vector_move_towards_helper(Py_ssize_t dim, double *origin_coords,
double *target_coords, double max_distance);

/* generic vector functions */
static pgVector *
_vector_subtype_new(pgVector *base);
static PyObject *
pgVector_NEW(Py_ssize_t dim);
static void
Expand DownExpand Up@@ -550,6 +552,28 @@ static PyMemberDef vector_members[] = {
{NULL} /* Sentinel */
};

static pgVector *
_vector_subtype_new(pgVector *base)
{
pgVector *vec;
PyTypeObject *type = Py_TYPE(base);
Py_ssize_t dim = base->dim;

vec = (pgVector *)(type->tp_new(type, NULL, NULL));

if (vec) {
vec->dim = dim;
vec->epsilon = VECTOR_EPSILON;
vec->coords = PyMem_New(double, dim);

if (vec->coords == NULL) {
Py_DECREF(vec);
return (pgVector *)PyErr_NoMemory();
}
}
return vec;
}

static PyObject *
pgVector_NEW(Py_ssize_t dim)
{
Expand DownExpand Up@@ -639,7 +663,7 @@ vector_generic_math(PyObject *o1, PyObject *o2, int op)
}
else if (op != (OP_MUL | OP_ARG_VECTOR) &&
op != (OP_MUL | OP_ARG_VECTOR | OP_ARG_REVERSE)) {
ret =(pgVector *)pgVector_NEW(dim);
ret =_vector_subtype_new(vec);
if (ret == NULL)
return NULL;
}
Expand DownExpand Up@@ -760,8 +784,9 @@ vector_inplace_floor_div(pgVector *o1, PyObject *o2)
static PyObject *
vector_neg(pgVector *self)
{
pgVector *ret = (pgVector *)pgVector_NEW(self->dim);
if (ret != NULL) {
pgVector *ret = _vector_subtype_new(self);

if (ret) {
Py_ssize_t i;

for (i = 0; i < self->dim; i++) {
Expand All@@ -774,8 +799,9 @@ vector_neg(pgVector *self)
static PyObject *
vector_pos(pgVector *self)
{
pgVector *ret = (pgVector *)pgVector_NEW(self->dim);
if (ret != NULL) {
pgVector *ret = _vector_subtype_new(self);

if (ret) {
memcpy(ret->coords, self->coords, sizeof(ret->coords[0]) * ret->dim);
}
return (PyObject *)ret;
Expand All@@ -796,8 +822,13 @@ vector_nonzero(pgVector *self)
static PyObject *
vector_copy(pgVector *self, PyObject *_null)
{
pgVector *ret = (pgVector *)pgVector_NEW(self->dim);
Py_ssize_t i;
pgVector *ret = _vector_subtype_new(self);

if (!ret) {
return NULL;
}

for (i = 0; i < self->dim; i++) {
ret->coords[i] = self->coords[i];
}
Expand All@@ -810,7 +841,7 @@ vector_clamp_magnitude(pgVector *self, PyObject *args, PyObject *kwargs)
Py_ssize_t i;
pgVector *ret;

ret =(pgVector *)pgVector_NEW(self->dim);
ret =_vector_subtype_new(self);
if (ret == NULL)
return NULL;

Expand DownExpand Up@@ -1261,9 +1292,7 @@ vector_length_squared(pgVector *self, PyObject *_null)
static PyObject *
vector_normalize(pgVector *self, PyObject *_null)
{
pgVector *ret;

ret = (pgVector *)pgVector_NEW(self->dim);
pgVector *ret = _vector_subtype_new(self);
if (ret == NULL) {
return NULL;
}
Expand DownExpand Up@@ -1399,7 +1428,7 @@ vector_move_towards(pgVector *self, PyObject *args)
return NULL;
}

ret =(pgVector *)pgVector_NEW(self->dim);
ret =_vector_subtype_new(self);
if (ret == NULL)
return NULL;

Expand DownExpand Up@@ -1481,7 +1510,7 @@ vector_slerp(pgVector *self, PyObject *args)
if (self->coords[0] * other_coords[1] < self->coords[1] * other_coords[0])
angle *= -1;

ret =(pgVector *)pgVector_NEW(self->dim);
ret =_vector_subtype_new(self);
if (ret == NULL) {
return NULL;
}
Expand DownExpand Up@@ -1533,7 +1562,7 @@ vector_lerp(pgVector *self, PyObject *args)
return NULL;
}

ret =(pgVector *)pgVector_NEW(self->dim);
ret =_vector_subtype_new(self);
if (ret == NULL) {
return NULL;
}
Expand DownExpand Up@@ -1578,7 +1607,8 @@ _vector_reflect_helper(double *dst_coords, const double *src_coords,
static PyObject *
vector_reflect(pgVector *self, PyObject *normal)
{
pgVector *ret = (pgVector *)pgVector_NEW(self->dim);
pgVector *ret = _vector_subtype_new(self);

if (ret == NULL) {
return NULL;
}
Expand DownExpand Up@@ -1733,7 +1763,7 @@ vector_project_onto(pgVector *self, PyObject *other)
return NULL;
}

ret =(pgVector *)pgVector_NEW(self->dim);
ret =_vector_subtype_new(self);
if (ret == NULL) {
return NULL;
}
Expand DownExpand Up@@ -2175,7 +2205,7 @@ vector2_rotate_rad(pgVector *self, PyObject *angleObject)
return NULL;
}

ret =(pgVector *)pgVector_NEW(self->dim);
ret =_vector_subtype_new(self);
if (ret == NULL || !_vector2_rotate_helper(ret->coords, self->coords,
angle, self->epsilon)) {
Py_XDECREF(ret);
Expand DownExpand Up@@ -2228,7 +2258,7 @@ vector2_rotate(pgVector *self, PyObject *angleObject)
}
angle = DEG2RAD(angle);

ret =(pgVector *)pgVector_NEW(self->dim);
ret =_vector_subtype_new(self);
if (ret == NULL || !_vector2_rotate_helper(ret->coords, self->coords,
angle, self->epsilon)) {
Py_XDECREF(ret);
Expand DownExpand Up@@ -2680,7 +2710,7 @@ vector3_rotate_rad(pgVector *self, PyObject *args)
return NULL;
}

ret =(pgVector *)pgVector_NEW(self->dim);
ret =_vector_subtype_new(self);
if (ret == NULL ||
!_vector3_rotate_helper(ret->coords, self->coords, axis_coords, angle,
self->epsilon)) {
Expand DownExpand Up@@ -2750,7 +2780,7 @@ vector3_rotate(pgVector *self, PyObject *args)
return NULL;
}

ret =(pgVector *)pgVector_NEW(self->dim);
ret =_vector_subtype_new(self);
if (ret == NULL ||
!_vector3_rotate_helper(ret->coords, self->coords, axis_coords, angle,
self->epsilon)) {
Expand DownExpand Up@@ -2802,7 +2832,7 @@ vector3_rotate_x_rad(pgVector *self, PyObject *angleObject)
sinValue = sin(angle);
cosValue = cos(angle);

ret =(pgVector *)pgVector_NEW(self->dim);
ret =_vector_subtype_new(self);
if (ret == NULL) {
return NULL;
}
Expand DownExpand Up@@ -2861,7 +2891,7 @@ vector3_rotate_x(pgVector *self, PyObject *angleObject)
sinValue = sin(angle);
cosValue = cos(angle);

ret =(pgVector *)pgVector_NEW(self->dim);
ret =_vector_subtype_new(self);
if (ret == NULL) {
return NULL;
}
Expand DownExpand Up@@ -2906,7 +2936,7 @@ vector3_rotate_y_rad(pgVector *self, PyObject *angleObject)
sinValue = sin(angle);
cosValue = cos(angle);

ret =(pgVector *)pgVector_NEW(self->dim);
ret =_vector_subtype_new(self);
if (ret == NULL) {
return NULL;
}
Expand DownExpand Up@@ -2966,7 +2996,7 @@ vector3_rotate_y(pgVector *self, PyObject *angleObject)
sinValue = sin(angle);
cosValue = cos(angle);

ret =(pgVector *)pgVector_NEW(self->dim);
ret =_vector_subtype_new(self);
if (ret == NULL) {
return NULL;
}
Expand DownExpand Up@@ -3012,7 +3042,7 @@ vector3_rotate_z_rad(pgVector *self, PyObject *angleObject)
sinValue = sin(angle);
cosValue = cos(angle);

ret =(pgVector *)pgVector_NEW(self->dim);
ret =_vector_subtype_new(self);
if (ret == NULL) {
return NULL;
}
Expand DownExpand Up@@ -3072,7 +3102,7 @@ vector3_rotate_z(pgVector *self, PyObject *angleObject)
sinValue = sin(angle);
cosValue = cos(angle);

ret =(pgVector *)pgVector_NEW(self->dim);
ret =_vector_subtype_new(self);
if (ret == NULL) {
return NULL;
}
Expand DownExpand Up@@ -3133,7 +3163,7 @@ vector3_cross(pgVector *self, PyObject *other)
}
}

ret =(pgVector *)pgVector_NEW(self->dim);
ret =_vector_subtype_new(self);
if (ret == NULL) {
if (!pgVector_Check(other))
PyMem_Free(other_coords);
Expand DownExpand Up@@ -3645,7 +3675,8 @@ vector_elementwiseproxy_generic_math(PyObject *o1, PyObject *o2, int op)
else
op |= OP_ARG_UNKNOWN;

ret = (pgVector *)pgVector_NEW(dim);
ret = _vector_subtype_new(vec);

if (ret == NULL) {
return NULL;
}
Expand All@@ -3655,8 +3686,9 @@ vector_elementwiseproxy_generic_math(PyObject *o1, PyObject *o2, int op)
switch (op) {
case OP_ADD | OP_ARG_NUMBER:
case OP_ADD | OP_ARG_NUMBER | OP_ARG_REVERSE:
for (i = 0; i < dim; i++)
for (i = 0; i < dim; i++) {
ret->coords[i] = vec->coords[i] + other_value;
}
break;
case OP_SUB | OP_ARG_NUMBER:
for (i = 0; i < dim; i++)
Expand DownExpand Up@@ -3844,6 +3876,7 @@ vector_elementwiseproxy_pow(PyObject *baseObj, PyObject *expoObj,
PyObject *mod)
{
Py_ssize_t i, dim;
pgVector *vec;
double *tmp;
PyObject *bases[VECTOR_MAX_SIZE] = {NULL};
PyObject *expos[VECTOR_MAX_SIZE] = {NULL};
Expand All@@ -3856,8 +3889,9 @@ vector_elementwiseproxy_pow(PyObject *baseObj, PyObject *expoObj,
}

if (vector_elementwiseproxy_Check(baseObj)) {
dim = ((vector_elementwiseproxy *)baseObj)->vec->dim;
tmp = ((vector_elementwiseproxy *)baseObj)->vec->coords;
vec = ((vector_elementwiseproxy *)baseObj)->vec;
dim = vec->dim;
tmp = vec->coords;
for (i = 0; i < dim; ++i)
bases[i] = PyFloat_FromDouble(tmp[i]);
if (vector_elementwiseproxy_Check(expoObj)) {
Expand All@@ -3883,8 +3917,9 @@ vector_elementwiseproxy_pow(PyObject *baseObj, PyObject *expoObj,
}
}
else {
dim = ((vector_elementwiseproxy *)expoObj)->vec->dim;
tmp = ((vector_elementwiseproxy *)expoObj)->vec->coords;
vec = ((vector_elementwiseproxy *)expoObj)->vec;
dim = vec->dim;
tmp = vec->coords;
for (i = 0; i < dim; ++i)
expos[i] = PyFloat_FromDouble(tmp[i]);
if (pgVectorCompatible_Check(baseObj, dim)) {
Expand All@@ -3909,7 +3944,7 @@ vector_elementwiseproxy_pow(PyObject *baseObj, PyObject *expoObj,
goto clean_up;
}

ret =pgVector_NEW(dim);
ret =(PyObject *)_vector_subtype_new(vec);
if (ret == NULL)
goto clean_up;
/* there are many special cases so we let python do the work for us */
Expand DownExpand Up@@ -3942,8 +3977,8 @@ vector_elementwiseproxy_pow(PyObject *baseObj, PyObject *expoObj,
static PyObject *
vector_elementwiseproxy_abs(vector_elementwiseproxy *self)
{
pgVector *ret =(pgVector *)pgVector_NEW(self->vec->dim);
if (ret != NULL) {
pgVector *ret =_vector_subtype_new(self->vec);
if (ret) {
Py_ssize_t i;

for (i = 0; i < self->vec->dim; i++) {
Expand DownExpand Up@@ -4011,6 +4046,7 @@ vector_elementwise(pgVector *vec, PyObject *_null)
return NULL;
Py_INCREF(vec);
proxy->vec = (pgVector *)vec;

return (PyObject *)proxy;
}

Expand Down
Loading

[8]ページ先頭

©2009-2025 Movatter.jp