OSDN Git Service

add tstools.
[rec10/rec10-git.git] / tstools / DtsEdit / src / gpac / math.h
diff --git a/tstools/DtsEdit/src/gpac/math.h b/tstools/DtsEdit/src/gpac/math.h
new file mode 100644 (file)
index 0000000..27d4cd4
--- /dev/null
@@ -0,0 +1,1048 @@
+/*\r
+ *                     GPAC - Multimedia Framework C SDK\r
+ *\r
+ *                     Copyright (c) Jean Le Feuvre 2000-2005 \r
+ *                                     All rights reserved\r
+ *\r
+ *  This file is part of GPAC / common tools sub-project\r
+ *\r
+ *  GPAC is free software; you can redistribute it and/or modify\r
+ *  it under the terms of the GNU Lesser General Public License as published by\r
+ *  the Free Software Foundation; either version 2, or (at your option)\r
+ *  any later version.\r
+ *   \r
+ *  GPAC is distributed in the hope that it will be useful,\r
+ *  but WITHOUT ANY WARRANTY; without even the implied warranty of\r
+ *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\r
+ *  GNU Lesser General Public License for more details.\r
+ *   \r
+ *  You should have received a copy of the GNU Lesser General Public\r
+ *  License along with this library; see the file COPYING.  If not, write to\r
+ *  the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. \r
+ *\r
+ */\r
+\r
+#ifndef _GF_MATH_H_\r
+#define _GF_MATH_H_\r
+\r
+#ifdef __cplusplus\r
+extern "C" {\r
+#endif\r
+\r
+/*!\r
+ *     \file <gpac/math.h>\r
+ *     \brief math and trigo functions.\r
+ */\r
+\r
+#include <gpac/setup.h>\r
+       \r
+/*NOTE: there is a conflict on Win32 VC6 with C++ and gpac headers when including <math.h>*/\r
+#if !defined(__cplusplus) || defined(__SYMBIAN32__)\r
+#include <math.h>\r
+#endif\r
+\r
+/*!\r
+ \cond DUMMY_DOXY_SECTION\r
+*/\r
+\r
+#ifndef GPAC_FIXED_POINT\r
+/*note: \r
+               to turn fp on, change to GPAC_FIXED_POINT\r
+               to turn fp off, change to GPAC_NO_FIXED_POINT\r
+       this is needed by configure+sed to modify this file directly\r
+*/\r
+#define GPAC_NO_FIXED_POINT\r
+#endif\r
+\r
+/*!\r
+ \endcond\r
+*/\r
+\r
+\r
+/*!\r
+ *\addtogroup math_grp math\r
+ *\ingroup utils_grp\r
+ *\brief Mathematics and Trigonometric functions\r
+ *\r
+ *This section documents the math and trigo functions used in the GPAC framework. GPAC can be compiled with\r
+ *fixed-point support, representing float values on a 16.16 signed integer, which implies a developer \r
+ *must take care of float computations when using GPAC.\n\r
+ *A developper should not need to know in which mode the framework has been compiled as long as he uses\r
+ *the math functions of GPAC which work in both float and fixed-point mode.\n\r
+ *Using fixed-point version is decided at compilation time and cannot be changed. The feature is signaled\r
+ *through the following macros:\r
+ *- GPAC_FIXED_POINT: when defined, GPAC has been compiled in fixed-point mode\r
+ *- GPAC_NO_FIXED_POINT: when defined, GPAC has been compiled in regular (float) mode\r
+ *     @{\r
+ */\r
+\r
+\r
+/*****************************************************************************************\r
+                       FIXED-POINT SUPPORT - HARDCODED FOR 16.16 representation\r
+       the software rasterizer also use a 16.16 representation even in non-fixed version\r
+******************************************************************************************/\r
+\r
+#ifdef GPAC_FIXED_POINT\r
+\r
+/*!\r
+ *Fixed 16.16 number\r
+ *\hideinitializer\r
+ \note This documentation has been generated for a fixed-point version of the GPAC framework.\r
+ */\r
+typedef s32 Fixed;\r
+#define FIX_ONE                        0x10000L\r
+#define INT2FIX(v)             ((Fixed)( ((s32) (v) ) << 16))\r
+#define FLT2FIX(v)             ((Fixed) ((v) * FIX_ONE))\r
+#define FIX2INT(v)             ((s32)(((v)+((FIX_ONE>>1)))>>16))\r
+#define FIX2FLT(v)             ((Float)( ((Float)(v)) / ((Float) FIX_ONE)))\r
+#define FIX_EPSILON            2\r
+#define FIX_MAX                        0x7FFFFFFF\r
+#define FIX_MIN                        -FIX_MAX\r
+#define GF_PI2         102944\r
+#define GF_PI          205887\r
+#define GF_2PI         411774\r
+\r
+/*!\return 1/a, expressed as fixed number*/\r
+Fixed gf_invfix(Fixed a);\r
+/*!\return a*b, expressed as fixed number*/\r
+Fixed gf_mulfix(Fixed a, Fixed b);\r
+/*!\return a*b/c, expressed as fixed number*/\r
+Fixed gf_muldiv(Fixed a, Fixed b, Fixed c);\r
+/*!\return a/b, expressed as fixed number*/\r
+Fixed gf_divfix(Fixed a, Fixed b);\r
+/*!\return sqrt(a), expressed as fixed number*/\r
+Fixed gf_sqrt(Fixed x);\r
+/*!\return ceil(a), expressed as fixed number*/\r
+Fixed gf_ceil(Fixed a);\r
+/*!\return floor(a), expressed as fixed number*/\r
+Fixed gf_floor(Fixed a);\r
+/*!\return cos(a), expressed as fixed number*/\r
+Fixed gf_cos(Fixed angle);\r
+/*!\return sin(a), expressed as fixed number*/\r
+Fixed gf_sin(Fixed angle);\r
+/*!\return tan(a), expressed as fixed number*/\r
+Fixed gf_tan(Fixed angle);\r
+/*!\return acos(a), expressed as fixed number*/\r
+Fixed gf_acos(Fixed angle);\r
+/*!\return asin(a), expressed as fixed number*/\r
+Fixed gf_asin(Fixed angle);\r
+/*!\return atan(y, x), expressed as fixed number*/\r
+Fixed gf_atan2(Fixed y, Fixed x);\r
+\r
+#else\r
+\r
+\r
+/*!Fixed is 32bit float number\r
+ \note This documentation has been generated for a float version of the GPAC framework.\r
+*/\r
+typedef Float Fixed;\r
+#define FIX_ONE                        1.0f\r
+#define INT2FIX(v)             ((Float) (v))\r
+#define FLT2FIX(v)             ((Float) (v))\r
+#define FIX2INT(v)             ((s32)(v))\r
+#define FIX2FLT(v)             ((Float) (v))\r
+#define FIX_EPSILON            GF_EPSILON_FLOAT\r
+#define FIX_MAX                        GF_MAX_FLOAT\r
+#define FIX_MIN                        -GF_MAX_FLOAT\r
+#define GF_PI2         1.5707963267949f\r
+#define GF_PI          3.1415926535898f\r
+#define GF_2PI         6.2831853071796f\r
+\r
+/*!\hideinitializer 1/_a, expressed as fixed number*/\r
+#define gf_invfix(_a)  (FIX_ONE/(_a))\r
+/*!\hideinitializer _a*_b, expressed as fixed number*/\r
+#define gf_mulfix(_a, _b)              ((_a)*(_b))\r
+/*!\hideinitializer _a*_b/_c, expressed as fixed number*/\r
+#define gf_muldiv(_a, _b, _c)  ((_c) ? (_a)*(_b)/(_c) : GF_MAX_FLOAT)\r
+/*!\hideinitializer _a/_b, expressed as fixed number*/\r
+#define gf_divfix(_a, _b)              ((_b) ? (_a)/(_b) : GF_MAX_FLOAT)\r
+/*!\hideinitializer sqrt(_a), expressed as fixed number*/\r
+#define gf_sqrt(_a) ((Float) sqrt(_a))\r
+/*!\hideinitializer ceil(_a), expressed as fixed number*/\r
+#define gf_ceil(_a) ((Float) ceil(_a))\r
+/*!\hideinitializer floor(_a), expressed as fixed number*/\r
+#define gf_floor(_a) ((Float) floor(_a))\r
+/*!\hideinitializer cos(_a), expressed as fixed number*/\r
+#define gf_cos(_a) ((Float) cos(_a))\r
+/*!\hideinitializer sin(_a), expressed as fixed number*/\r
+#define gf_sin(_a) ((Float) sin(_a))\r
+/*!\hideinitializer tan(_a), expressed as fixed number*/\r
+#define gf_tan(_a) ((Float) tan(_a))\r
+/*!\hideinitializer atan2(_y,_x), expressed as fixed number*/\r
+#define gf_atan2(_y, _x) ((Float) atan2(_y, _x))\r
+/*!\hideinitializer acos(_a), expressed as fixed number*/\r
+#define gf_acos(_a) ((Float) acos(_a))\r
+/*!\hideinitializer asin(_a), expressed as fixed number*/\r
+#define gf_asin(_a) ((Float) asin(_a))\r
+\r
+#endif\r
+\r
+/*!\def FIX_ONE\r
+ \hideinitializer\r
+ Fixed unit value\r
+*/\r
+/*!\def INT2FIX(v)\r
+ \hideinitializer\r
+ Conversion from integer to fixed\r
+*/\r
+/*!\def FLT2FIX(v)\r
+ \hideinitializer\r
+ Conversion from float to fixed\r
+*/\r
+/*!\def FIX2INT(v)\r
+ \hideinitializer\r
+ Conversion from fixed to integer\r
+*/\r
+/*!\def FIX2FLT(v)\r
+ \hideinitializer\r
+ Conversion from fixed to float\r
+*/\r
+/*!\def FIX_EPSILON\r
+ \hideinitializer\r
+ Epsilon Fixed (positive value closest to 0)\r
+*/\r
+/*!\def FIX_MAX\r
+ \hideinitializer\r
+ Maximum Fixed (maximum representable fixed value)\r
+*/\r
+/*!\def FIX_MIN\r
+ \hideinitializer\r
+ Minimum Fixed (minimum representable fixed value)\r
+*/\r
+/*!\def GF_PI2\r
+ \hideinitializer\r
+ PI/2 expressed as Fixed\r
+*/\r
+/*!\def GF_PI\r
+ \hideinitializer\r
+ PI expressed as Fixed\r
+*/\r
+/*!\def GF_2PI\r
+ \hideinitializer\r
+ 2*PI expressed as Fixed\r
+*/\r
+\r
+Fixed gf_angle_diff(Fixed a, Fixed b);\r
+\r
+/*!\r
+ *     \brief Field bit-size \r
+ *\r
+ *     Gets the number of bits needed to represent the value.\r
+ *     \param MaxVal Maximum value to be represented.\r
+ *     \return number of bits required to represent the value.\r
+ */\r
+u32 gf_get_bit_size(u32 MaxVal);\r
+\r
+/*!\r
+ *     \brief Get power of 2\r
+ *\r
+ *     Gets the closest power of 2 greater or equal to the value.\r
+ *     \param val value to be used.\r
+ *     \return requested power of 2.\r
+ */\r
+u32 gf_get_next_pow2(u32 val);\r
+\r
+/*!\r
+ *\addtogroup math2d_grp math2d\r
+ *\ingroup math_grp\r
+ *\brief 2D Mathematics functions\r
+ *\r
+ *This section documents mathematic tools for 2D geometry and color matrices operations\r
+ *     @{\r
+ */\r
+\r
+/*!\brief 2D point\r
+ *\r
+ *The 2D point object is used in all the GPAC framework for both point and vector representation.\r
+*/\r
+typedef struct __vec2f\r
+{\r
+       Fixed x;\r
+       Fixed y;\r
+} GF_Point2D;\r
+/*!\r
+ *\brief get 2D vector length\r
+ *\r
+ *Gets the length of a 2D vector\r
+ *\return length of the vector\r
+ */\r
+Fixed gf_v2d_len(GF_Point2D *vec);\r
+/*!\r
+ *\brief 2D vector from polar coordinates\r
+ *\r
+ *Constructs a 2D vector from its polar coordinates\r
+ *\param length the length of the vector\r
+ *\param angle the angle of the vector in radians\r
+ *\return the 2D vector\r
+ */\r
+GF_Point2D gf_v2d_from_polar(Fixed length, Fixed angle);\r
+\r
+/*!\brief rectangle 2D\r
+ *\r
+ *The 2D rectangle used in the GPAC project.\r
+ */\r
+typedef struct\r
+{\r
+       /*!the left coordinate of the rectangle*/\r
+       Fixed x;\r
+       /*!the top coordinate of the rectangle, regardless of the canvas orientation. In other words, y is always the \r
+       greatest coordinate value,      even if the rectangle is presented bottom-up. This insures proper rectangles testing*/\r
+       Fixed y;\r
+       /*!the width of the rectangle. Width must be greater than or equal to 0*/\r
+       Fixed width;\r
+       /*!the height of the rectangle. Height must be greater than or equal to 0*/\r
+       Fixed height;\r
+} GF_Rect;\r
+\r
+/*!\r
+ \brief rectangle union\r
+ *\r
+ *Gets the union of two rectangles.\r
+ *\param rc1 first rectangle of the union. Upon return, this rectangle will contain the result of the union\r
+ *\param rc2 second rectangle of the union\r
+*/\r
+void gf_rect_union(GF_Rect *rc1, GF_Rect *rc2);\r
+/*!\r
+ \brief centers a rectangle\r
+ *\r
+ *Builds a rectangle centered on the origin\r
+ *\param w width of the rectangle\r
+ *\param h height of the rectangle\r
+ *\return centered rectangle object\r
+*/\r
+GF_Rect gf_rect_center(Fixed w, Fixed h);\r
+/*!\r
+ \brief rectangle overlap test\r
+ *\r
+ *Tests if two rectangles overlap.\r
+ *\param rc1 first rectangle to test\r
+ *\param rc2 second rectangle to test\r
+ *\return 1 if rectangles overlap, 0 otherwise\r
+*/\r
+Bool gf_rect_overlaps(GF_Rect rc1, GF_Rect rc2);\r
+/*!\r
+ \brief rectangle identity test\r
+ *\r
+ *Tests if two rectangles are identical.\r
+ *\param rc1 first rectangle to test\r
+ *\param rc2 second rectangle to test\r
+ *\return 1 if rectangles are identical, 0 otherwise\r
+*/\r
+Bool gf_rect_equal(GF_Rect rc1, GF_Rect rc2);\r
+\r
+/*!\r
+ *\brief pixel-aligned rectangle\r
+ *\r
+ *Pixel-aligned rectangle used in the GPAC framework. This is usually needed for 2D drawing algorithms.\r
+ */\r
+typedef struct\r
+{\r
+       /*!the left coordinate of the rectangle*/\r
+       s32 x;\r
+       /*!the top coordinate of the rectangle, regardless of the canvas orientation. In other words, y is always the \r
+       greatest coordinate value, even if the rectangle is presented bottom-up. This insures proper rectangles operations*/\r
+       s32 y;\r
+       /*!the width of the rectangle. Width must be greater than or equal to 0*/\r
+       s32 width;\r
+       /*!the height of the rectangle. Height must be greater than or equal to 0*/\r
+       s32 height;\r
+} GF_IRect;\r
+/*!\r
+ *\brief gets the pixelized version of a rectangle\r
+ *\r
+ *Returns the smallest pixel-aligned rectangle completely containing a rectangle\r
+ *\param r the rectangle to transform\r
+ *\return the pixel-aligned transformed rectangle\r
+*/\r
+GF_IRect gf_rect_pixelize(GF_Rect *r);\r
+\r
+\r
+/*!\r
+ *\brief 2D matrix\r
+ *\r
+ *The 2D affine matrix object usied in GPAC. The transformation of P(x,y) in P'(X, Y) is:\r
+ \code\r
+       X = m[0]*x + m[1]*y + m[2];\r
+       Y = m[3]*x + m[4]*y + m[5];\r
+ \endcode\r
+*/\r
+typedef struct\r
+{\r
+       Fixed m[6];\r
+} GF_Matrix2D;\r
+\r
+/*!\brief matrix initialization\r
+ *\hideinitializer\r
+ *\r
+ *Inits the matrix to the identity matrix\r
+*/\r
+#define gf_mx2d_init(_obj) { memset((_obj).m, 0, sizeof(Fixed)*6); (_obj).m[0] = (_obj).m[4] = FIX_ONE; }\r
+/*!\brief matrix copy\r
+ *\hideinitializer\r
+ *\r
+ *Copies the matrix _from to the matrix _obj\r
+*/\r
+#define gf_mx2d_copy(_obj, from) memcpy((_obj).m, (from).m, sizeof(Fixed)*6)\r
+/*!\brief matrix identity testing\r
+ *\hideinitializer\r
+ *\r
+ *This macro evaluates to 1 if the matrix _obj is the identity matrix, 0 otherwise\r
+*/\r
+#define gf_mx2d_is_identity(_obj) ((!(_obj).m[1] && !(_obj).m[2] && !(_obj).m[3] && !(_obj).m[5] && ((_obj).m[0]==FIX_ONE) && ((_obj).m[4]==FIX_ONE)) ? 1 : 0)\r
+\r
+/*!\brief 2D matrix multiplication\r
+ *\r
+ *Multiplies two 2D matrices from*_this\r
+ *\param _this matrix being transformed. Once the function is called, _this contains the result matrix\r
+ *\param from transformation matrix to add\r
+*/\r
+void gf_mx2d_add_matrix(GF_Matrix2D *_this, GF_Matrix2D *from);\r
+\r
+/*!\brief 2D matrix pre-multiplication\r
+ *\r
+ *Multiplies two 2D matrices _this*from\r
+ *\param _this matrix being transformed. Once the function is called, _this contains the result matrix\r
+ *\param from transformation matrix to add\r
+*/\r
+void gf_mx2d_pre_multiply(GF_Matrix2D *_this, GF_Matrix2D *from);\r
+\r
+/*!\brief matrix translating\r
+ *\r
+ *Translates a 2D matrix\r
+ *\param _this matrix being transformed. Once the function is called, _this contains the result matrix\r
+ *\param cx horizontal translation\r
+ *\param cy vertical translation\r
+*/\r
+void gf_mx2d_add_translation(GF_Matrix2D *_this, Fixed cx, Fixed cy);\r
+/*!\brief matrix rotating\r
+ *\r
+ *Rotates a 2D matrix\r
+ *\param _this matrix being transformed. Once the function is called, _this contains the result matrix\r
+ *\param cx horizontal rotation center coordinate\r
+ *\param cy vertical rotation center coordinate\r
+ *\param angle rotation angle in radians\r
+*/\r
+void gf_mx2d_add_rotation(GF_Matrix2D *_this, Fixed cx, Fixed cy, Fixed angle);\r
+/*!\brief matrix scaling\r
+ *\r
+ *Scales a 2D matrix\r
+ *\param _this matrix being transformed. Once the function is called, _this contains the result matrix\r
+ *\param scale_x horizontal scaling factor\r
+ *\param scale_y vertical scaling factor\r
+*/\r
+void gf_mx2d_add_scale(GF_Matrix2D *_this, Fixed scale_x, Fixed scale_y);\r
+/*!\brief matrix uncentered scaling\r
+ *\r
+ *Scales a 2D matrix with a non-centered scale\r
+ *\param _this matrix being transformed. Once the function is called, _this contains the result matrix\r
+ *\param scale_x horizontal scaling factor\r
+ *\param scale_y vertical scaling factor\r
+ *\param cx horizontal scaling center coordinate\r
+ *\param cy vertical scaling center coordinate\r
+ *\param angle scale orienttion angle in radians\r
+*/\r
+void gf_mx2d_add_scale_at(GF_Matrix2D *_this, Fixed scale_x, Fixed scale_y, Fixed cx, Fixed cy, Fixed angle);\r
+/*!\brief matrix skewing\r
+ *\r
+ *Skews a 2D matrix\r
+ *\param _this matrix being transformed. Once the function is called, _this contains the result matrix\r
+ *\param skew_x horizontal skew factor\r
+ *\param skew_y vertical skew factor\r
+*/\r
+void gf_mx2d_add_skew(GF_Matrix2D *_this, Fixed skew_x, Fixed skew_y);\r
+/*!\brief matrix horizontal skewing\r
+ *\r
+ *Skews a 2D matrix horizontally by a given angle\r
+ *\param _this matrix being transformed. Once the function is called, _this contains the result matrix\r
+ *\param angle horizontal skew angle in radians\r
+*/\r
+void gf_mx2d_add_skew_x(GF_Matrix2D *_this, Fixed angle);\r
+/*!\brief matrix vertical skewing\r
+ *\r
+ *Skews a 2D matrix vertically by a given angle\r
+ *\param _this matrix being transformed. Once the function is called, _this contains the result matrix\r
+ *\param angle vertical skew angle in radians\r
+*/\r
+void gf_mx2d_add_skew_y(GF_Matrix2D *_this, Fixed angle);\r
+/*!\brief matrix inversing\r
+ *\r
+ *Inverses a 2D matrix \r
+ *\param _this matrix being transformed. Once the function is called, _this contains the result matrix\r
+*/\r
+void gf_mx2d_inverse(GF_Matrix2D *_this);\r
+/*!\brief matrix coordinate transformation\r
+ *\r
+ *Applies a 2D matrix transformation to coordinates\r
+ *\param _this transformation matrix\r
+ *\param x pointer to horizontal coordinate. Once the function is called, x contains the transformed horizontal coordinate\r
+ *\param y pointer to vertical coordinate. Once the function is called, y contains the transformed vertical coordinate\r
+*/\r
+void gf_mx2d_apply_coords(GF_Matrix2D *_this, Fixed *x, Fixed *y);\r
+/*!\brief matrix point transformation\r
+ *\r
+ *Applies a 2D matrix transformation to a 2D point\r
+ *\param _this transformation matrix\r
+ *\param pt pointer to 2D point. Once the function is called, pt contains the transformed point\r
+*/\r
+void gf_mx2d_apply_point(GF_Matrix2D *_this, GF_Point2D *pt);\r
+/*!\brief matrix rectangle transformation\r
+ *\r
+ *Applies a 2D matrix transformation to a rectangle, giving the enclosing rectangle of the transformed one\r
+ *\param _this transformation matrix\r
+ *\param rc pointer to rectangle. Once the function is called, rc contains the transformed rectangle\r
+*/\r
+void gf_mx2d_apply_rect(GF_Matrix2D *_this, GF_Rect *rc);\r
+\r
+/*!\brief matrix decomposition\r
+ *\r
+ *Decomposes a 2D matrix M as M=Scale x Rotation x Translation if possible\r
+ *\param _this transformation matrix\r
+ *\param scale resulting scale part\r
+ *\param rotate resulting rotation part\r
+ *\param translate resulting translation part\r
+ *\return 0 if matrix cannot be decomposed, 1 otherwise\r
+*/\r
+Bool gf_mx2d_decompose(GF_Matrix2D *_this, GF_Point2D *scale, Fixed *rotate, GF_Point2D *translate);\r
+\r
+/*! @} */\r
+\r
+\r
+/*!\r
+ *\addtogroup math3d_grp math3d\r
+ *\ingroup math_grp\r
+ *\brief 3D Mathematics functions\r
+ *\r
+ *This section documents mathematic tools for 3D geometry operations\r
+ *     @{\r
+ */\r
+\r
+/*!\brief 3D point or vector\r
+ *\r
+ *The 3D point object is used in all the GPAC framework for both point and vector representation.\r
+*/\r
+typedef struct __vec3f\r
+{\r
+       Fixed x;\r
+       Fixed y;\r
+       Fixed z;\r
+} GF_Vec;\r
+\r
+/*base vector operations are MACROs for faster access*/\r
+/*!\hideinitializer macro evaluating to 1 if vectors are equal, 0 otherwise*/\r
+#define gf_vec_equal(v1, v2) (((v1).x == (v2).x) && ((v1).y == (v2).y) && ((v1).z == (v2).z))\r
+/*!\hideinitializer macro reversing a vector v = v*/\r
+#define gf_vec_rev(v) { (v).x = -(v).x; (v).y = -(v).y; (v).z = -(v).z; }\r
+/*!\hideinitializer macro performing the minus operation res = v1 - v2*/\r
+#define gf_vec_diff(res, v1, v2) { (res).x = (v1).x - (v2).x; (res).y = (v1).y - (v2).y; (res).z = (v1).z - (v2).z; }\r
+/*!\hideinitializer macro performing the add operation res = v1 + v2*/\r
+#define gf_vec_add(res, v1, v2) { (res).x = (v1).x + (v2).x; (res).y = (v1).y + (v2).y; (res).z = (v1).z + (v2).z; }\r
+\r
+/*!\r
+ *\brief get 3D vector length\r
+ *\r
+ *Gets the length of a 3D vector\r
+ *\return length of the vector\r
+ */\r
+Fixed gf_vec_len(GF_Vec v);\r
+/*!\r
+ *\brief get 3D vector square length\r
+ *\r
+ *Gets the square length of a 3D vector\r
+ *\return square length of the vector\r
+ */\r
+Fixed gf_vec_lensq(GF_Vec v);\r
+/*!\r
+ *\brief get 3D vector dot product\r
+ *\r
+ *Gets the dot product of two vectors\r
+ *\return dot product of the vectors\r
+ */\r
+Fixed gf_vec_dot(GF_Vec v1, GF_Vec v2);\r
+/*!\r
+ *\brief vector normalization\r
+ *\r
+ *Norms the vector, eg make its length equal to \ref FIX_ONE\r
+ *\param v vector to normalize\r
+ */\r
+void gf_vec_norm(GF_Vec *v);\r
+/*!\r
+ *\brief vector scaling\r
+ *\r
+ *Scales a vector by a given amount\r
+ *\param v vector to scale\r
+ *\param f scale factor\r
+ *\return scaled vector\r
+ */\r
+GF_Vec gf_vec_scale(GF_Vec v, Fixed f);\r
+/*!\r
+ *\brief vector cross product\r
+ *\r
+ *Gets the cross product of two vectors\r
+ *\param v1 first vector\r
+ *\param v2 second vector\r
+ *\return cross-product vector\r
+ */\r
+GF_Vec gf_vec_cross(GF_Vec v1, GF_Vec v2);\r
+\r
+/*!\brief 4D vector\r
+ *\r
+ *The 4D vector object is used in all the GPAC framework for 4 dimension vectors, VRML Rotations and quaternions representation.\r
+*/\r
+typedef struct __vec4f\r
+{\r
+       Fixed x;\r
+       Fixed y;\r
+       Fixed z;\r
+       Fixed q;\r
+} GF_Vec4;\r
+\r
+\r
+/*!\brief 3D matrix\r
+ *\r
+ *The 3D matrix object used in GPAC. The matrix is oriented like OpenGL matrices (column-major ordering), with \r
+ the translation part at the end of the coefficients list.\r
+ \note Unless specified otherwise, the matrix object is always expected to represent an affine transformation.\r
+ */\r
+typedef struct\r
+{\r
+       Fixed m[16];\r
+} GF_Matrix;\r
+\r
+\r
+/*!\hideinitializer gets the len of a quaternion*/\r
+#define gf_quat_len(v) gf_sqrt(gf_mulfix((v).q,(v).q) + gf_mulfix((v).x,(v).x) + gf_mulfix((v).y,(v).y) + gf_mulfix((v).z,(v).z))\r
+/*!\hideinitializer normalizes a quaternion*/\r
+#define gf_quat_norm(v) { \\r
+       Fixed __mag = gf_quat_len(v);   \\r
+       (v).x = gf_divfix((v).x, __mag); (v).y = gf_divfix((v).y, __mag); (v).z = gf_divfix((v).z, __mag); (v).q = gf_divfix((v).q, __mag);     \\r
+       }       \\r
+\r
+/*!\brief quaternion to rotation\r
+ *\r
+ *Transforms a quaternion to a Rotation, expressed as a 4 dimension vector with x,y,z for axis and q for rotation angle\r
+ *\param quat the quaternion to transform\r
+ *\return the rotation value\r
+ */\r
+GF_Vec4 gf_quat_to_rotation(GF_Vec4 *quat);\r
+/*!\brief quaternion from rotation\r
+ *\r
+ *Transforms a Rotation to a quaternion\r
+ *\param rot the rotation to transform\r
+ *\return the quaternion value\r
+ */\r
+GF_Vec4 gf_quat_from_rotation(GF_Vec4 rot);\r
+/*!inverses a quaternion*/\r
+GF_Vec4 gf_quat_get_inv(GF_Vec4 *quat);\r
+/*!\brief quaternion multiplication\r
+ *\r
+ *Multiplies two quaternions\r
+ *\param q1 the first quaternion\r
+ *\param q2 the second quaternion\r
+ *\return the resulting quaternion\r
+ */\r
+GF_Vec4 gf_quat_multiply(GF_Vec4 *q1, GF_Vec4 *q2);\r
+/*!\brief quaternion vector rotating\r
+ *\r
+ *Rotates a vector with a quaternion \r
+ *\param quat the quaternion modelizing the rotation\r
+ *\param vec the vector to rotate\r
+ *\return the resulting vector\r
+ */\r
+GF_Vec gf_quat_rotate(GF_Vec4 *quat, GF_Vec *vec);\r
+/*!\brief quaternion from axis and cos\r
+ *\r
+ *Constructs a quaternion from an axis and a cosinus value (shortcut to \ref gf_quat_from_rotation)\r
+ *\param axis the rotation axis\r
+ *\param cos_a the rotation cosinus value\r
+ *\return the resulting quaternion\r
+ */\r
+GF_Vec4 gf_quat_from_axis_cos(GF_Vec axis, Fixed cos_a);\r
+/*!\brief quaternion interpolation\r
+ *\r
+ *Interpolates two quaternions using spherical linear interpolation\r
+ *\param q1 the first quaternion\r
+ *\param q2 the second quaternion\r
+ *\param frac the fraction of the interpolation, between 0 and \ref FIX_ONE\r
+ *\return the interpolated quaternion\r
+ */\r
+GF_Vec4 gf_quat_slerp(GF_Vec4 q1, GF_Vec4 q2, Fixed frac);\r
+\r
+/*!\brief 3D Bounding Box\r
+ *\r
+ *The 3D Bounding Box is a 3D Axis-Aligned Bounding Box used to in various tools of the GPAC framework for bounds \r
+ estimation of a 3D object. It features an axis-aligned box and a sphere bounding volume for fast intersection tests.\r
+ */\r
+typedef struct\r
+{\r
+       /*!minimum x, y, and z of the object*/\r
+       GF_Vec min_edge;\r
+       /*!maximum x, y, and z of the object*/\r
+       GF_Vec max_edge;\r
+\r
+       /*!center of the bounding box.\note this is computed from min_edge and max_edge*/\r
+       GF_Vec center;\r
+       /*!radius of the bounding sphere for this box.\note this is computed from min_edge and max_edge*/\r
+       Fixed radius;\r
+       /*!the bbox center and radius are valid*/\r
+       Bool is_set;\r
+} GF_BBox;\r
+/*!updates information of the bounding box based on the edge information*/\r
+void gf_bbox_refresh(GF_BBox *b);\r
+/*!builds a bounding box from a 2D rectangle*/\r
+void gf_bbox_from_rect(GF_BBox *box, GF_Rect *rc);\r
+/*!builds a rectangle from a 3D bounding box.\note The z dimension is lost and no projection is performed*/\r
+void gf_rect_from_bbox(GF_Rect *rc, GF_BBox *box);\r
+/*!\brief bounding box expansion\r
+ *\r
+ *Checks if a point is inside a bounding box and updates the bounding box to include it if not the case\r
+ *\param box the bounding box object\r
+ *\param pt the 3D point to check\r
+*/\r
+void gf_bbox_grow_point(GF_BBox *box, GF_Vec pt);\r
+/*!performs the union of two bounding boxes*/\r
+void gf_bbox_union(GF_BBox *b1, GF_BBox *b2);\r
+/*!checks if two bounding boxes are equal or not*/\r
+Bool gf_bbox_equal(GF_BBox *b1, GF_BBox *b2);\r
+/*!checks if a point is inside a bounding box or not*/\r
+Bool gf_bbox_point_inside(GF_BBox *box, GF_Vec *p);\r
+/*!\brief get box vertices\r
+ *\r
+ *Returns the 8 bounding box vertices given the minimum and maximum edge. Vertices are ordered to respect \r
+ "p-vertex indexes", (vertex from a box closest to plane) and so that n-vertex (vertex from a box farthest from plane) \r
+ is 7-p_vx_idx\r
+ *\param bmin minimum edge of the box\r
+ *\param bmax maximum edge of the box\r
+ *\param vecs list of 8 3D points used to store the vertices.\r
+*/\r
+void gf_bbox_get_vertices(GF_Vec bmin, GF_Vec bmax, GF_Vec *vecs);\r
+\r
+\r
+/*!\brief matrix initialization\r
+ *\hideinitializer\r
+ *\r
+ *Inits the matrix to the identity matrix\r
+*/\r
+#define gf_mx_init(_obj) { memset((_obj).m, 0, sizeof(Fixed)*16); (_obj).m[0] = (_obj).m[5] = (_obj).m[10] = (_obj).m[15] = FIX_ONE; }\r
+/*!\brief matrix copy\r
+ *\hideinitializer\r
+ *\r
+ *Copies the matrix _from to the matrix _obj\r
+*/\r
+#define gf_mx_copy(_obj, from) memcpy(&(_obj), &(from), sizeof(GF_Matrix));\r
+/*!\brief matrix constructor from 2D\r
+ *\r
+ *Initializes a 3D matrix from a 2D matrix.\note all z-related coefficients will be set to default.\r
+*/\r
+void gf_mx_from_mx2d(GF_Matrix *mx, GF_Matrix2D *mat2D);\r
+/*!\brief matrix identity testing\r
+ *\r
+ *Tests if two matrices are equal or not.\r
+ \return 1 if matrices are same, 0 otherwise\r
+*/\r
+Bool gf_mx_equal(GF_Matrix *mx1, GF_Matrix *mx2);\r
+/*!\brief matrix translation\r
+ *\r
+ *Translates a matrix \r
+ *\param mx the matrix being transformed. Once the function is called, contains the result matrix\r
+ *\param tx horizontal translation\r
+ *\param ty vertical translation\r
+ *\param tz depth translation\r
+*/\r
+void gf_mx_add_translation(GF_Matrix *mx, Fixed tx, Fixed ty, Fixed tz);\r
+/*!\brief matrix scaling\r
+ *\r
+ *Scales a matrix \r
+ *\param mx the matrix being transformed. Once the function is called, contains the result matrix\r
+ *\param sx horizontal translation scaling\r
+ *\param sy vertical translation scaling\r
+ *\param sz depth translation scaling\r
+*/\r
+void gf_mx_add_scale(GF_Matrix *mx, Fixed sx, Fixed sy, Fixed sz);\r
+/*!\brief matrix rotating\r
+ *\r
+ *Rotates a matrix \r
+ *\param mx the matrix being transformed. Once the function is called, contains the result matrix\r
+ *\param angle rotation angle in radians\r
+ *\param x horizontal coordinate of rotation axis\r
+ *\param y vertical coordinate of rotation axis\r
+ *\param z depth coordinate of rotation axis\r
+*/\r
+void gf_mx_add_rotation(GF_Matrix *mx, Fixed angle, Fixed x, Fixed y, Fixed z);\r
+/*!\brief matrices multiplication \r
+ *\r
+ *Multiplies a matrix with another one mx = mx*mul\r
+ *\param mx the matrix being transformed. Once the function is called, contains the result matrix\r
+ *\param mul the matrix to add\r
+*/\r
+void gf_mx_add_matrix(GF_Matrix *mx, GF_Matrix *mul);\r
+/*!\brief 2D matrix multiplication\r
+ *\r
+ *Adds a 2D affine matrix to a matrix\r
+ *\param mx the matrix \r
+ *\param mat2D the matrix to premultiply\r
+ */\r
+void gf_mx_add_matrix_2d(GF_Matrix *mx, GF_Matrix2D *mat2D);\r
+\r
+/*!\brief affine matrix inversion\r
+ *\r
+ *Inverses an affine matrix.\warning Results are undefined if the matrix is not an affine one\r
+ *\param mx the matrix to inverse\r
+ */\r
+void gf_mx_inverse(GF_Matrix *mx);\r
+/*!\brief matrix point transformation\r
+ *\r
+ *Applies a 3D matrix transformation to a 3D point\r
+ *\param mx transformation matrix\r
+ *\param pt pointer to 3D point. Once the function is called, pt contains the transformed point\r
+*/\r
+void gf_mx_apply_vec(GF_Matrix *mx, GF_Vec *pt);\r
+/*!\brief matrix rectangle transformation\r
+ *\r
+ *Applies a 3D matrix transformation to a rectangle, giving the enclosing rectangle of the transformed one.\note all depth information are discarded.\r
+ *\param _this transformation matrix\r
+ *\param rc pointer to rectangle. Once the function is called, rc contains the transformed rectangle\r
+*/\r
+void gf_mx_apply_rect(GF_Matrix *_this, GF_Rect *rc);\r
+/*!\brief ortho matrix construction\r
+ *\r
+ *Creates an orthogonal projection matrix\r
+ *\param mx matrix to initialize\r
+ *\param left min horizontal coordinate of viewport\r
+ *\param right max horizontal coordinate of viewport\r
+ *\param bottom min vertical coordinate of viewport\r
+ *\param top max vertical coordinate of viewport\r
+ *\param z_near min depth coordinate of viewport\r
+ *\param z_far max depth coordinate of viewport\r
+*/\r
+void gf_mx_ortho(GF_Matrix *mx, Fixed left, Fixed right, Fixed bottom, Fixed top, Fixed z_near, Fixed z_far);\r
+/*!\brief perspective matrix construction\r
+ *\r
+ *Creates a perspective projection matrix\r
+ *\param mx matrix to initialize\r
+ *\param foc camera field of view angle in radian\r
+ *\param aspect_ratio viewport aspect ratio\r
+ *\param z_near min depth coordinate of viewport\r
+ *\param z_far max depth coordinate of viewport\r
+*/\r
+void gf_mx_perspective(GF_Matrix *mx, Fixed foc, Fixed aspect_ratio, Fixed z_near, Fixed z_far);\r
+/*!\brief creates look matrix\r
+ *\r
+ *Creates a transformation matrix looking at a given direction from a given point (camera matrix).\r
+ *\param mx matrix to initialize\r
+ *\param position position\r
+ *\param target look direction\r
+ *\param up_vector vector describing the up direction\r
+*/\r
+void gf_mx_lookat(GF_Matrix *mx, GF_Vec position, GF_Vec target, GF_Vec up_vector);\r
+/*!\brief matrix box transformation\r
+ *\r
+ *Applies a 3D matrix transformation to a bounding box, giving the enclosing box of the transformed one\r
+ *\param mx transformation matrix\r
+ *\param b pointer to bounding box. Once the function is called, contains the transformed bounding box\r
+*/\r
+void gf_mx_apply_bbox(GF_Matrix *mx, GF_BBox *b);\r
+/*!\brief matrix box sphere transformation\r
+ *\r
+ *Applies a 3D matrix transformation to a bounding box, computing only the enclosing sphere of the transformed one.\r
+ *\param mx transformation matrix\r
+ *\param b pointer to bounding box. Once the function is called, contains the transformed bounding sphere\r
+*/\r
+void gf_mx_apply_bbox_sphere(GF_Matrix *mx, GF_BBox *box);\r
+/*!\brief non-affine matrix multiplication\r
+ *\r
+ *Multiplies two non-affine matrices mx = mx*mul\r
+*/\r
+void gf_mx_add_matrix_4x4(GF_Matrix *mat, GF_Matrix *mul);\r
+/*!\brief non-affine matrix inversion\r
+ *\r
+ *Inverses a non-affine matrices\r
+ *\return 1 if inversion was done, 0 if inversion not possible.\r
+*/\r
+Bool gf_mx_inverse_4x4(GF_Matrix *mx);\r
+/*!\brief matrix 4D vector transformation\r
+ *\r
+ *Applies a 3D non-affine matrix transformation to a 4 dimension vector\r
+ *\param mx transformation matrix\r
+ *\param vec pointer to the vector. Once the function is called, contains the transformed vector\r
+*/\r
+void gf_mx_apply_vec_4x4(GF_Matrix *mx, GF_Vec4 *vec);\r
+/*!\brief matrix decomposition\r
+ *\r
+ *Decomposes a matrix into translation, scale, shear and rotate\r
+ *\param mx the matrix to decompose\r
+ *\param translate the decomposed translation part\r
+ *\param scale the decomposed scaling part\r
+ *\param rotate the decomposed rotation part, expressed as a Rotataion (axis + angle)\r
+ *\param shear the decomposed shear part\r
+ */\r
+void gf_mx_decompose(GF_Matrix *mx, GF_Vec *translate, GF_Vec *scale, GF_Vec4 *rotate, GF_Vec *shear);\r
+/*!\brief matrix vector rotation\r
+ *\r
+ *Rotates a vector with a given matrix, ignoring any translation.\r
+ *\param mx transformation matrix\r
+ *\param pt pointer to 3D vector. Once the function is called, pt contains the transformed vector\r
+ */\r
+void gf_mx_rotate_vector(GF_Matrix *mx, GF_Vec *pt);\r
+/*!\brief matrix initialization from vectors\r
+ *\r
+ *Inits a matrix to rotate the local axis in the given vectors\r
+ \param mx matrix to initialize\r
+ \param x_axis target normalized X axis\r
+ \param y_axis target normalized Y axis\r
+ \param z_axis target normalized Z axis\r
+*/\r
+void gf_mx_rotation_matrix_from_vectors(GF_Matrix *mx, GF_Vec x_axis, GF_Vec y_axis, GF_Vec z_axis);\r
+/*!\brief matrix to 2D matrix \r
+ *\r
+ *Inits a 2D matrix by removing all depth info from a 3D matrix\r
+ *\param mx2d 2D matrix to initialize\r
+ *\param mx 3D matrix to use\r
+*/\r
+void gf_mx2d_from_mx(GF_Matrix2D *mx2d, GF_Matrix *mx);\r
+\r
+/*!\brief Plane object*/\r
+typedef struct\r
+{\r
+       /*!normal vector to the plane*/\r
+       GF_Vec normal;\r
+       /*!distance from origin of the plane*/\r
+       Fixed d;\r
+} GF_Plane;\r
+/*!\brief matrix plane transformation\r
+ *\r
+ *Transorms a plane by a given matrix\r
+ *\param mx the matrix to use\r
+ *\param plane pointer to 3D plane. Once the function is called, plane contains the transformed plane\r
+ */\r
+void gf_mx_apply_plane(GF_Matrix *mx, GF_Plane *plane);\r
+/*!\brief point to plane distance\r
+ *\r
+ *Gets the distance between a point and a plne\r
+ *\param plane the plane to use\r
+ *\param p pointer to ^point to check\r
+ *\return the distance between the place and the point\r
+ */\r
+Fixed gf_plane_get_distance(GF_Plane *plane, GF_Vec *p);\r
+/*!\brief closest point on a line\r
+ *\r
+ *Gets the closest point on a line from a given point in space\r
+ *\param line_pt a point of the line to test\r
+ *\param line_vec the normalized direction vector of the line\r
+ *\param pt the point to check\r
+ *\return the closest point on the line to the desired point\r
+ */\r
+GF_Vec gf_closest_point_to_line(GF_Vec line_pt, GF_Vec line_vec, GF_Vec pt);\r
+/*!\brief box p-vertex index\r
+ *\r
+ *Gets the p-vertex index for a given plane. The p-vertex index is the index of the closest vertex of a bounding box to the plane. The vertices of a box are always \r
+ *ordered in GPAC? cf \ref gf_bbox_get_vertices\r
+ *\param p the plane to check\r
+ *\return the p-vertex index value, ranging from 0 to 7\r
+*/\r
+u32 gf_plane_get_p_vertex_idx(GF_Plane *p);\r
+/*!\brief plane line intersection\r
+ *\r
+ *Checks for the intersection of a plane and a line\r
+ *\param plane plane to test\r
+ *\param linepoint a point on the line to test\r
+ *\param linevec normalized direction vector of the line to test\r
+ *\param outPoint optional pointer to retrieve the intersection point, NULL otherwise\r
+ *\return 1 if line and plane intersect, 0 otherwise\r
+*/\r
+Bool gf_plane_intersect_line(GF_Plane *plane, GF_Vec *linepoint, GF_Vec *linevec, GF_Vec *outPoint);\r
+\r
+/*!Classification types for box/plane position used in \ref gf_bbox_plane_relation*/\r
+enum \r
+{      \r
+       /*!box is in front of the plane*/\r
+       GF_BBOX_FRONT,\r
+       /*!box intersects the plane*/\r
+       GF_BBOX_INTER,\r
+       /*!box is back of the plane*/\r
+       GF_BBOX_BACK\r
+};\r
+/*!\brief box-plane relation\r
+ *\r
+ *Gets the spatial relation between a box and a plane\r
+ *\param box the box to check\r
+ *\param p the plane to check\r
+ *\return the relation type\r
+ */\r
+u32 gf_bbox_plane_relation(GF_BBox *box, GF_Plane *p);\r
+\r
+/*!\brief 3D Ray\r
+ *\r
+ *The 3D ray object is used in GPAC for all collision and mouse interaction tests\r
+*/\r
+typedef struct\r
+{\r
+       /*!origin point of the ray*/\r
+       GF_Vec orig;\r
+       /*!normalized direction vector of the ray*/\r
+       GF_Vec dir;\r
+} GF_Ray;\r
+\r
+/*!\brief ray constructor\r
+ *\r
+ *Constructs a ray object\r
+ *\param start starting point of the ray\r
+ *\param end end point of the ray, or any point on the ray\r
+ *\return the ray object\r
+*/\r
+GF_Ray gf_ray(GF_Vec start, GF_Vec end);\r
+/*!\brief matrix ray transformation\r
+ *\r
+ *Transforms a ray by a given transformation matrix\r
+ *\param mx the matrix to use\r
+ *\param r pointer to the ray. Once the function is called, contains the transformed ray\r
+*/\r
+void gf_mx_apply_ray(GF_Matrix *mx, GF_Ray *r);\r
+/*!\brief ray box intersection test\r
+ *\r
+ *Checks if a ray intersects a box or not\r
+ *\param ray the ray to check\r
+ *\param min_edge the minimum edge of the box to check\r
+ *\param max_edge the maximum edge of the box to check\r
+ *\param out_point optional location of a 3D point to store the intersection, NULL otherwise.\r
+ *\return retuns 1 if the ray intersects the box, 0 otherwise\r
+*/\r
+Bool gf_ray_hit_box(GF_Ray *ray, GF_Vec min_edge, GF_Vec max_edge, GF_Vec *out_point);\r
+/*!\brief ray sphere intersection test\r
+ *\r
+ *Checks if a ray intersects a box or not\r
+ *\param ray the ray to check\r
+ *\param center the center of the sphere to check. If NULL, the origin (0,0,0)is used\r
+ *\param radius the radius of the sphere to check\r
+ *\param out_point optional location of a 3D point to store the intersection, NULL otherwise\r
+ *\return retuns 1 if the ray intersects the sphere, 0 otherwise\r
+*/\r
+Bool gf_ray_hit_sphere(GF_Ray *ray, GF_Vec *center, Fixed radius, GF_Vec *out_point);\r
+/*!\brief ray triangle intersection test\r
+ *\r
+ *Checks if a ray intersects a triangle or not\r
+ *\param ray the ray to check\r
+ *\param v0 first vertex of the triangle\r
+ *\param v1 second vertex of the triangle\r
+ *\param v2 third vertex of the triangle\r
+ *\param dist optional location of a fixed number to store the intersection distance from ray origin if any, NULL otherwise\r
+ *\return retuns 1 if the ray intersects the triangle, 0 otherwise\r
+*/\r
+Bool gf_ray_hit_triangle(GF_Ray *ray, GF_Vec *v0, GF_Vec *v1, GF_Vec *v2, Fixed *dist);\r
+/*same as above and performs backface cull (solid meshes)*/\r
+/*!\brief ray triangle intersection test\r
+ *\r
+ *Checks if a ray intersects a triangle or not, performing backface culling. For parameters details, look at \ref gf_ray_hit_triangle_backcull\r
+ */\r
+Bool gf_ray_hit_triangle_backcull(GF_Ray *ray, GF_Vec *v0, GF_Vec *v1, GF_Vec *v2, Fixed *dist);\r
+\r
+/*! @} */\r
+\r
+/*! @} */\r
+\r
+#ifdef __cplusplus\r
+}\r
+#endif\r
+\r
+\r
+#endif         /*_GF_MATH_H_*/\r
+\r