Skip to content

Commit

Permalink
Add repr, equality operators for QgsMatrix4x4
Browse files Browse the repository at this point in the history
  • Loading branch information
nyalldawson committed Aug 3, 2023
1 parent 15adf1e commit 2ae70eb
Show file tree
Hide file tree
Showing 3 changed files with 100 additions and 8 deletions.
34 changes: 30 additions & 4 deletions python/core/auto_generated/qgsmatrix4x4.sip.in
Original file line number Diff line number Diff line change
Expand Up @@ -44,25 +44,51 @@ Initializes identity matrix
Initializes matrix by setting all values in row-major order
%End

QList< double > dataList() const /PyName=data/;
bool operator==( const QgsMatrix4x4 &other ) const;

bool operator!=( const QgsMatrix4x4 &other ) const;

QList< double > dataList() const /PyName=data,HoldGIL/;
%Docstring
Returns matrix data (in column-major order)
%End

QgsVector3D map( const QgsVector3D &vector ) const;
QgsVector3D map( const QgsVector3D &vector ) const /HoldGIL/;
%Docstring
Matrix-vector multiplication (vector is converted to homogenous coordinates [X,Y,Z,1] and back)
%End

bool isIdentity() const;
bool isIdentity() const /HoldGIL/;
%Docstring
Returns whether this matrix is an identity matrix
%End
void setToIdentity();
void setToIdentity() /HoldGIL/;
%Docstring
Sets matrix to be identity matrix
%End

SIP_PYOBJECT __repr__();
%MethodCode
QString str = QStringLiteral( "<QgsMatrix4x4(%1, %2, %3, %4, %5, %6, %7, %8, %9, %10, %11, %12, %13, %14, %15, %16)>" )
.arg( sipCpp->data()[0] )
.arg( sipCpp->data()[4] )
.arg( sipCpp->data()[8] )
.arg( sipCpp->data()[12] )
.arg( sipCpp->data()[1] )
.arg( sipCpp->data()[5] )
.arg( sipCpp->data()[9] )
.arg( sipCpp->data()[13] )
.arg( sipCpp->data()[2] )
.arg( sipCpp->data()[6] )
.arg( sipCpp->data()[10] )
.arg( sipCpp->data()[14] )
.arg( sipCpp->data()[3] )
.arg( sipCpp->data()[7] )
.arg( sipCpp->data()[11] )
.arg( sipCpp->data()[15] );
sipRes = PyUnicode_FromString( str.toUtf8().constData() );
%End


};

Expand Down
42 changes: 38 additions & 4 deletions src/core/qgsmatrix4x4.h
Original file line number Diff line number Diff line change
Expand Up @@ -47,23 +47,57 @@ class CORE_EXPORT QgsMatrix4x4
double m31, double m32, double m33, double m34,
double m41, double m42, double m43, double m44 );

bool operator==( const QgsMatrix4x4 &other ) const
{
return memcmp( m, other.m, sizeof( m ) ) == 0;
}

bool operator!=( const QgsMatrix4x4 &other ) const
{
return !( *this == other );
}

//! Returns pointer to the matrix data (stored in column-major order)
const double *constData() const SIP_SKIP { return *m; }
//! Returns pointer to the matrix data (stored in column-major order)
double *data() SIP_SKIP { return *m; }
//! Returns matrix data (in column-major order)
QList< double > dataList() const SIP_PYNAME( data );
QList< double > dataList() const SIP_PYNAME( data ) SIP_HOLDGIL;

//! Matrix-vector multiplication (vector is converted to homogenous coordinates [X,Y,Z,1] and back)
QgsVector3D map( const QgsVector3D &vector ) const
QgsVector3D map( const QgsVector3D &vector ) const SIP_HOLDGIL
{
return *this * vector;
}

//! Returns whether this matrix is an identity matrix
bool isIdentity() const;
bool isIdentity() const SIP_HOLDGIL;
//! Sets matrix to be identity matrix
void setToIdentity();
void setToIdentity() SIP_HOLDGIL;

#ifdef SIP_RUN
SIP_PYOBJECT __repr__();
% MethodCode
QString str = QStringLiteral( "<QgsMatrix4x4(%1, %2, %3, %4, %5, %6, %7, %8, %9, %10, %11, %12, %13, %14, %15, %16)>" )
.arg( sipCpp->data()[0] )
.arg( sipCpp->data()[4] )
.arg( sipCpp->data()[8] )
.arg( sipCpp->data()[12] )
.arg( sipCpp->data()[1] )
.arg( sipCpp->data()[5] )
.arg( sipCpp->data()[9] )
.arg( sipCpp->data()[13] )
.arg( sipCpp->data()[2] )
.arg( sipCpp->data()[6] )
.arg( sipCpp->data()[10] )
.arg( sipCpp->data()[14] )
.arg( sipCpp->data()[3] )
.arg( sipCpp->data()[7] )
.arg( sipCpp->data()[11] )
.arg( sipCpp->data()[15] );
sipRes = PyUnicode_FromString( str.toUtf8().constData() );
% End
#endif

#ifndef SIP_RUN
friend CORE_EXPORT QgsMatrix4x4 operator*( const QgsMatrix4x4 &m1, const QgsMatrix4x4 &m2 );
Expand Down
32 changes: 32 additions & 0 deletions tests/src/python/test_qgsmatrix4x4.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,38 @@ def test_basic(self):
m3 = m1 * m2
self.assertEqual(m3.data(), [2, 10, 18, 0, 4, 12, 20, 0, 6, 14, 22, 0, 4, 8, 12, 1])

def test_repr(self):
self.assertEqual(
str(QgsMatrix4x4(1, 2, 3, 4,
5, 6, 7, 8,
9, 10, 11, 12,
13, 14, 15, 16)),
'<QgsMatrix4x4(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16)>'
)

def test_equality(self):
self.assertEqual(
QgsMatrix4x4(1, 2, 3, 4,
5, 6, 7, 8,
9, 10, 11, 12,
13, 14, 15, 16),
QgsMatrix4x4(1, 2, 3, 4,
5, 6, 7, 8,
9, 10, 11, 12,
13, 14, 15, 16)
)
for i in range(16):
self.assertNotEqual(
QgsMatrix4x4(1, 2, 3, 4,
5, 6, 7, 8,
9, 10, 11, 12,
13, 14, 15, 16),
QgsMatrix4x4(*[j if k != i else 0 for k, j in enumerate([1, 2, 3, 4,
5, 6, 7, 8,
9, 10, 11, 12,
13, 14, 15, 16])])
)


if __name__ == '__main__':
unittest.main()

0 comments on commit 2ae70eb

Please sign in to comment.