summaryrefslogtreecommitdiff
path: root/src/vg/Math.h
blob: 7cbcdcecbcef86eaac06038c4e258a929763a40c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
#ifndef __MATH_H
#define __MATH_H

/*------------------------------------------------------------------------
 *
 * OpenVG 1.1 Reference Implementation
 * -----------------------------------
 *
 * Copyright (c) 2007 The Khronos Group Inc.
 *
 * Permission is hereby granted, free of charge, to any person obtaining a
 * copy of this software and /or associated documentation files
 * (the "Materials "), to deal in the Materials without restriction,
 * including without limitation the rights to use, copy, modify, merge,
 * publish, distribute, sublicense, and/or sell copies of the Materials,
 * and to permit persons to whom the Materials are furnished to do so,
 * subject to the following conditions: 
 *
 * The above copyright notice and this permission notice shall be included 
 * in all copies or substantial portions of the Materials. 
 *
 * THE MATERIALS ARE PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
 * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE MATERIALS OR
 * THE USE OR OTHER DEALINGS IN THE MATERIALS.
 *
 *//**
 * \file
 * \brief	Math functions, Vector and Matrix classes.
 * \note	
 *//*-------------------------------------------------------------------*/

#include "Defs.h"

#include <math.h>

namespace tgOpenVG
{

/*-------------------------------------------------------------------*//*!
* \brief	
* \param	
* \return	
* \note		
*//*-------------------------------------------------------------------*/

inline int		RI_ISNAN(float a)
{
	RIfloatInt p;
	p.f = a;
	unsigned int exponent = (p.i>>23) & 0xff;
	unsigned int mantissa = p.i & 0x7fffff;
	if(exponent == 255 && mantissa)
		return 1;
	return 0;
}

#if (RI_MANTISSA_BITS > 23)
#error RI_MANTISSA_BITS is greater than 23
#elif (RI_EXPONENT_BITS > 8)
#error RI_EXPONENT_BITS is greater than 8
#elif (RI_MANTISSA_BITS != 23) || (RI_EXPONENT_BITS != 8)

class RIfloat
{
public:
	RIfloat() : v(0.0f)						{ removeBits(); }
	RIfloat(float a) : v(a)					{ removeBits(); }
	RIfloat(double a) : v((float)a)			{ removeBits(); }
	RIfloat(int a) : v((float)a)			{ removeBits(); }
	RIfloat(unsigned int a) : v((float)a)	{ removeBits(); }
	RIfloat&	operator=(const RIfloat &a)	{ v = a.v; removeBits(); return *this; }
	RIfloat&	operator+=(const RIfloat &a){ v += a.v; removeBits(); return *this; }
	RIfloat&	operator-=(const RIfloat &a){ v -= a.v; removeBits(); return *this; }
	RIfloat&	operator*=(const RIfloat &a){ v *= a.v; removeBits(); return *this; }
	RIfloat&	operator/=(const RIfloat &a){ v /= a.v; removeBits(); return *this; }
	RIfloat		operator-() const			{ return -v; }
	operator float() const					{ return v; }
	operator double() const					{ return (double)v; }
	operator int() const					{ return (int)v; }

	friend RIfloat	operator+(const RIfloat &a, const RIfloat &b);
	friend RIfloat	operator+(float a, const RIfloat &b);
	friend RIfloat	operator+(const RIfloat &a, float b);
	friend RIfloat	operator-(const RIfloat &a, const RIfloat &b);
	friend RIfloat	operator-(float a, const RIfloat &b);
	friend RIfloat	operator-(const RIfloat &a, float b);
	friend RIfloat	operator*(const RIfloat &a, const RIfloat &b);
	friend RIfloat	operator*(float a, const RIfloat &b);
	friend RIfloat	operator*(const RIfloat &a, float b);
	friend RIfloat	operator/(const RIfloat &a, const RIfloat &b);
	friend RIfloat	operator/(float a, const RIfloat &b);
	friend RIfloat	operator/(const RIfloat &a, float b);

