2 * OPCODE - Optimized Collision Detection
3 * http://www.codercorner.com/Opcode.htm
5 * Copyright (c) 2001-2008 Pierre Terdiman, pierre@codercorner.com
7 This software is provided 'as-is', without any express or implied warranty.
8 In no event will the authors be held liable for any damages arising from the use of this software.
9 Permission is granted to anyone to use this software for any purpose,
10 including commercial applications, and to alter it and redistribute it freely,
11 subject to the following restrictions:
13 1. The origin of this software must not be misrepresented; you must not claim that you wrote the original software. If you use this software in a product, an acknowledgment in the product documentation would be appreciated but is not required.
14 2. Altered source versions must be plainly marked as such, and must not be misrepresented as being the original software.
15 3. This notice may not be removed or altered from any source distribution.
18 ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
20 * Contains common classes & defs used in OPCODE.
22 * \author Pierre Terdiman
23 * \date March, 20, 2001
25 ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
27 ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
29 #ifndef __OPC_COMMON_H__
30 #define __OPC_COMMON_H__
32 // [GOTTFRIED]: Just a small change for readability.
33 #ifdef OPC_CPU_COMPARE
34 #define GREATER(x, y) AIR(x) > IR(y)
36 #define GREATER(x, y) fabsf(x) > (y)
39 class OPCODE_API CollisionAABB
43 inline_ CollisionAABB() {}
45 inline_ CollisionAABB(const AABB& b) { b.GetCenter(mCenter); b.GetExtents(mExtents); }
47 inline_ ~CollisionAABB() {}
49 //! Get min point of the box
50 inline_ void GetMin(Point& min) const { min = mCenter - mExtents; }
51 //! Get max point of the box
52 inline_ void GetMax(Point& max) const { max = mCenter + mExtents; }
54 //! Get component of the box's min point along a given axis
55 inline_ float GetMin(udword axis) const { return mCenter[axis] - mExtents[axis]; }
56 //! Get component of the box's max point along a given axis
57 inline_ float GetMax(udword axis) const { return mCenter[axis] + mExtents[axis]; }
59 ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
61 * Setups an AABB from min & max vectors.
62 * \param min [in] the min point
63 * \param max [in] the max point
65 ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
66 inline_ void SetMinMax(const Point& min, const Point& max) { mCenter = (max + min)*0.5f; mExtents = (max - min)*0.5f; }
68 ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
70 * Checks a box is inside another box.
71 * \param box [in] the other box
72 * \return true if current box is inside input box
74 ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
75 inline_ BOOL IsInside(const CollisionAABB& box) const
77 if(box.GetMin(0)>GetMin(0)) return FALSE;
78 if(box.GetMin(1)>GetMin(1)) return FALSE;
79 if(box.GetMin(2)>GetMin(2)) return FALSE;
80 if(box.GetMax(0)<GetMax(0)) return FALSE;
81 if(box.GetMax(1)<GetMax(1)) return FALSE;
82 if(box.GetMax(2)<GetMax(2)) return FALSE;
86 Point mCenter; //!< Box center
87 Point mExtents; //!< Box extents
90 class OPCODE_API QuantizedAABB
94 inline_ QuantizedAABB() {}
96 inline_ ~QuantizedAABB() {}
98 sword mCenter[3]; //!< Quantized center
99 uword mExtents[3]; //!< Quantized extents
102 //! Quickly rotates & translates a vector
103 inline_ void TransformPoint(Point& dest, const Point& source, const Matrix3x3& rot, const Point& trans)
105 dest.x = trans.x + source.x * rot.m[0][0] + source.y * rot.m[1][0] + source.z * rot.m[2][0];
106 dest.y = trans.y + source.x * rot.m[0][1] + source.y * rot.m[1][1] + source.z * rot.m[2][1];
107 dest.z = trans.z + source.x * rot.m[0][2] + source.y * rot.m[1][2] + source.z * rot.m[2][2];
110 #endif //__OPC_COMMON_H__