diff options
Diffstat (limited to 'MatchBloxEngine/MatchBloxEngine/typedefs.h')
-rw-r--r-- | MatchBloxEngine/MatchBloxEngine/typedefs.h | 90 |
1 files changed, 59 insertions, 31 deletions
diff --git a/MatchBloxEngine/MatchBloxEngine/typedefs.h b/MatchBloxEngine/MatchBloxEngine/typedefs.h index 0262c14..0b9190c 100644 --- a/MatchBloxEngine/MatchBloxEngine/typedefs.h +++ b/MatchBloxEngine/MatchBloxEngine/typedefs.h @@ -5,22 +5,6 @@ #include <vector> #include <limits> -typedef struct bb -{ - bb() { - //init a bounding box with the min values set to max double and visa versa - m_dLeft = m_dBottom = m_dFront = std::numeric_limits<double>::max(); - m_dRight = m_dTop = m_dBack = -std::numeric_limits<double>::max(); - } - - double m_dLeft, //min x - m_dBottom, //min y - m_dFront, //min z - m_dRight, //max x - m_dTop, //max y - m_dBack; //max z - -} BoundingBox_t; typedef struct vec3d { @@ -55,23 +39,67 @@ typedef struct vec3d { return vec3d(*this) *= scal; } } Vect3D_t; +typedef struct bb +{ + Vect3D_t m_Min, //minimal corner vertex (-x, -y, -z) + m_Max; //maximal corner vertex (+x, +y, +z) + //default constructor creates a maximum inverted bounding box + bb() + { + //init a bounding box with the min values set to max double and visa versa + double l_doubleMax = std::numeric_limits<double>::max(); + m_Min = Vect3D_t(l_doubleMax, l_doubleMax, l_doubleMax); + m_Max = Vect3D_t(-l_doubleMax, -l_doubleMax, -l_doubleMax); + } + + //copy constructor + bb(const bb &b) : m_Min(b.m_Min), m_Max(b.m_Max) {} + + //translating a bounding box + inline bb& operator+=(const Vect3D_t &v) + { m_Min += v; m_Max += v; return *this; } + inline const bb& operator+(const Vect3D_t &v) + { return (bb(*this) += v); } + + //update the bounding box min/max coords with vertex v + inline bb& operator<<(const Vect3D_t &v) + { + if (v.x < m_Min.x) m_Min.x = v.x; + else if (v.x > m_Max.x) m_Max.x = v.x; + if (v.y < m_Min.y) m_Min.y = v.y; + else if (v.y > m_Max.y) m_Max.y = v.y; + if (v.z < m_Min.z) m_Min.z = v.z; + else if (v.z > m_Max.z) m_Max.z = v.z; + return *this; + } + + inline bool Contains(const Vect3D_t &v) + { + //check if the point is contained in the box + return m_Min.x < v.x && v.x < m_Max.x && + m_Min.y < v.y && v.y < m_Max.y && + m_Min.z < v.z && v.z < m_Max.z; + } + + inline bool Contains(const bb& b) + { + return Contains(b.m_Min) && Contains(b.m_Max); + } + + inline bool Overlap(const bb& rhs) + { + //check if the boxes overlap in all three axis + return ( ( m_Min.x < rhs.m_Min.x && rhs.m_Min.x < rhs.m_Max.x) || + ( m_Min.x < rhs.m_Max.x && rhs.m_Max.x < rhs.m_Max.x) ) && + ( ( m_Min.y < rhs.m_Min.y && rhs.m_Min.y < rhs.m_Max.y) || + ( m_Min.y < rhs.m_Max.y && rhs.m_Max.y < rhs.m_Max.y) ) && + ( ( m_Min.z < rhs.m_Min.z && rhs.m_Min.z < rhs.m_Max.z) || + ( m_Min.z < rhs.m_Max.z && rhs.m_Max.z < rhs.m_Max.z) ); + } + +} BoundingBox_t; -inline void UpdBBox(BoundingBox_t &B, Vect3D_t &V) -{ - if (V.x < B.m_dLeft) - B.m_dLeft = V.x; - else if (V.x > B.m_dRight) - B.m_dRight = V.x; - if (V.y < B.m_dBottom) - B.m_dBottom = V.y; - else if (V.y > B.m_dTop) - B.m_dTop = V.y; - if (V.z < B.m_dFront) - B.m_dFront = V.z; - else if (V.z > B.m_dBack) - B.m_dBack = V.z; -}; typedef struct triangle { |