	friend bool		operator<(const RIfloat &a, const RIfloat &b);
	friend bool		operator<(float a, const RIfloat &b);
	friend bool		operator<(const RIfloat &a, float b);
	friend bool		operator>(const RIfloat &a, const RIfloat &b);
	friend bool		operator>(float a, const RIfloat &b);
	friend bool		operator>(const RIfloat &a, float b);
	friend bool		operator<=(const RIfloat &a, const RIfloat &b);
	friend bool		operator<=(float a, const RIfloat &b);
	friend bool		operator<=(const RIfloat &a, float b);
	friend bool		operator>=(const RIfloat &a, const RIfloat &b);
	friend bool		operator>=(float a, const RIfloat &b);
	friend bool		operator>=(const RIfloat &a, float b);
	friend bool		operator==(const RIfloat &a, const RIfloat &b);
	friend bool		operator==(float a, const RIfloat &b);
	friend bool		operator==(const RIfloat &a, float b);
	friend bool		operator!=(const RIfloat &a, const RIfloat &b);
	friend bool		operator!=(float a, const RIfloat &b);
	friend bool		operator!=(const RIfloat &a, float b);
private:
	void	removeBits()
	{
		RIfloatInt p;
		p.f = v;
		unsigned int exponent = (p.i>>23) & 0xff;
		if(exponent == 0 || exponent == 255)
			return;	//zero, denormal, infinite, or NaN

		p.i &= ~((1<<(23-RI_MANTISSA_BITS))-1);

#if (RI_EXPONENT_BITS != 8)
		if (exponent > 127 + (1 << (RI_EXPONENT_BITS-1)))
			exponent = 127 + (1 << (RI_EXPONENT_BITS-1));

		if (exponent < 127 + 1 - (1 << (RI_EXPONENT_BITS-1)))
			exponent = 127 + 1 - (1 << (RI_EXPONENT_BITS-1));

		p.i &= ~(0xff<<23);
		p.i |= exponent<<23;
#endif
		v = p.f;
	}

	float		v;
};

inline RIfloat operator+(const RIfloat &a, const RIfloat &b)	{ return RIfloat(a.v+b.v); }
inline RIfloat operator+(float a, const RIfloat &b)			{ return RIfloat(a+b.v); }
inline RIfloat operator+(const RIfloat &a, float b)			{ return RIfloat(a.v+b); }
inline RIfloat operator-(const RIfloat &a, const RIfloat &b)	{ return RIfloat(a.v-b.v); }
inline RIfloat operator-(float a, const RIfloat &b)			{ return RIfloat(a-b.v); }
inline RIfloat operator-(const RIfloat &a, float b)			{ return RIfloat(a.v-b); }
inline RIfloat operator*(const RIfloat &a, const RIfloat &b)	{ return RIfloat(a.v*b.v); }
inline RIfloat operator*(float a, const RIfloat &b)			{ return RIfloat(a*b.v); }
inline RIfloat operator*(const RIfloat &a, float b)			{ return RIfloat(a.v*b); }
inline RIfloat operator/(const RIfloat &a, const RIfloat &b)	{ return RIfloat(a.v/b.v); }
inline RIfloat operator/(float a, const RIfloat &b)			{ return RIfloat(a/b.v); }
inline RIfloat operator/(const RIfloat &a, float b)			{ return RIfloat(a.v/b); }

inline bool operator<(const RIfloat &a, const RIfloat &b)	{ return a.v < b.v ? true : false; }
inline bool operator<(float a, const RIfloat &b)				{ return a < b.v ? true : false; }
inline bool operator<(const RIfloat &a, float b)				{ return a.v < b ? true : false; }
inline bool operator>(const RIfloat &a, const RIfloat &b)	{ return a.v > b.v ? true : false; }
inline bool operator>(float a, const RIfloat &b)				{ return a > b.v ? true : false; }
inline bool operator>(const RIfloat &a, float b)				{ return a.v > b ? true : false; }
inline bool operator<=(const RIfloat &a, const RIfloat &b)	{ return a.v <= b.v ? true : false; }
inline bool operator<=(float a, const RIfloat &b)			{ return a <= b.v ? true : false; }
inline bool operator<=(const RIfloat &a, float b)			{ return a.v <= b ? true : false; }
inline bool operator>=(const RIfloat &a, const RIfloat &b)	{ return a.v >= b.v ? true : false; }
inline bool operator>=(float a, const RIfloat &b)			{ return a >= b.v ? true : false; }
inline bool operator>=(const RIfloat &a, float b)			{ return a.v >= b ? true : false; }
inline bool operator==(const RIfloat &a, const RIfloat &b)	{ return a.v == b.v ? true : false; }
inline bool operator==(float a, const RIfloat &b)			{ return a == b.v ? true : false; }
inline bool operator==(const RIfloat &a, float b)			{ return a.v == b ? true : false; }
inline bool operator!=(const RIfloat &a, const RIfloat &b)	{ return a.v != b.v ? true : false; }
inline bool operator!=(float a, const RIfloat &b)			{ return a != b.v ? true : false; }
inline bool operator!=(const RIfloat &a, float b)			{ return a.v != b ? true : false; }

#else
typedef float RIfloat;
#endif

#define	PI						3.141592654f

inline RIfloat	RI_MAX(RIfloat a, RIfloat b)				{ return (a > b) ? a : b; }
inline RIfloat	RI_MIN(RIfloat a, RIfloat b)				{ return (a < b) ? a : b; }
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; }
inline void		RI_SWAP(RIfloat &a, RIfloat &b)				{ RIfloat tmp = a; a = b; b = tmp; }
inline RIfloat	RI_ABS(RIfloat a)							{ return (a < 0.0f) ? -a : a; }
inline RIfloat	RI_SQR(RIfloat a)							{ return a * a; }
inline RIfloat	RI_DEG_TO_RAD(RIfloat a)					{ return a * PI / 180.0f; }
inline RIfloat	RI_RAD_TO_DEG(RIfloat a)					{ return a * 180.0f/ PI; }
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; }

