bdaiinstitute / spatialmath-python

Create, manipulate and convert representations of position and orientation in 2D or 3D using Python
MIT License
497 stars 82 forks source link

Raise error on invalid rotation matrix in UnitQuaternion constructor. #87

Closed adamheins closed 10 months ago

adamheins commented 10 months ago

Previously, passing an invalid rotation matrix to the UnitQuaternion constructor would just silently fail and yield an identity quaternion. For example:

>>> R = 1.1 * np.eye(3)  # not a valid rotation matrix
>>> UnitQuaternion(R)
1.0000 <<  0.0000,  0.0000,  0.0000 >>

Funnily enough, this only happens when check=True, since when check=False the constructor assumes it can try to interpret the argument as a quaternion (i.e., a vector of length 4) and fails.

This PR fixes this problem by adding additional checks against the argument passed to the constructor, and also has a specific check for when the matrix passed is 3x3 but is not a valid rotation matrix.