#include "coordConvertor.h"
#include <cmath>
using namespace ANALYTICAL_GEOMETRY;

CoordConvertor::CoordConvertor( double serverToClientScaleFactor  )
{
	//note:  (A/B)/C == A/BC ...but more stable in our case!!!
	//this->serverToClientScaleFactor = ( clientMaxCoordVal / serverMaxCoordVal ) / serverToClientExtraScaleVal;

	//this->serverMaxValue = serverMaxValue;
	this->serverToClientScaleFactor =  serverToClientScaleFactor;
	this->centerOffset.set_zero();
}

double CoordConvertor::convertServerToClientValue( const double value ) const
{
	return value * this->serverToClientScaleFactor;
}

double CoordConvertor::convertClientToServerValue( const double value ) const
{
	return value / this->serverToClientScaleFactor;
}

Point_3<double> CoordConvertor::convertServerToClientPoint( const Point_3<double> &serverPoint ) const
{
	Point_3<double> clientPoint( serverPoint.x() * this->serverToClientScaleFactor, 
								 serverPoint.y() * this->serverToClientScaleFactor,
								 serverPoint.z() * this->serverToClientScaleFactor );

	Point_3<double> translatedClientPoint = clientPoint + this->centerOffset;
	
	return translatedClientPoint;
}

Point_3<double> CoordConvertor::convertClientToServerPoint( const Point_3<double> &clientPoint ) const
{
	Point_3<double> translatedClientPoint = clientPoint - this->centerOffset;
	Point_3<double> serverPoint( translatedClientPoint.x() / this->serverToClientScaleFactor, 
								 translatedClientPoint.y() / this->serverToClientScaleFactor,
								 translatedClientPoint.z() / this->serverToClientScaleFactor );
	return serverPoint;
}

void CoordConvertor::setClientCoordCenter( ANALYTICAL_GEOMETRY::Point_3<double> &clientCenter )
{
	this->centerOffset.set_data( -clientCenter.x(), -clientCenter.y(), -clientCenter.z() );
}

Vector_3<double> CoordConvertor::getCenterOffset(void) const
{
	return this->centerOffset;
}