inline int		RI_INT_MAX(int a, int b)			{ return (a > b) ? a : b; }
inline int		RI_INT_MIN(int a, int b)			{ return (a < b) ? a : b; }
inline void		RI_INT_SWAP(int &a, int &b)			{ int tmp = a; a = b; b = tmp; }
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; }
inline int		RI_INT_ADDSATURATE(int a, int b)	{ RI_ASSERT(b >= 0); int r = a + b; return (r >= a) ? r : RI_INT32_MAX; }

class Matrix3x3;
class Vector2;
class Vector3;

//==============================================================================================

//MatrixRxC, R = number of rows, C = number of columns
//indexing: matrix[row][column]
//Matrix3x3 inline functions cannot be inside the class because Vector3 is not defined yet when Matrix3x3 is defined

class Matrix3x3
{
public:
	inline					Matrix3x3		();						//initialized to identity
	inline					Matrix3x3		( const Matrix3x3& m );
	inline					Matrix3x3		( RIfloat m00, RIfloat m01, RIfloat m02, RIfloat m10, RIfloat m11, RIfloat m12, RIfloat m20, RIfloat m21, RIfloat m22 );
	inline					~Matrix3x3		();
	inline Matrix3x3&		operator=		( const Matrix3x3& m );
	inline Vector3&			operator[]		( int i );				//returns a row vector
	inline const Vector3&	operator[]		( int i ) const;
	inline void				set				( RIfloat m00, RIfloat m01, RIfloat m02, RIfloat m10, RIfloat m11, RIfloat m12, RIfloat m20, RIfloat m21, RIfloat m22 );
	inline const Vector3		getRow			( int i ) const;
	inline const Vector3		getColumn		( int i ) const;
	inline void				setRow			( int i, const Vector3& v );
	inline void				setColumn		( int i, const Vector3& v );
	inline void				operator*=		( const Matrix3x3& m );
	inline void				operator*=		( RIfloat f );
	inline void				operator+=		( const Matrix3x3& m );
	inline void				operator-=		( const Matrix3x3& m );
	inline const Matrix3x3	operator-		() const;
	inline void				identity		();
	inline void				transpose		();
	bool						invert			();	//if the matrix is singular, returns false and leaves it unmodified
	inline RIfloat				det				() const;
	inline bool				isAffine		() const;

private:
	RIfloat						matrix[3][3];
};

