/*
/ UTILS.H
/ *******
/ Core utilities class.
/
/ Will Beddow 2010 - http://343gs.com/brainstem
/
/ This software is currently under development
/ and is subject to change and improvement. Some
/ aspects of the system are incomplete and/or 
/ unstable due to ongoing implementation.
/
*/

#ifndef __UTILS_H__
#define __UTILS_H__

#include <math.h>
#include <sstream>

#define BSTM_PI			  ((FLOAT)  3.141592654f)
#define BSTM_PI_OVER_2    ((FLOAT)  1.570796326f)

#define BSTM_TO_RAD(degree) ((degree) * (BSTM_PI / 180.0f))
#define BSTM_TO_DEG(radian) ((radian) * (180.0f / BSTM_PI))

namespace Brainstem
{
	inline float Lerp(float a, float b, float t)
	{
		return a - (a*t) + (b*t);
	}

	inline float Noise(int x)
	{
		x = (x << 13) ^ x;
		return (1.0f - ((x * (x * x * 15731 + 789221) + 1376312589) & 0x7fffffff) / 1073741824.f);    
	}

	inline float CosInterpolate(float v1, float v2, float a)
	{
		float angle = a * BSTM_PI;
		float prc = (1.0f - cos(angle)) * 0.5f;
		return  v1*(1.0f - prc) + v2*prc;
	}

	inline std::string ToString(int x)
	{
		std::ostringstream o;
		o << x;
		return o.str();
	}

	inline std::string ToString(double x)
	{
		std::ostringstream o;
		o << x;
		return o.str();
	}

	inline std::string ToString(float x)
	{
		std::ostringstream o;
		o << x;
		return o.str();
	}

	inline bool ToBool(std::string x)
	{
		if (x == "1")
			return true;
		else
			return false;
	}

	inline std::string ToUpper(std::string x)
	{
		for (size_t i = 0; i < x.length(); ++i)
			x[i] = toupper(x[i]);
    
		return x;
	}

	inline std::string ToLower(std::string x)
	{
		for (size_t i = 0; i < x.length(); ++i)
			x[i] = tolower(x[i]);
    
		return x;
	}

	inline float Random(float seed, float multiplier)
	{
		srand(seed);
		return ((float)rand()/(float)RAND_MAX)*multiplier;
	}

	inline float Random(float seed)
	{
		return Random(seed, 1.0f);
	}
}

#endif