egl/sfopenvg/riMath.cpp
branchEGL_MERGE
changeset 88 a5a3a8cb368e
parent 57 2bf8a359aa2f
equal deleted inserted replaced
86:841b49c57c50 88:a5a3a8cb368e
     2  *
     2  *
     3  * OpenVG 1.1 Reference Implementation
     3  * OpenVG 1.1 Reference Implementation
     4  * -----------------------------------
     4  * -----------------------------------
     5  *
     5  *
     6  * Copyright (c) 2007 The Khronos Group Inc.
     6  * Copyright (c) 2007 The Khronos Group Inc.
       
     7  * Portions Copyright (c) 2010 Nokia Corporation and/or its subsidiary(-ies).
     7  *
     8  *
     8  * Permission is hereby granted, free of charge, to any person obtaining a
     9  * Permission is hereby granted, free of charge, to any person obtaining a
     9  * copy of this software and /or associated documentation files
    10  * copy of this software and /or associated documentation files
    10  * (the "Materials "), to deal in the Materials without restriction,
    11  * (the "Materials "), to deal in the Materials without restriction,
    11  * including without limitation the rights to use, copy, modify, merge,
    12  * including without limitation the rights to use, copy, modify, merge,
    31  *//*-------------------------------------------------------------------*/
    32  *//*-------------------------------------------------------------------*/
    32 
    33 
    33 #include "riDefs.h"
    34 #include "riDefs.h"
    34 #include "riMath.h"
    35 #include "riMath.h"
    35 
    36 
       
    37 #if 0
       
    38 #include <stdio.h>
       
    39 
       
    40 static void printMatrix(const Matrix3x3& m)
       
    41 {
       
    42     // For tracing a bug in matrix inverse in release-builds.
       
    43     for(int i = 0; i < 3; i++)
       
    44     {
       
    45         printf("[%.4f %.4f %.4f]\n", m[i][0], m[i][1], m[i][2]);
       
    46     }
       
    47 }
       
    48 
       
    49 #endif
       
    50 
    36 namespace OpenVGRI
    51 namespace OpenVGRI
    37 {
    52 {
    38 
    53 
    39 /*-------------------------------------------------------------------*//*!
    54 /*-------------------------------------------------------------------*//*!
    40 * \brief	Inverts a 3x3 matrix. Returns false if the matrix is singular.
    55 * \brief	Inverts a 3x3 matrix. Returns false if the matrix is singular.
    43 * \note		
    58 * \note		
    44 *//*-------------------------------------------------------------------*/
    59 *//*-------------------------------------------------------------------*/
    45 
    60 
    46 bool Matrix3x3::invert()
    61 bool Matrix3x3::invert()
    47 {
    62 {
       
    63     // \todo Save computation on affine matrices?
    48 	bool affine = isAffine();
    64 	bool affine = isAffine();
    49 	RIfloat det00 = matrix[1][1]*matrix[2][2] - matrix[2][1]*matrix[1][2];
    65 	RIfloat det00 = matrix[1][1]*matrix[2][2] - matrix[2][1]*matrix[1][2];
    50 	RIfloat det01 = matrix[2][0]*matrix[1][2] - matrix[1][0]*matrix[2][2];
    66 	RIfloat det01 = matrix[2][0]*matrix[1][2] - matrix[1][0]*matrix[2][2];
    51 	RIfloat det02 = matrix[1][0]*matrix[2][1] - matrix[2][0]*matrix[1][1];
    67 	RIfloat det02 = matrix[1][0]*matrix[2][1] - matrix[2][0]*matrix[1][1];
    52 
    68 
    53 	RIfloat d = matrix[0][0]*det00 + matrix[0][1]*det01 + matrix[0][2]*det02;
    69 	RIfloat d = matrix[0][0]*det00 + matrix[0][1]*det01 + matrix[0][2]*det02;
    54 	if( d == 0.0f ) return false;	//singular, leave the matrix unmodified and return false
    70 	if( d == 0.0f ) return false;	//singular, leave the matrix unmodified and return false
    55 	d = 1.0f / d;
    71 	d = 1.0f / d;
    56 
    72 
    57 	Matrix3x3 t;
    73 	Matrix3x3 t;
    58 	t[0][0] = d * det00;
    74 
    59 	t[1][0] = d * det01;
    75     // \note There is some bug (in GCC?) in accessing matrix elements: If data
    60 	t[2][0] = d * det02;
    76     // is accessed like: t[i][j], then the following will produce incorrect
    61 	t[0][1] = d * (matrix[2][1]*matrix[0][2] - matrix[0][1]*matrix[2][2]);
    77     // resulst on optimized builds. If the data is accessed through t.matrix,
    62 	t[1][1] = d * (matrix[0][0]*matrix[2][2] - matrix[2][0]*matrix[0][2]);
    78     // then the output is correct. Debug build works correctly, and if print
    63 	t[2][1] = d * (matrix[2][0]*matrix[0][1] - matrix[0][0]*matrix[2][1]);
    79     // calls are inserted, the code also works correctly. The context to get
    64 	t[0][2] = d * (matrix[0][1]*matrix[1][2] - matrix[1][1]*matrix[0][2]);
    80     // this bug appear are fill paints (linear and radial gradient test
    65 	t[1][2] = d * (matrix[1][0]*matrix[0][2] - matrix[0][0]*matrix[1][2]);
    81     // functions).
    66 	t[2][2] = d * (matrix[0][0]*matrix[1][1] - matrix[1][0]*matrix[0][1]);
    82 
       
    83 	t.matrix[0][0] = d * det00;
       
    84 	t.matrix[1][0] = d * det01;
       
    85 	t.matrix[2][0] = d * det02;
       
    86     //printf("t\n");
       
    87     //printMatrix(t);
       
    88 	t.matrix[0][1] = d * (matrix[2][1]*matrix[0][2] - matrix[0][1]*matrix[2][2]);
       
    89 	t.matrix[1][1] = d * (matrix[0][0]*matrix[2][2] - matrix[2][0]*matrix[0][2]);
       
    90 	t.matrix[2][1] = d * (matrix[2][0]*matrix[0][1] - matrix[0][0]*matrix[2][1]);
       
    91 	t.matrix[0][2] = d * (matrix[0][1]*matrix[1][2] - matrix[1][1]*matrix[0][2]);
       
    92 	t.matrix[1][2] = d * (matrix[1][0]*matrix[0][2] - matrix[0][0]*matrix[1][2]);
       
    93 	t.matrix[2][2] = d * (matrix[0][0]*matrix[1][1] - matrix[1][0]*matrix[0][1]);
    67 	if(affine)
    94 	if(affine)
    68 		t[2].set(0,0,1);	//affine matrix stays affine
    95 		t[2].set(0,0,1);	//affine matrix stays affine
    69 	*this = t;
    96 	*this = t;
    70 	return true;
    97 	return true;
    71 }
    98 }