PyAPI: add Matrix.is_identity read-only attribute

Add a convenient way of checking if the matrix is an identity matrix.
This commit is contained in:
Campbell Barton 2022-07-11 12:43:24 +10:00
parent d51bc8215f
commit 133d398120
1 changed files with 23 additions and 0 deletions

View File

@ -290,6 +290,18 @@ static PyObject *matrix__apply_to_copy(PyObject *(*matrix_func)(MatrixObject *),
return NULL;
}
static bool matrix_is_identity(MatrixObject *self)
{
for (int row = 0; row < self->row_num; row++) {
for (int col = 0; col < self->col_num; col++) {
if (MATRIX_ITEM(self, row, col) != ((row != col) ? 0.0f : 1.0f)) {
return false;
}
}
}
return true;
}
/** \} */
/* -------------------------------------------------------------------- */
@ -3104,6 +3116,16 @@ static PyObject *Matrix_median_scale_get(MatrixObject *self, void *UNUSED(closur
return PyFloat_FromDouble(mat3_to_scale(mat));
}
PyDoc_STRVAR(Matrix_is_identity_doc,
"True if this is an identity matrix (read-only).\n\n:type: bool");
static PyObject *Matrix_is_identity_get(MatrixObject *self, void *UNUSED(closure))
{
if (BaseMath_ReadCallback(self) == -1) {
return NULL;
}
return PyBool_FromLong(matrix_is_identity(self));
}
PyDoc_STRVAR(Matrix_is_negative_doc,
"True if this matrix results in a negative scale, 3x3 and 4x4 only, "
"(read-only).\n\n:type: bool");
@ -3187,6 +3209,7 @@ static PyGetSetDef Matrix_getseters[] = {
NULL},
{"row", (getter)Matrix_row_get, (setter)NULL, Matrix_row_doc, NULL},
{"col", (getter)Matrix_col_get, (setter)NULL, Matrix_col_doc, NULL},
{"is_identity", (getter)Matrix_is_identity_get, (setter)NULL, Matrix_is_identity_doc, NULL},
{"is_negative", (getter)Matrix_is_negative_get, (setter)NULL, Matrix_is_negative_doc, NULL},
{"is_orthogonal",
(getter)Matrix_is_orthogonal_get,