hal_gfx_math.h
Go to the documentation of this file.
1 /**
2 ****************************************************************************************
3 *
4 * @file hal_gfx_math.h
5 * @author BLE Driver Team
6 * @brief Header file containing functions prototypes of Graphics library.
7 *
8 ****************************************************************************************
9 * @attention
10 #####Copyright (c) 2019 GOODIX
11 All rights reserved.
12
13 Redistribution and use in source and binary forms, with or without
14 modification, are permitted provided that the following conditions are met:
15 * Redistributions of source code must retain the above copyright
16 notice, this list of conditions and the following disclaimer.
17 * Redistributions in binary form must reproduce the above copyright
18 notice, this list of conditions and the following disclaimer in the
19 documentation and/or other materials provided with the distribution.
20 * Neither the name of GOODIX nor the names of its contributors may be used
21 to endorse or promote products derived from this software without
22 specific prior written permission.
23
24 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
25 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 ARE DISCLAIMED. IN NO EVENT SHALL COPYRIGHT HOLDERS AND CONTRIBUTORS BE
28 LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
29 CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
30 SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
31 INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
32 CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
33 ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
34 POSSIBILITY OF SUCH DAMAGE.
35 ****************************************************************************************
36 */
37
38 /** @addtogroup GRAPHICS_SDK Graphics
39 * @{
40 */
41
42 /** @addtogroup HAL_GFX HAL GFX
43 * @{
44 */
45
46 /** @defgroup HAL_GFX_MATH GFX MATH
47 * @brief GPU base math interfaces.
48 * @{
49 */
50
51 #ifndef HAL_GFX_MATH_H__
52 #define HAL_GFX_MATH_H__
53
54 #ifdef __cplusplus
55 extern "C" {
56 #endif
57
58 /**
59 * @defgroup HAL_GFX_MATH_MACRO Defines
60 * @{
61 */
62 #define HAL_GFX_E 2.71828182845904523536f /**< e */
63 #define HAL_GFX_LOG2E 1.44269504088896340736f /**< log2(e) */
64 #define HAL_GFX_LOG10E 0.434294481903251827651f /**< log10(e) */
65 #define HAL_GFX_LN2 0.693147180559945309417f /**< ln(2) */
66 #define HAL_GFX_LN10 2.30258509299404568402f /**< ln(10) */
67 #define HAL_GFX_PI 3.14159265358979323846f /**< pi */
68 #define HAL_GFX_PI_2 1.57079632679489661923f /**< pi/2 */
69 #define HAL_GFX_PI_4 0.785398163397448309616f /**< pi/4 */
70 #define HAL_GFX_1_PI 0.318309886183790671538f /**< 1/pi */
71 #define HAL_GFX_2_PI 0.636619772367581343076f /**< 2/pi */
72 #define HAL_GFX_2_SQRTPI 1.12837916709551257390f /**< 2/sqrt(pi) */
73 #define HAL_GFX_SQRT2 1.41421356237309504880f /**< sqrt(2) */
74 #define HAL_GFX_SQRT1_2 0.707106781186547524401f /**< 1/sqrt(2) */
75
76 #define hal_gfx_min2(a,b) (((a)<(b))?( a):(b)) /**< Find the minimum of two values */
77 #define hal_gfx_max2(a,b) (((a)>(b))?( a):(b)) /**< Find the maximum of two values */
78 #define hal_gfx_clamp(val, min, max) hal_gfx_min2((max), hal_gfx_max2((min), (val))) /**< Clamp value. */
79 #define hal_gfx_abs(a) (((a)< 0 )?(-(a)):(a)) /**< Calculate the absolute value of int. */
80 #define hal_gfx_absf(a) (((a)< 0.f )?(-(a)):(a)) /**< Calculate the absolute value of float. */
81 #define hal_gfx_floats_equal(x, y) (hal_gfx_absf((x) - (y)) <= 0.00001f * hal_gfx_min2(hal_gfx_absf(x), hal_gfx_absf(y))) /**< Compare two floats. */
82 #define hal_gfx_float_is_zero(x) (hal_gfx_absf(x) <= 0.00001f) /**< Checks if value x is zero. */
83 #define hal_gfx_deg_to_rad(d) (0.0174532925199f * (d)) /**< Convert degrees to radians. */
84 #define hal_gfx_rad_to_deg(r) (57.295779513f * (r)) /**< onvert radians to degries. */
85 #define hal_gfx_i2fx(a) ((a)*0x10000) /**< Convert integer to 16.16 fixed point. */
86 #define hal_gfx_floor(f) ((int)(f) - ( (int)(f) > (f) )) /**< Floor function. */
87 #define hal_gfx_ceil(f) ((int)(f) + ( (int)(f) < (f) )) /**< Ceiling function. */
88 /** @} */
89
90 /**
91 * @defgroup HAL_GFX_MATH_FUNCTION Functions
92 * @{
93 */
94 /**
95 *****************************************************************************************
96 * @brief Fast sine approximation of a given angle
97 *
98 * @param[in] angle_degrees: Angle in degrees
99 *
100 * @return return Sine of the given angle
101 *****************************************************************************************
102 */
103 float hal_gfx_sin(float angle_degrees);
104
105 /**
106 *****************************************************************************************
107 * @brief Fast cosine approximation of a given angle
108 *
109 * @param[in] angle_degrees: Angle in degrees
110 *
111 * @return Cosine of the given angle
112 *****************************************************************************************
113 */
114 float hal_gfx_cos(float angle_degrees);
115
116 /**
117 *****************************************************************************************
118 * @brief Fast tangent approximation of a given angle
119 *
120 * @param[in] angle_degrees: Angle in degrees
121 *
122 * @return Tangent of the given angle
123 *****************************************************************************************
124 */
125 float hal_gfx_tan(float angle_degrees);
126
127 /**
128 *****************************************************************************************
129 * @brief A rough approximation of x raised to the power of y. USE WITH CAUTION!
130 *
131 * @param[in] x: base value. Must be non negative.
132 * @param[in] y: power value
133 *
134 * @return The result of raising x to the power y
135 *****************************************************************************************
136 */
137 float hal_gfx_pow(float x, float y);
138
139 /**
140 *****************************************************************************************
141 * @brief A rough approximation of the square root of x. USE WITH CAUTION!
142 *
143 * @param[in] x: X value. Must be non negative
144 *
145 * @return The square root of x
146 *****************************************************************************************
147 */
148 float hal_gfx_sqrt(float x);
149
150 /**
151 *****************************************************************************************
152 * @brief A floating-point approximation of the inverse tangent of x
153 *
154 * @param[in] x: X value
155 *
156 * @return Inverse tangent (angle) of x in degrees
157 *****************************************************************************************
158 */
159 float hal_gfx_atan(float x);
160
161 /**
162 *****************************************************************************************
163 * @brief Convert float to 16.16 fixed point
164 *
165 * @param[in] f: Value to be converted
166 *
167 * @return 16.16 fixed point value
168 *****************************************************************************************
169 */
170 int hal_gfx_f2fx(float f);
171 /** @} */
172 #ifdef __cplusplus
173 }
174 #endif
175
176 #endif
177 /** @} */
178 /** @} */
179 /** @} */
180
hal_gfx_cos
float hal_gfx_cos(float angle_degrees)
Fast cosine approximation of a given angle.
hal_gfx_sqrt
float hal_gfx_sqrt(float x)
A rough approximation of the square root of x.
hal_gfx_tan
float hal_gfx_tan(float angle_degrees)
Fast tangent approximation of a given angle.
hal_gfx_atan
float hal_gfx_atan(float x)
A floating-point approximation of the inverse tangent of x.
hal_gfx_sin
float hal_gfx_sin(float angle_degrees)
Fast sine approximation of a given angle.
hal_gfx_f2fx
int hal_gfx_f2fx(float f)
Convert float to 16.16 fixed point.
hal_gfx_pow
float hal_gfx_pow(float x, float y)
A rough approximation of x raised to the power of y.