Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
#include "tgeometry.h"
Toshihiro Shimizu 890ddd
#ifdef LINUX
Toshihiro Shimizu 890ddd
#include <values.h></values.h>
Toshihiro Shimizu 890ddd
#endif
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
using namespace std;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
const T3DPointD TConsts::nap3d((numeric_limits<double>::max)(),</double>
Toshihiro Shimizu 890ddd
							   (numeric_limits<double>::max)(),</double>
Toshihiro Shimizu 890ddd
							   (numeric_limits<double>::max)());</double>
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
const TThickPoint TConsts::natp((numeric_limits<double>::max)(),</double>
Toshihiro Shimizu 890ddd
								(numeric_limits<double>::max)(),</double>
Toshihiro Shimizu 890ddd
								(numeric_limits<double>::max)());</double>
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
const TPointD TConsts::napd((numeric_limits<double>::max)(),</double>
Toshihiro Shimizu 890ddd
							(numeric_limits<double>::max)());</double>
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
const TPointI TConsts::nap((numeric_limits<int>::max)(),</int>
Toshihiro Shimizu 890ddd
						   (numeric_limits<int>::max)());</int>
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
const TRectD TConsts::infiniteRectD(-(numeric_limits<double>::max)(),</double>
Toshihiro Shimizu 890ddd
									-(numeric_limits<double>::max)(),</double>
Toshihiro Shimizu 890ddd
									(numeric_limits<double>::max)(),</double>
Toshihiro Shimizu 890ddd
									(numeric_limits<double>::max)());</double>
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
const TRectI TConsts::infiniteRectI(-(numeric_limits<int>::max)(),</int>
Toshihiro Shimizu 890ddd
									-(numeric_limits<int>::max)(),</int>
Toshihiro Shimizu 890ddd
									(numeric_limits<int>::max)(),</int>
Toshihiro Shimizu 890ddd
									(numeric_limits<int>::max)());</int>
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//==================================================================================================
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
// operazioni fra affini
Toshihiro Shimizu 890ddd
TAffine &TAffine::operator=(const TAffine &a)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	a11 = a.a11;
Toshihiro Shimizu 890ddd
	a12 = a.a12;
Toshihiro Shimizu 890ddd
	a13 = a.a13;
Toshihiro Shimizu 890ddd
	a21 = a.a21;
Toshihiro Shimizu 890ddd
	a22 = a.a22;
Toshihiro Shimizu 890ddd
	a23 = a.a23;
Toshihiro Shimizu 890ddd
	return *this;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
