HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
mathUtils.h
Go to the documentation of this file.
1 //
2 // Copyright 2023 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 
25 #ifndef PXR_BASE_TS_MATH_UTILS_H
26 #define PXR_BASE_TS_MATH_UTILS_H
27 
28 #include "pxr/pxr.h"
29 #include "pxr/base/ts/api.h"
30 #include "pxr/base/ts/types.h"
31 
33 
34 class GfMatrix4d;
35 
36 // Solve the cubic polynomial time=f(u) where
37 // f(u) = c[0] + u * c[1] + u^2 * c[2] + u^3 * c[3].
38 // XXX: exported because used by templated functions starting from TsEvaluator
39 TS_API
40 double Ts_SolveCubic(const TsTime c[4], TsTime time);
41 
42 // Take the first derivative of a cubic polynomial:
43 // 3*c[3]*u^2 + 2*c[2] + c[1]
44 template <typename T>
45 void
46 Ts_CubicDerivative( const T poly[4], double deriv[3] )
47 {
48  deriv[2] = 3. * poly[3];
49  deriv[1] = 2. * poly[2];
50  deriv[0] = poly[1];
51 }
52 
53 // Solve f(x) = y for x in the given bounds where f is a cubic polynomial.
54 double
56  const TsTime poly[4], const TsTime polyDeriv[3],
57  TsTime y, const GfInterval& bounds);
58 
59 // Solve for the roots of a quadratic equation.
60 bool
61 Ts_SolveQuadratic( const double poly[3], double *root0, double *root1 );
62 
63 // Evaluate the quadratic polynomial in c[] at u.
64 template <typename T>
65 T
66 Ts_EvalQuadratic(const T c[3], double u)
67 {
68  return u * (u * c[2] + c[1]) + c[0];
69 }
70 
71 // Evaluate the cubic polynomial in c[] at u.
72 template <typename T>
73 T
74 Ts_EvalCubic(const T c[4], double u)
75 {
76  return u * (u * (u * c[3] + c[2]) + c[1]) + c[0];
77 }
78 
79 template <typename T>
80 T
81 Ts_EvalCubicDerivative(const T c[4], double u)
82 {
83  return u * (u * 3.0 * c[3] + 2.0 * c[2]) + c[1];
84 }
85 
87 
88 #endif
bool Ts_SolveQuadratic(const double poly[3], double *root0, double *root1)
GT_API const UT_StringHolder time
void Ts_CubicDerivative(const T poly[4], double deriv[3])
Definition: mathUtils.h:46
GLint y
Definition: glcorearb.h:103
T Ts_EvalCubic(const T c[4], double u)
Definition: mathUtils.h:74
T Ts_EvalCubicDerivative(const T c[4], double u)
Definition: mathUtils.h:81
T Ts_EvalQuadratic(const T c[3], double u)
Definition: mathUtils.h:66
TS_API double Ts_SolveCubic(const TsTime c[4], TsTime time)
PXR_NAMESPACE_OPEN_SCOPE typedef double TsTime
The time type used by Ts.
Definition: types.h:57
#define TS_API
Definition: api.h:41
PXR_NAMESPACE_CLOSE_SCOPE PXR_NAMESPACE_OPEN_SCOPE
Definition: path.h:1432
#define PXR_NAMESPACE_CLOSE_SCOPE
Definition: pxr.h:91
double Ts_SolveCubicInInterval(const TsTime poly[4], const TsTime polyDeriv[3], TsTime y, const GfInterval &bounds)