sl@0: /* sl@0: * Copyright (c) 2003 Nokia Corporation and/or its subsidiary(-ies). sl@0: * All rights reserved. sl@0: * This component and the accompanying materials are made available sl@0: * under the terms of the License "Eclipse Public License v1.0" sl@0: * which accompanies this distribution, and is available sl@0: * at the URL "http://www.eclipse.org/legal/epl-v10.html". sl@0: * sl@0: * Initial Contributors: sl@0: * Nokia Corporation - initial contribution. sl@0: * sl@0: * Contributors: sl@0: * sl@0: * Description: Vector and matrix math functions and data types sl@0: * sl@0: */ sl@0: sl@0: #ifndef __M3G_MATH_H__ sl@0: #define __M3G_MATH_H__ sl@0: sl@0: /*! sl@0: * \file sl@0: * \brief Vector and matrix math functions and data types sl@0: */ sl@0: sl@0: /*---------------------------------------------------------------------- sl@0: * Internal data types sl@0: *--------------------------------------------------------------------*/ sl@0: sl@0: /*! sl@0: * \internal sl@0: * \brief Axis-aligned bounding box sl@0: */ sl@0: typedef struct sl@0: { sl@0: M3Gfloat min[3], max[3]; sl@0: /* sl@0: M3Gbyte min[3], minExp; sl@0: M3Gbyte max[3], maxExp; sl@0: */ sl@0: } AABB; sl@0: sl@0: /*---------------------------------------------------------------------- sl@0: * Global constants sl@0: *--------------------------------------------------------------------*/ sl@0: sl@0: /*! sl@0: * \internal sl@0: * \brief Maximum positive float value sl@0: */ sl@0: #define M3G_MAX_POSITIVE_FLOAT (3.402e+38f) sl@0: sl@0: /*! sl@0: * \internal sl@0: * \brief Minimum negative float value sl@0: */ sl@0: #define M3G_MIN_NEGATIVE_FLOAT (-3.402e+38f) sl@0: sl@0: /*! sl@0: * \internal sl@0: * \brief Degrees to radians multiplier sl@0: */ sl@0: #define M3G_DEG2RAD (0.017453292519943295769236907684886f) sl@0: sl@0: #define EPSILON (1.0e-5f) sl@0: #define EPSILON_EXP (-17) sl@0: #define RAD2DEG (57.295779513082320876798154814105f) sl@0: #define PI (3.14159265359f) sl@0: #define HALF_PI (PI / 2.0f) sl@0: #define ONE_AND_HALF_PI (PI + HALF_PI) sl@0: #define TWO_PI (2.f * PI) sl@0: sl@0: /*! \internal \brief Extracts the bit pattern of a floating point number */ sl@0: #define FLOAT_AS_UINT(x) (*(M3Guint*)&(x)) sl@0: sl@0: /*! \internal \brief Returns an integer bit pattern as float */ sl@0: #define INT_AS_FLOAT(x) (*(M3Gfloat*)&(x)) sl@0: sl@0: /* IEEE floating point format */ sl@0: #define MANTISSA_MASK 0x007FFFFFu sl@0: #define EXP_MASK 0x7F800000u sl@0: #define SIGN_MASK 0x80000000u sl@0: sl@0: #define M3G_FLOAT_ONE 0x3F800000 sl@0: sl@0: /*! \internal \brief Extracts the exponent of a floating point number */ sl@0: #define EXPONENT(x) (((M3Gint)(FLOAT_AS_UINT(x) & EXP_MASK) >> 23) - 127) sl@0: sl@0: /*! \internal \brief Extracts the mantissa of a floating point number */ sl@0: #define MANTISSA(x) (FLOAT_AS_UINT(x) & MANTISSA_MASK) sl@0: sl@0: /*! \internal \brief Extracts the sign of a floating point number */ sl@0: #define SIGN(x) (1 - ((FLOAT_AS_UINT(x) & SIGN_MASK) >> 30)) sl@0: sl@0: /*! \internal \brief Extracts just the sign bit of a floating point number */ sl@0: #define SIGN_BIT(x) (FLOAT_AS_UINT(x) >> 31) sl@0: sl@0: /* Useful constants */ sl@0: #define LEADING_ONE (1 << 23) sl@0: sl@0: /*! \internal \brief Checks the sign of a floating point number */ sl@0: #define IS_NEGATIVE(x) ((FLOAT_AS_UINT(x) & SIGN_MASK) != 0) sl@0: sl@0: /* Floating-point constant identification macros */ sl@0: # define IS_ZERO(x) ((FLOAT_AS_UINT(x) & ~SIGN_MASK) <= 0x01000000) sl@0: # define IS_ONE(x) (((x) > 1.0f - EPSILON) && ((x) < 1.0f + EPSILON)) sl@0: # define IS_MINUS_ONE(x) (((x) > -1.0f - EPSILON) && ((x) < -1.0f + EPSILON)) sl@0: sl@0: /* Elementary vectors */ sl@0: static const Vec4 Vec4_X_AXIS = {1, 0, 0, 0}; sl@0: static const Vec4 Vec4_Y_AXIS = {0, 1, 0, 0}; sl@0: static const Vec4 Vec4_Z_AXIS = {0, 0, 1, 0}; sl@0: static const Vec4 Vec4_ORIGIN = {0, 0, 0, 1}; sl@0: sl@0: /*---------------------------------------------------------------------- sl@0: * Elementary floating-point math sl@0: *--------------------------------------------------------------------*/ sl@0: sl@0: #if defined(M3G_SOFT_FLOAT) sl@0: static M3Gfloat m3gAdd(const M3Gfloat a, const M3Gfloat b); sl@0: static M3Gfloat m3gMul(const M3Gfloat a, const M3Gfloat b); sl@0: static M3Gfloat m3gRcpSqrt(const M3Gfloat x); sl@0: static M3Gfloat m3gSqrt(const M3Gfloat x); sl@0: static M3G_INLINE M3Gfloat m3gAbs(const M3Gfloat a) sl@0: { sl@0: M3Guint temp = FLOAT_AS_UINT(a) & ~SIGN_MASK; sl@0: return INT_AS_FLOAT(temp); sl@0: } sl@0: static M3G_INLINE M3Gfloat m3gDiv(const M3Gfloat a, const M3Gfloat b) sl@0: { sl@0: return (a / b); sl@0: } sl@0: static M3G_INLINE M3Gfloat m3gDivif(const M3Gint a, const M3Gint b) sl@0: { sl@0: return m3gDiv((M3Gfloat) a, (M3Gfloat) b); sl@0: } sl@0: static M3G_INLINE M3Gfloat m3gMadd(const M3Gfloat a, const M3Gfloat b, const M3Gfloat c) sl@0: { sl@0: return m3gAdd(m3gMul(a, b), c); sl@0: } sl@0: static M3G_INLINE M3Gfloat m3gRcp(const M3Gfloat x) sl@0: { sl@0: return (1.0f / x); sl@0: } sl@0: static M3G_INLINE M3Gfloat m3gSub(const M3Gfloat a, const M3Gfloat b) sl@0: { sl@0: M3Guint bNeg = FLOAT_AS_UINT(b) ^ SIGN_MASK; sl@0: return m3gAdd(a, INT_AS_FLOAT(bNeg)); sl@0: } sl@0: #else sl@0: # include sl@0: # define m3gAbs(a) ((float)fabs(a)) sl@0: # define m3gAdd(a, b) ((float)(a) + (float)(b)) sl@0: # define m3gMadd(a, b, c) ((float)(a) * (float)(b) + (float)(c)) sl@0: # define m3gMul(a, b) ((float)(a) * (float)(b)) sl@0: # define m3gDiv(a, b) ((float)(a) / (float)(b)) sl@0: # define m3gDivif(a, b) ((float)(a) / (float)(b)) sl@0: # define m3gRcp(x) (1.0f / (float)(x)) sl@0: # define m3gRcpSqrt(x) (1.0f / (float)sqrt(x)) sl@0: # define m3gSqrt(x) ((float)sqrt(x)) sl@0: # define m3gSub(a, b) ((float)(a) - (float)(b)) sl@0: #endif /* M3G_SOFT_FLOAT */ sl@0: sl@0: /*---------------------------------------------------------------------- sl@0: * Trigonometric and exp functions sl@0: *--------------------------------------------------------------------*/ sl@0: sl@0: #if defined(M3G_SOFT_FLOAT) sl@0: static M3Gfloat m3gArcCos(const M3Gfloat x); sl@0: static M3Gfloat m3gArcTan(const M3Gfloat y, const M3Gfloat x); sl@0: static M3Gfloat m3gCos(const M3Gfloat x); sl@0: static M3Gfloat m3gSin(const M3Gfloat x); sl@0: static M3Gfloat m3gTan(const M3Gfloat x); sl@0: static M3Gfloat m3gExp(const M3Gfloat a); sl@0: #else sl@0: # define m3gArcCos(x) ((float)acos(x)) sl@0: # define m3gArcTan(y, x) ((float)atan2((y), (x))) sl@0: # define m3gCos(x) ((float)cos(x)) sl@0: # define m3gSin(x) ((float)sin(x)) sl@0: # define m3gTan(x) ((float)tan(x)) sl@0: # define m3gExp(x) ((float)exp(x)) sl@0: #endif sl@0: sl@0: /*---------------------------------------------------------------------- sl@0: * Matrix and quaternion stuff sl@0: *--------------------------------------------------------------------*/ sl@0: sl@0: static M3Gbool m3gIsWUnity (const Matrix *mtx); sl@0: sl@0: static void m3gExpQuat (Quat *quat, const Vec3 *qExp); sl@0: static void m3gLogQuat (Vec3 *qLog, const Quat *quat); sl@0: static void m3gLogDiffQuat (Vec3 *logDiff, sl@0: const Quat *from, const Quat *to); sl@0: static M3Gint m3gGetFixedPoint3x3Basis(const Matrix *mtx, M3Gshort *elem); sl@0: static M3Gint m3gGetFixedPointTranslation(const Matrix *mtx, M3Gshort *elem); sl@0: sl@0: /*---------------------------------------------------------------------- sl@0: * Bounding boxes sl@0: *--------------------------------------------------------------------*/ sl@0: sl@0: static void m3gFitAABB(AABB *box, const AABB *a, const AABB *b); sl@0: static void m3gTransformAABB(AABB *box, const Matrix *mtx); sl@0: #if defined(M3G_DEBUG) sl@0: static void m3gValidateAABB(const AABB *aabb); sl@0: #else sl@0: # define m3gValidateAABB(a) sl@0: #endif sl@0: sl@0: /*---------------------------------------------------------------------- sl@0: * Rounding and conversion sl@0: *--------------------------------------------------------------------*/ sl@0: sl@0: static M3Gint m3gRoundToInt(const M3Gfloat a); sl@0: sl@0: static M3Guint m3gAlpha1f(M3Gfloat a); sl@0: /*static M3Guint m3gColor1f(M3Gfloat i);*/ sl@0: static M3Guint m3gColor3f(M3Gfloat r, M3Gfloat g, M3Gfloat b); sl@0: static M3Guint m3gColor4f(M3Gfloat r, M3Gfloat g, M3Gfloat b, M3Gfloat a); sl@0: static void m3gFloatColor(M3Gint argb, M3Gfloat intensity, M3Gfloat *rgba); sl@0: sl@0: static M3Gbool m3gIntersectTriangle(const Vec3 *orig, const Vec3 *dir, sl@0: const Vec3 *vert0, const Vec3 *vert1, const Vec3 *vert2, sl@0: Vec3 *tuv, M3Gint cullMode); sl@0: static M3Gbool m3gIntersectBox(const Vec3 *orig, const Vec3 *dir, const AABB *box); sl@0: static M3Gbool m3gIntersectRectangle(M3GRectangle *dst, M3GRectangle *r1, M3GRectangle *r2); sl@0: sl@0: /*---------------------------------------------------------------------- sl@0: * Inline functions sl@0: *--------------------------------------------------------------------*/ sl@0: sl@0: /*! sl@0: * \internal sl@0: * \brief Multiplies a floating point number by 0.5. sl@0: * sl@0: * \param x the number to multiply sl@0: * \return 0.5 * \c x sl@0: */ sl@0: static M3G_INLINE M3Gfloat m3gHalf(M3Gfloat x) sl@0: { sl@0: M3Guint bits = FLOAT_AS_UINT(x); sl@0: M3Guint mask = 0xff; sl@0: M3Gint exponent = bits & (mask << 23); sl@0: bits ^= exponent; sl@0: exponent = exponent - (1 << 23); sl@0: if (exponent > 0) bits |= exponent; sl@0: return INT_AS_FLOAT(bits); sl@0: } sl@0: sl@0: /*! sl@0: * \internal sl@0: * \brief Multiplies a floating point number by two sl@0: * sl@0: * This does NOT handle overflows. sl@0: * sl@0: * \param x the number to multiply sl@0: * \return 2 * \c x sl@0: */ sl@0: static M3G_INLINE M3Gfloat m3gDouble(M3Gfloat x) sl@0: { sl@0: M3Guint bits = FLOAT_AS_UINT(x) + (1 << 23); sl@0: return INT_AS_FLOAT(bits); sl@0: } sl@0: sl@0: /*! sl@0: * \internal sl@0: * \brief Computes the square of a floating point number sl@0: * sl@0: * \param x the input number sl@0: * \return x * x sl@0: */ sl@0: static M3G_INLINE M3Gfloat m3gSquare(M3Gfloat x) sl@0: { sl@0: return m3gMul(x, x); sl@0: } sl@0: sl@0: /*! sl@0: * \internal sl@0: * \brief Negates a floating-point value sl@0: */ sl@0: static M3G_INLINE M3Gfloat m3gNegate(M3Gfloat x) sl@0: { sl@0: M3Guint ix = FLOAT_AS_UINT(x) ^ SIGN_MASK; sl@0: return INT_AS_FLOAT(ix); sl@0: } sl@0: sl@0: #endif /*__M3G_MATH_H__*/