Loading...
Searching...
No Matches
xformOp.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_GEOM_XFORM_OP_H
8#define PXR_USD_USD_GEOM_XFORM_OP_H
9
11
12#include "pxr/pxr.h"
13#include "pxr/usd/usdGeom/api.h"
14#include "pxr/usd/usd/attribute.h"
15#include "pxr/usd/usd/attributeQuery.h"
17
18#include <string>
19#include <variant>
20#include <vector>
21#include <typeinfo>
22
24
25PXR_NAMESPACE_OPEN_SCOPE
26
27
29#define USDGEOM_XFORM_OP_TYPES \
30 (translateX) \
31 (translateY) \
32 (translateZ) \
33 (translate) \
34 (scaleX) \
35 (scaleY) \
36 (scaleZ) \
37 (scale) \
38 (rotateX) \
39 (rotateY) \
40 (rotateZ) \
41 (rotateXYZ) \
42 (rotateXZY) \
43 (rotateYXZ) \
44 (rotateYZX) \
45 (rotateZXY) \
46 (rotateZYX) \
47 (orient) \
48 (transform) \
49 ((resetXformStack, "!resetXformStack!"))
50
80TF_DECLARE_PUBLIC_TOKENS(UsdGeomXformOpTypes, USDGEOM_API, USDGEOM_XFORM_OP_TYPES);
81
106{
107public:
108
138
145
146 // Default constructor returns an invalid XformOp. Exists for
147 // container classes
149 {
150 /* NOTHING */
151 }
152
169 USDGEOM_API
170 explicit UsdGeomXformOp(const UsdAttribute &attr, bool isInverseOp=false);
171
172 // -------------------------------------------------------
174 // -------------------------------------------------------
175
180 USDGEOM_API
181 static bool IsXformOp(const UsdAttribute &attr);
182
187 USDGEOM_API
188 static bool IsXformOp(const TfToken &attrName);
189
192 USDGEOM_API
193 static TfToken const &GetOpTypeToken(Type const opType);
194
196 USDGEOM_API
197 static Type GetOpTypeEnum(TfToken const &opTypeToken);
198
200 USDGEOM_API
202 const SdfValueTypeName& typeName);
203
206 USDGEOM_API
207 static const SdfValueTypeName &GetValueTypeName(const Type opType,
208 const Precision precision);
209
213 USDGEOM_API
214 static TfToken GetOpName(const Type opType,
215 const TfToken &opSuffix=TfToken(),
216 bool inverse=false);
217
218 // -------------------------------------------------------
220 // -------------------------------------------------------
221
223 Type GetOpType() const {
224 return _opType;
225 }
226
228 USDGEOM_API
230
232 bool IsInverseOp() const {
233 return _isInverseOp;
234 }
235
244 USDGEOM_API
246
248 USDGEOM_API
249 bool HasSuffix(TfToken const &suffix) const;
250
251 // ---------------------------------------------------------------
253 // ---------------------------------------------------------------
254
268 template <typename T>
269 bool GetAs(T* value, UsdTimeCode time) const {
270 VtValue v;
271 if (!Get(&v, time)) {
272 return false;
273 }
274 v.Cast<T>();
275 if (v.IsEmpty()){
276 TfType thisType = GetTypeName().GetType();
277 TF_CODING_ERROR("Unable to convert xformOp %s's value from %s to "
278 "requested type %s.", GetAttr().GetPath().GetText(),
279 thisType.GetTypeName().c_str(),
280 TfType::GetCanonicalTypeName(typeid(*value)).c_str());
281 return false;
282 }
283 *value = v.UncheckedGet<T>();
284 return true;
285 }
286
296 USDGEOM_API
297 static GfMatrix4d GetOpTransform(Type const opType,
298 VtValue const &opVal,
299 bool isInverseOp=false);
300
301
311 USDGEOM_API
313
319 bool MightBeTimeVarying() const {
320 return std::visit(_GetMightBeTimeVarying(), _attr);
321 }
322
323 // ---------------------------------------------------------------
325 // ---------------------------------------------------------------
326
330 operator UsdAttribute const& () const { return GetAttr(); }
331
333 UsdAttribute const &GetAttr() const {
334 return std::visit(_GetAttr(), _attr);
335 }
336
339 bool IsDefined() const { return IsXformOp(GetAttr()); }
340
341public:
347 explicit operator bool() const {
348 return IsDefined();
349 }
350
353 friend bool operator==(const UsdGeomXformOp &lhs,
354 const UsdGeomXformOp &rhs) {
355 return lhs.GetAttr() == rhs.GetAttr();
356 }
357
360 friend bool operator!=(const UsdGeomXformOp &lhs,
361 const UsdGeomXformOp &rhs) {
362 return !(lhs == rhs);
363 }
364
366 TfToken const &GetName() const { return GetAttr().GetName(); }
367
369 TfToken GetBaseName() const { return GetAttr().GetBaseName(); }
370
372 TfToken GetNamespace() const { return GetAttr().GetNamespace(); }
373
375 std::vector<std::string> SplitName() const { return GetAttr().SplitName(); };
376
379
384 template <typename T>
385 bool Get(T* value, UsdTimeCode time = UsdTimeCode::Default()) const {
386 return std::visit(_Get<T>(value, time), _attr);
387 }
388
395 template <typename T>
396 bool Set(T const & value, UsdTimeCode time = UsdTimeCode::Default()) const {
397 // Issue a coding error and return without setting value,
398 // if this is an inverse op.
399 if (_isInverseOp) {
400 TF_CODING_ERROR("Cannot set a value on the inverse xformOp '%s'. "
401 "Please set value on the paired non-inverse xformOp instead.",
402 GetOpName().GetText());
403 return false;
404 }
405
406 return GetAttr().Set(value, time);
407 }
408
411 bool GetTimeSamples(std::vector<double> *times) const {
412 return std::visit(_GetTimeSamples(times), _attr);
413 }
414
418 std::vector<double> *times) const {
419 return std::visit(
420 _GetTimeSamplesInInterval(interval, times), _attr);
421 }
422
424 size_t GetNumTimeSamples() const {
425 return std::visit(_GetNumTimeSamples(), _attr);
426 }
427
428private:
429 struct _ValidAttributeTagType {};
430
431public:
432 // Allow clients that guarantee \p attr is valid avoid having
433 // UsdGeomXformOp's ctor check again.
434 USDGEOM_API
435 UsdGeomXformOp(const UsdAttribute &attr, bool isInverseOp,
436 _ValidAttributeTagType);
437 USDGEOM_API
438 UsdGeomXformOp(UsdAttributeQuery &&query, bool isInverseOp,
439 _ValidAttributeTagType);
440private:
441 friend class UsdGeomXformable;
442
443 // Shared initialization function.
444 void _Init();
445
446 // Return the op-type for the string value \p str.
447 static Type _GetOpTypeEnumFromCString(char const *str, size_t len);
448
449 // Returns the attribute belonging to \p prim that corresponds to the
450 // given \p opName. It also populates the output parameter \p isInverseOp
451 // appropriately.
452 //
453 // The attribute that's returned will be invalid if the
454 // corresponding xformOp attribute doesn't exist on the prim.
455 //
456 static UsdAttribute _GetXformOpAttr(UsdPrim const& prim,
457 const TfToken &opName, bool *isInverseOp);
458
459 // Private method for creating and using an attribute query internally for
460 // this xformOp.
461 void _CreateAttributeQuery() const {
462 _attr = UsdAttributeQuery(GetAttr());
463 }
464
465 // Factory for UsdGeomXformable's use, so that we can encapsulate the
466 // logic of what discriminates XformOp in this class, while
467 // preserving the pattern that attributes can only be created
468 // via their container objects.
469 //
470 // \p opType must be one of UsdGeomXformOp::Type
471 //
472 // \p precision must be one of UsdGeomXformOp::Precision.
473 //
474 // \return an invalid UsdGeomXformOp if we failed to create a valid
475 // attribute, a valid UsdGeomXformOp otherwise. It is not an
476 // error to create over an existing, compatible attribute.
477 //
478 // It is a failed verification for \p prim to be invalid/expired
479 //
480 // \sa UsdPrim::CreateAttribute()
481 UsdGeomXformOp(UsdPrim const& prim, Type const opType,
482 Precision const precision, TfToken const &opSuffix=TfToken(),
483 bool inverse=false);
484
485 // UsdAttributeQuery already contains a copy of the associated UsdAttribute.
486 // To minimize the memory usage, we only store one or the other.
487 //
488 // The lifetime of a UsdAttributeQuery needs to be managed very carefully as
489 // it gets invalidated whenever the associated attribute is authored.
490 // Hence, access to the creation of an attribute query is restricted inside
491 // a private member function named _CreateAttributeQuery().
492 //
493 mutable std::variant<UsdAttribute, UsdAttributeQuery> _attr;
494
495 Type _opType;
496 bool _isInverseOp;
497
498 // Visitor for getting xformOp value.
499 template <class T>
500 struct _Get
501 {
502 _Get(T *value_,
503 UsdTimeCode time_ = UsdTimeCode::Default()) : value (value_), time(time_)
504 {}
505
506 bool operator()(const UsdAttribute &attr) const
507 {
508 return attr.Get(value, time);
509 }
510
511 bool operator()(const UsdAttributeQuery &attrQuery) const
512 {
513 return attrQuery.Get(value, time);
514 }
515
516 T *value;
517 UsdTimeCode time;
518 };
519
520 // Visitor for getting a const-reference to the UsdAttribute.
521 struct _GetAttr {
522
523 _GetAttr() {}
524
525 const UsdAttribute &operator()(const UsdAttribute &attr) const
526 {
527 return attr;
528 }
529
530 const UsdAttribute &operator()(const UsdAttributeQuery &attrQuery) const
531 {
532 return attrQuery.GetAttribute();
533 }
534 };
535
536 // Visitor for getting all the time samples.
537 struct _GetTimeSamples {
538
539 _GetTimeSamples(std::vector<double> *times_) : times(times_) {}
540
541 bool operator()(const UsdAttribute &attr) const
542 {
543 return attr.GetTimeSamples(times);
544 }
545
546 bool operator()(const UsdAttributeQuery &attrQuery) const
547 {
548 return attrQuery.GetTimeSamples(times);
549 }
550
551 std::vector<double> *times;
552 };
553
554 // Visitor for getting all the time samples within a given interval.
555 struct _GetTimeSamplesInInterval {
556
557 _GetTimeSamplesInInterval(const GfInterval &interval_,
558 std::vector<double> *times_)
559 : interval(interval_), times(times_)
560 {}
561
562 bool operator()(const UsdAttribute &attr) const
563 {
564 return attr.GetTimeSamplesInInterval(interval, times);
565 }
566
567 bool operator()(const UsdAttributeQuery &attrQuery) const
568 {
569 return attrQuery.GetTimeSamplesInInterval(interval, times);
570 }
571
572 const GfInterval &interval;
573 std::vector<double> *times;
574 };
575
576 // Visitor for getting the number of time samples.
577 struct _GetNumTimeSamples {
578
579 _GetNumTimeSamples() {}
580
581 size_t operator()(const UsdAttribute &attr) const
582 {
583 return attr.GetNumTimeSamples();
584 }
585
586 size_t operator()(const UsdAttributeQuery &attrQuery) const
587 {
588 return attrQuery.GetNumTimeSamples();
589 }
590 };
591
592 // Visitor for determining whether the op might vary over time.
593 struct _GetMightBeTimeVarying {
594
595 _GetMightBeTimeVarying() {}
596
597 bool operator()(const UsdAttribute &attr) const
598 {
599 return attr.ValueMightBeTimeVarying();
600 }
601
602 bool operator()(const UsdAttributeQuery &attrQuery) const
603 {
604 return attrQuery.ValueMightBeTimeVarying();
605 }
606 };
607
608};
609
610
611
612PXR_NAMESPACE_CLOSE_SCOPE
613
614#endif // USD_XFORMOP_H
A basic mathematical interval class.
Definition interval.h:33
Stores a 4x4 matrix of double elements.
Definition matrix4d.h:71
Represents a value type name, i.e.
SDF_API const TfType & GetType() const
Returns the TfType of the type.
Token for efficient comparison, assignment, and hashing of known strings.
Definition token.h:71
TfType represents a dynamic runtime type.
Definition type.h:48
TF_API const std::string & GetTypeName() const
Return the machine-independent name for this type.
static TF_API std::string GetCanonicalTypeName(const std::type_info &)
Return the canonical typeName used for a given std::type_info.
Scenegraph object for authoring and retrieving numeric, string, and array valued data,...
Definition attribute.h:183
USD_API bool GetTimeSamples(std::vector< double > *times) const
Populates a vector with authored sample times.
USD_API bool GetTimeSamplesInInterval(const GfInterval &interval, std::vector< double > *times) const
Populates a vector with authored sample times in interval.
bool Set(const T &value, UsdTimeCode time=UsdTimeCode::Default()) const
Set the value of this attribute in the current UsdEditTarget to value at UsdTimeCode time,...
Definition attribute.h:531
USD_API SdfValueTypeName GetTypeName() const
Return the "scene description" value type name for this attribute.
bool Get(T *value, UsdTimeCode time=UsdTimeCode::Default()) const
Perform value resolution to fetch the value of this attribute at the requested UsdTimeCode time,...
Definition attribute.h:468
USD_API size_t GetNumTimeSamples() const
Returns the number of time samples that have been authored.
USD_API bool ValueMightBeTimeVarying() const
Return true if it is possible, but not certain, that this attribute's value changes over time,...
Object for efficiently making repeated queries for attribute values.
USD_API bool GetTimeSamples(std::vector< double > *times) const
Populates a vector with authored sample times.
USD_API bool GetTimeSamplesInInterval(const GfInterval &interval, std::vector< double > *times) const
Populates a vector with authored sample times in interval.
USD_API const UsdAttribute & GetAttribute() const
Return the attribute associated with this query.
bool Get(T *value, UsdTimeCode time=UsdTimeCode::Default()) const
Perform value resolution to fetch the value of the attribute associated with this query at the reques...
USD_API size_t GetNumTimeSamples() const
Returns the number of time samples that have been authored.
USD_API bool ValueMightBeTimeVarying() const
Return true if it is possible, but not certain, that this attribute's value changes over time,...
Schema wrapper for UsdAttribute for authoring and computing transformation operations,...
Definition xformOp.h:106
friend bool operator==(const UsdGeomXformOp &lhs, const UsdGeomXformOp &rhs)
Equality comparison.
Definition xformOp.h:353
TfToken GetBaseName() const
Definition xformOp.h:369
Type
Enumerates the set of all transformation operation types.
Definition xformOp.h:110
@ TypeRotateY
Rotation about the Y-axis, in degrees.
Definition xformOp.h:121
@ TypeRotateXYZ
Set of 3 canonical Euler rotations in XYZ order.
Definition xformOp.h:123
@ TypeTranslateY
Translation along the Y-axis.
Definition xformOp.h:113
@ TypeScaleY
Scale along the Y-axis.
Definition xformOp.h:117
@ TypeScale
XYZ scale.
Definition xformOp.h:119
@ TypeOrient
Arbitrary axis/angle rotation, expressed as a quaternion.
Definition xformOp.h:135
@ TypeTranslate
XYZ translation.
Definition xformOp.h:115
@ TypeRotateZ
Rotation about the Z-axis, in degrees.
Definition xformOp.h:122
@ TypeScaleX
Scale along the X-axis.
Definition xformOp.h:116
@ TypeRotateX
Rotation about the X-axis, in degrees.
Definition xformOp.h:120
@ TypeTransform
A 4x4 matrix transformation.
Definition xformOp.h:136
@ TypeTranslateX
Translation along the X-axis.
Definition xformOp.h:112
@ TypeRotateZYX
Set of 3 canonical Euler rotations in ZYX order.
Definition xformOp.h:133
@ TypeInvalid
Represents an invalid xformOp.
Definition xformOp.h:111
@ TypeRotateYZX
Set of 3 canonical Euler rotations in YZX order.
Definition xformOp.h:129
@ TypeRotateZXY
Set of 3 canonical Euler rotations in ZXY order.
Definition xformOp.h:131
@ TypeRotateXZY
Set of 3 canonical Euler rotations in XZY order.
Definition xformOp.h:125
@ TypeTranslateZ
Translation along the Z-axis.
Definition xformOp.h:114
@ TypeRotateYXZ
Set of 3 canonical Euler rotations in YXZ order.
Definition xformOp.h:127
@ TypeScaleZ
Scale along the Z-axis.
Definition xformOp.h:118
USDGEOM_API UsdGeomXformOp(const UsdAttribute &attr, bool isInverseOp=false)
Speculative constructor that will produce a valid UsdGeomXformOp when attr already represents an attr...
USDGEOM_API bool HasSuffix(TfToken const &suffix) const
Does this op have the given suffix in its name.
friend bool operator!=(const UsdGeomXformOp &lhs, const UsdGeomXformOp &rhs)
Inequality comparison.
Definition xformOp.h:360
static USDGEOM_API TfToken GetOpName(const Type opType, const TfToken &opSuffix=TfToken(), bool inverse=false)
Returns the xformOp's name as it appears in xformOpOrder, given the opType, the (optional) suffix and...
USDGEOM_API Precision GetPrecision() const
Returns the precision level of the xform op.
static USDGEOM_API GfMatrix4d GetOpTransform(Type const opType, VtValue const &opVal, bool isInverseOp=false)
Return the 4x4 matrix that applies the transformation encoded by op opType and data value opVal.
bool IsInverseOp() const
Returns whether the xformOp represents an inverse operation.
Definition xformOp.h:232
USDGEOM_API GfMatrix4d GetOpTransform(UsdTimeCode time) const
Return the 4x4 matrix that applies the transformation encoded in this op at time.
TfToken GetNamespace() const
Definition xformOp.h:372
bool GetTimeSamples(std::vector< double > *times) const
Populates the list of time samples at which the associated attribute is authored.
Definition xformOp.h:411
static USDGEOM_API bool IsXformOp(const UsdAttribute &attr)
Test whether a given UsdAttribute represents valid XformOp, which implies that creating a UsdGeomXfor...
bool IsDefined() const
Return true if the wrapped UsdAttribute::IsDefined(), and in addition the attribute is identified as ...
Definition xformOp.h:339
std::vector< std::string > SplitName() const
Definition xformOp.h:375
Type GetOpType() const
Return the operation type of this op, one of UsdGeomXformOp::Type.
Definition xformOp.h:223
static USDGEOM_API TfToken const & GetOpTypeToken(Type const opType)
Returns the TfToken used to encode the given opType.
static USDGEOM_API Type GetOpTypeEnum(TfToken const &opTypeToken)
Returns the Type enum associated with the given opTypeToken.
bool MightBeTimeVarying() const
Determine whether there is any possibility that this op's value may vary over time.
Definition xformOp.h:319
bool Get(T *value, UsdTimeCode time=UsdTimeCode::Default()) const
Get the attribute value of the XformOp at time.
Definition xformOp.h:385
SdfValueTypeName GetTypeName() const
Definition xformOp.h:378
TfToken const & GetName() const
Definition xformOp.h:366
UsdAttribute const & GetAttr() const
Explicit UsdAttribute extractor.
Definition xformOp.h:333
USDGEOM_API TfToken GetOpName() const
Returns the opName as it appears in the xformOpOrder attribute.
Precision
Precision of the encoded transformation operation's value.
Definition xformOp.h:140
@ PrecisionFloat
Floating-point precision.
Definition xformOp.h:142
@ PrecisionDouble
Double precision.
Definition xformOp.h:141
@ PrecisionHalf
Half-float precision.
Definition xformOp.h:143
size_t GetNumTimeSamples() const
Returns the number of time samples authored for this xformOp.
Definition xformOp.h:424
bool GetTimeSamplesInInterval(const GfInterval &interval, std::vector< double > *times) const
Populates the list of time samples within the given interval, at which the associated attribute is au...
Definition xformOp.h:417
bool GetAs(T *value, UsdTimeCode time) const
We allow ops to be encoded with varying degrees of precision, depending on the clients needs and cons...
Definition xformOp.h:269
static USDGEOM_API bool IsXformOp(const TfToken &attrName)
Test whether a given attribute name represents a valid XformOp, which implies that creating a UsdGeom...
static USDGEOM_API const SdfValueTypeName & GetValueTypeName(const Type opType, const Precision precision)
Returns the value typeName token that corresponds to the given combination of opType and precision.
static USDGEOM_API Precision GetPrecisionFromValueTypeName(const SdfValueTypeName &typeName)
Returns the precision corresponding to the given value typeName.
bool Set(T const &value, UsdTimeCode time=UsdTimeCode::Default()) const
Set the attribute value of the XformOp at time.
Definition xformOp.h:396
Base class for all transformable prims, which allows arbitrary sequences of component affine transfor...
Definition xformable.h:242
const TfToken & GetName() const
Return the full name of this object, i.e.
Definition object.h:222
UsdPrim is the sole persistent scenegraph object on a UsdStage, and is the embodiment of a "Prim" as ...
Definition prim.h:117
USD_API TfToken GetBaseName() const
Return this property's name with all namespace prefixes removed, i.e.
USD_API TfToken GetNamespace() const
Return this property's complete namespace prefix.
USD_API std::vector< std::string > SplitName() const
Return this property's name elements including namespaces and its base name as the final element.
Represent a time value, which may be either numeric, holding a double value, or a sentinel value UsdT...
Definition timeCode.h:72
static constexpr UsdTimeCode Default()
Produce a UsdTimeCode representing the sentinel value for 'default'.
Definition timeCode.h:113
Provides a container which may hold any type, and provides introspection and iteration over array typ...
Definition value.h:90
bool IsEmpty() const
Returns true iff this value is empty.
Definition value.h:1227
static VtValue Cast(VtValue const &val)
Return a VtValue holding val cast to hold T.
Definition value.h:1131
T const & UncheckedGet() const &
Returns a const reference to the held object if the held object is of type T.
Definition value.h:1046
#define TF_CODING_ERROR(fmt, args)
Issue an internal programming error, but continue execution.
Definition diagnostic.h:68
This file defines some macros that are useful for declaring and using static TfTokens.
#define TF_DECLARE_PUBLIC_TOKENS(...)
Macro to define public tokens.