HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
metrics.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_METRICS_H
8 #define PXR_USD_USD_GEOM_METRICS_H
9 
10 #include "pxr/pxr.h"
11 #include "pxr/usd/usdGeom/api.h"
12 #include "pxr/base/tf/token.h"
13 #include "pxr/usd/usd/common.h"
14 
16 
17 
18 /// \file usdGeom/metrics.h
19 ///
20 /// Schema and utilities for encoding various spatial and geometric metrics of
21 /// a UsdStage and its contents.
22 ///
23 /// \defgroup UsdGeomUpAxis_group Encoding Stage UpAxis
24 ///
25 /// Even if all UsdGeomCamera objects in a scene are encoded with a
26 /// transformation that assumes the Y axis is up, that cannot always imply
27 /// that the geometry contained in the scene was modeled with the Y axis
28 /// pointing up, since some modeling applications assume Z is up, and others
29 /// allow you to configure whether Y or Z is up. Rather than impose one axis
30 /// or the other on all UsdGeom scenes - which would require either
31 /// transposing point coordinates into and out of some applications, or making
32 /// assumptions about where a -90 degree rotation has been applied and
33 /// effectively, mysteriously swapping the roles of Y and Z coordinates -
34 /// we allow each stage to carry, in its root layer, a declaration of the
35 /// up axis for all the geometry contained in the stage, and require
36 /// applications to consult it if they wish to create new cameras to view
37 /// the scene in its intended orientation.
38 ///
39 /// Note that because you can make only a Stage-level declaration of up axis,
40 /// it must hold true for all referenced geometry and assets. A conscientious
41 /// set/environment-construction tool could examine the up axis of referenced
42 /// assets as they are being referenced, and apply a corrective rotation
43 /// on the referencing prim as it is being added to the scene, when the
44 /// referenced up axis differs from the set's.
45 ///
46 /// We provide free functions for setting (UsdGeomSetStageUpAxis()),
47 /// retrieving (UsdGeomGetStageUpAxis()), and declaring a site-level
48 /// fallback up axis (UsdGeomGetFallbackUpAxis()) that can be configured
49 /// with a PlugPlugin plugInfo.json file. See the individual methods for
50 /// details.
51 ///
52 /// The stage up axis is encoded as stage metadatum \em upAxis, whose legal
53 /// values are "Y" and "Z", as represented by UsdGeomTokens->y and
54 /// UsdGeomTokens->z. Of course, constructing a correct camera view of a
55 /// scene depends not only on the up axis, but also on the handedness of the
56 /// coordinate system. Like OpenGL and the fallback for
57 /// UsdGeomGprim::GetOrientationAttr(), <b>UsdGeom stipulates a right-handed
58 /// coordinate system.</b> Therefore, when viewing a UsdStage with a "Y" up
59 /// axis, the stage's Z axis will be pointing out of the screen, and when
60 /// viewing a UsdStage with a "Z" up axis, the stage's Y axis will be
61 /// pointing into the screen.
62 ///
63 /// @{
64 
65 /// Fetch and return \p stage 's upAxis. If unauthored, will return the
66 /// value provided by UsdGeomGetFallbackUpAxis(). Exporters, however, are
67 /// strongly encouraged to always set the upAxis for every USD file they create.
68 ///
69 /// \return one of: UsdGeomTokens->y or UsdGeomTokens->z, unless there was
70 /// an error, in which case returns an empty TfToken
71 ///
72 /// \sa UsdGeomUpAxis_group
75 
76 /// Set \p stage 's upAxis to \p axis, which must be one of UsdGeomTokens->y
77 /// or UsdGeomTokens->z
78 ///
79 /// UpAxis is stage-level metadata, therefore see UsdStage::SetMetadata().
80 ///
81 /// \return true if upAxis was successfully set. The stage's UsdEditTarget
82 /// must be either its root layer or session layer.
83 /// \sa UsdGeomUpAxis_group
85 bool UsdGeomSetStageUpAxis(const UsdStageWeakPtr &stage, const TfToken &axis);
86 
87 /// Return the site-level fallback up axis as a TfToken.
88 ///
89 /// In a generic installation of USD, the fallback will be "Y". This can be
90 /// changed to "Z" by adding, in a plugInfo.json file discoverable by USD's
91 /// PlugPlugin mechanism:
92 /// \code{json}
93 /// "UsdGeomMetrics": {
94 /// "upAxis": "Z"
95 /// }
96 /// \endcode
97 ///
98 /// If more than one such entry is discovered and the values for upAxis differ,
99 /// we will issue a warning during the first call to this function, and ignore
100 /// all of them, so that we devolve to deterministic behavior of Y up axis
101 /// until the problem is rectified.
104 
105 /// @}
106 
107 /// \defgroup UsdGeomLinearUnits_group Encoding Stage Linear Units
108 ///
109 /// As with \ref UsdGeomUpAxis_group, we restrict the encoding of linear
110 /// units to be stage-wide; if assembling assets of different metrics, it is
111 /// the assembler's responsibility to apply suitable correctives to the
112 /// referenced data to bring it into the referencing stage's metric.
113 ///
114 /// We encode linear measure as **meters per unit** (stage-level metadata
115 /// *metersPerUnit*) rather than units-per-meter because it makes it easier
116 /// to encode precise imperial units (one imperial foot = 0.3048 meters exactly).
117 ///
118 /// If a UsdStage has no authored *metersPerUnit* we fall back to the value
119 /// \ref UsdGeomLinearUnits "UsdGeomLinearUnits::centimeters", i.e. 0.01.
120 ///
121 /// The specified *metersPerUnit* metric should only be applied to
122 /// **world space** (i.e. fully transformed) attributes of types:
123 /// - point3{h,f,d} (and Arrays thereof)
124 /// - vector3{h,f,d} (and Arrays thereof)
125 /// - schema attributes that define a length, such as Sphere.radius
126 /// and Boundable.extent
127 ///
128 /// It may be tempting to interpret raw attribute values, and even
129 /// xformOp:translate values in the *metersPerUnit* metric, but because any
130 /// xformOp:scale transformations will change the metric of the authored
131 /// attribute, and in fact we *require* this sort of scale change when
132 /// referencing assets of differing metrics, it is unreliable to do so.
133 
134 /// @{
135 
136 /// Return *stage*'s authored *metersPerUnit*, or 0.01 if unauthored.
137 /// \sa UsdGeomLinearUnits_group
139 double UsdGeomGetStageMetersPerUnit(const UsdStageWeakPtr &stage);
140 
141 /// Return whether *stage* has an authored *metersPerUnit*.
142 /// \sa UsdGeomLinearUnits_group
145 
146 /// Author *stage*'s *metersPerUnit*.
147 ///
148 /// \return true if metersPerUnit was successfully set. The stage's
149 /// UsdEditTarget must be either its root layer or session layer.
150 /// \sa UsdGeomLinearUnits_group
153  double metersPerUnit);
154 
155 /// Return *true* if the two given metrics are within the provided
156 /// relative *epsilon* of each other, when you need to know an absolute
157 /// metric rather than a scaling factor.
158 ///
159 /// Use like so:
160 /// \code
161 /// double stageUnits = UsdGeomGetStageMetersPerUnit(stage);
162 ///
163 /// if (UsdGeomLinearUnitsAre(stageUnits, UsdGeomLinearUnits::meters))
164 /// // do something for meters
165 /// else if (UsdGeomLinearUnitsAre(stageUnits, UsdGeomLinearUnits::feet))
166 /// // do something for feet
167 /// \endcode
168 ///
169 /// \return *false* if either input is zero or negative, otherwise relative
170 /// floating-point comparison between the two inputs.
171 /// \sa UsdGeomLinearUnits_group
173 bool UsdGeomLinearUnitsAre(double authoredUnits, double standardUnits,
174  double epsilon = 1e-5);
175 
176 /// \class UsdGeomLinearUnits
177 /// Container class for static double-precision symbols representing common
178 /// units of measure expressed in meters.
179 /// \sa UsdGeomLinearUnits_group
181 public:
182  static constexpr double nanometers = 1e-9;
183  static constexpr double micrometers = 1e-6;
184  static constexpr double millimeters = 0.001;
185  static constexpr double centimeters = 0.01;
186  static constexpr double meters = 1.0;
187  static constexpr double kilometers = 1000;
188 
189  /// Measured for one year = 365.25 days
190  static constexpr double lightYears = 9.4607304725808e15;
191 
192  static constexpr double inches = 0.0254;
193  static constexpr double feet = 0.3048;
194  static constexpr double yards = 0.9144;
195  static constexpr double miles = 1609.344;
196 };
197 
198 /// @}
199 
201 
202 #endif // PXR_USD_USD_GEOM_METRICS_H
static constexpr double nanometers
Definition: metrics.h:182
USDGEOM_API bool UsdGeomStageHasAuthoredMetersPerUnit(const UsdStageWeakPtr &stage)
static constexpr double centimeters
Definition: metrics.h:185
static constexpr double miles
Definition: metrics.h:195
UsdStagePtr UsdStageWeakPtr
Definition: common.h:38
USDGEOM_API TfToken UsdGeomGetStageUpAxis(const UsdStageWeakPtr &stage)
static constexpr double micrometers
Definition: metrics.h:183
Definition: token.h:70
static constexpr double feet
Definition: metrics.h:193
static constexpr double inches
Definition: metrics.h:192
static constexpr double lightYears
Measured for one year = 365.25 days.
Definition: metrics.h:190
USDGEOM_API bool UsdGeomLinearUnitsAre(double authoredUnits, double standardUnits, double epsilon=1e-5)
static constexpr double kilometers
Definition: metrics.h:187
PXR_NAMESPACE_CLOSE_SCOPE PXR_NAMESPACE_OPEN_SCOPE
Definition: path.h:1425
static constexpr double yards
Definition: metrics.h:194
USDGEOM_API bool UsdGeomSetStageUpAxis(const UsdStageWeakPtr &stage, const TfToken &axis)
#define PXR_NAMESPACE_CLOSE_SCOPE
Definition: pxr.h:74
static constexpr double millimeters
Definition: metrics.h:184
USDGEOM_API bool UsdGeomSetStageMetersPerUnit(const UsdStageWeakPtr &stage, double metersPerUnit)
#define USDGEOM_API
Definition: api.h:23
static constexpr double meters
Definition: metrics.h:186
USDGEOM_API TfToken UsdGeomGetFallbackUpAxis()
USDGEOM_API double UsdGeomGetStageMetersPerUnit(const UsdStageWeakPtr &stage)