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#include <typeinfo>
21
22PXR_NAMESPACE_OPEN_SCOPE
23
24class TfType;
25
28inline bool GfIsClose(double a, double b, double epsilon) {
29 return fabs(a-b) < epsilon;
30}
31
34inline double GfRadiansToDegrees(double radians) {
35 return radians * (180.0 / M_PI);
36}
37
40inline double GfDegreesToRadians(double degrees) {
41 return degrees * (M_PI / 180.0);
42}
43
51GF_API
52double GfSmoothStep(double min, double max, double val,
53 double slope0 = 0.0, double slope1 = 0.0);
54
165GF_API
166double GfSmoothRamp(double tmin, double tmax, double t, double w0, double w1);
167
171template <class T>
172inline double GfSqr(const T& x) {
173 return x * x;
174}
175
182template <typename T>
183inline T
184GfSgn(T v) {
185 return (v < 0) ? -1 : ((v > 0) ? 1 : 0);
186}
187
190inline double GfSqrt(double f) { return std::sqrt(f); }
193inline float GfSqrt(float f) { return std::sqrt(f); }
194
197inline double GfExp(double f) { return std::exp(f); }
200inline float GfExp(float f) { return std::exp(f); }
201
204inline double GfLog(double f) { return std::log(f); }
207inline float GfLog(float f) { return std::log(f); }
208
211inline double GfFloor(double f) { return std::floor(f); }
214inline float GfFloor(float f) { return std::floor(f); }
215
218inline double GfCeil(double f) { return std::ceil(f); }
221inline float GfCeil(float f) { return std::ceil(f); }
222
225inline double GfAbs(double f) { return std::fabs(f); }
228inline float GfAbs(float f) { return std::fabs(f); }
229
232inline double GfRound(double f) { return std::rint(f); }
235inline float GfRound(float f) { return std::rint(f); }
236
239inline double GfPow(double f, double p) { return std::pow(f, p); }
242inline float GfPow(float f, float p) { return std::pow(f, p); }
243
246inline double GfSin(double v) { return std::sin(v); }
249inline float GfSin(float v) { return std::sin(v); }
252inline double GfCos(double v) { return std::cos(v); }
255inline float GfCos(float v) { return std::cos(v); }
258inline void GfSinCos(double v, double *s, double *c) { ArchSinCos(v, s, c); }
261inline void GfSinCos(float v, float *s, float *c) { ArchSinCosf(v, s, c); }
262
266inline double GfClamp(double value, double min, double max) {
267 if (value < min) return min;
268 if (value > max) return max;
269 return value;
270}
271
274inline float GfClamp(float value, float min, float max) {
275 if (value < min) return min;
276 if (value > max) return max;
277 return value;
278}
279
287GF_API
288double GfMod(double a, double b);
290// \ingroup group_gf_BasicMath
291GF_API
292float GfMod(float a, float b);
293
302template <class T>
303inline T GfLerp( double alpha, const T& a, const T& b) {
304 return (1-alpha)* a + alpha * b;
305}
306
309template <class T>
310inline T GfMin(T a1, T a2) {
311 return (a1 < a2 ? a1 : a2);
312}
313template <class T>
314inline T GfMin(T a1, T a2, T a3) {
315 return GfMin(GfMin(a1, a2), a3);
316}
317template <class T>
318inline T GfMin(T a1, T a2, T a3, T a4) {
319 return GfMin(GfMin(a1, a2, a3), a4);
320}
321template <class T>
322inline T GfMin(T a1, T a2, T a3, T a4, T a5) {
323 return GfMin(GfMin(a1, a2, a3, a4), a5);
324}
325
328template <class T>
329inline T GfMax(T a1, T a2) {
330 return (a1 < a2 ? a2 : a1);
331}
332template <class T>
333inline T GfMax(T a1, T a2, T a3) {
334 return GfMax(GfMax(a1, a2), a3);
335}
336template <class T>
337inline T GfMax(T a1, T a2, T a3, T a4) {
338 return GfMax(GfMax(a1, a2, a3), a4);
339}
340template <class T>
341inline T GfMax(T a1, T a2, T a3, T a4, T a5) {
342 return GfMax(GfMax(a1, a2, a3, a4), a5);
343}
344
348template <typename Left, typename Right,
349 std::enable_if_t<GfIsArithmetic<Left>::value &&
351inline decltype(std::declval<Left>() * std::declval<Right>())
352GfDot(Left left, Right right) {
353 return left * right;
354}
355
359template <typename Left, typename Right,
360 std::enable_if_t<GfIsArithmetic<Left>::value &&
362inline decltype(std::declval<Left>() * std::declval<Right>())
363GfCompMult(Left left, Right right) {
364 return left * right;
365}
366
370template <typename Left, typename Right,
371 std::enable_if_t<GfIsArithmetic<Left>::value &&
373inline decltype(std::declval<Left>() / std::declval<Right>())
374GfCompDiv(Left left, Right right) {
375 return left / right;
376}
377
378PXR_NAMESPACE_CLOSE_SCOPE
379
380#endif // PXR_BASE_GF_MATH_H
Architecture-specific math function calls.
TfType represents a dynamic runtime type.
Definition type.h:48
void ArchSinCos(double v, double *s, double *c)
Computes the sine and cosine of the specified value as a double.
Definition math.h:98
void ArchSinCosf(float v, float *s, float *c)
Computes the sine and cosine of the specified value as a float.
Definition math.h:95
decltype(std::declval< Left >()/std::declval< Right >()) GfCompDiv(Left left, Right right)
Returns component-wise quotient of vectors.
Definition math.h:374
double GfDegreesToRadians(double degrees)
Converts an angle in degrees to radians.
Definition math.h:40
double GfPow(double f, double p)
Return pow(f, p).
Definition math.h:239
double GfSqr(const T &x)
Returns the inner product of x with itself: specifically, x*x.
Definition math.h:172
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:252
double GfRadiansToDegrees(double radians)
Converts an angle in radians to degrees.
Definition math.h:34
double GfRound(double f)
Return round(f).
Definition math.h:232
double GfFloor(double f)
Return floor(f).
Definition math.h:211
double GfClamp(double value, double min, double max)
Return the resulting of clamping value to lie between min and max.
Definition math.h:266
bool GfIsClose(double a, double b, double epsilon)
Returns true if a and b are with epsilon of each other.
Definition math.h:28
double GfCeil(double f)
Return ceil(f).
Definition math.h:218
T GfMin(T a1, T a2)
Returns the smallest of the given values.
Definition math.h:310
GF_API double GfSmoothStep(double min, double max, double val, double slope0=0.0, double slope1=0.0)
Smooth step function using a cubic hermite blend.
void GfSinCos(double v, double *s, double *c)
Return sin(v) in s and cos(v) in c.
Definition math.h:258
double GfSqrt(double f)
Return sqrt(f).
Definition math.h:190
double GfLog(double f)
Return log(f).
Definition math.h:204
T GfMax(T a1, T a2)
Returns the largest of the given values.
Definition math.h:329
decltype(std::declval< Left >() *std::declval< Right >()) GfDot(Left left, Right right)
Returns the dot (inner) product of two vectors.
Definition math.h:352
T GfLerp(double alpha, const T &a, const T &b)
Linear interpolation function.
Definition math.h:303
double GfAbs(double f)
Return abs(f).
Definition math.h:225
GF_API double GfSmoothRamp(double tmin, double tmax, double t, double w0, double w1)
Smooth Step with independently controllable shoulders.
T GfSgn(T v)
Return the signum of v (i.e.
Definition math.h:184
double GfSin(double v)
Return sin(v).
Definition math.h:246
double GfExp(double f)
Return exp(f).
Definition math.h:197
decltype(std::declval< Left >() *std::declval< Right >()) GfCompMult(Left left, Right right)
Returns component-wise multiplication of vectors.
Definition math.h:363
A metafunction which is equivalent to std::arithmetic but also includes any specializations from GfIs...
Definition traits.h:55