math_util.h
Go to the documentation of this file.
1 /**************************************************************************************************
2  Software License Agreement (BSD License)
3 
4  Copyright (c) 2011-2013, LAR toolkit developers - University of Aveiro - http://lars.mec.ua.pt
5  All rights reserved.
6 
7  Redistribution and use in source and binary forms, with or without modification, are permitted
8  provided that the following conditions are met:
9 
10  *Redistributions of source code must retain the above copyright notice, this list of
11  conditions and the following disclaimer.
12  *Redistributions in binary form must reproduce the above copyright notice, this list of
13  conditions and the following disclaimer in the documentation and/or other materials provided
14  with the distribution.
15  *Neither the name of the University of Aveiro nor the names of its contributors may be used to
16  endorse or promote products derived from this software without specific prior written permission.
17 
18  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR
19  IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
20  FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
21  CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22  DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23  DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
24  IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
25  OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 ***************************************************************************************************/
33 #ifndef _MATHUTIL_H
34 #define _MATHUTIL_H
35 
36 #include <math.h>
37 #include <stdlib.h>
38 #include <stdint.h>
39 #include <assert.h>
40 
41 #ifndef PI
42 #define PI 3.14159265358979323846264338
43 #endif
44 
45 #define to_radians(x) ( (x) * (PI / 180.0 ))
46 #define to_degrees(x) ( (x) * (180.0 / M_PI ))
47 
48 static inline double sq(double v)
49 {
50  return v*v;
51 }
52 
53 static inline double sgn(double v)
54 {
55  return (v>=0) ? 1 : -1;
56 }
57 
58 // random number between [0, 1)
59 static inline float randf()
60 {
61  return ((float) rand()) / (RAND_MAX + 1.0);
62 }
63 
64 static inline float signed_randf()
65 {
66  return randf()*2 - 1;
67 }
68 
69 // return a random integer between [0, bound)
70 static inline int irand(int bound)
71 {
72  int v = (int) (randf()*bound);
73  assert(v >= 0);
74  assert(v < bound);
75  return v;
76 }
77 
78 #define TWOPI_INV (0.5/PI)
79 #define TWOPI (2*PI)
80 
82 static inline double mod2pi_positive(double vin)
83 {
84  double q = vin * TWOPI_INV + 0.5;
85  int qi = (int) q;
86 
87  return vin - qi*TWOPI;
88 }
89 
91 static inline double mod2pi(double vin)
92 {
93  if (vin < 0)
94  return -mod2pi_positive(-vin);
95  else
96  return mod2pi_positive(vin);
97 }
98 
100 static inline double mod2pi_ref(double ref, double vin)
101 {
102  return ref + mod2pi(vin - ref);
103 }
104 
105 static inline int theta_to_int(double theta, int max)
106 {
107  theta = mod2pi_ref(PI, theta);
108  int v = (int) (theta / ( 2 * PI ) * max);
109 
110  if (v==max)
111  v = 0;
112 
113  assert (v >= 0 && v < max);
114 
115  return v;
116 }
117 
118 static inline int imin(int a, int b)
119 {
120  return (a < b) ? a : b;
121 }
122 
123 static inline int imax(int a, int b)
124 {
125  return (a > b) ? a : b;
126 }
127 
128 static inline int64_t imin64(int64_t a, int64_t b)
129 {
130  return (a < b) ? a : b;
131 }
132 
133 static inline int64_t imax64(int64_t a, int64_t b)
134 {
135  return (a > b) ? a : b;
136 }
137 
138 static inline int iclamp(int v, int minv, int maxv)
139 {
140  return imax(minv, imin(v, maxv));
141 }
142 
143 static inline double fclamp(double v, double minv, double maxv)
144 {
145  return fmax(minv, fmin(v, maxv));
146 }
147 
148 #endif
149 
static int iclamp(int v, int minv, int maxv)
Definition: math_util.h:138
static double fclamp(double v, double minv, double maxv)
Definition: math_util.h:143
#define PI
Definition: math_util.h:42
static int irand(int bound)
Definition: math_util.h:70
static int imax(int a, int b)
Definition: math_util.h:123
static double sgn(double v)
Definition: math_util.h:53
static double mod2pi(double vin)
Definition: math_util.h:91
static double sq(double v)
Definition: math_util.h:48
static int imin(int a, int b)
Definition: math_util.h:118
static float randf()
Definition: math_util.h:59
static float signed_randf()
Definition: math_util.h:64
#define TWOPI
Definition: math_util.h:79
static int64_t imin64(int64_t a, int64_t b)
Definition: math_util.h:128
static double mod2pi_ref(double ref, double vin)
Definition: math_util.h:100
#define TWOPI_INV
Definition: math_util.h:78
static int64_t imax64(int64_t a, int64_t b)
Definition: math_util.h:133
static int theta_to_int(double theta, int max)
Definition: math_util.h:105
static double mod2pi_positive(double vin)
Definition: math_util.h:82


mit_darpa_logs_player
Author(s): Miguel Oliveira
autogenerated on Mon Mar 2 2015 01:32:15