egl/sfopenvg/riMath.cpp
branchEGL_MERGE
changeset 180 f767bd5f4cfc
parent 119 5f371025658c
child 181 c1509651cd2b
equal deleted inserted replaced
119:5f371025658c 180:f767bd5f4cfc
     1 /*------------------------------------------------------------------------
       
     2  *
       
     3  * OpenVG 1.1 Reference Implementation
       
     4  * -----------------------------------
       
     5  *
       
     6  * Copyright (c) 2007 The Khronos Group Inc.
       
     7  *
       
     8  * Permission is hereby granted, free of charge, to any person obtaining a
       
     9  * copy of this software and /or associated documentation files
       
    10  * (the "Materials "), to deal in the Materials without restriction,
       
    11  * including without limitation the rights to use, copy, modify, merge,
       
    12  * publish, distribute, sublicense, and/or sell copies of the Materials,
       
    13  * and to permit persons to whom the Materials are furnished to do so,
       
    14  * subject to the following conditions: 
       
    15  *
       
    16  * The above copyright notice and this permission notice shall be included 
       
    17  * in all copies or substantial portions of the Materials. 
       
    18  *
       
    19  * THE MATERIALS ARE PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
       
    20  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
       
    21  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
       
    22  * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
       
    23  * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
       
    24  * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE MATERIALS OR
       
    25  * THE USE OR OTHER DEALINGS IN THE MATERIALS.
       
    26  *
       
    27  *//**
       
    28  * \file
       
    29  * \brief	Implementation of non-inline matrix functions.
       
    30  * \note	
       
    31  *//*-------------------------------------------------------------------*/
       
    32 
       
    33 #include "riDefs.h"
       
    34 #include "riMath.h"
       
    35 
       
    36 namespace OpenVGRI
       
    37 {
       
    38 
       
    39 /*-------------------------------------------------------------------*//*!
       
    40 * \brief	Inverts a 3x3 matrix. Returns false if the matrix is singular.
       
    41 * \param	
       
    42 * \return	
       
    43 * \note		
       
    44 *//*-------------------------------------------------------------------*/
       
    45 
       
    46 bool Matrix3x3::invert()
       
    47 {
       
    48 	bool affine = isAffine();
       
    49 	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];
       
    51 	RIfloat det02 = matrix[1][0]*matrix[2][1] - matrix[2][0]*matrix[1][1];
       
    52 
       
    53 	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
       
    55 	d = 1.0f / d;
       
    56 
       
    57 	Matrix3x3 t;
       
    58 	t[0][0] = d * det00;
       
    59 	t[1][0] = d * det01;
       
    60 	t[2][0] = d * det02;
       
    61 	t[0][1] = d * (matrix[2][1]*matrix[0][2] - matrix[0][1]*matrix[2][2]);
       
    62 	t[1][1] = d * (matrix[0][0]*matrix[2][2] - matrix[2][0]*matrix[0][2]);
       
    63 	t[2][1] = d * (matrix[2][0]*matrix[0][1] - matrix[0][0]*matrix[2][1]);
       
    64 	t[0][2] = d * (matrix[0][1]*matrix[1][2] - matrix[1][1]*matrix[0][2]);
       
    65 	t[1][2] = d * (matrix[1][0]*matrix[0][2] - matrix[0][0]*matrix[1][2]);
       
    66 	t[2][2] = d * (matrix[0][0]*matrix[1][1] - matrix[1][0]*matrix[0][1]);
       
    67 	if(affine)
       
    68 		t[2].set(0,0,1);	//affine matrix stays affine
       
    69 	*this = t;
       
    70 	return true;
       
    71 }
       
    72 
       
    73 //==============================================================================================
       
    74 
       
    75 }	//namespace OpenVGRI