📄 utils.h.svn-base
字号:
#ifndef UTILS_H
#define UTILS_H
#include <cstdlib>
#include <cmath>
#include <cassert>
#include "caPoint2d.h"
using cAni::Point2f;
#pragma warning(disable: 4511 4512) // copy constructor could not be generated, assignment operator could not be generated
// not allowed to copy
class NoCopy
{
protected:
NoCopy()
{
}
private:
NoCopy(const NoCopy &o)
{
o;
}
NoCopy &operator = (const NoCopy &o)
{
return o, *this;
}
};
// singleton
template<class T>
class Singleton : public virtual NoCopy
{
static T *ms_Singleton;
public:
Singleton()
{
assert(ms_Singleton == 0);
ms_Singleton = static_cast<T *>(this);
}
virtual ~Singleton()
{
assert(ms_Singleton);
ms_Singleton = 0;
}
static T& getSingleton()
{
assert(ms_Singleton);
return *ms_Singleton;
}
static T* getSingletonPtr()
{
return ms_Singleton;
}
virtual void releaseSingleton()
{
}
};
template<class T>
T *Singleton<T>::ms_Singleton = 0;
// instance count
template<class T>
class InstanceCount : public virtual NoCopy
{
public:
InstanceCount()
{
ms_count++;
}
virtual ~InstanceCount()
{
ms_count--;
}
static size_t getInstanceCount()
{
return ms_count;
}
protected:
static size_t ms_count;
};
template<class T>
size_t InstanceCount<T>::ms_count = 0;
inline float sign(float x)
{
return (x < 0.0f)? -1.0f : 1.0f;
}
inline float frand(float x = 1.0f)
{
return (rand() / (float) RAND_MAX) * x;
}
inline float Pi()
{
static const float pi = atan(1.0f) * 4.0f;
return pi;
}
template<typename T>
inline T clamp(const T &a, const T &lo, const T &hi)
{
assert(lo <= hi);
if (a >= hi)
return hi;
else if (a <= lo)
return lo;
return a;
}
template<typename T>
inline T lerp(const T &a, const T &b, float fLerp)
{
return a + (b - a) * fLerp;
}
extern bool LineCrossPoint(const Point2f &va, const Point2f &vb, const Point2f &vc, const Point2f &vd, Point2f &out);
extern bool LineCross(const Point2f &va, const Point2f &vb, const Point2f &vc, const Point2f &vd);
extern float DistancePointToEdge(const Point2f &p, const Point2f &a, const Point2f &b);
#endif//UTILS_H
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -