Exemple #1
0
//void dMatrix::PolarDecomposition (dMatrix& orthogonal, dMatrix& symetric) const
void dMatrix::PolarDecomposition (dMatrix & transformMatrix, dVector & scale, dMatrix & stretchAxis, const dMatrix & initialStretchAxis) const
{
   // a polar decomposition decompose matrix A = O * S
   // where S = sqrt (transpose (L) * L)
   // calculate transpose (L) * L
   dMatrix LL ((*this) * Transpose());
   // check is this si a pure uniformScale * rotation * translation
   dFloat det2 = (LL[0][0] + LL[1][1] + LL[2][2]) * (1.0f / 3.0f);
   dFloat invdet2 = 1.0f / det2;
   dMatrix pureRotation (LL);
   pureRotation[0] = pureRotation[0].Scale (invdet2);
   pureRotation[1] = pureRotation[1].Scale (invdet2);
   pureRotation[2] = pureRotation[2].Scale (invdet2);
   //dFloat soureSign = ((*this)[0] * (*this)[1]) % (*this)[2];
   dFloat sign = ((((*this)[0] * (*this)[1]) % (*this)[2]) > 0.0f) ? 1.0f : -1.0f;
   dFloat det = (pureRotation[0] * pureRotation[1]) % pureRotation[2];
   if (dAbs (det - 1.0f) < 1.e-5f)
   {
      // this is a pure scale * rotation * translation
      det = sign * dSqrt (det2);
      scale[0] = det;
      scale[1] = det;
      scale[2] = det;
      scale[3] = 1.0f;
      det = 1.0f / det;
      transformMatrix.m_front = m_front.Scale (det);
      transformMatrix.m_up = m_up.Scale (det);
      transformMatrix.m_right = m_right.Scale (det);
      transformMatrix[0][3] = 0.0f;
      transformMatrix[1][3] = 0.0f;
      transformMatrix[2][3] = 0.0f;
      transformMatrix.m_posit = m_posit;
      stretchAxis = dGetIdentityMatrix();
   }
   else
   {
      stretchAxis = LL.JacobiDiagonalization (scale, initialStretchAxis);
      // I need to deal with buy seeing of some of the Scale are duplicated
      // do this later (maybe by a given rotation around the non uniform axis but I do not know if it will work)
      // for now just us the matrix
      scale[0] = sign * dSqrt (scale[0]);
      scale[1] = sign * dSqrt (scale[1]);
      scale[2] = sign * dSqrt (scale[2]);
      scale[3] = 1.0f;
      dMatrix scaledAxis;
      scaledAxis[0] = stretchAxis[0].Scale (1.0f / scale[0]);
      scaledAxis[1] = stretchAxis[1].Scale (1.0f / scale[1]);
      scaledAxis[2] = stretchAxis[2].Scale (1.0f / scale[2]);
      scaledAxis[3] = stretchAxis[3];
      dMatrix symetricInv (stretchAxis.Transpose() * scaledAxis);
      transformMatrix = symetricInv * (*this);
      transformMatrix.m_posit = m_posit;
   }
}
void dgMatrix::PolarDecomposition (dgMatrix& transformMatrix, dgVector& scale, dgMatrix& stretchAxis, const dgMatrix* const initialStretchAxis) const
{
	// a polar decomposition decompose matrix A = O * S
	// where S = sqrt (transpose (L) * L)

/*
	// calculate transpose (L) * L 
	dgMatrix LL ((*this) * Transpose());

	// check is this is a pure uniformScale * rotation * translation
	dgFloat32 det2 = (LL[0][0] + LL[1][1] + LL[2][2]) * dgFloat32 (1.0f / 3.0f);

	dgFloat32 invdet2 = 1.0f / det2;

	dgMatrix pureRotation (LL);
	pureRotation[0] = pureRotation[0].Scale3 (invdet2);
	pureRotation[1] = pureRotation[1].Scale3 (invdet2);
	pureRotation[2] = pureRotation[2].Scale3 (invdet2);

	dgFloat32 sign = ((((*this)[0] * (*this)[1]) % (*this)[2]) > 0.0f) ? 1.0f : -1.0f;
	dgFloat32 det = (pureRotation[0] * pureRotation[1]) % pureRotation[2];
	if (dgAbsf (det - dgFloat32 (1.0f)) < dgFloat32 (1.0e-5f)) {
		// this is a pure scale * rotation * translation
		det = sign * dgSqrt (det2);
		scale[0] = det;
		scale[1] = det;
		scale[2] = det;
		det = dgFloat32 (1.0f)/ det;
		transformMatrix.m_front = m_front.Scale3 (det);
		transformMatrix.m_up = m_up.Scale3 (det);
		transformMatrix.m_right = m_right.Scale3 (det);
		transformMatrix[0][3] = dgFloat32 (0.0f);
		transformMatrix[1][3] = dgFloat32 (0.0f);
		transformMatrix[2][3] = dgFloat32 (0.0f);
		transformMatrix.m_posit = m_posit;
		stretchAxis = dgGetIdentityMatrix();

	} else {
		stretchAxis = LL;
		stretchAxis.EigenVectors (scale);

		// I need to deal with by seeing of some of the Scale are duplicated
		// do this later (maybe by a given rotation around the non uniform axis but I do not know if it will work)
		// for now just us the matrix

		scale[0] = sign * dgSqrt (scale[0]);
		scale[1] = sign * dgSqrt (scale[1]);
		scale[2] = sign * dgSqrt (scale[2]);
		scale[3] = dgFloat32 (0.0f);

		dgMatrix scaledAxis;
		scaledAxis[0] = stretchAxis[0].Scale3 (dgFloat32 (1.0f) / scale[0]);
		scaledAxis[1] = stretchAxis[1].Scale3 (dgFloat32 (1.0f) / scale[1]);
		scaledAxis[2] = stretchAxis[2].Scale3 (dgFloat32 (1.0f) / scale[2]);
		scaledAxis[3] = stretchAxis[3];
		dgMatrix symetricInv (stretchAxis.Transpose() * scaledAxis);

		transformMatrix = symetricInv * (*this);
		transformMatrix.m_posit = m_posit;
	}
*/

// test the f*****g factorization 
dgMatrix xxxxx(dgRollMatrix(30.0f * 3.1416f / 180.0f));
xxxxx = dgYawMatrix(30.0f * 3.1416f / 180.0f) * xxxxx;
dgMatrix xxxxx1(dgGetIdentityMatrix());
xxxxx1[0][0] = 2.0f;
dgMatrix xxxxx2(xxxxx.Inverse() * xxxxx1 * xxxxx);
dgMatrix xxxxx3 (xxxxx2);
xxxxx2.EigenVectors(scale);
dgMatrix xxxxx4(xxxxx2.Inverse() * xxxxx1 * xxxxx2);


	//dgFloat32 sign = ((((*this)[0] * (*this)[1]) % (*this)[2]) > 0.0f) ? 1.0f : -1.0f;
	dgFloat32 sign = dgSign(((*this)[0] * (*this)[1]) % (*this)[2]);
	stretchAxis = (*this) * Transpose();
	stretchAxis.EigenVectors (scale);

	// I need to deal with by seeing of some of the Scale are duplicated
	// do this later (maybe by a given rotation around the non uniform axis but I do not know if it will work)
	// for now just us the matrix

	scale[0] = sign * dgSqrt (scale[0]);
	scale[1] = sign * dgSqrt (scale[1]);
	scale[2] = sign * dgSqrt (scale[2]);
	scale[3] = dgFloat32 (0.0f);

	dgMatrix scaledAxis;
	scaledAxis[0] = stretchAxis[0].Scale3 (dgFloat32 (1.0f) / scale[0]);
	scaledAxis[1] = stretchAxis[1].Scale3 (dgFloat32 (1.0f) / scale[1]);
	scaledAxis[2] = stretchAxis[2].Scale3 (dgFloat32 (1.0f) / scale[2]);
	scaledAxis[3] = stretchAxis[3];
	dgMatrix symetricInv (stretchAxis.Transpose() * scaledAxis);

	transformMatrix = symetricInv * (*this);
	transformMatrix.m_posit = m_posit;

}