Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add sdf -> usd light functionality to usd component #818

Merged
merged 30 commits into from
Jan 25, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
30 commits
Select commit Hold shift + click to select a range
5e13b49
make SDF to USD a separate component of sdformat
adlarkin Jan 6, 2022
fa4568b
Added ignition-common4 as a dependency
ahcorde Jan 7, 2022
3443bbb
add light converting code to usd component
adlarkin Jan 8, 2022
18f3ac1
create world prim and add physics information
adlarkin Jan 10, 2022
3bbd6ea
add unit test for world conversion
adlarkin Jan 11, 2022
16e0789
Added ci to compile with USD
ahcorde Jan 11, 2022
0ea1ac9
Merge branch 'ahcorde/sdf_to_usd_cmake' into adlarkin/add_lights_to_u…
adlarkin Jan 11, 2022
4cdd054
cleanup light code before first review
adlarkin Jan 12, 2022
b32b0f3
remove extra includes, add symbol visibility
adlarkin Jan 12, 2022
ae3ee43
Added feedback
ahcorde Jan 17, 2022
36d42fc
improved doc
ahcorde Jan 17, 2022
4545723
update CMake code
adlarkin Jan 18, 2022
2d0e6d7
Merge branch 'ahcorde/sdf_to_usd_cmake' into adlarkin/add_lights_to_u…
adlarkin Jan 19, 2022
91629c0
included namespaces
ahcorde Jan 19, 2022
6347953
Added basic test to sdf2usd cmd
ahcorde Jan 19, 2022
bb6ee5a
changes in sdf2usd
ahcorde Jan 19, 2022
d1d7af6
Fixed usd tutorial
ahcorde Jan 19, 2022
bab9e37
Removed unneeded CMakeLists.txt
ahcorde Jan 19, 2022
cbdfa49
Moved CMakeLists.txt
ahcorde Jan 19, 2022
99fb170
update example docs and other nits before merge
adlarkin Jan 19, 2022
819d5f5
Merge branch 'sdf12' into ahcorde/sdf_to_usd_cmake
adlarkin Jan 19, 2022
92eca63
clean CI
ahcorde Jan 19, 2022
0618353
Fixed CMake warning
ahcorde Jan 19, 2022
bcc282b
Fixed cmake
ahcorde Jan 19, 2022
0fd2568
reverted changes in CMakeLists.txt
ahcorde Jan 19, 2022
d1c7b1a
Merge branch 'ahcorde/sdf_to_usd_cmake' into adlarkin/add_lights_to_u…
adlarkin Jan 19, 2022
5d2c902
fix namespaces and build warnings, use sdf::Errors, set usdLux intens…
adlarkin Jan 19, 2022
3b292c6
Merge branch 'sdf12' into adlarkin/add_lights_to_usd_cmake
adlarkin Jan 24, 2022
d14dc37
update to work with sdf12 branch
adlarkin Jan 24, 2022
8073b07
Merge branch 'sdf12' into adlarkin/add_lights_to_usd_cmake
ahcorde Jan 25, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 57 additions & 0 deletions usd/include/sdf/usd/sdf_parser/Light.hh
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
/*
* 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_SDF_PARSER_LIGHT_HH_
#define SDF_USD_SDF_PARSER_LIGHT_HH_

#include <string>

// TODO(adlarkin):this is to remove deprecated "warnings" in usd, these warnings
// are reported using #pragma message so normal diagnostic flags cannot remove
// them. This workaround requires this block to be used whenever usd is
// included.
#pragma push_macro ("__DEPRECATED")
#undef __DEPRECATED
#include <pxr/usd/usd/stage.h>
#pragma pop_macro ("__DEPRECATED")

#include "sdf/config.hh"
#include "sdf/system_util.hh"
#include "sdf/Light.hh"

namespace sdf
{
// Inline bracke to help doxygen filtering.
inline namespace SDF_VERSION_NAMESPACE {
//
namespace usd
{
/// \brief Parse an SDF light into a USD stage.
/// \param[in] _light The SDF light to parse.
/// \param[in] _stage The stage that should contain the USD representation
/// of _light.
/// \param[in] _path The USD path of the parsed light in _stage, which must
/// be a valid USD path.
/// \return Errors, which is a vector of Error objects. Each Error includes
/// an error code and message. An empty vector indicates no error.
sdf::Errors SDFORMAT_VISIBLE ParseSdfLight(const sdf::Light &_light,
pxr::UsdStageRefPtr &_stage, const std::string &_path);
}
}
}

#endif
94 changes: 94 additions & 0 deletions usd/include/sdf/usd/sdf_parser/Utils.hh
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
/*
* 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_SDF_PARSER_UTILS_HH_
#define SDF_USD_SDF_PARSER_UTILS_HH_

#include <ignition/math/Angle.hh>
#include <ignition/math/Pose3.hh>
#include <ignition/math/Vector3.hh>
#include <ignition/math/Quaternion.hh>

// TODO(adlarkin):this is to remove deprecated "warnings" in usd, these warnings
// are reported using #pragma message so normal diagnostic flags cannot remove
// them. This workaround requires this block to be used whenever usd is
// included.
#pragma push_macro ("__DEPRECATED")
#undef __DEPRECATED
#include <pxr/base/gf/vec3d.h>
#include <pxr/usd/usd/stage.h>
#include <pxr/usd/usdGeom/xformCommonAPI.h>
#pragma pop_macro ("__DEPRECATED")

#include "sdf/SemanticPose.hh"
#include "sdf/config.hh"
#include "sdf/system_util.hh"

namespace sdf
{
// Inline bracke to help doxygen filtering.
inline namespace SDF_VERSION_NAMESPACE {
//
namespace usd
{
/// \brief Get an object's pose w.r.t. its parent.
/// \param[in] _obj The object whose pose should be computed/retrieved.
/// \tparam T An object that has the following method signatures:
/// sdf::SemanticPose SemanticPose();
/// \return _obj's pose w.r.t. its parent. If there was an error computing
/// this pose, the pose's position will be NaNs.
template <typename T>
inline ignition::math::Pose3d SDFORMAT_VISIBLE PoseWrtParent(const T &_obj)
{
ignition::math::Pose3d pose(ignition::math::Vector3d::NaN,
ignition::math::Quaterniond::Identity);
auto errors = _obj.SemanticPose().Resolve(pose, "");
if (!errors.empty())
{
std::cerr << "Errors occurred when resolving the pose of ["
<< _obj.Name() << "] w.r.t its parent:\n\t" << errors;
}
return pose;
}

/// \brief Set the pose of a USD prim.
/// \param[in] _pose The pose to set.
/// \param[in] _stage The stage that contains the USD prim at path _usdPath.
/// \param[in] _usdPath The path to the USD prim that should have its
/// pose modified to match _pose.
inline void SDFORMAT_VISIBLE SetPose(const ignition::math::Pose3d &_pose,
pxr::UsdStageRefPtr &_stage, const pxr::SdfPath &_usdPath)
{
pxr::UsdGeomXformCommonAPI geomXformAPI(_stage->GetPrimAtPath(_usdPath));

const auto &position = _pose.Pos();
geomXformAPI.SetTranslate(pxr::GfVec3d(
position.X(), position.Y(), position.Z()));

const auto &rotation = _pose.Rot();
// roll/pitch/yaw from ignition::math::Pose3d are in radians, but this API
// call expects degrees
geomXformAPI.SetRotate(pxr::GfVec3f(
ignition::math::Angle(rotation.Roll()).Degree(),
ignition::math::Angle(rotation.Pitch()).Degree(),
ignition::math::Angle(rotation.Yaw()).Degree()));
}
}
}
}

#endif
2 changes: 2 additions & 0 deletions usd/src/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
set(sources
sdf_parser/Light.cc
sdf_parser/World.cc
)

Expand All @@ -17,6 +18,7 @@ target_link_libraries(${usd_target}

set(gtest_sources
sdf_parser/sdf2usd_TEST.cc
sdf_parser/Light_Sdf2Usd_TEST.cc
sdf_parser/World_Sdf2Usd_TEST.cc
)

Expand Down
100 changes: 100 additions & 0 deletions usd/src/sdf_parser/Light.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
/*
* 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/sdf_parser/Light.hh"

#include <string>

#include <pxr/base/tf/token.h>
#include <pxr/usd/sdf/path.h>
#include <pxr/usd/sdf/types.h>
#include <pxr/usd/usdLux/diskLight.h>
#include <pxr/usd/usdLux/distantLight.h>
#include <pxr/usd/usdLux/lightAPI.h>
#include <pxr/usd/usdLux/sphereLight.h>
#include <pxr/usd/usd/prim.h>
#pragma push_macro ("__DEPRECATED")
#undef __DEPRECATED
#include <pxr/usd/usd/stage.h>
#pragma pop_macro ("__DEPRECATED")

#include "sdf/Light.hh"
#include "sdf/usd/sdf_parser/Utils.hh"

namespace sdf
{
// Inline bracke to help doxygen filtering.
inline namespace SDF_VERSION_NAMESPACE {
//
namespace usd
{
sdf::Errors ParseSdfLight(const sdf::Light &_light,
pxr::UsdStageRefPtr &_stage, const std::string &_path)
{
const pxr::SdfPath sdfLightPath(_path);
sdf::Errors errors;
switch (_light.Type())
{
case sdf::LightType::POINT:
{
auto pointLight =
pxr::UsdLuxSphereLight::Define(_stage, sdfLightPath);
pointLight.CreateTreatAsPointAttr().Set(true);
}
break;
case sdf::LightType::SPOT:
pxr::UsdLuxDiskLight::Define(_stage, sdfLightPath);
break;
case sdf::LightType::DIRECTIONAL:
pxr::UsdLuxDistantLight::Define(_stage, sdfLightPath);
break;
case sdf::LightType::INVALID:
default:
errors.push_back(sdf::Error(sdf::ErrorCode::ATTRIBUTE_INCORRECT_TYPE,
"The light type that was given cannot be parsed to USD."));
return errors;
}

// TODO(adlarkin) incorporate sdf::Light's <direction> somehow? According
// to the USD API, things like UsdLuxDistantLight and UsdLuxDiskLight emit
// light along the -Z axis, so I'm not sure if this can be changed.
sdf::usd::SetPose(sdf::usd::PoseWrtParent(_light), _stage, sdfLightPath);

// This is a workaround to set the light's intensity attribute. Using the
// UsdLuxLightAPI sets the light's "inputs:intensity" attribute, but isaac
// sim reads the light's "intensity" attribute. Both inputs:intensity and
// intensity are set to provide flexibility with other USD renderers
const float usdLightIntensity =
static_cast<float>(_light.Intensity()) * 100.0f;
auto lightPrim = _stage->GetPrimAtPath(sdfLightPath);
lightPrim.CreateAttribute(pxr::TfToken("intensity"),
pxr::SdfValueTypeNames->Float, false).Set(usdLightIntensity);
auto lightAPI = pxr::UsdLuxLightAPI(lightPrim);
lightAPI.CreateIntensityAttr().Set(usdLightIntensity);

// TODO(adlarkin) Other things to look at (there may be more):
// * exposure - I don't think SDF has this, but USD does. See the
// UsdLightAPI::GetExposureAttr method
// * diffuse, specular - USD takes it as a scalar multiplier,
// SDF takes it as a RGB color vector. Perhaps this can be handled by
// applying a material to a light

return errors;
}
}
}
}
Loading