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>
Shinya Kitaoka 120a6e
inline T sq(T x) {
Shinya Kitaoka 120a6e
  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
 */
Shinya Kitaoka 120a6e
inline int tfloor(double x) { return ((int)(x) > (x) ? (int)(x)-1 : (int)(x)); }
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
 */
Shinya Kitaoka 120a6e
inline int tceil(double x) {
Shinya Kitaoka 120a6e
  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
 */
Shinya Kitaoka 120a6e
inline bool isInt(double x) { return (int)(x) == (x); }
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
inline int tfloor(int x, int step) {
Shinya Kitaoka 120a6e
  return step * (x >= 0 ? (x / step) : -((-1 - x + step) / step));
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
inline int tceil(int x, int step) {
Shinya Kitaoka 120a6e
  return step * (x >= 0 ? ((x + step - 1) / step) : -((-x) / step));
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
inline int intLE(double x) { return tfloor(x); }
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
inline int intGT(double x) { return tfloor(x) + 1; }
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
inline int intLT(double x) { return tceil(x) - 1; }
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
inline int intGE(double x) { return tceil(x); }
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
 */
Shinya Kitaoka 120a6e
inline double rad2degree(double rad) { return rad * M_180_PI; }
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
 */
Shinya Kitaoka 120a6e
inline double degree2rad(double degree) { return degree * M_PI_180; }
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>
Shinya Kitaoka 120a6e
inline int tsign(T arg) {
Shinya Kitaoka 120a6e
  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
 */
Shinya Kitaoka 120a6e
inline bool areAlmostEqual(double a, double b, double err = TConsts::epsilon) {
Shinya Kitaoka 120a6e
  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>
Shinya Kitaoka 120a6e
inline bool areAlmostEqual(const T &a, const T &b,
Shinya Kitaoka 120a6e
                           double err = TConsts::epsilon) {
Shinya Kitaoka 120a6e
  return tdistance(a, b) < err;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
struct TDeleteObjectFunctor {
Shinya Kitaoka 120a6e
  template <typename t=""></typename>
Shinya Kitaoka 120a6e
  void operator()(T *ptr) {
Shinya Kitaoka 120a6e
    delete ptr;
Shinya Kitaoka 120a6e
  }
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>
Shinya Kitaoka 120a6e
inline void clearPointerContainer(T &c) throw() {
Shinya Kitaoka 120a6e
  T tmp;
Shinya Kitaoka 120a6e
  std::for_each(c.begin(), c.end(), TDeleteObjectFunctor());
Shinya Kitaoka 120a6e
  c.swap(tmp);
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
#endif