# d3dx9: Implementation of D3DXMatrixDecompose and tests

Mon Aug 4 16:25:34 CDT 2008

```Hi,

Some more remarks:

+#include "d3d9.h" is unneeded: already set in d3dx9.h.

why not use the more common syntax: pointer->field instead of
(*pointer).field?

+#define module(vx,vy,vz) sqrt((vx)*(vx)+(vy)*(vy)+(vz)*
>
> (vz)) can be avoided by calling d3dxVector3Length.
>

/*Let's calculate rotation now*/
+    /*If some of the scale factors is zero, it is impossible to obtain the
rotation*/
+    if (((*poutscale).x==0)||((*poutscale).y==0)||((*poutscale).z==0))
+    {
+        return D3DERR_INVALIDCALL;
+    }
+    /*We calculate the different products with simple sums of the members
of the 3x3 submatrix
+        Quaternion (x,y,z,w) to Matrix:
+        | 1-2y2-2z2  2xy-2wz    2xz+2wy   |
+        | 2xy+2wz    1-2x2-2z2  2yz-wx    |
+        | 2xz-2wy    2yz+2wx    1-2x2-2y2 |*/
+    wx=-((*pM).m[2][1]/(*poutscale).z-(*pM).m[1][2]/(*poutscale).y)/4;
+    wy=-((*pM).m[0][2]/(*poutscale).x-(*pM).m[2][0]/(*poutscale).z)/4;
+    wz=-((*pM).m[1][0]/(*poutscale).y-(*pM).m[0][1]/(*poutscale).x)/4;
+    xy=((*pM).m[0][1]/(*poutscale).x+(*pM).m[1][0]/(*poutscale).y)/4;
+    xz=((*pM).m[0][2]/(*poutscale).x+(*pM).m[2][0]/(*poutscale).z)/4;
+    yz=((*pM).m[1][2]/(*poutscale).y+(*pM).m[2][1]/(*poutscale).z)/4;
+
xx=(1+(*pM).m[0][0]/(*poutscale).x-(*pM).m[1][1]/(*poutscale).y-(*pM).m[2][2]/(*poutscale).z)/4;
+
yy=(1-(*pM).m[0][0]/(*poutscale).x+(*pM).m[1][1]/(*poutscale).y-(*pM).m[2][2]/(*poutscale).z)/4;
+
zz=(1-(*pM).m[0][0]/(*poutscale).x-(*pM).m[1][1]/(*poutscale).y+(*pM).m[2][2]/(*poutscale).z)/4;
+    /*Then we go for the biggest component in order to minimise division
error as commonly explained in the literacy.
+      We just calculate one component from the diagonal to avoid sign loss
of the square root. The first sign is not important
+      because the rotations (x,y,z,w) and (-x,-y,-z,-w) are equivalent. But
if we calc ulated the other two components we would have
+      to choose and could be wrong.*/
+    if ((xx>yy) && (xx>zz))
+    {
+        (*poutrotation).x=sqrt(xx);
+        (*poutrotation).y=xy/(*poutrotation).x;
+        (*poutrotation).z=xz/(*poutrotation).x;
+        (*poutrotation).w=wx/(*poutrotation).x;
+        return S_OK;
+    }
+    if (yy>zz)
+    {
+        (*poutrotation).y=sqrt(yy);
+        (*poutrotation).x=xy/(*poutrotation).y;
+        (*poutrotation).z=yz/(*poutrotation).y;
+        (*poutrotation).w=wy/(*poutrotation).y;
+        return S_OK;
+    }
+    (*poutrotation).z=sqrt(zz);
+    (*poutrotation).x=xz/(*poutrotation).z;
+    (*poutrotation).y=yz/(*poutrotation).z;
+    (*poutrotation).w=wz/(*poutrotation).z;
+    return S_OK;
+}
+
can be avoided by calling D3DXQuaternionRotationMatrix

You could avoid useless explicit allocation of memory by writing
D3DXMATRIX pM; instead of D3DXMATRIX *pM;......
and then D3DXMatrixDecompose(&poutscale, &poutrotation, &pouttranslation,
pouttranslation, pM);

David

2008/8/4 H. Verbeet <hverbeet at gmail.com>

> 2008/8/4 Luis Busquets <luis.busquets at ilidium.com>:
> > +#include "d3d9.h"
> ...
> > +#define module(vx,vy,vz) sqrt((vx)*(vx)+(vy)*(vy)+(vz)*(vz))
> ...
> > +#include "stdio.h"
>
> You don't really need those.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.winehq.org/pipermail/wine-patches/attachments/20080804/6cbd2e23/attachment.htm
```