Skip to content

Commit 5d05f55

Browse files
authored
Merge 9617797 into b7447df
2 parents b7447df + 9617797 commit 5d05f55

14 files changed

Lines changed: 604 additions & 15 deletions

File tree

test/usd/upAxisY.usda

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,6 @@
66

77
def "shapes"
88
{
9-
def PhysicsScene "physics"
10-
{
11-
vector3f physics:gravityDirection = (0, 0, -1)
12-
float physics:gravityMagnitude = 9.8
13-
}
14-
159
def Xform "ground_plane"
1610
{
1711
float3 xformOp:rotateXYZ = (0, 0, 0)
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
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_PARSER_HH
19+
#define SDF_USD_USD_PARSER_PARSER_HH
20+
21+
#include <string>
22+
23+
#include "sdf/config.hh"
24+
#include "sdf/usd/Export.hh"
25+
#include "sdf/usd/UsdError.hh"
26+
27+
namespace sdf
28+
{
29+
// Inline bracket to help doxygen filtering.
30+
inline namespace SDF_VERSION_NAMESPACE {
31+
//
32+
namespace usd
33+
{
34+
/// \brief Parse a USD file and convert it to a SDF file
35+
/// \param[in] _inputFilenameUsd Path of the USD file to parse
36+
/// \param[in] _outputFilenameSdf Path where the SDF file will be located
37+
/// \return UsdErrors, which is a vector of UsdError objects. Each UsdError
38+
/// includes an error code and message. An empty vector indicates no error
39+
/// occurred when parsing the USD file to its SDF representation.
40+
UsdErrors IGNITION_SDFORMAT_USD_VISIBLE parseUSDFile(
41+
const std::string &_inputFilenameUsd,
42+
const std::string &_outputFilenameSdf);
43+
}
44+
}
45+
}
46+
#endif

usd/src/CMakeLists.txt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,13 @@ set(sources
1010
sdf_parser/Sensor.cc
1111
sdf_parser/Visual.cc
1212
sdf_parser/World.cc
13+
usd_parser/Parser.cc
14+
usd_parser/USD2SDF.cc
1315
usd_parser/USDData.cc
1416
usd_parser/USDMaterial.cc
17+
usd_parser/USDPhysics.cc
1518
usd_parser/USDStage.cc
19+
usd_parser/USDWorld.cc
1620
)
1721

1822
ign_add_component(usd SOURCES ${sources} GET_TARGET_NAME usd_target)
@@ -41,6 +45,7 @@ set(gtest_sources
4145
sdf_parser/Visual_Sdf2Usd_TEST.cc
4246
sdf_parser/World_Sdf2Usd_TEST.cc
4347
usd_parser/USDData_TEST.cc
48+
usd_parser/USDPhysics_TEST.cc
4449
usd_parser/USDStage_TEST.cc
4550
Conversions_TEST.cc
4651
UsdError_TEST.cc

usd/src/cmd/usd2sdf.cc

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020

2121
#include <ignition/utils/cli/CLI.hpp>
2222

23+
#include "sdf/usd/usd_parser/Parser.hh"
2324
#include "sdf/config.hh"
2425

2526
//////////////////////////////////////////////////
@@ -43,10 +44,17 @@ struct Options
4344
std::string outputFilename{"output.sdf"};
4445
};
4546

46-
void runCommand(const Options &/*_opt*/)
47+
void runCommand(const Options &_opt)
4748
{
48-
// TODO(ahcorde): Call here the USD to SDF converter code
49-
std::cerr << "USD to SDF conversion is not implemented yet.\n";
49+
const auto errors =
50+
sdf::usd::parseUSDFile(_opt.inputFilename, _opt.outputFilename);
51+
if (!errors.empty())
52+
{
53+
std::cerr << "Errors occurred when generating [" << _opt.outputFilename
54+
<< "] from [" << _opt.inputFilename << "]:\n";
55+
for (const auto &e : errors)
56+
std::cerr << "\t" << e << "\n";
57+
}
5058
}
5159

5260
void addFlags(CLI::App &_app)

usd/src/usd_parser/Parser.cc

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
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+
#include "sdf/usd/usd_parser/Parser.hh"
19+
#include "USD2SDF.hh"
20+
21+
#include "sdf/Root.hh"
22+
23+
namespace sdf
24+
{
25+
inline namespace SDF_VERSION_NAMESPACE {
26+
namespace usd
27+
{
28+
UsdErrors parseUSDFile(
29+
const std::string &_inputFilenameUsd,
30+
const std::string &_outputFilenameSdf)
31+
{
32+
UsdErrors errors;
33+
USD2SDF usd2sdf;
34+
sdf::Root root;
35+
errors = usd2sdf.Read(_inputFilenameUsd, root);
36+
if (!errors.empty())
37+
{
38+
return errors;
39+
}
40+
41+
std::ofstream out(_outputFilenameSdf.c_str(), std::ios::out);
42+
std::string string = root.ToElement()->ToString("");
43+
if (!out)
44+
{
45+
errors.emplace_back(UsdError(
46+
UsdErrorCode::SDF_TO_USD_PARSING_ERROR,
47+
"Unable to open file [" + _outputFilenameSdf + "] for writing"));
48+
return errors;
49+
}
50+
out << string;
51+
out.close();
52+
return errors;
53+
}
54+
}
55+
}
56+
}

usd/src/usd_parser/USD2SDF.cc

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
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+
#include "USD2SDF.hh"
19+
20+
#include "sdf/Console.hh"
21+
#include "sdf/Types.hh"
22+
#include "USDWorld.hh"
23+
24+
#include "sdf/Root.hh"
25+
#include "sdf/World.hh"
26+
27+
namespace sdf {
28+
inline namespace SDF_VERSION_NAMESPACE {
29+
namespace usd {
30+
////////////////////////////////////////////////
31+
UsdErrors USD2SDF::Read(const std::string &_fileName,
32+
sdf::Root &_root)
33+
{
34+
UsdErrors errors;
35+
36+
sdf::World sdfWorld;
37+
38+
const auto errorsParseUSD = parseUSDWorld(_fileName, sdfWorld);
39+
if (!errorsParseUSD.empty())
40+
{
41+
errors.emplace_back(UsdError(
42+
UsdErrorCode::SDF_TO_USD_PARSING_ERROR,
43+
"Error parsing usd file [" + _fileName + "]"));
44+
return errors;
45+
}
46+
47+
auto addWorldErrors = _root.AddWorld(sdfWorld);
48+
if (!addWorldErrors.empty())
49+
{
50+
for (auto & error: addWorldErrors)
51+
{
52+
errors.emplace_back(error);
53+
}
54+
errors.emplace_back(UsdError(
55+
UsdErrorCode::SDF_ERROR,
56+
"Error adding the world [" + sdfWorld.Name() + "]"));
57+
return errors;
58+
}
59+
60+
return errors;
61+
}
62+
}
63+
}
64+
}

usd/src/usd_parser/USD2SDF.hh

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
/*
2+
* Copyright 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 USD_PARSER_USD2SDF_HH_
19+
#define USD_PARSER_USD2SDF_HH_
20+
21+
#include <string>
22+
23+
#include "sdf/sdf_config.h"
24+
#include "sdf/usd/UsdError.hh"
25+
26+
#include "sdf/Root.hh"
27+
28+
namespace sdf
29+
{
30+
// Inline bracket to help doxygen filtering.
31+
inline namespace SDF_VERSION_NAMESPACE {
32+
namespace usd
33+
{
34+
/// \brief USD to SDF converter
35+
/// This class helps generate the SDF file
36+
class USD2SDF
37+
{
38+
/// \brief constructor
39+
public: USD2SDF() = default;
40+
41+
/// \brief convert USD file to sdf xml document
42+
/// \param[in] _fileMame string containing USD filename.
43+
/// \param[out] _sdfXmlDoc Document to populate with the sdf model.
44+
/// \return UsdErrors, which is a list of UsdError objects. An empty list
45+
/// means no errors occurred when populating _sdfXmlOut with the contents
46+
/// of _fileName
47+
public: UsdErrors Read(
48+
const std::string &_fileName,
49+
sdf::Root &_root);
50+
};
51+
}
52+
}
53+
}
54+
55+
#endif

usd/src/usd_parser/USDPhysics.cc

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
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+
#include "USDPhysics.hh"
19+
20+
#pragma push_macro ("__DEPRECATED")
21+
#undef __DEPRECATED
22+
#include <pxr/base/gf/vec3d.h>
23+
#include <pxr/usd/usdPhysics/scene.h>
24+
#pragma pop_macro ("__DEPRECATED")
25+
26+
#include "sdf/World.hh"
27+
28+
namespace sdf
29+
{
30+
inline namespace SDF_VERSION_NAMESPACE {
31+
namespace usd
32+
{
33+
void ParseUSDPhysicsScene(
34+
const pxr::UsdPhysicsScene &_scene,
35+
sdf::World &_world,
36+
double _metersPerUnit)
37+
{
38+
ignition::math::Vector3d worldGravity{0, 0, -1};
39+
float magnitude {9.8f};
40+
const auto gravityAttr = _scene.GetGravityDirectionAttr();
41+
if (gravityAttr)
42+
{
43+
pxr::GfVec3f gravity;
44+
gravityAttr.Get(&gravity);
45+
if (!ignition::math::equal(0.0f, gravity[0]) &&
46+
!ignition::math::equal(0.0f, gravity[1]) &&
47+
!ignition::math::equal(0.0f, gravity[2]))
48+
{
49+
worldGravity[0] = gravity[0];
50+
worldGravity[1] = gravity[1];
51+
worldGravity[2] = gravity[2];
52+
}
53+
}
54+
55+
const auto magnitudeAttr = _scene.GetGravityMagnitudeAttr();
56+
if (magnitudeAttr)
57+
{
58+
magnitudeAttr.Get(&magnitude);
59+
if (!std::isnan(magnitude) && !std::isinf(magnitude))
60+
{
61+
magnitude = magnitude * _metersPerUnit;
62+
}
63+
}
64+
_world.SetGravity(worldGravity * magnitude);
65+
}
66+
}
67+
}
68+
}

0 commit comments

Comments
 (0)