/*
This program is distributed under the terms of the 'MIT license'. The text
of this licence follows...

Copyright (c) 2005 J.D.Medhurst (a.k.a. Tixy)

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
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 Software.

THE SOFTWARE IS 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 SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/

/**
@file

@brief 3D vector maths using fixed-point arithmetic
*/

#include "common.h"
#include "vector3.h"


/*
Members or class Vector3
*/


Vector3 Vector3::operator - () const
	{
	return Vector3(-X,-Y,-Z);
	}


Vector3 Vector3::operator + (const Vector3& vector) const
	{
	return Vector3(X+vector.X,Y+vector.Y,Z+vector.Z);
	}


Vector3 Vector3::operator - (const Vector3& vector) const
	{
	return Vector3(X-vector.X,Y-vector.Y,Z-vector.Z);
	}


Vector3 Vector3::operator * (fix scalar) const
	{
	return Vector3(Fix::MulNS(X,scalar),Fix::MulNS(Y,scalar),Fix::MulNS(Z,scalar));
	}


Vector3 Vector3::operator / (fix scalar) const
	{
	return Vector3(Fix::Div(X,scalar),Fix::Div(Y,scalar),Fix::Div(Z,scalar));
	}


Vector3& Vector3::operator += (const Vector3& vector)
	{
	X += vector.X;
	Y += vector.Y;
	Z += vector.Z;
	return *this;
	}


Vector3& Vector3::operator -= (const Vector3& vector)
	{
	X -= vector.X;
	Y -= vector.Y;
	Z -= vector.Z;
	return *this;
	}


Vector3& Vector3::operator *= (fix scalar)
	{
	X = Fix::MulNS(X,scalar);
	Y = Fix::MulNS(Y,scalar);
	Z = Fix::MulNS(Z,scalar);
	return *this;
	}


Vector3& Vector3::operator /= (fix scalar)
	{
	X = Fix::Div(X,scalar);
	Y = Fix::Div(Y,scalar);
	Z = Fix::Div(Z,scalar);
	return *this;
	}


bool Vector3::operator == (const Vector3& vector) const
	{
	return (X==vector.X && Y==vector.Y && Z==vector.Z);
	}


fix Vector3::DotProduct(const Vector3& vector) const
	{
	return Fix::MulNS(X,vector.X)+Fix::MulNS(Y,vector.Y)+Fix::MulNS(Z,vector.Z);
	}


Vector3 Vector3::CrossProduct(const Vector3& vector) const
	{
	return Vector3(Fix::MulNS(Y,vector.Z)-Fix::MulNS(Z,vector.Y),
				   Fix::MulNS(Z,vector.X)-Fix::MulNS(X,vector.Z),
				   Fix::MulNS(X,vector.Y)-Fix::MulNS(Y,vector.X));
	}


static unsigned MostSignificantBit(uint32_t a)
	{
	unsigned b = 0;
	if(a>=(1<<16)) b |= 16, a >>= 16;
	if(a>=(1<<8))  b |= 8,	a >>= 8;
	if(a>=(1<<4))  b |= 4,	a >>= 4;
	if(a>=(1<<2))  b |= 2,	a >>= 2;
	b |= a>>1;
	return b;
	}


ufix Vector3::Length() const
	{
	// calculate square of the length
	unsigned f;
	unsigned i = LengthSquared(f);

	// if no integer part, return the square root
	if(!i)
		return Fix::Sqrt(f)>>8;

	// shift square of result so it fits into 32 bits
	int shift = (2+MostSignificantBit(i))&~1;
	ufix s;
	if(shift==32)
		s = i;
	else
		{
		s = f>>shift;
		s |= i<<(32-shift);
		}

	// take the square root
	ufix r = Fix::Sqrt(s);

	// shift result to get binary point in the correct place
	shift = (16-shift)>>1;
	if(shift>0)
		r >>= shift;
	if(shift<0)
		r <<= -shift;

	return r;
	}


int Vector3::CompareLength(ufix length) const
	{
	// calculate the square of this vectors length as ai:af
	uint32_t af;
	uint32_t ai = LengthSquared(af);

	// calculate the square of 'length' as bi:bf
	uint32_t l = length&0xFFFF;
	uint32_t h = length>>16;
	uint32_t bf = l*l;
	uint32_t bi = h*h;
	uint32_t r = l*h;
	uint32_t obf = bf;
	bf += r<<(16+1);
	if(bf<obf) bi++;
	bi += r>>(16-1);

	// compare the squares of the length
	if(ai<bi)
		return -1;
	if(ai>bi)
		return 1;
	if(af<bf)
		return -1;
	if(af>bf)
		return 1;
	return 0;
	}


int Vector3::CompareLengths(const Vector3& vector) const
	{
	uint32_t af,bf;
	uint32_t ai = LengthSquared(af);
	uint32_t bi = vector.LengthSquared(bf);

	if(ai<bi)
		return -1;
	if(ai>bi)
		return 1;
	if(af<bf)
		return -1;
	if(af>bf)
		return 1;
	return 0;
	}


