Skip to content

Commit c7ebde4

Browse files
authored
Merge pull request #3927 from Ansariel/develop-math-improvements
Add a bunch of old and new math improvements
2 parents 293462d + 4c6afbb commit c7ebde4

32 files changed

+1910
-2155
lines changed

indra/llappearance/llpolymorph.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -550,12 +550,12 @@ void LLPolyMorphTarget::apply( ESex avatar_sex )
550550

551551
mLastSex = avatar_sex;
552552

553-
// Check for NaN condition (NaN is detected if a variable doesn't equal itself.
554-
if (mCurWeight != mCurWeight)
553+
// Check for NaN condition
554+
if (llisnan(mCurWeight))
555555
{
556-
mCurWeight = 0.0;
556+
mCurWeight = 0.f;
557557
}
558-
if (mLastWeight != mLastWeight)
558+
if (llisnan(mLastWeight))
559559
{
560560
mLastWeight = mCurWeight+.001f;
561561
}

indra/llcommon/indra_constants.h

Lines changed: 204 additions & 204 deletions
Large diffs are not rendered by default.

indra/llcommon/lldefs.h

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -171,34 +171,34 @@ constexpr U32 MAXADDRSTR = 17; // 123.567.901.345 = 15 chars + \0 +
171171

172172
// recursion tail
173173
template <typename T>
174-
inline auto llmax(T data)
174+
constexpr auto llmax(T data)
175175
{
176176
return data;
177177
}
178178

179179
template <typename T0, typename T1, typename... Ts>
180-
inline auto llmax(T0 d0, T1 d1, Ts... rest)
180+
constexpr auto llmax(T0 d0, T1 d1, Ts... rest)
181181
{
182182
auto maxrest = llmax(d1, rest...);
183183
return (d0 > maxrest)? d0 : maxrest;
184184
}
185185

186186
// recursion tail
187187
template <typename T>
188-
inline auto llmin(T data)
188+
constexpr auto llmin(T data)
189189
{
190190
return data;
191191
}
192192

193193
template <typename T0, typename T1, typename... Ts>
194-
inline auto llmin(T0 d0, T1 d1, Ts... rest)
194+
constexpr auto llmin(T0 d0, T1 d1, Ts... rest)
195195
{
196196
auto minrest = llmin(d1, rest...);
197197
return (d0 < minrest) ? d0 : minrest;
198198
}
199199

200200
template <typename A, typename MIN, typename MAX>
201-
inline A llclamp(A a, MIN minval, MAX maxval)
201+
constexpr A llclamp(A a, MIN minval, MAX maxval)
202202
{
203203
A aminval{ static_cast<A>(minval) }, amaxval{ static_cast<A>(maxval) };
204204
if ( a < aminval )
@@ -213,13 +213,13 @@ inline A llclamp(A a, MIN minval, MAX maxval)
213213
}
214214

215215
template <class LLDATATYPE>
216-
inline LLDATATYPE llclampf(LLDATATYPE a)
216+
constexpr LLDATATYPE llclampf(LLDATATYPE a)
217217
{
218218
return llmin(llmax(a, LLDATATYPE(0)), LLDATATYPE(1));
219219
}
220220

221221
template <class LLDATATYPE>
222-
inline LLDATATYPE llclampb(LLDATATYPE a)
222+
constexpr LLDATATYPE llclampb(LLDATATYPE a)
223223
{
224224
return llmin(llmax(a, LLDATATYPE(0)), LLDATATYPE(255));
225225
}

indra/llcommon/stdtypes.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -164,14 +164,14 @@ class narrow
164164
FROM mValue;
165165

166166
public:
167-
narrow(FROM value): mValue(value) {}
167+
constexpr narrow(FROM value): mValue(value) {}
168168

