|
| 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