-
Notifications
You must be signed in to change notification settings - Fork 102
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
USD -> SDF: Parse Physics and generated SDF file (#863)
Signed-off-by: ahcorde <[email protected]> Co-authored-by: Ashton Larkin <[email protected]>
- Loading branch information
Showing
14 changed files
with
611 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
/* | ||
* Copyright (C) 2022 Open Source Robotics Foundation | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
* | ||
*/ | ||
|
||
#ifndef SDF_USD_USD_PARSER_PARSER_HH | ||
#define SDF_USD_USD_PARSER_PARSER_HH | ||
|
||
#include <string> | ||
|
||
#include "sdf/config.hh" | ||
#include "sdf/usd/Export.hh" | ||
#include "sdf/usd/UsdError.hh" | ||
|
||
namespace sdf | ||
{ | ||
// Inline bracket to help doxygen filtering. | ||
inline namespace SDF_VERSION_NAMESPACE { | ||
// | ||
namespace usd | ||
{ | ||
/// \brief Parse a USD file and convert it to a SDF file | ||
/// \param[in] _inputFilenameUsd Path of the USD file to parse | ||
/// \param[in] _outputFilenameSdf Path where the SDF file will be located | ||
/// \return UsdErrors, which is a vector of UsdError objects. Each UsdError | ||
/// includes an error code and message. An empty vector indicates no error | ||
/// occurred when parsing the USD file to its SDF representation. | ||
UsdErrors IGNITION_SDFORMAT_USD_VISIBLE parseUSDFile( | ||
const std::string &_inputFilenameUsd, | ||
const std::string &_outputFilenameSdf); | ||
} | ||
} | ||
} | ||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
/* | ||
* Copyright (C) 2022 Open Source Robotics Foundation | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
* | ||
*/ | ||
|
||
#include "sdf/usd/usd_parser/Parser.hh" | ||
#include "USD2SDF.hh" | ||
|
||
#include "sdf/Root.hh" | ||
|
||
namespace sdf | ||
{ | ||
inline namespace SDF_VERSION_NAMESPACE { | ||
namespace usd | ||
{ | ||
UsdErrors parseUSDFile( | ||
const std::string &_inputFilenameUsd, | ||
const std::string &_outputFilenameSdf) | ||
{ | ||
UsdErrors errors; | ||
USD2SDF usd2sdf; | ||
sdf::Root root; | ||
errors = usd2sdf.Read(_inputFilenameUsd, root); | ||
if (!errors.empty()) | ||
{ | ||
return errors; | ||
} | ||
|
||
std::ofstream out(_outputFilenameSdf.c_str(), std::ios::out); | ||
if (!out) | ||
{ | ||
errors.emplace_back(UsdError( | ||
UsdErrorCode::SDF_TO_USD_PARSING_ERROR, | ||
"Unable to open file [" + _outputFilenameSdf + "] for writing")); | ||
return errors; | ||
} | ||
out << root.ToElement()->ToString(""); | ||
out.close(); | ||
return errors; | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
/* | ||
* Copyright (C) 2022 Open Source Robotics Foundation | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
* | ||
*/ | ||
|
||
#include "USD2SDF.hh" | ||
|
||
#include "sdf/Console.hh" | ||
#include "sdf/Types.hh" | ||
#include "USDWorld.hh" | ||
|
||
#include "sdf/Error.hh" | ||
#include "sdf/Root.hh" | ||
#include "sdf/World.hh" | ||
|
||
#include "sdf/usd/UsdError.hh" | ||
|
||
namespace sdf { | ||
inline namespace SDF_VERSION_NAMESPACE { | ||
namespace usd { | ||
//////////////////////////////////////////////// | ||
UsdErrors USD2SDF::Read(const std::string &_fileName, | ||
sdf::Root &_root) | ||
{ | ||
UsdErrors errors; | ||
|
||
sdf::World sdfWorld; | ||
|
||
errors = parseUSDWorld(_fileName, sdfWorld); | ||
if (!errors.empty()) | ||
{ | ||
errors.emplace_back(UsdError( | ||
UsdErrorCode::SDF_TO_USD_PARSING_ERROR, | ||
"Error parsing usd file [" + _fileName + "]")); | ||
return errors; | ||
} | ||
|
||
const auto addWorldErrors = _root.AddWorld(sdfWorld); | ||
if (!addWorldErrors.empty()) | ||
{ | ||
for (const auto & error: addWorldErrors) | ||
{ | ||
errors.emplace_back(error); | ||
} | ||
errors.emplace_back(UsdError(sdf::Error( | ||
sdf::ErrorCode::ELEMENT_INVALID, | ||
"Error adding the world [" + sdfWorld.Name() + "] to the SDF DOM"))); | ||
} | ||
|
||
return errors; | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
/* | ||
* Copyright 2022 Open Source Robotics Foundation | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
* | ||
*/ | ||
|
||
#ifndef USD_PARSER_USD2SDF_HH_ | ||
#define USD_PARSER_USD2SDF_HH_ | ||
|
||
#include <string> | ||
|
||
#include "sdf/sdf_config.h" | ||
#include "sdf/usd/UsdError.hh" | ||
|
||
#include "sdf/Root.hh" | ||
|
||
namespace sdf | ||
{ | ||
// Inline bracket to help doxygen filtering. | ||
inline namespace SDF_VERSION_NAMESPACE { | ||
namespace usd | ||
{ | ||
/// \brief USD to SDF converter | ||
/// This class helps generate the SDF file | ||
class USD2SDF | ||
{ | ||
/// \brief constructor | ||
public: USD2SDF() = default; | ||
|
||
/// \brief convert USD file to a sdf::Root object | ||
/// \param[in] _fileName string containing USD filename. | ||
/// \param[out] _root Root element to populate with the equivalent sdf | ||
/// information from _fileName. | ||
/// \return UsdErrors, which is a list of UsdError objects. An empty list | ||
/// means no errors occurred when populating _root with the contents | ||
/// of _fileName | ||
public: UsdErrors Read( | ||
const std::string &_fileName, | ||
sdf::Root &_root); | ||
}; | ||
} | ||
} | ||
} | ||
|
||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
/* | ||
* Copyright (C) 2022 Open Source Robotics Foundation | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
* | ||
*/ | ||
|
||
#include "USDPhysics.hh" | ||
|
||
#pragma push_macro ("__DEPRECATED") | ||
#undef __DEPRECATED | ||
#include <pxr/base/gf/vec3d.h> | ||
#include <pxr/usd/usdPhysics/scene.h> | ||
#pragma pop_macro ("__DEPRECATED") | ||
|
||
#include "sdf/World.hh" | ||
|
||
namespace sdf | ||
{ | ||
inline namespace SDF_VERSION_NAMESPACE { | ||
namespace usd | ||
{ | ||
void ParseUSDPhysicsScene( | ||
const pxr::UsdPhysicsScene &_scene, | ||
sdf::World &_world, | ||
double _metersPerUnit) | ||
{ | ||
ignition::math::Vector3d worldGravity{0, 0, -1}; | ||
float magnitude {9.8f}; | ||
const auto gravityAttr = _scene.GetGravityDirectionAttr(); | ||
if (gravityAttr) | ||
{ | ||
pxr::GfVec3f gravity; | ||
gravityAttr.Get(&gravity); | ||
if (!ignition::math::equal(0.0f, gravity[0]) && | ||
!ignition::math::equal(0.0f, gravity[1]) && | ||
!ignition::math::equal(0.0f, gravity[2])) | ||
{ | ||
worldGravity[0] = gravity[0]; | ||
worldGravity[1] = gravity[1]; | ||
worldGravity[2] = gravity[2]; | ||
} | ||
} | ||
|
||
const auto magnitudeAttr = _scene.GetGravityMagnitudeAttr(); | ||
if (magnitudeAttr) | ||
{ | ||
magnitudeAttr.Get(&magnitude); | ||
if (!std::isnan(magnitude) && !std::isinf(magnitude)) | ||
{ | ||
magnitude = magnitude * _metersPerUnit; | ||
} | ||
} | ||
_world.SetGravity(worldGravity * magnitude); | ||
} | ||
} | ||
} | ||
} |
Oops, something went wrong.