//==============================================================================================

class Vector2
{
public:
	inline					Vector2			() : x(0.0f), y(0.0f)					{}
	inline					Vector2			( const Vector2& v ) : x(v.x), y(v.y)	{}
	inline					Vector2			( RIfloat fx, RIfloat fy ) : x(fx), y(fy)	{}
	inline					~Vector2		()								{}
	inline Vector2&			operator=		( const Vector2& v )			{ x = v.x; y = v.y; return *this; }
	inline RIfloat&			operator[]		( int i )						{ RI_ASSERT(i>=0&&i<2); return (&x)[i]; }
	inline const RIfloat&	operator[]		( int i ) const					{ RI_ASSERT(i>=0&&i<2); return (&x)[i]; }
	inline void				set				( RIfloat fx, RIfloat fy )			{ x = fx; y = fy; }
	inline void				operator*=		( RIfloat f )						{ x *= f; y *= f; }
	inline void				operator+=		( const Vector2& v )			{ x += v.x; y += v.y; }
	inline void				operator-=		( const Vector2& v )			{ x -= v.x; y -= v.y; }
	inline const Vector2		operator-		() const						{ return Vector2(-x,-y); }
	//if the vector is zero, returns false and leaves it unmodified
	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; }
	inline RIfloat			length			() const						{ return (RIfloat)sqrt((double)x*(double)x+(double)y*(double)y); }
	inline void				scale			( const Vector2& v )			{ x *= v.x; y *= v.y; }	//component-wise scale
	inline void				negate			()								{ x = -x; y = -y; }

	RIfloat						x,y;
};

//==============================================================================================

class Vector3
{
public:
	inline					Vector3			() : x(0.0f), y(0.0f), z(0.0f)							{}
	inline					Vector3			( const Vector3& v ) : x(v.x), y(v.y), z(v.z)			{}
	inline					Vector3			( RIfloat fx, RIfloat fy, RIfloat fz ) : x(fx), y(fy), z(fz)	{}
	inline					~Vector3		()								{}
	inline Vector3&			operator=		( const Vector3& v )			{ x = v.x; y = v.y; z = v.z; return *this; }
	inline RIfloat&			operator[]		( int i )						{ RI_ASSERT(i>=0&&i<3); return (&x)[i]; }
	inline const RIfloat&	operator[]		( int i ) const					{ RI_ASSERT(i>=0&&i<3); return (&x)[i]; }
	inline void				set				( RIfloat fx, RIfloat fy, RIfloat fz ){ x = fx; y = fy; z = fz; }
	inline void				operator*=		( RIfloat f )						{ x *= f; y *= f; z *= f; }
	inline void				operator+=		( const Vector3& v )			{ x += v.x; y += v.y; z += v.z; }
	inline void				operator-=		( const Vector3& v )			{ x -= v.x; y -= v.y; z -= v.z; }
	inline const Vector3		operator-		() const						{ return Vector3(-x,-y,-z); }
	//if the vector is zero, returns false and leaves it unmodified
	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; }
	inline RIfloat			length			() const						{ return (RIfloat)sqrt((double)x*(double)x+(double)y*(double)y+(double)z*(double)z); }
	inline void				scale			( const Vector3& v )			{ x *= v.x; y *= v.y; z *= v.z; }	//component-wise scale
	inline void				negate			()								{ x = -x; y = -y; z = -z; }

	RIfloat						x,y,z;
};

//==============================================================================================