169169
/*---------------------- Narrowing unsigned to signed ----------------------*/
170170
template <typename TO,
171171
typename std::enable_if<std::is_unsigned<FROM>::value &&
172172
std::is_signed<TO>::value,
173173
bool>::type = true>
174-
inline
174+
constexpr
175175
operator TO() const
176176
{
177177
// The reason we skip the
@@ -189,7 +189,7 @@ class narrow
189189
typename std::enable_if<! (std::is_unsigned<FROM>::value &&
190190
std::is_signed<TO>::value),
191191
bool>::type = true>
192-
inline
192+
constexpr
193193
operator TO() const
194194
{
195195
// two different assert()s so we can tell which condition failed

indra/llmath/llcamera.h

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -33,23 +33,23 @@
3333
#include "llplane.h"
3434
#include "llvector4a.h"
3535

36-
const F32 DEFAULT_FIELD_OF_VIEW = 60.f * DEG_TO_RAD;
37-
const F32 DEFAULT_ASPECT_RATIO = 640.f / 480.f;
38-
const F32 DEFAULT_NEAR_PLANE = 0.25f;
39-
const F32 DEFAULT_FAR_PLANE = 64.f; // far reaches across two horizontal, not diagonal, regions
36+
constexpr F32 DEFAULT_FIELD_OF_VIEW = 60.f * DEG_TO_RAD;
37+
constexpr F32 DEFAULT_ASPECT_RATIO = 640.f / 480.f;
38+
constexpr F32 DEFAULT_NEAR_PLANE = 0.25f;
39+
constexpr F32 DEFAULT_FAR_PLANE = 64.f; // far reaches across two horizontal, not diagonal, regions
4040

41-
const F32 MAX_ASPECT_RATIO = 50.0f;
42-
const F32 MAX_NEAR_PLANE = 1023.9f; // Clamp the near plane just before the skybox ends
43-
const F32 MAX_FAR_PLANE = 100000.0f; //1000000.0f; // Max allowed. Not good Z precision though.
44-
const F32 MAX_FAR_CLIP = 512.0f;
41+
constexpr F32 MAX_ASPECT_RATIO = 50.0f;
42+
constexpr F32 MAX_NEAR_PLANE = 1023.9f; // Clamp the near plane just before the skybox ends
43+
constexpr F32 MAX_FAR_PLANE = 100000.0f; //1000000.0f; // Max allowed. Not good Z precision though.
44+
constexpr F32 MAX_FAR_CLIP = 512.0f;
4545

46-
const F32 MIN_ASPECT_RATIO = 0.02f;
47-
const F32 MIN_NEAR_PLANE = 0.1f;
48-
const F32 MIN_FAR_PLANE = 0.2f;
46+
constexpr F32 MIN_ASPECT_RATIO = 0.02f;
47+
constexpr F32 MIN_NEAR_PLANE = 0.1f;
48+
constexpr F32 MIN_FAR_PLANE = 0.2f;
4949

5050
// Min/Max FOV values for square views. Call getMin/MaxView to get extremes based on current aspect ratio.
51-
static const F32 MIN_FIELD_OF_VIEW = 5.0f * DEG_TO_RAD;
52-
static const F32 MAX_FIELD_OF_VIEW = 175.f * DEG_TO_RAD;
51+
constexpr F32 MIN_FIELD_OF_VIEW = 5.0f * DEG_TO_RAD;
52+
constexpr F32 MAX_FIELD_OF_VIEW = 175.f * DEG_TO_RAD;
5353

5454
// An LLCamera is an LLCoorFrame with a view frustum.
5555
// This means that it has several methods for moving it around

indra/llmath/llcoordframe.cpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@
2626

2727
#include "linden_common.h"
2828

29-
//#include "vmath.h"
3029
#include "v3math.h"
3130
#include "m3math.h"
3231
#include "v4math.h"

indra/llmath/llcoordframe.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ class LLCoordFrame
6161
//LLCoordFrame(const F32 *origin, const F32 *rotation); // Assumes "origin" is 1x3 and "rotation" is 1x9 array
6262
//LLCoordFrame(const F32 *origin_and_rotation); // Assumes "origin_and_rotation" is 1x12 array
6363

64-
bool isFinite() { return mOrigin.isFinite() && mXAxis.isFinite() && mYAxis.isFinite() && mZAxis.isFinite(); }
64+
bool isFinite() const { return mOrigin.isFinite() && mXAxis.isFinite() && mYAxis.isFinite() && mZAxis.isFinite(); }
6565

6666
void reset();
6767
void resetAxes();

indra/llmath/llline.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@
3333
#include "stdtypes.h"
3434
#include "v3math.h"
3535

36-
const F32 DEFAULT_INTERSECTION_ERROR = 0.000001f;
36+
constexpr F32 DEFAULT_INTERSECTION_ERROR = 0.000001f;
3737

3838
class LLLine
3939
{

indra/llmath/llmath.h

Lines changed: 26 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -39,18 +39,8 @@
3939
// llcommon depend on llmath.
4040
#include "is_approx_equal_fraction.h"
4141

42-
// work around for Windows & older gcc non-standard function names.
43-
#if LL_WINDOWS
44-
#include <float.h>
45-
#define llisnan(val) _isnan(val)
46-
#define llfinite(val) _finite(val)
47-
#elif (LL_LINUX && __GNUC__ <= 2)
48-
#define llisnan(val) isnan(val)
49-
#define llfinite(val) isfinite(val)
50-
#else
51-
#define llisnan(val) std::isnan(val)
52-
#define llfinite(val) std::isfinite(val)
53-
#endif
42+
#define llisnan(val) std::isnan(val)
43+
#define llfinite(val) std::isfinite(val)
5444

5545
// Single Precision Floating Point Routines
5646
// (There used to be more defined here, but they appeared to be redundant and
@@ -89,7 +79,7 @@ constexpr F32 GIMBAL_THRESHOLD = 0.000436f; // sets the gimballock threshold 0
8979
constexpr F32 FP_MAG_THRESHOLD = 0.0000001f;
9080

9181
// TODO: Replace with logic like is_approx_equal
92-
inline bool is_approx_zero( F32 f ) { return (-F_APPROXIMATELY_ZERO < f) && (f < F_APPROXIMATELY_ZERO); }
82+
constexpr bool is_approx_zero(F32 f) { return (-F_APPROXIMATELY_ZERO < f) && (f < F_APPROXIMATELY_ZERO); }
9383

9484
// These functions work by interpreting sign+exp+mantissa as an unsigned
9585
// integer.
@@ -148,33 +138,17 @@ inline F64 llabs(const F64 a)
148138
return F64(std::fabs(a));
149139
}
150140

151-
inline S32 lltrunc( F32 f )
141+
constexpr S32 lltrunc(F32 f)
152142
{
153-
#if LL_WINDOWS && !defined( __INTEL_COMPILER ) && (ADDRESS_SIZE == 32)
154-
// Avoids changing the floating point control word.
155-
// Add or subtract 0.5 - epsilon and then round
156-
const static U32 zpfp[] = { 0xBEFFFFFF, 0x3EFFFFFF };
157-
S32 result;
158-
__asm {
159-
fld f
160-
mov eax, f
161-
shr eax, 29
162-
and eax, 4
163-
fadd dword ptr [zpfp + eax]
164-
fistp result
165-
}
166-
return result;
167-
#else
168-
return (S32)f;
169-
#endif
143+
return narrow(f);
170144
}
171145

172-
inline S32 lltrunc( F64 f )
146+
constexpr S32 lltrunc(F64 f)
173147
{
174-
return (S32)f;
148+
return narrow(f);
175149
}
176150

177-
inline S32 llfloor( F32 f )
151+
inline S32 llfloor(F32 f)
178152
{
179153
#if LL_WINDOWS && !defined( __INTEL_COMPILER ) && (ADDRESS_SIZE == 32)
180154
// Avoids changing the floating point control word.
@@ -284,7 +258,7 @@ constexpr F32 FAST_MAG_BETA = 0.397824734759f;
284258
//constexpr F32 FAST_MAG_ALPHA = 0.948059448969f;
285259
//constexpr F32 FAST_MAG_BETA = 0.392699081699f;
286260

287-
inline F32 fastMagnitude(F32 a, F32 b)
261+
constexpr F32 fastMagnitude(F32 a, F32 b)
288262
{
289263
a = (a > 0) ? a : -a;
290264
b = (b > 0) ? b : -b;
@@ -342,7 +316,7 @@ inline F32 llfastpow(const F32 x, const F32 y)
342316
}
343317

344318

345-
inline F32 snap_to_sig_figs(F32 foo, S32 sig_figs)
319+
constexpr F32 snap_to_sig_figs(F32 foo, S32 sig_figs)
346320
{
347321
// compute the power of ten
348322
F32 bar = 1.f;
@@ -358,30 +332,27 @@ inline F32 snap_to_sig_figs(F32 foo, S32 sig_figs)
358332
return new_foo;
359333
}
360334

361-
inline F32 lerp(F32 a, F32 b, F32 u)
362-
{
363-
return a + ((b - a) * u);
364-
}
335+
using std::lerp;
365336

366-
inline F32 lerp2d(F32 x00, F32 x01, F32 x10, F32 x11, F32 u, F32 v)
337+
constexpr F32 lerp2d(F32 x00, F32 x01, F32 x10, F32 x11, F32 u, F32 v)
367338
{
368339
F32 a = x00 + (x01-x00)*u;
369340
F32 b = x10 + (x11-x10)*u;
370341
F32 r = a + (b-a)*v;
371342
return r;
372343
}
373344

374-
inline F32 ramp(F32 x, F32 a, F32 b)
345+
constexpr F32 ramp(F32 x, F32 a, F32 b)
375346
{
376347
return (a == b) ? 0.0f : ((a - x) / (a - b));
377348
}
378349

379-
inline F32 rescale(F32 x, F32 x1, F32 x2, F32 y1, F32 y2)
350+
constexpr F32 rescale(F32 x, F32 x1, F32 x2, F32 y1, F32 y2)
380351
{
381352
return lerp(y1, y2, ramp(x, x1, x2));
382353
}
383354

384-
inline F32 clamp_rescale(F32 x, F32 x1, F32 x2, F32 y1, F32 y2)
355+
constexpr F32 clamp_rescale(F32 x, F32 x1, F32 x2, F32 y1, F32 y2)
385356
{
386357
if (y1 < y2)
387358
{
@@ -394,7 +365,7 @@ inline F32 clamp_rescale(F32 x, F32 x1, F32 x2, F32 y1, F32 y2)
394365
}
395366

396367

397-
inline F32 cubic_step( F32 x, F32 x0, F32 x1, F32 s0, F32 s1 )
368+
constexpr F32 cubic_step( F32 x, F32 x0, F32 x1, F32 s0, F32 s1 )
398369
{
399370
if (x <= x0)
400371
return s0;
@@ -407,14 +378,14 @@ inline F32 cubic_step( F32 x, F32 x0, F32 x1, F32 s0, F32 s1 )
407378
return s0 + (s1 - s0) * (f * f) * (3.0f - 2.0f * f);
408379
}
409380

410-
inline F32 cubic_step( F32 x )
381+
constexpr F32 cubic_step( F32 x )
411382
{
412383
x = llclampf(x);
413384

414385
return (x * x) * (3.0f - 2.0f * x);
415386
}
416387

417-
inline F32 quadratic_step( F32 x, F32 x0, F32 x1, F32 s0, F32 s1 )
388+
constexpr F32 quadratic_step( F32 x, F32 x0, F32 x1, F32 s0, F32 s1 )
418389
{
419390
if (x <= x0)
420391
return s0;
@@ -428,7 +399,7 @@ inline F32 quadratic_step( F32 x, F32 x0, F32 x1, F32 s0, F32 s1 )
428399
return (s0 * (1.f - f_squared)) + ((s1 - s0) * f_squared);
429400
}
430401

431-
inline F32 llsimple_angle(F32 angle)
402+
constexpr F32 llsimple_angle(F32 angle)
432403
{
433404
while(angle <= -F_PI)
434405
angle += F_TWO_PI;
@@ -438,7 +409,7 @@ inline F32 llsimple_angle(F32 angle)
438409
}
439410

440411
//SDK - Renamed this to get_lower_power_two, since this is what this actually does.
441-
inline U32 get_lower_power_two(U32 val, U32 max_power_two)
412+
constexpr U32 get_lower_power_two(U32 val, U32 max_power_two)
442413
{
443414
if(!max_power_two)
444415
{
@@ -460,7 +431,7 @@ inline U32 get_lower_power_two(U32 val, U32 max_power_two)
460431
// number of digits, then add one. We subtract 1 initially to handle
461432
// the case where the number passed in is actually a power of two.
462433
// WARNING: this only works with 32 bit ints.
463-
inline U32 get_next_power_two(U32 val, U32 max_power_two)
434+
constexpr U32 get_next_power_two(U32 val, U32 max_power_two)
464435
{
465436
if(!max_power_two)
466437
{
@@ -486,7 +457,7 @@ inline U32 get_next_power_two(U32 val, U32 max_power_two)
486457
//get the gaussian value given the linear distance from axis x and guassian value o
487458
inline F32 llgaussian(F32 x, F32 o)
488459
{
489-
return 1.f/(F_SQRT_TWO_PI*o)*powf(F_E, -(x*x)/(2*o*o));
460+
return 1.f/(F_SQRT_TWO_PI*o)*powf(F_E, -(x*x)/(2.f*o*o));
490461
}
491462

492463
//helper function for removing outliers
@@ -539,7 +510,8 @@ inline void ll_remove_outliers(std::vector<VEC_TYPE>& data, F32 k)
539510
// Note: in our code, values labeled as sRGB are ALWAYS gamma corrected linear values, NOT linear values with monitor gamma applied
540511
// Note: stored color values should always be gamma corrected linear (i.e. the values returned from an on-screen color swatch)
541512
// Note: DO NOT cache the conversion. This leads to error prone synchronization and is actually slower in the typical case due to cache misses
542-
inline float linearTosRGB(const float val) {
513+
inline float linearTosRGB(const float val)
514+
{
543515
if (val < 0.0031308f) {
544516
return val * 12.92f;
545517
}
@@ -554,7 +526,8 @@ inline float linearTosRGB(const float val) {
554526
// Note: Stored color values should generally be gamma corrected sRGB.
555527
// If you're serializing the return value of this function, you're probably doing it wrong.
556528
// Note: DO NOT cache the conversion. This leads to error prone synchronization and is actually slower in the typical case due to cache misses.
557-
inline float sRGBtoLinear(const float val) {
529+
inline float sRGBtoLinear(const float val)
530+
{
558531
if (val < 0.04045f) {
559532
return val / 12.92f;
560533
}

indra/llmath/llquaternion.cpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@
3030

3131
#include "llquaternion.h"
3232

33-
//#include "vmath.h"
3433
#include "v3math.h"
3534
#include "v3dmath.h"
3635
#include "v4math.h"

0 commit comments

Comments
 (0)