blob: 9271a01834501a311f28525ca460f61117524eab [file] [log] [blame]
Austin Schuh3333ec72022-12-29 16:21:06 -08001/* Copyright (C) 2013-2016, The Regents of The University of Michigan.
2All rights reserved.
3This software was developed in the APRIL Robotics Lab under the
4direction of Edwin Olson, ebolson@umich.edu. This software may be
5available under alternative licensing terms; contact the address above.
6Redistribution and use in source and binary forms, with or without
7modification, are permitted provided that the following conditions are met:
81. Redistributions of source code must retain the above copyright notice, this
9 list of conditions and the following disclaimer.
102. 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.
13THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
14ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
15WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
16DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
17ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
18(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
19LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
20ON 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
22SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
23The views and conclusions contained in the software and documentation are those
24of the authors and should not be interpreted as representing official policies,
25either 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
38extern "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.
56static inline int dequals(double a, double b)
57{
58 double thresh = 1e-9;
59 return (fabs(a-b) < thresh);
60}
61 */
62
63static inline int dequals_mag(double a, double b, double thresh)
64{
65 return (fabs(a-b) < thresh);
66}
67
68static inline int isq(int v)
69{
70 return v*v;
71}
72
73static inline float fsq(float v)
74{
75 return v*v;
76}
77
78static inline double sq(double v)
79{
80 return v*v;
81}
82
83static inline double sgn(double v)
84{
85 return (v>=0) ? 1 : -1;
86}
87
88// random number between [0, 1)
89static inline float randf()
90{
91 return (float)(rand() / (RAND_MAX + 1.0));
92}
93
94
95static inline float signed_randf()
96{
97 return randf()*2 - 1;
98}
99
100// return a random integer between [0, bound)
101static 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
111/** Map vin to [0, 2*PI) **/
112static inline double mod2pi_positive(double vin)
113{
114 return vin - M_TWOPI * floor(vin / M_TWOPI);
115}
116
117/** Map vin to [-PI, PI) **/
118static inline double mod2pi(double vin)
119{
120 return mod2pi_positive(vin + M_PI) - M_PI;
121}
122
123/** Return vin such that it is within PI degrees of ref **/
124static inline double mod2pi_ref(double ref, double vin)
125{
126 return ref + mod2pi(vin - ref);
127}
128
129/** Map vin to [0, 360) **/
130static inline double mod360_positive(double vin)
131{
132 return vin - 360 * floor(vin / 360);
133}
134
135/** Map vin to [-180, 180) **/
136static inline double mod360(double vin)
137{
138 return mod360_positive(vin + 180) - 180;
139}
140
141static inline int mod_positive(int vin, int mod) {
142 return (vin % mod + mod) % mod;
143}
144
145static 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
158static inline int imin(int a, int b)
159{
160 return (a < b) ? a : b;
161}
162
163static inline int imax(int a, int b)
164{
165 return (a > b) ? a : b;
166}
167
168static inline int64_t imin64(int64_t a, int64_t b)
169{
170 return (a < b) ? a : b;
171}
172
173static inline int64_t imax64(int64_t a, int64_t b)
174{
175 return (a > b) ? a : b;
176}
177
178static inline int iclamp(int v, int minv, int maxv)
179{
180 return imax(minv, imin(v, maxv));
181}
182
183static 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
192static 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
203static 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