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 PXR_BASE_GF_MATH_H
25 #define PXR_BASE_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 #include "pxr/base/gf/traits.h"
35 
36 #include <type_traits>
37 
38 PXR_NAMESPACE_OPEN_SCOPE
39 
42 inline bool GfIsClose(double a, double b, double epsilon) {
43  return fabs(a-b) < epsilon;
44 }
45 
48 inline double GfRadiansToDegrees(double radians) {
49  return radians * (180.0 / M_PI);
50 }
51 
54 inline double GfDegreesToRadians(double degrees) {
55  return degrees * (M_PI / 180.0);
56 }
57 
61 template <class T>
62 inline double GfSqr(const T& x) {
63  return x * x;
64 }
65 
72 template <typename T>
73 inline T
74 GfSgn(T v) {
75  return (v < 0) ? -1 : ((v > 0) ? 1 : 0);
76 }
77 
80 inline double GfSqrt(double f) { return std::sqrt(f); }
83 inline float GfSqrt(float f) { return std::sqrt(f); }
84 
87 inline double GfExp(double f) { return std::exp(f); }
90 inline float GfExp(float f) { return std::exp(f); }
91 
94 inline double GfLog(double f) { return std::log(f); }
97 inline float GfLog(float f) { return std::log(f); }
98 
101 inline double GfFloor(double f) { return std::floor(f); }
104 inline float GfFloor(float f) { return std::floor(f); }
105 
108 inline double GfCeil(double f) { return std::ceil(f); }
111 inline float GfCeil(float f) { return std::ceil(f); }
112 
115 inline double GfAbs(double f) { return std::fabs(f); }
118 inline float GfAbs(float f) { return std::fabs(f); }
119 
122 inline double GfRound(double f) { return std::rint(f); }
125 inline float GfRound(float f) { return std::rint(f); }
126 
129 inline double GfPow(double f, double p) { return std::pow(f, p); }
132 inline float GfPow(float f, float p) { return std::pow(f, p); }
133 
136 inline double GfSin(double v) { return std::sin(v); }
139 inline float GfSin(float v) { return std::sin(v); }
142 inline double GfCos(double v) { return std::cos(v); }
145 inline float GfCos(float v) { return std::cos(v); }
148 inline void GfSinCos(double v, double *s, double *c) { ArchSinCos(v, s, c); }
151 inline void GfSinCos(float v, float *s, float *c) { ArchSinCosf(v, s, c); }
152 
156 inline double GfClamp(double value, double min, double max) {
157  if (value < min) return min;
158  if (value > max) return max;
159  return value;
160 }
161 
164 inline float GfClamp(float value, float min, float max) {
165  if (value < min) return min;
166  if (value > max) return max;
167  return value;
168 }
169 
177 GF_API
178 double GfMod(double a, double b);
180 // \ingroup group_gf_BasicMath
181 GF_API
182 float GfMod(float a, float b);
183 
192 template <class T>
193 inline T GfLerp( double alpha, const T& a, const T& b) {
194  return (1-alpha)* a + alpha * b;
195 }
196 
199 template <class T>
200 inline T GfMin(T a1, T a2) {
201  return (a1 < a2 ? a1 : a2);
202 }
203 template <class T>
204 inline T GfMin(T a1, T a2, T a3) {
205  return GfMin(GfMin(a1, a2), a3);
206 }
207 template <class T>
208 inline T GfMin(T a1, T a2, T a3, T a4) {
209  return GfMin(GfMin(a1, a2, a3), a4);
210 }
211 template <class T>
212 inline T GfMin(T a1, T a2, T a3, T a4, T a5) {
213  return GfMin(GfMin(a1, a2, a3, a4), a5);
214 }
215 
218 template <class T>
219 inline T GfMax(T a1, T a2) {
220  return (a1 < a2 ? a2 : a1);
221 }
222 template <class T>
223 inline T GfMax(T a1, T a2, T a3) {
224  return GfMax(GfMax(a1, a2), a3);
225 }
226 template <class T>
227 inline T GfMax(T a1, T a2, T a3, T a4) {
228  return GfMax(GfMax(a1, a2, a3), a4);
229 }
230 template <class T>
231 inline T GfMax(T a1, T a2, T a3, T a4, T a5) {
232  return GfMax(GfMax(a1, a2, a3, a4), a5);
233 }
234 
238 template <typename Left, typename Right,
239  std::enable_if_t<GfIsArithmetic<Left>::value &&
241 inline decltype(std::declval<Left>() * std::declval<Right>())
242 GfDot(Left left, Right right) {
243  return left * right;
244 }
245 
249 template <typename Left, typename Right,
250  std::enable_if_t<GfIsArithmetic<Left>::value &&
252 inline decltype(std::declval<Left>() * std::declval<Right>())
253 GfCompMult(Left left, Right right) {
254  return left * right;
255 }
256 
260 template <typename Left, typename Right,
261  std::enable_if_t<GfIsArithmetic<Left>::value &&
263 inline decltype(std::declval<Left>() / std::declval<Right>())
264 GfCompDiv(Left left, Right right) {
265  return left / right;
266 }
267 
268 
269 PXR_NAMESPACE_CLOSE_SCOPE
270 
271 #endif // PXR_BASE_GF_MATH_H
GF_API double GfMod(double a, double b)
The mod function with "correct" behaviour for negative numbers.
Architecture-specific math function calls.
double GfCos(double v)
Return cos(v).
Definition: math.h:142
bool GfIsClose(double a, double b, double epsilon)
Returns true if a and b are with epsilon of each other.
Definition: math.h:42
double GfRadiansToDegrees(double radians)
Converts an angle in radians to degrees.
Definition: math.h:48
decltype(std::declval< Left >() *std::declval< Right >()) GfCompMult(Left left, Right right)
Returns component-wise multiplication of vectors.
Definition: math.h:253
double GfFloor(double f)
Return floor(f).
Definition: math.h:101
double GfSqr(const T &x)
Returns the inner product of x with itself: specifically, x*x.
Definition: math.h:62
double GfDegreesToRadians(double degrees)
Converts an angle in degrees to radians.
Definition: math.h:54
double GfAbs(double f)
Return abs(f).
Definition: math.h:115
T GfSgn(T v)
Return the signum of v (i.e.
Definition: math.h:74
T GfMin(T a1, T a2)
Returns the smallest of the given values.
Definition: math.h:200
double GfPow(double f, double p)
Return pow(f, p).
Definition: math.h:129
double GfCeil(double f)
Return ceil(f).
Definition: math.h:108
T GfLerp(double alpha, const T &a, const T &b)
Linear interpolation function.
Definition: math.h:193
double GfClamp(double value, double min, double max)
Return the resulting of clamping value to lie between min and max.
Definition: math.h:156
decltype(std::declval< Left >()/std::declval< Right >()) GfCompDiv(Left left, Right right)
Returns component-wise quotient of vectors.
Definition: math.h:264
double GfLog(double f)
Return log(f).
Definition: math.h:94
decltype(std::declval< Left >() *std::declval< Right >()) GfDot(Left left, Right right)
Returns the dot (inner) product of two vectors.
Definition: math.h:242
T GfMax(T a1, T a2)
Returns the largest of the given values.
Definition: math.h:219
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:80
void GfSinCos(double v, double *s, double *c)
Return sin(v) in s and cos(v) in c.
Definition: math.h:148
double GfExp(double f)
Return exp(f).
Definition: math.h:87
A metafunction which is equivalent to std::arithmetic but also includes any specializations from GfIs...
Definition: traits.h:62
double GfSin(double v)
Return sin(v).
Definition: math.h:136
void ArchSinCos(double v, double *s, double *c)
Computes the sine and cosine of the specified value as a double.
Definition: math.h:110
double GfRound(double f)
Return round(f).
Definition: math.h:122