Skip to content

Commit

Permalink
USD -> SDF: Added cmd line tool (#862)
Browse files Browse the repository at this point in the history
Signed-off-by: ahcorde <ahcorde@gmail.com>
Co-authored-by: Ashton Larkin <42042756+adlarkin@users.noreply.github.com>
  • Loading branch information
ahcorde and adlarkin authored Mar 10, 2022
1 parent 346f3ae commit b7447df
Show file tree
Hide file tree
Showing 4 changed files with 170 additions and 0 deletions.
1 change: 1 addition & 0 deletions usd/src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ target_link_libraries(${usd_target}

set(gtest_sources
sdf_parser/sdf2usd_TEST.cc
usd_parser/usd2sdf_TEST.cc
sdf_parser/Geometry_Sdf2Usd_TEST.cc
sdf_parser/Joint_Sdf2Usd_TEST.cc
sdf_parser/Light_Sdf2Usd_TEST.cc
Expand Down
11 changes: 11 additions & 0 deletions usd/src/cmd/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,20 @@ if(TARGET ${usd_target})
${usd_target}
)

add_executable(usd2sdf
usd2sdf.cc
)

target_link_libraries(usd2sdf
PUBLIC
ignition-utils${IGN_UTILS_VER}::ignition-utils${IGN_UTILS_VER}
${usd_target}
)

install(
TARGETS
sdf2usd
usd2sdf
DESTINATION
${BIN_INSTALL_DIR}
)
Expand Down
85 changes: 85 additions & 0 deletions usd/src/cmd/usd2sdf.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
/*
* 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 <iostream>
#include <string>

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

#include "sdf/config.hh"

//////////////////////////////////////////////////
/// \brief Enumeration of available commands
enum class Command
{
kNone,
};

//////////////////////////////////////////////////
/// \brief Structure to hold all filenames
struct Options
{
/// \brief Command to execute
Command command{Command::kNone};

/// \brief input filename
std::string inputFilename{"input.usd"};

/// \brief output filename
std::string outputFilename{"output.sdf"};
};

void runCommand(const Options &/*_opt*/)
{
// TODO(ahcorde): Call here the USD to SDF converter code
std::cerr << "USD to SDF conversion is not implemented yet.\n";
}

void addFlags(CLI::App &_app)
{
auto opt = std::make_shared<Options>();

_app.add_option("input",
opt->inputFilename,
"Input filename. Defaults to input.usd unless otherwise specified.");

_app.add_option("output",
opt->outputFilename,
"Output filename. Defaults to output.sdf unless otherwise specified.");

_app.callback([&_app, opt](){
runCommand(*opt);
});
}

//////////////////////////////////////////////////
int main(int argc, char** argv)
{
CLI::App app{"USD to SDF converter"};

app.set_help_all_flag("--help-all", "Show all help");

app.add_flag_callback("--version", [](){
std::cout << SDF_VERSION_FULL << std::endl;
throw CLI::Success();
});

addFlags(app);
CLI11_PARSE(app, argc, argv);

return 0;
}
73 changes: 73 additions & 0 deletions usd/src/usd_parser/usd2sdf_TEST.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
/*
* 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 <string>

#include <gtest/gtest.h>

#include <ignition/common/Filesystem.hh>
#include <ignition/utilities/ExtraTestMacros.hh>

#include "test_config.h"
#include "test_utils.hh"

#ifdef _WIN32
#define popen _popen
#define pclose _pclose
#endif

static std::string usd2sdfCommand()
{
return ignition::common::joinPaths(std::string(PROJECT_BINARY_DIR), "bin",
"usd2sdf");
}

/////////////////////////////////////////////////
std::string custom_exec_str(std::string _cmd)
{
_cmd += " 2>&1";
FILE *pipe = popen(_cmd.c_str(), "r");

if (!pipe)
return "ERROR";

char buffer[128];
std::string result = "";

while (!feof(pipe))
{
if (fgets(buffer, 128, pipe) != NULL)
result += buffer;
}

pclose(pipe);
return result;
}

/////////////////////////////////////////////////
TEST(version_cmd, IGN_UTILS_TEST_DISABLED_ON_WIN32(SDF))
{
// Check a good SDF file
{
std::string output =
custom_exec_str(usd2sdfCommand() + " --version");

EXPECT_EQ(output, std::string(SDF_VERSION_FULL) + "\n");

// TODO(ahcorde): Check the contents of outputUsdFilePath when the parser
// is implemented
}
}

0 comments on commit b7447df

Please sign in to comment.