Commit 576028d7 authored by Alexis Girault's avatar Alexis Girault
Browse files

ENH: Implement SceneManager independent from Scene

parent dedf109b
......@@ -55,7 +55,10 @@ public:
protected:
Module(std::string name) : m_name(name) {}
Module(std::string name, int loopDelay = 0) :
m_name(name),
m_loopDelay(loopDelay)
{}
virtual void initModule() = 0;
virtual void runModule() = 0;
......@@ -63,7 +66,7 @@ protected:
std::atomic<ModuleStatus> m_status{ModuleStatus::INACTIVE};
std::string m_name;
int m_loopDelay = 0;
int m_loopDelay;
};
}
......
......@@ -154,45 +154,15 @@ Scene::removeLight(std::string lightName)
LOG(INFO) << lightName << " light removed from " << m_name;
}
std::shared_ptr<Camera>
Scene::getCamera()
{
return m_camera;
}
void
Scene::initModule()
{
LOG(DEBUG) << m_name << " : init";
if (auto camController = m_camera->getController())
{
this->startModuleInNewThread(camController);
}
}
void
Scene::cleanUpModule()
{
LOG(DEBUG) << m_name << " : cleanUp";
if (auto camController = m_camera->getController())
{
camController->end();
m_threadMap.at(camController->getName()).join();
}
}
void
Scene::runModule()
const std::string&
Scene::getName() const
{
LOG(DEBUG) << m_name << " : running";
return m_name;
}
void
Scene::startModuleInNewThread(std::shared_ptr<Module>module)
std::shared_ptr<Camera>
Scene::getCamera() const
{
m_threadMap[module->getName()] = std::thread([module] { module->start(); });
return m_camera;
}
}
......@@ -22,24 +22,22 @@
#ifndef imstkScene_h
#define imstkScene_h
#include <vector>
#include <unordered_map>
#include <memory>
#include "imstkModule.h"
#include "imstkSceneObject.h"
#include "imstkLight.h"
#include "imstkCamera.h"
namespace imstk {
class Scene : public Module
class Scene
{
template<class T>
using NamedMap = std::unordered_map<std::string, std::shared_ptr<T>>;
public:
Scene(std::string name) : Module(name) {}
Scene(std::string name) : m_name(name) {}
~Scene() = default;
......@@ -55,17 +53,13 @@ public:
void addLight(std::shared_ptr<Light> newLight);
void removeLight(std::string lightName);
std::shared_ptr<Camera> getCamera();
const std::string& getName() const;
protected:
void initModule() override;
void runModule() override;
void cleanUpModule() override;
std::shared_ptr<Camera> getCamera() const;
void startModuleInNewThread(std::shared_ptr<Module> module);
std::unordered_map<std::string, std::thread> m_threadMap;
protected:
std::string m_name;
NamedMap<SceneObject> m_sceneObjectsMap;
NamedMap<Light> m_lightsMap;
std::shared_ptr<Camera> m_camera = std::make_shared<Camera>();
......
/*=========================================================================
Library: iMSTK
Copyright (c) Kitware, Inc. & Center for Modeling, Simulation,
& Imaging in Medicine, Rensselaer Polytechnic Institute.
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.txt
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 "imstkSceneManager.h"
#include "imstkCameraController.h"
#include "g3log/g3log.hpp"
namespace imstk {
std::shared_ptr<Scene>
SceneManager::getScene()
{
return m_scene;
}
void
SceneManager::initModule()
{
LOG(DEBUG) << m_name << " manager : init";
if (auto camController = m_scene->getCamera()->getController())
{
this->startModuleInNewThread(camController);
}
}
void
SceneManager::cleanUpModule()
{
LOG(DEBUG) << m_name << " manager : cleanUp";
if (auto camController = m_scene->getCamera()->getController())
{
camController->end();
m_threadMap.at(camController->getName()).join();
}
}
void
SceneManager::runModule()
{
LOG(DEBUG) << m_name << " manager : running";
}
void
SceneManager::startModuleInNewThread(std::shared_ptr<Module> module)
{
m_threadMap[module->getName()] = std::thread([module] { module->start(); });
}
}
/*=========================================================================
Library: iMSTK
Copyright (c) Kitware, Inc. & Center for Modeling, Simulation,
& Imaging in Medicine, Rensselaer Polytechnic Institute.
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.txt
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.
=========================================================================*/
#ifndef imstkSceneManager_h
#define imstkSceneManager_h
#include <unordered_map>
#include <memory>
#include <thread>
#include "imstkModule.h"
#include "imstkScene.h"
namespace imstk {
class SceneManager : public Module
{
public:
SceneManager(std::shared_ptr<Scene> scene) :
Module(scene->getName(), 1000),
m_scene(scene)
{}
~SceneManager() = default;
std::shared_ptr<Scene> getScene();
protected:
void initModule() override;
void runModule() override;
void cleanUpModule() override;
std::shared_ptr<Scene> m_scene;
void startModuleInNewThread(std::shared_ptr<Module> module);
std::unordered_map<std::string, std::thread> m_threadMap;
};
}
#endif // ifndef imstkSceneManager_h
......@@ -35,7 +35,7 @@ SimulationStatus& SimulationManager::getStatus() const
bool
SimulationManager::isSceneRegistered(std::string sceneName) const
{
return m_sceneMap.find(sceneName) != m_sceneMap.end();
return m_sceneManagerMap.find(sceneName) != m_sceneManagerMap.end();
}
std::shared_ptr<Scene>
......@@ -48,7 +48,7 @@ SimulationManager::getScene(std::string sceneName) const
return nullptr;
}
return m_sceneMap.at(sceneName);
return m_sceneManagerMap.at(sceneName)->getScene();
}
std::shared_ptr<Scene>
......@@ -68,22 +68,23 @@ SimulationManager::createNewScene(std::string newSceneName)
return nullptr;
}
m_sceneMap[newSceneName] = std::make_shared<Scene>(newSceneName);
auto newScene = std::make_shared<Scene>(newSceneName);
m_sceneManagerMap[newSceneName] = std::make_shared<SceneManager>(newScene);
LOG(INFO) << "New scene added: " << newSceneName;
return m_sceneMap.at(newSceneName);
return newScene;
}
std::shared_ptr<Scene>
SimulationManager::createNewScene()
{
int id = m_sceneMap.size() + 1;
int id = m_sceneManagerMap.size() + 1;
std::string newSceneName = "Scene_" + std::to_string(id);
return this->createNewScene(newSceneName);
}
void
SimulationManager::addScene(std::shared_ptr<Scene>newScene)
SimulationManager::addScene(std::shared_ptr<Scene> newScene)
{
std::string newSceneName = newScene->getName();
......@@ -95,7 +96,7 @@ SimulationManager::addScene(std::shared_ptr<Scene>newScene)
return;
}
m_sceneMap[newSceneName] = newScene;
m_sceneManagerMap[newSceneName] = std::make_shared<SceneManager>(newScene);
LOG(INFO) << "Scene added: " << newSceneName;
}
......@@ -109,7 +110,7 @@ SimulationManager::removeScene(std::string sceneName)
return;
}
m_sceneMap.erase(sceneName);
m_sceneManagerMap.erase(sceneName);
LOG(INFO) << "Scene removed: " << sceneName;
}
......@@ -260,25 +261,27 @@ SimulationManager::setCurrentScene(std::string newSceneName, bool unloadCurrentS
m_viewer->setRenderingMode(Renderer::Mode::SIMULATION);
// Stop/Pause running scene
auto oldSceneManager = m_sceneManagerMap.at(m_currentSceneName);
if (unloadCurrentScene)
{
LOG(INFO) << "Unloading '" << m_currentSceneName << "'";
m_sceneMap.at(m_currentSceneName)->end();
oldSceneManager->end();
m_threadMap.at(m_currentSceneName).join();
}
else
{
m_sceneMap.at(m_currentSceneName)->pause();
oldSceneManager->pause();
}
// Start/Run new scene
if (newScene->getStatus() == ModuleStatus::INACTIVE)
auto newSceneManager = m_sceneManagerMap.at(newSceneName);
if (newSceneManager->getStatus() == ModuleStatus::INACTIVE)
{
this->startModuleInNewThread(newScene);
this->startModuleInNewThread(newSceneManager);
}
else if (newScene->getStatus() == ModuleStatus::PAUSED)
else if (newSceneManager->getStatus() == ModuleStatus::PAUSED)
{
newScene->run();
newSceneManager->run();
}
m_currentSceneName = newSceneName;
}
......@@ -292,14 +295,15 @@ SimulationManager::startSimulation(bool debug)
return;
}
std::shared_ptr<Scene> startingScene = this->getScene(m_currentSceneName);
std::shared_ptr<Scene> startingScene = this->getCurrentScene();
if (!startingScene)
{
LOG(WARNING) << "Simulation canceled";
return;
}
if (startingScene->getStatus() != ModuleStatus::INACTIVE)
auto startingSceneManager = m_sceneManagerMap.at(m_currentSceneName);
if (startingSceneManager->getStatus() != ModuleStatus::INACTIVE)
{
LOG(WARNING) << "Scene '" << m_currentSceneName << "' is already active";
return;
......@@ -324,7 +328,7 @@ SimulationManager::startSimulation(bool debug)
}
// Start scene
this->startModuleInNewThread(startingScene);
this->startModuleInNewThread(startingSceneManager);
m_status = SimulationStatus::RUNNING;
}
......@@ -361,7 +365,7 @@ SimulationManager::runSimulation()
}
// Run scene
m_sceneMap.at(m_currentSceneName)->run();
m_sceneManagerMap.at(m_currentSceneName)->run();
// Run device servers
for(const auto& pair : m_deviceServerMap)
......@@ -391,7 +395,7 @@ SimulationManager::pauseSimulation()
}
// Pause scene
m_sceneMap.at(m_currentSceneName)->pause();
m_sceneManagerMap.at(m_currentSceneName)->pause();
// Pause device clients
for(const auto& pair : m_deviceClientMap)
......@@ -439,14 +443,14 @@ SimulationManager::endSimulation()
}
// End all scenes
for (auto pair : m_sceneMap)
for (auto pair : m_sceneManagerMap)
{
std::string sceneName = pair.first;
ModuleStatus sceneStatus = m_sceneMap.at(sceneName)->getStatus();
ModuleStatus sceneStatus = pair.second->getStatus();
if (sceneStatus != ModuleStatus::INACTIVE)
{
m_sceneMap.at(sceneName)->end();
m_sceneManagerMap.at(sceneName)->end();
m_threadMap.at(sceneName).join();
}
}
......
......@@ -30,7 +30,7 @@
#include "imstkScene.h"
#include "imstkVRPNDeviceServer.h"
#include "imstkDeviceClient.h"
#include "imstkScene.h"
#include "imstkSceneManager.h"
#include "imstkViewer.h"
#include "imstkLogUtility.h"
......@@ -89,7 +89,7 @@ private:
SimulationStatus m_status = SimulationStatus::INACTIVE;
std::string m_currentSceneName = "";
std::unordered_map<std::string, std::shared_ptr<Scene>> m_sceneMap;
std::unordered_map<std::string, std::shared_ptr<SceneManager>> m_sceneManagerMap;
std::unordered_map<std::string, std::shared_ptr<VRPNDeviceServer>> m_deviceServerMap;
std::unordered_map<std::string, std::shared_ptr<DeviceClient>> m_deviceClientMap;
......
......@@ -66,7 +66,6 @@ void testDevices()
// SDK and Scene
auto sdk = std::make_shared<imstk::SimulationManager>();
auto scene = sdk->createNewScene("SceneTestDevice");
scene->setLoopDelay(1000);
// Device server
auto server = std::make_shared<imstk::VRPNDeviceServer>("127.0.0.1");
......@@ -111,7 +110,6 @@ void testReadMesh()
// SDK and Scene
auto sdk = std::make_shared<imstk::SimulationManager>();
auto scene = sdk->createNewScene("SceneTestMesh");
scene->setLoopDelay(1000);
// Read surface mesh
auto objMesh = imstk::MeshReader::read("/home/virtualfls/Projects/IMSTK/resources/asianDragon/asianDragon.obj");
......@@ -144,7 +142,6 @@ void testViewer()
// SDK and Scene
auto sdk = std::make_shared<imstk::SimulationManager>();
auto sceneTest = sdk->createNewScene("SceneTest");
sceneTest->setLoopDelay(1000);
// Plane
auto planeGeom = std::make_shared<imstk::Plane>();
......@@ -243,12 +240,10 @@ void testScenesManagement()
// Scenes
LOG(INFO) << "-- Test add scenes";
auto scene1 = std::make_shared<imstk::Scene>("scene1");
scene1->setLoopDelay(500);
sdk->addScene(scene1);
sdk->createNewScene("scene2");
auto scene2 = sdk->getScene("scene2");
scene2->setLoopDelay(500);
auto scene3 = sdk->createNewScene();
sdk->removeScene("Scene_3");
......@@ -287,7 +282,6 @@ void testIsometricMap()
// SDK and Scene
auto sdk = std::make_shared<imstk::SimulationManager>();
auto geometryMapTest = sdk->createNewScene("geometryMapTest");
geometryMapTest->setLoopDelay(1000);
// Cube
auto cubeGeom = std::make_shared<imstk::Cube>();
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment