本文主要是介绍一个可复用的C++ 3阶实方阵类和4阶实方阵类(兼容与扩展了DX中的4阶实方阵类);四元数(quaternion)模板类的使用,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
一个可复用的C++ 3阶实方阵类和4阶实方阵类(兼容与扩展了DX中的4阶实方阵类)
部分DX矩阵函数的实现
namespace Han
{
FLOAT WINAPI D3DXMatrixDeterminant(CONST D3DXMATRIX *pM)
{
D3DXMATRIX mtx=*pM;
FLOAT ret=Bsdet(&mtx(0,0),4);//第一个参数是输入输出参数
return ret;
}
D3DXMATRIX* D3DXMatrixIdentity(D3DXMATRIX *pOut)
{
//identity matrix
memset(pOut,0,sizeof(D3DXMATRIX));
pOut->m[0][0]=1;
pOut->m[1][1]=1;
pOut->m[2][2]=1;
pOut->m[3][3]=1;
return pOut;
}
//Build a matrix which scales by (sx,sy,sz)
D3DXMATRIX* D3DXMatrixScaling(D3DXMATRIX *pOut,FLOAT sx,FLOAT sy,FLOAT sz)
{
//创建一个沿着X,Y和Z轴方向缩放矩阵
memset(pOut,0,sizeof(D3DXMATRIX));
pOut->m[0][0]=sx;
pOut->m[1][1]=sy;
pOut->m[2][2]=sz;
pOut->m[3][3]=1;
return pOut;
}
//Build a matrix which translates by (x,y,z)
D3DXMATRIX* D3DXMatrixTranslation(D3DXMATRIX *pOut,FLOAT x,FLOAT y,FLOAT z)
{
//创建一个沿着X,Y和Z轴方向平移矩阵
memset(pOut,0,sizeof(D3DXMATRIX));
pOut->m[0][0]=1;
pOut->m[1][1]=1;
pOut->m[2][2]=1;
pOut->m[3][3]=1;
pOut->m[3][0]=x;
pOut->m[3][1]=y;
pOut->m[3][2]=z;
return pOut;
}
//Build a matrix which rotates around the X axis
D3DXMATRIX* D3DXMatrixRotationX(D3DXMATRIX *pOut,FLOAT Angle)
{
//创建一个绕X轴旋转Angle弧度的旋转矩阵
memset(pOut,0,sizeof(D3DXMATRIX));
pOut->m[0][0]=1;
pOut->m[1][1]=cos(Angle);
pOut->m[2][2]=cos(Angle);
pOut->m[3][3]=1;
pOut->m[1][2]=sin(Angle);
pOut->m[2][1]=-sin(Angle);
return pOut;
}
//Build a matrix which rotates around the Y axis
D3DXMATRIX* D3DXMatrixRotationY(D3DXMATRIX *pOut,FLOAT Angle)
{
//创建一个绕Y轴旋转Angle弧度的旋转矩阵
memset(pOut,0,sizeof(D3DXMATRIX));
pOut->m[0][0]=cos(Angle);
pOut->m[1][1]=1;
pOut->m[2][2]=cos(Angle);
pOut->m[3][3]=1;
pOut->m[0][2]=-sin(Angle);
pOut->m[2][0]=sin(Angle);
return pOut;
}
//Build a matrix which rotates around the Z axis
D3DXMATRIX* D3DXMatrixRotationZ(D3DXMATRIX *pOut,FLOAT Angle)
{
//创建一个绕Z轴旋转Angle弧度的旋转矩阵
memset(pOut,0,sizeof(D3DXMATRIX));
pOut->m[0][0]=cos(Angle);
pOut->m[1][1]=cos(Angle);
pOut->m[2][2]=1;
pOut->m[3][3]=1;
pOut->m[0][1]=sin(Angle);
pOut->m[1][0]=-sin(Angle);
return pOut;
}
//Transform (x,y,z,1) by matrix
D3DXVECTOR4* D3DXVec3Transform(D3DXVECTOR4 *pOut,CONST D3DXVECTOR4 *pV,CONST D3DXMATRIX *pM)
{
#if 1
//MV
D3DXVECTOR4 v(pV->x,pV->y,pV->z,1);
pOut->x=(*pM)(0,0)*v.x+(*pM)(0,1)*v.y+(*pM)(0,2)*v.z+(*pM)(0,3)*v.w;
pOut->y=(*pM)(1,0)*v.x+(*pM)(1,1)*v.y+(*pM)(1,2)*v.z+(*pM)(1,3)*v.w;
pOut->z=(*pM)(2,0)*v.x+(*pM)(2,1)*v.y+(*pM)(2,2)*v.z+(*pM)(2,3)*v.w;
pOut->w=(*pM)(3,0)*v.x+(*pM)(3,1)*v.y+(*pM)(3,2)*v.z+(*pM)(3,3)*v.w;
return pOut;
#else
//VM
D3DXVECTOR4 v(pV->x,pV->y,pV->z,1);
pOut->x=(*pM)(0,0)*v.x+(*pM)(1,0)*v.y+(*pM)(2,0)*v.z+(*pM)(3,0)*v.w;
pOut->y=(*pM)(0,1)*v.x+(*pM)(1,1)*v.y+(*pM)(2,1)*v.z+(*pM)(3,1)*v.w;
pOut->z=(*pM)(0,2)*v.x+(*pM)(1,2)*v.y+(*pM)(2,2)*v.z+(*pM)(3,2)*v.w;
pOut->w=(*pM)(0,3)*v.x+(*pM)(1,3)*v.y+(*pM)(2,3)*v.z+(*pM)(3,3)*v.w;
return pOut;
#endif
return NULL;
}
};
----实矩阵乘法----
-3,1,-1,
-7,5,-1,
-6,6,-2,
-3,1,-1,
-7,5,-1,
-6,6,-2,
8,-4,4,
-8,12,4,
-12,12,4,
----实矩阵求逆----
-0.25,-0.25,0.25,
-0.5,3.47694e-008,0.25,
-0.75,0.75,-0.5,
-3,1,-1,
-7,5,-1,
-6,6,-2,
----实矩阵求行列式的值----
16
16
// 矩阵标准API(实矩阵相乘,Bsdet求实方阵的行列式值,求实方阵的逆)的C++封装,一个可复用的C++ 3阶方阵类
//
#include"stdafx.h"
#include<cmath>
#include<iostream>
using namespace std;
template<class T>
void __stdcall Brmul(T *a,T *b,int m,int n,int k,T *c)
{
int i,j,l,u;
for (i=0; i<=m-1; i++)
for (j=0; j<=k-1; j++)
{
u=i*k+j;
c[u]=0.0;
for(l=0; l<=n-1; l++)
c[u]=c[u]+a[i*n+l]*b[l*k+j];
}
return;
}
template<class T>
//第一个参数是输入输出参数
T __stdcall Bsdet(T *a,int n)
{
int i,j,k,is,js,l,u,v;
T f,det,q,d;
f=1.0; det=1.0;
for (k=0; k<=n-2; k++)
{
q=0.0;
for (i=k; i<=n-1; i++)
for (j=k; j<=n-1; j++)
{
l=i*n+j;
d=fabs(a[l]);
if (d>q)
{
q=d;
is=i;
js=j;
}
}
if(q+1.0==1.0)
{
det=0.0;
return(det);
}
if(is!=k)
{
f=-f;
for (j=k; j<=n-1; j++)
{
u=k*n+j;
v=is*n+j;
d=a[u];
a[u]=a[v];
a[v]=d;
}
}
if(js!=k)
{
f=-f;
for (i=k; i<=n-1; i++)
{
u=i*n+js;
v=i*n+k;
d=a[u];
a[u]=a[v];
a[v]=d;
}
}
l=k*n+k;
det=det*a[l];
for (i=k+1; i<=n-1; i++)
{
d=a[i*n+k]/a[l];
for (j=k+1; j<=n-1; j++)
{
u=i*n+j;
a[u]=a[u]-d*a[k*n+j];
}
}
}
det=f*det*a[n*n-1];
return(det);
}
template<class T>
int __stdcall Brinv(T *a,int n)
{
int *is,*js,i,j,k,l,u,v;
T d,p;
is=(int*)malloc(n*sizeof(int));
js=(int*)malloc(n*sizeof(int));
for (k=0; k<=n-1; k++)
{
d=0.0;
for (i=k; i<=n-1; i++)
for (j=k; j<=n-1; j++)
{
l=i*n+j;
p=fabs(a[l]);
if (p>d)
{
d=p;
is[k]=i;
js[k]=j;
}
}
if (d+1.0==1.0)
{
free(is);
free(js);
printf("err**not inv\n");
return(0);
}
if (is[k]!=k)
for (j=0; j<=n-1; j++)
{
u=k*n+j;
v=is[k]*n+j;
p=a[u];
a[u]=a[v];
a[v]=p;
}
if (js[k]!=k)
for (i=0; i<=n-1; i++)
{
u=i*n+k;
v=i*n+js[k];
p=a[u];
a[u]=a[v];
a[v]=p;
}
l=k*n+k;
a[l]=1.0/a[l];
for (j=0; j<=n-1; j++)
if (j!=k)
{
u=k*n+j;
a[u]=a[u]*a[l];
}
for (i=0; i<=n-1; i++)
if (i!=k)
for (j=0; j<=n-1; j++)
if (j!=k)
{
u=i*n+j;
a[u]=a[u]-a[i*n+k]*a[k*n+j];
}
for (i=0; i<=n-1; i++)
if (i!=k)
{
u=i*n+k;
a[u]=-a[u]*a[l];
}
}
for(k=n-1; k>=0; k--)
{
if (js[k]!=k)
for (j=0; j<=n-1; j++)
{
u=k*n+j;
v=js[k]*n+j;
p=a[u];
a[u]=a[v];
a[v]=p;
}
if (is[k]!=k)
for (i=0; i<=n-1; i++)
{
u=i*n+k;
v=i*n+is[k];
p=a[u];
a[u]=a[v];
a[v]=p;
}
}
free(is);
free(js);
return(1);
}
// From gamasutra. This file may follow different licence features.
// A floating point number
//
typedef float SCALAR;
//
// A 3D vector
//
class VECTOR
{
public:
SCALAR x,y,z; //x,y,z coordinates
public:
VECTOR() : x(0), y(0), z(0) {}
VECTOR( const SCALAR& a, const SCALAR& b, const SCALAR& c ) : x(a), y(b), z(c) {}
//index a component
//NOTE: returning a reference allows
//you to assign the indexed element
SCALAR& operator [] ( const long i )
{
return *((&x) + i);
}
//compare
这篇关于一个可复用的C++ 3阶实方阵类和4阶实方阵类(兼容与扩展了DX中的4阶实方阵类);四元数(quaternion)模板类的使用的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!