任给一个法向量vNormal,以下代码就会给出一个唯一确定的坐标系。
AcGeVector vNormal;
AcGeMatrix3d mTmpMatrix;
mTmpMatrix = mTmpMatrix.planeToWorld(vNormal);
AcGePoint3d ptOriginTemp;
AcGeVector3d xAxisTemp;
AcGeVector3d yAxisTemp;
AcGeVector3d zAxisTemp;
mTmpMatrix.getCoordSystem(ptOriginTemp, xAxisTemp, yAxisTemp, zAxisTemp);
数学上来说,一个法向量只能确定一个平面,那autocad是如何求出一个固定的坐标系的呢?应该是基于一个固定的规则或习惯求出,比如给一个法向量(0,0,1), 就可以得到俯视图的坐标系;给定一个法向量(0,-1,0),就可以得到前视图的坐标系等等。
static inline AcGeMatrix3d GetOCSMatrix(const AcGeVector3d& ocs)
{
AcGeMatrix3d mRetMatrix;
static const double one_64th = 1.0 / 64.0;
if (ocs == AcGeVector3d(0, 0, 1))//不加这个,后续求出的也是一样的,此处仅仅是优化速度。
{
return mRetMatrix;
}
AcGeVector3d ax(1, 0, 0), ay(0, 1, 0), az(0, 0, 1);
AcGeVector3d ocsaxis(ocs);
ocsaxis.normalize();
if (fabs(ocsaxis.x) < one_64th && fabs(ocsaxis.y) < one_64th) {
ax = ay.crossProduct( ocsaxis );//结合俯视图、仰视图理解
}
else {
ax = az.crossProduct( ocsaxis );//结合前后左右视图理解
}
ax.normalize();
ay = ocsaxis.crossProduct(ax);
ay.normalize();
mRetMatrix.entry[0][0] = ax.x;
mRetMatrix.entry[1][0] = ax.y;
mRetMatrix.entry[2][0] = ax.z;
mRetMatrix.entry[3][0] = 0;
mRetMatrix.entry[0][1] = ay.x;
mRetMatrix.entry[1][1] = ay.y;
mRetMatrix.entry[2][1] = ay.z;
mRetMatrix.entry[3][1] = 0;
mRetMatrix.entry[0][2] = ocsaxis.x;
mRetMatrix.entry[1][2] = ocsaxis.y;
mRetMatrix.entry[2][2] = ocsaxis.z;
mRetMatrix.entry[3][2] = 0;
mRetMatrix.entry[0][3] = 0;
mRetMatrix.entry[1][3] = 0;
mRetMatrix.entry[2][3] = 0;
mRetMatrix.entry[3][3] = 1;
return mRetMatrix;
}
vNormal = ( 0, 0, 1),俯视图坐标系x=(1,0,0), y=(0,1,0), z=(0,0,1)
vNormal=(0, 0, -1),仰视图坐标系x=(-1,0,0), y=(0,1,0), z=(0,0,-1)
vNormal=(1, 0, 0),右视图坐标系x=(0,1,0), y=(0,0,1), z=(1, 0, 0)
vNormal=(-1,0,0),左视图坐标系x=(0,-1,0), y=(0,0,1), z=(-1, 0, 0)
vNormal=(0,1,0),后视图坐标系x=(1,0,0), y=(0,0,1), z=(0,1,0)
vNormal=(0,-1,0),前视图坐标系x=(-1,0,0), y=(0,0,1), z=(0,-1,0)