From 62fdba0409267de1de40f5c03e45044fcecb948b Mon Sep 17 00:00:00 2001 From: ahcorde Date: Mon, 17 May 2021 10:14:20 +0200 Subject: [PATCH] Removed duplicated code with rendering::sceneFromFirstRenderEngine Signed-off-by: ahcorde --- src/gui/plugins/align_tool/AlignTool.cc | 58 ++++--------------- src/gui/plugins/grid_config/GridConfig.cc | 37 +----------- .../transform_control/TransformControl.cc | 37 +----------- .../CameraVideoRecorder.cc | 33 +---------- 4 files changed, 13 insertions(+), 152 deletions(-) diff --git a/src/gui/plugins/align_tool/AlignTool.cc b/src/gui/plugins/align_tool/AlignTool.cc index c5b6094882..ca3520fde5 100644 --- a/src/gui/plugins/align_tool/AlignTool.cc +++ b/src/gui/plugins/align_tool/AlignTool.cc @@ -82,6 +82,9 @@ namespace ignition::gazebo /// \brief The map of the original transparency values for the nodes. public: std::map originalTransparency; + + /// \brief Pointer to the scene. + public: rendering::ScenePtr scene{nullptr}; }; } @@ -316,52 +319,9 @@ void AlignTool::Align() if (this->dataPtr->currentState == AlignState::NONE) return; - auto loadedEngNames = rendering::loadedEngines(); - if (loadedEngNames.empty()) - { - ignerr << "Internal error: engine should be loaded at this point." - << std::endl; - return; - } - - // Assume there is only one engine loaded - auto engineName = loadedEngNames[0]; - if (loadedEngNames.size() > 1) - { - ignwarn << "Found more than one loaded engine " - "- using " << engineName << "." << std::endl; - } - auto engine = rendering::engine(engineName); - - if (!engine) - { - ignerr << "Internal error: failed to load engine [" << engineName - << "]. Align tool plugin won't work." << std::endl; - return; - } - - if (engine->SceneCount() == 0) - { - ignerr<< "Internal error: no scenes are available with the loaded engine." - << std::endl; - return; - } - // assume there is only one scene // load scene - auto scene = engine->SceneByIndex(0); - - if (!scene) - { - ignerr << "Internal error: scene is null." << std::endl; - return; - } - - if (!scene->IsInitialized() || scene->VisualCount() == 0) - { - ignerr << "Internal error: scene is either not initialized " - "or there are no visuals within it." << std::endl; - return; - } + if (!this->dataPtr->scene) + this->dataPtr->scene = rendering::sceneFromFirstRenderEngine(); // Get current list of selected entities std::vector selectedList; @@ -369,9 +329,10 @@ void AlignTool::Align() for (const auto &entityId : this->dataPtr->selectedEntities) { - for (auto i = 0u; i < scene->VisualCount(); ++i) + for (auto i = 0u; i < this->dataPtr->scene->VisualCount(); ++i) { - ignition::rendering::VisualPtr vis = scene->VisualByIndex(i); + ignition::rendering::VisualPtr vis = + this->dataPtr->scene->VisualByIndex(i); if (!vis) continue; if (std::get(vis->UserData("gazebo-entity")) == @@ -417,7 +378,8 @@ void AlignTool::Align() rendering::VisualPtr vis = selectedList[i]; // Check here to see if visual is top level or not, continue if not - rendering::VisualPtr topLevelVis = this->TopLevelVisual(scene, vis); + rendering::VisualPtr topLevelVis = this->TopLevelVisual( + this->dataPtr->scene, vis); if (topLevelVis != vis) continue; diff --git a/src/gui/plugins/grid_config/GridConfig.cc b/src/gui/plugins/grid_config/GridConfig.cc index df449039c0..aa7fac9280 100644 --- a/src/gui/plugins/grid_config/GridConfig.cc +++ b/src/gui/plugins/grid_config/GridConfig.cc @@ -149,42 +149,7 @@ void GridConfig::UpdateGrid() ///////////////////////////////////////////////// void GridConfig::LoadGrid() { - auto loadedEngNames = rendering::loadedEngines(); - if (loadedEngNames.empty()) - return; - - // assume there is only one engine loaded - auto engineName = loadedEngNames[0]; - if (loadedEngNames.size() > 1) - { - igndbg << "More than one engine is available. " - << "Grid config plugin will use engine [" - << engineName << "]" << std::endl; - } - auto engine = rendering::engine(engineName); - if (!engine) - { - ignerr << "Internal error: failed to load engine [" << engineName - << "]. Grid plugin won't work." << std::endl; - return; - } - - if (engine->SceneCount() == 0) - return; - - // assume there is only one scene - // load scene - auto scene = engine->SceneByIndex(0); - if (!scene) - { - ignerr << "Internal error: scene is null." << std::endl; - return; - } - - if (!scene->IsInitialized() || nullptr == scene->RootVisual()) - { - return; - } + auto scene = rendering::sceneFromFirstRenderEngine(); // load grid // if gridPtr found, load the existing gridPtr to class diff --git a/src/gui/plugins/transform_control/TransformControl.cc b/src/gui/plugins/transform_control/TransformControl.cc index d4b1d8c94e..cf2a39738e 100644 --- a/src/gui/plugins/transform_control/TransformControl.cc +++ b/src/gui/plugins/transform_control/TransformControl.cc @@ -164,42 +164,7 @@ void TransformControl::SnapToGrid() ///////////////////////////////////////////////// void TransformControl::LoadGrid() { - auto loadedEngNames = rendering::loadedEngines(); - if (loadedEngNames.empty()) - return; - - // assume there is only one engine loaded - auto engineName = loadedEngNames[0]; - if (loadedEngNames.size() > 1) - { - igndbg << "More than one engine is available. " - << "Grid config plugin will use engine [" - << engineName << "]" << std::endl; - } - auto engine = rendering::engine(engineName); - if (!engine) - { - ignerr << "Internal error: failed to load engine [" << engineName - << "]. Grid plugin won't work." << std::endl; - return; - } - - if (engine->SceneCount() == 0) - return; - - // assume there is only one scene - // load scene - auto scene = engine->SceneByIndex(0); - if (!scene) - { - ignerr << "Internal error: scene is null." << std::endl; - return; - } - - if (!scene->IsInitialized() || scene->VisualCount() == 0) - { - return; - } + auto scene = rendering::sceneFromFirstRenderEngine(); // load grid // if gridPtr found, load the existing gridPtr to class diff --git a/src/systems/camera_video_recorder/CameraVideoRecorder.cc b/src/systems/camera_video_recorder/CameraVideoRecorder.cc index 52c24ad0f9..22fadcacee 100644 --- a/src/systems/camera_video_recorder/CameraVideoRecorder.cc +++ b/src/systems/camera_video_recorder/CameraVideoRecorder.cc @@ -218,38 +218,7 @@ void CameraVideoRecorderPrivate::OnPostRender() // get scene if (!this->scene) { - auto loadedEngNames = rendering::loadedEngines(); - if (loadedEngNames.empty()) - return; - - // assume there is only one engine loaded - auto engineName = loadedEngNames[0]; - if (loadedEngNames.size() > 1) - { - igndbg << "More than one engine is available. " - << "Camera video recorder system will use engine [" - << engineName << "]" << std::endl; - } - auto engine = rendering::engine(engineName); - if (!engine) - { - ignerr << "Internal error: failed to load engine [" << engineName - << "]. Camera video recorder system won't work." << std::endl; - return; - } - - if (engine->SceneCount() == 0) - return; - - // assume there is only one scene - // load scene - auto s = engine->SceneByIndex(0); - if (!s) - { - ignerr << "Internal error: scene is null." << std::endl; - return; - } - this->scene = s; + this->scene = rendering::sceneFromFirstRenderEngine(); } // return if scene not ready or no sensors available.