//Vector2 global functions
inline bool			operator==	( const Vector2& v1, const Vector2& v2 )	{ return (v1.x == v2.x) && (v1.y == v2.y); }
inline bool			operator!=	( const Vector2& v1, const Vector2& v2 )	{ return (v1.x != v2.x) || (v1.y != v2.y); }
inline bool			isEqual		( const Vector2& v1, const Vector2& v2, RIfloat epsilon )	{ return RI_SQR(v2.x-v1.x) + RI_SQR(v2.y-v1.y) <= epsilon*epsilon; }
inline bool			isZero		( const Vector2& v )						{ return (v.x == 0.0f) && (v.y == 0.0f); }
inline const Vector2	operator*	( RIfloat f, const Vector2& v )				{ return Vector2(v.x*f,v.y*f); }
inline const Vector2	operator*	( const Vector2& v, RIfloat f )				{ return Vector2(v.x*f,v.y*f); }
inline const Vector2	operator+	( const Vector2& v1, const Vector2& v2 )	{ return Vector2(v1.x+v2.x, v1.y+v2.y); }
inline const Vector2	operator-	( const Vector2& v1, const Vector2& v2 )	{ return Vector2(v1.x-v2.x, v1.y-v2.y); }
inline RIfloat		dot			( const Vector2& v1, const Vector2& v2 )	{ return v1.x*v2.x+v1.y*v2.y; }
//if v is a zero vector, returns a zero vector
inline const Vector2	normalize	( const Vector2& v )						{ double l = (double)v.x*(double)v.x+(double)v.y*(double)v.y; if( l != 0.0 ) l = 1.0 / sqrt(l); return Vector2((RIfloat)((double)v.x * l), (RIfloat)((double)v.y * l)); }
//if onThis is a zero vector, returns a zero vector
inline const Vector2	project		( const Vector2& v, const Vector2& onThis ) { RIfloat l = dot(onThis,onThis); if( l != 0.0f ) l = dot(v, onThis)/l; return onThis * l; }
inline const Vector2	lerp		( const Vector2& v1, const Vector2& v2, RIfloat ratio )	{ return v1 + ratio * (v2 - v1); }
inline const Vector2	scale		( const Vector2& v1, const Vector2& v2 )	{ return Vector2(v1.x*v2.x, v1.y*v2.y); }
//matrix * column vector. The input vector2 is implicitly expanded to (x,y,1)
inline const Vector2 affineTransform( const Matrix3x3& m, const Vector2& v )	{ RI_ASSERT(m.isAffine()); return Vector2(v.x * m[0][0] + v.y * m[0][1] + m[0][2], v.x * m[1][0] + v.y * m[1][1] + m[1][2]); }
//matrix * column vector. The input vector2 is implicitly expanded to (x,y,0)
inline const Vector2 affineTangentTransform(const Matrix3x3& m, const Vector2& v)	{ RI_ASSERT(m.isAffine()); return Vector2(v.x * m[0][0] + v.y * m[0][1], v.x * m[1][0] + v.y * m[1][1]); }
inline const Vector2 perpendicularCW(const Vector2& v)						{ return Vector2(v.y, -v.x); }
inline const Vector2 perpendicularCCW(const Vector2& v)						{ return Vector2(-v.y, v.x); }
inline const Vector2 perpendicular(const Vector2& v, bool cw)				{ if(cw) return Vector2(v.y, -v.x); return Vector2(-v.y, v.x); }

//==============================================================================================

