tahoma2d/toonz/sources/include/tutil.h

146 lines
3.3 KiB
C
Raw Normal View History

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