Skip to content

Commit 3d7ba57

Browse files
authored
Merge branch 'sdf12' into ahcorde/usd_to_sdf_sensors
2 parents 7a277cc + b45fcd0 commit 3d7ba57

File tree

8 files changed

+68
-18
lines changed

8 files changed

+68
-18
lines changed

usd/src/UsdUtils.hh

+29
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@
1818
#ifndef SDF_USD_SDF_PARSER_UTILS_HH_
1919
#define SDF_USD_SDF_PARSER_UTILS_HH_
2020

21+
#include <string>
22+
2123
#include <ignition/math/Angle.hh>
2224
#include <ignition/math/Pose3.hh>
2325
#include <ignition/math/Vector3.hh>
@@ -34,6 +36,8 @@
3436
#include <pxr/usd/usdGeom/xformCommonAPI.h>
3537
#pragma pop_macro ("__DEPRECATED")
3638

39+
#include <ignition/common/Util.hh>
40+
3741
#include "sdf/Collision.hh"
3842
#include "sdf/Error.hh"
3943
#include "sdf/Geometry.hh"
@@ -51,6 +55,31 @@ namespace sdf
5155
//
5256
namespace usd
5357
{
58+
/// \brief Return a valid USD path
59+
/// Path must not:
60+
/// - start with a digit
61+
/// - Contain spaces
62+
/// - Contain dots
63+
/// - Contain dashes
64+
/// \param[in] _path Path to check
65+
/// \return A valid path
66+
inline std::string validPath(const std::string &_path)
67+
{
68+
std::string result;
69+
if (_path.empty())
70+
{
71+
return result;
72+
}
73+
result = ignition::common::replaceAll(_path, " ", "");
74+
result = ignition::common::replaceAll(result, ".", "_");
75+
result = ignition::common::replaceAll(result, "-", "_");
76+
if (std::isdigit(result[0]))
77+
{
78+
result = "_" + result;
79+
}
80+
return result;
81+
}
82+
5483
/// \brief Get an object's pose w.r.t. its parent.
5584
/// \param[in] _obj The object whose pose should be computed/retrieved.
5685
/// \param[out] _pose The pose of _obj w.r.t. its parent.

usd/src/UsdUtils_TEST.cc

+20
Original file line numberDiff line numberDiff line change
@@ -112,3 +112,23 @@ TEST(UsdUtils, IsPlane)
112112
mutableModel->SetStatic(false);
113113
EXPECT_FALSE(sdf::usd::IsPlane(*mutableModel));
114114
}
115+
116+
//////////////////////////////////////////////////
117+
TEST(UsdUtils, validPath)
118+
{
119+
const std::string alreadyValid = "/valid/path";
120+
EXPECT_EQ(alreadyValid, sdf::usd::validPath(alreadyValid));
121+
122+
EXPECT_EQ("", sdf::usd::validPath(""));
123+
124+
EXPECT_EQ("_0/start/with/digit", sdf::usd::validPath("0/start/with/digit"));
125+
126+
EXPECT_EQ("/hasSpaces", sdf::usd::validPath("/has Spaces"));
127+
128+
EXPECT_EQ("/has_period", sdf::usd::validPath("/has.period"));
129+
130+
EXPECT_EQ("/has_dash", sdf::usd::validPath("/has-dash"));
131+
132+
EXPECT_EQ("_5/has_period/hasSpace/has_dash",
133+
sdf::usd::validPath("5/has.period/has Space/has-dash"));
134+
}

usd/src/cmd/sdf2usd.cc

+4-6
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
#include "sdf/sdf.hh"
3636
#include "sdf/usd/sdf_parser/World.hh"
3737
#include "../sdf_parser/Model.hh"
38+
#include "../UsdUtils.hh"
3839

3940
//////////////////////////////////////////////////
4041
/// \brief Enumeration of available commands
@@ -264,11 +265,7 @@ void runCommand(const Options &_opt)
264265

