Skip to content

Commit 7861815

Browse files
ahcordeadlarkin
andauthored
usd -> sdf: Read transforms (#871)
Signed-off-by: ahcorde <ahcorde@gmail.com> Co-authored-by: Ashton Larkin <42042756+adlarkin@users.noreply.github.com>
1 parent 6ecfcc2 commit 7861815

File tree

8 files changed

+727
-10
lines changed

8 files changed

+727
-10
lines changed

test/usd/nested_transforms.usda

+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
#usda 1.0
2+
(
3+
metersPerUnit = 0.01
4+
upAxis = "Z"
5+
)
6+
7+
def "transforms"
8+
{
9+
def Xform "nested_transforms_XYZ"
10+
{
11+
float3 xformOp:rotateXYZ = (0, 0, 90)
12+
double3 xformOp:translate = (1, 0, 0)
13+
uniform token[] xformOpOrder = ["xformOp:translate", "xformOp:rotateXYZ"]
14+
15+
def Xform "child_transform"
16+
{
17+
float3 xformOp:rotateXYZ = (0, 0, 0)
18+
double3 xformOp:translate = (1, 0, 0)
19+
uniform token[] xformOpOrder = ["xformOp:translate", "xformOp:rotateXYZ"]
20+
}
21+
}
22+
23+
def Xform "nested_transforms_ZYX"
24+
{
25+
float3 xformOp:rotateZYX = (90, 0, 0)
26+
double3 xformOp:translate = (1, 0, 0)
27+
uniform token[] xformOpOrder = ["xformOp:translate", "xformOp:rotateZYX"]
28+
29+
def Xform "child_transform"
30+
{
31+
float3 xformOp:rotateZYX = (0, 0, 0)
32+
double3 xformOp:translate = (1, 0, 0)
33+
uniform token[] xformOpOrder = ["xformOp:translate", "xformOp:rotateZYX"]
34+
}
35+
}
36+
}

test/usd/upAxisZ.usda

+8-8
Original file line numberDiff line numberDiff line change
@@ -120,9 +120,9 @@ def "shapes"
120120

121121
def Xform "sphere"
122122
{
123-
float3 xformOp:rotateXYZ = (0, 0, 0)
123+
float3 xformOp:rotateZYX = (-69, 31, -62)
124124
double3 xformOp:translate = (0, 1.5, 0.5)
125-
uniform token[] xformOpOrder = ["xformOp:translate", "xformOp:rotateXYZ"]
125+
uniform token[] xformOpOrder = ["xformOp:translate", "xformOp:rotateZYX"]
126126

127127
def Xform "sphere_link" (
128128
prepend apiSchemas = ["PhysicsRigidBodyAPI", "PhysicsMassAPI"]
@@ -155,9 +155,9 @@ def "shapes"
155155

156156
def Xform "capsule"
157157
{
158-
float3 xformOp:rotateXYZ = (0, 0, 0)
158+
float3 xformOp:rotateZYX = (15, 80, -55)
159159
double3 xformOp:translate = (0, -3, 0.5)
160-
uniform token[] xformOpOrder = ["xformOp:translate", "xformOp:rotateXYZ"]
160+
uniform token[] xformOpOrder = ["xformOp:translate", "xformOp:rotateZYX"]
161161

162162
def Xform "capsule_link" (
163163
prepend apiSchemas = ["PhysicsRigidBodyAPI", "PhysicsMassAPI"]
@@ -172,9 +172,9 @@ def "shapes"
172172

173173
def Xform "capsule_visual"
174174
{
175-
float3 xformOp:rotateXYZ = (0, 0, 0)
175+
float3 xformOp:rotateZYX = (0, 0, 90)
176176
double3 xformOp:translate = (0, 0, 0)
177-
uniform token[] xformOpOrder = ["xformOp:translate", "xformOp:rotateXYZ"]
177+
uniform token[] xformOpOrder = ["xformOp:translate", "xformOp:rotateZYX"]
178178

179179
def Capsule "geometry" (
180180
prepend apiSchemas = ["PhysicsCollisionAPI"]
@@ -191,7 +191,7 @@ def "shapes"
191191

192192
def Xform "ellipsoid"
193193
{
194-
float3 xformOp:rotateXYZ = (0, 0, 0)
194+
float3 xformOp:rotateXYZ = (15, 80, -55)
195195
double3 xformOp:translate = (0, 3, 0.5)
196196
uniform token[] xformOpOrder = ["xformOp:translate", "xformOp:rotateXYZ"]
197197

@@ -230,7 +230,7 @@ def "shapes"
230230
{
231231
float inputs:intensity = 1000
232232
float intensity = 100
233-
float3 xformOp:rotateXYZ = (0, 0, 0)
233+
float3 xformOp:rotateXYZ = (0, -35, 0)
234234
double3 xformOp:translate = (0, 0, 10)
235235
uniform token[] xformOpOrder = ["xformOp:translate", "xformOp:rotateXYZ"]
236236
}

usd/include/sdf/usd/usd_parser/USDData.hh

+1-1
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ namespace sdf
8282
/// \param[in] _name Name of the path to find
8383
/// \return A pair with the name of the stage and the data
8484
public: const std::pair<std::string, std::shared_ptr<sdf::usd::USDStage>>
85-
FindStage(const std::string &_name);
85+
FindStage(const std::string &_name) const;
8686

8787
public: friend std::ostream& operator<<(
8888
std::ostream& os, const USDData& data)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
/*
2+
* Copyright (C) 2022 Open Source Robotics Foundation
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*
16+
*/
17+
18+
#ifndef SDF_USD_USD_PARSER_USDTRANSFORMS_HH_
19+
#define SDF_USD_USD_PARSER_USDTRANSFORMS_HH_
20+
21+
#include <string>
22+
#include <vector>
23+
24+
#include <ignition/math/Pose3.hh>
25+
#include <ignition/math/Quaternion.hh>
26+
#include <ignition/math/Vector3.hh>
27+
28+
#include <ignition/utils/ImplPtr.hh>
29+
30+
// TODO(ahcorde) this is to remove deprecated "warnings" in usd, these warnings
31+
// are reported using #pragma message so normal diagnostic flags cannot remove
32+
// them. This workaround requires this block to be used whenever usd is
33+
// included.
34+
#pragma push_macro ("__DEPRECATED")
35+
#undef __DEPRECATED
36+
#include <pxr/usd/usdGeom/gprim.h>
37+
#pragma pop_macro ("__DEPRECATED")
38+
39+
#include "sdf/sdf_config.h"
40+
#include "sdf/system_util.hh"
41+
#include "sdf/usd/Export.hh"
42+
#include "sdf/usd/UsdError.hh"
43+
#include "USDData.hh"
44+
45+
namespace sdf
46+
{
47+
// Inline bracket to help doxygen filtering.
48+
inline namespace SDF_VERSION_NAMESPACE {
49+
//
50+
namespace usd
51+
{
52+
/// \brief This class stores the transforms of a schema
53+
/// This might contain scale, translate or rotation operations
54+
/// The booleans are used to check if there is a transform defined
55+
/// in the schema
56+
class IGNITION_SDFORMAT_USD_VISIBLE UDSTransforms
57+
{
58+
/// \brief Default constructor
59+
public: UDSTransforms();
60+
61+
/// \brief Translate
62+
/// \return A 3D vector with the translation
63+
public: const ignition::math::Vector3d Translation() const;
64+
65+
/// \brief Scale
66+
/// \return A 3D vector with the scale
67+
public: const ignition::math::Vector3d Scale() const;
68+
69+
/// \brief Get the Rotation
70+
/// \return Return The rotation, if one exists. If no rotation exists,
71+
/// std::nullopt is returned
72+
public: const std::optional<ignition::math::Quaterniond> Rotation() const;
73+
74+
/// \brief Set translate
75+
/// \param[in] _translate Translate to set
76+
public: void SetTranslation(const ignition::math::Vector3d &_translate);
77+
78+
/// \brief Set scale
79+
/// \param[in] _scale Scale to set
80+
public: void SetScale(const ignition::math::Vector3d &_scale);
81+
82+
/// \brief Set rotation
83+
/// \param[in] _q Quaternion that defines the rotation
84+
public: void SetRotation(const ignition::math::Quaterniond &_q);
85+
86+
/// \brief Private data pointer.
87+
IGN_UTILS_IMPL_PTR(dataPtr)
88+
};
89+
90+
/// \brief This function gets the transform from a prim to the specified
91+
/// _schemaToStop variable
92+
/// \param[in] _prim Initial prim to read the transform
93+
/// \param[in] _usdData USDData structure to get info about the prim, for
94+
/// example: metersperunit
95+
/// \param[out] _pose Pose of the prim. From _prim to _schemaToStop.
96+
/// \param[out] _scale The scale of the prim
97+
/// \param[in] _schemaToStop Name of the prim where the loop will stop
98+
/// reading transforms
99+
void IGNITION_SDFORMAT_USD_VISIBLE GetTransform(
100+
const pxr::UsdPrim &_prim,
101+
const USDData &_usdData,
102+
ignition::math::Pose3d &_pose,
103+
ignition::math::Vector3d &_scale,
104+
const std::string &_schemaToStop);
105+
106+
/// \brief Read the usd prim transforms. Scale, rotation or transform might
107+
/// be defined as float or doubles
108+
/// \param[in] _prim Prim where the transforms are read
109+
/// \return A USDTransforms class with all the transforms related to
110+
/// the prim
111+
UDSTransforms IGNITION_SDFORMAT_USD_VISIBLE ParseUSDTransform(
112+
const pxr::UsdPrim &_prim);
113+
}
114+
}
115+
}
116+
#endif

usd/src/CMakeLists.txt

+2
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ set(sources
1616
usd_parser/USDMaterial.cc
1717
usd_parser/USDPhysics.cc
1818
usd_parser/USDStage.cc
19+
usd_parser/USDTransforms.cc
1920
usd_parser/USDWorld.cc
2021
)
2122

@@ -47,6 +48,7 @@ set(gtest_sources
4748
usd_parser/USDData_TEST.cc
4849
usd_parser/USDPhysics_TEST.cc
4950
usd_parser/USDStage_TEST.cc
51+
usd_parser/USDTransforms_TEST.cc
5052
Conversions_TEST.cc
5153
UsdError_TEST.cc
5254
UsdUtils_TEST.cc

usd/src/usd_parser/USDData.cc

+1-1
Original file line numberDiff line numberDiff line change
@@ -236,7 +236,7 @@ namespace usd {
236236

237237
/////////////////////////////////////////////////
238238
const std::pair<std::string, std::shared_ptr<USDStage>>
239-
USDData::FindStage(const std::string &_name)
239+
USDData::FindStage(const std::string &_name) const
240240
{
241241
for (auto &ref : this->dataPtr->references)
242242
{

0 commit comments

Comments
 (0)