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


apriltag
Author(s): Edwin Olson , Max Krogius
autogenerated on Mon Jun 26 2023 02:26:12