scene/time_samples.h
| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | // SPDX-License-Identifier: BSD-3-Clause | ||
| 2 | /// @file time_samples.h | ||
| 3 | /// @brief An attribute's values at time codes, and its value between them, as USD interpolates | ||
| 4 | /// them. | ||
| 5 | /// | ||
| 6 | /// References: | ||
| 7 | /// - Pixar, "UsdAttribute: Interpolation of Time Samples", | ||
| 8 | /// https://openusd.org/release/api/class_usd_attribute.html | ||
| 9 | #pragma once | ||
| 10 | |||
| 11 | #include "ernie/scene/value.h" | ||
| 12 | #include "ernie/util/tensor.h" | ||
| 13 | |||
| 14 | #include <cstddef> | ||
| 15 | #include <optional> | ||
| 16 | #include <span> | ||
| 17 | |||
| 18 | namespace ernie { | ||
| 19 | |||
| 20 | /// @brief One time sample: a value, or for an array attribute a tensor. | ||
| 21 | 145 | struct TimeSample { | |
| 22 | double time = 0.0; | ||
| 23 | Value value; | ||
| 24 | Tensor tensor; | ||
| 25 | }; | ||
| 26 | |||
| 27 | /// @brief Where a time falls among samples: the samples either side, and how far between them | ||
| 28 | /// (0 at the lower). At or beyond the ends, and on a sample, both are that sample. | ||
| 29 | struct SampleBracket { | ||
| 30 | 4 | std::size_t lower = 0; | |
| 31 | 4 | std::size_t upper = 0; | |
| 32 | 4 | double alpha = 0.0; | |
| 33 | 4 | bool operator==(const SampleBracket&) const = default; | |
| 34 | }; | ||
| 35 | |||
| 36 | /// @param samples In increasing time, at least one. | ||
| 37 | SampleBracket Bracket(std::span<const TimeSample> samples, double time); | ||
| 38 | |||
| 39 | /// @return `a` and `b` mixed `alpha` of the way: linearly for numbers, vectors, matrices (by | ||
| 40 | /// element, as USD does) and float arrays of one size, spherically for quaternions; `a`, held, | ||
| 41 | /// for the rest (bools, ints, tokens, paths) and for values of different kinds. | ||
| 42 | Value Interpolate(const Value& a, const Value& b, double alpha); | ||
| 43 | /// @return Two float CPU tensors of one shape and type mixed linearly, element by element; none | ||
| 44 | /// for any others, which hold the lower sample (a device's consumers mix them there). | ||
| 45 | std::optional<Tensor> Interpolate(const Tensor& a, const Tensor& b, double alpha); | ||
| 46 | |||
| 47 | /// @return The samples' value or tensor at `time`. | ||
| 48 | TimeSample Evaluate(std::span<const TimeSample> samples, double time); | ||
| 49 | |||
| 50 | } // namespace ernie | ||
| 51 |