WPILibC++ 2025.1.1
Loading...
Searching...
No Matches
math_util.h
Go to the documentation of this file.
1/* 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#define _USE_MATH_DEFINES
31#include <math.h>
32#include <stdlib.h>
33#include <stdint.h>
34#include <assert.h>
35
36#ifdef __cplusplus
37extern "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.
44static inline int dequals(double a, double b)
45{
46 double thresh = 1e-9;
47 return (fabs(a-b) < thresh);
48}
49 */
50
51static inline int dequals_mag(double a, double b, double thresh)
52{
53 return (fabs(a-b) < thresh);
54}
55
56static inline int isq(int v)
57{
58 return v*v;
59}
60
61static inline float fsq(float v)
62{
63 return v*v;
64}
65
66static inline double sq(double v)
67{
68 return v*v;
69}
70
71static inline double sgn(double v)
72{
73 return (v>=0) ? 1 : -1;
74}
75
76// random number between [0, 1)
77static inline float randf(void)
78{
79 return (float)(rand() / (RAND_MAX + 1.0));
80}
81
82
83static inline float signed_randf(void)
84{
85 return randf()*2 - 1;
86}
87
88// return a random integer between [0, bound)
89static 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
99/** Map vin to [0, 2*PI) **/
100static inline double mod2pi_positive(double vin)
101{
102 return vin - M_2_PI * floor(vin / M_2_PI);
103}
104
105/** Map vin to [-PI, PI) **/
106static inline double mod2pi(double vin)
107{
108 return mod2pi_positive(vin + M_PI) - M_PI;
109}
110
111/** Return vin such that it is within PI degrees of ref **/
112static inline double mod2pi_ref(double ref, double vin)
113{
114 return ref + mod2pi(vin - ref);
115}
116
117/** Map vin to [0, 360) **/
118static inline double mod360_positive(double vin)
119{
120 return vin - 360 * floor(vin / 360);
121}
122
123/** Map vin to [-180, 180) **/
124static inline double mod360(double vin)
125{
126 return mod360_positive(vin + 180) - 180;
127}
128
129static inline int mod_positive(int vin, int mod) {
130 return (vin % mod + mod) % mod;
131}
132
133static 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
146static inline int imin(int a, int b)
147{
148 return (a < b) ? a : b;
149}
150
151static inline int imax(int a, int b)
152{
153 return (a > b) ? a : b;
154}
155
156static inline int64_t imin64(int64_t a, int64_t b)
157{
158 return (a < b) ? a : b;
159}
160
161static inline int64_t imax64(int64_t a, int64_t b)
162{
163 return (a > b) ? a : b;
164}
165
166static inline int iclamp(int v, int minv, int maxv)
167{
168 return imax(minv, imin(v, maxv));
169}
170
171static 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
180static 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
191static 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
static double mod2pi_positive(double vin)
Map vin to [0, 2*PI)
Definition math_util.h:100
static int fltcmp(float f1, float f2)
Definition math_util.h:180
static float fsq(float v)
Definition math_util.h:61
static float signed_randf(void)
Definition math_util.h:83
static double mod360_positive(double vin)
Map vin to [0, 360)
Definition math_util.h:118
static double dclamp(double a, double min, double max)
Definition math_util.h:171
static double mod2pi_ref(double ref, double vin)
Return vin such that it is within PI degrees of ref.
Definition math_util.h:112
static double mod360(double vin)
Map vin to [-180, 180)
Definition math_util.h:124
static int theta_to_int(double theta, int max)
Definition math_util.h:133
static int isq(int v)
Definition math_util.h:56
static float randf(void)
Definition math_util.h:77
static int mod_positive(int vin, int mod)
Definition math_util.h:129
static int64_t imin64(int64_t a, int64_t b)
Definition math_util.h:156
static double sgn(double v)
Definition math_util.h:71
static int dequals_mag(double a, double b, double thresh)
Definition math_util.h:51
static int imin(int a, int b)
Definition math_util.h:146
static int irand(int bound)
Definition math_util.h:89
static int64_t imax64(int64_t a, int64_t b)
Definition math_util.h:161
static double sq(double v)
Definition math_util.h:66
static double mod2pi(double vin)
Map vin to [-PI, PI)
Definition math_util.h:106
static int dblcmp(double d1, double d2)
Definition math_util.h:191
static int iclamp(int v, int minv, int maxv)
Definition math_util.h:166
static int imax(int a, int b)
Definition math_util.h:151