Skip to content

Commit 3bbd6ea

Browse files
committed
add unit test for world conversion
Signed-off-by: Ashton Larkin <42042756+adlarkin@users.noreply.github.com>
1 parent 18f3ac1 commit 3bbd6ea

File tree

4 files changed

+117
-2
lines changed

4 files changed

+117
-2
lines changed

test/test_utils.hh

+21
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,9 @@
1818
#define SDF_TEST_UTILS_HH_
1919

2020
#include <ostream>
21+
#include <string>
2122
#include "sdf/Console.hh"
23+
#include "sdf/Root.hh"
2224

2325
namespace sdf
2426
{
@@ -104,6 +106,25 @@ class RedirectConsoleStream
104106
private: sdf::Console::ConsoleStream oldStream;
105107
};
106108

109+
/// \brief Load an SDF file into a sdf::Root object
110+
/// \param[in] _fileName The name of the file to load
111+
/// \param[in] _root The sdf::Root object to load the file into
112+
/// \return True if a file named _fileName was successfully loaded into
113+
/// _root. False otherwise
114+
bool LoadSdfFile(const std::string &_fileName, sdf::Root &_root)
115+
{
116+
auto errors = _root.Load(_fileName);
117+
if (!errors.empty())
118+
{
119+
std::cerr << "Errors encountered:\n";
120+
for (const auto &e : errors)
121+
std::cerr << e << "\n";
122+
return false;
123+
}
124+
125+
return true;
126+
}
127+
107128
} // namespace testing
108129
} // namespace sdf
109130

usd/src/CMakeLists.txt

+6-1
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,11 @@ target_link_libraries(${usd_target}
1717
)
1818

1919
# Build the unit tests
20-
ign_build_tests(TYPE UNIT SOURCES ${gtest_sources} LIB_DEPS ${usd_target})
20+
ign_build_tests(
21+
TYPE UNIT
22+
SOURCES ${gtest_sources}
23+
LIB_DEPS ${usd_target}
24+
INCLUDE_DIRS ${PROJECT_SOURCE_DIR}/test
25+
)
2126

2227
add_subdirectory(cmd)

usd/src/World.cc

+3-1
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,9 @@ namespace usd
5353
static_cast<float>(sdfWorldGravity.Length()));
5454

5555
// TODO(ahcorde) Add parser
56-
std::cerr << "Parser for a sdf world is not yet implemented\n";
56+
std::cerr << "Parser for a sdf world only parses physics information at "
57+
<< "the moment. Models and lights that are children of the world "
58+
<< "are currently being ignored.\n";
5759

5860
return true;
5961
}

usd/src/World_Sdf2Usd_TEST.cc

+87
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
/*
2+
* Copyright 2022 Open Source Robotics Foundation
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*
16+
*/
17+
18+
#include <string>
19+
20+
#include <gtest/gtest.h>
21+
#include <pxr/base/gf/vec3f.h>
22+
#include <pxr/base/tf/token.h>
23+
#include <pxr/usd/usd/prim.h>
24+
#include <pxr/usd/usd/primRange.h>
25+
#include <pxr/usd/usd/stage.h>
26+
#include <pxr/usd/usdGeom/tokens.h>
27+
#include <pxr/usd/usdPhysics/scene.h>
28+
29+
#include "sdf/usd/World.hh"
30+
#include "sdf/Root.hh"
31+
#include "test_config.h"
32+
#include "test_utils.hh"
33+
34+
/////////////////////////////////////////////////
35+
// Fixture that creates a USD stage for each test case.
36+
class UsdStageFixture : public::testing::Test
37+
{
38+
public: UsdStageFixture() = default;
39+
40+
protected: void SetUp() override
41+
{
42+
this->stage = pxr::UsdStage::CreateInMemory();
43+
ASSERT_TRUE(this->stage);
44+
}
45+
46+
public: pxr::UsdStageRefPtr stage;
47+
};
48+
49+
/////////////////////////////////////////////////
50+
TEST_F(UsdStageFixture, World)
51+
{
52+
const auto path = sdf::testing::TestFile("sdf", "empty.sdf");
53+
sdf::Root root;
54+
55+
ASSERT_TRUE(sdf::testing::LoadSdfFile(path, root));
56+
ASSERT_EQ(1u, root.WorldCount());
57+
auto world = root.WorldByIndex(0u);
58+
59+
const auto worldPath = std::string("/" + world->Name());
60+
EXPECT_TRUE(usd::ParseSdfWorld(*world, this->stage, worldPath));
61+
62+
// check top-level stage information
63+
EXPECT_DOUBLE_EQ(100.0, this->stage->GetEndTimeCode());
64+
EXPECT_DOUBLE_EQ(0.0, this->stage->GetStartTimeCode());
65+
EXPECT_DOUBLE_EQ(24.0, this->stage->GetTimeCodesPerSecond());
66+
pxr::TfToken upAxisVal;
67+
EXPECT_TRUE(this->stage->GetMetadata(pxr::UsdGeomTokens->upAxis, &upAxisVal));
68+
EXPECT_EQ(pxr::UsdGeomTokens->z, upAxisVal);
69+
double metersPerUnitVal;
70+
EXPECT_TRUE(this->stage->GetMetadata(pxr::TfToken("metersPerUnit"),
71+
&metersPerUnitVal));
72+
EXPECT_DOUBLE_EQ(1.0, metersPerUnitVal);
73+
74+
// Check that world prim exists, and that things like physics information
75+
// were parsed correctly
76+
auto worldPrim = this->stage->GetPrimAtPath(pxr::SdfPath(worldPath));
77+
ASSERT_TRUE(worldPrim);
78+
auto physicsScene = pxr::UsdPhysicsScene::Get(this->stage,
79+
pxr::SdfPath(worldPath + "/physics"));
80+
ASSERT_TRUE(physicsScene);
81+
pxr::GfVec3f gravityDirectionVal;
82+
EXPECT_TRUE(physicsScene.GetGravityDirectionAttr().Get(&gravityDirectionVal));
83+
EXPECT_EQ(gravityDirectionVal, pxr::GfVec3f(0, 0, -1));
84+
float gravityMagnitudeVal;
85+
EXPECT_TRUE(physicsScene.GetGravityMagnitudeAttr().Get(&gravityMagnitudeVal));
86+
EXPECT_FLOAT_EQ(gravityMagnitudeVal, 9.8f);
87+
}

0 commit comments

Comments
 (0)