diff --git a/PyGLM/type_methods/glmArray.h b/PyGLM/type_methods/glmArray.h index 6d3e55f..6259d4f 100644 --- a/PyGLM/type_methods/glmArray.h +++ b/PyGLM/type_methods/glmArray.h @@ -5134,15 +5134,13 @@ static PyObject* glmArray_mul_T_MMUL(glmArray* arr1, glmArray* arr2) { outArray->readonly = false; outArray->reference = NULL; - Py_ssize_t n, arr1Stride, arr2Stride, trueShape1; + Py_ssize_t n, arr1Stride, arr2Stride; if (arr1->glmType == PyGLM_TYPE_VEC) { n = arr1->shape[0]; arr1Stride = 1; arr2Stride = arr2->shape[1]; - trueShape1 = 1; - outArray->glmType = PyGLM_TYPE_VEC; outArray->shape[0] = arr2->shape[0]; outArray->shape[1] = 0; @@ -5155,8 +5153,6 @@ static PyObject* glmArray_mul_T_MMUL(glmArray* arr1, glmArray* arr2) { arr1Stride = arr1->shape[1]; arr2Stride = 0; - trueShape1 = arr1->shape[1]; - outArray->glmType = PyGLM_TYPE_VEC; outArray->shape[0] = arr1->shape[1]; outArray->shape[1] = 0; @@ -5169,8 +5165,6 @@ static PyObject* glmArray_mul_T_MMUL(glmArray* arr1, glmArray* arr2) { arr1Stride = arr1->shape[1]; arr2Stride = arr2->shape[1]; - trueShape1 = arr1->shape[1]; - outArray->glmType = PyGLM_TYPE_MAT; outArray->shape[0] = arr2->shape[0]; outArray->shape[1] = arr1->shape[1]; @@ -5198,13 +5192,16 @@ static PyObject* glmArray_mul_T_MMUL(glmArray* arr1, glmArray* arr2) { for (Py_ssize_t j = 0; j < outArrayRatio; j++) { T result = (T)0; for (Py_ssize_t k = 0; k < n; k++) { - T a = arr1DataPtr[k * arr1Stride + j % trueShape1]; - T b = arr2DataPtr[k + (j / trueShape1) * arr2Stride]; + T a = arr1DataPtr[k * arr1Stride + j % arr1Stride]; + T b = arr2DataPtr[k + (j / arr1Stride) * arr2Stride]; result += a * b; } outArrayDataPtr[outArrayIndex++] = result; } + // move pointers by one item + arr1DataPtr = reinterpret_cast(reinterpret_cast(arr1DataPtr) + arr1->itemSize); + arr2DataPtr = reinterpret_cast(reinterpret_cast(arr2DataPtr) + arr2->itemSize); } return (PyObject*)outArray; @@ -5283,15 +5280,13 @@ static PyObject* glmArray_mulO_T(glmArray* arr, T* o, Py_ssize_t o_size, PyGLMTy return (PyObject*)outArray; } - Py_ssize_t n, arrStride, oStride, trueShape1; + Py_ssize_t n, arrStride, oStride; if (arr->glmType == PyGLM_TYPE_VEC) { n = arr->shape[0]; arrStride = 1; oStride = pto->R; - trueShape1 = 1; - outArray->glmType = PyGLM_TYPE_VEC; outArray->shape[0] = pto->C; outArray->shape[1] = 0; @@ -5304,8 +5299,6 @@ static PyObject* glmArray_mulO_T(glmArray* arr, T* o, Py_ssize_t o_size, PyGLMTy arrStride = arr->shape[1]; oStride = 0; - trueShape1 = arr->shape[1]; - outArray->glmType = PyGLM_TYPE_VEC; outArray->shape[0] = arr->shape[1]; outArray->shape[1] = 0; @@ -5318,8 +5311,6 @@ static PyObject* glmArray_mulO_T(glmArray* arr, T* o, Py_ssize_t o_size, PyGLMTy arrStride = arr->shape[1]; oStride = pto->R; - trueShape1 = arr->shape[1]; - outArray->glmType = PyGLM_TYPE_MAT; outArray->shape[0] = pto->C; outArray->shape[1] = arr->shape[1]; @@ -5346,13 +5337,14 @@ static PyObject* glmArray_mulO_T(glmArray* arr, T* o, Py_ssize_t o_size, PyGLMTy for (Py_ssize_t j = 0; j < outArrayRatio; j++) { T result = (T)0; for (Py_ssize_t k = 0; k < n; k++) { - T a = arrDataPtr[k * arrStride + j % trueShape1]; - T b = o[k + (j / trueShape1) * oStride]; + T a = arrDataPtr[k * arrStride + j % arrStride]; + T b = o[k + (j / arrStride) * oStride]; result = result + a * b; } outArrayDataPtr[outArrayIndex++] = result; } + arrDataPtr = reinterpret_cast(reinterpret_cast(arrDataPtr) + arr->itemSize); } return (PyObject*)outArray; @@ -5441,6 +5433,7 @@ static PyObject* glmArray_rmulO_T(glmArray* arr, T* o, Py_ssize_t o_size, PyGLMT } outArrayDataPtr[outArrayIndex++] = result; } + arrDataPtr = reinterpret_cast(reinterpret_cast(arrDataPtr) + arr->itemSize); } return (PyObject*)outArray; diff --git a/test/PyGLM_test.py b/test/PyGLM_test.py index f815211..1d4b403 100644 --- a/test/PyGLM_test.py +++ b/test/PyGLM_test.py @@ -3643,7 +3643,36 @@ def test_mat2x2(): ##/core_type_mat2x2 ## ###/GLM TESTS ### - +def test_array_matmul(): + hitbox = glm.array( + glm.vec2(0., 1.), + glm.vec2(1., 1.), + glm.vec2(1., 0.), + glm.vec2(0., 0.), + ) + rotation = glm.radians(90) + scale_x = 1. + scale_y = 2. + + cos_rotation = glm.cos(rotation) + sin_rotation = glm.sin(rotation) + + rotation_scale_matrix = glm.mat2x2( + scale_x * cos_rotation, -scale_y * sin_rotation, + scale_x * sin_rotation, scale_y * cos_rotation + ) + hitbox_rotated = hitbox * rotation_scale_matrix + + # I expect to see the points rotated 90 degrees, approximately: + # array(vec2(-2, 0), vec2(-2, 1), vec2(0, 1), vec2(0, 0)) + assert glm.equal(hitbox_rotated[0].x, -2, 0.00001) + assert glm.equal(hitbox_rotated[0].y, 0, 0.00001) + assert glm.equal(hitbox_rotated[1].x, -2, 0.00001) + assert glm.equal(hitbox_rotated[1].y, 1, 0.00001) + assert glm.equal(hitbox_rotated[2].x, 0, 0.00001) + assert glm.equal(hitbox_rotated[2].y, 1, 0.00001) + assert glm.equal(hitbox_rotated[3].x, 0, 0.00001) + assert glm.equal(hitbox_rotated[3].y, 0, 0.00001) ### TEST TEST ###