Loading...
Searching...
No Matches
math.h
Go to the documentation of this file.
1//
2// Copyright 2016 Pixar
3//
4// Licensed under the terms set forth in the LICENSE.txt file available at
5// https://openusd.org/license.
6//
7#ifndef PXR_BASE_GF_MATH_H
8#define PXR_BASE_GF_MATH_H
9
13
14#include "pxr/pxr.h"
15#include "pxr/base/arch/math.h"
16#include "pxr/base/gf/api.h"
17#include "pxr/base/gf/traits.h"
18
19#include <type_traits>
20
21PXR_NAMESPACE_OPEN_SCOPE
22
25inline bool GfIsClose(double a, double b, double epsilon) {
26 return fabs(a-b) < epsilon;
27}
28
31inline double GfRadiansToDegrees(double radians) {
32 return radians * (180.0 / M_PI);
33}
34
37inline double GfDegreesToRadians(double degrees) {
38 return degrees * (M_PI / 180.0);
39}
40
44template <class T>
45inline double GfSqr(const T& x) {
46 return x * x;
47}
48
55template <typename T>
56inline T
57GfSgn(T v) {
58 return (v < 0) ? -1 : ((v > 0) ? 1 : 0);
59}
60
63inline double GfSqrt(double f) { return std::sqrt(f); }
66inline float GfSqrt(float f) { return std::sqrt(f); }
67
70inline double GfExp(double f) { return std::exp(f); }
73inline float GfExp(float f) { return std::exp(f); }
74
77inline double GfLog(double f) { return std::log(f); }
80inline float GfLog(float f) { return std::log(f); }
81
84inline double GfFloor(double f) { return std::floor(f); }
87inline float GfFloor(float f) { return std::floor(f); }
88
91inline double GfCeil(double f) { return std::ceil(f); }
94inline float GfCeil(float f) { return std::ceil(f); }
95
98inline double GfAbs(double f) { return std::fabs(f); }
101inline float GfAbs(float f) { return std::fabs(f); }
102
105inline double GfRound(double f) { return std::rint(f); }
108inline float GfRound(float f) { return std::rint(f); }
109
112inline double GfPow(double f, double p) { return std::pow(f, p); }
115inline float GfPow(float f, float p) { return std::pow(f, p); }
116
119inline double GfSin(double v) { return std::sin(v); }
122inline float GfSin(float v) { return std::sin(v); }
125inline double GfCos(double v) { return std::cos(v); }
128inline float GfCos(float v) { return std::cos(v); }
131inline void GfSinCos(double v, double *s, double *c) { ArchSinCos(v, s, c); }
134inline void GfSinCos(float v, float *s, float *c) { ArchSinCosf(v, s, c); }
135
139inline double GfClamp(double value, double min, double max) {
140 if (value < min) return min;
141 if (value > max) return max;
142 return value;
143}
144
147inline float GfClamp(float value, float min, float max) {
148 if (value < min) return min;
149 if (value > max) return max;
150 return value;
151}
152
160GF_API
161double GfMod(double a, double b);
163// \ingroup group_gf_BasicMath
164GF_API
165float GfMod(float a, float b);
166
175template <class T>
176inline T GfLerp( double alpha, const T& a, const T& b) {
177 return (1-alpha)* a + alpha * b;
178}
179
182template <class T>
183inline T GfMin(T a1, T a2) {
184 return (a1 < a2 ? a1 : a2);
185}
186template <class T>
187inline T GfMin(T a1, T a2, T a3) {
188 return GfMin(GfMin(a1, a2), a3);
189}
190template <class T>
191inline T GfMin(T a1, T a2, T a3, T a4) {
192 return GfMin(GfMin(a1, a2, a3), a4);
193}
194template <class T>
195inline T GfMin(T a1, T a2, T a3, T a4, T a5) {
196 return GfMin(GfMin(a1, a2, a3, a4), a5);
197}
198
201template <class T>
202inline T GfMax(T a1, T a2) {
203 return (a1 < a2 ? a2 : a1);
204}
205template <class T>
206inline T GfMax(T a1, T a2, T a3) {
207 return GfMax(GfMax(a1, a2), a3);
208}
209template <class T>
210inline T GfMax(T a1, T a2, T a3, T a4) {
211 return GfMax(GfMax(a1, a2, a3), a4);
212}
213template <class T>
214inline T GfMax(T a1, T a2, T a3, T a4, T a5) {
215 return GfMax(GfMax(a1, a2, a3, a4), a5);
216}
217
221template <typename Left, typename Right,
222 std::enable_if_t<GfIsArithmetic<Left>::value &&
224inline decltype(std::declval<Left>() * std::declval<Right>())
225GfDot(Left left, Right right) {
226 return left * right;
227}
228
232template <typename Left, typename Right,
233 std::enable_if_t<GfIsArithmetic<Left>::value &&
235inline decltype(std::declval<Left>() * std::declval<Right>())
236GfCompMult(Left left, Right right) {
237 return left * right;
238}
239
243template <typename Left, typename Right,
244 std::enable_if_t<GfIsArithmetic<Left>::value &&
246inline decltype(std::declval<Left>() / std::declval<Right>())
247GfCompDiv(Left left, Right right) {
248 return left / right;
249}
250
251
252PXR_NAMESPACE_CLOSE_SCOPE
253
254#endif // PXR_BASE_GF_MATH_H
Architecture-specific math function calls.
void ArchSinCos(double v, double *s, double *c)
Computes the sine and cosine of the specified value as a double.
Definition: math.h:97
void ArchSinCosf(float v, float *s, float *c)
Computes the sine and cosine of the specified value as a float.
Definition: math.h:94
decltype(std::declval< Left >()/std::declval< Right >()) GfCompDiv(Left left, Right right)
Returns component-wise quotient of vectors.
Definition: math.h:247
double GfDegreesToRadians(double degrees)
Converts an angle in degrees to radians.
Definition: math.h:37
double GfPow(double f, double p)
Return pow(f, p).
Definition: math.h:112
double GfSqr(const T &x)
Returns the inner product of x with itself: specifically, x*x.
Definition: math.h:45
GF_API double GfMod(double a, double b)
The mod function with "correct" behaviour for negative numbers.
double GfCos(double v)
Return cos(v).
Definition: math.h:125
double GfRadiansToDegrees(double radians)
Converts an angle in radians to degrees.
Definition: math.h:31
double GfRound(double f)
Return round(f).
Definition: math.h:105
double GfFloor(double f)
Return floor(f).
Definition: math.h:84
double GfClamp(double value, double min, double max)
Return the resulting of clamping value to lie between min and max.
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:25
double GfCeil(double f)
Return ceil(f).
Definition: math.h:91
T GfMin(T a1, T a2)
Returns the smallest of the given values.
Definition: math.h:183
void GfSinCos(double v, double *s, double *c)
Return sin(v) in s and cos(v) in c.
Definition: math.h:131
double GfSqrt(double f)
Return sqrt(f).
Definition: math.h:63
double GfLog(double f)
Return log(f).
Definition: math.h:77
T GfMax(T a1, T a2)
Returns the largest of the given values.
Definition: math.h:202
decltype(std::declval< Left >() *std::declval< Right >()) GfDot(Left left, Right right)
Returns the dot (inner) product of two vectors.
Definition: math.h:225
T GfLerp(double alpha, const T &a, const T &b)
Linear interpolation function.
Definition: math.h:176
double GfAbs(double f)
Return abs(f).
Definition: math.h:98
T GfSgn(T v)
Return the signum of v (i.e.
Definition: math.h:57
double GfSin(double v)
Return sin(v).
Definition: math.h:119
double GfExp(double f)
Return exp(f).
Definition: math.h:70
decltype(std::declval< Left >() *std::declval< Right >()) GfCompMult(Left left, Right right)
Returns component-wise multiplication of vectors.
Definition: math.h:236
A metafunction which is equivalent to std::arithmetic but also includes any specializations from GfIs...
Definition: traits.h:51