265266
auto stage = pxr::UsdStage::CreateInMemory();
266267
std::string modelName = model->Name();
267-
modelName = ignition::common::replaceAll(modelName, " ", "");
268-
if (!modelName.empty() && std::isdigit(modelName[0]))
269-
{
270-
modelName = "_" + modelName;
271-
}
268+
modelName = sdf::usd::validPath(modelName);
272269
auto modelPath = std::string("/" + modelName);
273270
auto usdErrors = sdf::usd::ParseSdfModel(
274271
*model,
@@ -309,7 +306,8 @@ void runCommand(const Options &_opt)
309306

310307
auto stage = pxr::UsdStage::CreateInMemory();
311308

312-
const auto worldPath = std::string("/" + world->Name());
309+
auto worldPath = std::string("/" + world->Name());
310+
worldPath = sdf::usd::validPath(worldPath);
313311
auto usdErrors = sdf::usd::ParseSdfWorld(*world, stage, worldPath);
314312
if (!usdErrors.empty())
315313
{

usd/src/sdf_parser/Geometry.cc

+1-1
Original file line numberDiff line numberDiff line change
@@ -350,7 +350,7 @@ namespace usd
350350
else
351351
primName = _path + "/submesh_" + std::to_string(i);
352352

353-
primName = ignition::common::replaceAll(primName, "-", "_");
353+
primName = sdf::usd::validPath(primName);
354354

355355
auto usdMesh = pxr::UsdGeomMesh::Define(_stage, pxr::SdfPath(primName));
356356
if (!usdMesh)

usd/src/sdf_parser/Link.cc

+7-3
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,8 @@ namespace usd
131131
for (uint64_t i = 0; i < _link.VisualCount(); ++i)
132132
{
133133
const auto visual = *(_link.VisualByIndex(i));
134-
const auto visualPath = std::string(_path + "/" + visual.Name());
134+
auto visualPath = std::string(_path + "/" + visual.Name());
135+
visualPath = sdf::usd::validPath(visualPath);
135136
auto errorsLink = ParseSdfVisual(visual, _stage, visualPath);
136137
if (!errorsLink.empty())
137138
{
@@ -147,7 +148,8 @@ namespace usd
147148
for (uint64_t i = 0; i < _link.CollisionCount(); ++i)
148149
{
149150
const auto collision = *(_link.CollisionByIndex(i));
150-
const auto collisionPath = std::string(_path + "/" + collision.Name());
151+
auto collisionPath = std::string(_path + "/" + collision.Name());
152+
collisionPath = sdf::usd::validPath(collisionPath);
151153
auto errorsCollision = ParseSdfCollision(collision, _stage,
152154
collisionPath);
153155
if (!errorsCollision.empty())
@@ -166,7 +168,8 @@ namespace usd
166168
for (uint64_t i = 0; i < _link.SensorCount(); ++i)
167169
{
168170
const auto sensor = *(_link.SensorByIndex(i));
169-
const auto sensorPath = std::string(_path + "/" + sensor.Name());
171+
auto sensorPath = std::string(_path + "/" + sensor.Name());
172+
sensorPath = sdf::usd::validPath(sensorPath);
170173
UsdErrors errorsSensor = ParseSdfSensor(sensor, _stage, sensorPath);
171174
if (!errorsSensor.empty())
172175
{
@@ -183,6 +186,7 @@ namespace usd
183186
{
184187
const auto light = *(_link.LightByIndex(i));
185188
auto lightPath = std::string(_path + "/" + light.Name());
189+
lightPath = sdf::usd::validPath(lightPath);
186190
UsdErrors lightErrors = ParseSdfLight(light, _stage, lightPath);
187191
if (!lightErrors.empty())
188192
{

usd/src/sdf_parser/Model.cc

+2-1
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,8 @@ namespace usd
118118
for (uint64_t i = 0; i < _model.LinkCount(); ++i)
119119
{
120120
const auto link = *(_model.LinkByIndex(i));
121-
const auto linkPath = std::string(_path + "/" + link.Name());
121+
auto linkPath = std::string(_path + "/" + link.Name());
122+
linkPath = sdf::usd::validPath(linkPath);
122123
sdfLinkToUSDPath[link.Name()] = pxr::SdfPath(linkPath);
123124
UsdErrors linkErrors = ParseSdfLink(
124125
link, _stage, linkPath, !_model.Static());

usd/src/sdf_parser/Visual.cc

+2-1
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,8 @@ namespace usd
8080
}
8181

8282
const auto geometry = *(_visual.Geom());
83-
const auto geometryPath = std::string(_path + "/geometry");
83+
auto geometryPath = std::string(_path + "/geometry");
84+
geometryPath = sdf::usd::validPath(geometryPath);
8485
auto geomErrors = ParseSdfGeometry(geometry, _stage, geometryPath);
8586
if (!geomErrors.empty())
8687
{

usd/src/sdf_parser/World.cc

+3-6
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040
#include "sdf/World.hh"
4141
#include "Light.hh"
4242
#include "Model.hh"
43+
#include "../UsdUtils.hh"
4344

4445
namespace sdf
4546
{
@@ -75,12 +76,8 @@ namespace usd
7576
{
7677
const auto model = *(_world.ModelByIndex(i));
7778
std::string modelName = model.Name();
78-
if (!modelName.empty() && std::isdigit(modelName[0]))
79-
{
80-
modelName = "_" + modelName;
81-
}
79+
modelName = sdf::usd::validPath(modelName);
8280
auto modelPath = std::string(_path + "/" + modelName);
83-
modelPath = ignition::common::replaceAll(modelPath, " ", "");
8481
UsdErrors modelErrors =
8582
ParseSdfModel(model, _stage, modelPath, worldPrimPath);
8683
if (!modelErrors.empty())
@@ -96,7 +93,7 @@ namespace usd
9693
{
9794
const auto light = *(_world.LightByIndex(i));
9895
auto lightPath = std::string(_path + "/" + light.Name());
99-
lightPath = ignition::common::replaceAll(lightPath, " ", "");
96+
lightPath = sdf::usd::validPath(lightPath);
10097
UsdErrors lightErrors = ParseSdfLight(light, _stage, lightPath);
10198
if (!lightErrors.empty())
10299
{

0 commit comments

Comments
 (0)