aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/linden/indra/llmath/v3math.cpp
diff options
context:
space:
mode:
authorJacek Antonelli2008-08-15 23:44:46 -0500
committerJacek Antonelli2008-08-15 23:44:46 -0500
commit38d6d37f2d982fa959e9e8a4a3f7e1ccfad7b5d4 (patch)
treeadca584755d22ca041a2dbfc35d4eca01f70b32c /linden/indra/llmath/v3math.cpp
parentREADME.txt (diff)
downloadmeta-impy-38d6d37f2d982fa959e9e8a4a3f7e1ccfad7b5d4.zip
meta-impy-38d6d37f2d982fa959e9e8a4a3f7e1ccfad7b5d4.tar.gz
meta-impy-38d6d37f2d982fa959e9e8a4a3f7e1ccfad7b5d4.tar.bz2
meta-impy-38d6d37f2d982fa959e9e8a4a3f7e1ccfad7b5d4.tar.xz
Second Life viewer sources 1.13.2.12
Diffstat (limited to 'linden/indra/llmath/v3math.cpp')
-rw-r--r--linden/indra/llmath/v3math.cpp232
1 files changed, 232 insertions, 0 deletions
diff --git a/linden/indra/llmath/v3math.cpp b/linden/indra/llmath/v3math.cpp
new file mode 100644
index 0000000..39d3b70
--- /dev/null
+++ b/linden/indra/llmath/v3math.cpp
@@ -0,0 +1,232 @@
1/**
2 * @file v3math.cpp
3 * @brief LLVector3 class implementation.
4 *
5 * Copyright (c) 2000-2007, Linden Research, Inc.
6 *
7 * The source code in this file ("Source Code") is provided by Linden Lab
8 * to you under the terms of the GNU General Public License, version 2.0
9 * ("GPL"), unless you have obtained a separate licensing agreement
10 * ("Other License"), formally executed by you and Linden Lab. Terms of
11 * the GPL can be found in doc/GPL-license.txt in this distribution, or
12 * online at http://secondlife.com/developers/opensource/gplv2
13 *
14 * There are special exceptions to the terms and conditions of the GPL as
15 * it is applied to this Source Code. View the full text of the exception
16 * in the file doc/FLOSS-exception.txt in this software distribution, or
17 * online at http://secondlife.com/developers/opensource/flossexception
18 *
19 * By copying, modifying or distributing this software, you acknowledge
20 * that you have read and understood your obligations described above,
21 * and agree to abide by those obligations.
22 *
23 * ALL LINDEN LAB SOURCE CODE IS PROVIDED "AS IS." LINDEN LAB MAKES NO
24 * WARRANTIES, EXPRESS, IMPLIED OR OTHERWISE, REGARDING ITS ACCURACY,
25 * COMPLETENESS OR PERFORMANCE.
26 */
27
28#include "linden_common.h"
29
30#include "v3math.h"
31
32//#include "vmath.h"
33#include "v4math.h"
34#include "m4math.h"
35#include "m3math.h"
36#include "llquaternion.h"
37#include "llquantize.h"
38#include "v3dmath.h"
39
40// LLVector3
41// WARNING: Don't use these for global const definitions!
42// For example:
43// const LLQuaternion(0.5f * F_PI, LLVector3::zero);
44// at the top of a *.cpp file might not give you what you think.
45const LLVector3 LLVector3::zero(0,0,0);
46const LLVector3 LLVector3::x_axis(1.f, 0, 0);
47const LLVector3 LLVector3::y_axis(0, 1.f, 0);
48const LLVector3 LLVector3::z_axis(0, 0, 1.f);
49const LLVector3 LLVector3::x_axis_neg(-1.f, 0, 0);
50const LLVector3 LLVector3::y_axis_neg(0, -1.f, 0);
51const LLVector3 LLVector3::z_axis_neg(0, 0, -1.f);
52const LLVector3 LLVector3::all_one(1.f,1.f,1.f);
53
54
55// Clamps each values to range (min,max).
56// Returns TRUE if data changed.
57BOOL LLVector3::clamp(F32 min, F32 max)
58{
59 BOOL ret = FALSE;
60
61 if (mV[0] < min) { mV[0] = min; ret = TRUE; }
62 if (mV[1] < min) { mV[1] = min; ret = TRUE; }
63 if (mV[2] < min) { mV[2] = min; ret = TRUE; }
64
65 if (mV[0] > max) { mV[0] = max; ret = TRUE; }
66 if (mV[1] > max) { mV[1] = max; ret = TRUE; }
67 if (mV[2] > max) { mV[2] = max; ret = TRUE; }
68
69 return ret;
70}
71
72// Sets all values to absolute value of their original values
73// Returns TRUE if data changed
74BOOL LLVector3::abs()
75{
76 BOOL ret = FALSE;
77
78 if (mV[0] < 0.f) { mV[0] = -mV[0]; ret = TRUE; }
79 if (mV[1] < 0.f) { mV[1] = -mV[1]; ret = TRUE; }
80 if (mV[2] < 0.f) { mV[2] = -mV[2]; ret = TRUE; }
81
82 return ret;
83}
84
85// Quatizations
86void LLVector3::quantize16(F32 lowerxy, F32 upperxy, F32 lowerz, F32 upperz)
87{
88 F32 x = mV[VX];
89 F32 y = mV[VY];
90 F32 z = mV[VZ];
91
92 x = U16_to_F32(F32_to_U16(x, lowerxy, upperxy), lowerxy, upperxy);
93 y = U16_to_F32(F32_to_U16(y, lowerxy, upperxy), lowerxy, upperxy);
94 z = U16_to_F32(F32_to_U16(z, lowerz, upperz), lowerz, upperz);
95
96 mV[VX] = x;
97 mV[VY] = y;
98 mV[VZ] = z;
99}
100
101void LLVector3::quantize8(F32 lowerxy, F32 upperxy, F32 lowerz, F32 upperz)
102{
103 mV[VX] = U8_to_F32(F32_to_U8(mV[VX], lowerxy, upperxy), lowerxy, upperxy);;
104 mV[VY] = U8_to_F32(F32_to_U8(mV[VY], lowerxy, upperxy), lowerxy, upperxy);
105 mV[VZ] = U8_to_F32(F32_to_U8(mV[VZ], lowerz, upperz), lowerz, upperz);
106}
107
108
109void LLVector3::snap(S32 sig_digits)
110{
111 mV[VX] = snap_to_sig_figs(mV[VX], sig_digits);
112 mV[VY] = snap_to_sig_figs(mV[VY], sig_digits);
113 mV[VZ] = snap_to_sig_figs(mV[VZ], sig_digits);
114}
115
116
117std::ostream& operator<<(std::ostream& s, const LLVector3 &a)
118{
119 s << "{ " << a.mV[VX] << ", " << a.mV[VY] << ", " << a.mV[VZ] << " }";
120 return s;
121}
122
123
124const LLVector3& LLVector3::rotVec(const LLMatrix3 &mat)
125{
126 *this = *this * mat;
127 return *this;
128}
129
130const LLVector3& LLVector3::rotVec(const LLQuaternion &q)
131{
132 *this = *this * q;
133 return *this;
134}
135
136const LLVector3& LLVector3::rotVec(F32 angle, const LLVector3 &vec)
137{
138 if ( !vec.isExactlyZero() && angle )
139 {
140 *this = *this * LLMatrix3(angle, vec);
141 }
142 return *this;
143}
144
145const LLVector3& LLVector3::rotVec(F32 angle, F32 x, F32 y, F32 z)
146{
147 LLVector3 vec(x, y, z);
148 if ( !vec.isExactlyZero() && angle )
149 {
150 *this = *this * LLMatrix3(angle, vec);
151 }
152 return *this;
153}
154
155const LLVector3& LLVector3::scaleVec(const LLVector3& vec)
156{
157 mV[VX] *= vec.mV[VX];
158 mV[VY] *= vec.mV[VY];
159 mV[VZ] *= vec.mV[VZ];
160
161 return *this;
162}
163
164LLVector3 LLVector3::scaledVec(const LLVector3& vec) const
165{
166 LLVector3 ret = LLVector3(*this);
167 ret.scaleVec(vec);
168 return ret;
169}
170
171const LLVector3& LLVector3::setVec(const LLVector3d &vec)
172{
173 mV[0] = (F32)vec.mdV[0];
174 mV[1] = (F32)vec.mdV[1];
175 mV[2] = (F32)vec.mdV[2];
176 return (*this);
177}
178
179const LLVector3& LLVector3::setVec(const LLVector4 &vec)
180{
181 mV[0] = vec.mV[0];
182 mV[1] = vec.mV[1];
183 mV[2] = vec.mV[2];
184 return (*this);
185}
186
187LLVector3::LLVector3(const LLVector3d &vec)
188{
189 mV[VX] = (F32)vec.mdV[VX];
190 mV[VY] = (F32)vec.mdV[VY];
191 mV[VZ] = (F32)vec.mdV[VZ];
192}
193
194LLVector3::LLVector3(const LLVector4 &vec)
195{
196 mV[VX] = (F32)vec.mV[VX];
197 mV[VY] = (F32)vec.mV[VY];
198 mV[VZ] = (F32)vec.mV[VZ];
199}
200
201const LLVector3& operator*=(LLVector3 &a, const LLQuaternion &rot)
202{
203 const F32 rw = - rot.mQ[VX] * a.mV[VX] - rot.mQ[VY] * a.mV[VY] - rot.mQ[VZ] * a.mV[VZ];
204 const F32 rx = rot.mQ[VW] * a.mV[VX] + rot.mQ[VY] * a.mV[VZ] - rot.mQ[VZ] * a.mV[VY];
205 const F32 ry = rot.mQ[VW] * a.mV[VY] + rot.mQ[VZ] * a.mV[VX] - rot.mQ[VX] * a.mV[VZ];
206 const F32 rz = rot.mQ[VW] * a.mV[VZ] + rot.mQ[VX] * a.mV[VY] - rot.mQ[VY] * a.mV[VX];
207
208 a.mV[VX] = - rw * rot.mQ[VX] + rx * rot.mQ[VW] - ry * rot.mQ[VZ] + rz * rot.mQ[VY];
209 a.mV[VY] = - rw * rot.mQ[VY] + ry * rot.mQ[VW] - rz * rot.mQ[VX] + rx * rot.mQ[VZ];
210 a.mV[VZ] = - rw * rot.mQ[VZ] + rz * rot.mQ[VW] - rx * rot.mQ[VY] + ry * rot.mQ[VX];
211
212 return a;
213}
214
215// static
216BOOL LLVector3::parseVector3(const char* buf, LLVector3* value)
217{
218 if( buf == NULL || buf[0] == '\0' || value == NULL)
219 {
220 return FALSE;
221 }
222
223 LLVector3 v;
224 S32 count = sscanf( buf, "%f %f %f", v.mV + 0, v.mV + 1, v.mV + 2 );
225 if( 3 == count )
226 {
227 value->setVec( v );
228 return TRUE;
229 }
230
231 return FALSE;
232}