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

USD to SDF converter #736

Closed
wants to merge 30 commits into from
Closed

USD to SDF converter #736

wants to merge 30 commits into from

Conversation

ahcorde
Copy link
Collaborator

@ahcorde ahcorde commented Oct 27, 2021

🎉 New feature

Summary

FYI @adlarkin @koonpeng . Feel free to push code, cleanups or suggestions

This draft PR creates a prototype to convert USD files into SDF files.

For now I already tested with simple_articulated.usd and ur10.usd.

Regarding joints there are two fields physics:body0 and physics:body1 that indicate the parent and child of the joint, but in usd format there is no distinction between them. We need to rework this part a little bit more. I have to modify ur10.usda to set parent as body1 and child as body0. No changes are required in the simple_articulated.usd file.

The current implementation supports:

  • Links
    • Geometris: cylinders, spheres and boxes
    • Collisions
    • Color
  • Joints: fixed, revolute and prismatic
  • Physics (only fill data structure)

Code need a huge refactor. We can probably use some ignition library such as ignition-common or ignition-math in the usd_parser folders because I copy-paste the code from urdf. There are object like Sphere, poses, Vectors quaternions, links and many other that I'm pretty sure that we can use something from these libraries.

ur10

articulated_arm

Test it

  • Install PXR lbrary.
    • If you only want the cpp API you should compile the library with -DPXR_ENABLE_PYTHON_SUPPORT=0
    • if you compile the library with python support you will be able to use some of the usd tools such us:
      • usdcat: It allows to convert usd (binary format) into usda (human redeable format). `usdcat binary.usa > output.usda
      • usdview. It allows to visualize usd files
  • There is a cmd util called sdfconverter which allows to convert a urdf or usd files in to sdf. it's simple to use: sdfconverter -i <input> -o <ouput>

Checklist

  • Signed all commits for DCO
  • Added tests
  • Added example and/or tutorial
  • Updated documentation (as needed)
  • Updated migration guide (as needed)
  • codecheck passed (See contributing)
  • All tests passed (See test coverage)
  • While waiting for a review on your PR, please help review another open pull request to support the maintainers

Note to maintainers: Remember to use Squash-Merge

Signed-off-by: Alejandro Hernández <ahcorde@gmail.com>
Signed-off-by: ahcorde <ahcorde@gmail.com>
Signed-off-by: ahcorde <ahcorde@gmail.com>
@ahcorde ahcorde requested a review from adlarkin October 27, 2021 16:21
@osrf-triage osrf-triage added this to Inbox in Core development Oct 27, 2021
@chapulina chapulina moved this from Inbox to In progress in Core development Oct 27, 2021
@scpeters
Copy link
Member

Of all the parts of libsdformat, the parser_urdf code is probably the most in need of improvement. So building USD support off of this code is not the best foundation, though I understand why you would do it, since it is there and basically functional. If we could think of a better implementation for this type of conversion, I think it would be valuable, since this parser_urdf code is so hard to maintain.

cc @azeey

########################################
# Find ignition common
# Set a variable for generating ProjectConfig.cmake
find_package(ignition-common4 4.0 QUIET)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is ign-common needed for anything other than common::split? I'd avoid adding a dependency if we don't need it. We already have sdf::split that provides the same functionality.

@azeey
Copy link
Collaborator

azeey commented Oct 28, 2021

I agree with @scpeters about parser_urdf.cc. I would also vote putting this converter in a separate repository and keep libsdformat be more focused on parsing SDFormat. I'm concerned that adding a dependency like pxr would make it difficult for downstream applications that only use libsdformat for parsing SDFormat files (eg. Drake).

@chapulina
Copy link
Contributor

I would also vote putting this converter in a separate repository and keep libsdformat be more focused on parsing SDFormat.

Another possibility is keeping USD support in a separate component within this codebase. The advantage would be giving it more visibility, tighter maintenance, and maybe setting the standard for adding other converters in the future. My concern with a separate repository is that unless we make it a first-class Ignition library, it may just go unmaintained very quickly (similar to ign-rndf).

@scpeters
Copy link
Member

I would also vote putting this converter in a separate repository and keep libsdformat be more focused on parsing SDFormat.

Another possibility is keeping USD support in a separate component within this codebase. The advantage would be giving it more visibility, tighter maintenance, and maybe setting the standard for adding other converters in the future. My concern with a separate repository is that unless we make it a first-class Ignition library, it may just go unmaintained very quickly (similar to ign-rndf).

sdformat doesn't use ign-cmake2, so it would be some work to add a component to this package

@chapulina
Copy link
Contributor

sdformat doesn't use ign-cmake2, so it would be some work to add a component to this package

If this is something that we think would be valuable, it would be a good excuse to update SDFormat to use ign-cmake (#181)

@koonpeng
Copy link

koonpeng commented Nov 2, 2021

Another possibility is keeping USD support in a separate component within this codebase.

I'm very new to the ignition build system so I might be totally off, does a separate component means declaring it with ign_add_component? From what I understand, the function adds a "library or component" to a "core library", but I think this PR adds a new executable.

@scpeters
Copy link
Member

scpeters commented Nov 2, 2021

sdformat doesn't use ign-cmake2, so it would be some work to add a component to this package

If this is something that we think would be valuable, it would be a good excuse to update SDFormat to use ign-cmake (#181)

I've taken an initial step towards this in gazebosim/gz-cmake#190, which is awaiting review

@scpeters
Copy link
Member

scpeters commented Nov 2, 2021

Another possibility is keeping USD support in a separate component within this codebase.

I'm very new to the ignition build system so I might be totally off, does a separate component means declaring it with ign_add_component? From what I understand, the function adds a "library or component" to a "core library", but I think this PR adds a new executable.

There is an aspect that is more significant than whether the code produces a library or executable, which is to define a cmake component for packaging purposes. See for instance the discussion of the COMPONENTS keyword in the find_package documentation. A different component can be found installed and found independently, and is especially useful for isolating dependencies, so you can build or install the core components without needing all the dependencies used by other components.

Signed-off-by: ahcorde <ahcorde@gmail.com>
Signed-off-by: ahcorde <ahcorde@gmail.com>
Signed-off-by: ahcorde <ahcorde@gmail.com>
@adlarkin
Copy link
Contributor

adlarkin commented Nov 4, 2021

sdformat doesn't use ign-cmake2, so it would be some work to add a component to this package

It looks like ign-cmake2 is listed in sdformat's packages.apt file: https://github.com/ignitionrobotics/sdformat/blob/5385ce9500bd1662d52ba3d0c2c919d13f86c949/.github/ci/packages.apt#L1

Is this a mistake, or does sdformat actually depend on ign-cmake2 somehow?

@adlarkin
Copy link
Contributor

adlarkin commented Nov 4, 2021

Of all the parts of libsdformat, the parser_urdf code is probably the most in need of improvement. So building USD support off of this code is not the best foundation, though I understand why you would do it, since it is there and basically functional. If we could think of a better implementation for this type of conversion, I think it would be valuable, since this parser_urdf code is so hard to maintain.

On a somewhat related note, it looks like parser_urdf is deprecated and should have been removed by now (I think?): https://github.com/ignitionrobotics/sdformat/blob/913a7754c319190defa549bb73dc584e59f9734f/src/parser_urdf.hh#L35-L40

Do we need to wait until the next sdformat version to remove this class, or can we do it now since it's not installed?

@scpeters
Copy link
Member

scpeters commented Nov 4, 2021

sdformat doesn't use ign-cmake2, so it would be some work to add a component to this package

It looks like ign-cmake2 is listed in sdformat's packages.apt file:

https://github.com/ignitionrobotics/sdformat/blob/5385ce9500bd1662d52ba3d0c2c919d13f86c949/.github/ci/packages.apt#L1

Is this a mistake, or does sdformat actually depend on ign-cmake2 somehow?

libsdformat depends on ignition-cmake2 but isn't an ignition-cmake project that uses the cmake macros like ign_configure_project and ign_configure_build. Until recently, it only depended on ignition-cmake2 indirectly via ignition-math6, although it now uses IgnCodeCheck.cmake as of #682. It will take a bit more work (see #181) including changes in ign-cmake to allow libsdformat to use the ign-cmake cmake helpers to add a component to this package

@scpeters
Copy link
Member

scpeters commented Nov 4, 2021

Of all the parts of libsdformat, the parser_urdf code is probably the most in need of improvement. So building USD support off of this code is not the best foundation, though I understand why you would do it, since it is there and basically functional. If we could think of a better implementation for this type of conversion, I think it would be valuable, since this parser_urdf code is so hard to maintain.

On a somewhat related note, it looks like parser_urdf is deprecated and should have been removed by now (I think?):

https://github.com/ignitionrobotics/sdformat/blob/913a7754c319190defa549bb73dc584e59f9734f/src/parser_urdf.hh#L35-L40

Do we need to wait until the next sdformat version to remove this class, or can we do it now since it's not installed?

that comment is wrong; this code is still needed. parser_urdf.hh is a private header similar to FrameSemantics.hh

@adlarkin
Copy link
Contributor

adlarkin commented Nov 4, 2021

that comment is wrong; this code is still needed. parser_urdf.hh is a private header similar to FrameSemantics.hh

I made a PR that removes this comment in #740

It will take a bit more work (see #181) including changes in ign-cmake to allow libsdformat to use the ign-cmake cmake helpers to add a component to this package

Before we go ahead and make changes to ign-cmake, I think we should determine if we actually want to place the functionality of this PR into a separate component. If we decide on taking a different approach (for example, placing the USD converter in a separate repository), then it wouldn't make sense to put forth the effort of updating ign-cmake right now. Does everyone think we should place this PR in a separate component of sdformat, or should we consider other alternatives?

@scpeters
Copy link
Member

scpeters commented Nov 4, 2021

It will take a bit more work (see #181) including changes in ign-cmake to allow libsdformat to use the ign-cmake cmake helpers to add a component to this package

Before we go ahead and make changes to ign-cmake, I think we should determine if we actually want to place the functionality of this PR into a separate component. If we decide on taking a different approach (for example, placing the USD converter in a separate repository), then it wouldn't make sense to put forth the effort of updating ign-cmake right now. Does everyone think we should place this PR in a separate component of sdformat, or should we consider other alternatives?

If we want to perpetually support conversion between SDFormat and USD, I think there are advantages to keeping this code in the same repository. Since it requires additional dependencies, I think adding it in a separate component would be appropriate.

If we wanted to put this code in a separate repository, I think that would be a valid choice as well; it would just take a bit more effort to keep the code synchronized and manage version differences. It would also make it easier to limit our commitment to support of the USD format, for better or worse.

Signed-off-by: ahcorde <ahcorde@gmail.com>
Signed-off-by: ahcorde <ahcorde@gmail.com>
Signed-off-by: ahcorde <ahcorde@gmail.com>
This was referenced Nov 30, 2021
Signed-off-by: ahcorde <ahcorde@gmail.com>
Signed-off-by: ahcorde <ahcorde@gmail.com>
fit != triangulation.finite_facets_end(); ++fit)
{
auto &face = fit;
if (face->first->vertex(0)->info() > maxIndex ||
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maxIndex is not defined.


/////////////////////////////////////////////////
/// Main
int main(int argc, char **argv)
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This shouldn't be needed if you link to gtest_main.

{
auto opt = std::make_shared<Options>();

_app.add_option("-i,--input",
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: It will be nice to have the same cli for usd -> sdf and sdf -> usd converter, iirc, the sdf -> usd converter takes the input and output args as positionals.

@@ -713,6 +714,25 @@ bool readFileInternal(const std::string &_filename, const bool _convert,
return false;
}

if (USD2SDF::IsUSD(filename))
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is embedding the usd converter into the sdf parser something we should be doing?

auto doc = makeSdfDoc();
usd2g.read(filename, &doc);
sdferr << "here! it's a USD!\n";
if (sdf::readDoc(&doc, _sdf, "usd file", _convert, _config, _errors))
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't quite understand this, shouldn't the 3rd param be the filename?

{
std::cerr << "UsdPhysicsMassAPI " << pxr::TfStringify(_prim.GetPath()) << '\n';
if (pxr::UsdAttribute massAttribute =
_prim.GetAttribute(pxr::TfToken("physics:mass")))
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could we use pxr::UsdPhysicsMassApi to avoid hard coded tokens?

Comment on lines +452 to +455
if (mass < 0.0001)
{
mass = 0.1;
}
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

whats the reason for having a minimal mass?

Comment on lines +31 to +33
_world->gravity[0] = gravity[0];
_world->gravity[1] = gravity[1];
_world->gravity[2] = gravity[2];
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we need to account for up axis?

sdf::Camera camera;

auto variantCamera = pxr::UsdGeomCamera(_prim);
float horizontalAperture = 20.955;
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What is this value based off from?

{
pxr::SdfAssetPath materialPath;
pxr::UsdShadeInput diffuseTextureShaderInput =
variantshader.GetInput(pxr::TfToken("diffuse_texture"));
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there a doc explaining the available tokens?

@ahcorde
Copy link
Collaborator Author

ahcorde commented Jan 6, 2022

TODO

  • Review "Y axis" usd files
  • Generate triangles from a polygon (with more than 3 vertices per polygon)
    • Remove CGAL dependency, use instead gts
  • Cleanup
  • Document

adlarkin and others added 4 commits January 13, 2022 09:45
Signed-off-by: Ashton Larkin <42042756+adlarkin@users.noreply.github.com>
Signed-off-by: ahcorde <ahcorde@gmail.com>
Signed-off-by: ahcorde <ahcorde@gmail.com>
Signed-off-by: ahcorde <ahcorde@gmail.com>
@adlarkin adlarkin added the needs upstream release Blocked by a release of an upstream library label Jan 13, 2022
@adlarkin
Copy link
Contributor

adlarkin commented Jan 13, 2022

I've added the needs upstream release label to this PR because the build fails without gazebosim/gz-math#366. Based on the discussion had in gazebosim/gz-math#366 (comment), I'm wondering if this PR is actually needed - it seems like an unrelated change/addition in this PR is what's introducing the build error. I will follow-up on this if I find anything in this PR that could be related to the build error.

Copy link
Contributor

@adlarkin adlarkin left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This PR is really large and doesn't have documentation, so it's hard to review all at once. Once #817 is merged, would you mind refactoring this PR to match the component structure? Then, we can break it up into smaller PRs and review the smaller PRs, like what we're doing for SDF -> USD conversion.

Comment on lines +204 to +224
if (tokens.size() > 1)
{
bool shortName = false;
if (tokens.size() == 2)
{
if (prim.IsA<pxr::UsdGeomGprim>() ||
(std::string(prim.GetPrimTypeInfo().GetTypeName().GetText()) == "Plane"))
{
if (prim.GetPath().GetName() != "imu")
{
baseLink = "/" + tokens[0];
shortName = true;
// exit(-1);
}
}
}
if(!shortName)
{
baseLink = "/" + tokens[0] + "/" + tokens[1];
}
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it looks like this code block can be deleted, because tokens.size() > 1 is already being checked in the above if statement: https://github.com/ignitionrobotics/sdformat/blob/58ac549f454601bcf1f58917965c64d93617f527/src/usd/usd_parser/parser_usd.cc#L183

Comment on lines +303 to +306
if (pxr::TfStringify(parent.GetPath()) == _name)
{
return;
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This check can be removed, because it's comparing the prim's path (which has a /) with the prim's name (which does not have a /).

src/usd/usd_parser/utils.cc Outdated Show resolved Hide resolved
Core development automation moved this from In progress to In review Jan 15, 2022
Teo Koon Peng and others added 6 commits January 18, 2022 13:58
* polygon triangulation using the fan-triangulation algorithm

Signed-off-by: Teo Koon Peng <koonpeng@openrobotics.org>

* remove cgal dep

Signed-off-by: Teo Koon Peng <koonpeng@openrobotics.org>

* add todo

Signed-off-by: Teo Koon Peng <koonpeng@openrobotics.org>

* fix xformOp:transform parsing (#822)

Signed-off-by: Teo Koon Peng <koonpeng@openrobotics.org>

Co-authored-by: ahcorde <ahcorde@gmail.com>
Signed-off-by: ahcorde <ahcorde@gmail.com>
Signed-off-by: ahcorde <ahcorde@gmail.com>
Signed-off-by: ahcorde <ahcorde@gmail.com>
* fix y up

Signed-off-by: Teo Koon Peng <koonpeng@openrobotics.org>

* cleanup

Signed-off-by: Teo Koon Peng <koonpeng@openrobotics.org>
Signed-off-by: ahcorde <ahcorde@gmail.com>
@scpeters scpeters added the usd label Feb 21, 2022
Signed-off-by: ahcorde <ahcorde@gmail.com>
@chapulina chapulina moved this from In review to In progress in Core development Mar 12, 2022
@ahcorde
Copy link
Collaborator Author

ahcorde commented Apr 19, 2022

Closing this PR in favour of this metaticket #866

@ahcorde ahcorde closed this Apr 19, 2022
Core development automation moved this from In progress to Done Apr 19, 2022
@j-rivero j-rivero removed this from Done in Core development May 6, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
needs upstream release Blocked by a release of an upstream library usd
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

6 participants