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
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
|
/**
* @file v3dmath.h
* @brief High precision 3 dimensional vector.
*
* $LicenseInfo:firstyear=2000&license=viewerlgpl$
* Second Life Viewer Source Code
* Copyright (C) 2010, Linden Research, Inc.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation;
* version 2.1 of the License only.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*
* Linden Research, Inc., 945 Battery Street, San Francisco, CA 94111 USA
* $/LicenseInfo$
*/
#ifndef LL_V3DMATH_H
#define LL_V3DMATH_H
//#include "llerror.h"
#include "v3math.h"
class LLVector3d
{
public:
F64 mdV[3];
const static LLVector3d zero;
const static LLVector3d x_axis;
const static LLVector3d y_axis;
const static LLVector3d z_axis;
const static LLVector3d x_axis_neg;
const static LLVector3d y_axis_neg;
const static LLVector3d z_axis_neg;
inline LLVector3d(); // Initializes LLVector3d to (0, 0, 0)
inline LLVector3d(const F64 x, const F64 y, const F64 z); // Initializes LLVector3d to (x. y, z)
inline explicit LLVector3d(const F64 *vec); // Initializes LLVector3d to (vec[0]. vec[1], vec[2])
inline explicit LLVector3d(const LLVector3 &vec);
explicit LLVector3d(const LLSD& sd)
{
setValue(sd);
}
void setValue(const LLSD& sd)
{
mdV[0] = sd[0].asReal();
mdV[1] = sd[1].asReal();
mdV[2] = sd[2].asReal();
}
LLSD getValue() const
{
LLSD ret;
ret[0] = mdV[0];
ret[1] = mdV[1];
ret[2] = mdV[2];
return ret;
}
inline BOOL isFinite() const; // checks to see if all values of LLVector3d are finite
BOOL clamp(const F64 min, const F64 max); // Clamps all values to (min,max), returns TRUE if data changed
BOOL abs(); // sets all values to absolute value of original value (first octant), returns TRUE if changed
inline const LLVector3d& clearVec(); // Clears LLVector3d to (0, 0, 0, 1)
inline const LLVector3d& setZero(); // Zero LLVector3d to (0, 0, 0, 0)
inline const LLVector3d& zeroVec(); // deprecated
inline const LLVector3d& setVec(const F64 x, const F64 y, const F64 z); // Sets LLVector3d to (x, y, z, 1)
inline const LLVector3d& setVec(const LLVector3d &vec); // Sets LLVector3d to vec
inline const LLVector3d& setVec(const F64 *vec); // Sets LLVector3d to vec
inline const LLVector3d& setVec(const LLVector3 &vec);
F64 magVec() const; // Returns magnitude of LLVector3d
F64 magVecSquared() const; // Returns magnitude squared of LLVector3d
inline F64 normVec(); // Normalizes and returns the magnitude of LLVector3d
F64 length() const; // Returns magnitude of LLVector3d
F64 lengthSquared() const; // Returns magnitude squared of LLVector3d
inline F64 normalize(); // Normalizes and returns the magnitude of LLVector3d
const LLVector3d& rotVec(const F64 angle, const LLVector3d &vec); // Rotates about vec by angle radians
const LLVector3d& rotVec(const F64 angle, const F64 x, const F64 y, const F64 z); // Rotates about x,y,z by angle radians
const LLVector3d& rotVec(const LLMatrix3 &mat); // Rotates by LLMatrix4 mat
const LLVector3d& rotVec(const LLQuaternion &q); // Rotates by LLQuaternion q
BOOL isNull() const; // Returns TRUE if vector has a _very_small_ length
BOOL isExactlyZero() const { return !mdV[VX] && !mdV[VY] && !mdV[VZ]; }
const LLVector3d& operator=(const LLVector4 &a);
F64 operator[](int idx) const { return mdV[idx]; }
F64 &operator[](int idx) { return mdV[idx]; }
friend LLVector3d operator+(const LLVector3d &a, const LLVector3d &b); // Return vector a + b
friend LLVector3d operator-(const LLVector3d &a, const LLVector3d &b); // Return vector a minus b
friend F64 operator*(const LLVector3d &a, const LLVector3d &b); // Return a dot b
friend LLVector3d operator%(const LLVector3d &a, const LLVector3d &b); // Return a cross b
friend LLVector3d operator*(const LLVector3d &a, const F64 k); // Return a times scaler k
friend LLVector3d operator/(const LLVector3d &a, const F64 k); // Return a divided by scaler k
friend LLVector3d operator*(const F64 k, const LLVector3d &a); // Return a times scaler k
friend bool operator==(const LLVector3d &a, const LLVector3d &b); // Return a == b
friend bool operator!=(const LLVector3d &a, const LLVector3d &b); // Return a != b
friend const LLVector3d& operator+=(LLVector3d &a, const LLVector3d &b); // Return vector a + b
friend const LLVector3d& operator-=(LLVector3d &a, const LLVector3d &b); // Return vector a minus b
friend const LLVector3d& operator%=(LLVector3d &a, const LLVector3d &b); // Return a cross b
friend const LLVector3d& operator*=(LLVector3d &a, const F64 k); // Return a times scaler k
friend const LLVector3d& operator/=(LLVector3d &a, const F64 k); // Return a divided by scaler k
friend LLVector3d operator-(const LLVector3d &a); // Return vector -a
friend std::ostream& operator<<(std::ostream& s, const LLVector3d &a); // Stream a
static BOOL parseVector3d(const std::string& buf, LLVector3d* value);
};
typedef LLVector3d LLGlobalVec;
const LLVector3d &LLVector3d::setVec(const LLVector3 &vec)
{
mdV[0] = vec.mV[0];
mdV[1] = vec.mV[1];
mdV[2] = vec.mV[2];
return *this;
}
inline LLVector3d::LLVector3d(void)
{
mdV[0] = 0.f;
mdV[1] = 0.f;
mdV[2] = 0.f;
}
inline LLVector3d::LLVector3d(const F64 x, const F64 y, const F64 z)
{
mdV[VX] = x;
mdV[VY] = y;
mdV[VZ] = z;
}
inline LLVector3d::LLVector3d(const F64 *vec)
{
mdV[VX] = vec[VX];
mdV[VY] = vec[VY];
mdV[VZ] = vec[VZ];
}
inline LLVector3d::LLVector3d(const LLVector3 &vec)
{
mdV[VX] = vec.mV[VX];
mdV[VY] = vec.mV[VY];
mdV[VZ] = vec.mV[VZ];
}
/*
inline LLVector3d::LLVector3d(const LLVector3d ©)
{
mdV[VX] = copy.mdV[VX];
mdV[VY] = copy.mdV[VY];
mdV[VZ] = copy.mdV[VZ];
}
*/
// Destructors
// checker
inline BOOL LLVector3d::isFinite() const
{
return (llfinite(mdV[VX]) && llfinite(mdV[VY]) && llfinite(mdV[VZ]));
}
// Clear and Assignment Functions
inline const LLVector3d& LLVector3d::clearVec(void)
{
mdV[0] = 0.f;
mdV[1] = 0.f;
mdV[2]= 0.f;
return (*this);
}
inline const LLVector3d& LLVector3d::setZero(void)
{
mdV[0] = 0.f;
mdV[1] = 0.f;
mdV[2] = 0.f;
return (*this);
}
inline const LLVector3d& LLVector3d::zeroVec(void)
{
mdV[0] = 0.f;
mdV[1] = 0.f;
mdV[2] = 0.f;
return (*this);
}
inline const LLVector3d& LLVector3d::setVec(const F64 x, const F64 y, const F64 z)
{
mdV[VX] = x;
mdV[VY] = y;
mdV[VZ] = z;
return (*this);
}
inline const LLVector3d& LLVector3d::setVec(const LLVector3d &vec)
{
mdV[0] = vec.mdV[0];
mdV[1] = vec.mdV[1];
mdV[2] = vec.mdV[2];
return (*this);
}
inline const LLVector3d& LLVector3d::setVec(const F64 *vec)
{
mdV[0] = vec[0];
mdV[1] = vec[1];
mdV[2] = vec[2];
return (*this);
}
inline F64 LLVector3d::normVec(void)
{
F64 mag = (F32) sqrt(mdV[0]*mdV[0] + mdV[1]*mdV[1] + mdV[2]*mdV[2]);
F64 oomag;
if (mag > FP_MAG_THRESHOLD)
{
oomag = 1.f/mag;
mdV[0] *= oomag;
mdV[1] *= oomag;
mdV[2] *= oomag;
}
else
{
mdV[0] = 0.f;
mdV[1] = 0.f;
mdV[2] = 0.f;
mag = 0;
}
return (mag);
}
inline F64 LLVector3d::normalize(void)
{
F64 mag = (F32) sqrt(mdV[0]*mdV[0] + mdV[1]*mdV[1] + mdV[2]*mdV[2]);
F64 oomag;
if (mag > FP_MAG_THRESHOLD)
{
oomag = 1.f/mag;
mdV[0] *= oomag;
mdV[1] *= oomag;
mdV[2] *= oomag;
}
else
{
mdV[0] = 0.f;
mdV[1] = 0.f;
mdV[2] = 0.f;
mag = 0;
}
return (mag);
}
// LLVector3d Magnitude and Normalization Functions
inline F64 LLVector3d::magVec(void) const
{
return (F32) sqrt(mdV[0]*mdV[0] + mdV[1]*mdV[1] + mdV[2]*mdV[2]);
}
inline F64 LLVector3d::magVecSquared(void) const
{
return mdV[0]*mdV[0] + mdV[1]*mdV[1] + mdV[2]*mdV[2];
}
inline F64 LLVector3d::length(void) const
{
return (F32) sqrt(mdV[0]*mdV[0] + mdV[1]*mdV[1] + mdV[2]*mdV[2]);
}
inline F64 LLVector3d::lengthSquared(void) const
{
return mdV[0]*mdV[0] + mdV[1]*mdV[1] + mdV[2]*mdV[2];
}
inline LLVector3d operator+(const LLVector3d &a, const LLVector3d &b)
{
LLVector3d c(a);
return c += b;
}
inline LLVector3d operator-(const LLVector3d &a, const LLVector3d &b)
{
LLVector3d c(a);
return c -= b;
}
inline F64 operator*(const LLVector3d &a, const LLVector3d &b)
{
return (a.mdV[0]*b.mdV[0] + a.mdV[1]*b.mdV[1] + a.mdV[2]*b.mdV[2]);
}
inline LLVector3d operator%(const LLVector3d &a, const LLVector3d &b)
{
return LLVector3d( a.mdV[1]*b.mdV[2] - b.mdV[1]*a.mdV[2], a.mdV[2]*b.mdV[0] - b.mdV[2]*a.mdV[0], a.mdV[0]*b.mdV[1] - b.mdV[0]*a.mdV[1] );
}
inline LLVector3d operator/(const LLVector3d &a, const F64 k)
{
F64 t = 1.f / k;
return LLVector3d( a.mdV[0] * t, a.mdV[1] * t, a.mdV[2] * t );
}
inline LLVector3d operator*(const LLVector3d &a, const F64 k)
{
return LLVector3d( a.mdV[0] * k, a.mdV[1] * k, a.mdV[2] * k );
}
inline LLVector3d operator*(F64 k, const LLVector3d &a)
{
return LLVector3d( a.mdV[0] * k, a.mdV[1] * k, a.mdV[2] * k );
}
inline bool operator==(const LLVector3d &a, const LLVector3d &b)
{
return ( (a.mdV[0] == b.mdV[0])
&&(a.mdV[1] == b.mdV[1])
&&(a.mdV[2] == b.mdV[2]));
}
inline bool operator!=(const LLVector3d &a, const LLVector3d &b)
{
return ( (a.mdV[0] != b.mdV[0])
||(a.mdV[1] != b.mdV[1])
||(a.mdV[2] != b.mdV[2]));
}
inline const LLVector3d& operator+=(LLVector3d &a, const LLVector3d &b)
{
a.mdV[0] += b.mdV[0];
a.mdV[1] += b.mdV[1];
a.mdV[2] += b.mdV[2];
return a;
}
inline const LLVector3d& operator-=(LLVector3d &a, const LLVector3d &b)
{
a.mdV[0] -= b.mdV[0];
a.mdV[1] -= b.mdV[1];
a.mdV[2] -= b.mdV[2];
return a;
}
inline const LLVector3d& operator%=(LLVector3d &a, const LLVector3d &b)
{
LLVector3d ret( a.mdV[1]*b.mdV[2] - b.mdV[1]*a.mdV[2], a.mdV[2]*b.mdV[0] - b.mdV[2]*a.mdV[0], a.mdV[0]*b.mdV[1] - b.mdV[0]*a.mdV[1]);
a = ret;
return a;
}
inline const LLVector3d& operator*=(LLVector3d &a, const F64 k)
{
a.mdV[0] *= k;
a.mdV[1] *= k;
a.mdV[2] *= k;
return a;
}
inline const LLVector3d& operator/=(LLVector3d &a, const F64 k)
{
F64 t = 1.f / k;
a.mdV[0] *= t;
a.mdV[1] *= t;
a.mdV[2] *= t;
return a;
}
inline LLVector3d operator-(const LLVector3d &a)
{
return LLVector3d( -a.mdV[0], -a.mdV[1], -a.mdV[2] );
}
inline F64 dist_vec(const LLVector3d &a, const LLVector3d &b)
{
F64 x = a.mdV[0] - b.mdV[0];
F64 y = a.mdV[1] - b.mdV[1];
F64 z = a.mdV[2] - b.mdV[2];
return (F32) sqrt( x*x + y*y + z*z );
}
inline F64 dist_vec_squared(const LLVector3d &a, const LLVector3d &b)
{
F64 x = a.mdV[0] - b.mdV[0];
F64 y = a.mdV[1] - b.mdV[1];
F64 z = a.mdV[2] - b.mdV[2];
return x*x + y*y + z*z;
}
inline F64 dist_vec_squared2D(const LLVector3d &a, const LLVector3d &b)
{
F64 x = a.mdV[0] - b.mdV[0];
F64 y = a.mdV[1] - b.mdV[1];
return x*x + y*y;
}
inline LLVector3d lerp(const LLVector3d &a, const LLVector3d &b, const F64 u)
{
return LLVector3d(
a.mdV[VX] + (b.mdV[VX] - a.mdV[VX]) * u,
a.mdV[VY] + (b.mdV[VY] - a.mdV[VY]) * u,
a.mdV[VZ] + (b.mdV[VZ] - a.mdV[VZ]) * u);
}
inline BOOL LLVector3d::isNull() const
{
if ( F_APPROXIMATELY_ZERO > mdV[VX]*mdV[VX] + mdV[VY]*mdV[VY] + mdV[VZ]*mdV[VZ] )
{
return TRUE;
}
return FALSE;
}
inline F64 angle_between(const LLVector3d& a, const LLVector3d& b)
{
LLVector3d an = a;
LLVector3d bn = b;
an.normalize();
bn.normalize();
F64 cosine = an * bn;
F64 angle = (cosine >= 1.0f) ? 0.0f :
(cosine <= -1.0f) ? F_PI :
acos(cosine);
return angle;
}
inline BOOL are_parallel(const LLVector3d &a, const LLVector3d &b, const F64 epsilon)
{
LLVector3d an = a;
LLVector3d bn = b;
an.normalize();
bn.normalize();
F64 dot = an * bn;
if ( (1.0f - fabs(dot)) < epsilon)
{
return TRUE;
}
return FALSE;
}
inline LLVector3d projected_vec(const LLVector3d &a, const LLVector3d &b)
{
LLVector3d project_axis = b;
project_axis.normalize();
return project_axis * (a * project_axis);
}
#endif // LL_V3DMATH_H
|