//--------------------------------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
TAffine TAffine::operator*(const TAffine &b) const
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	return TAffine(
Toshihiro Shimizu 890ddd
		a11 * b.a11 + a12 * b.a21,
Toshihiro Shimizu 890ddd
		a11 * b.a12 + a12 * b.a22,
Toshihiro Shimizu 890ddd
		a11 * b.a13 + a12 * b.a23 + a13,
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
		a21 * b.a11 + a22 * b.a21,
Toshihiro Shimizu 890ddd
		a21 * b.a12 + a22 * b.a22,
Toshihiro Shimizu 890ddd
		a21 * b.a13 + a22 * b.a23 + a23);
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
//--------------------------------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
TAffine TAffine::operator*=(const TAffine &b)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	return *this = *this * b;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
//--------------------------------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
TAffine TAffine::inv() const
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	if (a12 == 0.0 && a21 == 0.0) {
Toshihiro Shimizu 890ddd
		assert(a11 != 0.0);
Toshihiro Shimizu 890ddd
		assert(a22 != 0.0);
Toshihiro Shimizu 890ddd
		double inv_a11 = 1.0 / a11;
Toshihiro Shimizu 890ddd
		double inv_a22 = 1.0 / a22;
Toshihiro Shimizu 890ddd
		return TAffine(inv_a11, 0, -a13 * inv_a11,
Toshihiro Shimizu 890ddd
					   0, inv_a22, -a23 * inv_a22);
Toshihiro Shimizu 890ddd
	} else if (a11 == 0.0 && a22 == 0.0) {
Toshihiro Shimizu 890ddd
		assert(a12 != 0.0);
Toshihiro Shimizu 890ddd
		assert(a21 != 0.0);
Toshihiro Shimizu 890ddd
		double inv_a21 = 1.0 / a21;
Toshihiro Shimizu 890ddd
		double inv_a12 = 1.0 / a12;
Toshihiro Shimizu 890ddd
		return TAffine(0, inv_a21, -a23 * inv_a21,
Toshihiro Shimizu 890ddd
					   inv_a12, 0, -a13 * inv_a12);
Toshihiro Shimizu 890ddd
	} else {
Toshihiro Shimizu 890ddd
		double d = 1. / det();
Toshihiro Shimizu 890ddd
		return TAffine(a22 * d, -a12 * d, (a12 * a23 - a22 * a13) * d,
Toshihiro Shimizu 890ddd
					   -a21 * d, a11 * d, (a21 * a13 - a11 * a23) * d);
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
//--------------------------------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
double TAffine::det() const
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	return a11 * a22 - a12 * a21;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//--------------------------------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//Confronti tra affini
Toshihiro Shimizu 890ddd
bool TAffine::operator==(const TAffine &a) const
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	return a11 == a.a11 && a12 == a.a12 && a13 == a.a13 &&
Toshihiro Shimizu 890ddd
		   a21 == a.a21 && a22 == a.a22 && a23 == a.a23;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
//--------------------------------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
bool TAffine::operator!=(const TAffine &a) const
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	return a11 != a.a11 || a12 != a.a12 || a13 != a.a13 ||
Toshihiro Shimizu 890ddd
		   a21 != a.a21 || a22 != a.a22 || a23 != a.a23;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
//--------------------------------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
bool TAffine::isIdentity(double err) const
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	return ((a11 - 1.0) * (a11 - 1.0) + (a22 - 1.0) * (a22 - 1.0) +
Toshihiro Shimizu 890ddd
			a12 * a12 + a13 * a13 + a21 * a21 + a23 * a23) < err;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
//--------------------------------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
bool TAffine::isTranslation(double err) const
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	return ((a11 - 1.0) * (a11 - 1.0) + (a22 - 1.0) * (a22 - 1.0) +
Toshihiro Shimizu 890ddd
			a12 * a12 + a21 * a21) < err;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
//--------------------------------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
bool TAffine::isIsotropic(double err) const
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	return areAlmostEqual(a11, a22, err) && areAlmostEqual(a12, -a21, err);
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//--------------------------------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
// applicazione
Toshihiro Shimizu 890ddd
TPointD TAffine::operator*(const TPointD &p) const
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	return TPointD(p.x * a11 + p.y * a12 + a13, p.x * a21 + p.y * a22 + a23);
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//--------------------------------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
TRectD TAffine::operator*(const TRectD &rect) const
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	if (rect != TConsts::infiniteRectD) {
Toshihiro Shimizu 890ddd
		TPointD p1 = *this * rect.getP00(),
Toshihiro Shimizu 890ddd
				p2 = *this * rect.getP01(),
Toshihiro Shimizu 890ddd
				p3 = *this * rect.getP10(),
Toshihiro Shimizu 890ddd
				p4 = *this * rect.getP11();
Toshihiro Shimizu 890ddd
		return TRectD(tmin(p1.x, p2.x, p3.x, p4.x), tmin(p1.y, p2.y, p3.y, p4.y),
Toshihiro Shimizu 890ddd
					  tmax(p1.x, p2.x, p3.x, p4.x), tmax(p1.y, p2.y, p3.y, p4.y));
Toshihiro Shimizu 890ddd
	} else
Toshihiro Shimizu 890ddd
		return TConsts::infiniteRectD;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//--------------------------------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
TAffine TAffine::place(double u, double v, double x, double y) const
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	return TAffine(a11, a12, x - (a11 * u + a12 * v),
Toshihiro Shimizu 890ddd
				   a21, a22, y - (a21 * u + a22 * v));
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//--------------------------------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
TAffine TAffine::place(const TPointD &pIn, const TPointD &pOut) const
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	return TAffine(a11, a12, pOut.x - (a11 * pIn.x + a12 * pIn.y),
Toshihiro Shimizu 890ddd
				   a21, a22, pOut.y - (a21 * pIn.x + a22 * pIn.y));
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//==================================================================================================
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
TRotation::TRotation(double degrees)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	double rad, sn, cs;
Toshihiro Shimizu 890ddd
	int idegrees = (int)degrees;
Toshihiro Shimizu 890ddd
	if ((double)idegrees == degrees && idegrees % 90 == 0) {
Toshihiro Shimizu 890ddd
		switch ((idegrees / 90) & 3) {
Toshihiro Shimizu 890ddd
		case 0:
Toshihiro Shimizu 890ddd
			sn = 0;
Toshihiro Shimizu 890ddd
			cs = 1;
Toshihiro Shimizu 890ddd
			break;
Toshihiro Shimizu 890ddd
		case 1:
Toshihiro Shimizu 890ddd
			sn = 1;
Toshihiro Shimizu 890ddd
			cs = 0;
Toshihiro Shimizu 890ddd
			break;
Toshihiro Shimizu 890ddd
		case 2:
Toshihiro Shimizu 890ddd
			sn = 0;
Toshihiro Shimizu 890ddd
			cs = -1;
Toshihiro Shimizu 890ddd
			break;
Toshihiro Shimizu 890ddd
		case 3:
Toshihiro Shimizu 890ddd
			sn = -1;
Toshihiro Shimizu 890ddd
			cs = 0;
Toshihiro Shimizu 890ddd
			break;
Toshihiro Shimizu 890ddd
		default:
Toshihiro Shimizu 890ddd
			sn = 0;
Toshihiro Shimizu 890ddd
			cs = 0;
Toshihiro Shimizu 890ddd
			break;
Toshihiro Shimizu 890ddd
		}
Toshihiro Shimizu 890ddd
	} else {
Toshihiro Shimizu 890ddd
		rad = degrees * (TConsts::pi_180);
Toshihiro Shimizu 890ddd
		sn = sin(rad);
Toshihiro Shimizu 890ddd
		cs = cos(rad);
Toshihiro Shimizu 890ddd
		if (sn == 1 || sn == -1)
Toshihiro Shimizu 890ddd
			cs = 0;
Toshihiro Shimizu 890ddd
		if (cs == 1 || cs == -1)
Toshihiro Shimizu 890ddd
			sn = 0;
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
	a11 = cs;
Toshihiro Shimizu 890ddd
	a12 = -sn;
Toshihiro Shimizu 890ddd
	a21 = -a12;
Toshihiro Shimizu 890ddd
	a22 = a11;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
//--------------------------------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
TRotation::TRotation(const TPointD ¢er, double degrees)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	TAffine a = TTranslation(center) * TRotation(degrees) * TTranslation(-center);
Toshihiro Shimizu 890ddd
	a11 = a.a11;
Toshihiro Shimizu 890ddd
	a12 = a.a12;
Toshihiro Shimizu 890ddd
	a13 = a.a13;
Toshihiro Shimizu 890ddd
	a21 = a.a21;
Toshihiro Shimizu 890ddd
	a22 = a.a22;
Toshihiro Shimizu 890ddd
	a23 = a.a23;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//==================================================================================================
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
TScale::TScale(const TPointD ¢er, double sx, double sy)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	TAffine a = TTranslation(center) * TScale(sx, sy) * TTranslation(-center);
Toshihiro Shimizu 890ddd
	a11 = a.a11;
Toshihiro Shimizu 890ddd
	a12 = a.a12;
Toshihiro Shimizu 890ddd
	a13 = a.a13;
Toshihiro Shimizu 890ddd
	a21 = a.a21;
Toshihiro Shimizu 890ddd
	a22 = a.a22;
Toshihiro Shimizu 890ddd
	a23 = a.a23;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
//--------------------------------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
TScale::TScale(const TPointD ¢er, double s)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	TAffine a = TTranslation(center) * TScale(s) * TTranslation(-center);
Toshihiro Shimizu 890ddd
	a11 = a.a11;
Toshihiro Shimizu 890ddd
	a12 = a.a12;
Toshihiro Shimizu 890ddd
	a13 = a.a13;
Toshihiro Shimizu 890ddd
	a21 = a.a21;
Toshihiro Shimizu 890ddd
	a22 = a.a22;
Toshihiro Shimizu 890ddd
	a23 = a.a23;
Toshihiro Shimizu 890ddd
}