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 #define _USE_MATH_DEFINES
31 #include <math.h>
32 #include <stdlib.h>
33 #include <stdint.h>
34 #include <assert.h>
35 
36 #ifdef __cplusplus
37 extern "C" {
38 #endif
39 
40 #define to_radians(x) ( (x) * (M_PI / 180.0 ))
41 #define to_degrees(x) ( (x) * (180.0 / M_PI ))
42 
43  /* DEPRECATE, threshold meaningless without context.
44 static inline int dequals(double a, double b)
45 {
46  double thresh = 1e-9;
47  return (fabs(a-b) < thresh);
48 }
49  */
50 
51 static inline int dequals_mag(double a, double b, double thresh)
52 {
53  return (fabs(a-b) < thresh);
54 }
55 
56 static inline int isq(int v)
57 {
58  return v*v;
59 }
60 
61 static inline float fsq(float v)
62 {
63  return v*v;
64 }
65 
66 static inline double sq(double v)
67 {
68  return v*v;
69 }
70 
71 static inline double sgn(double v)
72 {
73  return (v>=0) ? 1 : -1;
74 }
75 
76 // random number between [0, 1)
77 static inline float randf()
78 {
79  return (float)(rand() / (RAND_MAX + 1.0));
80 }
81 
82 
83 static inline float signed_randf()
84 {
85  return randf()*2 - 1;
86 }
87 
88 // return a random integer between [0, bound)
89 static inline int irand(int bound)
90 {
91  int v = (int) (randf()*bound);
92  if (v == bound)
93  return (bound-1);
94  //assert(v >= 0);
95  //assert(v < bound);
96  return v;
97 }
98 
100 static inline double mod2pi_positive(double vin)
101 {
102  return vin - M_2_PI * floor(vin / M_2_PI);
103 }
104 
106 static inline double mod2pi(double vin)
107 {
108  return mod2pi_positive(vin + M_PI) - M_PI;
109 }
110 
112 static inline double mod2pi_ref(double ref, double vin)
113 {
114  return ref + mod2pi(vin - ref);
115 }
116 
118 static inline double mod360_positive(double vin)
119 {
120  return vin - 360 * floor(vin / 360);
121 }
122 
124 static inline double mod360(double vin)
125 {
126  return mod360_positive(vin + 180) - 180;
127 }
128 
129 static inline int mod_positive(int vin, int mod) {
130  return (vin % mod + mod) % mod;
131 }
132 
133 static inline int theta_to_int(double theta, int max)
134 {
135  theta = mod2pi_ref(M_PI, theta);
136  int v = (int) (theta / M_2_PI * max);
137 
138  if (v == max)
139  v = 0;
140 
141  assert (v >= 0 && v < max);
142 
143  return v;
144 }
145 
146 static inline int imin(int a, int b)
147 {
148  return (a < b) ? a : b;
149 }
150 
151 static inline int imax(int a, int b)
152 {
153  return (a > b) ? a : b;
154 }
155 
156 static inline int64_t imin64(int64_t a, int64_t b)
157 {
158  return (a < b) ? a : b;
159 }
160 
161 static inline int64_t imax64(int64_t a, int64_t b)
162 {
163  return (a > b) ? a : b;
164 }
165 
166 static inline int iclamp(int v, int minv, int maxv)
167 {
168  return imax(minv, imin(v, maxv));
169 }
170 
171 static inline double dclamp(double a, double min, double max)
172 {
173  if (a < min)
174  return min;
175  if (a > max)
176  return max;
177  return a;
178 }
179 
180 static inline int fltcmp (float f1, float f2)
181 {
182  float epsilon = f1-f2;
183  if (epsilon < 0.0)
184  return -1;
185  else if (epsilon > 0.0)
186  return 1;
187  else
188  return 0;
189 }
190 
191 static inline int dblcmp (double d1, double d2)
192 {
193  double epsilon = d1-d2;
194  if (epsilon < 0.0)
195  return -1;
196  else if (epsilon > 0.0)
197  return 1;
198  else
199  return 0;
200 }
201 
202 #ifdef __cplusplus
203 }
204 #endif
dequals_mag
static int dequals_mag(double a, double b, double thresh)
Definition: math_util.h:51
mod360
static double mod360(double vin)
Definition: math_util.h:124
imin
static int imin(int a, int b)
Definition: math_util.h:146
dclamp
static double dclamp(double a, double min, double max)
Definition: math_util.h:171
signed_randf
static float signed_randf()
Definition: math_util.h:83
mod2pi_ref
static double mod2pi_ref(double ref, double vin)
Definition: math_util.h:112
theta_to_int
static int theta_to_int(double theta, int max)
Definition: math_util.h:133
iclamp
static int iclamp(int v, int minv, int maxv)
Definition: math_util.h:166
fsq
static float fsq(float v)
Definition: math_util.h:61
imin64
static int64_t imin64(int64_t a, int64_t b)
Definition: math_util.h:156
imax64
static int64_t imax64(int64_t a, int64_t b)
Definition: math_util.h:161
isq
static int isq(int v)
Definition: math_util.h:56
mod_positive
static int mod_positive(int vin, int mod)
Definition: math_util.h:129
mod2pi
static double mod2pi(double vin)
Definition: math_util.h:106
sq
static double sq(double v)
Definition: math_util.h:66
imax
static int imax(int a, int b)
Definition: math_util.h:151
sgn
static double sgn(double v)
Definition: math_util.h:71
dblcmp
static int dblcmp(double d1, double d2)
Definition: math_util.h:191
mod360_positive
static double mod360_positive(double vin)
Definition: math_util.h:118
irand
static int irand(int bound)
Definition: math_util.h:89
randf
static float randf()
Definition: math_util.h:77
mod2pi_positive
static double mod2pi_positive(double vin)
Definition: math_util.h:100
fltcmp
static int fltcmp(float f1, float f2)
Definition: math_util.h:180


apriltag
Author(s): Edwin Olson , Max Krogius
autogenerated on Sun Apr 20 2025 02:08:19