Discrepancy between Matrix3d.Rotation and Matrix3D.RotateAt

Discrepancy between Matrix3d.Rotation and Matrix3D.RotateAt

dhaverstick
Advocate Advocate
1,228 Views
3 Replies
Message 1 of 4

Discrepancy between Matrix3d.Rotation and Matrix3D.RotateAt

dhaverstick
Advocate
Advocate

I'm trying to port some code from the AutoCAD API to a more generic .Net API and I found a discrepancy between two functions that are supposed to do the same thing.

 

In AutoCAD, I created an AutoCAD.Geometry.Matrix3d object and then rotated it about the Z Axis by AnglePhi (PI/8). Here's the code snippet.

Dim RotationMatrixZAxis As Autodesk.AutoCAD.Geometry.Matrix3d
RotationMatrixZAxis = Autodesk.AutoCAD.Geometry.Matrix3d.Rotation(AnglePhi, New Autodesk.AutoCAD.Geometry.Vector3d(0, 0, 1), New Autodesk.AutoCAD.Geometry.Point3d(0, 0, 0))

The resultant element values from this operation are: 1,1 = 0.9238, 1,2 = -0.3826, 1,3 = 0, 2,1 = 0.3826, 2,2 = 0.9238, 2,3 = 0, 3,1 = 0, 3,2 = 0, 3,3 = 1

 

Now I do the same thing using the System.Windows.Media.Media3D library.

Dim RotationMatrixZAxis As New System.Windows.Media.Media3D.Matrix3D
Dim MyQuaternion As New System.Windows.Media.Media3D.Quaternion(New Vector3D(0, 0, 1), AnglePhi * 180 / PI)
RotationMatrixYAxis.RotateAt(MyQuaternion, New Point3D(0, 0, 0))

The resultant element values from this operation are: 1,1 = 0.9238, 1,2 = 0.3826, 1,3 = 0, 2,1 = -0.3826, 2,2 = 0.9238, 2,3 = 0, 3,1 = 0, 3,2 = 0, 3,3 = 1

 

Notice that the rotation matrix using the System.Windows.Media.Media3D library is the inverse of the rotation matrix using Autodesk.AutoCAD.Geometry

 

Can anyone explain to me why this is? My code porting project is stopped dead in its tracks until I get this little mystery solved.

 

Thanks in advance!

 

Darren Haverstick

Paul Mueller Company

Accepted solutions (1)
1,229 Views
3 Replies
Replies (3)
Message 2 of 4

dhaverstick
Advocate
Advocate

To help illustrate what I am talking about, I have attached VS 2019 project that creates an AutoCAD addin. The addin is compiled for AutoCAD 2019. All source code is included.

 

To run the addin, use the NETLOAD command, navigate to the ".\bin\Release\" folder, and choose the library, "QuaternionAutoCADExample.dll". After the library is loaded, invoke the command, "RUNQUATERNIONEXAMPLE". 

 

Darren Haverstick

Paul Mueller Company

0 Likes
Message 3 of 4

_gile
Consultant
Consultant
Accepted solution

Hi,

 

From some tests I did,you're right. System.Windows.Media.Media3D represents the inversed transposed transformation matrix. But when you use it (e.g. apply it to a vector), it returns the same result as the Autodesk.AutoCAD.Geometry one.

 

See this example:

AcGe = Autodesk.AutoCAD.Geometry
WM3D = System.Windows.Media.Media3D

 

[CommandMethod("TEST")]
public static void Test()
{
    var ed = AcAp.DocumentManager.MdiActiveDocument.Editor;

    var m1 = AcGe.Matrix3d.Rotation(Math.PI / 8.0, AcGe.Vector3d.ZAxis, AcGe.Point3d.Origin);
    var v1 = new AcGe.Vector3d(3.0, 2.0, 0.0).TransformBy(m1);
    ed.WriteMessage($"\nAutoCAD\n{m1:0.000}\n{v1:0.000}");

    var quaternion = new WM3D.Quaternion(new WM3D.Vector3D(0.0, 0.0, 1.0), 22.5);
    var m2 = new WM3D.Matrix3D();
    m2.Rotate(quaternion);
    var v2 = WM3D.Vector3D.Multiply(new WM3D.Vector3D(3.0, 2.0, 0.0), m2);
    ed.WriteMessage($"\nMedia3D\n{m2:0.000}\n{v2:0.000}");
}

 

 

 

 

Output:

 

 

AutoCAD
((0.924,-0.383,0.000,0.000),(0.383,0.924,0.000,0.000),(0.000,0.000,1.000,0.000),(0.000,0.000,0.000,1.000))
(2.006,2.996,0.000)
Media3D
0.924,0.383,0.000,0.000,-0.383,0.924,0.000,0.000,0.000,0.000,1.000,0.000,0.000,0.000,0.000,1.000
2.006,2.996,0.000

 

 

 



Gilles Chanteau
Programmation AutoCAD LISP/.NET
GileCAD
GitHub

0 Likes
Message 4 of 4

dhaverstick
Advocate
Advocate

Gilles, you are correct! I did not go any further in my testing because I wanted an explanation about the discrepancy first. It appears that it all works out in the end so no harm, no foul.

 

You are also correct that the correct term is one matrix is the transpose of the other. I'm not sure why the Media3D version uses a method called "Invert" to return the transpose.

 

Thanks for your help!

 

Darren Haverstick

0 Likes