//Vector3 global functions
inline bool			operator==	( const Vector3& v1, const Vector3& v2 )	{ return (v1.x == v2.x) && (v1.y == v2.y) && (v1.z == v2.z); }
inline bool			operator!=	( const Vector3& v1, const Vector3& v2 )	{ return (v1.x != v2.x) || (v1.y != v2.y) || (v1.z != v2.z); }
inline bool			isEqual		( const Vector3& v1, const Vector3& v2, RIfloat epsilon )	{ return RI_SQR(v2.x-v1.x) + RI_SQR(v2.y-v1.y) + RI_SQR(v2.z-v1.z) <= epsilon*epsilon; }
inline const Vector3	operator*	( RIfloat f, const Vector3& v )				{ return Vector3(v.x*f,v.y*f,v.z*f); }
inline const Vector3	operator*	( const Vector3& v, RIfloat f )				{ return Vector3(v.x*f,v.y*f,v.z*f); }
inline const Vector3	operator+	( const Vector3& v1, const Vector3& v2 )	{ return Vector3(v1.x+v2.x, v1.y+v2.y, v1.z+v2.z); }
inline const Vector3	operator-	( const Vector3& v1, const Vector3& v2 )	{ return Vector3(v1.x-v2.x, v1.y-v2.y, v1.z-v2.z); }
inline RIfloat		dot			( const Vector3& v1, const Vector3& v2 )	{ return v1.x*v2.x+v1.y*v2.y+v1.z*v2.z; }
inline const Vector3	cross		( const Vector3& v1, const Vector3& v2 )	{ return Vector3( v1.y*v2.z-v1.z*v2.y, v1.z*v2.x-v1.x*v2.z, v1.x*v2.y-v1.y*v2.x ); }
//if v is a zero vector, returns a zero vector
inline const Vector3	normalize	( const Vector3& v )						{ double l = (double)v.x*(double)v.x+(double)v.y*(double)v.y+(double)v.z*(double)v.z; if( l != 0.0 ) l = 1.0 / sqrt(l); return Vector3((RIfloat)((double)v.x * l), (RIfloat)((double)v.y * l), (RIfloat)((double)v.z * l)); }
inline const Vector3	lerp		( const Vector3& v1, const Vector3& v2, RIfloat ratio )	{ return v1 + ratio * (v2 - v1); }
inline const Vector3	scale		( const Vector3& v1, const Vector3& v2 )	{ return Vector3(v1.x*v2.x, v1.y*v2.y, v1.z*v2.z); }

//==============================================================================================

//matrix * column vector
inline const Vector3	operator*	( const Matrix3x3& m, const Vector3& v)		{ return Vector3( v.x*m[0][0]+v.y*m[0][1]+v.z*m[0][2], v.x*m[1][0]+v.y*m[1][1]+v.z*m[1][2], v.x*m[2][0]+v.y*m[2][1]+v.z*m[2][2] ); }

//==============================================================================================

//Matrix3x3 global functions
inline bool				operator==	( const Matrix3x3& m1, const Matrix3x3& m2 )	{ for(int i=0;i<3;i++) for(int j=0;j<3;j++) if( m1[i][j] != m2[i][j] ) return false; return true; }
inline bool				operator!=	( const Matrix3x3& m1, const Matrix3x3& m2 )	{ return !(m1 == m2); }
inline const Matrix3x3	operator*	( const Matrix3x3& m1, const Matrix3x3& m2 )	{ Matrix3x3 t; for(int i=0;i<3;i++) for(int j=0;j<3;j++) t[i][j] = m1[i][0] * m2[0][j] + m1[i][1] * m2[1][j] + m1[i][2] * m2[2][j]; return t; }
inline const Matrix3x3	operator*	( RIfloat f, const Matrix3x3& m )					{ Matrix3x3 t(m); t *= f; return t; }
inline const Matrix3x3	operator*	( const Matrix3x3& m, RIfloat f )					{ Matrix3x3 t(m); t *= f; return t; }
inline const Matrix3x3	operator+	( const Matrix3x3& m1, const Matrix3x3& m2 )	{ Matrix3x3 t(m1); t += m2; return t; }
inline const Matrix3x3	operator-	( const Matrix3x3& m1, const Matrix3x3& m2 )	{ Matrix3x3 t(m1); t -= m2; return t; }
inline const Matrix3x3	transpose	( const Matrix3x3& m )							{ Matrix3x3 t(m); t.transpose(); return t; }
// if the matrix is singular, returns it unmodified
inline const Matrix3x3	invert		( const Matrix3x3& m )							{ Matrix3x3 t(m); t.invert(); return t; }

//==============================================================================================

