All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
math.h
Go to the documentation of this file.
1 //
2 // Copyright 2016 Pixar
3 //
4 // Licensed under the Apache License, Version 2.0 (the "Apache License")
5 // with the following modification; you may not use this file except in
6 // compliance with the Apache License and the following modification to it:
7 // Section 6. Trademarks. is deleted and replaced with:
8 //
9 // 6. Trademarks. This License does not grant permission to use the trade
10 // names, trademarks, service marks, or product names of the Licensor
11 // and its affiliates, except as required to comply with Section 4(c) of
12 // the License and to reproduce the content of the NOTICE file.
13 //
14 // You may obtain a copy of the Apache License at
15 //
16 // http://www.apache.org/licenses/LICENSE-2.0
17 //
18 // Unless required by applicable law or agreed to in writing, software
19 // distributed under the Apache License with the above modification is
20 // distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
21 // KIND, either express or implied. See the Apache License for the specific
22 // language governing permissions and limitations under the Apache License.
23 //
24 #ifndef GF_MATH_H
25 #define GF_MATH_H
26 
30 
31 #include "pxr/pxr.h"
32 #include "pxr/base/arch/math.h"
33 #include "pxr/base/gf/api.h"
34 
35 PXR_NAMESPACE_OPEN_SCOPE
36 
39 inline bool GfIsClose(double a, double b, double epsilon) {
40  return fabs(a-b) < epsilon;
41 }
42 
45 inline double GfRadiansToDegrees(double radians) {
46  return radians * (180.0 / M_PI);
47 }
48 
51 inline double GfDegreesToRadians(double degrees) {
52  return degrees * (M_PI / 180.0);
53 }
54 
58 template <class T>
59 inline double GfSqr(const T& x) {
60  return x * x;
61 }
62 
69 template <typename T>
70 inline T
71 GfSgn(T v) {
72  return (v < 0) ? -1 : ((v > 0) ? 1 : 0);
73 }
74 
77 inline double GfSqrt(double f) { return std::sqrt(f); }
80 inline float GfSqrt(float f) { return std::sqrt(f); }
81 
84 inline double GfExp(double f) { return std::exp(f); }
87 inline float GfExp(float f) { return std::exp(f); }
88 
91 inline double GfLog(double f) { return std::log(f); }
94 inline float GfLog(float f) { return std::log(f); }
95 
98 inline double GfFloor(double f) { return std::floor(f); }
101 inline float GfFloor(float f) { return std::floor(f); }
102 
105 inline double GfCeil(double f) { return std::ceil(f); }
108 inline float GfCeil(float f) { return std::ceil(f); }
109 
112 inline double GfAbs(double f) { return std::fabs(f); }
115 inline float GfAbs(float f) { return std::fabs(f); }
116 
119 inline double GfRound(double f) { return std::rint(f); }
122 inline float GfRound(float f) { return std::rint(f); }
123 
126 inline double GfPow(double f, double p) { return std::pow(f, p); }
129 inline float GfPow(float f, float p) { return std::pow(f, p); }
130 
133 inline double GfSin(double v) { return std::sin(v); }
136 inline float GfSin(float v) { return std::sin(v); }
139 inline double GfCos(double v) { return std::cos(v); }
142 inline float GfCos(float v) { return std::cos(v); }
145 inline void GfSinCos(double v, double *s, double *c) { ArchSinCos(v, s, c); }
148 inline void GfSinCos(float v, float *s, float *c) { ArchSinCosf(v, s, c); }
149 
153 inline double GfClamp(double value, double min, double max) {
154  if (value < min) return min;
155  if (value > max) return max;
156  return value;
157 }
158 
161 inline float GfClamp(float value, float min, float max) {
162  if (value < min) return min;
163  if (value > max) return max;
164  return value;
165 }
166 
174 GF_API
175 double GfMod(double a, double b);
177 // \ingroup group_gf_BasicMath
178 GF_API
179 float GfMod(float a, float b);
180 
189 template <class T>
190 inline T GfLerp( double alpha, const T& a, const T& b) {
191  return (1-alpha)* a + alpha * b;
192 }
193 
196 template <class T>
197 inline T GfMin(T a1, T a2) {
198  return (a1 < a2 ? a1 : a2);
199 }
200 template <class T>
201 inline T GfMin(T a1, T a2, T a3) {
202  return GfMin(GfMin(a1, a2), a3);
203 }
204 template <class T>
205 inline T GfMin(T a1, T a2, T a3, T a4) {
206  return GfMin(GfMin(a1, a2, a3), a4);
207 }
208 template <class T>
209 inline T GfMin(T a1, T a2, T a3, T a4, T a5) {
210  return GfMin(GfMin(a1, a2, a3, a4), a5);
211 }
212 
215 template <class T>
216 inline T GfMax(T a1, T a2) {
217  return (a1 < a2 ? a2 : a1);
218 }
219 template <class T>
220 inline T GfMax(T a1, T a2, T a3) {
221  return GfMax(GfMax(a1, a2), a3);
222 }
223 template <class T>
224 inline T GfMax(T a1, T a2, T a3, T a4) {
225  return GfMax(GfMax(a1, a2, a3), a4);
226 }
227 template <class T>
228 inline T GfMax(T a1, T a2, T a3, T a4, T a5) {
229  return GfMax(GfMax(a1, a2, a3, a4), a5);
230 }
231 
235 inline float GfDot(float a, float b) {
236  return a * b;
237 }
241 inline double GfDot(double a, double b) {
242  return a * b;
243 }
244 
245 PXR_NAMESPACE_CLOSE_SCOPE
246 
247 #endif // GF_MATH_H
GF_API double GfMod(double a, double b)
The mod function with &quot;correct&quot; behaviour for negative numbers.
double GfCos(double v)
Return cos(v).
Definition: math.h:139
bool GfIsClose(double a, double b, double epsilon)
Returns true if a and b are with epsilon of each other.
Definition: math.h:39
double GfRadiansToDegrees(double radians)
Converts an angle in radians to degrees.
Definition: math.h:45
double GfFloor(double f)
Return floor(f).
Definition: math.h:98
double GfSqr(const T &x)
Returns the inner product of x with itself: specifically, x*x.
Definition: math.h:59
double GfDegreesToRadians(double degrees)
Converts an angle in degrees to radians.
Definition: math.h:51
double GfAbs(double f)
Return abs(f).
Definition: math.h:112
T GfSgn(T v)
Return the signum of v (i.e.
Definition: math.h:71
T GfMin(T a1, T a2)
Returns the smallest of the given values.
Definition: math.h:197
double GfPow(double f, double p)
Return pow(f, p).
Definition: math.h:126
double GfCeil(double f)
Return ceil(f).
Definition: math.h:105
T GfLerp(double alpha, const T &a, const T &b)
Linear interpolation function.
Definition: math.h:190
double GfClamp(double value, double min, double max)
Return the resulting of clamping value to lie between min and max.
Definition: math.h:153
double GfLog(double f)
Return log(f).
Definition: math.h:91
T GfMax(T a1, T a2)
Returns the largest of the given values.
Definition: math.h:216
void ArchSinCosf(float v, float *s, float *c)
Computes the sine and cosine of the specified value as a float.
Definition: math.h:107
double GfSqrt(double f)
Return sqrt(f).
Definition: math.h:77
void GfSinCos(double v, double *s, double *c)
Return sin(v) in s and cos(v) in c.
Definition: math.h:145
double GfExp(double f)
Return exp(f).
Definition: math.h:84
double GfSin(double v)
Return sin(v).
Definition: math.h:133
void ArchSinCos(double v, double *s, double *c)
Computes the sine and cosine of the specified value as a double.
Definition: math.h:110
GfHalf GfDot(GfHalf a, GfHalf b)
Returns the dot (inner) product of two vectors.
Definition: half.h:55
double GfRound(double f)
Return round(f).
Definition: math.h:119