uint32_t Vector3::LengthSquared(uint32_t& fraction) const
	{
	int32_t 	v,r;
	uint32_t	l,h,lo,hi,ol;

	v = X;
	l = v&0xFFFF;
	h = v>>16;
	lo = l*l;
	hi = h*h;
	r = l*h;
	ol = lo;
	lo += r<<(16+1);
	if(lo<ol) hi++;
	hi += r>>(16-1);

	v = Y;
	l = v&0xFFFF;
	h = v>>16;
	ol = lo;
	lo += l*l;
	if(lo<ol) hi++;
	hi += h*h;
	r = l*h;
	ol = lo;
	lo += r<<(16+1);
	if(lo<ol) hi++;
	hi += r>>(16-1);

	v = Z;
	l = v&0xFFFF;
	h = v>>16;
	ol = lo;
	lo += l*l;
	if(lo<ol) hi++;
	hi += h*h;
	r = l*h;
	ol = lo;
	lo += r<<(16+1);
	if(lo<ol) hi++;
	hi += r>>(16-1);

	fraction = lo;
	return hi;
	}


void Vector3::NormaliseComponents(unsigned bits)
	{
	// get components
	fix x = X;
	fix y = Y;
	fix z = Z;

	// find the magnitude of the largest component
	ufix max = x;
	if(x<0)
		max = ~x;

	ufix ay = y;
	if(y<0)
		ay = ~y;
	if(ay>max)
		max = ay;

	ufix az = z;
	if(z<0)
		az = ~z;
	if(az>max)
		max = az;

	// calculate shift value to get msb in correct place
	int shift = MostSignificantBit(max)-bits;

	// shift all components
	if(shift>0)
		{
		x >>= shift;
		y >>= shift;
		z >>= shift;
		}
	else if(shift<0)
		{
		shift = -shift;
		x <<= shift;
		y <<= shift;
		z <<= shift;
		}

	X = x;
	Y = y;
	Z = z;
	}


Vector3 Vector3::UnitVector() const
	{
	// normalise component values to 15 bits
	Vector3 normalised(*this);
	normalised.NormaliseComponents(14);

	// calculate length of normalised vector
	fix x = normalised.X;
	fix y = normalised.Y;
	fix z = normalised.Z;
	ufix l = Fix::Sqrt(x*x+y*y+z*z);

	// divide components by length to get the unit vector
	x = Fix::Div(x<<8,l);
	y = Fix::Div(y<<8,l);
	z = Fix::Div(z<<8,l);

	return Vector3(x,y,z);
	}


Vector3 Vector3::Normal(const Vector3& vector) const
	{
	// scale both vectors to 15 bits
	Vector3 a(*this);
	a.NormaliseComponents(14);
	Vector3 b(vector);
	b.NormaliseComponents(14);

	// calculate cross product (a vector normal to the two vectors)
	a = Vector3(a.Y*b.Z - a.Z*b.Y,
				a.Z*b.X - a.X*b.Z,
				a.X*b.Y - a.Y*b.X);

	// return the normal as a length 1.0 unit vector
	return a.UnitVector();
	}


fixangle Vector3::Angle(const Vector3& vector) const
	{
	return Fix::ACos(UnitVector().DotProduct(vector.UnitVector()));
	}


Vector3 Vector3::Normal(const Vector3& point1,const Vector3& point2) const
	{
	return (point1-*this).Normal(point2-*this);
	}


fixangle Vector3::Angle(const Vector3& point1,const Vector3& point2) const
	{
	return (point1-*this).Angle(point2-*this);
	}


void Vector3::Translate(Vector3* outVectors,unsigned vectorCount,const Vector3* inVectors,const Vector3& offset)
	{
	fix x = offset.X;
	fix y = offset.Y;
	fix z = offset.Z;
	Vector3* end = outVectors+vectorCount;
	while(outVectors<end)
		{
		outVectors->X = inVectors->X+x;
		outVectors->Y = inVectors->Y+y;
		outVectors->Z = inVectors->Z+z;
		++inVectors;
		++outVectors;
		}
	}


void Vector3::Scale(Vector3* outVectors,unsigned vectorCount,const Vector3* inVectors,fix scale)
	{
	Vector3* end = outVectors+vectorCount;
	while(outVectors<end)
		{
		outVectors->X = Fix::MulNS(inVectors->X,scale);
		outVectors->Y = Fix::MulNS(inVectors->Y,scale);
		outVectors->Z = Fix::MulNS(inVectors->Z,scale);
		++inVectors;
		++outVectors;
		}
	}


/*
Members of class Matrix3
*/


Vector3 Matrix3::operator * (const Vector3& vector) const
	{
	fix ax = vector.X;
	fix ay = vector.Y;
	fix az = vector.Z;

	fix rx = Fix::MulNS(ax,Row1.X)+Fix::MulNS(ay,Row1.Y)+Fix::MulNS(az,Row1.Z);
	fix ry = Fix::MulNS(ax,Row2.X)+Fix::MulNS(ay,Row2.Y)+Fix::MulNS(az,Row2.Z);
	fix rz = Fix::MulNS(ax,Row3.X)+Fix::MulNS(ay,Row3.Y)+Fix::MulNS(az,Row3.Z);

	return Vector3(rx,ry,rz);
	}


Matrix3 Matrix3::Transposition() const
	{
	return Matrix3(Vector3(Row1.X,Row2.X,Row3.X),
				   Vector3(Row1.Y,Row2.Y,Row3.Y),
				   Vector3(Row1.Z,Row2.Z,Row3.Z));
	}


void Matrix3::Transform(Vector3* outVectors,unsigned vectorCount,const Vector3* inVectors)
	{
	Vector3* end = outVectors+vectorCount;
	while(outVectors<end)
		*outVectors++ = *this * *inVectors++;
	}