//Matrix3x3 inline functions (cannot be inside the class because Vector3 is not defined yet when Matrix3x3 is defined)
inline					Matrix3x3::Matrix3x3	()									{ identity(); }
inline					Matrix3x3::Matrix3x3	( const Matrix3x3& m )				{ *this = m; }
inline					Matrix3x3::Matrix3x3	( RIfloat m00, RIfloat m01, RIfloat m02, RIfloat m10, RIfloat m11, RIfloat m12, RIfloat m20, RIfloat m21, RIfloat m22 )	{ set(m00,m01,m02,m10,m11,m12,m20,m21,m22); }
inline					Matrix3x3::~Matrix3x3	()									{}
inline Matrix3x3&		Matrix3x3::operator=	( const Matrix3x3& m )				{ for(int i=0;i<3;i++) for(int j=0;j<3;j++) matrix[i][j] = m.matrix[i][j]; return *this; }
inline Vector3&			Matrix3x3::operator[]	( int i )							{ RI_ASSERT(i>=0&&i<3); return (Vector3&)matrix[i][0]; }
inline const Vector3&	Matrix3x3::operator[]	( int i ) const						{ RI_ASSERT(i>=0&&i<3); return (const Vector3&)matrix[i][0]; }
inline void				Matrix3x3::set			( RIfloat m00, RIfloat m01, RIfloat m02, RIfloat m10, RIfloat m11, RIfloat m12, RIfloat m20, RIfloat m21, RIfloat m22 ) { matrix[0][0] = m00; matrix[0][1] = m01; matrix[0][2] = m02; matrix[1][0] = m10; matrix[1][1] = m11; matrix[1][2] = m12; matrix[2][0] = m20; matrix[2][1] = m21; matrix[2][2] = m22; }
inline const Vector3		Matrix3x3::getRow		( int i ) const						{ RI_ASSERT(i>=0&&i<3); return Vector3(matrix[i][0], matrix[i][1], matrix[i][2]); }
inline const Vector3		Matrix3x3::getColumn	( int i ) const						{ RI_ASSERT(i>=0&&i<3); return Vector3(matrix[0][i], matrix[1][i], matrix[2][i]); }
inline void				Matrix3x3::setRow		( int i, const Vector3& v )			{ RI_ASSERT(i>=0&&i<3); matrix[i][0] = v.x; matrix[i][1] = v.y; matrix[i][2] = v.z; }
inline void				Matrix3x3::setColumn	( int i, const Vector3& v )			{ RI_ASSERT(i>=0&&i<3); matrix[0][i] = v.x; matrix[1][i] = v.y; matrix[2][i] = v.z; }
inline void				Matrix3x3::operator*=	( const Matrix3x3& m )				{ *this = *this * m; }
inline void				Matrix3x3::operator*=	( RIfloat f )							{ for(int i=0;i<3;i++) for(int j=0;j<3;j++) matrix[i][j] *= f; }
inline void				Matrix3x3::operator+=	( const Matrix3x3& m )				{ for(int i=0;i<3;i++) for(int j=0;j<3;j++) matrix[i][j] += m.matrix[i][j]; }
inline void				Matrix3x3::operator-=	( const Matrix3x3& m )				{ for(int i=0;i<3;i++) for(int j=0;j<3;j++) matrix[i][j] -= m.matrix[i][j]; }
inline const Matrix3x3	Matrix3x3::operator-	() const							{ return Matrix3x3( -matrix[0][0],-matrix[0][1],-matrix[0][2], -matrix[1][0],-matrix[1][1],-matrix[1][2], -matrix[2][0],-matrix[2][1],-matrix[2][2]); }
inline void				Matrix3x3::identity		()									{ for(int i=0;i<3;i++) for(int j=0;j<3;j++) matrix[i][j] = (i == j) ? 1.0f : 0.0f; }
inline void				Matrix3x3::transpose	()									{ RI_SWAP(matrix[1][0], matrix[0][1]); RI_SWAP(matrix[2][0], matrix[0][2]); RI_SWAP(matrix[2][1], matrix[1][2]); }
inline RIfloat			Matrix3x3::det			() const							{ return matrix[0][0] * (matrix[1][1]*matrix[2][2] - matrix[2][1]*matrix[1][2]) + matrix[0][1] * (matrix[2][0]*matrix[1][2] - matrix[1][0]*matrix[2][2]) + matrix[0][2] * (matrix[1][0]*matrix[2][1] - matrix[2][0]*matrix[1][1]); }
inline bool				Matrix3x3::isAffine		() const							{ if(matrix[2][0] == 0.0f && matrix[2][1] == 0.0f && matrix[2][2] == 1.0f) return true; return false; }

//==============================================================================================

}	//namespace tgOpenVG

#endif /* __MATH_H */