-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathQuickTrig.h
79 lines (63 loc) · 2.43 KB
/
QuickTrig.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
// Copyright (c) 1999-2003 Robin Davies.
// Placed into the public domain.
// All uses permitted without attribution.
#define PI 3.141592653589793238462643383279502
struct TSinCos {
float msin, mcos;
};
enum {
kMSBits = 10,
kLSBits = 10,
kMsTableSize = 1 << kMSBits,
kLsTableSize = 1 << kLSBits,
kBits = kMSBits + kLSBits,
kPowBits = 8,
};
static TSinCos mMsBitsTable[kMsTableSize+1];
static TSinCos mLsBitsTable[kLsTableSize+1];
class CQuickTrigConsts {
public:
enum {
kMaxValidIndex = (2 << 20),
};
protected:
protected:
static void Initialize();
};
class CQuickTrig
: public CQuickTrigConsts
{
public:
inline double QuickSinQ(int nIndex) const {
// Based on the identity sin(u+v) = sinu cosv + cosu sinv
TSinCos *pscu = mMsBitsTable +( (nIndex >> kLSBits) & (kMsTableSize-1));
TSinCos *pscv = mLsBitsTable + ( (nIndex) & (kLsTableSize-1));
return pscu->msin * pscv->mcos + pscu->mcos * pscv->msin;
};
inline double QuickSin(double dAngle) const // Returns sin with 20 bits of precision.
{
return QuickSinQ((int)(dAngle*(kMsTableSize*kLsTableSize/(2*PI))) );
}
inline double QuickCosQ(int nIndex) const {
// based on the identity cos(u+v) = cosu cosv + sinu sinv
TSinCos *pscu = mMsBitsTable +( (nIndex >> kLSBits) & (kMsTableSize-1));
TSinCos *pscv = mLsBitsTable + ( (nIndex) & (kLsTableSize-1));
return pscu->mcos * pscv->mcos - pscu->msin * pscv->msin;
};
inline double QuickCos(double dAngle) const // Returns cos with 20 bits of precision.
{
return QuickCosQ((int)(dAngle*(kMsTableSize*kLsTableSize/(2*PI))) );
}
inline void QuickSinCos(double dAngle,float * cos, float* sin) const // Returns cos with 20 bits of precision.
{
return QuickSinCosQ((int)(dAngle*(kMsTableSize*kLsTableSize/(2*PI))),cos,sin );
}
inline void QuickSinCosQ(int nIndex, float* cos, float*sin) const {
// Based on the identity sin(u+v) = sinu cosv + cosu sinv
// based on the identity cos(u+v) = cosu cosv + sinu sinv
TSinCos *pscu = mMsBitsTable +( (nIndex >> kLSBits) & (kMsTableSize-1));
TSinCos *pscv = mLsBitsTable + ( (nIndex) & (kLsTableSize-1));
*cos= pscu->mcos * pscv->mcos - pscu->msin * pscv->msin;
*sin= pscu->msin * pscv->mcos + pscu->mcos * pscv->msin;
}
};