Skip to content

Commit e50536a

Browse files
committed
Added initializer types.
1 parent b45cb32 commit e50536a

File tree

3 files changed

+223
-22
lines changed

3 files changed

+223
-22
lines changed

include/bx/inline/math.inl

+139-19
Original file line numberDiff line numberDiff line change
@@ -286,9 +286,24 @@ namespace bx
286286
return _c - _a * _b;
287287
}
288288

289+
inline BX_CONSTEXPR_FUNC float add(float _a, float _b)
290+
{
291+
return _a + _b;
292+
}
293+
294+
inline BX_CONSTEXPR_FUNC float sub(float _a, float _b)
295+
{
296+
return _a - _b;
297+
}
298+
299+
inline BX_CONSTEXPR_FUNC float mul(float _a, float _b)
300+
{
301+
return _a * _b;
302+
}
303+
289304
inline BX_CONSTEXPR_FUNC float mad(float _a, float _b, float _c)
290305
{
291-
return _a * _b + _c;
306+
return add(mul(_a, _b), _c);
292307
}
293308

294309
inline BX_CONSTEXPR_FUNC float rcp(float _a)
@@ -383,7 +398,7 @@ namespace bx
383398
template<typename Ty>
384399
inline Ty load(const void* _ptr)
385400
{
386-
Ty result;
401+
Ty result(init::None);
387402
memCopy(&result, _ptr, sizeof(Ty) );
388403
return result;
389404
}
@@ -394,7 +409,21 @@ namespace bx
394409
memCopy(_ptr, &_a, sizeof(Ty) );
395410
}
396411

397-
inline Vec3::Vec3()
412+
inline Vec3::Vec3(init::NoneType)
413+
{
414+
}
415+
416+
constexpr Vec3::Vec3(init::ZeroType)
417+
: x(0.0f)
418+
, y(0.0f)
419+
, z(0.0f)
420+
{
421+
}
422+
423+
constexpr Vec3::Vec3(init::IdentityType)
424+
: x(0.0f)
425+
, y(0.0f)
426+
, z(0.0f)
398427
{
399428
}
400429

@@ -412,6 +441,57 @@ namespace bx
412441
{
413442
}
414443

444+
inline Plane::Plane(init::NoneType)
445+
: normal(init::None)
446+
{
447+
}
448+
449+
constexpr Plane::Plane(init::ZeroType)
450+
: normal(init::Zero)
451+
, dist(0.0f)
452+
{
453+
}
454+
455+
constexpr Plane::Plane(init::IdentityType)
456+
: normal(0.0f, 1.0f, 0.0f)
457+
, dist(0.0f)
458+
{
459+
}
460+
461+
constexpr Plane::Plane(Vec3 _normal, float _dist)
462+
: normal(_normal)
463+
, dist(_dist)
464+
{
465+
}
466+
467+
inline Quaternion::Quaternion(init::NoneType)
468+
{
469+
}
470+
471+
constexpr Quaternion::Quaternion(init::ZeroType)
472+
: x(0.0f)
473+
, y(0.0f)
474+
, z(0.0f)
475+
, w(0.0f)
476+
{
477+
}
478+
479+
constexpr Quaternion::Quaternion(init::IdentityType)
480+
: x(0.0f)
481+
, y(0.0f)
482+
, z(0.0f)
483+
, w(1.0f)
484+
{
485+
}
486+
487+
constexpr Quaternion::Quaternion(float _x, float _y, float _z, float _w)
488+
: x(_x)
489+
, y(_y)
490+
, z(_z)
491+
, w(_w)
492+
{
493+
}
494+
415495
inline BX_CONSTEXPR_FUNC Vec3 round(const Vec3 _a)
416496
{
417497
return
@@ -650,7 +730,7 @@ namespace bx
650730

651731
inline BX_CONST_FUNC Vec3 fromLatLong(float _u, float _v)
652732
{
653-
Vec3 result;
733+
Vec3 result(init::None);
654734
const float phi = _u * kPi2;
655735
const float theta = _v * kPi;
656736

@@ -705,6 +785,39 @@ namespace bx
705785
};
706786
}
707787

