Loading...
Searching...
No Matches
xformOp.h
Go to the documentation of this file.
1//
2// Copyright 2016 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#ifndef PXR_USD_USD_GEOM_XFORM_OP_H
25#define PXR_USD_USD_GEOM_XFORM_OP_H
26
28
29#include "pxr/pxr.h"
30#include "pxr/usd/usdGeom/api.h"
31#include "pxr/usd/usd/attribute.h"
32#include "pxr/usd/usd/attributeQuery.h"
34
35#include <string>
36#include <variant>
37#include <vector>
38#include <typeinfo>
39
41
42PXR_NAMESPACE_OPEN_SCOPE
43
44
46#define USDGEOM_XFORM_OP_TYPES \
47 (translate) \
48 (scale) \
49 (rotateX) \
50 (rotateY) \
51 (rotateZ) \
52 (rotateXYZ) \
53 (rotateXZY) \
54 (rotateYXZ) \
55 (rotateYZX) \
56 (rotateZXY) \
57 (rotateZYX) \
58 (orient) \
59 (transform) \
60 ((resetXformStack, "!resetXformStack!"))
61
85TF_DECLARE_PUBLIC_TOKENS(UsdGeomXformOpTypes, USDGEOM_API, USDGEOM_XFORM_OP_TYPES);
86
111{
112public:
113
115 enum Type {
136 };
137
143 };
144
145 // Default constructor returns an invalid XformOp. Exists for
146 // container classes
148 {
149 /* NOTHING */
150 }
151
168 USDGEOM_API
169 explicit UsdGeomXformOp(const UsdAttribute &attr, bool isInverseOp=false);
170
171 // -------------------------------------------------------
173 // -------------------------------------------------------
174
179 USDGEOM_API
180 static bool IsXformOp(const UsdAttribute &attr);
181
186 USDGEOM_API
187 static bool IsXformOp(const TfToken &attrName);
188
191 USDGEOM_API
192 static TfToken const &GetOpTypeToken(Type const opType);
193
195 USDGEOM_API
196 static Type GetOpTypeEnum(TfToken const &opTypeToken);
197
199 USDGEOM_API
201
204 USDGEOM_API
205 static const SdfValueTypeName &GetValueTypeName(const Type opType,
206 const Precision precision);
207
211 USDGEOM_API
212 static TfToken GetOpName(const Type opType,
213 const TfToken &opSuffix=TfToken(),
214 bool inverse=false);
215
216 // -------------------------------------------------------
218 // -------------------------------------------------------
219
221 Type GetOpType() const {
222 return _opType;
223 }
224
226 USDGEOM_API
228
230 bool IsInverseOp() const {
231 return _isInverseOp;
232 }
233
241 USDGEOM_API
243
245 USDGEOM_API
246 bool HasSuffix(TfToken const &suffix) const;
247
248 // ---------------------------------------------------------------
250 // ---------------------------------------------------------------
251
265 template <typename T>
266 bool GetAs(T* value, UsdTimeCode time) const {
267 VtValue v;
268 if (!Get(&v, time)) {
269 return false;
270 }
271 v.Cast<T>();
272 if (v.IsEmpty()){
273 TfType thisType = GetTypeName().GetType();
274 TF_CODING_ERROR("Unable to convert xformOp %s's value from %s to "
275 "requested type %s.", GetAttr().GetPath().GetText(),
276 thisType.GetTypeName().c_str(),
277 TfType::GetCanonicalTypeName(typeid(*value)).c_str());
278 return false;
279 }
280 *value = v.UncheckedGet<T>();
281 return true;
282 }
283
293 USDGEOM_API
294 static GfMatrix4d GetOpTransform(Type const opType,
295 VtValue const &opVal,
296 bool isInverseOp=false);
297
298
308 USDGEOM_API
310
316 bool MightBeTimeVarying() const {
317 return std::visit(_GetMightBeTimeVarying(), _attr);
318 }
319
320 // ---------------------------------------------------------------
322 // ---------------------------------------------------------------
323
327 operator UsdAttribute const& () const { return GetAttr(); }
328
330 UsdAttribute const &GetAttr() const {
331 return std::visit(_GetAttr(), _attr);
332 }
333
336 bool IsDefined() const { return IsXformOp(GetAttr()); }
337
338public:
344 explicit operator bool() const {
345 return IsDefined();
346 }
347
350 friend bool operator==(const UsdGeomXformOp &lhs,
351 const UsdGeomXformOp &rhs) {
352 return lhs.GetAttr() == rhs.GetAttr();
353 }
354
357 friend bool operator!=(const UsdGeomXformOp &lhs,
358 const UsdGeomXformOp &rhs) {
359 return !(lhs == rhs);
360 }
361
363 TfToken const &GetName() const { return GetAttr().GetName(); }
364
366 TfToken GetBaseName() const { return GetAttr().GetBaseName(); }
367
369 TfToken GetNamespace() const { return GetAttr().GetNamespace(); }
370
372 std::vector<std::string> SplitName() const { return GetAttr().SplitName(); };
373
376
381 template <typename T>
382 bool Get(T* value, UsdTimeCode time = UsdTimeCode::Default()) const {
383 return std::visit(_Get<T>(value, time), _attr);
384 }
385
392 template <typename T>
393 bool Set(T const & value, UsdTimeCode time = UsdTimeCode::Default()) const {
394 // Issue a coding error and return without setting value,
395 // if this is an inverse op.
396 if (_isInverseOp) {
397 TF_CODING_ERROR("Cannot set a value on the inverse xformOp '%s'. "
398 "Please set value on the paired non-inverse xformOp instead.",
399 GetOpName().GetText());
400 return false;
401 }
402
403 return GetAttr().Set(value, time);
404 }
405
408 bool GetTimeSamples(std::vector<double> *times) const {
409 return std::visit(_GetTimeSamples(times), _attr);
410 }
411
415 std::vector<double> *times) const {
416 return std::visit(
417 _GetTimeSamplesInInterval(interval, times), _attr);
418 }
419
421 size_t GetNumTimeSamples() const {
422 return std::visit(_GetNumTimeSamples(), _attr);
423 }
424
425private:
426 struct _ValidAttributeTagType {};
427
428public:
429 // Allow clients that guarantee \p attr is valid avoid having
430 // UsdGeomXformOp's ctor check again.
431 USDGEOM_API
432 UsdGeomXformOp(const UsdAttribute &attr, bool isInverseOp,
433 _ValidAttributeTagType);
434 USDGEOM_API
435 UsdGeomXformOp(UsdAttributeQuery &&query, bool isInverseOp,
436 _ValidAttributeTagType);
437private:
438 friend class UsdGeomXformable;
439
440 // Shared initialization function.
441 void _Init();
442
443 // Return the op-type for the string value \p str.
444 static Type _GetOpTypeEnumFromCString(char const *str, size_t len);
445
446 // Returns the attribute belonging to \p prim that corresponds to the
447 // given \p opName. It also populates the output parameter \p isInverseOp
448 // appropriately.
449 //
450 // The attribute that's returned will be invalid if the
451 // corresponding xformOp attribute doesn't exist on the prim.
452 //
453 static UsdAttribute _GetXformOpAttr(UsdPrim const& prim,
454 const TfToken &opName, bool *isInverseOp);
455
456 // Private method for creating and using an attribute query interally for
457 // this xformOp.
458 void _CreateAttributeQuery() const {
459 _attr = UsdAttributeQuery(GetAttr());
460 }
461
462 // Factory for UsdGeomXformable's use, so that we can encapsulate the
463 // logic of what discriminates XformOp in this class, while
464 // preserving the pattern that attributes can only be created
465 // via their container objects.
466 //
467 // \p opType must be one of UsdGeomXformOp::Type
468 //
469 // \p precision must be one of UsdGeomXformOp::Precision.
470 //
471 // \return an invalid UsdGeomXformOp if we failed to create a valid
472 // attribute, a valid UsdGeomXformOp otherwise. It is not an
473 // error to create over an existing, compatible attribute.
474 //
475 // It is a failed verification for \p prim to be invalid/expired
476 //
477 // \sa UsdPrim::CreateAttribute()
478 UsdGeomXformOp(UsdPrim const& prim, Type const opType,
479 Precision const precision, TfToken const &opSuffix=TfToken(),
480 bool inverse=false);
481
482 // UsdAttributeQuery already contains a copy of the associated UsdAttribute.
483 // To minimize the memory usage, we only store one or the other.
484 //
485 // The lifetime of a UsdAttributeQuery needs to be managed very carefully as
486 // it gets invalidated whenever the associated attribute is authored.
487 // Hence, access to the creation of an attribute query is restricted inside
488 // a private member function named _CreateAttributeQuery().
489 //
490 mutable std::variant<UsdAttribute, UsdAttributeQuery> _attr;
491
492 Type _opType;
493 bool _isInverseOp;
494
495 // Visitor for getting xformOp value.
496 template <class T>
497 struct _Get
498 {
499 _Get(T *value_,
500 UsdTimeCode time_ = UsdTimeCode::Default()) : value (value_), time(time_)
501 {}
502
503 bool operator()(const UsdAttribute &attr) const
504 {
505 return attr.Get(value, time);
506 }
507
508 bool operator()(const UsdAttributeQuery &attrQuery) const
509 {
510 return attrQuery.Get(value, time);
511 }
512
513 T *value;
514 UsdTimeCode time;
515 };
516
517 // Visitor for getting a const-reference to the UsdAttribute.
518 struct _GetAttr {
519
520 _GetAttr() {}
521
522 const UsdAttribute &operator()(const UsdAttribute &attr) const
523 {
524 return attr;
525 }
526
527 const UsdAttribute &operator()(const UsdAttributeQuery &attrQuery) const
528 {
529 return attrQuery.GetAttribute();
530 }
531 };
532
533 // Visitor for getting all the time samples.
534 struct _GetTimeSamples {
535
536 _GetTimeSamples(std::vector<double> *times_) : times(times_) {}
537
538 bool operator()(const UsdAttribute &attr) const
539 {
540 return attr.GetTimeSamples(times);
541 }
542
543 bool operator()(const UsdAttributeQuery &attrQuery) const
544 {
545 return attrQuery.GetTimeSamples(times);
546 }
547
548 std::vector<double> *times;
549 };
550
551 // Visitor for getting all the time samples within a given interval.
552 struct _GetTimeSamplesInInterval {
553
554 _GetTimeSamplesInInterval(const GfInterval &interval_,
555 std::vector<double> *times_)
556 : interval(interval_), times(times_)
557 {}
558
559 bool operator()(const UsdAttribute &attr) const
560 {
561 return attr.GetTimeSamplesInInterval(interval, times);
562 }
563
564 bool operator()(const UsdAttributeQuery &attrQuery) const
565 {
566 return attrQuery.GetTimeSamplesInInterval(interval, times);
567 }
568
569 const GfInterval &interval;
570 std::vector<double> *times;
571 };
572
573 // Visitor for getting the number of time samples.
574 struct _GetNumTimeSamples {
575
576 _GetNumTimeSamples() {}
577
578 size_t operator()(const UsdAttribute &attr) const
579 {
580 return attr.GetNumTimeSamples();
581 }
582
583 size_t operator()(const UsdAttributeQuery &attrQuery) const
584 {
585 return attrQuery.GetNumTimeSamples();
586 }
587 };
588
589 // Visitor for determining whether the op might vary over time.
590 struct _GetMightBeTimeVarying {
591
592 _GetMightBeTimeVarying() {}
593
594 bool operator()(const UsdAttribute &attr) const
595 {
596 return attr.ValueMightBeTimeVarying();
597 }
598
599 bool operator()(const UsdAttributeQuery &attrQuery) const
600 {
601 return attrQuery.ValueMightBeTimeVarying();
602 }
603 };
604
605};
606
607
608
609PXR_NAMESPACE_CLOSE_SCOPE
610
611#endif // USD_XFORMOP_H
A basic mathematical interval class.
Definition: interval.h:50
Stores a 4x4 matrix of double elements.
Definition: matrix4d.h:88
Represents a value type name, i.e.
Definition: valueTypeName.h:88
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:88
TfType represents a dynamic runtime type.
Definition: type.h:65
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:176
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:477
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:436
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:111
friend bool operator==(const UsdGeomXformOp &lhs, const UsdGeomXformOp &rhs)
Equality comparison.
Definition: xformOp.h:350
TfToken GetBaseName() const
Definition: xformOp.h:366
Type
Enumerates the set of all transformation operation types.
Definition: xformOp.h:115
@ TypeRotateY
Rotation about the Y-axis, in degrees.
Definition: xformOp.h:120
@ TypeRotateXYZ
Set of 3 canonical Euler rotations in XYZ order.
Definition: xformOp.h:122
@ TypeScale
XYZ scale.
Definition: xformOp.h:118
@ TypeOrient
Arbitrary axis/angle rotation, expressed as a quaternion.
Definition: xformOp.h:134
@ TypeTranslate
XYZ translation.
Definition: xformOp.h:117
@ TypeRotateZ
Rotation about the Z-axis, in degrees.
Definition: xformOp.h:121
@ TypeRotateX
Rotation about the X-axis, in degrees.
Definition: xformOp.h:119
@ TypeTransform
A 4x4 matrix transformation.
Definition: xformOp.h:135
@ TypeRotateZYX
Set of 3 canonical Euler rotations in ZYX order.
Definition: xformOp.h:132
@ TypeInvalid
Represents an invalid xformOp.
Definition: xformOp.h:116
@ TypeRotateYZX
Set of 3 canonical Euler rotations in YZX order.
Definition: xformOp.h:128
@ TypeRotateZXY
Set of 3 canonical Euler rotations in ZXY order.
Definition: xformOp.h:130
@ TypeRotateXZY
Set of 3 canonical Euler rotations in XZY order.
Definition: xformOp.h:124
@ TypeRotateYXZ
Set of 3 canonical Euler rotations in YXZ order.
Definition: xformOp.h:126
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:357
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:230
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:369
bool GetTimeSamples(std::vector< double > *times) const
Populates the list of time samples at which the associated attribute is authored.
Definition: xformOp.h:408
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:336
std::vector< std::string > SplitName() const
Definition: xformOp.h:372
Type GetOpType() const
Return the operation type of this op, one of UsdGeomXformOp::Type.
Definition: xformOp.h:221
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:316
bool Get(T *value, UsdTimeCode time=UsdTimeCode::Default()) const
Get the attribute value of the XformOp at time.
Definition: xformOp.h:382
SdfValueTypeName GetTypeName() const
Definition: xformOp.h:375
TfToken const & GetName() const
Definition: xformOp.h:363
UsdAttribute const & GetAttr() const
Explicit UsdAttribute extractor.
Definition: xformOp.h:330
USDGEOM_API TfToken GetOpName() const
Returns the opName as it appears in the xformOpOrder attribute.
Precision
Precision with which the value of the tranformation operation is encoded.
Definition: xformOp.h:139
@ PrecisionFloat
Floating-point precision.
Definition: xformOp.h:141
@ PrecisionDouble
Double precision.
Definition: xformOp.h:140
@ PrecisionHalf
Half-float precision.
Definition: xformOp.h:142
size_t GetNumTimeSamples() const
Returns the number of time samples authored for this xformOp.
Definition: xformOp.h:421
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:414
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:266
static USDGEOM_API bool IsXformOp(const TfToken &attrName)
Test whether a given attrbute name represents a valid XformOp, which implies that creating a UsdGeomX...
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:393
Base class for all transformable prims, which allows arbitrary sequences of component affine transfor...
Definition: xformable.h:253
const TfToken & GetName() const
Return the full name of this object, i.e.
Definition: object.h:238
UsdPrim is the sole persistent scenegraph object on a UsdStage, and is the embodiment of a "Prim" as ...
Definition: prim.h:134
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:84
static constexpr UsdTimeCode Default()
Produce a UsdTimeCode representing the sentinel value for 'default'.
Definition: timeCode.h:112
Provides a container which may hold any type, and provides introspection and iteration over array typ...
Definition: value.h:164
bool IsEmpty() const
Returns true iff this value is empty.
Definition: value.h:1300
static VtValue Cast(VtValue const &val)
Return a VtValue holding val cast to hold T.
Definition: value.h:1204
T const & UncheckedGet() const &
Returns a const reference to the held object if the held object is of type T.
Definition: value.h:1121
#define TF_CODING_ERROR(fmt, args)
Issue an internal programming error, but continue execution.
Definition: diagnostic.h:85
This file defines some macros that are useful for declaring and using static TfTokens.
#define TF_DECLARE_PUBLIC_TOKENS(...)
Macro to define public tokens.
Definition: staticTokens.h:98