math_util.h
Go to the documentation of this file.
1 /* Copyright (C) 2013-2016, The Regents of The University of Michigan.
2 All rights reserved.
3 
4 This software was developed in the APRIL Robotics Lab under the
5 direction of Edwin Olson, ebolson@umich.edu. This software may be
6 available under alternative licensing terms; contact the address above.
7 
8 Redistribution and use in source and binary forms, with or without
9 modification, are permitted provided that the following conditions are met:
10 
11 1. Redistributions of source code must retain the above copyright notice, this
12  list of conditions and the following disclaimer.
13 2. Redistributions in binary form must reproduce the above copyright notice,
14  this list of conditions and the following disclaimer in the documentation
15  and/or other materials provided with the distribution.
16 
17 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
18 ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
19 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
20 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
21 ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
22 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
23 LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
24 ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
26 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 
28 The views and conclusions contained in the software and documentation are those
29 of the authors and should not be interpreted as representing official policies,
30 either expressed or implied, of the Regents of The University of Michigan.
31 */
32 
33 #ifndef _MATHUTIL_H
34 #define _MATHUTIL_H
35 
36 #include <math.h>
37 #include <float.h>
38 #include <stdlib.h>
39 #include <stdint.h>
40 #include <assert.h>
41 #include <string.h> // memcpy
42 
43 #ifdef __cplusplus
44 extern "C" {
45 #endif
46 
47 #ifndef M_TWOPI
48 # define M_TWOPI 6.2831853071795862319959 /* 2*pi */
49 #endif
50 
51 #ifndef M_PI
52 # define M_PI 3.141592653589793238462643383279502884196
53 #endif
54 
55 #define to_radians(x) ( (x) * (M_PI / 180.0 ))
56 #define to_degrees(x) ( (x) * (180.0 / M_PI ))
57 
58 #define max(A, B) (A < B ? B : A)
59 #define min(A, B) (A < B ? A : B)
60 
61  /* DEPRECATE, threshold meaningless without context.
62 static inline int dequals(double a, double b)
63 {
64  double thresh = 1e-9;
65  return (fabs(a-b) < thresh);
66 }
67  */
68 
69 static inline int dequals_mag(double a, double b, double thresh)
70 {
71  return (fabs(a-b) < thresh);
72 }
73 
74 static inline int isq(int v)
75 {
76  return v*v;
77 }
78 
79 static inline float fsq(float v)
80 {
81  return v*v;
82 }
83 
84 static inline double sq(double v)
85 {
86  return v*v;
87 }
88 
89 static inline double sgn(double v)
90 {
91  return (v>=0) ? 1 : -1;
92 }
93 
94 // random number between [0, 1)
95 static inline float randf()
96 {
97  return ((float) rand()) / (RAND_MAX + 1.0);
98 }
99 
100 
101 static inline float signed_randf()
102 {
103  return randf()*2 - 1;
104 }
105 
106 // return a random integer between [0, bound)
107 static inline int irand(int bound)
108 {
109  int v = (int) (randf()*bound);
110  if (v == bound)
111  return (bound-1);
112  //assert(v >= 0);
113  //assert(v < bound);
114  return v;
115 }
116 
118 static inline double mod2pi_positive(double vin)
119 {
120  return vin - M_TWOPI * floor(vin / M_TWOPI);
121 }
122 
124 static inline double mod2pi(double vin)
125 {
126  return mod2pi_positive(vin + M_PI) - M_PI;
127 }
128 
130 static inline double mod2pi_ref(double ref, double vin)
131 {
132  return ref + mod2pi(vin - ref);
133 }
134 
136 static inline double mod360_positive(double vin)
137 {
138  return vin - 360 * floor(vin / 360);
139 }
140 
142 static inline double mod360(double vin)
143 {
144  return mod360_positive(vin + 180) - 180;
145 }
146 
147 static inline int theta_to_int(double theta, int max)
148 {
149  theta = mod2pi_ref(M_PI, theta);
150  int v = (int) (theta / M_TWOPI * max);
151 
152  if (v == max)
153  v = 0;
154 
155  assert (v >= 0 && v < max);
156 
157  return v;
158 }
159 
160 static inline int imin(int a, int b)
161 {
162  return (a < b) ? a : b;
163 }
164 
165 static inline int imax(int a, int b)
166 {
167  return (a > b) ? a : b;
168 }
169 
170 static inline int64_t imin64(int64_t a, int64_t b)
171 {
172  return (a < b) ? a : b;
173 }
174 
175 static inline int64_t imax64(int64_t a, int64_t b)
176 {
177  return (a > b) ? a : b;
178 }
179 
180 static inline int iclamp(int v, int minv, int maxv)
181 {
182  return imax(minv, imin(v, maxv));
183 }
184 
185 static inline double dclamp(double a, double min, double max)
186 {
187  if (a < min)
188  return min;
189  if (a > max)
190  return max;
191  return a;
192 }
193 
194 static inline int fltcmp (float f1, float f2)
195 {
196  float epsilon = f1-f2;
197  if (epsilon < 0.0)
198  return -1;
199  else if (epsilon > 0.0)
200  return 1;
201  else
202  return 0;
203 }
204 
205 static inline int dblcmp (double d1, double d2)
206 {
207  double epsilon = d1-d2;
208  if (epsilon < 0.0)
209  return -1;
210  else if (epsilon > 0.0)
211  return 1;
212  else
213  return 0;
214 }
215 
216 #ifdef __cplusplus
217 }
218 #endif
219 
220 #endif
static int iclamp(int v, int minv, int maxv)
Definition: math_util.h:180
static int dblcmp(double d1, double d2)
Definition: math_util.h:205
static double mod360_positive(double vin)
Definition: math_util.h:136
static int irand(int bound)
Definition: math_util.h:107
static int isq(int v)
Definition: math_util.h:74
static int imax(int a, int b)
Definition: math_util.h:165
static double sgn(double v)
Definition: math_util.h:89
static double mod2pi(double vin)
Definition: math_util.h:124
static int fltcmp(float f1, float f2)
Definition: math_util.h:194
static double sq(double v)
Definition: math_util.h:84
static int imin(int a, int b)
Definition: math_util.h:160
static float randf()
Definition: math_util.h:95
static float fsq(float v)
Definition: math_util.h:79
static double mod360(double vin)
Definition: math_util.h:142
static float signed_randf()
Definition: math_util.h:101
static int64_t imin64(int64_t a, int64_t b)
Definition: math_util.h:170
static double mod2pi_ref(double ref, double vin)
Definition: math_util.h:130
#define M_TWOPI
Definition: math_util.h:48
#define min(A, B)
Definition: math_util.h:59
static double dclamp(double a, double min, double max)
Definition: math_util.h:185
static int64_t imax64(int64_t a, int64_t b)
Definition: math_util.h:175
static int theta_to_int(double theta, int max)
Definition: math_util.h:147
#define M_PI
Definition: math_util.h:52
#define max(A, B)
Definition: math_util.h:58
static int dequals_mag(double a, double b, double thresh)
Definition: math_util.h:69
static double mod2pi_positive(double vin)
Definition: math_util.h:118


apriltags2
Author(s): Danylo Malyuta
autogenerated on Fri Oct 19 2018 04:02:32