788+
inline BX_CONSTEXPR_FUNC Quaternion add(const Quaternion _a, const Quaternion _b)
789+
{
790+
return
791+
{
792+
_a.x + _b.x,
793+
_a.y + _b.y,
794+
_a.z + _b.z,
795+
_a.w + _b.w,
796+
};
797+
}
798+
799+
inline BX_CONSTEXPR_FUNC Quaternion sub(const Quaternion _a, const Quaternion _b)
800+
{
801+
return
802+
{
803+
_a.x - _b.x,
804+
_a.y - _b.y,
805+
_a.z - _b.z,
806+
_a.w - _b.w,
807+
};
808+
}
809+
810+
inline BX_CONSTEXPR_FUNC Quaternion mul(const Quaternion _a, float _b)
811+
{
812+
return
813+
{
814+
_a.x * _b,
815+
_a.y * _b,
816+
_a.z * _b,
817+
_a.w * _b,
818+
};
819+
}
820+
708821
inline BX_CONSTEXPR_FUNC Quaternion mul(const Quaternion _a, const Quaternion _b)
709822
{
710823
const float ax = _a.x;
@@ -751,15 +864,9 @@ namespace bx
751864
const float norm = dot(_a, _a);
752865
if (0.0f < norm)
753866
{
754-
const float invNorm = 1.0f / sqrt(norm);
755-
756-
return
757-
{
758-
_a.x * invNorm,
759-
_a.y * invNorm,
760-
_a.z * invNorm,
761-
_a.w * invNorm,
762-
};
867+
const float invNorm = rsqrt(norm);
868+
869+
return mul(_a, invNorm);
763870
}
764871

765872
return
@@ -771,6 +878,19 @@ namespace bx
771878
};
772879
}
773880

