cogl/matrix: Invert matrices with Graphene

This commit is contained in:
Georges Basile Stavracas Neto 2019-02-27 16:32:37 -03:00
parent 8e2658a601
commit c938b25d6a
No known key found for this signature in database
GPG Key ID: 886C17EE170D1385

View File

@ -405,749 +405,20 @@ cogl_debug_matrix_print (const CoglMatrix *matrix)
_cogl_matrix_prefix_print ("", matrix);
}
/*
* References an element of 4x4 matrix.
*
* @m matrix array.
* @c column of the desired element.
* @r row of the desired element.
*
* Returns: value of the desired element.
*
* Calculate the linear storage index of the element and references it.
*/
#define MAT(m,r,c) (m)[(c)*4+(r)]
/*
* Swaps the values of two floating pointer variables.
*
* Used by invert_matrix_general() to swap the row pointers.
*/
#define SWAP_ROWS(a, b) { float *_tmp = a; (a)=(b); (b)=_tmp; }
/*
* Compute inverse of 4x4 transformation matrix.
*
* @mat pointer to a CoglMatrix structure. The matrix inverse will be
* stored in the CoglMatrix::inv attribute.
*
* Returns: %TRUE for success, %FALSE for failure (\p singular matrix).
*
* \author
* Code contributed by Jacques Leroy jle@star.be
*
* Calculates the inverse matrix by performing the gaussian matrix reduction
* with partial pivoting followed by back/substitution with the loops manually
* unrolled.
*/
static gboolean
invert_matrix_general (CoglMatrix *matrix)
{
const float *m = (float *)matrix;
float *out = matrix->inv;
float wtmp[4][8];
float m0, m1, m2, m3, s;
float *r0, *r1, *r2, *r3;
r0 = wtmp[0], r1 = wtmp[1], r2 = wtmp[2], r3 = wtmp[3];
r0[0] = MAT (m, 0, 0), r0[1] = MAT (m, 0, 1),
r0[2] = MAT (m, 0, 2), r0[3] = MAT (m, 0, 3),
r0[4] = 1.0, r0[5] = r0[6] = r0[7] = 0.0,
r1[0] = MAT (m, 1, 0), r1[1] = MAT (m, 1, 1),
r1[2] = MAT (m, 1, 2), r1[3] = MAT (m, 1, 3),
r1[5] = 1.0, r1[4] = r1[6] = r1[7] = 0.0,
r2[0] = MAT (m, 2, 0), r2[1] = MAT (m, 2, 1),
r2[2] = MAT (m, 2, 2), r2[3] = MAT (m, 2, 3),
r2[6] = 1.0, r2[4] = r2[5] = r2[7] = 0.0,
r3[0] = MAT (m, 3, 0), r3[1] = MAT (m, 3, 1),
r3[2] = MAT (m, 3, 2), r3[3] = MAT (m, 3, 3),
r3[7] = 1.0, r3[4] = r3[5] = r3[6] = 0.0;
/* choose pivot - or die */
if (fabsf (r3[0]) > fabsf (r2[0]))
SWAP_ROWS (r3, r2);
if (fabsf (r2[0]) > fabsf (r1[0]))
SWAP_ROWS (r2, r1);
if (fabsf (r1[0]) > fabsf (r0[0]))
SWAP_ROWS (r1, r0);
if (0.0 == r0[0])
return FALSE;
/* eliminate first variable */
m1 = r1[0]/r0[0]; m2 = r2[0]/r0[0]; m3 = r3[0]/r0[0];
s = r0[1]; r1[1] -= m1 * s; r2[1] -= m2 * s; r3[1] -= m3 * s;
s = r0[2]; r1[2] -= m1 * s; r2[2] -= m2 * s; r3[2] -= m3 * s;
s = r0[3]; r1[3] -= m1 * s; r2[3] -= m2 * s; r3[3] -= m3 * s;
s = r0[4];
if (s != 0.0) { r1[4] -= m1 * s; r2[4] -= m2 * s; r3[4] -= m3 * s; }
s = r0[5];
if (s != 0.0) { r1[5] -= m1 * s; r2[5] -= m2 * s; r3[5] -= m3 * s; }
s = r0[6];
if (s != 0.0) { r1[6] -= m1 * s; r2[6] -= m2 * s; r3[6] -= m3 * s; }
s = r0[7];
if (s != 0.0) { r1[7] -= m1 * s; r2[7] -= m2 * s; r3[7] -= m3 * s; }
/* choose pivot - or die */
if (fabsf (r3[1]) > fabsf (r2[1]))
SWAP_ROWS (r3, r2);
if (fabsf (r2[1]) > fabsf (r1[1]))
SWAP_ROWS (r2, r1);
if (0.0 == r1[1])
return FALSE;
/* eliminate second variable */
m2 = r2[1] / r1[1]; m3 = r3[1] / r1[1];
r2[2] -= m2 * r1[2]; r3[2] -= m3 * r1[2];
r2[3] -= m2 * r1[3]; r3[3] -= m3 * r1[3];
s = r1[4]; if (0.0 != s) { r2[4] -= m2 * s; r3[4] -= m3 * s; }
s = r1[5]; if (0.0 != s) { r2[5] -= m2 * s; r3[5] -= m3 * s; }
s = r1[6]; if (0.0 != s) { r2[6] -= m2 * s; r3[6] -= m3 * s; }
s = r1[7]; if (0.0 != s) { r2[7] -= m2 * s; r3[7] -= m3 * s; }
/* choose pivot - or die */
if (fabsf (r3[2]) > fabsf (r2[2]))
SWAP_ROWS (r3, r2);
if (0.0 == r2[2])
return FALSE;
/* eliminate third variable */
m3 = r3[2] / r2[2];
r3[3] -= m3 * r2[3], r3[4] -= m3 * r2[4],
r3[5] -= m3 * r2[5], r3[6] -= m3 * r2[6],
r3[7] -= m3 * r2[7];
/* last check */
if (0.0 == r3[3])
return FALSE;
s = 1.0f / r3[3]; /* now back substitute row 3 */
r3[4] *= s; r3[5] *= s; r3[6] *= s; r3[7] *= s;
m2 = r2[3]; /* now back substitute row 2 */
s = 1.0f / r2[2];
r2[4] = s * (r2[4] - r3[4] * m2), r2[5] = s * (r2[5] - r3[5] * m2),
r2[6] = s * (r2[6] - r3[6] * m2), r2[7] = s * (r2[7] - r3[7] * m2);
m1 = r1[3];
r1[4] -= r3[4] * m1, r1[5] -= r3[5] * m1,
r1[6] -= r3[6] * m1, r1[7] -= r3[7] * m1;
m0 = r0[3];
r0[4] -= r3[4] * m0, r0[5] -= r3[5] * m0,
r0[6] -= r3[6] * m0, r0[7] -= r3[7] * m0;
m1 = r1[2]; /* now back substitute row 1 */
s = 1.0f / r1[1];
r1[4] = s * (r1[4] - r2[4] * m1), r1[5] = s * (r1[5] - r2[5] * m1),
r1[6] = s * (r1[6] - r2[6] * m1), r1[7] = s * (r1[7] - r2[7] * m1);
m0 = r0[2];
r0[4] -= r2[4] * m0, r0[5] -= r2[5] * m0,
r0[6] -= r2[6] * m0, r0[7] -= r2[7] * m0;
m0 = r0[1]; /* now back substitute row 0 */
s = 1.0f / r0[0];
r0[4] = s * (r0[4] - r1[4] * m0), r0[5] = s * (r0[5] - r1[5] * m0),
r0[6] = s * (r0[6] - r1[6] * m0), r0[7] = s * (r0[7] - r1[7] * m0);
MAT (out, 0, 0) = r0[4]; MAT (out, 0, 1) = r0[5],
MAT (out, 0, 2) = r0[6]; MAT (out, 0, 3) = r0[7],
MAT (out, 1, 0) = r1[4]; MAT (out, 1, 1) = r1[5],
MAT (out, 1, 2) = r1[6]; MAT (out, 1, 3) = r1[7],
MAT (out, 2, 0) = r2[4]; MAT (out, 2, 1) = r2[5],
MAT (out, 2, 2) = r2[6]; MAT (out, 2, 3) = r2[7],
MAT (out, 3, 0) = r3[4]; MAT (out, 3, 1) = r3[5],
MAT (out, 3, 2) = r3[6]; MAT (out, 3, 3) = r3[7];
return TRUE;
}
#undef SWAP_ROWS
/*
* Compute inverse of a general 3d transformation matrix.
*
* @mat pointer to a CoglMatrix structure. The matrix inverse will be
* stored in the CoglMatrix::inv attribute.
*
* Returns: %TRUE for success, %FALSE for failure (\p singular matrix).
*
* \author Adapted from graphics gems II.
*
* Calculates the inverse of the upper left by first calculating its
* determinant and multiplying it to the symmetric adjust matrix of each
* element. Finally deals with the translation part by transforming the
* original translation vector using by the calculated submatrix inverse.
*/
static gboolean
invert_matrix_3d_general (CoglMatrix *matrix)
{
const float *in = (float *)matrix;
float *out = matrix->inv;
float pos, neg, t;
float det;
/* Calculate the determinant of upper left 3x3 submatrix and
* determine if the matrix is singular.
*/
pos = neg = 0.0;
t = MAT (in,0,0) * MAT (in,1,1) * MAT (in,2,2);
if (t >= 0.0) pos += t; else neg += t;
t = MAT (in,1,0) * MAT (in,2,1) * MAT (in,0,2);
if (t >= 0.0) pos += t; else neg += t;
t = MAT (in,2,0) * MAT (in,0,1) * MAT (in,1,2);
if (t >= 0.0) pos += t; else neg += t;
t = -MAT (in,2,0) * MAT (in,1,1) * MAT (in,0,2);
if (t >= 0.0) pos += t; else neg += t;
t = -MAT (in,1,0) * MAT (in,0,1) * MAT (in,2,2);
if (t >= 0.0) pos += t; else neg += t;
t = -MAT (in,0,0) * MAT (in,2,1) * MAT (in,1,2);
if (t >= 0.0) pos += t; else neg += t;
det = pos + neg;
if (det*det < 1e-25)
return FALSE;
det = 1.0f / det;
MAT (out,0,0) =
( (MAT (in, 1, 1)*MAT (in, 2, 2) - MAT (in, 2, 1)*MAT (in, 1, 2) )*det);
MAT (out,0,1) =
(- (MAT (in, 0, 1)*MAT (in, 2, 2) - MAT (in, 2, 1)*MAT (in, 0, 2) )*det);
MAT (out,0,2) =
( (MAT (in, 0, 1)*MAT (in, 1, 2) - MAT (in, 1, 1)*MAT (in, 0, 2) )*det);
MAT (out,1,0) =
(- (MAT (in,1,0)*MAT (in,2,2) - MAT (in,2,0)*MAT (in,1,2) )*det);
MAT (out,1,1) =
( (MAT (in,0,0)*MAT (in,2,2) - MAT (in,2,0)*MAT (in,0,2) )*det);
MAT (out,1,2) =
(- (MAT (in,0,0)*MAT (in,1,2) - MAT (in,1,0)*MAT (in,0,2) )*det);
MAT (out,2,0) =
( (MAT (in,1,0)*MAT (in,2,1) - MAT (in,2,0)*MAT (in,1,1) )*det);
MAT (out,2,1) =
(- (MAT (in,0,0)*MAT (in,2,1) - MAT (in,2,0)*MAT (in,0,1) )*det);
MAT (out,2,2) =
( (MAT (in,0,0)*MAT (in,1,1) - MAT (in,1,0)*MAT (in,0,1) )*det);
/* Do the translation part */
MAT (out,0,3) = - (MAT (in, 0, 3) * MAT (out, 0, 0) +
MAT (in, 1, 3) * MAT (out, 0, 1) +
MAT (in, 2, 3) * MAT (out, 0, 2) );
MAT (out,1,3) = - (MAT (in, 0, 3) * MAT (out, 1, 0) +
MAT (in, 1, 3) * MAT (out, 1, 1) +
MAT (in, 2, 3) * MAT (out, 1, 2) );
MAT (out,2,3) = - (MAT (in, 0, 3) * MAT (out, 2 ,0) +
MAT (in, 1, 3) * MAT (out, 2, 1) +
MAT (in, 2, 3) * MAT (out, 2, 2) );
return TRUE;
}
/*
* Compute inverse of a 3d transformation matrix.
*
* @mat pointer to a CoglMatrix structure. The matrix inverse will be
* stored in the CoglMatrix::inv attribute.
*
* Returns: %TRUE for success, %FALSE for failure (\p singular matrix).
*
* If the matrix is not an angle preserving matrix then calls
* invert_matrix_3d_general for the actual calculation. Otherwise calculates
* the inverse matrix analyzing and inverting each of the scaling, rotation and
* translation parts.
*/
static gboolean
invert_matrix_3d (CoglMatrix *matrix)
{
const float *in = (float *)matrix;
float *out = matrix->inv;
memcpy (out, identity, 16 * sizeof (float));
if (!TEST_MAT_FLAGS(matrix, MAT_FLAGS_ANGLE_PRESERVING))
return invert_matrix_3d_general (matrix);
if (matrix->flags & MAT_FLAG_UNIFORM_SCALE)
{
float scale = (MAT (in, 0, 0) * MAT (in, 0, 0) +
MAT (in, 0, 1) * MAT (in, 0, 1) +
MAT (in, 0, 2) * MAT (in, 0, 2));
if (scale == 0.0)
return FALSE;
scale = 1.0f / scale;
/* Transpose and scale the 3 by 3 upper-left submatrix. */
MAT (out, 0, 0) = scale * MAT (in, 0, 0);
MAT (out, 1, 0) = scale * MAT (in, 0, 1);
MAT (out, 2, 0) = scale * MAT (in, 0, 2);
MAT (out, 0, 1) = scale * MAT (in, 1, 0);
MAT (out, 1, 1) = scale * MAT (in, 1, 1);
MAT (out, 2, 1) = scale * MAT (in, 1, 2);
MAT (out, 0, 2) = scale * MAT (in, 2, 0);
MAT (out, 1, 2) = scale * MAT (in, 2, 1);
MAT (out, 2, 2) = scale * MAT (in, 2, 2);
}
else if (matrix->flags & MAT_FLAG_ROTATION)
{
/* Transpose the 3 by 3 upper-left submatrix. */
MAT (out, 0, 0) = MAT (in, 0, 0);
MAT (out, 1, 0) = MAT (in, 0, 1);
MAT (out, 2, 0) = MAT (in, 0, 2);
MAT (out, 0, 1) = MAT (in, 1, 0);
MAT (out, 1, 1) = MAT (in, 1, 1);
MAT (out, 2, 1) = MAT (in, 1, 2);
MAT (out, 0, 2) = MAT (in, 2, 0);
MAT (out, 1, 2) = MAT (in, 2, 1);
MAT (out, 2, 2) = MAT (in, 2, 2);
}
else
{
/* pure translation */
memcpy (out, identity, 16 * sizeof (float));
MAT (out, 0, 3) = - MAT (in, 0, 3);
MAT (out, 1, 3) = - MAT (in, 1, 3);
MAT (out, 2, 3) = - MAT (in, 2, 3);
return TRUE;
}
if (matrix->flags & MAT_FLAG_TRANSLATION)
{
/* Do the translation part */
MAT (out,0,3) = - (MAT (in, 0, 3) * MAT (out, 0, 0) +
MAT (in, 1, 3) * MAT (out, 0, 1) +
MAT (in, 2, 3) * MAT (out, 0, 2) );
MAT (out,1,3) = - (MAT (in, 0, 3) * MAT (out, 1, 0) +
MAT (in, 1, 3) * MAT (out, 1, 1) +
MAT (in, 2, 3) * MAT (out, 1, 2) );
MAT (out,2,3) = - (MAT (in, 0, 3) * MAT (out, 2, 0) +
MAT (in, 1, 3) * MAT (out, 2, 1) +
MAT (in, 2, 3) * MAT (out, 2, 2) );
}
else
MAT (out, 0, 3) = MAT (out, 1, 3) = MAT (out, 2, 3) = 0.0;
return TRUE;
}
/*
* Compute inverse of an identity transformation matrix.
*
* @mat pointer to a CoglMatrix structure. The matrix inverse will be
* stored in the CoglMatrix::inv attribute.
*
* Returns: always %TRUE.
*
* Simply copies identity into CoglMatrix::inv.
*/
static gboolean
invert_matrix_identity (CoglMatrix *matrix)
{
memcpy (matrix->inv, identity, 16 * sizeof (float));
return TRUE;
}
/*
* Compute inverse of a no-rotation 3d transformation matrix.
*
* @mat pointer to a CoglMatrix structure. The matrix inverse will be
* stored in the CoglMatrix::inv attribute.
*
* Returns: %TRUE for success, %FALSE for failure (\p singular matrix).
*
* Calculates the
*/
static gboolean
invert_matrix_3d_no_rotation (CoglMatrix *matrix)
{
const float *in = (float *)matrix;
float *out = matrix->inv;
if (MAT (in,0,0) == 0 || MAT (in,1,1) == 0 || MAT (in,2,2) == 0)
return FALSE;
memcpy (out, identity, 16 * sizeof (float));
MAT (out,0,0) = 1.0f / MAT (in,0,0);
MAT (out,1,1) = 1.0f / MAT (in,1,1);
MAT (out,2,2) = 1.0f / MAT (in,2,2);
if (matrix->flags & MAT_FLAG_TRANSLATION)
{
MAT (out,0,3) = - (MAT (in,0,3) * MAT (out,0,0));
MAT (out,1,3) = - (MAT (in,1,3) * MAT (out,1,1));
MAT (out,2,3) = - (MAT (in,2,3) * MAT (out,2,2));
}
return TRUE;
}
/*
* Compute inverse of a no-rotation 2d transformation matrix.
*
* @mat pointer to a CoglMatrix structure. The matrix inverse will be
* stored in the CoglMatrix::inv attribute.
*
* Returns: %TRUE for success, %FALSE for failure (\p singular matrix).
*
* Calculates the inverse matrix by applying the inverse scaling and
* translation to the identity matrix.
*/
static gboolean
invert_matrix_2d_no_rotation (CoglMatrix *matrix)
{
const float *in = (float *)matrix;
float *out = matrix->inv;
if (MAT (in, 0, 0) == 0 || MAT (in, 1, 1) == 0)
return FALSE;
memcpy (out, identity, 16 * sizeof (float));
MAT (out, 0, 0) = 1.0f / MAT (in, 0, 0);
MAT (out, 1, 1) = 1.0f / MAT (in, 1, 1);
if (matrix->flags & MAT_FLAG_TRANSLATION)
{
MAT (out, 0, 3) = - (MAT (in, 0, 3) * MAT (out, 0, 0));
MAT (out, 1, 3) = - (MAT (in, 1, 3) * MAT (out, 1, 1));
}
return TRUE;
}
#if 0
/* broken */
static gboolean
invert_matrix_perspective (CoglMatrix *matrix)
{
const float *in = matrix;
float *out = matrix->inv;
if (MAT (in,2,3) == 0)
return FALSE;
memcpy( out, identity, 16 * sizeof(float) );
MAT (out, 0, 0) = 1.0f / MAT (in, 0, 0);
MAT (out, 1, 1) = 1.0f / MAT (in, 1, 1);
MAT (out, 0, 3) = MAT (in, 0, 2);
MAT (out, 1, 3) = MAT (in, 1, 2);
MAT (out,2,2) = 0;
MAT (out,2,3) = -1;
MAT (out,3,2) = 1.0f / MAT (in,2,3);
MAT (out,3,3) = MAT (in,2,2) * MAT (out,3,2);
return TRUE;
}
#endif
/*
* Matrix inversion function pointer type.
*/
typedef gboolean (*inv_mat_func)(CoglMatrix *matrix);
/*
* Table of the matrix inversion functions according to the matrix type.
*/
static inv_mat_func inv_mat_tab[7] = {
invert_matrix_general,
invert_matrix_identity,
invert_matrix_3d_no_rotation,
#if 0
/* Don't use this function for now - it fails when the projection matrix
* is premultiplied by a translation (ala Chromium's tilesort SPU).
*/
invert_matrix_perspective,
#else
invert_matrix_general,
#endif
invert_matrix_3d, /* lazy! */
invert_matrix_2d_no_rotation,
invert_matrix_3d
};
#define ZERO(x) (1<<x)
#define ONE(x) (1<<(x+16))
#define MASK_NO_TRX (ZERO(12) | ZERO(13) | ZERO(14))
#define MASK_NO_2D_SCALE ( ONE(0) | ONE(5))
#define MASK_IDENTITY ( ONE(0) | ZERO(4) | ZERO(8) | ZERO(12) |\
ZERO(1) | ONE(5) | ZERO(9) | ZERO(13) |\
ZERO(2) | ZERO(6) | ONE(10) | ZERO(14) |\
ZERO(3) | ZERO(7) | ZERO(11) | ONE(15) )
#define MASK_2D_NO_ROT ( ZERO(4) | ZERO(8) | \
ZERO(1) | ZERO(9) | \
ZERO(2) | ZERO(6) | ONE(10) | ZERO(14) |\
ZERO(3) | ZERO(7) | ZERO(11) | ONE(15) )
#define MASK_2D ( ZERO(8) | \
ZERO(9) | \
ZERO(2) | ZERO(6) | ONE(10) | ZERO(14) |\
ZERO(3) | ZERO(7) | ZERO(11) | ONE(15) )
#define MASK_3D_NO_ROT ( ZERO(4) | ZERO(8) | \
ZERO(1) | ZERO(9) | \
ZERO(2) | ZERO(6) | \
ZERO(3) | ZERO(7) | ZERO(11) | ONE(15) )
#define MASK_3D ( \
\
\
ZERO(3) | ZERO(7) | ZERO(11) | ONE(15) )
#define MASK_PERSPECTIVE ( ZERO(4) | ZERO(12) |\
ZERO(1) | ZERO(13) |\
ZERO(2) | ZERO(6) | \
ZERO(3) | ZERO(7) | ZERO(15) )
#define SQ(x) ((x)*(x))
/*
* Determine type and flags from scratch.
*
* This is expensive enough to only want to do it once.
*/
static void
analyse_from_scratch (CoglMatrix *matrix)
{
const float *m = (float *)matrix;
unsigned int mask = 0;
unsigned int i;
for (i = 0 ; i < 16 ; i++)
{
if (m[i] == 0.0) mask |= (1<<i);
}
if (m[0] == 1.0f) mask |= (1<<16);
if (m[5] == 1.0f) mask |= (1<<21);
if (m[10] == 1.0f) mask |= (1<<26);
if (m[15] == 1.0f) mask |= (1<<31);
matrix->flags &= ~MAT_FLAGS_GEOMETRY;
/* Check for translation - no-one really cares
*/
if ((mask & MASK_NO_TRX) != MASK_NO_TRX)
matrix->flags |= MAT_FLAG_TRANSLATION;
/* Do the real work
*/
if (mask == (unsigned int) MASK_IDENTITY)
matrix->type = COGL_MATRIX_TYPE_IDENTITY;
else if ((mask & MASK_2D_NO_ROT) == (unsigned int) MASK_2D_NO_ROT)
{
matrix->type = COGL_MATRIX_TYPE_2D_NO_ROT;
if ((mask & MASK_NO_2D_SCALE) != MASK_NO_2D_SCALE)
matrix->flags |= MAT_FLAG_GENERAL_SCALE;
}
else if ((mask & MASK_2D) == (unsigned int) MASK_2D)
{
float mm = DOT2 (m, m);
float m4m4 = DOT2 (m+4,m+4);
float mm4 = DOT2 (m,m+4);
matrix->type = COGL_MATRIX_TYPE_2D;
/* Check for scale */
if (SQ (mm-1) > SQ (1e-6) ||
SQ (m4m4-1) > SQ (1e-6))
matrix->flags |= MAT_FLAG_GENERAL_SCALE;
/* Check for rotation */
if (SQ (mm4) > SQ (1e-6))
matrix->flags |= MAT_FLAG_GENERAL_3D;
else
matrix->flags |= MAT_FLAG_ROTATION;
}
else if ((mask & MASK_3D_NO_ROT) == (unsigned int) MASK_3D_NO_ROT)
{
matrix->type = COGL_MATRIX_TYPE_3D_NO_ROT;
/* Check for scale */
if (SQ (m[0]-m[5]) < SQ (1e-6) &&
SQ (m[0]-m[10]) < SQ (1e-6))
{
if (SQ (m[0]-1.0) > SQ (1e-6))
matrix->flags |= MAT_FLAG_UNIFORM_SCALE;
}
else
matrix->flags |= MAT_FLAG_GENERAL_SCALE;
}
else if ((mask & MASK_3D) == (unsigned int) MASK_3D)
{
float c1 = DOT3 (m,m);
float c2 = DOT3 (m+4,m+4);
float c3 = DOT3 (m+8,m+8);
float d1 = DOT3 (m, m+4);
float cp[3];
matrix->type = COGL_MATRIX_TYPE_3D;
/* Check for scale */
if (SQ (c1-c2) < SQ (1e-6) && SQ (c1-c3) < SQ (1e-6))
{
if (SQ (c1-1.0) > SQ (1e-6))
matrix->flags |= MAT_FLAG_UNIFORM_SCALE;
/* else no scale at all */
}
else
matrix->flags |= MAT_FLAG_GENERAL_SCALE;
/* Check for rotation */
if (SQ (d1) < SQ (1e-6))
{
CROSS3 ( cp, m, m+4);
SUB_3V ( cp, cp, (m+8));
if (LEN_SQUARED_3FV(cp) < SQ(1e-6))
matrix->flags |= MAT_FLAG_ROTATION;
else
matrix->flags |= MAT_FLAG_GENERAL_3D;
}
else
matrix->flags |= MAT_FLAG_GENERAL_3D; /* shear, etc */
}
else if ((mask & MASK_PERSPECTIVE) == MASK_PERSPECTIVE && m[11]==-1.0f)
{
matrix->type = COGL_MATRIX_TYPE_PERSPECTIVE;
matrix->flags |= MAT_FLAG_GENERAL;
}
else
{
matrix->type = COGL_MATRIX_TYPE_GENERAL;
matrix->flags |= MAT_FLAG_GENERAL;
}
}
/*
* Analyze a matrix given that its flags are accurate.
*
* This is the more common operation, hopefully.
*/
static void
analyse_from_flags (CoglMatrix *matrix)
{
const float *m = (float *)matrix;
if (TEST_MAT_FLAGS(matrix, 0))
matrix->type = COGL_MATRIX_TYPE_IDENTITY;
else if (TEST_MAT_FLAGS(matrix, (MAT_FLAG_TRANSLATION |
MAT_FLAG_UNIFORM_SCALE |
MAT_FLAG_GENERAL_SCALE)))
{
if ( m[10] == 1.0f && m[14] == 0.0f )
matrix->type = COGL_MATRIX_TYPE_2D_NO_ROT;
else
matrix->type = COGL_MATRIX_TYPE_3D_NO_ROT;
}
else if (TEST_MAT_FLAGS (matrix, MAT_FLAGS_3D))
{
if ( m[ 8]==0.0f
&& m[ 9]==0.0f
&& m[2]==0.0f && m[6]==0.0f && m[10]==1.0f && m[14]==0.0f)
{
matrix->type = COGL_MATRIX_TYPE_2D;
}
else
matrix->type = COGL_MATRIX_TYPE_3D;
}
else if ( m[4]==0.0f && m[12]==0.0f
&& m[1]==0.0f && m[13]==0.0f
&& m[2]==0.0f && m[6]==0.0f
&& m[3]==0.0f && m[7]==0.0f && m[11]==-1.0f && m[15]==0.0f)
{
matrix->type = COGL_MATRIX_TYPE_PERSPECTIVE;
}
else
matrix->type = COGL_MATRIX_TYPE_GENERAL;
}
/*
* Analyze and update the type and flags of a matrix.
*
* If the matrix type is dirty then calls either analyse_from_scratch() or
* analyse_from_flags() to determine its type, according to whether the flags
* are dirty or not, respectively. If the matrix has an inverse and it's dirty
* then calls matrix_invert(). Finally clears the dirty flags.
*/
static void
_cogl_matrix_update_type_and_flags (CoglMatrix *matrix)
{
if (matrix->flags & MAT_DIRTY_TYPE)
{
if (matrix->flags & MAT_DIRTY_FLAGS)
analyse_from_scratch (matrix);
else
analyse_from_flags (matrix);
}
matrix->flags &= ~(MAT_DIRTY_FLAGS | MAT_DIRTY_TYPE);
}
/*
* Compute inverse of a transformation matrix.
*
* @mat pointer to a CoglMatrix structure. The matrix inverse will be
* stored in the CoglMatrix::inv attribute.
*
* Returns: %TRUE for success, %FALSE for failure (\p singular matrix).
*
* Calls the matrix inversion function in inv_mat_tab corresponding to the
* given matrix type. In case of failure, updates the MAT_FLAG_SINGULAR flag,
* and copies the identity matrix into CoglMatrix::inv.
*/
static gboolean
_cogl_matrix_update_inverse (CoglMatrix *matrix)
{
if (matrix->flags & MAT_DIRTY_FLAGS ||
matrix->flags & MAT_DIRTY_INVERSE)
{
_cogl_matrix_update_type_and_flags (matrix);
if (inv_mat_tab[matrix->type](matrix))
matrix->flags &= ~MAT_FLAG_SINGULAR;
else
{
matrix->flags |= MAT_FLAG_SINGULAR;
memcpy (matrix->inv, identity, 16 * sizeof (float));
}
matrix->flags &= ~MAT_DIRTY_INVERSE;
}
if (matrix->flags & MAT_FLAG_SINGULAR)
return FALSE;
else
return TRUE;
}
gboolean
cogl_matrix_get_inverse (const CoglMatrix *matrix, CoglMatrix *inverse)
{
if (_cogl_matrix_update_inverse ((CoglMatrix *)matrix))
{
cogl_matrix_init_from_array (inverse, matrix->inv);
return TRUE;
}
else
{
cogl_matrix_init_identity (inverse);
return FALSE;
}
graphene_matrix_t m;
gboolean success;
cogl_matrix_to_graphene_matrix (matrix, &m);
success = graphene_matrix_inverse (&m, &m);
if (!success)
graphene_matrix_init_identity (&m);
graphene_matrix_to_cogl_matrix (&m, inverse);
return success;
}
/*