193 lines
No EOL
4.4 KiB
C++
193 lines
No EOL
4.4 KiB
C++
// The following ifdef block is the standard way of creating macros which make exporting
|
|
// from a DLL simpler. All files within this DLL are compiled with the CCMATH_EXPORTS
|
|
// symbol defined on the command line. this symbol should not be defined on any project
|
|
// that uses this DLL. This way any other project whose source files include this file see
|
|
// CCMATH_API functions as being imported from a DLL, whereas this DLL sees symbols
|
|
// defined with this macro as being exported.
|
|
#ifndef ___CCMATH_H___
|
|
#define ___CCMATH_H___
|
|
|
|
//#define CCMATH_DLL_MODE
|
|
|
|
#ifdef CCMATH_DLL_MODE
|
|
#ifdef CCMATH_EXPORTS
|
|
#define CCMATH_API __declspec(dllexport)
|
|
#else
|
|
#define CCMATH_API __declspec(dllimport)
|
|
#endif
|
|
#else
|
|
#define CCMATH_API
|
|
#endif
|
|
|
|
class CCMATH_API Vector
|
|
{
|
|
public:
|
|
union {
|
|
struct {float x,y,z;};
|
|
float Scalar[3];
|
|
};
|
|
|
|
Vector();
|
|
Vector(const float _x, const float _y, const float _z);
|
|
|
|
Vector operator+(const Vector &vec2) const;
|
|
Vector operator-(const Vector &vec2) const;
|
|
Vector operator&(const Vector &vec2) const;
|
|
Vector operator*(const float Value) const;
|
|
Vector operator^(const Vector &B) const;
|
|
|
|
float operator*(const Vector &vec2) const;
|
|
void operator+=(const Vector &vec2);
|
|
void operator+=(const float Value);
|
|
void operator-=(const Vector &vec2);
|
|
void operator*=(const float Value);
|
|
void operator&=(const Vector &vec2);
|
|
int operator!=(const Vector &vec2);
|
|
|
|
int CheckNAN();
|
|
void Clean();
|
|
|
|
int isRightXY(const Vector &A,const Vector &B) const;
|
|
float DistanceToLineXY(const Vector &A,const Vector &B) const;
|
|
Vector VectorToLine(const Vector &DirNormalized) const;
|
|
|
|
float SqrNorm() const;
|
|
float Norm() const;
|
|
|
|
float NormXY() const;
|
|
float SqrNormXY() const;
|
|
|
|
void Normalize();
|
|
|
|
void Max(const Vector &vecB);
|
|
void Min(const Vector &vecB);
|
|
|
|
int IsIn2D(const Vector &Min,const Vector &Max);
|
|
|
|
void Rotate90XY()
|
|
{
|
|
float Swap;
|
|
Swap = y;
|
|
y = x;
|
|
x = -Swap;
|
|
};
|
|
|
|
int IsRightLeftXY(const Vector &A,const Vector &B, int IsLeft = 0) const
|
|
{
|
|
Vector D,E;
|
|
float R;
|
|
D = *this;
|
|
D -= A;
|
|
E = B;
|
|
E -= A;
|
|
R = (D.y * E.x) - (D.x * E.y);
|
|
if (IsLeft)
|
|
return R > 0 ? 0 : 1;
|
|
else
|
|
return R > 0 ? 1 : 0;
|
|
};
|
|
|
|
};
|
|
|
|
class CCMATH_API MATRIX
|
|
{
|
|
public:
|
|
Vector I,J,K,T;
|
|
|
|
MATRIX();
|
|
|
|
void Identity();
|
|
int CheckNAN();
|
|
void Clean();
|
|
void Transp();
|
|
|
|
void Inverse(MATRIX &Dst);
|
|
void Blend(MATRIX &End, float Factor);
|
|
|
|
void GetScale(Vector &V);
|
|
void SetScale(Vector &V);
|
|
|
|
void RotateAround_I(float Alpha);
|
|
void RotateAround_J(float Alpha);
|
|
void RotateAround_K(float Alpha);
|
|
|
|
void RotateAround_X(float Alpha);
|
|
void RotateAround_Y(float Alpha);
|
|
void RotateAround_Z(float Alpha);
|
|
|
|
void RotateAround(int Axis, float Alpha);
|
|
|
|
inline Vector operator*(const Vector &V) const;
|
|
void operator*=(MATRIX &M);
|
|
void operator*=(float Scale);
|
|
|
|
void ToEuler(Vector &eulerVect);
|
|
void FromEuler(Vector eulerVect);
|
|
};
|
|
|
|
class CCMATH_API Quat
|
|
{
|
|
public:
|
|
float x,y,z,w;
|
|
Quat(void);
|
|
Quat(float _x,float _y,float _z,float _w);
|
|
Quat(Vector V,float _w);;
|
|
Quat(Vector V);
|
|
|
|
void ComputeFromMatrix(MATRIX *ActualMat);
|
|
void TransforToMatrix(MATRIX *ActualMat);
|
|
void TransforToVector(Vector &V);
|
|
|
|
|
|
void ExtractFrom2Edges(Vector *Src,Vector *Dst);
|
|
|
|
void operator*=(const float F);
|
|
void operator+=(const Quat &Q2);
|
|
Quat operator+(const Quat &Q2);
|
|
|
|
void Normalize();
|
|
void NormalizeW();
|
|
|
|
Quat operator*(const Quat &Q2) const;
|
|
Quat operator*(const float F) const;
|
|
void operator*=(const Quat &Q2);
|
|
};
|
|
|
|
// BIT functions
|
|
|
|
void ComputeBitNum();
|
|
unsigned int GetBitNumberMIT(unsigned int n);
|
|
unsigned int GetBitNumberT(unsigned int Value);
|
|
unsigned int GetBitNumber(unsigned int n);
|
|
unsigned int GetBitReverse(unsigned int Value);
|
|
unsigned int GetBitExpand(unsigned int Value); // Transform 0xFFFF into 0x55555555
|
|
unsigned int GetBitContract(unsigned int Value); // Transform 0x55555555 into 0xFFFF
|
|
int GetLogUpperPO2(float Value);
|
|
int GetUpperPO2(float Value);
|
|
int GetFirstBit(unsigned int Mask);
|
|
|
|
_inline float FastInvSqrt(float x)
|
|
{
|
|
float xhalf = 0.5f*x;
|
|
int i = *(int*)&x; // get bits for floating value
|
|
i = 0x5f375a86- (i>>1); // gives initial guess y0
|
|
x = *(float*)&i; // convert bits back to float
|
|
x = x*(1.5f-xhalf*x*x); // Newton step, repeating increases accuracy
|
|
return x;
|
|
}
|
|
|
|
_inline float FastSqrt(float number) {
|
|
long i;
|
|
float x, y;
|
|
x = number * 0.5F;
|
|
y = number;
|
|
i = * ( long * ) &y;
|
|
i = 0x5f3759df - ( i >> 1 );
|
|
y = * ( float * ) &i;
|
|
y = y * ( 1.5F - ( x * y * y ) );
|
|
y = y * ( 1.5F - ( x * y * y ) );// 2nd iteration : Can be removed
|
|
return number * y;
|
|
}
|
|
|
|
|
|
#endif |