HDK
 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 
27 /// \file gf/math.h
28 /// \ingroup group_gf_BasicMath
29 /// Assorted mathematical utility functions.
30 
31 #include "pxr/pxr.h"
32 #include "pxr/base/arch/math.h"
33 #include "pxr/base/gf/api.h"
34 
36 
37 /// Returns true if \p a and \p b are with \p epsilon of each other.
38 /// \ingroup group_gf_BasicMath
39 inline bool GfIsClose(double a, double b, double epsilon) {
40  return fabs(a-b) < epsilon;
41 }
42 
43 /// Converts an angle in radians to degrees.
44 /// \ingroup group_gf_BasicMath
45 inline double GfRadiansToDegrees(double radians) {
46  return radians * (180.0 / M_PI);
47 }
48 
49 /// Converts an angle in degrees to radians.
50 /// \ingroup group_gf_BasicMath
51 inline double GfDegreesToRadians(double degrees) {
52  return degrees * (M_PI / 180.0);
53 }
54 
55 /// Returns the inner product of \c x with itself: specifically, \c x*x.
56 /// Defined for \c int, \c float, \c double, and all \c GfVec types.
57 /// \ingroup group_gf_BasicMath
58 template <class T>
59 inline double GfSqr(const T& x) {
60  return x * x;
61 }
62 
63 /// Return the signum of \p v (i.e. -1, 0, or 1).
64 ///
65 /// The type \c T must implement the < and > operators; the function returns
66 /// zero only if value neither positive, nor negative.
67 ///
68 /// \ingroup group_gf_BasicMath
69 template <typename T>
70 inline T
71 GfSgn(T v) {
72  return (v < 0) ? -1 : ((v > 0) ? 1 : 0);
73 }
74 
75 /// Return sqrt(\p f).
76 /// \ingroup group_gf_BasicMath
77 inline double GfSqrt(double f) { return std::sqrt(f); }
78 /// Return sqrt(\p f).
79 /// \ingroup group_gf_BasicMath
80 inline float GfSqrt(float f) { return std::sqrt(f); }
81 
82 /// Return exp(\p f).
83 /// \ingroup group_gf_BasicMath
84 inline double GfExp(double f) { return std::exp(f); }
85 /// Return exp(\p f).
86 /// \ingroup group_gf_BasicMath
87 inline float GfExp(float f) { return std::exp(f); }
88 
89 /// Return log(\p f).
90 /// \ingroup group_gf_BasicMath
91 inline double GfLog(double f) { return std::log(f); }
92 /// Return log(\p f).
93 /// \ingroup group_gf_BasicMath
94 inline float GfLog(float f) { return std::log(f); }
95 
96 /// Return floor(\p f).
97 /// \ingroup group_gf_BasicMath
98 inline double GfFloor(double f) { return std::floor(f); }
99 /// Return floor(\p f).
100 /// \ingroup group_gf_BasicMath
101 inline float GfFloor(float f) { return std::floor(f); }
102 
103 /// Return ceil(\p f).
104 /// \ingroup group_gf_BasicMath
105 inline double GfCeil(double f) { return std::ceil(f); }
106 /// Return ceil(\p f).
107 /// \ingroup group_gf_BasicMath
108 inline float GfCeil(float f) { return std::ceil(f); }
109 
110 /// Return abs(\p f).
111 /// \ingroup group_gf_BasicMath
112 inline double GfAbs(double f) { return std::fabs(f); }
113 /// Return abs(\p f).
114 /// \ingroup group_gf_BasicMath
115 inline float GfAbs(float f) { return std::fabs(f); }
116 
117 /// Return round(\p f).
118 /// \ingroup group_gf_BasicMath
119 inline double GfRound(double f) { return std::rint(f); }
120 /// Return round(\p f).
121 /// \ingroup group_gf_BasicMath
122 inline float GfRound(float f) { return std::rint(f); }
123 
124 /// Return pow(\p f, \p p).
125 /// \ingroup group_gf_BasicMath
126 inline double GfPow(double f, double p) { return std::pow(f, p); }
127 /// Return pow(\p f, \p p).
128 /// \ingroup group_gf_BasicMath
129 inline float GfPow(float f, float p) { return std::pow(f, p); }
130 
131 /// Return sin(\p v).
132 /// \ingroup group_gf_BasicMath
133 inline double GfSin(double v) { return std::sin(v); }
134 /// Return sin(\p v).
135 /// \ingroup group_gf_BasicMath
136 inline float GfSin(float v) { return std::sin(v); }
137 /// Return cos(\p v).
138 /// \ingroup group_gf_BasicMath
139 inline double GfCos(double v) { return std::cos(v); }
140 /// Return cos(\p v).
141 /// \ingroup group_gf_BasicMath
142 inline float GfCos(float v) { return std::cos(v); }
143 /// Return sin(\p v) in \p s and cos(\p v) in \p c.
144 /// \ingroup group_gf_BasicMath
145 inline void GfSinCos(double v, double *s, double *c) { ArchSinCos(v, s, c); }
146 /// Return sin(\p v) in \p s and cos(\p v) in \p c.
147 /// \ingroup group_gf_BasicMath
148 inline void GfSinCos(float v, float *s, float *c) { ArchSinCosf(v, s, c); }
149 
150 /// Return the resulting of clamping \p value to lie between
151 /// \p min and \p max. This function is also defined for GfVecs.
152 /// \ingroup group_gf_BasicMath
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 
159 /// \overload
160 /// \ingroup group_gf_BasicMath
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 
167 /// The mod function with "correct" behaviour for negative numbers.
168 ///
169 /// If \p a = \c n \p b for some integer \p n, zero is returned.
170 /// Otherwise, for positive \p a, the value returned is \c fmod(a,b),
171 /// and for negative \p a, the value returned is \c fmod(a,b)+b.
172 ///
173 /// \ingroup group_gf_BasicMath
174 GF_API
175 double GfMod(double a, double b);
176 /// \overload
177 // \ingroup group_gf_BasicMath
178 GF_API
179 float GfMod(float a, float b);
180 
181 /// Linear interpolation function.
182 ///
183 /// For any type that supports multiplication by a scalar and binary addition, returns
184 /// \code
185 /// (1-alpha) * a + alpha * b
186 /// \endcode
187 ///
188 /// \ingroup group_gf_BasicMath
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 
194 /// Returns the smallest of the given \c values.
195 /// \ingroup group_gf_BasicMath
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 
213 /// Returns the largest of the given \c values.
214 /// \ingroup group_gf_BasicMath
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 
232 /// Returns the dot (inner) product of two vectors.
233 /// For scalar types, this is just the regular product.
234 /// \ingroup group_gf_BasicMath
235 inline float GfDot(float a, float b) {
236  return a * b;
237 }
238 /// Returns the dot (inner) product of two vectors.
239 /// For scalar types, this is just the regular product.
240 /// \ingroup group_gf_BasicMath
241 inline double GfDot(double a, double b) {
242  return a * b;
243 }
244 
246 
247 #endif // GF_MATH_H
GLdouble s
Definition: glew.h:1390
vint4 max(const vint4 &a, const vint4 &b)
Definition: simd.h:4703
SYS_API double cos(double x)
Definition: SYS_FPUMath.h:69
#define M_PI
Definition: math.h:37
GLclampf GLclampf GLclampf alpha
Definition: glew.h:1520
double GfSqrt(double f)
Definition: math.h:77
double GfClamp(double value, double min, double max)
Definition: math.h:153
OIIO_HOSTDEVICE T radians(T deg)
Convert degrees to radians.
Definition: fmath.h:525
GLboolean GLboolean GLboolean GLboolean a
Definition: glew.h:9477
vfloat4 sqrt(const vfloat4 &a)
Definition: simd.h:7231
T GfSgn(T v)
Definition: math.h:71
const GLdouble * v
Definition: glew.h:1391
void GfSinCos(double v, double *s, double *c)
Definition: math.h:145
T GfLerp(double alpha, const T &a, const T &b)
Definition: math.h:190
double GfPow(double f, double p)
Definition: math.h:126
double GfAbs(double f)
Definition: math.h:112
T GfMin(T a1, T a2)
Definition: math.h:197
double GfExp(double f)
Definition: math.h:84
double GfRound(double f)
Definition: math.h:119
ImageBuf OIIO_API pow(const ImageBuf &A, cspan< float > B, ROI roi={}, int nthreads=0)
float GfDot(float a, float b)
Definition: math.h:235
GLclampf f
Definition: glew.h:3499
GLint GLint GLint GLint GLint x
Definition: glew.h:1252
T GfMax(T a1, T a2)
Definition: math.h:216
double GfCeil(double f)
Definition: math.h:105
OIIO_HOSTDEVICE T degrees(T rad)
Convert radians to degrees.
Definition: fmath.h:529
const GLfloat * c
Definition: glew.h:16296
double GfSin(double v)
Definition: math.h:133
double GfSqr(const T &x)
Definition: math.h:59
T exp(const T &v)
Definition: simd.h:7377
PXR_NAMESPACE_OPEN_SCOPE bool GfIsClose(double a, double b, double epsilon)
Definition: math.h:39
double GfCos(double v)
Definition: math.h:139
double GfDegreesToRadians(double degrees)
Definition: math.h:51
int floor(T x)
Definition: ImathFun.h:150
double GfFloor(double f)
Definition: math.h:98
GLdouble GLdouble GLdouble b
Definition: glew.h:9122
double GfLog(double f)
Definition: math.h:91
GLfloat GLfloat p
Definition: glew.h:16321
GF_API double GfMod(double a, double b)
T log(const T &v)
Definition: simd.h:7432
PXR_NAMESPACE_CLOSE_SCOPE PXR_NAMESPACE_OPEN_SCOPE
Definition: path.h:1245
#define PXR_NAMESPACE_CLOSE_SCOPE
Definition: pxr.h:83
int ceil(T x)
Definition: ImathFun.h:158
vint4 min(const vint4 &a, const vint4 &b)
Definition: simd.h:4694
vint4 rint(const vfloat4 &a)
Definition: simd.h:7206
double GfRadiansToDegrees(double radians)
Definition: math.h:45
GLsizei const GLfloat * value
Definition: glew.h:1849
SYS_API double sin(double x)
Definition: SYS_FPUMath.h:71
#define GF_API
Definition: api.h:40