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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
|
#include "stdafx.h"
#include "Vec3.h"
#include "AABB.h"
DWORD Vec3::tlsIdx = 0;
Vec3::ThreadStorage *Vec3::tlsDefault = NULL;
Vec3::ThreadStorage::ThreadStorage()
{
pool = new Vec3[POOL_SIZE];
poolPointer = 0;
}
Vec3::ThreadStorage::~ThreadStorage()
{
delete [] pool;
}
void Vec3::CreateNewThreadStorage()
{
ThreadStorage *tls = new ThreadStorage();
if(tlsDefault == NULL )
{
tlsIdx = TlsAlloc();
tlsDefault = tls;
}
TlsSetValue(tlsIdx, tls);
}
void Vec3::UseDefaultThreadStorage()
{
TlsSetValue(tlsIdx, tlsDefault);
}
void Vec3::ReleaseThreadStorage()
{
ThreadStorage *tls = (ThreadStorage *)TlsGetValue(tlsIdx);
if( tls == tlsDefault ) return;
delete tls;
}
Vec3 *Vec3::newPermanent(double x, double y, double z)
{
return new Vec3(x,y,z);
};
void Vec3::clearPool()
{
}
void Vec3::resetPool()
{
}
Vec3 *Vec3::newTemp(double x, double y, double z)
{
ThreadStorage *tls = (ThreadStorage *)TlsGetValue(tlsIdx);
Vec3 *thisVec = &tls->pool[tls->poolPointer];
thisVec->set(x, y, z);
tls->poolPointer = ( tls->poolPointer + 1 ) % ThreadStorage::POOL_SIZE;
return thisVec;
}
Vec3::Vec3(double x, double y, double z)
{
if (x == -0.0) x = 0.0;
if (y == -0.0) y = 0.0;
if (z == -0.0) z = 0.0;
this->x = x;
this->y = y;
this->z = z;
}
Vec3 *Vec3::set(double x, double y, double z)
{
this->x = x;
this->y = y;
this->z = z;
return this;
}
Vec3 *Vec3::interpolateTo(Vec3 *t, double p)
{
double xt = x + (t->x - x) * p;
double yt = y + (t->y - y) * p;
double zt = z + (t->z - z) * p;
return Vec3::newTemp(xt, yt, zt);
}
Vec3 *Vec3::vectorTo(Vec3 *p)
{
return Vec3::newTemp(p->x - x, p->y - y, p->z - z);
}
Vec3 *Vec3::normalize()
{
double dist = (double) (sqrt(x * x + y * y + z * z));
if (dist < 0.0001) return Vec3::newTemp(0, 0, 0);
return Vec3::newTemp(x / dist, y / dist, z / dist);
}
double Vec3::dot(Vec3 *p)
{
return x * p->x + y * p->y + z * p->z;
}
Vec3 *Vec3::cross(Vec3 *p)
{
return Vec3::newTemp(y * p->z - z * p->y, z * p->x - x * p->z, x * p->y - y * p->x);
}
Vec3 *Vec3::add(double x, double y, double z)
{
return Vec3::newTemp(this->x + x, this->y + y, this->z + z);
}
double Vec3::distanceTo(Vec3 *p)
{
double xd = p->x - x;
double yd = p->y - y;
double zd = p->z - z;
return (double) sqrt(xd * xd + yd * yd + zd * zd);
}
double Vec3::distanceToSqr(Vec3 *p)
{
double xd = p->x - x;
double yd = p->y - y;
double zd = p->z - z;
return xd * xd + yd * yd + zd * zd;
}
double Vec3::distanceToSqr(double x2, double y2, double z2)
{
double xd = x2 - x;
double yd = y2 - y;
double zd = z2 - z;
return xd * xd + yd * yd + zd * zd;
}
Vec3 *Vec3::scale(double l)
{
return Vec3::newTemp(x * l, y * l, z * l);
}
double Vec3::length()
{
return sqrt(x * x + y * y + z * z);
}
Vec3 *Vec3::clipX(Vec3 *b, double xt)
{
double xd = b->x - x;
double yd = b->y - y;
double zd = b->z - z;
if (xd * xd < 0.0000001f) return NULL;
double d = (xt - x) / xd;
if (d < 0 || d > 1) return NULL;
return Vec3::newTemp(x + xd * d, y + yd * d, z + zd * d);
}
Vec3 *Vec3::clipY(Vec3 *b, double yt)
{
double xd = b->x - x;
double yd = b->y - y;
double zd = b->z - z;
if (yd * yd < 0.0000001f) return NULL;
double d = (yt - y) / yd;
if (d < 0 || d > 1) return NULL;
return Vec3::newTemp(x + xd * d, y + yd * d, z + zd * d);
}
Vec3 *Vec3::clipZ(Vec3 *b, double zt)
{
double xd = b->x - x;
double yd = b->y - y;
double zd = b->z - z;
if (zd * zd < 0.0000001f) return NULL;
double d = (zt - z) / zd;
if (d < 0 || d > 1) return NULL;
return Vec3::newTemp(x + xd * d, y + yd * d, z + zd * d);
}
wstring Vec3::toString()
{
static wchar_t buf[128];
swprintf(buf, 128, L"(%f,%f,%f)",x,y,z);
return wstring(buf);
}
Vec3 *Vec3::lerp(Vec3 *v, double a)
{
return Vec3::newTemp(x + (v->x - x) * a, y + (v->y - y) * a, z + (v->z - z) * a);
}
void Vec3::xRot(float degs)
{
double _cos = cos(degs); // 4J - cos/sin were floats but seems pointless wasting precision here
double _sin = sin(degs);
double xx = x;
double yy = y * _cos + z * _sin;
double zz = z * _cos - y * _sin;
this->x = xx;
this->y = yy;
this->z = zz;
}
void Vec3::yRot(float degs)
{
double _cos = cos(degs); // 4J - cos/sin were floats but seems pointless wasting precision here
double _sin = sin(degs);
double xx = x * _cos + z * _sin;
double yy = y;
double zz = z * _cos - x * _sin;
this->x = xx;
this->y = yy;
this->z = zz;
}
void Vec3::zRot(float degs)
{
double _cos = cos(degs); // 4J - cos/sin were floats but seems pointless wasting precision here
double _sin = sin(degs);
double xx = x * _cos + y * _sin;
double yy = y * _cos - x * _sin;
double zz = z;
this->x = xx;
this->y = yy;
this->z = zz;
}
// Returns 0 if this point is within the box
// Otherwise returns the distance to the box
double Vec3::distanceTo(AABB *box)
{
if(box->contains(this)) return 0;
double xd = 0, yd = 0, zd = 0;
if(x < box->x0) xd = box->x0 - x;
else if( x > box->x1) xd = x - box->x1;
if(y < box->y0) yd = box->y0 - y;
else if( y > box->y1) yd = y - box->y1;
if(z < box->z0) zd = box->z0 - z;
else if( z > box->z1) zd = z - box->z1;
return sqrt(xd * xd + yd * yd + zd * zd);
}
|