881+
inline BX_CONSTEXPR_FUNC Quaternion lerp(const Quaternion _a, const Quaternion _b, float _t)
882+
{
883+
const float sa = 1.0f - _t;
884+
const float adotb = dot(_a, _b);
885+
const float sb = sign(adotb) * _t;
886+
887+
const Quaternion aa = mul(_a, sa);
888+
const Quaternion bb = mul(_b, sb);
889+
const Quaternion qq = add(aa, bb);
890+
891+
return normalize(qq);
892+
}
893+
774894
inline BX_CONST_FUNC Vec3 toEuler(const Quaternion _a)
775895
{
776896
const float xx = _a.x;
@@ -872,8 +992,8 @@ namespace bx
872992

873993
inline void mtxFromNormal(float* _result, const Vec3& _normal, float _scale, const Vec3& _pos)
874994
{
875-
Vec3 tangent;
876-
Vec3 bitangent;
995+
Vec3 tangent(init::None);
996+
Vec3 bitangent(init::None);
877997
calcTangentFrame(tangent, bitangent, _normal);
878998

879999
store(&_result[ 0], mul(bitangent, _scale) );
@@ -891,8 +1011,8 @@ namespace bx
8911011

8921012
inline void mtxFromNormal(float* _result, const Vec3& _normal, float _scale, const Vec3& _pos, float _angle)
8931013
{
894-
Vec3 tangent;
895-
Vec3 bitangent;
1014+
Vec3 tangent(init::None);
1015+
Vec3 bitangent(init::None);
8961016
calcTangentFrame(tangent, bitangent, _normal, _angle);
8971017

8981018
store(&_result[0], mul(bitangent, _scale) );
@@ -969,7 +1089,7 @@ namespace bx
9691089

9701090
inline Vec3 mul(const Vec3& _vec, const float* _mat)
9711091
{
972-
Vec3 result;
1092+
Vec3 result(init::None);
9731093
result.x = _vec.x * _mat[0] + _vec.y * _mat[4] + _vec.z * _mat[ 8] + _mat[12];
9741094
result.y = _vec.x * _mat[1] + _vec.y * _mat[5] + _vec.z * _mat[ 9] + _mat[13];
9751095
result.z = _vec.x * _mat[2] + _vec.y * _mat[6] + _vec.z * _mat[10] + _mat[14];
@@ -978,7 +1098,7 @@ namespace bx
9781098

9791099
inline Vec3 mulXyz0(const Vec3& _vec, const float* _mat)
9801100
{
981-
Vec3 result;
1101+
Vec3 result(init::None);
9821102
result.x = _vec.x * _mat[0] + _vec.y * _mat[4] + _vec.z * _mat[ 8];
9831103
result.y = _vec.x * _mat[1] + _vec.y * _mat[5] + _vec.z * _mat[ 9];
9841104
result.z = _vec.x * _mat[2] + _vec.y * _mat[6] + _vec.z * _mat[10];

include/bx/math.h

+83-2
Original file line numberDiff line numberDiff line change
@@ -50,11 +50,38 @@ namespace bx
5050
};
5151
};
5252

53+
/// Structure initializer types.
54+
namespace init
55+
{
56+
/// Fields are left uninitialized.
57+
///
58+
struct NoneType {};
59+
constexpr NoneType None;
60+
61+
/// Fields are initialized to zero.
62+
///
63+
struct ZeroType {};
64+
constexpr ZeroType Zero;
65+
66+
/// Fields are initialized to identity value.
67+
///
68+
struct IdentityType {};
69+
constexpr IdentityType Identity;
70+
}
71+
5372
///
5473
struct Vec3
5574
{
75+
Vec3() = delete;
76+
77+
///
78+
Vec3(init::NoneType);
79+
80+
///
81+
constexpr Vec3(init::ZeroType);
82+
5683
///
57-
Vec3();
84+
constexpr Vec3(init::IdentityType);
5885

5986
///
6087
explicit constexpr Vec3(float _v);
@@ -68,13 +95,41 @@ namespace bx
6895
///
6996
struct Plane
7097
{
98+
Plane() = delete;
99+
100+
///
101+
Plane(init::NoneType);
102+
103+
///
104+
constexpr Plane(init::ZeroType);
105+
106+
///
107+
constexpr Plane(init::IdentityType);
108+
109+
///
110+
constexpr Plane(Vec3 _normal, float _dist);
111+
71112
Vec3 normal;
72113
float dist;
73114
};
74115

75116
///
76117
struct Quaternion
77118
{
119+
Quaternion() = delete;
120+
121+
///
122+
Quaternion(init::NoneType);
123+
124+
///
125+
constexpr Quaternion(init::ZeroType);
126+
127+
///
128+
constexpr Quaternion(init::IdentityType);
129+
130+
///
131+
constexpr Quaternion(float _x, float _y, float _z, float _w);
132+
78133
float x, y, z, w;
79134
};
80135

@@ -253,11 +308,24 @@ namespace bx
253308
///
254309
BX_CONSTEXPR_FUNC float nms(float _a, float _b, float _c);
255310

256-
/// Returns result of multipla and add (_a * _b + _c).
311+
/// Returns resul of addition (_a + _b).
312+
///
313+
BX_CONSTEXPR_FUNC float add(float _a, float _b);
314+
315+
/// Returns resul of subtracion (_a - _b).
316+
///
317+
BX_CONSTEXPR_FUNC float sub(float _a, float _b);
318+
319+
/// Returns result of multiply (_a * _b).
320+
///
321+
BX_CONSTEXPR_FUNC float mul(float _a, float _b);
322+
323+
/// Returns result of multiply and add (_a * _b + _c).
257324
///
258325
BX_CONSTEXPR_FUNC float mad(float _a, float _b, float _c);
259326

260327
/// Returns reciprocal of _a.
328+
///
261329
BX_CONSTEXPR_FUNC float rcp(float _a);
262330

263331
/// Returns the floating-point remainder of the division operation _a/_b.
@@ -376,6 +444,7 @@ namespace bx
376444
BX_CONSTEXPR_FUNC Vec3 max(const Vec3 _a, const Vec3 _b);
377445

378446
/// Returns component wise reciprocal of _a.
447+
///
379448
BX_CONSTEXPR_FUNC Vec3 rcp(const Vec3 _a);
380449

381450
///
@@ -396,6 +465,15 @@ namespace bx
396465
///
397466
BX_CONSTEXPR_FUNC Vec3 mulXyz(const Quaternion _a, const Quaternion _b);
398467

468+
///
469+
BX_CONSTEXPR_FUNC Quaternion add(const Quaternion _a, const Quaternion _b);
470+
471+
///
472+
BX_CONSTEXPR_FUNC Quaternion sub(const Quaternion _a, const Quaternion _b);
473+
474+
///
475+
BX_CONSTEXPR_FUNC Quaternion mul(const Quaternion _a, float _b);
476+
399477
///
400478
BX_CONSTEXPR_FUNC Quaternion mul(const Quaternion _a, const Quaternion _b);
401479

@@ -408,6 +486,9 @@ namespace bx
408486
///
409487
BX_CONSTEXPR_FUNC Quaternion normalize(const Quaternion _a);
410488

489+
///
490+
BX_CONSTEXPR_FUNC Quaternion lerp(const Quaternion _a, const Quaternion _b, float _t);
491+
411492
///
412493
BX_CONST_FUNC Vec3 toEuler(const Quaternion _a);
413494

tests/math_test.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -237,7 +237,7 @@ TEST_CASE("quaternion", "")
237237
float mtxQ[16];
238238
float mtx[16];
239239

240-
bx::Quaternion quat = { 0.0f, 0.0f, 0.0f, 1.0f };
240+
bx::Quaternion quat = bx::init::Identity;
241241
bx::mtxQuat(mtxQ, quat);
242242
bx::mtxIdentity(mtx);
243243
mtxCheck(mtxQ, mtx);

0 commit comments

Comments
 (0)