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
animMapper.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_USD_USD_SKEL_ANIM_MAPPER_H
8#define PXR_USD_USD_SKEL_ANIM_MAPPER_H
9
11
12#include "pxr/pxr.h"
13#include "pxr/usd/usdSkel/api.h"
14
17#include "pxr/base/tf/span.h"
18#include "pxr/base/vt/array.h"
19#include "pxr/usd/sdf/types.h"
20
21#include <type_traits>
22#include <vector>
23
24
25PXR_NAMESPACE_OPEN_SCOPE
26
27
28using UsdSkelAnimMapperRefPtr = std::shared_ptr<class UsdSkelAnimMapper>;
29
30
35class UsdSkelAnimMapper {
36public:
38 USDSKEL_API
39 UsdSkelAnimMapper();
40
43 USDSKEL_API
44 UsdSkelAnimMapper(size_t size);
45
48 USDSKEL_API
49 UsdSkelAnimMapper(const VtTokenArray& sourceOrder,
50 const VtTokenArray& targetOrder);
51
55 USDSKEL_API
56 UsdSkelAnimMapper(const TfToken* sourceOrder, size_t sourceOrderSize,
57 const TfToken* targetOrder, size_t targetOrderSize);
58
67 template <typename Container>
68 bool Remap(const Container& source,
69 Container* target,
70 int elementSize=1,
71 const typename Container::value_type*
72 defaultValue=nullptr) const;
73
83 USDSKEL_API
84 bool Remap(const VtValue& source, VtValue* target,
85 int elementSize=1, const VtValue& defaultValue=VtValue()) const;
86
90 template <typename Matrix4>
91 USDSKEL_API
92 bool RemapTransforms(const VtArray<Matrix4>& source,
93 VtArray<Matrix4>* target,
94 int elementSize=1) const;
95
98 USDSKEL_API
99 bool IsIdentity() const;
100
104 USDSKEL_API
105 bool IsSparse() const;
106
109 USDSKEL_API
110 bool IsNull() const;
111
114 USDSKEL_API
115 size_t size() const { return _targetSize; }
116
117 bool operator==(const UsdSkelAnimMapper& o) const;
118
119 bool operator!=(const UsdSkelAnimMapper& o) const {
120 return !(*this == o);
121 }
122
123private:
124
125 template <typename T>
126 bool _UntypedRemap(const VtValue& source, VtValue* target,
127 int elementSize, const VtValue& defaultValue) const;
128
129 template <typename T>
130 static void _ResizeContainer(VtArray<T>* array,
131 size_t size,
132 const T& defaultValue);
133
134 template <typename Container>
135 static void _ResizeContainer(
136 Container* container,
137 size_t size,
138 const typename Container::value_type& defaultValue,
139 typename std::enable_if<
141 Container>::type* = 0)
142 { container->resize(size, defaultValue); }
143
144 USDSKEL_API
145 bool _IsOrdered() const;
146
148 size_t _targetSize;
149
152 size_t _offset;
153
156 VtIntArray _indexMap;
157 int _flags;
158};
159
160
161template <typename T>
162void
163UsdSkelAnimMapper::_ResizeContainer(VtArray<T>* array, size_t size,
164 const T& defaultValue)
165{
166 // XXX: VtArray::resize() doesn't take an default value atm.
167 // We should fix this...
168 const size_t prevSize = array->size();
169 array->resize(size);
170 auto span = TfMakeSpan(*array);
171 for(size_t i = prevSize; i < size; ++i) {
172 span[i] = defaultValue;
173 }
174}
175
176
177template <typename Container>
178bool
179UsdSkelAnimMapper::Remap(const Container& source,
180 Container* target,
181 int elementSize,
182 const typename Container::value_type* defaultValue) const
183{
184 using _ValueType = typename Container::value_type;
185
186 if (!target) {
187 TF_CODING_ERROR("'target' is null");
188 return false;
189 }
190 if (elementSize <= 0) {
191 TF_WARN("Invalid elementSize [%d]: "
192 "size must be greater than zero.", elementSize);
193 return false;
194 }
195
196 const size_t targetArraySize = _targetSize*elementSize;
197
198 if (IsIdentity() && source.size() == targetArraySize) {
199 // Can make copy of the array.
200 *target = source;
201 return true;
202 }
203
204 // Resize the target array to the expected size.
205 _ResizeContainer(target, targetArraySize,
206 defaultValue ? *defaultValue : _ValueType());
207
208 if (IsNull()) {
209 return true;
210 } else if (_IsOrdered()) {
211
212 size_t copyCount =
213 std::min(source.size(), targetArraySize - _offset*elementSize);
214 std::copy(source.cdata(), source.cdata()+copyCount,
215 target->data() + _offset*elementSize);
216 } else {
217
218 const _ValueType* sourceData = source.cdata();
219
220 _ValueType* targetData = target->data();
221 size_t copyCount = std::min(source.size()/elementSize,
222 _indexMap.size());
223
224 const int* indexMap = _indexMap.data();
225
226 for (size_t i = 0; i < copyCount; ++i) {
227 int targetIdx = indexMap[i];
228 if (targetIdx >= 0 &&
229 static_cast<size_t>(targetIdx) < target->size()) {
230 TF_DEV_AXIOM(i*elementSize < source.size());
231 TF_DEV_AXIOM((i+1)*elementSize <= source.size());
232 TF_DEV_AXIOM(static_cast<size_t>((targetIdx+1)*elementSize)
233 <= target->size());
234 std::copy(sourceData + i*elementSize,
235 sourceData + (i+1)*elementSize,
236 targetData + targetIdx*elementSize);
237 }
238 }
239 }
240 return true;
241}
242
243
244PXR_NAMESPACE_CLOSE_SCOPE
245
246#endif // PXR_USD_USD_SKEL_ANIM_MAPPER_H
Token for efficient comparison, assignment, and hashing of known strings.
Definition: token.h:71
Represents an arbitrary dimensional rectangular container class.
Definition: array.h:211
Provides a container which may hold any type, and provides introspection and iteration over array typ...
Definition: value.h:147
size_t size() const
Return the total number of elements in this array.
Definition: array.h:472
void resize(size_t newSize)
Resize this array.
Definition: array.h:543
#define TF_DEV_AXIOM(cond)
The same as TF_AXIOM, but compiled only in dev builds.
Definition: diagnostic.h:205
#define TF_CODING_ERROR(fmt, args)
Issue an internal programming error, but continue execution.
Definition: diagnostic.h:68
#define TF_WARN(...)
Issue a warning, but continue execution.
Definition: diagnostic.h:132
TfSpan< typename Container::value_type > TfMakeSpan(Container &cont)
Helper for constructing a non-const TfSpan from a container.
Definition: span.h:224
Array concept. By default, types are not arrays.
Definition: traits.h:22
Basic Sdf data types.