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

REFAC: Move the scene simulation logic to scene class

a. Move the scene simulation logic to scene class.
b. Simulation manager calles scene::advance() in the run module
c. Add launchModules() to scene meant to launch scene-specific modules in a separate thread
parent 4ece7524
......@@ -23,11 +23,26 @@
#include "imstkCameraController.h"
#include "imstkSceneObjectControllerBase.h"
#include "imstkDebugGeometry.h"
#include "imstkPbdObject.h"
#include "imstkDeformableObject.h"
#include "imstkVirtualCouplingPBDObject.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 +59,25 @@ 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(); });
}
// Init virtual coupling objects offsets
for (auto obj : this->getSceneObjects())
{
if (auto virtualCouplingPBD = std::dynamic_pointer_cast<VirtualCouplingPBDObject>(obj))
{
virtualCouplingPBD->initOffsets();
}
}
}
bool
Scene::isObjectRegistered(const std::string& sceneObjectName) const
{
......@@ -273,4 +307,127 @@ 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();
}
// todo: refactor pbd
// description: so that the transform obtained from device can be applied
if (auto virtualCouplingPBD = std::dynamic_pointer_cast<VirtualCouplingPBDObject>(obj))
{
virtualCouplingPBD->resetCollidingGeometry();
}
}
// 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();
intPair->computeContactForces();
}
// Apply forces on device
for (auto controller : this->getSceneObjectControllers())
{
controller->applyForces();
}
// Update the solvers
for (auto solvers : this->getSolvers())
{
solvers->solve();
auto xx = std::dynamic_pointer_cast<PbdSolver>(solvers);
}
// Apply the geometry and apply maps to all the objects
for (auto obj : this->getSceneObjects())
{
obj->updateGeometries();
}
// Do collision detection and response for pbd objects
for (auto intPair : this->getCollisionGraph()->getPbdPairList())
{
intPair->resetConstraints();
if (intPair->doBroadPhaseCollision())
{
intPair->doNarrowPhaseCollision();
}
intPair->resolveCollision();
}
// Update velocity of PBD objects
for (auto obj : this->getSceneObjects())
{
if (auto pbdObj = std::dynamic_pointer_cast<PbdObject>(obj))
{
pbdObj->updateVelocity();
}
}
// Set the trackers of virtual coupling PBD objects to out-of-date
for (auto obj : this->getSceneObjects())
{
if (auto virtualCouplingPBD = std::dynamic_pointer_cast<VirtualCouplingPBDObject>(obj))
{
virtualCouplingPBD->setTrackerToOutOfDate();
}
}
// 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((size_t)(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;
......
......@@ -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
......
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