 # Rotation(double[][], double) constructs inverse quaternion due to bug in mat2quat

XMLWordPrintableJSON

#### Details

• Type: Bug
• Status: Resolved
• Priority: Major
• Resolution: Won't Fix
• Affects Version/s: 3.6.1
• Fix Version/s:
• Labels:
None

#### Description

Constructing a Rotation object from a rotation matrix and extracting the quaternion gives the inverse of the expected quaternion.

E.g. something like this:

```Rotation rot = new Rotation(Array2DRowRealMatrix(matrixData).getData())
Quaternion q = new Quaternion(rot.getQ0(), rot.getQ1(), rot.getQ2(), rot.getQ3());
```

results in q being the inverse of what is expected.

I tracked this down to Rotation#mat2quat(final double[][]) which seems to access the matrix elements as if they were stored transposed. E.g. compare with Quat4f#set(Matrix3f):

Rotation.java
```quat = inv * (ort - ort);
quat = inv * (ort - ort);
quat = inv * (ort - ort); // <-- m01 - m10
```
Quat4f.java
```this.x = (m1.m21 - m1.m12) * ww;
this.y = (m1.m02 - m1.m20) * ww;
this.z = (m1.m10 - m1.m01) * ww; // <-- m10 - m01
```

I compared the result from Commons Math with JavaFX, JavaX Vecmath and NumPy + http://www.lfd.uci.edu/~gohlke/code/transformations.py.html. All but Commons Math agree on the result.

You can find my test program here: http://pastebin.com/jxwFi9mt
It prints the following output (Python results added manually):

```[ 0.7  0.0  0.0 -0.7] (Commons Math)
[ 0.7  0.0  0.0  0.7] (JavaFX)
[ 0.7  0.0  0.0  0.7] (JavaX Vecmath)
[ 0.7  0.0  0.0  0.7] (NumPy + transformations.py)

[-0.2  1.0  0.0  0.0] (Commons Math)
[ 0.2  1.0  0.0  0.0] (JavaFX)
[ 0.2  1.0  0.0  0.0] (JavaX Vecmath)
[ 0.2  1.0  0.0  0.0] (NumPy + transformations.py)

[ 0.2  0.0  1.0  0.0] (Commons Math)
[ 0.2  0.0 -1.0  0.0] (JavaFX)
[ 0.2  0.0 -1.0  0.0] (JavaX Vecmath)
[-0.2  0.0  1.0  0.0] (NumPy + transformations.py)

[-0.2  0.0  0.0  1.0] (Commons Math)
[ 0.2  0.0  0.0  1.0] (JavaFX)
[ 0.2  0.0  0.0  1.0] (JavaX Vecmath)
[ 0.2  0.0  0.0  1.0] (NumPy + transformations.py)
```

The other constructor using mat2quat() is probably also affected although I did not verify this.

#### People

• Assignee: Unassigned
Reporter: Robert Huitl