This document is for a version of USD that is under development. See this page for the current release.
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
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
48GF_API
49double GfSmoothStep(double min, double max, double val,
50 double slope0 = 0.0, double slope1 = 0.0);
51
162GF_API
163double GfSmoothRamp(double tmin, double tmax, double t, double w0, double w1);
164
168template <class T>
169inline double GfSqr(const T& x) {
170 return x * x;
171}
172
179template <typename T>
180inline T
181GfSgn(T v) {
182 return (v < 0) ? -1 : ((v > 0) ? 1 : 0);
183}
184
187inline double GfSqrt(double f) { return std::sqrt(f); }
190inline float GfSqrt(float f) { return std::sqrt(f); }
191
194inline double GfExp(double f) { return std::exp(f); }
197inline float GfExp(float f) { return std::exp(f); }
198
201inline double GfLog(double f) { return std::log(f); }
204inline float GfLog(float f) { return std::log(f); }
205
208inline double GfFloor(double f) { return std::floor(f); }
211inline float GfFloor(float f) { return std::floor(f); }
212
215inline double GfCeil(double f) { return std::ceil(f); }
218inline float GfCeil(float f) { return std::ceil(f); }
219
222inline double GfAbs(double f) { return std::fabs(f); }
225inline float GfAbs(float f) { return std::fabs(f); }
226
229inline double GfRound(double f) { return std::rint(f); }
232inline float GfRound(float f) { return std::rint(f); }
233
236inline double GfPow(double f, double p) { return std::pow(f, p); }
239inline float GfPow(float f, float p) { return std::pow(f, p); }
240
243inline double GfSin(double v) { return std::sin(v); }
246inline float GfSin(float v) { return std::sin(v); }
249inline double GfCos(double v) { return std::cos(v); }
252inline float GfCos(float v) { return std::cos(v); }
255inline void GfSinCos(double v, double *s, double *c) { ArchSinCos(v, s, c); }
258inline void GfSinCos(float v, float *s, float *c) { ArchSinCosf(v, s, c); }
259
263inline double GfClamp(double value, double min, double max) {
264 if (value < min) return min;
265 if (value > max) return max;
266 return value;
267}
268
271inline float GfClamp(float value, float min, float max) {
272 if (value < min) return min;
273 if (value > max) return max;
274 return value;
275}
276
284GF_API
285double GfMod(double a, double b);
287// \ingroup group_gf_BasicMath
288GF_API
289float GfMod(float a, float b);
290
299template <class T>
300inline T GfLerp( double alpha, const T& a, const T& b) {
301 return (1-alpha)* a + alpha * b;
302}
303
306template <class T>
307inline T GfMin(T a1, T a2) {
308 return (a1 < a2 ? a1 : a2);
309}
310template <class T>
311inline T GfMin(T a1, T a2, T a3) {
312 return GfMin(GfMin(a1, a2), a3);
313}
314template <class T>
315inline T GfMin(T a1, T a2, T a3, T a4) {
316 return GfMin(GfMin(a1, a2, a3), a4);
317}
318template <class T>
319inline T GfMin(T a1, T a2, T a3, T a4, T a5) {
320 return GfMin(GfMin(a1, a2, a3, a4), a5);
321}
322
325template <class T>
326inline T GfMax(T a1, T a2) {
327 return (a1 < a2 ? a2 : a1);
328}
329template <class T>
330inline T GfMax(T a1, T a2, T a3) {
331 return GfMax(GfMax(a1, a2), a3);
332}
333template <class T>
334inline T GfMax(T a1, T a2, T a3, T a4) {
335 return GfMax(GfMax(a1, a2, a3), a4);
336}
337template <class T>
338inline T GfMax(T a1, T a2, T a3, T a4, T a5) {
339 return GfMax(GfMax(a1, a2, a3, a4), a5);
340}
341
345template <typename Left, typename Right,
346 std::enable_if_t<GfIsArithmetic<Left>::value &&
348inline decltype(std::declval<Left>() * std::declval<Right>())
349GfDot(Left left, Right right) {
350 return left * right;
351}
352
356template <typename Left, typename Right,
357 std::enable_if_t<GfIsArithmetic<Left>::value &&
359inline decltype(std::declval<Left>() * std::declval<Right>())
360GfCompMult(Left left, Right right) {
361 return left * right;
362}
363
367template <typename Left, typename Right,
368 std::enable_if_t<GfIsArithmetic<Left>::value &&
370inline decltype(std::declval<Left>() / std::declval<Right>())
371GfCompDiv(Left left, Right right) {
372 return left / right;
373}
374
375
376PXR_NAMESPACE_CLOSE_SCOPE
377
378#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:371
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:236
double GfSqr(const T &x)
Returns the inner product of x with itself: specifically, x*x.
Definition: math.h:169
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:249
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:229
double GfFloor(double f)
Return floor(f).
Definition: math.h:208
double GfClamp(double value, double min, double max)
Return the resulting of clamping value to lie between min and max.
Definition: math.h:263
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:215
T GfMin(T a1, T a2)
Returns the smallest of the given values.
Definition: math.h:307
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:255
double GfSqrt(double f)
Return sqrt(f).
Definition: math.h:187
double GfLog(double f)
Return log(f).
Definition: math.h:201
T GfMax(T a1, T a2)
Returns the largest of the given values.
Definition: math.h:326
decltype(std::declval< Left >() *std::declval< Right >()) GfDot(Left left, Right right)
Returns the dot (inner) product of two vectors.
Definition: math.h:349
T GfLerp(double alpha, const T &a, const T &b)
Linear interpolation function.
Definition: math.h:300
double GfAbs(double f)
Return abs(f).
Definition: math.h:222
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:181
double GfSin(double v)
Return sin(v).
Definition: math.h:243
double GfExp(double f)
Return exp(f).
Definition: math.h:194
decltype(std::declval< Left >() *std::declval< Right >()) GfCompMult(Left left, Right right)
Returns component-wise multiplication of vectors.
Definition: math.h:360
A metafunction which is equivalent to std::arithmetic but also includes any specializations from GfIs...
Definition: traits.h:51