hostsupport/hostopenvg/src/src/riMath.cpp
branchbug235_bringup_0
changeset 54 067180f57b12
parent 53 c2ef9095503a
child 55 09263774e342
equal deleted inserted replaced
53:c2ef9095503a 54:067180f57b12
     1 /*------------------------------------------------------------------------
       
     2  *
       
     3  * OpenVG 1.1 Reference Implementation
       
     4  * -----------------------------------
       
     5  *
       
     6  * Copyright (c) 2007 The Khronos Group Inc.
       
     7  * Portions copyright (c) 2010 Nokia Corporation and/or its subsidiary(-ies).
       
     8  *
       
     9  * Permission is hereby granted, free of charge, to any person obtaining a
       
    10  * copy of this software and /or associated documentation files
       
    11  * (the "Materials "), to deal in the Materials without restriction,
       
    12  * including without limitation the rights to use, copy, modify, merge,
       
    13  * publish, distribute, sublicense, and/or sell copies of the Materials,
       
    14  * and to permit persons to whom the Materials are furnished to do so,
       
    15  * subject to the following conditions: 
       
    16  *
       
    17  * The above copyright notice and this permission notice shall be included 
       
    18  * in all copies or substantial portions of the Materials. 
       
    19  *
       
    20  * THE MATERIALS ARE PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
       
    21  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
       
    22  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
       
    23  * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
       
    24  * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
       
    25  * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE MATERIALS OR
       
    26  * THE USE OR OTHER DEALINGS IN THE MATERIALS.
       
    27  *
       
    28  *//**
       
    29  * \file
       
    30  * \brief	Implementation of non-inline matrix functions.
       
    31  * \note	
       
    32  *//*-------------------------------------------------------------------*/
       
    33 
       
    34 #include "riDefs.h"
       
    35 #include "riMath.h"
       
    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 
       
    51 namespace OpenVGRI
       
    52 {
       
    53 
       
    54 /*-------------------------------------------------------------------*//*!
       
    55 * \brief	Inverts a 3x3 matrix. Returns false if the matrix is singular.
       
    56 * \param	
       
    57 * \return	
       
    58 * \note		
       
    59 *//*-------------------------------------------------------------------*/
       
    60 
       
    61 bool Matrix3x3::invert()
       
    62 {
       
    63     // \todo Save computation on affine matrices?
       
    64 	bool affine = isAffine();
       
    65 	RIfloat det00 = matrix[1][1]*matrix[2][2] - matrix[2][1]*matrix[1][2];
       
    66 	RIfloat det01 = matrix[2][0]*matrix[1][2] - matrix[1][0]*matrix[2][2];
       
    67 	RIfloat det02 = matrix[1][0]*matrix[2][1] - matrix[2][0]*matrix[1][1];
       
    68 
       
    69 	RIfloat d = matrix[0][0]*det00 + matrix[0][1]*det01 + matrix[0][2]*det02;
       
    70 	if( d == 0.0f ) return false;	//singular, leave the matrix unmodified and return false
       
    71 	d = 1.0f / d;
       
    72 
       
    73 	Matrix3x3 t;
       
    74 
       
    75     // \note There is some bug (in GCC?) in accessing matrix elements: If data
       
    76     // is accessed like: t[i][j], then the following will produce incorrect
       
    77     // resulst on optimized builds. If the data is accessed through t.matrix,
       
    78     // then the output is correct. Debug build works correctly, and if print
       
    79     // calls are inserted, the code also works correctly. The context to get
       
    80     // this bug appear are fill paints (linear and radial gradient test
       
    81     // functions).
       
    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]);
       
    94 	if(affine)
       
    95 		t[2].set(0,0,1);	//affine matrix stays affine
       
    96 	*this = t;
       
    97 	return true;
       
    98 }
       
    99 
       
   100 //==============================================================================================
       
   101 
       
   102 }	//namespace OpenVGRI