Commit fe0d3920 authored by Sreekanth Arikatla's avatar Sreekanth Arikatla
Browse files

Merge branch 'refactorSimManager' into 'master'

Add back-end mode for simulation manager

See merge request !312
parents a4fdc6e1 90147313
Pipeline #139796 failed with stage
in 0 seconds
......@@ -38,7 +38,7 @@ using namespace imstk;
///
int main()
{
auto sdk = std::make_shared<SimulationManager>(false);
auto sdk = std::make_shared<SimulationManager>(SimulationManager::Mode::runInBackground);
auto scene = sdk->createNewScene("NoRendering");
// Create surface mesh
......
......@@ -26,15 +26,13 @@
using namespace imstk;
const bool runSimWithoutRendering = false;
///
/// \brief This example demonstrates the cloth simulation
/// using Position based dynamics
///
int main()
{
auto sdk = std::make_shared<SimulationManager>(runSimWithoutRendering);
auto sdk = std::make_shared<SimulationManager>();
auto scene = sdk->createNewScene("PBDCloth");
// Create surface mesh
......@@ -129,8 +127,8 @@ int main()
scene->addSceneObject(deformableObj);
// print UPS
auto ups = std::make_shared<UPSCounter>();
apiutils::printUPS(sdk->getSceneManager(scene), ups);
/*auto ups = std::make_shared<UPSCounter>();
apiutils::printUPS(sdk->getSceneManager(scene), ups);*/
scene->getCamera()->setFocalPoint(0, -5, 5);
scene->getCamera()->setPosition(-15., -5.0, 15.0);
......
......@@ -36,7 +36,7 @@ using namespace imstk;
///
int main()
{
auto sdk = std::make_shared<SimulationManager>(0);
auto sdk = std::make_shared<SimulationManager>();
auto scene = sdk->createNewScene("PBDFluid");
scene->getCamera()->setPosition(0, 10.0, 15.0);
......
......@@ -119,8 +119,8 @@ int main()
scene->addLight(light);
// print UPS
auto ups = std::make_shared<UPSCounter>();
apiutils::printUPS(sdk->getSceneManager(scene), ups);
/*auto ups = std::make_shared<UPSCounter>();
apiutils::printUPS(sdk->getSceneManager(scene), ups);*/
sdk->setActiveScene(scene);
sdk->getViewer()->setBackgroundColors(Vec3d(0.3285, 0.3285, 0.6525), Vec3d(0.13836, 0.13836, 0.2748), true);
......
......@@ -39,7 +39,7 @@ std::vector<std::shared_ptr<CollidingObject>> generateSolids(const std::shared_p
int main(int argc, char* argv[])
{
// SimulationManager must be created first
auto sdk = std::make_shared<SimulationManager>(0);
auto sdk = std::make_shared<SimulationManager>();
int threads = -1;
double particleRadius = 0.1;
......
......@@ -98,18 +98,6 @@ Module::start()
}
}
// Cleanup
if (m_preCleanUpCallback)
{
m_preCleanUpCallback(this);
}
this->cleanUpModule();
if (m_postCleanUpCallback)
{
m_postCleanUpCallback(this);
}
m_status = ModuleStatus::INACTIVE;
}
......@@ -144,6 +132,19 @@ Module::pause()
void
Module::end()
{
// Cleanup
if (m_preCleanUpCallback)
{
m_preCleanUpCallback(this);
}
this->cleanUpModule();
if (m_postCleanUpCallback)
{
m_postCleanUpCallback(this);
}
if ((m_status == ModuleStatus::INACTIVE) ||
(m_status == ModuleStatus::TERMINATING))
{
......
......@@ -23,11 +23,25 @@
#include "imstkCameraController.h"
#include "imstkSceneObjectControllerBase.h"
#include "imstkDebugGeometry.h"
#include "imstkPbdObject.h"
#include "imstkDeformableObject.h"
#include "imstkTimer.h"
#include "imstkPbdSolver.h"
#include <g3log/g3log.hpp>
namespace imstk
{
Scene::~Scene()
{
// End Camera Controller
if (auto camController = this->getCamera()->getController())
{
camController->end();
m_threadMap.at(camController->getName()).join();
}
}
bool
Scene::initialize()
{
......@@ -44,6 +58,16 @@ Scene::initialize()
return true;
}
void
Scene::launchModules()
{
// Start Camera Controller (asynchronous)
if (auto camController = this->getCamera()->getController())
{
m_threadMap[camController->getName()] = std::thread([camController] { camController->start(); });
}
}
bool
Scene::isObjectRegistered(const std::string& sceneObjectName) const
{
......@@ -273,4 +297,95 @@ Scene::reset()
}
}
}
void
Scene::advance()
{
StopWatch wwt;
wwt.start();
// Reset Contact forces to 0
for (auto obj : this->getSceneObjects())
{
if (auto defObj = std::dynamic_pointer_cast<DeformableObject>(obj))
{
defObj->getContactForce().setConstant(0.0);
}
else if (auto collidingObj = std::dynamic_pointer_cast<CollidingObject>(obj))
{
collidingObj->resetForce();
}
}
// Update objects controlled by the device controllers
for (auto controller : this->getSceneObjectControllers())
{
controller->updateControlledObjects();
}
// Compute collision data per interaction pair
for (auto intPair : this->getCollisionGraph()->getInteractionPairList())
{
intPair->computeCollisionData();
}
// Process collision data per interaction pair
for (auto intPair : this->getCollisionGraph()->getInteractionPairList())
{
intPair->processCollisionData();
}
// Run the solvers
for (auto solvers : this->getSolvers())
{
solvers->solve();
}
// Apply updated forces on device
for (auto controller : this->getSceneObjectControllers())
{
controller->applyForces();
}
// Apply the geometry and apply maps to all the objects
for (auto obj : this->getSceneObjects())
{
obj->updateGeometries();
}
// Set the trackers of the scene object controllers to out-of-date
for (auto controller : this->getSceneObjectControllers())
{
controller->setTrackerToOutOfDate();
}
auto timeElapsed = wwt.getTimeElapsed(StopWatch::TimeUnitType::seconds);
// Update time step size of the dynamic objects
for (auto obj : this->getSceneObjects())
{
if (obj->getType() == SceneObject::Type::Pbd)
{
if (auto dynaObj = std::dynamic_pointer_cast<PbdObject>(obj))
{
if (dynaObj->getDynamicalModel()->getTimeStepSizeType() == TimeSteppingType::realTime)
{
dynaObj->getDynamicalModel()->setTimeStep(timeElapsed);
}
}
}
else if (obj->getType() == SceneObject::Type::FEMDeformable)
{
if (auto dynaObj = std::dynamic_pointer_cast<DeformableObject>(obj))
{
if (dynaObj->getDynamicalModel()->getTimeStepSizeType() == TimeSteppingType::realTime)
{
dynaObj->getDynamicalModel()->setTimeStep(timeElapsed);
}
}
}
}
this->setFPS(1. / wwt.getTimeElapsed(StopWatch::TimeUnitType::seconds));
}
} // imstk
\ No newline at end of file
......@@ -58,13 +58,28 @@ public:
///
/// \brief Destructor
///
~Scene() = default;
~Scene();
///
/// \brief Initialize the scene
///
bool initialize();
///
/// \brief Launch camera controller and other scene specific modules that need to run independently
///
void launchModules();
///
/// \brief Reset the scene
///
void reset();
///
/// \brief Advance the scene from current to next frame
///
void advance();
///
/// \brief Returns true if the object with a given name is registered, else false
///
......@@ -161,11 +176,6 @@ public:
///
bool isInitialized() const { return m_isInitialized; }
///
/// \brief Reset the scene
///
void reset();
///
/// \brief Set/Get the FPS
///
......@@ -183,6 +193,7 @@ protected:
std::shared_ptr<CollisionGraph> m_collisionGraph = std::make_shared<CollisionGraph>();
std::vector<std::shared_ptr<SolverBase>> m_solvers; ///> List of non-linear solvers
std::vector<std::shared_ptr<SceneObjectControllerBase>> m_objectControllers; ///> List of controllers
std::unordered_map<std::string, std::thread> m_threadMap; ///>
double m_fps = 0.0;
......
......@@ -27,11 +27,6 @@
namespace imstk
{
void
CameraController::initModule()
{
}
void
CameraController::runModule()
{
......
......@@ -74,7 +74,7 @@ protected:
///
/// \brief
///
virtual void initModule() override;
virtual void initModule() override {};
///
/// \brief
......
......@@ -186,7 +186,7 @@ VTKInteractorStyle::OnChar()
(key == 'q' || key == 'Q' || key == 'e' || key == 'E')) // end Simulation
{
m_fpsActor->VisibilityOff();
m_simManager->endSimulation();
//m_simManager->endSimulation();
}
else if (key == 'd' || key == 'D') // switch rendering mode
{
......
......@@ -34,111 +34,10 @@
namespace imstk
{
std::shared_ptr<Scene>
SceneManager::getScene()
{
return m_scene;
}
void
SceneManager::initModule()
{
// Start Camera Controller (asynchronous)
if (auto camController = m_scene->getCamera()->getController())
{
this->startModuleInNewThread(camController);
}
}
void
SceneManager::runModule()
{
StopWatch wwt;
wwt.start();
// Reset Contact forces to 0
for (auto obj : m_scene->getSceneObjects())
{
if (auto defObj = std::dynamic_pointer_cast<DeformableObject>(obj))
{
defObj->getContactForce().setConstant(0.0);
}
else if (auto collidingObj = std::dynamic_pointer_cast<CollidingObject>(obj))
{
collidingObj->resetForce();
}
}
// Update objects controlled by the device controllers
for (auto controller : m_scene->getSceneObjectControllers())
{
controller->updateControlledObjects();
}
// Compute collision data per interaction pair
for (auto intPair : m_scene->getCollisionGraph()->getInteractionPairList())
{
intPair->computeCollisionData();
}
// Process collision data per interaction pair
for (auto intPair : m_scene->getCollisionGraph()->getInteractionPairList())
{
intPair->processCollisionData();
}
// Run the solvers
for (auto solvers : m_scene->getSolvers())
{
solvers->solve();
}
// Apply updated forces on device
for (auto controller : m_scene->getSceneObjectControllers())
{
controller->applyForces();
}
// Apply the geometry and apply maps to all the objects
for (auto obj : m_scene->getSceneObjects())
{
obj->updateGeometries();
}
// Set the trackers of the scene object controllers to out-of-date
for (auto controller : m_scene->getSceneObjectControllers())
{
controller->setTrackerToOutOfDate();
}
auto timeElapsed = wwt.getTimeElapsed(StopWatch::TimeUnitType::seconds);
// Update time step size of the dynamic objects
for (auto obj : m_scene->getSceneObjects())
{
if (obj->getType() == SceneObject::Type::Pbd)
{
if (auto dynaObj = std::dynamic_pointer_cast<PbdObject>(obj))
{
if (dynaObj->getDynamicalModel()->getTimeStepSizeType() == TimeSteppingType::realTime)
{
dynaObj->getDynamicalModel()->setTimeStep(timeElapsed);
}
}
}
else if (obj->getType() == SceneObject::Type::FEMDeformable)
{
if (auto dynaObj = std::dynamic_pointer_cast<DeformableObject>(obj))
{
if (dynaObj->getDynamicalModel()->getTimeStepSizeType() == TimeSteppingType::realTime)
{
dynaObj->getDynamicalModel()->setTimeStep(timeElapsed);
}
}
}
}
m_scene->setFPS(1. / wwt.getTimeElapsed(StopWatch::TimeUnitType::seconds));
m_scene->launchModules();
}
void
......
......@@ -54,7 +54,7 @@ public:
///
/// \brief Get the scene that the scene manager is managing
///
std::shared_ptr<Scene> getScene();
std::shared_ptr<Scene> getScene() { return m_scene; };
protected:
///
......@@ -65,7 +65,7 @@ protected:
///
/// \brief Run the module
///
void runModule() override;
void runModule() override { m_scene->advance(); };
///
/// \brief Clean up the module
......
......@@ -28,12 +28,14 @@
namespace imstk
{
SimulationManager::SimulationManager(const bool disableRendering, const bool enableVR)
SimulationManager::SimulationManager(const SimulationManager::Mode mode, const bool enableVR)
{
m_simulationMode = mode;
// Init g3logger
m_logUtil->createLogger("simulation", "./");
if (!disableRendering)
if (mode == Mode::rendering)
{
#ifdef iMSTK_USE_Vulkan
m_viewer = std::make_shared<VulkanViewer>(this, enableVR);
......@@ -53,12 +55,6 @@ SimulationManager::SimulationManager(const bool disableRendering, const bool ena
}
}
const SimulationStatus&
SimulationManager::getStatus() const
{
return m_status;
}
void
SimulationManager::setThreadPoolSize(const int nThreads)
{
......@@ -81,12 +77,25 @@ SimulationManager::setOptimalThreadPoolSize()
bool
SimulationManager::isSceneRegistered(const std::string& sceneName) const
{
return m_sceneManagerMap.find(sceneName) != m_sceneManagerMap.end();
if (m_simulationMode != Mode::backend)
{
return m_sceneManagerMap.find(sceneName) != m_sceneManagerMap.end();
}
else
{
return m_sceneMap.find(sceneName) != m_sceneMap.end();
}
}
std::shared_ptr<SceneManager>
SimulationManager::getSceneManager(const std::string& sceneName) const
{
if (m_simulationMode == Mode::backend)
{
LOG(WARNING) << "The simulation manager is in backend mode. No scene managers are created!";
return nullptr;
}
if (!this->isSceneRegistered(sceneName))
{
LOG(WARNING) << "No scene named '" << sceneName
......@@ -110,8 +119,15 @@ SimulationManager::getSceneManager(std::shared_ptr<Scene> scene) const
std::shared_ptr<Scene>
SimulationManager::getScene(const std::string& sceneName) const
{
auto sceneManager = this->getSceneManager(sceneName);
return sceneManager ? sceneManager->getScene() : nullptr;
if (m_simulationMode != Mode::backend)
{
auto sceneManager = this->getSceneManager(sceneName);
return sceneManager ? sceneManager->getScene() : nullptr;
}
else
{
return m_sceneMap.find(sceneName) != m_sceneMap.end() ? m_sceneMap.at(sceneName) : nullptr;
}
}
std::shared_ptr<Scene>
......@@ -132,24 +148,14 @@ SimulationManager::createNewScene(const std::string& newSceneName)
}
auto newScene = std::make_shared<Scene>(newSceneName);
m_sceneManagerMap[newScene->getName()] = std::make_shared<SceneManager>(newScene);
LOG(INFO) << "New scene added: " << newScene->getName();
return newScene;
}
std::shared_ptr<Scene>
SimulationManager::createNewScene(std::string&& newSceneName)
{
if (this->isSceneRegistered(newSceneName))
if (m_simulationMode != Mode::backend)
{
LOG(WARNING) << "Can not create new scene: '" << newSceneName
<< "' is already registered in this simulation\n"
<< "You can create a new scene using an unique name";
return nullptr;
m_sceneManagerMap[newScene->getName()] = std::make_shared<SceneManager>(newScene);
}
else
{
m_sceneMap[newScene->getName()] = newScene;
}
auto newScene = std::make_shared<Scene>(std::move(newSceneName));
m_sceneManagerMap[newScene->getName()] = std::make_shared<SceneManager>(newScene);
LOG(INFO) << "New scene added: " << newScene->getName();
return newScene;
}
......@@ -157,7 +163,15 @@ SimulationManager::createNewScene(std::string&& newSceneName)
std::shared_ptr<Scene>
SimulationManager::createNewScene()
{
int id = (int)m_sceneManagerMap.size() + 1;
int id = 0;
if (m_simulationMode != Mode::backend)
{
id = (int)m_sceneMap.size() + 1;
}
else
{
id = (int)m_sceneManagerMap.size() + 1;
}
std::string newSceneName = "Scene_" + std::to_string(id);
return this->createNewScene(newSceneName);
......@@ -175,8 +189,15 @@ SimulationManager::addScene(std::shared_ptr<Scene> newScene)
<< "Set this scene name to a unique name first";
return;
}
if (m_simulationMode != Mode::backend)
{
m_sceneManagerMap[newSceneName] = std::make_shared<SceneManager>(newScene);
}
else
{
m_sceneMap[newSceneName] = newScene;
}
m_sceneManagerMap[newSceneName] = std::make_shared<SceneManager>(newScene);
LOG(INFO) << "Scene added: " << newSceneName;
}
......@@ -190,6 +211,15 @@ SimulationManager::removeScene(const std::string& sceneName)
return;
}
if (m_simulationMode != Mode::backend)
{
m_sceneManagerMap.erase(sceneName);
}
else
{
m_sceneMap.erase(sceneName);
}
m_sceneManagerMap.erase(sceneName);