From f22729452d1360fc04f120142c915068ee2b254d Mon Sep 17 00:00:00 2001 From: Ian Chen Date: Fri, 17 Jan 2025 02:23:55 +0000 Subject: [PATCH] abi compatibility changes, add test Signed-off-by: Ian Chen --- examples/frustum_visual/CMakeLists.txt | 2 +- examples/frustum_visual/Main.cc | 7 +- include/gz/rendering/FrustumVisual.hh | 2 - include/gz/rendering/Scene.hh | 22 +++- include/gz/rendering/base/BaseScene.hh | 31 +++-- ogre/include/gz/rendering/ogre/OgreScene.hh | 5 +- ogre/src/OgreScene.cc | 10 +- .../include/gz/rendering/ogre2/Ogre2Scene.hh | 5 +- ogre2/src/Ogre2FrustumVisual.cc | 4 - ogre2/src/Ogre2Scene.cc | 10 +- src/base/BaseScene.cc | 60 ++++----- test/common_test/CMakeLists.txt | 1 + test/common_test/FrustumVisual_TEST.cc | 123 ++++++++++++++++++ 13 files changed, 215 insertions(+), 67 deletions(-) create mode 100644 test/common_test/FrustumVisual_TEST.cc diff --git a/examples/frustum_visual/CMakeLists.txt b/examples/frustum_visual/CMakeLists.txt index 675e5081a..793487382 100644 --- a/examples/frustum_visual/CMakeLists.txt +++ b/examples/frustum_visual/CMakeLists.txt @@ -1,6 +1,6 @@ cmake_minimum_required(VERSION 3.22.1 FATAL_ERROR) project(gz-rendering-frustum-visual) -find_package(gz-rendering10 REQUIRED) +find_package(gz-rendering9 REQUIRED) find_package(GLUT REQUIRED) include_directories(SYSTEM ${GLUT_INCLUDE_DIRS}) diff --git a/examples/frustum_visual/Main.cc b/examples/frustum_visual/Main.cc index f05e8edef..8f388e69e 100644 --- a/examples/frustum_visual/Main.cc +++ b/examples/frustum_visual/Main.cc @@ -92,7 +92,12 @@ NodePtr createMainNode(ScenePtr _scene) root->AddChild(box); // create frustum visual and attach to main node - FrustumVisualPtr frustumVisual = _scene->CreateFrustumVisual(); + // \todo(iche033) Commented out for ABI compatibility. Uncomment in + // gz-rendering10. + // \todo(iche033) uncomment and use official API in gz-rendering10 + // FrustumVisualPtr frustumVisual = scene->CreateFrustumVisual(); + FrustumVisualPtr frustumVisual = std::dynamic_pointer_cast( + _scene->Extension()->CreateExt("frustum_visual")); frustumVisual->SetNearClipPlane(1); frustumVisual->SetFarClipPlane(5); frustumVisual->SetHFOV(0.7); diff --git a/include/gz/rendering/FrustumVisual.hh b/include/gz/rendering/FrustumVisual.hh index 59552bbe5..158d09bdc 100644 --- a/include/gz/rendering/FrustumVisual.hh +++ b/include/gz/rendering/FrustumVisual.hh @@ -18,7 +18,6 @@ #define GZ_RENDERING_FRUSTUMVISUAL_HH_ #include -#include #include #include #include @@ -26,7 +25,6 @@ #include "gz/rendering/Visual.hh" #include "gz/rendering/Object.hh" #include "gz/rendering/RenderTypes.hh" -#include "gz/rendering/Marker.hh" namespace gz { diff --git a/include/gz/rendering/Scene.hh b/include/gz/rendering/Scene.hh index 71f2467da..5fd62db4a 100644 --- a/include/gz/rendering/Scene.hh +++ b/include/gz/rendering/Scene.hh @@ -1111,31 +1111,39 @@ namespace gz /// \brief Create new frusum visual. A unique ID and name will /// automatically be assigned to the frustum visual. /// \return The created frustum visual - public: virtual FrustumVisualPtr CreateFrustumVisual() = 0; + /// \todo(iche033) Commented out for ABI compatibility. Uncomment in + /// gz-rendering10 + /// public: virtual FrustumVisualPtr CreateFrustumVisual() = 0; /// \brief Create new frustum visual with the given ID. A unique name /// will automatically be assigned to the frustum visual. If the given /// ID is already in use, NULL will be returned. /// \param[in] _id ID of the new frustum visual /// \return The created frustum visual - public: virtual FrustumVisualPtr CreateFrustumVisual( - unsigned int _id) = 0; + /// \todo(iche033) Commented out for ABI compatibility. Uncomment in + /// gz-rendering10 + /// public: virtual FrustumVisualPtr CreateFrustumVisual( + /// unsigned int _id) = 0; /// \brief Create new frustum visual with the given name. A unique ID /// will automatically be assigned to the frustum visual. If the given /// name is already in use, NULL will be returned. /// \param[in] _name Name of the new frustum visual /// \return The created frustum visual - public: virtual FrustumVisualPtr CreateFrustumVisual( - const std::string &_name) = 0; + /// \todo(iche033) Commented out for ABI compatibility. Uncomment in + /// gz-rendering10 + /// public: virtual FrustumVisualPtr CreateFrustumVisual( + /// const std::string &_name) = 0; /// \brief Create new frustum visual with the given name. If either /// the given ID or name is already in use, NULL will be returned. /// \param[in] _id ID of the frustum visual. /// \param[in] _name Name of the new frustum visual. /// \return The created frustum visual - public: virtual FrustumVisualPtr CreateFrustumVisual( - unsigned int _id, const std::string &_name) = 0; + /// \todo(iche033) Commented out for ABI compatibility. Uncomment in + /// gz-rendering10 + /// public: virtual FrustumVisualPtr CreateFrustumVisual( + /// unsigned int _id, const std::string &_name) = 0; /// \brief Create new heightmap geomerty. The rendering::Heightmap will be /// created from the given HeightmapDescriptor. diff --git a/include/gz/rendering/base/BaseScene.hh b/include/gz/rendering/base/BaseScene.hh index 7ec3b8688..cc2951cee 100644 --- a/include/gz/rendering/base/BaseScene.hh +++ b/include/gz/rendering/base/BaseScene.hh @@ -538,19 +538,23 @@ namespace gz const std::string &_name) override; // Documentation inherited. - public: virtual FrustumVisualPtr CreateFrustumVisual() override; + // \todo(iche033) commented out for ABI compatibility + // public: virtual FrustumVisualPtr CreateFrustumVisual() override; // Documentation inherited. - public: virtual FrustumVisualPtr CreateFrustumVisual( - unsigned int _id) override; + // \todo(iche033) commented out for ABI compatibility + // public: virtual FrustumVisualPtr CreateFrustumVisual( + // unsigned int _id) override; // Documentation inherited. - public: virtual FrustumVisualPtr CreateFrustumVisual( - const std::string &_name) override; + // \todo(iche033) commented out for ABI compatibility + // public: virtual FrustumVisualPtr CreateFrustumVisual( + // const std::string &_name) override; // Documentation inherited. - public: virtual FrustumVisualPtr CreateFrustumVisual(unsigned int _id, - const std::string &_name) override; + // \todo(iche033) commented out for ABI compatibility + // public: virtual FrustumVisualPtr CreateFrustumVisual(unsigned int _id, + // const std::string &_name) override; // Documentation inherited. public: virtual HeightmapPtr CreateHeightmap( @@ -847,8 +851,17 @@ namespace gz /// \param[in] _id unique object id. /// \param[in] _name unique object name. /// \return Pointer to a frustum visual - protected: virtual FrustumVisualPtr CreateFrustumVisualImpl( - unsigned int _id, const std::string &_name) = 0; + /// \todo(iche033) Commented out for ABI compatibility. Uncomment in + /// gz-rendering10 + /// protected: virtual FrustumVisualPtr CreateFrustumVisualImpl( + /// unsigned int _id, const std::string &_name) + /// { + /// (void)_id; + /// (void)_name; + /// gzerr << "FrustumVisual not supported by: " + /// << this->Engine()->Name() << std::endl; + /// return FrustumVisualPtr(); + /// } /// \brief Implementation for creating a heightmap geometry /// \param[in] _id Unique object id. diff --git a/ogre/include/gz/rendering/ogre/OgreScene.hh b/ogre/include/gz/rendering/ogre/OgreScene.hh index bebc39dd9..50d2fda3c 100644 --- a/ogre/include/gz/rendering/ogre/OgreScene.hh +++ b/ogre/include/gz/rendering/ogre/OgreScene.hh @@ -181,8 +181,9 @@ namespace gz const std::string &_name) override; // Documentation inherited - protected: virtual FrustumVisualPtr CreateFrustumVisualImpl( - unsigned int _id, const std::string &_name) override; + // \todo(iche033) make this virtual in gz-rendering10 + protected: FrustumVisualPtr CreateFrustumVisualImpl( + unsigned int _id, const std::string &_name); // Documentation inherited protected: virtual WireBoxPtr CreateWireBoxImpl(unsigned int _id, diff --git a/ogre/src/OgreScene.cc b/ogre/src/OgreScene.cc index 981c64fe4..587cc154f 100644 --- a/ogre/src/OgreScene.cc +++ b/ogre/src/OgreScene.cc @@ -781,7 +781,7 @@ OgreSceneExt::OgreSceneExt(Scene *_scene) ObjectPtr OgreSceneExt::CreateExt(const std::string &_type, const std::string &_name) { - if (_type == "projector") + if (_type == "frustum_visual") { OgreScene *ogreScene = dynamic_cast(this->scene); unsigned int objId = ogreScene->CreateObjectId(); @@ -789,14 +789,14 @@ ObjectPtr OgreSceneExt::CreateExt(const std::string &_type, if (objName.empty()) { std::stringstream ss; - ss << ogreScene->Name() << "::" << "Projector"; + ss << ogreScene->Name() << "::" << "FrustumVisual"; ss << "(" << std::to_string(objId) << ")"; objName = ss.str(); } - ProjectorPtr projector = ogreScene->CreateProjectorImpl( + FrustumVisualPtr frustumVisual = ogreScene->CreateFrustumVisualImpl( objId, objName); - bool result = ogreScene->Visuals()->Add(projector); - return (result) ? projector : nullptr; + bool result = ogreScene->Visuals()->Add(frustumVisual); + return (result) ? frustumVisual : nullptr; } return ObjectPtr(); diff --git a/ogre2/include/gz/rendering/ogre2/Ogre2Scene.hh b/ogre2/include/gz/rendering/ogre2/Ogre2Scene.hh index 5be78d9c1..32c84fdba 100644 --- a/ogre2/include/gz/rendering/ogre2/Ogre2Scene.hh +++ b/ogre2/include/gz/rendering/ogre2/Ogre2Scene.hh @@ -344,8 +344,9 @@ namespace gz const std::string &_name) override; // Documentation inherited - protected: virtual FrustumVisualPtr CreateFrustumVisualImpl( - unsigned int _id, const std::string &_name) override; + // \todo(iche033) make this virtual in gz-rendering10 + protected: FrustumVisualPtr CreateFrustumVisualImpl( + unsigned int _id, const std::string &_name); // Documentation inherited protected: virtual WireBoxPtr CreateWireBoxImpl(unsigned int _id, diff --git a/ogre2/src/Ogre2FrustumVisual.cc b/ogre2/src/Ogre2FrustumVisual.cc index 361f8d29f..6b3b6e83f 100644 --- a/ogre2/src/Ogre2FrustumVisual.cc +++ b/ogre2/src/Ogre2FrustumVisual.cc @@ -58,10 +58,6 @@ class gz::rendering::Ogre2FrustumVisualPrivate /// \brief Frustum Ray DynamicLines Object to display public: std::vector> rayLines; - /// \brief Frustum visual type - // public: FrustumVisualPlane frustumVisPlane = - // FrustumVisualPlane::FRUSTUM_PLANE_TOP; - /// \brief The visibility of the visual public: bool visible = true; diff --git a/ogre2/src/Ogre2Scene.cc b/ogre2/src/Ogre2Scene.cc index 22d172593..6c9c07a3c 100644 --- a/ogre2/src/Ogre2Scene.cc +++ b/ogre2/src/Ogre2Scene.cc @@ -1694,7 +1694,7 @@ Ogre2SceneExt::Ogre2SceneExt(Scene *_scene) ObjectPtr Ogre2SceneExt::CreateExt(const std::string &_type, const std::string &_name) { - if (_type == "projector") + if (_type == "frustum_visual") { Ogre2Scene *ogreScene = dynamic_cast(this->scene); unsigned int objId = ogreScene->CreateObjectId(); @@ -1702,14 +1702,14 @@ ObjectPtr Ogre2SceneExt::CreateExt(const std::string &_type, if (objName.empty()) { std::stringstream ss; - ss << ogreScene->Name() << "::" << "Projector"; + ss << ogreScene->Name() << "::" << "FrustumVisual"; ss << "(" << std::to_string(objId) << ")"; objName = ss.str(); } - ProjectorPtr projector = ogreScene->CreateProjectorImpl( + FrustumVisualPtr frustumVisual = ogreScene->CreateFrustumVisualImpl( objId, objName); - bool result = ogreScene->Visuals()->Add(projector); - return (result) ? projector : nullptr; + bool result = ogreScene->Visuals()->Add(frustumVisual); + return (result) ? frustumVisual : nullptr; } return ObjectPtr(); diff --git a/src/base/BaseScene.cc b/src/base/BaseScene.cc index 2a03743c7..7fe39840a 100644 --- a/src/base/BaseScene.cc +++ b/src/base/BaseScene.cc @@ -1263,35 +1263,37 @@ LidarVisualPtr BaseScene::CreateLidarVisual(unsigned int _id, return (result) ? lidar : nullptr; } -////////////////////////////////////////////////// -FrustumVisualPtr BaseScene::CreateFrustumVisual() -{ - unsigned int objId = this->CreateObjectId(); - return this->CreateFrustumVisual(objId); -} - -////////////////////////////////////////////////// -FrustumVisualPtr BaseScene::CreateFrustumVisual(unsigned int _id) -{ - const std::string objName = this->CreateObjectName(_id, "FrustumVisual"); - return this->CreateFrustumVisual(_id, objName); -} - -////////////////////////////////////////////////// -FrustumVisualPtr BaseScene::CreateFrustumVisual(const std::string &_name) -{ - unsigned int objId = this->CreateObjectId(); - return this->CreateFrustumVisual(objId, _name); -} - -////////////////////////////////////////////////// -FrustumVisualPtr BaseScene::CreateFrustumVisual(unsigned int _id, - const std::string &_name) -{ - FrustumVisualPtr frustum = this->CreateFrustumVisualImpl(_id, _name); - bool result = this->RegisterVisual(frustum); - return (result) ? frustum : nullptr; -} +// \todo(iche033) Commented out for ABI compatibility. Uncomment in +// gz-rendering10 +// ////////////////////////////////////////////////// +// FrustumVisualPtr BaseScene::CreateFrustumVisual() +// { +// unsigned int objId = this->CreateObjectId(); +// return this->CreateFrustumVisual(objId); +// } +// +// ////////////////////////////////////////////////// +// FrustumVisualPtr BaseScene::CreateFrustumVisual(unsigned int _id) +// { +// const std::string objName = this->CreateObjectName(_id, "FrustumVisual"); +// return this->CreateFrustumVisual(_id, objName); +// } +// +// ////////////////////////////////////////////////// +// FrustumVisualPtr BaseScene::CreateFrustumVisual(const std::string &_name) +// { +// unsigned int objId = this->CreateObjectId(); +// return this->CreateFrustumVisual(objId, _name); +// } +// +// ////////////////////////////////////////////////// +// FrustumVisualPtr BaseScene::CreateFrustumVisual(unsigned int _id, +// const std::string &_name) +// { +// FrustumVisualPtr frustum = this->CreateFrustumVisualImpl(_id, _name); +// bool result = this->RegisterVisual(frustum); +// return (result) ? frustum : nullptr; +// } ////////////////////////////////////////////////// WireBoxPtr BaseScene::CreateWireBox() diff --git a/test/common_test/CMakeLists.txt b/test/common_test/CMakeLists.txt index 385e70fc5..bad08e487 100644 --- a/test/common_test/CMakeLists.txt +++ b/test/common_test/CMakeLists.txt @@ -8,6 +8,7 @@ set(tests Camera_TEST Capsule_TEST COMVisual_TEST + FrustumVisual_TEST GaussianNoisePass_TEST GizmoVisual_TEST GlobalIllumination_TEST diff --git a/test/common_test/FrustumVisual_TEST.cc b/test/common_test/FrustumVisual_TEST.cc new file mode 100644 index 000000000..222290130 --- /dev/null +++ b/test/common_test/FrustumVisual_TEST.cc @@ -0,0 +1,123 @@ +/* + * Copyright (C) 2025 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 +#include + +#include "CommonRenderingTest.hh" + +#include "gz/rendering/FrustumVisual.hh" +#include "gz/rendering/Scene.hh" + +using namespace gz; +using namespace rendering; + +/// \brief The test fixture. +class FrustumVisualTest : public CommonRenderingTest +{ +}; + +///////////////////////////////////////////////// +TEST_F(FrustumVisualTest, FrustumVisual) +{ + CHECK_SUPPORTED_ENGINE("ogre2"); + + ScenePtr scene = engine->CreateScene("scene"); + EXPECT_NE(nullptr, scene); + + // FrustumVisual and can only be accessed by the scene extension API + // in gz-rendering9 + // \todo(iche033) Remove this in gz-rendering10 + if (!scene->Extension()) + return; + + // Create a frustum visual + // \todo(iche033) uncomment and use official API in gz-rendering10 + // FrustumVisualPtr frustumVisual = scene->CreateFrustumVisual(); + FrustumVisualPtr frustumVisual = std::dynamic_pointer_cast( + scene->Extension()->CreateExt("frustum_visual")); + + // Check default properties + EXPECT_DOUBLE_EQ(0, frustumVisual->NearClipPlane()); + EXPECT_DOUBLE_EQ(1.0, frustumVisual->FarClipPlane()); + EXPECT_EQ(math::Angle(GZ_DTOR(45)), frustumVisual->HFOV().Radian()); + EXPECT_DOUBLE_EQ(1.0, frustumVisual->AspectRatio()); + auto emptyPlane = math::Planed(); + EXPECT_DOUBLE_EQ(emptyPlane.Offset(), + frustumVisual->Plane(FRUSTUM_PLANE_NEAR).Offset()); + EXPECT_EQ(emptyPlane.Normal(), + frustumVisual->Plane(FRUSTUM_PLANE_NEAR).Normal()); + EXPECT_DOUBLE_EQ(emptyPlane.Offset(), + frustumVisual->Plane(FRUSTUM_PLANE_FAR).Offset()); + EXPECT_EQ(emptyPlane.Normal(), + frustumVisual->Plane(FRUSTUM_PLANE_FAR).Normal()); + EXPECT_DOUBLE_EQ(emptyPlane.Offset(), + frustumVisual->Plane(FRUSTUM_PLANE_LEFT).Offset()); + EXPECT_EQ(emptyPlane.Normal(), + frustumVisual->Plane(FRUSTUM_PLANE_LEFT).Normal()); + EXPECT_DOUBLE_EQ(emptyPlane.Offset(), + frustumVisual->Plane(FRUSTUM_PLANE_RIGHT).Offset()); + EXPECT_EQ(emptyPlane.Normal(), + frustumVisual->Plane(FRUSTUM_PLANE_RIGHT).Normal()); + EXPECT_DOUBLE_EQ(emptyPlane.Offset(), + frustumVisual->Plane(FRUSTUM_PLANE_TOP).Offset()); + EXPECT_EQ(emptyPlane.Normal(), + frustumVisual->Plane(FRUSTUM_PLANE_TOP).Normal()); + EXPECT_DOUBLE_EQ(emptyPlane.Offset(), + frustumVisual->Plane(FRUSTUM_PLANE_BOTTOM).Offset()); + EXPECT_EQ(emptyPlane.Normal(), + frustumVisual->Plane(FRUSTUM_PLANE_BOTTOM).Normal()); + + // Test APIs + double nearClip = 1.1; + double farClip = 15.5; + math::Angle hfov(1.06); + double aspect = 1.3333; + + frustumVisual->SetNearClipPlane(nearClip); + EXPECT_DOUBLE_EQ(nearClip, frustumVisual->NearClipPlane()); + + frustumVisual->SetFarClipPlane(farClip); + EXPECT_DOUBLE_EQ(farClip, frustumVisual->FarClipPlane()); + + frustumVisual->SetHFOV(hfov); + EXPECT_EQ(hfov, frustumVisual->HFOV()); + + frustumVisual->SetAspectRatio(aspect); + EXPECT_EQ(aspect, frustumVisual->AspectRatio()); + + frustumVisual->Update(); + + EXPECT_DOUBLE_EQ(nearClip, + std::fabs(frustumVisual->Plane(FRUSTUM_PLANE_NEAR).Offset())); + EXPECT_EQ(math::Vector3d::UnitX, + frustumVisual->Plane(FRUSTUM_PLANE_NEAR).Normal()); + EXPECT_DOUBLE_EQ(farClip, + std::fabs(frustumVisual->Plane(FRUSTUM_PLANE_FAR).Offset())); + EXPECT_EQ(-math::Vector3d::UnitX, + frustumVisual->Plane(FRUSTUM_PLANE_FAR).Normal()); + EXPECT_NEAR(0.0, frustumVisual->Plane(FRUSTUM_PLANE_LEFT).Offset(), 1e-6); + EXPECT_GT(0.0, frustumVisual->Plane(FRUSTUM_PLANE_LEFT).Normal().Y()); + EXPECT_NEAR(0.0, frustumVisual->Plane(FRUSTUM_PLANE_RIGHT).Offset(), 1e-6); + EXPECT_LT(0.0, frustumVisual->Plane(FRUSTUM_PLANE_RIGHT).Normal().Y()); + EXPECT_NEAR(0.0, frustumVisual->Plane(FRUSTUM_PLANE_TOP).Offset(), 1e-6); + EXPECT_GT(0.0, frustumVisual->Plane(FRUSTUM_PLANE_TOP).Normal().Z()); + EXPECT_NEAR(0.0, frustumVisual->Plane(FRUSTUM_PLANE_BOTTOM).Offset(), 1e-6); + EXPECT_LT(0.0, frustumVisual->Plane(FRUSTUM_PLANE_BOTTOM).Normal().Z()); + + engine->DestroyScene(scene); +}