Loading...
Searching...
No Matches
mathUtils.h
1//
2// Copyright 2023 Pixar
3//
4// Licensed under the terms set forth in the LICENSE.txt file available at
5// https://openusd.org/license.
6//
7
8#ifndef PXR_BASE_TS_MATH_UTILS_H
9#define PXR_BASE_TS_MATH_UTILS_H
10
11#include "pxr/pxr.h"
12#include "pxr/base/ts/api.h"
13#include "pxr/base/ts/types.h"
14
15PXR_NAMESPACE_OPEN_SCOPE
16
17class GfMatrix4d;
18
19// Solve the cubic polynomial time=f(u) where
20// f(u) = c[0] + u * c[1] + u^2 * c[2] + u^3 * c[3].
21// XXX: exported because used by templated functions starting from TsEvaluator
22TS_API
23double Ts_SolveCubic(const TsTime c[4], TsTime time);
24
25// Take the first derivative of a cubic polynomial:
26// 3*c[3]*u^2 + 2*c[2] + c[1]
27template <typename T>
28void
29Ts_CubicDerivative( const T poly[4], double deriv[3] )
30{
31 deriv[2] = 3. * poly[3];
32 deriv[1] = 2. * poly[2];
33 deriv[0] = poly[1];
34}
35
36// Solve f(x) = y for x in the given bounds where f is a cubic polynomial.
37double
38Ts_SolveCubicInInterval(
39 const TsTime poly[4], const TsTime polyDeriv[3],
40 TsTime y, const GfInterval& bounds);
41
42// Solve for the roots of a quadratic equation.
43bool
44Ts_SolveQuadratic( const double poly[3], double *root0, double *root1 );
45
46// Evaluate the quadratic polynomial in c[] at u.
47template <typename T>
48T
49Ts_EvalQuadratic(const T c[3], double u)
50{
51 return u * (u * c[2] + c[1]) + c[0];
52}
53
54// Evaluate the cubic polynomial in c[] at u.
55template <typename T>
56T
57Ts_EvalCubic(const T c[4], double u)
58{
59 return u * (u * (u * c[3] + c[2]) + c[1]) + c[0];
60}
61
62template <typename T>
63T
64Ts_EvalCubicDerivative(const T c[4], double u)
65{
66 return u * (u * 3.0 * c[3] + 2.0 * c[2]) + c[1];
67}
68
69PXR_NAMESPACE_CLOSE_SCOPE
70
71#endif
A basic mathematical interval class.
Definition: interval.h:33
Stores a 4x4 matrix of double elements.
Definition: matrix4d.h:71