MAV'RIC
quick_trig.h
1 /*******************************************************************************
2  * Copyright (c) 2009-2014, MAV'RIC Development Team
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions are met:
7  *
8  * 1. Redistributions of source code must retain the above copyright notice,
9  * this list of conditions and the following disclaimer.
10  *
11  * 2. Redistributions in binary form must reproduce the above copyright notice,
12  * this list of conditions and the following disclaimer in the documentation
13  * and/or other materials provided with the distribution.
14  *
15  * 3. Neither the name of the copyright holder nor the names of its contributors
16  * may be used to endorse or promote products derived from this software without
17  * specific prior written permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
20  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
23  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29  * POSSIBILITY OF SUCH DAMAGE.
30  ******************************************************************************/
31 
32 /*******************************************************************************
33  * \file quick_trig.h
34  *
35  * \author MAV'RIC Team
36  * \author Julien Lecoeur
37  *
38  * \brief Quick implementation of trigonometric functions
39  *
40  ******************************************************************************/
41 
42 
43 #ifndef QUICK_TRIG_H_
44 #define QUICK_TRIG_H_
45 
46 #ifdef __cplusplus
47 extern "C"
48 {
49 #endif
50 
51 #include "maths.h"
52 
53 
54 #define INTERP_POINTS 50
55 
56 
64 float quick_trig_sin(float x);
65 
66 
74 float quick_trig_cos(float x);
75 
76 
84 float quick_trig_acos(float x);
85 
86 
94 float quick_trig_asin(float x);
95 
96 
104 float quick_trig_tan(float x);
105 
106 
114 float quick_trig_atan(float x);
115 
116 
128 static inline float quick_trig_func(float x, const float func_x_min, const float func_x_max, float func_x_step, const float func_y[])
129 {
130  float y;
131  int32_t i;
132 
133  if ( x <= func_x_min )
134  {
135  y = func_y[0];
136  }
137  else if ( x >= func_x_max )
138  {
139  y = func_y[INTERP_POINTS - 1];
140  }
141  else
142  {
143  i = (int32_t) ((x - func_x_min) / func_x_step);
144  y = maths_interpolate(x,
145  func_x_min + i * func_x_step,
146  func_x_min + (i + 1) * func_x_step,
147  func_y[i],
148  func_y[i+1]);
149  }
150  return y;
151 }
152 
153 #ifdef __cplusplus
154 }
155 #endif
156 
157 #endif /* QUICK_TRIG_H_ */