/****************************************************************************************

		Transform2D.h

		Copyright: (c) 2001 Eovia Corp. All rights reserved.

		Revision History

		when		who			what & why
		==========	==========	======================================================
		05/18/99	Frh			Modified the * operator
		05/13/99	Frh			Added SetTranslation/SetRotation

*****************************************************************************************/

#ifndef H_TRANSFORM2D
#define H_TRANSFORM2D

#pragma once




#include "Matrix33.h"
#include "Matrix22.h"


/// TTransform2D is a matrix of the form :
/// \verbatim
///	[ r00 r01 tx ]
///	[ r10 r11 ty ]
///	[ 0   0   1  ]
///	\endverbatim 
///
class TTransform2D : public TMatrix33
{
public:
	TTransform2D() {}
	TTransform2D(const TMatrix33& original ) : TMatrix33(original) {}
	TTransform2D (
				const real r00, const real r01, const real tx,
				const real r10, const real r11, const real ty)
				: TMatrix33(
							r00, r01, tx,
							r10, r11, ty,
							0.0f, 0.0f, 1.0f)
				{}
	TTransform2D (const TMatrix22 &m, const TVector2 &v) : TMatrix33()	{Reset(); SetTranslation(v); SetRotation(m);}

	inline TVector2 operator*(const TVector2 &v);

	/// [x, y, w] -> [x/w, y/w, 1]
	///
	inline TVector2		Homogenize		(const TVector3 &v);
	inline void			SetTranslation	(const TVector2 &v);
	inline void			SetRotation		(const TMatrix22 &v);
};


inline TVector2 TTransform2D::operator*(const TVector2 &v)
{
	TVector2 result;
	TVector2 scaledVector(v[0] - value[0][2], v[1] - value[1][2]);

	result[0] = scaledVector[0]*value[0][0] + scaledVector[1]*value[0][1];
	result[1] = scaledVector[0]*value[1][0] + scaledVector[1]*value[1][1];
	return result;
}

inline TVector2 TTransform2D::Homogenize(const TVector3 &v)
{
	TVector2 result;
	result[0] = v[0]/v[2];
	result[1] = v[1]/v[2];
	return result;
}

inline void TTransform2D::SetTranslation(const TVector2 &v)
{
	value[0][2] = v[0];
	value[1][2] = v[1];
}

inline void TTransform2D::SetRotation(const TMatrix22 &m)
{
	value[0][0] = m[0][0];
	value[0][1] = m[0][1];
	value[1][0] = m[1][0];
	value[1][1] = m[1][1];
}


#endif
