-
Notifications
You must be signed in to change notification settings - Fork 110
/
Copy pathC3DMathUtilty.cpp
111 lines (91 loc) · 1.94 KB
/
C3DMathUtilty.cpp
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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
//
// C3DUtilty.cpp
// test3D
//
// Created by ck04-040 on 13-8-5.
//
//
#include "C3DMath.h"
namespace cocos3d
{
C3DMathUtility::C3DMathUtility(): _costab(NULL), _sintab(NULL)
{
}
C3DMathUtility::~C3DMathUtility()
{
delete[] _costab;
delete[] _sintab;
}
C3DMathUtility& C3DMathUtility::getInstance()
{
static C3DMathUtility instance;
return instance;
}
// approximate value of sin(rad), precision 1 degree
float C3DMathUtility::sin(float rad, bool isUseTable)
{
if (isUseTable)
{
initMathUtility();
int degree = rad2degree(rad);
return _sintab[degree];
}
return sinf(rad);
}
// approximate value of sin(rad), precision 1 degree
float C3DMathUtility::cos(float rad, bool isUseTable)
{
if (isUseTable)
{
initMathUtility();
int degree = rad2degree(rad);
return _costab[degree];
}
return cosf(rad);
}
int C3DMathUtility::rad2degree(float rad)
{
int degree = (int)(MATH_RAD_TO_DEG(rad) + 0.5f);
if (degree >= 360 || degree <= -360)
{
degree = degree % 360;
}
if (degree < 0)
degree += 360;
return degree;
}
void C3DMathUtility::sincos(float rad, float *sinvalue, float* cosvalue, bool isUseTable)
{
if (isUseTable)
{
initMathUtility();
int degree = rad2degree(rad);
if (sinvalue)
*sinvalue = _sintab[degree];
if (cosvalue)
*cosvalue = _costab[degree];
}
else
{
if (sinvalue)
*sinvalue = sinf(rad);
if (cosvalue)
*cosvalue = cosf(rad);
}
}
void C3DMathUtility::initMathUtility()
{
if (_sintab && _costab)
return;
delete[] _sintab;
delete[] _costab;
const int count = 360;
_costab = new float[count];
_sintab = new float[count];
for (int i = 0; i < count; i++) {
float rad = MATH_DEG_TO_RAD(i);
_sintab[i] = sinf(rad);
_costab[i] = cosf(rad);
}
}
}