aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/linden/indra/llmath/v3math.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'linden/indra/llmath/v3math.cpp')
-rw-r--r--linden/indra/llmath/v3math.cpp80
1 files changed, 77 insertions, 3 deletions
diff --git a/linden/indra/llmath/v3math.cpp b/linden/indra/llmath/v3math.cpp
index bbe460f..cb87836 100644
--- a/linden/indra/llmath/v3math.cpp
+++ b/linden/indra/llmath/v3math.cpp
@@ -34,6 +34,7 @@
34#include "v3math.h" 34#include "v3math.h"
35 35
36//#include "vmath.h" 36//#include "vmath.h"
37#include "v2math.h"
37#include "v4math.h" 38#include "v4math.h"
38#include "m4math.h" 39#include "m4math.h"
39#include "m3math.h" 40#include "m3math.h"
@@ -73,6 +74,72 @@ BOOL LLVector3::clamp(F32 min, F32 max)
73 return ret; 74 return ret;
74} 75}
75 76
77// Clamps length to an upper limit.
78// Returns TRUE if the data changed
79BOOL LLVector3::clampLength( F32 length_limit )
80{
81 BOOL changed = FALSE;
82
83 F32 len = length();
84 if (llfinite(len))
85 {
86 if ( len > length_limit)
87 {
88 normalize();
89 if (length_limit < 0.f)
90 {
91 length_limit = 0.f;
92 }
93 mV[0] *= length_limit;
94 mV[1] *= length_limit;
95 mV[2] *= length_limit;
96 changed = TRUE;
97 }
98 }
99 else
100 { // this vector may still be salvagable
101 F32 max_abs_component = 0.f;
102 for (S32 i = 0; i < 3; ++i)
103 {
104 F32 abs_component = fabs(mV[i]);
105 if (llfinite(abs_component))
106 {
107 if (abs_component > max_abs_component)
108 {
109 max_abs_component = abs_component;
110 }
111 }
112 else
113 {
114 // no it can't be salvaged --> clear it
115 clear();
116 changed = TRUE;
117 break;
118 }
119 }
120 if (!changed)
121 {
122 // yes it can be salvaged -->
123 // bring the components down before we normalize
124 mV[0] /= max_abs_component;
125 mV[1] /= max_abs_component;
126 mV[2] /= max_abs_component;
127 normalize();
128
129 if (length_limit < 0.f)
130 {
131 length_limit = 0.f;
132 }
133 mV[0] *= length_limit;
134 mV[1] *= length_limit;
135 mV[2] *= length_limit;
136 }
137 }
138
139 return changed;
140}
141
142
76// Sets all values to absolute value of their original values 143// Sets all values to absolute value of their original values
77// Returns TRUE if data changed 144// Returns TRUE if data changed
78BOOL LLVector3::abs() 145BOOL LLVector3::abs()
@@ -204,6 +271,13 @@ const LLVector3& LLVector3::setVec(const LLVector4 &vec)
204 return (*this); 271 return (*this);
205} 272}
206 273
274LLVector3::LLVector3(const LLVector2 &vec)
275{
276 mV[VX] = (F32)vec.mV[VX];
277 mV[VY] = (F32)vec.mV[VY];
278 mV[VZ] = 0;
279}
280
207LLVector3::LLVector3(const LLVector3d &vec) 281LLVector3::LLVector3(const LLVector3d &vec)
208{ 282{
209 mV[VX] = (F32)vec.mdV[VX]; 283 mV[VX] = (F32)vec.mdV[VX];
@@ -260,15 +334,15 @@ const LLVector3& operator*=(LLVector3 &a, const LLQuaternion &rot)
260} 334}
261 335
262// static 336// static
263BOOL LLVector3::parseVector3(const char* buf, LLVector3* value) 337BOOL LLVector3::parseVector3(const std::string& buf, LLVector3* value)
264{ 338{
265 if( buf == NULL || buf[0] == '\0' || value == NULL) 339 if( buf.empty() || value == NULL)
266 { 340 {
267 return FALSE; 341 return FALSE;
268 } 342 }
269 343
270 LLVector3 v; 344 LLVector3 v;
271 S32 count = sscanf( buf, "%f %f %f", v.mV + 0, v.mV + 1, v.mV + 2 ); 345 S32 count = sscanf( buf.c_str(), "%f %f %f", v.mV + 0, v.mV + 1, v.mV + 2 );
272 if( 3 == count ) 346 if( 3 == count )
273 { 347 {
274 value->setVec( v ); 348 value->setVec( v );