From 2443d31a461f2f237d65f2b4891ae0e158404983 Mon Sep 17 00:00:00 2001 From: Sam Hocevar Date: Sun, 26 Apr 2015 23:06:38 +0000 Subject: [PATCH] math: merge the box2 and box3 type declarations. We now have a box_t template class. Which means we can have 4-dimensional AABBs. Why? Because we can. --- src/lol/base/types.h | 3 +- src/lol/math/geometry.h | 158 ++++++++++++---------------------------- 2 files changed, 49 insertions(+), 112 deletions(-) diff --git a/src/lol/base/types.h b/src/lol/base/types.h index e02016a2..def8769d 100644 --- a/src/lol/base/types.h +++ b/src/lol/base/types.h @@ -27,7 +27,7 @@ typedef Real<16> real; class half; /* - * Forward declaration of vec_t, mat_t, cmplx_t, quat_t + * Forward declaration of vec_t, mat_t, cmplx_t, quat_t, etc. */ /* HACK: if this is declared int const, Visual Studio becomes unable @@ -35,6 +35,7 @@ class half; #define FULL_SWIZZLE (0) template struct vec_t; +template struct box_t; template struct mat_t; template struct cmplx_t; template struct quat_t; diff --git a/src/lol/math/geometry.h b/src/lol/math/geometry.h index 32484588..12aef6f6 100644 --- a/src/lol/math/geometry.h +++ b/src/lol/math/geometry.h @@ -67,176 +67,112 @@ protected: }; typedef SafeEnum Direction; -//LOL_BOX_TYPEDEFS ------------------------------------------------------------ -#define LOL_BOX_TYPEDEFS(tname, suffix) \ - template struct tname; \ - typedef tname suffix; \ - typedef tname d##suffix; \ - typedef tname i##suffix; \ - typedef tname u##suffix; +/* + * Generic box type names + */ -LOL_BOX_TYPEDEFS(Box2, box2) -LOL_BOX_TYPEDEFS(Box3, box3) +#define _T(tleft, tright, suffix) \ + typedef tleft float tright suffix; \ + typedef tleft double tright d##suffix; \ + typedef tleft int32_t tright i##suffix; \ + typedef tleft uint32_t tright u##suffix; -#undef LOL_BOX_TYPEDEFS +/* Idiotic hack to put "," inside a macro argument */ +#define _C , -/* - * 2D boxes - */ +_T(box_t<, _C 2>, box2) +_T(box_t<, _C 3>, box3) +_T(box_t<, _C 4>, box4) -template struct Box2 +#undef _C +#undef _T + +template +struct box_t { - inline Box2() - : aa(T(0)), - bb(T(0)) + inline box_t() + : aa(vec_t(T(0))), + bb(vec_t(T(0))) {} - inline Box2(vec_t const &a, vec_t const &b) + inline box_t(vec_t const &a, vec_t const &b) : aa(a), bb(b) {} - inline Box2(T const &ax, T const &ay, T const &bx, T const &by) + inline box_t(T const &ax, T const &ay, T const &bx, T const &by) : aa(ax, ay), bb(bx, by) {} - Box2 operator +(vec_t const &v) const - { - return Box2(aa + v, bb + v); - } - - Box2 &operator +=(vec_t const &v) - { - return *this = *this + v; - } - - Box2 operator -(vec_t const &v) const - { - return Box2(aa - v, bb - v); - } - - Box2 &operator -=(vec_t const &v) - { - return *this = *this - v; - } - - Box2 operator *(vec_t const &v) const - { - return Box2(aa * v, bb * v); - } - - Box2 &operator *=(vec_t const &v) - { - return *this = *this * v; - } - - Box2 operator *(T const &s) const - { - return Box2(aa * s, bb * s); - } - - Box2 &operator *=(T const &s) - { - return *this = *this * s; - } - - bool operator ==(Box2 const &box) const - { - return aa == box.aa && bb == box.bb; - } - - bool operator !=(Box2 const &box) const - { - return aa != box.aa || bb != box.bb; - } - - inline vec_t center() const { return (bb + aa) / 2; } - - inline vec_t extent() const { return bb - aa; } - - vec_t aa, bb; -}; - -/* - * 3D boxes - */ - -template struct Box3 -{ - inline Box3() - : aa(T(0)), - bb(T(0)) - {} - - inline Box3(vec_t const &a, vec_t const &b) - : aa(a), - bb(b) - {} - - inline Box3(T const &ax, T const &ay, T const &az, - T const &bx, T const &by, T const &bz) + inline box_t(T const &ax, T const &ay, T const &az, + T const &bx, T const &by, T const &bz) : aa(ax, ay, az), bb(bx, by, bz) {} - Box3 operator +(vec_t const &v) const + box_t operator +(vec_t const &v) const { - return Box3(aa + v, bb + v); + return box_t(aa + v, bb + v); } - Box3 &operator +=(vec_t const &v) + box_t &operator +=(vec_t const &v) { return *this = *this + v; } - Box3 operator -(vec_t const &v) const + box_t operator -(vec_t const &v) const { - return Box3(aa - v, bb - v); + return box_t(aa - v, bb - v); } - Box3 &operator -=(vec_t const &v) + box_t &operator -=(vec_t const &v) { return *this = *this - v; } - Box3 operator *(vec_t const &v) const + box_t operator *(vec_t const &v) const { - return Box3(aa * v, bb * v); + return box_t(aa * v, bb * v); } - Box3 &operator *=(vec_t const &v) + box_t &operator *=(vec_t const &v) { return *this = *this * v; } - Box3 operator *(T const &s) const + box_t operator *(T const &s) const { - return Box3(aa * s, bb * s); + return box_t(aa * s, bb * s); } - Box3 &operator *=(T const &s) + box_t &operator *=(T const &s) { return *this = *this * s; } - bool operator ==(Box3 const &box) const + bool operator ==(box_t const &box) const { return aa == box.aa && bb == box.bb; } - bool operator !=(Box3 const &box) const + bool operator !=(box_t const &box) const { return aa != box.aa || bb != box.bb; } - inline vec_t center() const { return (bb + aa) / 2; } + inline vec_t center() const { return (bb + aa) / 2; } - inline vec_t extent() const { return bb - aa; } + inline vec_t extent() const { return bb - aa; } - vec_t aa, bb; + vec_t aa, bb; }; +static_assert(sizeof(box2) == 16, "sizeof(box2) == 16"); +static_assert(sizeof(box3) == 24, "sizeof(box3) == 24"); +static_assert(sizeof(dbox2) == 32, "sizeof(dbox2) == 32"); +static_assert(sizeof(dbox3) == 48, "sizeof(dbox3) == 48"); + /* * Helper geometry functions */