Shinya Kitaoka 810553
#pragma once
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
#ifndef TUTIL_INCLUDED
Toshihiro Shimizu 890ddd
#define TUTIL_INCLUDED
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
#include "tcommon.h"
Toshihiro Shimizu 890ddd
#include <math.h></math.h>
Toshihiro Shimizu 890ddd
Shinya Kitaoka 9f5a1b
#ifdef _WIN32
Toshihiro Shimizu 890ddd
#include <windows.h></windows.h>
Toshihiro Shimizu 890ddd
#include <winbase.h></winbase.h>
Toshihiro Shimizu 890ddd
#endif
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//! Type definition for a pair of double.
Toshihiro Shimizu 890ddd
typedef std::pair<double, double=""> DoublePair;</double,>
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//! Type definition for a pair of integer.
Toshihiro Shimizu 890ddd
typedef std::pair<int, int=""> IntPair;</int,>
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//! Square of x.
Toshihiro Shimizu 890ddd
/*!
Toshihiro Shimizu 890ddd
  \par x val to square
Toshihiro Shimizu 890ddd
 */
Toshihiro Shimizu 890ddd
template <class t=""></class>
Toshihiro Shimizu 890ddd
inline T sq(T x)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	return x * x;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//! Calculates the floor of a value.
Toshihiro Shimizu 890ddd
/*!
Toshihiro Shimizu 890ddd
  Return the largest integer that is less than or equal to x.
Toshihiro Shimizu 890ddd
  \par x val to floor
Toshihiro Shimizu 890ddd
  \ret largest integer that is less than or equal to x
Toshihiro Shimizu 890ddd
 */
Toshihiro Shimizu 890ddd
inline int tfloor(double x)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	return ((int)(x) > (x) ? (int)(x)-1 : (int)(x));
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//! Calculates the ceiling of a value.
Toshihiro Shimizu 890ddd
/*!
Toshihiro Shimizu 890ddd
  Return the smallest integer that is greater than or equal to x.
Toshihiro Shimizu 890ddd
  \par x val to floor
Toshihiro Shimizu 890ddd
  \ret smallest integer that is greater than or equal to x.
Toshihiro Shimizu 890ddd
 */
Toshihiro Shimizu 890ddd
inline int tceil(double x)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	return ((int)(x) < (x) ? (int)(x) + 1 : (int)(x));
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//! Check if value is an integer.
Toshihiro Shimizu 890ddd
/*!
Toshihiro Shimizu 890ddd
  Return largest integer that is less than or equal to x
Toshihiro Shimizu 890ddd
  \par x val to test
Toshihiro Shimizu 890ddd
  \ret true if val is integer
Toshihiro Shimizu 890ddd
 */
Toshihiro Shimizu 890ddd
inline bool isInt(double x)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	return (int)(x) == (x);
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
inline int tfloor(int x, int step)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	return step * (x >= 0 ? (x / step) : -((-1 - x + step) / step));
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
inline int tceil(int x, int step)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	return step * (x >= 0 ? ((x + step - 1) / step) : -((-x) / step));
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
inline int intLE(double x)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	return tfloor(x);
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
inline int intGT(double x)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	return tfloor(x) + 1;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
inline int intLT(double x)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	return tceil(x) - 1;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
inline int intGE(double x)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	return tceil(x);
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//! convert radiant to degree
Toshihiro Shimizu 890ddd
/*!
Toshihiro Shimizu 890ddd
  Convert an angle from radiant to angle.
Toshihiro Shimizu 890ddd
  \par angle in radiant
Toshihiro Shimizu 890ddd
  \ret angle in degree
Toshihiro Shimizu 890ddd
 */
Toshihiro Shimizu 890ddd
inline double rad2degree(double rad)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	return rad * TConsts::invOf_pi_180;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//! convert degree to radiant
Toshihiro Shimizu 890ddd
/*!
Toshihiro Shimizu 890ddd
  Convert an angle from degree to radiant.
Toshihiro Shimizu 890ddd
  \par angle in degree
Toshihiro Shimizu 890ddd
  \ret angle in radiant
Toshihiro Shimizu 890ddd
 */
Toshihiro Shimizu 890ddd
inline double degree2rad(double degree)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	return degree * TConsts::pi_180;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//! Sign of argument.
Toshihiro Shimizu 890ddd
/*!
Toshihiro Shimizu 890ddd
 Return sign of argument.
Toshihiro Shimizu 890ddd
 \par arg value to test
Toshihiro Shimizu 890ddd
 \ret -1 if arg is negative, 1 if arg is positive, 0 if arg is zero
Toshihiro Shimizu 890ddd
 */
Toshihiro Shimizu 890ddd
template <class t=""></class>
Toshihiro Shimizu 890ddd
inline int tsign(T arg)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	return arg < 0 ? -1 : arg > 0 ? 1 : 0;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//! Check if two values are very similar.
Toshihiro Shimizu 890ddd
/*!
Toshihiro Shimizu 890ddd
  Check if two values are very similar.
Toshihiro Shimizu 890ddd
  \par a first value
Toshihiro Shimizu 890ddd
  \par b second value
Toshihiro Shimizu 890ddd
  \par err max distance from value
Toshihiro Shimizu 890ddd
  \ret bool if value are very similar.
Toshihiro Shimizu 890ddd
 */
Toshihiro Shimizu 890ddd
inline bool areAlmostEqual(double a, double b, double err = TConsts::epsilon)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	return fabs(a - b) < err;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//! Check if two values are very similar.
Toshihiro Shimizu 890ddd
/*!
Toshihiro Shimizu 890ddd
  Check if two values are very similar.
Toshihiro Shimizu 890ddd
  \par a first value
Toshihiro Shimizu 890ddd
  \par b second value
Toshihiro Shimizu 890ddd
  \par err max distance from value
Toshihiro Shimizu 890ddd
  \ret bool if value are very similar.
Toshihiro Shimizu 890ddd
 */
Toshihiro Shimizu 890ddd
template <class t=""></class>
Toshihiro Shimizu 890ddd
inline bool areAlmostEqual(const T &a, const T &b, double err = TConsts::epsilon)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	return tdistance(a, b) < err;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
struct TDeleteObjectFunctor {
Toshihiro Shimizu 890ddd
	template <typename t=""></typename>
Toshihiro Shimizu 890ddd
	void operator()(T *ptr)
Toshihiro Shimizu 890ddd
	{
Toshihiro Shimizu 890ddd
		delete ptr;
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
};
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//! Clear a container deleting all elements.
Toshihiro Shimizu 890ddd
/*!
Toshihiro Shimizu 890ddd
  Clear a container, but before recall delete for all elements.
Toshihiro Shimizu 890ddd
  \par c container
Toshihiro Shimizu 890ddd
  \note the code doesn't work with map because it's impossible
Toshihiro Shimizu 890ddd
        to deduce template
Toshihiro Shimizu 890ddd
 */
Toshihiro Shimizu 890ddd
template <class t=""></class>
Toshihiro Shimizu 890ddd
inline void clearPointerContainer(T &c) throw()
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	T tmp;
Toshihiro Shimizu 890ddd
	std::for_each(c.begin(), c.end(), TDeleteObjectFunctor());
Toshihiro Shimizu 890ddd
	c.swap(tmp);
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
#endif