egl/sfopenvg/riMath.h
branchEGL_MERGE
changeset 88 a5a3a8cb368e
parent 57 2bf8a359aa2f
equal deleted inserted replaced
86:841b49c57c50 88:a5a3a8cb368e
     5  *
     5  *
     6  * OpenVG 1.1 Reference Implementation
     6  * OpenVG 1.1 Reference Implementation
     7  * -----------------------------------
     7  * -----------------------------------
     8  *
     8  *
     9  * Copyright (c) 2007 The Khronos Group Inc.
     9  * Copyright (c) 2007 The Khronos Group Inc.
       
    10  * Portions Copyright (c) 2010 Nokia Corporation and/or its subsidiary(-ies).
    10  *
    11  *
    11  * Permission is hereby granted, free of charge, to any person obtaining a
    12  * Permission is hereby granted, free of charge, to any person obtaining a
    12  * copy of this software and /or associated documentation files
    13  * copy of this software and /or associated documentation files
    13  * (the "Materials "), to deal in the Materials without restriction,
    14  * (the "Materials "), to deal in the Materials without restriction,
    14  * including without limitation the rights to use, copy, modify, merge,
    15  * including without limitation the rights to use, copy, modify, merge,
    15  * publish, distribute, sublicense, and/or sell copies of the Materials,
    16  * publish, distribute, sublicense, and/or sell copies of the Materials,
    16  * and to permit persons to whom the Materials are furnished to do so,
    17  * and to permit persons to whom the Materials are furnished to do so,
    17  * subject to the following conditions: 
    18  * subject to the following conditions:
    18  *
    19  *
    19  * The above copyright notice and this permission notice shall be included 
    20  * The above copyright notice and this permission notice shall be included
    20  * in all copies or substantial portions of the Materials. 
    21  * in all copies or substantial portions of the Materials.
    21  *
    22  *
    22  * THE MATERIALS ARE PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
    23  * THE MATERIALS ARE PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
    23  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
    24  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
    24  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
    25  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
    25  * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
    26  * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
    28  * THE USE OR OTHER DEALINGS IN THE MATERIALS.
    29  * THE USE OR OTHER DEALINGS IN THE MATERIALS.
    29  *
    30  *
    30  *//**
    31  *//**
    31  * \file
    32  * \file
    32  * \brief	Math functions, Vector and Matrix classes.
    33  * \brief	Math functions, Vector and Matrix classes.
    33  * \note	
    34  * \note
    34  *//*-------------------------------------------------------------------*/
    35  *//*-------------------------------------------------------------------*/
    35 
    36 
    36 #ifndef __RIDEFS_H
    37 #ifndef __RIDEFS_H
    37 #include "riDefs.h"
    38 #include "riDefs.h"
    38 #endif
    39 #endif
    41 
    42 
    42 namespace OpenVGRI
    43 namespace OpenVGRI
    43 {
    44 {
    44 
    45 
    45 /*-------------------------------------------------------------------*//*!
    46 /*-------------------------------------------------------------------*//*!
    46 * \brief	
    47 * \brief
    47 * \param	
    48 * \param
    48 * \return	
    49 * \return
    49 * \note		
    50 * \note
    50 *//*-------------------------------------------------------------------*/
    51 *//*-------------------------------------------------------------------*/
    51 
    52 
    52 RI_INLINE int		RI_ISNAN(float a)
    53 RI_INLINE int		RI_ISNAN(float a)
    53 {
    54 {
    54 	RIfloatInt p;
    55     RIfloatInt p;
    55 	p.f = a;
    56     p.f = a;
    56 	unsigned int exponent = (p.i>>23) & 0xff;
    57     unsigned int exponent = (p.i>>23) & 0xff;
    57 	unsigned int mantissa = p.i & 0x7fffff;
    58     unsigned int mantissa = p.i & 0x7fffff;
    58 	if(exponent == 255 && mantissa)
    59     if(exponent == 255 && mantissa)
    59 		return 1;
    60         return 1;
    60 	return 0;
    61     return 0;
    61 }
    62 }
    62 
    63 
    63 #if (RI_MANTISSA_BITS > 23)
    64 #if (RI_MANTISSA_BITS > 23)
    64 #error RI_MANTISSA_BITS is greater than 23
    65 #error RI_MANTISSA_BITS is greater than 23
    65 #elif (RI_EXPONENT_BITS > 8)
    66 #elif (RI_EXPONENT_BITS > 8)
    67 #elif (RI_MANTISSA_BITS != 23) || (RI_EXPONENT_BITS != 8)
    68 #elif (RI_MANTISSA_BITS != 23) || (RI_EXPONENT_BITS != 8)
    68 
    69 
    69 class RIfloat
    70 class RIfloat
    70 {
    71 {
    71 public:
    72 public:
    72 	RIfloat() : v(0.0f)						{ removeBits(); }
    73     RIfloat() : v(0.0f)						{ removeBits(); }
    73 	RIfloat(float a) : v(a)					{ removeBits(); }
    74     RIfloat(float a) : v(a)					{ removeBits(); }
    74 	RIfloat(double a) : v((float)a)			{ removeBits(); }
    75     RIfloat(double a) : v((float)a)			{ removeBits(); }
    75 	RIfloat(int a) : v((float)a)			{ removeBits(); }
    76     RIfloat(int a) : v((float)a)			{ removeBits(); }
    76 	RIfloat(unsigned int a) : v((float)a)	{ removeBits(); }
    77     RIfloat(unsigned int a) : v((float)a)	{ removeBits(); }
    77 	RIfloat&	operator=(const RIfloat &a)	{ v = a.v; removeBits(); return *this; }
    78     RIfloat&	operator=(const RIfloat &a)	{ v = a.v; removeBits(); return *this; }
    78 	RIfloat&	operator+=(const RIfloat &a){ v += a.v; removeBits(); return *this; }
    79     RIfloat&	operator+=(const RIfloat &a){ v += a.v; removeBits(); return *this; }
    79 	RIfloat&	operator-=(const RIfloat &a){ v -= a.v; removeBits(); return *this; }
    80     RIfloat&	operator-=(const RIfloat &a){ v -= a.v; removeBits(); return *this; }
    80 	RIfloat&	operator*=(const RIfloat &a){ v *= a.v; removeBits(); return *this; }
    81     RIfloat&	operator*=(const RIfloat &a){ v *= a.v; removeBits(); return *this; }
    81 	RIfloat&	operator/=(const RIfloat &a){ v /= a.v; removeBits(); return *this; }
    82     RIfloat&	operator/=(const RIfloat &a){ v /= a.v; removeBits(); return *this; }
    82 	RIfloat		operator-() const			{ return -v; }
    83     RIfloat		operator-() const			{ return -v; }
    83 	operator float() const					{ return v; }
    84     operator float() const					{ return v; }
    84 	operator double() const					{ return (double)v; }
    85     operator double() const					{ return (double)v; }
    85 	operator int() const					{ return (int)v; }
    86     operator int() const					{ return (int)v; }
    86 
    87 
    87 	friend RIfloat	operator+(const RIfloat &a, const RIfloat &b);
    88     friend RIfloat	operator+(const RIfloat &a, const RIfloat &b);
    88 	friend RIfloat	operator+(float a, const RIfloat &b);
    89     friend RIfloat	operator+(float a, const RIfloat &b);
    89 	friend RIfloat	operator+(const RIfloat &a, float b);
    90     friend RIfloat	operator+(const RIfloat &a, float b);
    90 	friend RIfloat	operator-(const RIfloat &a, const RIfloat &b);
    91     friend RIfloat	operator-(const RIfloat &a, const RIfloat &b);
    91 	friend RIfloat	operator-(float a, const RIfloat &b);
    92     friend RIfloat	operator-(float a, const RIfloat &b);
    92 	friend RIfloat	operator-(const RIfloat &a, float b);
    93     friend RIfloat	operator-(const RIfloat &a, float b);
    93 	friend RIfloat	operator*(const RIfloat &a, const RIfloat &b);
    94     friend RIfloat	operator*(const RIfloat &a, const RIfloat &b);
    94 	friend RIfloat	operator*(float a, const RIfloat &b);
    95     friend RIfloat	operator*(float a, const RIfloat &b);
    95 	friend RIfloat	operator*(const RIfloat &a, float b);
    96     friend RIfloat	operator*(const RIfloat &a, float b);
    96 	friend RIfloat	operator/(const RIfloat &a, const RIfloat &b);
    97     friend RIfloat	operator/(const RIfloat &a, const RIfloat &b);
    97 	friend RIfloat	operator/(float a, const RIfloat &b);
    98     friend RIfloat	operator/(float a, const RIfloat &b);
    98 	friend RIfloat	operator/(const RIfloat &a, float b);
    99     friend RIfloat	operator/(const RIfloat &a, float b);
    99 
   100 
   100 	friend bool		operator<(const RIfloat &a, const RIfloat &b);
   101     friend bool		operator<(const RIfloat &a, const RIfloat &b);
   101 	friend bool		operator<(float a, const RIfloat &b);
   102     friend bool		operator<(float a, const RIfloat &b);
   102 	friend bool		operator<(const RIfloat &a, float b);
   103     friend bool		operator<(const RIfloat &a, float b);
   103 	friend bool		operator>(const RIfloat &a, const RIfloat &b);
   104     friend bool		operator>(const RIfloat &a, const RIfloat &b);
   104 	friend bool		operator>(float a, const RIfloat &b);
   105     friend bool		operator>(float a, const RIfloat &b);
   105 	friend bool		operator>(const RIfloat &a, float b);
   106     friend bool		operator>(const RIfloat &a, float b);
   106 	friend bool		operator<=(const RIfloat &a, const RIfloat &b);
   107     friend bool		operator<=(const RIfloat &a, const RIfloat &b);
   107 	friend bool		operator<=(float a, const RIfloat &b);
   108     friend bool		operator<=(float a, const RIfloat &b);
   108 	friend bool		operator<=(const RIfloat &a, float b);
   109     friend bool		operator<=(const RIfloat &a, float b);
   109 	friend bool		operator>=(const RIfloat &a, const RIfloat &b);
   110     friend bool		operator>=(const RIfloat &a, const RIfloat &b);
   110 	friend bool		operator>=(float a, const RIfloat &b);
   111     friend bool		operator>=(float a, const RIfloat &b);
   111 	friend bool		operator>=(const RIfloat &a, float b);
   112     friend bool		operator>=(const RIfloat &a, float b);
   112 	friend bool		operator==(const RIfloat &a, const RIfloat &b);
   113     friend bool		operator==(const RIfloat &a, const RIfloat &b);
   113 	friend bool		operator==(float a, const RIfloat &b);
   114     friend bool		operator==(float a, const RIfloat &b);
   114 	friend bool		operator==(const RIfloat &a, float b);
   115     friend bool		operator==(const RIfloat &a, float b);
   115 	friend bool		operator!=(const RIfloat &a, const RIfloat &b);
   116     friend bool		operator!=(const RIfloat &a, const RIfloat &b);
   116 	friend bool		operator!=(float a, const RIfloat &b);
   117     friend bool		operator!=(float a, const RIfloat &b);
   117 	friend bool		operator!=(const RIfloat &a, float b);
   118     friend bool		operator!=(const RIfloat &a, float b);
   118 private:
   119 private:
   119 	void	removeBits()
   120     void	removeBits()
   120 	{
   121     {
   121 		RIfloatInt p;
   122         RIfloatInt p;
   122 		p.f = v;
   123         p.f = v;
   123 		unsigned int exponent = (p.i>>23) & 0xff;
   124         unsigned int exponent = (p.i>>23) & 0xff;
   124 		if(exponent == 0 || exponent == 255)
   125         if(exponent == 0 || exponent == 255)
   125 			return;	//zero, denormal, infinite, or NaN
   126             return;	//zero, denormal, infinite, or NaN
   126 
   127 
   127 		p.i &= ~((1<<(23-RI_MANTISSA_BITS))-1);
   128         p.i &= ~((1<<(23-RI_MANTISSA_BITS))-1);
   128 
   129 
   129 #if (RI_EXPONENT_BITS != 8)
   130 #if (RI_EXPONENT_BITS != 8)
   130 		if (exponent > 127 + (1 << (RI_EXPONENT_BITS-1)))
   131         if (exponent > 127 + (1 << (RI_EXPONENT_BITS-1)))
   131 			exponent = 127 + (1 << (RI_EXPONENT_BITS-1));
   132             exponent = 127 + (1 << (RI_EXPONENT_BITS-1));
   132 
   133 
   133 		if (exponent < 127 + 1 - (1 << (RI_EXPONENT_BITS-1)))
   134         if (exponent < 127 + 1 - (1 << (RI_EXPONENT_BITS-1)))
   134 			exponent = 127 + 1 - (1 << (RI_EXPONENT_BITS-1));
   135             exponent = 127 + 1 - (1 << (RI_EXPONENT_BITS-1));
   135 
   136 
   136 		p.i &= ~(0xff<<23);
   137         p.i &= ~(0xff<<23);
   137 		p.i |= exponent<<23;
   138         p.i |= exponent<<23;
   138 #endif
   139 #endif
   139 		v = p.f;
   140         v = p.f;
   140 	}
   141     }
   141 
   142 
   142 	float		v;
   143     float		v;
   143 };
   144 };
   144 
   145 
   145 RI_INLINE RIfloat operator+(const RIfloat &a, const RIfloat &b)	{ return RIfloat(a.v+b.v); }
   146 RI_INLINE RIfloat operator+(const RIfloat &a, const RIfloat &b)	{ return RIfloat(a.v+b.v); }
   146 RI_INLINE RIfloat operator+(float a, const RIfloat &b)			{ return RIfloat(a+b.v); }
   147 RI_INLINE RIfloat operator+(float a, const RIfloat &b)			{ return RIfloat(a+b.v); }
   147 RI_INLINE RIfloat operator+(const RIfloat &a, float b)			{ return RIfloat(a.v+b); }
   148 RI_INLINE RIfloat operator+(const RIfloat &a, float b)			{ return RIfloat(a.v+b); }
   178 typedef float RIfloat;
   179 typedef float RIfloat;
   179 #endif
   180 #endif
   180 
   181 
   181 #define	PI						3.141592654f
   182 #define	PI						3.141592654f
   182 
   183 
       
   184 RI_INLINE int       RI_ROUND_TO_INT(RIfloat v)                  { return (v >= 0.0f) ? (int)(v+0.5f) : (int)(v-0.5f); }
   183 RI_INLINE RIfloat	RI_MAX(RIfloat a, RIfloat b)				{ return (a > b) ? a : b; }
   185 RI_INLINE RIfloat	RI_MAX(RIfloat a, RIfloat b)				{ return (a > b) ? a : b; }
       
   186 RI_INLINE int		RI_MAX(int a, int b)						{ return (a > b) ? a : b; }
   184 RI_INLINE RIfloat	RI_MIN(RIfloat a, RIfloat b)				{ return (a < b) ? a : b; }
   187 RI_INLINE RIfloat	RI_MIN(RIfloat a, RIfloat b)				{ return (a < b) ? a : b; }
       
   188 RI_INLINE int		RI_MIN(int a, int b)						{ return (a < b) ? a : b; }
   185 RI_INLINE RIfloat	RI_CLAMP(RIfloat a, RIfloat l, RIfloat h)	{ if(RI_ISNAN(a)) return l; RI_ASSERT(l <= h); return (a < l) ? l : (a > h) ? h : a; }
   189 RI_INLINE RIfloat	RI_CLAMP(RIfloat a, RIfloat l, RIfloat h)	{ if(RI_ISNAN(a)) return l; RI_ASSERT(l <= h); return (a < l) ? l : (a > h) ? h : a; }
   186 RI_INLINE void		RI_SWAP(RIfloat &a, RIfloat &b)				{ RIfloat tmp = a; a = b; b = tmp; }
   190 RI_INLINE void		RI_SWAP(RIfloat &a, RIfloat &b)				{ RIfloat tmp = a; a = b; b = tmp; }
   187 RI_INLINE RIfloat	RI_ABS(RIfloat a)							{ return (a < 0.0f) ? -a : a; }
   191 RI_INLINE RIfloat	RI_ABS(RIfloat a)							{ return (a < 0.0f) ? -a : a; }
   188 RI_INLINE RIfloat	RI_SQR(RIfloat a)							{ return a * a; }
   192 RI_INLINE RIfloat	RI_SQR(RIfloat a)							{ return a * a; }
   189 RI_INLINE RIfloat	RI_DEG_TO_RAD(RIfloat a)					{ return a * PI / 180.0f; }
   193 RI_INLINE RIfloat	RI_DEG_TO_RAD(RIfloat a)					{ return a * PI / 180.0f; }
   190 RI_INLINE RIfloat	RI_RAD_TO_DEG(RIfloat a)					{ return a * 180.0f/ PI; }
   194 RI_INLINE RIfloat	RI_RAD_TO_DEG(RIfloat a)					{ return a * 180.0f/ PI; }
   191 RI_INLINE RIfloat	RI_MOD(RIfloat a, RIfloat b)				{ if(RI_ISNAN(a) || RI_ISNAN(b)) return 0.0f; RI_ASSERT(b >= 0.0f); if(b == 0.0f) return 0.0f; RIfloat f = (RIfloat)fmod(a, b); if(f < 0.0f) f += b; RI_ASSERT(f >= 0.0f && f <= b); return f; }
   195 RI_INLINE RIfloat	RI_MOD(RIfloat a, RIfloat b)				{ if(RI_ISNAN(a) || RI_ISNAN(b)) return 0.0f; RI_ASSERT(b >= 0.0f); if(b == 0.0f) return 0.0f; RIfloat f = (RIfloat)fmod(a, b); if(f < 0.0f) f += b; RI_ASSERT(f >= 0.0f && f <= b); return f; }
   192 
   196 
       
   197 #define RI_ANY_SWAP(type, a, b) {type tmp = a; a = b; b = tmp;}
       
   198 
       
   199 RI_INLINE void      RI_INT16_SWAP(RIint16 &a, RIint16 &b) {RIint16 tmp = a; a = b; b = tmp;}
       
   200 RI_INLINE int       RI_INT_ABS(int a)                   { return (a >= 0) ? a : -a; }
   193 RI_INLINE int		RI_INT_MAX(int a, int b)			{ return (a > b) ? a : b; }
   201 RI_INLINE int		RI_INT_MAX(int a, int b)			{ return (a > b) ? a : b; }
   194 RI_INLINE int		RI_INT_MIN(int a, int b)			{ return (a < b) ? a : b; }
   202 RI_INLINE int		RI_INT_MIN(int a, int b)			{ return (a < b) ? a : b; }
       
   203 RI_INLINE int       RI_INT_CLAMP(int a, int l, int h)   { return (a < l) ? l : (a > h) ? h : a; }
   195 RI_INLINE void		RI_INT_SWAP(int &a, int &b)			{ int tmp = a; a = b; b = tmp; }
   204 RI_INLINE void		RI_INT_SWAP(int &a, int &b)			{ int tmp = a; a = b; b = tmp; }
   196 RI_INLINE int		RI_INT_MOD(int a, int b)			{ RI_ASSERT(b >= 0); if(!b) return 0; int i = a % b; if(i < 0) i += b; RI_ASSERT(i >= 0 && i < b); return i; }
   205 RI_INLINE int		RI_INT_MOD(int a, int b)			{ RI_ASSERT(b >= 0); if(!b) return 0; int i = a % b; if(i < 0) i += b; RI_ASSERT(i >= 0 && i < b); return i; }
   197 RI_INLINE int		RI_INT_ADDSATURATE(int a, int b)	{ RI_ASSERT(b >= 0); int r = a + b; return (r >= a) ? r : RI_INT32_MAX; }
   206 RI_INLINE int		RI_INT_ADDSATURATE(int a, int b)	{ RI_ASSERT(b >= 0); int r = a + b; return (r >= a) ? r : RI_INT32_MAX; }
   198 
   207 
       
   208 RI_INLINE int       RI_SHL(int a, int sh)
       
   209 {
       
   210     RI_ASSERT(sh >= 0 && sh <= 31);
       
   211     int r = a << sh;
       
   212     RI_ASSERT(a >= 0 ? (r >= 0) : (r < 0));
       
   213     return r;
       
   214 }
       
   215 
       
   216 RI_INLINE int RI_SHR(int a, int sh)
       
   217 {
       
   218     RI_ASSERT(sh >= 0 && sh <= 31);
       
   219     int r = a >> sh;
       
   220     return r;
       
   221 }
       
   222 
   199 class Matrix3x3;
   223 class Matrix3x3;
   200 class Vector2;
   224 class Vector2;
   201 class Vector3;
   225 class Vector3;
   202 
   226 
   203 //==============================================================================================
   227 //==============================================================================================
   207 //Matrix3x3 inline functions cannot be inside the class because Vector3 is not defined yet when Matrix3x3 is defined
   231 //Matrix3x3 inline functions cannot be inside the class because Vector3 is not defined yet when Matrix3x3 is defined
   208 
   232 
   209 class Matrix3x3
   233 class Matrix3x3
   210 {
   234 {
   211 public:
   235 public:
   212 	RI_INLINE					Matrix3x3		();						//initialized to identity
   236     RI_INLINE					Matrix3x3		();						//initialized to identity
   213 	RI_INLINE					Matrix3x3		( const Matrix3x3& m );
   237     RI_INLINE					Matrix3x3		( const Matrix3x3& m );
   214 	RI_INLINE					Matrix3x3		( RIfloat m00, RIfloat m01, RIfloat m02, RIfloat m10, RIfloat m11, RIfloat m12, RIfloat m20, RIfloat m21, RIfloat m22 );
   238     RI_INLINE					Matrix3x3		( RIfloat m00, RIfloat m01, RIfloat m02, RIfloat m10, RIfloat m11, RIfloat m12, RIfloat m20, RIfloat m21, RIfloat m22 );
   215 	RI_INLINE					~Matrix3x3		();
   239     RI_INLINE					~Matrix3x3		();
   216 	RI_INLINE Matrix3x3&		operator=		( const Matrix3x3& m );
   240     RI_INLINE Matrix3x3&		operator=		( const Matrix3x3& m );
   217 	RI_INLINE Vector3&			operator[]		( int i );				//returns a row vector
   241     RI_INLINE Vector3&			operator[]		( int i );				//returns a row vector
   218 	RI_INLINE const Vector3&	operator[]		( int i ) const;
   242     RI_INLINE const Vector3&	operator[]		( int i ) const;
   219 	RI_INLINE void				set				( RIfloat m00, RIfloat m01, RIfloat m02, RIfloat m10, RIfloat m11, RIfloat m12, RIfloat m20, RIfloat m21, RIfloat m22 );
   243     RI_INLINE void				set				( RIfloat m00, RIfloat m01, RIfloat m02, RIfloat m10, RIfloat m11, RIfloat m12, RIfloat m20, RIfloat m21, RIfloat m22 );
   220 	RI_INLINE const Vector3		getRow			( int i ) const;
   244     RI_INLINE const Vector3		getRow			( int i ) const;
   221 	RI_INLINE const Vector3		getColumn		( int i ) const;
   245     RI_INLINE const Vector3		getColumn		( int i ) const;
   222 	RI_INLINE void				setRow			( int i, const Vector3& v );
   246     RI_INLINE void				setRow			( int i, const Vector3& v );
   223 	RI_INLINE void				setColumn		( int i, const Vector3& v );
   247     RI_INLINE void				setColumn		( int i, const Vector3& v );
   224 	RI_INLINE void				operator*=		( const Matrix3x3& m );
   248     RI_INLINE void				operator*=		( const Matrix3x3& m );
   225 	RI_INLINE void				operator*=		( RIfloat f );
   249     RI_INLINE void				operator*=		( RIfloat f );
   226 	RI_INLINE void				operator+=		( const Matrix3x3& m );
   250     RI_INLINE void				operator+=		( const Matrix3x3& m );
   227 	RI_INLINE void				operator-=		( const Matrix3x3& m );
   251     RI_INLINE void				operator-=		( const Matrix3x3& m );
   228 	RI_INLINE const Matrix3x3	operator-		() const;
   252     RI_INLINE const Matrix3x3	operator-		() const;
   229 	RI_INLINE void				identity		();
   253     RI_INLINE void				identity		();
   230 	RI_INLINE void				transpose		();
   254     RI_INLINE void				transpose		();
   231 	bool						invert			();	//if the matrix is singular, returns false and leaves it unmodified
   255     bool						invert			();	//if the matrix is singular, returns false and leaves it unmodified
   232 	RI_INLINE RIfloat				det				() const;
   256     RI_INLINE RIfloat				det				() const;
   233 	RI_INLINE bool				isAffine		() const;
   257     RI_INLINE bool				isAffine		() const;
   234 
   258 
   235 private:
   259 private:
   236 	RIfloat						matrix[3][3];
   260     RIfloat						matrix[3][3];
   237 };
   261 };
   238 
   262 
   239 //==============================================================================================
   263 //==============================================================================================
   240 
   264 
   241 class Vector2
   265 class Vector2
   242 {
   266 {
   243 public:
   267 public:
   244 	RI_INLINE					Vector2			() : x(0.0f), y(0.0f)					{}
   268     RI_INLINE					Vector2			() : x(0.0f), y(0.0f)					{}
   245 	RI_INLINE					Vector2			( const Vector2& v ) : x(v.x), y(v.y)	{}
   269     RI_INLINE					Vector2			( const Vector2& v ) : x(v.x), y(v.y)	{}
   246 	RI_INLINE					Vector2			( RIfloat fx, RIfloat fy ) : x(fx), y(fy)	{}
   270     RI_INLINE					Vector2			( RIfloat fx, RIfloat fy ) : x(fx), y(fy)	{}
   247 	RI_INLINE					~Vector2		()								{}
   271     RI_INLINE					~Vector2		()								{}
   248 	RI_INLINE Vector2&			operator=		( const Vector2& v )			{ x = v.x; y = v.y; return *this; }
   272     RI_INLINE Vector2&			operator=		( const Vector2& v )			{ x = v.x; y = v.y; return *this; }
   249 	RI_INLINE RIfloat&			operator[]		( int i )						{ RI_ASSERT(i>=0&&i<2); return (&x)[i]; }
   273     RI_INLINE RIfloat&			operator[]		( int i )						{ RI_ASSERT(i>=0&&i<2); return (&x)[i]; }
   250 	RI_INLINE const RIfloat&	operator[]		( int i ) const					{ RI_ASSERT(i>=0&&i<2); return (&x)[i]; }
   274     RI_INLINE const RIfloat&	operator[]		( int i ) const					{ RI_ASSERT(i>=0&&i<2); return (&x)[i]; }
   251 	RI_INLINE void				set				( RIfloat fx, RIfloat fy )			{ x = fx; y = fy; }
   275     RI_INLINE void				set				( RIfloat fx, RIfloat fy )			{ x = fx; y = fy; }
   252 	RI_INLINE void				operator*=		( RIfloat f )						{ x *= f; y *= f; }
   276     RI_INLINE void				operator*=		( RIfloat f )						{ x *= f; y *= f; }
   253 	RI_INLINE void				operator+=		( const Vector2& v )			{ x += v.x; y += v.y; }
   277     RI_INLINE void				operator+=		( const Vector2& v )			{ x += v.x; y += v.y; }
   254 	RI_INLINE void				operator-=		( const Vector2& v )			{ x -= v.x; y -= v.y; }
   278     RI_INLINE void				operator-=		( const Vector2& v )			{ x -= v.x; y -= v.y; }
   255 	RI_INLINE const Vector2		operator-		() const						{ return Vector2(-x,-y); }
   279     RI_INLINE const Vector2		operator-		() const						{ return Vector2(-x,-y); }
   256 	//if the vector is zero, returns false and leaves it unmodified
   280     //if the vector is zero, returns false and leaves it unmodified
   257 	RI_INLINE bool				normalize		()								{ double l = (double)x*(double)x+(double)y*(double)y; if( l == 0.0 ) return false; l = 1.0 / sqrt(l); x = (RIfloat)((double)x * l); y = (RIfloat)((double)y * l); return true; }
   281     RI_INLINE bool				normalize		()								{ double l = (double)x*(double)x+(double)y*(double)y; if( l == 0.0 ) return false; l = 1.0 / sqrt(l); x = (RIfloat)((double)x * l); y = (RIfloat)((double)y * l); return true; }
   258 	RI_INLINE RIfloat			length			() const						{ return (RIfloat)sqrt((double)x*(double)x+(double)y*(double)y); }
   282     RI_INLINE RIfloat			length			() const						{ return (RIfloat)sqrt((double)x*(double)x+(double)y*(double)y); }
   259 	RI_INLINE void				scale			( const Vector2& v )			{ x *= v.x; y *= v.y; }	//component-wise scale
   283     RI_INLINE void				scale			( const Vector2& v )			{ x *= v.x; y *= v.y; }	//component-wise scale
   260 	RI_INLINE void				negate			()								{ x = -x; y = -y; }
   284     RI_INLINE void				negate			()								{ x = -x; y = -y; }
   261 
   285 
   262 	RIfloat						x,y;
   286     RIfloat						x,y;
   263 };
   287 };
   264 
   288 
   265 //==============================================================================================
   289 //==============================================================================================
   266 
   290 
   267 class Vector3
   291 class Vector3
   268 {
   292 {
   269 public:
   293 public:
   270 	RI_INLINE					Vector3			() : x(0.0f), y(0.0f), z(0.0f)							{}
   294     RI_INLINE					Vector3			() : x(0.0f), y(0.0f), z(0.0f)							{}
   271 	RI_INLINE					Vector3			( const Vector3& v ) : x(v.x), y(v.y), z(v.z)			{}
   295     RI_INLINE					Vector3			( const Vector3& v ) : x(v.x), y(v.y), z(v.z)			{}
   272 	RI_INLINE					Vector3			( RIfloat fx, RIfloat fy, RIfloat fz ) : x(fx), y(fy), z(fz)	{}
   296     RI_INLINE					Vector3			( RIfloat fx, RIfloat fy, RIfloat fz ) : x(fx), y(fy), z(fz)	{}
   273 	RI_INLINE					~Vector3		()								{}
   297     RI_INLINE					~Vector3		()								{}
   274 	RI_INLINE Vector3&			operator=		( const Vector3& v )			{ x = v.x; y = v.y; z = v.z; return *this; }
   298     RI_INLINE Vector3&			operator=		( const Vector3& v )			{ x = v.x; y = v.y; z = v.z; return *this; }
   275 	RI_INLINE RIfloat&			operator[]		( int i )						{ RI_ASSERT(i>=0&&i<3); return (&x)[i]; }
   299     RI_INLINE RIfloat&			operator[]		( int i )						{ RI_ASSERT(i>=0&&i<3); return (&x)[i]; }
   276 	RI_INLINE const RIfloat&	operator[]		( int i ) const					{ RI_ASSERT(i>=0&&i<3); return (&x)[i]; }
   300     RI_INLINE const RIfloat&	operator[]		( int i ) const					{ RI_ASSERT(i>=0&&i<3); return (&x)[i]; }
   277 	RI_INLINE void				set				( RIfloat fx, RIfloat fy, RIfloat fz ){ x = fx; y = fy; z = fz; }
   301     RI_INLINE void				set				( RIfloat fx, RIfloat fy, RIfloat fz ){ x = fx; y = fy; z = fz; }
   278 	RI_INLINE void				operator*=		( RIfloat f )						{ x *= f; y *= f; z *= f; }
   302     RI_INLINE void				operator*=		( RIfloat f )						{ x *= f; y *= f; z *= f; }
   279 	RI_INLINE void				operator+=		( const Vector3& v )			{ x += v.x; y += v.y; z += v.z; }
   303     RI_INLINE void				operator+=		( const Vector3& v )			{ x += v.x; y += v.y; z += v.z; }
   280 	RI_INLINE void				operator-=		( const Vector3& v )			{ x -= v.x; y -= v.y; z -= v.z; }
   304     RI_INLINE void				operator-=		( const Vector3& v )			{ x -= v.x; y -= v.y; z -= v.z; }
   281 	RI_INLINE const Vector3		operator-		() const						{ return Vector3(-x,-y,-z); }
   305     RI_INLINE const Vector3		operator-		() const						{ return Vector3(-x,-y,-z); }
   282 	//if the vector is zero, returns false and leaves it unmodified
   306     //if the vector is zero, returns false and leaves it unmodified
   283 	RI_INLINE bool				normalize		()								{ double l = (double)x*(double)x+(double)y*(double)y+(double)z*(double)z; if( l == 0.0 ) return false; l = 1.0 / sqrt(l); x = (RIfloat)((double)x * l); y = (RIfloat)((double)y * l); z = (RIfloat)((double)z * l); return true; }
   307     RI_INLINE bool				normalize		()								{ double l = (double)x*(double)x+(double)y*(double)y+(double)z*(double)z; if( l == 0.0 ) return false; l = 1.0 / sqrt(l); x = (RIfloat)((double)x * l); y = (RIfloat)((double)y * l); z = (RIfloat)((double)z * l); return true; }
   284 	RI_INLINE RIfloat			length			() const						{ return (RIfloat)sqrt((double)x*(double)x+(double)y*(double)y+(double)z*(double)z); }
   308     RI_INLINE RIfloat			length			() const						{ return (RIfloat)sqrt((double)x*(double)x+(double)y*(double)y+(double)z*(double)z); }
   285 	RI_INLINE void				scale			( const Vector3& v )			{ x *= v.x; y *= v.y; z *= v.z; }	//component-wise scale
   309     RI_INLINE void				scale			( const Vector3& v )			{ x *= v.x; y *= v.y; z *= v.z; }	//component-wise scale
   286 	RI_INLINE void				negate			()								{ x = -x; y = -y; z = -z; }
   310     RI_INLINE void				negate			()								{ x = -x; y = -y; z = -z; }
   287 
   311 
   288 	RIfloat						x,y,z;
   312     RIfloat						x,y,z;
   289 };
   313 };
   290 
   314 
   291 //==============================================================================================
   315 //==============================================================================================
   292 
   316 
   293 //Vector2 global functions
   317 //Vector2 global functions